1 |
198 |
zero_gravi |
// #################################################################################################
|
2 |
|
|
// # < GPIO interrupt example program > #
|
3 |
|
|
// # ********************************************************************************************* #
|
4 |
|
|
// # Prints a message whenever a GPIO input pin goes HIGH. Uses the PIO pin-change interrupt. #
|
5 |
|
|
// # ********************************************************************************************* #
|
6 |
|
|
// # BSD 3-Clause License #
|
7 |
|
|
// # #
|
8 |
|
|
// # Copyright (c) 2020, Stephan Nolting. All rights reserved. #
|
9 |
|
|
// # #
|
10 |
|
|
// # Redistribution and use in source and binary forms, with or without modification, are #
|
11 |
|
|
// # permitted provided that the following conditions are met: #
|
12 |
|
|
// # #
|
13 |
|
|
// # 1. Redistributions of source code must retain the above copyright notice, this list of #
|
14 |
|
|
// # conditions and the following disclaimer. #
|
15 |
|
|
// # #
|
16 |
|
|
// # 2. Redistributions in binary form must reproduce the above copyright notice, this list of #
|
17 |
|
|
// # conditions and the following disclaimer in the documentation and/or other materials #
|
18 |
|
|
// # provided with the distribution. #
|
19 |
|
|
// # #
|
20 |
|
|
// # 3. Neither the name of the copyright holder nor the names of its contributors may be used to #
|
21 |
|
|
// # endorse or promote products derived from this software without specific prior written #
|
22 |
|
|
// # permission. #
|
23 |
|
|
// # #
|
24 |
|
|
// # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS #
|
25 |
|
|
// # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF #
|
26 |
|
|
// # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE #
|
27 |
|
|
// # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, #
|
28 |
|
|
// # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
|
29 |
|
|
// # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED #
|
30 |
|
|
// # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING #
|
31 |
|
|
// # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED #
|
32 |
|
|
// # OF THE POSSIBILITY OF SUCH DAMAGE. #
|
33 |
|
|
// # ********************************************************************************************* #
|
34 |
|
|
// # The NEO430 Processor - https://github.com/stnolting/neo430 #
|
35 |
|
|
// #################################################################################################
|
36 |
|
|
|
37 |
|
|
|
38 |
|
|
// Libraries
|
39 |
|
|
#include <stdint.h>
|
40 |
|
|
#include <neo430.h>
|
41 |
|
|
|
42 |
|
|
// Configuration
|
43 |
|
|
#define BAUD_RATE 19200
|
44 |
|
|
|
45 |
|
|
// Function prototypes
|
46 |
|
|
void __attribute__((__interrupt__)) gpio_irq_handler(void);
|
47 |
|
|
|
48 |
|
|
|
49 |
|
|
/* ------------------------------------------------------------
|
50 |
|
|
* INFO Main function
|
51 |
|
|
* ------------------------------------------------------------ */
|
52 |
|
|
int main(void) {
|
53 |
|
|
|
54 |
|
|
// setup UART
|
55 |
|
|
neo430_uart_setup(BAUD_RATE);
|
56 |
|
|
|
57 |
|
|
// intro text
|
58 |
|
|
neo430_uart_br_print("\nGPIO pin change interrupt demo program\n\n");
|
59 |
|
|
|
60 |
|
|
// check if GPIO present
|
61 |
|
|
if (!(SYS_FEATURES & (1<<SYS_GPIO_EN))) {
|
62 |
|
|
neo430_uart_br_print("Error! No GPIO unit synthesized!");
|
63 |
|
|
return 1;
|
64 |
|
|
}
|
65 |
|
|
|
66 |
|
|
// deactivate all LEDs
|
67 |
|
|
GPIO_OUTPUT = 0;
|
68 |
|
|
|
69 |
|
|
// set address of IRQ handler
|
70 |
|
|
IRQVEC_GPIO = (uint16_t)(&gpio_irq_handler);
|
71 |
|
|
|
72 |
|
|
// configure GPIO pin-change interrupt
|
73 |
|
|
GPIO_IRQMASK = 0xFFFF; // use all input pins as trigger
|
74 |
|
|
|
75 |
|
|
// enable global IRQs
|
76 |
|
|
neo430_eint();
|
77 |
|
|
|
78 |
|
|
// go to sleep mode, increment counter whenever the CPU wakes up
|
79 |
|
|
while (1) {
|
80 |
|
|
neo430_sleep();
|
81 |
|
|
GPIO_OUTPUT = (GPIO_OUTPUT + 1) & 0x00FF; // increment LED counter
|
82 |
|
|
}
|
83 |
|
|
|
84 |
|
|
return 0;
|
85 |
|
|
}
|
86 |
|
|
|
87 |
|
|
|
88 |
|
|
/* ------------------------------------------------------------
|
89 |
|
|
* INFO GPIO pin-change interrupt handler
|
90 |
|
|
* ------------------------------------------------------------ */
|
91 |
|
|
void __attribute__((__interrupt__)) gpio_irq_handler(void) {
|
92 |
|
|
|
93 |
|
|
// we cannot 100% ensure to actually get the specific state of
|
94 |
|
|
// the input, which caused the IRQ
|
95 |
|
|
|
96 |
|
|
neo430_uart_br_print("GPIO pin change interrupt! Current input state: 0x");
|
97 |
|
|
neo430_uart_print_hex_word(GPIO_INPUT);
|
98 |
|
|
neo430_uart_br_print("\n");
|
99 |
|
|
}
|
100 |
|
|
|