凌云物联网实验室ISKBoard(IoT Starter Kits Board)开发板项目源码
guowenxue
2023-04-03 32806da6f5647ac637fa7d48aa9c221b091ab35e
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/* Reference: https://blog.csdn.net/Lennon8_8/article/details/108980808
 * ws2812b.c
 *
 *  Created on: 2023年2月13日
 *      Author: Think
 */
#include <string.h>
#include <stdint.h>
#include "ws2812b.h"
#include "miscdev.h"
 
static color_t g_ColorPanel[WS2812_NUM];
 
typedef struct ws_gpio_s
{
    GPIO_TypeDef        *group;
    uint16_t             pin;
} ws_gpio_t;
 
static ws_gpio_t   ws_gpio =  /* IO pin connected to PC2 */
{
        .group = GPIOC,
        .pin   = GPIO_PIN_2,
};
 
#define ws_setpin(x) if(!x) ws_gpio.group->BRR=ws_gpio.pin; else ws_gpio.group->BSRR=ws_gpio.pin
 
/*
 * 1 NOP = 1/80MHz = 12.5ns => 100ns = 8 * NOP
 *
 * */
#define delay_200ns() { __NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP(); \
                        __NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP(); }
 
void ws2812b_init(void)
{
    GPIO_InitTypeDef GPIO_InitStruct = {0};
 
    GPIO_InitStruct.Pin = ws_gpio.pin;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; 
    GPIO_InitStruct.Pull = GPIO_NOPULL; 
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; 
 
    HAL_GPIO_Init(ws_gpio.group, &GPIO_InitStruct); 
 
    ws2812b_reset();
 
    return ;
}
 
static inline void ws2812b_WriteByte(uint8_t byte)
{
    int      i;
 
    for(i=0; i<8; i++)
    {
        if(byte & 0x80)
        {
            /* Sequence Send 1: T1H: 580ns~1.6us  T1L: 200ns~420ns:
             *
             * +--------------+         +
             * |<-   T1H    ->|<- T1L ->|
             * |              +---------+
             */
            ws_setpin(1); // T1H
            delay_us(1);
 
            ws_setpin(0); // T1L
            delay_200ns(); //200ns test okay
        }
        else
        {
            /* Sequence Send 0: T0H: 220ns~380ns  T0L: 580ns~1.6ns:
             *
             * +---------+              +
             * |<- T0H ->|<-    T0L   ->| 
             * |         +--------------+
             */
            ws_setpin(1);  // T0H
            delay_200ns(); //200ns test okay
 
            ws_setpin(0); // T0L
            delay_us(1);
        }
 
        byte<<=1;
    }
 
    return ;
}
 
void ws2812b_reset(void)
{
    /* Sequence Reset: Treset >280us:
     *
     * +              +
     * |<-  Treset  ->| 
     * +--------------+
     */
    ws_setpin(0);
    delay_us(300);
 
    memset(g_ColorPanel, 0, sizeof(g_ColorPanel));
 
    return ;
}
 
static inline void ws2812b_SendColor(color_t color)
{
    ws2812b_WriteByte(color.pixel.green);
    ws2812b_WriteByte(color.pixel.red);
    ws2812b_WriteByte(color.pixel.blue);
 
    return ;
}
 
static inline void ws2812b_Refresh(void)
{
    int      i;
 
    for(i=0; i<WS2812_NUM; i++)
    {
        ws2812b_SendColor(g_ColorPanel[i]);
    }
 
    return ;
}
 
static inline int ws2812b_SetPixel(int which, color_t color)
{
    if( which<0 || which>=WS2812_NUM )
        return -1;
 
    g_ColorPanel[which].pixel.red   = color.pixel.red;
    g_ColorPanel[which].pixel.green = color.pixel.green;
    g_ColorPanel[which].pixel.blue  = color.pixel.blue;
 
    return 0;
}
 
int ws2812b_turn(int which, color_t color)
{
    if( which<0 || which>=WS2812_NUM )
        return -1;
 
    ws2812b_SetPixel(which, color);
    ws2812b_Refresh();
 
    return 0;
}
 
void ws2812b_blink(void)
{
    int      c, i;
    color_t  colors[4] = {[0].data=PIXEL_B, [1].data=PIXEL_R, [2].data=PIXEL_G, [3].data=PIXEL_W};
    color_t  color_off = {.data = PIXEL_OFF};
 
    for(c=0; c<4; c++) /* color */
    {
        for(i=0; i<WS2812_NUM; i++)
        {
            ws2812b_turn(i, colors[c]);
            HAL_Delay(200);
            ws2812b_turn(i, (color_t)color_off);
            HAL_Delay(200);
        }
    }
}