| | |
| | | #include "string.h" |
| | | #include "stdio.h" |
| | | #include "delay.h" |
| | | #include "dw_mbx_anc.h" |
| | | static RadioEvents_t RadioEvents; |
| | | #define USE_MODEM_LORA |
| | | //#define USE_MODEM_FSK |
| | |
| | | TX_TIMEOUT, |
| | | }States_t; |
| | | |
| | | static uint8_t flag_lora_wait_sync = 0; |
| | | |
| | | #define RX_TIMEOUT_VALUE 1000 |
| | | #define BUFFER_SIZE 64 // Define the payload size here |
| | | |
| | |
| | | Radio.Sleep( ); |
| | | //printf("TX SUCCESS2\r\n"); |
| | | } |
| | | static uint8_t rxbuffer[255],rxbuff_len; |
| | | static uint16_t rec_lp_count; |
| | | uint8_t GetLoraBufferAndLen(uint8_t* buffer,uint16_t* lp_count) |
| | | { |
| | | memcpy(buffer,rxbuffer,rxbuff_len); |
| | | memcpy(lp_count,&rec_lp_count,2); |
| | | return rxbuff_len; |
| | | } |
| | | extern uint16_t tx_num; |
| | | void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr ) |
| | | { |
| | | BufferSize = size; |
| | | memcpy( RX_Buffer, payload, BufferSize ); |
| | | rec_lp_count = HAL_LPTIM_ReadCounter(&hlptim1); |
| | | rxbuff_len = size; |
| | | memcpy( rxbuffer, payload, BufferSize ); |
| | | RssiValue = rssi; |
| | | SnrValue = snr; |
| | | |
| | | Radio.Standby(); |
| | | |
| | | if(EnableMaster) |
| | | { |
| | | if(memcmp(RX_Buffer,PongMsg,4)==0) |
| | | { |
| | | LedToggle();//LED闪烁 |
| | | printf("Master RXD PONG!\r\n"); |
| | | |
| | | } |
| | | |
| | | |
| | | // HAL_Delay_nMS(1000); |
| | | // |
| | | // TX_Buffer[0] = 'P'; |
| | | // TX_Buffer[1] = 'I'; |
| | | // TX_Buffer[2] = 'N'; |
| | | // TX_Buffer[3] = 'G'; |
| | | // |
| | | //// crc_value=RadioComputeCRC(TX_Buffer,4,CRC_TYPE_IBM);//计算得出要发送数据包CRC值 |
| | | //// TX_Buffer[4]=crc_value>>8; |
| | | //// TX_Buffer[5]=crc_value; |
| | | //// //Radio.Send( TX_Buffer, 6); |
| | | // Radio.Send( TX_Buffer, 4); |
| | | |
| | | // HAL_GPIO_WritePin( OUT1_GPIO_Port, OUT1_Pin,GPIO_PIN_SET); |
| | | // printf("Master TXD PING!\r\n"); |
| | | // HAL_Delay_nMS( 1 ); |
| | | // HAL_GPIO_WritePin( OUT1_GPIO_Port, OUT1_Pin,GPIO_PIN_RESET); |
| | | |
| | | //printf("Master TXD PING!\r\n"); |
| | | } |
| | | else |
| | | { |
| | | if(memcmp(RX_Buffer,PingMsg,4)==0) |
| | | { |
| | | |
| | | // HAL_GPIO_WritePin( OUT1_GPIO_Port, OUT1_Pin,GPIO_PIN_SET); |
| | | printf("Slave RXD PING!\r\n"); |
| | | // HAL_Delay_nMS( 1 ); |
| | | // HAL_GPIO_WritePin( OUT1_GPIO_Port, OUT1_Pin,GPIO_PIN_RESET); |
| | | |
| | | // LedToggle();//LED闪烁 |
| | | //printf("Slave RXD PING!\r\n"); |
| | | |
| | | // T_R_Init(1); |
| | | |
| | | // TX_Buffer[0] = 'P'; |
| | | // TX_Buffer[1] = 'O'; |
| | | // TX_Buffer[2] = 'N'; |
| | | // TX_Buffer[3] = 'G'; |
| | | |
| | | |
| | | // crc_value=RadioComputeCRC(TX_Buffer,4,CRC_TYPE_IBM);//计算得出要发送数据包CRC值 |
| | | // TX_Buffer[4]=crc_value>>8; |
| | | // TX_Buffer[5]=crc_value; |
| | | // //Radio.Send( TX_Buffer, 6); |
| | | |
| | | |
| | | Radio.Send( TX_Buffer, 4); |
| | | printf("Slave TXD PONG!\r\n"); |
| | | } |
| | | else |
| | | { |
| | | Radio.Rx( RX_TIMEOUT_VALUE ); |
| | | printf("Slave TXD XXXX\r\n"); |
| | | } |
| | | } |
| | | flag_lora_wait_sync = 0; |
| | | SetFlagSyncSuccess(1); |
| | | } |
| | | |
| | | void OnTxTimeout( void ) |
| | |
| | | void OnRxTimeout( void ) |
| | | { |
| | | Radio.Standby(); |
| | | if(EnableMaster) |
| | | { |
| | | TX_Buffer[0] = 'P'; |
| | | TX_Buffer[1] = 'I'; |
| | | TX_Buffer[2] = 'N'; |
| | | TX_Buffer[3] = 'G'; |
| | | |
| | | // crc_value=RadioComputeCRC(TX_Buffer,4,CRC_TYPE_IBM);//计算得出要发送数据包CRC值 |
| | | // TX_Buffer[4]=crc_value>>8; |
| | | // TX_Buffer[5]=crc_value; |
| | | //Radio.Send( TX_Buffer, 6); |
| | | Radio.Send( TX_Buffer, 4); |
| | | |
| | | // HAL_GPIO_WritePin( OUT1_GPIO_Port, OUT1_Pin,GPIO_PIN_SET); |
| | | // HAL_Delay_nMS( 1 ); |
| | | // HAL_GPIO_WritePin( OUT1_GPIO_Port, OUT1_Pin,GPIO_PIN_RESET); |
| | | printf("Master TXD PING!\r\n"); |
| | | } |
| | | else |
| | | { |
| | | Radio.Rx( RX_TIMEOUT_VALUE ); |
| | | } |
| | | flag_lora_wait_sync = 0; |
| | | } |
| | | |
| | | void OnRxError( void ) |
| | | { |
| | | |
| | | flag_lora_wait_sync = 0; |
| | | Radio.Standby(); |
| | | if(EnableMaster) |
| | | { |
| | | TX_Buffer[0] = 'P'; |
| | | TX_Buffer[1] = 'I'; |
| | | TX_Buffer[2] = 'N'; |
| | | TX_Buffer[3] = 'G'; |
| | | |
| | | // crc_value=RadioComputeCRC(TX_Buffer,4,CRC_TYPE_IBM);//计算得出要发送数据包CRC值 |
| | | // TX_Buffer[4]=crc_value>>8; |
| | | // TX_Buffer[5]=crc_value; |
| | | //Radio.Send( TX_Buffer, 6); |
| | | Radio.Send( TX_Buffer, 4); |
| | | // HAL_GPIO_WritePin( OUT1_GPIO_Port, OUT1_Pin,GPIO_PIN_SET); |
| | | // HAL_Delay_nMS( 1 ); |
| | | // HAL_GPIO_WritePin( OUT1_GPIO_Port, OUT1_Pin,GPIO_PIN_RESET); |
| | | printf("Master TXD PING!\r\n"); |
| | | } |
| | | else |
| | | { |
| | | Radio.Rx( RX_TIMEOUT_VALUE ); |
| | | } |
| | | |
| | | } |
| | | |
| | | |
| | |
| | | // TX_Buffer[5]=crc_value; |
| | | |
| | | } |
| | | #define SYNC_CHANNEL_FRQ 48000000 |
| | | uint8_t GetFlagLoraWaitSync(void) |
| | | { |
| | | return flag_lora_wait_sync; |
| | | } |
| | | void SwitchLoraToSyncRec() |
| | | { |
| | | RadioEvents.RxDone = OnRxDone; |
| | | RadioEvents.RxTimeout = OnRxTimeout; |
| | | RadioEvents.RxError = OnRxError; |
| | | Radio.Init( &RadioEvents ); |
| | | Radio.SetChannel( SYNC_CHANNEL_FRQ ); |
| | | flag_lora_wait_sync = 1; |
| | | } |
| | | |