utils.c 19 KB

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