sam4l_hal.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. This file is part of the ChipWhisperer Example Targets
  3. Copyright (C) 2017 NewAE Technology Inc.
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. Portions of this HAL are based on Atmel ASF.
  15. */
  16. #include "sam4l_hal.h"
  17. #define OSC_MODE_EXTERNAL 0
  18. #define SYSCLK_SRC_OSC0 1
  19. void sysclk_init(void);
  20. void sysclk_priv_enable_module(uint32_t bus_id, uint32_t module_index);
  21. static inline void sysclk_enable_pba_divmask(uint32_t mask);
  22. void sysclk_priv_enable_module(uint32_t bus_id, uint32_t module_index)
  23. {
  24. //irqflags_t flags;
  25. uint32_t mask;
  26. //Assume IRQ not used in our simple example...
  27. //flags = cpu_irq_save();
  28. /* Enable the clock */
  29. mask = *(&PM->PM_CPUMASK + bus_id);
  30. mask |= 1U << module_index;
  31. PM->PM_UNLOCK = PM_UNLOCK_KEY(0xAAu) |
  32. BPM_UNLOCK_ADDR(((uint32_t)&PM->PM_CPUMASK - (uint32_t)PM) + (4 * bus_id));
  33. *(&PM->PM_CPUMASK + bus_id) = mask;
  34. //cpu_irq_restore(flags);
  35. }
  36. static inline void sysclk_enable_pba_divmask(uint32_t mask)
  37. {
  38. uint32_t temp_mask;
  39. temp_mask = PM->PM_PBADIVMASK;
  40. temp_mask |= mask;
  41. PM->PM_UNLOCK = PM_UNLOCK_KEY(0xAAu)
  42. | PM_UNLOCK_ADDR((uint32_t)&PM->PM_PBADIVMASK - (uint32_t)PM);
  43. PM->PM_PBADIVMASK = temp_mask;
  44. }
  45. //! \name Bus index of maskable module clocks
  46. //@{
  47. #define PM_CLK_GRP_CPU 0
  48. #define PM_CLK_GRP_HSB 1
  49. #define PM_CLK_GRP_PBA 2
  50. #define PM_CLK_GRP_PBB 3
  51. #define PM_CLK_GRP_PBC 4
  52. #define PM_CLK_GRP_PBD 5
  53. //@}
  54. //! \name Clocks derived from the CPU clock
  55. //@{
  56. //! On-Chip Debug system
  57. #define SYSCLK_OCD 0
  58. //@}
  59. //! \name Clocks derived from the HSB clock
  60. //@{
  61. //! PDCA memory interface
  62. #define SYSCLK_PDCA_HSB 0
  63. //! Flash data interface
  64. #define SYSCLK_HFLASHC_DATA 1
  65. //! HRAMC data interface
  66. #define SYSCLK_HRAMC1_DATA 2
  67. //! USBC DMA and FIFO interface
  68. #define SYSCLK_USBC_DATA 3
  69. //! CRCCU data interface
  70. #define SYSCLK_CRCCU_DATA 4
  71. //! HSB<->PBA bridge
  72. #define SYSCLK_PBA_BRIDGE 5
  73. //! HSB<->PBB bridge
  74. #define SYSCLK_PBB_BRIDGE 6
  75. //! HSB<->PBC bridge
  76. #define SYSCLK_PBC_BRIDGE 7
  77. //! HSB<->PBD bridge
  78. #define SYSCLK_PBD_BRIDGE 8
  79. //! Advanced Encryption Standard
  80. #define SYSCLK_AESA_HSB 9
  81. #define PBA_DIVMASK_CLK_USART (1u << 2)
  82. #define SYSCLK_USART0 8
  83. void periclk_usart0_init(void)
  84. {
  85. if (PM->PM_PBAMASK == 0) {
  86. sysclk_priv_enable_module(PM_CLK_GRP_HSB, SYSCLK_PBA_BRIDGE);
  87. }
  88. /* Enable the module */
  89. sysclk_priv_enable_module(PM_CLK_GRP_PBA, SYSCLK_USART0);
  90. sysclk_enable_pba_divmask(PBA_DIVMASK_CLK_USART);
  91. }
  92. void periclk_aesa_init(void)
  93. {
  94. /* Enable APB clock for AES */
  95. sysclk_priv_enable_module(PM_CLK_GRP_HSB, SYSCLK_AESA_HSB);
  96. }
  97. void sysclk_init(void)
  98. {
  99. /* OSC0 Turn-On */
  100. //Turn on OSC0 as external oscillator
  101. SCIF->SCIF_UNLOCK = SCIF_UNLOCK_KEY(0xAAu) | SCIF_UNLOCK_ADDR((uint32_t)&SCIF->SCIF_OSCCTRL0 - (uint32_t)SCIF);
  102. SCIF->SCIF_OSCCTRL0 = SCIF_OSCCTRL0_STARTUP(2) | OSC_MODE_EXTERNAL | SCIF_OSCCTRL0_OSCEN;
  103. //Wait for OSC0 to work
  104. while((SCIF->SCIF_PCLKSR & SCIF_PCLKSR_OSC0RDY) == 0);
  105. //Set OSC0 as source
  106. PM->PM_UNLOCK = PM_UNLOCK_KEY(0xAAu)| PM_UNLOCK_ADDR((uint32_t)&PM->PM_MCCTRL - (uint32_t)PM);
  107. PM->PM_MCCTRL = SYSCLK_SRC_OSC0;
  108. }
  109. void platform_init(void)
  110. {
  111. sysclk_init();
  112. }