stm32l431_xl_current/Drivers/e22/e22.c

116 lines
2.6 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "e22.h"
#include "main.h"
#include "inflash.h"
#include "uart_dev.h"
uint8_t e22_state = 0;
//c0 00 09 0000 00 64 00 0b 41 00 00 WOR定点发送
//c0 00 09 0000 00 64 00 0b 49 00 00 WOR定点收
/* 参数配置 */
e22_config_pack e22_config_data = { 0xc0, 0x00, 0x09, //写0起始9长度
0x00, 0x30, //地址
0x00, //网络ID
0x64, //96008N19.6k
0x00,
0x0A, //信道10
0x40,
0x00, 0x00}; //加密
static unsigned short CRC16(unsigned char *arr_buff, unsigned char len)
{
unsigned short crc=0xFFFF;
unsigned char i, j;
for ( j=0; j<len; j++){
crc=crc ^*arr_buff++;
for ( i=0; i<8; i++){
if( ( crc&0x0001) >0){
crc=crc>>1;
crc=crc^ 0xa001;
}else{
crc=crc>>1;
}
}
}
return crc;
}
void lora_set_mode_normal()
{
HAL_Delay(E22_DELAY_MS);
HAL_GPIO_WritePin(RF_M0_GPIO_Port, RF_M0_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(RF_M1_GPIO_Port, RF_M1_Pin, GPIO_PIN_RESET);
HAL_Delay(E22_DELAY_MS);
}
void lora_set_mode_wor()
{
HAL_Delay(E22_DELAY_MS);
HAL_GPIO_WritePin(RF_M0_GPIO_Port, RF_M0_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(RF_M1_GPIO_Port, RF_M1_Pin, GPIO_PIN_RESET);
HAL_Delay(E22_DELAY_MS);
}
void lora_set_mode_config()
{
HAL_Delay(E22_DELAY_MS);
HAL_GPIO_WritePin(RF_M0_GPIO_Port, RF_M0_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(RF_M1_GPIO_Port, RF_M1_Pin, GPIO_PIN_SET);
HAL_Delay(E22_DELAY_MS);
}
void lora_set_mode_sleep()
{
HAL_Delay(E22_DELAY_MS);
HAL_GPIO_WritePin(RF_M0_GPIO_Port, RF_M0_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(RF_M1_GPIO_Port, RF_M1_Pin, GPIO_PIN_SET);
HAL_Delay(E22_DELAY_MS);
}
void lora_set_mode(e22_mode mode)
{
switch(mode)
{
case NORMAL:
lora_set_mode_normal();
break;
case WOR:
lora_set_mode_wor();
break;
case CONFIG:
lora_set_mode_config();
break;
case SLEEP:
lora_set_mode_sleep();
break;
default:
break;
}
}
void e22_init()
{
/*E22模块上电*/
HAL_GPIO_WritePin(RF_AUX_GPIO_Port, RF_PWR_CTRL_Pin, GPIO_PIN_SET);
lora_set_mode(CONFIG);
uart_dev_write(g_lora_uart_handle, (void *)&e22_config_data, sizeof(e22_config_data)/sizeof(e22_config_data.func));
lora_set_mode(NORMAL);
}