mxc_sys.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  1. /**
  2. * @file mxc_sys.c
  3. * @brief System level setup help
  4. */
  5. /*******************************************************************************
  6. * Copyright (C) 2015 Maxim Integrated Products, Inc., All Rights Reserved.
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a
  9. * copy of this software and associated documentation files (the "Software"),
  10. * to deal in the Software without restriction, including without limitation
  11. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  12. * and/or sell copies of the Software, and to permit persons to whom the
  13. * Software is furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included
  16. * in all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  19. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  21. * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
  22. * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  23. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  24. * OTHER DEALINGS IN THE SOFTWARE.
  25. *
  26. * Except as contained in this notice, the name of Maxim Integrated
  27. * Products, Inc. shall not be used except as stated in the Maxim Integrated
  28. * Products, Inc. Branding Policy.
  29. *
  30. * The mere transfer of this software does not imply any licenses
  31. * of trade secrets, proprietary technology, copyrights, patents,
  32. * trademarks, maskwork rights, or any other form of intellectual
  33. * property whatsoever. Maxim Integrated Products, Inc. retains all
  34. * ownership rights.
  35. *
  36. * $Date: 2020-01-17 08:38:51 -0600 (Fri, 17 Jan 2020) $
  37. * $Revision: 50772 $
  38. *
  39. ******************************************************************************/
  40. #include <stddef.h>
  41. #include "mxc_config.h"
  42. #include "mxc_assert.h"
  43. #include "mxc_sys.h"
  44. #include "gpio.h"
  45. #include "mxc_pins.h"
  46. #include "gcr_regs.h"
  47. #include "tmr_regs.h"
  48. #include "pwrseq_regs.h"
  49. #include "spi17y_regs.h"
  50. #include "spimss_regs.h"
  51. #include "mxc_delay.h"
  52. #include "rtc.h"
  53. /**
  54. * @ingroup MXC_sys
  55. * @{
  56. */
  57. /***** Definitions *****/
  58. #define SYS_CLOCK_TIMEOUT MXC_DELAY_MSEC(1)
  59. #define SYS_RTC_CLK 32768UL
  60. /***** Functions ******/
  61. static int SYS_Clock_Timeout(uint32_t ready)
  62. {
  63. // Start timeout, wait for ready
  64. mxc_delay_start(SYS_CLOCK_TIMEOUT);
  65. do {
  66. if (MXC_GCR->clkcn & ready) {
  67. mxc_delay_stop();
  68. return E_NO_ERROR;
  69. }
  70. } while (mxc_delay_check() == E_BUSY);
  71. return E_TIME_OUT;
  72. }
  73. /* ************************************************************************ */
  74. int SYS_Clock_Select(sys_system_clock_t clock, mxc_tmr_regs_t* tmr)
  75. {
  76. uint32_t current_clock,ovr, div;
  77. // Save the current system clock
  78. current_clock = MXC_GCR->clkcn & MXC_F_GCR_CLKCN_CLKSEL;
  79. // Set FWS higher than what the minimum for the fastest clock is
  80. MXC_GCR->memckcn = (MXC_GCR->memckcn & ~(MXC_F_GCR_MEMCKCN_FWS)) | (0x5UL << MXC_F_GCR_MEMCKCN_FWS_POS);
  81. switch(clock) {
  82. case SYS_CLOCK_NANORING:
  83. // Set NANORING clock as System Clock
  84. MXC_SETFIELD(MXC_GCR->clkcn, MXC_F_GCR_CLKCN_CLKSEL, MXC_S_GCR_CLKCN_CLKSEL_NANORING);
  85. break;
  86. case SYS_CLOCK_HFXIN:
  87. // Enable 32k Oscillator
  88. MXC_GCR->clkcn |=MXC_F_GCR_CLKCN_X32K_EN;
  89. // Check if 32k clock is ready
  90. if (SYS_Clock_Timeout(MXC_F_GCR_CLKCN_X32K_RDY) != E_NO_ERROR) {
  91. return E_TIME_OUT;
  92. }
  93. MXC_RTC->ctrl |= MXC_F_RTC_CTRL_WE; // Allow writing to registers
  94. // Set 32k clock as System Clock
  95. MXC_SETFIELD(MXC_GCR->clkcn, MXC_F_GCR_CLKCN_CLKSEL, MXC_S_GCR_CLKCN_CLKSEL_HFXIN);
  96. break;
  97. case SYS_CLOCK_HFXIN_DIGITAL:
  98. // Enable 32k Oscillator
  99. MXC_GCR->clkcn |=MXC_F_GCR_CLKCN_X32K_EN;
  100. // Check if 32k clock is ready
  101. if (SYS_Clock_Timeout(MXC_F_GCR_CLKCN_X32K_RDY) != E_NO_ERROR) {
  102. return E_TIME_OUT;
  103. }
  104. MXC_RTC->ctrl |= MXC_F_RTC_CTRL_WE; // Allow writing to registers
  105. MXC_RTC->oscctrl |= MXC_F_RTC_OSCCTRL_BYPASS; // To allow square wave driven on 32KIN
  106. // Set 32k clock as System Clock
  107. MXC_SETFIELD(MXC_GCR->clkcn, MXC_F_GCR_CLKCN_CLKSEL, MXC_S_GCR_CLKCN_CLKSEL_HFXIN);
  108. break;
  109. case SYS_CLOCK_HIRC:
  110. // Enable 96MHz Clock
  111. MXC_GCR->clkcn |=MXC_F_GCR_CLKCN_HIRC_EN;
  112. // Check if 96MHz clock is ready
  113. if (SYS_Clock_Timeout(MXC_F_GCR_CLKCN_HIRC_RDY) != E_NO_ERROR) {
  114. return E_TIME_OUT;
  115. }
  116. // Set 96MHz clock as System Clock
  117. MXC_SETFIELD(MXC_GCR->clkcn, MXC_F_GCR_CLKCN_CLKSEL, MXC_S_GCR_CLKCN_CLKSEL_HIRC);
  118. break;
  119. default:
  120. return E_BAD_PARAM;
  121. }
  122. // Wait for system clock to be ready
  123. if (SYS_Clock_Timeout(MXC_F_GCR_CLKCN_CKRDY) != E_NO_ERROR) {
  124. // Restore the old system clock if timeout
  125. MXC_SETFIELD(MXC_GCR->clkcn, MXC_F_GCR_CLKCN_CLKSEL, current_clock);
  126. return E_TIME_OUT;
  127. }
  128. // Disable other clocks
  129. switch(clock) {
  130. case SYS_CLOCK_NANORING:
  131. MXC_GCR->clkcn &= ~(MXC_F_GCR_CLKCN_HIRC_EN);
  132. break;
  133. case SYS_CLOCK_HFXIN:
  134. MXC_GCR->clkcn &= ~(MXC_F_GCR_CLKCN_HIRC_EN);
  135. break;
  136. case SYS_CLOCK_HFXIN_DIGITAL:
  137. MXC_GCR->clkcn &= ~(MXC_F_GCR_CLKCN_HIRC_EN);
  138. break;
  139. case SYS_CLOCK_HIRC:
  140. //Don't disable 32KHz clock
  141. break;
  142. }
  143. // Update the system core clock
  144. SystemCoreClockUpdate();
  145. // Get the clock divider
  146. div = (MXC_GCR->clkcn & MXC_F_GCR_CLKCN_PSC) >> MXC_F_GCR_CLKCN_PSC_POS;
  147. //get ovr setting
  148. ovr = (MXC_PWRSEQ->lp_ctrl & MXC_F_PWRSEQ_LP_CTRL_OVR);
  149. //Set flash wait settings
  150. if(ovr == MXC_S_PWRSEQ_LP_CTRL_OVR_0_9V){
  151. if(div == 0){
  152. MXC_GCR->memckcn = (MXC_GCR->memckcn & ~(MXC_F_GCR_MEMCKCN_FWS)) | (0x2UL << MXC_F_GCR_MEMCKCN_FWS_POS);
  153. } else{
  154. MXC_GCR->memckcn = (MXC_GCR->memckcn & ~(MXC_F_GCR_MEMCKCN_FWS)) | (0x1UL << MXC_F_GCR_MEMCKCN_FWS_POS);
  155. }
  156. } else if( ovr == MXC_S_PWRSEQ_LP_CTRL_OVR_1_0V){
  157. if(div == 0){
  158. MXC_GCR->memckcn = (MXC_GCR->memckcn & ~(MXC_F_GCR_MEMCKCN_FWS)) | (0x2UL << MXC_F_GCR_MEMCKCN_FWS_POS);
  159. } else{
  160. MXC_GCR->memckcn = (MXC_GCR->memckcn & ~(MXC_F_GCR_MEMCKCN_FWS)) | (0x1UL << MXC_F_GCR_MEMCKCN_FWS_POS);
  161. }
  162. } else {
  163. if(div == 0){
  164. MXC_GCR->memckcn = (MXC_GCR->memckcn & ~(MXC_F_GCR_MEMCKCN_FWS)) | (0x4UL << MXC_F_GCR_MEMCKCN_FWS_POS);
  165. } else if(div == 1){
  166. MXC_GCR->memckcn = (MXC_GCR->memckcn & ~(MXC_F_GCR_MEMCKCN_FWS)) | (0x2UL << MXC_F_GCR_MEMCKCN_FWS_POS);
  167. } else{
  168. MXC_GCR->memckcn = (MXC_GCR->memckcn & ~(MXC_F_GCR_MEMCKCN_FWS)) | (0x1UL << MXC_F_GCR_MEMCKCN_FWS_POS);
  169. }
  170. }
  171. return E_NO_ERROR;
  172. }
  173. /* ************************************************************************ */
  174. int SYS_ClockEnable_X32K(sys_cfg_rtc_t *sys_cfg)
  175. {
  176. // Enable 32k Oscillator
  177. MXC_GCR->clkcn |=MXC_F_GCR_CLKCN_X32K_EN;
  178. return E_NO_ERROR;
  179. }
  180. /* ************************************************************************ */
  181. int SYS_ClockDisable_X32K()
  182. {
  183. // Disable 32k Oscillator
  184. MXC_GCR->clkcn &= (~MXC_F_GCR_CLKCN_X32K_EN);
  185. return E_NO_ERROR;
  186. }
  187. /* ************************************************************************ */
  188. int SYS_UART_Init(mxc_uart_regs_t *uart, const sys_cfg_uart_t* sys_cfg)
  189. {
  190. // Configure GPIO for UART
  191. if (uart == MXC_UART0) {
  192. SYS_ClockEnable(SYS_PERIPH_CLOCK_UART0);
  193. if(sys_cfg->map == MAP_A){
  194. GPIO_Config(&gpio_cfg_uart0a);
  195. }
  196. else{
  197. return E_BAD_PARAM;
  198. }
  199. if(sys_cfg->flow_flag == UART_FLOW_ENABLE){
  200. GPIO_Config(&gpio_cfg_uart0rtscts);
  201. }
  202. }
  203. if (uart == MXC_UART1) {
  204. SYS_ClockEnable(SYS_PERIPH_CLOCK_UART1);
  205. if(sys_cfg->map == MAP_A){
  206. GPIO_Config(&gpio_cfg_uart1a);
  207. }
  208. else if(sys_cfg->map == MAP_B){
  209. GPIO_Config(&gpio_cfg_uart1b);
  210. }
  211. else if(sys_cfg->map == MAP_C){
  212. GPIO_Config(&gpio_cfg_uart1c);
  213. }
  214. else{
  215. return E_BAD_PARAM;
  216. }
  217. if(sys_cfg->flow_flag == UART_FLOW_ENABLE){
  218. GPIO_Config(&gpio_cfg_uart1rtscts);
  219. }
  220. }
  221. return E_NO_ERROR;
  222. }
  223. /* ************************************************************************ */
  224. int SYS_UART_Shutdown(mxc_uart_regs_t *uart)
  225. {
  226. if (uart == MXC_UART0) {
  227. SYS_ClockDisable(SYS_PERIPH_CLOCK_UART0);
  228. }
  229. else if (uart == MXC_UART1) {
  230. SYS_ClockDisable(SYS_PERIPH_CLOCK_UART1);
  231. }
  232. return E_NO_ERROR;
  233. }
  234. /* ************************************************************************ */
  235. int SYS_I2C_Init(mxc_i2c_regs_t *i2c, const sys_cfg_i2c_t* sys_cfg)
  236. {
  237. // Configure GPIO for I2C
  238. if (i2c == MXC_I2C0) {
  239. SYS_ClockEnable(SYS_PERIPH_CLOCK_I2C0);
  240. GPIO_Config(&gpio_cfg_i2c0);
  241. } else if (i2c == MXC_I2C1) {
  242. SYS_ClockEnable(SYS_PERIPH_CLOCK_I2C1);
  243. GPIO_Config(&gpio_cfg_i2c1);
  244. } else {
  245. return E_NO_DEVICE;
  246. }
  247. return E_NO_ERROR;
  248. }
  249. /* ************************************************************************ */
  250. int SYS_I2C_Shutdown(mxc_i2c_regs_t *i2c)
  251. {
  252. if (i2c == MXC_I2C0) {
  253. gpio_cfg_t cfg = { gpio_cfg_i2c0.port, gpio_cfg_i2c0.mask, GPIO_FUNC_IN, GPIO_PAD_NONE };
  254. SYS_ClockDisable(SYS_PERIPH_CLOCK_I2C0);
  255. GPIO_Config(&cfg);
  256. } else if (i2c == MXC_I2C1) {
  257. gpio_cfg_t cfg = { gpio_cfg_i2c1.port, gpio_cfg_i2c1.mask, GPIO_FUNC_IN, GPIO_PAD_NONE };
  258. SYS_ClockDisable(SYS_PERIPH_CLOCK_I2C1);
  259. GPIO_Config(&cfg);
  260. } else {
  261. return E_NO_DEVICE;
  262. }
  263. // Clear registers
  264. i2c->ctrl = 0;
  265. return E_NO_ERROR;
  266. }
  267. /* ************************************************************************ */
  268. int SYS_DMA_Init(void)
  269. {
  270. SYS_ClockEnable(SYS_PERIPH_CLOCK_DMA);
  271. return E_NO_ERROR;
  272. }
  273. /* ************************************************************************ */
  274. int SYS_DMA_Shutdown(void)
  275. {
  276. SYS_ClockDisable(SYS_PERIPH_CLOCK_DMA);
  277. return E_NO_ERROR;
  278. }
  279. /* ************************************************************************ */
  280. unsigned SYS_I2C_GetFreq(mxc_i2c_regs_t *i2c)
  281. {
  282. return PeripheralClock;
  283. }
  284. /* ************************************************************************ */
  285. unsigned SYS_TMR_GetFreq(mxc_tmr_regs_t *tmr)
  286. {
  287. return PeripheralClock;
  288. }
  289. /* ************************************************************************ */
  290. void SYS_Reset0(sys_reset0_t reset)
  291. {
  292. MXC_GCR->rstr0 = reset;
  293. while(MXC_GCR->rstr0 != 0x0) {}
  294. }
  295. /* ************************************************************************ */
  296. void SYS_Reset1(sys_reset1_t reset)
  297. {
  298. MXC_GCR->rstr1 = reset;
  299. while(MXC_GCR->rstr0 != 0x0) {}
  300. }
  301. /* ************************************************************************ */
  302. void SYS_ClockDisable(sys_periph_clock_t clock)
  303. {
  304. /* The sys_periph_clock_t enum uses bit 27 (an unused bit in both perkcn registers)
  305. to determine which of the two perckcn registers to write to. */
  306. if (clock & (1<<27)) {
  307. clock &= ~(1<<27);
  308. MXC_GCR->perckcn1 |= clock;
  309. } else {
  310. MXC_GCR->perckcn0 |= clock;
  311. }
  312. }
  313. /* ************************************************************************ */
  314. void SYS_ClockEnable(sys_periph_clock_t clock)
  315. {
  316. /* The sys_periph_clock_t enum uses bit 27 (an unused bit in both perkcn registers)
  317. to determine which of the two perckcn registers to write to. */
  318. if (clock & (1<<27)) {
  319. clock &= ~(1<<27);
  320. MXC_GCR->perckcn1 &= ~(clock);
  321. } else {
  322. MXC_GCR->perckcn0 &= ~(clock);
  323. }
  324. }
  325. /* ************************************************************************ */
  326. #if defined (__ICCARM__)
  327. #pragma optimize=none /* Turn off optimizations for next function */
  328. #elif defined ( __CC_ARM )
  329. /* Keil MDK - Turn off optimizations after saving current state */
  330. #pragma push /* Save current optimization level */
  331. #pragma O0 /* Optimization level 0 */
  332. #elif ( __GNUC__ )
  333. /* GCC - Turn off optimizations after saving current state */
  334. #pragma GCC push_options /* Save current optimization level */
  335. #pragma GCC optimize ("O0") /* Set optimization level to none for this function */
  336. #endif
  337. void SYS_Flash_Operation(void)
  338. {
  339. volatile uint32_t *line_addr;
  340. volatile uint32_t __attribute__ ((unused)) line;
  341. // Clear the cache
  342. MXC_ICC->cache_ctrl ^= MXC_F_ICC_CACHE_CTRL_CACHE_EN;
  343. MXC_ICC->cache_ctrl ^= MXC_F_ICC_CACHE_CTRL_CACHE_EN;
  344. // Clear the line fill buffer
  345. line_addr = (uint32_t*)(MXC_FLASH_MEM_BASE);
  346. line = *line_addr;
  347. line_addr = (uint32_t*)(MXC_FLASH_MEM_BASE + MXC_FLASH_PAGE_SIZE);
  348. line = *line_addr;
  349. }
  350. /* Set optimizations to the previous level. For IAR, the optimize none applies
  351. only to the next function. Keil MDK and GNUC need state restored. */
  352. #if defined ( __CC_ARM )
  353. #pragma pop /* Restore Kiel MDK optimizations to saved level */
  354. #elif defined ( __GNUC__ )
  355. #pragma GCC pop_options /* Restore GCC optimization level */
  356. #endif
  357. /* ************************************************************************ */
  358. int SYS_TMR_Init(mxc_tmr_regs_t *tmr, const sys_cfg_tmr_t* sys_cfg)
  359. {
  360. if(sys_cfg) {
  361. if(sys_cfg->out_en) {
  362. if (tmr == MXC_TMR0) {
  363. GPIO_Config(&gpio_cfg_tmr0);
  364. }
  365. }
  366. }
  367. if (tmr == MXC_TMR0) {
  368. SYS_ClockEnable(SYS_PERIPH_CLOCK_T0);
  369. }
  370. else if (tmr == MXC_TMR1) {
  371. SYS_ClockEnable(SYS_PERIPH_CLOCK_T1);
  372. }
  373. else if (tmr == MXC_TMR2) {
  374. SYS_ClockEnable(SYS_PERIPH_CLOCK_T2);
  375. }
  376. return E_NO_ERROR;
  377. }
  378. /* ************************************************************************ */
  379. int SYS_FLC_Init(const sys_cfg_flc_t* sys_cfg)
  380. {
  381. return E_NO_ERROR;
  382. }
  383. /* ************************************************************************ */
  384. int SYS_FLC_Shutdown(void)
  385. {
  386. return E_NO_ERROR;
  387. }
  388. /* ************************************************************************ */
  389. int SYS_SPI17Y_Init(mxc_spi17y_regs_t *spi, const sys_cfg_spi17y_t* sys_cfg)
  390. {
  391. // Configure GPIO for spi17y
  392. if (spi == MXC_SPI17Y) {
  393. SYS_ClockEnable(SYS_PERIPH_CLOCK_SPI17Y);
  394. if(sys_cfg->map == MAP_A){
  395. GPIO_Config(&gpio_cfg_spi17y);
  396. MXC_GPIO0->ds |= 0x0003BF0;
  397. }else{
  398. return E_BAD_PARAM;
  399. }
  400. } else {
  401. return E_NO_DEVICE;
  402. }
  403. return E_NO_ERROR;
  404. }
  405. /* ************************************************************************ */
  406. int SYS_SPI17Y_Shutdown(mxc_spi17y_regs_t *spi)
  407. {
  408. if (spi == MXC_SPI17Y) {
  409. SYS_ClockDisable(SYS_PERIPH_CLOCK_SPI17Y);
  410. }
  411. return E_NO_ERROR;
  412. }
  413. /* ************************************************************************ */
  414. int SYS_SPIMSS_Init(mxc_spimss_regs_t *spi, const sys_cfg_spimss_t* sys_cfg)
  415. {
  416. // Configure GPIO for spimss
  417. if (spi == MXC_SPIMSS) {
  418. SYS_ClockEnable(SYS_PERIPH_CLOCK_SPIMSS);
  419. if(sys_cfg->map == MAP_A){
  420. GPIO_Config(&gpio_cfg_spimss1a); // SPI1A chosen
  421. }else if(sys_cfg->map == MAP_B){
  422. GPIO_Config(&gpio_cfg_spimss1b); // SPI1B chosen
  423. }else{
  424. return E_BAD_PARAM;
  425. }
  426. } else {
  427. return E_NO_DEVICE;
  428. }
  429. return E_NO_ERROR;
  430. }
  431. /* ************************************************************************ */
  432. int SYS_SPIMSS_Shutdown(mxc_spimss_regs_t *spi)
  433. {
  434. if(spi == MXC_SPIMSS) {
  435. SYS_ClockDisable(SYS_PERIPH_CLOCK_SPIMSS);
  436. }
  437. return E_NO_ERROR;
  438. }
  439. int SYS_TMR_Shutdown(mxc_tmr_regs_t *tmr)
  440. {
  441. return E_NO_ERROR;
  442. }
  443. /* ************************************************************************ */
  444. int SYS_I2S_Init(const sys_cfg_i2s_t* sys_cfg)
  445. {
  446. if(sys_cfg->map == MAP_A) {
  447. GPIO_Config(&gpio_cfg_i2s1a);
  448. }
  449. else if(sys_cfg->map == MAP_B) {
  450. GPIO_Config(&gpio_cfg_i2s1b);
  451. }
  452. else {
  453. return E_BAD_PARAM;
  454. }
  455. SYS_ClockEnable(SYS_PERIPH_CLOCK_SPIMSS);
  456. return E_NO_ERROR;
  457. }
  458. /* ************************************************************************ */
  459. int SYS_I2S_Shutdown(void)
  460. {
  461. SYS_ClockDisable(SYS_PERIPH_CLOCK_SPIMSS);
  462. return E_NO_ERROR;
  463. }
  464. /* ************************************************************************ */
  465. int SYS_I2S_GetFreq(mxc_spimss_regs_t *spimss)
  466. {
  467. return PeripheralClock;
  468. }
  469. /* ************************************************************************ */
  470. int SYS_RTC_SqwavInit(const sys_cfg_rtc_t* sys_cfg)
  471. {
  472. GPIO_Config(&gpio_cfg_rtc);
  473. return E_NO_ERROR;
  474. }
  475. /* ************************************************************************ */
  476. uint32_t SYS_SysTick_GetFreq(void)
  477. {
  478. // Determine is using internal (SystemCoreClock) or external (32768) clock
  479. if ( (SysTick->CTRL & SysTick_CTRL_CLKSOURCE_Msk) || !(SysTick->CTRL & SysTick_CTRL_ENABLE_Msk)) {
  480. return SystemCoreClock;
  481. } else {
  482. return SYS_RTC_CLK;
  483. }
  484. }
  485. /* ************************************************************************ */
  486. int SYS_SysTick_Config(uint32_t ticks, int clk_src, mxc_tmr_regs_t* tmr)
  487. {
  488. if(ticks == 0)
  489. return E_BAD_PARAM;
  490. // If SystemClock, call default CMSIS config and return
  491. if (clk_src) {
  492. return SysTick_Config(ticks);
  493. } else { /* External clock source requested
  494. enable RTC clock in run mode*/
  495. RTC_Init(MXC_RTC, 0, 0, NULL);
  496. RTC_EnableRTCE(MXC_RTC);
  497. // Disable SysTick Timer
  498. SysTick->CTRL = 0;
  499. // Check reload value for valid
  500. if ((ticks - 1) > SysTick_LOAD_RELOAD_Msk) {
  501. // Reload value impossible
  502. return E_BAD_PARAM;
  503. }
  504. // set reload register
  505. SysTick->LOAD = ticks - 1;
  506. // set Priority for Systick Interrupt
  507. NVIC_SetPriority(SysTick_IRQn, (1<<__NVIC_PRIO_BITS) - 1);
  508. // Load the SysTick Counter Value
  509. SysTick->VAL = 0;
  510. // Enable SysTick IRQ and SysTick Timer leaving clock source as external
  511. SysTick->CTRL = SysTick_CTRL_TICKINT_Msk | SysTick_CTRL_ENABLE_Msk;
  512. // Function successful
  513. return E_NO_ERROR;
  514. }
  515. }
  516. /* ************************************************************************ */
  517. void SYS_SysTick_Disable(void)
  518. {
  519. SysTick->CTRL = 0;
  520. }
  521. /* ************************************************************************ */
  522. int SYS_SysTick_Delay(uint32_t ticks)
  523. {
  524. uint32_t cur_ticks, num_full, num_remain, previous_ticks, num_subtract, i;
  525. uint32_t reload, value, ctrl; // save/restore variables
  526. if(ticks == 0)
  527. return E_BAD_PARAM;
  528. // If SysTick is not enabled we can take it for our delay
  529. if (!(SysTick->CTRL & SysTick_CTRL_ENABLE_Msk)) {
  530. // Save current state in case it's disabled but already configured, restore at return.
  531. reload = SysTick->LOAD;
  532. value = SysTick->VAL;
  533. ctrl = SysTick->CTRL;
  534. // get the number of ticks less than max RELOAD.
  535. num_remain = ticks % SysTick_LOAD_RELOAD_Msk;
  536. /* if ticks is < Max SysTick Reload num_full will be 0, otherwise it will
  537. give us the number of max SysTicks cycles required */
  538. num_full = (ticks - 1) / SysTick_LOAD_RELOAD_Msk;
  539. // Do the required full systick countdowns
  540. if (num_full) {
  541. // load the max count value into systick
  542. SysTick->LOAD = SysTick_LOAD_RELOAD_Msk;
  543. // load the starting value
  544. SysTick->VAL = 0;
  545. // enable SysTick counter with SystemClock source internal, immediately forces LOAD register into VAL register
  546. SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk | SysTick_CTRL_ENABLE_Msk;
  547. // CountFlag will get set when VAL reaches zero
  548. for (i = num_full; i > 0; i--) {
  549. do {
  550. cur_ticks = SysTick->CTRL;
  551. } while (!(cur_ticks & SysTick_CTRL_COUNTFLAG_Msk));
  552. }
  553. // Disable systick
  554. SysTick->CTRL = 0;
  555. }
  556. // Now handle the remainder of ticks
  557. if (num_remain) {
  558. SysTick->LOAD = num_remain;
  559. SysTick->VAL = 0;
  560. SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk | SysTick_CTRL_ENABLE_Msk;
  561. // wait for countflag to get set
  562. do {
  563. cur_ticks = SysTick->CTRL;
  564. } while (!(cur_ticks & SysTick_CTRL_COUNTFLAG_Msk));
  565. // Disable systick
  566. SysTick->CTRL = 0;
  567. }
  568. // restore original state of SysTick and return
  569. SysTick->LOAD = reload;
  570. SysTick->VAL = value;
  571. SysTick->CTRL = ctrl;
  572. return E_NO_ERROR;
  573. } else { /* SysTick is enabled
  574. When SysTick is enabled count flag can not be used
  575. and the reload can not be changed.
  576. Do not read the CTRL register -> clears count flag */
  577. // Get the reload value for wrap/reload case
  578. reload = SysTick->LOAD;
  579. // Read the starting systick value
  580. previous_ticks = SysTick->VAL;
  581. do {
  582. // get current SysTick value
  583. cur_ticks = SysTick->VAL;
  584. // Check for wrap/reload of timer countval
  585. if (cur_ticks > previous_ticks) {
  586. // subtract count to 0 (previous_ticks) and wrap (reload value - cur_ticks)
  587. num_subtract = (previous_ticks + (reload - cur_ticks));
  588. } else { /* standard case (no wrap)
  589. subtract off the number of ticks since last pass */
  590. num_subtract = (previous_ticks - cur_ticks);
  591. }
  592. // check to see if we are done.
  593. if (num_subtract >= ticks)
  594. return E_NO_ERROR;
  595. else
  596. ticks -= num_subtract;
  597. // cur_ticks becomes previous_ticks for next timer read.
  598. previous_ticks = cur_ticks;
  599. } while (ticks > 0);
  600. // Should not ever be reached
  601. return E_NO_ERROR;
  602. }
  603. }
  604. /* ************************************************************************ */
  605. void SYS_SysTick_DelayUs(uint32_t us)
  606. {
  607. SYS_SysTick_Delay((uint32_t)(((uint64_t)SYS_SysTick_GetFreq() * us) / 1000000));
  608. }
  609. /* ************************************************************************ */
  610. int SYS_WDT_Init(mxc_wdt_regs_t* wdt, const sys_cfg_wdt_t* sys_cfg)
  611. {
  612. return E_NO_ERROR;
  613. }
  614. /**@} end of ingroup MXC_sys*/