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

DISCO_WB5MMG: add RGB LED #14831

Merged
merged 1 commit into from
Jul 26, 2021
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@
extern "C" {
#endif

void rgb_led_on(uint8_t RedValue, uint8_t GreenValue, uint8_t BlueValue);
void rgb_led_off(void);
void rgb_led_red(void);
void rgb_led_green(void);
void rgb_led_blue(void);


typedef enum {
ALT0 = 0x100,
} ALTx;
Expand Down Expand Up @@ -185,10 +192,13 @@ typedef enum {
} PinName;

// Standardized LED and button names
#define LED1 PA_7 // LD4 is a RGB LED connected to a PWM LED driver // TODO
#define BUTTON1 PC_12
#define BUTTON2 PC_13

// RBG LED
#define RGB_LED PA_7 // LD4 is a RGB LED connected to a PWM LED driver
#define GPIO_SELECT2 PH_1 // See User Manual

#ifdef __cplusplus
}
#endif
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/* mbed Microcontroller Library
* SPDX-License-Identifier: BSD-3-Clause
******************************************************************************
*
* Copyright (c) 2021 STMicroelectronics.
* All rights reserved.
*
* This software component is licensed by ST under BSD 3-Clause license,
* the "License"; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
******************************************************************************
*/

/* application example

#include "mbed.h"

int main()
{
while (true) {
rgb_led_off();
ThisThread::sleep_for(2s);

rgb_led_red();
ThisThread::sleep_for(2s);

rgb_led_green();
ThisThread::sleep_for(2s);

rgb_led_blue();
ThisThread::sleep_for(2s);

rgb_led_on(0x41, 0x41, 0); // yellow
ThisThread::sleep_for(2s);

rgb_led_on(0x41, 0, 0x41); // magenta
ThisThread::sleep_for(2s);

rgb_led_on(0, 0x41, 0x41); // cyan
ThisThread::sleep_for(2s);

rgb_led_on(0x41, 0x41, 0x41); // white
ThisThread::sleep_for(2s);
}
}
*/

#include "drivers/DigitalOut.h"
#include "platform/mbed_wait_api.h"


#define DELAY 1
#define T_CYCLE_0 4
#define T_CYCLE_1 1
#define WRITE_COMMAND 0x3A

#define rgb_led_wait(NbCycles) wait_us(NbCycles * 10)


void rgb_led_send_bit(uint8_t bit)
{
mbed::DigitalOut RGB_LED_SDI(RGB_LED);

/* Start next cycle */
RGB_LED_SDI = 1;
rgb_led_wait(DELAY);
RGB_LED_SDI = 0;
rgb_led_wait(DELAY);

if (bit) {
RGB_LED_SDI = 1;
rgb_led_wait(DELAY);
RGB_LED_SDI = 0;
rgb_led_wait(T_CYCLE_1);
} else {
rgb_led_wait(T_CYCLE_0);
}
}


void rgb_led_send_byte(uint8_t byte)
{
rgb_led_send_bit(byte & (1 << 7));
rgb_led_send_bit(byte & (1 << 6));
rgb_led_send_bit(byte & (1 << 5));
rgb_led_send_bit(byte & (1 << 4));
rgb_led_send_bit(byte & (1 << 3));
rgb_led_send_bit(byte & (1 << 2));
rgb_led_send_bit(byte & (1 << 1));
rgb_led_send_bit(byte & (1 << 0));
}


void rgb_led_on(uint8_t RedValue, uint8_t GreenValue, uint8_t BlueValue)
{
mbed::DigitalOut RGB_SELECT(GPIO_SELECT2);
mbed::DigitalOut RGB_LED_SDI(RGB_LED);

RGB_SELECT = 1;

/* TCycle measurement sequence */
RGB_LED_SDI = 1;
rgb_led_wait(DELAY);
RGB_LED_SDI = 0;
rgb_led_wait(T_CYCLE_0);

/* Write command */
rgb_led_send_byte(WRITE_COMMAND);

/* Write the GS data */
rgb_led_send_byte(RedValue);
rgb_led_send_byte(GreenValue);
rgb_led_send_byte(BlueValue);

rgb_led_wait(T_CYCLE_0 * 2);

RGB_SELECT = 0;
}

void rgb_led_off(void)
{
rgb_led_on(0, 0, 0);
}

void rgb_led_red(void)
{
rgb_led_on(0x41, 0, 0);
}

void rgb_led_green(void)
{
rgb_led_on(0, 0x41, 0);
}

void rgb_led_blue(void)
{
rgb_led_on(0, 0, 0x41);
}