Răsfoiți Sursa

Проверил modbus на USART1. Сохраняется проблема на USART3.

TelenkovDmitry 1 an în urmă
părinte
comite
ac7ab2e6d1

+ 86 - 2
fw/modules/misc/misc.c

@@ -1,16 +1,20 @@
-#include "at32f403a_407.h"
 #include "misc.h"
 #include "FreeRTOS.h"
 #include "task.h"
 #include "mux.h"
 #include "mb.h"
 #include "mbport.h"
+#include "at32f403a_407_board.h"
 #include <stdio.h>
 
+char recv_buf[1024] = {0};
 
 // TIM1, 8, 9, 10, 11 тактируются от 120 МГц
 // TIM2, 3, 4, 5, 6, 7, 12, 13, 14 тактируются от 240 МГц
 
+void nop(uint32_t cnt);
+
+
 
 void modbus_tim_init(void)
 {
@@ -32,7 +36,6 @@ void modbus_tim_init(void)
     tmr_interrupt_enable(TMR7, TMR_OVF_INT, TRUE);
 }
 
-
 //
 void modbus_init(void)
 {
@@ -48,6 +51,87 @@ void modbus_init(void)
 #endif    
 }
 
+//
+void test_modbus_port_init(void)
+{
+    gpio_init_type gpio_init_struct;
+
+    crm_periph_clock_enable(CRM_USART3_PERIPH_CLOCK, TRUE);
+    crm_periph_clock_enable(CRM_GPIOD_PERIPH_CLOCK, TRUE);
+    crm_periph_clock_enable(CRM_IOMUX_PERIPH_CLOCK, TRUE);
+    gpio_pin_remap_config(USART3_GMUX_0011, 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_MUX;
+    gpio_init_struct.gpio_pins = GPIO_PINS_8;
+    gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
+    gpio_init(GPIOD, &gpio_init_struct);
+
+    // configure the uart rx pin
+    gpio_init_struct.gpio_mode = GPIO_MODE_INPUT;
+    gpio_init_struct.gpio_pins = GPIO_PINS_9;
+    gpio_init_struct.gpio_pull = GPIO_PULL_UP;
+    gpio_init(GPIOD, &gpio_init_struct);
+    
+    // configure the uart rx 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_10;
+    gpio_init_struct.gpio_pull = GPIO_PULL_UP;
+    gpio_init(GPIOD, &gpio_init_struct);
+    
+    MB_RX_ENABLE;
+    
+    // configure uart param
+    usart_init(USART3, 115200, USART_DATA_8BITS, USART_STOP_1_BIT);
+    usart_parity_selection_config(USART3, USART_PARITY_NONE);
+    
+    usart_flag_clear(USART3, USART_RDBF_FLAG);
+    usart_interrupt_enable(USART3, USART_RDBF_INT, TRUE);
+    usart_receiver_enable(USART3, TRUE);
+    usart_transmitter_enable(USART3, TRUE);
+    usart_enable(USART3, TRUE);  
+    
+    nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);
+    nvic_irq_enable(USART3_IRQn, 5, 0);
+}
+
+
+void nop(uint32_t cnt)
+{
+    for (uint32_t i = 0; i < cnt; i++)
+        __NOP();
+}
+
+#if 0
+void USART3_IRQHandler(void)
+{
+    char rx_byte;
+    static int counter = 0;
+    
+    if (usart_flag_get(USART3, USART_RDBF_FLAG) != RESET) {
+        rx_byte = USART3->dt;
+        PRINT_UART->dt = ((uint16_t)rx_byte & 0x01FF); 
+        recv_buf[counter++] = rx_byte;
+  
+        if (counter == 1024)
+            counter = 0;
+        
+        MB_TX_ENABLE;
+        nop(10000);
+        while(usart_flag_get(USART3, USART_TDBE_FLAG) == RESET);
+        USART3->dt = ((uint16_t)(rx_byte + 1) & 0x01FF);
+        while(usart_flag_get(USART3, USART_TDC_FLAG) == RESET);
+        MB_RX_ENABLE;
+        
+    }  
+}      
+#endif
 
 #if 0
 //

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

@@ -1,6 +1,10 @@
 #ifndef __MISC_H
 #define __MISC_H
 
+#include "at32f403a_407.h"
+
+#define MB_RX_ENABLE gpio_bits_reset(GPIOD, GPIO_PINS_10)
+#define MB_TX_ENABLE gpio_bits_set(GPIOD, GPIO_PINS_10)
 
 //
 void modbus_tim_init(void);
@@ -8,6 +12,9 @@ void modbus_tim_init(void);
 //
 void modbus_init(void);
 
+//
+void test_modbus_port_init(void);
+
 
 #endif  // __MISC_H
 

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

@@ -258,11 +258,12 @@ eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs, eMBRegi
 	uint8_t *ptr_buf = pucRegBuffer;
 	uint16_t size;
 
+/*    
 	printf("usAddress: 0x%X\r\n", usAddress);
 	printf("usNRegs: %u\r\n", usNRegs);
 	printf("eMode: %u\r\n", eMode);
     printf("pucRegBuffer :%X\r\n", pucRegBuffer[0]);
-    
+*/    
     mb_package_flag = true;
     
 	iRegIndex = (int)(usAddress - usRegHoldingStart);

+ 8 - 5
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();
@@ -78,12 +78,15 @@ void init_task(void *argument)
     //modbus_tim_init();
     
     // Тесты modbus
-    //modbus_init();
+    //modbus_init(); // функция из теста
     mb_init();
     
+    // Пока тесты modbus-порта
+    //test_modbus_port_init();
+    
     // Тесты SPI flash
-    common_spi_init();
-    InitFS(PRIM_DRIVE);
+    //common_spi_init();
+    //InitFS(PRIM_DRIVE);
     //spi_flash_test();
       
     // Тесты USB

Fișier diff suprimat deoarece este prea mare
+ 757 - 752
project/ewarm/module_universal_io.dep


+ 88 - 98
shared/freemodbus/port/portserial.c

@@ -1,6 +1,5 @@
 #include "at32f403a_407.h"
-//#include "at32f403a_407_board.h"
-//#include "at32f403a_407_clock.h"
+#include "at32f403a_407_board.h"
 #include "FreeRTOS.h"
 #include "task.h"
 #include "mb.h"
@@ -8,27 +7,35 @@
 #include "mbport.h"
 #include <stdlib.h>
 
+
+#define MB_UART                 USART3
 #define USART_INVALID_PORT      ( 0xFF )
 
 
 
+
 void vMBPortSerialEnable( BOOL xRxEnable, BOOL xTxEnable )
 {
+    char foo = 0;
+    
     if (xRxEnable) {
         gpio_bits_reset(GPIOD, GPIO_PINS_10);
-        usart_interrupt_enable(USART3, USART_RDBF_INT, TRUE);
+        usart_interrupt_enable(MB_UART, USART_RDBF_INT, TRUE);
 	}
 	else {
-        gpio_bits_set(GPIOD, GPIO_PINS_10);
-        usart_interrupt_enable(USART3, USART_RDBF_INT, FALSE);
+        //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);
-        usart_interrupt_enable(USART3, USART_TDC_INT, TRUE);
+        vTaskDelay(100);
+        usart_interrupt_enable(MB_UART, USART_TDBE_INT, TRUE);
 	}
 	else {
-        gpio_bits_reset(GPIOD, GPIO_PINS_10);
-        usart_interrupt_enable(USART3, USART_TDC_INT, FALSE);
+        //gpio_bits_reset(GPIOD, GPIO_PINS_10);
+        usart_interrupt_enable(MB_UART, USART_TDBE_INT, FALSE);
 	}
 }
 
@@ -36,6 +43,7 @@ void vMBPortSerialEnable( BOOL xRxEnable, BOOL xTxEnable )
 BOOL xMBPortSerialInit( UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits, 
                    eMBParity eParity, unsigned int stop_bit )
 {
+#if 1  
     gpio_init_type gpio_init_struct;
     usart_parity_selection_type parity;
     usart_data_bit_num_type data_bits;
@@ -48,21 +56,15 @@ BOOL xMBPortSerialInit( UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits,
     
     gpio_default_para_init(&gpio_init_struct);
 
-    // configure the uart tx pin
+    // configure 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_MUX;
-    gpio_init_struct.gpio_pins = GPIO_PINS_8;
+    gpio_init_struct.gpio_pins = GPIO_PINS_8 | GPIO_PINS_9;
     gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
     gpio_init(GPIOD, &gpio_init_struct);
-
-    // configure the uart rx pin
-    gpio_init_struct.gpio_mode = GPIO_MODE_INPUT;
-    gpio_init_struct.gpio_pins = GPIO_PINS_9;
-    gpio_init_struct.gpio_pull = GPIO_PULL_UP;
-    gpio_init(GPIOD, &gpio_init_struct);
     
-    // configure the uart rx pin
+    // configure DE 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;
@@ -107,77 +109,54 @@ BOOL xMBPortSerialInit( UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits,
     
     
     // configure uart param
-    usart_init(USART3, ulBaudRate, data_bits, stop_bits);
-    usart_parity_selection_config(USART3, parity);
-    
-    usart_flag_clear(USART3, USART_RDBF_FLAG);
-    usart_interrupt_enable(USART3, USART_RDBF_INT, TRUE);
-    usart_receiver_enable(USART3, TRUE);
-    usart_transmitter_enable(USART3, TRUE);
-    usart_enable(USART3, TRUE);  
-    
+    usart_parity_selection_config(MB_UART, parity);
+    usart_init(MB_UART, ulBaudRate, data_bits, stop_bits);
+    usart_transmitter_enable(MB_UART, TRUE);
+    usart_receiver_enable(MB_UART, TRUE);
+    usart_enable(MB_UART, TRUE);  
     nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);
+    NVIC_ClearPendingIRQ(USART3_IRQn);
     nvic_irq_enable(USART3_IRQn, 5, 0);
+    //nvic_irq_enable(USART3_IRQn, 0, 5);
     
     //usart_interrupt_enable(USART3, USART_TDC_INT, TRUE);
     //usart_interrupt_enable(USART3, USART_RDBF_INT, TRUE);
     
     return TRUE;
-    
-    
-#if 0  
-	GPIO_InitTypeDef GPIO_InitStruct = {0};
-    
-    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
-    GPIO_InitStruct.Pull = GPIO_NOPULL;
-    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
+#endif
+#if 0    
+    gpio_init_type gpio_init_struct;
+    usart_parity_selection_type parity;
+    usart_data_bit_num_type data_bits;
+    usart_stop_bit_num_type stop_bits;
 
-    __HAL_RCC_USART4_FORCE_RESET();
-    __HAL_RCC_USART4_RELEASE_RESET();
-    
-  	huart.Init.BaudRate = ulBaudRate;
-  	huart.Init.Mode = UART_MODE_TX_RX;
-  	huart.Init.HwFlowCtl = UART_HWCONTROL_NONE;
-  	huart.Init.OverSampling = UART_OVERSAMPLING_16;
-  	huart.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
-  	huart.Init.ClockPrescaler = UART_PRESCALER_DIV1;
-  	huart.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
+    crm_periph_clock_enable(CRM_USART1_PERIPH_CLOCK, TRUE);
+    crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE);
     
-	switch (ucPORT)
-	{
-		case 4 :
-			__HAL_RCC_USART4_CLK_ENABLE();
-    		__HAL_RCC_GPIOA_CLK_ENABLE();
-
-			GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1;
-			GPIO_InitStruct.Alternate = GPIO_AF4_USART4;
-			HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
-
-			huart.Instance = USART4;
-
-			HAL_NVIC_SetPriority(USART3_4_IRQn, 5, 0);
-
-			break;
-
-		default : 
-			return FALSE;
-	}
+    gpio_default_para_init(&gpio_init_struct);
 
-	switch (eParity)
+    // configure 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_MUX;
+    gpio_init_struct.gpio_pins = GPIO_PINS_10 | GPIO_PINS_9;
+    gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
+    gpio_init(GPIOA, &gpio_init_struct);
+    
+    switch (eParity)
 	{
 		case MB_PAR_NONE :
-			huart.Init.Parity = UART_PARITY_NONE;
-            huart.Init.WordLength = UART_WORDLENGTH_8B;
+            parity = USART_PARITY_NONE;
+            data_bits = USART_DATA_8BITS;
 			break;
 		case MB_PAR_ODD :
-			huart.Init.Parity = UART_PARITY_ODD;
-            huart.Init.WordLength = UART_WORDLENGTH_9B;
+            parity = USART_PARITY_ODD;
+            data_bits = USART_DATA_9BITS;
 			break;
 		case MB_PAR_EVEN :
-			huart.Init.Parity = UART_PARITY_EVEN;
-            huart.Init.WordLength = UART_WORDLENGTH_9B;
+            parity = USART_PARITY_EVEN;
+            data_bits = USART_DATA_9BITS;
 			break;
-
 		default :
 			return FALSE;
 	}
@@ -185,21 +164,28 @@ BOOL xMBPortSerialInit( UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits,
     switch (stop_bit)
     {
         case 1 :
-            huart.Init.StopBits = UART_STOPBITS_1;
+            stop_bits = USART_STOP_1_BIT;
         break;
         
         case 3 :
-            huart.Init.StopBits = UART_STOPBITS_2;
+            stop_bits = USART_STOP_2_BIT;
         break;
         
         default :
             return FALSE;
-         
     }
     
-	HAL_UART_Init(&huart);
-	HAL_NVIC_EnableIRQ(USART3_4_IRQn);
-
+    
+    // configure uart param
+    usart_parity_selection_config(MB_UART, parity);
+    usart_init(MB_UART, ulBaudRate, data_bits, stop_bits);
+    usart_transmitter_enable(MB_UART, TRUE);
+    usart_receiver_enable(MB_UART, TRUE);
+    usart_enable(MB_UART, TRUE);  
+    nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);
+    NVIC_ClearPendingIRQ(USART1_IRQn);
+    nvic_irq_enable(USART1_IRQn, 5, 0);
+    
     return TRUE;
 #endif    
 }
@@ -213,38 +199,42 @@ void vMBPortSerialClose( void )
 //
 BOOL xMBPortSerialPutByte( CHAR ucByte )
 {
-  
-    //while(usart_flag_get(USART3, USART_TDBE_FLAG) == RESET);
-    USART3->dt = ((uint16_t)ucByte & 0x01FF);
-    //while(usart_flag_get(USART3, USART_TDC_FLAG) == RESET);
-    return TRUE;
-    
-#if 0    
-    USART3->dt = ((uint16_t)ucByte & 0x01FF);
+    //usart_data_transmit(PRINT_UART, ucByte);
+    usart_data_transmit(MB_UART, ucByte);
     return TRUE;
-#endif    
 }
 
 //
 BOOL xMBPortSerialGetByte( CHAR * pucByte )
 {
-    *pucByte = USART3->dt;
+    *pucByte = usart_data_receive(MB_UART);
     return TRUE;
 }
 
-//
+
+#if 1
 void USART3_IRQHandler(void)
-{
-    if (usart_flag_get(USART3, USART_RDBF_FLAG) != RESET) {
-        pxMBFrameCBByteReceived();
+//void USART1_IRQHandler(void)
+{   
+    if (MB_UART->ctrl1_bit.rdbfien == SET)
+    {
+        if (usart_flag_get(MB_UART, USART_RDBF_FLAG) == SET) 
+        {
+            pxMBFrameCBByteReceived();
+        }
     }  
-    if (usart_flag_get(USART3, USART_TDC_FLAG) != RESET) {
-        vMBPortSetWithinException(TRUE);
-        pxMBFrameCBTransmitterEmpty();
-        vMBPortSetWithinException(FALSE);
-    }
-}      
-
+    
+    if (MB_UART->ctrl1_bit.tdbeien == SET)
+    {
+        if (usart_flag_get(MB_UART, USART_TDBE_FLAG) == SET) 
+        {
+            vMBPortSetWithinException(TRUE);
+            pxMBFrameCBTransmitterEmpty();
+            vMBPortSetWithinException(FALSE);
+        }
+    }      
+}
+#endif
 
 //
 void vMB_USART_IRQHandler(void)

+ 22 - 0
shared/utils/at32f403a_407_board.c

@@ -153,9 +153,31 @@ void uart_print_init(uint32_t baudrate)
   /* configure uart param */
   usart_init(PRINT_UART, baudrate, USART_DATA_8BITS, USART_STOP_1_BIT);
   usart_transmitter_enable(PRINT_UART, TRUE);
+  usart_receiver_enable(PRINT_UART, TRUE);
   usart_enable(PRINT_UART, TRUE);
+  
+  nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);
+  NVIC_ClearPendingIRQ(USART1_IRQn);
+  nvic_irq_enable(USART1_IRQn, 0, 5);
+    
+  usart_interrupt_enable(USART1, USART_RDBF_INT, TRUE);
 }
 
+#if 0
+void USART1_IRQHandler(void)
+{   
+    static char data = 0;
+      
+    if (USART1->ctrl1_bit.rdbfien == SET)
+    {
+        if (usart_flag_get(USART1, USART_RDBF_FLAG) == SET) 
+        {
+            data = usart_data_receive(USART1);
+        }
+    }  
+}
+#endif
+
 /**
   * @brief  board initialize interface init led and button
   * @param  none

Unele fișiere nu au fost afișate deoarece prea multe fișiere au fost modificate în acest diff