-
-
Notifications
You must be signed in to change notification settings - Fork 725
/
Copy pathmain.c
280 lines (235 loc) · 7.01 KB
/
main.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
Copyright (c) 2015 Arduino LLC. All right reserved.
Copyright (c) 2015 Atmel Corporation/Thibaut VIARD. All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdio.h>
#include <sam.h>
#include "sam_ba_monitor.h"
#include "sam_ba_serial.h"
#include "board_definitions.h"
#include "board_driver_led.h"
#include "board_driver_i2c.h"
#include "board_driver_pmic.h"
#include "board_driver_jtag.h"
#include "sam_ba_usb.h"
#include "sam_ba_cdc.h"
extern uint32_t __sketch_vectors_ptr; // Exported value from linker script
extern void board_init(void);
volatile uint32_t* pulSketch_Start_Address;
static void jump_to_application(void) {
/* Rebase the Stack Pointer */
__set_MSP( (uint32_t)(__sketch_vectors_ptr) );
/* Rebase the vector table base address */
SCB->VTOR = ((uint32_t)(&__sketch_vectors_ptr) & SCB_VTOR_TBLOFF_Msk);
/* Jump to application Reset Handler in the application */
asm("bx %0"::"r"(*pulSketch_Start_Address));
}
static volatile bool main_b_cdc_enable = false;
#ifdef CONFIGURE_PMIC
static volatile bool jump_to_app = false;
#endif
/**
* \brief Check the application startup condition
*
*/
static void check_start_application(void)
{
// LED_init();
// LED_off();
/*
* Test sketch stack pointer @ &__sketch_vectors_ptr
* Stay in SAM-BA if value @ (&__sketch_vectors_ptr) == 0xFFFFFFFF (Erased flash cell value)
*/
if (__sketch_vectors_ptr == 0xFFFFFFFF)
{
/* Stay in bootloader */
return;
}
/*
* Load the sketch Reset Handler address
* __sketch_vectors_ptr is exported from linker script and point on first 32b word of sketch vector table
* First 32b word is sketch stack
* Second 32b word is sketch entry point: Reset_Handler()
*/
pulSketch_Start_Address = &__sketch_vectors_ptr ;
pulSketch_Start_Address++ ;
/*
* Test vector table address of sketch @ &__sketch_vectors_ptr
* Stay in SAM-BA if this function is not aligned enough, ie not valid
*/
if ( ((uint32_t)(&__sketch_vectors_ptr) & ~SCB_VTOR_TBLOFF_Msk) != 0x00)
{
/* Stay in bootloader */
return;
}
#if defined(BOOT_DOUBLE_TAP_ADDRESS)
#define DOUBLE_TAP_MAGIC 0x07738135
if (PM->RCAUSE.bit.POR)
{
/* On power-on initialize double-tap */
BOOT_DOUBLE_TAP_DATA = 0;
}
else
{
if (BOOT_DOUBLE_TAP_DATA == DOUBLE_TAP_MAGIC)
{
/* Second tap, stay in bootloader */
BOOT_DOUBLE_TAP_DATA = 0;
return;
}
#ifdef HAS_EZ6301QI
// wait a tiny bit for the EZ6301QI to settle,
// as it's connected to RESETN and might reset
// the chip when the cable is plugged in fresh
for (uint32_t i=0; i<2500; i++) /* 10ms */
/* force compiler to not optimize this... */
__asm__ __volatile__("");
#endif
/* First tap */
BOOT_DOUBLE_TAP_DATA = DOUBLE_TAP_MAGIC;
/* Wait 0.5sec to see if the user tap reset again.
* The loop value is based on SAMD21 default 1MHz clock @ reset.
*/
for (uint32_t i=0; i<125000; i++) /* 500ms */
/* force compiler to not optimize this... */
__asm__ __volatile__("");
/* Timeout happened, continue boot... */
BOOT_DOUBLE_TAP_DATA = 0;
}
#endif
/*
#if defined(BOOT_LOAD_PIN)
volatile PortGroup *boot_port = (volatile PortGroup *)(&(PORT->Group[BOOT_LOAD_PIN / 32]));
volatile bool boot_en;
// Enable the input mode in Boot GPIO Pin
boot_port->DIRCLR.reg = BOOT_PIN_MASK;
boot_port->PINCFG[BOOT_LOAD_PIN & 0x1F].reg = PORT_PINCFG_INEN | PORT_PINCFG_PULLEN;
boot_port->OUTSET.reg = BOOT_PIN_MASK;
// Read the BOOT_LOAD_PIN status
boot_en = (boot_port->IN.reg) & BOOT_PIN_MASK;
// Check the bootloader enable condition
if (!boot_en)
{
// Stay in bootloader
return;
}
#endif
*/
// LED_on();
#ifdef CONFIGURE_PMIC
jump_to_app = true;
#else
jump_to_application();
#endif
}
#if DEBUG_ENABLE
# define DEBUG_PIN_HIGH port_pin_set_output_level(BOOT_LED, 1)
# define DEBUG_PIN_LOW port_pin_set_output_level(BOOT_LED, 0)
#else
# define DEBUG_PIN_HIGH do{}while(0)
# define DEBUG_PIN_LOW do{}while(0)
#endif
/**
* \brief SAMD21 SAM-BA Main loop.
* \return Unused (ANSI-C compatibility).
*/
int main(void)
{
#if defined(SAM_BA_USBCDC_ONLY) || defined(SAM_BA_BOTH_INTERFACES)
P_USB_CDC pCdc;
#endif
DEBUG_PIN_HIGH;
/* Jump in application if condition is satisfied */
check_start_application();
/* We have determined we should stay in the monitor. */
/* System initialization */
board_init();
__enable_irq();
#ifdef CONFIGURE_PMIC
configure_pmic();
#endif
#ifdef ENABLE_JTAG_LOAD
uint32_t temp ;
// Get whole current setup for both odd and even pins and remove odd one
temp = (PORT->Group[0].PMUX[27 >> 1].reg) & PORT_PMUX_PMUXE( 0xF ) ;
// Set new muxing
PORT->Group[0].PMUX[27 >> 1].reg = temp|PORT_PMUX_PMUXO( 7 ) ;
// Enable port mux
PORT->Group[0].PINCFG[27].reg |= PORT_PINCFG_PMUXEN ;
clockout(0, 1);
jtagInit();
if ((jtagBitstreamVersion() & 0xFF000000) != 0xB0000000) {
// FPGA is not in the bootloader, restart it
jtagReload();
}
#endif
#ifdef CONFIGURE_PMIC
if (jump_to_app == true) {
jump_to_application();
}
#endif
#if defined(SAM_BA_UART_ONLY) || defined(SAM_BA_BOTH_INTERFACES)
/* UART is enabled in all cases */
serial_open();
#endif
#if defined(SAM_BA_USBCDC_ONLY) || defined(SAM_BA_BOTH_INTERFACES)
pCdc = usb_init();
#endif
DEBUG_PIN_LOW;
/* Initialize LEDs */
LED_init();
LEDRX_init();
LEDRX_off();
LEDTX_init();
LEDTX_off();
/* Start the sys tick (1 ms) */
SysTick_Config(1000);
/* Wait for a complete enum on usb or a '#' char on serial line */
while (1)
{
#if defined(SAM_BA_USBCDC_ONLY) || defined(SAM_BA_BOTH_INTERFACES)
if (pCdc->IsConfigured(pCdc) != 0)
{
main_b_cdc_enable = true;
}
/* Check if a USB enumeration has succeeded and if comm port has been opened */
if (main_b_cdc_enable)
{
sam_ba_monitor_init(SAM_BA_INTERFACE_USBCDC);
/* SAM-BA on USB loop */
while( 1 )
{
sam_ba_monitor_run();
}
}
#endif
#if defined(SAM_BA_UART_ONLY) || defined(SAM_BA_BOTH_INTERFACES)
/* Check if a '#' has been received */
if (!main_b_cdc_enable && serial_sharp_received())
{
sam_ba_monitor_init(SAM_BA_INTERFACE_USART);
/* SAM-BA on Serial loop */
while(1)
{
sam_ba_monitor_run();
}
}
#endif
}
}
void SysTick_Handler(void)
{
LED_pulse();
sam_ba_monitor_sys_tick();
}