driver_init.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 <hal_init.h>
  10. #include <hpl_pmc.h>
  11. #include <peripheral_clk_config.h>
  12. #include <utils.h>
  13. #include <hpl_usart_base.h>
  14. /*! The buffer size for USART */
  15. #define TARGET_IO_BUFFER_SIZE 16
  16. struct usart_async_descriptor TARGET_IO;
  17. struct can_async_descriptor CAN_0;
  18. static uint8_t TARGET_IO_buffer[TARGET_IO_BUFFER_SIZE];
  19. struct adc_sync_descriptor ADC_0;
  20. struct i2c_m_sync_desc I2C_0;
  21. struct mac_async_descriptor MACIF;
  22. void ADC_0_PORT_init(void)
  23. {
  24. gpio_set_pin_function(PB0, GPIO_PIN_FUNCTION_OFF);
  25. }
  26. void ADC_0_CLOCK_init(void)
  27. {
  28. _pmc_enable_periph_clock(ID_AFEC0);
  29. }
  30. void ADC_0_init(void)
  31. {
  32. ADC_0_CLOCK_init();
  33. ADC_0_PORT_init();
  34. adc_sync_init(&ADC_0, AFEC0, (void *)NULL);
  35. }
  36. void I2C_0_PORT_init(void)
  37. {
  38. gpio_set_pin_function(PA4, MUX_PA4A_TWIHS0_TWCK0);
  39. gpio_set_pin_function(PA3, MUX_PA3A_TWIHS0_TWD0);
  40. }
  41. void I2C_0_CLOCK_init(void)
  42. {
  43. _pmc_enable_periph_clock(ID_TWIHS0);
  44. }
  45. void I2C_0_init(void)
  46. {
  47. I2C_0_CLOCK_init();
  48. i2c_m_sync_init(&I2C_0, TWIHS0);
  49. I2C_0_PORT_init();
  50. }
  51. /**
  52. * \brief USART Clock initialization function
  53. *
  54. * Enables register interface and peripheral clock
  55. */
  56. void TARGET_IO_CLOCK_init()
  57. {
  58. _pmc_enable_periph_clock(ID_USART1);
  59. }
  60. /**
  61. * \brief USART pinmux initialization function
  62. *
  63. * Set each required pin to USART functionality
  64. */
  65. void TARGET_IO_PORT_init()
  66. {
  67. gpio_set_pin_function(PA21, MUX_PA21A_USART1_RXD1);
  68. gpio_set_pin_function(PB4, MUX_PB4D_USART1_TXD1);
  69. }
  70. /**
  71. * \brief USART initialization function
  72. *
  73. * Enables USART peripheral, clocks and initializes USART driver
  74. */
  75. void TARGET_IO_init(void)
  76. {
  77. TARGET_IO_CLOCK_init();
  78. TARGET_IO_PORT_init();
  79. usart_async_init(&TARGET_IO, USART1, TARGET_IO_buffer, TARGET_IO_BUFFER_SIZE, _usart_get_usart_async());
  80. }
  81. void MACIF_PORT_init(void)
  82. {
  83. gpio_set_pin_function(PD8, MUX_PD8A_GMAC_GMDC);
  84. gpio_set_pin_function(PD9, MUX_PD9A_GMAC_GMDIO);
  85. gpio_set_pin_function(PD5, MUX_PD5A_GMAC_GRX0);
  86. gpio_set_pin_function(PD6, MUX_PD6A_GMAC_GRX1);
  87. gpio_set_pin_function(PD4, MUX_PD4A_GMAC_GRXDV);
  88. gpio_set_pin_function(PD7, MUX_PD7A_GMAC_GRXER);
  89. gpio_set_pin_function(PD2, MUX_PD2A_GMAC_GTX0);
  90. gpio_set_pin_function(PD3, MUX_PD3A_GMAC_GTX1);
  91. gpio_set_pin_function(PD0, MUX_PD0A_GMAC_GTXCK);
  92. gpio_set_pin_function(PD1, MUX_PD1A_GMAC_GTXEN);
  93. }
  94. void MACIF_CLOCK_init(void)
  95. {
  96. _pmc_enable_periph_clock(ID_GMAC);
  97. }
  98. void MACIF_init(void)
  99. {
  100. MACIF_CLOCK_init();
  101. mac_async_init(&MACIF, GMAC);
  102. MACIF_PORT_init();
  103. }
  104. /**
  105. * \brief MCAN Clock initialization function
  106. *
  107. * Enables register interface and peripheral clock
  108. */
  109. void CAN_0_CLOCK_init()
  110. {
  111. _pmc_enable_periph_clock(ID_MCAN1);
  112. }
  113. /**
  114. * \brief MCAN pinmux initialization function
  115. *
  116. * Set each required pin to MCAN functionality
  117. */
  118. void CAN_0_PORT_init(void)
  119. {
  120. gpio_set_pin_function(PC12, MUX_PC12C_MCAN1_CANRX1);
  121. gpio_set_pin_function(PC14, MUX_PC14C_MCAN1_CANTX1);
  122. }
  123. /**
  124. * \brief CAN initialization function
  125. *
  126. * Enables CAN peripheral, clocks and initializes CAN driver
  127. */
  128. void CAN_0_init(void)
  129. {
  130. CAN_0_CLOCK_init();
  131. CAN_0_PORT_init();
  132. can_async_init(&CAN_0, MCAN1);
  133. }
  134. void system_init(void)
  135. {
  136. init_mcu();
  137. _pmc_enable_periph_clock(ID_PIOA);
  138. _pmc_enable_periph_clock(ID_PIOC);
  139. /* Disable Watchdog */
  140. hri_wdt_set_MR_WDDIS_bit(WDT);
  141. /* GPIO on PA11 */
  142. // Set pin direction to input
  143. gpio_set_pin_direction(SW0, GPIO_DIRECTION_IN);
  144. gpio_set_pin_pull_mode(SW0,
  145. // <y> Pull configuration
  146. // <id> pad_pull_config
  147. // <GPIO_PULL_OFF"> Off
  148. // <GPIO_PULL_UP"> Pull-up
  149. // <GPIO_PULL_DOWN"> Pull-down
  150. GPIO_PULL_UP);
  151. gpio_set_pin_function(SW0, GPIO_PIN_FUNCTION_OFF);
  152. /* GPIO on PC8 */
  153. gpio_set_pin_level(LED0,
  154. // <y> Initial level
  155. // <id> pad_initial_level
  156. // <false"> Low
  157. // <true"> High
  158. true);
  159. // Set pin direction to output
  160. gpio_set_pin_direction(LED0, GPIO_DIRECTION_OUT);
  161. gpio_set_pin_function(LED0, GPIO_PIN_FUNCTION_OFF);
  162. ADC_0_init();
  163. I2C_0_init();
  164. TARGET_IO_init();
  165. MACIF_init();
  166. CAN_0_init();
  167. }