Skip to content

Commit e2ec3cd

Browse files
authored
Merge pull request #14831 from jeromecoutant/PR_DISCO_WB_LED
DISCO_WB5MMG: add RGB LED
2 parents efaf159 + 6852932 commit e2ec3cd

File tree

2 files changed

+152
-1
lines changed

2 files changed

+152
-1
lines changed

targets/TARGET_STM/TARGET_STM32WB/TARGET_STM32WB5MxG/TARGET_DISCO_WB5MMG/PinNames.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@
2727
extern "C" {
2828
#endif
2929

30+
void rgb_led_on(uint8_t RedValue, uint8_t GreenValue, uint8_t BlueValue);
31+
void rgb_led_off(void);
32+
void rgb_led_red(void);
33+
void rgb_led_green(void);
34+
void rgb_led_blue(void);
35+
36+
3037
typedef enum {
3138
ALT0 = 0x100,
3239
} ALTx;
@@ -185,10 +192,13 @@ typedef enum {
185192
} PinName;
186193

187194
// Standardized LED and button names
188-
#define LED1 PA_7 // LD4 is a RGB LED connected to a PWM LED driver // TODO
189195
#define BUTTON1 PC_12
190196
#define BUTTON2 PC_13
191197

198+
// RBG LED
199+
#define RGB_LED PA_7 // LD4 is a RGB LED connected to a PWM LED driver
200+
#define GPIO_SELECT2 PH_1 // See User Manual
201+
192202
#ifdef __cplusplus
193203
}
194204
#endif
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
/* mbed Microcontroller Library
2+
* SPDX-License-Identifier: BSD-3-Clause
3+
******************************************************************************
4+
*
5+
* Copyright (c) 2021 STMicroelectronics.
6+
* All rights reserved.
7+
*
8+
* This software component is licensed by ST under BSD 3-Clause license,
9+
* the "License"; You may not use this file except in compliance with the
10+
* License. You may obtain a copy of the License at:
11+
* opensource.org/licenses/BSD-3-Clause
12+
*
13+
******************************************************************************
14+
*/
15+
16+
/* application example
17+
18+
#include "mbed.h"
19+
20+
int main()
21+
{
22+
while (true) {
23+
rgb_led_off();
24+
ThisThread::sleep_for(2s);
25+
26+
rgb_led_red();
27+
ThisThread::sleep_for(2s);
28+
29+
rgb_led_green();
30+
ThisThread::sleep_for(2s);
31+
32+
rgb_led_blue();
33+
ThisThread::sleep_for(2s);
34+
35+
rgb_led_on(0x41, 0x41, 0); // yellow
36+
ThisThread::sleep_for(2s);
37+
38+
rgb_led_on(0x41, 0, 0x41); // magenta
39+
ThisThread::sleep_for(2s);
40+
41+
rgb_led_on(0, 0x41, 0x41); // cyan
42+
ThisThread::sleep_for(2s);
43+
44+
rgb_led_on(0x41, 0x41, 0x41); // white
45+
ThisThread::sleep_for(2s);
46+
}
47+
}
48+
*/
49+
50+
#include "drivers/DigitalOut.h"
51+
#include "platform/mbed_wait_api.h"
52+
53+
54+
#define DELAY 1
55+
#define T_CYCLE_0 4
56+
#define T_CYCLE_1 1
57+
#define WRITE_COMMAND 0x3A
58+
59+
#define rgb_led_wait(NbCycles) wait_us(NbCycles * 10)
60+
61+
62+
void rgb_led_send_bit(uint8_t bit)
63+
{
64+
mbed::DigitalOut RGB_LED_SDI(RGB_LED);
65+
66+
/* Start next cycle */
67+
RGB_LED_SDI = 1;
68+
rgb_led_wait(DELAY);
69+
RGB_LED_SDI = 0;
70+
rgb_led_wait(DELAY);
71+
72+
if (bit) {
73+
RGB_LED_SDI = 1;
74+
rgb_led_wait(DELAY);
75+
RGB_LED_SDI = 0;
76+
rgb_led_wait(T_CYCLE_1);
77+
} else {
78+
rgb_led_wait(T_CYCLE_0);
79+
}
80+
}
81+
82+
83+
void rgb_led_send_byte(uint8_t byte)
84+
{
85+
rgb_led_send_bit(byte & (1 << 7));
86+
rgb_led_send_bit(byte & (1 << 6));
87+
rgb_led_send_bit(byte & (1 << 5));
88+
rgb_led_send_bit(byte & (1 << 4));
89+
rgb_led_send_bit(byte & (1 << 3));
90+
rgb_led_send_bit(byte & (1 << 2));
91+
rgb_led_send_bit(byte & (1 << 1));
92+
rgb_led_send_bit(byte & (1 << 0));
93+
}
94+
95+
96+
void rgb_led_on(uint8_t RedValue, uint8_t GreenValue, uint8_t BlueValue)
97+
{
98+
mbed::DigitalOut RGB_SELECT(GPIO_SELECT2);
99+
mbed::DigitalOut RGB_LED_SDI(RGB_LED);
100+
101+
RGB_SELECT = 1;
102+
103+
/* TCycle measurement sequence */
104+
RGB_LED_SDI = 1;
105+
rgb_led_wait(DELAY);
106+
RGB_LED_SDI = 0;
107+
rgb_led_wait(T_CYCLE_0);
108+
109+
/* Write command */
110+
rgb_led_send_byte(WRITE_COMMAND);
111+
112+
/* Write the GS data */
113+
rgb_led_send_byte(RedValue);
114+
rgb_led_send_byte(GreenValue);
115+
rgb_led_send_byte(BlueValue);
116+
117+
rgb_led_wait(T_CYCLE_0 * 2);
118+
119+
RGB_SELECT = 0;
120+
}
121+
122+
void rgb_led_off(void)
123+
{
124+
rgb_led_on(0, 0, 0);
125+
}
126+
127+
void rgb_led_red(void)
128+
{
129+
rgb_led_on(0x41, 0, 0);
130+
}
131+
132+
void rgb_led_green(void)
133+
{
134+
rgb_led_on(0, 0x41, 0);
135+
}
136+
137+
void rgb_led_blue(void)
138+
{
139+
rgb_led_on(0, 0, 0x41);
140+
}
141+

0 commit comments

Comments
 (0)