| 1 |
198 |
zero_gravi |
// #################################################################################################
|
| 2 |
|
|
// # < Nested IRQs example > #
|
| 3 |
|
|
// # ********************************************************************************************* #
|
| 4 |
|
|
// # Generates a run time clock using the timer IRQ. #
|
| 5 |
|
|
// # Whenever a char via the UART is received, the according ISR show the current time. #
|
| 6 |
|
|
// # ********************************************************************************************* #
|
| 7 |
|
|
// # BSD 3-Clause License #
|
| 8 |
|
|
// # #
|
| 9 |
|
|
// # Copyright (c) 2020, Stephan Nolting. All rights reserved. #
|
| 10 |
|
|
// # #
|
| 11 |
|
|
// # Redistribution and use in source and binary forms, with or without modification, are #
|
| 12 |
|
|
// # permitted provided that the following conditions are met: #
|
| 13 |
|
|
// # #
|
| 14 |
|
|
// # 1. Redistributions of source code must retain the above copyright notice, this list of #
|
| 15 |
|
|
// # conditions and the following disclaimer. #
|
| 16 |
|
|
// # #
|
| 17 |
|
|
// # 2. Redistributions in binary form must reproduce the above copyright notice, this list of #
|
| 18 |
|
|
// # conditions and the following disclaimer in the documentation and/or other materials #
|
| 19 |
|
|
// # provided with the distribution. #
|
| 20 |
|
|
// # #
|
| 21 |
|
|
// # 3. Neither the name of the copyright holder nor the names of its contributors may be used to #
|
| 22 |
|
|
// # endorse or promote products derived from this software without specific prior written #
|
| 23 |
|
|
// # permission. #
|
| 24 |
|
|
// # #
|
| 25 |
|
|
// # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS #
|
| 26 |
|
|
// # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF #
|
| 27 |
|
|
// # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE #
|
| 28 |
|
|
// # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, #
|
| 29 |
|
|
// # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
|
| 30 |
|
|
// # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED #
|
| 31 |
|
|
// # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING #
|
| 32 |
|
|
// # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED #
|
| 33 |
|
|
// # OF THE POSSIBILITY OF SUCH DAMAGE. #
|
| 34 |
|
|
// # ********************************************************************************************* #
|
| 35 |
|
|
// # The NEO430 Processor - https://github.com/stnolting/neo430 #
|
| 36 |
|
|
// #################################################################################################
|
| 37 |
|
|
|
| 38 |
|
|
|
| 39 |
|
|
// Libraries
|
| 40 |
|
|
#include <stdint.h>
|
| 41 |
|
|
#include <neo430.h>
|
| 42 |
|
|
|
| 43 |
|
|
// Configuration
|
| 44 |
|
|
#define BAUD_RATE 19200
|
| 45 |
|
|
|
| 46 |
|
|
// Function prototypes
|
| 47 |
|
|
void __attribute__((__interrupt__)) timer_irq_handler(void);
|
| 48 |
|
|
void __attribute__((__interrupt__)) uart_irq_handler(void);
|
| 49 |
|
|
|
| 50 |
|
|
// Variable
|
| 51 |
|
|
volatile uint64_t time_ms;
|
| 52 |
|
|
|
| 53 |
|
|
|
| 54 |
|
|
/* ------------------------------------------------------------
|
| 55 |
|
|
* INFO Main function
|
| 56 |
|
|
* ------------------------------------------------------------ */
|
| 57 |
|
|
int main(void) {
|
| 58 |
|
|
|
| 59 |
|
|
// setup UART
|
| 60 |
|
|
neo430_uart_setup(BAUD_RATE);
|
| 61 |
|
|
|
| 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 |
|
|
|
| 70 |
|
|
// reset time
|
| 71 |
|
|
time_ms = 0;
|
| 72 |
|
|
|
| 73 |
|
|
|
| 74 |
|
|
// intro text
|
| 75 |
|
|
//neo430_uart_br_print("\nClock example. Press any key to show the current time.\n");
|
| 76 |
|
|
|
| 77 |
|
|
|
| 78 |
|
|
// init TIMER IRQ
|
| 79 |
|
|
// ------------------------------------------------------
|
| 80 |
|
|
|
| 81 |
|
|
// set address of timer IRQ handler
|
| 82 |
|
|
IRQVEC_TIMER = (uint16_t)(&timer_irq_handler);
|
| 83 |
|
|
|
| 84 |
|
|
// configure timer frequency
|
| 85 |
|
|
neo430_timer_disable();
|
| 86 |
|
|
uint16_t timer_thres;
|
| 87 |
|
|
if (neo430_timer_config_freq(1000, &timer_thres)) { // 1kHz to increment every 1ms
|
| 88 |
|
|
neo430_uart_br_print("Invalid TIMER frequency!\n");
|
| 89 |
|
|
}
|
| 90 |
|
|
|
| 91 |
|
|
neo430_printf("THR: %x, CTR: %x\n", TMR_THRES, TMR_CT);
|
| 92 |
|
|
TMR_CT |= (1<<TMR_CT_EN) | (1<<TMR_CT_ARST) | (1<<TMR_CT_IRQ) | (1<<TMR_CT_RUN); // enable timer, auto-reset, irq enabled, start timer
|
| 93 |
|
|
neo430_printf("THR: %x, CTR: %x\n", TMR_THRES, TMR_CT);
|
| 94 |
|
|
|
| 95 |
|
|
|
| 96 |
|
|
// init UART RX IRQ
|
| 97 |
|
|
// ------------------------------------------------------
|
| 98 |
|
|
|
| 99 |
|
|
// set address of UART IRQ handler
|
| 100 |
|
|
IRQVEC_SERIAL = (uint16_t)(&uart_irq_handler);
|
| 101 |
|
|
|
| 102 |
|
|
// activate UART RX interrupt
|
| 103 |
|
|
UART_CT |= (1<<UART_CT_RX_IRQ);
|
| 104 |
|
|
|
| 105 |
|
|
|
| 106 |
|
|
// enable global IRQs
|
| 107 |
|
|
neo430_eint();
|
| 108 |
|
|
|
| 109 |
|
|
|
| 110 |
|
|
// do nothing
|
| 111 |
|
|
while (1) {
|
| 112 |
|
|
neo430_sleep();
|
| 113 |
|
|
}
|
| 114 |
|
|
|
| 115 |
|
|
return 0;
|
| 116 |
|
|
}
|
| 117 |
|
|
|
| 118 |
|
|
|
| 119 |
|
|
/* ------------------------------------------------------------
|
| 120 |
|
|
* INFO Timer interrupt handler
|
| 121 |
|
|
* ------------------------------------------------------------ */
|
| 122 |
|
|
void __attribute__((__interrupt__)) timer_irq_handler(void) {
|
| 123 |
|
|
|
| 124 |
|
|
time_ms++;
|
| 125 |
|
|
}
|
| 126 |
|
|
|
| 127 |
|
|
|
| 128 |
|
|
/* ------------------------------------------------------------
|
| 129 |
|
|
* INFO UART interrupt handler
|
| 130 |
|
|
* ------------------------------------------------------------ */
|
| 131 |
|
|
void __attribute__((__interrupt__)) uart_irq_handler(void) {
|
| 132 |
|
|
|
| 133 |
|
|
// reactive IRQs to allow nested interrupts
|
| 134 |
|
|
neo430_eint();
|
| 135 |
|
|
|
| 136 |
|
|
// show time
|
| 137 |
|
|
uint32_t current_time = time_ms; // in seconds
|
| 138 |
|
|
uint16_t hour = (uint16_t)( (((current_time/10000)/60)/3600)%24 );
|
| 139 |
|
|
uint16_t minute = (uint16_t)( ((current_time/1000)/60)%60 );
|
| 140 |
|
|
uint16_t second = (uint16_t)( (current_time/1000)%60 );
|
| 141 |
|
|
uint16_t m_second = (uint16_t)( (current_time%1000)%1000 );
|
| 142 |
|
|
neo430_printf("Current runtime: %u:%u:%u:%u\n", hour, minute, second, m_second);
|
| 143 |
|
|
}
|
| 144 |
|
|
|