sb_main.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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 CC2530EB 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 HAL_KEY
  62. #define SB1_PRESS (P0_1 != 0)
  63. #define SB2_PRESS (P2_0 != 0)
  64. #else
  65. #define SB1_PRESS 0
  66. #define SB2_PRESS 0
  67. #endif
  68. #if HAL_LED
  69. #define SB_INIT_LEDS() st ( \
  70. HAL_TURN_OFF_LED1(); \
  71. LED1_DDR |= LED1_BV; \
  72. HAL_TURN_OFF_LED2(); \
  73. LED2_DDR |= LED2_BV; \
  74. )
  75. #define SB_TURN_OFF_LED1() HAL_TURN_OFF_LED1()
  76. #define SB_TURN_ON_LED1() HAL_TURN_ON_LED1()
  77. #define SB_TOGGLE_LED1() HAL_TOGGLE_LED1()
  78. #define SB_TURN_OFF_LED2() HAL_TURN_OFF_LED2()
  79. #define SB_TURN_ON_LED2() HAL_TURN_ON_LED2()
  80. #define SB_TOGGLE_LED2() HAL_TOGGLE_LED2()
  81. #else
  82. #define SB_TURN_OFF_LED1()
  83. #define SB_TURN_ON_LED1()
  84. #define SB_TOGGLE_LED1()
  85. #define SB_TURN_OFF_LED2()
  86. #define SB_TURN_ON_LED2()
  87. #define SB_TOGGLE_LED2()
  88. #endif
  89. #if !defined ResetWasWatchDog
  90. #define ResetWasWatchDog ((SLEEPSTA & 0x18) == 0x10)
  91. #endif
  92. /* ------------------------------------------------------------------------------------------------
  93. * Global Variables
  94. * ------------------------------------------------------------------------------------------------
  95. */
  96. halDMADesc_t dmaCh0;
  97. /* ------------------------------------------------------------------------------------------------
  98. * Local Functions
  99. * ------------------------------------------------------------------------------------------------
  100. */
  101. static void sblExec(void);
  102. static void sblInit(void);
  103. static void sblJump(void);
  104. static void sblWait(void);
  105. static void vddWait(uint8 vdd);
  106. #include "_hal_uart_isr.c"
  107. /**************************************************************************************************
  108. * @fn main
  109. *
  110. * @brief C-code main functionality.
  111. *
  112. * input parameters
  113. *
  114. * None.
  115. *
  116. * output parameters
  117. *
  118. * None.
  119. *
  120. * @return None.
  121. **************************************************************************************************
  122. */
  123. void main(void)
  124. {
  125. vddWait(VDD_MIN_RUN);
  126. HAL_BOARD_INIT();
  127. if (sbImgValid())
  128. {
  129. if ((SB_UART_DELAY == 0) || ResetWasWatchDog)
  130. {
  131. sblJump();
  132. }
  133. sblInit();
  134. sblWait();
  135. }
  136. else
  137. {
  138. sblInit();
  139. }
  140. vddWait(VDD_MIN_NV);
  141. sblExec();
  142. HAL_SYSTEM_RESET();
  143. }
  144. /**************************************************************************************************
  145. * @fn sblExec
  146. *
  147. * @brief Infinite SBL execute loop that jumps upon receiving a code enable.
  148. *
  149. * input parameters
  150. *
  151. * None.
  152. *
  153. * output parameters
  154. *
  155. * None.
  156. *
  157. * @return None.
  158. **************************************************************************************************
  159. */
  160. static void sblExec(void)
  161. {
  162. uint32 dlyCnt = 0;
  163. while (1)
  164. {
  165. HalUARTPollISR();
  166. if (sbExec() && sbImgValid())
  167. {
  168. SB_TURN_ON_LED1();
  169. SB_TURN_ON_LED2();
  170. // Delay to allow the SB_ENABLE_CMD response to be flushed.
  171. for (dlyCnt = 0; dlyCnt < 0x40000; dlyCnt++)
  172. {
  173. HalUARTPollISR();
  174. }
  175. sblJump();
  176. }
  177. else if (dlyCnt++ & 0x4000)
  178. {
  179. SB_TOGGLE_LED1();
  180. }
  181. }
  182. }
  183. /**************************************************************************************************
  184. * @fn sblInit
  185. *
  186. * @brief SBL initialization.
  187. *
  188. * input parameters
  189. *
  190. * None.
  191. *
  192. * output parameters
  193. *
  194. * None.
  195. *
  196. * @return None.
  197. */
  198. static void sblInit(void)
  199. {
  200. halUARTCfg_t uartConfig;
  201. /* This is in place of calling HalDmaInit() which would require init of the other 4 DMA
  202. * descriptors in addition to just Channel 0.
  203. */
  204. HAL_DMA_SET_ADDR_DESC0(&dmaCh0);
  205. HalUARTInitISR();
  206. uartConfig.configured = TRUE;
  207. uartConfig.baudRate = HAL_UART_BR_115200;
  208. uartConfig.flowControl = FALSE;
  209. uartConfig.flowControlThreshold = 0; // CC2530 by #define - see hal_board_cfg.h
  210. uartConfig.rx.maxBufSize = 0; // CC2530 by #define - see hal_board_cfg.h
  211. uartConfig.tx.maxBufSize = 0; // CC2530 by #define - see hal_board_cfg.h
  212. uartConfig.idleTimeout = 0; // CC2530 by #define - see hal_board_cfg.h
  213. uartConfig.intEnable = TRUE;
  214. uartConfig.callBackFunc = NULL;
  215. HalUARTOpenISR(&uartConfig);
  216. SB_INIT_LEDS();
  217. }
  218. /**************************************************************************************************
  219. * @fn sblJump
  220. *
  221. * @brief Execute a simple long jump from non-banked SBL code to non-banked RC code space.
  222. *
  223. * input parameters
  224. *
  225. * None.
  226. *
  227. * output parameters
  228. *
  229. * None.
  230. *
  231. * @return None.
  232. */
  233. static void sblJump(void)
  234. {
  235. SB_TURN_ON_LED1();
  236. SB_TURN_ON_LED2();
  237. while (SB1_PRESS || SB2_PRESS);
  238. SB_TURN_OFF_LED1();
  239. SB_TURN_OFF_LED2();
  240. asm("LJMP 0x2000\n"); // Immediate jump to run-code.
  241. HAL_SYSTEM_RESET();
  242. }
  243. /**************************************************************************************************
  244. * @fn sblWait
  245. *
  246. * @brief A timed-out wait loop that exits early upon receiving a force code/sbl byte.
  247. *
  248. * input parameters
  249. *
  250. * None.
  251. *
  252. * output parameters
  253. *
  254. * None.
  255. *
  256. * @return None.
  257. **************************************************************************************************
  258. */
  259. static void sblWait(void)
  260. {
  261. uint32 dlyCnt = SB_UART_DELAY;
  262. while (1)
  263. {
  264. uint8 ch;
  265. HalUARTPollISR();
  266. if (HalUARTReadISR(&ch, 1))
  267. {
  268. if (ch == SB_FORCE_BOOT)
  269. {
  270. break;
  271. }
  272. else if (ch == SB_FORCE_RUN)
  273. {
  274. dlyCnt = 0;
  275. }
  276. }
  277. if (SB1_PRESS)
  278. {
  279. break;
  280. }
  281. if (SB2_PRESS || (dlyCnt-- == 0))
  282. {
  283. sblJump();
  284. }
  285. // RR-xing LED display while waiting.
  286. if (dlyCnt & 0x2000)
  287. {
  288. SB_TURN_OFF_LED2();
  289. SB_TURN_ON_LED1();
  290. }
  291. else
  292. {
  293. SB_TURN_OFF_LED1();
  294. SB_TURN_ON_LED2();
  295. }
  296. }
  297. SB_TURN_OFF_LED1();
  298. SB_TURN_OFF_LED2();
  299. }
  300. /**************************************************************************************************
  301. * @fn vddWait
  302. *
  303. * @brief Loop waiting for 16 reads of the Vdd over the requested limit.
  304. *
  305. * input parameters
  306. *
  307. * @param vdd - Vdd level to wait for.
  308. *
  309. * output parameters
  310. *
  311. * None.
  312. *
  313. * @return None.
  314. **************************************************************************************************
  315. */
  316. static void vddWait(uint8 vdd)
  317. {
  318. uint8 cnt = 16;
  319. do {
  320. do {
  321. ADCCON3 = 0x0F;
  322. while (!(ADCCON1 & 0x80));
  323. } while (ADCH < vdd);
  324. } while (--cnt);
  325. }
  326. /**************************************************************************************************
  327. */