1 /*
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 
14 /*! The buffer size for USART */
15 #define TARGET_IO_BUFFER_SIZE 16
16 
17 struct usart_async_descriptor TARGET_IO;
18 
19 static uint8_t TARGET_IO_buffer[TARGET_IO_BUFFER_SIZE];
20 
21 /**
22  * \brief USART Clock initialization function
23  *
24  * Enables register interface and peripheral clock
25  */
TARGET_IO_CLOCK_init()26 void TARGET_IO_CLOCK_init()
27 {
28 
29 	hri_gclk_write_PCHCTRL_reg(GCLK, SERCOM3_GCLK_ID_CORE, CONF_GCLK_SERCOM3_CORE_SRC | (1 << GCLK_PCHCTRL_CHEN_Pos));
30 	hri_gclk_write_PCHCTRL_reg(GCLK, SERCOM3_GCLK_ID_SLOW, CONF_GCLK_SERCOM3_SLOW_SRC | (1 << GCLK_PCHCTRL_CHEN_Pos));
31 
32 	hri_mclk_set_APBBMASK_SERCOM3_bit(MCLK);
33 }
34 
35 /**
36  * \brief USART pinmux initialization function
37  *
38  * Set each required pin to USART functionality
39  */
TARGET_IO_PORT_init()40 void TARGET_IO_PORT_init()
41 {
42 
43 	gpio_set_pin_function(PA22, PINMUX_PA22C_SERCOM3_PAD0);
44 
45 	gpio_set_pin_function(PA23, PINMUX_PA23C_SERCOM3_PAD1);
46 }
47 
48 /**
49  * \brief USART initialization function
50  *
51  * Enables USART peripheral, clocks and initializes USART driver
52  */
TARGET_IO_init(void)53 void TARGET_IO_init(void)
54 {
55 	TARGET_IO_CLOCK_init();
56 	usart_async_init(&TARGET_IO, SERCOM3, TARGET_IO_buffer, TARGET_IO_BUFFER_SIZE, (void *)NULL);
57 	TARGET_IO_PORT_init();
58 }
59 
USB_DEVICE_INSTANCE_PORT_init(void)60 void USB_DEVICE_INSTANCE_PORT_init(void)
61 {
62 
63 	gpio_set_pin_direction(PA24,
64 	                       // <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 
71 	gpio_set_pin_level(PA24,
72 	                   // <y> Initial level
73 	                   // <id> pad_initial_level
74 	                   // <false"> Low
75 	                   // <true"> High
76 	                   false);
77 
78 	gpio_set_pin_pull_mode(PA24,
79 	                       // <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 
86 	gpio_set_pin_function(PA24,
87 	                      // <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 
108 	gpio_set_pin_direction(PA25,
109 	                       // <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 
116 	gpio_set_pin_level(PA25,
117 	                   // <y> Initial level
118 	                   // <id> pad_initial_level
119 	                   // <false"> Low
120 	                   // <true"> High
121 	                   false);
122 
123 	gpio_set_pin_pull_mode(PA25,
124 	                       // <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 
131 	gpio_set_pin_function(PA25,
132 	                      // <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 
USB_DEVICE_INSTANCE_CLOCK_init(void)160 void 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 
USB_DEVICE_INSTANCE_init(void)168 void USB_DEVICE_INSTANCE_init(void)
169 {
170 	USB_DEVICE_INSTANCE_CLOCK_init();
171 	usb_d_init();
172 	USB_DEVICE_INSTANCE_PORT_init();
173 }
174 
system_init(void)175 void system_init(void)
176 {
177 	init_mcu();
178 
179 	// GPIO on PA16
180 
181 	gpio_set_pin_level(LED0,
182 	                   // <y> Initial level
183 	                   // <id> pad_initial_level
184 	                   // <false"> Low
185 	                   // <true"> High
186 	                   false);
187 
188 	// Set pin direction to output
189 	gpio_set_pin_direction(LED0, GPIO_DIRECTION_OUT);
190 
191 	gpio_set_pin_function(LED0, GPIO_PIN_FUNCTION_OFF);
192 
193 	TARGET_IO_init();
194 
195 	USB_DEVICE_INSTANCE_init();
196 }
197