1 |
198 |
zero_gravi |
// #################################################################################################
|
2 |
|
|
// # < simple timer usage example > #
|
3 |
|
|
// # ********************************************************************************************* #
|
4 |
|
|
// # Flashes LED using the timer 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 |
|
|
// Macros
|
43 |
|
|
#define xstr(a) str(a)
|
44 |
|
|
#define str(a) #a
|
45 |
|
|
|
46 |
|
|
// Configuration
|
47 |
|
|
#define BAUD_RATE 19200
|
48 |
|
|
#define BLINK_LED 0 // pin 0 on GPIO.out
|
49 |
|
|
#define BLINK_FREQ 4 // in Hz
|
50 |
|
|
|
51 |
|
|
// Function prototypes
|
52 |
|
|
void __attribute__((__interrupt__)) timer_irq_handler(void);
|
53 |
|
|
|
54 |
|
|
|
55 |
|
|
/* ------------------------------------------------------------
|
56 |
|
|
* INFO Main function
|
57 |
|
|
* ------------------------------------------------------------ */
|
58 |
|
|
int main(void) {
|
59 |
|
|
|
60 |
|
|
// setup UART
|
61 |
|
|
neo430_uart_setup(BAUD_RATE);
|
62 |
|
|
|
63 |
|
|
// check if TIMER unit was synthesized, exit if no TIMER is available
|
64 |
|
|
if (!(SYS_FEATURES & (1<<SYS_TIMER_EN))) {
|
65 |
|
|
neo430_uart_br_print("Error! No TIMER unit synthesized!");
|
66 |
|
|
return 1;
|
67 |
|
|
}
|
68 |
|
|
|
69 |
|
|
neo430_gpio_pin_clr(BLINK_LED); // clear LED
|
70 |
|
|
|
71 |
|
|
// intro text
|
72 |
|
|
neo430_uart_br_print("\nTimer blinking status LED at "xstr(BLINK_FREQ)" Hz.\n");
|
73 |
|
|
|
74 |
|
|
// set address of timer IRQ handler
|
75 |
|
|
IRQVEC_TIMER = (uint16_t)(&timer_irq_handler);
|
76 |
|
|
|
77 |
|
|
// configure timer frequency
|
78 |
|
|
neo430_timer_disable();
|
79 |
|
|
uint16_t timer_thres;
|
80 |
|
|
if (neo430_timer_config_freq(BLINK_FREQ, &timer_thres)) {
|
81 |
|
|
neo430_uart_br_print("Invalid TIMER frequency!\n");
|
82 |
|
|
}
|
83 |
|
|
|
84 |
|
|
TMR_CT |= (1<<TMR_CT_EN) | (1<<TMR_CT_ARST) | (1<<TMR_CT_IRQ) | (1<<TMR_CT_RUN); // enable timer, auto-reset, irq enabled, timer start
|
85 |
|
|
|
86 |
|
|
// enable global IRQs
|
87 |
|
|
neo430_eint();
|
88 |
|
|
|
89 |
|
|
while(1) {
|
90 |
|
|
neo430_sleep();
|
91 |
|
|
}
|
92 |
|
|
|
93 |
|
|
return 0;
|
94 |
|
|
}
|
95 |
|
|
|
96 |
|
|
|
97 |
|
|
/* ------------------------------------------------------------
|
98 |
|
|
* INFO Timer interrupt handler
|
99 |
|
|
* ------------------------------------------------------------ */
|
100 |
|
|
void __attribute__((__interrupt__)) timer_irq_handler(void) {
|
101 |
|
|
|
102 |
|
|
neo430_gpio_pin_toggle(BLINK_LED);
|
103 |
|
|
}
|
104 |
|
|
|