856b54565aa86f523624ec745cbde5d17b918dc4.svn-base 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /******************************************************************************/
  2. /* -- 居加居-- */
  3. /*
  4. // 文件名:
  5. // 说明:
  6. // 编写人员:kinve
  7. // 编写日期:2018-8-21
  8. // 程序维护:
  9. // 维护记录:
  10. //
  11. //
  12. */
  13. // 免责声明:
  14. // (c) Anxinke Corporation. All rights reserved.
  15. /******************************************************************************/
  16. #include <string.h>
  17. #include "Common.h"
  18. #include "Delay.h"
  19. #include "Timer.h"
  20. #include "Interrupt.h"
  21. #include "Flash.h"
  22. #include "Version.h"
  23. #define USE_2401_API
  24. #ifdef USE_2401_API
  25. #include "nRF24L01_API.h"
  26. #else
  27. #include "NRF24L01_inte.H"
  28. #define TX_PLOAD_WIDTH NRF24L01_TX_PLOAD_LEN
  29. #define RX_PLOAD_WIDTH NRF24L01_TX_PLOAD_LEN
  30. #endif
  31. unsigned char P12_Led_state = 0;
  32. //***********************************************************************************************************
  33. // Application: SPI Function
  34. // Master send 0x90 and recevie 0x4E
  35. // Master send 0x01 and recevie 0x55
  36. // Master send 0x02 and recevie 0x56
  37. // Master send 0x03 and recevie 0x4F
  38. // Master send 0x04 and recevie 0x54
  39. //
  40. // Master recevie 0x4E and 0x4F form slave after transmitting
  41. //***********************************************************************************************************
  42. //-----------------------------------------------------------------------------------------------------------
  43. void SPI_Error ( void )
  44. {
  45. DBG ( "\nSPI error.\n" );
  46. while ( 1 ) // SPI error and P0.7 flash/
  47. {
  48. P07 = 1;
  49. Timer0_Delay1ms ( 500 );
  50. P07 = 0;
  51. Timer0_Delay1ms ( 500 );
  52. }
  53. }
  54. //-----------------------------------------------------------------------------------------------------------
  55. void SPI_Initial ( void )
  56. {
  57. clr_SPIEN; //禁止SPI
  58. P15_Quasi_Mode; // P15 (SS) Quasi mode
  59. P10_Quasi_Mode; // P10(SPCLK) Quasi mode
  60. P00_Quasi_Mode; // P00 (MOSI) Quasi mode
  61. P01_Quasi_Mode; // P22 (MISO) Quasi mode
  62. //SS仅在从机有效
  63. set_DISMODF; // 1 = 禁止模式错误检测. SS的特征依赖SSOE 位
  64. clr_SSOE; // SS 作为 0.普通IO 1.自动拉低
  65. clr_LSBFE; // 0.MSB first
  66. clr_CPOL; // 0.SPI空闲时SCL为低
  67. clr_CPHA; // 0.在第1个边沿采样
  68. set_MSTR; // 主设备
  69. //SPICLK_DIV16; // Select SPI clock=1Mhz
  70. SPICLK_DIV4; // 16/4=4Mhz, 24l01速度不能太快,最大的数据传输率为 10Mbps
  71. set_SPIEN; // 使能SPI
  72. clr_SPIF; // 清标志位
  73. }
  74. //-----------------------------------------------------------------------------------------------------------
  75. void Start_Sending_SPI ( uint8 *pu8MID, uint8 *pu8DID )
  76. {
  77. SS = 0;
  78. SPDR = 0x90;
  79. Timer3_Delay10us ( 1 ); // Send 0x90 to Slave
  80. while ( ! ( SPSR & SET_BIT7 ) );
  81. clr_SPIF;
  82. if ( SPDR != 0x4E )
  83. SPI_Error();
  84. DBG ( "\nSlave Return %c!\n", SPDR );
  85. SPDR = 0x01; // Send 0x01 to Slave
  86. Timer3_Delay10us ( 1 );
  87. while ( ! ( SPSR & SET_BIT7 ) );
  88. clr_SPIF;
  89. if ( SPDR != 0x55 )
  90. SPI_Error();
  91. DBG ( "\nSlave Return %c!\n", SPDR );
  92. SPDR = 0x02; // Send 0x02 to Slave
  93. Timer3_Delay10us ( 1 );
  94. while ( ! ( SPSR & SET_BIT7 ) );
  95. clr_SPIF;
  96. if ( SPDR != 0x56 )
  97. SPI_Error();
  98. DBG ( "\nSlave Return %c!\n", SPDR );
  99. SPDR = 0x03; // Send 0x03 to Slave
  100. Timer3_Delay10us ( 1 );
  101. while ( ! ( SPSR & SET_BIT7 ) );
  102. clr_SPIF;
  103. if ( SPDR != 0x4F )
  104. SPI_Error();
  105. DBG ( "\nSlave Return %c!\n", SPDR );
  106. SPDR = 0x04; // Send 0x04 to Slave
  107. Timer3_Delay10us ( 1 );
  108. while ( ! ( SPSR & SET_BIT7 ) );
  109. clr_SPIF;
  110. if ( SPDR != 0x54 )
  111. SPI_Error();
  112. DBG ( "\nSlave Return %c!\n", SPDR );
  113. SPDR = 0xFF;
  114. Timer3_Delay10us ( 1 );
  115. while ( ! ( SPSR & SET_BIT7 ) );
  116. clr_SPIF;
  117. *pu8MID = SPDR; // Receive Slave 1st DATA from Slave
  118. DBG ( "\nSlave Return %c!\n", SPDR );
  119. SPDR = 0xFF;
  120. Timer3_Delay10us ( 1 );
  121. while ( ! ( SPSR & SET_BIT7 ) );
  122. clr_SPIF;
  123. *pu8DID = SPDR; // Receive Slave 2nd DATA from Slave
  124. DBG ( "\nSlave Return %c!\n", SPDR );
  125. SS = 1;
  126. }
  127. //1.关闭POR (TA 保护) 上电复位(检测电源上升到系统可以工作的电压)
  128. #ifdef N76E003_IAR
  129. __sfr __no_init volatile unsigned char PORDIS @ 0XFD;
  130. #else
  131. sfr PORDIS = 0XFD;
  132. #endif
  133. void CLOSE_POR ( void )
  134. {
  135. TA = 0XAA;
  136. TA = 0X55;
  137. PORDIS = 0X5A;
  138. TA = 0XAA;
  139. TA = 0X55;
  140. PORDIS = 0XA5;
  141. }
  142. /******************************************************************************
  143. This code illustrates how to do APROM and CONFIG IAP from LDROM.
  144. APROM are re-programmed by the code to output P1 as 55h and P0 as aah.
  145. The CONFIG2 is also updated to disable BOD reset.
  146. User needs to configure CONFIG0 = 0x7F, CONFIG1 = 0xFE, CONFIG2 = 0xFF.
  147. ******************************************************************************/
  148. //2.关闭BOD 欠压检测 (监测运行期间VDD电平)
  149. #ifdef N76E003_IAR
  150. __sfr __no_init volatile unsigned char BODCON0 @ 0XA3;
  151. __sfr __no_init volatile unsigned char BODCON1 @ 0XAB;
  152. //__sfr __no_init volatile unsigned char CONFIG2 @ 0XFD;
  153. #else
  154. //sfr BODCON0 = 0XA3;
  155. //sfr BODCON1 = 0XAB;
  156. #endif
  157. void CLOSE_BOD ( void )
  158. {
  159. BODCON0 &= ~ ( 1 << 7 ); //0 =禁用欠压检测电路
  160. }
  161. enum
  162. {
  163. UMKNOW_RESET,
  164. POWER_ON_RESET, //上电复位
  165. BROWN_OUT_RESET, //欠压复位
  166. RST_PIN_RESET, //RST引脚复位
  167. HARD_FAULT_RESET, //硬件故障复位
  168. WATCHGOD_TIMER_RESET, //看门狗定时器复位
  169. SOFTWARE_RESET, //软件复位
  170. MAX_RESET
  171. };
  172. /*
  173. RST pin reset
  174. Brown-out reset
  175. Software reset
  176. Power-on reset
  177. Watchgod Timer reset
  178. Hard Fault reset
  179. */
  180. unsigned char Reset_Reason ( void )
  181. {
  182. unsigned char res = UMKNOW_RESET;
  183. if ( PCON & ( 1 << 4 ) )
  184. {
  185. PCON &= ~ ( 1 << 4 );
  186. res = POWER_ON_RESET;
  187. }
  188. if ( BODCON0 & ( 1 << 1 ) )
  189. {
  190. BODCON0 &= ~ ( 1 << 1 );
  191. res = BROWN_OUT_RESET;
  192. }
  193. if ( AUXR1 & ( 1 << 6 ) )
  194. {
  195. AUXR1 &= ~ ( 1 << 6 );
  196. res = RST_PIN_RESET;
  197. }
  198. if ( AUXR1 & ( 1 << 5 ) )
  199. {
  200. AUXR1 &= ~ ( 1 << 5 );
  201. res = HARD_FAULT_RESET;
  202. }
  203. if ( WDCON & ( 1 << 3 ) )
  204. {
  205. WDCON &= ~ ( 1 << 3 );
  206. res = WATCHGOD_TIMER_RESET;
  207. }
  208. if ( AUXR1 & ( 1 << 7 ) )
  209. {
  210. AUXR1 &= ~ ( 1 << 7 );
  211. res = SOFTWARE_RESET;
  212. }
  213. return res;
  214. }
  215. void ResetSys ( void )
  216. {
  217. AUXR1 &= ~ ( 1 << 7 ); //software reset flag clear
  218. EA = 0;
  219. TA = 0xAA;
  220. TA = 0x55;
  221. CHPCON |= ( 1 << 7 ); //software reset
  222. }
  223. void P12_Flash ( unsigned char num )
  224. {
  225. unsigned char i;
  226. for ( i = 0; i < num && num < 5 ; i++ )
  227. {
  228. P12 = 1;
  229. Timer3_Delay100ms ( 1 );
  230. P12 = 0;
  231. Timer3_Delay100ms ( 1 );
  232. }
  233. P12_Led_state = P12;
  234. }
  235. #if 0
  236. unsigned char flag;
  237. //NRF24L01中断处理
  238. #ifdef N76E003_IAR
  239. #pragma vector=0x3B
  240. __interrupt void NRF24L01_Handler ( void )
  241. #else
  242. void NRF24L01_Handler ( void ) interrupt 7 //interrupt address is 0x001B
  243. #endif
  244. {
  245. clr_EPI; //关闭管脚中断7
  246. flag = PIF;
  247. PIF = 0x00; //清除全部标志
  248. DBG ( "\nNRF24L01 interrupt!\n" );
  249. set_EPI; //开启管脚中断7
  250. }
  251. void NRF24L01_Interrupt_P03 ( void )
  252. {
  253. //P0M1|=SET_BIT3;P0M2&=~SET_BIT3; //input
  254. PICON &= ~ ( SET_BIT0 | SET_BIT1 );
  255. PICON |= ( 0x00 ); //[1:0]=0.端口0
  256. PICON |= SET_BIT5; //通道3=1.边沿触发
  257. PINEN |= SET_BIT3; // 1 = 低电平/ 下降沿触发中断
  258. PIPEN &= ~SET_BIT3; //0 = 关闭中断(高电平/ 上升沿触发中断)
  259. EIE |= SET_BIT1; //管脚中断,最多8个,中断号7
  260. EA = 1; //总中断
  261. }
  262. #endif
  263. //96位 UID与128位UCID
  264. unsigned char xdata UIDBuf[12]; //存储96bit的UID内容
  265. UINT8 UID_BYTE ( UINT8 Addr )
  266. {
  267. UINT8 DATATEMP;
  268. set_IAPEN;
  269. IAPAL = Addr;
  270. IAPAH = 0x00;
  271. IAPCN = READ_UID;
  272. set_IAPGO;
  273. DATATEMP = IAPFD;
  274. clr_IAPEN;
  275. return DATATEMP;
  276. }
  277. //读出芯片的96bit的UID
  278. void ReadUID_UCID ( UINT8 UID, UINT8 *out )
  279. {
  280. UINT8 i;
  281. for ( i = 0; i < 12; i++ )
  282. {
  283. out[i] = UID_BYTE ( i + UID ); //ucid=0x20
  284. }
  285. }
  286. //最长4byte
  287. #define GET_VER_POS(NUM,VER) ((VER>>(8*NUM))&0xff)
  288. //-----------------------------------------------------------------------------------------------------------
  289. void main ( void )
  290. {
  291. unsigned char reset_num, i;
  292. unsigned char datatemp[8];
  293. unsigned char TxBuf[TX_PLOAD_WIDTH] = {'>', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0'}; //
  294. unsigned char RxBuf[RX_PLOAD_WIDTH] = {0};
  295. //uint8 u8MID, u8DID;
  296. reset_num = Reset_Reason();
  297. CLOSE_POR();
  298. CLOSE_BOD();
  299. Set_All_GPIO_Quasi_Mode;
  300. //NRF24L01_Interrupt_P03();
  301. //定时器
  302. Timer0_init ( TIMER_SCAN ); //1ms
  303. //ResetSys();
  304. InitialUART0_Timer1 ( 115200 ); /* 115200 Baud Rate*/
  305. Timer3_Delay10us(1000);
  306. DBG ( "\nHome Light %s, Build by Kinve in %ld\n", THIS_VER, CUSTOM_VER );
  307. ReadUID_UCID ( 0x00, UIDBuf ); //0x00=uid 0x20=ucid
  308. DBG ( "UID=%x%x%x%x\n", UIDBuf[0], UIDBuf[1], UIDBuf[2], UIDBuf[3] );
  309. //call read byte
  310. datatemp[5] = Read_APROM_BYTE ( 0x3885 );
  311. if ( datatemp[5] != 0x4A )
  312. {
  313. //call write byte
  314. for ( i = 0; i < sizeof ( datatemp ); i++ )
  315. {
  316. if ( i == 5 ) Write_DATAFLASH_BYTE ( 0x3880 + i, 0x4A );
  317. else Write_DATAFLASH_BYTE ( 0x3880 + i, GET_VER_POS ( i, CUSTOM_VER ) );
  318. datatemp[i] = Read_APROM_BYTE ( ( UINT16 code * ) ( 0x3880 + i ) );
  319. }
  320. }
  321. SPI_Initial();
  322. #ifdef USE_2401_API
  323. NRF24L01_Wait_Check(); // 等待检测到NRF24L01,程序才会向下执行
  324. NRF24L01_RT_Init ();
  325. #else
  326. NRF24L01_Init();
  327. DBG ( "NRF24L01 Check Reg: %s\n", ( NRF24L01_FLAG & NRF24L01_CHECK )?("SUCCESS"):("FAIL"));
  328. #endif
  329. DBG ( "NRF24L01 init OK!\n" );
  330. while ( 1 )
  331. {
  332. if ( System_Flag_1s )
  333. {
  334. System_Flag_1s = 0;
  335. P12_Led_state = !P12_Led_state;
  336. P12 = P12_Led_state;
  337. }
  338. if ( System_Flag_5s )
  339. {
  340. System_Flag_5s = 0;
  341. //TxBuf[1] = TxBuf[1] == '1' ? '2' : '1';
  342. #ifdef USE_2401_API
  343. SEND_BUF ( TxBuf, 10 );
  344. #else
  345. NRF24L01_SendFrame ( TxBuf, 10 );
  346. #endif
  347. DBG ( "Send: %s\n", TxBuf );
  348. }
  349. #ifdef USE_2401_API
  350. if ( NRF_IRQ == 0 ) // 如果无线模块接收到数据
  351. {
  352. if ( NRF24L01_RxPacket ( RxBuf, sizeof(RxBuf) ) )
  353. {
  354. P12_Flash ( 2 );
  355. DBG ( "Recv: %s\n", RxBuf );
  356. }
  357. }
  358. #else
  359. if ( NRF24L01_FLAG & NRF24L01_TX_ACCESS )
  360. {
  361. NRF24L01_FLAG &= ~NRF24L01_TX_ACCESS; //清除发送成功标志
  362. P12_Flash ( 1 );
  363. DBG ( "Send: OK\n" );
  364. }
  365. if ( NRF24L01_FLAG & NRF24L01_RECIVE )
  366. {
  367. NRF24L01_FLAG &= ~NRF24L01_RECIVE; //清除接收成功标志
  368. NRF24L01_RecvFrame ( RxBuf );
  369. P12_Flash ( 2 );
  370. DBG ( "Recv: %s\n", RxBuf );
  371. }
  372. #endif
  373. }
  374. }
  375. //-----------------------------------------------------------------------------------------------------------