|
|
|
@@ -0,0 +1,97 @@ |
|
|
|
#include "stm32f4xx.h" |
|
|
|
#include "hardware.h" |
|
|
|
|
|
|
|
uint8_t ModbusRxBuf[MODBUS_RX_BUF_SIZE]; |
|
|
|
uint16_t ModbusRxLen = 0; |
|
|
|
|
|
|
|
void ModbusGpioInit(void) |
|
|
|
{ |
|
|
|
GPIO_InitTypeDef GPIO_InitStructure; |
|
|
|
|
|
|
|
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); |
|
|
|
|
|
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10; |
|
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; |
|
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; |
|
|
|
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; |
|
|
|
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; |
|
|
|
GPIO_Init(GPIOA, &GPIO_InitStructure); |
|
|
|
|
|
|
|
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1); |
|
|
|
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1); |
|
|
|
} |
|
|
|
|
|
|
|
void ModbusUsartInit(void) |
|
|
|
{ |
|
|
|
USART_InitTypeDef USART_InitStructure; |
|
|
|
|
|
|
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); |
|
|
|
|
|
|
|
USART_InitStructure.USART_BaudRate = MODBUS_USART_BAUDRATE; |
|
|
|
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); |
|
|
|
|
|
|
|
USART_ITConfig(USART1, USART_IT_IDLE, ENABLE); |
|
|
|
USART_Cmd(USART1, ENABLE); |
|
|
|
} |
|
|
|
|
|
|
|
void ModbusDmaInit(void) |
|
|
|
{ |
|
|
|
DMA_InitTypeDef DMA_InitStructure; |
|
|
|
|
|
|
|
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE); |
|
|
|
|
|
|
|
/* DMA2_Stream2 用于 USART1_RX */ |
|
|
|
DMA_DeInit(DMA2_Stream2); |
|
|
|
DMA_InitStructure.DMA_Channel = DMA_Channel_4; |
|
|
|
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&(USART1->DR); |
|
|
|
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)ModbusRxBuf; |
|
|
|
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; |
|
|
|
DMA_InitStructure.DMA_BufferSize = MODBUS_RX_BUF_SIZE; |
|
|
|
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; |
|
|
|
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; |
|
|
|
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; |
|
|
|
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; |
|
|
|
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; |
|
|
|
DMA_InitStructure.DMA_Priority = DMA_Priority_High; |
|
|
|
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; |
|
|
|
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_1QuarterFull; |
|
|
|
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; |
|
|
|
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; |
|
|
|
DMA_Init(DMA2_Stream2, &DMA_InitStructure); |
|
|
|
|
|
|
|
/* DMA2_Stream7 用于 USART1_TX */ |
|
|
|
DMA_DeInit(DMA2_Stream7); |
|
|
|
DMA_InitStructure.DMA_Channel = DMA_Channel_4; |
|
|
|
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&(USART1->DR); |
|
|
|
DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral; |
|
|
|
DMA_Init(DMA2_Stream7, &DMA_InitStructure); |
|
|
|
|
|
|
|
/* 使能USART的DMA请求 */ |
|
|
|
USART_DMACmd(USART1, USART_DMAReq_Rx | USART_DMAReq_Tx, ENABLE); |
|
|
|
DMA_Cmd(DMA2_Stream2, ENABLE); |
|
|
|
} |
|
|
|
|
|
|
|
void ModbusNvicInit(void) |
|
|
|
{ |
|
|
|
NVIC_InitTypeDef NVIC_InitStructure; |
|
|
|
|
|
|
|
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; |
|
|
|
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 5; |
|
|
|
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; |
|
|
|
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; |
|
|
|
NVIC_Init(&NVIC_InitStructure); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void MdobusHardwareInit(void) |
|
|
|
{ |
|
|
|
ModbusGpioInit(); |
|
|
|
ModbusUsartInit(); |
|
|
|
ModbusDmaInit(); |
|
|
|
ModbusNvicInit(); |
|
|
|
} |