OnBoard.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /**************************************************************************************************
  2. Filename: OnBoard.c
  3. Revised: $Date: 2012-03-29 12:09:02 -0700 (Thu, 29 Mar 2012) $
  4. Revision: $Revision: 29943 $
  5. Description: This file contains the UI and control for the
  6. peripherals on the EVAL development board
  7. Notes: This file targets the Chipcon CC2530/31
  8. Copyright 2005-2010 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. #include "ZComDef.h"
  39. #include "ZGlobals.h"
  40. #include "OnBoard.h"
  41. #include "OSAL.h"
  42. #include "MT.h"
  43. #include "MT_SYS.h"
  44. #include "DebugTrace.h"
  45. /* Hal */
  46. #include "hal_lcd.h"
  47. #include "hal_mcu.h"
  48. #include "hal_timer.h"
  49. #include "hal_key.h"
  50. #include "hal_led.h"
  51. /* Allow access macRandomByte() */
  52. #include "mac_radio_defs.h"
  53. /*********************************************************************
  54. * CONSTANTS
  55. */
  56. // Task ID not initialized
  57. #define NO_TASK_ID 0xFF
  58. // Minimum length RAM "pattern" for Stack check
  59. #define MIN_RAM_INIT 12
  60. /*********************************************************************
  61. * GLOBAL VARIABLES
  62. */
  63. #if defined MAKE_CRC_SHDW
  64. #pragma location="CRC_SHDW"
  65. const CODE uint16 _crcShdw = 0xFFFF;
  66. #pragma required=_crcShdw
  67. #else // if !defined MAKE_CRC_SHDW
  68. #pragma location="LOCK_BITS_ADDRESS_SPACE"
  69. __no_init uint8 _lockBits[16];
  70. #pragma required=_lockBits
  71. #if defined ZCL_KEY_ESTABLISH
  72. #include "zcl_cert_data.c"
  73. #else
  74. #pragma location="IEEE_ADDRESS_SPACE"
  75. __no_init uint8 _nvIEEE[Z_EXTADDR_LEN];
  76. #pragma required=_nvIEEE
  77. #endif
  78. #pragma location="RESERVED_ADDRESS_SPACE"
  79. __no_init uint8 _reserved[1932];
  80. #pragma required=_reserved
  81. #endif
  82. // 64-bit Extended Address of this device
  83. uint8 aExtendedAddress[8];
  84. /*********************************************************************
  85. * LOCAL VARIABLES
  86. */
  87. // Registered keys task ID, initialized to NOT USED.
  88. //static uint8 registeredKeysTaskID = NO_TASK_ID;
  89. /*********************************************************************
  90. * LOCAL FUNCTIONS
  91. */
  92. static void ChkReset( void );
  93. /*********************************************************************
  94. * @fn InitBoard()
  95. * @brief Initialize the CC2420DB Board Peripherals
  96. * @param level: COLD,WARM,READY
  97. * @return None
  98. */
  99. void InitBoard( uint8 level )
  100. {
  101. if ( level == OB_COLD )
  102. {
  103. // IAR does not zero-out this byte below the XSTACK.
  104. *(uint8 *)0x0 = 0;
  105. // Interrupts off
  106. osal_int_disable( INTS_ALL );
  107. // Check for Brown-Out reset
  108. ChkReset();
  109. }
  110. else // !OB_COLD
  111. {
  112. /* Initialize Key stuff */
  113. //HalKeyConfig(HAL_KEY_INTERRUPT_DISABLE, OnBoard_KeyCallback);
  114. }
  115. }
  116. /*********************************************************************
  117. * @fn ChkReset()
  118. * @brief Check reset bits - if reset cause is unknown, assume a
  119. * brown-out (low power), assume batteries are not reliable,
  120. * hang in a loop and sequence through the LEDs.
  121. * @param None
  122. * @return None
  123. *********************************************************************/
  124. void ChkReset( void )
  125. {
  126. uint8 rib;
  127. // Isolate reset indicator bits
  128. rib = SLEEPSTA & LRESET;
  129. if ( rib == RESETPO )
  130. {
  131. // Put code here to handle Power-On reset
  132. }
  133. else if ( rib == RESETEX )
  134. {
  135. // Put code here to handle External reset
  136. }
  137. else if ( rib == RESETWD )
  138. {
  139. // Put code here to handle WatchDog reset
  140. }
  141. else // Unknown reason - not expected.
  142. {
  143. HAL_ASSERT(0);
  144. }
  145. }
  146. /*********************************************************************
  147. * "Keyboard" Support
  148. *********************************************************************/
  149. ///*********************************************************************
  150. // * Keyboard Register function
  151. // *
  152. // * The keyboard handler is setup to send all keyboard changes to
  153. // * one task (if a task is registered).
  154. // *
  155. // * If a task registers, it will get all the keys. You can change this
  156. // * to register for individual keys.
  157. // *********************************************************************/
  158. //uint8 RegisterForKeys( uint8 task_id )
  159. //{
  160. // // Allow only the first task
  161. // if ( registeredKeysTaskID == NO_TASK_ID )
  162. // {
  163. // registeredKeysTaskID = task_id;
  164. // return ( true );
  165. // }
  166. // else
  167. // return ( false );
  168. //}
  169. //
  170. ///*********************************************************************
  171. // * @fn OnBoard_SendKeys
  172. // *
  173. // * @brief Send "Key Pressed" message to application.
  174. // *
  175. // * @param keys - keys that were pressed
  176. // * state - shifted
  177. // *
  178. // * @return status
  179. // *********************************************************************/
  180. //uint8 OnBoard_SendKeys( uint8 keys, uint8 state )
  181. //{
  182. // keyChange_t *msgPtr;
  183. //
  184. // if ( registeredKeysTaskID != NO_TASK_ID )
  185. // {
  186. // // Send the address to the task
  187. // msgPtr = (keyChange_t *)osal_msg_allocate( sizeof(keyChange_t) );
  188. // if ( msgPtr )
  189. // {
  190. // msgPtr->hdr.event = KEY_CHANGE;
  191. // msgPtr->state = state;
  192. // msgPtr->keys = keys;
  193. //
  194. // osal_msg_send( registeredKeysTaskID, (uint8 *)msgPtr );
  195. // }
  196. // return ( ZSuccess );
  197. // }
  198. // else
  199. // return ( ZFailure );
  200. //}
  201. //
  202. ///*********************************************************************
  203. // * @fn OnBoard_KeyCallback
  204. // *
  205. // * @brief Callback service for keys
  206. // *
  207. // * @param keys - keys that were pressed
  208. // * state - shifted
  209. // *
  210. // * @return void
  211. // *********************************************************************/
  212. //void OnBoard_KeyCallback ( uint8 keys, uint8 state )
  213. //{
  214. // uint8 shift;
  215. // (void)state;
  216. //
  217. // shift = (keys & HAL_KEY_SW_6) ? true : false;
  218. //
  219. // if ( OnBoard_SendKeys( keys, shift ) != ZSuccess )
  220. // {
  221. // // Process SW1 here
  222. // if ( keys & HAL_KEY_SW_1 ) // Switch 1
  223. // {
  224. // }
  225. // // Process SW2 here
  226. // if ( keys & HAL_KEY_SW_2 ) // Switch 2
  227. // {
  228. // }
  229. // // Process SW3 here
  230. // if ( keys & HAL_KEY_SW_3 ) // Switch 3
  231. // {
  232. // }
  233. // // Process SW4 here
  234. // if ( keys & HAL_KEY_SW_4 ) // Switch 4
  235. // {
  236. // }
  237. // // Process SW5 here
  238. // if ( keys & HAL_KEY_SW_5 ) // Switch 5
  239. // {
  240. // }
  241. // // Process SW6 here
  242. // if ( keys & HAL_KEY_SW_6 ) // Switch 6
  243. // {
  244. // }
  245. // }
  246. //}
  247. /*********************************************************************
  248. * @fn OnBoard_stack_used
  249. *
  250. * @brief Runs through the stack looking for touched memory.
  251. *
  252. * @param none
  253. *
  254. * @return Maximum number of bytes used by the stack.
  255. *********************************************************************/
  256. uint16 OnBoard_stack_used(void)
  257. {
  258. uint8 const *ptr;
  259. uint8 cnt = 0;
  260. for (ptr = CSTACK_END; ptr > CSTACK_BEG; ptr--)
  261. {
  262. if (STACK_INIT_VALUE == *ptr)
  263. {
  264. if (++cnt >= MIN_RAM_INIT)
  265. {
  266. ptr += MIN_RAM_INIT;
  267. break;
  268. }
  269. }
  270. else
  271. {
  272. cnt = 0;
  273. }
  274. }
  275. return (uint16)(CSTACK_END - ptr + 1);
  276. }
  277. /*********************************************************************
  278. * @fn _itoa
  279. *
  280. * @brief convert a 16bit number to ASCII
  281. *
  282. * @param num -
  283. * buf -
  284. * radix -
  285. *
  286. * @return void
  287. *
  288. *********************************************************************/
  289. void _itoa(uint16 num, uint8 *buf, uint8 radix)
  290. {
  291. char c,i;
  292. uint8 *p, rst[5];
  293. p = rst;
  294. for ( i=0; i<5; i++,p++ )
  295. {
  296. c = num % radix; // Isolate a digit
  297. *p = c + (( c < 10 ) ? '0' : '7'); // Convert to Ascii
  298. num /= radix;
  299. if ( !num )
  300. break;
  301. }
  302. for ( c=0 ; c<=i; c++ )
  303. *buf++ = *p--; // Reverse character order
  304. *buf = '\0';
  305. }
  306. /*********************************************************************
  307. * @fn Onboard_rand
  308. *
  309. * @brief Random number generator
  310. *
  311. * @param none
  312. *
  313. * @return uint16 - new random number
  314. *
  315. *********************************************************************/
  316. uint16 Onboard_rand( void )
  317. {
  318. return ( MAC_RADIO_RANDOM_WORD() );
  319. }
  320. /*********************************************************************
  321. * @fn Onboard_wait
  322. *
  323. * @brief Delay wait
  324. *
  325. * @param uint16 - time to wait
  326. *
  327. * @return none
  328. *
  329. *********************************************************************/
  330. void Onboard_wait( uint16 timeout )
  331. {
  332. while (timeout--)
  333. {
  334. asm("NOP");
  335. asm("NOP");
  336. asm("NOP");
  337. }
  338. }
  339. /*********************************************************************
  340. * @fn Onboard_soft_reset
  341. *
  342. * @brief Effect a soft reset.
  343. *
  344. * @param none
  345. *
  346. * @return none
  347. *
  348. *********************************************************************/
  349. __near_func void Onboard_soft_reset( void )
  350. {
  351. HAL_DISABLE_INTERRUPTS();
  352. // Abort all DMA channels to insure that ongoing operations do not
  353. // interfere with re-configuration.
  354. DMAARM = 0x80 | 0x1F;
  355. asm("LJMP 0x0");
  356. }
  357. /*********************************************************************
  358. * EXTERNAL I/O FUNCTIONS
  359. *
  360. * User defined functions to control external devices. Add your code
  361. * to the following functions to control devices wired to DB outputs.
  362. *
  363. *********************************************************************/
  364. void BigLight_On( void )
  365. {
  366. // Put code here to turn on an external light
  367. }
  368. void BigLight_Off( void )
  369. {
  370. // Put code here to turn off an external light
  371. }
  372. void BuzzerControl( uint8 on )
  373. {
  374. // Put code here to turn a buzzer on/off
  375. (void)on;
  376. }
  377. void Dimmer( uint8 lvl )
  378. {
  379. // Put code here to control a dimmer
  380. (void)lvl;
  381. }
  382. // No dip switches on this board
  383. uint8 GetUserDipSw( void )
  384. {
  385. return 0;
  386. }
  387. /*********************************************************************
  388. *********************************************************************/