372 lines
12 KiB
C
372 lines
12 KiB
C
/************************************************************************************
|
||
* @Copyright (c) :(C)2017, Qindao ieslab Co., Ltd
|
||
* @FileName :pic24_nb_uart_driver.h
|
||
* @Author :QDIES PLATFORM TEAM
|
||
* @Version :V1.0
|
||
* @Date :2017-03-01
|
||
* @Description :
|
||
************************************************************************************/
|
||
|
||
#ifndef __NB_UART_DRIVER_BC28_H
|
||
#define __NB_UART_DRIVER_BC28_H
|
||
|
||
#include "sys_config.h"
|
||
#ifdef NB_MODULE_BC28
|
||
#include "ddl.h"
|
||
|
||
#define HDSC_HC32L196
|
||
|
||
|
||
#define CELL_ID_SIZE 10
|
||
#define NBUART_RCV_OVERTIME_S1 1
|
||
|
||
//所有串口,将要开启的串口置1
|
||
#define NB_LPCOM_OPEN 0 //用于BC95
|
||
#define NB_COM_OPEN 1 //用于BC95
|
||
|
||
//串口IO配置(IO一定要配置准确)、优先级、时钟及定时器配置
|
||
#define LPUART_TXRX_AF_NULL (GpioAf0)
|
||
#define UART_TXRX_AF_NULL (GpioAf0)
|
||
|
||
#if NB_LPCOM_OPEN
|
||
//LPUART0
|
||
#define NBLPUART0_RX_PORT (GpioPortB)
|
||
#define NBLPUART0_RX_PIN (GpioPin11)
|
||
#define NBLPUART0_RX_AF (GpioAf3)
|
||
|
||
#define NBLPUART0_TX_PORT (GpioPortB)
|
||
#define NBLPUART0_TX_PIN (GpioPin12)
|
||
#define NBLPUART0_TX_AF (GpioAf3)
|
||
|
||
#endif
|
||
|
||
#if NB_COM_OPEN
|
||
//UART1
|
||
#define NBUART_RX_PORT (GpioPortA)
|
||
#define NBUART_RX_PIN (GpioPin3)
|
||
#define NBUART_RX_AF (GpioAf1)
|
||
|
||
#define NBUART_TX_PORT (GpioPortA)
|
||
#define NBUART_TX_PIN (GpioPin2)
|
||
#define NBUART_TX_AF (GpioAf1)
|
||
|
||
#endif
|
||
|
||
#define NBCOM_MAX_NUM 2
|
||
#define NBLPCOM0 0 //低功耗串口
|
||
#define NBLPCOM1 1 //串口
|
||
|
||
//UART波特率
|
||
#define BAUD_RATE_1200 416 //晶振:4M
|
||
#define BAUD_RATE_2400 207 //晶振:4M
|
||
#define BAUD_RATE_9600 47 //晶振:4M
|
||
#define BAUD_RATE_115200 3 //晶振:4M
|
||
//UART数据位
|
||
#define DATABIT_EIGHT 8
|
||
#define DATABIT_NINE 9
|
||
//UART停止位
|
||
#define STOP_ONE 1
|
||
#define STOP_TWO 2
|
||
//UART校验位
|
||
#define NO_CHECK 0 //无校验
|
||
#define ODD_CHECK 1 //奇校验
|
||
#define EVEN_CHECK 2 //偶校验
|
||
|
||
#define NBIOT_FUNCTION 1 //NBIOT功能裁剪(1:保留该功能 0:裁剪掉该功能)
|
||
#define USE_ALT 1 //1:使用备用中断向量表 0:使用主中断向量表
|
||
#define AT_ERROR 5 //AT发送失败时,重发次数
|
||
|
||
#define NB_RCV_COAP_DATA 0
|
||
#define NB_RCV_UDP1_DATA 1
|
||
#define NB_RCV_UDP2_DATA 2
|
||
|
||
//UART查询的状态信息
|
||
#define UART_ING ((s8)-1) //查询中
|
||
#define UART_FINISH ((s8)-2) //完成
|
||
#define UART_ERROR ((s8)-3) //错误帧
|
||
#define UART_OK ((s8)-4) //正确帧
|
||
|
||
|
||
/*************************************普通使用者无需关心下述宏定义******************************************/
|
||
//串口接收状态机中的状态变量S0-S10
|
||
#define S_IDLE 0
|
||
#define S0 10
|
||
#define S1 11
|
||
#define S2 12
|
||
#define S3 13
|
||
#define S4 14
|
||
#define S5 15
|
||
#define S6 16
|
||
#define S7 17
|
||
#define S8 18
|
||
#define S9 19
|
||
#define S10 20
|
||
#define S11 21
|
||
#define S12 22
|
||
#define S13 23
|
||
#define S14 24
|
||
#define S15 25
|
||
#define S16 26
|
||
#define S17 27
|
||
#define S18 28
|
||
#define S19 29
|
||
#define S20 30
|
||
#define S21 31
|
||
#define S22 32
|
||
#define S23 33
|
||
#define S24 34
|
||
#define S25 35
|
||
#define S26 36
|
||
#define S27 37
|
||
#define S28 38
|
||
#define S29 39
|
||
#define S30 40
|
||
|
||
#define S6_1 50
|
||
#define S7_1 51
|
||
#define S8_1 52
|
||
#define S9_1 53
|
||
#define S10_1 54
|
||
#define S11_1 55
|
||
|
||
|
||
|
||
|
||
//以下为配置AT指令编号
|
||
#define cst_AT_No_AT 0
|
||
#define cst_AT_No_AT_CFUN 1
|
||
#define cst_AT_No_AT_NBAND 2
|
||
#define cst_AT_No_AT_CGATT 3
|
||
#define cst_AT_No_AT_CSQ 4
|
||
#define cst_AT_No_AT_CGATT_how 5
|
||
#define cst_AT_No_AT_CEREG 6
|
||
#define cst_AT_No_AT_CSCON 7
|
||
#define cst_AT_No_AT_CIMI 8
|
||
//以下是NBIOT的AT指令编号
|
||
#define cst_AT_No_AT_CGDCONT 9
|
||
#define cst_AT_No_AT_NSOCR 10
|
||
#define cst_AT_No_AT_NSOST 11
|
||
#define cst_AT_No_AT_NBIOT_REV 12
|
||
#define cst_AT_No_AT_DATA_REV 13
|
||
//以下为注销GSM网络AT指令
|
||
#define cst_AT_No_AT_NSOCL 14
|
||
#define cst_AT_No_AT_NETCLOSE 15
|
||
#define cst_AT_No_AT_COPS 16
|
||
|
||
#define cst_AT_No_AT_CGSN 17
|
||
#define cst_AT_No_AT_NCDP 18
|
||
#define cst_AT_No_AT_NCDP_C 19
|
||
#define cst_AT_No_AT_NSMI 20
|
||
#define cst_AT_No_AT_NNMI 21
|
||
#define cst_AT_No_AT_NMGS 22
|
||
#define cst_AT_No_AT_CFUN_CK 23
|
||
#define cst_AT_No_AT_NUESTATS 24
|
||
#define cst_AT_NBAND 25
|
||
#define cst_AT_No_AT_RD_REV 26
|
||
#define cst_AT_No_AT_NCDP_CK 27
|
||
#define cst_AT_No_AT_DATE_TIME 28
|
||
#define cst_AT_No_AT_IMEI 29
|
||
//以下为移动ONENET指令
|
||
#define cst_AT_No_AT_MIPLDEL 30
|
||
#define cst_AT_No_AT_MIPLCONFIG 31
|
||
#define cst_AT_No_AT_MIPLADDOBJ 32
|
||
#define cst_AT_No_AT_MIPLNOTIFY 33
|
||
#define cst_AT_No_AT_MIPLOPEN 34
|
||
#define cst_AT_No_AT_MIPLWRITE 35
|
||
#define cst_AT_No_AT_MIPLNOTIFY_ACKID 36
|
||
#define cst_AT_No_AT_CPSMS 37
|
||
#define cst_AT_No_AT_MIPLCLOSE 38
|
||
#define cst_AT_No_AT_MIPLDELOBJ 39
|
||
#define cst_AT_No_AT_CMEE 40
|
||
#define cst_AT_No_AT_CCLK 41
|
||
#define cst_AT_No_AT_NCSEARFCN 42
|
||
#define cst_AT_No_AT_NPTWEDRXS 43
|
||
|
||
#define cst_AT_No_AT_MIPLCREATE 44
|
||
#define cst_AT_No_AT_MIPLDISCOVERRSP 45
|
||
#define cst_AT_No_AT_MIPLDISCOVER 46
|
||
#define cst_AT_No_AT_ICCID 47
|
||
#define cst_AT_No_AT_CFUN0 48
|
||
#define cst_AT_No_AT_CFUN1 49
|
||
|
||
#define cst_AT_No_AT_QIOPEN 50
|
||
#define cst_AT_No_AT_UDP_REV 51
|
||
#define cst_AT_No_AT_UPD_READ 52
|
||
|
||
#define cst_AT_No_AT_QPOWD 53
|
||
#define cst_AT_No_AT_MIPLCONFIG_CK 54
|
||
#define cst_AT_No_AT_NMGS_ACKID 55
|
||
#define cst_AT_QSCLK_OPEN 56
|
||
#define cst_AT_QSCLK_CLOSE 57
|
||
#define cst_AT_No_CSCON1 58
|
||
#define cst_AT_No_AT_NSONMI 59
|
||
//波特率
|
||
#define NBBAUD1200 0
|
||
#define NBBAUD2400 1
|
||
#define NBBAUD4800 2
|
||
#define NBBAUD9600 3
|
||
#define NBBAUD19200 4
|
||
#define NBBAUD38400 5
|
||
#define NBBAUD57600 6
|
||
#define NBBAUD115200 7
|
||
|
||
//时钟频率
|
||
#define NBUART1_CLOCK_HZ 2000000
|
||
#define NBUART2_CLOCK_HZ 2000000
|
||
#define NBUART3_CLOCK_HZ 2000000
|
||
#define NBUART4_CLOCK_HZ 2000000
|
||
#define NBUART5_CLOCK_HZ 2000000
|
||
#define NBLPUART_CLOCK_HZ 3686400 //32768
|
||
#define NBUART_CLOCK_HZ(_COM_) (((_COM_) == NBCOM1) ? NBUART1_CLOCK_HZ : ((_COM_) == NBCOM2) ? NBUART2_CLOCK_HZ : ((_COM_) == NBCOM3) ? NBUART3_CLOCK_HZ : ((_COM_) == NBCOM4) ? NBUART4_CLOCK_HZ : ((_COM_) == NBCOM5) ? NBUART5_CLOCK_HZ : NBLPUART_CLOCK_HZ)
|
||
|
||
//奇偶校验
|
||
#define NBODD_PARITY 0
|
||
#define NBEVEN_PARITY 1
|
||
#define NBNONE_PARITY 2
|
||
|
||
//数据位
|
||
#define NBUART_WORDLEN_8B ((u32)0x00000000) //8位
|
||
|
||
//停止位
|
||
#define NBUART_STOPBIT_1 ((u32)0x00000) //1个停止位
|
||
#define NBUART_STOPBIT_1_5 ((u32)0x4000u) //1.5个停止位
|
||
#define NBUART_STOPBIT_2 ((u32)0x8000u) //2个停止位
|
||
//校验
|
||
#define NBUART_NONE_PARITY ((u32)0x00000000) //无校验
|
||
#define NBUART_EVEN_PARITY ((u32)0x00000004) //偶校验
|
||
#define NBUART_ODD_PARITY ((u32)0x00000008) //奇校验
|
||
|
||
#define NBUART_MODE_REC ((u32)0x00000001) // RX mode
|
||
#define NBUART_MODE_SEND ((u32)0x00000000) // TX mode
|
||
#define NBUART_MODE_SEND_REC ((u32)0x00000001) // RX and TX mode
|
||
|
||
//硬件流控制
|
||
#define NBUART_HWCTR_NONE ((u32)0x00000000) // 无硬件流
|
||
|
||
#define NBUART_OVERSAMPLE_16 ((u32)0x0000)
|
||
#define NBUART_OVERSAMPLE_8 ((u32)0x200u)
|
||
|
||
#define NBUART_ONE_BIT_SAMPLE_DIS ((u32)0x00000000)
|
||
#define NBUART_ONE_BIT_SAMPLE_EN ((u32)USART_CR3_ONEBIT)
|
||
|
||
//缓冲器大小(可修改)
|
||
#define NBLPUART0_BUF_SIZE 512
|
||
#define NBLPUART1_BUF_SIZE 512
|
||
|
||
|
||
#define ORDER_WRITE 0 //???????+MIPLWRITE
|
||
#define ORDER_EXECUTE 1 //???????+MIPLEXECUTE
|
||
//运营商
|
||
#define CHINA_MOBILE 0x7A //中国移动
|
||
#define CHINA_UNICOM 0x7B //中国联通
|
||
#define CHINA_TELECOM 0x7C //中国电信
|
||
#define CHINA_MOBILE_IOT 0x7D //中国移动 物联网卡
|
||
|
||
|
||
|
||
#define BC25_UP_BUF_SIZE 512
|
||
//串口通信结构体
|
||
typedef struct
|
||
{
|
||
u8 *send_buf; //发送缓冲区首地址
|
||
u16 send_len; //记录发送的总长度
|
||
u16 send_count; //记录发送的当前位置
|
||
u8 send_style; //记录发送方式(AT指令、短信、NBIOT数据)
|
||
u8 hex_flag; //记录发送的高低位(HEX数据的高低位)
|
||
u16 rev_len; //NBIOT接收数据长度
|
||
u8 rev_buf[BC25_UP_BUF_SIZE*2]; //NBIOT接收缓冲区 ASCII
|
||
u8 rev_buf_handled[BC25_UP_BUF_SIZE]; //处理为hex
|
||
u8 sms_rev_len; //短信接收数据长度
|
||
u8 sms_rev_num[7]; //短信接收手机号
|
||
s8 rev_ack_flag; //接收状态标志位
|
||
u8 sms_total_count; //短信总存储空间
|
||
u8 nbiot_rev_flag; //NBIOT收到数据
|
||
u8 nbiot_rev_ack; //NBIOT收到数据--需要先写应答
|
||
u32 flow_count; //流量计数
|
||
|
||
char nbiot_rev_serial_number[10];//接收ACK_NUM号码
|
||
u8 nbiot_rev_serial_number_cnt;//接收ACK_NUM号码数组指针
|
||
u8 udp1_rev_data_hint;//udp收到消息提示
|
||
u8 udp2_rev_data_hint;//udp收到消息提示
|
||
u8 nb_data_type;
|
||
u8 Onenet_type_down_order;
|
||
char nbiot_discover_num[10];
|
||
} UART_COMM;
|
||
|
||
typedef struct
|
||
{
|
||
u32 BaudRate;
|
||
|
||
u32 WordLength;
|
||
|
||
u32 StopBits;
|
||
|
||
u32 Parity;
|
||
|
||
u32 Mode;
|
||
|
||
u32 HwFlowCtl;
|
||
|
||
u32 OverSampling;
|
||
|
||
u32 OneBitSampling;
|
||
|
||
u8 LPModeFunction;
|
||
|
||
u8 UARTMode;
|
||
|
||
} NBUART_PARA_STRUCT;
|
||
|
||
//******************************* 外部变量***************************************
|
||
|
||
|
||
|
||
//******************************* 外部接口函数***********************************
|
||
|
||
void NbiotUartSend(u8 *buf, u16 len, u8 style);
|
||
s8 NbiotCheckAtAck(void);
|
||
void NbiotClearAtAck(void);
|
||
void GetRevSms(u8 *num, u8 *sms, u8 *len);
|
||
void NbiotRcv(u16 *len, u8 **data);
|
||
u8 NbiotRcvFinish(void);
|
||
void NBUartReceiveIT(void);
|
||
void NbiotClearMsg(void);
|
||
void NbiotGetRSRP(s16 *rsrp_data);
|
||
void NbiotGetSNR(s16 *snr_data);
|
||
void NbiotGetIMEI(char *imei);
|
||
void NbiotGetIMSI(char *imsi);
|
||
void NbiotGetICCID(char *iccid);
|
||
u8 NbiotGetNBAND(void);
|
||
void NbiotGetECL(s16 *ecl_data);
|
||
void NbiotGetCell_ID(u8 *cell_id_data);
|
||
void NbiotGetPCI(u16 *pci_data);
|
||
void NbiotGetCSQ(u8 *csq_data);
|
||
void NbiotGetEARFCN(u16 *earfcn_data);
|
||
void UdpStartCheckData(void);
|
||
void UdpEndCheckData(void);
|
||
u8 UdpIfNeedReadDataBc25(u8 connect_type);
|
||
u8 UdpIfNeedReadData(void);
|
||
//清楚udp接收数据标志
|
||
void ClearUdpReadDataFlag(u8 connect_type);
|
||
u8 GetRcvDataType(void);//获取收到的数据类型
|
||
void SetRcvDataType(u8 temp_type);
|
||
void NbiotGetDiscover_num (char *discover_num);
|
||
u8 NbiotGetRevAck (void);
|
||
void ClearNbiotGetRevAck(void);
|
||
u8 NbiotGetOnenetTypeDownOrder (void);
|
||
void NbiotGetWriteAck_num (char *ack_num);
|
||
void SetUartCommRevFlag (void);
|
||
void NbiotSetRevMipldiscover(void);
|
||
//沈阳规约修改,UDP接到数据再查一次,应答帧和下发指令间隔太短,不查会丢指令
|
||
u8 Check_Udp_Data_Received(void);
|
||
void Clear_Udp_Rec_Data_Flag(void);
|
||
void UdpReadStart(void);
|
||
void UdpReadStop(void);
|
||
u8 getTempUdpCode(void);
|
||
u8 getRegindState(void);
|
||
void clearRegindState(void);
|
||
#endif
|
||
|
||
#endif /* __PIC24_NB_UART_DRIVER_H */
|
||
|