Browse Source

Seems to works

Dmitry Yu Okunev 4 years ago
parent
commit
427533b7a3

+ 2 - 0
Inc/stm32f0xx_it.h

@@ -46,6 +46,8 @@
 /* Exported functions ------------------------------------------------------- */
 
 void SysTick_Handler(void);
+void DMA1_Ch1_IRQHandler(void);
+void DMA1_Ch2_3_DMA2_Ch1_2_IRQHandler(void);
 void SPI1_IRQHandler(void);
 
 #ifdef __cplusplus

File diff suppressed because it is too large
+ 2 - 2
SW4STM32/nucleo-f091rc-rs232-enc28j60 Configuration/.cproject


+ 8 - 8
SW4STM32/nucleo-f091rc-rs232-enc28j60 Configuration/.project

@@ -27,14 +27,6 @@
 	<linkedResources>
 		
 	<link>
-			<name>Application/User/main.c</name>
-			<type>1</type>
-			<location>PARENT-2-PROJECT_LOC/Src/main.c</location>
-		</link><link>
-			<name>Drivers/STM32F0xx_HAL_Driver/stm32f0xx_hal_rcc.c</name>
-			<type>1</type>
-			<location>PARENT-2-PROJECT_LOC/Drivers/STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_rcc.c</location>
-		</link><link>
 			<name>Drivers/STM32F0xx_HAL_Driver/stm32f0xx_hal_spi_ex.c</name>
 			<type>1</type>
 			<location>PARENT-2-PROJECT_LOC/Drivers/STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_spi_ex.c</location>
@@ -102,5 +94,13 @@
 			<name>Application/User/stm32f0xx_hal_msp.c</name>
 			<type>1</type>
 			<location>PARENT-2-PROJECT_LOC/Src/stm32f0xx_hal_msp.c</location>
+		</link><link>
+			<name>Application/User/main.c</name>
+			<type>1</type>
+			<location>PARENT-2-PROJECT_LOC/Src/main.c</location>
+		</link><link>
+			<name>Drivers/STM32F0xx_HAL_Driver/stm32f0xx_hal_rcc.c</name>
+			<type>1</type>
+			<location>PARENT-2-PROJECT_LOC/Drivers/STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_rcc.c</location>
 		</link></linkedResources>
 </projectDescription>

+ 86 - 12
Src/main.c

@@ -38,7 +38,7 @@
 #include <string.h>
 #include "EtherShield.h"
 #define NET_BUF_SIZE (1<<10)
-#define UART_BUF_SIZE 1
+#define UART_BUF_SIZE (NET_BUF_SIZE << 2)
 
 #define MAX(a,b) ((a)>(b)?(a):(b))
 #define BUF_SIZE MAX(NET_BUF_SIZE, UART_BUF_SIZE)
@@ -49,6 +49,8 @@
 SPI_HandleTypeDef hspi1;
 
 UART_HandleTypeDef huart3;
+DMA_HandleTypeDef hdma_usart3_rx;
+DMA_HandleTypeDef hdma_usart3_tx;
 
 /* USER CODE BEGIN PV */
 /* Private variables ---------------------------------------------------------*/
@@ -58,6 +60,7 @@ UART_HandleTypeDef huart3;
 /* Private function prototypes -----------------------------------------------*/
 void SystemClock_Config(void);
 static void MX_GPIO_Init(void);
+static void MX_DMA_Init(void);
 static void MX_SPI1_Init(void);
 static void MX_USART3_UART_Init(void);
 
@@ -174,6 +177,7 @@ int main(void)
 
   /* Initialize all configured peripherals */
   MX_GPIO_Init();
+  MX_DMA_Init();
   MX_SPI1_Init();
   MX_USART3_UART_Init();
 
@@ -183,14 +187,23 @@ int main(void)
 
   /* Infinite loop */
   /* USER CODE BEGIN WHILE */
-	static uint8_t uart_buf[UART_BUF_SIZE + 1];
+	static uint8_t uart_buf[UART_BUF_SIZE], *uart_buf_parsed, *uart_buf_filled, *uart_buf_end;
 
-	uint8_t mac[] = {02, 03, 04, 05, 06, 07};
-	uint8_t ip[]  = {10,4,33,123};
+	uint16_t netanswer_initialpacket_len = 0;
+	uint8_t  mac[] = {02, 03, 04, 05, 06, 07};
+	uint8_t  ip[]  = {10,  4, 33, 123};
 	static uint8_t net_buf[NET_BUF_SIZE + 1];
-	static uint8_t buf[BUF_SIZE + 1];
+	static uint8_t netanswer_buf[NET_BUF_SIZE + 1];
 
-	/* // USART echo node
+	uart_buf_parsed =  uart_buf;
+	{
+		HAL_StatusTypeDef r = HAL_UART_Receive_DMA(&huart3, uart_buf, UART_BUF_SIZE);
+		if (r != HAL_OK)
+			error(r, 0);
+	}
+	uart_buf_end    = &uart_buf[UART_BUF_SIZE];
+
+	/* // USART echo node (simple)
 	while (1)
 	{
 		if (HAL_UART_Receive(&huart3, uart_buf, UART_BUF_SIZE, 0) == HAL_OK) {
@@ -199,6 +212,29 @@ int main(void)
 	}
 	*/
 
+	/* // USART echo node (DMA)
+	while (1)
+	{
+		uart_buf_filled = &uart_buf[UART_BUF_SIZE - huart3.hdmarx->Instance->CNDTR];
+		while (uart_buf_parsed != uart_buf_filled) {
+			uint16_t outlen;
+
+			if (uart_buf_filled > uart_buf_parsed) {
+				outlen = uart_buf_filled - uart_buf_parsed;
+			} else {
+				outlen = uart_buf_end    - uart_buf_parsed;
+			}
+
+			HAL_UART_Transmit(&huart3, uart_buf_parsed, outlen, 0xffffffff);
+
+			uart_buf_parsed += outlen;
+
+			if (uart_buf_parsed >= uart_buf_end)
+				uart_buf_parsed = uart_buf;
+		}
+	}
+	*/
+
 	ES_enc28j60SpiInit(&hspi1);
 	ES_enc28j60Init(mac);
 
@@ -208,7 +244,6 @@ int main(void)
 		error(2, 0);
 
 	ES_init_ip_arp_udp_tcp(mac, ip, 80);
-	uint16_t dat_p = 0;
 
 	/* // UDP echo server
 	while (1)
@@ -221,26 +256,49 @@ int main(void)
 	}
 	*/
 
-
 	while (1)
 	{
+		uint16_t dat_p;
 
+		uart_buf_filled = &uart_buf[UART_BUF_SIZE - huart3.hdmarx->Instance->CNDTR];
+		while (uart_buf_parsed != uart_buf_filled) {
+			uint16_t outlen;
+
+			if (uart_buf_filled > uart_buf_parsed) {
+				outlen = uart_buf_filled - uart_buf_parsed;
+			} else {
+				outlen = uart_buf_end    - uart_buf_parsed;
+			}
+
+			if (netanswer_initialpacket_len != 0) {
+				memcpy(net_buf, netanswer_buf, netanswer_initialpacket_len);
+				make_udp_reply_from_request(net_buf, (char *)uart_buf_parsed, outlen, 23);
+			}
+
+			uart_buf_parsed += outlen;
+
+			if (uart_buf_parsed >= uart_buf_end)
+				uart_buf_parsed = uart_buf;
+		}
+
+/*
 		while (HAL_UART_Receive(&huart3, uart_buf, UART_BUF_SIZE, 0xff) == HAL_OK) {
 			if (dat_p != 0) {
 				make_udp_reply_from_request(net_buf, (char *)uart_buf, UART_BUF_SIZE, 23);
 			}
 			HAL_Delay(400);
 		}
-
-		HAL_Delay(1);
+*/
 		dat_p = packetloop_icmp_udp(net_buf, ES_enc28j60PacketReceive(NET_BUF_SIZE, net_buf));
 
 		if (dat_p == 0)
 			continue;
 
-		HAL_UART_Transmit(&huart3, &net_buf[dat_p], info_data_len, 0xffffffff);
-		HAL_Delay(400);
+		netanswer_initialpacket_len = dat_p+info_data_len;
+		memcpy(netanswer_buf, net_buf, dat_p+info_data_len);
+		
 
+		HAL_UART_Transmit(&huart3, &net_buf[dat_p], info_data_len, 0xffffffff);
 
   /* USER CODE END WHILE */
 
@@ -324,6 +382,22 @@ void MX_USART3_UART_Init(void)
 
 }
 
+/** 
+  * Enable DMA controller clock
+  */
+void MX_DMA_Init(void) 
+{
+  /* DMA controller clock enable */
+  __DMA1_CLK_ENABLE();
+
+  /* DMA interrupt init */
+  HAL_NVIC_SetPriority(DMA1_Ch1_IRQn, 0, 0);
+  HAL_NVIC_EnableIRQ(DMA1_Ch1_IRQn);
+  HAL_NVIC_SetPriority(DMA1_Ch2_3_DMA2_Ch1_2_IRQn, 0, 0);
+  HAL_NVIC_EnableIRQ(DMA1_Ch2_3_DMA2_Ch1_2_IRQn);
+
+}
+
 /** Configure pins as 
         * Analog 
         * Input 

+ 37 - 0
Src/stm32f0xx_hal_msp.c

@@ -34,6 +34,10 @@
 /* Includes ------------------------------------------------------------------*/
 #include "stm32f0xx_hal.h"
 
+extern DMA_HandleTypeDef hdma_usart3_rx;
+
+extern DMA_HandleTypeDef hdma_usart3_tx;
+
 /* USER CODE BEGIN 0 */
 
 /* USER CODE END 0 */
@@ -143,6 +147,36 @@ void HAL_UART_MspInit(UART_HandleTypeDef* huart)
     GPIO_InitStruct.Alternate = GPIO_AF4_USART3;
     HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
 
+    /* Peripheral DMA init*/
+  
+    hdma_usart3_rx.Instance = DMA1_Channel1;
+    hdma_usart3_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
+    hdma_usart3_rx.Init.PeriphInc = DMA_PINC_DISABLE;
+    hdma_usart3_rx.Init.MemInc = DMA_MINC_ENABLE;
+    hdma_usart3_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
+    hdma_usart3_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
+    hdma_usart3_rx.Init.Mode = DMA_CIRCULAR;
+    hdma_usart3_rx.Init.Priority = DMA_PRIORITY_LOW;
+    HAL_DMA_Init(&hdma_usart3_rx);
+
+    __HAL_DMA1_REMAP(HAL_DMA1_CH1_USART3_RX);
+
+    __HAL_LINKDMA(huart,hdmarx,hdma_usart3_rx);
+
+    hdma_usart3_tx.Instance = DMA1_Channel2;
+    hdma_usart3_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
+    hdma_usart3_tx.Init.PeriphInc = DMA_PINC_DISABLE;
+    hdma_usart3_tx.Init.MemInc = DMA_MINC_DISABLE;
+    hdma_usart3_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
+    hdma_usart3_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
+    hdma_usart3_tx.Init.Mode = DMA_NORMAL;
+    hdma_usart3_tx.Init.Priority = DMA_PRIORITY_LOW;
+    HAL_DMA_Init(&hdma_usart3_tx);
+
+    __HAL_DMA1_REMAP(HAL_DMA1_CH2_USART3_TX);
+
+    __HAL_LINKDMA(huart,hdmatx,hdma_usart3_tx);
+
   /* USER CODE BEGIN USART3_MspInit 1 */
 
   /* USER CODE END USART3_MspInit 1 */
@@ -167,6 +201,9 @@ void HAL_UART_MspDeInit(UART_HandleTypeDef* huart)
     */
     HAL_GPIO_DeInit(GPIOB, GPIO_PIN_10|GPIO_PIN_11);
 
+    /* Peripheral DMA DeInit*/
+    HAL_DMA_DeInit(huart->hdmarx);
+    HAL_DMA_DeInit(huart->hdmatx);
   }
   /* USER CODE BEGIN USART3_MspDeInit 1 */
 

+ 30 - 0
Src/stm32f0xx_it.c

@@ -41,6 +41,8 @@
 
 /* External variables --------------------------------------------------------*/
 extern SPI_HandleTypeDef hspi1;
+extern DMA_HandleTypeDef hdma_usart3_rx;
+extern DMA_HandleTypeDef hdma_usart3_tx;
 
 /******************************************************************************/
 /*            Cortex-M0 Processor Interruption and Exception Handlers         */ 
@@ -69,6 +71,34 @@ void SysTick_Handler(void)
 /******************************************************************************/
 
 /**
+* @brief This function handles DMA1 channel 1 interrupt.
+*/
+void DMA1_Ch1_IRQHandler(void)
+{
+  /* USER CODE BEGIN DMA1_Ch1_IRQn 0 */
+
+  /* USER CODE END DMA1_Ch1_IRQn 0 */
+  HAL_DMA_IRQHandler(&hdma_usart3_rx);
+  /* USER CODE BEGIN DMA1_Ch1_IRQn 1 */
+
+  /* USER CODE END DMA1_Ch1_IRQn 1 */
+}
+
+/**
+* @brief This function handles DMA1 channel 2 to 3 and DMA2 channel 1 to 2 interrupts.
+*/
+void DMA1_Ch2_3_DMA2_Ch1_2_IRQHandler(void)
+{
+  /* USER CODE BEGIN DMA1_Ch2_3_DMA2_Ch1_2_IRQn 0 */
+
+  /* USER CODE END DMA1_Ch2_3_DMA2_Ch1_2_IRQn 0 */
+  HAL_DMA_IRQHandler(&hdma_usart3_tx);
+  /* USER CODE BEGIN DMA1_Ch2_3_DMA2_Ch1_2_IRQn 1 */
+
+  /* USER CODE END DMA1_Ch2_3_DMA2_Ch1_2_IRQn 1 */
+}
+
+/**
 * @brief This function handles SPI1 global interrupt.
 */
 void SPI1_IRQHandler(void)

+ 36 - 8
nucleo-f091rc-rs232-enc28j60.ioc

@@ -1,13 +1,39 @@
 #MicroXplorer Configuration settings - do not modify
+Dma.Request0=USART3_RX
+Dma.Request1=USART3_TX
+Dma.RequestsNb=2
+Dma.USART3_RX.0.DMA1_Remap_Macro_Arg=HAL_DMA1_CH1_USART3_RX
+Dma.USART3_RX.0.Direction=DMA_PERIPH_TO_MEMORY
+Dma.USART3_RX.0.Instance=DMA1_Channel1
+Dma.USART3_RX.0.MemDataAlignment=DMA_MDATAALIGN_BYTE
+Dma.USART3_RX.0.MemInc=DMA_MINC_ENABLE
+Dma.USART3_RX.0.Mode=DMA_CIRCULAR
+Dma.USART3_RX.0.PeriphDataAlignment=DMA_PDATAALIGN_BYTE
+Dma.USART3_RX.0.PeriphInc=DMA_PINC_DISABLE
+Dma.USART3_RX.0.Priority=DMA_PRIORITY_LOW
+Dma.USART3_RX.0.Request=DMA_REQUEST_10
+Dma.USART3_RX.0.RequestParameters=Instance,Request,Direction,PeriphInc,MemInc,PeriphDataAlignment,MemDataAlignment,Mode,Priority,DMA1_Remap_Macro_Arg
+Dma.USART3_TX.1.DMA1_Remap_Macro_Arg=HAL_DMA1_CH2_USART3_TX
+Dma.USART3_TX.1.Direction=DMA_MEMORY_TO_PERIPH
+Dma.USART3_TX.1.Instance=DMA1_Channel2
+Dma.USART3_TX.1.MemDataAlignment=DMA_MDATAALIGN_BYTE
+Dma.USART3_TX.1.MemInc=DMA_MINC_DISABLE
+Dma.USART3_TX.1.Mode=DMA_NORMAL
+Dma.USART3_TX.1.PeriphDataAlignment=DMA_PDATAALIGN_BYTE
+Dma.USART3_TX.1.PeriphInc=DMA_PINC_DISABLE
+Dma.USART3_TX.1.Priority=DMA_PRIORITY_LOW
+Dma.USART3_TX.1.Request=DMA_REQUEST_10
+Dma.USART3_TX.1.RequestParameters=Instance,Request,Direction,PeriphInc,MemInc,PeriphDataAlignment,MemDataAlignment,Mode,Priority,DMA1_Remap_Macro_Arg
 File.Version=5
 KeepUserPlacement=true
 Mcu.Family=STM32F0
-Mcu.IP0=NVIC
-Mcu.IP1=RCC
-Mcu.IP2=SPI1
-Mcu.IP3=SYS
-Mcu.IP4=USART3
-Mcu.IPNb=5
+Mcu.IP0=DMA
+Mcu.IP1=NVIC
+Mcu.IP2=RCC
+Mcu.IP3=SPI1
+Mcu.IP4=SYS
+Mcu.IP5=USART3
+Mcu.IPNb=6
 Mcu.Name=STM32F091R(B-C)Tx
 Mcu.Package=LQFP64
 Mcu.Pin0=PC13
@@ -31,6 +57,8 @@ Mcu.UserConstants=
 Mcu.UserName=STM32F091RCTx
 MxCube.Version=4.10.0
 MxDb.Version=DB.4.0.100
+NVIC.DMA1_Ch1_IRQn=true\:0\:0\:false
+NVIC.DMA1_Ch2_3_DMA2_Ch1_2_IRQn=true\:0\:0\:false
 NVIC.SPI1_IRQn=true\:0\:0\:false
 NVIC.SysTick_IRQn=true\:0\:0\:false
 PA13.GPIOParameters=GPIO_Label
@@ -59,7 +87,7 @@ PA3.Locked=true
 PA3.Signal=USART2_RX
 PA4.Locked=true
 PA4.Signal=GPIO_Output
-PA5.GPIOParameters=GPIO_Label,GPIO_ModeDefaultOutputPP,GPIO_Speed,GPIO_PuPd,GPIO_Mode
+PA5.GPIOParameters=GPIO_ModeDefaultOutputPP,GPIO_Label,GPIO_Speed,GPIO_PuPd,GPIO_Mode
 PA5.GPIO_Label=LD2 [Green Led]
 PA5.GPIO_Mode=GPIO_MODE_OUTPUT_PP
 PA5.GPIO_ModeDefaultOutputPP=GPIO_MODE_OUTPUT_PP
@@ -161,5 +189,5 @@ USART3.OverSampling=UART_OVERSAMPLING_8
 USART3.Parity=UART_PARITY_NONE
 USART3.StopBits=UART_STOPBITS_1
 USART3.WordLength=UART_WORDLENGTH_8B
-board=NUCLEO-F091RC
+board=custom
 boardIOC=true