-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.c
287 lines (244 loc) · 8.27 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
281
282
283
284
285
286
287
/**
* \file
*
* \brief Main functions for MSC example
*
* Copyright (c) 2009-2014 Atmel Corporation. All rights reserved.
*
* \asf_license_start
*
* \page License
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. The name of Atmel may not be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* 4. This software may only be redistributed and used in connection with an
* Atmel microcontroller product.
*
* THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
* EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* \asf_license_stop
*
*/
#include "main.h"
static volatile bool main_b_msc_enable = false;
struct adc_module adc_instance;
void configure_adc(void)
{
struct adc_config config_adc;
adc_get_config_defaults(&config_adc);
config_adc.clock_prescaler = ADC_CLOCK_PRESCALER_DIV32;
config_adc.positive_input = ADC_POSITIVE_INPUT_PIN7;
config_adc.negative_input = ADC_NEGATIVE_INPUT_GND;
adc_init(&adc_instance, ADC, &config_adc);
adc_enable(&adc_instance);
}
/**
* \brief Function for starting application
*
* This function will configure the WDT module and enable it. The LED is
* kept toggling till WDT reset occurs.
*/
void start_application(void)
{
struct wdt_conf wdt_config;
/* Turn off LED */
port_pin_set_output_level(BOOT_LED, false);
/* Get WDT default configuration */
wdt_get_config_defaults(&wdt_config);
/* Set the required clock source and timeout period */
wdt_config.clock_source = GCLK_GENERATOR_4;
wdt_config.timeout_period = WDT_PERIOD_256CLK;
/* Initialize and enable the Watchdog with the user settings */
wdt_set_config(&wdt_config);
/* Turn ON LED after watchdog has initialized */
port_pin_set_output_level(BOOT_LED, true);
while (1) {
/* Wait for watchdog reset */
}
}
/**
* \brief Function for checking whether to enter boot mode or application mode
*
* This function will check the state of BOOT_LOAD_PIN. If it is pressed, it
* continues execution in bootloader mode. Else, it reads the first location
* from the application section and checks whether it is 0xFFFFFFFF. If yes,
* then the application section is empty and it waits indefinitely there. If
* not, it jumps to the application and starts execution from there.
* Access to direct peripheral registers are made in this routine to enable
* quick decision on application or bootloader mode.
*/
void check_boot_mode(void)
{
uint32_t app_check_address;
uint32_t *app_check_address_ptr;
/* Check if WDT is locked */
if (!(WDT->CTRL.reg & WDT_CTRL_ALWAYSON)) {
/* Disable the Watchdog module */
WDT->CTRL.reg &= ~WDT_CTRL_ENABLE;
}
delay_init();
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 = GPIO_BOOT_PIN_MASK;
boot_port->PINCFG[BOOT_LOAD_PIN & 0x1F].reg = PORT_PINCFG_INEN | PORT_PINCFG_PULLEN;
boot_port->OUTSET.reg = GPIO_BOOT_PIN_MASK;
delay_ms(8); // Allow 100nF debouncing capacitor to charge
/* Read the BOOT_LOAD_PIN status */
boot_en = ((boot_port->IN.reg) & GPIO_BOOT_PIN_MASK);
#if (BOOT_TEST_VBUS_VALUE != 0)
uint16_t vbus_voltage;
adc_start_conversion(&adc_instance);
do {
/* Wait for conversion to be done and read out result */
} while (adc_read(&adc_instance, &vbus_voltage) == STATUS_BUSY);
/* Check the BOOT pin, vbus voltage, or the reset cause is Watchdog */
if (((boot_en) && (vbus_voltage < BOOT_TEST_VBUS_VALUE)) || (PM->RCAUSE.reg & PM_RCAUSE_WDT)) {
#else
/* Check the BOOT pin or the reset cause is Watchdog */
if ((boot_en) || (PM->RCAUSE.reg & PM_RCAUSE_WDT)) {
#endif
app_check_address = APP_START_ADDRESS;
app_check_address_ptr = (uint32_t *) app_check_address;
/*
* Read the first location of application section
* which contains the address of stack pointer.
* If it is 0xFFFFFFFF then the application section is empty.
*/
if (*app_check_address_ptr == 0xFFFFFFFF) {
while (1) {
/* Wait indefinitely */
}
}
/* Pointer to the Application Section */
void (*application_code_entry)(void);
/* Rebase the Stack Pointer */
__set_MSP(*(uint32_t *) APP_START_ADDRESS);
/* Rebase the vector table base address */
SCB->VTOR = ((uint32_t) APP_START_ADDRESS & SCB_VTOR_TBLOFF_Msk);
/* Load the Reset Handler address of the application */
application_code_entry = (void (*)(void))(unsigned *)(*(unsigned *)(APP_START_ADDRESS + 4));
/* Jump to user Reset Handler in the application */
application_code_entry();
}
}
/*! \brief Main function. Execution starts here.
*/
int main(void)
{
struct nvm_config config;
/* Check switch state to enter boot mode or application mode */
#if (BOOT_TEST_VBUS_VALUE != 0)
configure_adc();
#endif
check_boot_mode();
irq_initialize_vectors();
cpu_irq_enable();
// Initialize the sleep manager
sleepmgr_init();
system_init();
/* Get NVM default configuration and load the same */
nvm_get_config_defaults(&config);
nvm_set_config(&config);
/* Turn on LED */
port_pin_set_output_level(BOOT_LED, true);
ui_init();
ui_powerdown();
// Start USB stack to authorize VBus monitoring
udc_start();
// The main loop manages only the power mode
// because the USB management is done by interrupt
while (true) {
if (main_b_msc_enable) {
if (!udi_msc_process_trans()) {
sleepmgr_enter_sleep();
}
}else{
sleepmgr_enter_sleep();
}
}
start_application();
}
void main_suspend_action(void)
{
ui_powerdown();
}
void main_resume_action(void)
{
ui_wakeup();
}
void main_sof_action(void)
{
if (!main_b_msc_enable)
return;
ui_process(udd_get_frame_number());
}
bool main_msc_enable(void)
{
main_b_msc_enable = true;
return true;
}
void main_msc_disable(void)
{
main_b_msc_enable = false;
}
/**
* \mainpage ASF USB Device MSC
*
* \section intro Introduction
* This example shows how to implement a USB Device Mass Storage
* on Atmel MCU with USB module.
*
* \section startup Startup
* The example uses all memories available on the board and connects these to
* USB Device Mass Storage stack. After loading firmware, connect the board
* (EVKxx,Xplain,...) to the USB Host. When connected to a USB host system
* this application allows to display all available memories as a
* removable disks in the Unix/Mac/Windows operating systems.
* \note
* This example uses the native MSC driver on Unix/Mac/Windows OS, except for Win98.
*
* \copydoc UI
*
* \section example About example
*
* The example uses the following module groups:
* - Basic modules:
* Startup, board, clock, interrupt, power management
* - USB Device stack and MSC modules:
* <br>services/usb/
* <br>services/usb/udc/
* <br>services/usb/class/msc/
* - Specific implementation:
* - main.c,
* <br>initializes clock
* <br>initializes interrupt
* <br>manages UI
* - specific implementation for each target "./examples/product_board/":
* - conf_foo.h configuration of each module
* - ui.c implement of user's interface (leds)
*
* <SUP>1</SUP> The memory data transfers are done outside USB interrupt routine.
* This is done in the MSC process ("udi_msc_process_trans()") called by main loop.
*/