diff --git a/epicardium/modules/gpio.c b/epicardium/modules/gpio.c index 58bf4e29e735a669308f5362903852a9b3bdb9ca..a923812f5f49d90604bdce150a29d01821dc9ff6 100644 --- a/epicardium/modules/gpio.c +++ b/epicardium/modules/gpio.c @@ -11,7 +11,7 @@ * Despite what the schematic (currently, 2019-08-18) says these are the correct * pins for wristband GPIO 1-4 (not 0-3 as the schematic states) */ -extern gpio_cfg_t gpio_configs[] = { +gpio_cfg_t gpio_configs[] = { [EPIC_GPIO_WRISTBAND_1] = { PORT_0, PIN_21, GPIO_FUNC_OUT, diff --git a/epicardium/modules/gpio_common.h b/epicardium/modules/gpio_common.h deleted file mode 100644 index faadc06b39ac6713e6453944a47d0c1a026d7edb..0000000000000000000000000000000000000000 --- a/epicardium/modules/gpio_common.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef GPIO_COMMON_H -#define GPIO_COMMON_H - -#include "gpio.h" - -extern gpio_cfg_t gpio_configs[]; - -#endif diff --git a/epicardium/modules/modules.h b/epicardium/modules/modules.h index 2d51f786373cea0bb108212b225df7badf0bdc25..3f11766af61a86ebff2ca0290233342e5bd4e9b7 100644 --- a/epicardium/modules/modules.h +++ b/epicardium/modules/modules.h @@ -3,6 +3,7 @@ #include "FreeRTOS.h" #include "semphr.h" +#include "gpio.h" #include <stdint.h> #include <stdbool.h> @@ -105,4 +106,8 @@ void vBhi160Task(void *pvParameters); void vMAX30001Task(void *pvParameters); void max30001_mutex_init(void); +/* ---------- GPIO --------------------------------------------------------- */ +#define MAX30001_MUTEX_WAIT_MS 50 +extern gpio_cfg_t gpio_configs[]; + #endif /* MODULES_H */ diff --git a/epicardium/modules/ws2812.c b/epicardium/modules/ws2812.c index fc41d2a9876172f3cf9d1dbf5f9689a0288f8e84..8d74559b4dafc24b9dac1c5dffa4743d7cb008f4 100644 --- a/epicardium/modules/ws2812.c +++ b/epicardium/modules/ws2812.c @@ -6,7 +6,6 @@ #include "max32665.h" #include "gpio.h" #include "modules.h" -#include "modules/gpio_common.h" #include <stdbool.h> @@ -18,49 +17,56 @@ #define EPIC_WS2812_ONE_LOW_TICKS 34 - OVERHEAD #define EPIC_WS2812_RESET_TCKS 4800 - OVERHEAD -#define EPIC_WS2812_DELAY_TICKS(ticks) \ - counter = ticks; \ - while (--counter) \ - ; +static volatile uint32_t counter = 0; -#define EPIC_WS2812_TRANSMIT_BIT(pin, bit) \ - if ((bit) != 0) { \ - GPIO_OutSet(pin); \ - EPIC_WS2812_DELAY_TICKS(EPIC_WS2812_ONE_HIGH_TICKS); \ - GPIO_OutClr(pin); \ - EPIC_WS2812_DELAY_TICKS(EPIC_WS2812_ONE_LOW_TICKS); \ - } else { \ - GPIO_OutSet(pin); \ - EPIC_WS2812_DELAY_TICKS(EPIC_WS2812_ZERO_HIGH_TICKS); \ - GPIO_OutClr(pin); \ - EPIC_WS2812_DELAY_TICKS(EPIC_WS2812_ZERO_LOW_TICKS); \ +static inline __attribute__((always_inline)) void epic_ws2812_delay_ticks(uint32_t ticks) +{ + counter = ticks; + while (--counter); +} + +static inline __attribute__((always_inline)) void epic_ws2812_transmit_bit(uint32_t pin, uint8_t bit) +{ + if ((bit) != 0) { + GPIO_OutSet(pin); + epic_ws2812_delay_ticks(EPIC_WS2812_ONE_HIGH_TICKS); + GPIO_OutClr(pin); + epic_ws2812_delay_ticks(EPIC_WS2812_ONE_LOW_TICKS); + } else { + GPIO_OutSet(pin); + epic_ws2812_delay_ticks(EPIC_WS2812_ZERO_HIGH_TICKS); + GPIO_OutClr(pin); + epic_ws2812_delay_ticks(EPIC_WS2812_ZERO_LOW_TICKS); } +} -#define EPIC_WS2812_TRANSMIT_BYTE(pin, byte) \ - EPIC_WS2812_TRANSMIT_BIT(pin, byte & 0b10000000); \ - EPIC_WS2812_TRANSMIT_BIT(pin, byte & 0b01000000); \ - EPIC_WS2812_TRANSMIT_BIT(pin, byte & 0b00100000); \ - EPIC_WS2812_TRANSMIT_BIT(pin, byte & 0b00010000); \ - EPIC_WS2812_TRANSMIT_BIT(pin, byte & 0b00001000); \ - EPIC_WS2812_TRANSMIT_BIT(pin, byte & 0b00000100); \ - EPIC_WS2812_TRANSMIT_BIT(pin, byte & 0b00000010); \ - EPIC_WS2812_TRANSMIT_BIT(pin, byte & 0b00000001); +static inline __attribute__((always_inline)) void epic_ws2812_transmit_byte(uint32_t pin, uint8_t byte) +{ + epic_ws2812_transmit_bit(pin, byte & 0b10000000); + epic_ws2812_transmit_bit(pin, byte & 0b01000000); + epic_ws2812_transmit_bit(pin, byte & 0b00100000); + epic_ws2812_transmit_bit(pin, byte & 0b00010000); + epic_ws2812_transmit_bit(pin, byte & 0b00001000); + epic_ws2812_transmit_bit(pin, byte & 0b00000100); + epic_ws2812_transmit_bit(pin, byte & 0b00000010); + epic_ws2812_transmit_bit(pin, byte & 0b00000001); +} void epic_ws2812_write(uint8_t pin, uint8_t *pixels, uint32_t n_bytes) { uint8_t *pixels_end = pixels + n_bytes; gpio_cfg_t *pin_cfg = &gpio_configs[pin]; - volatile uint32_t counter; - __disable_irq(); + taskENTER_CRITICAL(); epic_gpio_set_pin_mode(pin, EPIC_GPIO_MODE_OUT); do { - EPIC_WS2812_TRANSMIT_BYTE(pin_cfg, *pixels); + epic_ws2812_transmit_byte(pin_cfg, *pixels); } while (++pixels != pixels_end); - EPIC_WS2812_DELAY_TICKS(EPIC_WS2812_RESET_TCKS); + GPIO_OutClr(pin); + epic_ws2812_delay_ticks(EPIC_WS2812_RESET_TCKS); - __enable_irq(); + taskEXIT_CRITICAL(); } diff --git a/pycardium/modules/py/meson.build b/pycardium/modules/py/meson.build index 256d214c22559d754a251c3eba4083b49dfd716c..75bf937c7b2d394d0834542e333bb050328c0823 100644 --- a/pycardium/modules/py/meson.build +++ b/pycardium/modules/py/meson.build @@ -8,7 +8,6 @@ python_modules = files( 'pride.py', 'ledfx.py', 'simple_menu.py', - 'ws2812.py', # MicroPython Standard-Library 'col_defaultdict.py', diff --git a/pycardium/modules/py/ws2812.py b/pycardium/modules/py/ws2812.py deleted file mode 100644 index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000 diff --git a/pycardium/modules/qstrdefs.h b/pycardium/modules/qstrdefs.h index 648a1726df71f33a834812e3301fa59d9d9b8a9e..50178cc8e578158a6362add774f04d67c5e50715 100644 --- a/pycardium/modules/qstrdefs.h +++ b/pycardium/modules/qstrdefs.h @@ -173,4 +173,4 @@ Q(MAX30001_ECG) /* ws2812 */ Q(ws2812) -Q(write) +Q(set_all) diff --git a/pycardium/modules/ws2812.c b/pycardium/modules/ws2812.c index ce8ad6014ca00b9c05a3292a9a0fef5b4331eee9..e4228aee22a9de4e83d5552a8c47da92cbfb6ff7 100644 --- a/pycardium/modules/ws2812.c +++ b/pycardium/modules/ws2812.c @@ -5,19 +5,28 @@ #include <stdlib.h> #include <stdio.h> -/* Define the pixel write function in this module */ -static mp_obj_t mp_ws2812_write(mp_obj_t pin, mp_obj_t pixels) +/* Define the pixel set_all function in this module */ +static mp_obj_t mp_ws2812_set_all(mp_obj_t pin, mp_obj_t color_in) { - mp_int_t pin_int = mp_obj_get_int(pin); - mp_int_t pixels_len = mp_obj_get_int(mp_obj_len(pixels)); + mp_int_t pin_int = mp_obj_get_int(pin); + mp_int_t len = mp_obj_get_int(mp_obj_len(color_in)); + mp_int_t pixels_len = len * 3; uint8_t *pixels_arr = alloca(pixels_len * sizeof(uint8_t)); - for (int i = 0; i < pixels_len; i++) { - mp_obj_t elem = mp_obj_subscr( - pixels, MP_OBJ_NEW_SMALL_INT(i), MP_OBJ_SENTINEL + for (int i = 0; i < len; i++) { + mp_obj_t color = mp_obj_subscr( + color_in, mp_obj_new_int(i), MP_OBJ_SENTINEL ); - pixels_arr[i] = mp_obj_get_int(elem); + pixels_arr[i * 3] = mp_obj_get_int(mp_obj_subscr( + color, mp_obj_new_int(1), MP_OBJ_SENTINEL) + ); + pixels_arr[i * 3 + 1] = mp_obj_get_int(mp_obj_subscr( + color, mp_obj_new_int(0), MP_OBJ_SENTINEL) + ); + pixels_arr[i * 3 + 2] = mp_obj_get_int(mp_obj_subscr( + color, mp_obj_new_int(2), MP_OBJ_SENTINEL) + ); } /* call epicardium to be fast enough */ @@ -25,12 +34,12 @@ static mp_obj_t mp_ws2812_write(mp_obj_t pin, mp_obj_t pixels) return mp_const_none; } -static MP_DEFINE_CONST_FUN_OBJ_2(ws2812_write_obj, mp_ws2812_write); +static MP_DEFINE_CONST_FUN_OBJ_2(ws2812_set_all_obj, mp_ws2812_set_all); /* The globals table for this module */ static const mp_rom_map_elem_t ws2812_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_ws2812) }, - { MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&ws2812_write_obj) }, + { MP_ROM_QSTR(MP_QSTR_set_all), MP_ROM_PTR(&ws2812_set_all_obj) }, }; static MP_DEFINE_CONST_DICT(ws2812_module_globals, ws2812_module_globals_table);