main_k66.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  1. /*
  2. * Kinetis K66 board support for the bootloader.
  3. *
  4. */
  5. #include "kinetis.h"
  6. #include "gpio/fsl_gpio.h"
  7. #include "port/fsl_port.h"
  8. #include "smc/smc.h"
  9. #include "flash/fsl_flash.h"
  10. #include "hw_config.h"
  11. #include <stdint.h>
  12. #include <stdbool.h>
  13. #include <stdlib.h>
  14. #include "bl.h"
  15. #include "uart.h"
  16. #define BOOTLOADER_RESERVATION_SIZE (24 * 1024)
  17. #define FIRST_FLASH_SECTOR_TO_ERASE (BOARD_FIRST_FLASH_SECTOR_TO_ERASE + (BOOTLOADER_RESERVATION_SIZE/FLASH_SECTOR_SIZE))
  18. #define BOARD_RESETCLOCKRUN_CORE_CLOCK 20971520U /*!< Core clock frequency: 20971520Hz */
  19. #define MCG_IRCLK_DISABLE 0U /*!< MCGIRCLK disabled */
  20. #define MCG_PLL_DISABLE 0U /*!< MCGPLLCLK disabled */
  21. #define OSC_CAP0P 0U /*!< Oscillator 0pF capacitor load */
  22. #define OSC_ER_CLK_DISABLE 0U /*!< Disable external reference clock */
  23. #define SIM_OSC32KSEL_OSC32KCLK_CLK 0U /*!< OSC32KSEL select: OSC32KCLK clock */
  24. #define SIM_PLLFLLSEL_MCGFLLCLK_CLK 0U /*!< PLLFLL select: MCGFLLCLK clock */
  25. // SIM_SDID
  26. #define KINETIS_UNKNOWN 0
  27. #define KINETIS_K66
  28. #define PIN_MASK 0x0000000f
  29. #define PIN_SHIFTS 0
  30. #define FAM_MASK 0x00000070
  31. #define FAM_SHIFTS 4
  32. #define DIEID_MASK 0x00000f80
  33. #define DIE_SHIFTS 7
  34. #define REVID_MASK 0x0000f000
  35. #define REVID_SHIFTS 12
  36. #define RESID_MASK 0x000f0000
  37. #define RESID_SHIFTS 16
  38. #define SERIESID_MASK 0x00f00000
  39. #define SERIESID_SHIFTS 20
  40. #define SUBFAMID_MASK 0x0f000000
  41. #define SUBFAMID_SHIFTS 24
  42. #define FAMID_MASK 0xf0000000
  43. #define FAMID_SHIFTS 28
  44. #define APP_SIZE_MAX (BOARD_FLASH_SIZE - (BOOTLOADER_RESERVATION_SIZE + APP_RESERVATION_SIZE))
  45. /* context passed to cinit */
  46. #if INTERFACE_USART
  47. # define BOARD_INTERFACE_CONFIG_USART (void *)BOARD_USART
  48. #endif
  49. #if INTERFACE_USB
  50. # define BOARD_INTERFACE_CONFIG_USB NULL
  51. #endif
  52. flash_config_t s_flashDriver; //!< Flash driver instance.
  53. static uint32_t s_flashRunCommand[kFLASH_ExecuteInRamFunctionMaxSizeInWords];
  54. static uint32_t s_flashCacheClearCommand[kFLASH_ExecuteInRamFunctionMaxSizeInWords];
  55. static flash_execute_in_ram_function_config_t s_flashExecuteInRamFunctionInfo = {
  56. .activeFunctionCount = 0,
  57. .flashRunCommand = s_flashRunCommand,
  58. .flashCacheClearCommand = s_flashCacheClearCommand,
  59. };
  60. /* board definition */
  61. struct boardinfo board_info = {
  62. .board_type = BOARD_TYPE,
  63. .board_rev = 0,
  64. .fw_size = 0,
  65. .systick_mhz = 120,
  66. };
  67. static void board_init(void);
  68. #define BOOT_RTC_SIGNATURE 0xb007b007
  69. #define POWER_DOWN_RTC_SIGNATURE 0xdeaddead // Written by app fw to not re-power on.
  70. #define BOOT_RTC_REG 0
  71. /* State of an inserted USB cable */
  72. static bool usb_connected = false;
  73. static uint32_t
  74. board_get_rtc_signature()
  75. {
  76. return RFVBAT->REG[BOOT_RTC_REG];
  77. }
  78. static void
  79. board_set_rtc_signature(uint32_t sig)
  80. {
  81. RFVBAT->REG[BOOT_RTC_REG] = sig;
  82. }
  83. static bool
  84. board_test_force_pin()
  85. {
  86. return false;
  87. }
  88. #if INTERFACE_USART == 1
  89. static bool
  90. board_test_usart_receiving_break()
  91. {
  92. #if !defined(SERIAL_BREAK_DETECT_DISABLED)
  93. /* (re)start the SysTick timer system */
  94. systick_interrupt_disable(); // Kill the interrupt if it is still active
  95. systick_counter_disable(); // Stop the timer
  96. systick_set_clocksource(SYSTIC_CLKSOURCE_AHB);
  97. /* Set the timer period to be half the bit rate
  98. *
  99. * Baud rate = 115200, therefore bit period = 8.68us
  100. * Half the bit rate = 4.34us
  101. * Set period to 4.34 microseconds (timer_period = timer_tick / timer_reset_frequency = 168MHz / (1/4.34us) = 729.12 ~= 729)
  102. */
  103. systick_set_reload(729); /* 4.3us tick, magic number */
  104. systick_counter_enable(); // Start the timer
  105. uint8_t cnt_consecutive_low = 0;
  106. uint8_t cnt = 0;
  107. /* Loop for 3 transmission byte cycles and count the low and high bits. Sampled at a rate to be able to count each bit twice.
  108. *
  109. * One transmission byte is 10 bits (8 bytes of data + 1 start bit + 1 stop bit)
  110. * We sample at every half bit time, therefore 20 samples per transmission byte,
  111. * therefore 60 samples for 3 transmission bytes
  112. */
  113. while (cnt < 60) {
  114. // Only read pin when SysTick timer is true
  115. if (systick_get_countflag() == 1) {
  116. if (GPIO_ReadPinInput(BOARD_PORT_USART, BOARD_PIN_RX) == 0) {
  117. cnt_consecutive_low++; // Increment the consecutive low counter
  118. } else {
  119. cnt_consecutive_low = 0; // Reset the consecutive low counter
  120. }
  121. cnt++;
  122. }
  123. // If 9 consecutive low bits were received break out of the loop
  124. if (cnt_consecutive_low >= 18) {
  125. break;
  126. }
  127. }
  128. systick_counter_disable(); // Stop the timer
  129. /*
  130. * If a break is detected, return true, else false
  131. *
  132. * Break is detected if line was low for 9 consecutive bits.
  133. */
  134. if (cnt_consecutive_low >= 18) {
  135. return true;
  136. }
  137. #endif // !defined(SERIAL_BREAK_DETECT_DISABLED)
  138. return false;
  139. }
  140. #endif
  141. uint32_t
  142. board_get_devices(void)
  143. {
  144. uint32_t devices = BOOT_DEVICES_SELECTION;
  145. if (usb_connected) {
  146. devices &= BOOT_DEVICES_FILTER_ONUSB;
  147. }
  148. return devices;
  149. }
  150. static void
  151. board_init(void)
  152. {
  153. exit_vlpr();
  154. /* fix up the max firmware size, we have to read memory to get this */
  155. board_info.fw_size = APP_SIZE_MAX;
  156. #if defined(BOARD_POWER_PIN_OUT)
  157. /* Configure the Power pins */
  158. /* Sets the ports clocking */
  159. CLOCK_EnableClock(KINETIS_CLOCK_PORT(BOARD_POWER_PORT));
  160. port_pin_config_t power_port_config = {
  161. .pullSelect = kPORT_PullDown,
  162. .slewRate = kPORT_FastSlewRate,
  163. .passiveFilterEnable = kPORT_PassiveFilterDisable,
  164. .openDrainEnable = kPORT_OpenDrainDisable,
  165. .driveStrength = kPORT_LowDriveStrength,
  166. .mux = kPORT_MuxAsGpio,
  167. .lockRegister = kPORT_UnLockRegister,
  168. };
  169. /* Sets the port configuration */
  170. PORT_SetPinConfig(KINETIS_PORT(BOARD_POWER_PORT), BOARD_POWER_PIN_OUT, &power_port_config);
  171. gpio_pin_config_t power_pin_config = {
  172. kGPIO_DigitalOutput,
  173. 1,
  174. };
  175. /* Sets the pin configuration */
  176. GPIO_PinInit(KINETIS_GPIO(BOARD_POWER_PORT), BOARD_POWER_PIN_OUT, &power_pin_config);
  177. BOARD_POWER_ON(KINETIS_GPIO(BOARD_POWER_PORT), BOARD_POWER_PIN_OUT);
  178. #endif
  179. #if INTERFACE_USB
  180. // Disable the MPU otherwise USB cannot access the bus
  181. MPU->CESR = 0;
  182. /* enable Port pin to sample VBUS */
  183. CLOCK_EnableClock(KINETIS_CLOCK_PORT(BOARD_PORT_VBUS));
  184. port_pin_config_t vbus_port_config = {
  185. .pullSelect = kPORT_PullDown,
  186. .slewRate = kPORT_FastSlewRate,
  187. .passiveFilterEnable = kPORT_PassiveFilterDisable,
  188. .openDrainEnable = kPORT_OpenDrainDisable,
  189. .driveStrength = kPORT_LowDriveStrength,
  190. .mux = kPORT_MuxAsGpio,
  191. .lockRegister = kPORT_UnLockRegister,
  192. };
  193. /* Sets the port configuration */
  194. PORT_SetPinConfig(KINETIS_PORT(BOARD_PORT_VBUS), BOARD_PIN_VBUS, &vbus_port_config);
  195. gpio_pin_config_t vbus_pin_config = {
  196. kGPIO_DigitalInput,
  197. 0,
  198. };
  199. /* Sets the pin configuration */
  200. GPIO_PinInit(KINETIS_GPIO(BOARD_PORT_VBUS), BOARD_PIN_VBUS, &vbus_pin_config);
  201. #endif
  202. #if INTERFACE_USART
  203. /* configure USART clock */
  204. CLOCK_EnableClock(KINETIS_CLOCK_UART(BOARD_USART));
  205. /* configure USART pins */
  206. CLOCK_EnableClock(KINETIS_CLOCK_PORT(BOARD_PORT_USART));
  207. port_pin_config_t uart_port_config = {
  208. .pullSelect = kPORT_PullDisable,
  209. .slewRate = kPORT_FastSlewRate,
  210. .passiveFilterEnable = kPORT_PassiveFilterDisable,
  211. .openDrainEnable = kPORT_OpenDrainDisable,
  212. .driveStrength = kPORT_LowDriveStrength,
  213. .mux = BOARD_PORT_USART_AF,
  214. .lockRegister = kPORT_UnLockRegister,
  215. };
  216. /* Sets the port configuration */
  217. PORT_SetMultiplePinsConfig(KINETIS_PORT(BOARD_PORT_USART), KINETIS_MASK(BOARD_PIN_TX) | KINETIS_MASK(BOARD_PIN_RX),
  218. &uart_port_config);
  219. #endif
  220. /* Initialize LEDs */
  221. port_pin_config_t led_port_config = {
  222. .pullSelect = kPORT_PullDisable,
  223. .slewRate = kPORT_FastSlewRate,
  224. .passiveFilterEnable = kPORT_PassiveFilterDisable,
  225. .openDrainEnable = kPORT_OpenDrainDisable,
  226. .driveStrength = kPORT_LowDriveStrength,
  227. .mux = kPORT_MuxAsGpio,
  228. .lockRegister = kPORT_UnLockRegister,
  229. };
  230. uint32_t leds = 0;
  231. #if defined(BOARD_PIN_LED_ACTIVITY)
  232. leds |= KINETIS_MASK(BOARD_PIN_LED_ACTIVITY);
  233. #endif
  234. #if defined(BOARD_PIN_LED_BOOTLOADER)
  235. leds |= KINETIS_MASK(BOARD_PIN_LED_BOOTLOADER);
  236. #endif
  237. if (leds) {
  238. CLOCK_EnableClock(KINETIS_CLOCK_PORT(BOARD_PORT_LEDS));
  239. PORT_SetMultiplePinsConfig(KINETIS_PORT(BOARD_PORT_LEDS), leds, &led_port_config);
  240. gpio_pin_config_t led_pin_config = {
  241. kGPIO_DigitalOutput,
  242. 1,
  243. };
  244. /* Sets the pin configuration */
  245. #if defined(BOARD_PIN_LED_ACTIVITY)
  246. GPIO_PinInit(KINETIS_GPIO(BOARD_PORT_LEDS), BOARD_PIN_LED_ACTIVITY, &led_pin_config);
  247. #endif
  248. #if defined(BOARD_PIN_LED_BOOTLOADER)
  249. GPIO_PinInit(KINETIS_GPIO(BOARD_PORT_LEDS), BOARD_PIN_LED_BOOTLOADER, &led_pin_config);
  250. #endif
  251. BOARD_LED_ON(KINETIS_GPIO(BOARD_PORT_LEDS), leds);
  252. }
  253. }
  254. void
  255. board_deinit(void)
  256. {
  257. port_pin_config_t unconfigure_port_config = {
  258. kPORT_PullDisable,
  259. kPORT_FastSlewRate,
  260. kPORT_PassiveFilterDisable,
  261. kPORT_OpenDrainDisable,
  262. kPORT_LowDriveStrength,
  263. kPORT_PinDisabledOrAnalog,
  264. kPORT_UnLockRegister,
  265. };
  266. gpio_pin_config_t unconfigure__pin_config = {
  267. kGPIO_DigitalInput,
  268. 0,
  269. };
  270. #if defined(BOARD_POWER_PIN_OUT) && defined(BOARD_POWER_PIN_RELEASE)
  271. /* deinitialize the POWER pin - with the assumption the hold up time of
  272. * the voltage being bleed off by an inupt pin impedance will allow
  273. * enough time to boot the app
  274. */
  275. GPIO_PinInit(KINETIS_GPIO(BOARD_POWER_PORT), BOARD_POWER_PIN_OUT, &unconfigure__pin_config);
  276. PORT_SetPinConfig(KINETIS_PORT(BOARD_POWER_PORT), BOARD_POWER_PIN, &unconfigure_port_config);
  277. #endif
  278. #if INTERFACE_USB
  279. PORT_SetPinConfig(KINETIS_PORT(BOARD_PORT_VBUS), BOARD_PIN_VBUS, &unconfigure_port_config);
  280. #endif
  281. #if INTERFACE_USART
  282. PORT_SetMultiplePinsConfig(KINETIS_PORT(BOARD_PORT_USART), KINETIS_MASK(BOARD_PIN_TX) | KINETIS_MASK(BOARD_PIN_RX),
  283. &unconfigure_port_config);
  284. #endif
  285. /* deinitialise LEDs */
  286. uint32_t leds = 0;
  287. #if defined(BOARD_PIN_LED_ACTIVITY)
  288. leds |= KINETIS_MASK(BOARD_PIN_LED_ACTIVITY);
  289. #endif
  290. #if defined(BOARD_PIN_LED_BOOTLOADER)
  291. leds |= KINETIS_MASK(BOARD_PIN_LED_BOOTLOADER);
  292. #endif
  293. if (leds) {
  294. GPIO_ClearPinsOutput(KINETIS_GPIO(BOARD_PORT_LEDS), leds);
  295. #if defined(BOARD_PIN_LED_ACTIVITY)
  296. GPIO_PinInit(KINETIS_GPIO(BOARD_PORT_LEDS), BOARD_PIN_LED_ACTIVITY, &unconfigure__pin_config);
  297. #endif
  298. #if defined(BOARD_PIN_LED_BOOTLOADER)
  299. GPIO_PinInit(KINETIS_GPIO(BOARD_PORT_LEDS), BOARD_PIN_LED_BOOTLOADER, &unconfigure__pin_config);
  300. #endif
  301. PORT_SetMultiplePinsConfig(KINETIS_PORT(BOARD_PORT_LEDS), leds, &unconfigure_port_config);
  302. }
  303. /* Incase of any over lap un-configure clocks last -*/
  304. #if defined(BOARD_POWER_PIN_OUT) && defined(BOARD_POWER_PIN_RELEASE)
  305. CLOCK_DisableClock(KINETIS_CLOCK_PORT(BOARD_POWER_PORT));
  306. #endif
  307. #if INTERFACE_USB
  308. CLOCK_DisableClock(KINETIS_CLOCK_PORT(BOARD_PORT_VBUS));
  309. #endif
  310. #if INTERFACE_USART
  311. CLOCK_DisableClock(KINETIS_CLOCK_PORT(BOARD_PORT_USART));
  312. CLOCK_DisableClock(KINETIS_CLOCK_UART(BOARD_USART));
  313. #endif
  314. if (leds) {
  315. CLOCK_DisableClock(KINETIS_CLOCK_PORT(BOARD_PORT_LEDS));
  316. }
  317. }
  318. static void CLOCK_CONFIG_FllStableDelay(void)
  319. {
  320. uint32_t i = 30000U;
  321. while (i--) {
  322. __NOP();
  323. }
  324. }
  325. void
  326. clock_deinit(void)
  327. {
  328. const mcg_config_t mcgConfig_BOARD_BootClockHSRUN = {
  329. .mcgMode = kMCG_ModePBE, /* PEE - PLL Engaged External */
  330. .irclkEnableMode = kMCG_IrclkEnable, /* MCGIRCLK enabled, MCGIRCLK disabled in STOP mode */
  331. .ircs = kMCG_IrcSlow, /* Slow internal reference clock selected */
  332. .fcrdiv = 0x1U, /* Fast IRC divider: divided by 2 */
  333. .frdiv = 0x0U, /* FLL reference clock divider: divided by 32 */
  334. .drs = kMCG_DrsLow, /* Low frequency range */
  335. .dmx32 = kMCG_Dmx32Default, /* DCO has a default range of 25% */
  336. .oscsel = kMCG_OscselOsc, /* Selects System Oscillator (OSCCLK) */
  337. .pll0Config =
  338. {
  339. .enableMode = MCG_PLL_DISABLE, /* MCGPLLCLK disabled */
  340. .prdiv = 0x0U, /* PLL Reference divider: divided by 1 */
  341. .vdiv = 0xeU, /* VCO divider: multiplied by 30 */
  342. },
  343. .pllcs = kMCG_PllClkSelPll0, /* PLL0 output clock is selected */
  344. };
  345. const osc_config_t oscConfig_BOARD_BootClockHSRUN = {
  346. .freq = 12000000U, /* Oscillator frequency: 12000000Hz */
  347. .capLoad = (OSC_CAP0P), /* Oscillator capacity load: 0pF */
  348. .workMode = kOSC_ModeOscLowPower, /* Oscillator low power */
  349. .oscerConfig =
  350. {
  351. .enableMode = kOSC_ErClkEnable, /* Enable external reference clock, disable external reference clock in STOP mode */
  352. .erclkDiv = 0, /* Divider for OSCERCLK: divided by 1 */
  353. }
  354. };
  355. /*******************************************************************************
  356. * Code for BOARD_BootClockHSRUN configuration
  357. ******************************************************************************/
  358. /* Set the system clock dividers in SIM to safe value. */
  359. CLOCK_SetSimSafeDivs();
  360. /* Initializes OSC0 according to board configuration. */
  361. CLOCK_InitOsc0(&oscConfig_BOARD_BootClockHSRUN);
  362. CLOCK_SetXtal0Freq(oscConfig_BOARD_BootClockHSRUN.freq);
  363. /* Configure the Internal Reference clock (MCGIRCLK). */
  364. CLOCK_SetInternalRefClkConfig(mcgConfig_BOARD_BootClockHSRUN.irclkEnableMode,
  365. mcgConfig_BOARD_BootClockHSRUN.ircs,
  366. mcgConfig_BOARD_BootClockHSRUN.fcrdiv);
  367. /* Set MCG to PEE mode. */
  368. CLOCK_BootToBlpeMode(mcgConfig_BOARD_BootClockHSRUN.oscsel);
  369. const mcg_config_t mcgConfig_ClocksFunc_1 = {
  370. .mcgMode = kMCG_ModeFEI, /* FEI - FLL Engaged Internal */
  371. .irclkEnableMode = MCG_IRCLK_DISABLE, /* MCGIRCLK disabled */
  372. .ircs = kMCG_IrcSlow, /* Slow internal reference clock selected */
  373. .fcrdiv = 0x1U, /* Fast IRC divider: divided by 2 */
  374. .frdiv = 0x0U, /* FLL reference clock divider: divided by 1 */
  375. .drs = kMCG_DrsLow, /* Low frequency range */
  376. .dmx32 = kMCG_Dmx32Default, /* DCO has a default range of 25% */
  377. .oscsel = kMCG_OscselOsc, /* Selects System Oscillator (OSCCLK) */
  378. .pll0Config =
  379. {
  380. .enableMode = MCG_PLL_DISABLE, /* MCGPLLCLK disabled */
  381. .prdiv = 0x0U, /* PLL Reference divider: divided by 1 */
  382. .vdiv = 0x0U, /* VCO divider: multiplied by 16 */
  383. },
  384. .pllcs = kMCG_PllClkSelPll0, /* PLL0 output clock is selected */
  385. };
  386. const sim_clock_config_t simConfig_ClocksFunc_1 = {
  387. .pllFllSel = SIM_PLLFLLSEL_MCGFLLCLK_CLK, /* PLLFLL select: MCGFLLCLK clock */
  388. .pllFllDiv = 0, /* PLLFLLSEL clock divider divisor: divided by 1 */
  389. .pllFllFrac = 0, /* PLLFLLSEL clock divider fraction: multiplied by 1 */
  390. .er32kSrc = SIM_OSC32KSEL_OSC32KCLK_CLK, /* OSC32KSEL select: OSC32KCLK clock */
  391. .clkdiv1 = 0x110000U, /* SIM_CLKDIV1 - OUTDIV1: /1, OUTDIV2: /1, OUTDIV3: /2, OUTDIV4: /2 */
  392. };
  393. /* Set MCG to FEI mode. */
  394. #if FSL_CLOCK_DRIVER_VERSION >= MAKE_VERSION(2, 2, 0)
  395. CLOCK_SetFbeMode(0, mcgConfig_ClocksFunc_1.dmx32,
  396. mcgConfig_ClocksFunc_1.drs,
  397. CLOCK_CONFIG_FllStableDelay);
  398. CLOCK_BootToFeiMode(mcgConfig_ClocksFunc_1.dmx32,
  399. mcgConfig_ClocksFunc_1.drs,
  400. CLOCK_CONFIG_FllStableDelay);
  401. #else
  402. CLOCK_BootToFeiMode(mcgConfig_ClocksFunc_1.drs,
  403. CLOCK_CONFIG_FllStableDelay);
  404. #endif
  405. /* Set the clock configuration in SIM module. */
  406. CLOCK_SetSimConfig(&simConfig_ClocksFunc_1);
  407. /* Set SystemCoreClock variable. */
  408. SystemCoreClock = BOARD_RESETCLOCKRUN_CORE_CLOCK;
  409. }
  410. void flash_lock(void)
  411. {
  412. }
  413. uint32_t flash_func_sector_size(unsigned sector)
  414. {
  415. return (sector < BOARD_FLASH_SECTORS) ? FLASH_SECTOR_SIZE : 0;
  416. }
  417. void flash_erase_sector(unsigned sector)
  418. {
  419. __disable_irq();
  420. FLASH_Erase(&s_flashDriver, (sector * FLASH_SECTOR_SIZE), FLASH_SECTOR_SIZE, kFLASH_ApiEraseKey);
  421. __enable_irq();
  422. }
  423. static bool flash_verify_erase(unsigned sector)
  424. {
  425. /* Calculate the physical address of the sector
  426. */
  427. volatile uint32_t address = APP_LOAD_ADDRESS + (sector - FIRST_FLASH_SECTOR_TO_ERASE) * FLASH_SECTOR_SIZE;
  428. __disable_irq();
  429. volatile status_t status = FLASH_VerifyErase(&s_flashDriver, address, FLASH_SECTOR_SIZE, kFLASH_MarginValueNormal);
  430. __enable_irq();
  431. return status == kStatus_FLASH_Success;
  432. }
  433. void flash_func_erase_sector(unsigned sector)
  434. {
  435. if (sector >= BOARD_FLASH_SECTORS || sector < FIRST_FLASH_SECTOR_TO_ERASE) {
  436. return;
  437. }
  438. if (!flash_verify_erase(sector)) {
  439. /* erase the sector if it failed the blank check */
  440. flash_erase_sector(sector);
  441. }
  442. }
  443. static uint32_t words[2] = {0, 0};
  444. static uint32_t second_word = 0xffffffff;
  445. static uint32_t pending = 0;
  446. void
  447. flash_func_write_word(uint32_t address, uint32_t word)
  448. {
  449. address += APP_LOAD_ADDRESS;
  450. uint32_t loc = (address & 4) >> 2;
  451. pending = loc == 0;
  452. // Cache words
  453. words[loc] = word;
  454. /* Program the 64bits. */
  455. // Time to write first and second word
  456. if (address == APP_LOAD_ADDRESS && second_word != 0xffffffff) {
  457. words[1] = second_word;
  458. loc = 1;
  459. pending = 0;
  460. address += sizeof(words[1]);
  461. }
  462. if (loc == 1) {
  463. if (address == APP_LOAD_ADDRESS + sizeof(word) && second_word == 0xffffffff) {
  464. second_word = words[1];
  465. return;
  466. }
  467. __disable_irq();
  468. FLASH_Program(&s_flashDriver, address - sizeof(words[1]), &words[0], sizeof(words));
  469. __enable_irq();
  470. }
  471. }
  472. uint32_t flash_func_read_word(uint32_t address)
  473. {
  474. if (address & 3) {
  475. return 0;
  476. }
  477. if (address == sizeof(uint32_t) && second_word != 0xffffffff) { return second_word; }
  478. return (pending) ? words[0] : *(uint32_t *)(address + APP_LOAD_ADDRESS);
  479. }
  480. uint32_t
  481. flash_func_read_otp(uint32_t address)
  482. {
  483. return 0;
  484. }
  485. uint32_t get_mcu_id(void)
  486. {
  487. return SIM->SDID;
  488. }
  489. int get_mcu_desc(int max, uint8_t *revstr)
  490. {
  491. const char dig[] = "0123456789ABCDEF";
  492. const char none[] = "MK66FN2M0VMD18,0";
  493. int i;
  494. for (i = 0; none[i] && i < max - 1; i++) {
  495. revstr[i] = none[i];
  496. }
  497. uint32_t id = (get_mcu_id() & REVID_MASK) >> REVID_SHIFTS;
  498. revstr[i - 1] = dig[id];
  499. return i;
  500. }
  501. int check_silicon(void)
  502. {
  503. return 0;
  504. }
  505. uint32_t
  506. flash_func_read_sn(uint32_t address)
  507. {
  508. address /= sizeof(address);
  509. const volatile uint32_t *p = &SIM->UIDH;
  510. return p[address];
  511. }
  512. void
  513. led_on(unsigned led)
  514. {
  515. switch (led) {
  516. case LED_ACTIVITY:
  517. #if defined(BOARD_PIN_LED_ACTIVITY)
  518. BOARD_LED_ON(KINETIS_GPIO(BOARD_PORT_LEDS), KINETIS_MASK(BOARD_PIN_LED_ACTIVITY));
  519. #endif
  520. break;
  521. case LED_BOOTLOADER:
  522. #if defined(BOARD_PIN_LED_BOOTLOADER)
  523. BOARD_LED_ON(KINETIS_GPIO(BOARD_PORT_LEDS), KINETIS_MASK(BOARD_PIN_LED_BOOTLOADER));
  524. #endif
  525. break;
  526. }
  527. }
  528. void
  529. led_off(unsigned led)
  530. {
  531. switch (led) {
  532. case LED_ACTIVITY:
  533. #if defined(BOARD_PIN_LED_ACTIVITY)
  534. BOARD_LED_OFF(KINETIS_GPIO(BOARD_PORT_LEDS), KINETIS_MASK(BOARD_PIN_LED_ACTIVITY));
  535. #endif
  536. break;
  537. case LED_BOOTLOADER:
  538. #if defined(BOARD_PIN_LED_BOOTLOADER)
  539. BOARD_LED_OFF(KINETIS_GPIO(BOARD_PORT_LEDS), KINETIS_MASK(BOARD_PIN_LED_BOOTLOADER));
  540. #endif
  541. break;
  542. }
  543. }
  544. void
  545. led_toggle(unsigned led)
  546. {
  547. switch (led) {
  548. case LED_ACTIVITY:
  549. #if defined(BOARD_PIN_LED_ACTIVITY)
  550. GPIO_TogglePinsOutput(KINETIS_GPIO(BOARD_PORT_LEDS), KINETIS_MASK(BOARD_PIN_LED_ACTIVITY));
  551. #endif
  552. break;
  553. case LED_BOOTLOADER:
  554. #if defined(BOARD_PIN_LED_BOOTLOADER)
  555. GPIO_TogglePinsOutput(KINETIS_GPIO(BOARD_PORT_LEDS), KINETIS_MASK(BOARD_PIN_LED_BOOTLOADER));
  556. #endif
  557. break;
  558. }
  559. }
  560. int
  561. main(void)
  562. {
  563. // The Kinetis start up code has initialized Clocks and MPU, FPU
  564. bool try_boot = true; /* try booting before we drop to the bootloader */
  565. unsigned timeout = BOOTLOADER_DELAY; /* if nonzero, drop out of the bootloader after this time */
  566. #if defined(BOARD_POWER_PIN_OUT)
  567. /* Here we check for the app setting the POWER_DOWN_RTC_SIGNATURE
  568. * in this case, we reset the signature and wait to die
  569. */
  570. if (board_get_rtc_signature() == POWER_DOWN_RTC_SIGNATURE) {
  571. board_set_rtc_signature(0);
  572. while (1);
  573. }
  574. #endif
  575. // Initialize the RAM based function in the Kinetis Flash Lib (this could be removed with run_from_ram tags
  576. // and some linker magic.
  577. s_flashDriver.flashExecuteInRamFunctionInfo = &s_flashExecuteInRamFunctionInfo.activeFunctionCount;
  578. FLASH_PrepareExecuteInRamFunctions(&s_flashDriver);
  579. FLASH_Init(&s_flashDriver);
  580. /* do board-specific initialization */
  581. board_init();
  582. /*
  583. * Check the force-bootloader register; if we find the signature there, don't
  584. * try booting.
  585. */
  586. if (board_get_rtc_signature() == BOOT_RTC_SIGNATURE) {
  587. /*
  588. * Don't even try to boot before dropping to the bootloader.
  589. */
  590. try_boot = false;
  591. /*
  592. * Don't drop out of the bootloader until something has been uploaded.
  593. */
  594. timeout = 0;
  595. /*
  596. * Clear the signature so that if someone resets us while we're
  597. * in the bootloader we'll try to boot next time.
  598. */
  599. board_set_rtc_signature(0);
  600. }
  601. /*
  602. * Check if the force-bootloader pins are strapped; if strapped,
  603. * don't try booting.
  604. */
  605. if (board_test_force_pin()) {
  606. try_boot = false;
  607. }
  608. #if INTERFACE_USB
  609. /*
  610. * Check for USB connection - if present, don't try to boot, but set a timeout after
  611. * which we will fall out of the bootloader.
  612. *
  613. * If the force-bootloader pins are tied, we will stay here until they are removed and
  614. * we then time out.
  615. */
  616. #if defined(BOARD_USB_VBUS_SENSE_DISABLED)
  617. try_boot = false;
  618. #else
  619. if (GPIO_ReadPinInput(KINETIS_GPIO(BOARD_PORT_VBUS), BOARD_PIN_VBUS) != 0) {
  620. usb_connected = true;
  621. /* don't try booting before we set up the bootloader */
  622. try_boot = false;
  623. }
  624. #endif
  625. #endif
  626. #if INTERFACE_USART
  627. /*
  628. * Check for if the USART port RX line is receiving a break command, or is being held low. If yes,
  629. * don't try to boot, but set a timeout after
  630. * which we will fall out of the bootloader.
  631. *
  632. * If the force-bootloader pins are tied, we will stay here until they are removed and
  633. * we then time out.
  634. */
  635. if (board_test_usart_receiving_break()) {
  636. try_boot = false;
  637. }
  638. #endif
  639. /* Try to boot the app if we think we should just go straight there */
  640. if (try_boot) {
  641. /* set the boot-to-bootloader flag so that if boot fails on reset we will stop here */
  642. #ifdef BOARD_BOOT_FAIL_DETECT
  643. board_set_rtc_signature(BOOT_RTC_SIGNATURE);
  644. #endif
  645. /* try to boot immediately */
  646. jump_to_app();
  647. // If it failed to boot, reset the boot signature and stay in bootloader
  648. board_set_rtc_signature(BOOT_RTC_SIGNATURE);
  649. /* booting failed, stay in the bootloader forever */
  650. timeout = 0;
  651. }
  652. /* start the interface */
  653. #if INTERFACE_USART
  654. cinit(BOARD_INTERFACE_CONFIG_USART, USART);
  655. #endif
  656. #if INTERFACE_USB
  657. cinit(BOARD_INTERFACE_CONFIG_USB, USB);
  658. #endif
  659. while (1) {
  660. /* run the bootloader, come back after an app is uploaded or we time out */
  661. bootloader(timeout);
  662. /* if the force-bootloader pins are strapped, just loop back */
  663. if (board_test_force_pin()) {
  664. continue;
  665. }
  666. #if INTERFACE_USART
  667. /* if the USART port RX line is still receiving a break, just loop back */
  668. if (board_test_usart_receiving_break()) {
  669. continue;
  670. }
  671. #endif
  672. /* set the boot-to-bootloader flag so that if boot fails on reset we will stop here */
  673. #ifdef BOARD_BOOT_FAIL_DETECT
  674. board_set_rtc_signature(BOOT_RTC_SIGNATURE);
  675. #endif
  676. /* look to see if we can boot the app */
  677. jump_to_app();
  678. /* launching the app failed - stay in the bootloader forever */
  679. timeout = 0;
  680. }
  681. }
  682. void _start()
  683. {
  684. main();
  685. }
  686. void SysTick_Handler()
  687. {
  688. sys_tick_handler();
  689. }
  690. int DbgConsole_Printf(char *fmt_s, ...)
  691. {
  692. return 0;
  693. }