Skip to content
Snippets Groups Projects
Commit 77d95395 authored by q3k's avatar q3k
Browse files

flow3r_bsp/i2c: expose scan to micropython

parent e52b5925
No related branches found
No related tags found
No related merge requests found
Pipeline #5728 passed
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
#include "freertos/semphr.h" #include "freertos/semphr.h"
#include "esp_log.h" #include "esp_log.h"
#include <string.h>
static SemaphoreHandle_t mutex; static SemaphoreHandle_t mutex;
static const char *TAG = "flow3r-bsp-i2c"; static const char *TAG = "flow3r-bsp-i2c";
...@@ -74,7 +76,7 @@ void flow3r_bsp_i2c_init(void) { ...@@ -74,7 +76,7 @@ void flow3r_bsp_i2c_init(void) {
assert(i2c_param_config(I2C_NUM_0, &i2c_conf) == ESP_OK); assert(i2c_param_config(I2C_NUM_0, &i2c_conf) == ESP_OK);
assert(i2c_driver_install(I2C_NUM_0, i2c_conf.mode, 0, 0, ESP_INTR_FLAG_LOWMED | ESP_INTR_FLAG_SHARED) == ESP_OK); assert(i2c_driver_install(I2C_NUM_0, i2c_conf.mode, 0, 0, ESP_INTR_FLAG_LOWMED | ESP_INTR_FLAG_SHARED) == ESP_OK);
flow3r_bsp_i2c_scan(); flow3r_bsp_i2c_scan(NULL);
} }
// Take I2C bus lock. // Take I2C bus lock.
...@@ -101,7 +103,10 @@ esp_err_t flow3r_bsp_i2c_write_read_device(uint8_t address, const uint8_t *write ...@@ -101,7 +103,10 @@ esp_err_t flow3r_bsp_i2c_write_read_device(uint8_t address, const uint8_t *write
return res; return res;
} }
void flow3r_bsp_i2c_scan(void) { void flow3r_bsp_i2c_scan(flow3r_bsp_i2c_scan_result_t *res) {
if (res != NULL) {
memset(res, 0, sizeof(flow3r_bsp_i2c_scan_result_t));
}
ESP_LOGI(TAG, "Scan: starting..."); ESP_LOGI(TAG, "Scan: starting...");
for (uint8_t i = 1; i < 127; i++) { for (uint8_t i = 1; i < 127; i++) {
i2c_cmd_handle_t cmd = i2c_cmd_link_create(); i2c_cmd_handle_t cmd = i2c_cmd_link_create();
...@@ -112,6 +117,11 @@ void flow3r_bsp_i2c_scan(void) { ...@@ -112,6 +117,11 @@ void flow3r_bsp_i2c_scan(void) {
i2c_cmd_link_delete(cmd); i2c_cmd_link_delete(cmd);
if (ret == ESP_OK) { if (ret == ESP_OK) {
ESP_LOGI(TAG, "Scan: detected %02x", i); ESP_LOGI(TAG, "Scan: detected %02x", i);
if (res != NULL) {
size_t ix = i / 32;
size_t offs = i % 32;
res->res[ix] |= (1 << offs);
}
} }
} }
ESP_LOGI(TAG, "Scan: done."); ESP_LOGI(TAG, "Scan: done.");
......
...@@ -37,4 +37,8 @@ esp_err_t flow3r_bsp_i2c_write_to_device(flow3r_i2c_address address, const uint8 ...@@ -37,4 +37,8 @@ esp_err_t flow3r_bsp_i2c_write_to_device(flow3r_i2c_address address, const uint8
// This can be called concurrently from different tassks. // This can be called concurrently from different tassks.
esp_err_t flow3r_bsp_i2c_write_read_device(flow3r_i2c_address address, const uint8_t *write_buffer, size_t write_size, uint8_t *read_buffer, size_t read_size, TickType_t ticks_to_wait); esp_err_t flow3r_bsp_i2c_write_read_device(flow3r_i2c_address address, const uint8_t *write_buffer, size_t write_size, uint8_t *read_buffer, size_t read_size, TickType_t ticks_to_wait);
void flow3r_bsp_i2c_scan(void); typedef struct {
\ No newline at end of file uint32_t res[4]; // Bitfield of addresses from 0 to 127.
} flow3r_bsp_i2c_scan_result_t;
void flow3r_bsp_i2c_scan(flow3r_bsp_i2c_scan_result_t *res);
\ No newline at end of file
...@@ -197,6 +197,23 @@ STATIC mp_obj_t mp_scope_draw(mp_obj_t ctx_in) { ...@@ -197,6 +197,23 @@ STATIC mp_obj_t mp_scope_draw(mp_obj_t ctx_in) {
} }
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_scope_draw_obj, mp_scope_draw); STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_scope_draw_obj, mp_scope_draw);
STATIC mp_obj_t mp_i2c_scan(void) {
flow3r_bsp_i2c_scan_result_t scan;
flow3r_bsp_i2c_scan(&scan);
mp_obj_t res = mp_obj_new_list(0, NULL);
for (int i = 0; i < 127; i++) {
size_t ix = i / 32;
size_t offs = i % 32;
if (scan.res[ix] & (1 << offs)) {
mp_obj_list_append(res, mp_obj_new_int_from_uint(i));
}
}
return res;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_i2c_scan_obj, mp_i2c_scan);
STATIC const mp_rom_map_elem_t mp_module_hardware_globals_table[] = { STATIC const mp_rom_map_elem_t mp_module_hardware_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_hardware) }, { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_hardware) },
...@@ -224,6 +241,7 @@ STATIC const mp_rom_map_elem_t mp_module_hardware_globals_table[] = { ...@@ -224,6 +241,7 @@ STATIC const mp_rom_map_elem_t mp_module_hardware_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR_display_set_backlight), MP_ROM_PTR(&mp_display_set_backlight_obj) }, { MP_ROM_QSTR(MP_QSTR_display_set_backlight), MP_ROM_PTR(&mp_display_set_backlight_obj) },
{ MP_ROM_QSTR(MP_QSTR_version), MP_ROM_PTR(&mp_version_obj) }, { MP_ROM_QSTR(MP_QSTR_version), MP_ROM_PTR(&mp_version_obj) },
{ MP_ROM_QSTR(MP_QSTR_get_ctx), MP_ROM_PTR(&mp_get_ctx_obj) }, { MP_ROM_QSTR(MP_QSTR_get_ctx), MP_ROM_PTR(&mp_get_ctx_obj) },
{ MP_ROM_QSTR(MP_QSTR_i2c_scan), MP_ROM_PTR(&mp_i2c_scan_obj) },
{ MP_ROM_QSTR(MP_QSTR_BUTTON_PRESSED_LEFT), MP_ROM_INT(st3m_tripos_left) }, { MP_ROM_QSTR(MP_QSTR_BUTTON_PRESSED_LEFT), MP_ROM_INT(st3m_tripos_left) },
{ MP_ROM_QSTR(MP_QSTR_BUTTON_PRESSED_RIGHT), MP_ROM_INT(st3m_tripos_right) }, { MP_ROM_QSTR(MP_QSTR_BUTTON_PRESSED_RIGHT), MP_ROM_INT(st3m_tripos_right) },
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment