driver_init.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. #include "driver_init.h"
  9. #include <peripheral_clk_config.h>
  10. #include <utils.h>
  11. #include <hal_init.h>
  12. /*! The buffer size for USART */
  13. #define TARGET_IO_BUFFER_SIZE 16
  14. struct usart_async_descriptor TARGET_IO;
  15. static uint8_t TARGET_IO_buffer[TARGET_IO_BUFFER_SIZE];
  16. /**
  17. * \brief USART Clock initialization function
  18. *
  19. * Enables register interface and peripheral clock
  20. */
  21. void TARGET_IO_CLOCK_init()
  22. {
  23. hri_gclk_write_PCHCTRL_reg(GCLK, SERCOM2_GCLK_ID_CORE, CONF_GCLK_SERCOM2_CORE_SRC | (1 << GCLK_PCHCTRL_CHEN_Pos));
  24. hri_gclk_write_PCHCTRL_reg(GCLK, SERCOM2_GCLK_ID_SLOW, CONF_GCLK_SERCOM2_SLOW_SRC | (1 << GCLK_PCHCTRL_CHEN_Pos));
  25. hri_mclk_set_APBBMASK_SERCOM2_bit(MCLK);
  26. }
  27. /**
  28. * \brief USART pinmux initialization function
  29. *
  30. * Set each required pin to USART functionality
  31. */
  32. void TARGET_IO_PORT_init()
  33. {
  34. gpio_set_pin_function(PB26, PINMUX_PB26C_SERCOM2_PAD0);
  35. gpio_set_pin_function(PB27, PINMUX_PB27C_SERCOM2_PAD1);
  36. }
  37. /**
  38. * \brief USART initialization function
  39. *
  40. * Enables USART peripheral, clocks and initializes USART driver
  41. */
  42. void TARGET_IO_init(void)
  43. {
  44. TARGET_IO_CLOCK_init();
  45. usart_async_init(&TARGET_IO, SERCOM2, TARGET_IO_buffer, TARGET_IO_BUFFER_SIZE, (void *)NULL);
  46. TARGET_IO_PORT_init();
  47. }
  48. void USB_DEVICE_INSTANCE_PORT_init(void)
  49. {
  50. gpio_set_pin_direction(PA24,
  51. // <y> Pin direction
  52. // <id> pad_direction
  53. // <GPIO_DIRECTION_OFF"> Off
  54. // <GPIO_DIRECTION_IN"> In
  55. // <GPIO_DIRECTION_OUT"> Out
  56. GPIO_DIRECTION_OUT);
  57. gpio_set_pin_level(PA24,
  58. // <y> Initial level
  59. // <id> pad_initial_level
  60. // <false"> Low
  61. // <true"> High
  62. false);
  63. gpio_set_pin_pull_mode(PA24,
  64. // <y> Pull configuration
  65. // <id> pad_pull_config
  66. // <GPIO_PULL_OFF"> Off
  67. // <GPIO_PULL_UP"> Pull-up
  68. // <GPIO_PULL_DOWN"> Pull-down
  69. GPIO_PULL_OFF);
  70. gpio_set_pin_function(PA24,
  71. // <y> Pin function
  72. // <id> pad_function
  73. // <i> Auto : use driver pinmux if signal is imported by driver, else turn off function
  74. // <PINMUX_PA24H_USB_DM"> Auto
  75. // <GPIO_PIN_FUNCTION_OFF"> Off
  76. // <GPIO_PIN_FUNCTION_A"> A
  77. // <GPIO_PIN_FUNCTION_B"> B
  78. // <GPIO_PIN_FUNCTION_C"> C
  79. // <GPIO_PIN_FUNCTION_D"> D
  80. // <GPIO_PIN_FUNCTION_E"> E
  81. // <GPIO_PIN_FUNCTION_F"> F
  82. // <GPIO_PIN_FUNCTION_G"> G
  83. // <GPIO_PIN_FUNCTION_H"> H
  84. // <GPIO_PIN_FUNCTION_I"> I
  85. // <GPIO_PIN_FUNCTION_J"> J
  86. // <GPIO_PIN_FUNCTION_K"> K
  87. // <GPIO_PIN_FUNCTION_L"> L
  88. // <GPIO_PIN_FUNCTION_M"> M
  89. // <GPIO_PIN_FUNCTION_N"> N
  90. PINMUX_PA24H_USB_DM);
  91. gpio_set_pin_direction(PA25,
  92. // <y> Pin direction
  93. // <id> pad_direction
  94. // <GPIO_DIRECTION_OFF"> Off
  95. // <GPIO_DIRECTION_IN"> In
  96. // <GPIO_DIRECTION_OUT"> Out
  97. GPIO_DIRECTION_OUT);
  98. gpio_set_pin_level(PA25,
  99. // <y> Initial level
  100. // <id> pad_initial_level
  101. // <false"> Low
  102. // <true"> High
  103. false);
  104. gpio_set_pin_pull_mode(PA25,
  105. // <y> Pull configuration
  106. // <id> pad_pull_config
  107. // <GPIO_PULL_OFF"> Off
  108. // <GPIO_PULL_UP"> Pull-up
  109. // <GPIO_PULL_DOWN"> Pull-down
  110. GPIO_PULL_OFF);
  111. gpio_set_pin_function(PA25,
  112. // <y> Pin function
  113. // <id> pad_function
  114. // <i> Auto : use driver pinmux if signal is imported by driver, else turn off function
  115. // <PINMUX_PA25H_USB_DP"> Auto
  116. // <GPIO_PIN_FUNCTION_OFF"> Off
  117. // <GPIO_PIN_FUNCTION_A"> A
  118. // <GPIO_PIN_FUNCTION_B"> B
  119. // <GPIO_PIN_FUNCTION_C"> C
  120. // <GPIO_PIN_FUNCTION_D"> D
  121. // <GPIO_PIN_FUNCTION_E"> E
  122. // <GPIO_PIN_FUNCTION_F"> F
  123. // <GPIO_PIN_FUNCTION_G"> G
  124. // <GPIO_PIN_FUNCTION_H"> H
  125. // <GPIO_PIN_FUNCTION_I"> I
  126. // <GPIO_PIN_FUNCTION_J"> J
  127. // <GPIO_PIN_FUNCTION_K"> K
  128. // <GPIO_PIN_FUNCTION_L"> L
  129. // <GPIO_PIN_FUNCTION_M"> M
  130. // <GPIO_PIN_FUNCTION_N"> N
  131. PINMUX_PA25H_USB_DP);
  132. }
  133. /* The USB module requires a GCLK_USB of 48 MHz ~ 0.25% clock
  134. * for low speed and full speed operation. */
  135. #if (CONF_GCLK_USB_FREQUENCY > (48000000 + 48000000 / 400)) || (CONF_GCLK_USB_FREQUENCY < (48000000 - 48000000 / 400))
  136. #warning USB clock should be 48MHz ~ 0.25% clock, check your configuration!
  137. #endif
  138. void USB_DEVICE_INSTANCE_CLOCK_init(void)
  139. {
  140. hri_gclk_write_PCHCTRL_reg(GCLK, USB_GCLK_ID, CONF_GCLK_USB_SRC | GCLK_PCHCTRL_CHEN);
  141. hri_mclk_set_AHBMASK_USB_bit(MCLK);
  142. hri_mclk_set_APBBMASK_USB_bit(MCLK);
  143. }
  144. void USB_DEVICE_INSTANCE_init(void)
  145. {
  146. USB_DEVICE_INSTANCE_CLOCK_init();
  147. usb_d_init();
  148. USB_DEVICE_INSTANCE_PORT_init();
  149. }
  150. void system_init(void)
  151. {
  152. init_mcu();
  153. // GPIO on PA15
  154. gpio_set_pin_level(LED0,
  155. // <y> Initial level
  156. // <id> pad_initial_level
  157. // <false"> Low
  158. // <true"> High
  159. false);
  160. // Set pin direction to output
  161. gpio_set_pin_direction(LED0, GPIO_DIRECTION_OUT);
  162. gpio_set_pin_function(LED0, GPIO_PIN_FUNCTION_OFF);
  163. TARGET_IO_init();
  164. USB_DEVICE_INSTANCE_init();
  165. }