kinetis_hw.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /* kinetis_hw.c
  2. *
  3. * Copyright (C) 2006-2017 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. #include "hw.h"
  22. #include "user_settings.h"
  23. #if defined(FREESCALE) && defined(K_SERIES)
  24. /**********************************************
  25. * NOTE: Customize for actual hardware
  26. **********************************************/
  27. // CPU include for Rowley CrossWorks packages
  28. // $(TargetsDir) location:
  29. // On Mac OS/X: Users/USERNAME/Library/Rowley Associates Limited/CrossWorks for ARM/packages/targets/
  30. // On Windows: C:/Users/USERNAME/Application Data/Local/Rowley Associates Limited/CrossWorks for ARM/packages/targets/
  31. // Located in $(TargetsDir)/Kinetis/CMSIS/
  32. #ifdef FREESCALE_KSDK_BM
  33. #include "fsl_common.h"
  34. #include "fsl_debug_console.h"
  35. #include "fsl_rtc.h"
  36. #include "fsl_trng.h"
  37. #include "fsl_lpuart.h"
  38. #include "fsl_port.h"
  39. #include "clock_config.h"
  40. #else
  41. #include <MK64F12.h> // Located in $(TargetsDir)/Kinetis/CMSIS/
  42. #endif
  43. // System clock
  44. #ifdef FREESCALE_KSDK_BM
  45. #define SYS_CLK_HZ SystemCoreClock
  46. #else
  47. #define SYS_CLK_HZ 96000000ul /* Core system clock in Hz */
  48. #define SYS_CLK_DRS MCG_C4_DRST_DRS(0x03) /* DRS 0=24MHz, 1=48MHz, 2=72MHz, 3=96MHz */
  49. #define SYS_CLK_DMX MCG_C4_DMX32_MASK /* 0=Disable DMX32 (lower actual speed), MCG_C4_DMX32_MASK=Enable DMX32 */
  50. #define SYS_CLK_DIV 1 /* System clock divisor */
  51. #define BUS_CLK_DIV 2 /* Bus clock divisor */
  52. #define BUS_CLK_KHZ (SYS_CLK_HZ/BUS_CLK_DIV) /* Helper to calculate bus speed for UART */
  53. #define FLASH_CLK_DIV 4 /* Flash clock divisor */
  54. #endif
  55. // UART TX Port, Pin, Mux and Baud
  56. #ifdef FREESCALE_KSDK_BM
  57. #define UART_PORT LPUART0 /* UART Port */
  58. #define UART_TX_PORT PORTA /* UART TX Port */
  59. #define UART_TX_PIN 2U /* UART TX Pin */
  60. #define UART_TX_MUX kPORT_MuxAlt2 /* Kinetis UART pin mux */
  61. #else
  62. #define UART_PORT UART4 /* UART Port */
  63. #define UART_TX_PORT PORTE /* UART TX Port */
  64. #define UART_TX_PIN 24U /* UART TX Pin */
  65. #define UART_TX_MUX 0x3 /* Kinetis UART pin mux */
  66. #endif
  67. #define UART_BAUD 115200 /* UART Baud Rate */
  68. /* Note: You will also need to update the UART clock gate in hw_uart_init (SIM_SCGC1_UART5_MASK) */
  69. /* Note: TWR-K60 is UART3, PTC17 */
  70. /* Note: FRDM-K64 is UART4, PTE24 */
  71. /* Note: TWR-K64 is UART5, PTE8 */
  72. /* Note: FRDM-K82F is LPUART0 A2, LPUART4 PTC15 */
  73. /***********************************************/
  74. // Private functions
  75. static uint32_t mDelayCyclesPerUs = 0;
  76. #define NOP_FOR_LOOP_INSTRUCTION_COUNT 6
  77. static void delay_nop(uint32_t count)
  78. {
  79. int i;
  80. for(i=0; i<count; i++) {
  81. __asm volatile("nop");
  82. }
  83. }
  84. static void hw_mcg_init(void)
  85. {
  86. #ifdef FREESCALE_KSDK_BM
  87. BOARD_BootClockHSRUN();
  88. #else
  89. /* Adjust clock dividers (core/system=div/1, bus=div/2, flex bus=div/2, flash=div/4) */
  90. SIM->CLKDIV1 = SIM_CLKDIV1_OUTDIV1(SYS_CLK_DIV-1) | SIM_CLKDIV1_OUTDIV2(BUS_CLK_DIV-1) |
  91. SIM_CLKDIV1_OUTDIV3(BUS_CLK_DIV-1) | SIM_CLKDIV1_OUTDIV4(FLASH_CLK_DIV-1);
  92. /* Configure FEI internal clock speed */
  93. MCG->C4 = (SYS_CLK_DMX | SYS_CLK_DRS);
  94. while((MCG->C4 & (MCG_C4_DRST_DRS_MASK | MCG_C4_DMX32_MASK)) != (SYS_CLK_DMX | SYS_CLK_DRS));
  95. #endif
  96. }
  97. static void hw_gpio_init(void)
  98. {
  99. #ifdef FREESCALE_KSDK_BM
  100. CLOCK_EnableClock(kCLOCK_PortA);
  101. CLOCK_EnableClock(kCLOCK_PortB);
  102. CLOCK_EnableClock(kCLOCK_PortC);
  103. CLOCK_EnableClock(kCLOCK_PortD);
  104. CLOCK_EnableClock(kCLOCK_PortE);
  105. #else
  106. /* Enable clocks to all GPIO ports */
  107. SIM->SCGC5 |= (SIM_SCGC5_PORTA_MASK | SIM_SCGC5_PORTB_MASK
  108. #ifdef SIM_SCGC5_PORTC_MASK
  109. | SIM_SCGC5_PORTC_MASK
  110. #endif
  111. #ifdef SIM_SCGC5_PORTD_MASK
  112. | SIM_SCGC5_PORTD_MASK
  113. #endif
  114. #ifdef SIM_SCGC5_PORTE_MASK
  115. | SIM_SCGC5_PORTE_MASK
  116. #endif
  117. );
  118. #endif
  119. }
  120. static void hw_uart_init(void)
  121. {
  122. register uint16_t sbr, brfa;
  123. uint8_t temp;
  124. #ifdef FREESCALE_KSDK_BM
  125. PORT_SetPinMux(UART_TX_PORT, UART_TX_PIN, UART_TX_MUX);
  126. CLOCK_SetLpuartClock(1); /* MCGPLLCLK */
  127. DbgConsole_Init((uint32_t)UART_PORT, UART_BAUD, DEBUG_CONSOLE_DEVICE_TYPE_LPUART, SYS_CLK_HZ);
  128. #else
  129. /* Enable UART core clock */
  130. /* Note: Remember to update me if UART_PORT changes */
  131. SIM->SCGC1 |= SIM_SCGC1_UART4_MASK;
  132. /* Configure UART TX pin */
  133. UART_TX_PORT->PCR[UART_TX_PIN] = PORT_PCR_MUX(UART_TX_MUX);
  134. /* Disable transmitter and receiver while we change settings. */
  135. UART_PORT->C2 &= ~(UART_C2_TE_MASK | UART_C2_RE_MASK );
  136. /* Configure the UART for 8-bit mode, no parity */
  137. UART_PORT->C1 = 0;
  138. /* Calculate baud settings */
  139. sbr = (uint16_t)((BUS_CLK_KHZ * 1000)/(UART_BAUD * 16));
  140. temp = UART_PORT->BDH & ~(UART_BDH_SBR(0x1F));
  141. UART_PORT->BDH = temp | UART_BDH_SBR(((sbr & 0x1F00) >> 8));
  142. UART_PORT->BDL = (uint8_t)(sbr & UART_BDL_SBR_MASK);
  143. /* Determine if a fractional divider is needed to get closer to the baud rate */
  144. brfa = (((BUS_CLK_KHZ * 32000)/(UART_BAUD * 16)) - (sbr * 32));
  145. temp = UART_PORT->C4 & ~(UART_C4_BRFA(0x1F));
  146. UART_PORT->C4 = temp | UART_C4_BRFA(brfa);
  147. /* Enable receiver and transmitter */
  148. UART_PORT->C2 |= (UART_C2_TE_MASK | UART_C2_RE_MASK);
  149. #endif
  150. }
  151. static void hw_rtc_init(void)
  152. {
  153. /* Init nop delay */
  154. mDelayCyclesPerUs = (SYS_CLK_HZ / 1000000 / NOP_FOR_LOOP_INSTRUCTION_COUNT);
  155. /* Enable RTC clock and oscillator */
  156. SIM->SCGC6 |= SIM_SCGC6_RTC_MASK;
  157. if (RTC->SR & RTC_SR_TIF_MASK) {
  158. /* Resets the RTC registers except for the SWR bit */
  159. RTC->CR |= RTC_CR_SWR_MASK;
  160. RTC->CR &= ~RTC_CR_SWR_MASK;
  161. /* Set TSR register to 0x1 to avoid the TIF bit being set in the SR register */
  162. RTC->TSR = 1;
  163. }
  164. /* Disable RTC Interrupts */
  165. RTC->IER = 0;
  166. /* Enable OSC */
  167. if ((RTC->CR & RTC_CR_OSCE_MASK) == 0) {
  168. int i;
  169. /* Turn on */
  170. RTC->CR |= RTC_CR_OSCE_MASK;
  171. /* Wait RTC startup delay 1000 us */
  172. delay_us(1000);
  173. }
  174. /* Enable counter */
  175. RTC->SR |= RTC_SR_TCE_MASK;
  176. }
  177. static void hw_rand_init(void)
  178. {
  179. #ifdef FREESCALE_KSDK_BM
  180. trng_config_t trngConfig;
  181. TRNG_GetDefaultConfig(&trngConfig);
  182. /* Set sample mode of the TRNG ring oscillator to Von Neumann, for better random data.*/
  183. trngConfig.sampleMode = kTRNG_SampleModeVonNeumann;
  184. /* Initialize TRNG */
  185. TRNG_Init(TRNG0, &trngConfig);
  186. #else
  187. /* Enable RNG clocks */
  188. SIM->SCGC6 |= SIM_SCGC6_RNGA_MASK;
  189. SIM->SCGC3 |= SIM_SCGC3_RNGA_MASK;
  190. /* Wake up RNG to normal mode (take out of sleep) */
  191. RNG->CR &= ~RNG_CR_SLP_MASK;
  192. /* Enable High Assurance mode (Enables notification of security violations via SR[SECV]) */
  193. RNG->CR |= RNG_CR_HA_MASK;
  194. /* Enable RNG generation to RANDOUT FIFO */
  195. RNG->CR |= RNG_CR_GO_MASK;
  196. #endif
  197. }
  198. /* Public Functions */
  199. void hw_init(void)
  200. {
  201. hw_mcg_init();
  202. hw_gpio_init();
  203. hw_uart_init();
  204. hw_rtc_init();
  205. hw_rand_init();
  206. }
  207. uint32_t hw_get_time_sec(void)
  208. {
  209. /* Return RTC seconds */
  210. return RTC->TSR;
  211. }
  212. uint32_t hw_get_time_msec(void)
  213. {
  214. /* RTC TPR precision register increments every 32.768 kHz clock cycle */
  215. /* Convert with rounding crystal count (32768 or (1 << 15)) to milliseconds */
  216. return ( ((uint32_t)RTC->TPR * 1000) + ((1 << 15) / 2) ) / (1 << 15);
  217. }
  218. void hw_uart_printchar(int c)
  219. {
  220. #ifdef FREESCALE_KSDK_BM
  221. LPUART_WriteBlocking(UART_PORT, (const uint8_t*)&c, 1); /* Send the character */
  222. #else
  223. while(!(UART_PORT->S1 & UART_S1_TDRE_MASK)); /* Wait until space is available in the FIFO */
  224. UART_PORT->D = (uint8_t)c; /* Send the character */
  225. #endif
  226. }
  227. uint32_t hw_rand(void)
  228. {
  229. uint32_t rng;
  230. #ifdef FREESCALE_KSDK_BM
  231. TRNG_GetRandomData(TRNG0, &rng, sizeof(rng));
  232. #else
  233. while((RNG->SR & RNG_SR_OREG_LVL(0xF)) == 0) {}; /* Wait until FIFO has a value available */
  234. rng = RNG->OR; /* Return next value in FIFO output register */
  235. #endif
  236. return rng;
  237. }
  238. void delay_us(uint32_t microseconds)
  239. {
  240. delay_nop(mDelayCyclesPerUs * microseconds);
  241. }
  242. // Watchdog
  243. void hw_watchdog_disable(void)
  244. {
  245. WDOG->UNLOCK = 0xC520;
  246. WDOG->UNLOCK = 0xD928;
  247. WDOG->STCTRLH = WDOG_STCTRLH_ALLOWUPDATE_MASK;
  248. }
  249. // Flash configuration
  250. #define FSEC_UNSECURE 2
  251. #define FSEC_SECURE 0
  252. #define FSEC_FSLACC_DENIED 2
  253. #define FSEC_FSLACC_GRANTED 3
  254. #define FSEC_KEY_ENABLED 2
  255. #define FSEC_KEY_DISABLED 3
  256. #define FSEC_MASS_ERASE_DISABLE 2
  257. #define FSEC_MASS_ERASE_ENABLE 3
  258. struct flash_conf {
  259. uint8_t backdoor_key[8]; /* Backdoor Comparison Key */
  260. uint8_t fprot[4]; /* Program flash protection bytes */
  261. uint8_t fsec; /* Flash security byte */
  262. uint8_t fopt; /* Flash nonvolatile option byte */
  263. uint8_t feprot; /* FlexNVM: EEPROM protection byte */
  264. uint8_t fdprot; /* FlexNVM: Data flash protection byte */
  265. };
  266. const struct flash_conf flash_conf __attribute__ ((section (".flashconf"),used)) =
  267. {
  268. .backdoor_key = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
  269. .fprot = { 0xFF, 0xFF, 0xFF, 0xFF },
  270. .fsec = NV_FSEC_SEC(FSEC_UNSECURE) | NV_FSEC_FSLACC(FSEC_FSLACC_GRANTED) |
  271. NV_FSEC_MEEN(FSEC_MASS_ERASE_ENABLE) | NV_FSEC_KEYEN(FSEC_KEY_DISABLED),
  272. .fopt = 0xFF,
  273. .feprot = 0xFF,
  274. .fdprot = 0xFF
  275. };
  276. #endif /* FREESCALE && K_SERIES */