ZMain.c 12 KB

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