Added DMA ISR.

Signed-off-by: Yilin Sun <imi415@imi.moe>
This commit is contained in:
Yilin Sun 2023-10-19 09:48:39 +08:00
parent 5e568857b2
commit 9b148c6fff
Signed by: imi415
GPG Key ID: 17F01E106F9F5E0A
1 changed files with 27 additions and 7 deletions

View File

@ -6,6 +6,10 @@
#include "ch32x035_gpio.h"
#include "ch32x035_spi.h"
/* FreeRTOS */
#include "FreeRTOS.h"
#include "semphr.h"
/* Private */
#include "app_led1w.h"
@ -18,6 +22,8 @@
/* SPI buffer to store waveform data */
static uint8_t s_led1w_spi_buf[APP_LED1W_MAXIMUM_LEDS * 3 * 8 / 2 + APP_LED1W_ZERO_PADDING];
static SemaphoreHandle_t s_led1w_semphr = NULL;
static void app_led1w_encode(const uint8_t *data, uint16_t len);
void app_led1w_init(void) {
@ -57,9 +63,23 @@ void app_led1w_init(void) {
gpio_init.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &gpio_init);
s_led1w_semphr = xSemaphoreCreateBinary();
xSemaphoreGive(s_led1w_semphr);
NVIC_EnableIRQ(DMA1_Channel3_IRQn);
/* Pre-filling RESET timing to SPI - In case IO glitching */
for (uint32_t i = 0; i < APP_LED1W_ZERO_PADDING; i++) {
s_led1w_spi_buf[i] = 0x00;
}
}
int app_led1w_write(uint8_t *data, uint16_t led_count) {
if (xSemaphoreTake(s_led1w_semphr, portMAX_DELAY) != pdTRUE) {
return -1;
}
uint16_t byte_count = led_count * 3 * 8 / 2;
if ((byte_count + APP_LED1W_ZERO_PADDING) > sizeof(s_led1w_spi_buf)) {
@ -88,9 +108,9 @@ int app_led1w_write(uint8_t *data, uint16_t led_count) {
/* Enable SPI TX DMA request generation */
SPI_I2S_DMACmd(SPI1, SPI_I2S_DMAReq_Tx, ENABLE);
DMA_Cmd(DMA1_Channel3, ENABLE);
DMA_ITConfig(DMA1_Channel3, DMA_IT_TC | DMA_IT_TE, ENABLE);
NVIC_EnableIRQ(DMA1_Channel3_IRQn);
DMA_Cmd(DMA1_Channel3, ENABLE);
SPI_Cmd(SPI1, ENABLE);
@ -98,10 +118,6 @@ int app_led1w_write(uint8_t *data, uint16_t led_count) {
}
static void app_led1w_encode(const uint8_t *data, uint16_t len) {
for (uint32_t i = 0; i < APP_LED1W_ZERO_PADDING; i++) {
s_led1w_spi_buf[i] = 0x00;
}
for (uint16_t i = 0; i < len + 0; i++) {
uint16_t buf_index = i * 4 + APP_LED1W_ZERO_PADDING; // Add 0 padding
@ -118,5 +134,9 @@ static void app_led1w_encode(const uint8_t *data, uint16_t len) {
}
void app_led1w_irq_handler(void) {
DMA_ClearFlag(DMA1_FLAG_TC3 | DMA1_FLAG_TE3);
}
BaseType_t higher_priority_woken = pdFALSE;
xSemaphoreGiveFromISR(s_led1w_semphr, &higher_priority_woken);
portYIELD_FROM_ISR(higher_priority_woken);
}