ZMain.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /**************************************************************************************************
  2. Filename: ZMain.c
  3. Revised: $Date: 2009-04-03 12:59:44 -0700 (Fri, 03 Apr 2009) $
  4. Revision: $Revision: 19677 $
  5. Description: Startup and shutdown code for ZStack
  6. Notes: This version targets the Chipcon CC2530
  7. Copyright 2005-2009 Texas Instruments Incorporated. All rights reserved.
  8. IMPORTANT: Your use of this Software is limited to those specific rights
  9. granted under the terms of a software license agreement between the user
  10. who downloaded the software, his/her employer (which must be your employer)
  11. and Texas Instruments Incorporated (the "License"). You may not use this
  12. Software unless you agree to abide by the terms of the License. The License
  13. limits your use, and you acknowledge, that the Software may not be modified,
  14. copied or distributed unless embedded on a Texas Instruments microcontroller
  15. or used solely and exclusively in conjunction with a Texas Instruments radio
  16. frequency transceiver, which is integrated into your product. Other than for
  17. the foregoing purpose, you may not use, reproduce, copy, prepare derivative
  18. works of, modify, distribute, perform, display or sell this Software and/or
  19. its documentation for any purpose.
  20. YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
  21. PROVIDED “AS IS?WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
  22. INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
  23. NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
  24. TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,
  25. NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
  26. LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
  27. INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
  28. OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
  29. OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
  30. (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
  31. Should you have any questions regarding your right to use this Software,
  32. contact Texas Instruments Incorporated at www.TI.com.
  33. **************************************************************************************************/
  34. /*********************************************************************
  35. * INCLUDES
  36. */
  37. #include "ZComDef.h"
  38. #include "OSAL.h"
  39. #include "OSAL_Nv.h"
  40. #include "OnBoard.h"
  41. #include "ZMAC.h"
  42. #ifndef NONWK
  43. #include "AF.h"
  44. #endif
  45. /* Hal */
  46. #include "hal_lcd.h"
  47. #include "hal_led.h"
  48. #include "hal_adc.h"
  49. #include "hal_drivers.h"
  50. #include "hal_assert.h"
  51. #include "hal_flash.h"
  52. /*********************************************************************
  53. * MACROS
  54. */
  55. /*********************************************************************
  56. * CONSTANTS
  57. */
  58. // Maximun number of Vdd samples checked before go on
  59. #define MAX_VDD_SAMPLES 3
  60. #define ZMAIN_VDD_LIMIT HAL_ADC_VDD_LIMIT_4
  61. /*********************************************************************
  62. * TYPEDEFS
  63. */
  64. /*********************************************************************
  65. * GLOBAL VARIABLES
  66. */
  67. /*********************************************************************
  68. * EXTERNAL VARIABLES
  69. */
  70. /*********************************************************************
  71. * EXTERNAL FUNCTIONS
  72. */
  73. extern bool HalAdcCheckVdd (uint8 limit);
  74. /*********************************************************************
  75. * LOCAL VARIABLES
  76. */
  77. /*********************************************************************
  78. * LOCAL FUNCTIONS
  79. */
  80. static void zmain_dev_info( void );
  81. static void zmain_ext_addr( void );
  82. static void zmain_ram_init( void );
  83. static void zmain_vdd_check( void );
  84. #ifdef LCD_SUPPORTED
  85. static void zmain_lcd_init( void );
  86. #endif
  87. /*********************************************************************
  88. * @fn main
  89. * @brief First function called after startup.
  90. * @return don't care
  91. *********************************************************************/
  92. int main( void )
  93. {
  94. // Turn off interrupts
  95. osal_int_disable( INTS_ALL );
  96. // Initialization for board related stuff such as LEDs
  97. HAL_BOARD_INIT();
  98. // Make sure supply voltage is high enough to run
  99. zmain_vdd_check();
  100. // Initialize stack memory
  101. zmain_ram_init();
  102. // Initialize board I/O
  103. InitBoard( OB_COLD );
  104. // Initialze HAL drivers
  105. HalDriverInit();
  106. // Initialize NV System
  107. osal_nv_init( NULL );
  108. // Initialize basic NV items
  109. zgInit();
  110. // Initialize the MAC
  111. ZMacInit();
  112. // Determine the extended address
  113. zmain_ext_addr();
  114. #ifndef NONWK
  115. // Since the AF isn't a task, call it's initialization routine
  116. afInit();
  117. #endif
  118. // Initialize the operating system
  119. osal_init_system();
  120. // Allow interrupts
  121. osal_int_enable( INTS_ALL );
  122. // Final board initialization
  123. InitBoard( OB_READY );
  124. // Display information about this device
  125. zmain_dev_info();
  126. /* Display the device info on the LCD */
  127. #ifdef LCD_SUPPORTED
  128. zmain_lcd_init();
  129. #endif
  130. #ifdef WDT_IN_PM1
  131. /* If WDT is used, this is a good place to enable it. */
  132. WatchDogEnable( WDTIMX );
  133. #endif
  134. osal_start_system(); // No Return from here
  135. // Shouldn't get here
  136. return ( 0 );
  137. } // main()
  138. /*********************************************************************
  139. * @fn zmain_vdd_check
  140. * @brief Check if the Vdd is OK to run the processor.
  141. * @return Return if Vdd is ok; otherwise, flash LED, then reset
  142. *********************************************************************/
  143. static void zmain_vdd_check( void )
  144. {
  145. uint8 vdd_passed_count = 0;
  146. bool toggle = 0;
  147. // Repeat getting the sample until number of failures or successes hits MAX
  148. // then based on the count value, determine if the device is ready or not
  149. while ( vdd_passed_count < MAX_VDD_SAMPLES )
  150. {
  151. if ( HalAdcCheckVdd (ZMAIN_VDD_LIMIT) )
  152. {
  153. vdd_passed_count++; // Keep track # times Vdd passes in a row
  154. MicroWait (10000); // Wait 10ms to try again
  155. }
  156. else
  157. {
  158. vdd_passed_count = 0; // Reset passed counter
  159. MicroWait (50000); // Wait 50ms
  160. MicroWait (50000); // Wait another 50ms to try again
  161. }
  162. /* toggle LED1 and LED2 */
  163. if (vdd_passed_count == 0)
  164. {
  165. if ((toggle = !(toggle)))
  166. HAL_TOGGLE_LED1();
  167. else
  168. HAL_TOGGLE_LED2();
  169. }
  170. }
  171. /* turn off LED1 */
  172. HAL_TURN_OFF_LED1();
  173. HAL_TURN_OFF_LED2();
  174. }
  175. /**************************************************************************************************
  176. * @fn zmain_ext_addr
  177. *
  178. * @brief Execute a prioritized search for a valid extended address and write the results
  179. * into the OSAL NV system for use by the system.
  180. *
  181. * input parameters
  182. *
  183. * None.
  184. *
  185. * output parameters
  186. *
  187. * None.
  188. *
  189. * @return None.
  190. **************************************************************************************************
  191. */
  192. static void zmain_ext_addr(void)
  193. {
  194. uint8 nullAddr[Z_EXTADDR_LEN] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
  195. uint8 writeNV = TRUE;
  196. // First check whether a non-erased extended address exists in the OSAL NV.
  197. if ((SUCCESS != osal_nv_item_init(ZCD_NV_EXTADDR, Z_EXTADDR_LEN, NULL)) ||
  198. (SUCCESS != osal_nv_read(ZCD_NV_EXTADDR, 0, Z_EXTADDR_LEN, aExtendedAddress)) ||
  199. (osal_memcmp(aExtendedAddress, nullAddr, Z_EXTADDR_LEN)))
  200. {
  201. // Attempt to read the extended address from the location on the lock bits page
  202. // where the programming tools know to reserve it.
  203. HalFlashRead(HAL_FLASH_IEEE_PAGE, HAL_FLASH_IEEE_OSET, aExtendedAddress, Z_EXTADDR_LEN);
  204. if (osal_memcmp(aExtendedAddress, nullAddr, Z_EXTADDR_LEN))
  205. {
  206. // Attempt to read the extended address from the designated location in the Info Page.
  207. if (!osal_memcmp((uint8 *)(P_INFOPAGE+HAL_INFOP_IEEE_OSET), nullAddr, Z_EXTADDR_LEN))
  208. {
  209. osal_memcpy(aExtendedAddress, (uint8 *)(P_INFOPAGE+HAL_INFOP_IEEE_OSET), Z_EXTADDR_LEN);
  210. }
  211. else // No valid extended address was found.
  212. {
  213. uint8 idx;
  214. #if !defined ( NV_RESTORE )
  215. writeNV = FALSE; // Make this a temporary IEEE address
  216. #endif
  217. /* Attempt to create a sufficiently random extended address for expediency.
  218. * Note: this is only valid/legal in a test environment and
  219. * must never be used for a commercial product.
  220. */
  221. for (idx = 0; idx < (Z_EXTADDR_LEN - 2);)
  222. {
  223. uint16 randy = osal_rand();
  224. aExtendedAddress[idx++] = LO_UINT16(randy);
  225. aExtendedAddress[idx++] = HI_UINT16(randy);
  226. }
  227. // Next-to-MSB identifies ZigBee devicetype.
  228. #if defined ZDO_COORDINATOR
  229. aExtendedAddress[idx++] = 0x10;
  230. #elif defined RTR_NWK
  231. aExtendedAddress[idx++] = 0x20;
  232. #else
  233. aExtendedAddress[idx++] = 0x30;
  234. #endif
  235. // MSB has historical signficance.
  236. aExtendedAddress[idx] = 0xF8;
  237. }
  238. }
  239. if ( writeNV )
  240. {
  241. (void)osal_nv_write(ZCD_NV_EXTADDR, 0, Z_EXTADDR_LEN, aExtendedAddress);
  242. }
  243. }
  244. // Set the MAC PIB extended address according to results from above.
  245. (void)ZMacSetReq(MAC_EXTENDED_ADDRESS, aExtendedAddress);
  246. }
  247. /**************************************************************************************************
  248. * @fn zmain_dev_info
  249. *
  250. * @brief This displays the IEEE (MSB to LSB) on the LCD.
  251. *
  252. * input parameters
  253. *
  254. * None.
  255. *
  256. * output parameters
  257. *
  258. * None.
  259. *
  260. * @return None.
  261. **************************************************************************************************
  262. */
  263. static void zmain_dev_info(void)
  264. {
  265. #ifdef LCD_SUPPORTED
  266. uint8 i;
  267. uint8 *xad;
  268. uint8 lcd_buf[Z_EXTADDR_LEN*2+1];
  269. // Display the extended address.
  270. xad = aExtendedAddress + Z_EXTADDR_LEN - 1;
  271. for (i = 0; i < Z_EXTADDR_LEN*2; xad--)
  272. {
  273. uint8 ch;
  274. ch = (*xad >> 4) & 0x0F;
  275. lcd_buf[i++] = ch + (( ch < 10 ) ? '0' : '7');
  276. ch = *xad & 0x0F;
  277. lcd_buf[i++] = ch + (( ch < 10 ) ? '0' : '7');
  278. }
  279. lcd_buf[Z_EXTADDR_LEN*2] = '\0';
  280. HalLcdWriteString( "IEEE: ", HAL_LCD_LINE_1 );
  281. HalLcdWriteString( (char*)lcd_buf, HAL_LCD_LINE_2 );
  282. #endif
  283. }
  284. /*********************************************************************
  285. * @fn zmain_ram_init
  286. * @brief Initialize ram for stack "high-water-mark" observations.
  287. * @return none
  288. *********************************************************************/
  289. static void zmain_ram_init( void )
  290. {
  291. uint8 *end;
  292. uint8 *ptr;
  293. // Initialize the call (parameter) stack
  294. end = (uint8*)CSTK_BEG; // Lower end
  295. ptr = (uint8*)(*( __idata uint16*)(CSTK_PTR)); // Upper end
  296. while ( --ptr > end )
  297. *ptr = STACK_INIT_VALUE;
  298. // Initialize the return (address) stack
  299. ptr = (uint8*)RSTK_END - 1; // Upper end
  300. while ( --ptr > (uint8*)SP )
  301. *(__idata uint8*)ptr = STACK_INIT_VALUE;
  302. }
  303. #ifdef LCD_SUPPORTED
  304. /*********************************************************************
  305. * @fn zmain_lcd_init
  306. * @brief Initialize LCD at start up.
  307. * @return none
  308. *********************************************************************/
  309. static void zmain_lcd_init ( void )
  310. {
  311. #ifdef SERIAL_DEBUG_SUPPORTED
  312. {
  313. HalLcdWriteString( "TexasInstruments", HAL_LCD_LINE_1 );
  314. #if defined( MT_MAC_FUNC )
  315. #if defined( ZDO_COORDINATOR )
  316. HalLcdWriteString( "MAC-MT Coord", HAL_LCD_LINE_2 );
  317. #else
  318. HalLcdWriteString( "MAC-MT Device", HAL_LCD_LINE_2 );
  319. #endif // ZDO
  320. #elif defined( MT_NWK_FUNC )
  321. #if defined( ZDO_COORDINATOR )
  322. HalLcdWriteString( "NWK Coordinator", HAL_LCD_LINE_2 );
  323. #else
  324. HalLcdWriteString( "NWK Device", HAL_LCD_LINE_2 );
  325. #endif // ZDO
  326. #endif // MT_FUNC
  327. }
  328. #endif // SERIAL_DEBUG_SUPPORTED
  329. }
  330. #endif
  331. /*********************************************************************
  332. *********************************************************************/