-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver_init.c
425 lines (329 loc) · 11.4 KB
/
driver_init.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/*
* Code generated from Atmel Start.
*
* This file will be overwritten when reconfiguring your Atmel Start project.
* Please copy examples or other code you want to keep to a separate file
* to avoid losing it when reconfiguring.
*/
#include "driver_init.h"
#include <peripheral_clk_config.h>
#include <utils.h>
#include <hal_init.h>
#include <hpl_gclk_base.h>
#include <hpl_pm_base.h>
/*! The buffer size for USART */
#define DEBUG_USART_BUFFER_SIZE 16
/*! The buffer size for USART */
#define DCE_USART_BUFFER_SIZE 16
struct usart_async_descriptor DEBUG_USART;
struct spi_m_sync_descriptor SPI_0;
struct usart_async_descriptor DCE_USART;
static uint8_t DEBUG_USART_buffer[DEBUG_USART_BUFFER_SIZE];
static uint8_t DCE_USART_buffer[DCE_USART_BUFFER_SIZE];
struct i2c_m_sync_desc I2C_0;
struct wdt_descriptor WDT_0;
/**
* \brief USART Clock initialization function
*
* Enables register interface and peripheral clock
*/
void DEBUG_USART_CLOCK_init()
{
_pm_enable_bus_clock(PM_BUS_APBC, SERCOM0);
_gclk_enable_channel(SERCOM0_GCLK_ID_CORE, CONF_GCLK_SERCOM0_CORE_SRC);
}
/**
* \brief USART pinmux initialization function
*
* Set each required pin to USART functionality
*/
void DEBUG_USART_PORT_init()
{
gpio_set_pin_function(DEBUG_TX, PINMUX_PA08C_SERCOM0_PAD0);
gpio_set_pin_function(DEBUG_RX, PINMUX_PA09C_SERCOM0_PAD1);
}
/**
* \brief USART initialization function
*
* Enables USART peripheral, clocks and initializes USART driver
*/
void DEBUG_USART_init(void)
{
DEBUG_USART_CLOCK_init();
usart_async_init(&DEBUG_USART, SERCOM0, DEBUG_USART_buffer, DEBUG_USART_BUFFER_SIZE, (void *)NULL);
DEBUG_USART_PORT_init();
}
void I2C_0_PORT_init(void)
{
gpio_set_pin_pull_mode(SDA,
// <y> Pull configuration
// <id> pad_pull_config
// <GPIO_PULL_OFF"> Off
// <GPIO_PULL_UP"> Pull-up
// <GPIO_PULL_DOWN"> Pull-down
GPIO_PULL_OFF);
gpio_set_pin_function(SDA, PINMUX_PA16C_SERCOM1_PAD0);
gpio_set_pin_pull_mode(SCL,
// <y> Pull configuration
// <id> pad_pull_config
// <GPIO_PULL_OFF"> Off
// <GPIO_PULL_UP"> Pull-up
// <GPIO_PULL_DOWN"> Pull-down
GPIO_PULL_OFF);
gpio_set_pin_function(SCL, PINMUX_PA17C_SERCOM1_PAD1);
}
void I2C_0_CLOCK_init(void)
{
_pm_enable_bus_clock(PM_BUS_APBC, SERCOM1);
_gclk_enable_channel(SERCOM1_GCLK_ID_CORE, CONF_GCLK_SERCOM1_CORE_SRC);
_gclk_enable_channel(SERCOM1_GCLK_ID_SLOW, CONF_GCLK_SERCOM1_SLOW_SRC);
}
void I2C_0_init(void)
{
I2C_0_CLOCK_init();
i2c_m_sync_init(&I2C_0, SERCOM1);
I2C_0_PORT_init();
}
void SPI_0_PORT_init(void)
{
gpio_set_pin_level(SPI_MOSI,
// <y> Initial level
// <id> pad_initial_level
// <false"> Low
// <true"> High
false);
// Set pin direction to output
gpio_set_pin_direction(SPI_MOSI, GPIO_DIRECTION_OUT);
gpio_set_pin_function(SPI_MOSI, PINMUX_PA22C_SERCOM3_PAD0);
gpio_set_pin_level(SPI_SCK,
// <y> Initial level
// <id> pad_initial_level
// <false"> Low
// <true"> High
false);
// Set pin direction to output
gpio_set_pin_direction(SPI_SCK, GPIO_DIRECTION_OUT);
gpio_set_pin_function(SPI_SCK, PINMUX_PA23C_SERCOM3_PAD1);
// Set pin direction to input
gpio_set_pin_direction(SPI_MISO, GPIO_DIRECTION_IN);
gpio_set_pin_pull_mode(SPI_MISO,
// <y> Pull configuration
// <id> pad_pull_config
// <GPIO_PULL_OFF"> Off
// <GPIO_PULL_UP"> Pull-up
// <GPIO_PULL_DOWN"> Pull-down
GPIO_PULL_OFF);
gpio_set_pin_function(SPI_MISO, PINMUX_PA20D_SERCOM3_PAD2);
}
void SPI_0_CLOCK_init(void)
{
_pm_enable_bus_clock(PM_BUS_APBC, SERCOM3);
_gclk_enable_channel(SERCOM3_GCLK_ID_CORE, CONF_GCLK_SERCOM3_CORE_SRC);
}
void SPI_0_init(void)
{
SPI_0_CLOCK_init();
spi_m_sync_init(&SPI_0, SERCOM3);
SPI_0_PORT_init();
}
/**
* \brief USART Clock initialization function
*
* Enables register interface and peripheral clock
*/
void DCE_USART_CLOCK_init()
{
_pm_enable_bus_clock(PM_BUS_APBC, SERCOM4);
_gclk_enable_channel(SERCOM4_GCLK_ID_CORE, CONF_GCLK_SERCOM4_CORE_SRC);
}
/**
* \brief USART pinmux initialization function
*
* Set each required pin to USART functionality
*/
void DCE_USART_PORT_init()
{
gpio_set_pin_function(DCE_RX, PINMUX_PB12C_SERCOM4_PAD0);
gpio_set_pin_function(DCE_TX, PINMUX_PB13C_SERCOM4_PAD1);
}
/**
* \brief USART initialization function
*
* Enables USART peripheral, clocks and initializes USART driver
*/
void DCE_USART_init(void)
{
DCE_USART_CLOCK_init();
usart_async_init(&DCE_USART, SERCOM4, DCE_USART_buffer, DCE_USART_BUFFER_SIZE, (void *)NULL);
DCE_USART_PORT_init();
}
void WDT_0_CLOCK_init(void)
{
_pm_enable_bus_clock(PM_BUS_APBA, WDT);
_gclk_enable_channel(WDT_GCLK_ID, CONF_GCLK_WDT_SRC);
}
void WDT_0_init(void)
{
WDT_0_CLOCK_init();
wdt_init(&WDT_0, WDT);
}
void system_init(void)
{
init_mcu();
// GPIO on PA02
// Set pin direction to input
gpio_set_pin_direction(PWR_GOOD_3V8, GPIO_DIRECTION_IN);
gpio_set_pin_pull_mode(PWR_GOOD_3V8,
// <y> Pull configuration
// <id> pad_pull_config
// <GPIO_PULL_OFF"> Off
// <GPIO_PULL_UP"> Pull-up
// <GPIO_PULL_DOWN"> Pull-down
GPIO_PULL_OFF);
gpio_set_pin_function(PWR_GOOD_3V8, GPIO_PIN_FUNCTION_OFF);
// GPIO on PA05
gpio_set_pin_level(LED_R,
// <y> Initial level
// <id> pad_initial_level
// <false"> Low
// <true"> High
false);
// Set pin direction to output
gpio_set_pin_direction(LED_R, GPIO_DIRECTION_OUT);
gpio_set_pin_function(LED_R, GPIO_PIN_FUNCTION_OFF);
// GPIO on PA06
gpio_set_pin_level(LED_G,
// <y> Initial level
// <id> pad_initial_level
// <false"> Low
// <true"> High
false);
// Set pin direction to output
gpio_set_pin_direction(LED_G, GPIO_DIRECTION_OUT);
gpio_set_pin_function(LED_G, GPIO_PIN_FUNCTION_OFF);
// GPIO on PA07
gpio_set_pin_level(LED_B,
// <y> Initial level
// <id> pad_initial_level
// <false"> Low
// <true"> High
false);
// Set pin direction to output
gpio_set_pin_direction(LED_B, GPIO_DIRECTION_OUT);
gpio_set_pin_function(LED_B, GPIO_PIN_FUNCTION_OFF);
// GPIO on PA14
gpio_set_pin_function(DCE_RTS, GPIO_PIN_FUNCTION_OFF);
// GPIO on PA15
gpio_set_pin_direction(OSC32_OUT,
// <y> Pin direction
// <id> pad_direction
// <GPIO_DIRECTION_OFF"> Off
// <GPIO_DIRECTION_IN"> In
// <GPIO_DIRECTION_OUT"> Out
GPIO_DIRECTION_OUT);
gpio_set_pin_level(OSC32_OUT,
// <y> Initial level
// <id> pad_initial_level
// <false"> Low
// <true"> High
false);
gpio_set_pin_pull_mode(OSC32_OUT,
// <y> Pull configuration
// <id> pad_pull_config
// <GPIO_PULL_OFF"> Off
// <GPIO_PULL_UP"> Pull-up
// <GPIO_PULL_DOWN"> Pull-down
GPIO_PULL_OFF);
gpio_set_pin_function(OSC32_OUT,
// <y> Pin function
// <id> pad_function
// <i> Auto : use driver pinmux if signal is imported by driver, else turn off function
// <GPIO_PIN_FUNCTION_OFF"> Auto
// <GPIO_PIN_FUNCTION_OFF"> Off
// <GPIO_PIN_FUNCTION_A"> A
// <GPIO_PIN_FUNCTION_B"> B
// <GPIO_PIN_FUNCTION_C"> C
// <GPIO_PIN_FUNCTION_D"> D
// <GPIO_PIN_FUNCTION_E"> E
// <GPIO_PIN_FUNCTION_F"> F
// <GPIO_PIN_FUNCTION_G"> G
// <GPIO_PIN_FUNCTION_H"> H
GPIO_PIN_FUNCTION_H);
// GPIO on PA18
gpio_set_pin_level(DCE_W_DIS,
// <y> Initial level
// <id> pad_initial_level
// <false"> Low
// <true"> High
false);
// Set pin direction to output
gpio_set_pin_direction(DCE_W_DIS, GPIO_DIRECTION_OUT);
gpio_set_pin_function(DCE_W_DIS, GPIO_PIN_FUNCTION_OFF);
// GPIO on PA19
gpio_set_pin_level(DCE_RESET,
// <y> Initial level
// <id> pad_initial_level
// <false"> Low
// <true"> High
true);
// Set pin direction to output
gpio_set_pin_direction(DCE_RESET, GPIO_DIRECTION_OUT);
gpio_set_pin_function(DCE_RESET, GPIO_PIN_FUNCTION_OFF);
// GPIO on PA21
gpio_set_pin_level(SPI_CS,
// <y> Initial level
// <id> pad_initial_level
// <false"> Low
// <true"> High
true);
// Set pin direction to output
gpio_set_pin_direction(SPI_CS, GPIO_DIRECTION_OUT);
gpio_set_pin_function(SPI_CS, GPIO_PIN_FUNCTION_OFF);
// GPIO on PB01
gpio_set_pin_level(DCE_PWRKEY,
// <y> Initial level
// <id> pad_initial_level
// <false"> Low
// <true"> High
false);
// Set pin direction to output
gpio_set_pin_direction(DCE_PWRKEY, GPIO_DIRECTION_OUT);
gpio_set_pin_function(DCE_PWRKEY, GPIO_PIN_FUNCTION_OFF);
// GPIO on PB04
// Set pin direction to input
gpio_set_pin_direction(BUTTON, GPIO_DIRECTION_IN);
gpio_set_pin_pull_mode(BUTTON,
// <y> Pull configuration
// <id> pad_pull_config
// <GPIO_PULL_OFF"> Off
// <GPIO_PULL_UP"> Pull-up
// <GPIO_PULL_DOWN"> Pull-down
GPIO_PULL_OFF);
gpio_set_pin_function(BUTTON, GPIO_PIN_FUNCTION_OFF);
// GPIO on PB15
// Set pin direction to input
gpio_set_pin_direction(DCE_CTS, GPIO_DIRECTION_IN);
gpio_set_pin_pull_mode(DCE_CTS,
// <y> Pull configuration
// <id> pad_pull_config
// <GPIO_PULL_OFF"> Off
// <GPIO_PULL_UP"> Pull-up
// <GPIO_PULL_DOWN"> Pull-down
GPIO_PULL_OFF);
gpio_set_pin_function(DCE_CTS, GPIO_PIN_FUNCTION_OFF);
// GPIO on PB17
gpio_set_pin_level(EN_3V8,
// <y> Initial level
// <id> pad_initial_level
// <false"> Low
// <true"> High
false);
// Set pin direction to output
gpio_set_pin_direction(EN_3V8, GPIO_DIRECTION_OUT);
gpio_set_pin_function(EN_3V8, GPIO_PIN_FUNCTION_OFF);
DEBUG_USART_init();
I2C_0_init();
SPI_0_init();
DCE_USART_init();
WDT_0_init();
}