Hieu Vo
/
NUCLEO_BTN_LED_DEMO
Simple firmware to test the I/O capabilities of a Nucleo 64 board
main.cpp@1:69c43e344369, 2016-03-15 (annotated)
- Committer:
- hieuvt6
- Date:
- Tue Mar 15 21:55:12 2016 +0000
- Revision:
- 1:69c43e344369
- Parent:
- 0:43a77ff47221
Move interrupt work to main to avoid heavy workload in interrupt handler.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hieuvt6 | 0:43a77ff47221 | 1 | #include "mbed.h" |
hieuvt6 | 0:43a77ff47221 | 2 | |
hieuvt6 | 0:43a77ff47221 | 3 | #define DEBUG_0_PRINT_OUT_ENABLE 1 |
hieuvt6 | 0:43a77ff47221 | 4 | #if DEBUG_0_PRINT_OUT_ENABLE |
hieuvt6 | 0:43a77ff47221 | 5 | #define DEBUG_0(...) { printf(__VA_ARGS__); } |
hieuvt6 | 0:43a77ff47221 | 6 | #else |
hieuvt6 | 0:43a77ff47221 | 7 | #define DEBUG_0(...) /* nothing */ |
hieuvt6 | 0:43a77ff47221 | 8 | #endif /* #if DEBUG_0_PRINT_OUT_ENABLE */ |
hieuvt6 | 0:43a77ff47221 | 9 | |
hieuvt6 | 0:43a77ff47221 | 10 | #define LED_ON 1 // LED is ACTIVE HIGH. It turns ON when the control pin is HIGH |
hieuvt6 | 0:43a77ff47221 | 11 | #define LED_OFF 0 // LED is ACTIVE HIGH. It turns OFF when the control pin is LOW |
hieuvt6 | 0:43a77ff47221 | 12 | #define BUTTON_PRESSED_LED_STATE LED_ON |
hieuvt6 | 0:43a77ff47221 | 13 | #define BUTTON_RELEASED_LED_STATE LED_OFF |
hieuvt6 | 0:43a77ff47221 | 14 | |
hieuvt6 | 0:43a77ff47221 | 15 | DigitalOut led1(LED1); |
hieuvt6 | 0:43a77ff47221 | 16 | InterruptIn userButton(USER_BUTTON); |
hieuvt6 | 0:43a77ff47221 | 17 | |
hieuvt6 | 1:69c43e344369 | 18 | bool buttonPressedInterrupt = false; |
hieuvt6 | 1:69c43e344369 | 19 | bool buttonReleasedInterrupt = false; |
hieuvt6 | 1:69c43e344369 | 20 | |
hieuvt6 | 0:43a77ff47221 | 21 | void |
hieuvt6 | 0:43a77ff47221 | 22 | buttonPressedCallback(void) |
hieuvt6 | 0:43a77ff47221 | 23 | { |
hieuvt6 | 1:69c43e344369 | 24 | // Flag main to handle heavy work. Heavy work should not be handle in interrupt handler |
hieuvt6 | 1:69c43e344369 | 25 | buttonPressedInterrupt = true; |
hieuvt6 | 0:43a77ff47221 | 26 | } |
hieuvt6 | 0:43a77ff47221 | 27 | |
hieuvt6 | 0:43a77ff47221 | 28 | void |
hieuvt6 | 0:43a77ff47221 | 29 | buttonReleasedCallback(void) |
hieuvt6 | 0:43a77ff47221 | 30 | { |
hieuvt6 | 1:69c43e344369 | 31 | // Flag main to handle heavy work. Heavy work should not be handle in interrupt handler |
hieuvt6 | 1:69c43e344369 | 32 | buttonReleasedInterrupt = true; |
hieuvt6 | 0:43a77ff47221 | 33 | } |
hieuvt6 | 0:43a77ff47221 | 34 | |
hieuvt6 | 0:43a77ff47221 | 35 | void |
hieuvt6 | 0:43a77ff47221 | 36 | initBoard(void) |
hieuvt6 | 0:43a77ff47221 | 37 | { |
hieuvt6 | 0:43a77ff47221 | 38 | // Init LED to turn ON by default |
hieuvt6 | 0:43a77ff47221 | 39 | led1 = BUTTON_RELEASED_LED_STATE; |
hieuvt6 | 0:43a77ff47221 | 40 | // Init button pressed and released behavior |
hieuvt6 | 0:43a77ff47221 | 41 | userButton.rise(buttonReleasedCallback); // Button is ACTIVE LOW. Released the button pull input pin HIGH, triggering the Rising Edge interrupt. |
hieuvt6 | 0:43a77ff47221 | 42 | userButton.fall(buttonPressedCallback); // Button is ACTIVE LOW. Pressed the button pull input pin LOW, triggering the Falling Edge interrupt. |
hieuvt6 | 0:43a77ff47221 | 43 | userButton.enable_irq(); |
hieuvt6 | 0:43a77ff47221 | 44 | } |
hieuvt6 | 0:43a77ff47221 | 45 | |
hieuvt6 | 0:43a77ff47221 | 46 | int |
hieuvt6 | 0:43a77ff47221 | 47 | main() |
hieuvt6 | 0:43a77ff47221 | 48 | { |
hieuvt6 | 0:43a77ff47221 | 49 | initBoard(); |
hieuvt6 | 1:69c43e344369 | 50 | |
hieuvt6 | 1:69c43e344369 | 51 | DEBUG_0("NUCLEO_BTN_LED_DEMO started\n"); |
hieuvt6 | 1:69c43e344369 | 52 | |
hieuvt6 | 0:43a77ff47221 | 53 | led1 = LED_ON; |
hieuvt6 | 0:43a77ff47221 | 54 | wait_ms(1000); |
hieuvt6 | 0:43a77ff47221 | 55 | led1 = LED_OFF; |
hieuvt6 | 0:43a77ff47221 | 56 | wait_ms(1000); |
hieuvt6 | 0:43a77ff47221 | 57 | while(1) { |
hieuvt6 | 1:69c43e344369 | 58 | if (buttonReleasedInterrupt) |
hieuvt6 | 1:69c43e344369 | 59 | { |
hieuvt6 | 1:69c43e344369 | 60 | led1 = BUTTON_RELEASED_LED_STATE; |
hieuvt6 | 1:69c43e344369 | 61 | DEBUG_0("button released\n"); |
hieuvt6 | 1:69c43e344369 | 62 | |
hieuvt6 | 1:69c43e344369 | 63 | buttonReleasedInterrupt = false; |
hieuvt6 | 1:69c43e344369 | 64 | } |
hieuvt6 | 1:69c43e344369 | 65 | |
hieuvt6 | 1:69c43e344369 | 66 | if (buttonPressedInterrupt) |
hieuvt6 | 1:69c43e344369 | 67 | { |
hieuvt6 | 1:69c43e344369 | 68 | led1 = BUTTON_PRESSED_LED_STATE; |
hieuvt6 | 1:69c43e344369 | 69 | DEBUG_0("button pressed\n"); |
hieuvt6 | 1:69c43e344369 | 70 | |
hieuvt6 | 1:69c43e344369 | 71 | buttonPressedInterrupt = false; |
hieuvt6 | 1:69c43e344369 | 72 | } |
hieuvt6 | 0:43a77ff47221 | 73 | } |
hieuvt6 | 0:43a77ff47221 | 74 | } |