sb_main.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /**************************************************************************************************
  2. Filename: sb_main.c
  3. Revised: $Date: 2012-03-29 12:09:02 -0700 (Thu, 29 Mar 2012) $
  4. Revision: $Revision: 29943 $
  5. Description: This module contains the main functionality of a Boot Loader for CC2530.
  6. It is a minimal subset of functionality from ZMain.c, OnBoard.c and various
  7. _hal_X.c modules for the CC2530ZNP target.
  8. Copyright 2009-2012 Texas Instruments Incorporated. All rights reserved.
  9. IMPORTANT: Your use of this Software is limited to those specific rights
  10. granted under the terms of a software license agreement between the user
  11. who downloaded the software, his/her employer (which must be your employer)
  12. and Texas Instruments Incorporated (the "License"). You may not use this
  13. Software unless you agree to abide by the terms of the License. The License
  14. limits your use, and you acknowledge, that the Software may not be modified,
  15. copied or distributed unless embedded on a Texas Instruments microcontroller
  16. or used solely and exclusively in conjunction with a Texas Instruments radio
  17. frequency transceiver, which is integrated into your product. Other than for
  18. the foregoing purpose, you may not use, reproduce, copy, prepare derivative
  19. works of, modify, distribute, perform, display or sell this Software and/or
  20. its documentation for any purpose.
  21. YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
  22. PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
  23. INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
  24. NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
  25. TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,
  26. NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
  27. LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
  28. INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
  29. OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
  30. OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
  31. (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
  32. Should you have any questions regarding your right to use this Software,
  33. contact Texas Instruments Incorporated at www.TI.com.
  34. **************************************************************************************************/
  35. /* ------------------------------------------------------------------------------------------------
  36. * Includes
  37. * ------------------------------------------------------------------------------------------------
  38. */
  39. #include "hal_board_cfg.h"
  40. #include "hal_adc.h"
  41. #include "hal_dma.h"
  42. #include "hal_flash.h"
  43. #include "hal_types.h"
  44. #include "sb_exec.h"
  45. #include "sb_main.h"
  46. /* ------------------------------------------------------------------------------------------------
  47. * Constants
  48. * ------------------------------------------------------------------------------------------------
  49. */
  50. /* Delay jump to valid RC code, waiting for a force boot or force run indication via the
  51. * physical transport or button press indication. Set to zero to jump immediately, this
  52. * necessitates the RC to invalidate checksum/shadow to force boot mode.
  53. */
  54. #if !defined SB_UART_DELAY
  55. #define SB_UART_DELAY 0x260000 // About 1 minute.
  56. #endif
  57. /* ------------------------------------------------------------------------------------------------
  58. * Macros
  59. * ------------------------------------------------------------------------------------------------
  60. */
  61. #if !defined ResetWasWatchDog
  62. #define ResetWasWatchDog ((SLEEPSTA & 0x18) == 0x10)
  63. #endif
  64. /* ------------------------------------------------------------------------------------------------
  65. * Global Variables
  66. * ------------------------------------------------------------------------------------------------
  67. */
  68. halDMADesc_t dmaCh0;
  69. /* ------------------------------------------------------------------------------------------------
  70. * Local Variables
  71. * ------------------------------------------------------------------------------------------------
  72. */
  73. static uint8 znpCfg1;
  74. static uint8 spiPoll;
  75. /* ------------------------------------------------------------------------------------------------
  76. * Local Functions
  77. * ------------------------------------------------------------------------------------------------
  78. */
  79. static void sblExec(void);
  80. static void sblInit(void);
  81. static void sblJump(void);
  82. static void sblWait(void);
  83. static void vddWait(uint8 vdd);
  84. #include "_hal_uart_isr.c"
  85. #include "_hal_uart_spi.c"
  86. /**************************************************************************************************
  87. * @fn main
  88. *
  89. * @brief C-code main functionality.
  90. *
  91. * input parameters
  92. *
  93. * None.
  94. *
  95. * output parameters
  96. *
  97. * None.
  98. *
  99. * @return None.
  100. **************************************************************************************************
  101. */
  102. void main(void)
  103. {
  104. vddWait(VDD_MIN_RUN);
  105. HAL_BOARD_INIT();
  106. if (sbImgValid())
  107. {
  108. if ((SB_UART_DELAY == 0) || ResetWasWatchDog)
  109. {
  110. sblJump();
  111. }
  112. sblInit();
  113. sblWait();
  114. }
  115. else
  116. {
  117. sblInit();
  118. }
  119. vddWait(VDD_MIN_NV);
  120. sblExec();
  121. HAL_SYSTEM_RESET();
  122. }
  123. /**************************************************************************************************
  124. * @fn sblExec
  125. *
  126. * @brief Infinite SBL execute loop that jumps upon receiving a code enable.
  127. *
  128. * input parameters
  129. *
  130. * None.
  131. *
  132. * output parameters
  133. *
  134. * None.
  135. *
  136. * @return None.
  137. **************************************************************************************************
  138. */
  139. static void sblExec(void)
  140. {
  141. uint32 dlyCnt = 0;
  142. while (1)
  143. {
  144. if (znpCfg1 == ZNP_CFG1_UART)
  145. {
  146. HalUARTPollISR();
  147. }
  148. if (sbExec() && sbImgValid())
  149. {
  150. // Delay to allow the SB_ENABLE_CMD response to be flushed.
  151. if (znpCfg1 == ZNP_CFG1_UART)
  152. {
  153. for (dlyCnt = 0; dlyCnt < 0x40000; dlyCnt++)
  154. {
  155. HalUARTPollISR();
  156. }
  157. }
  158. sblJump();
  159. }
  160. }
  161. }
  162. /**************************************************************************************************
  163. * @fn sblInit
  164. *
  165. * @brief SBL initialization.
  166. *
  167. * input parameters
  168. *
  169. * None.
  170. *
  171. * output parameters
  172. *
  173. * None.
  174. *
  175. * @return None.
  176. */
  177. static void sblInit(void)
  178. {
  179. #if defined CC2530_MK
  180. znpCfg1 = ZNP_CFG1_SPI;
  181. #else
  182. znpCfg1 = P2_0;
  183. #endif
  184. /* This is in place of calling HalDmaInit() which would require init of the other 4 DMA
  185. * descriptors in addition to just Channel 0.
  186. */
  187. HAL_DMA_SET_ADDR_DESC0(&dmaCh0);
  188. if (znpCfg1 == ZNP_CFG1_SPI)
  189. {
  190. SRDY_CLR();
  191. // Select general purpose on I/O pins.
  192. P0SEL &= ~(NP_RDYIn_BIT); // P0.3 MRDY - GPIO
  193. P0SEL &= ~(NP_RDYOut_BIT); // P0.4 SRDY - GPIO
  194. // Select GPIO direction.
  195. P0DIR &= ~NP_RDYIn_BIT; // P0.3 MRDY - IN
  196. P0DIR |= NP_RDYOut_BIT; // P0.4 SRDY - OUT
  197. P0INP &= ~NP_RDYIn_BIT; // Pullup/down enable of MRDY input.
  198. P2INP &= ~BV(5); // Pullup all P0 inputs.
  199. HalUARTInitSPI();
  200. }
  201. else
  202. {
  203. halUARTCfg_t uartConfig;
  204. HalUARTInitISR();
  205. uartConfig.configured = TRUE;
  206. uartConfig.baudRate = HAL_UART_BR_115200;
  207. uartConfig.flowControl = FALSE;
  208. uartConfig.flowControlThreshold = 0; // CC2530 by #define - see hal_board_cfg.h
  209. uartConfig.rx.maxBufSize = 0; // CC2530 by #define - see hal_board_cfg.h
  210. uartConfig.tx.maxBufSize = 0; // CC2530 by #define - see hal_board_cfg.h
  211. uartConfig.idleTimeout = 0; // CC2530 by #define - see hal_board_cfg.h
  212. uartConfig.intEnable = TRUE;
  213. uartConfig.callBackFunc = NULL;
  214. HalUARTOpenISR(&uartConfig);
  215. }
  216. }
  217. /**************************************************************************************************
  218. * @fn sblJump
  219. *
  220. * @brief Execute a simple long jump from non-banked SBL code to non-banked RC code space.
  221. *
  222. * input parameters
  223. *
  224. * None.
  225. *
  226. * output parameters
  227. *
  228. * None.
  229. *
  230. * @return None.
  231. */
  232. static void sblJump(void)
  233. {
  234. asm("LJMP 0x2000\n"); // Immediate jump to run-code.
  235. HAL_SYSTEM_RESET();
  236. }
  237. /**************************************************************************************************
  238. * @fn sblWait
  239. *
  240. * @brief A timed-out wait loop that exits early upon receiving a force code/sbl byte.
  241. *
  242. * input parameters
  243. *
  244. * None.
  245. *
  246. * output parameters
  247. *
  248. * None.
  249. *
  250. * @return None.
  251. **************************************************************************************************
  252. */
  253. static void sblWait(void)
  254. {
  255. uint32 dlyCnt;
  256. if (znpCfg1 == ZNP_CFG1_SPI)
  257. {
  258. // Slave signals ready for read by setting its ready flag first.
  259. SRDY_SET();
  260. // Flag to sbRx() to poll for 1 Rx byte instead of blocking read until MRDY_CLR.
  261. spiPoll = TRUE;
  262. dlyCnt = 0x38; // About 50 msecs.
  263. }
  264. else
  265. {
  266. dlyCnt = SB_UART_DELAY;
  267. }
  268. while (--dlyCnt)
  269. {
  270. uint8 ch;
  271. if (znpCfg1 == ZNP_CFG1_UART)
  272. {
  273. HalUARTPollISR();
  274. }
  275. if (sbRx(&ch, 1))
  276. {
  277. if (ch == SB_FORCE_BOOT)
  278. {
  279. break;
  280. }
  281. else if (ch == SB_FORCE_RUN)
  282. {
  283. dlyCnt = 0;
  284. }
  285. }
  286. }
  287. if (znpCfg1 == ZNP_CFG1_SPI)
  288. {
  289. // Master blocks waiting for slave to clear its ready flag before continuing.
  290. SRDY_CLR();
  291. // Flag to sbRx() to now block while reading Rx bytes until MRDY_CLR.
  292. spiPoll = FALSE;
  293. }
  294. if (dlyCnt == 0)
  295. {
  296. sblJump();
  297. }
  298. }
  299. /**************************************************************************************************
  300. * @fn sbRx
  301. *
  302. * @brief Serial Boot loader read API that makes the low-level read according to RPC mode.
  303. *
  304. * input parameters
  305. *
  306. * @param buf - Pointer to a buffer to fill with up to 'len' bytes.
  307. * @param len - Maximum count of bytes to fill into the 'buf'.
  308. *
  309. *
  310. * output parameters
  311. *
  312. * None.
  313. *
  314. * @return The count of the number of bytes filled into the 'buf'.
  315. **************************************************************************************************
  316. */
  317. uint16 sbRx(uint8 *buf, uint16 len)
  318. {
  319. if (znpCfg1 == ZNP_CFG1_UART)
  320. {
  321. return HalUARTReadISR(buf, len);
  322. }
  323. else
  324. {
  325. if (spiPoll)
  326. {
  327. if (URXxIF)
  328. {
  329. *buf = UxDBUF;
  330. URXxIF = 0;
  331. return 1;
  332. }
  333. else
  334. {
  335. return 0;
  336. }
  337. }
  338. else
  339. {
  340. return HalUARTReadSPI(buf, len);
  341. }
  342. }
  343. }
  344. /**************************************************************************************************
  345. * @fn sbTx
  346. *
  347. * @brief Serial Boot loader write API that makes the low-level write according to RPC mode.
  348. *
  349. * input parameters
  350. *
  351. * @param buf - Pointer to a buffer of 'len' bytes to write to the serial transport.
  352. * @param len - Length in bytes of the 'buf'.
  353. *
  354. *
  355. * output parameters
  356. *
  357. * None.
  358. *
  359. * @return The count of the number of bytes written from the 'buf'.
  360. **************************************************************************************************
  361. */
  362. uint16 sbTx(uint8 *buf, uint16 len)
  363. {
  364. if (znpCfg1 == ZNP_CFG1_UART)
  365. {
  366. return HalUARTWriteISR(buf, len);
  367. }
  368. else
  369. {
  370. return HalUARTWriteSPI(buf, len);
  371. }
  372. }
  373. /**************************************************************************************************
  374. * @fn vddWait
  375. *
  376. * @brief Loop waiting for 16 reads of the Vdd over the requested limit.
  377. *
  378. * input parameters
  379. *
  380. * @param vdd - Vdd level to wait for.
  381. *
  382. * output parameters
  383. *
  384. * None.
  385. *
  386. * @return None.
  387. **************************************************************************************************
  388. */
  389. static void vddWait(uint8 vdd)
  390. {
  391. uint8 cnt = 16;
  392. do {
  393. do {
  394. ADCCON3 = 0x0F;
  395. while (!(ADCCON1 & 0x80));
  396. } while (ADCH < vdd);
  397. } while (--cnt);
  398. }
  399. /**************************************************************************************************
  400. */