utils.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  1. /**
  2. *********************************************************************************
  3. *
  4. * @file utils.c
  5. * @brief This file contains the Utilities functions/types for the driver.
  6. *
  7. * @version V1.1
  8. * @date 13 Apr 2021
  9. * @author AE Team
  10. * @note
  11. * Change Logs:
  12. * Date Author Notes
  13. * 07 Nov 2019 AE Team The first version
  14. * 13 Apr 2021 AE Team Add API: sys_config()
  15. *
  16. * Copyright (C) Shanghai Eastsoft Microelectronics Co. Ltd. All rights reserved.
  17. *
  18. * SPDX-License-Identifier: Apache-2.0
  19. *
  20. * Licensed under the Apache License, Version 2.0 (the License); you may
  21. * not use this file except in compliance with the License.
  22. * You may obtain a copy of the License at
  23. *
  24. * www.apache.org/licenses/LICENSE-2.0
  25. *
  26. * Unless required by applicable law or agreed to in writing, software
  27. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  28. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  29. * See the License for the specific language governing permissions and
  30. * limitations under the License.
  31. *
  32. *********************************************************************************
  33. */
  34. #include <string.h>
  35. #include "utils.h"
  36. #include "ald_dma.h"
  37. #include "ald_cmu.h"
  38. #include "ald_iap.h"
  39. /** @defgroup ES32FXXX_ALD EASTSOFT ES32F3xx ALD
  40. * @brief Shanghai Eastsoft Microelectronics Cortex-M Chip Abstraction Layer Driver(ALD)
  41. * @{
  42. */
  43. /** @defgroup UTILS Utils
  44. * @brief Utils module driver
  45. * @{
  46. */
  47. /** @defgroup ALD_Private_Constants Private Constants
  48. * @brief ALD Private Constants
  49. * @{
  50. */
  51. /**
  52. * @brief ALD version number
  53. */
  54. #define __ALD_VERSION_MAIN (0x01) /**< [31:24] main version */
  55. #define __ALD_VERSION_SUB1 (0x00) /**< [23:16] sub1 version */
  56. #define __ALD_VERSION_SUB2 (0x00) /**< [15:8] sub2 version */
  57. #define __ALD_VERSION_RC (0x00) /**< [7:0] release candidate */
  58. #define __ALD_VERSION ((__ALD_VERSION_MAIN << 24) | \
  59. (__ALD_VERSION_SUB1 << 16) | \
  60. (__ALD_VERSION_SUB2 << 8 ) | \
  61. (__ALD_VERSION_RC))
  62. /**
  63. * @}
  64. */
  65. /** @defgroup ALD_Private_Variables Private Variables
  66. * @{
  67. */
  68. /** @brief lib_tick: Increase by one millisecond
  69. */
  70. static __IO uint32_t lib_tick;
  71. uint32_t __systick_interval = SYSTICK_INTERVAL_1MS;
  72. /**
  73. * @}
  74. */
  75. /** @defgroup ALD_Public_Functions Public Functions
  76. * @{
  77. */
  78. /** @defgroup ALD_Public_Functions_Group1 Initialization Function
  79. * @brief Initialization functions
  80. *
  81. * @verbatim
  82. ===============================================================================
  83. ##### Initialization functions #####
  84. ===============================================================================
  85. [..] This section provides functions allowing to:
  86. (+) Initializes interface, the NVIC allocation and initial clock
  87. configuration. It initializes the source of time base also when timeout
  88. is needed and the backup domain when enabled.
  89. (+) Configure The time base source to have 1ms time base with a dedicated
  90. Tick interrupt priority.
  91. (++) Systick timer is used by default as source of time base, but user
  92. can eventually implement his proper time base source (a general purpose
  93. timer for example or other time source), keeping in mind that Time base
  94. duration should be kept 1ms.
  95. (++) Time base configuration function (ald_tick_init()) is called automatically
  96. at the beginning of the program after reset by ald_cmu_init() or at
  97. any time when clock is configured.
  98. (++) Source of time base is configured to generate interrupts at regular
  99. time intervals. Care must be taken if ald_delay_ms() is called from a
  100. peripheral ISR process, the Tick interrupt line must have higher priority
  101. (numerically lower) than the peripheral interrupt. Otherwise the caller
  102. ISR process will be blocked.
  103. (++) functions affecting time base configurations are declared as __weak
  104. to make override possible in case of other implementations in user file.
  105. (+) Configure the interval of Systick interrupt.
  106. @endverbatim
  107. * @{
  108. */
  109. /**
  110. * @brief This function Configures time base source, NVIC and DMA.
  111. * @note This function is called at the beginning of program after reset and before
  112. * the clock configuration.
  113. * @note The time base configuration is based on MSI clock when exiting from Reset.
  114. * Once done, time base tick start incrementing.
  115. * In the default implementation, Systick is used as source of time base.
  116. * The tick variable is incremented each 1ms in its ISR.
  117. * @retval None
  118. */
  119. void ald_cmu_init(void)
  120. {
  121. NVIC_SetPriorityGrouping(NVIC_PRIORITY_GROUP_2);
  122. ald_cmu_clock_config_default();
  123. ald_tick_init(TICK_INT_PRIORITY);
  124. #ifdef ALD_DMA
  125. ald_cmu_perh_clock_config(CMU_PERH_DMA, ENABLE);
  126. ald_dma_init(DMA0);
  127. #endif
  128. return;
  129. }
  130. /**
  131. * @brief This function configures the source of the time base.
  132. * The time source is configured to have 1ms time base with a dedicated
  133. * Tick interrupt priority.
  134. * @note In the default implementation, SysTick timer is the source of time base.
  135. * It is used to generate interrupts at regular time intervals.
  136. * Care must be taken if ald_delay_ms() is called from a peripheral ISR process,
  137. * The SysTick interrupt must have higher priority (numerically lower)
  138. * than the peripheral interrupt. Otherwise the caller ISR process will be blocked.
  139. * The function is declared as __weak to be overwritten in case of other
  140. * implementation in user file.
  141. * @param prio: Tick interrupt priority.
  142. * @retval None
  143. */
  144. __weak void ald_tick_init(uint32_t prio)
  145. {
  146. /* Configure the SysTick IRQ */
  147. SysTick_Config(ald_cmu_get_sys_clock() / SYSTICK_INTERVAL_1MS);
  148. NVIC_SetPriority(SysTick_IRQn, prio);
  149. return;
  150. }
  151. /**
  152. * @brief Selects the interval of systick interrupt.
  153. * @param value: The value of interval:
  154. * @arg @ref SYSTICK_INTERVAL_1MS 1 millisecond
  155. * @arg @ref SYSTICK_INTERVAL_10MS 10 milliseconds
  156. * @arg @ref SYSTICK_INTERVAL_100MS 100 milliseconds
  157. * @arg @ref SYSTICK_INTERVAL_1000MS 1 second
  158. * @retval None
  159. */
  160. void ald_systick_interval_select(systick_interval_t value)
  161. {
  162. assert_param(IS_SYSTICK_INTERVAL(value));
  163. if (value == 0) return;
  164. SysTick_Config(ald_cmu_get_sys_clock() / value);
  165. __systick_interval = value;
  166. if (TICK_INT_PRIORITY != 15)
  167. NVIC_SetPriority(SysTick_IRQn, TICK_INT_PRIORITY);
  168. return;
  169. }
  170. /**
  171. * @}
  172. */
  173. /** @defgroup ALD_Public_Functions_Group2 Control functions
  174. * @brief Control functions
  175. *
  176. * @verbatim
  177. ===============================================================================
  178. ##### Control functions #####
  179. ===============================================================================
  180. [..] This section provides functions allowing to:
  181. (+) Provide a tick value in millisecond
  182. (+) Provide a blocking delay in millisecond
  183. (+) Suspend the time base source interrupt
  184. (+) Resume the time base source interrupt
  185. (+) Get the ALD version
  186. (+) Waiting for flag
  187. (+) Configure the interrupt
  188. (+) Provide system tick value
  189. (+) Initialize core timestamp
  190. (+) Get core timestamp
  191. (+) Get CPU ID
  192. (+) Get UID
  193. (+) Get CHIPID
  194. @endverbatim
  195. * @{
  196. */
  197. /**
  198. * @brief This function invoked by Systick ISR.
  199. * @note This function is declared as __weak to be overwritten in case of
  200. * other implementations in user file.
  201. * @retval None
  202. */
  203. __weak void ald_systick_irq_cbk(void)
  204. {
  205. /* do nothing */
  206. return;
  207. }
  208. /**
  209. * @brief This function is called to increment a global variable "lib_tick"
  210. * used as application time base.
  211. * @note In the default implementation, this variable is incremented each 1ms
  212. * in Systick ISR.
  213. * @note This function is declared as __weak to be overwritten in case of other
  214. * implementations in user file.
  215. * @retval None
  216. */
  217. __weak void ald_inc_tick(void)
  218. {
  219. ++lib_tick;
  220. ald_systick_irq_cbk();
  221. }
  222. /**
  223. * @brief Provides a tick value in millisecond.
  224. * @note This function is declared as __weak to be overwritten in case of other
  225. * implementations in user file.
  226. * @retval tick value
  227. */
  228. __weak uint32_t ald_get_tick(void)
  229. {
  230. return lib_tick;
  231. }
  232. /**
  233. * @brief This function provides accurate delay (in milliseconds) based
  234. * on variable incremented.
  235. * @note In the default implementation, SysTick timer is the source of time base.
  236. * It is used to generate interrupts at regular time intervals where lib_tick
  237. * is incremented.
  238. * @note This function is declared as __weak to be overwritten in case of other
  239. * implementations in user file.
  240. * @param delay: specifies the delay time length, in microseconds(us).
  241. * @retval None
  242. */
  243. __weak void ald_delay_us(__IO uint32_t delay)
  244. {
  245. uint32_t start, now, delta, reload, us_tick;
  246. start = SysTick->VAL;
  247. reload = SysTick->LOAD;
  248. us_tick = ald_cmu_get_sys_clock() / 1000000UL;
  249. do
  250. {
  251. now = SysTick->VAL;
  252. delta = (start > now) ? (start - now) : (reload + start - now);
  253. }
  254. while (delta < (us_tick * delay));
  255. }
  256. /**
  257. * @brief This function provides accurate delay (in milliseconds) based
  258. * on variable incremented.
  259. * @note In the default implementation, SysTick timer is the source of time base.
  260. * It is used to generate interrupts at regular time intervals where lib_tick
  261. * is incremented.
  262. * @note This function is declared as __weak to be overwritten in case of other
  263. * implementations in user file.
  264. * @param delay: specifies the delay time length, in milliseconds.
  265. * @retval None
  266. */
  267. __weak void ald_delay_ms(__IO uint32_t delay)
  268. {
  269. uint32_t tick, __delay;
  270. switch (__systick_interval) {
  271. case SYSTICK_INTERVAL_1MS:
  272. __delay = delay;
  273. break;
  274. case SYSTICK_INTERVAL_10MS:
  275. __delay = delay / 10;
  276. break;
  277. case SYSTICK_INTERVAL_100MS:
  278. __delay = delay / 100;
  279. break;
  280. case SYSTICK_INTERVAL_1000MS:
  281. __delay = delay / 1000;
  282. break;
  283. default:
  284. __delay = delay;
  285. break;
  286. }
  287. tick = ald_get_tick();
  288. __delay = __delay == 0 ? 1 : __delay;
  289. while ((ald_get_tick() - tick) < __delay)
  290. ;
  291. }
  292. /**
  293. * @brief Suspend Tick increment.
  294. * @note In the default implementation, SysTick timer is the source of time base.
  295. * It is used to generate interrupts at regular time intervals.
  296. * Once ald_suspend_tick() is called, the the SysTick interrupt
  297. * will be disabled and so Tick increment is suspended.
  298. * @note This function is declared as __weak to be overwritten
  299. * in case of other implementations in user file.
  300. * @retval None
  301. */
  302. __weak void ald_suspend_tick(void)
  303. {
  304. CLEAR_BIT(SysTick->CTRL, SysTick_CTRL_TICKINT_Msk);
  305. }
  306. /**
  307. * @brief Resume Tick increment.
  308. * @note In the default implementation, SysTick timer is the source of
  309. * time base. It is used to generate interrupts at regular time
  310. * intervals. Once ald_resume_tick() is called, the the SysTick
  311. * interrupt will be enabled and so Tick increment is resumed.
  312. * @note This function is declared as __weak to be overwritten
  313. * in case of other implementations in user file.
  314. * @retval None
  315. */
  316. __weak void ald_resume_tick(void)
  317. {
  318. SET_BIT(SysTick->CTRL, SysTick_CTRL_TICKINT_Msk);
  319. }
  320. /**
  321. * @brief This method returns the ALD revision
  322. * @retval version: 0xXYZR (8bits for each decimal, R for RC)
  323. */
  324. uint32_t ald_get_ald_version(void)
  325. {
  326. return __ALD_VERSION;
  327. }
  328. /**
  329. * @brief Configure the flash wait period.
  330. * @param cycle: The period.
  331. * @retval None
  332. */
  333. void ald_flash_wait_config(uint8_t cycle)
  334. {
  335. uint32_t tmp;
  336. tmp = MSC->MEMWAIT;
  337. MODIFY_REG(tmp, MSC_MEMWAIT_FLASH_W_MSK, (0x30U | cycle) << MSC_MEMWAIT_FLASH_W_POSS);
  338. MSC->MEMWAIT = tmp;
  339. return;
  340. }
  341. /**
  342. * @brief Waiting the specified bit in the register change to SET/RESET.
  343. * @param reg: The register address.
  344. * @param bit: The specified bit.
  345. * @param status: The status for waiting.
  346. * @param timeout: Timeout duration.
  347. * @retval Status, see @ref ald_status_t.
  348. */
  349. ald_status_t ald_wait_flag(uint32_t *reg, uint32_t bit, flag_status_t status, uint32_t timeout)
  350. {
  351. uint32_t tick = ald_get_tick();
  352. assert_param(timeout > 0);
  353. if (status == SET) {
  354. while (!(IS_BIT_SET(*reg, bit))) {
  355. if (((ald_get_tick()) - tick) > timeout)
  356. return TIMEOUT;
  357. }
  358. }
  359. else {
  360. while ((IS_BIT_SET(*reg, bit))) {
  361. if (((ald_get_tick()) - tick) > timeout)
  362. return TIMEOUT;
  363. }
  364. }
  365. return OK;
  366. }
  367. /**
  368. * @brief Configure interrupt.
  369. * @param irq: Interrunpt type.
  370. * @param preempt_prio: preempt priority(0-3).
  371. * @param sub_prio: sub-priority(0-3).
  372. * @param status: Status.
  373. * @arg ENABLE
  374. * @arg DISABLE
  375. * @retval None
  376. */
  377. void ald_mcu_irq_config(IRQn_Type irq, uint8_t preempt_prio, uint8_t sub_prio, type_func_t status)
  378. {
  379. uint32_t pri;
  380. uint8_t sub_bw, pre_bw;
  381. uint8_t sub_mask = 0xF;
  382. assert_param(IS_FUNC_STATE(status));
  383. assert_param(IS_PREEMPT_PRIO(preempt_prio));
  384. assert_param(IS_SUB_PRIO(sub_prio));
  385. if (status == ENABLE) {
  386. pre_bw = 7 - (((SCB->AIRCR) >> 8) & 7);
  387. sub_bw = 4 - pre_bw;
  388. sub_mask >>= pre_bw;
  389. pri = preempt_prio << sub_bw;
  390. pri |= sub_prio & sub_mask;
  391. NVIC_SetPriority(irq, pri);
  392. NVIC_EnableIRQ(irq);
  393. }
  394. else {
  395. NVIC_DisableIRQ(irq);
  396. }
  397. return;
  398. }
  399. /**
  400. * @brief Initialize core timestamp.
  401. * @retval None
  402. */
  403. void ald_mcu_timestamp_init(void)
  404. {
  405. DEM_CR |= (uint32_t)DEM_CR_TRCENA;
  406. DWT_CYCCNT = 0x0;
  407. DWT_CR |= (uint32_t)DWT_CR_CYCCNTEA;
  408. return;
  409. }
  410. /**
  411. * @brief Get core timestamp.
  412. * @retval None
  413. */
  414. uint32_t ald_mcu_get_timestamp(void)
  415. {
  416. return (uint32_t)DWT_CYCCNT;
  417. }
  418. /**
  419. * @brief Get the CPU ID.
  420. * @retval CPU ID.
  421. */
  422. uint32_t ald_mcu_get_cpu_id(void)
  423. {
  424. return SCB->CPUID;
  425. }
  426. /**
  427. * @brief Get the UID.
  428. * @param buf: Pointer to UID, len: 12Bytes(96-bits)
  429. * @retval None
  430. */
  431. void ald_mcu_get_uid(uint8_t *buf)
  432. {
  433. memcpy(&buf[0], (void *)MCU_UID0_ADDR, 4);
  434. memcpy(&buf[4], (void *)MCU_UID1_ADDR, 4);
  435. memcpy(&buf[8], (void *)MCU_UID2_ADDR, 4);
  436. return;
  437. }
  438. /**
  439. * @brief Get the CHIPID
  440. * @retval CHPID
  441. */
  442. uint32_t ald_mcu_get_chipid(void)
  443. {
  444. return (uint32_t)*(uint32_t *)MCU_CHIPID_ADDR;
  445. }
  446. /**
  447. * @brief Bypass bootroom and set VR1_Ref 0xA
  448. * @retval None
  449. */
  450. void sys_config(void)
  451. {
  452. uint32_t i = 0, tmp = 0;
  453. uint8_t err = 0, flag = 0;
  454. uint32_t inf014 = 0, inf0154 = 0, inf0244 = 0;
  455. uint8_t cnt = 4;
  456. uint32_t *inf0_addr = (uint32_t *)0x20003C00;
  457. /* read bootroom cfg register */
  458. inf014 = *((uint32_t *)(0x80000 + 56));
  459. /* read VR1_VREF register */
  460. inf0154 = *((uint32_t *)(0x80000 + 616));
  461. /* read Chip_v */
  462. inf0244 = *((uint32_t *)(0x80000 + 0x03D0));
  463. /* if D version ,do nothing */
  464. if (inf0244 == 0xFFFFFF44) return;
  465. if (inf0154 == 0xFFFFFFFF)
  466. while(1);
  467. /* if bypass bootroom */
  468. if ((0xFFFFFFFF != inf014)) {
  469. /* change cfg_boot value = 0xffff */
  470. inf014 = 0xFFFFFFFF;
  471. flag = 0x1;
  472. }
  473. /* change CFG_VR1_VREF value, FLASH ref 0xA */
  474. tmp = (inf0154 >> 8) & 0xF;
  475. if (0xA != tmp) {
  476. inf0154 &= (uint32_t)~(0xF << 8);
  477. inf0154 |= (0xA << 8);
  478. inf0154 = (inf0154 & (0x0000FFFF)) | ((~(inf0154 & 0xFFFF)) << 16);
  479. flag = 0x1;
  480. }
  481. /* if flag reset, return */
  482. if (0x0 == flag)
  483. return;
  484. /* 0x80000, 256words,INFO0 value */
  485. for (i = 0; i < 256; i++)
  486. inf0_addr[i] = *((uint32_t *)(0x80000 + i * 4));
  487. /* refresh value */
  488. inf0_addr[14] = inf014;
  489. inf0_addr[154] = inf0154;
  490. while(--cnt) {
  491. err = 0;
  492. /* unlock */
  493. *((volatile uint32_t *)(0x40080000)) = 0x55AA6996;
  494. *((volatile uint32_t *)(0x40080100)) = 0x5A962814;
  495. *((volatile uint32_t *)(0x40080100)) = 0xE7CB69A5;
  496. /* erase */
  497. if (ald_iap_erase_page(0x80000) == OK) {
  498. /* program 256*4bytes, info0 */
  499. if (ald_iap_program_words(0x80000, (uint8_t *)inf0_addr, 1024, 0) == OK) {
  500. /* check */
  501. for (i = 0; i < 256; i++) {
  502. if (inf0_addr[i] != *((uint32_t *)(0x80000 + i * 4))) {
  503. err = 1;
  504. break;;
  505. }
  506. }
  507. if (err == 0) {
  508. /* lock */
  509. *((volatile uint32_t *)(0x40080100)) = 0x123456;
  510. *((volatile uint32_t *)(0x40080100)) = 0x123456;
  511. *((volatile uint32_t *)(0x40080000)) = 0x123456;
  512. return;
  513. }
  514. }
  515. else {
  516. err = 1;
  517. }
  518. }
  519. else {
  520. err = 1;
  521. }
  522. }
  523. if (err) {
  524. ald_iap_erase_page(0x80000);
  525. /* lock */
  526. *((volatile uint32_t *)(0x40080100)) = 0x123456;
  527. *((volatile uint32_t *)(0x40080100)) = 0x123456;
  528. *((volatile uint32_t *)(0x40080000)) = 0x123456;
  529. while(1);
  530. }
  531. }
  532. /**
  533. * @brief ADC adjust parameter config
  534. * @retval None
  535. */
  536. void adc_config(void)
  537. {
  538. uint32_t inf0176 = 0, inf0178 = 0;
  539. uint32_t inf0250 = 0, inf0251 = 0;
  540. uint32_t inf0242 = 0, i = 0;
  541. uint8_t flag = 0, err = 0;
  542. uint8_t cnt = 4;
  543. /* ram store inf0 1k buffer, 15k ~ 16k */
  544. uint32_t *inf0_addr = (uint32_t *)0x20003C00;
  545. /* Read ADC_GE */
  546. inf0242 = *((uint32_t *)(0x80000 + 968));
  547. if (0xF5230ADC == inf0242) return;
  548. /* read Lot ID */
  549. inf0250 = *((uint32_t *)(0x80000 + 1000));
  550. inf0251 = *((uint32_t *)(0x80000 + 1004));
  551. inf0251 = (inf0251 & 0xFFFF0000) >> 16;
  552. /* read CFG_ADC0DA/CFG_ADC1DA */
  553. inf0176 = *((uint32_t *)(0x80000 + 704));
  554. inf0178 = *((uint32_t *)(0x80000 + 712));
  555. switch(inf0250) {
  556. case 0x45465537:
  557. if ((inf0251 == 0x3034) || (inf0251 == 0x3035))
  558. flag = 1;
  559. break;
  560. case 0x45503931:
  561. if ((inf0251 == 0x3732) || (inf0251 == 0x3734))
  562. flag = 1;
  563. break;
  564. case 0x45503935:
  565. if ((inf0251 == 0x3837) || (inf0251 == 0x3839))
  566. flag = 1;
  567. break;
  568. default:
  569. break;
  570. }
  571. if (!flag) return;
  572. inf0176 ^= (0x1 << 15);
  573. inf0176 = (inf0176 & 0x0000FFFF) | ((~(inf0176 & 0xFFFF)) << 16);
  574. inf0178 ^= (0x1 << 15);
  575. inf0178 = (inf0178 & 0x0000FFFF) | ((~(inf0178 & 0xFFFF)) << 16);
  576. /* 0x80000, 256words,INFO0 value */
  577. for (i = 0; i < 256; i++)
  578. inf0_addr[i] = *((uint32_t *)(0x80000 + i * 4));
  579. inf0_addr[176] = inf0176;
  580. inf0_addr[178] = inf0178;
  581. inf0_addr[242] = 0xF5230ADC;
  582. while(--cnt) {
  583. err = 0;
  584. /* unlock */
  585. *((volatile uint32_t *)(0x40080000)) = 0x55AA6996;
  586. *((volatile uint32_t *)(0x40080100)) = 0x5A962814;
  587. *((volatile uint32_t *)(0x40080100)) = 0xE7CB69A5;
  588. /* erase */
  589. if (ald_iap_erase_page(0x80000) == OK) {
  590. /* program 256*4bytes, info0 */
  591. if (ald_iap_program_words(0x80000, (uint8_t *)inf0_addr, 1024, 0) == OK) {
  592. /* check */
  593. for (i = 0; i < 256; i++) {
  594. if (inf0_addr[i] != *((uint32_t *)(0x80000 + i * 4))) {
  595. err = 1;
  596. break;;
  597. }
  598. }
  599. if (err == 0) {
  600. /* lock */
  601. *((volatile uint32_t *)(0x40080100)) = 0x123456;
  602. *((volatile uint32_t *)(0x40080100)) = 0x123456;
  603. *((volatile uint32_t *)(0x40080000)) = 0x123456;
  604. return;
  605. }
  606. }
  607. else {
  608. err = 1;
  609. }
  610. }
  611. else {
  612. err = 1;
  613. }
  614. }
  615. if (err) {
  616. ald_iap_erase_page(0x80000);
  617. /* lock */
  618. *((volatile uint32_t *)(0x40080100)) = 0x123456;
  619. *((volatile uint32_t *)(0x40080100)) = 0x123456;
  620. *((volatile uint32_t *)(0x40080000)) = 0x123456;
  621. while(1);
  622. }
  623. }
  624. /**
  625. * @}
  626. */
  627. /**
  628. * @}
  629. */
  630. /**
  631. * @}
  632. */
  633. /**
  634. * @}
  635. */