1
0
forked from me/IronOS

Removing timer as wont work & replace with bit bang

Cant keep up with irq
This commit is contained in:
Ben V. Brown
2021-05-03 21:52:18 +10:00
parent dd5714fa17
commit 5ea2908fa2
8 changed files with 1239 additions and 1292 deletions

View File

@@ -5,178 +5,106 @@
* Author: Ralim
*/
#include "FreeRTOS.h"
#include "task.h"
#include <WS2812.h>
#include "Pins.h"
#include <string.h>
uint8_t WS2812::leds_colors[WS2812_LED_CHANNEL_COUNT * WS2812_LED_COUNT];
volatile uint16_t WS2812::tmp_led_data[2 * WS2812_RAW_BYTES_PER_LED];
volatile uint8_t WS2812::is_reset_pulse; /*!< Status if we are sending reset pulse or led data */
volatile uint8_t WS2812::is_updating; /*!< Is updating in progress? */
volatile uint32_t WS2812::current_led; /*!< Current LED number we are sending */
uint8_t WS2812::leds_colors[WS2812_LED_CHANNEL_COUNT * WS2812_LED_COUNT];
void WS2812::init(void) {
memset(leds_colors, 0, sizeof(leds_colors));
hdma_tim1_ch1.XferHalfCpltCallback = DMAHalfComplete;
hdma_tim1_ch1.XferCpltCallback = DMAComplete;
htim1.Instance->CCR1 = htim1.Instance->ARR / 2 - 1;
htim1.Instance->DIER |= TIM_DIER_CC1DE;
memset(leds_colors, 0, sizeof(leds_colors));
}
uint8_t WS2812::led_update(uint8_t block) {
if (is_updating) { /* Check if update in progress already */
return 0;
}
is_updating = 1; /* We are now updating */
void WS2812::led_update() {
__disable_irq();
//Bitbang it out as our cpu irq latency is too high
for (unsigned int i = 0; i < sizeof(leds_colors); i++) {
//Shove out MSB first
for (int x = 0; x < 8; x++) {
WS2812_GPIO_Port->BSRR = WS2812_Pin;
if ((leds_colors[i] & (1 << (7 - x))) == (1 << (7 - x))) {
__asm__ __volatile__("nop");
__asm__ __volatile__("nop");
__asm__ __volatile__("nop");
__asm__ __volatile__("nop");
__asm__ __volatile__("nop");
__asm__ __volatile__("nop");
__asm__ __volatile__("nop");
__asm__ __volatile__("nop");
__asm__ __volatile__("nop");
__asm__ __volatile__("nop");
__asm__ __volatile__("nop");
__asm__ __volatile__("nop");
__asm__ __volatile__("nop");
__asm__ __volatile__("nop");
__asm__ __volatile__("nop");
__asm__ __volatile__("nop");
__asm__ __volatile__("nop");
__asm__ __volatile__("nop");
__asm__ __volatile__("nop");
__asm__ __volatile__("nop");
__asm__ __volatile__("nop");
__asm__ __volatile__("nop");
__asm__ __volatile__("nop");
__asm__ __volatile__("nop");
__asm__ __volatile__("nop");
__asm__ __volatile__("nop");
__asm__ __volatile__("nop");
__asm__ __volatile__("nop");
__asm__ __volatile__("nop");
__asm__ __volatile__("nop");
} else {
led_start_reset_pulse(1); /* Start reset pulse */
if (block) {
while (!led_is_update_finished()) {
vTaskDelay(1);
}; /* Wait to finish */
}
return 1;
__asm__ __volatile__("nop");
__asm__ __volatile__("nop");
__asm__ __volatile__("nop");
__asm__ __volatile__("nop");
__asm__ __volatile__("nop");
__asm__ __volatile__("nop");
}
WS2812_GPIO_Port->BSRR = (uint32_t) WS2812_Pin << 16u;
__asm__ __volatile__("nop");
__asm__ __volatile__("nop");
__asm__ __volatile__("nop");
__asm__ __volatile__("nop");
__asm__ __volatile__("nop");
__asm__ __volatile__("nop");
__asm__ __volatile__("nop");
__asm__ __volatile__("nop");
__asm__ __volatile__("nop");
__asm__ __volatile__("nop");
__asm__ __volatile__("nop");
__asm__ __volatile__("nop");
__asm__ __volatile__("nop");
__asm__ __volatile__("nop");
__asm__ __volatile__("nop");
__asm__ __volatile__("nop");
__asm__ __volatile__("nop");
__asm__ __volatile__("nop");
__asm__ __volatile__("nop");
__asm__ __volatile__("nop");
__asm__ __volatile__("nop");
__asm__ __volatile__("nop");
__asm__ __volatile__("nop");
__asm__ __volatile__("nop");
__asm__ __volatile__("nop");
__asm__ __volatile__("nop");
__asm__ __volatile__("nop");
}
}
__enable_irq();
}
void WS2812::led_set_color(size_t index, uint8_t r, uint8_t g, uint8_t b) {
leds_colors[index * WS2812_LED_CHANNEL_COUNT + 0] = r;
leds_colors[index * WS2812_LED_CHANNEL_COUNT + 1] = g;
leds_colors[index * WS2812_LED_CHANNEL_COUNT + 2] = b;
leds_colors[index * WS2812_LED_CHANNEL_COUNT + 0] = g;
leds_colors[index * WS2812_LED_CHANNEL_COUNT + 1] = r;
leds_colors[index * WS2812_LED_CHANNEL_COUNT + 2] = b;
}
void WS2812::led_set_color_all(uint8_t r, uint8_t g, uint8_t b) {
for (int index = 0; index < WS2812_LED_COUNT; index++) {
leds_colors[index * WS2812_LED_CHANNEL_COUNT + 0] = r;
leds_colors[index * WS2812_LED_CHANNEL_COUNT + 1] = g;
leds_colors[index * WS2812_LED_CHANNEL_COUNT + 2] = b;
}
}
uint8_t WS2812::led_is_update_finished(void) { return !is_updating; }
void WS2812::led_start_reset_pulse(uint8_t num) {
is_reset_pulse = num; /* Set reset pulse flag */
memset((void *)tmp_led_data, 0, sizeof(tmp_led_data)); /* Set all bytes to 0 to achieve 50us pulse */
if (num == 1) {
tmp_led_data[0] = (htim1.Instance->ARR * 2) / 3; // start with half width pulse
}
/* Set DMA to normal mode, set memory to beginning of data and length to 40 elements */
/* 800kHz PWM x 40 samples = ~50us pulse low */
hdma_tim1_ch1.Instance->CCR &= (~DMA_CCR_CIRC); // clear circular flag -> normal mode
hdma_tim1_ch1.State = HAL_DMA_STATE_READY;
HAL_DMA_Start_IT(&hdma_tim1_ch1, (uint32_t)tmp_led_data, (uint32_t)&htim1.Instance->CCR1, 2 * WS2812_RAW_BYTES_PER_LED);
HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1);
}
void WS2812::DMAHalfComplete(DMA_HandleTypeDef *hdma) { led_update_sequence(0); }
void WS2812::DMAComplete(DMA_HandleTypeDef *hdma) { led_update_sequence(1); }
void WS2812::led_update_sequence(uint8_t tc) {
tc = !!tc; /* Convert to 1 or 0 value only */
/* Check for reset pulse at the end of PWM stream */
if (is_reset_pulse == 2) { /* Check for reset pulse at the end */
HAL_TIM_PWM_Stop(&htim1, TIM_CHANNEL_1);
HAL_DMA_Abort(&hdma_tim1_ch1);
is_updating = 0; /* We are not updating anymore */
return;
}
/* Check for reset pulse on beginning of PWM stream */
if (is_reset_pulse == 1) { /* Check if we finished with reset pulse */
/*
* When reset pulse is active, we have to wait full DMA response,
* before we can start modifying array which is shared with DMA and PWM
*/
if (!tc) { /* We must wait for transfer complete */
return; /* Return and wait to finish */
}
/* Disable timer output and disable DMA stream */
HAL_TIM_PWM_Stop(&htim1, TIM_CHANNEL_1);
HAL_DMA_Abort(&hdma_tim1_ch1);
is_reset_pulse = 0; /* Not in reset pulse anymore */
current_led = 0; /* Reset current led */
} else {
/*
* When we are not in reset mode,
* go to next led and process data for it
*/
current_led++; /* Go to next LED */
}
/*
* This part is used to prepare data for "next" led,
* for which update will start once current transfer stops in circular mode
*/
if (current_led < WS2812_LED_COUNT) {
/*
* If we are preparing data for first time (current_led == 0)
* or if there was no TC event (it was HT):
*
* - Prepare first part of array, because either there is no transfer
* or second part (from HT to TC) is now in process for PWM transfer
*
* In other case (TC = 1)
*/
if (current_led == 0 || !tc) {
led_fill_led_pwm_data(current_led, &tmp_led_data[0]);
} else {
led_fill_led_pwm_data(current_led, &tmp_led_data[WS2812_RAW_BYTES_PER_LED]);
}
/*
* If we are preparing first led (current_led = 0), then:
*
* - We setup first part of array for first led,
* - We have to prepare second part for second led to have one led prepared in advance
* - Set DMA to circular mode and start the transfer + PWM output
*/
if (current_led == 0) {
current_led++; /* Go to next LED */
led_fill_led_pwm_data(current_led, &tmp_led_data[WS2812_RAW_BYTES_PER_LED]); /* Prepare second LED too */
hdma_tim1_ch1.Instance->CCR |= (DMA_CCR_CIRC); // set circular flag for circular mode
hdma_tim1_ch1.State = HAL_DMA_STATE_READY;
HAL_DMA_Start_IT(&hdma_tim1_ch1, (uint32_t)tmp_led_data, (uint32_t)&htim1.Instance->CCR1, 2 * WS2812_RAW_BYTES_PER_LED);
HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1);
}
/*
* When we reached all leds, we have to wait to transmit data for all leds before we can disable DMA and PWM:
*
* - If TC event is enabled and we have EVEN number of LEDS (2, 4, 6, ...)
* - If HT event is enabled and we have ODD number of LEDS (1, 3, 5, ...)
*/
} else if ((!tc && (WS2812_LED_COUNT & 0x01)) || (tc && !(WS2812_LED_COUNT & 0x01))) {
HAL_TIM_PWM_Stop(&htim1, TIM_CHANNEL_1);
HAL_DMA_Abort(&hdma_tim1_ch1);
/* It is time to send final reset pulse, 50us at least */
led_start_reset_pulse(2); /* Start reset pulse at the end */
}
}
void WS2812::led_fill_led_pwm_data(size_t ledx, volatile uint16_t *ptr) {
size_t i;
uint16_t OnOffValues[] = {2 * htim1.Instance->ARR / 3, (4 * htim1.Instance->ARR) / 3};
if (ledx < WS2812_LED_COUNT) {
for (i = 0; i < 8; i++) {
// Also unmux RGB -> GRB in the index order here
ptr[i] = (leds_colors[WS2812_LED_CHANNEL_COUNT * ledx + 1] & (1 << (7 - i))) ? OnOffValues[1] : OnOffValues[0];
ptr[8 + i] = (leds_colors[WS2812_LED_CHANNEL_COUNT * ledx + 0] & (1 << (7 - i))) ? OnOffValues[1] : OnOffValues[0];
ptr[16 + i] = (leds_colors[WS2812_LED_CHANNEL_COUNT * ledx + 2] & (1 << (7 - i))) ? OnOffValues[1] : OnOffValues[0];
#if WS2812_LED_CHANNEL_COUNT == 4
ptr[24 + i] = (leds_colors[WS2812_LED_CHANNEL_COUNT * ledx + 3] & (1 << (7 - i))) ? OnOffValues[1] : OnOffValues[0];
#endif
}
} else {
// Fill with zero?
}
for (int index = 0; index < WS2812_LED_COUNT; index++) {
leds_colors[index * WS2812_LED_CHANNEL_COUNT + 0] = g;
leds_colors[index * WS2812_LED_CHANNEL_COUNT + 1] = r;
leds_colors[index * WS2812_LED_CHANNEL_COUNT + 2] = b;
}
}

View File

@@ -11,7 +11,7 @@
#ifndef CORE_DRIVERS_WS2812_H_
#define CORE_DRIVERS_WS2812_H_
#ifndef WS2812_LED_COUNT
#define WS2812_LED_COUNT 3
#define WS2812_LED_COUNT 2
#endif
#ifndef WS2812_LED_CHANNEL_COUNT
#define WS2812_LED_CHANNEL_COUNT 3
@@ -19,23 +19,14 @@
#define WS2812_RAW_BYTES_PER_LED (WS2812_LED_CHANNEL_COUNT * 8)
class WS2812 {
public:
static void init(void);
static uint8_t led_update(uint8_t block);
static void led_set_color(size_t index, uint8_t r, uint8_t g, uint8_t b);
static void led_set_color_all(uint8_t r, uint8_t g, uint8_t b);
static void init(void);
static void led_update();
static void led_set_color(size_t index, uint8_t r, uint8_t g, uint8_t b);
static void led_set_color_all(uint8_t r, uint8_t g, uint8_t b);
private:
static uint8_t led_is_update_finished(void);
static void led_start_reset_pulse(uint8_t num);
static void DMAHalfComplete(DMA_HandleTypeDef *hdma);
static void DMAComplete(DMA_HandleTypeDef *hdma);
static void led_update_sequence(uint8_t tc);
static void led_fill_led_pwm_data(size_t ledx, volatile uint16_t *ptr);
static uint8_t leds_colors[WS2812_LED_CHANNEL_COUNT * WS2812_LED_COUNT];
static volatile uint16_t tmp_led_data[2 * WS2812_RAW_BYTES_PER_LED];
static volatile uint8_t is_reset_pulse; /*!< Status if we are sending reset pulse or led data */
static volatile uint8_t is_updating; /*!< Is updating in progress? */
static volatile uint32_t current_led; /*!< Current LED number we are sending */
static uint8_t leds_colors[WS2812_LED_CHANNEL_COUNT * WS2812_LED_COUNT];
};
#endif /* CORE_DRIVERS_WS2812_H_ */