FreeRTOS introduced
This commit is contained in:
parent
e7fdc831a4
commit
d54d0a8992
49 changed files with 29101 additions and 151 deletions
|
@ -13,6 +13,10 @@
|
|||
#include <stm32f1xx_ll_gpio.h>
|
||||
#include <stm32f1xx_ll_spi.h>
|
||||
|
||||
#include <FreeRTOS.h>
|
||||
#include <task.h>
|
||||
#include <semphr.h>
|
||||
|
||||
struct pixel_t {
|
||||
uint8_t g;
|
||||
uint8_t r;
|
||||
|
@ -30,39 +34,54 @@ public:
|
|||
friend void HandleSpiDmaIrq();
|
||||
void Update();
|
||||
|
||||
static void RefillTaskEntry(void *param) { reinterpret_cast<WS28xxStrip<pixels, spi_pixels>*>(param)->RefillTask(); }
|
||||
|
||||
private:
|
||||
void SpiDmaIsr();
|
||||
void Refill();
|
||||
void RefillTask();
|
||||
void Convert(uint8_t *src, uint8_t *dst, uint16_t src_size);
|
||||
template<typename T> T Min(T a, T b) { return a < b ? a : b; }
|
||||
|
||||
TaskHandle_t m_task_handle = nullptr;
|
||||
|
||||
static constexpr const unsigned int SPI_PIXEL_SIZE = sizeof(pixel_t) * 4;
|
||||
static constexpr const unsigned int SPI_BUFFER_SIZE = spi_pixels * SPI_PIXEL_SIZE;
|
||||
|
||||
pixel_t m_pixels[pixels];
|
||||
uint8_t m_spi_buffer[2][SPI_BUFFER_SIZE];
|
||||
|
||||
volatile uint8_t m_spi_idle = 0;
|
||||
|
||||
StaticSemaphore_t m_main_mutex_buffer;
|
||||
SemaphoreHandle_t m_main_mutex;
|
||||
//volatile uint8_t m_spi_idle = 0;
|
||||
volatile uint8_t m_buffer_in_transmit;
|
||||
volatile uint16_t m_pixels_converted = 0;
|
||||
volatile bool m_endframe = false;
|
||||
volatile bool m_endprev = false;
|
||||
volatile bool m_need_refill = false;
|
||||
};
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
template <uint16_t pixels, uint8_t spi_pixels> WS28xxStrip<pixels, spi_pixels>::WS28xxStrip(pixel_t defcolor)
|
||||
: m_main_mutex(xSemaphoreCreateBinaryStatic(&m_main_mutex_buffer))
|
||||
{
|
||||
for(uint16_t p=0; p < pixels; ++p)
|
||||
m_pixels[p] = defcolor;
|
||||
|
||||
xSemaphoreGive(m_main_mutex);
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
template <uint16_t pixels, uint8_t spi_pixels> pixel_t& WS28xxStrip<pixels, spi_pixels>::operator[](int16_t index)
|
||||
{
|
||||
return m_pixels[index];
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
template <uint16_t pixels, uint8_t spi_pixels> void WS28xxStrip<pixels, spi_pixels>::SpiDmaIsr()
|
||||
{
|
||||
BaseType_t woken;
|
||||
|
||||
if(LL_DMA_IsActiveFlag_TE3(DMA1)) {
|
||||
LL_DMA_ClearFlag_TE3(DMA1);
|
||||
}
|
||||
|
@ -82,45 +101,60 @@ template <uint16_t pixels, uint8_t spi_pixels> void WS28xxStrip<pixels, spi_pixe
|
|||
if(m_endframe && m_endprev) {
|
||||
LL_DMA_DisableChannel(DMA1, LL_DMA_CHANNEL_3);
|
||||
LL_DMA_SetMode(DMA1, LL_DMA_CHANNEL_3, LL_DMA_MODE_CIRCULAR);
|
||||
m_spi_idle = true;
|
||||
woken = pdFALSE;
|
||||
xSemaphoreGiveFromISR(m_main_mutex, &woken);
|
||||
portYIELD_FROM_ISR(woken);
|
||||
m_endframe = m_endprev = false;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
m_need_refill = true;
|
||||
woken = pdFALSE;
|
||||
|
||||
if(m_task_handle) {
|
||||
vTaskNotifyGiveFromISR(m_task_handle, &woken);
|
||||
portYIELD_FROM_ISR(woken);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
template <uint16_t pixels, uint8_t spi_pixels> void WS28xxStrip<pixels, spi_pixels>::Refill()
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
template <uint16_t pixels, uint8_t spi_pixels> void WS28xxStrip<pixels, spi_pixels>::RefillTask()
|
||||
{
|
||||
m_need_refill = false;
|
||||
m_endprev = m_endframe;
|
||||
m_task_handle = xTaskGetCurrentTaskHandle();
|
||||
|
||||
uint8_t convert_now = Min((uint8_t)(pixels - m_pixels_converted), spi_pixels);
|
||||
|
||||
if(convert_now)
|
||||
while(true)
|
||||
{
|
||||
LL_GPIO_TogglePin(GPIOC, LL_GPIO_PIN_13);
|
||||
Convert((uint8_t*)&m_pixels[m_pixels_converted],
|
||||
m_spi_buffer[m_buffer_in_transmit ^ 1],
|
||||
convert_now * sizeof(pixel_t));
|
||||
LL_GPIO_TogglePin(GPIOC, LL_GPIO_PIN_13);
|
||||
m_pixels_converted += convert_now;
|
||||
}
|
||||
if( ulTaskNotifyTake(pdFALSE, pdMS_TO_TICKS(1000)) > 0)
|
||||
{
|
||||
m_endprev = m_endframe;
|
||||
|
||||
if(convert_now < spi_pixels) {
|
||||
memset(m_spi_buffer[m_buffer_in_transmit ^ 1] + convert_now * SPI_PIXEL_SIZE,
|
||||
0, SPI_BUFFER_SIZE - convert_now * SPI_PIXEL_SIZE);
|
||||
m_endframe = true;
|
||||
}
|
||||
uint8_t convert_now = Min((uint8_t)(pixels - m_pixels_converted), spi_pixels);
|
||||
|
||||
if(convert_now)
|
||||
{
|
||||
LL_GPIO_TogglePin(GPIOC, LL_GPIO_PIN_13);
|
||||
Convert((uint8_t*)&m_pixels[m_pixels_converted],
|
||||
m_spi_buffer[m_buffer_in_transmit ^ 1],
|
||||
convert_now * sizeof(pixel_t));
|
||||
LL_GPIO_TogglePin(GPIOC, LL_GPIO_PIN_13);
|
||||
m_pixels_converted += convert_now;
|
||||
}
|
||||
|
||||
if(convert_now < spi_pixels) {
|
||||
memset(m_spi_buffer[m_buffer_in_transmit ^ 1] + convert_now * SPI_PIXEL_SIZE,
|
||||
0, SPI_BUFFER_SIZE - convert_now * SPI_PIXEL_SIZE);
|
||||
m_endframe = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
template <uint16_t pixels, uint8_t spi_pixels> void WS28xxStrip<pixels, spi_pixels>::Update()
|
||||
{
|
||||
xSemaphoreTake(m_main_mutex, portMAX_DELAY);
|
||||
|
||||
m_pixels_converted = 0;
|
||||
|
||||
Convert((uint8_t*)m_pixels, m_spi_buffer[0], spi_pixels * sizeof(pixel_t));
|
||||
|
@ -129,7 +163,6 @@ template <uint16_t pixels, uint8_t spi_pixels> void WS28xxStrip<pixels, spi_pixe
|
|||
m_pixels_converted += spi_pixels;
|
||||
|
||||
m_buffer_in_transmit = 0;
|
||||
m_spi_idle = false;
|
||||
|
||||
LL_DMA_ConfigAddresses(DMA1, LL_DMA_CHANNEL_3, (uint32_t)m_spi_buffer, LL_SPI_DMA_GetRegAddr(SPI1), LL_DMA_DIRECTION_MEMORY_TO_PERIPH);
|
||||
LL_DMA_SetDataLength(DMA1, LL_DMA_CHANNEL_3, sizeof(m_spi_buffer));
|
||||
|
@ -137,15 +170,9 @@ template <uint16_t pixels, uint8_t spi_pixels> void WS28xxStrip<pixels, spi_pixe
|
|||
LL_DMA_EnableIT_HT(DMA1, LL_DMA_CHANNEL_3);
|
||||
LL_DMA_EnableIT_TC(DMA1, LL_DMA_CHANNEL_3);
|
||||
LL_DMA_EnableIT_TE(DMA1, LL_DMA_CHANNEL_3);
|
||||
|
||||
while(!m_spi_idle) {
|
||||
while(!m_spi_idle && !m_need_refill);
|
||||
if(m_need_refill)
|
||||
Refill();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
template <uint16_t pixels, uint8_t spi_pixels> void WS28xxStrip<pixels, spi_pixels>::Convert(uint8_t *src, uint8_t *dst, uint16_t src_size)
|
||||
{
|
||||
static uint16_t const bits[16] = { // due to LE-ness the bit order is 1 0 3 2
|
||||
|
@ -163,5 +190,4 @@ template <uint16_t pixels, uint8_t spi_pixels> void WS28xxStrip<pixels, spi_pixe
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
#endif /* WS28XXSTRIP_H_ */
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue