Skip to content

Commit

Permalink
add l431 ws2812 support
Browse files Browse the repository at this point in the history
  • Loading branch information
Huibean committed Nov 29, 2024
1 parent 03ea156 commit e895aa5
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 1 deletion.
9 changes: 8 additions & 1 deletion Inc/targets.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,13 @@
#define DEAD_TIME 45
#define HARDWARE_GROUP_L4_B
#define TARGET_VOLTAGE_DIVIDER 94
#define MILLIVOLT_PER_AMP 100
#define MILLIVOLT_PER_AMP 30
#define CURRENT_OFFSET 110
#define USE_SERIAL_TELEMETRY
#define EEPROM_START_ADD (uint32_t)0x0800F800
#define USE_LED_STRIP
#define WS2812_PORT GPIOB
#define WS2812_PIN LL_GPIO_PIN_3
#endif

#ifdef VIMDRONES_L431_CAN
Expand All @@ -94,6 +98,9 @@
#define MILLIVOLT_PER_AMP 30
#define CURRENT_OFFSET 110
#define USE_SERIAL_TELEMETRY
#define USE_LED_STRIP
#define WS2812_PORT GPIOB
#define WS2812_PIN LL_GPIO_PIN_3
#endif

#ifdef VIMDRONES_NANO_L431
Expand Down
14 changes: 14 additions & 0 deletions Mcu/l431/Inc/WS2812.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef INC_WS2812_H_
#define INC_WS2812_H_

#include "main.h"
#include <stdint.h>

void enableDWT_CycleCounter(void);
void WS2812_Init(void);
void WS2812_Disable(void);
void waitClockCycles(uint16_t cycles);
void sendBit(uint8_t inbit);
void send_LED_RGB(uint8_t red, uint8_t green, uint8_t blue);

#endif /* INC_WS2812_H_ */
61 changes: 61 additions & 0 deletions Mcu/l431/Src/WS2812.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include "WS2812.h"

#include "targets.h"

#ifdef USE_LED_STRIP

#ifndef WS2812_PORT
#error "WS2812_PORT is not defined"
#endif

#ifndef WS2812_PIN
#error "WS2812_PIN is not defined"
#endif

void enableDWT_CycleCounter(void)
{
// Enable TRCENA in the DEMCR (Debug Exception and Monitor Control Register)
CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
// Enable the cycle counter
DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;
// Reset the cycle counter
DWT->CYCCNT = 0;
}

void waitClockCycles(uint16_t cycles)
{
DWT->CYCCNT = 0; // Reset the cycle counter
while (DWT->CYCCNT < cycles) {
// Wait until the specified number of cycles has passed
}
}

void sendBit(uint8_t inbit)
{
WS2812_PORT->BSRR = WS2812_PIN;
waitClockCycles(CPU_FREQUENCY_MHZ >> (2 - inbit));
WS2812_PORT->BRR = WS2812_PIN;
waitClockCycles(CPU_FREQUENCY_MHZ >> (1 + inbit));
}

void send_LED_RGB(uint8_t red, uint8_t green, uint8_t blue)
{
__disable_irq();
uint32_t twenty_four_bit_color_number = green << 16 | red << 8 | blue;
for (int i = 0; i < 24; i++) {
sendBit((twenty_four_bit_color_number >> (23 - i)) & 1);
}
WS2812_PORT->BSRR = (1 << (WS2812_PIN + 16)); // Set pin low by writing to BSRR high half
__enable_irq();
}

void WS2812_Init(void)
{
LL_GPIO_SetPinMode(WS2812_PORT, WS2812_PIN, LL_GPIO_MODE_OUTPUT);
LL_GPIO_SetPinSpeed(WS2812_PORT, WS2812_PIN, LL_GPIO_SPEED_FREQ_HIGH);
LL_GPIO_SetPinOutputType(WS2812_PORT, WS2812_PIN, LL_GPIO_OUTPUT_PUSHPULL);
LL_GPIO_SetPinPull(WS2812_PORT, WS2812_PIN, LL_GPIO_PULL_NO);
enableDWT_CycleCounter();
}

#endif
2 changes: 2 additions & 0 deletions Mcu/l431/Src/peripherals.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "ADC.h"
#include "serial_telemetry.h"
#include "targets.h"
#include "WS2812.h"

extern char bemf_timeout;

Expand Down Expand Up @@ -702,6 +703,7 @@ void enableCorePeripherals()
#endif

#ifdef USE_LED_STRIP
WS2812_Init();
send_LED_RGB(255, 0, 0);
#endif

Expand Down

0 comments on commit e895aa5

Please sign in to comment.