#include "user_lora.h"
#include "main.h"
#include "string.h"
#include "stdio.h"
#include "usart.h"
uint8_t AT_Buff[30];
uint8_t AT_LENGTH = 0;
//lora模块的AT指令初始化
void usrLoraAT_Init(void)
{
uint16_t temp = 0;
temp = 10;// 0-65535
HAL_GPIO_WritePin(MD11_GPIO_Port, MD11_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(MD10_GPIO_Port, MD10_Pin, GPIO_PIN_RESET);
HAL_Delay(3500);
HAL_GPIO_WritePin(MD10_GPIO_Port, MD10_Pin, GPIO_PIN_SET);
HAL_Delay(3500);
memset((uint8_t *)&AT_Buff,0,sizeof(AT_Buff));
sprintf((char*)AT_Buff,"+++");
HAL_UART_Transmit_DMA(&huart1, (uint8_t *)&AT_Buff, 3);//串口1
HAL_Delay(20);
memset((uint8_t *)&AT_Buff,0,sizeof(AT_Buff));
sprintf((char*)AT_Buff,"a");
HAL_UART_Transmit_DMA(&huart1, (uint8_t *)&AT_Buff, 1);//串口1
HAL_Delay(20);
memset((uint8_t *)&AT_Buff,0,sizeof(AT_Buff));
sprintf((char*)AT_Buff,"AT+WMODE=FP\r\n");
HAL_UART_Transmit_DMA(&huart1, (uint8_t *)&AT_Buff, 13);//串口1
HAL_Delay(200);
memset((uint8_t *)&AT_Buff,0,sizeof(AT_Buff));
sprintf((char*)AT_Buff,"AT+PMODE=RUN\r\n");
HAL_UART_Transmit_DMA(&huart1, (uint8_t *)&AT_Buff, 14);//串口1
HAL_Delay(200);
memset((uint8_t *)&AT_Buff,0,sizeof(AT_Buff));
sprintf((char*)AT_Buff,"AT+UART=115200,8,1,NONE,NFC\r\n");
HAL_UART_Transmit_DMA(&huart1, (uint8_t *)&AT_Buff, 29);//串口1
HAL_Delay(200);
memset((uint8_t *)&AT_Buff,0,sizeof(AT_Buff));
sprintf((char*)AT_Buff,"AT+SPD=2\r\n");
HAL_UART_Transmit_DMA(&huart1, (uint8_t *)&AT_Buff, 10);//串口1
HAL_Delay(900);
memset((uint8_t *)&AT_Buff,0,sizeof(AT_Buff));
sprintf((char *)AT_Buff,"AT+CH=%d\r\n",127);//初始化信道
AT_LENGTH = bufferSizeCalc((uint8_t *)&AT_Buff,sizeof(AT_Buff));
HAL_UART_Transmit_DMA(&huart1, (uint8_t *)&AT_Buff,AT_LENGTH);//串口1
AT_LENGTH = 0;
HAL_Delay(200);
memset((uint8_t *)&AT_Buff,0,sizeof(AT_Buff));
sprintf((char*)AT_Buff,"AT+FEC=OFF\r\n");
HAL_UART_Transmit_DMA(&huart1, (uint8_t *)&AT_Buff, 12);//串口1
HAL_Delay(200);
memset((uint8_t *)&AT_Buff,0,sizeof(AT_Buff));
sprintf((char*)AT_Buff,"AT+PWR=20\r\n");
HAL_UART_Transmit_DMA(&huart1, (uint8_t *)&AT_Buff, 11);//串口1
HAL_Delay(200);
memset((uint8_t *)&AT_Buff,0,sizeof(AT_Buff));
sprintf((char*)AT_Buff,"AT+ADDR=%d\r\n",temp);//初始化ID
AT_LENGTH = bufferSizeCalc((uint8_t *)&AT_Buff,sizeof(AT_Buff));
HAL_UART_Transmit_DMA(&huart1, (uint8_t *)&AT_Buff, AT_LENGTH);//串口1
HAL_Delay(1200);
memset((uint8_t *)&AT_Buff,0,sizeof(AT_Buff));
sprintf((char*)AT_Buff,"AT+Z\r\n");
HAL_UART_Transmit_DMA(&huart1, (uint8_t *)&AT_Buff, 6);//串口1
HAL_Delay(200);
memset((uint8_t *)&AT_Buff,0,sizeof(AT_Buff));
}
//次函数用于计算AT指令的数据长度 方便串口能正确发送数据
uint16_t bufferSizeCalc(uint8_t *buff,uint8_t size)
{
uint8_t i;
uint16_t length = 0;
uint8_t temp;
for(i = 0; i < size; i++)
{
temp = *buff;
if(temp != 0x00)
length ++;
else
break;
buff ++;
}
return length;
}
//点对点定向传输
HAL_StatusTypeDef BT_LoraTransmit(ModChoose mod, uint16_t addres,uint8_t chanel, uint8_t *data,uint8_t length )
{
memset((uint8_t *)&bt_buff,0,sizeof(bt_buff));
bt_buff.ADDH = (uint8_t)(addres>>8);
bt_buff.ADDL = (uint8_t)addres;
bt_buff.CHANEL = chanel;
memcpy((uint8_t *)&bt_buff.tt_buff,(uint8_t *)data,length);
switch(mod)
{
case LORA1: HAL_UART_Transmit_DMA(&huart1, (uint8_t *)&bt_buff, (length+3));
break;
case LORA2: HAL_UART_Transmit_DMA(&huart2, (uint8_t *)&bt_buff, (length+3));//
break;
default:
break;
}
return HAL_OK;
}