sb_main.c 9.5 KB

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