-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdriver_init.c
375 lines (292 loc) · 8.78 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
/*
* 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>
#include <hpl_adc_base.h>
/* The channel amount for ADC */
#define ADC_0_CH_AMOUNT 1
/* The buffer size for ADC */
#define ADC_0_BUFFER_SIZE 16
/* The maximal channel number of enabled channels */
#define ADC_0_CH_MAX 0
/*! The buffer size for USART */
#define USART_LORA_BUFFER_SIZE 16
/*! The buffer size for USART */
#define USART_BT_BUFFER_SIZE 16
/*! The buffer size for USART */
#define DEBUG_BUFFER_SIZE 16
struct adc_async_descriptor ADC_0;
struct adc_async_channel_descriptor ADC_0_ch[ADC_0_CH_AMOUNT];
struct usart_async_descriptor USART_LORA;
struct usart_async_descriptor USART_BT;
struct usart_async_descriptor Debug;
static uint8_t ADC_0_buffer[ADC_0_BUFFER_SIZE];
static uint8_t ADC_0_map[ADC_0_CH_MAX + 1];
static uint8_t USART_LORA_buffer[USART_LORA_BUFFER_SIZE];
static uint8_t USART_BT_buffer[USART_BT_BUFFER_SIZE];
static uint8_t Debug_buffer[DEBUG_BUFFER_SIZE];
struct i2c_m_sync_desc I2C_0;
struct pwm_descriptor PWM_1;
struct pwm_descriptor PWM_0;
struct timer_descriptor TIMER_0;
/**
* \brief ADC initialization function
*
* Enables ADC peripheral, clocks and initializes ADC driver
*/
void ADC_0_init(void)
{
_pm_enable_bus_clock(PM_BUS_APBC, ADC);
_gclk_enable_channel(ADC_GCLK_ID, CONF_GCLK_ADC_SRC);
adc_async_init(&ADC_0, ADC, ADC_0_map, ADC_0_CH_MAX, ADC_0_CH_AMOUNT, &ADC_0_ch[0], (void *)NULL);
adc_async_register_channel_buffer(&ADC_0, 0, ADC_0_buffer, ADC_0_BUFFER_SIZE);
// Disable digital pin circuitry
gpio_set_pin_direction(PA04, GPIO_DIRECTION_OFF);
gpio_set_pin_function(PA04, PINMUX_PA04B_ADC_AIN4);
}
/**
* \brief USART Clock initialization function
*
* Enables register interface and peripheral clock
*/
void USART_LORA_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 USART_LORA_PORT_init()
{
gpio_set_pin_function(PA05, PINMUX_PA05D_SERCOM0_PAD1);
gpio_set_pin_function(PA06, PINMUX_PA06D_SERCOM0_PAD2);
}
/**
* \brief USART initialization function
*
* Enables USART peripheral, clocks and initializes USART driver
*/
void USART_LORA_init(void)
{
USART_LORA_CLOCK_init();
usart_async_init(&USART_LORA, SERCOM0, USART_LORA_buffer, USART_LORA_BUFFER_SIZE, (void *)NULL);
USART_LORA_PORT_init();
}
void I2C_0_PORT_init(void)
{
gpio_set_pin_pull_mode(PA08,
// <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(PA08, PINMUX_PA08D_SERCOM2_PAD0);
gpio_set_pin_pull_mode(PA09,
// <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(PA09, PINMUX_PA09D_SERCOM2_PAD1);
}
void I2C_0_CLOCK_init(void)
{
_pm_enable_bus_clock(PM_BUS_APBC, SERCOM2);
_gclk_enable_channel(SERCOM2_GCLK_ID_CORE, CONF_GCLK_SERCOM2_CORE_SRC);
_gclk_enable_channel(SERCOM2_GCLK_ID_SLOW, CONF_GCLK_SERCOM2_SLOW_SRC);
}
void I2C_0_init(void)
{
I2C_0_CLOCK_init();
i2c_m_sync_init(&I2C_0, SERCOM2);
I2C_0_PORT_init();
}
/**
* \brief USART Clock initialization function
*
* Enables register interface and peripheral clock
*/
void USART_BT_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 USART_BT_PORT_init()
{
gpio_set_pin_function(PB13, PINMUX_PB13C_SERCOM4_PAD1);
gpio_set_pin_function(PB14, PINMUX_PB14C_SERCOM4_PAD2);
}
/**
* \brief USART initialization function
*
* Enables USART peripheral, clocks and initializes USART driver
*/
void USART_BT_init(void)
{
USART_BT_CLOCK_init();
usart_async_init(&USART_BT, SERCOM4, USART_BT_buffer, USART_BT_BUFFER_SIZE, (void *)NULL);
USART_BT_PORT_init();
}
/**
* \brief USART Clock initialization function
*
* Enables register interface and peripheral clock
*/
void Debug_CLOCK_init()
{
_pm_enable_bus_clock(PM_BUS_APBC, SERCOM5);
_gclk_enable_channel(SERCOM5_GCLK_ID_CORE, CONF_GCLK_SERCOM5_CORE_SRC);
}
/**
* \brief USART pinmux initialization function
*
* Set each required pin to USART functionality
*/
void Debug_PORT_init()
{
gpio_set_pin_function(PB30, PINMUX_PB30D_SERCOM5_PAD0);
gpio_set_pin_function(PB31, PINMUX_PB31D_SERCOM5_PAD1);
}
/**
* \brief USART initialization function
*
* Enables USART peripheral, clocks and initializes USART driver
*/
void Debug_init(void)
{
Debug_CLOCK_init();
usart_async_init(&Debug, SERCOM5, Debug_buffer, DEBUG_BUFFER_SIZE, (void *)NULL);
Debug_PORT_init();
}
void PWM_1_PORT_init(void)
{
gpio_set_pin_function(PB08, PINMUX_PB08E_TC4_WO0);
}
void PWM_1_CLOCK_init(void)
{
_pm_enable_bus_clock(PM_BUS_APBC, TC4);
_gclk_enable_channel(TC4_GCLK_ID, CONF_GCLK_TC4_SRC);
}
void PWM_1_init(void)
{
PWM_1_CLOCK_init();
PWM_1_PORT_init();
pwm_init(&PWM_1, TC4, _tc_get_pwm());
}
void PWM_0_PORT_init(void)
{
gpio_set_pin_function(PB10, PINMUX_PB10E_TC5_WO0);
}
void PWM_0_CLOCK_init(void)
{
_pm_enable_bus_clock(PM_BUS_APBC, TC5);
_gclk_enable_channel(TC5_GCLK_ID, CONF_GCLK_TC5_SRC);
}
void PWM_0_init(void)
{
PWM_0_CLOCK_init();
PWM_0_PORT_init();
pwm_init(&PWM_0, TC5, _tc_get_pwm());
}
void TIMER_0_CLOCK_init(void)
{
_pm_enable_bus_clock(PM_BUS_APBC, TCC0);
_gclk_enable_channel(TCC0_GCLK_ID, CONF_GCLK_TCC0_SRC);
}
void TIMER_0_init(void)
{
TIMER_0_CLOCK_init();
timer_init(&TIMER_0, TCC0, _tcc_get_timer());
}
void system_init(void)
{
init_mcu();
// GPIO on PA02
gpio_set_pin_level(PWM_OUT,
// <y> Initial level
// <id> pad_initial_level
// <false"> Low
// <true"> High
false);
// Set pin direction to output
gpio_set_pin_direction(PWM_OUT, GPIO_DIRECTION_OUT);
gpio_set_pin_function(PWM_OUT, GPIO_PIN_FUNCTION_OFF);
// GPIO on PA12
gpio_set_pin_level(PIN_LED_RED,
// <y> Initial level
// <id> pad_initial_level
// <false"> Low
// <true"> High
false);
// Set pin direction to output
gpio_set_pin_direction(PIN_LED_RED, GPIO_DIRECTION_OUT);
gpio_set_pin_function(PIN_LED_RED, GPIO_PIN_FUNCTION_OFF);
// GPIO on PA13
gpio_set_pin_level(PIN_LED_BLUE,
// <y> Initial level
// <id> pad_initial_level
// <false"> Low
// <true"> High
false);
// Set pin direction to output
gpio_set_pin_direction(PIN_LED_BLUE, GPIO_DIRECTION_OUT);
gpio_set_pin_function(PIN_LED_BLUE, GPIO_PIN_FUNCTION_OFF);
// GPIO on PA15
// Set pin direction to input
gpio_set_pin_direction(PIN_BT_WAKE, GPIO_DIRECTION_IN);
gpio_set_pin_pull_mode(PIN_BT_WAKE,
// <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(PIN_BT_WAKE, GPIO_PIN_FUNCTION_OFF);
// GPIO on PB15
gpio_set_pin_level(PIN_LED_GREEN,
// <y> Initial level
// <id> pad_initial_level
// <false"> Low
// <true"> High
false);
// Set pin direction to output
gpio_set_pin_direction(PIN_LED_GREEN, GPIO_DIRECTION_OUT);
gpio_set_pin_function(PIN_LED_GREEN, GPIO_PIN_FUNCTION_OFF);
// GPIO on PB17
gpio_set_pin_level(PIN_BT_RST,
// <y> Initial level
// <id> pad_initial_level
// <false"> Low
// <true"> High
true);
// Set pin direction to output
gpio_set_pin_direction(PIN_BT_RST, GPIO_DIRECTION_OUT);
gpio_set_pin_function(PIN_BT_RST, GPIO_PIN_FUNCTION_OFF);
ADC_0_init();
USART_LORA_init();
I2C_0_init();
USART_BT_init();
Debug_init();
PWM_1_init();
PWM_0_init();
TIMER_0_init();
}