![](/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@14:c1a43abb901f, 2016-03-14 (annotated)
- Committer:
- Davidroid
- Date:
- Mon Mar 14 10:09:40 2016 +0000
- Revision:
- 14:c1a43abb901f
- Parent:
- 13:97083451e538
- Child:
- 15:dd6dc0e07402
+ Moved starting-up sequence into the driver.
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 | 1:1c01b873ad45 | 46 | #include "led6001_class.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 | 14:c1a43abb901f | 60 | #define PWM_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 | 2:dbd596540d46 | 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 | 2:dbd596540d46 | 83 | void LEDHandler(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 | 14:c1a43abb901f | 88 | float pwm_dimming = 0.5f * sin(2 * M_PI * (tick++ * LOOP_PERIOD_us) / PWM_SIN_PERIOD_us) + 0.5f; |
Davidroid | 7:7a608b07fa9f | 89 | tick %= (int) (PWM_SIN_PERIOD_us / LOOP_PERIOD_us); |
Davidroid | 3:ed347127fc38 | 90 | |
Davidroid | 7:7a608b07fa9f | 91 | /* Printing to the console. */ |
Davidroid | 7:7a608b07fa9f | 92 | printf("Sinusoidal PWM Dimming --> %0.2f\r", pwm_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 | 7:7a608b07fa9f | 98 | + Replace "SetPWMDimming()" with "SetAnalogDimming()" for an analog control. |
Davidroid | 7:7a608b07fa9f | 99 | */ |
Davidroid | 7:7a608b07fa9f | 100 | led->SetPWMDimming(pwm_dimming); |
Davidroid | 2:dbd596540d46 | 101 | } |
Davidroid | 2:dbd596540d46 | 102 | |
Davidroid | 2:dbd596540d46 | 103 | /** |
Davidroid | 2:dbd596540d46 | 104 | * @brief Interrupt Request for the main loop's ticker related interrupt. |
Davidroid | 2:dbd596540d46 | 105 | * @param None. |
Davidroid | 2:dbd596540d46 | 106 | * @retval None. |
Davidroid | 2:dbd596540d46 | 107 | */ |
Davidroid | 2:dbd596540d46 | 108 | void TickerIRQ(void) |
Davidroid | 2:dbd596540d46 | 109 | { |
Davidroid | 2:dbd596540d46 | 110 | ticker_irq_triggered = true; |
Davidroid | 2:dbd596540d46 | 111 | } |
Davidroid | 2:dbd596540d46 | 112 | |
Davidroid | 2:dbd596540d46 | 113 | /** |
Davidroid | 2:dbd596540d46 | 114 | * @brief Interrupt Request for the component's XFAULT interrupt. |
Davidroid | 2:dbd596540d46 | 115 | * @param None. |
Davidroid | 2:dbd596540d46 | 116 | * @retval None. |
Davidroid | 1:1c01b873ad45 | 117 | */ |
Davidroid | 1:1c01b873ad45 | 118 | void XFaultIRQ(void) |
Davidroid | 1:1c01b873ad45 | 119 | { |
Davidroid | 1:1c01b873ad45 | 120 | xfault_irq_triggered = true; |
Davidroid | 1:1c01b873ad45 | 121 | led->DisableXFaultIRQ(); |
Davidroid | 1:1c01b873ad45 | 122 | } |
Davidroid | 1:1c01b873ad45 | 123 | |
Davidroid | 1:1c01b873ad45 | 124 | /** |
Davidroid | 2:dbd596540d46 | 125 | * @brief Interrupt Handler for the component's XFAULT interrupt. |
Davidroid | 2:dbd596540d46 | 126 | * @param None. |
Davidroid | 2:dbd596540d46 | 127 | * @retval None. |
Davidroid | 2:dbd596540d46 | 128 | */ |
Davidroid | 2:dbd596540d46 | 129 | void XFaultHandler(void) |
Davidroid | 2:dbd596540d46 | 130 | { |
Davidroid | 2:dbd596540d46 | 131 | /* Printing to the console. */ |
Davidroid | 14:c1a43abb901f | 132 | printf("XFAULT Interrupt detected! Re-initializing LED driver..."); |
Davidroid | 2:dbd596540d46 | 133 | |
Davidroid | 14:c1a43abb901f | 134 | /* Re-starting-up LED Control Component. */ |
Davidroid | 14:c1a43abb901f | 135 | led->StartUp(); |
Davidroid | 14:c1a43abb901f | 136 | |
Davidroid | 14:c1a43abb901f | 137 | /* Printing to the console. */ |
Davidroid | 14:c1a43abb901f | 138 | printf("Done.\r\n\n"); |
Davidroid | 2:dbd596540d46 | 139 | |
Davidroid | 2:dbd596540d46 | 140 | led->EnableXFaultIRQ(); |
Davidroid | 2:dbd596540d46 | 141 | } |
Davidroid | 2:dbd596540d46 | 142 | |
Davidroid | 0:f5a41692e1fb | 143 | |
Davidroid | 0:f5a41692e1fb | 144 | /* Main ----------------------------------------------------------------------*/ |
Davidroid | 0:f5a41692e1fb | 145 | |
Davidroid | 0:f5a41692e1fb | 146 | int main() |
Davidroid | 0:f5a41692e1fb | 147 | { |
Davidroid | 0:f5a41692e1fb | 148 | /*----- Initialization. -----*/ |
Davidroid | 0:f5a41692e1fb | 149 | |
Davidroid | 2:dbd596540d46 | 150 | /* Printing to the console. */ |
Davidroid | 7:7a608b07fa9f | 151 | printf("LED Control Application Example\r\n\n"); |
Davidroid | 2:dbd596540d46 | 152 | |
Davidroid | 1:1c01b873ad45 | 153 | /* Initializing LED Control Component. */ |
Davidroid | 13:97083451e538 | 154 | led = new LED6001(D4, A3, D7, D5); |
Davidroid | 2:dbd596540d46 | 155 | if (led->Init() != COMPONENT_OK) |
Davidroid | 1:1c01b873ad45 | 156 | exit(EXIT_FAILURE); |
Davidroid | 1:1c01b873ad45 | 157 | |
Davidroid | 1:1c01b873ad45 | 158 | /* Attaching interrupt request functions. */ |
Davidroid | 1:1c01b873ad45 | 159 | led->AttachXFaultIRQ(&XFaultIRQ); |
Davidroid | 5:b00bbf1edfa9 | 160 | led->EnableXFaultIRQ(); |
Davidroid | 2:dbd596540d46 | 161 | ticker.attach_us(TickerIRQ, LOOP_PERIOD_us); |
Davidroid | 1:1c01b873ad45 | 162 | |
Davidroid | 14:c1a43abb901f | 163 | /* Starting-up LED Control Component. */ |
Davidroid | 14:c1a43abb901f | 164 | led->StartUp(); |
Davidroid | 0:f5a41692e1fb | 165 | |
Davidroid | 0:f5a41692e1fb | 166 | |
Davidroid | 0:f5a41692e1fb | 167 | /*----- LED Control. -----*/ |
Davidroid | 1:1c01b873ad45 | 168 | |
Davidroid | 2:dbd596540d46 | 169 | /* Either performing the component handler, interrupt handlers, or waiting for events. */ |
Davidroid | 1:1c01b873ad45 | 170 | while (true) |
Davidroid | 1:1c01b873ad45 | 171 | { |
Davidroid | 2:dbd596540d46 | 172 | if (ticker_irq_triggered) |
Davidroid | 1:1c01b873ad45 | 173 | { |
Davidroid | 2:dbd596540d46 | 174 | ticker_irq_triggered = false; |
Davidroid | 1:1c01b873ad45 | 175 | LEDHandler(); |
Davidroid | 5:b00bbf1edfa9 | 176 | } else if (xfault_irq_triggered) |
Davidroid | 1:1c01b873ad45 | 177 | { |
Davidroid | 1:1c01b873ad45 | 178 | xfault_irq_triggered = false; |
Davidroid | 1:1c01b873ad45 | 179 | XFaultHandler(); |
Davidroid | 5:b00bbf1edfa9 | 180 | } else { |
Davidroid | 1:1c01b873ad45 | 181 | /* It is recommended that SEVONPEND in the System Control Register is NOT set. */ |
Davidroid | 1:1c01b873ad45 | 182 | __WFE(); |
Davidroid | 1:1c01b873ad45 | 183 | } |
Davidroid | 1:1c01b873ad45 | 184 | } |
Davidroid | 1:1c01b873ad45 | 185 | } |