MMC56x3 mag+set/reset

This commit is contained in:
leo 2022-12-20 12:00:27 +01:00
parent ed23d0e97f
commit 65da56d15a
Signed by: leo
GPG Key ID: 0DD993BFB2B307DB
3 changed files with 53 additions and 4 deletions

View File

@ -9,9 +9,15 @@
#define TEMPERATURE 0x09 #define TEMPERATURE 0x09
#define IC0 0x1B #define IC0 0x1B
#define STATUS1 0x18 #define STATUS1 0x18
#define XOUT0 0x00
#define XOUT2 0x06
#define TAKE_MEAS_T 1 #define TAKE_MEAS_T 1
#define MEAS_T_DONE 7 #define MEAS_T_DONE 7
#define TAKE_MEAS_M 0
#define MEAS_M_DONE 6
#define DO_SET 3
#define DO_RESET 4
esp_err_t MMC56x3_get_product_ID(uint8_t* id){ esp_err_t MMC56x3_get_product_ID(uint8_t* id){
ESP_ERROR_CHECK(i2c_read_reg(PRODUCT_ID_1, id, 1)); ESP_ERROR_CHECK(i2c_read_reg(PRODUCT_ID_1, id, 1));
@ -19,7 +25,7 @@ esp_err_t MMC56x3_get_product_ID(uint8_t* id){
} }
esp_err_t MMC56x3_get_temperature(uint8_t* temp){ esp_err_t MMC56x3_get_temperature(uint8_t* temp){
uint8_t startMeas = 1<<TAKE_MEAS_T; uint8_t startMeas = 1 << TAKE_MEAS_T;
ESP_ERROR_CHECK(i2c_write_reg(IC0, &startMeas, 1)); ESP_ERROR_CHECK(i2c_write_reg(IC0, &startMeas, 1));
for(int i=0; i<5; i++){ // wait for meas_t_done for(int i=0; i<5; i++){ // wait for meas_t_done
uint8_t status1_content; uint8_t status1_content;
@ -31,3 +37,34 @@ esp_err_t MMC56x3_get_temperature(uint8_t* temp){
ESP_ERROR_CHECK(i2c_read_reg(TEMPERATURE, temp, 1)); ESP_ERROR_CHECK(i2c_read_reg(TEMPERATURE, temp, 1));
return ESP_OK; return ESP_OK;
} }
esp_err_t MMC56x3_get_mag_field(int32_t* x, int32_t* y, int32_t* z){
uint8_t startMeas = 1 << TAKE_MEAS_M;
ESP_ERROR_CHECK(i2c_write_reg(IC0, &startMeas, 1));
for(int i=0; i<5; i++){ // wait for meas_m_done
i2c_hal_wait_ms(10);
uint8_t status1_content;
ESP_ERROR_CHECK(i2c_read_reg(STATUS1, &status1_content, 1));
if(status1_content & (1 << MEAS_M_DONE)) break;
}
uint8_t buff[3*3];
ESP_ERROR_CHECK(i2c_read_reg(XOUT0, buff, sizeof(buff)));
*x=(buff[0] << 12) | (buff[1] << 4) | (buff[6] >> 4);
*y=(buff[2] << 12) | (buff[3] << 4) | (buff[7] >> 4);
*z=(buff[4] << 12) | (buff[5] << 4) | (buff[8] >> 4);
*x-=1 << 19;
*y-=1 << 19;
*z-=1 << 19;
return ESP_OK;
}
esp_err_t MMC56x3_set(){
uint8_t set = 1 << DO_SET;
ESP_ERROR_CHECK(i2c_write_reg(IC0, &set, 1));
return ESP_OK;
}
esp_err_t MMC56x3_reset(){
uint8_t set = 1 << DO_RESET;
ESP_ERROR_CHECK(i2c_write_reg(IC0, &set, 1));
return ESP_OK;
}

View File

@ -1,3 +1,10 @@
#include "esp_err.h" #include "esp_err.h"
#include <stdint.h>
esp_err_t MMC56x3_get_product_ID(uint8_t* id); esp_err_t MMC56x3_get_product_ID(uint8_t* id);
esp_err_t MMC56x3_get_temperature(uint8_t* temp); esp_err_t MMC56x3_get_temperature(uint8_t* temp);
esp_err_t MMC56x3_get_mag_field(int32_t* x, int32_t* y, int32_t* z);
esp_err_t MMC56x3_set();
esp_err_t MMC56x3_reset();

View File

@ -45,7 +45,7 @@ void app_main(void){
.min_freq_mhz = 40, .min_freq_mhz = 40,
.light_sleep_enable = true, // enable light sleep .light_sleep_enable = true, // enable light sleep
}; };
//ESP_ERROR_CHECK(esp_pm_configure(&pm_config)); ESP_ERROR_CHECK(esp_pm_configure(&pm_config));
configuration_data_t* conf = malloc(sizeof(configuration_data_t)); configuration_data_t* conf = malloc(sizeof(configuration_data_t));
@ -62,6 +62,8 @@ void app_main(void){
init_battery_level_adc(conf->battery_conf); init_battery_level_adc(conf->battery_conf);
MMC56x3_set();
MMC56x3_reset();
uint8_t id=0; uint8_t id=0;
MMC56x3_get_product_ID(&id); MMC56x3_get_product_ID(&id);
uint8_t temp; uint8_t temp;
@ -87,6 +89,9 @@ void app_main(void){
} }
while(1){ while(1){
/*int32_t x,y,z;
MMC56x3_get_mag_field(&x, &y, &z);
ESP_LOGI("MAIN", "BField x %ld, y %ld, z %ld", x, y, z);*/
vTaskDelay(1000 / portTICK_PERIOD_MS); vTaskDelay(1000 / portTICK_PERIOD_MS);
} }
} }