forked from RIOT-OS/RIOT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpu.c
126 lines (106 loc) · 3.62 KB
/
cpu.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
* Copyright (C) 2017 RWTH Aachen, Josua Arndt
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @ingroup cpu_atmega256rfr2
* @{
*
* @file
* @brief Implementation of the CPU initialization
*
* @author Steffen Robertz <[email protected]>
* @author Josua Arndt <[email protected]>
* @}
*/
#include <avr/io.h>
#include <avr/wdt.h>
#include <avr/pgmspace.h>
#include "cpu.h"
#include "board.h"
#define ENABLE_DEBUG (0)
#include "debug.h"
/*
* Since this MCU does not feature a software reset, the watchdog timer
* is being used. It will be set to the shortest time and then force a
* reset. Therefore the MCUSR register needs to be resetted as fast as
* possible. In this case in the bootloader already. In order to regain
* information about the reset cause, the MCUSR is copied to r2 beforehand.
* When a software reset was triggered, r3 will contain 0xAA. In order to
* prevent changes to the values from the .init section, r2 and r3 are saved
* in the .init0 section
*/
uint8_t mcusr_mirror __attribute__((section(".noinit")));
uint8_t soft_rst __attribute__((section(".noinit")));
void get_mcusr(void) __attribute__((naked)) __attribute__((section(".init0")));
void get_mcusr(void)
{
/* save the reset flags passed from the bootloader */
__asm__ __volatile__("mov %0, r2\n" : "=r" (mcusr_mirror) :);
__asm__ __volatile__("mov %0, r3\n" : "=r" (soft_rst) :);
}
void _reset_cause(void)
{
if(mcusr_mirror & (1 << PORF)) {
DEBUG("Power-on reset.\n");
}
if(mcusr_mirror & (1 << EXTRF)) {
DEBUG("External reset!\n");
}
if(mcusr_mirror & (1 << BORF)) {
DEBUG("Brownout reset!\n");
}
if(mcusr_mirror & (1 << WDRF)) {
if(soft_rst & 0xAA) {
DEBUG("Software reset!\n");
} else {
DEBUG("Watchdog reset!\n");
}
}
if(mcusr_mirror & (1 << JTRF)) {
DEBUG("JTAG reset!\n");
}
}
void cpu_init(void)
{
_reset_cause();
wdt_reset(); /* should not be nececessary as done in bootloader */
wdt_disable(); /* but when used without bootloader this is needed */
/* Set system clock Prescaler */
CLKPR = (1 << CLKPCE); /* enable a change to CLKPR */
/* set the Division factor to 1 results in divisor 2 for internal Oscillator
* So FCPU = 8MHz
* */
CLKPR = 0;
}
/* This is a vector which is aliased to __vector_default,
* the vector executed when an ISR fires with no accompanying
* ISR handler. This may be used along with the ISR() macro to
* create a catch-all for undefined but used ISRs for debugging
* purposes.
* SCIRQS – Symbol Counter Interrupt Status Register
* BATMON – Battery Monitor Control and Status Register
* IRQ_STATUS /1 – Transceiver Interrupt Status Register
* EIFR – External Interrupt Flag Register
* PCIFR – Pin Change Interrupt Flag Register
*/
ISR(BADISR_vect){
_reset_cause();
printf_P(PSTR("FATAL ERROR: BADISR_vect called, unprocessed Interrupt.\n"
"STOP Execution.\n"));
printf("IRQ_STATUS %#02x\nIRQ_STATUS1 %#02x\n",
(unsigned int)IRQ_STATUS, (unsigned int)IRQ_STATUS1 );
printf("SCIRQS %#02x\nBATMON %#02x\n", (unsigned int)SCIRQS, (unsigned int)BATMON );
printf("EIFR %#02x\nPCIFR %#02x\n", (unsigned int)EIFR, (unsigned int)PCIFR );
/* White LED light is used to signal ERROR. */
LED_PORT |= (LED2_MASK | LED1_MASK | LED0_MASK);
while (1) {}
}
ISR(BAT_LOW_vect, ISR_BLOCK){
__enter_isr();
DEBUG("BAT_LOW \n");
__exit_isr();
}