main.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. #include "main.h"
  2. #include "ReadKey.h"
  3. #include "key.h"
  4. #include "ReadKey.h"
  5. #include "crc8.h"
  6. #include "led.h"
  7. #include "eventUnit.h"
  8. #include "myADC.h"
  9. #include "myInputCapture.h"
  10. #include "myLcd.h"
  11. #include "myDisplayUnit.h"
  12. #include "myFlashData.h"
  13. #include "myTim.h"
  14. #include "myUart.h"
  15. #include "myRadio.h"
  16. //---------------key
  17. KeyParamExt_ts *getKeyReturn;
  18. key_value_te keyPressValue;
  19. static uint16_t present_adcValue;
  20. static bool startToCountingRx = false;
  21. static float present_moduleCurrendValue;
  22. static float packageCount = 1;
  23. static float validPackageCount = 1;
  24. static uint32_t rfContinuousFreq = 1;
  25. static float rfRxTestRate = 1;
  26. static rfRxPacket_ts rfRecvPacket;
  27. static rfTxPacket_ts rfTxPacket;
  28. static uint32_t rfChannelList[][4] = {
  29. 433000000, 434000000, 435000000, 436000000,
  30. 433000000, 434000000, 435000000, 436000000,
  31. 868000000, 869000000, 870000000, 871000000,
  32. 915000000, 916000000, 917000000, 918000000,
  33. };
  34. static char deviceNameList[][20] =
  35. {
  36. /*"0"*/"VG2319S433N0M1",
  37. /*"1"*/"VG2342S433N0M1",
  38. /*"1"*/"VG2342S868N0M1",
  39. /*"2"*/"VG2342S915N0M1",
  40. };
  41. userParams_ts deviceInfor =
  42. {
  43. 1, //deviceID
  44. 0, //rfChannel
  45. 20, //txPower
  46. 1, //chipType
  47. 1, //beepNumb
  48. };
  49. #define EVENT_TIME_CYCLE_10ms 0
  50. #define EVENT_TIME_CYCLE_500ms 1
  51. #define EVENT_WORK_STOP_RESTART 2
  52. #define EVENT_UART_RECV 3
  53. #define EVENT_TEST_LED 4
  54. #define EVENT_RF_CONTINUOUS_TX 5
  55. #define EVENT_RF_CONTINUOUS_RX 6
  56. #define EVENT_RF_IDLE 7
  57. #define EVENT_RF_CONTINUOUS_TX_ABORT 8
  58. #define EVENT_TIMEOUT_TO_SAVE_PARAMS 9
  59. #define EVENT_RF_GET_RX_PACKET 10
  60. #define EVENT_RF_PACKET_TX 11
  61. #define EVENT_RF_PACKET_RX 12
  62. #define EVENT_TIMEOUT_CHECK_RF_PACKET 13
  63. #define EVENT_RF_RX_ERROR 14
  64. static uint16_t eventReturn;
  65. void dealKeyPressProccess(void)
  66. {
  67. if (getKeyReturn->haveKey == false)
  68. {
  69. return;
  70. }
  71. getKeyReturn->haveKey = false;
  72. switch (getKeyReturn->value)
  73. {
  74. case LEFT_KEY:
  75. {
  76. if(getLongKeySt() == true)
  77. {
  78. clearLongKey();
  79. EnableReleaseKey();
  80. myDisplay_enter(ENTER_LAST_PAGE);
  81. }
  82. else
  83. {
  84. if (getReleaseKeySt())
  85. {
  86. }
  87. else
  88. {
  89. EnableLongKey(5);
  90. }
  91. }
  92. }
  93. break;
  94. case RIGHT_KEY:
  95. {
  96. if(getLongKeySt() == true)
  97. {
  98. clearLongKey();
  99. EnableReleaseKey();
  100. }
  101. else
  102. {
  103. if (getReleaseKeySt())
  104. {
  105. }
  106. else
  107. {
  108. EnableLongKey(5);
  109. }
  110. }
  111. }
  112. break;
  113. case TOP_KEY:
  114. {
  115. if(getLongKeySt() == true)
  116. {
  117. clearLongKey();
  118. EnableReleaseKey();
  119. beep_shortBeep();
  120. myDisplay_change(1);
  121. }
  122. else
  123. {
  124. if (getReleaseKeySt())
  125. {
  126. }
  127. else
  128. {
  129. EnableLongKey(5);
  130. }
  131. }
  132. }
  133. break;
  134. case BOTTOM_KEY:
  135. {
  136. if(getLongKeySt() == true)
  137. {
  138. clearLongKey();
  139. EnableReleaseKey();
  140. beep_shortBeep();
  141. myDisplay_change(0);
  142. }
  143. else
  144. {
  145. if (getReleaseKeySt())
  146. {
  147. }
  148. else
  149. {
  150. EnableLongKey(5);
  151. }
  152. }
  153. }
  154. break;
  155. case OK_KEY:
  156. {
  157. if(getLongKeySt() == true)
  158. {
  159. clearLongKey();
  160. EnableReleaseKey();
  161. beep_shortBeep();
  162. myDisplay_enter(ENTER_NEXT_PAGE);
  163. }
  164. else
  165. {
  166. if (getReleaseKeySt())
  167. {
  168. }
  169. else
  170. {
  171. EnableLongKey(5);
  172. }
  173. }
  174. }
  175. break;
  176. default:
  177. break;
  178. }
  179. }
  180. /**
  181. *
  182. * 串口回调函数,当串口有硬件超时时会调用该函数
  183. */
  184. static void rcc_init(void)
  185. {
  186. //---------普通IO口时钟使能
  187. RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA, ENABLE );
  188. RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB, ENABLE );
  189. RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOC, ENABLE );
  190. RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOD, ENABLE );
  191. //----------SPI1时钟使能
  192. RCC_APB2PeriphClockCmd( RCC_APB2Periph_SPI1, ENABLE );
  193. //----------复用功能时钟使能
  194. RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
  195. #if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || defined (STM32F10X_HD_VL)
  196. /* ADCCLK = PCLK2/2 */
  197. RCC_ADCCLKConfig(RCC_PCLK2_Div2);
  198. #else
  199. /* ADCCLK = PCLK2/4 */
  200. RCC_ADCCLKConfig(RCC_PCLK2_Div4);
  201. #endif
  202. }
  203. /**
  204. *
  205. * 串口回调函数,当串口有硬件超时时会调用该函数
  206. */
  207. void UART1_CALLBACK(uint8_t *buf, uint16_t len)
  208. {
  209. // if(uartPackage_Rx.isValid == 0)
  210. // {
  211. // memcpy(uartPackage_Rx.packet, buf, len);
  212. // uartPackage_Rx.len = len;
  213. // uartPackage_Rx.isValid = 1;
  214. // event_post(EVENT_UART_RECV);
  215. // }
  216. }
  217. /**
  218. *
  219. * 定时器中断回调,当产生定时器中断会调用该函数
  220. */
  221. void TIM3_CALLBACK(void)
  222. {
  223. static uint8_t timeCnt_1ms = 0;
  224. beep_onDriver();
  225. if(timeCnt_1ms ++ == 5)
  226. {
  227. timeCnt_1ms = 0;
  228. eventDriver();
  229. }
  230. }
  231. // #define CHECK_BASE_FREQUENCE 2400
  232. // #define MAX_OFFSET_FREQUENCE 200
  233. #define CHECK_BASE_FREQUENCE 5000
  234. #define MAX_OFFSET_FREQUENCE 500
  235. void myInputCaptureCallback(uint32_t captureValue1, uint32_t captureValue2, uint32_t freq)
  236. {
  237. uint32_t offsetFrequence;
  238. rfContinuousFreq = freq;
  239. //计算基准偏差值
  240. offsetFrequence = (freq > CHECK_BASE_FREQUENCE) ?
  241. (freq - CHECK_BASE_FREQUENCE) :
  242. (CHECK_BASE_FREQUENCE - freq);
  243. if (startToCountingRx)
  244. {
  245. packageCount ++;
  246. if(offsetFrequence < MAX_OFFSET_FREQUENCE)
  247. {
  248. validPackageCount ++;
  249. }
  250. rfRxTestRate = validPackageCount / packageCount * 100;
  251. }
  252. }
  253. void uiEnterCallback(int pageId, int cursorCount, int status, int value)
  254. {
  255. switch (pageId - 1)
  256. {
  257. case UI_PAGE_ID_RF_CONTINUOUS:
  258. {
  259. switch (cursorCount)
  260. {
  261. case 0:
  262. {
  263. event_post(status ? EVENT_RF_CONTINUOUS_TX : EVENT_RF_IDLE);
  264. }
  265. break;
  266. case 1:
  267. {
  268. event_post(status ? EVENT_RF_CONTINUOUS_RX : EVENT_RF_IDLE);
  269. }
  270. break;
  271. default:
  272. break;
  273. }
  274. }
  275. break;
  276. case UI_PAGE_ID_TX_PACKET:
  277. {
  278. switch (cursorCount)
  279. {
  280. case 0:
  281. {
  282. setEvent(status ? EVENT_RF_PACKET_TX : EVENT_RF_IDLE,
  283. status ? true : false,
  284. status ? 500 : 0);
  285. }
  286. break;
  287. case 1:
  288. default:
  289. break;
  290. }
  291. }
  292. break;
  293. case UI_PAGE_ID_RX_PACKET:
  294. {
  295. switch (cursorCount)
  296. {
  297. case 0:
  298. {
  299. event_post(status ? EVENT_RF_PACKET_RX : EVENT_RF_IDLE);
  300. }
  301. break;
  302. default:
  303. break;
  304. }
  305. }
  306. break;
  307. case UI_PAGE_ID_SETTING:
  308. {
  309. switch (cursorCount)
  310. {
  311. case 0://Freq
  312. {
  313. deviceInfor.rfChannel = value;
  314. myDisplay_ui_rf_setting_freq(rfChannelList[deviceInfor.chipType][0]+deviceInfor.rfChannel*1000000);
  315. myRadio_setFrequency(rfChannelList[deviceInfor.chipType][0]+deviceInfor.rfChannel*1000000);
  316. setEvent(EVENT_TIMEOUT_TO_SAVE_PARAMS, false, 200);
  317. }
  318. break;
  319. case 1://chipType
  320. {
  321. deviceInfor.chipType = value;
  322. myDisplay_ui_rf_setting_type(deviceNameList[deviceInfor.chipType]);
  323. setEvent(EVENT_TIMEOUT_TO_SAVE_PARAMS, false, 200);
  324. }
  325. break;
  326. case 2://TxPower
  327. {
  328. deviceInfor.txPower = value;
  329. myRadio_setTxPower(value);
  330. setEvent(EVENT_TIMEOUT_TO_SAVE_PARAMS, false, 200);
  331. }
  332. break;
  333. default:
  334. break;
  335. }
  336. }
  337. break;
  338. default:
  339. break;
  340. }
  341. }
  342. void rfRx_callback(uint8_t status, rfRxPacket_ts packet)
  343. {
  344. rfRecvPacket = packet;
  345. switch (status)
  346. {
  347. case RX_STA_SECCESS:
  348. {
  349. event_post(EVENT_RF_GET_RX_PACKET);
  350. }
  351. break;
  352. case RX_STA_TIMEOUT:
  353. {
  354. event_post(EVENT_RF_RX_ERROR);
  355. }
  356. break;
  357. case RX_STA_PAYLOAD_ERROR:
  358. {
  359. event_post(EVENT_RF_RX_ERROR);
  360. }
  361. break;
  362. case TX_STA_SECCESS:
  363. {
  364. // myRadio_receiver();
  365. }
  366. break;
  367. default:
  368. break;
  369. }
  370. }
  371. int main(void)
  372. {
  373. userParams_ts userParamsTemp;
  374. #ifdef BOOTLOADER_APP
  375. SCB->VTOR = FLASH_BASE | 0x000C800;
  376. #endif
  377. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置中断优先级分组为组2:2位抢占优先级,2位响应优先级
  378. rcc_init();
  379. GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);//关闭jtag , 开启swd
  380. myFlash_read((uint8_t*)&userParamsTemp, sizeof(userParams_ts));
  381. if (crc8_gernCheckT((unsigned char*)&userParamsTemp,
  382. sizeof(userParams_ts) - 1,
  383. userParamsTemp.checkSum))
  384. {
  385. deviceInfor = userParamsTemp;
  386. }
  387. key_init();
  388. //初始化LED灯
  389. LED_Init();
  390. //初始化串口
  391. myUart1_init(115200, UART1_CALLBACK);
  392. //初始化定时器
  393. //myTim3_init(200, TIM3_CALLBACK);
  394. myTim1_init(200, TIM3_CALLBACK);
  395. myADC_init();
  396. myInputCaptureTIM2_CH3_init(myInputCaptureCallback);
  397. // myInputCaptureTIM3_CH4_init(myInputCaptureCallback);
  398. beep_init();
  399. beep_setFreq(deviceInfor.beepNumb);
  400. beep_longBeep();
  401. myRadio_setChipType(deviceInfor.chipType);
  402. myRadio_init(0, rfRx_callback);
  403. myRadio_setFrequency(rfChannelList[deviceInfor.chipType][0]+deviceInfor.rfChannel*1000000);
  404. myRadio_setTxPower(deviceInfor.txPower);
  405. myLCD_init();
  406. myDisplay_init(uiEnterCallback);
  407. myDisplay_ui_firstUi_setDeviceName(deviceNameList[deviceInfor.chipType]);
  408. myDisplay_ui_firstUi_setFreq(rfChannelList[deviceInfor.chipType][0]+deviceInfor.rfChannel*1000000 / 1000000);
  409. myDisplay_ui_firstUi_setRfPower(deviceInfor.txPower);
  410. myDisplay_setSettingParams( deviceInfor.rfChannel,
  411. deviceInfor.chipType,
  412. deviceInfor.txPower);
  413. myDisplay_ui_rf_setting_freq(rfChannelList[deviceInfor.chipType][0]+deviceInfor.rfChannel*1000000);
  414. myDisplay_ui_rf_setting_type(deviceNameList[deviceInfor.chipType]);
  415. setEvent(EVENT_TIME_CYCLE_10ms, true, 10);
  416. setEvent(EVENT_TIME_CYCLE_500ms, true, 500);
  417. setEvent(EVENT_TEST_LED, true, 200);
  418. while(1)
  419. {
  420. eventReturn = event_pend();
  421. if (getEvent(eventReturn, EVENT_TIME_CYCLE_10ms))
  422. {
  423. getKeyReturn = KeyValueChange(keyPressValue);
  424. dealKeyPressProccess();
  425. }
  426. if (getEvent(eventReturn, EVENT_TIME_CYCLE_500ms))
  427. {
  428. present_adcValue = myADC_getValue();
  429. present_moduleCurrendValue = (float)present_adcValue/4095*3.3/50/0.5*1000;
  430. myDisplay_ui_rf_continuos_txCurrent(present_moduleCurrendValue);
  431. myDisplay_ui_rf_rxPacket_rxCurrent(present_moduleCurrendValue);
  432. myDisplay_ui_rf_continuos_rxContinuousFreq(rfContinuousFreq);
  433. uiTimerFlash_callBack();
  434. myDisplay_ui_rf_continuos_rxErrorRate(rfRxTestRate);
  435. validPackageCount =0;
  436. packageCount = 1;
  437. }
  438. if (getEvent(eventReturn, EVENT_RF_CONTINUOUS_RX))
  439. {
  440. myRadio_setCtrl(RADIO_EXT_CONTROL_RX_SENSITIVITY,
  441. rfChannelList[deviceInfor.chipType][0]+deviceInfor.rfChannel*1000000);
  442. startToCountingRx = true;
  443. packageCount = 1;
  444. validPackageCount = 1;
  445. }
  446. if (getEvent(eventReturn, EVENT_RF_CONTINUOUS_TX))
  447. {
  448. myRadio_setTxPower(deviceInfor.txPower);
  449. myRadio_setCtrl(RADIO_EXT_CONTROL_TX_UNMODULATED,
  450. rfChannelList[deviceInfor.chipType][0]+deviceInfor.rfChannel*1000000);
  451. }
  452. if (getEvent(eventReturn, EVENT_RF_PACKET_TX))
  453. {
  454. static uint32_t txCount = 0;
  455. rfTxPacket.len = strlen("hello world");
  456. memcpy(rfTxPacket.payload, "hello world", rfTxPacket.len);
  457. rfTxPacket.payload[rfTxPacket.len] = ((++txCount) & 0x0f) + 0x30;
  458. rfTxPacket.len ++;
  459. rfTxPacket.payload[rfTxPacket.len] = 0;
  460. myDisplay_ui_rf_tx_packet_buffer(rfTxPacket.payload);
  461. myRadio_transmit(&rfTxPacket);
  462. event_clear(EVENT_TIMEOUT_CHECK_RF_PACKET);
  463. }
  464. if (getEvent(eventReturn, EVENT_RF_PACKET_RX))
  465. {
  466. myRadio_receiver();
  467. validPackageCount = 0;
  468. packageCount = 0;
  469. }
  470. if (getEvent(eventReturn, EVENT_RF_GET_RX_PACKET))
  471. {
  472. myRadio_receiver();
  473. myDisplay_ui_rf_rxPacket_rssi(rfRecvPacket.rssi, 0);
  474. // myDisplay_ui_rf_rxPacket_rssi(myRadio_getRssi(), 0);
  475. myDisplay_ui_rf_rxPacket_buffer(rfRecvPacket.payload, 0);
  476. }
  477. if (getEvent(eventReturn, EVENT_RF_RX_ERROR))
  478. {
  479. myRadio_receiver();
  480. }
  481. if (getEvent(eventReturn, EVENT_RF_IDLE))
  482. {
  483. startToCountingRx = false;
  484. // myRadio_abort();
  485. event_clear(EVENT_RF_PACKET_TX);
  486. }
  487. if (getEvent(eventReturn, EVENT_TEST_LED))
  488. {
  489. testAllLed();
  490. }
  491. if (getEvent(eventReturn, EVENT_UART_RECV))
  492. {
  493. // if (uartPackage_Rx.isValid)
  494. // {
  495. // uartPackage_Rx.packet = 0;
  496. // }
  497. }
  498. if (getEvent(eventReturn, EVENT_TIMEOUT_TO_SAVE_PARAMS))
  499. {
  500. deviceInfor.checkSum = crc8_ger((unsigned char*)&deviceInfor, sizeof(userParams_ts) - 1);
  501. myFlash_write((uint8_t*)&deviceInfor, sizeof(userParams_ts));
  502. }
  503. keyPressValue = keyScan();
  504. myRadio_process();
  505. }
  506. }