-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathws2812b_hal_pwm.h
136 lines (114 loc) · 4.37 KB
/
ws2812b_hal_pwm.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/**
*******************************************************************************
* @file: ws2812b_hal_pwm.h
* @brief: WS2812B functions: abstracting STM32 HAL: PWM.
*******************************************************************************
* @note:
* Based on Phil’s Lab "STM32 + RGB LEDs Firmware Tutorial (TIM + DMA) - Phil's
* Lab #136" (https://youtu.be/MqbJTj0Cw6o).
*******************************************************************************
*/
#ifndef NERVE__WS2812B_HAL_PWM_H
#define NERVE__WS2812B_HAL_PWM_H
/** Includes. *****************************************************************/
#include "stm32f4xx_hal.h"
/** STM32 port and pin configs. ***********************************************/
extern TIM_HandleTypeDef htim1;
// PWM timer channel.
#define WS2812B_TIM htim1
#define WS2812B_TIM_CHANNEL TIM_CHANNEL_1
/** Definitions. **************************************************************/
#define LED_COUNT 4
// Based on 180 MHz peripheral clock at 9-1 prescaler and 25-1 ARR with time
// period of 1.25 us. See README.md calculations.
#define WS2812B_LO_VAL_DUTY 8 // 0.4 us.
#define WS2812B_HI_VAL_DUTY 16 // 0.8 us.
#define WS2812B_RST_VAL_PERIODS 40 // 50 us.
#define WS2812B_BITS_PER_LED 24 // (8 * 3 = 24) 1 byte per R, G and B value.
#define WS2812B_DMA_BUF_LEN \
((uint16_t)((LED_COUNT * WS2812B_BITS_PER_LED) + WS2812B_RST_VAL_PERIODS))
/** Public structs. ***********************************************************/
/**
* @brief Union for representing WS2812B LED color data.
*
* This union is designed to store and manipulate the color data for a single
* WS2812B LED. The color data can be accessed either as individual RGB
* components (red, green, blue) or as a combined 32-bit value.
*
* The structure within the union allows for easy manipulation of the red,
* green, and blue color channels individually, while the `data` field provides
* a way to handle the entire 24-bit color value as a single unit.
*
* The bit order in the `data` field is:
* - G7, G6, ..., G0 (Green channel, most to least significant bit).
* - R7, R6, ..., R0 (Red channel, most to least significant bit).
* - B7, B6, ..., B0 (Blue channel, most to least significant bit).
*
* Usage example:
* @code
* WS2812B_LED_DATA_RGB led;
*
* // Set individual color components.
* led.colour.r = 255; // Max red intensity.
* led.colour.g = 128; // Half green intensity.
* led.colour.b = 64; // Quarter blue intensity.
*
* // Alternatively, set the entire color using the data field.
* led.data = 0xFF8040; // RGB color: (255, 128, 64).
* @endcode
*/
typedef union {
struct {
uint8_t g;
uint8_t r;
uint8_t b;
} colour;
uint32_t data;
} ws2812b_led_data_t;
/** Public variables. *********************************************************/
extern ws2812b_led_data_t led_data[LED_COUNT]; // Stores individual LED data.
extern uint16_t dma_buffer[WS2812B_DMA_BUF_LEN]; // Stores duty cycle values.
extern volatile uint8_t dma_complete_flag; // Flag for DMA send complete.
/** Public functions. *********************************************************/
/**
* @brief Initializes the WS2812B LED control system.
* @retval HAL_StatusTypeDef: Returns the status of the initialization process.
*/
HAL_StatusTypeDef ws2812b_init();
/**
* @brief Sets the color of a specific WS2812B LED.
* @param index: The index of the LED in the array (0-based index).
* @param r: The red component of the color (0-255).
* @param g: The green component of the color (0-255).
* @param b: The blue component of the color (0-255).
* @retval None
*
* Example Usage:
* @code
* // Set the first LED to bright red.
* WS2812B_Set_Colour(0, 255, 0, 0);
*
* // Set the second LED to green.
* WS2812B_Set_Colour(1, 0, 255, 0);
* @endcode
*/
void ws2812b_set_colour(uint8_t index, uint8_t r, uint8_t g, uint8_t b);
/**
* @brief Updates the WS2812B LEDs with the latest color data.
* @retval HAL_StatusTypeDef: Returns the status of the operation.
*/
HAL_StatusTypeDef ws2812b_update();
/**
* @brief Callback function to handle the completion of WS2812B data transfer.
*
* Example usage within `HAL_TIM_PWM_PulseFinishedCallback()`:
* @code
* void HAL_TIM_PWM_PulseFinishedCallback(TIM_HandleTypeDef *htim) {
* ws2812b_callback();
* }
* @endcode
*
* @retval None
*/
void ws2812b_callback(void);
#endif