-
Notifications
You must be signed in to change notification settings - Fork 271
/
Copy pathnrf5.c
186 lines (142 loc) · 4.38 KB
/
nrf5.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/* Copyright (C) 2018 by Jacob Alexander
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
// ----- Includes -----
// Debug Includes
#if defined(_bootloader_)
#include <inttypes.h>
#include <debug.h>
#else
#include <print.h>
#endif
// Local Includes
#include "entropy.h"
#include "nrf5.h"
// ----- Variables -----
/* Initialize segments */
extern uint32_t _sfixed;
extern uint32_t _efixed;
extern uint32_t _etext;
extern uint32_t _srelocate;
extern uint32_t _erelocate;
extern uint32_t _szero;
extern uint32_t _ezero;
extern uint32_t _sstack;
extern uint32_t _estack;
uintptr_t __stack_chk_guard = 0xdeadbeef;
//__attribute__((__aligned__(TRACE_BUFFER_SIZE * sizeof(uint32_t)))) uint32_t mtb[TRACE_BUFFER_SIZE];
// ----- Function Declarations -----
extern int main();
void ResetHandler();
// ----- Interrupts -----
// NVIC - Default ISR
void fault_isr()
{
print("Fault!" NL );
#if defined(DEBUG) && defined(JLINK)
asm volatile("BKPT #01");
#else
while ( 1 )
{
// keep polling some communication while in fault
// mode, so we don't completely die.
/*if ( SIM_SCGC4 & SIM_SCGC4_USBOTG ) usb_isr();
if ( SIM_SCGC4 & SIM_SCGC4_UART0 ) uart0_status_isr();
if ( SIM_SCGC4 & SIM_SCGC4_UART1 ) uart1_status_isr();
if ( SIM_SCGC4 & SIM_SCGC4_UART2 ) uart2_status_isr();*/
}
#endif
}
// Stack Overflow Interrupt
void __stack_chk_fail(void)
{
print("Segfault!" NL );
#if defined(DEBUG) && defined(JLINK)
asm volatile("BKPT #01");
#else
fault_isr();
#endif
}
// ----- Flash Configuration -----
// ----- Functions -----
void *memset( void *addr, int val, unsigned int len )
{
char *buf = addr;
for (; len > 0; --len, ++buf)
*buf = val;
return (addr);
}
int memcmp( const void *a, const void *b, unsigned int len )
{
const uint8_t *ap = a, *bp = b;
int val = 0;
for (; len > 0 && (val = *ap - *bp) == 0; --len, ++ap, ++bp)
/* NOTHING */;
return (val);
}
void *memcpy( void *dst, const void *src, unsigned int len )
{
// Fast memcpy (Cortex-M4 only), adapted from:
// https://cboard.cprogramming.com/c-programming/154333-fast-memcpy-alternative-32-bit-embedded-processor-posted-just-fyi-fwiw.html#post1149163
// Cortex-M4 can do unaligned accesses, even for 32-bit values
// Uses 32-bit values to do copies instead of 8-bit (should effectively speed up memcpy by 3x)
#if defined(_cortex_m4_)
uint32_t i;
uint32_t *pLongSrc;
uint32_t *pLongDest;
uint32_t numLongs = len / 4;
uint32_t endLen = len & 0x03;
// Convert byte addressing to long addressing
pLongSrc = (uint32_t*) src;
pLongDest = (uint32_t*) dst;
// Copy long values, disregarding any 32-bit alignment issues
for ( i = 0; i < numLongs; i++ )
{
*pLongDest++ = *pLongSrc++;
}
// Convert back to byte addressing
uint8_t *srcbuf = (uint8_t*) pLongSrc;
uint8_t *dstbuf = (uint8_t*) pLongDest;
// Copy trailing bytes byte-by-byte
for (; endLen > 0; --endLen, ++dstbuf, ++srcbuf)
*dstbuf = *srcbuf;
return (dst);
#else
char *dstbuf = dst;
const char *srcbuf = src;
for (; len > 0; --len, ++dstbuf, ++srcbuf)
*dstbuf = *srcbuf;
return (dst);
#endif
}
// ----- Chip Entry Point -----
void ResetHandler()
{
// TODO (HaaTa) - Add initialization code, this should be the firmware entry point
// Enable IRQs
__enable_irq();
// Intialize entropy for random numbers
rand_initialize();
// Start main
main();
while ( 1 ); // Shouldn't get here...
}
// ----- RAM Setup -----
// ----- Interrupt Execution Priority -----