diff --git a/main/i2c.c b/main/i2c.c index 520543b..6f8e010 100644 --- a/main/i2c.c +++ b/main/i2c.c @@ -7,6 +7,7 @@ #include "esp_err.h" #include "esp_log.h" #include "freertos/idf_additions.h" +#include "hal/gpio_types.h" #include "ota_update.h" #include #include @@ -17,7 +18,18 @@ static i2c_master_bus_handle_t bus_handle; static i2c_master_dev_handle_t bma456_dev_handle; static struct bma4_dev bma456_struct; +volatile uint8_t interrupt_status = 0; +uint8_t int_line; +struct bma4_int_pin_config pin_config = {0}; +uint16_t int_status = 0; + #define BMA4_READ_WRITE_LEN UINT8_C(46) +#define BMA456W_INT_PIN 7 + +static void interrupt_callback(void *) { + interrupt_status = 1; + // ESP_LOGI("INTERRUPT", "STEP DETECTED"); +} /******************************************************************************/ /*! User interface functions */ @@ -123,7 +135,14 @@ void read_sensor_task(void *params) { ret = bma4_read_accel_xyz(&sens_data, &bma456_struct); bma4_error_codes_print_result("bma4_read_accel_xyz", ret); - ESP_LOGI("i2c", "X:%d, Y%d, Z%d", sens_data.x, sens_data.y, sens_data.z); + if (interrupt_status) { + ESP_LOGI("INTERRUPT", "Da war der Interrupt resetting"); + interrupt_status = 0; + ret = bma456w_read_int_status(&int_status, &bma456_struct); + bma4_error_codes_print_result("bma456w_step_counter_output status", ret); + } + + // ESP_LOGI("i2c", "X:%d, Y%d, Z%d", sens_data.x, sens_data.y, sens_data.z); vTaskDelay(pdMS_TO_TICKS(100)); } } @@ -172,6 +191,45 @@ void init_bma456() { ret = bma4_set_accel_enable(BMA4_ENABLE, &bma456_struct); bma4_error_codes_print_result("bma4_set_accel_enable status", ret); + ret = bma456w_feature_enable(BMA456W_STEP_CNTR, BMA4_ENABLE, &bma456_struct); + bma4_error_codes_print_result("bma456w_feature_enable status", ret); + + /* Setting watermark level 1, the output step resolution is 20 steps. + * Eg: 1 means, 1 * 20 = 20. Every 20 steps once output triggers + */ + ret = bma456w_step_counter_set_watermark(1, &bma456_struct); + bma4_error_codes_print_result("bma456w_step_counter_set_watermark status", + ret); + + /* Hardware interrupt configuration */ + int_line = BMA4_INTR2_MAP; + + ret = bma4_get_int_pin_config(&pin_config, int_line, &bma456_struct); + bma4_error_codes_print_result("bma4_get_int_pin_config status", ret); + + ret = bma456w_map_interrupt(int_line, BMA456W_STEP_CNTR_INT, BMA4_ENABLE, + &bma456_struct); + bma4_error_codes_print_result("bma456w_map_interrupt status", ret); + + pin_config.edge_ctrl = BMA4_EDGE_TRIGGER; + pin_config.output_en = BMA4_OUTPUT_ENABLE; + pin_config.lvl = BMA4_ACTIVE_HIGH; + pin_config.od = BMA4_PUSH_PULL; + pin_config.input_en = BMA4_INPUT_DISABLE; + + ret = bma4_set_int_pin_config(&pin_config, int_line, &bma456_struct); + bma4_error_codes_print_result("bma4_set_int_pin_config status", ret); + + gpio_reset_pin(BMA456W_INT_PIN); + gpio_set_direction(BMA456W_INT_PIN, GPIO_MODE_INPUT); + gpio_set_pull_mode(BMA456W_INT_PIN, GPIO_PULLDOWN_ONLY); + gpio_set_intr_type(BMA456W_INT_PIN, GPIO_INTR_POSEDGE); + gpio_intr_enable(BMA456W_INT_PIN); + + gpio_install_isr_service(0); + gpio_isr_handler_add(BMA456W_INT_PIN, interrupt_callback, + (void *)BMA456W_INT_PIN); + xTaskCreate(read_sensor_task, "READ_SENSOR", 4096, NULL, 1, NULL); }