#include "project.h"
/*
初始化
*/
#define ML302_PWR_KEY_GPIO_PORT GPIOA
#define ML302_PWR_KEY_GPIO_PIN GPIO_PIN_4
#define ML302_PWR_KEY_GPIO_RCC __HAL_RCC_GPIOA_CLK_ENABLE()
#define ML320_PWR_KEY_OUT(v) (v?(ML302_PWR_KEY_GPIO_PORT->BSRR = ML302_PWR_KEY_GPIO_PIN):(ML302_PWR_KEY_GPIO_PORT->BRR = ML302_PWR_KEY_GPIO_PIN))
#define ML302_MOD_RST_GPIO_PORT GPIOA
#define ML302_MOD_RST_GPIO_PIN GPIO_PIN_0
#define ML302_MOD_RST_GPIO_RCC __HAL_RCC_GPIOA_CLK_ENABLE()
void ML302_CleanReceiveBuf(void);
/*
ML302 电源控制脚输出
*/
void ML302_PWR_KEY_OUT(int v)
{
if(v){
ML302_PWR_KEY_GPIO_PORT->BSRR = ML302_PWR_KEY_GPIO_PIN;
}
else{
ML302_PWR_KEY_GPIO_PORT->BRR = ML302_PWR_KEY_GPIO_PIN;
}
}
/*
ML302 复位控制脚输出
*/
void ML302_MOD_RST_OUT(int v)
{
if(v){
ML302_MOD_RST_GPIO_PORT->BSRR = ML302_MOD_RST_GPIO_PIN;
}
else{
ML302_MOD_RST_GPIO_PORT->BRR = ML302_MOD_RST_GPIO_PIN;
}
}
/*
向ML302发送数据
*/
void ML302_UART_Send(uint8_t *pbuf,uint32_t len)
{
while(len--){
while((USART3->SR&0X40)==0);//循环发送,直到发送完毕
USART3->DR = (unsigned char) *pbuf;
pbuf++;
}
}
/*
ML302接收一个字节,这个函数放在中断中。
*/
#define ML302_UART_RECEIVE_BUFF_LEN 128
uint8_t ML302_UART_ReceiveBuff[ML302_UART_RECEIVE_BUFF_LEN],ML302_UART_ReceiveCount;
static uint8_t ReceiveByte0,ReceiveByte1;
uint8_t Flag_Get_OK = 0,Flag_Get_0D0A = 0;
void ML302_UART_ReceiveByte(uint8_t byte)
{
ML302_UART_ReceiveBuff[ML302_UART_ReceiveCount++] = byte;
ReceiveByte1 = byte;
if((ReceiveByte0 == 'O')&&(ReceiveByte1 == 'K')){
Flag_Get_OK = 1;
}
else if((ReceiveByte0 == '\r')&&(ReceiveByte1 == '\n')){
Flag_Get_0D0A = 1;
}
ReceiveByte0 = ReceiveByte1;
if(ML302_UART_ReceiveCount >= (ML302_UART_RECEIVE_BUFF_LEN - 1)){
ML302_UART_ReceiveCount = 0;
}
}
void ML302_IO_Init(void)
{
GPIO_InitTypeDef GPIO_Initure;
ML302_MOD_RST_GPIO_RCC;
ML302_PWR_KEY_GPIO_RCC; //开启GPIOB时钟
GPIO_Initure.Mode=GPIO_MODE_OUTPUT_PP; //推挽输出
GPIO_Initure.Pull=GPIO_PULLUP; //上拉
GPIO_Initure.Speed=GPIO_SPEED_HIGH; //高速
GPIO_Initure.Pin=ML302_PWR_KEY_GPIO_PIN; //
HAL_GPIO_Init(ML302_PWR_KEY_GPIO_PORT,&GPIO_Initure);
GPIO_Initure.Pin=ML302_MOD_RST_GPIO_PIN; //
HAL_GPIO_Init(ML302_MOD_RST_GPIO_PORT,&GPIO_Initure);
ML302_MOD_RST_OUT(1); //处于复位状态
ML302_PWR_KEY_OUT(1); //模组PWR_KEY输入低电平
}
void ML302_Init(void)
{
USART3_Init(115200);
ML302_IO_Init();
}
enum{
ML302_RSP_NULL,//暂时还没有返回数据,
ML302_RSP_TIMEOUT,//等待一段时间什么都没有
ML302_RSP_OK,//得到返回数据,但是不一定是想要的。
ML302_RSP_0D0A,
ML302_RSP_CONNECT
};
/*
send_times 发送次数
*/
uint8_t ML302_Send_ATCommand(uint8_t *buf,uint8_t len,uint32_t timeout,uint8_t send_times,uint8_t *expect)
{
uint8_t ret = ML302_RSP_NULL;
static uint8_t state0 = 0,count = 0;
static uint32_t nextticks = 0;
if(state0 == 0){
ML302_CleanReceiveBuf();//发之前清理缓冲
ML302_UART_Send(buf,len);
printf("ML302>>>:%s\r\n",buf);
state0 = 1;
nextticks = HAL_GetTick() + timeout;
Flag_Get_0D0A = 0;
Flag_Get_OK = 0;
count++;
}
else if(state0 == 1){
if(HAL_GetTick() >= nextticks){
if(send_times == count){//多次发送之后 还是没有反应就返回
ret = ML302_RSP_TIMEOUT;
count = 0;
}
state0 = 0;
}
else{
if(Flag_Get_0D0A && (strstr((const char*)ML302_UART_ReceiveBuff,(const char*)expect))){
ret = ML302_RSP_OK;
state0 = 0;
count = 0;
}
}
}
return ret;
}
/*
向OneNET平台发送
*/
uint32_t ML302_LossOneNETRSP_Count = 0;
uint8_t ML302_CheckOneNETRSP(void)
{
uint8_t ret = 0;
if(HAL_GetTick() > ML302_LossOneNETRSP_Count){
ret = 1;
}
if(Flag_Get_0D0A && (strstr((const char*)ML302_UART_ReceiveBuff,(const char*)"zjj"))){
ML302_LossOneNETRSP_Count = HAL_GetTick() + 20000;//10s
ML302_CleanReceiveBuf();
Flag_Get_0D0A = 0;
printf("get onenet rsp\r\n");
}
if(Flag_Get_0D0A && (strstr((const char*)ML302_UART_ReceiveBuff,(const char*)"test"))){
ML302_LossOneNETRSP_Count = HAL_GetTick() + 20000;//10s
ML302_CleanReceiveBuf();
Flag_Get_0D0A = 0;
printf("get onenet rsp\r\n");
}
return ret;
}
void ML302_CleanReceiveBuf(void)
{
int i = 0;
for(i = 0;i<ML302_UART_RECEIVE_BUFF_LEN;i++){
ML302_UART_ReceiveBuff[i] = 0;
}
ML302_UART_ReceiveCount = 0;
}
uint8_t ML302_GetTarget(uint8_t *str)
{
return (strstr((const void*)ML302_UART_ReceiveBuff,(const void*)str))?1:0;
}
uint8_t ML302_Connect_OneNET(uint8_t control)
{
static uint8_t state = 0,state0 = 0,nextstate,count = 0;//发送次数计数器
static uint32_t nextticks = 0;
uint8_t ret = 0;
control = control;
switch(state)
{
case 0:
ML302_MOD_RST_OUT(1);//处于非复位状态
HAL_Delay(1000);
ML302_MOD_RST_OUT(0);//处于非复位状态
ML302_PWR_KEY_OUT(0);//模组PWR_KEY输入高电平
HAL_Delay(1000);
ML302_PWR_KEY_OUT(1);
nextticks = HAL_GetTick()+2005;
nextstate = 1;
printf("电源初始化1:完成\r\n");
break;
case 1://开机延时2s
if(HAL_GetTick() >= nextticks){
ML302_PWR_KEY_OUT(0);
nextstate = 2;
printf("电源初始化2:完成\r\n");
}
break;
case 2:
switch(ML302_Send_ATCommand("AT\r\n",4,2000,5,"OK"))
{
case ML302_RSP_OK:
nextstate = 3;
printf("go to state3\r\n");
break;
case ML302_RSP_TIMEOUT:
nextstate = 0xff;
break;
}
break;
case 3:
switch(ML302_Send_ATCommand("AT+CPIN?\r\n",10,2000,5,"OK"))
{
case ML302_RSP_OK:
nextstate = 4;
break;
case ML302_RSP_TIMEOUT:
nextstate = 0xff;
break;
}
break;
case 4:
switch(ML302_Send_ATCommand("AT+CFUN?\r\n",strlen("AT+CFUN?\r\n"),2000,5,"OK"))
{
case ML302_RSP_OK:
nextstate = 5;
break;
case ML302_RSP_TIMEOUT:
nextstate = 0xff;
break;
}
break;
case 5:
switch(ML302_Send_ATCommand("AT+CSQ=?\r\n",strlen("AT+CSQ=?\r\n"),2000,5,"OK"))
{
case ML302_RSP_OK:
nextstate = 8;
break;
case ML302_RSP_TIMEOUT:
nextstate = 0xff;
break;
}
break;
case 7:
switch(ML302_Send_ATCommand("AT+CSQ?\r\n",strlen("AT+CSQ?\r\n"),2000,5,"OK"))
{
case ML302_RSP_OK:
nextstate = 8;
break;
case ML302_RSP_TIMEOUT:
nextstate = 0xff;
break;
}
break;
case 8:
switch(ML302_Send_ATCommand("AT+CGDCONT=1,\"IP\",\"CMIOT\"\r\n",strlen("AT+CGDCONT
- 1
- 2
前往页