![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
striscia led
Dependencies: X_NUCLEO_LED61A1 mbed
Fork of HelloWorld_LED61A1 by
main.cpp@13:97083451e538, 2016-03-11 (annotated)
- Committer:
- Davidroid
- Date:
- Fri Mar 11 15:59:03 2016 +0000
- Revision:
- 13:97083451e538
- Parent:
- 7:7a608b07fa9f
- Child:
- 14:c1a43abb901f
+ Updated with the new version of the library.
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 | 5:b00bbf1edfa9 | 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 | 7:7a608b07fa9f | 57 | #define LOOP_PERIOD_us (5E5) /* 0.5 seconds. */ |
Davidroid | 3:ed347127fc38 | 58 | |
Davidroid | 3:ed347127fc38 | 59 | /* Sin period in micro-seconds. */ |
Davidroid | 7:7a608b07fa9f | 60 | #define PWM_SIN_PERIOD_us (1E7) /* 10 seconds. */ |
Davidroid | 5:b00bbf1edfa9 | 61 | |
Davidroid | 5:b00bbf1edfa9 | 62 | /* Duration of initialization interval in milli-seconds. */ |
Davidroid | 7:7a608b07fa9f | 63 | #define INITIALIZATION_INTERVAL_ms (2E3) /* 2 seconds. */ |
Davidroid | 1:1c01b873ad45 | 64 | |
Davidroid | 1:1c01b873ad45 | 65 | |
Davidroid | 1:1c01b873ad45 | 66 | /* Variables -----------------------------------------------------------------*/ |
Davidroid | 1:1c01b873ad45 | 67 | |
Davidroid | 2:dbd596540d46 | 68 | /* Main loop's ticker. */ |
Davidroid | 2:dbd596540d46 | 69 | static Ticker ticker; |
Davidroid | 1:1c01b873ad45 | 70 | |
Davidroid | 2:dbd596540d46 | 71 | /* LED Control Component. */ |
Davidroid | 2:dbd596540d46 | 72 | LED6001 *led; |
Davidroid | 2:dbd596540d46 | 73 | |
Davidroid | 2:dbd596540d46 | 74 | /* Interrupt flags. */ |
Davidroid | 2:dbd596540d46 | 75 | static volatile bool ticker_irq_triggered; |
Davidroid | 2:dbd596540d46 | 76 | static volatile bool xfault_irq_triggered; |
Davidroid | 2:dbd596540d46 | 77 | |
Davidroid | 7:7a608b07fa9f | 78 | /* PWM dimming values. */ |
Davidroid | 2:dbd596540d46 | 79 | static float pwm_dimming; |
Davidroid | 1:1c01b873ad45 | 80 | |
Davidroid | 1:1c01b873ad45 | 81 | |
Davidroid | 1:1c01b873ad45 | 82 | /* Functions -----------------------------------------------------------------*/ |
Davidroid | 1:1c01b873ad45 | 83 | |
Davidroid | 1:1c01b873ad45 | 84 | /** |
Davidroid | 2:dbd596540d46 | 85 | * @brief Initilizing the demo. |
Davidroid | 2:dbd596540d46 | 86 | * @param None. |
Davidroid | 2:dbd596540d46 | 87 | * @retval None. |
Davidroid | 1:1c01b873ad45 | 88 | */ |
Davidroid | 2:dbd596540d46 | 89 | void LEDInit(void) |
Davidroid | 1:1c01b873ad45 | 90 | { |
Davidroid | 5:b00bbf1edfa9 | 91 | /* Printing to the console. */ |
Davidroid | 5:b00bbf1edfa9 | 92 | printf("Initializing LED driver..."); |
Davidroid | 5:b00bbf1edfa9 | 93 | |
Davidroid | 2:dbd596540d46 | 94 | /* Initializing Interrupt flags. */ |
Davidroid | 2:dbd596540d46 | 95 | xfault_irq_triggered = false; |
Davidroid | 2:dbd596540d46 | 96 | ticker_irq_triggered = false; |
Davidroid | 1:1c01b873ad45 | 97 | |
Davidroid | 7:7a608b07fa9f | 98 | /* Initializing PWM dimming to maximum values. */ |
Davidroid | 2:dbd596540d46 | 99 | pwm_dimming = 1.0f; |
Davidroid | 5:b00bbf1edfa9 | 100 | |
Davidroid | 5:b00bbf1edfa9 | 101 | /* Start-up sequence. */ |
Davidroid | 5:b00bbf1edfa9 | 102 | led->PowerOFF(); |
Davidroid | 5:b00bbf1edfa9 | 103 | wait_ms(INITIALIZATION_INTERVAL_ms); |
Davidroid | 5:b00bbf1edfa9 | 104 | led->PowerON(); |
Davidroid | 5:b00bbf1edfa9 | 105 | |
Davidroid | 5:b00bbf1edfa9 | 106 | /* Printing to the console. */ |
Davidroid | 7:7a608b07fa9f | 107 | printf("Done.\r\n\n"); |
Davidroid | 1:1c01b873ad45 | 108 | } |
Davidroid | 1:1c01b873ad45 | 109 | |
Davidroid | 1:1c01b873ad45 | 110 | /** |
Davidroid | 7:7a608b07fa9f | 111 | * @brief Handling the LED capabilities. |
Davidroid | 2:dbd596540d46 | 112 | * @param None. |
Davidroid | 2:dbd596540d46 | 113 | * @retval None. |
Davidroid | 1:1c01b873ad45 | 114 | */ |
Davidroid | 2:dbd596540d46 | 115 | void LEDHandler(void) |
Davidroid | 1:1c01b873ad45 | 116 | { |
Davidroid | 3:ed347127fc38 | 117 | static int tick = 0; |
Davidroid | 3:ed347127fc38 | 118 | |
Davidroid | 7:7a608b07fa9f | 119 | /* Handling the LED dimming when powered ON. */ |
Davidroid | 7:7a608b07fa9f | 120 | pwm_dimming = 0.5f * sin(2 * M_PI * (tick++ * LOOP_PERIOD_us) / PWM_SIN_PERIOD_us) + 0.5f; |
Davidroid | 7:7a608b07fa9f | 121 | tick %= (int) (PWM_SIN_PERIOD_us / LOOP_PERIOD_us); |
Davidroid | 3:ed347127fc38 | 122 | |
Davidroid | 7:7a608b07fa9f | 123 | /* Printing to the console. */ |
Davidroid | 7:7a608b07fa9f | 124 | printf("Sinusoidal PWM Dimming --> %0.2f\r", pwm_dimming); |
Davidroid | 2:dbd596540d46 | 125 | |
Davidroid | 7:7a608b07fa9f | 126 | /* |
Davidroid | 7:7a608b07fa9f | 127 | Writing PWM dimming values to the LED. |
Davidroid | 3:ed347127fc38 | 128 | |
Davidroid | 7:7a608b07fa9f | 129 | Notes: |
Davidroid | 7:7a608b07fa9f | 130 | + Replace "SetPWMDimming()" with "SetAnalogDimming()" for an analog control. |
Davidroid | 7:7a608b07fa9f | 131 | */ |
Davidroid | 7:7a608b07fa9f | 132 | led->SetPWMDimming(pwm_dimming); |
Davidroid | 2:dbd596540d46 | 133 | } |
Davidroid | 2:dbd596540d46 | 134 | |
Davidroid | 2:dbd596540d46 | 135 | /** |
Davidroid | 2:dbd596540d46 | 136 | * @brief Interrupt Request for the main loop's ticker related interrupt. |
Davidroid | 2:dbd596540d46 | 137 | * @param None. |
Davidroid | 2:dbd596540d46 | 138 | * @retval None. |
Davidroid | 2:dbd596540d46 | 139 | */ |
Davidroid | 2:dbd596540d46 | 140 | void TickerIRQ(void) |
Davidroid | 2:dbd596540d46 | 141 | { |
Davidroid | 2:dbd596540d46 | 142 | ticker_irq_triggered = true; |
Davidroid | 2:dbd596540d46 | 143 | } |
Davidroid | 2:dbd596540d46 | 144 | |
Davidroid | 2:dbd596540d46 | 145 | /** |
Davidroid | 2:dbd596540d46 | 146 | * @brief Interrupt Request for the component's XFAULT interrupt. |
Davidroid | 2:dbd596540d46 | 147 | * @param None. |
Davidroid | 2:dbd596540d46 | 148 | * @retval None. |
Davidroid | 1:1c01b873ad45 | 149 | */ |
Davidroid | 1:1c01b873ad45 | 150 | void XFaultIRQ(void) |
Davidroid | 1:1c01b873ad45 | 151 | { |
Davidroid | 1:1c01b873ad45 | 152 | xfault_irq_triggered = true; |
Davidroid | 1:1c01b873ad45 | 153 | led->DisableXFaultIRQ(); |
Davidroid | 1:1c01b873ad45 | 154 | } |
Davidroid | 1:1c01b873ad45 | 155 | |
Davidroid | 1:1c01b873ad45 | 156 | /** |
Davidroid | 2:dbd596540d46 | 157 | * @brief Interrupt Handler for the component's XFAULT interrupt. |
Davidroid | 2:dbd596540d46 | 158 | * @param None. |
Davidroid | 2:dbd596540d46 | 159 | * @retval None. |
Davidroid | 2:dbd596540d46 | 160 | */ |
Davidroid | 2:dbd596540d46 | 161 | void XFaultHandler(void) |
Davidroid | 2:dbd596540d46 | 162 | { |
Davidroid | 2:dbd596540d46 | 163 | /* Printing to the console. */ |
Davidroid | 2:dbd596540d46 | 164 | printf("XFAULT Interrupt detected! Re-initializing LED driver...\r\n"); |
Davidroid | 2:dbd596540d46 | 165 | |
Davidroid | 2:dbd596540d46 | 166 | /* Re-initializing the demo. */ |
Davidroid | 2:dbd596540d46 | 167 | LEDInit(); |
Davidroid | 2:dbd596540d46 | 168 | |
Davidroid | 2:dbd596540d46 | 169 | led->EnableXFaultIRQ(); |
Davidroid | 2:dbd596540d46 | 170 | } |
Davidroid | 2:dbd596540d46 | 171 | |
Davidroid | 0:f5a41692e1fb | 172 | |
Davidroid | 0:f5a41692e1fb | 173 | /* Main ----------------------------------------------------------------------*/ |
Davidroid | 0:f5a41692e1fb | 174 | |
Davidroid | 0:f5a41692e1fb | 175 | int main() |
Davidroid | 0:f5a41692e1fb | 176 | { |
Davidroid | 0:f5a41692e1fb | 177 | /*----- Initialization. -----*/ |
Davidroid | 0:f5a41692e1fb | 178 | |
Davidroid | 2:dbd596540d46 | 179 | /* Printing to the console. */ |
Davidroid | 7:7a608b07fa9f | 180 | printf("LED Control Application Example\r\n\n"); |
Davidroid | 2:dbd596540d46 | 181 | |
Davidroid | 1:1c01b873ad45 | 182 | /* Initializing LED Control Component. */ |
Davidroid | 13:97083451e538 | 183 | led = new LED6001(D4, A3, D7, D5); |
Davidroid | 2:dbd596540d46 | 184 | if (led->Init() != COMPONENT_OK) |
Davidroid | 1:1c01b873ad45 | 185 | exit(EXIT_FAILURE); |
Davidroid | 1:1c01b873ad45 | 186 | |
Davidroid | 1:1c01b873ad45 | 187 | /* Attaching interrupt request functions. */ |
Davidroid | 1:1c01b873ad45 | 188 | led->AttachXFaultIRQ(&XFaultIRQ); |
Davidroid | 5:b00bbf1edfa9 | 189 | led->EnableXFaultIRQ(); |
Davidroid | 2:dbd596540d46 | 190 | ticker.attach_us(TickerIRQ, LOOP_PERIOD_us); |
Davidroid | 1:1c01b873ad45 | 191 | |
Davidroid | 2:dbd596540d46 | 192 | /* Initializing the demo. */ |
Davidroid | 2:dbd596540d46 | 193 | LEDInit(); |
Davidroid | 0:f5a41692e1fb | 194 | |
Davidroid | 0:f5a41692e1fb | 195 | |
Davidroid | 0:f5a41692e1fb | 196 | /*----- LED Control. -----*/ |
Davidroid | 1:1c01b873ad45 | 197 | |
Davidroid | 2:dbd596540d46 | 198 | /* Either performing the component handler, interrupt handlers, or waiting for events. */ |
Davidroid | 1:1c01b873ad45 | 199 | while (true) |
Davidroid | 1:1c01b873ad45 | 200 | { |
Davidroid | 2:dbd596540d46 | 201 | if (ticker_irq_triggered) |
Davidroid | 1:1c01b873ad45 | 202 | { |
Davidroid | 2:dbd596540d46 | 203 | ticker_irq_triggered = false; |
Davidroid | 1:1c01b873ad45 | 204 | LEDHandler(); |
Davidroid | 5:b00bbf1edfa9 | 205 | } else if (xfault_irq_triggered) |
Davidroid | 1:1c01b873ad45 | 206 | { |
Davidroid | 1:1c01b873ad45 | 207 | xfault_irq_triggered = false; |
Davidroid | 1:1c01b873ad45 | 208 | XFaultHandler(); |
Davidroid | 5:b00bbf1edfa9 | 209 | } else { |
Davidroid | 1:1c01b873ad45 | 210 | /* It is recommended that SEVONPEND in the System Control Register is NOT set. */ |
Davidroid | 1:1c01b873ad45 | 211 | __WFE(); |
Davidroid | 1:1c01b873ad45 | 212 | } |
Davidroid | 1:1c01b873ad45 | 213 | } |
Davidroid | 1:1c01b873ad45 | 214 | } |