Skip to content
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

add l431 ws2812 support #136

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions Inc/targets.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,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 @@ -106,6 +110,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 Expand Up @@ -3408,8 +3415,6 @@
#define PHASE_B_COMP LL_COMP_INPUT_MINUS_IO5 // pa5
#define PHASE_C_COMP LL_COMP_INPUT_MINUS_IO4 // pa4
#define COMMON_COMP LL_COMP_INPUT_PLUS_IO1
//#define USE_LED_STRIP
//#define WS2812_PIN LL_GPIO_PIN_3

#define CURRENT_ADC_CHANNEL LL_ADC_CHANNEL_8
#define VOLTAGE_ADC_CHANNEL LL_ADC_CHANNEL_11
Expand Down
10 changes: 10 additions & 0 deletions Mcu/l431/Inc/WS2812.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef INC_WS2812_H_
#define INC_WS2812_H_

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

void WS2812_Init(void);
void send_LED_RGB(uint8_t red, uint8_t green, uint8_t blue);

#endif /* INC_WS2812_H_ */
70 changes: 70 additions & 0 deletions Mcu/l431/Src/WS2812.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#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

static 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;
}

static void disableDWT_CycleCounter(void)
{
// Disable the cycle counter
DWT->CTRL &= ~DWT_CTRL_CYCCNTENA_Msk;
// Disable TRCENA in the DEMCR
CoreDebug->DEMCR &= ~CoreDebug_DEMCR_TRCENA_Msk;
}

static 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
}
}

static 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();
enableDWT_CycleCounter();
uint32_t twenty_four_bit_color_number = green << 16 | red << 8 | blue;
tridge marked this conversation as resolved.
Show resolved Hide resolved
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
disableDWT_CycleCounter();
__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);
}

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

extern char bemf_timeout;

Expand Down Expand Up @@ -679,6 +682,7 @@ void enableCorePeripherals()
#endif

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

Expand Down
Loading