Kévin Redon | 69b92d9 | 2019-01-24 16:39:20 +0100 | [diff] [blame] | 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 | |
Kévin Redon | ccbed0b | 2019-01-24 18:30:26 +0100 | [diff] [blame] | 14 | /*! The buffer size for USART */ |
| 15 | #define UART_DEBUG_BUFFER_SIZE 16 |
Kévin Redon | 4cd3f7d | 2019-01-24 17:57:13 +0100 | [diff] [blame] | 16 | |
Kévin Redon | ccbed0b | 2019-01-24 18:30:26 +0100 | [diff] [blame] | 17 | struct usart_async_descriptor UART_debug; |
| 18 | |
| 19 | static 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 | */ |
| 26 | void UART_debug_CLOCK_init() |
| 27 | { |
| 28 | |
| 29 | hri_gclk_write_PCHCTRL_reg(GCLK, SERCOM2_GCLK_ID_CORE, CONF_GCLK_SERCOM2_CORE_SRC | (1 << GCLK_PCHCTRL_CHEN_Pos)); |
| 30 | hri_gclk_write_PCHCTRL_reg(GCLK, SERCOM2_GCLK_ID_SLOW, CONF_GCLK_SERCOM2_SLOW_SRC | (1 << GCLK_PCHCTRL_CHEN_Pos)); |
| 31 | |
| 32 | hri_mclk_set_APBBMASK_SERCOM2_bit(MCLK); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * \brief USART pinmux initialization function |
| 37 | * |
| 38 | * Set each required pin to USART functionality |
| 39 | */ |
| 40 | void UART_debug_PORT_init() |
Kévin Redon | 4cd3f7d | 2019-01-24 17:57:13 +0100 | [diff] [blame] | 41 | { |
| 42 | |
| 43 | gpio_set_pin_function(PB25, PINMUX_PB25D_SERCOM2_PAD0); |
| 44 | |
| 45 | gpio_set_pin_function(PB24, PINMUX_PB24D_SERCOM2_PAD1); |
| 46 | } |
| 47 | |
Kévin Redon | ccbed0b | 2019-01-24 18:30:26 +0100 | [diff] [blame] | 48 | /** |
| 49 | * \brief USART initialization function |
| 50 | * |
| 51 | * Enables USART peripheral, clocks and initializes USART driver |
| 52 | */ |
Kévin Redon | 4cd3f7d | 2019-01-24 17:57:13 +0100 | [diff] [blame] | 53 | void UART_debug_init(void) |
| 54 | { |
| 55 | UART_debug_CLOCK_init(); |
Kévin Redon | ccbed0b | 2019-01-24 18:30:26 +0100 | [diff] [blame] | 56 | usart_async_init(&UART_debug, SERCOM2, UART_debug_buffer, UART_DEBUG_BUFFER_SIZE, (void *)NULL); |
Kévin Redon | 4cd3f7d | 2019-01-24 17:57:13 +0100 | [diff] [blame] | 57 | UART_debug_PORT_init(); |
| 58 | } |
| 59 | |
Kévin Redon | 69b92d9 | 2019-01-24 16:39:20 +0100 | [diff] [blame] | 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 | |
| 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 | |
| 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 | |
| 175 | void system_init(void) |
| 176 | { |
| 177 | init_mcu(); |
| 178 | |
Kévin Redon | 5908a5c | 2019-01-24 18:45:23 +0100 | [diff] [blame^] | 179 | // GPIO on PC18 |
| 180 | |
| 181 | gpio_set_pin_level(LED_system, |
| 182 | // <y> Initial level |
| 183 | // <id> pad_initial_level |
| 184 | // <false"> Low |
| 185 | // <true"> High |
| 186 | true); |
| 187 | |
| 188 | // Set pin direction to output |
| 189 | gpio_set_pin_direction(LED_system, GPIO_DIRECTION_OUT); |
| 190 | |
| 191 | gpio_set_pin_function(LED_system, GPIO_PIN_FUNCTION_OFF); |
| 192 | |
Kévin Redon | 4cd3f7d | 2019-01-24 17:57:13 +0100 | [diff] [blame] | 193 | UART_debug_init(); |
| 194 | |
Kévin Redon | 69b92d9 | 2019-01-24 16:39:20 +0100 | [diff] [blame] | 195 | USB_DEVICE_INSTANCE_init(); |
| 196 | } |