WiP on WS2812 Protocol driver
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
#include "Setup.h"
|
||||
#include "TipThermoModel.h"
|
||||
#include "Utils.h"
|
||||
#include "WS2812.h"
|
||||
#include "configuration.h"
|
||||
#include "history.hpp"
|
||||
#include "main.hpp"
|
||||
@@ -410,4 +411,7 @@ bool isTipDisconnected() {
|
||||
return tipDisconnected;
|
||||
}
|
||||
|
||||
void setStatusLED(const enum StatusLED state) {}
|
||||
void setStatusLED(const enum StatusLED state) {
|
||||
WS2812::led_set_color(0, 0xFF, 0, 0);
|
||||
WS2812::led_update(1);
|
||||
}
|
||||
|
||||
@@ -51,6 +51,6 @@
|
||||
#define PLATE_SENSOR_ADC1_CHANNEL ADC_CHANNEL_5
|
||||
#define PLATE_SENSOR_ADC2_CHANNEL ADC_CHANNEL_5
|
||||
|
||||
#define WS2812_Pin GPIO_PIN_3
|
||||
#define WS2812_Pin GPIO_PIN_8
|
||||
#define WS2812_GPIO_Port GPIOA
|
||||
#endif /* BSP_MINIWARE_PINS_H_ */
|
||||
|
||||
@@ -17,7 +17,7 @@ DMA_HandleTypeDef hdma_i2c1_tx;
|
||||
|
||||
IWDG_HandleTypeDef hiwdg;
|
||||
TIM_HandleTypeDef htim1;
|
||||
DMA_HandleTypeDef hdma_tim1_ch2;
|
||||
DMA_HandleTypeDef hdma_tim1_ch1;
|
||||
TIM_HandleTypeDef htim2;
|
||||
TIM_HandleTypeDef htim3;
|
||||
#define ADC_CHANNELS 4
|
||||
@@ -77,9 +77,10 @@ void SystemClock_Config(void) {
|
||||
|
||||
/**Initializes the CPU, AHB and APB busses clocks
|
||||
*/
|
||||
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_LSI;
|
||||
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI
|
||||
| RCC_OSCILLATORTYPE_LSI;
|
||||
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
|
||||
RCC_OscInitStruct.HSICalibrationValue = 16;
|
||||
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
|
||||
RCC_OscInitStruct.LSIState = RCC_LSI_ON;
|
||||
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
|
||||
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI_DIV2;
|
||||
@@ -88,7 +89,8 @@ void SystemClock_Config(void) {
|
||||
|
||||
/**Initializes the CPU, AHB and APB busses clocks
|
||||
*/
|
||||
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
|
||||
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
|
||||
| RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
|
||||
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
|
||||
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
|
||||
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV16; // TIM
|
||||
@@ -240,17 +242,21 @@ void MX_TIM1_Init(void) {
|
||||
htim1.Instance = TIM1;
|
||||
htim1.Init.Prescaler = 0;
|
||||
htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
|
||||
htim1.Init.Period = 104;
|
||||
htim1.Init.Period = 40;
|
||||
htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
|
||||
htim1.Init.RepetitionCounter = 0;
|
||||
htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
|
||||
if (HAL_TIM_Base_Init(&htim1) != HAL_OK) {}
|
||||
htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
|
||||
HAL_TIM_Base_Init(&htim1);
|
||||
|
||||
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
|
||||
if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK) {}
|
||||
if (HAL_TIM_PWM_Init(&htim1) != HAL_OK) {}
|
||||
HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig);
|
||||
|
||||
HAL_TIM_PWM_Init(&htim1);
|
||||
|
||||
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
|
||||
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
|
||||
if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK) {}
|
||||
HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig);
|
||||
|
||||
sConfigOC.OCMode = TIM_OCMODE_PWM1;
|
||||
sConfigOC.Pulse = 0;
|
||||
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
|
||||
@@ -258,27 +264,28 @@ void MX_TIM1_Init(void) {
|
||||
sConfigOC.OCFastMode = TIM_OCFAST_ENABLE;
|
||||
sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET;
|
||||
sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET;
|
||||
if (HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_2) != HAL_OK) {}
|
||||
HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_1);
|
||||
|
||||
sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE;
|
||||
sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE;
|
||||
sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF;
|
||||
sBreakDeadTimeConfig.DeadTime = 0;
|
||||
sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE;
|
||||
sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH;
|
||||
sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_LOW;
|
||||
sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE;
|
||||
if (HAL_TIMEx_ConfigBreakDeadTime(&htim1, &sBreakDeadTimeConfig) != HAL_OK) {}
|
||||
/* USER CODE BEGIN TIM1_Init 2 */
|
||||
HAL_TIMEx_ConfigBreakDeadTime(&htim1, &sBreakDeadTimeConfig);
|
||||
|
||||
/* USER CODE END TIM1_Init 2 */
|
||||
__HAL_RCC_GPIOA_CLK_ENABLE();
|
||||
GPIO_InitTypeDef GPIO_InitStruct;
|
||||
/**TIM1 GPIO Configuration
|
||||
PA9 ------> TIM1_CH2
|
||||
PA8 ------> TIM1_CH1
|
||||
*/
|
||||
GPIO_InitStruct.Pin = WS2812_Pin;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
|
||||
HAL_GPIO_Init(WS2812_GPIO_Port, &GPIO_InitStruct);
|
||||
__HAL_AFIO_REMAP_TIM1_DISABLE();
|
||||
}
|
||||
|
||||
/* TIM3 init function */
|
||||
@@ -378,8 +385,8 @@ static void MX_DMA_Init(void) {
|
||||
HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 10, 0);
|
||||
HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn);
|
||||
/* DMA1_Channel1_IRQn interrupt configuration */
|
||||
HAL_NVIC_SetPriority(DMA1_Channel3_IRQn, 6, 0); // DMA 1 ch3 is used from TIM CH2 for WS2812
|
||||
HAL_NVIC_EnableIRQ(DMA1_Channel3_IRQn);
|
||||
HAL_NVIC_SetPriority(DMA1_Channel2_IRQn, 2, 0); // DMA 1 ch2 is used from TIM CH1 for WS2812
|
||||
HAL_NVIC_EnableIRQ(DMA1_Channel2_IRQn);
|
||||
/* DMA1_Channel6_IRQn interrupt configuration */
|
||||
HAL_NVIC_SetPriority(DMA1_Channel6_IRQn, 5, 0);
|
||||
HAL_NVIC_EnableIRQ(DMA1_Channel6_IRQn);
|
||||
@@ -411,11 +418,15 @@ static void MX_GPIO_Init(void) {
|
||||
/*
|
||||
* Configure All pins as analog by default
|
||||
*/
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7 | GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_15;
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3
|
||||
| GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7 | GPIO_PIN_8
|
||||
| GPIO_PIN_10 | GPIO_PIN_15;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
|
||||
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7 | GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12
|
||||
| GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15;
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3
|
||||
| GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7 | GPIO_PIN_8
|
||||
| GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_13
|
||||
| GPIO_PIN_14 | GPIO_PIN_15;
|
||||
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : KEY_B_Pin KEY_A_Pin */
|
||||
@@ -439,6 +450,7 @@ static void MX_GPIO_Init(void) {
|
||||
HAL_GPIO_WritePin(OLED_RESET_GPIO_Port, OLED_RESET_Pin, GPIO_PIN_RESET);
|
||||
HAL_Delay(30);
|
||||
HAL_GPIO_WritePin(OLED_RESET_GPIO_Port, OLED_RESET_Pin, GPIO_PIN_SET);
|
||||
|
||||
}
|
||||
|
||||
#ifdef USE_FULL_ASSERT
|
||||
|
||||
@@ -25,7 +25,7 @@ extern I2C_HandleTypeDef hi2c1;
|
||||
extern IWDG_HandleTypeDef hiwdg;
|
||||
|
||||
extern TIM_HandleTypeDef htim1;
|
||||
extern DMA_HandleTypeDef hdma_tim1_ch2;
|
||||
extern DMA_HandleTypeDef hdma_tim1_ch1;
|
||||
extern TIM_HandleTypeDef htim2;
|
||||
extern TIM_HandleTypeDef htim3;
|
||||
void Setup_HAL();
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#include "I2C_Wrapper.hpp"
|
||||
#include "QC3.h"
|
||||
#include "Settings.h"
|
||||
#include "WS2812.h"
|
||||
#include "cmsis_os.h"
|
||||
#include "fusbpd.h"
|
||||
#include "main.hpp"
|
||||
@@ -11,4 +12,14 @@
|
||||
#include "task.h"
|
||||
|
||||
// Initialisation to be performed with scheduler active
|
||||
void postRToSInit() {}
|
||||
void postRToSInit() {
|
||||
WS2812::init();
|
||||
WS2812::led_set_color(0, 0xAA, 0x55, 0x00);
|
||||
while (true) {
|
||||
// osDelay(1);
|
||||
// WS2812::led_set_color(0, 0xFF, 0xFF, 0xFF);
|
||||
// WS2812::led_update(1);
|
||||
osDelay(10);
|
||||
WS2812::led_update(1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -128,16 +128,17 @@ void HAL_TIM_Base_MspInit(TIM_HandleTypeDef *htim_base) {
|
||||
|
||||
if (htim_base->Instance == TIM1) {
|
||||
__HAL_RCC_TIM1_CLK_ENABLE();
|
||||
hdma_tim1_ch2.Instance = DMA1_Channel3;
|
||||
hdma_tim1_ch2.Init.Direction = DMA_MEMORY_TO_PERIPH;
|
||||
hdma_tim1_ch2.Init.PeriphInc = DMA_PINC_DISABLE;
|
||||
hdma_tim1_ch2.Init.MemInc = DMA_MINC_ENABLE;
|
||||
hdma_tim1_ch2.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;
|
||||
hdma_tim1_ch2.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
|
||||
hdma_tim1_ch2.Init.Mode = DMA_CIRCULAR;
|
||||
hdma_tim1_ch2.Init.Priority = DMA_PRIORITY_LOW;
|
||||
if (HAL_DMA_Init(&hdma_tim1_ch2) != HAL_OK) {}
|
||||
__HAL_LINKDMA(htim_base, hdma[TIM_DMA_ID_CC2], hdma_tim1_ch2);
|
||||
__HAL_RCC_DMA1_CLK_ENABLE();
|
||||
hdma_tim1_ch1.Instance = DMA1_Channel2;
|
||||
hdma_tim1_ch1.Init.Direction = DMA_MEMORY_TO_PERIPH;
|
||||
hdma_tim1_ch1.Init.PeriphInc = DMA_PINC_DISABLE;
|
||||
hdma_tim1_ch1.Init.MemInc = DMA_MINC_ENABLE;
|
||||
hdma_tim1_ch1.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;
|
||||
hdma_tim1_ch1.Init.MemDataAlignment = DMA_PDATAALIGN_HALFWORD;
|
||||
hdma_tim1_ch1.Init.Mode = DMA_CIRCULAR;
|
||||
hdma_tim1_ch1.Init.Priority = DMA_PRIORITY_VERY_HIGH;
|
||||
HAL_DMA_Init(&hdma_tim1_ch1);
|
||||
__HAL_LINKDMA(htim_base, hdma[TIM_DMA_ID_CC1], hdma_tim1_ch1);
|
||||
} else if (htim_base->Instance == TIM3) {
|
||||
__HAL_RCC_TIM3_CLK_ENABLE();
|
||||
} else if (htim_base->Instance == TIM2) {
|
||||
|
||||
@@ -49,7 +49,7 @@ void TIM4_IRQHandler(void) { HAL_TIM_IRQHandler(&htim4); }
|
||||
void I2C1_EV_IRQHandler(void) { HAL_I2C_EV_IRQHandler(&hi2c1); }
|
||||
void I2C1_ER_IRQHandler(void) { HAL_I2C_ER_IRQHandler(&hi2c1); }
|
||||
|
||||
void DMA1_Channel3_IRQHandler(void) { HAL_DMA_IRQHandler(&hdma_tim1_ch2); }
|
||||
void DMA1_Channel2_IRQHandler(void) { HAL_DMA_IRQHandler(&hdma_tim1_ch1); }
|
||||
void DMA1_Channel6_IRQHandler(void) { HAL_DMA_IRQHandler(&hdma_i2c1_tx); }
|
||||
|
||||
void DMA1_Channel7_IRQHandler(void) { HAL_DMA_IRQHandler(&hdma_i2c1_rx); }
|
||||
|
||||
198
source/Core/Drivers/WS2812.cpp
Normal file
198
source/Core/Drivers/WS2812.cpp
Normal file
@@ -0,0 +1,198 @@
|
||||
/*
|
||||
* WS2812.cpp
|
||||
*
|
||||
* Created on: 2 May 2021
|
||||
* Author: Ralim
|
||||
*/
|
||||
|
||||
#include "FreeRTOS.h"
|
||||
#include "task.h"
|
||||
#include <WS2812.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 */
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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 */
|
||||
|
||||
led_start_reset_pulse(1); /* Start reset pulse */
|
||||
if (block) {
|
||||
while (!led_is_update_finished()) {
|
||||
vTaskDelay(1);
|
||||
}; /* Wait to finish */
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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; // 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, 40);
|
||||
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];
|
||||
}
|
||||
} else {
|
||||
//Fill with zero?
|
||||
}
|
||||
}
|
||||
41
source/Core/Drivers/WS2812.h
Normal file
41
source/Core/Drivers/WS2812.h
Normal file
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* WS2812.h
|
||||
*
|
||||
* Created on: 2 May 2021
|
||||
* Author: Ralim
|
||||
*/
|
||||
#include "Setup.h"
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#ifndef CORE_DRIVERS_WS2812_H_
|
||||
#define CORE_DRIVERS_WS2812_H_
|
||||
#ifndef WS2812_LED_COUNT
|
||||
#define WS2812_LED_COUNT 4
|
||||
#endif
|
||||
#ifndef WS2812_LED_CHANNEL_COUNT
|
||||
#define WS2812_LED_CHANNEL_COUNT 3
|
||||
#endif
|
||||
#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);
|
||||
|
||||
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 */
|
||||
};
|
||||
|
||||
#endif /* CORE_DRIVERS_WS2812_H_ */
|
||||
@@ -127,7 +127,7 @@ CPUFLAGS= -mcpu=cortex-m3 \
|
||||
-mthumb \
|
||||
-mfloat-abi=soft
|
||||
flash_size=128k
|
||||
bootldr_size=0x0000
|
||||
bootldr_size=0x8000
|
||||
endif
|
||||
ifeq ($(model),$(ALL_PINE_MODELS))
|
||||
$(info Building for Pine64 )
|
||||
|
||||
Reference in New Issue
Block a user