OnBoard.c 12 KB

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