ZMain.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /**************************************************************************************************
  2. Filename: ZMain.c
  3. Revised: $Date: 2010-09-17 16:25:30 -0700 (Fri, 17 Sep 2010) $
  4. Revision: $Revision: 23835 $
  5. Description: Startup and shutdown code for ZStack
  6. Notes: This version targets the Chipcon CC2530
  7. Copyright 2005-2010 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. #ifndef NONWK
  38. #include "AF.h"
  39. #endif
  40. #include "hal_adc.h"
  41. #include "hal_flash.h"
  42. #include "hal_lcd.h"
  43. #include "hal_led.h"
  44. #include "hal_drivers.h"
  45. #include "OnBoard.h"
  46. #include "OSAL.h"
  47. #include "OSAL_Nv.h"
  48. #include "ZComDef.h"
  49. #include "ZMAC.h"
  50. #include "znp_app.h"
  51. extern void osal_start_znp( void );
  52. /*********************************************************************
  53. * LOCAL FUNCTIONS
  54. */
  55. static void zmain_ext_addr( void );
  56. #if defined ZCL_KEY_ESTABLISH
  57. static void zmain_cert_init( void );
  58. #endif
  59. static void zmain_dev_info( void );
  60. static void zmain_vdd_check( void );
  61. #ifdef LCD_SUPPORTED
  62. static void zmain_lcd_init( void );
  63. #endif
  64. /*********************************************************************
  65. * @fn main
  66. * @brief First function called after startup.
  67. * @return don't care
  68. */
  69. int main( void )
  70. {
  71. // Turn off interrupts
  72. osal_int_disable( INTS_ALL );
  73. // Initialization for board related stuff such as LEDs
  74. HAL_BOARD_INIT();
  75. // Make sure supply voltage is high enough to run
  76. zmain_vdd_check();
  77. // Initialize board I/O
  78. InitBoard( OB_COLD );
  79. // Initialze HAL drivers
  80. HalDriverInit();
  81. // Initialize NV System
  82. osal_nv_init( NULL );
  83. // Initialize and check the ZNP RF Test Mode NV items.
  84. znpTestRF();
  85. // Initialize the MAC
  86. ZMacInit();
  87. // Determine the extended address
  88. zmain_ext_addr();
  89. #if defined ZCL_KEY_ESTABLISH
  90. // Initialize the Certicom certificate information.
  91. zmain_cert_init();
  92. #endif
  93. // Initialize basic NV items
  94. zgInit();
  95. #ifndef NONWK
  96. // Since the AF isn't a task, call it's initialization routine
  97. afInit();
  98. #endif
  99. // Initialize the operating system
  100. osal_init_system();
  101. // Allow interrupts
  102. osal_int_enable( INTS_ALL );
  103. // Final board initialization
  104. InitBoard( OB_READY );
  105. // Display information about this device
  106. zmain_dev_info();
  107. /* Display the device info on the LCD */
  108. #ifdef LCD_SUPPORTED
  109. zmain_lcd_init();
  110. #endif
  111. #ifdef WDT_IN_PM1
  112. /* If WDT is used, this is a good place to enable it. */
  113. WatchDogEnable( WDTIMX );
  114. #endif
  115. osal_start_znp(); // No Return from here
  116. return 0; // Shouldn't get here.
  117. } // main()
  118. /*********************************************************************
  119. * @fn zmain_vdd_check
  120. * @brief Check if the Vdd is OK to run the processor.
  121. * @return Return if Vdd is ok; otherwise, flash LED, then reset
  122. *********************************************************************/
  123. static void zmain_vdd_check( void )
  124. {
  125. uint8 cnt = 16;
  126. do {
  127. while (!HalAdcCheckVdd(VDD_MIN_RUN));
  128. } while (--cnt);
  129. }
  130. /**************************************************************************************************
  131. * @fn zmain_ext_addr
  132. *
  133. * @brief Execute a prioritized search for a valid extended address and write the results
  134. * into the OSAL NV system for use by the system. Temporary address not saved to NV.
  135. *
  136. * input parameters
  137. *
  138. * None.
  139. *
  140. * output parameters
  141. *
  142. * None.
  143. *
  144. * @return None.
  145. **************************************************************************************************
  146. */
  147. static void zmain_ext_addr(void)
  148. {
  149. uint8 nullAddr[Z_EXTADDR_LEN] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
  150. uint8 writeNV = TRUE;
  151. // First check whether a non-erased extended address exists in the OSAL NV.
  152. if ((SUCCESS != osal_nv_item_init(ZCD_NV_EXTADDR, Z_EXTADDR_LEN, NULL)) ||
  153. (SUCCESS != osal_nv_read(ZCD_NV_EXTADDR, 0, Z_EXTADDR_LEN, aExtendedAddress)) ||
  154. (osal_memcmp(aExtendedAddress, nullAddr, Z_EXTADDR_LEN)))
  155. {
  156. // Attempt to read the extended address from the location on the lock bits page
  157. // where the programming tools know to reserve it.
  158. HalFlashRead(HAL_FLASH_IEEE_PAGE, HAL_FLASH_IEEE_OSET, aExtendedAddress, Z_EXTADDR_LEN);
  159. if (osal_memcmp(aExtendedAddress, nullAddr, Z_EXTADDR_LEN))
  160. {
  161. // Attempt to read the extended address from the designated location in the Info Page.
  162. if (!osal_memcmp((uint8 *)(P_INFOPAGE+HAL_INFOP_IEEE_OSET), nullAddr, Z_EXTADDR_LEN))
  163. {
  164. osal_memcpy(aExtendedAddress, (uint8 *)(P_INFOPAGE+HAL_INFOP_IEEE_OSET), Z_EXTADDR_LEN);
  165. }
  166. else // No valid extended address was found.
  167. {
  168. uint8 idx;
  169. #if !defined ( NV_RESTORE )
  170. writeNV = FALSE; // Make this a temporary IEEE address
  171. #endif
  172. /* Attempt to create a sufficiently random extended address for expediency.
  173. * Note: this is only valid/legal in a test environment and
  174. * must never be used for a commercial product.
  175. */
  176. for (idx = 0; idx < (Z_EXTADDR_LEN - 2);)
  177. {
  178. uint16 randy = osal_rand();
  179. aExtendedAddress[idx++] = LO_UINT16(randy);
  180. aExtendedAddress[idx++] = HI_UINT16(randy);
  181. }
  182. // Next-to-MSB identifies ZigBee devicetype.
  183. #if ZG_BUILD_COORDINATOR_TYPE && !ZG_BUILD_JOINING_TYPE
  184. aExtendedAddress[idx++] = 0x10;
  185. #elif ZG_BUILD_RTRONLY_TYPE
  186. aExtendedAddress[idx++] = 0x20;
  187. #else
  188. aExtendedAddress[idx++] = 0x30;
  189. #endif
  190. // MSB has historical signficance.
  191. aExtendedAddress[idx] = 0xF8;
  192. }
  193. }
  194. if (writeNV)
  195. {
  196. (void)osal_nv_write(ZCD_NV_EXTADDR, 0, Z_EXTADDR_LEN, aExtendedAddress);
  197. }
  198. }
  199. // Set the MAC PIB extended address according to results from above.
  200. (void)ZMacSetReq(MAC_EXTENDED_ADDRESS, aExtendedAddress);
  201. }
  202. #if defined ZCL_KEY_ESTABLISH
  203. /**************************************************************************************************
  204. * @fn zmain_cert_init
  205. *
  206. * @brief Initialize the Certicom certificate information.
  207. *
  208. * input parameters
  209. *
  210. * None.
  211. *
  212. * output parameters
  213. *
  214. * None.
  215. *
  216. * @return None.
  217. **************************************************************************************************
  218. */
  219. static void zmain_cert_init(void)
  220. {
  221. uint8 certData[ZCL_KE_IMPLICIT_CERTIFICATE_LEN];
  222. uint8 nullData[ZCL_KE_IMPLICIT_CERTIFICATE_LEN] = {
  223. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  224. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  225. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  226. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  227. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  228. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
  229. };
  230. (void)osal_nv_item_init(ZCD_NV_IMPLICIT_CERTIFICATE, ZCL_KE_IMPLICIT_CERTIFICATE_LEN, NULL);
  231. (void)osal_nv_item_init(ZCD_NV_DEVICE_PRIVATE_KEY, ZCL_KE_DEVICE_PRIVATE_KEY_LEN, NULL);
  232. // First check whether non-null certificate data exists in the OSAL NV. To save on code space,
  233. // just use the ZCD_NV_CA_PUBLIC_KEY as the bellwether for all three.
  234. if ((SUCCESS != osal_nv_item_init(ZCD_NV_CA_PUBLIC_KEY, ZCL_KE_CA_PUBLIC_KEY_LEN, NULL)) ||
  235. (SUCCESS != osal_nv_read(ZCD_NV_CA_PUBLIC_KEY, 0, ZCL_KE_CA_PUBLIC_KEY_LEN, certData)) ||
  236. (osal_memcmp(certData, nullData, ZCL_KE_CA_PUBLIC_KEY_LEN)))
  237. {
  238. // Attempt to read the certificate data from its corresponding location on the lock bits page.
  239. HalFlashRead(HAL_FLASH_IEEE_PAGE, HAL_FLASH_CA_PUBLIC_KEY_OSET, certData,
  240. ZCL_KE_CA_PUBLIC_KEY_LEN);
  241. // If the certificate data is not NULL, use it to update the corresponding NV items.
  242. if (!osal_memcmp(certData, nullData, ZCL_KE_CA_PUBLIC_KEY_LEN))
  243. {
  244. (void)osal_nv_write(ZCD_NV_CA_PUBLIC_KEY, 0, ZCL_KE_CA_PUBLIC_KEY_LEN, certData);
  245. HalFlashRead(HAL_FLASH_IEEE_PAGE, HAL_FLASH_IMPLICIT_CERT_OSET, certData,
  246. ZCL_KE_IMPLICIT_CERTIFICATE_LEN);
  247. (void)osal_nv_write(ZCD_NV_IMPLICIT_CERTIFICATE, 0,
  248. ZCL_KE_IMPLICIT_CERTIFICATE_LEN, certData);
  249. HalFlashRead(HAL_FLASH_IEEE_PAGE, HAL_FLASH_DEV_PRIVATE_KEY_OSET, certData,
  250. ZCL_KE_DEVICE_PRIVATE_KEY_LEN);
  251. (void)osal_nv_write(ZCD_NV_DEVICE_PRIVATE_KEY, 0, ZCL_KE_DEVICE_PRIVATE_KEY_LEN, certData);
  252. }
  253. }
  254. }
  255. #endif
  256. /**************************************************************************************************
  257. * @fn zmain_dev_info
  258. *
  259. * @brief This displays the IEEE (MSB to LSB) on the LCD.
  260. *
  261. * input parameters
  262. *
  263. * None.
  264. *
  265. * output parameters
  266. *
  267. * None.
  268. *
  269. * @return None.
  270. **************************************************************************************************
  271. */
  272. static void zmain_dev_info(void)
  273. {
  274. #ifdef LCD_SUPPORTED
  275. uint8 i;
  276. uint8 *xad;
  277. uint8 lcd_buf[Z_EXTADDR_LEN*2+1];
  278. // Display the extended address.
  279. xad = aExtendedAddress + Z_EXTADDR_LEN - 1;
  280. for (i = 0; i < Z_EXTADDR_LEN*2; xad--)
  281. {
  282. uint8 ch;
  283. ch = (*xad >> 4) & 0x0F;
  284. lcd_buf[i++] = ch + (( ch < 10 ) ? '0' : '7');
  285. ch = *xad & 0x0F;
  286. lcd_buf[i++] = ch + (( ch < 10 ) ? '0' : '7');
  287. }
  288. lcd_buf[Z_EXTADDR_LEN*2] = '\0';
  289. HalLcdWriteString( "IEEE: ", HAL_LCD_LINE_1 );
  290. HalLcdWriteString( (char*)lcd_buf, HAL_LCD_LINE_2 );
  291. #endif
  292. }
  293. #ifdef LCD_SUPPORTED
  294. /*********************************************************************
  295. * @fn zmain_lcd_init
  296. * @brief Initialize LCD at start up.
  297. * @return none
  298. *********************************************************************/
  299. static void zmain_lcd_init ( void )
  300. {
  301. #ifdef SERIAL_DEBUG_SUPPORTED
  302. {
  303. HalLcdWriteString( "TexasInstruments", HAL_LCD_LINE_1 );
  304. #if defined( MT_MAC_FUNC )
  305. #if defined( ZDO_COORDINATOR )
  306. HalLcdWriteString( "MAC-MT Coord", HAL_LCD_LINE_2 );
  307. #else
  308. HalLcdWriteString( "MAC-MT Device", HAL_LCD_LINE_2 );
  309. #endif // ZDO
  310. #elif defined( MT_NWK_FUNC )
  311. #if defined( ZDO_COORDINATOR )
  312. HalLcdWriteString( "NWK Coordinator", HAL_LCD_LINE_2 );
  313. #else
  314. HalLcdWriteString( "NWK Device", HAL_LCD_LINE_2 );
  315. #endif // ZDO
  316. #endif // MT_FUNC
  317. }
  318. #endif // SERIAL_DEBUG_SUPPORTED
  319. }
  320. #endif
  321. /*********************************************************************
  322. *********************************************************************/