diff --git a/modbus/modbus.c b/modbus/modbus.c new file mode 100644 index 0000000..d36e38c --- /dev/null +++ b/modbus/modbus.c @@ -0,0 +1,78 @@ +#include "stm32f4xx.h" +#include "hardware.h" +#include "ucos_ii.h" +#include "os.h" +#include "app_cfg.h" +#include "sys_cfg.h" + +extern OS_EVENT* ModbusReceived; /*接收到modbus请求报文*/ +extern OS_EVENT* ModbusAssembled; /*modbus响应帧组装完成*/ + +/*USART接收空闲中断*/ +void USART1_IRQHandler(void) +{ + OSIntEnter(); /*进入中断*/ + + uint32_t temp; + uint16_t rxLenRemain; + + if(USART_GetITStatus(USART1, USART_IT_IDLE) == SET) + { + /*在计算数据长度之前先关闭DMA接收通道,防止计算过程中DMA继续转运数据*/ + DMA_Cmd(DMA2_Stream2, DISABLE); + + /*计算数据长度*/ + rxLenRemain = DMA_GetCurrDataCounter(DMA2_Stream2); + ModbusRxLen = MODBUS_RX_BUF_SIZE - rxLenRemain; + + if(ModbusRxLen != 0) + { + OSSemPost(ModbusReceived); + } + + /*重启DMA*/ + DMA_SetCurrDataCounter(DMA2_Stream2, MODBUS_RX_BUF_SIZE); + DMA_Cmd(DMA2_Stream2, ENABLE); + + /*清除IDLE中断标志位*/ + temp = USART1->SR; + temp = USART1->DR; + } + + OSIntExit(); /*退出中断*/ +} + + +/*modbus响应帧发送*/ +void ModbusSend(uint8_t *txBuf, uint16_t len) +{ + /*先关闭DMA发送通道*/ + DMA_Cmd(DMA2_Stream7, DISABLE); + + /*等待DMA关闭*/ + while (DMA_GetCmdStatus(DMA2_Stream7) != DISABLE); + + /*配置发送地址和长度*/ + DMA_MemoryTargetConfig(DMA2_Stream7, (uint32_t)txBuf, DMA_Memory_0); + DMA_SetCurrDataCounter(DMA2_Stream7, len); + + /*启动 DMA 发送*/ + DMA_Cmd(DMA2_Stream7, ENABLE); +} + +/*modbus响应帧发送任务*/ +void AppTaskModbusSend (void *p_arg) +{ + + +} + + +/*modbus协议帧解析任务*/ +void AppTaskModbusParse (void *p_arg) +{ + + +} + + diff --git a/modbus/modbus.h b/modbus/modbus.h new file mode 100644 index 0000000..63359a7 --- /dev/null +++ b/modbus/modbus.h @@ -0,0 +1,26 @@ +#ifndef __MODBUS_H +#define __MODBUS_H + +#include "ucos_ii.h" + +/* + * Modbus task synchronization objects. + * + * ModbusReceived: + * USART IDLE ISR -> parse task + * ModbusAssembled: + * parse task -> send task + * ModbusTxCompleted: + * USART TC ISR -> send task + * ModbusTxBufferFree: + * ownership of the single transmit buffer + */ +extern OS_EVENT *ModbusReceived; +extern OS_EVENT *ModbusAssembled; +extern OS_EVENT *ModbusTxCompleted; +extern OS_EVENT *ModbusTxBufferFree; + +void AppTaskModbusParse(void *p_arg); +void AppTaskModbusSend(void *p_arg); + +#endif