更新代码
This commit is contained in:
parent
b0e4bfe45a
commit
966811baf6
File diff suppressed because one or more lines are too long
@ -1272,7 +1272,7 @@
|
|||||||
<GroupNumber>9</GroupNumber>
|
<GroupNumber>9</GroupNumber>
|
||||||
<FileNumber>83</FileNumber>
|
<FileNumber>83</FileNumber>
|
||||||
<FileType>1</FileType>
|
<FileType>1</FileType>
|
||||||
<tvExp>1</tvExp>
|
<tvExp>0</tvExp>
|
||||||
<tvExpOptDlg>0</tvExpOptDlg>
|
<tvExpOptDlg>0</tvExpOptDlg>
|
||||||
<bDave2>0</bDave2>
|
<bDave2>0</bDave2>
|
||||||
<PathWithFileName>..\..\Src\Usr\Driver\Ldrv\BME680\bme68x.c</PathWithFileName>
|
<PathWithFileName>..\..\Src\Usr\Driver\Ldrv\BME680\bme68x.c</PathWithFileName>
|
||||||
|
|||||||
304
ZheDaXiaoFuZi/Src/Usr/Driver/Ldrv/USR-4GCATDTU/Usr_4GCatDtuDrv.c
Normal file
304
ZheDaXiaoFuZi/Src/Usr/Driver/Ldrv/USR-4GCATDTU/Usr_4GCatDtuDrv.c
Normal file
@ -0,0 +1,304 @@
|
|||||||
|
#include "Usr_4GCatDtuDrv.h"
|
||||||
|
#include "McuBspUart.h"
|
||||||
|
#include "McuBspGPIO.h"
|
||||||
|
#include "ZthyToolCheck.h"
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#define DRV_4GCATDTU_RXBUF_LEN 1024
|
||||||
|
|
||||||
|
static char g4GCatDtuRxBuf[DRV_4GCATDTU_RXBUF_LEN] = {0}; // 能够存放住两帧数据,主要为了容错
|
||||||
|
static unsigned short g4GCatDtuRxBufTemp = 0; // 当前接收数据的末期所在位置
|
||||||
|
static unsigned char g4GCatDtuRetryOpenSta = 0; // 北斗模块当前的开关状态
|
||||||
|
static uint32_t gDeviceType = 0; // 设备类型
|
||||||
|
static uint32_t gDeviceId = 12; // 设备ID
|
||||||
|
|
||||||
|
static uint8_t gTransTBuf4g[220] = {0};
|
||||||
|
|
||||||
|
/* 接收函数 */
|
||||||
|
static void McuDrv4GCatDtuRecv(unsigned char data);
|
||||||
|
/* 获取4G返回值 */
|
||||||
|
static int McuDrv4GCatDtuRead(uint8_t *Buf, uint16_t BufLen);
|
||||||
|
/* 4G发送 */
|
||||||
|
static int McuDrv4GCatDtuSendMessage(uint8_t *Buf, uint16_t BufLen);
|
||||||
|
|
||||||
|
int McuDrv4GCatDtuInit(void)
|
||||||
|
{
|
||||||
|
MCU_BSP_UART_CONF UartConfig = {0};
|
||||||
|
UartConfig.BaudRate = MCU_DRV_4G_CAT_DTU_UART_BAUDRATE;
|
||||||
|
UartConfig.WordLength = MCU_DRV_4G_CAT_DTU_UART_WORDLEN;
|
||||||
|
UartConfig.StopBits = MCU_DRV_4G_CAT_DTU_UART_STOPBITS;
|
||||||
|
UartConfig.Parity = MCU_DRV_4G_CAT_DTU_UART_PARITY;
|
||||||
|
UartConfig.Mode = MCU_DRV_4G_CAT_DTU_UART_MODE;
|
||||||
|
if (McuBspUartInit(MCU_DRV_4G_CAT_DTU_UART_ID, &UartConfig, NULL, NULL) < 0) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
/* 设置接收 */
|
||||||
|
McuBspUartStartRecv(MCU_DRV_4G_CAT_DTU_UART_ID);
|
||||||
|
/* 电源控制 */
|
||||||
|
McuBspGpioInit(MCU_DRV_4G_CAT_DTU_CTRL_POWER_GPIO_PORT, MCU_DRV_4G_CAT_DTU_CTRL_POWER_GPIO_PIN,
|
||||||
|
MCU_DRV_4G_CAT_DTU_CTRL_POWER_GPIO_MODE, MCU_DRV_4G_CAT_DTU_CTRL_POWER_GPIO_SPEED,
|
||||||
|
MCU_DRV_4G_CAT_DTU_CTRL_POWER_GPIO_PULL, 0, NULL);
|
||||||
|
McuDrv4GCatDtuClosePower();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 设置4G连接 */
|
||||||
|
void McuDrv4GCatDtuSetConnOpt(unsigned char type, unsigned char *Ipbuf, unsigned short port)
|
||||||
|
{
|
||||||
|
char buff[128] = {0};
|
||||||
|
// 切换到4G接收
|
||||||
|
McuDrv4GCatDtuSetRecv();
|
||||||
|
McuDrv4GCatDtuSendMessage((uint8_t *)"+++", (uint16_t)strlen("+++"));
|
||||||
|
HAL_Delay(800);
|
||||||
|
McuDrv4GCatDtuSendMessage((uint8_t *)"a", (uint16_t)strlen("a"));
|
||||||
|
HAL_Delay(800);
|
||||||
|
if (type == MCU_DRV_4G_CAT_DTU_TCP_TYPE) {
|
||||||
|
sprintf(buff, "AT+SOCKA=TCP,%s,%d\r\n", Ipbuf, port);
|
||||||
|
McuDrv4GCatDtuSendMessage((uint8_t *)buff, (uint16_t)strlen(buff));
|
||||||
|
HAL_Delay(800);
|
||||||
|
sprintf(buff, "AT+SOCKASL=LONG\r\n");
|
||||||
|
McuDrv4GCatDtuSendMessage((uint8_t *)buff, (uint16_t)strlen(buff));
|
||||||
|
HAL_Delay(800);
|
||||||
|
} else {
|
||||||
|
sprintf(buff, "AT+SOCKA=UDP,%s,%d\r\n", Ipbuf, port);
|
||||||
|
McuDrv4GCatDtuSendMessage((uint8_t *)buff, (uint16_t)strlen(buff));
|
||||||
|
HAL_Delay(800);
|
||||||
|
}
|
||||||
|
sprintf(buff, "AT+S\r\n");
|
||||||
|
McuDrv4GCatDtuSendMessage((uint8_t *)buff, (uint16_t)strlen(buff));
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 查询4G模块的通信状态 */
|
||||||
|
int McuDrv4GCatDtuSta(void)
|
||||||
|
{
|
||||||
|
char Rbuf[64] = {0};
|
||||||
|
char buff[128] = {0};
|
||||||
|
int Csq;
|
||||||
|
char *p;
|
||||||
|
// 切换到4G接收
|
||||||
|
McuDrv4GCatDtuSetRecv();
|
||||||
|
McuDrv4GCatDtuSendMessage((uint8_t *)"+++", (uint16_t)strlen("+++"));
|
||||||
|
HAL_Delay(800);
|
||||||
|
McuDrv4GCatDtuSendMessage((uint8_t *)"a", (uint16_t)strlen("a"));
|
||||||
|
HAL_Delay(800);
|
||||||
|
sprintf(buff, "AT+CSQ\r\n");
|
||||||
|
McuDrv4GCatDtuSendMessage((uint8_t *)buff, (uint16_t)strlen(buff));
|
||||||
|
HAL_Delay(800);
|
||||||
|
McuDrv4GCatDtuRead((uint8_t *)Rbuf, sizeof(Rbuf));
|
||||||
|
HAL_Delay(800);
|
||||||
|
sprintf(buff, "AT+ENTM\r\n");
|
||||||
|
McuDrv4GCatDtuSendMessage((uint8_t *)buff, (uint16_t)strlen(buff));
|
||||||
|
HAL_Delay(800);
|
||||||
|
p = strstr(Rbuf, "+CSQ:");
|
||||||
|
if (p != NULL) {
|
||||||
|
p[8] = 0;
|
||||||
|
Csq = atoi(p + 6);
|
||||||
|
if (Csq > 100) {
|
||||||
|
return -1;
|
||||||
|
} else {
|
||||||
|
return Csq;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*判断电源模块的当前状态*/
|
||||||
|
unsigned char McuDrv4GCatDtuPowerSta(void)
|
||||||
|
{
|
||||||
|
return g4GCatDtuRetryOpenSta;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 设置北斗接收 */
|
||||||
|
void McuDrv4GCatDtuSetRecv(void)
|
||||||
|
{
|
||||||
|
McuBspUartSetRecvCallback(MCU_DRV_4G_CAT_DTU_UART_ID, McuDrv4GCatDtuRecv);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 4G打包发送 */
|
||||||
|
uint32_t McuDrv4GSendMessage(uint16_t MessageId, uint16_t *Serial, uint8_t *SendBuf, uint32_t SendBufLen)
|
||||||
|
{
|
||||||
|
uint32_t i = 0;
|
||||||
|
uint32_t j = 0;
|
||||||
|
uint32_t k = 0;
|
||||||
|
uint32_t e =0;
|
||||||
|
uint32_t RealSendLen = 0;
|
||||||
|
uint16_t SendPiece = 0; // 发送条数,用来记录是否具有分包
|
||||||
|
uint16_t MessageProperties = 0; // 消息属性
|
||||||
|
uint8_t SendBufTmp[1050] = {0};
|
||||||
|
if (Serial == NULL) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
SendPiece = (SendBufLen / 1023) + 1; // 计算总共需要发几条信息
|
||||||
|
for (i = 0; i < SendPiece; i++) {
|
||||||
|
if (SendPiece != 1) {
|
||||||
|
if (i != (SendPiece - 1)) {
|
||||||
|
MessageProperties = 0x2000 | 0x0c00 | 1023;
|
||||||
|
} else {
|
||||||
|
MessageProperties = 0x2000 | 0x0c00 | (SendBufLen % 1023);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
MessageProperties = 0x0000 | 0x0c00 | SendBufLen;
|
||||||
|
}
|
||||||
|
j = 0;
|
||||||
|
/* 消息ID */
|
||||||
|
SendBufTmp[j] = (uint8_t)((MessageId & 0xff00) >> 8);
|
||||||
|
j++;
|
||||||
|
SendBufTmp[j] = (uint8_t)(MessageId & 0x00ff);
|
||||||
|
j++;
|
||||||
|
/* 设备类型 */
|
||||||
|
SendBufTmp[j] = (uint8_t)((gDeviceType & 0xff000000) >> 24);
|
||||||
|
j++;
|
||||||
|
SendBufTmp[j] = (uint8_t)((gDeviceType & 0x00ff0000) >> 16);
|
||||||
|
j++;
|
||||||
|
SendBufTmp[j] = (uint8_t)((gDeviceType & 0x0000ff00) >> 8);
|
||||||
|
j++;
|
||||||
|
SendBufTmp[j] = (uint8_t)(gDeviceType & 0x000000ff);
|
||||||
|
j++;
|
||||||
|
/* 设备ID号 */
|
||||||
|
SendBufTmp[j] = (uint8_t)((gDeviceId & 0xff000000) >> 24);
|
||||||
|
j++;
|
||||||
|
SendBufTmp[j] = (uint8_t)((gDeviceId & 0x00ff0000) >> 16);
|
||||||
|
j++;
|
||||||
|
SendBufTmp[j] = (uint8_t)((gDeviceId & 0x0000ff00) >> 8);
|
||||||
|
j++;
|
||||||
|
SendBufTmp[j] = (uint8_t)(gDeviceId & 0x000000ff);
|
||||||
|
j++;
|
||||||
|
/* 消息体属性 */
|
||||||
|
SendBufTmp[j] = (uint8_t)((MessageProperties & 0xff00) >> 8);
|
||||||
|
j++;
|
||||||
|
SendBufTmp[j] = (uint8_t)(MessageProperties & 0x00ff);
|
||||||
|
j++;
|
||||||
|
/* 协议版本号 */
|
||||||
|
SendBufTmp[j] = 0x00;
|
||||||
|
j++;
|
||||||
|
SendBufTmp[j] = 0x01;
|
||||||
|
j++;
|
||||||
|
/* 消息流水号 */
|
||||||
|
SendBufTmp[j] = (uint8_t)(((*Serial) & 0xff00) >> 8);
|
||||||
|
j++;
|
||||||
|
SendBufTmp[j] = (uint8_t)((*Serial) & 0x00ff);
|
||||||
|
j++;
|
||||||
|
/* 消息包装项 + 消息内容 */
|
||||||
|
if (SendPiece != 1) {
|
||||||
|
/* 消息总包数 */
|
||||||
|
SendBufTmp[j] = (uint8_t)((SendPiece & 0xff00) >> 8);
|
||||||
|
j++;
|
||||||
|
SendBufTmp[j] = (uint8_t)(SendPiece & 0x00ff);
|
||||||
|
j++;
|
||||||
|
/* 包序号 */
|
||||||
|
SendBufTmp[j] = (uint8_t)(((i + 1) & 0xff00) >> 8);
|
||||||
|
j++;
|
||||||
|
SendBufTmp[j] = (uint8_t)((i + 1) & 0x00ff);
|
||||||
|
j++;
|
||||||
|
if (i != (SendPiece - 1)) {
|
||||||
|
memcpy(&SendBufTmp[j], &(SendBuf[1023 * i]), 1023);
|
||||||
|
j += 1023;
|
||||||
|
} else {
|
||||||
|
memcpy(&SendBufTmp[j], &(SendBuf[1023 * i]), SendBufLen % 1023);
|
||||||
|
j += (SendBufLen % 1023);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if ((SendBuf != NULL) && (SendBufLen != 0)) {
|
||||||
|
memcpy(&SendBufTmp[j], SendBuf, SendBufLen);
|
||||||
|
j += SendBufLen;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/* 进行校验 */
|
||||||
|
SendBufTmp[j] = 0;
|
||||||
|
ZthyToolCheckCrc8_0x31(SendBufTmp, j, &SendBufTmp[j]);
|
||||||
|
j++;
|
||||||
|
/* 进行转义 */
|
||||||
|
e = 0;
|
||||||
|
gTransTBuf4g[e] = 0x7f; // 标志位
|
||||||
|
e++;
|
||||||
|
for (k = 0; k < j; k++) {
|
||||||
|
if (SendBufTmp[k] == 0x7e) {
|
||||||
|
gTransTBuf4g[e] = 0x7e;
|
||||||
|
e++;
|
||||||
|
gTransTBuf4g[e] = 0x01;
|
||||||
|
e++;
|
||||||
|
} else if (SendBufTmp[k] == 0x7f) {
|
||||||
|
gTransTBuf4g[e] = 0x7e;
|
||||||
|
e++;
|
||||||
|
gTransTBuf4g[e] = 0x02;
|
||||||
|
e++;
|
||||||
|
} else {
|
||||||
|
gTransTBuf4g[e] = SendBufTmp[k];
|
||||||
|
e++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
gTransTBuf4g[e] = 0x7f; // 标志位
|
||||||
|
e++;
|
||||||
|
/* 发送数据 */
|
||||||
|
if (1 == McuDrv4GCatDtuPowerSta()) {
|
||||||
|
McuBspUartSend(MCU_DRV_4G_CAT_DTU_UART_ID, gTransTBuf4g, e);
|
||||||
|
}
|
||||||
|
RealSendLen += e;
|
||||||
|
}
|
||||||
|
return RealSendLen;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 4G发送 */
|
||||||
|
static int McuDrv4GCatDtuSendMessage(uint8_t *Buf, uint16_t BufLen)
|
||||||
|
{
|
||||||
|
if (g4GCatDtuRxBufTemp > 0) {
|
||||||
|
char Rbuf[DRV_4GCATDTU_RXBUF_LEN] = {0};
|
||||||
|
McuDrv4GCatDtuRead((uint8_t *)Rbuf, g4GCatDtuRxBufTemp);
|
||||||
|
}
|
||||||
|
return McuBspUartSend(MCU_DRV_4G_CAT_DTU_UART_ID, Buf, BufLen);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 打开电源 */
|
||||||
|
void McuDrv4GCatDtuOpenPower(void)
|
||||||
|
{
|
||||||
|
g4GCatDtuRetryOpenSta = 1;
|
||||||
|
McuBspGpioSetLevel(MCU_DRV_4G_CAT_DTU_CTRL_POWER_GPIO_PORT, MCU_DRV_4G_CAT_DTU_CTRL_POWER_GPIO_PIN,
|
||||||
|
MCU_BSP_GPIO_OUT_HIGH);
|
||||||
|
HAL_Delay(50);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 关闭电源 */
|
||||||
|
void McuDrv4GCatDtuClosePower(void)
|
||||||
|
{
|
||||||
|
g4GCatDtuRetryOpenSta = 0;
|
||||||
|
McuBspGpioSetLevel(MCU_DRV_4G_CAT_DTU_CTRL_POWER_GPIO_PORT, MCU_DRV_4G_CAT_DTU_CTRL_POWER_GPIO_PIN,
|
||||||
|
MCU_BSP_GPIO_OUT_LOW);
|
||||||
|
HAL_Delay(50);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 获取4G返回值 */
|
||||||
|
static int McuDrv4GCatDtuRead(uint8_t *Buf, uint16_t BufLen)
|
||||||
|
{
|
||||||
|
int i = 0;
|
||||||
|
while (g4GCatDtuRxBufTemp < BufLen){
|
||||||
|
HAL_Delay(10);
|
||||||
|
i++;
|
||||||
|
/* 超时 */
|
||||||
|
if (i >= 100) {
|
||||||
|
if (g4GCatDtuRxBufTemp == 0) {
|
||||||
|
return -1;
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (i = 0; i < g4GCatDtuRxBufTemp; i++) {
|
||||||
|
Buf[i] = g4GCatDtuRxBuf[i];
|
||||||
|
}
|
||||||
|
g4GCatDtuRxBufTemp = 0;
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 接收函数 */
|
||||||
|
static void McuDrv4GCatDtuRecv(unsigned char data)
|
||||||
|
{
|
||||||
|
if (data == 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
g4GCatDtuRxBuf[g4GCatDtuRxBufTemp] = data;
|
||||||
|
g4GCatDtuRxBufTemp ++;
|
||||||
|
g4GCatDtuRxBufTemp = g4GCatDtuRxBufTemp % DRV_4GCATDTU_RXBUF_LEN;
|
||||||
|
}
|
||||||
@ -0,0 +1,40 @@
|
|||||||
|
#ifndef _USR_4G_CAT_DTU_DRV_H_
|
||||||
|
#define _USR_4G_CAT_DTU_DRV_H_
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#define MCU_DRV_4G_CAT_DTU_UART_ID MCU_BSP_UART1_ID
|
||||||
|
#define MCU_DRV_4G_CAT_DTU_UART_BAUDRATE 115200 // 波特率
|
||||||
|
#define MCU_DRV_4G_CAT_DTU_UART_WORDLEN UART_WORDLENGTH_8B // 8位字长
|
||||||
|
#define MCU_DRV_4G_CAT_DTU_UART_STOPBITS UART_STOPBITS_1 // 1位停止位
|
||||||
|
#define MCU_DRV_4G_CAT_DTU_UART_PARITY UART_PARITY_NONE // 无奇偶校验位
|
||||||
|
#define MCU_DRV_4G_CAT_DTU_UART_MODE UART_MODE_TX_RX // 读写模式
|
||||||
|
|
||||||
|
#define MCU_DRV_4G_CAT_DTU_CTRL_POWER_GPIO_PIN GPIO_PIN_9
|
||||||
|
#define MCU_DRV_4G_CAT_DTU_CTRL_POWER_GPIO_PORT GPIOE
|
||||||
|
#define MCU_DRV_4G_CAT_DTU_CTRL_POWER_GPIO_MODE GPIO_MODE_OUTPUT_PP
|
||||||
|
#define MCU_DRV_4G_CAT_DTU_CTRL_POWER_GPIO_PULL GPIO_PULLDOWN
|
||||||
|
#define MCU_DRV_4G_CAT_DTU_CTRL_POWER_GPIO_SPEED GPIO_SPEED_FREQ_HIGH
|
||||||
|
|
||||||
|
#define MCU_DRV_4G_CAT_DTU_TCP_TYPE 0
|
||||||
|
#define MCU_DRV_4G_CAT_DTU_UDP_TYPE 1
|
||||||
|
|
||||||
|
int McuDrv4GCatDtuInit(void);
|
||||||
|
/* 设置4G连接 */
|
||||||
|
void McuDrv4GCatDtuSetConnOpt(unsigned char type, unsigned char *Ipbuf, unsigned short port);
|
||||||
|
/* 查询4G模块的通信状态 */
|
||||||
|
int McuDrv4GCatDtuSta(void);
|
||||||
|
/* 设置北斗接收 */
|
||||||
|
void McuDrv4GCatDtuSetRecv(void);
|
||||||
|
/*判断电源模块的当前状态*/
|
||||||
|
unsigned char McuDrv4GCatDtuPowerSta(void);
|
||||||
|
/* 打开电源 */
|
||||||
|
void McuDrv4GCatDtuOpenPower(void);
|
||||||
|
/* 关闭电源 */
|
||||||
|
void McuDrv4GCatDtuClosePower(void);
|
||||||
|
/*判断电源模块的当前状态*/
|
||||||
|
unsigned char McuDrv4GCatDtuPowerSta(void);
|
||||||
|
/* 4G打包发送 */
|
||||||
|
uint32_t McuDrv4GSendMessage(uint16_t MessageId, uint16_t *Serial, uint8_t *SendBuf, uint32_t SendBufLen);
|
||||||
|
|
||||||
|
#endif
|
||||||
Loading…
Reference in New Issue
Block a user