clkconf.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  1. /***********************************************************************************************************************
  2. * DISCLAIMER
  3. * This software is supplied by Renesas Electronics Corporation and is only intended for use with Renesas products. No
  4. * other uses are authorized. This software is owned by Renesas Electronics Corporation and is protected under all
  5. * applicable laws, including copyright laws.
  6. * THIS SOFTWARE IS PROVIDED "AS IS" AND RENESAS MAKES NO WARRANTIES REGARDING
  7. * THIS SOFTWARE, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY,
  8. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. ALL SUCH WARRANTIES ARE EXPRESSLY DISCLAIMED. TO THE MAXIMUM
  9. * EXTENT PERMITTED NOT PROHIBITED BY LAW, NEITHER RENESAS ELECTRONICS CORPORATION NOR ANY OF ITS AFFILIATED COMPANIES
  10. * SHALL BE LIABLE FOR ANY DIRECT, INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY REASON RELATED TO THIS
  11. * SOFTWARE, EVEN IF RENESAS OR ITS AFFILIATES HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  12. * Renesas reserves the right, without notice, to make changes to this software and to discontinue the availability of
  13. * this software. By using this software, you agree to the additional terms and conditions found by accessing the
  14. * following link:
  15. * http://www.renesas.com/disclaimer
  16. *
  17. * Copyright (C) 2016 Renesas Electronics Corporation. All rights reserved.
  18. ***********************************************************************************************************************/
  19. /***********************************************************************************************************************
  20. * File Name : clkconf.c
  21. * Device(s) : RX65N
  22. * Description : Configures the clock settings for each of the device clocks.
  23. ***********************************************************************************************************************/
  24. /***********************************************************************************************************************
  25. * History : DD.MM.YYYY Version Description
  26. * : 01.10.2016 1.00 First Release (as of resetprg.c)
  27. * : 15.05.2017 2.00 Deleted unnecessary comments.
  28. * Applied the following technical update.
  29. * - TN-RX*-A164A - Added initialization procedure when the realtime clock
  30. * is not to be used.
  31. * Changed the sub-clock oscillator settings.
  32. * Added the bsp startup module disable function.
  33. * : 01.07.2018 2.01 Include RTOS /timer preprocessing.
  34. * Added processing after writing ROMWT register.
  35. * : 27.07.2018 2.02 Added the comment to for statement.
  36. * Added the comment to while statement.
  37. * : xx.xx.xxxx 2.03 Added bsp_ram_initialize function call.
  38. * Added wait time after LOCO stop.
  39. * Removed RTOS header files because they are included in the platform.h.
  40. * Added support for GNUC and ICCRX.
  41. * Splitted resetprg.c between resetprg.c and clkconf.c.
  42. ***********************************************************************************************************************/
  43. /***********************************************************************************************************************
  44. Includes <System Includes> , "Project Includes"
  45. ***********************************************************************************************************************/
  46. /* Define the target platform */
  47. #include "platform.h"
  48. /* When using the user startup program, disable the following code. */
  49. #if (BSP_CFG_STARTUP_DISABLE == 0)
  50. /***********************************************************************************************************************
  51. Macro definitions
  52. ***********************************************************************************************************************/
  53. #define ROMWT_FREQ_THRESHOLD_01 50000000 // ICLK > 50MHz requires ROMWT register update
  54. #define ROMWT_FREQ_THRESHOLD_02 100000000 // ICLK > 100MHz requires ROMWT register update
  55. /***********************************************************************************************************************
  56. Private global variables and functions
  57. ***********************************************************************************************************************/
  58. /* Clock source select function declaration */
  59. static void clock_source_select(void);
  60. /***********************************************************************************************************************
  61. * Function name: operating_frequency_set
  62. * Description : Configures the clock settings for each of the device clocks
  63. * Arguments : none
  64. * Return value : none
  65. ***********************************************************************************************************************/
  66. void operating_frequency_set (void)
  67. {
  68. /* Used for constructing value to write to SCKCR register. */
  69. uint32_t temp_clock = 0;
  70. /*
  71. Default settings:
  72. Clock Description Frequency
  73. ----------------------------------------
  74. Input Clock Frequency............ 24 MHz
  75. PLL frequency (x10).............. 240 MHz
  76. Internal Clock Frequency......... 120 MHz
  77. Peripheral Clock Frequency A..... 120 MHz
  78. Peripheral Clock Frequency B..... 60 MHz
  79. Peripheral Clock Frequency C..... 60 MHz
  80. Peripheral Clock Frequency D..... 60 MHz
  81. Flash IF Clock Frequency......... 60 MHz
  82. External Bus Clock Frequency..... 120 MHz
  83. USB Clock Frequency.............. 48 MHz */
  84. /* Protect off. */
  85. SYSTEM.PRCR.WORD = 0xA50B;
  86. /* Select the clock based upon user's choice. */
  87. clock_source_select();
  88. /* Figure out setting for FCK bits. */
  89. #if BSP_CFG_FCK_DIV == 1
  90. /* Do nothing since FCK bits should be 0. */
  91. #elif BSP_CFG_FCK_DIV == 2
  92. temp_clock |= 0x10000000;
  93. #elif BSP_CFG_FCK_DIV == 4
  94. temp_clock |= 0x20000000;
  95. #elif BSP_CFG_FCK_DIV == 8
  96. temp_clock |= 0x30000000;
  97. #elif BSP_CFG_FCK_DIV == 16
  98. temp_clock |= 0x40000000;
  99. #elif BSP_CFG_FCK_DIV == 32
  100. temp_clock |= 0x50000000;
  101. #elif BSP_CFG_FCK_DIV == 64
  102. temp_clock |= 0x60000000;
  103. #else
  104. #error "Error! Invalid setting for BSP_CFG_FCK_DIV in r_bsp_config.h"
  105. #endif
  106. /* Figure out setting for ICK bits. */
  107. #if BSP_CFG_ICK_DIV == 1
  108. /* Do nothing since ICK bits should be 0. */
  109. #elif BSP_CFG_ICK_DIV == 2
  110. temp_clock |= 0x01000000;
  111. #elif BSP_CFG_ICK_DIV == 4
  112. temp_clock |= 0x02000000;
  113. #elif BSP_CFG_ICK_DIV == 8
  114. temp_clock |= 0x03000000;
  115. #elif BSP_CFG_ICK_DIV == 16
  116. temp_clock |= 0x04000000;
  117. #elif BSP_CFG_ICK_DIV == 32
  118. temp_clock |= 0x05000000;
  119. #elif BSP_CFG_ICK_DIV == 64
  120. temp_clock |= 0x06000000;
  121. #else
  122. #error "Error! Invalid setting for BSP_CFG_ICK_DIV in r_bsp_config.h"
  123. #endif
  124. /* Figure out setting for BCK bits. */
  125. #if BSP_CFG_BCK_DIV == 1
  126. /* Do nothing since BCK bits should be 0. */
  127. #elif BSP_CFG_BCK_DIV == 2
  128. temp_clock |= 0x00010000;
  129. #elif BSP_CFG_BCK_DIV == 4
  130. temp_clock |= 0x00020000;
  131. #elif BSP_CFG_BCK_DIV == 8
  132. temp_clock |= 0x00030000;
  133. #elif BSP_CFG_BCK_DIV == 16
  134. temp_clock |= 0x00040000;
  135. #elif BSP_CFG_BCK_DIV == 32
  136. temp_clock |= 0x00050000;
  137. #elif BSP_CFG_BCK_DIV == 64
  138. temp_clock |= 0x00060000;
  139. #else
  140. #error "Error! Invalid setting for BSP_CFG_BCK_DIV in r_bsp_config.h"
  141. #endif
  142. /* Configure PSTOP1 bit for BCLK output. */
  143. #if BSP_CFG_BCLK_OUTPUT == 0
  144. /* Set PSTOP1 bit */
  145. temp_clock |= 0x00800000;
  146. #elif BSP_CFG_BCLK_OUTPUT == 1
  147. /* Clear PSTOP1 bit */
  148. temp_clock &= ~0x00800000;
  149. #elif BSP_CFG_BCLK_OUTPUT == 2
  150. /* Clear PSTOP1 bit */
  151. temp_clock &= ~0x00800000;
  152. /* Set BCLK divider bit */
  153. SYSTEM.BCKCR.BIT.BCLKDIV = 1;
  154. #else
  155. #error "Error! Invalid setting for BSP_CFG_BCLK_OUTPUT in r_bsp_config.h"
  156. #endif
  157. /* Configure PSTOP0 bit for SDCLK output. */
  158. #if BSP_CFG_SDCLK_OUTPUT == 0
  159. /* Set PSTOP0 bit */
  160. temp_clock |= 0x00400000;
  161. #elif BSP_CFG_SDCLK_OUTPUT == 1
  162. /* Clear PSTOP0 bit */
  163. temp_clock &= ~0x00400000;
  164. #else
  165. #error "Error! Invalid setting for BSP_CFG_SDCLK_OUTPUT in r_bsp_config.h"
  166. #endif
  167. /* Figure out setting for PCKA bits. */
  168. #if BSP_CFG_PCKA_DIV == 1
  169. /* Do nothing since PCKA bits should be 0. */
  170. #elif BSP_CFG_PCKA_DIV == 2
  171. temp_clock |= 0x00001000;
  172. #elif BSP_CFG_PCKA_DIV == 4
  173. temp_clock |= 0x00002000;
  174. #elif BSP_CFG_PCKA_DIV == 8
  175. temp_clock |= 0x00003000;
  176. #elif BSP_CFG_PCKA_DIV == 16
  177. temp_clock |= 0x00004000;
  178. #elif BSP_CFG_PCKA_DIV == 32
  179. temp_clock |= 0x00005000;
  180. #elif BSP_CFG_PCKA_DIV == 64
  181. temp_clock |= 0x00006000;
  182. #else
  183. #error "Error! Invalid setting for BSP_CFG_PCKA_DIV in r_bsp_config.h"
  184. #endif
  185. /* Figure out setting for PCKB bits. */
  186. #if BSP_CFG_PCKB_DIV == 1
  187. /* Do nothing since PCKB bits should be 0. */
  188. #elif BSP_CFG_PCKB_DIV == 2
  189. temp_clock |= 0x00000100;
  190. #elif BSP_CFG_PCKB_DIV == 4
  191. temp_clock |= 0x00000200;
  192. #elif BSP_CFG_PCKB_DIV == 8
  193. temp_clock |= 0x00000300;
  194. #elif BSP_CFG_PCKB_DIV == 16
  195. temp_clock |= 0x00000400;
  196. #elif BSP_CFG_PCKB_DIV == 32
  197. temp_clock |= 0x00000500;
  198. #elif BSP_CFG_PCKB_DIV == 64
  199. temp_clock |= 0x00000600;
  200. #else
  201. #error "Error! Invalid setting for BSP_CFG_PCKB_DIV in r_bsp_config.h"
  202. #endif
  203. /* Figure out setting for PCKC bits. */
  204. #if BSP_CFG_PCKC_DIV == 1
  205. /* Do nothing since PCKA bits should be 0. */
  206. #elif BSP_CFG_PCKC_DIV == 2
  207. temp_clock |= 0x00000010;
  208. #elif BSP_CFG_PCKC_DIV == 4
  209. temp_clock |= 0x00000020;
  210. #elif BSP_CFG_PCKC_DIV == 8
  211. temp_clock |= 0x00000030;
  212. #elif BSP_CFG_PCKC_DIV == 16
  213. temp_clock |= 0x00000040;
  214. #elif BSP_CFG_PCKC_DIV == 32
  215. temp_clock |= 0x00000050;
  216. #elif BSP_CFG_PCKC_DIV == 64
  217. temp_clock |= 0x00000060;
  218. #else
  219. #error "Error! Invalid setting for BSP_CFG_PCKC_DIV in r_bsp_config.h"
  220. #endif
  221. /* Figure out setting for PCKD bits. */
  222. #if BSP_CFG_PCKD_DIV == 1
  223. /* Do nothing since PCKD bits should be 0. */
  224. #elif BSP_CFG_PCKD_DIV == 2
  225. temp_clock |= 0x00000001;
  226. #elif BSP_CFG_PCKD_DIV == 4
  227. temp_clock |= 0x00000002;
  228. #elif BSP_CFG_PCKD_DIV == 8
  229. temp_clock |= 0x00000003;
  230. #elif BSP_CFG_PCKD_DIV == 16
  231. temp_clock |= 0x00000004;
  232. #elif BSP_CFG_PCKD_DIV == 32
  233. temp_clock |= 0x00000005;
  234. #elif BSP_CFG_PCKD_DIV == 64
  235. temp_clock |= 0x00000006;
  236. #else
  237. #error "Error! Invalid setting for BSP_CFG_PCKD_DIV in r_bsp_config.h"
  238. #endif
  239. /* Set SCKCR register. */
  240. SYSTEM.SCKCR.LONG = temp_clock;
  241. /* Re-init temp_clock to use to set SCKCR2. */
  242. temp_clock = 0;
  243. /* Figure out setting for UCK bits. */
  244. #if BSP_CFG_UCK_DIV == 2
  245. temp_clock |= 0x00000011;
  246. #elif BSP_CFG_UCK_DIV == 3
  247. temp_clock |= 0x00000021;
  248. #elif BSP_CFG_UCK_DIV == 4
  249. temp_clock |= 0x00000031;
  250. #elif BSP_CFG_UCK_DIV == 5
  251. temp_clock |= 0x00000041;
  252. #else
  253. #error "Error! Invalid setting for BSP_CFG_UCK_DIV in r_bsp_config.h"
  254. #endif
  255. /* Set SCKCR2 register. */
  256. SYSTEM.SCKCR2.WORD = (uint16_t)temp_clock;
  257. /* Choose clock source. Default for r_bsp_config.h is PLL. */
  258. SYSTEM.SCKCR3.WORD = ((uint16_t)BSP_CFG_CLOCK_SOURCE) << 8;
  259. #if (BSP_CFG_CLOCK_SOURCE != 0)
  260. /* We can now turn LOCO off since it is not going to be used. */
  261. SYSTEM.LOCOCR.BYTE = 0x01;
  262. /* Wait for five the LOCO cycles */
  263. /* 5 count of LOCO : (1000000/216000)*5�à23.148148148us
  264. 23 + 2 = 25us ("+2" is overhead cycle) */
  265. R_BSP_SoftwareDelay((uint32_t)25, BSP_DELAY_MICROSECS);
  266. #endif
  267. #if (BSP_CFG_ROM_CACHE_ENABLE == 1)
  268. FLASH.ROMCIV.WORD = 0x0001;
  269. /* WAIT_LOOP */
  270. while (FLASH.ROMCIV.WORD != 0x0000)
  271. {
  272. /* wait for bit to set */
  273. }
  274. FLASH.ROMCE.WORD = 0x0001;
  275. /* WAIT_LOOP */
  276. while (FLASH.ROMCE.WORD != 0x0001)
  277. {
  278. /* wait for bit to set */
  279. }
  280. #endif
  281. /* Protect on. */
  282. SYSTEM.PRCR.WORD = 0xA500;
  283. }
  284. /***********************************************************************************************************************
  285. * Function name: clock_source_select
  286. * Description : Enables and disables clocks as chosen by the user. This function also implements the delays
  287. * needed for the clocks to stabilize.
  288. * Arguments : none
  289. * Return value : none
  290. ***********************************************************************************************************************/
  291. static void clock_source_select (void)
  292. {
  293. volatile uint8_t i;
  294. volatile uint8_t dummy;
  295. volatile uint8_t tmp;
  296. /* Main clock will be not oscillate in software standby or deep software standby modes. */
  297. SYSTEM.MOFCR.BIT.MOFXIN = 0;
  298. /* Set the oscillation source of the main clock oscillator. */
  299. SYSTEM.MOFCR.BIT.MOSEL = BSP_CFG_MAIN_CLOCK_SOURCE;
  300. /* Use HOCO if HOCO is chosen or if PLL is chosen with HOCO as source. */
  301. #if (BSP_CFG_CLOCK_SOURCE == 1) || ((BSP_CFG_CLOCK_SOURCE == 4) && (BSP_CFG_PLL_SRC == 1))
  302. /* HOCO is chosen. Start it operating if it is not already operating. */
  303. if (1 == SYSTEM.HOCOCR.BIT.HCSTP)
  304. {
  305. /* Turn on power to HOCO. */
  306. SYSTEM.HOCOPCR.BYTE = 0x00;
  307. /* Stop HOCO. */
  308. SYSTEM.HOCOCR.BYTE = 0x01;
  309. /* WAIT_LOOP */
  310. while(1 == SYSTEM.OSCOVFSR.BIT.HCOVF)
  311. {
  312. /* The delay period needed is to make sure that the HOCO has stopped. */
  313. }
  314. /* Set HOCO frequency. */
  315. #if (BSP_CFG_HOCO_FREQUENCY == 0)
  316. SYSTEM.HOCOCR2.BYTE = 0x00; //16MHz
  317. #elif (BSP_CFG_HOCO_FREQUENCY == 1)
  318. SYSTEM.HOCOCR2.BYTE = 0x01; //18MHz
  319. #elif (BSP_CFG_HOCO_FREQUENCY == 2)
  320. SYSTEM.HOCOCR2.BYTE = 0x02; //20MHz
  321. #else
  322. #error "Error! Invalid setting for BSP_CFG_HOCO_FREQUENCY in r_bsp_config.h"
  323. #endif
  324. /* HOCO is chosen. Start it operating. */
  325. SYSTEM.HOCOCR.BYTE = 0x00;
  326. if(0x00 == SYSTEM.HOCOCR.BYTE)
  327. {
  328. /* Dummy read and compare. cf."5. I/O Registers", "(2) Notes on writing to I/O registers" in User's manual.
  329. This is done to ensure that the register has been written before the next register access. The RX has a
  330. pipeline architecture so the next instruction could be executed before the previous write had finished. */
  331. R_NOP();
  332. }
  333. }
  334. /* WAIT_LOOP */
  335. while(0 == SYSTEM.OSCOVFSR.BIT.HCOVF)
  336. {
  337. /* The delay period needed is to make sure that the HOCO has stabilized. */
  338. }
  339. #else
  340. /* If HOCO is already operating, it doesn't stop. */
  341. if (1 == SYSTEM.HOCOCR.BIT.HCSTP)
  342. {
  343. /* Turn off power to HOCO. */
  344. SYSTEM.HOCOPCR.BYTE = 0x01;
  345. }
  346. else
  347. {
  348. /* WAIT_LOOP */
  349. while(0 == SYSTEM.OSCOVFSR.BIT.HCOVF)
  350. {
  351. /* The delay period needed is to make sure that the HOCO has stabilized. */
  352. }
  353. }
  354. #endif
  355. /* Use Main clock if Main clock is chosen or if PLL is chosen with Main clock as source. */
  356. #if (BSP_CFG_CLOCK_SOURCE == 2) || ((BSP_CFG_CLOCK_SOURCE == 4) && (BSP_CFG_PLL_SRC == 0))
  357. /* Main clock oscillator is chosen. Start it operating. */
  358. /* If the main oscillator is >10MHz then the main clock oscillator forced oscillation control register (MOFCR) must
  359. be changed. */
  360. if (BSP_CFG_XTAL_HZ > 20000000)
  361. {
  362. /* 20 - 24MHz. */
  363. SYSTEM.MOFCR.BIT.MODRV2 = 0;
  364. }
  365. else if (BSP_CFG_XTAL_HZ > 16000000)
  366. {
  367. /* 16 - 20MHz. */
  368. SYSTEM.MOFCR.BIT.MODRV2 = 1;
  369. }
  370. else if (BSP_CFG_XTAL_HZ > 8000000)
  371. {
  372. /* 8 - 16MHz. */
  373. SYSTEM.MOFCR.BIT.MODRV2 = 2;
  374. }
  375. else
  376. {
  377. /* 8MHz. */
  378. SYSTEM.MOFCR.BIT.MODRV2 = 3;
  379. }
  380. /* Set the oscillation stabilization wait time of the main clock oscillator. */
  381. #if (BSP_CFG_MAIN_CLOCK_SOURCE == 0) /* Resonator */
  382. SYSTEM.MOSCWTCR.BYTE = BSP_CFG_MOSC_WAIT_TIME;
  383. #elif (BSP_CFG_MAIN_CLOCK_SOURCE == 1) /* External oscillator input */
  384. SYSTEM.MOSCWTCR.BYTE = 0x00;
  385. #else
  386. #error "Error! Invalid setting for BSP_CFG_MAIN_CLOCK_SOURCE in r_bsp_config.h"
  387. #endif
  388. /* Set the main clock to operating. */
  389. SYSTEM.MOSCCR.BYTE = 0x00;
  390. if(0x00 == SYSTEM.MOSCCR.BYTE)
  391. {
  392. /* Dummy read and compare. cf."5. I/O Registers", "(2) Notes on writing to I/O registers" in User's manual.
  393. This is done to ensure that the register has been written before the next register access. The RX has a
  394. pipeline architecture so the next instruction could be executed before the previous write had finished. */
  395. R_NOP();
  396. }
  397. /* WAIT_LOOP */
  398. while(0 == SYSTEM.OSCOVFSR.BIT.MOOVF)
  399. {
  400. /* The delay period needed is to make sure that the Main clock has stabilized. */
  401. }
  402. #else
  403. /* Set the main clock to stopped. */
  404. SYSTEM.MOSCCR.BYTE = 0x01;
  405. /* Dummy read and compare. cf."5. I/O Registers", "(2) Notes on writing to I/O registers" in User's manual. */
  406. if(0x01 == SYSTEM.MOSCCR.BYTE)
  407. {
  408. /* Dummy read and compare. cf."5. I/O Registers", "(2) Notes on writing to I/O registers" in User's manual.
  409. This is done to ensure that the register has been written before the next register access. The RX has a
  410. pipeline architecture so the next instruction could be executed before the previous write had finished. */
  411. R_NOP();
  412. }
  413. #endif
  414. /* Sub-clock setting. */
  415. /* Cold start setting */
  416. if (0 == SYSTEM.RSTSR1.BIT.CWSF)
  417. {
  418. /* Stop the sub-clock oscillator */
  419. /* RCR4 - RTC Control Register 4
  420. b7:b1 Reserved - The write value should be 0.
  421. b0 RCKSEL - Count Source Select - Sub-clock oscillator is selected. */
  422. RTC.RCR4.BIT.RCKSEL = 0;
  423. /* dummy read four times */
  424. /* WAIT_LOOP */
  425. for (i = 0; i < 4; i++)
  426. {
  427. dummy = RTC.RCR4.BYTE;
  428. R_INTERNAL_NOT_USED(&dummy); /* The '&' is for volatile declaration of the "dummy". */
  429. }
  430. if (0 != RTC.RCR4.BIT.RCKSEL)
  431. {
  432. /* Confirm that the written */
  433. R_NOP();
  434. }
  435. /* RCR3 - RTC Control Register 3
  436. b7:b4 Reserved - The write value should be 0.
  437. b3:b1 RTCDV - Sub-clock oscillator Drive Ability Control.
  438. b0 RTCEN - Sub-clock oscillator is stopped. */
  439. RTC.RCR3.BIT.RTCEN = 0;
  440. /* dummy read four times */
  441. /* WAIT_LOOP */
  442. for (i = 0; i < 4; i++)
  443. {
  444. dummy = RTC.RCR3.BYTE;
  445. R_INTERNAL_NOT_USED(&dummy); /* The '&' is for volatile declaration of the "dummy". */
  446. }
  447. if (0 != RTC.RCR3.BIT.RTCEN)
  448. {
  449. /* Confirm that the written */
  450. R_NOP();
  451. }
  452. /* SOSCCR - Sub-Clock Oscillator Control Register
  453. b7:b1 Reserved - The write value should be 0.
  454. b0 SOSTP - Sub-clock oscillator Stop - Sub-clock oscillator is stopped. */
  455. SYSTEM.SOSCCR.BYTE = 0x01;
  456. if (0x01 != SYSTEM.SOSCCR.BYTE)
  457. {
  458. /* Dummy read and compare. cf."5. I/O Registers", "(2) Notes on writing to I/O registers" in User's manual.
  459. This is done to ensure that the register has been written before the next register access. The RX has a
  460. pipeline architecture so the next instruction could be executed before the previous write had finished. */
  461. R_NOP();
  462. }
  463. /* WAIT_LOOP */
  464. while (0 != SYSTEM.OSCOVFSR.BIT.SOOVF)
  465. {
  466. /* The delay period needed is to make sure that the sub-clock has stopped. */
  467. }
  468. #if (BSP_CFG_CLOCK_SOURCE == 3) || (BSP_CFG_RTC_ENABLE == 1)
  469. /* Set the drive capacity of the sub-clock oscillator */
  470. #if (BSP_CFG_SOSC_DRV_CAP == 0) /* Standard CL */
  471. tmp = 0x06;
  472. #elif (BSP_CFG_SOSC_DRV_CAP == 1) /* Low CL */
  473. tmp = 0x01;
  474. #else
  475. #error "Error! Invalid setting for BSP_CFG_SOSC_DRV_CAP in r_bsp_config.h"
  476. #endif
  477. RTC.RCR3.BIT.RTCDV = tmp;
  478. /* dummy read four times */
  479. /* WAIT_LOOP */
  480. for (i = 0; i < 4; i++)
  481. {
  482. dummy = RTC.RCR3.BYTE;
  483. R_INTERNAL_NOT_USED(&dummy); /* The '&' is for volatile declaration of the "dummy". */
  484. }
  485. if (tmp != RTC.RCR3.BIT.RTCDV)
  486. {
  487. /* Confirm that the written */
  488. R_NOP();
  489. }
  490. /* Set wait time until the sub-clock oscillator stabilizes */
  491. /* SOSCWTCR - Sub-Clock Oscillator Wait Control Register
  492. b7:b5 Reserved - The write value should be 0.
  493. b4:b0 SSTS - Sub-Clock Oscillator Waiting Time - Waiting time is 2.044ms(the maximum value of fLOCO). */
  494. SYSTEM.SOSCWTCR.BYTE = BSP_CFG_SOSC_WAIT_TIME;
  495. /* Operate the Sub-clock oscillator */
  496. SYSTEM.SOSCCR.BYTE = 0x00;
  497. if (0x00 != SYSTEM.SOSCCR.BYTE)
  498. {
  499. /* Dummy read and compare. cf."5. I/O Registers", "(2) Notes on writing to I/O registers" in User's manual.
  500. This is done to ensure that the register has been written before the next register access. The RX has a
  501. pipeline architecture so the next instruction could be executed before the previous write had finished. */
  502. R_NOP();
  503. }
  504. /* WAIT_LOOP */
  505. while (1 != SYSTEM.OSCOVFSR.BIT.SOOVF)
  506. {
  507. /* The delay period needed is to make sure that the sub-clock has stabilized. */
  508. }
  509. #endif
  510. #if (BSP_CFG_RTC_ENABLE == 1)
  511. /* ---- Set wait time until the sub-clock oscillator stabilizes ---- */
  512. SYSTEM.SOSCWTCR.BYTE = 0x00;
  513. /* ---- Operate the sub-clock oscillator ---- */
  514. RTC.RCR3.BIT.RTCEN = 1;
  515. /* dummy read four times */
  516. /* WAIT_LOOP */
  517. for (i = 0; i < 4; i++)
  518. {
  519. dummy = RTC.RCR3.BIT.RTCEN;
  520. R_INTERNAL_NOT_USED(&dummy); /* The '&' is for volatile declaration of the "dummy". */
  521. }
  522. if (1 != RTC.RCR3.BIT.RTCEN)
  523. {
  524. /* Confirm that the written value can be read correctly. */
  525. R_NOP();
  526. }
  527. #endif
  528. #if (BSP_CFG_CLOCK_SOURCE == 3) || (BSP_CFG_RTC_ENABLE == 1)
  529. /* Wait for six the sub-clock cycles */
  530. /* 6 count of sub-clock : (1000000/32768)*6=183.10546875us
  531. In the case of LOCO frequency is 264kHz : 183.10546875/(1000000/264000)=48.33984375cycle
  532. (48.33984375+2)*(1000000/240000)=209.7493489583333us ("+2" is overhead cycle) */
  533. R_BSP_SoftwareDelay((uint32_t)210, BSP_DELAY_MICROSECS);
  534. #endif
  535. #if (BSP_CFG_CLOCK_SOURCE == 3) && (BSP_CFG_RTC_ENABLE == 0)
  536. /* Stop prescaler and counter */
  537. /* RCR2 - RTC Control Register 2
  538. b7 CNTMD - Count Mode Select - The calendar count mode.
  539. b6 HR24 - Hours Mode - The RTC operates in 24-hour mode.
  540. b5 AADJP - Automatic Adjustment Period Select - The RADJ.ADJ[5:0] setting value is adjusted from
  541. the count value of the prescaler every 10 seconds.
  542. b4 AADJE - Automatic Adjustment Enable - Automatic adjustment is enabled.
  543. b3 RTCOE - RTCOUT Output Enable - RTCOUT output enabled.
  544. b2 ADJ30 - 30-Second Adjustment - 30-second adjustment is executed.
  545. b1 RESET - RTC Software Reset - The prescaler and the target registers for RTC software reset are initialized.
  546. b0 START - start - Prescaler is stopped. */
  547. RTC.RCR2.BYTE &= 0x7E;
  548. /* WAIT_LOOP */
  549. while (0 != RTC.RCR2.BIT.START)
  550. {
  551. /* Confirm that the written value can be read correctly. */
  552. }
  553. /* RTC Software Reset */
  554. RTC.RCR2.BIT.RESET = 1;
  555. /* WAIT_LOOP */
  556. while (0 != RTC.RCR2.BIT.RESET)
  557. {
  558. /* Confirm that the written value can be read correctly. */
  559. }
  560. /* An alarm interrupt request is disabled */
  561. /* RCR1 - RTC Control Register 1
  562. b7:b4 PES - Periodic Interrupt Select - These bits specify the period for the periodic interrupt.
  563. b3 RTCOS - RTCOUT Output Select - RTCOUT outputs 1 Hz.
  564. b2 PIE - Periodic Interrupt Enable - A periodic interrupt request is disabled.
  565. b1 CIE - Carry Interrupt Enable - A carry interrupt request is disabled.
  566. b0 AIE - Alarm Interrupt Enable - An alarm interrupt request is disabled. */
  567. RTC.RCR1.BYTE &= 0xF8;
  568. /* Wait for one the sub-clock cycles */
  569. /* 1 count of sub-clock : (1000000/32768)=30.517578125us
  570. In the case of LOCO frequency is 264kHz : 30.517578125/(1000000/264000)=8.056640625cycle
  571. (8.056640625+2)*(1000000/240000)=41.902669270833us ("+2" is overhead cycle) */
  572. R_BSP_SoftwareDelay((uint32_t)42, BSP_DELAY_MICROSECS);
  573. if (0x00 != (RTC.RCR1.BYTE & 0x07))
  574. {
  575. /* Confirm that the written value can be read correctly. */
  576. R_NOP();
  577. }
  578. #endif
  579. }
  580. /* Warm start setting */
  581. else
  582. {
  583. #if (BSP_CFG_CLOCK_SOURCE == 3) || ((BSP_CFG_CLOCK_SOURCE != 3) && (BSP_CFG_RTC_ENABLE == 0))
  584. /* SOSCCR - Sub-Clock Oscillator Control Register
  585. b7:b1 Reserved - The write value should be 0.
  586. b0 SOSTP - Sub-clock oscillator Stop - Sub-clock oscillator is stopped. */
  587. SYSTEM.SOSCCR.BYTE = 0x01;
  588. if (0x01 != SYSTEM.SOSCCR.BYTE)
  589. {
  590. /* Dummy read and compare. cf."5. I/O Registers", "(2) Notes on writing to I/O registers" in User's manual.
  591. This is done to ensure that the register has been written before the next register access. The RX has a
  592. pipeline architecture so the next instruction could be executed before the previous write had finished. */
  593. R_NOP();
  594. }
  595. /* WAIT_LOOP */
  596. while (0 != SYSTEM.OSCOVFSR.BIT.SOOVF)
  597. {
  598. /* Confirm that the Sub clock stopped. */
  599. }
  600. #endif
  601. #if (BSP_CFG_CLOCK_SOURCE == 3)
  602. /* Set wait time until the sub-clock oscillator stabilizes */
  603. /* SOSCWTCR - Sub-Clock Oscillator Wait Control Register
  604. b7:b5 Reserved - The write value should be 0.
  605. b4:b0 SSTS - Sub-Clock Oscillator Waiting Time - Waiting time is 2.044ms(the maximum value of fLOCO). */
  606. SYSTEM.SOSCWTCR.BYTE = BSP_CFG_SOSC_WAIT_TIME;
  607. /* Operate the Sub-clock oscillator */
  608. SYSTEM.SOSCCR.BYTE = 0x00;
  609. if (0x00 != SYSTEM.SOSCCR.BYTE)
  610. {
  611. /* Dummy read and compare. cf."5. I/O Registers", "(2) Notes on writing to I/O registers" in User's manual.
  612. This is done to ensure that the register has been written before the next register access. The RX has a
  613. pipeline architecture so the next instruction could be executed before the previous write had finished. */
  614. R_NOP();
  615. }
  616. #endif
  617. #if (BSP_CFG_CLOCK_SOURCE == 3) || (BSP_CFG_RTC_ENABLE == 1)
  618. /* WAIT_LOOP */
  619. while (1 != SYSTEM.OSCOVFSR.BIT.SOOVF)
  620. {
  621. /* The delay period needed is to make sure that the sub-clock has stabilized. */
  622. }
  623. #endif
  624. #if (BSP_CFG_RTC_ENABLE == 1)
  625. /* ---- Set wait time until the sub-clock oscillator stabilizes ---- */
  626. SYSTEM.SOSCWTCR.BYTE = 0x00;
  627. #endif
  628. }
  629. #if (BSP_CFG_CLOCK_SOURCE == 4)
  630. /* Set PLL Input Divisor. */
  631. SYSTEM.PLLCR.BIT.PLIDIV = BSP_CFG_PLL_DIV - 1;
  632. #if (BSP_CFG_PLL_SRC == 0)
  633. /* Clear PLL clock source if PLL clock source is Main clock. */
  634. SYSTEM.PLLCR.BIT.PLLSRCSEL = 0;
  635. #else
  636. /* Set PLL clock source if PLL clock source is HOCO clock. */
  637. SYSTEM.PLLCR.BIT.PLLSRCSEL = 1;
  638. #endif
  639. /* Set PLL Multiplier. */
  640. SYSTEM.PLLCR.BIT.STC = ((uint8_t)((float)BSP_CFG_PLL_MUL * 2.0)) - 1;
  641. /* Set the PLL to operating. */
  642. SYSTEM.PLLCR2.BYTE = 0x00;
  643. /* WAIT_LOOP */
  644. while(0 == SYSTEM.OSCOVFSR.BIT.PLOVF)
  645. {
  646. /* The delay period needed is to make sure that the PLL has stabilized. */
  647. }
  648. #else
  649. /* Set the PLL to stopped. */
  650. SYSTEM.PLLCR2.BYTE = 0x01;
  651. #endif
  652. /* LOCO is saved for last since it is what is running by default out of reset. This means you do not want to turn
  653. it off until another clock has been enabled and is ready to use. */
  654. #if (BSP_CFG_CLOCK_SOURCE == 0)
  655. /* LOCO is chosen. This is the default out of reset. */
  656. SYSTEM.LOCOCR.BYTE = 0x00;
  657. #else
  658. /* LOCO is not chosen but it cannot be turned off yet since it is still being used. */
  659. #endif
  660. /* Make sure a valid clock was chosen. */
  661. #if (BSP_CFG_CLOCK_SOURCE > 4) || (BSP_CFG_CLOCK_SOURCE < 0)
  662. #error "ERROR - Valid clock source must be chosen in r_bsp_config.h using BSP_CFG_CLOCK_SOURCE macro."
  663. #endif
  664. /* RX65N has a ROMWT register which controls the cycle waiting for access to code flash memory.
  665. It is set as zero coming out of reset.
  666. When setting ICLK to [50 MHz < ICLK <= 100 MHz], set the ROMWT.ROMWT[1:0] bits to 01b.
  667. When setting ICLK to [100 MHz < ICLK <= 120 MHz], set the ROMWT.ROMWT[1:0] bits to 10b. */
  668. if (BSP_ICLK_HZ > ROMWT_FREQ_THRESHOLD_02)
  669. {
  670. SYSTEM.ROMWT.BYTE = 0x02;
  671. /* Dummy read and compare. cf."5. I/O Registers", "(2) Notes on writing to I/O registers" in User's manual. */
  672. if(0x02 == SYSTEM.ROMWT.BYTE)
  673. {
  674. /* Dummy read and compare. cf."5. I/O Registers", "(2) Notes on writing to I/O registers" in User's manual.
  675. This is done to ensure that the register has been written before the next register access. The RX has a
  676. pipeline architecture so the next instruction could be executed before the previous write had finished.
  677. */
  678. R_NOP();
  679. }
  680. }
  681. else if (BSP_ICLK_HZ > ROMWT_FREQ_THRESHOLD_01)
  682. {
  683. SYSTEM.ROMWT.BYTE = 0x01;
  684. /* Dummy read and compare. cf."5. I/O Registers", "(2) Notes on writing to I/O registers" in User's manual. */
  685. if(0x01 == SYSTEM.ROMWT.BYTE)
  686. {
  687. /* Dummy read and compare. cf."5. I/O Registers", "(2) Notes on writing to I/O registers" in User's manual.
  688. This is done to ensure that the register has been written before the next register access. The RX has a
  689. pipeline architecture so the next instruction could be executed before the previous write had finished.
  690. */
  691. R_NOP();
  692. }
  693. }
  694. else
  695. {
  696. /* Do nothing. */
  697. }
  698. }
  699. #endif /* BSP_CFG_STARTUP_DISABLE == 0 */