![](/media/cache/group/ST_logo_2020_blue_V_cmyk.jpg.50x50_q85.jpg)
Simple test application for the STMicroelectronics X-NUCLEO-LED61A1 LED Control Expansion Board.
Dependencies: X_NUCLEO_LED61A1 mbed
Fork of HelloWorld_LED61A1 by
LED Control with the X-NUCLEO-LED61A1 Expansion Board
This application provides a simple example of usage of the X-NUCLEO-LED61A1 LED Control Expansion Board.
It shows how to control a LED stripe load connected to the board by means of a sinusoidal wave form injected into the PWM dimming control pin.
main.cpp@19:4b259f97c1e3, 2017-10-17 (annotated)
- Committer:
- Davidroid
- Date:
- Tue Oct 17 13:03:29 2017 +0000
- Revision:
- 19:4b259f97c1e3
- Parent:
- 18:bef2ec6d10ab
Updating mbed library. Minor changes.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Davidroid | 0:f5a41692e1fb | 1 | /** |
Davidroid | 0:f5a41692e1fb | 2 | ****************************************************************************** |
Davidroid | 0:f5a41692e1fb | 3 | * @file main.cpp |
Davidroid | 6:899828fd9d9d | 4 | * @author Davide Aliprandi, STMicroelectronics |
Davidroid | 0:f5a41692e1fb | 5 | * @version V1.0.0 |
Davidroid | 7:7a608b07fa9f | 6 | * @date February 4h, 2016 |
Davidroid | 6:899828fd9d9d | 7 | * @brief mbed test application for the STMicroelectronics X-NUCLEO-LED61A1 |
Davidroid | 0:f5a41692e1fb | 8 | * LED expansion board. |
Davidroid | 0:f5a41692e1fb | 9 | ****************************************************************************** |
Davidroid | 0:f5a41692e1fb | 10 | * @attention |
Davidroid | 0:f5a41692e1fb | 11 | * |
Davidroid | 0:f5a41692e1fb | 12 | * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2> |
Davidroid | 0:f5a41692e1fb | 13 | * |
Davidroid | 0:f5a41692e1fb | 14 | * Redistribution and use in source and binary forms, with or without modification, |
Davidroid | 0:f5a41692e1fb | 15 | * are permitted provided that the following conditions are met: |
Davidroid | 0:f5a41692e1fb | 16 | * 1. Redistributions of source code must retain the above copyright notice, |
Davidroid | 0:f5a41692e1fb | 17 | * this list of conditions and the following disclaimer. |
Davidroid | 0:f5a41692e1fb | 18 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
Davidroid | 0:f5a41692e1fb | 19 | * this list of conditions and the following disclaimer in the documentation |
Davidroid | 0:f5a41692e1fb | 20 | * and/or other materials provided with the distribution. |
Davidroid | 0:f5a41692e1fb | 21 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
Davidroid | 0:f5a41692e1fb | 22 | * may be used to endorse or promote products derived from this software |
Davidroid | 0:f5a41692e1fb | 23 | * without specific prior written permission. |
Davidroid | 0:f5a41692e1fb | 24 | * |
Davidroid | 0:f5a41692e1fb | 25 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
Davidroid | 0:f5a41692e1fb | 26 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
Davidroid | 0:f5a41692e1fb | 27 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
Davidroid | 0:f5a41692e1fb | 28 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
Davidroid | 0:f5a41692e1fb | 29 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
Davidroid | 0:f5a41692e1fb | 30 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
Davidroid | 0:f5a41692e1fb | 31 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
Davidroid | 0:f5a41692e1fb | 32 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
Davidroid | 0:f5a41692e1fb | 33 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
Davidroid | 0:f5a41692e1fb | 34 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Davidroid | 0:f5a41692e1fb | 35 | * |
Davidroid | 0:f5a41692e1fb | 36 | ****************************************************************************** |
Davidroid | 0:f5a41692e1fb | 37 | */ |
Davidroid | 0:f5a41692e1fb | 38 | |
Davidroid | 0:f5a41692e1fb | 39 | |
Davidroid | 0:f5a41692e1fb | 40 | /* Includes ------------------------------------------------------------------*/ |
Davidroid | 0:f5a41692e1fb | 41 | |
Davidroid | 0:f5a41692e1fb | 42 | /* mbed specific header files. */ |
Davidroid | 0:f5a41692e1fb | 43 | #include "mbed.h" |
Davidroid | 0:f5a41692e1fb | 44 | |
Davidroid | 1:1c01b873ad45 | 45 | /* Component specific header files. */ |
Davidroid | 16:0d6f96345c56 | 46 | #include "Led6001.h" |
Davidroid | 1:1c01b873ad45 | 47 | |
Davidroid | 1:1c01b873ad45 | 48 | |
Davidroid | 1:1c01b873ad45 | 49 | /* Definitions ---------------------------------------------------------------*/ |
Davidroid | 1:1c01b873ad45 | 50 | |
Davidroid | 3:ed347127fc38 | 51 | /* PI. */ |
Davidroid | 3:ed347127fc38 | 52 | #ifndef M_PI |
Davidroid | 14:c1a43abb901f | 53 | #define M_PI (3.14159265358979323846f) |
Davidroid | 3:ed347127fc38 | 54 | #endif |
Davidroid | 3:ed347127fc38 | 55 | |
Davidroid | 2:dbd596540d46 | 56 | /* Loop period in micro-seconds. */ |
Davidroid | 14:c1a43abb901f | 57 | #define LOOP_PERIOD_us (5E5) /* 0.5 seconds. */ |
Davidroid | 3:ed347127fc38 | 58 | |
Davidroid | 3:ed347127fc38 | 59 | /* Sin period in micro-seconds. */ |
Davidroid | 19:4b259f97c1e3 | 60 | #define LED_SIN_PERIOD_us (1E7) /* 10 seconds. */ |
Davidroid | 1:1c01b873ad45 | 61 | |
Davidroid | 1:1c01b873ad45 | 62 | |
Davidroid | 1:1c01b873ad45 | 63 | /* Variables -----------------------------------------------------------------*/ |
Davidroid | 1:1c01b873ad45 | 64 | |
Davidroid | 2:dbd596540d46 | 65 | /* Main loop's ticker. */ |
Davidroid | 2:dbd596540d46 | 66 | static Ticker ticker; |
Davidroid | 1:1c01b873ad45 | 67 | |
Davidroid | 2:dbd596540d46 | 68 | /* LED Control Component. */ |
Davidroid | 16:0d6f96345c56 | 69 | Led6001 *led; |
Davidroid | 2:dbd596540d46 | 70 | |
Davidroid | 2:dbd596540d46 | 71 | /* Interrupt flags. */ |
Davidroid | 14:c1a43abb901f | 72 | static volatile bool ticker_irq_triggered = false; |
Davidroid | 14:c1a43abb901f | 73 | static volatile bool xfault_irq_triggered = false; |
Davidroid | 1:1c01b873ad45 | 74 | |
Davidroid | 1:1c01b873ad45 | 75 | |
Davidroid | 1:1c01b873ad45 | 76 | /* Functions -----------------------------------------------------------------*/ |
Davidroid | 1:1c01b873ad45 | 77 | |
Davidroid | 1:1c01b873ad45 | 78 | /** |
Davidroid | 7:7a608b07fa9f | 79 | * @brief Handling the LED capabilities. |
Davidroid | 2:dbd596540d46 | 80 | * @param None. |
Davidroid | 2:dbd596540d46 | 81 | * @retval None. |
Davidroid | 1:1c01b873ad45 | 82 | */ |
Davidroid | 16:0d6f96345c56 | 83 | void led_handler(void) |
Davidroid | 1:1c01b873ad45 | 84 | { |
Davidroid | 3:ed347127fc38 | 85 | static int tick = 0; |
Davidroid | 3:ed347127fc38 | 86 | |
Davidroid | 7:7a608b07fa9f | 87 | /* Handling the LED dimming when powered ON. */ |
Davidroid | 19:4b259f97c1e3 | 88 | float dimming = 0.5f * sin(2 * M_PI * (tick++ * LOOP_PERIOD_us) / LED_SIN_PERIOD_us) + 0.5f; |
Davidroid | 19:4b259f97c1e3 | 89 | tick %= (int) (LED_SIN_PERIOD_us / LOOP_PERIOD_us); |
Davidroid | 3:ed347127fc38 | 90 | |
Davidroid | 7:7a608b07fa9f | 91 | /* Printing to the console. */ |
Davidroid | 19:4b259f97c1e3 | 92 | printf("Sinusoidal PWM Dimming --> %0.2f\r", dimming); |
Davidroid | 2:dbd596540d46 | 93 | |
Davidroid | 7:7a608b07fa9f | 94 | /* |
Davidroid | 7:7a608b07fa9f | 95 | Writing PWM dimming values to the LED. |
Davidroid | 3:ed347127fc38 | 96 | |
Davidroid | 7:7a608b07fa9f | 97 | Notes: |
Davidroid | 19:4b259f97c1e3 | 98 | + Use "set_pwm_dimming()" for a PWM control, or "set_analog_dimming()" |
Davidroid | 19:4b259f97c1e3 | 99 | for an analog control. |
Davidroid | 7:7a608b07fa9f | 100 | */ |
Davidroid | 19:4b259f97c1e3 | 101 | led->set_analog_dimming(dimming); |
Davidroid | 2:dbd596540d46 | 102 | } |
Davidroid | 2:dbd596540d46 | 103 | |
Davidroid | 2:dbd596540d46 | 104 | /** |
Davidroid | 2:dbd596540d46 | 105 | * @brief Interrupt Request for the main loop's ticker related interrupt. |
Davidroid | 2:dbd596540d46 | 106 | * @param None. |
Davidroid | 2:dbd596540d46 | 107 | * @retval None. |
Davidroid | 2:dbd596540d46 | 108 | */ |
Davidroid | 16:0d6f96345c56 | 109 | void ticker_irq(void) |
Davidroid | 2:dbd596540d46 | 110 | { |
Davidroid | 2:dbd596540d46 | 111 | ticker_irq_triggered = true; |
Davidroid | 2:dbd596540d46 | 112 | } |
Davidroid | 2:dbd596540d46 | 113 | |
Davidroid | 2:dbd596540d46 | 114 | /** |
Davidroid | 2:dbd596540d46 | 115 | * @brief Interrupt Request for the component's XFAULT interrupt. |
Davidroid | 2:dbd596540d46 | 116 | * @param None. |
Davidroid | 2:dbd596540d46 | 117 | * @retval None. |
Davidroid | 1:1c01b873ad45 | 118 | */ |
Davidroid | 16:0d6f96345c56 | 119 | void xfault_irq(void) |
Davidroid | 1:1c01b873ad45 | 120 | { |
Davidroid | 1:1c01b873ad45 | 121 | xfault_irq_triggered = true; |
Davidroid | 16:0d6f96345c56 | 122 | led->disable_xfault_irq(); |
Davidroid | 1:1c01b873ad45 | 123 | } |
Davidroid | 1:1c01b873ad45 | 124 | |
Davidroid | 1:1c01b873ad45 | 125 | /** |
Davidroid | 2:dbd596540d46 | 126 | * @brief Interrupt Handler for the component's XFAULT interrupt. |
Davidroid | 2:dbd596540d46 | 127 | * @param None. |
Davidroid | 2:dbd596540d46 | 128 | * @retval None. |
Davidroid | 2:dbd596540d46 | 129 | */ |
Davidroid | 16:0d6f96345c56 | 130 | void xfault_handler(void) |
Davidroid | 2:dbd596540d46 | 131 | { |
Davidroid | 2:dbd596540d46 | 132 | /* Printing to the console. */ |
Davidroid | 14:c1a43abb901f | 133 | printf("XFAULT Interrupt detected! Re-initializing LED driver..."); |
Davidroid | 2:dbd596540d46 | 134 | |
Davidroid | 14:c1a43abb901f | 135 | /* Re-starting-up LED Control Component. */ |
Davidroid | 16:0d6f96345c56 | 136 | led->start_up(); |
Davidroid | 14:c1a43abb901f | 137 | |
Davidroid | 14:c1a43abb901f | 138 | /* Printing to the console. */ |
Davidroid | 14:c1a43abb901f | 139 | printf("Done.\r\n\n"); |
Davidroid | 2:dbd596540d46 | 140 | |
Davidroid | 16:0d6f96345c56 | 141 | led->enable_xfault_irq(); |
Davidroid | 2:dbd596540d46 | 142 | } |
Davidroid | 2:dbd596540d46 | 143 | |
Davidroid | 0:f5a41692e1fb | 144 | |
Davidroid | 0:f5a41692e1fb | 145 | /* Main ----------------------------------------------------------------------*/ |
Davidroid | 0:f5a41692e1fb | 146 | |
Davidroid | 0:f5a41692e1fb | 147 | int main() |
Davidroid | 0:f5a41692e1fb | 148 | { |
Davidroid | 0:f5a41692e1fb | 149 | /*----- Initialization. -----*/ |
Davidroid | 0:f5a41692e1fb | 150 | |
Davidroid | 2:dbd596540d46 | 151 | /* Printing to the console. */ |
Davidroid | 7:7a608b07fa9f | 152 | printf("LED Control Application Example\r\n\n"); |
Davidroid | 2:dbd596540d46 | 153 | |
Davidroid | 1:1c01b873ad45 | 154 | /* Initializing LED Control Component. */ |
Davidroid | 16:0d6f96345c56 | 155 | led = new Led6001(D4, A3, D6, D5); |
Davidroid | 18:bef2ec6d10ab | 156 | if (led->init() != COMPONENT_OK) { |
Davidroid | 1:1c01b873ad45 | 157 | exit(EXIT_FAILURE); |
Davidroid | 18:bef2ec6d10ab | 158 | } |
Davidroid | 1:1c01b873ad45 | 159 | |
Davidroid | 1:1c01b873ad45 | 160 | /* Attaching interrupt request functions. */ |
Davidroid | 16:0d6f96345c56 | 161 | led->attach_xfault_irq(&xfault_irq); |
Davidroid | 16:0d6f96345c56 | 162 | led->enable_xfault_irq(); |
Davidroid | 16:0d6f96345c56 | 163 | ticker.attach_us(ticker_irq, LOOP_PERIOD_us); |
Davidroid | 1:1c01b873ad45 | 164 | |
Davidroid | 14:c1a43abb901f | 165 | /* Starting-up LED Control Component. */ |
Davidroid | 16:0d6f96345c56 | 166 | led->start_up(); |
Davidroid | 0:f5a41692e1fb | 167 | |
Davidroid | 0:f5a41692e1fb | 168 | |
Davidroid | 0:f5a41692e1fb | 169 | /*----- LED Control. -----*/ |
Davidroid | 1:1c01b873ad45 | 170 | |
Davidroid | 2:dbd596540d46 | 171 | /* Either performing the component handler, interrupt handlers, or waiting for events. */ |
Davidroid | 18:bef2ec6d10ab | 172 | while (true) { |
Davidroid | 18:bef2ec6d10ab | 173 | if (ticker_irq_triggered) { |
Davidroid | 2:dbd596540d46 | 174 | ticker_irq_triggered = false; |
Davidroid | 16:0d6f96345c56 | 175 | led_handler(); |
Davidroid | 18:bef2ec6d10ab | 176 | } else if (xfault_irq_triggered) { |
Davidroid | 1:1c01b873ad45 | 177 | xfault_irq_triggered = false; |
Davidroid | 16:0d6f96345c56 | 178 | xfault_handler(); |
Davidroid | 5:b00bbf1edfa9 | 179 | } else { |
Davidroid | 1:1c01b873ad45 | 180 | /* It is recommended that SEVONPEND in the System Control Register is NOT set. */ |
Davidroid | 1:1c01b873ad45 | 181 | __WFE(); |
Davidroid | 1:1c01b873ad45 | 182 | } |
Davidroid | 1:1c01b873ad45 | 183 | } |
Davidroid | 1:1c01b873ad45 | 184 | } |