瀏覽代碼

Modbus работает на RS485

TelenkovDmitry 1 年之前
父節點
當前提交
f3fbce6790

二進制
doc/~$_модули_входов_выходов_редакция_4.doc


+ 23 - 0
fw/modules/misc/misc.c

@@ -108,6 +108,29 @@ void nop(uint32_t cnt)
         __NOP();
 }
 
+
+
+// PA_2 (pin_25)
+void mb_test_gpio_init(void)
+{
+    gpio_init_type gpio_init_struct;
+    
+    crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE);
+    
+    gpio_default_para_init(&gpio_init_struct);
+
+    // configure the uart tx pin
+    gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
+    gpio_init_struct.gpio_out_type  = GPIO_OUTPUT_PUSH_PULL;
+    gpio_init_struct.gpio_mode = GPIO_MODE_OUTPUT;
+    gpio_init_struct.gpio_pins = GPIO_PINS_2;
+    gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
+    gpio_init(GPIOA, &gpio_init_struct);
+    
+    
+}
+
+
 #if 0
 void USART3_IRQHandler(void)
 {

+ 6 - 0
fw/modules/misc/misc.h

@@ -15,6 +15,12 @@ void modbus_init(void);
 //
 void test_modbus_port_init(void);
 
+//
+void mb_tim_test(void);
+
+//
+void mb_test_gpio_init(void);
+
 
 #endif  // __MISC_H
 

+ 2 - 5
fw/modules/modbus/modbus.c

@@ -1,15 +1,11 @@
 #include "at32f403a_407.h"
 #include "modbus.h"
-//#include "modbus_params.h"
 #include "FreeRTOS.h"
 #include "task.h"
-//#include "settings.h"
 #include "mb.h"
 #include "mbport.h"
-//#include "common_settings.h"
-//#include "sys_api.h"
-//#include "pid_hal.h"
 #include "mbrtu.h"
+#include "tim_delay.h"
 #include <stdio.h>
 #include <string.h>
 #include <stdbool.h>
@@ -47,6 +43,7 @@ void mb_init(void)
     
 	//mb_init_params();
 
+    mb_helper_tim_init(baud);
     eMBInit(MB_RTU, mb_addr, 4, baud, par, stop_bits);
 	eMBSetSlaveID(0x34, TRUE, ucSlaveID, 3);
 	eMBEnable();

+ 8 - 15
fw/user/main.c

@@ -41,9 +41,9 @@ int main(void)
     
     crm_periph_clock_enable(CRM_USB_PERIPH_CLOCK, TRUE);
     
-    //uart_print_init(115200);
+    uart_print_init(115200);
     
-    //printf("\n\n\n\nModule universal IO [FW %s] loading....\r\n\n", VERSION);
+    printf("\n\n\n\nModule universal IO [FW %s] loading....\r\n\n", VERSION);
         
     //
     //usb_init();
@@ -66,24 +66,17 @@ int main(void)
 
 void init_task(void *argument)
 {
-    //static uint32_t cnt = 0;
-       
     //
     //usb_eth_init();
     
     mux_gpio_init();
     
     // Тесты таймеров
-    mux_led_test_init();
-    //modbus_tim_init();
+    //mux_led_test_init();
     
-    // Тесты modbus
-    //modbus_init(); // функция из теста
+    // Modbus
     mb_init();
     
-    // Пока тесты modbus-порта
-    //test_modbus_port_init();
-    
     // Тесты SPI flash
     //common_spi_init();
     //InitFS(PRIM_DRIVE);
@@ -94,14 +87,12 @@ void init_task(void *argument)
     
     for (;;)
     {
-        //printf("%u\r\n", cnt++);
         mux_led_proc();
-        
     }
 
 // -----------------------------------------------------------------------------              
-    vTaskDelete(NULL);
-    taskYIELD();
+    
+    //taskYIELD();
 }
 
 
@@ -109,6 +100,8 @@ void test_hw_task(void *argument)
 {
     for (;;)
     {
+        vTaskDelay(100);
+        
         mux_led_blink();
     }
 }

File diff suppressed because it is too large
+ 526 - 485
project/ewarm/module_universal_io.dep


+ 3 - 0
project/ewarm/module_universal_io.ewp

@@ -2623,6 +2623,9 @@
                 <file>
                     <name>$PROJ_DIR$\..\..\shared\freemodbus\port\porttimer.c</name>
                 </file>
+                <file>
+                    <name>$PROJ_DIR$\..\..\shared\freemodbus\port\tim_delay.c</name>
+                </file>
             </group>
             <group>
                 <name>rtu</name>

+ 3 - 0
project/ewarm/module_universal_io.ewt

@@ -2846,6 +2846,9 @@
                 <file>
                     <name>$PROJ_DIR$\..\..\shared\freemodbus\port\porttimer.c</name>
                 </file>
+                <file>
+                    <name>$PROJ_DIR$\..\..\shared\freemodbus\port\tim_delay.c</name>
+                </file>
             </group>
             <group>
                 <name>rtu</name>

+ 6 - 0
shared/freemodbus/include/mbport.h

@@ -58,6 +58,12 @@ typedef enum
     MB_PAR_EVEN                 /*!< Even parity. */
 } eMBParity;
 
+
+#define MB_RX_SET   gpio_bits_reset(GPIOD, GPIO_PINS_10)
+
+#define MB_TX_SET   gpio_bits_set(GPIOD, GPIO_PINS_10)
+
+
 /* ----------------------- Supporting functions -----------------------------*/
 BOOL            xMBPortEventInit( void );
 

+ 10 - 9
shared/freemodbus/port/portserial.c

@@ -5,7 +5,9 @@
 #include "mb.h"
 #include "port.h"
 #include "mbport.h"
+#include "tim_delay.h"
 #include <stdlib.h>
+#include <stdbool.h>
 
 
 #define MB_UART                 USART3
@@ -16,25 +18,24 @@
 
 void vMBPortSerialEnable( BOOL xRxEnable, BOOL xTxEnable )
 {
-    char foo = 0;
-    
     if (xRxEnable) {
-        gpio_bits_reset(GPIOD, GPIO_PINS_10);
+        // В обработчике прерывания таймера будет включен режим приемника
+        mb_helper_set_tx_state(false);
+        mb_helper_tim_enable(); 
         usart_interrupt_enable(MB_UART, USART_RDBF_INT, TRUE);
 	}
 	else {
-        //gpio_bits_set(GPIOD, GPIO_PINS_10);
-        //foo = usart_data_receive(MB_UART);
         usart_interrupt_enable(MB_UART, USART_RDBF_INT, FALSE);
 	}
 	if (xTxEnable) {
-        //foo = usart_data_receive(MB_UART);
         gpio_bits_set(GPIOD, GPIO_PINS_10);
-        vTaskDelay(100);
-        usart_interrupt_enable(MB_UART, USART_TDBE_INT, TRUE);
+        mb_helper_set_tx_state(true);
+        mb_helper_tim_enable();
 	}
 	else {
-        //gpio_bits_reset(GPIOD, GPIO_PINS_10);
+        // В обработчике прерывания таймера будет включен режим приемника
+        mb_helper_set_tx_state(false);
+        mb_helper_tim_enable();
         usart_interrupt_enable(MB_UART, USART_TDBE_INT, FALSE);
 	}
 }

+ 23 - 33
shared/freemodbus/port/porttimer.c

@@ -4,38 +4,33 @@
 #include "port.h"
 #include "mb.h"
 #include "mbport.h"
+#include <stdbool.h>
 
 
 static void prvvTIMERExpiredISR( void );
 
-static uint16_t timeout = 0;
-static uint16_t downcounter = 0;
 
 
 BOOL xMBPortTimersInit( USHORT usTim1Timerout50us )
 {
     crm_clocks_freq_type crm_clocks_freq_struct = {0};
     
-    crm_periph_clock_enable(CRM_TMR7_PERIPH_CLOCK, TRUE);
-
     crm_clocks_freq_get(&crm_clocks_freq_struct);
-    tmr_base_init(TMR7, 1, 5999);
-    tmr_cnt_dir_set(TMR7, TMR_COUNT_UP);
-        
-    tmr_flag_clear(TMR7, TMR_OVF_FLAG);
-
-    nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);
-    nvic_irq_enable(TMR7_GLOBAL_IRQn, 5, 0);
-    
-    timeout = usTim1Timerout50us;
     
-    tmr_counter_enable(TMR7, FALSE);
+    nvic_irq_disable(TMR7_GLOBAL_IRQn);
     
-    tmr_interrupt_enable(TMR7, TMR_OVF_INT, TRUE);
+    crm_periph_clock_enable(CRM_TMR7_PERIPH_CLOCK, TRUE);
+
+    tmr_base_init(TMR7, (usTim1Timerout50us * 50) - 1, (crm_clocks_freq_struct.ahb_freq / 1000000) - 1);
+    tmr_cnt_dir_set(TMR7, TMR_COUNT_UP);
     
+    NVIC_ClearPendingIRQ(TMR7_GLOBAL_IRQn);
+    nvic_irq_enable(TMR7_GLOBAL_IRQn, 5, 0);
+  
     return TRUE;
 }
 
+
 /* Create an ISR which is called whenever the timer has expired. This function
  * must then call pxMBPortCBTimerExpired( ) to notify the protocol stack that
  * the timer has expired.
@@ -45,49 +40,44 @@ static void prvvTIMERExpiredISR( void )
     ( void )pxMBPortCBTimerExpired(  );
 }
 
+
 //
 void TMR7_GLOBAL_IRQHandler(void)
 {
     if(tmr_flag_get(TMR7, TMR_OVF_FLAG) != RESET)
     {
-        tmr_flag_clear(TMR7, TMR_OVF_FLAG);
-        if (!--downcounter) {
-            vMBPortSetWithinException(TRUE);
-			prvvTIMERExpiredISR();
-            vMBPortSetWithinException(FALSE);
-        }
+      tmr_flag_clear(TMR7, TMR_OVF_FLAG);
+
+      vMBPortSetWithinException(TRUE);
+      prvvTIMERExpiredISR();
+      vMBPortSetWithinException(FALSE);
     }
 }
 
+
 //
 void vMBPortTimerClose( void )
 {
     tmr_interrupt_enable(TMR7, TMR_OVF_INT, FALSE);
     tmr_counter_enable(TMR7, FALSE);
-        
-#if 0  
-	HAL_NVIC_DisableIRQ(TIM6_IRQn);
-	__HAL_RCC_TIM6_CLK_DISABLE();
-#endif
 }
 
 //
 void vMBPortTimersEnable()
 {
-    downcounter = timeout;
-    tmr_counter_enable(TMR7, TRUE);
+    tmr_flag_clear(TMR7, TMR_OVF_FLAG);
     tmr_interrupt_enable(TMR7, TMR_OVF_INT, TRUE);
-#if 0  
-	downcounter = timeout;
-	HAL_TIM_Base_Start_IT(&htim);
-#endif   
+    tmr_counter_value_set(TMR7, 0);
+    tmr_counter_enable(TMR7, TRUE);  
 }
 
 //
 void vMBPortTimersDisable()
 {
+    tmr_flag_clear(TMR7, TMR_OVF_FLAG);
     tmr_interrupt_enable(TMR7, TMR_OVF_INT, FALSE);
-//	HAL_TIM_Base_Stop_IT(&htim);
+    tmr_counter_value_set(TMR7, 0);
+    tmr_counter_enable(TMR7, FALSE);
 }
 
 //

+ 74 - 0
shared/freemodbus/port/tim_delay.c

@@ -0,0 +1,74 @@
+#include "at32f403a_407.h"
+#include "tim_delay.h"
+
+
+static bool tx_enable = false;
+
+
+//
+void mb_helper_tim_init(uint32_t baudrate)
+{
+    float foo;
+    
+    foo = 1.0/((float)baudrate / 11.0);
+    foo *= 2000.0; // время в мс (длительность в два символа)
+    
+    
+    crm_clocks_freq_type crm_clocks_freq_struct = {0};
+    
+    crm_clocks_freq_get(&crm_clocks_freq_struct);
+    
+    nvic_irq_disable(TMR6_GLOBAL_IRQn);
+    
+    crm_periph_clock_enable(CRM_TMR6_PERIPH_CLOCK, TRUE);
+
+    tmr_base_init(TMR6, (uint32_t)(foo * 1000) - 1, (crm_clocks_freq_struct.ahb_freq / 1000000) - 1);
+    tmr_cnt_dir_set(TMR6, TMR_COUNT_UP);
+    
+    NVIC_ClearPendingIRQ(TMR6_GLOBAL_IRQn);
+    nvic_irq_enable(TMR6_GLOBAL_IRQn, 5, 0);
+}
+
+
+//
+void mb_helper_tim_enable(void)
+{
+    tmr_flag_clear(TMR6, TMR_OVF_FLAG);
+    tmr_interrupt_enable(TMR6, TMR_OVF_INT, TRUE);
+    tmr_counter_value_set(TMR6, 0);
+    tmr_counter_enable(TMR6, TRUE);  
+}
+
+
+//
+void mb_helper_tim_disable(void)
+{
+    tmr_flag_clear(TMR6, TMR_OVF_FLAG);
+    tmr_interrupt_enable(TMR6, TMR_OVF_INT, FALSE);
+    tmr_counter_value_set(TMR6, 0);
+    tmr_counter_enable(TMR6, FALSE);
+}
+
+
+//
+void mb_helper_set_tx_state(bool state)
+{
+    tx_enable = state;
+}
+
+
+//
+void TMR6_GLOBAL_IRQHandler(void)
+{
+    if(tmr_flag_get(TMR6, TMR_OVF_FLAG) != RESET)
+    {
+        tmr_flag_clear(TMR6, TMR_OVF_FLAG);
+        if (tx_enable)
+            usart_interrupt_enable(USART3, USART_TDBE_INT, TRUE);
+        else
+            gpio_bits_reset(GPIOD, GPIO_PINS_10);
+        
+        mb_helper_tim_disable();
+    }
+}
+

+ 19 - 0
shared/freemodbus/port/tim_delay.h

@@ -0,0 +1,19 @@
+#ifndef __TIM_DELAY_H
+#define __TIM_DELAY_H
+
+#include <stdbool.h>
+
+//
+void mb_helper_tim_init(uint32_t baudrate);
+
+//
+void mb_helper_tim_enable(void);
+
+//
+void mb_helper_tim_disable(void);
+
+//
+void mb_helper_set_tx_state(bool state);
+
+
+#endif // __TIM_DELAY_H

Some files were not shown because too many files changed in this diff