OnBoard.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. /**************************************************************************************************
  2. Filename: OnBoard.c
  3. Revised: $Date: 2009-03-12 16:26:27 -0700 (Thu, 12 Mar 2009) $
  4. Revision: $Revision: 19405 $
  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
  8. Copyright 2005-2008 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 "OnBoard.h"
  40. #include "OSAL.h"
  41. #include "MT.h"
  42. #include "MT_SYS.h"
  43. #include "DebugTrace.h"
  44. /* Hal */
  45. #include "hal_lcd.h"
  46. #include "hal_mcu.h"
  47. #include "hal_timer.h"
  48. #include "hal_key.h"
  49. #include "hal_led.h"
  50. /*********************************************************************
  51. * MACROS
  52. */
  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. * TYPEDEFS
  62. */
  63. /*********************************************************************
  64. * GLOBAL VARIABLES
  65. */
  66. uint8 OnboardKeyIntEnable;
  67. // 64-bit Extended Address of this device
  68. uint8 aExtendedAddress[8];
  69. /*********************************************************************
  70. * EXTERNAL VARIABLES
  71. */
  72. /*********************************************************************
  73. * EXTERNAL FUNCTIONS
  74. */
  75. extern uint8 macMcuRandomByte(void);
  76. /*********************************************************************
  77. * LOCAL VARIABLES
  78. */
  79. // Registered keys task ID, initialized to NOT USED.
  80. static byte registeredKeysTaskID = NO_TASK_ID;
  81. /*********************************************************************
  82. * LOCAL FUNCTIONS
  83. */
  84. static void ChkReset( void );
  85. /*********************************************************************
  86. * @fn InitBoard()
  87. * @brief Initialize the CC2420DB Board Peripherals
  88. * @param level: COLD,WARM,READY
  89. * @return None
  90. */
  91. void InitBoard( byte level )
  92. {
  93. if ( level == OB_COLD )
  94. {
  95. // Interrupts off
  96. osal_int_disable( INTS_ALL );
  97. // Turn all LEDs off
  98. HalLedSet( HAL_LED_ALL, HAL_LED_MODE_OFF );
  99. // Check for Brown-Out reset
  100. ChkReset();
  101. }
  102. else // !OB_COLD
  103. {
  104. #ifdef ZTOOL_PORT
  105. MT_SysResetInd();
  106. #endif
  107. /* Initialize Key stuff */
  108. OnboardKeyIntEnable = HAL_KEY_INTERRUPT_DISABLE;
  109. HalKeyConfig( OnboardKeyIntEnable, OnBoard_KeyCallback);
  110. }
  111. }
  112. /*********************************************************************
  113. * @fn ChkReset()
  114. * @brief Check reset bits - if reset cause is unknown, assume a
  115. * brown-out (low power), assume batteries are not reliable,
  116. * hang in a loop and sequence through the LEDs.
  117. * @param None
  118. * @return None
  119. *********************************************************************/
  120. void ChkReset( void )
  121. {
  122. uint8 led;
  123. uint8 rib;
  124. // Isolate reset indicator bits
  125. rib = SLEEPSTA & LRESET;
  126. if ( rib == RESETPO )
  127. {
  128. // Put code here to handle Power-On reset
  129. }
  130. else if ( rib == RESETEX )
  131. {
  132. // Put code here to handle External reset
  133. }
  134. else if ( rib == RESETWD )
  135. {
  136. // Put code here to handle WatchDog reset
  137. }
  138. else
  139. {
  140. // Unknown, hang and blink
  141. HAL_DISABLE_INTERRUPTS();
  142. led = HAL_LED_4;
  143. while ( 1 ) {
  144. HalLedSet( led, HAL_LED_MODE_ON );
  145. MicroWait( 62500 );
  146. MicroWait( 62500 );
  147. HalLedSet( led, HAL_LED_MODE_OFF );
  148. MicroWait( 37500 );
  149. MicroWait( 37500 );
  150. if ( !(led >>= 1) )
  151. led = HAL_LED_4;
  152. }
  153. }
  154. }
  155. /*********************************************************************
  156. * "Keyboard" Support
  157. *********************************************************************/
  158. /*********************************************************************
  159. * Keyboard Register function
  160. *
  161. * The keyboard handler is setup to send all keyboard changes to
  162. * one task (if a task is registered).
  163. *
  164. * If a task registers, it will get all the keys. You can change this
  165. * to register for individual keys.
  166. *********************************************************************/
  167. byte RegisterForKeys( byte task_id )
  168. {
  169. // Allow only the first task
  170. if ( registeredKeysTaskID == NO_TASK_ID )
  171. {
  172. registeredKeysTaskID = task_id;
  173. return ( true );
  174. }
  175. else
  176. return ( false );
  177. }
  178. /*********************************************************************
  179. * @fn OnBoard_SendKeys
  180. *
  181. * @brief Send "Key Pressed" message to application.
  182. *
  183. * @param keys - keys that were pressed
  184. * state - shifted
  185. *
  186. * @return status
  187. *********************************************************************/
  188. byte OnBoard_SendKeys( byte keys, byte state )
  189. {
  190. keyChange_t *msgPtr;
  191. if ( registeredKeysTaskID != NO_TASK_ID )
  192. {
  193. // Send the address to the task
  194. msgPtr = (keyChange_t *)osal_msg_allocate( sizeof(keyChange_t) );
  195. if ( msgPtr )
  196. {
  197. msgPtr->hdr.event = KEY_CHANGE;
  198. msgPtr->state = state;
  199. msgPtr->keys = keys;
  200. osal_msg_send( registeredKeysTaskID, (uint8 *)msgPtr );
  201. }
  202. return ( ZSuccess );
  203. }
  204. else
  205. return ( ZFailure );
  206. }
  207. /*********************************************************************
  208. * @fn OnBoard_KeyCallback
  209. *
  210. * @brief Callback service for keys
  211. *
  212. * @param keys - keys that were pressed
  213. * state - shifted
  214. *
  215. * @return void
  216. *********************************************************************/
  217. void OnBoard_KeyCallback ( uint8 keys, uint8 state )
  218. {
  219. uint8 shift;
  220. (void)state; // Intentionally unreferenced parameter
  221. // shift key (S1) is used to generate key interrupt
  222. // applications should not use S1 when key interrupt is enabled
  223. shift = (OnboardKeyIntEnable == HAL_KEY_INTERRUPT_ENABLE) ? false : ((keys & HAL_KEY_SW_6) ? true : false);
  224. if ( OnBoard_SendKeys( keys, shift ) != ZSuccess )
  225. {
  226. // Process SW1 here
  227. if ( keys & HAL_KEY_SW_1 ) // Switch 1
  228. {
  229. }
  230. // Process SW2 here
  231. if ( keys & HAL_KEY_SW_2 ) // Switch 2
  232. {
  233. }
  234. // Process SW3 here
  235. if ( keys & HAL_KEY_SW_3 ) // Switch 3
  236. {
  237. }
  238. // Process SW4 here
  239. if ( keys & HAL_KEY_SW_4 ) // Switch 4
  240. {
  241. }
  242. // Process SW5 here
  243. if ( keys & HAL_KEY_SW_5 ) // Switch 5
  244. {
  245. }
  246. // Process SW6 here
  247. if ( keys & HAL_KEY_SW_6 ) // Switch 6
  248. {
  249. }
  250. }
  251. }
  252. /*********************************************************************
  253. * SLEEP MANAGEMENT FUNCTIONS
  254. *
  255. * These functions support processing of MAC and ZStack power mode
  256. * transitions, used when the system goes into or awakes from sleep.
  257. */
  258. /*********************************************************************
  259. * @fn OnBoard_stack_used()
  260. *
  261. * @brief
  262. *
  263. * Runs through the stack looking for touched memory.
  264. *
  265. * @param none
  266. *
  267. * @return number of bytes used by the stack
  268. *********************************************************************/
  269. uint16 OnBoard_stack_used( void )
  270. {
  271. byte *pStack = (byte*)MCU_RAM_END;
  272. byte *pHold;
  273. byte found = false;
  274. byte x;
  275. // Look from the end of RAM for MIN_RAM_INIT number of "pattern" bytes
  276. // This should be the high water stack mark.
  277. while ( !found && pStack )
  278. {
  279. // Found an init value?
  280. if ( *pStack == STACK_INIT_VALUE )
  281. {
  282. // Look for a bunch in a row
  283. pHold = pStack;
  284. for ( x = 0; x < MIN_RAM_INIT; x++ )
  285. {
  286. if ( *pHold != STACK_INIT_VALUE )
  287. break;
  288. else
  289. pHold--;
  290. }
  291. // Did we find the needed minimum number in a row
  292. if ( x >= MIN_RAM_INIT )
  293. found = true;
  294. }
  295. if ( !found )
  296. pStack--;
  297. }
  298. if ( pStack )
  299. return ( (uint16)((byte*)MCU_RAM_END - pStack) );
  300. else
  301. return ( 0 );
  302. }
  303. /*********************************************************************
  304. * @fn _itoa
  305. *
  306. * @brief convert a 16bit number to ASCII
  307. *
  308. * @param num -
  309. * buf -
  310. * radix -
  311. *
  312. * @return void
  313. *
  314. *********************************************************************/
  315. void _itoa(uint16 num, byte *buf, byte radix)
  316. {
  317. char c,i;
  318. byte *p, rst[5];
  319. p = rst;
  320. for ( i=0; i<5; i++,p++ )
  321. {
  322. c = num % radix; // Isolate a digit
  323. *p = c + (( c < 10 ) ? '0' : '7'); // Convert to Ascii
  324. num /= radix;
  325. if ( !num )
  326. break;
  327. }
  328. for ( c=0 ; c<=i; c++ )
  329. *buf++ = *p--; // Reverse character order
  330. *buf = '\0';
  331. }
  332. /*********************************************************************
  333. * @fn Onboard_rand
  334. *
  335. * @brief Random number generator
  336. *
  337. * @param none
  338. *
  339. * @return uint16 - new random number
  340. *
  341. *********************************************************************/
  342. uint16 Onboard_rand( void )
  343. {
  344. uint16 randNum;
  345. randNum = macMcuRandomByte();
  346. randNum += (macMcuRandomByte() << 8);
  347. return ( randNum );
  348. }
  349. /*********************************************************************
  350. * @fn Onboard_wait
  351. *
  352. * @brief Random number generator
  353. *
  354. * @param uint16 - time to wait
  355. *
  356. * @return none
  357. *
  358. *********************************************************************/
  359. void Onboard_wait( uint16 timeout )
  360. {
  361. while (timeout--)
  362. {
  363. asm("NOP");
  364. asm("NOP");
  365. asm("NOP");
  366. }
  367. }
  368. /*********************************************************************
  369. * EXTERNAL I/O FUNCTIONS
  370. *
  371. * User defined functions to control external devices. Add your code
  372. * to the following functions to control devices wired to DB outputs.
  373. *
  374. *********************************************************************/
  375. void BigLight_On( void )
  376. {
  377. // Put code here to turn on an external light
  378. }
  379. void BigLight_Off( void )
  380. {
  381. // Put code here to turn off an external light
  382. }
  383. void BuzzerControl( byte on )
  384. {
  385. // Put code here to turn a buzzer on/off
  386. (void)on;
  387. }
  388. void Dimmer( byte lvl )
  389. {
  390. // Put code here to control a dimmer
  391. (void)lvl;
  392. }
  393. // No dip switches on this board
  394. byte GetUserDipSw( void )
  395. {
  396. return 0;
  397. }
  398. /*********************************************************************
  399. *********************************************************************/