Skip to content
Snippets Groups Projects
Commit 07e35a63 authored by Daniel Busch's avatar Daniel Busch
Browse files

Implement change suggestions and fix a bug concerning the last bit sent

parent 6fd65c1c
Branches
No related tags found
No related merge requests found
Pipeline #3704 failed
......@@ -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,
......
#ifndef GPIO_COMMON_H
#define GPIO_COMMON_H
#include "gpio.h"
extern gpio_cfg_t gpio_configs[];
#endif
......@@ -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 */
......@@ -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);
}
#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_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);
}
}
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();
}
......@@ -8,7 +8,6 @@ python_modules = files(
'pride.py',
'ledfx.py',
'simple_menu.py',
'ws2812.py',
# MicroPython Standard-Library
'col_defaultdict.py',
......
......@@ -173,4 +173,4 @@ Q(MAX30001_ECG)
/* ws2812 */
Q(ws2812)
Q(write)
Q(set_all)
......@@ -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 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);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment