-
Notifications
You must be signed in to change notification settings - Fork 290
GD32E230 feature: add WS2812 support #270
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
df92f64
1b859c4
c56101e
8993db6
702ce94
5ed5bdb
7b646e4
d6c5b0b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| /* | ||
| * WS2812.h | ||
| * | ||
| * Created on: Sep 9, 2020 | ||
| * Author: Alka | ||
| */ | ||
|
|
||
| #ifndef INC_WS2812_H_ | ||
| #define INC_WS2812_H_ | ||
|
|
||
| #include "main.h" | ||
| #define RGB_BUFFER_SIZE 28 | ||
|
|
||
| void send_LED_DMA(); | ||
| void WS2812_Init(void); | ||
| void send_LED_RGB(uint8_t red, uint8_t green, uint8_t blue); | ||
|
|
||
| extern char dma_busy; | ||
|
|
||
| #endif /* INC_WS2812_H_ */ |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,154 @@ | ||||||||||||||||||||||
| /* | ||||||||||||||||||||||
| * WS2812.c | ||||||||||||||||||||||
| * | ||||||||||||||||||||||
| * Created on: Sep 9, 2020 | ||||||||||||||||||||||
| * Author: Alka | ||||||||||||||||||||||
| * | ||||||||||||||||||||||
| * Modified for GD32E230 on: Oct 23, 2025 | ||||||||||||||||||||||
| * Author: Scarittagle | ||||||||||||||||||||||
| */ | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| #include "WS2812.h" | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| #include "targets.h" | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| char dma_busy; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| uint16_t led_Buffer[RGB_BUFFER_SIZE] = { | ||||||||||||||||||||||
| 0, | ||||||||||||||||||||||
| 0, | ||||||||||||||||||||||
| 20, | ||||||||||||||||||||||
| 20, | ||||||||||||||||||||||
| 20, | ||||||||||||||||||||||
| 20, | ||||||||||||||||||||||
| 20, | ||||||||||||||||||||||
| 20, | ||||||||||||||||||||||
| 20, | ||||||||||||||||||||||
| 20, | ||||||||||||||||||||||
| 60, | ||||||||||||||||||||||
| 60, | ||||||||||||||||||||||
| 60, | ||||||||||||||||||||||
| 60, | ||||||||||||||||||||||
| 60, | ||||||||||||||||||||||
| 60, | ||||||||||||||||||||||
| 60, | ||||||||||||||||||||||
| 60, | ||||||||||||||||||||||
| 20, | ||||||||||||||||||||||
| 20, | ||||||||||||||||||||||
| 20, | ||||||||||||||||||||||
| 20, | ||||||||||||||||||||||
| 20, | ||||||||||||||||||||||
| 20, | ||||||||||||||||||||||
| 20, | ||||||||||||||||||||||
| 20, | ||||||||||||||||||||||
| 0, | ||||||||||||||||||||||
| 0, | ||||||||||||||||||||||
| }; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| void send_LED_DMA() | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| dma_busy = 1; | ||||||||||||||||||||||
| dma_channel_disable(LED_DMA_CHANNEL); | ||||||||||||||||||||||
| dma_memory_address_config(LED_DMA_CHANNEL, (uint32_t)led_Buffer); | ||||||||||||||||||||||
| dma_transfer_number_config(LED_DMA_CHANNEL, RGB_BUFFER_SIZE); | ||||||||||||||||||||||
| dma_channel_enable(LED_DMA_CHANNEL); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| void send_LED_RGB(uint8_t red, uint8_t green, uint8_t blue) | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| if (!dma_busy) { | ||||||||||||||||||||||
| uint32_t twenty_four_bit_color_number = green << 16 | red << 8 | blue; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| for (int i = 0; i < 24; i++) { | ||||||||||||||||||||||
| led_Buffer[i + 2] = (((twenty_four_bit_color_number >> (23 - i)) & 1) * 31) + 28; | ||||||||||||||||||||||
| // Bit 0: 0*31+28 = 28 (~311ns HIGH, ~939ns LOW) - blog CODE_0 | ||||||||||||||||||||||
| // Bit 1: 1*31+28 = 59 (~656ns HIGH, ~594ns LOW) - blog CODE_1 | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| send_LED_DMA(); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| void WS2812_Init(void) | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| // Init Timer&DMA config structures | ||||||||||||||||||||||
| timer_oc_parameter_struct timer_ocinitpara; | ||||||||||||||||||||||
| timer_parameter_struct timer_initpara; | ||||||||||||||||||||||
| dma_parameter_struct dma_initpara; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| // NVIC Settings | ||||||||||||||||||||||
| NVIC_SetPriority(DMA_Channel3_4_IRQn, 1); | ||||||||||||||||||||||
| NVIC_EnableIRQ(DMA_Channel3_4_IRQn); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| // Periph Clocks settings | ||||||||||||||||||||||
| rcu_periph_clock_enable(RCU_DMA); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| #ifdef LED_USES_PB4 | ||||||||||||||||||||||
| rcu_periph_clock_enable(RCU_TIMER2); | ||||||||||||||||||||||
| rcu_periph_clock_enable(RCU_GPIOB); | ||||||||||||||||||||||
| #endif | ||||||||||||||||||||||
| #ifdef LED_USES_PA2 | ||||||||||||||||||||||
| rcu_periph_clock_enable(RCU_TIMER14); | ||||||||||||||||||||||
|
Comment on lines
+86
to
+90
|
||||||||||||||||||||||
| rcu_periph_clock_enable(RCU_TIMER2); | |
| rcu_periph_clock_enable(RCU_GPIOB); | |
| #endif | |
| #ifdef LED_USES_PA2 | |
| rcu_periph_clock_enable(RCU_TIMER14); | |
| rcu_periph_clock_enable(RCU_TIMER14); | |
| rcu_periph_clock_enable(RCU_GPIOB); | |
| #endif | |
| #ifdef LED_USES_PA2 | |
| rcu_periph_clock_enable(RCU_TIMER2); |
Copilot
AI
Nov 4, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comments incorrectly map pin-to-timer relationships. According to the code at lines 86 and 90, PA2 uses TIMER14 and PB4 uses TIMER2, but the comments state the opposite. The comments should read: 'PA2 --> TIMER14' and 'PB4 --> TIMER2'.
| //PA2 --> TIM14 | |
| //PB4 --> TIM2 | |
| //PA2 --> TIMER14 | |
| //PB4 --> TIMER2 |
Copilot
AI
Nov 4, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing space after #ifdef LED_USES_PB4 before the comment delimiter. Should be #ifdef LED_USES_PB4 //.
Copilot
AI
Nov 4, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing space after the conditional directive. Should be '#ifdef LED_USES_PB4 //' for better readability.
| #ifdef LED_USES_PB4// that means the esc is using PA2 as signal pin, so we configure PB4 as ws2812 data pin | |
| #ifdef LED_USES_PB4 // that means the esc is using PA2 as signal pin, so we configure PB4 as ws2812 data pin |
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -24,6 +24,7 @@ uint16_t interrupt_time = 0; | |||||||||||||
| #include "main.h" | ||||||||||||||
| #include "systick.h" | ||||||||||||||
| #include "targets.h" | ||||||||||||||
| #include "WS2812.h" | ||||||||||||||
|
Comment on lines
26
to
+27
|
||||||||||||||
| #include "targets.h" | |
| #include "WS2812.h" | |
| #include "targets.h" | |
| #ifdef USE_LED_STRIP | |
| #include "WS2812.h" | |
| #endif |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,10 @@ | |
| #include "functions.h" | ||
| #include "serial_telemetry.h" | ||
| #include "targets.h" | ||
| #ifdef USE_LED_STRIP | ||
| #include "WS2812.h" | ||
| #endif | ||
|
|
||
|
|
||
| void initCorePeripherals(void) | ||
| { | ||
|
|
@@ -34,6 +38,9 @@ void initCorePeripherals(void) | |
| #ifdef USE_RGB_LED | ||
| LED_GPIO_init(); | ||
| #endif | ||
| #ifdef USE_LED_STRIP | ||
| WS2812_Init(); | ||
| #endif | ||
| #ifdef USE_SERIAL_TELEMETRY | ||
| telem_UART_Init(); | ||
| #endif | ||
|
|
@@ -238,8 +245,6 @@ void MX_DMA_Init(void) | |
| NVIC_SetPriority(DMA_Channel1_2_IRQn, 3); | ||
| NVIC_EnableIRQ(DMA_Channel1_2_IRQn); | ||
|
|
||
| // NVIC_SetPriority(DMA_Channel3_4_IRQn, 1); | ||
| // NVIC_EnableIRQ(DMA_Channel3_4_IRQn); | ||
| } | ||
|
|
||
| void MX_GPIO_Init(void) { } | ||
|
|
@@ -270,10 +275,16 @@ void UN_TIM_Init(void) | |
|
|
||
| NVIC_SetPriority(IC_DMA_IRQ_NAME, 1); | ||
| NVIC_EnableIRQ(IC_DMA_IRQ_NAME); | ||
| #ifdef LED_USES_PA2 | ||
| rcu_periph_clock_enable(RCU_TIMER2); | ||
| rcu_periph_clock_enable(RCU_TIMER14); | ||
| TIMER_CAR(TIMER2) = 0xFFFF; | ||
| TIMER_PSC(TIMER2) = 10; | ||
| #endif | ||
| #ifdef LED_USES_PB4 | ||
| rcu_periph_clock_enable(RCU_TIMER14); | ||
| TIMER_CAR(TIMER14) = 0xFFFF; | ||
| TIMER_PSC(TIMER14) = 10; | ||
| #endif | ||
|
Comment on lines
+278
to
+287
|
||
| /* enable a TIMER */ | ||
|
|
||
| // LL_TIM_DisableARRPreload(IC_TIMER_REGISTER); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Corrected spelling of 'blog' to 'below' in timing comments.