123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543 |
- #include "main.h"
- #include "ReadKey.h"
- #include "key.h"
- #include "ReadKey.h"
- #include "crc8.h"
- #include "led.h"
- #include "eventUnit.h"
- #include "myADC.h"
- #include "myInputCapture.h"
- #include "myLcd.h"
- #include "myDisplayUnit.h"
- #include "myFlashData.h"
- #include "myTim.h"
- #include "myUart.h"
- #include "myRadio.h"
- //---------------key
- KeyParamExt_ts *getKeyReturn;
- key_value_te keyPressValue;
- static uint16_t present_adcValue;
- static bool startToCountingRx = false;
- static float present_moduleCurrendValue;
- static float packageCount = 1;
- static float validPackageCount = 1;
- static uint32_t rfContinuousFreq = 1;
- static float rfRxTestRate = 1;
- static rfRxPacket_ts rfRecvPacket;
- static rfTxPacket_ts rfTxPacket;
- static uint32_t rfChannelList[][4] = {
- 433000000, 434000000, 435000000, 436000000,
- 433000000, 434000000, 435000000, 436000000,
- 868000000, 869000000, 870000000, 871000000,
- 915000000, 916000000, 917000000, 918000000,
- };
- static char deviceNameList[][20] =
- {
- /*"0"*/"VG2319S433N0M1",
- /*"1"*/"VG2342S433N0M1",
- /*"1"*/"VG2342S868N0M1",
- /*"2"*/"VG2342S915N0M1",
- };
- userParams_ts deviceInfor =
- {
- 1, //deviceID
- 0, //rfChannel
- 20, //txPower
- 1, //chipType
- 1, //beepNumb
- };
- #define EVENT_TIME_CYCLE_10ms 0
- #define EVENT_TIME_CYCLE_500ms 1
- #define EVENT_WORK_STOP_RESTART 2
- #define EVENT_UART_RECV 3
- #define EVENT_TEST_LED 4
- #define EVENT_RF_CONTINUOUS_TX 5
- #define EVENT_RF_CONTINUOUS_RX 6
- #define EVENT_RF_IDLE 7
- #define EVENT_RF_CONTINUOUS_TX_ABORT 8
- #define EVENT_TIMEOUT_TO_SAVE_PARAMS 9
- #define EVENT_RF_GET_RX_PACKET 10
- #define EVENT_RF_PACKET_TX 11
- #define EVENT_RF_PACKET_RX 12
- #define EVENT_TIMEOUT_CHECK_RF_PACKET 13
- #define EVENT_RF_RX_ERROR 14
- static uint16_t eventReturn;
- void dealKeyPressProccess(void)
- {
- if (getKeyReturn->haveKey == false)
- {
- return;
- }
- getKeyReturn->haveKey = false;
- switch (getKeyReturn->value)
- {
- case LEFT_KEY:
- {
- if(getLongKeySt() == true)
- {
- clearLongKey();
- EnableReleaseKey();
- myDisplay_enter(ENTER_LAST_PAGE);
- }
- else
- {
- if (getReleaseKeySt())
- {
- }
- else
- {
- EnableLongKey(5);
- }
- }
- }
- break;
- case RIGHT_KEY:
- {
- if(getLongKeySt() == true)
- {
- clearLongKey();
- EnableReleaseKey();
- }
- else
- {
- if (getReleaseKeySt())
- {
- }
- else
- {
- EnableLongKey(5);
- }
- }
- }
- break;
- case TOP_KEY:
- {
- if(getLongKeySt() == true)
- {
- clearLongKey();
- EnableReleaseKey();
- beep_shortBeep();
- myDisplay_change(1);
- }
- else
- {
- if (getReleaseKeySt())
- {
- }
- else
- {
- EnableLongKey(5);
- }
- }
- }
- break;
- case BOTTOM_KEY:
- {
- if(getLongKeySt() == true)
- {
- clearLongKey();
- EnableReleaseKey();
- beep_shortBeep();
- myDisplay_change(0);
- }
- else
- {
- if (getReleaseKeySt())
- {
- }
- else
- {
- EnableLongKey(5);
- }
- }
- }
- break;
- case OK_KEY:
- {
- if(getLongKeySt() == true)
- {
- clearLongKey();
- EnableReleaseKey();
- beep_shortBeep();
- myDisplay_enter(ENTER_NEXT_PAGE);
- }
- else
- {
- if (getReleaseKeySt())
- {
- }
- else
- {
- EnableLongKey(5);
- }
- }
- }
- break;
- default:
- break;
- }
- }
- /**
- *
- * 串口回调函数,当串口有硬件超时时会调用该函数
- */
- static void rcc_init(void)
- {
- //---------普通IO口时钟使能
- RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA, ENABLE );
- RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB, ENABLE );
- RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOC, ENABLE );
- RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOD, ENABLE );
- //----------SPI1时钟使能
- RCC_APB2PeriphClockCmd( RCC_APB2Periph_SPI1, ENABLE );
- //----------复用功能时钟使能
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
- #if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || defined (STM32F10X_HD_VL)
- /* ADCCLK = PCLK2/2 */
- RCC_ADCCLKConfig(RCC_PCLK2_Div2);
- #else
- /* ADCCLK = PCLK2/4 */
- RCC_ADCCLKConfig(RCC_PCLK2_Div4);
- #endif
- }
- /**
- *
- * 串口回调函数,当串口有硬件超时时会调用该函数
- */
- void UART1_CALLBACK(uint8_t *buf, uint16_t len)
- {
- // if(uartPackage_Rx.isValid == 0)
- // {
- // memcpy(uartPackage_Rx.packet, buf, len);
- // uartPackage_Rx.len = len;
- // uartPackage_Rx.isValid = 1;
- // event_post(EVENT_UART_RECV);
- // }
- }
- /**
- *
- * 定时器中断回调,当产生定时器中断会调用该函数
- */
- void TIM3_CALLBACK(void)
- {
- static uint8_t timeCnt_1ms = 0;
- beep_onDriver();
- if(timeCnt_1ms ++ == 5)
- {
- timeCnt_1ms = 0;
- eventDriver();
- }
- }
- // #define CHECK_BASE_FREQUENCE 2400
- // #define MAX_OFFSET_FREQUENCE 200
- #define CHECK_BASE_FREQUENCE 5000
- #define MAX_OFFSET_FREQUENCE 500
- void myInputCaptureCallback(uint32_t captureValue1, uint32_t captureValue2, uint32_t freq)
- {
- uint32_t offsetFrequence;
-
- rfContinuousFreq = freq;
- //计算基准偏差值
- offsetFrequence = (freq > CHECK_BASE_FREQUENCE) ?
- (freq - CHECK_BASE_FREQUENCE) :
- (CHECK_BASE_FREQUENCE - freq);
- if (startToCountingRx)
- {
- packageCount ++;
- if(offsetFrequence < MAX_OFFSET_FREQUENCE)
- {
- validPackageCount ++;
- }
- rfRxTestRate = validPackageCount / packageCount * 100;
- }
- }
- void uiEnterCallback(int pageId, int cursorCount, int status, int value)
- {
- switch (pageId - 1)
- {
- case UI_PAGE_ID_RF_CONTINUOUS:
- {
- switch (cursorCount)
- {
- case 0:
- {
- event_post(status ? EVENT_RF_CONTINUOUS_TX : EVENT_RF_IDLE);
- }
- break;
- case 1:
- {
- event_post(status ? EVENT_RF_CONTINUOUS_RX : EVENT_RF_IDLE);
- }
- break;
- default:
- break;
- }
- }
- break;
- case UI_PAGE_ID_TX_PACKET:
- {
- switch (cursorCount)
- {
- case 0:
- {
- setEvent(status ? EVENT_RF_PACKET_TX : EVENT_RF_IDLE,
- status ? true : false,
- status ? 500 : 0);
- }
- break;
- case 1:
- default:
- break;
- }
- }
- break;
- case UI_PAGE_ID_RX_PACKET:
- {
- switch (cursorCount)
- {
- case 0:
- {
- event_post(status ? EVENT_RF_PACKET_RX : EVENT_RF_IDLE);
- }
- break;
- default:
- break;
- }
- }
- break;
- case UI_PAGE_ID_SETTING:
- {
- switch (cursorCount)
- {
- case 0://Freq
- {
- deviceInfor.rfChannel = value;
- myDisplay_ui_rf_setting_freq(rfChannelList[deviceInfor.chipType][0]+deviceInfor.rfChannel*1000000);
- myRadio_setFrequency(rfChannelList[deviceInfor.chipType][0]+deviceInfor.rfChannel*1000000);
- setEvent(EVENT_TIMEOUT_TO_SAVE_PARAMS, false, 200);
- }
- break;
- case 1://chipType
- {
- deviceInfor.chipType = value;
- myDisplay_ui_rf_setting_type(deviceNameList[deviceInfor.chipType]);
- setEvent(EVENT_TIMEOUT_TO_SAVE_PARAMS, false, 200);
- }
- break;
- case 2://TxPower
- {
- deviceInfor.txPower = value;
- myRadio_setTxPower(value);
- setEvent(EVENT_TIMEOUT_TO_SAVE_PARAMS, false, 200);
- }
- break;
- default:
- break;
- }
- }
- break;
- default:
- break;
- }
- }
- void rfRx_callback(uint8_t status, rfRxPacket_ts packet)
- {
-
- rfRecvPacket = packet;
-
- switch (status)
- {
- case RX_STA_SECCESS:
- {
- event_post(EVENT_RF_GET_RX_PACKET);
- }
- break;
- case RX_STA_TIMEOUT:
- {
- event_post(EVENT_RF_RX_ERROR);
- }
- break;
- case RX_STA_PAYLOAD_ERROR:
- {
- event_post(EVENT_RF_RX_ERROR);
- }
- break;
- case TX_STA_SECCESS:
- {
- // myRadio_receiver();
- }
- break;
- default:
- break;
- }
- }
- int main(void)
- {
- userParams_ts userParamsTemp;
- #ifdef BOOTLOADER_APP
- SCB->VTOR = FLASH_BASE | 0x000C800;
- #endif
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置中断优先级分组为组2:2位抢占优先级,2位响应优先级
- rcc_init();
-
- GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);//关闭jtag , 开启swd
- myFlash_read((uint8_t*)&userParamsTemp, sizeof(userParams_ts));
- if (crc8_gernCheckT((unsigned char*)&userParamsTemp,
- sizeof(userParams_ts) - 1,
- userParamsTemp.checkSum))
- {
- deviceInfor = userParamsTemp;
- }
- key_init();
- //初始化LED灯
- LED_Init();
- //初始化串口
- myUart1_init(115200, UART1_CALLBACK);
- //初始化定时器
- //myTim3_init(200, TIM3_CALLBACK);
- myTim1_init(200, TIM3_CALLBACK);
- myADC_init();
- myInputCaptureTIM2_CH3_init(myInputCaptureCallback);
- // myInputCaptureTIM3_CH4_init(myInputCaptureCallback);
- beep_init();
- beep_setFreq(deviceInfor.beepNumb);
- beep_longBeep();
- myRadio_setChipType(deviceInfor.chipType);
- myRadio_init(0, rfRx_callback);
- myRadio_setFrequency(rfChannelList[deviceInfor.chipType][0]+deviceInfor.rfChannel*1000000);
- myRadio_setTxPower(deviceInfor.txPower);
- myLCD_init();
- myDisplay_init(uiEnterCallback);
- myDisplay_ui_firstUi_setDeviceName(deviceNameList[deviceInfor.chipType]);
- myDisplay_ui_firstUi_setFreq(rfChannelList[deviceInfor.chipType][0]+deviceInfor.rfChannel*1000000 / 1000000);
- myDisplay_ui_firstUi_setRfPower(deviceInfor.txPower);
- myDisplay_setSettingParams( deviceInfor.rfChannel,
- deviceInfor.chipType,
- deviceInfor.txPower);
- myDisplay_ui_rf_setting_freq(rfChannelList[deviceInfor.chipType][0]+deviceInfor.rfChannel*1000000);
- myDisplay_ui_rf_setting_type(deviceNameList[deviceInfor.chipType]);
- setEvent(EVENT_TIME_CYCLE_10ms, true, 10);
- setEvent(EVENT_TIME_CYCLE_500ms, true, 500);
- setEvent(EVENT_TEST_LED, true, 200);
- while(1)
- {
- eventReturn = event_pend();
- if (getEvent(eventReturn, EVENT_TIME_CYCLE_10ms))
- {
- getKeyReturn = KeyValueChange(keyPressValue);
- dealKeyPressProccess();
- }
- if (getEvent(eventReturn, EVENT_TIME_CYCLE_500ms))
- {
- present_adcValue = myADC_getValue();
- present_moduleCurrendValue = (float)present_adcValue/4095*3.3/50/0.5*1000;
- myDisplay_ui_rf_continuos_txCurrent(present_moduleCurrendValue);
- myDisplay_ui_rf_rxPacket_rxCurrent(present_moduleCurrendValue);
- myDisplay_ui_rf_continuos_rxContinuousFreq(rfContinuousFreq);
- uiTimerFlash_callBack();
- myDisplay_ui_rf_continuos_rxErrorRate(rfRxTestRate);
- validPackageCount =0;
- packageCount = 1;
- }
- if (getEvent(eventReturn, EVENT_RF_CONTINUOUS_RX))
- {
- myRadio_setCtrl(RADIO_EXT_CONTROL_RX_SENSITIVITY,
- rfChannelList[deviceInfor.chipType][0]+deviceInfor.rfChannel*1000000);
- startToCountingRx = true;
- packageCount = 1;
- validPackageCount = 1;
- }
- if (getEvent(eventReturn, EVENT_RF_CONTINUOUS_TX))
- {
- myRadio_setTxPower(deviceInfor.txPower);
- myRadio_setCtrl(RADIO_EXT_CONTROL_TX_UNMODULATED,
- rfChannelList[deviceInfor.chipType][0]+deviceInfor.rfChannel*1000000);
- }
- if (getEvent(eventReturn, EVENT_RF_PACKET_TX))
- {
- static uint32_t txCount = 0;
- rfTxPacket.len = strlen("hello world");
- memcpy(rfTxPacket.payload, "hello world", rfTxPacket.len);
- rfTxPacket.payload[rfTxPacket.len] = ((++txCount) & 0x0f) + 0x30;
- rfTxPacket.len ++;
- rfTxPacket.payload[rfTxPacket.len] = 0;
- myDisplay_ui_rf_tx_packet_buffer(rfTxPacket.payload);
- myRadio_transmit(&rfTxPacket);
- event_clear(EVENT_TIMEOUT_CHECK_RF_PACKET);
- }
- if (getEvent(eventReturn, EVENT_RF_PACKET_RX))
- {
- myRadio_receiver();
- validPackageCount = 0;
- packageCount = 0;
- }
- if (getEvent(eventReturn, EVENT_RF_GET_RX_PACKET))
- {
- myRadio_receiver();
- myDisplay_ui_rf_rxPacket_rssi(rfRecvPacket.rssi, 0);
- // myDisplay_ui_rf_rxPacket_rssi(myRadio_getRssi(), 0);
- myDisplay_ui_rf_rxPacket_buffer(rfRecvPacket.payload, 0);
- }
- if (getEvent(eventReturn, EVENT_RF_RX_ERROR))
- {
- myRadio_receiver();
- }
- if (getEvent(eventReturn, EVENT_RF_IDLE))
- {
- startToCountingRx = false;
- // myRadio_abort();
- event_clear(EVENT_RF_PACKET_TX);
- }
- if (getEvent(eventReturn, EVENT_TEST_LED))
- {
- testAllLed();
- }
- if (getEvent(eventReturn, EVENT_UART_RECV))
- {
- // if (uartPackage_Rx.isValid)
- // {
- // uartPackage_Rx.packet = 0;
- // }
- }
- if (getEvent(eventReturn, EVENT_TIMEOUT_TO_SAVE_PARAMS))
- {
- deviceInfor.checkSum = crc8_ger((unsigned char*)&deviceInfor, sizeof(userParams_ts) - 1);
- myFlash_write((uint8_t*)&deviceInfor, sizeof(userParams_ts));
- }
-
- keyPressValue = keyScan();
- myRadio_process();
-
- }
- }
|