hal_oad.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. /**************************************************************************************************
  2. Filename: _hal_oad.c
  3. Revised: $Date: 2010-07-08 18:39:25 -0700 (Thu, 08 Jul 2010) $
  4. Revision: $Revision: 22957 $
  5. Description: This module contains optionally-compiled Boot Code to support OAD.
  6. The rest of the functionality is the H/W specific drivers to read/write
  7. the flash/NV containing the ACTIVE and the DOWNLOADED images.
  8. Notes: This version targets the Texas Instruments CC253x family of processors.
  9. Copyright 2008-2010 Texas Instruments Incorporated. All rights reserved.
  10. IMPORTANT: Your use of this Software is limited to those specific rights
  11. granted under the terms of a software license agreement between the user
  12. who downloaded the software, his/her employer (which must be your employer)
  13. and Texas Instruments Incorporated (the "License"). You may not use this
  14. Software unless you agree to abide by the terms of the License. The License
  15. limits your use, and you acknowledge, that the Software may not be modified,
  16. copied or distributed unless embedded on a Texas Instruments microcontroller
  17. or used solely and exclusively in conjunction with a Texas Instruments radio
  18. frequency transceiver, which is integrated into your product. Other than for
  19. the foregoing purpose, you may not use, reproduce, copy, prepare derivative
  20. works of, modify, distribute, perform, display or sell this Software and/or
  21. its documentation for any purpose.
  22. YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
  23. PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
  24. INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
  25. NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
  26. TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,
  27. NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
  28. LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
  29. INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
  30. OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
  31. OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
  32. (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
  33. Should you have any questions regarding your right to use this Software,
  34. contact Texas Instruments Incorporated at www.TI.com.
  35. **************************************************************************************************/
  36. /* ------------------------------------------------------------------------------------------------
  37. * Includes
  38. * ------------------------------------------------------------------------------------------------
  39. */
  40. #include "comdef.h"
  41. #include "hal_board_cfg.h"
  42. #include "hal_dma.h"
  43. #include "hal_flash.h"
  44. #include "hal_oad.h"
  45. #include "hal_types.h"
  46. /* ------------------------------------------------------------------------------------------------
  47. * Local Variables
  48. * ------------------------------------------------------------------------------------------------
  49. */
  50. #if HAL_OAD_BOOT_CODE
  51. halDMADesc_t dmaCh0;
  52. #endif
  53. /* ------------------------------------------------------------------------------------------------
  54. * Local Functions
  55. * ------------------------------------------------------------------------------------------------
  56. */
  57. static uint16 runPoly(uint16 crc, uint8 val);
  58. #if HAL_OAD_XNV_IS_SPI
  59. static void HalSPIRead(uint32 addr, uint8 *pBuf, uint16 len);
  60. static void HalSPIWrite(uint32 addr, uint8 *pBuf, uint16 len);
  61. #endif
  62. #if HAL_OAD_BOOT_CODE
  63. static void vddWait(uint8 vdd);
  64. static void dl2rc(void);
  65. static uint16 crcCalc(void);
  66. /**************************************************************************************************
  67. * @fn main
  68. *
  69. * @brief ISR for the reset vector.
  70. *
  71. * input parameters
  72. *
  73. * None.
  74. *
  75. * output parameters
  76. *
  77. * None.
  78. *
  79. * @return None.
  80. **************************************************************************************************
  81. */
  82. #pragma location="NEAR_CODE"
  83. void main(void)
  84. {
  85. HAL_BOARD_INIT();
  86. vddWait(VDD_MIN_RUN);
  87. #if HAL_OAD_XNV_IS_SPI
  88. XNV_SPI_INIT();
  89. #endif
  90. /* This is in place of calling HalDmaInit() which would require init of the other 4 DMA
  91. * descriptors in addition to just Channel 0.
  92. */
  93. HAL_DMA_SET_ADDR_DESC0( &dmaCh0 );
  94. while (1)
  95. {
  96. uint16 crc[2];
  97. HalFlashRead(HAL_OAD_CRC_ADDR / HAL_FLASH_PAGE_SIZE,
  98. HAL_OAD_CRC_ADDR % HAL_FLASH_PAGE_SIZE,
  99. (uint8 *)crc, sizeof(crc));
  100. if (crc[0] == crc[1])
  101. {
  102. break;
  103. }
  104. else if ((crc[0] != 0) && (crc[0] == crcCalc()))
  105. {
  106. crc[1] = crc[0];
  107. HalFlashWrite((HAL_OAD_CRC_ADDR / HAL_FLASH_WORD_SIZE), (uint8 *)crc, 1);
  108. }
  109. else
  110. {
  111. dl2rc();
  112. }
  113. }
  114. // Simulate a reset for the Application code by an absolute jump to location 0x0800.
  115. asm("LJMP 0x800\n");
  116. }
  117. /*********************************************************************
  118. * @fn vddWait
  119. *
  120. * @brief Loop waiting for 256 reads of the Vdd over the requested limit.
  121. *
  122. * @param vdd - Vdd level to wait for.
  123. *
  124. * @return None.
  125. *********************************************************************/
  126. static void vddWait(uint8 vdd)
  127. {
  128. uint8 cnt = 16;
  129. do {
  130. do {
  131. ADCCON3 = 0x0F;
  132. while (!(ADCCON1 & 0x80));
  133. } while (ADCH < vdd);
  134. } while (--cnt);
  135. }
  136. /*********************************************************************
  137. * @fn dl2rc
  138. *
  139. * @brief Copy the DL image to the RC image location.
  140. *
  141. * NOTE: Assumes that DL image ends on a flash word boundary.
  142. *
  143. * @param None.
  144. *
  145. * @return None.
  146. *********************************************************************/
  147. static void dl2rc(void)
  148. {
  149. preamble_t preamble;
  150. uint32 oset;
  151. uint16 addr = HAL_OAD_RC_START / HAL_FLASH_WORD_SIZE;
  152. uint8 buf[4];
  153. vddWait(VDD_MIN_OAD);
  154. HalOADRead(PREAMBLE_OFFSET, (uint8 *)&preamble, sizeof(preamble_t), HAL_OAD_DL);
  155. for (oset = 0; oset < preamble.len; oset += HAL_FLASH_WORD_SIZE)
  156. {
  157. HalOADRead(oset, buf, HAL_FLASH_WORD_SIZE, HAL_OAD_DL);
  158. if ((addr % (HAL_FLASH_PAGE_SIZE / HAL_FLASH_WORD_SIZE)) == 0)
  159. {
  160. HalFlashErase(addr / (HAL_FLASH_PAGE_SIZE / HAL_FLASH_WORD_SIZE));
  161. }
  162. HalFlashWrite(addr++, buf, 1);
  163. }
  164. }
  165. /*********************************************************************
  166. * @fn crcCalc
  167. *
  168. * @brief Run the CRC16 Polynomial calculation over the RC image.
  169. *
  170. * @param None.
  171. *
  172. * @return The CRC16 calculated.
  173. */
  174. static uint16 crcCalc(void)
  175. {
  176. preamble_t preamble;
  177. uint32 oset;
  178. uint16 crc = 0;
  179. HalOADRead(PREAMBLE_OFFSET, (uint8 *)&preamble, sizeof(preamble_t), HAL_OAD_RC);
  180. if (preamble.len > HAL_OAD_DL_SIZE)
  181. {
  182. return 0;
  183. }
  184. // Run the CRC calculation over the active body of code.
  185. for (oset = 0; oset < preamble.len; oset++)
  186. {
  187. if (oset == HAL_OAD_CRC_OSET)
  188. {
  189. oset += 3;
  190. }
  191. else
  192. {
  193. uint8 buf;
  194. HalOADRead(oset, &buf, 1, HAL_OAD_RC);
  195. crc = runPoly(crc, buf);
  196. }
  197. }
  198. // IAR note explains that poly must be run with value zero for each byte of crc.
  199. crc = runPoly(crc, 0);
  200. crc = runPoly(crc, 0);
  201. return crc;
  202. }
  203. #endif
  204. /*********************************************************************
  205. * @fn runPoly
  206. *
  207. * @brief Run the CRC16 Polynomial calculation over the byte parameter.
  208. *
  209. * @param crc - Running CRC calculated so far.
  210. * @param val - Value on which to run the CRC16.
  211. *
  212. * @return crc - Updated for the run.
  213. */
  214. static uint16 runPoly(uint16 crc, uint8 val)
  215. {
  216. const uint16 poly = 0x1021;
  217. uint8 cnt;
  218. for (cnt = 0; cnt < 8; cnt++, val <<= 1)
  219. {
  220. uint8 msb = (crc & 0x8000) ? 1 : 0;
  221. crc <<= 1;
  222. if (val & 0x80) crc |= 0x0001;
  223. if (msb) crc ^= poly;
  224. }
  225. return crc;
  226. }
  227. /*********************************************************************
  228. * @fn HalOADChkDL
  229. *
  230. * @brief Run the CRC16 Polynomial calculation over the DL image.
  231. *
  232. * @param dlImagePreambleOffset - Offset into the monolithic DL image to read the preamble.
  233. *
  234. * @return SUCCESS or FAILURE.
  235. *********************************************************************/
  236. uint8 HalOADChkDL(uint8 dlImagePreambleOffset)
  237. {
  238. preamble_t preamble;
  239. uint32 oset;
  240. uint16 crc = 0, crc2;
  241. HalOADRead(dlImagePreambleOffset, (uint8 *)&preamble, sizeof(preamble_t), HAL_OAD_DL);
  242. // Run the CRC calculation over the downloaded image.
  243. for (oset = 0; oset < preamble.len; oset++)
  244. {
  245. if ((oset < HAL_OAD_CRC_OSET) || (oset >= HAL_OAD_CRC_OSET+4))
  246. {
  247. uint8 buf;
  248. HalOADRead(oset, &buf, 1, HAL_OAD_DL);
  249. crc = runPoly(crc, buf);
  250. }
  251. }
  252. // IAR note explains that poly must be run with value zero for each byte of crc.
  253. crc = runPoly(crc, 0);
  254. crc = runPoly(crc, 0);
  255. HalOADRead(HAL_OAD_CRC_OSET, (uint8 *)&crc2, sizeof(crc2), HAL_OAD_DL);
  256. return (crc2 == crc) ? SUCCESS : FAILURE;
  257. }
  258. /*********************************************************************
  259. * @fn HalOADInvRC
  260. *
  261. * @brief Invalidate the active image so that the boot code will instantiate the DL image on the
  262. * next reset.
  263. *
  264. * @param None.
  265. *
  266. * @return None.
  267. *********************************************************************/
  268. void HalOADInvRC(void)
  269. {
  270. uint16 crc[2] = {0,0xFFFF};
  271. HalFlashWrite((HAL_OAD_CRC_ADDR / HAL_FLASH_WORD_SIZE), (uint8 *)crc, 1);
  272. }
  273. /*********************************************************************
  274. * @fn HalOADRead
  275. *
  276. * @brief Read from the storage medium according to image type.
  277. *
  278. * @param oset - Offset into the monolithic image.
  279. * @param pBuf - Pointer to the buffer in which to copy the bytes read.
  280. * @param len - Number of bytes to read.
  281. * @param type - Which image: HAL_OAD_RC or HAL_OAD_DL.
  282. *
  283. * @return None.
  284. *********************************************************************/
  285. void HalOADRead(uint32 oset, uint8 *pBuf, uint16 len, image_t type)
  286. {
  287. if (HAL_OAD_RC != type)
  288. {
  289. #if HAL_OAD_XNV_IS_INT
  290. preamble_t preamble;
  291. HalOADRead(PREAMBLE_OFFSET, (uint8 *)&preamble, sizeof(preamble_t), HAL_OAD_RC);
  292. //oset += HAL_OAD_RC_START + preamble.len;
  293. oset += HAL_OAD_RC_START + HAL_OAD_DL_OSET;
  294. #elif HAL_OAD_XNV_IS_SPI
  295. oset += HAL_OAD_DL_OSET;
  296. HalSPIRead(oset, pBuf, len);
  297. return;
  298. #endif
  299. }
  300. else
  301. {
  302. oset += HAL_OAD_RC_START;
  303. }
  304. HalFlashRead(oset / HAL_FLASH_PAGE_SIZE, oset % HAL_FLASH_PAGE_SIZE, pBuf, len);
  305. }
  306. /*********************************************************************
  307. * @fn HalOADWrite
  308. *
  309. * @brief Write to the storage medium according to the image type.
  310. *
  311. * NOTE: Destructive write on page boundary! When writing to the first flash word
  312. * of a page boundary, the page is erased without saving/restoring the bytes not written.
  313. * Writes anywhere else on a page assume that the location written to has been erased.
  314. *
  315. * @param oset - Offset into the monolithic image, aligned to HAL_FLASH_WORD_SIZE.
  316. * @param pBuf - Pointer to the buffer in from which to write.
  317. * @param len - Number of bytes to write. If not an even multiple of HAL_FLASH_WORD_SIZE,
  318. * remainder bytes are overwritten with garbage.
  319. * @param type - Which image: HAL_OAD_RC or HAL_OAD_DL.
  320. *
  321. * @return None.
  322. *********************************************************************/
  323. void HalOADWrite(uint32 oset, uint8 *pBuf, uint16 len, image_t type)
  324. {
  325. if (HAL_OAD_RC != type)
  326. {
  327. #if HAL_OAD_XNV_IS_INT
  328. preamble_t preamble;
  329. HalOADRead(PREAMBLE_OFFSET, (uint8 *)&preamble, sizeof(preamble_t), HAL_OAD_RC);
  330. //oset += HAL_OAD_RC_START + preamble.len;
  331. oset += HAL_OAD_RC_START + HAL_OAD_DL_OSET;
  332. #elif HAL_OAD_XNV_IS_SPI
  333. oset += HAL_OAD_DL_OSET;
  334. HalSPIWrite(oset, pBuf, len);
  335. return;
  336. #endif
  337. }
  338. else
  339. {
  340. oset += HAL_OAD_RC_START;
  341. }
  342. if ((oset % HAL_FLASH_PAGE_SIZE) == 0)
  343. {
  344. HalFlashErase(oset / HAL_FLASH_PAGE_SIZE);
  345. }
  346. HalFlashWrite(oset / HAL_FLASH_WORD_SIZE, pBuf, len / HAL_FLASH_WORD_SIZE);
  347. }
  348. #if HAL_OAD_XNV_IS_INT
  349. /*********************************************************************
  350. * @fn HalOADAvail
  351. *
  352. * @brief Determine the space available for downloading an image.
  353. *
  354. * @param None.
  355. *
  356. * @return Number of bytes available for storing an OAD image.
  357. *********************************************************************/
  358. uint32 HalOADAvail(void)
  359. {
  360. /*
  361. preamble_t preamble;
  362. HalOADRead(PREAMBLE_OFFSET, (uint8 *)&preamble, sizeof(preamble_t), HAL_OAD_RC);
  363. return HAL_OAD_DL_MAX - preamble.len;
  364. */
  365. return HAL_OAD_DL_MAX - HAL_OAD_DL_OSET;
  366. }
  367. #elif HAL_OAD_XNV_IS_SPI
  368. /*********************************************************************
  369. * CONSTANTS
  370. */
  371. #define XNV_STAT_CMD 0x05
  372. #define XNV_WREN_CMD 0x06
  373. #define XNV_WRPG_CMD 0x0A
  374. #define XNV_READ_CMD 0x0B
  375. #define XNV_STAT_WIP 0x01
  376. /*********************************************************************
  377. * @fn xnvSPIWrite
  378. *
  379. * @brief SPI write sequence for code size savings.
  380. *
  381. * @param ch - The byte to write to the SPI.
  382. *
  383. * @return None.
  384. *********************************************************************/
  385. static void xnvSPIWrite(uint8 ch);
  386. static void xnvSPIWrite(uint8 ch)
  387. {
  388. XNV_SPI_TX(ch);
  389. XNV_SPI_WAIT_RXRDY();
  390. }
  391. /*********************************************************************
  392. * @fn HalOADAvail
  393. *
  394. * @brief Determine the space available for downloading an image.
  395. *
  396. * @param None.
  397. *
  398. * @return Number of bytes available for storing an OAD image.
  399. *********************************************************************/
  400. uint32 HalOADAvail(void)
  401. {
  402. return HAL_OAD_DL_MAX - HAL_OAD_DL_OSET;
  403. }
  404. /*********************************************************************
  405. * @fn HalSPIRead
  406. *
  407. * @brief Read from the external NV storage via SPI.
  408. *
  409. * @param addr - Offset into the external NV.
  410. * @param pBuf - Pointer to the buffer in which to copy the bytes read from external NV.
  411. * @param len - Number of bytes to read from external NV.
  412. *
  413. * @return None.
  414. *********************************************************************/
  415. static void HalSPIRead(uint32 addr, uint8 *pBuf, uint16 len)
  416. {
  417. #if !HAL_OAD_BOOT_CODE
  418. uint8 shdw = P1DIR;
  419. halIntState_t his;
  420. HAL_ENTER_CRITICAL_SECTION(his);
  421. P1DIR |= BV(3);
  422. #endif
  423. XNV_SPI_BEGIN();
  424. do {
  425. xnvSPIWrite(XNV_STAT_CMD);
  426. } while (XNV_SPI_RX() & XNV_STAT_WIP);
  427. XNV_SPI_END();
  428. asm("NOP"); asm("NOP");
  429. XNV_SPI_BEGIN();
  430. xnvSPIWrite(XNV_READ_CMD);
  431. xnvSPIWrite(addr >> 16);
  432. xnvSPIWrite(addr >> 8);
  433. xnvSPIWrite(addr);
  434. xnvSPIWrite(0);
  435. while (len--)
  436. {
  437. xnvSPIWrite(0);
  438. *pBuf++ = XNV_SPI_RX();
  439. }
  440. XNV_SPI_END();
  441. #if !HAL_OAD_BOOT_CODE
  442. P1DIR = shdw;
  443. HAL_EXIT_CRITICAL_SECTION(his);
  444. #endif
  445. }
  446. /*********************************************************************
  447. * @fn HalSPIWrite
  448. *
  449. * @brief Write to the external NV storage via SPI.
  450. *
  451. * @param addr - Offset into the external NV.
  452. * @param pBuf - Pointer to the buffer in from which to write bytes to external NV.
  453. * @param len - Number of bytes to write to external NV.
  454. *
  455. * @return None.
  456. *********************************************************************/
  457. static void HalSPIWrite(uint32 addr, uint8 *pBuf, uint16 len)
  458. {
  459. uint8 cnt;
  460. #if !HAL_OAD_BOOT_CODE
  461. uint8 shdw = P1DIR;
  462. halIntState_t his;
  463. HAL_ENTER_CRITICAL_SECTION(his);
  464. P1DIR |= BV(3);
  465. #endif
  466. while (len)
  467. {
  468. XNV_SPI_BEGIN();
  469. do {
  470. xnvSPIWrite(XNV_STAT_CMD);
  471. } while (XNV_SPI_RX() & XNV_STAT_WIP);
  472. XNV_SPI_END();
  473. asm("NOP"); asm("NOP");
  474. XNV_SPI_BEGIN();
  475. xnvSPIWrite(XNV_WREN_CMD);
  476. XNV_SPI_END();
  477. asm("NOP"); asm("NOP");
  478. XNV_SPI_BEGIN();
  479. xnvSPIWrite(XNV_WRPG_CMD);
  480. xnvSPIWrite(addr >> 16);
  481. xnvSPIWrite(addr >> 8);
  482. xnvSPIWrite(addr);
  483. // Can only write within any one page boundary, so prepare for next page write if bytes remain.
  484. cnt = 0 - (uint8)addr;
  485. if (cnt)
  486. {
  487. addr += cnt;
  488. }
  489. else
  490. {
  491. addr += 256;
  492. }
  493. do
  494. {
  495. xnvSPIWrite(*pBuf++);
  496. cnt--;
  497. len--;
  498. } while (len && cnt);
  499. XNV_SPI_END();
  500. }
  501. #if !HAL_OAD_BOOT_CODE
  502. P1DIR = shdw;
  503. HAL_EXIT_CRITICAL_SECTION(his);
  504. #endif
  505. }
  506. #else
  507. #error Invalid Xtra-NV for OAD.
  508. #endif
  509. /**************************************************************************************************
  510. */