317 lines
9.2 KiB
C
317 lines
9.2 KiB
C
/**
|
|
******************************************************************************
|
|
* @file tim.c
|
|
* @author Jerry
|
|
* @version V2.1
|
|
* @date 19-April-2022
|
|
* @brief tim program body.
|
|
******************************************************************************
|
|
* @attention
|
|
*
|
|
*
|
|
******************************************************************************
|
|
*/
|
|
|
|
/* Includes ------------------------------------------------------------------*/
|
|
#include "stm32f10x.h"
|
|
#include "global.h"
|
|
|
|
|
|
uint8_t Screen_RevFlg;
|
|
uint8_t Screen_RevCount;
|
|
uint8_t Screen_RevHandlerFlg; //在主函数执行分析处理
|
|
|
|
uint8_t MODBUS_RevFlg;
|
|
uint8_t MODBUS_RevCount;
|
|
|
|
uint8_t MODBUS1_RevFlg;
|
|
uint8_t MODBUS1_RevCount;
|
|
|
|
/*三选一模块*/
|
|
#if BLE_Conn
|
|
uint8_t BLE_RevFlg;
|
|
uint8_t BLE_RevCount;
|
|
#endif
|
|
#if WIFI_Conn
|
|
uint8_t WIFI_RevFlg;
|
|
uint8_t WIFI_RevCount;
|
|
#endif
|
|
#if LTE_Conn
|
|
uint8_t LTE_RevFlg;
|
|
uint8_t LTE_RevCount;
|
|
#endif
|
|
|
|
|
|
/**RS485 网口3.4**/
|
|
//usart1 init
|
|
void uf_UART1_Init( u32 bound )
|
|
{
|
|
GPIO_InitTypeDef GPIO_InitStructure;
|
|
NVIC_InitTypeDef NVIC_InitStructure;
|
|
USART_InitTypeDef USART_InitStructure;
|
|
|
|
/* Enable the USART1 Pins Software Remapping */
|
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE);
|
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
|
|
|
|
USART_InitStructure.USART_BaudRate = bound;
|
|
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
|
|
USART_InitStructure.USART_StopBits = USART_StopBits_1;
|
|
USART_InitStructure.USART_Parity = USART_Parity_No;
|
|
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
|
|
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
|
|
USART_Init(USART1, &USART_InitStructure);
|
|
|
|
/* Configure USART1 Rx (PA.10) as input floating */
|
|
/* Configure USART1 Tx (PA.09) as alternate function push-pull */
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
|
|
GPIO_Init(GPIOA, &GPIO_InitStructure);
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
|
|
GPIO_Init(GPIOA, &GPIO_InitStructure);
|
|
|
|
/* Enable the USART1 Interrupt */
|
|
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
|
|
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3; //485通信主从机 优先级:3,0
|
|
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
|
|
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
|
NVIC_Init(&NVIC_InitStructure);
|
|
|
|
if(bmsMem.E2_485Addr == 1)
|
|
{
|
|
USART_ITConfig(USART1, USART_IT_RXNE, DISABLE); //设备做主机时关闭接收中断
|
|
}
|
|
else
|
|
{
|
|
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //设备做从机时打开接收中断
|
|
}
|
|
//USART_ITConfig(USART1, USART_IT_IDLE, ENABLE);
|
|
USART_Cmd(USART1, ENABLE);
|
|
}
|
|
|
|
|
|
//send multiple bytes
|
|
void USART1_SendMulByte(uint8_t *p, uint8_t size)
|
|
{
|
|
uint8_t i;
|
|
|
|
for(i=0;i<size;i++)
|
|
{
|
|
USART_SendData(USART1, *p);
|
|
while(USART_GetFlagStatus(USART1,USART_FLAG_TC) == RESET);
|
|
p++;
|
|
}
|
|
}
|
|
|
|
//usart1 interrupt
|
|
void USART1_IRQHandler(void)
|
|
{
|
|
uint8_t tmp = tmp;
|
|
|
|
if(USART_GetITStatus(USART1,USART_IT_RXNE) != RESET)
|
|
{
|
|
MODBUS_RevFlg = 1;
|
|
MODBUS_RevCount = 0;
|
|
MODBUS_IT_Receive();
|
|
}
|
|
}
|
|
|
|
/**屏幕**/
|
|
//usart2 init
|
|
void uf_UART2_Init( u32 bound )
|
|
{
|
|
GPIO_InitTypeDef GPIO_InitStructure;
|
|
NVIC_InitTypeDef NVIC_InitStructure;
|
|
USART_InitTypeDef USART_InitStructure;
|
|
|
|
/* Enable the USART1 Pins Software Remapping */
|
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE);
|
|
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
|
|
|
|
USART_InitStructure.USART_BaudRate = bound;
|
|
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
|
|
USART_InitStructure.USART_StopBits = USART_StopBits_1;
|
|
USART_InitStructure.USART_Parity = USART_Parity_No;
|
|
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
|
|
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
|
|
USART_Init(USART2, &USART_InitStructure);
|
|
|
|
/* Configure USART1 Rx (PA.03) as input floating */
|
|
/* Configure USART1 Tx (PA.02) as alternate function push-pull */
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
|
|
GPIO_Init(GPIOA, &GPIO_InitStructure);
|
|
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
|
|
GPIO_Init(GPIOA, &GPIO_InitStructure);
|
|
|
|
/* Enable the USART1 Interrupt */
|
|
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
|
|
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3; //TTL通信屏幕 优先级:3,2
|
|
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
|
|
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
|
NVIC_Init(&NVIC_InitStructure);
|
|
|
|
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
|
|
//USART_ITConfig(USART1, USART_IT_IDLE, ENABLE);
|
|
USART_Cmd(USART2, ENABLE);
|
|
}
|
|
|
|
//usart2 interrupt
|
|
void USART2_IRQHandler(void)
|
|
{
|
|
uint8_t tmp = tmp;
|
|
|
|
if(USART_GetITStatus(USART2,USART_IT_RXNE) != RESET)
|
|
{
|
|
Screen_RevFlg = 1;
|
|
Screen_RevCount = 0;
|
|
Screen_IT_Receive();
|
|
}
|
|
}
|
|
|
|
/**RS485 网口1**/
|
|
//usart3 init
|
|
void uf_UART3_Init( u32 bound )
|
|
{
|
|
GPIO_InitTypeDef GPIO_InitStructure;
|
|
NVIC_InitTypeDef NVIC_InitStructure;
|
|
USART_InitTypeDef USART_InitStructure;
|
|
|
|
/* Enable the USART3 Pins Software Remapping */
|
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB , ENABLE);
|
|
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
|
|
|
|
USART_InitStructure.USART_BaudRate = bound;
|
|
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
|
|
USART_InitStructure.USART_StopBits = USART_StopBits_1;
|
|
USART_InitStructure.USART_Parity = USART_Parity_No;
|
|
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
|
|
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
|
|
USART_Init(USART3, &USART_InitStructure);
|
|
|
|
/* Configure USART3 Rx (PB.11) as input floating */
|
|
/* Configure USART3 Tx (PB.10) as alternate function push-pull */
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
|
|
GPIO_Init(GPIOB, &GPIO_InitStructure);
|
|
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
|
|
GPIO_Init(GPIOB, &GPIO_InitStructure);
|
|
|
|
/* Enable the USART3 Interrupt */
|
|
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
|
|
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3; //485通信上位机 优先级:3,1
|
|
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
|
|
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
|
NVIC_Init(&NVIC_InitStructure);
|
|
|
|
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE); //设备做从机时打开接收中断
|
|
//USART_ITConfig(USART1, USART_IT_IDLE, ENABLE);
|
|
USART_Cmd(USART3, ENABLE);
|
|
}
|
|
|
|
//send multiple bytes
|
|
void USART3_SendMulByte(uint8_t *p, uint8_t size)
|
|
{
|
|
uint8_t i;
|
|
|
|
for(i=0;i<size;i++)
|
|
{
|
|
USART_SendData(USART3, *p);
|
|
while(USART_GetFlagStatus(USART3,USART_FLAG_TC) == RESET);
|
|
p++;
|
|
}
|
|
}
|
|
|
|
//usart3 interrupt
|
|
void USART3_IRQHandler(void)
|
|
{
|
|
uint8_t tmp = tmp;
|
|
|
|
if(USART_GetITStatus(USART3,USART_IT_RXNE) != RESET)
|
|
{
|
|
MODBUS1_RevFlg = 1;
|
|
MODBUS1_RevCount = 0;
|
|
MODBUS1_IT_Receive();
|
|
}
|
|
}
|
|
|
|
/**蓝牙/WIFI/4G模块**/
|
|
//uart4 init
|
|
void uf_UART4_Init( u32 bound )
|
|
{
|
|
GPIO_InitTypeDef GPIO_InitStructure;
|
|
NVIC_InitTypeDef NVIC_InitStructure;
|
|
USART_InitTypeDef USART_InitStructure;
|
|
|
|
/* Enable the UART4 Pins Software Remapping */
|
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
|
|
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE);
|
|
|
|
USART_InitStructure.USART_BaudRate = bound;
|
|
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
|
|
USART_InitStructure.USART_StopBits = USART_StopBits_1;
|
|
USART_InitStructure.USART_Parity = USART_Parity_No;
|
|
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
|
|
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
|
|
USART_Init(UART4, &USART_InitStructure);
|
|
|
|
/* Configure UART4 Rx (PC.11) as input floating */
|
|
/* Configure UART4 Tx (PC.10) as alternate function push-pull */
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
|
|
GPIO_Init(GPIOC, &GPIO_InitStructure);
|
|
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
|
|
GPIO_Init(GPIOC, &GPIO_InitStructure);
|
|
|
|
/* Enable the UART4 Interrupt */
|
|
NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn;
|
|
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //蓝牙通信 优先级:1,1
|
|
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
|
|
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
|
NVIC_Init(&NVIC_InitStructure);
|
|
|
|
USART_ITConfig(UART4, USART_IT_RXNE, ENABLE); //打开接收中断
|
|
//USART_ITConfig(UART4, USART_IT_IDLE, ENABLE);
|
|
USART_Cmd(UART4, ENABLE);
|
|
}
|
|
|
|
//uart4 interrupt
|
|
void UART4_IRQHandler(void)
|
|
{
|
|
if(USART_GetITStatus(UART4,USART_IT_RXNE) != RESET)
|
|
{
|
|
/*三选一模块*/
|
|
#if BLE_Conn
|
|
BLE_RevFlg = 1;
|
|
BLE_RevCount = 0;
|
|
BLE_IT_Receive();
|
|
#endif
|
|
#if WIFI_Conn
|
|
WIFI_RevFlg = 1;
|
|
WIFI_RevCount = 0;
|
|
WIFI_IT_Receive();
|
|
#endif
|
|
#if LTE_Conn
|
|
LTE_RevFlg = 1;
|
|
LTE_RevCount = 0;
|
|
LTE_4G_IT_Receive();
|
|
#endif
|
|
}
|
|
}
|
|
|