Browse Source

Trying to make RS232<->UDP bridge

Dmitry Yu Okunev 4 years ago
parent
commit
f49c6b6401

+ 1 - 1
Drivers/enc28j60

@@ -1 +1 @@
-Subproject commit 4890274cf111b0c272648f1c1f00d8eb82c498c2
+Subproject commit 5578e2d6fe9abb118cada82330d7815e498ae89f

+ 1 - 0
Inc/stm32f0xx_it.h

@@ -46,6 +46,7 @@
 /* Exported functions ------------------------------------------------------- */
 
 void SysTick_Handler(void);
+void SPI1_IRQHandler(void);
 
 #ifdef __cplusplus
 }

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


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

@@ -27,14 +27,6 @@
 	<linkedResources>
 		
 	<link>
-			<name>Drivers/STM32F0xx_HAL_Driver/stm32f0xx_hal_spi.c</name>
-			<type>1</type>
-			<location>PARENT-2-PROJECT_LOC/Drivers/STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_spi.c</location>
-		</link><link>
-			<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>
@@ -102,5 +94,13 @@
 			<name>Drivers/CMSIS/system_stm32f0xx.c</name>
 			<type>1</type>
 			<location>PARENT-2-PROJECT_LOC/Drivers/CMSIS/Device/ST/STM32F0xx/Source/Templates/system_stm32f0xx.c</location>
+		</link><link>
+			<name>Drivers/STM32F0xx_HAL_Driver/stm32f0xx_hal_spi.c</name>
+			<type>1</type>
+			<location>PARENT-2-PROJECT_LOC/Drivers/STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_spi.c</location>
+		</link><link>
+			<name>Application/User/stm32f0xx_hal_msp.c</name>
+			<type>1</type>
+			<location>PARENT-2-PROJECT_LOC/Src/stm32f0xx_hal_msp.c</location>
 		</link></linkedResources>
 </projectDescription>

+ 93 - 18
Src/main.c

@@ -35,8 +35,13 @@
 
 /* USER CODE BEGIN Includes */
 
+#include <string.h>
 #include "EtherShield.h"
-#define BUF_SIZE (1<<10)
+#define NET_BUF_SIZE (1<<10)
+#define UART_BUF_SIZE 1
+
+#define MAX(a,b) ((a)>(b)?(a):(b))
+#define BUF_SIZE MAX(NET_BUF_SIZE, UART_BUF_SIZE)
 
 /* USER CODE END Includes */
 
@@ -103,7 +108,47 @@ static inline void blink(int times, int delay) {
 }
 
 void ES_PingCallback(void) {
-	//STM_EVAL_LEDToggle(LED4);
+}
+
+uint16_t get_udp_data_len(uint8_t *buf)
+{
+	int16_t i;
+	i=(((int16_t)buf[IP_TOTLEN_H_P])<<8)|(buf[IP_TOTLEN_L_P]&0xff);
+	i-=IP_HEADER_LEN;
+	i-=8;
+	if (i<=0){
+		i=0;
+	}
+	return((uint16_t)i);
+}
+
+static uint16_t info_data_len = 0;
+uint16_t packetloop_icmp_udp(uint8_t *buf,uint16_t plen)
+{
+	if(eth_type_is_arp_and_my_ip(buf,plen)){
+		if (buf[ETH_ARP_OPCODE_L_P]==ETH_ARP_OPCODE_REQ_L_V){
+			// is it an arp request 
+			make_arp_answer_from_request(buf);
+		}
+		return(0);
+
+	}
+	// check if ip packets are for us:
+	if(eth_type_is_ip_and_my_ip(buf,plen)==0){
+		return(0);
+	}
+
+	if(buf[IP_PROTO_P]==IP_PROTO_ICMP_V && buf[ICMP_TYPE_P]==ICMP_TYPE_ECHOREQUEST_V){
+		make_echo_reply_from_request(buf,plen);
+		return(0);
+	}
+
+	if (buf[IP_PROTO_P]==IP_PROTO_UDP_V) {
+		info_data_len=get_udp_data_len(buf);
+		return(IP_HEADER_LEN+8+14);
+	}
+
+	return(0);
 }
 
 /* USER CODE END PFP */
@@ -138,11 +183,22 @@ int main(void)
 
   /* Infinite loop */
   /* USER CODE BEGIN WHILE */
+	static uint8_t uart_buf[UART_BUF_SIZE + 1];
 
 	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];
 
+	/* // USART echo node
+	while (1)
+	{
+		if (HAL_UART_Receive(&huart3, uart_buf, UART_BUF_SIZE, 0) == HAL_OK) {
+			HAL_UART_Transmit(&huart3, uart_buf, 1, 0xffffffff);
+		}
+	}
+	*/
+
 	ES_enc28j60SpiInit(&hspi1);
 	ES_enc28j60Init(mac);
 
@@ -151,22 +207,41 @@ int main(void)
 	if (enc28j60_rev <= 0)
 		error(2, 0);
 
-	ES_init_ip_arp_udp_tcp(mac, ip, 23);
+	ES_init_ip_arp_udp_tcp(mac, ip, 80);
+	uint16_t dat_p = 0;
 
+	/* // UDP echo server
 	while (1)
 	{
-		uint16_t dat_p;
+		dat_p = packetloop_icmp_udp(net_buf, ES_enc28j60PacketReceive(NET_BUF_SIZE, net_buf));
+		if (dat_p != 0) {
+			memcpy(buf, &net_buf[dat_p], info_data_len);
+			make_udp_reply_from_request(net_buf, (char *)buf, info_data_len, 23);
+		}
+	}
+	*/
 
-		// read packet, handle ping and wait for a tcp packet:
-		dat_p = ES_packetloop_icmp_tcp(buf,
-				ES_enc28j60PacketReceive(BUF_SIZE, buf));
 
-		/* dat_p will be unequal to zero if there is a valid
-		 * http get */
-		if (dat_p == 0) {
-			// no http request
-			continue;
+	while (1)
+	{
+
+		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);
+
+
   /* USER CODE END WHILE */
 
   /* USER CODE BEGIN 3 */
@@ -186,11 +261,11 @@ void SystemClock_Config(void)
   RCC_PeriphCLKInitTypeDef PeriphClkInit;
 
   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
-  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
+  RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS;
   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
-  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL12;
-  RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV2;
+  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL6;
+  RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV1;
   HAL_RCC_OscConfig(&RCC_OscInitStruct);
 
   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK;
@@ -220,7 +295,7 @@ void MX_SPI1_Init(void)
   hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
   hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
   hspi1.Init.NSS = SPI_NSS_SOFT;
-  hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8;
+  hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4;
   hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
   hspi1.Init.TIMode = SPI_TIMODE_DISABLED;
   hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED;
@@ -237,12 +312,12 @@ void MX_USART3_UART_Init(void)
 
   huart3.Instance = USART3;
   huart3.Init.BaudRate = 115200;
-  huart3.Init.WordLength = UART_WORDLENGTH_7B;
+  huart3.Init.WordLength = UART_WORDLENGTH_8B;
   huart3.Init.StopBits = UART_STOPBITS_1;
   huart3.Init.Parity = UART_PARITY_NONE;
   huart3.Init.Mode = UART_MODE_TX_RX;
   huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
-  huart3.Init.OverSampling = UART_OVERSAMPLING_16;
+  huart3.Init.OverSampling = UART_OVERSAMPLING_8;
   huart3.Init.OneBitSampling = UART_ONEBIT_SAMPLING_DISABLED;
   huart3.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
   HAL_UART_Init(&huart3);

+ 6 - 0
Src/stm32f0xx_hal_msp.c

@@ -82,6 +82,9 @@ void HAL_SPI_MspInit(SPI_HandleTypeDef* hspi)
     GPIO_InitStruct.Alternate = GPIO_AF0_SPI1;
     HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
 
+  /* Peripheral interrupt init*/
+    HAL_NVIC_SetPriority(SPI1_IRQn, 0, 0);
+    HAL_NVIC_EnableIRQ(SPI1_IRQn);
   /* USER CODE BEGIN SPI1_MspInit 1 */
 
   /* USER CODE END SPI1_MspInit 1 */
@@ -107,6 +110,9 @@ void HAL_SPI_MspDeInit(SPI_HandleTypeDef* hspi)
     */
     HAL_GPIO_DeInit(GPIOB, GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5);
 
+    /* Peripheral interrupt DeInit*/
+    HAL_NVIC_DisableIRQ(SPI1_IRQn);
+
   }
   /* USER CODE BEGIN SPI1_MspDeInit 1 */
 

+ 15 - 0
Src/stm32f0xx_it.c

@@ -40,6 +40,7 @@
 /* USER CODE END 0 */
 
 /* External variables --------------------------------------------------------*/
+extern SPI_HandleTypeDef hspi1;
 
 /******************************************************************************/
 /*            Cortex-M0 Processor Interruption and Exception Handlers         */ 
@@ -67,6 +68,20 @@ void SysTick_Handler(void)
 /* please refer to the startup file (startup_stm32f0xx.s).                    */
 /******************************************************************************/
 
+/**
+* @brief This function handles SPI1 global interrupt.
+*/
+void SPI1_IRQHandler(void)
+{
+  /* USER CODE BEGIN SPI1_IRQn 0 */
+
+  /* USER CODE END SPI1_IRQn 0 */
+  HAL_SPI_IRQHandler(&hspi1);
+  /* USER CODE BEGIN SPI1_IRQn 1 */
+
+  /* USER CODE END SPI1_IRQn 1 */
+}
+
 /* USER CODE BEGIN 1 */
 
 /* USER CODE END 1 */

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

@@ -31,6 +31,7 @@ Mcu.UserConstants=
 Mcu.UserName=STM32F091RCTx
 MxCube.Version=4.10.0
 MxDb.Version=DB.4.0.100
+NVIC.SPI1_IRQn=true\:0\:0\:false
 NVIC.SysTick_IRQn=true\:0\:0\:false
 PA13.GPIOParameters=GPIO_Label
 PA13.GPIO_Label=TMS
@@ -58,7 +59,7 @@ PA3.Locked=true
 PA3.Signal=USART2_RX
 PA4.Locked=true
 PA4.Signal=GPIO_Output
-PA5.GPIOParameters=GPIO_ModeDefaultOutputPP,GPIO_Label,GPIO_Speed,GPIO_PuPd,GPIO_Mode
+PA5.GPIOParameters=GPIO_Label,GPIO_ModeDefaultOutputPP,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
@@ -100,10 +101,9 @@ PCC.Series=STM32F0
 PCC.Temperature=25
 PCC.Vdd=3.6
 PF0-OSC_IN.Locked=true
-PF0-OSC_IN.Mode=HSE-External-Oscillator
+PF0-OSC_IN.Mode=HSE-External-Clock-Source
 PF0-OSC_IN.Signal=RCC_OSC_IN
 PF1-OSC_OUT.Locked=true
-PF1-OSC_OUT.Mode=HSE-External-Oscillator
 PF1-OSC_OUT.Signal=RCC_OSC_OUT
 ProjectManager.AskForMigrate=true
 ProjectManager.BackupPrevious=false
@@ -133,12 +133,11 @@ RCC.FamilyName=M
 RCC.HCLKFreq_Value=48000000
 RCC.HSICECFreq_Value=32786.88524590164
 RCC.I2SFreq_Value=48000000
-RCC.IPParameters=HCLKFreq_Value,SYSCLKFreq_VALUE,FamilyName,PLLCLKFreq_Value,PREFETCH_ENABLE-AdvancedSettings,PLLMCOFreq_Value,SYSCLKSource,CECFreq_Value,FCLKCortexFreq_Value,FLatency-AdvancedSettings,PLLMUL,HSICECFreq_Value,MCOFreq_Value,I2SFreq_Value,USART2Freq_Value,APB1TimFreq_Value,APB1Freq_Value,USART1Freq_Value,AHBFreq_Value,TimSysFreq_Value,USART3Freq_Value,PLLDivider,PLLSource
+RCC.IPParameters=HCLKFreq_Value,SYSCLKFreq_VALUE,FamilyName,PLLCLKFreq_Value,PREFETCH_ENABLE-AdvancedSettings,PLLMCOFreq_Value,CECFreq_Value,FCLKCortexFreq_Value,FLatency-AdvancedSettings,PLLMUL,HSICECFreq_Value,MCOFreq_Value,I2SFreq_Value,USART2Freq_Value,APB1TimFreq_Value,APB1Freq_Value,USART1Freq_Value,AHBFreq_Value,TimSysFreq_Value,USART3Freq_Value,VCOOutput2Freq_Value,SYSCLKSource,PLLSource
 RCC.MCOFreq_Value=48000000
 RCC.PLLCLKFreq_Value=48000000
-RCC.PLLDivider=RCC_PREDIV_DIV2
 RCC.PLLMCOFreq_Value=48000000
-RCC.PLLMUL=RCC_PLL_MUL12
+RCC.PLLMUL=RCC_PLL_MUL6
 RCC.PLLSource=RCC_PLLSOURCE_HSE
 RCC.PREFETCH_ENABLE-AdvancedSettings=1
 RCC.SYSCLKFreq_VALUE=48000000
@@ -147,13 +146,20 @@ RCC.TimSysFreq_Value=48000000
 RCC.USART1Freq_Value=48000000
 RCC.USART2Freq_Value=48000000
 RCC.USART3Freq_Value=48000000
+RCC.VCOOutput2Freq_Value=8000000
 SH.GPXTI13.0=GPIO_EXTI13
 SH.GPXTI13.ConfNb=1
-SPI1.BaudRatePrescaler=SPI_BAUDRATEPRESCALER_8
+SPI1.BaudRatePrescaler=SPI_BAUDRATEPRESCALER_4
 SPI1.CRCCalculation=SPI_CRCCALCULATION_DISABLED
-SPI1.CalculateBaudRate=6.0 MBits/s
+SPI1.CalculateBaudRate=12.0 MBits/s
 SPI1.DataSize=SPI_DATASIZE_8BIT
 SPI1.IPParameters=Mode,BaudRatePrescaler,CalculateBaudRate,DataSize,CRCCalculation
 SPI1.Mode=SPI_MODE_MASTER
+USART3.BaudRate=115200
+USART3.IPParameters=WordLength,OverSampling,BaudRate,StopBits,Parity
+USART3.OverSampling=UART_OVERSAMPLING_8
+USART3.Parity=UART_PARITY_NONE
+USART3.StopBits=UART_STOPBITS_1
+USART3.WordLength=UART_WORDLENGTH_8B
 board=NUCLEO-F091RC
 boardIOC=true