GD32F103作为从站
一、在github上看到一个很好的modbus 开源协议,包括RTU的主站和从站,之前一直关注,今年没更新,主要原因是已经稳定了。最近比较闲,就将这个协议移植到GD32F103上,硬件接口主要是RS485。
二、底层的定时器驱动函数
- #include "tim3.h"
- #include "Sys_Config.h"
- #if MD_USD_SALVE
- #include "MDS_RTU_Serial.h"
- #include "MDS_RTU_Serial_1.h"
- #else
- #include "MDM_RTU_Serial.h"
- #endif
- vu32 sys_tick_100us=0;
- void TIM3_Int_Init(u16 arr,u16 psc)
- {
- timer_parameter_struct modbus_timer_structure;
-
- rcu_periph_clock_enable(RCU_TIMER5);
-
- modbus_timer_structure.alignedmode=TIMER_COUNTER_EDGE;
- modbus_timer_structure.clockdivision=TIMER_CKDIV_DIV1;
- modbus_timer_structure.counterdirection=TIMER_COUNTER_UP;
- modbus_timer_structure.period=arr;
- modbus_timer_structure.prescaler=psc;
- modbus_timer_structure.repetitioncounter=0;
-
- timer_init(TIMER5,&modbus_timer_structure);
-
- timer_interrupt_enable(TIMER5,TIMER_INT_UP);
-
- nvic_irq_enable(TIMER5_IRQn,1,3);
- timer_enable(TIMER5);
- }
- void TIMER5_IRQHandler(void)
- {
- if(timer_interrupt_flag_get(TIMER5,TIMER_INT_FLAG_UP)!=RESET)
- {
- timer_interrupt_flag_clear(TIMER5,TIMER_INT_FLAG_UP);
- sys_tick_100us++;
- #if !MD_RTU_USED_OS
- #if MD_USD_SALVE
- //MDSTimeHandler100US(sys_tick_100us);
- MDSTimeHandler100US_1(sys_tick_100us);
- #else
- MDMTimeHandler100US(sys_tick_100us);
- #endif
- #endif
- }
- }
复制代码
三、底层的USART驱动
- #include "usart3.h"
- #include "Sys_Config.h"
- #if MD_USD_SALVE
- #include "MDS_RTU_Serial_1.h"
- #else
- #include "MDM_RTU_Serial.h"
- #include "MD_RTU_SysInterface.h"
- #include "MDM_RTU_Fun.h"
- #endif
- void RS485RWConvInit(void)
- {
- rcu_periph_clock_enable(RCU_GPIOA);
- gpio_init(GPIOA,GPIO_MODE_OUT_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_1);
- }
- void init_usart3(u32 baudRate)
- {
- rcu_periph_clock_enable(RCU_GPIOA);
- rcu_periph_clock_enable(RCU_USART1);
-
- gpio_init(GPIOA,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_2);
- gpio_init(GPIOA,GPIO_MODE_IN_FLOATING,GPIO_OSPEED_50MHZ,GPIO_PIN_3);
-
- usart_baudrate_set(USART1,baudRate);
- usart_word_length_set(USART1,USART_WL_8BIT);
- usart_stop_bit_set(USART1,USART_STB_1BIT);
- usart_parity_config(USART1,USART_PM_NONE);
- usart_transmit_config(USART1,USART_TRANSMIT_ENABLE);
- usart_receive_config(USART1,USART_RECEIVE_ENABLE);
- usart_hardware_flow_cts_config(USART1,USART_CTS_DISABLE);
- usart_hardware_flow_rts_config(USART1,USART_RTS_DISABLE);
-
- usart_interrupt_enable(USART1,USART_INT_RBNE);
-
- usart_enable(USART1);
-
- nvic_irq_enable(USART1_IRQn,0,0);
-
- RS485RWConvInit();
- }
- void usart3_send_byte(u8 byte)
- {
- while(usart_flag_get(USART1,USART_FLAG_TC )==RESET);
- usart_data_transmit(USART1,byte);
- while(usart_flag_get(USART1,USART_FLAG_TC )==RESET);
- }
- void usart3_send_bytes(u8 *bytes,int len)
- {
- int i;
- for(i=0;i
- {
- usart3_send_byte(bytes[i]);
- }
- }
- void usart3_send_string(char *string)
- {
- while(*string)
- {
- usart3_send_byte(*string++);
- }
- }
- void USART1_IRQHandler(void)
- {
- if (usart_interrupt_flag_get(USART1, USART_INT_FLAG_RBNE) != RESET)
- {
- uint8_t data = usart_data_receive(USART1);
- #if !MD_RTU_USED_OS
- #if MD_USD_SALVE
- MDSSerialRecvByte_1(data);
- #else
- #if MDM_USD_USART3
- MDMSerialRecvByte(data);
- #endif
- #endif
- #else
- extern Modbus_RTU modbus_RTU;
- MD_RTU_MsgPut((PModbusBase)(&modbus_RTU), MD_RTU_MSG_HANDLE_ARG(&modbus_RTU),(void*)(data),0);
- #endif
- }
- }
复制代码
四、从站主函数
- #include "bitband.h"
- #include "systick.h"
- #include "usart.h"
- #include "tim3.h"
- #include "Sys_Config.h"
- #include "MDM_RTU_RW_Man.h"
- #if MD_USD_SALVE
- #include "MDS_RTU_APP.h"
- #include "MDS_RTU_APP_1.h"
- #else
- #include "MDM_RTU_APP.h"
- #include "MDM_RTU_Fun.h"
- #include "MDM_RTU_User_Fun.h"
- #endif
- #if MD_RTU_USED_OS
- PTASK_TCB task0;
- PTASK_TCB task1;
- PTASK_TCB task2;
- extern Modbus_RTU_CB modbusRWRTUCB;
- extern Modbus_RTU_CB modbusRWRTUCB1;
- void mdTestTask0(void *arg){
- uint16 temp;
- for(;;){
- if(ERR_RW_FIN==MDM_RTU_ReadHoldReg(&modbusRWRTUCB,1,0,1)){
- MDM_RTU_ReadRegs(modbusRWRTUCB.pModbus_RTU,0,1, (&temp),HOLD_REGS_TYPE,0x1);
- }
- OSTaskDelay(500);
- }
- }
- void mdTestTask1(void *arg){
- uint16 temp=0;
- for(;;){
- if(ERR_RW_FIN==MDM_RTU_WriteSingleReg(&modbusRWRTUCB1,2,0,temp)){
- //MDM_RTU_ReadRegs(modbusRWRTUCB1.pModbus_RTU,0,1, (&temp),HOLD_REGS_TYPE,2);
- }
- temp++;
- OSTaskDelay(500);
- }
- }
- #endif
- int main(void)
- {
- #if MD_RTU_USED_OS
- OSInit();
- #endif
- //delay_init();
- nvic_priority_group_set(NVIC_PRIGROUP_PRE2_SUB2);
- systick_set(96);
-
- usart_init(9600);
- #if MD_USD_SALVE
- //MDS_RTU_APPInit();
- MDS_RTU_APPInit_1();
- #else
- MDM_RTU_APPInit();
-
- #endif
- TIM3_Int_Init(96-1,100-1);
-
- #if MD_RTU_USED_OS
- task1=OSCreateTask(mdTestTask0,NULL,6,256);
- task2=OSCreateTask(mdTestTask1,NULL,6,256);
- SysTick_Config(SystemCoreClock / OS_TICKS_PER_SEC);
- OSStart();
- #endif
- while(1){
- #if !MD_RTU_USED_OS
- #if MD_USD_SALVE
- //MDS_RTU_Loop();
- MDS_RTU_Loop_1();
- #else
- MDM_RTU_Loop();
- MDM_RW_CtrlLoop();
- #endif
- #endif
- }
- }
复制代码
五、主站主函数
- #include "bitband.h"
- #include "systick.h"
- #include "usart.h"
- #include "tim3.h"
- #include "Sys_Config.h"
- #include "MDM_RTU_RW_Man.h"
- #if MD_USD_SALVE
- #include "MDS_RTU_APP.h"
- #include "MDS_RTU_APP_1.h"
- #else
- #include "MDM_RTU_APP.h"
- #include "MDM_RTU_Fun.h"
- #include "MDM_RTU_User_Fun.h"
- #endif
- int main(void)
- {
- nvic_priority_group_set(NVIC_PRIGROUP_PRE2_SUB2);
- systick_set(96);
-
- usart_init(9600);
-
-
- #if MD_RTU_USED_OS
- OSInit();
- #endif
- #if MD_USD_SALVE
- MDS_RTU_APPInit();
- MDS_RTU_APPInit_1();
- #else
- MDM_RTU_APPInit();
-
- #endif
- TIM3_Int_Init(96-1,100-1);
-
- #if MD_RTU_USED_OS
- task1=OSCreateTask(mdTestTask0,NULL,6,256);
- task2=OSCreateTask(mdTestTask1,NULL,6,256);
- SysTick_Config(SystemCoreClock / OS_TICKS_PER_SEC);
- OSStart();
- #endif
- while(1){
- #if !MD_RTU_USED_OS
- #if MD_USD_SALVE
- MDS_RTU_Loop();
- MDS_RTU_Loop_1();
- #else
- MDM_RTU_Loop();
- MDM_RW_CtrlLoop();
- #endif
- #endif
- }
-
-
- }
复制代码
GD32F103作为主站
|