blob: 4ab622f0f355a4ede49aa0b6a6f5fbff605aff56 [file] [log] [blame]
Kévin Redon69b92d92019-01-24 16:39:20 +01001/*
2 * Code generated from Atmel Start.
3 *
4 * This file will be overwritten when reconfiguring your Atmel Start project.
5 * Please copy examples or other code you want to keep to a separate file
6 * to avoid losing it when reconfiguring.
7 */
8
9#include "driver_init.h"
10#include <peripheral_clk_config.h>
11#include <utils.h>
12#include <hal_init.h>
13
Kévin Redonccbed0b2019-01-24 18:30:26 +010014/*! The buffer size for USART */
15#define UART_DEBUG_BUFFER_SIZE 16
Kévin Redon4cd3f7d2019-01-24 17:57:13 +010016
Kévin Redonccbed0b2019-01-24 18:30:26 +010017struct usart_async_descriptor UART_debug;
18
19static uint8_t UART_debug_buffer[UART_DEBUG_BUFFER_SIZE];
20
21/**
22 * \brief USART Clock initialization function
23 *
24 * Enables register interface and peripheral clock
25 */
26void UART_debug_CLOCK_init()
27{
28
Kévin Redon4e39b012019-01-30 15:55:58 +010029 hri_gclk_write_PCHCTRL_reg(GCLK, SERCOM7_GCLK_ID_CORE, CONF_GCLK_SERCOM7_CORE_SRC | (1 << GCLK_PCHCTRL_CHEN_Pos));
30 hri_gclk_write_PCHCTRL_reg(GCLK, SERCOM7_GCLK_ID_SLOW, CONF_GCLK_SERCOM7_SLOW_SRC | (1 << GCLK_PCHCTRL_CHEN_Pos));
Kévin Redonccbed0b2019-01-24 18:30:26 +010031
Kévin Redon4e39b012019-01-30 15:55:58 +010032 hri_mclk_set_APBDMASK_SERCOM7_bit(MCLK);
Kévin Redonccbed0b2019-01-24 18:30:26 +010033}
34
35/**
36 * \brief USART pinmux initialization function
37 *
38 * Set each required pin to USART functionality
39 */
40void UART_debug_PORT_init()
Kévin Redon4cd3f7d2019-01-24 17:57:13 +010041{
42
Kévin Redon4e39b012019-01-30 15:55:58 +010043 gpio_set_pin_function(UART_TX, PINMUX_PB30C_SERCOM7_PAD0);
Kévin Redon4cd3f7d2019-01-24 17:57:13 +010044
Kévin Redon4e39b012019-01-30 15:55:58 +010045 gpio_set_pin_function(UART_RX, PINMUX_PB31C_SERCOM7_PAD1);
Kévin Redon4cd3f7d2019-01-24 17:57:13 +010046}
47
Kévin Redonccbed0b2019-01-24 18:30:26 +010048/**
49 * \brief USART initialization function
50 *
51 * Enables USART peripheral, clocks and initializes USART driver
52 */
Kévin Redon4cd3f7d2019-01-24 17:57:13 +010053void UART_debug_init(void)
54{
55 UART_debug_CLOCK_init();
Kévin Redon4e39b012019-01-30 15:55:58 +010056 usart_async_init(&UART_debug, SERCOM7, UART_debug_buffer, UART_DEBUG_BUFFER_SIZE, (void *)NULL);
Kévin Redon4cd3f7d2019-01-24 17:57:13 +010057 UART_debug_PORT_init();
58}
59
Kévin Redon69b92d92019-01-24 16:39:20 +010060void USB_DEVICE_INSTANCE_PORT_init(void)
61{
62
Kévin Redon4e39b012019-01-30 15:55:58 +010063 gpio_set_pin_direction(USBUP_D_N,
Kévin Redon69b92d92019-01-24 16:39:20 +010064 // <y> Pin direction
65 // <id> pad_direction
66 // <GPIO_DIRECTION_OFF"> Off
67 // <GPIO_DIRECTION_IN"> In
68 // <GPIO_DIRECTION_OUT"> Out
69 GPIO_DIRECTION_OUT);
70
Kévin Redon4e39b012019-01-30 15:55:58 +010071 gpio_set_pin_level(USBUP_D_N,
Kévin Redon69b92d92019-01-24 16:39:20 +010072 // <y> Initial level
73 // <id> pad_initial_level
74 // <false"> Low
75 // <true"> High
76 false);
77
Kévin Redon4e39b012019-01-30 15:55:58 +010078 gpio_set_pin_pull_mode(USBUP_D_N,
Kévin Redon69b92d92019-01-24 16:39:20 +010079 // <y> Pull configuration
80 // <id> pad_pull_config
81 // <GPIO_PULL_OFF"> Off
82 // <GPIO_PULL_UP"> Pull-up
83 // <GPIO_PULL_DOWN"> Pull-down
84 GPIO_PULL_OFF);
85
Kévin Redon4e39b012019-01-30 15:55:58 +010086 gpio_set_pin_function(USBUP_D_N,
Kévin Redon69b92d92019-01-24 16:39:20 +010087 // <y> Pin function
88 // <id> pad_function
89 // <i> Auto : use driver pinmux if signal is imported by driver, else turn off function
90 // <PINMUX_PA24H_USB_DM"> Auto
91 // <GPIO_PIN_FUNCTION_OFF"> Off
92 // <GPIO_PIN_FUNCTION_A"> A
93 // <GPIO_PIN_FUNCTION_B"> B
94 // <GPIO_PIN_FUNCTION_C"> C
95 // <GPIO_PIN_FUNCTION_D"> D
96 // <GPIO_PIN_FUNCTION_E"> E
97 // <GPIO_PIN_FUNCTION_F"> F
98 // <GPIO_PIN_FUNCTION_G"> G
99 // <GPIO_PIN_FUNCTION_H"> H
100 // <GPIO_PIN_FUNCTION_I"> I
101 // <GPIO_PIN_FUNCTION_J"> J
102 // <GPIO_PIN_FUNCTION_K"> K
103 // <GPIO_PIN_FUNCTION_L"> L
104 // <GPIO_PIN_FUNCTION_M"> M
105 // <GPIO_PIN_FUNCTION_N"> N
106 PINMUX_PA24H_USB_DM);
107
Kévin Redon4e39b012019-01-30 15:55:58 +0100108 gpio_set_pin_direction(USBUP_D_P,
Kévin Redon69b92d92019-01-24 16:39:20 +0100109 // <y> Pin direction
110 // <id> pad_direction
111 // <GPIO_DIRECTION_OFF"> Off
112 // <GPIO_DIRECTION_IN"> In
113 // <GPIO_DIRECTION_OUT"> Out
114 GPIO_DIRECTION_OUT);
115
Kévin Redon4e39b012019-01-30 15:55:58 +0100116 gpio_set_pin_level(USBUP_D_P,
Kévin Redon69b92d92019-01-24 16:39:20 +0100117 // <y> Initial level
118 // <id> pad_initial_level
119 // <false"> Low
120 // <true"> High
121 false);
122
Kévin Redon4e39b012019-01-30 15:55:58 +0100123 gpio_set_pin_pull_mode(USBUP_D_P,
Kévin Redon69b92d92019-01-24 16:39:20 +0100124 // <y> Pull configuration
125 // <id> pad_pull_config
126 // <GPIO_PULL_OFF"> Off
127 // <GPIO_PULL_UP"> Pull-up
128 // <GPIO_PULL_DOWN"> Pull-down
129 GPIO_PULL_OFF);
130
Kévin Redon4e39b012019-01-30 15:55:58 +0100131 gpio_set_pin_function(USBUP_D_P,
Kévin Redon69b92d92019-01-24 16:39:20 +0100132 // <y> Pin function
133 // <id> pad_function
134 // <i> Auto : use driver pinmux if signal is imported by driver, else turn off function
135 // <PINMUX_PA25H_USB_DP"> Auto
136 // <GPIO_PIN_FUNCTION_OFF"> Off
137 // <GPIO_PIN_FUNCTION_A"> A
138 // <GPIO_PIN_FUNCTION_B"> B
139 // <GPIO_PIN_FUNCTION_C"> C
140 // <GPIO_PIN_FUNCTION_D"> D
141 // <GPIO_PIN_FUNCTION_E"> E
142 // <GPIO_PIN_FUNCTION_F"> F
143 // <GPIO_PIN_FUNCTION_G"> G
144 // <GPIO_PIN_FUNCTION_H"> H
145 // <GPIO_PIN_FUNCTION_I"> I
146 // <GPIO_PIN_FUNCTION_J"> J
147 // <GPIO_PIN_FUNCTION_K"> K
148 // <GPIO_PIN_FUNCTION_L"> L
149 // <GPIO_PIN_FUNCTION_M"> M
150 // <GPIO_PIN_FUNCTION_N"> N
151 PINMUX_PA25H_USB_DP);
152}
153
154/* The USB module requires a GCLK_USB of 48 MHz ~ 0.25% clock
155 * for low speed and full speed operation. */
156#if (CONF_GCLK_USB_FREQUENCY > (48000000 + 48000000 / 400)) || (CONF_GCLK_USB_FREQUENCY < (48000000 - 48000000 / 400))
157#warning USB clock should be 48MHz ~ 0.25% clock, check your configuration!
158#endif
159
160void USB_DEVICE_INSTANCE_CLOCK_init(void)
161{
162
163 hri_gclk_write_PCHCTRL_reg(GCLK, USB_GCLK_ID, CONF_GCLK_USB_SRC | GCLK_PCHCTRL_CHEN);
164 hri_mclk_set_AHBMASK_USB_bit(MCLK);
165 hri_mclk_set_APBBMASK_USB_bit(MCLK);
166}
167
168void USB_DEVICE_INSTANCE_init(void)
169{
170 USB_DEVICE_INSTANCE_CLOCK_init();
171 usb_d_init();
172 USB_DEVICE_INSTANCE_PORT_init();
173}
174
175void system_init(void)
176{
177 init_mcu();
178
Kévin Redon6a8295c2019-01-30 18:58:44 +0100179 // GPIO on PA11
180
181 gpio_set_pin_direction(SIMCLK_20MHZ,
182 // <y> Pin direction
183 // <id> pad_direction
184 // <GPIO_DIRECTION_OFF"> Off
185 // <GPIO_DIRECTION_IN"> In
186 // <GPIO_DIRECTION_OUT"> Out
187 GPIO_DIRECTION_OUT);
188
189 gpio_set_pin_level(SIMCLK_20MHZ,
190 // <y> Initial level
191 // <id> pad_initial_level
192 // <false"> Low
193 // <true"> High
194 false);
195
196 gpio_set_pin_pull_mode(SIMCLK_20MHZ,
197 // <y> Pull configuration
198 // <id> pad_pull_config
199 // <GPIO_PULL_OFF"> Off
200 // <GPIO_PULL_UP"> Pull-up
201 // <GPIO_PULL_DOWN"> Pull-down
202 GPIO_PULL_OFF);
203
204 gpio_set_pin_function(SIMCLK_20MHZ,
205 // <y> Pin function
206 // <id> pad_function
207 // <i> Auto : use driver pinmux if signal is imported by driver, else turn off function
208 // <GPIO_PIN_FUNCTION_OFF"> Auto
209 // <GPIO_PIN_FUNCTION_OFF"> Off
210 // <GPIO_PIN_FUNCTION_A"> A
211 // <GPIO_PIN_FUNCTION_B"> B
212 // <GPIO_PIN_FUNCTION_C"> C
213 // <GPIO_PIN_FUNCTION_D"> D
214 // <GPIO_PIN_FUNCTION_E"> E
215 // <GPIO_PIN_FUNCTION_F"> F
216 // <GPIO_PIN_FUNCTION_G"> G
217 // <GPIO_PIN_FUNCTION_H"> H
218 // <GPIO_PIN_FUNCTION_I"> I
219 // <GPIO_PIN_FUNCTION_J"> J
220 // <GPIO_PIN_FUNCTION_K"> K
221 // <GPIO_PIN_FUNCTION_L"> L
222 // <GPIO_PIN_FUNCTION_M"> M
223 // <GPIO_PIN_FUNCTION_N"> N
224 GPIO_PIN_FUNCTION_M);
225
226 // GPIO on PA20
227
228 // Set pin direction to input
229 gpio_set_pin_direction(VB0, GPIO_DIRECTION_IN);
230
231 gpio_set_pin_pull_mode(VB0,
232 // <y> Pull configuration
233 // <id> pad_pull_config
234 // <GPIO_PULL_OFF"> Off
235 // <GPIO_PULL_UP"> Pull-up
236 // <GPIO_PULL_DOWN"> Pull-down
237 GPIO_PULL_OFF);
238
239 gpio_set_pin_function(VB0, GPIO_PIN_FUNCTION_OFF);
240
241 // GPIO on PA21
242
243 // Set pin direction to input
244 gpio_set_pin_direction(VB1, GPIO_DIRECTION_IN);
245
246 gpio_set_pin_pull_mode(VB1,
247 // <y> Pull configuration
248 // <id> pad_pull_config
249 // <GPIO_PULL_OFF"> Off
250 // <GPIO_PULL_UP"> Pull-up
251 // <GPIO_PULL_DOWN"> Pull-down
252 GPIO_PULL_OFF);
253
254 gpio_set_pin_function(VB1, GPIO_PIN_FUNCTION_OFF);
255
256 // GPIO on PA22
257
258 // Set pin direction to input
259 gpio_set_pin_direction(VB2, GPIO_DIRECTION_IN);
260
261 gpio_set_pin_pull_mode(VB2,
262 // <y> Pull configuration
263 // <id> pad_pull_config
264 // <GPIO_PULL_OFF"> Off
265 // <GPIO_PULL_UP"> Pull-up
266 // <GPIO_PULL_DOWN"> Pull-down
267 GPIO_PULL_OFF);
268
269 gpio_set_pin_function(VB2, GPIO_PIN_FUNCTION_OFF);
270
271 // GPIO on PA23
272
273 // Set pin direction to input
274 gpio_set_pin_direction(VB3, GPIO_DIRECTION_IN);
275
276 gpio_set_pin_pull_mode(VB3,
277 // <y> Pull configuration
278 // <id> pad_pull_config
279 // <GPIO_PULL_OFF"> Off
280 // <GPIO_PULL_UP"> Pull-up
281 // <GPIO_PULL_DOWN"> Pull-down
282 GPIO_PULL_OFF);
283
284 gpio_set_pin_function(VB3, GPIO_PIN_FUNCTION_OFF);
285
286 // GPIO on PC14
287
288 // Set pin direction to input
289 gpio_set_pin_direction(SWITCH, GPIO_DIRECTION_IN);
290
291 gpio_set_pin_pull_mode(SWITCH,
292 // <y> Pull configuration
293 // <id> pad_pull_config
294 // <GPIO_PULL_OFF"> Off
295 // <GPIO_PULL_UP"> Pull-up
296 // <GPIO_PULL_DOWN"> Pull-down
297 GPIO_PULL_OFF);
298
299 gpio_set_pin_function(SWITCH, GPIO_PIN_FUNCTION_OFF);
300
Kévin Redon4e39b012019-01-30 15:55:58 +0100301 // GPIO on PC26
Kévin Redon5908a5c2019-01-24 18:45:23 +0100302
Kévin Redon6a8295c2019-01-30 18:58:44 +0100303 gpio_set_pin_level(USER_LED,
Kévin Redon5908a5c2019-01-24 18:45:23 +0100304 // <y> Initial level
305 // <id> pad_initial_level
306 // <false"> Low
307 // <true"> High
Kévin Redon4e39b012019-01-30 15:55:58 +0100308 false);
Kévin Redon5908a5c2019-01-24 18:45:23 +0100309
310 // Set pin direction to output
Kévin Redon6a8295c2019-01-30 18:58:44 +0100311 gpio_set_pin_direction(USER_LED, GPIO_DIRECTION_OUT);
Kévin Redon5908a5c2019-01-24 18:45:23 +0100312
Kévin Redon6a8295c2019-01-30 18:58:44 +0100313 gpio_set_pin_function(USER_LED, GPIO_PIN_FUNCTION_OFF);
Kévin Redon5908a5c2019-01-24 18:45:23 +0100314
Kévin Redon4cd3f7d2019-01-24 17:57:13 +0100315 UART_debug_init();
316
Kévin Redon69b92d92019-01-24 16:39:20 +0100317 USB_DEVICE_INSTANCE_init();
318}