Настройка Ethernet. Приём пакетов и настройка таймера отправки широковещательного сообщения.

This commit is contained in:
MaD_CaT
2024-09-06 15:30:26 +03:00
parent 4c7517be0d
commit 51d31651fc
15 changed files with 41684 additions and 35247 deletions

File diff suppressed because one or more lines are too long

View File

@@ -36,6 +36,16 @@ extern "C" {
/* Exported types ------------------------------------------------------------*/
/* USER CODE BEGIN ET */
typedef struct rcv_msg
{
uint32_t rcv_addr;
uint16_t port;
uint16_t cmd_num;
uint16_t cmd_code;
_Bool clnt_con;
unsigned char rx_buf[2048];
uint16_t length;
}_rcv_msg;
/* USER CODE END ET */

42
Core/Inc/pribors.h Normal file
View File

@@ -0,0 +1,42 @@
/*
* pribors.h
*
* Created on: 6 сент. 2024 г.
* Author: user
*/
#ifndef INC_PRIBORS_H_
#define INC_PRIBORS_H_
#define PRIBOR_PRDN1 0x00
#define PRIBOR_PRDN2 0x04
#define PRIBOR_PRDN3 0x08
#define PRIBOR_PRDN4 0x0C
#define PRIBOR_PRDV1 0x01
#define PRIBOR_PRDV2 0x05
#define PRIBOR_PRDV3 0x09
#define PRIBOR_PRDV4 0x0D
#define PRIBOR_PRDK1 0x02
#define PRIBOR_PRDK2 0x06
#define PRIBOR_PRDK3 0x0A
#define PRIBOR_PRDK4 0x0E
#define PRIBOR_UF 0x1B
#define IP_PRIBOR_PRDN1 "10.1.1.11"
#define IP_PRIBOR_PRDN2 "10.1.1.21"
#define IP_PRIBOR_PRDN3 "10.1.1.31"
#define IP_PRIBOR_PRDN4 "10.1.1.41"
#define IP_PRIBOR_PRDV1 "10.1.1.12"
#define IP_PRIBOR_PRDV2 "10.1.1.22"
#define IP_PRIBOR_PRDV3 "10.1.1.32"
#define IP_PRIBOR_PRDV4 "10.1.1.42"
#define IP_PRIBOR_PRDK1 "10.1.1.13"
#define IP_PRIBOR_PRDK2 "10.1.1.23"
#define IP_PRIBOR_PRDK3 "10.1.1.33"
#define IP_PRIBOR_PRDK4 "10.1.1.43"
#define IP_PRIBOR_UF "10.1.1.52"
#endif /* INC_PRIBORS_H_ */

View File

@@ -63,7 +63,7 @@
/* #define HAL_SD_MODULE_ENABLED */
/* #define HAL_MMC_MODULE_ENABLED */
/* #define HAL_SPI_MODULE_ENABLED */
/* #define HAL_TIM_MODULE_ENABLED */
#define HAL_TIM_MODULE_ENABLED
/* #define HAL_UART_MODULE_ENABLED */
/* #define HAL_USART_MODULE_ENABLED */
/* #define HAL_IRDA_MODULE_ENABLED */

View File

@@ -55,6 +55,7 @@ void SVC_Handler(void);
void DebugMon_Handler(void);
void PendSV_Handler(void);
void SysTick_Handler(void);
void TIM3_IRQHandler(void);
/* USER CODE BEGIN EFP */
/* USER CODE END EFP */

View File

@@ -19,10 +19,12 @@
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "lwip.h"
#include "lwip/udp.h"
#include "lwip/debug.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "pribors.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
@@ -40,20 +42,30 @@
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
TIM_HandleTypeDef htim3;
SRAM_HandleTypeDef hsram1;
/* USER CODE BEGIN PV */
/* USER CODE BEGIN PV */
uint32_t br_counter = 0;
unsigned char msg[1400];
struct rcv_msg rx_msg;
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_FMC_Init(void);
static void MX_TIM3_Init(void);
/* USER CODE BEGIN PFP */
void write_isa(uint16_t addr, uint16_t data);
uint16_t read_isa(uint16_t addr);
void sock_init(struct udp_pcb * pcb);
void udp_echo_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p,
const ip_addr_t *addr, u16_t port);
void rcv_command(void);
void set_pbuf_params(struct pbuf *p, u16_t length);
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
@@ -90,17 +102,26 @@ int main(void)
MX_GPIO_Init();
MX_LWIP_Init();
MX_FMC_Init();
MX_TIM3_Init();
/* USER CODE BEGIN 2 */
struct udp_pcb * pcb = udp_new();
sock_init(pcb);
const char* msg = "hello world1234";
struct pbuf *p = pbuf_alloc(0, 100, PBUF_RAM);
uint32_t br_addr = ipaddr_addr("10.1.1.5");
u16_t dst_port = 50000U;
p->len = strlen(msg);
p->tot_len = p->len;
strcpy(p->payload, msg);
memset(msg, 0, sizeof msg);
msg[0] = 1;
msg[1] = 2;
msg[2] = 3;
msg[3] = 4;
struct pbuf *p = pbuf_alloc(0, sizeof msg, PBUF_RAM);
uint32_t br_addr = ipaddr_addr("10.1.1.255");
u16_t br_port = 50000U;
set_pbuf_params(p, sizeof msg);
// timer start
if (HAL_TIM_Base_Start_IT(&htim3) != HAL_OK)
{
/* Starting Error */
Error_Handler();
}
/* USER CODE END 2 */
/* Infinite loop */
@@ -108,14 +129,13 @@ int main(void)
while (1)
{
MX_LWIP_Process();
//write_isa(0x110, 0x5555);
//write_isa(0x108, 0xaaaa);
//read_isa(0x2);
if (br_counter > 100)
{
err_t errok = udp_sendto(pcb, p, (ip_addr_t*)&br_addr, br_port);
br_counter = 0;
}
//HAL_Delay(100);
//read_isa(0x4);
err_t errok = udp_sendto(pcb, p, (ip_addr_t*)&br_addr, dst_port);
HAL_Delay(100);
read_isa(0x4);
}
/* USER CODE END WHILE */
}
@@ -166,6 +186,51 @@ void SystemClock_Config(void)
}
}
/**
* @brief TIM3 Initialization Function
* @param None
* @retval None
*/
static void MX_TIM3_Init(void)
{
/* USER CODE BEGIN TIM3_Init 0 */
/* USER CODE END TIM3_Init 0 */
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};
/* USER CODE BEGIN TIM3_Init 1 */
/* USER CODE END TIM3_Init 1 */
htim3.Instance = TIM3;
htim3.Init.Prescaler = 22;
htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
htim3.Init.Period = 2170;
htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_Base_Init(&htim3) != HAL_OK)
{
Error_Handler();
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN TIM3_Init 2 */
/* USER CODE END TIM3_Init 2 */
}
/* FMC initialization function */
static void MX_FMC_Init(void)
{
@@ -252,14 +317,58 @@ static void MX_GPIO_Init(void)
/* USER CODE BEGIN 4 */
void set_pbuf_params(struct pbuf *p, u16_t length)
{
p->len = length;
p->tot_len = p->len;
memcpy(p->payload, msg, p->tot_len);
}
void sock_init(struct udp_pcb * pcb)
{
uint32_t ipaddr = ipaddr_addr("10.1.1.69");
u16_t port = 50069U;
err_t errok = udp_bind(pcb, (ip_addr_t*)&ipaddr, port);
udp_recv(pcb, udp_echo_recv, NULL);
rx_msg.rcv_addr = 0;
rx_msg.cmd_num = 0;
rx_msg.cmd_code = 0;
rx_msg.clnt_con = 0;
rx_msg.length = 0;
memset(rx_msg.rx_buf, 0, sizeof rx_msg.rx_buf);
return;
}
void udp_echo_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p,
const ip_addr_t *addr, u16_t port)
{
if (p != NULL) {
/* send received packet back to sender */
udp_sendto(pcb, p, addr, port);
/* free the pbuf */
ip_addr_t rcv_addr = addr[0];
if (p->tot_len < sizeof rx_msg.rx_buf)
{
rx_msg.rcv_addr = (uint32_t) rcv_addr.addr;
rx_msg.port = port;
memcpy(rx_msg.rx_buf, p->payload, p->tot_len);
rx_msg.length = p->tot_len;
rcv_command();
}
pbuf_free(p);
}
}
void rcv_command(void)
{
if (rx_msg.length > 6)
{
rx_msg.cmd_num = rx_msg.rx_buf[2] | (rx_msg.rx_buf[3] << 8);
rx_msg.cmd_code = rx_msg.rx_buf[6];
rx_msg.clnt_con = 1;
}
}
uint16_t read_isa(uint16_t addr)
{
return *(uint16_t*)(ISA_BASE_RD + (addr << 1));

8
Core/Src/pribors.c Normal file
View File

@@ -0,0 +1,8 @@
/*
* pribors.c
*
* Created on: 6 сент. 2024 г.
* Author: user
*/

View File

@@ -76,6 +76,56 @@ void HAL_MspInit(void)
/* USER CODE END MspInit 1 */
}
/**
* @brief TIM_Base MSP Initialization
* This function configures the hardware resources used in this example
* @param htim_base: TIM_Base handle pointer
* @retval None
*/
void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base)
{
if(htim_base->Instance==TIM3)
{
/* USER CODE BEGIN TIM3_MspInit 0 */
/* USER CODE END TIM3_MspInit 0 */
/* Peripheral clock enable */
__HAL_RCC_TIM3_CLK_ENABLE();
/* TIM3 interrupt Init */
HAL_NVIC_SetPriority(TIM3_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(TIM3_IRQn);
/* USER CODE BEGIN TIM3_MspInit 1 */
/* USER CODE END TIM3_MspInit 1 */
}
}
/**
* @brief TIM_Base MSP De-Initialization
* This function freeze the hardware resources used in this example
* @param htim_base: TIM_Base handle pointer
* @retval None
*/
void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* htim_base)
{
if(htim_base->Instance==TIM3)
{
/* USER CODE BEGIN TIM3_MspDeInit 0 */
/* USER CODE END TIM3_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_TIM3_CLK_DISABLE();
/* TIM3 interrupt DeInit */
HAL_NVIC_DisableIRQ(TIM3_IRQn);
/* USER CODE BEGIN TIM3_MspDeInit 1 */
/* USER CODE END TIM3_MspDeInit 1 */
}
}
static uint32_t FMC_Initialized = 0;
static void HAL_FMC_MspInit(void){

View File

@@ -41,7 +41,7 @@
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
extern uint32_t br_counter;
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
@@ -55,7 +55,7 @@
/* USER CODE END 0 */
/* External variables --------------------------------------------------------*/
extern TIM_HandleTypeDef htim3;
/* USER CODE BEGIN EV */
/* USER CODE END EV */
@@ -198,6 +198,21 @@ void SysTick_Handler(void)
/* please refer to the startup file (startup_stm32f4xx.s). */
/******************************************************************************/
/**
* @brief This function handles TIM3 global interrupt.
*/
void TIM3_IRQHandler(void)
{
/* USER CODE BEGIN TIM3_IRQn 0 */
/* USER CODE END TIM3_IRQn 0 */
HAL_TIM_IRQHandler(&htim3);
/* USER CODE BEGIN TIM3_IRQn 1 */
br_counter++;
/* USER CODE END TIM3_IRQn 1 */
}
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */

View File

@@ -6,18 +6,21 @@
# Add inputs and outputs from these tool invocations to the build variables
C_SRCS += \
../Core/Src/main.c \
../Core/Src/pribors.c \
../Core/Src/stm32f4xx_hal_msp.c \
../Core/Src/stm32f4xx_it.c \
../Core/Src/system_stm32f4xx.c
OBJS += \
./Core/Src/main.o \
./Core/Src/pribors.o \
./Core/Src/stm32f4xx_hal_msp.o \
./Core/Src/stm32f4xx_it.o \
./Core/Src/system_stm32f4xx.o
C_DEPS += \
./Core/Src/main.d \
./Core/Src/pribors.d \
./Core/Src/stm32f4xx_hal_msp.d \
./Core/Src/stm32f4xx_it.d \
./Core/Src/system_stm32f4xx.d
@@ -30,7 +33,7 @@ Core/Src/%.o Core/Src/%.su: ../Core/Src/%.c Core/Src/subdir.mk
clean: clean-Core-2f-Src
clean-Core-2f-Src:
-$(RM) ./Core/Src/main.d ./Core/Src/main.o ./Core/Src/main.su ./Core/Src/stm32f4xx_hal_msp.d ./Core/Src/stm32f4xx_hal_msp.o ./Core/Src/stm32f4xx_hal_msp.su ./Core/Src/stm32f4xx_it.d ./Core/Src/stm32f4xx_it.o ./Core/Src/stm32f4xx_it.su ./Core/Src/system_stm32f4xx.d ./Core/Src/system_stm32f4xx.o ./Core/Src/system_stm32f4xx.su
-$(RM) ./Core/Src/main.d ./Core/Src/main.o ./Core/Src/main.su ./Core/Src/pribors.d ./Core/Src/pribors.o ./Core/Src/pribors.su ./Core/Src/stm32f4xx_hal_msp.d ./Core/Src/stm32f4xx_hal_msp.o ./Core/Src/stm32f4xx_hal_msp.su ./Core/Src/stm32f4xx_it.d ./Core/Src/stm32f4xx_it.o ./Core/Src/stm32f4xx_it.su ./Core/Src/system_stm32f4xx.d ./Core/Src/system_stm32f4xx.o ./Core/Src/system_stm32f4xx.su
.PHONY: clean-Core-2f-Src

View File

@@ -1,4 +1,5 @@
"./Core/Src/main.o"
"./Core/Src/pribors.o"
"./Core/Src/stm32f4xx_hal_msp.o"
"./Core/Src/stm32f4xx_it.o"
"./Core/Src/system_stm32f4xx.o"

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -147,12 +147,12 @@ static void low_level_init(struct netif *netif)
uint8_t MACAddr[6] ;
heth.Instance = ETH;
MACAddr[0] = 0xFB;
MACAddr[0] = 0x00;
MACAddr[1] = 0x80;
MACAddr[2] = 0xE1;
MACAddr[3] = 0x15;
MACAddr[4] = 0x85;
MACAddr[5] = 0xA3;
MACAddr[3] = 0x00;
MACAddr[4] = 0x00;
MACAddr[5] = 0x00;
heth.Init.MACAddr = &MACAddr[0];
heth.Init.MediaInterface = HAL_ETH_RMII_MODE;
heth.Init.TxDesc = DMATxDscrTab;

View File

@@ -30,7 +30,8 @@ Mcu.IP2=LWIP
Mcu.IP3=NVIC
Mcu.IP4=RCC
Mcu.IP5=SYS
Mcu.IPNb=6
Mcu.IP6=TIM3
Mcu.IPNb=7
Mcu.Name=STM32F429ZITx
Mcu.Package=LQFP144
Mcu.Pin0=PE3
@@ -87,11 +88,12 @@ Mcu.Pin54=PE0
Mcu.Pin55=PE1
Mcu.Pin56=VP_LWIP_VS_Enabled
Mcu.Pin57=VP_SYS_VS_Systick
Mcu.Pin58=VP_TIM3_VS_ClockSourceINT
Mcu.Pin6=PF1
Mcu.Pin7=PF2
Mcu.Pin8=PF3
Mcu.Pin9=PF4
Mcu.PinsNb=58
Mcu.PinsNb=59
Mcu.ThirdPartyNb=0
Mcu.UserConstants=
Mcu.UserName=STM32F429ZITx
@@ -107,6 +109,7 @@ NVIC.PendSV_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false
NVIC.PriorityGroup=NVIC_PRIORITYGROUP_4
NVIC.SVCall_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false
NVIC.SysTick_IRQn=true\:15\:0\:false\:false\:true\:false\:true\:false
NVIC.TIM3_IRQn=true\:0\:0\:false\:false\:true\:true\:true\:true
NVIC.UsageFault_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false
PA1.Mode=RMII
PA1.Signal=ETH_REF_CLK
@@ -332,8 +335,13 @@ SH.FMC_NOE.0=FMC_NOE,Sram1
SH.FMC_NOE.ConfNb=1
SH.FMC_NWE.0=FMC_NWE,Sram1
SH.FMC_NWE.ConfNb=1
TIM3.IPParameters=Prescaler,Period
TIM3.Period=10000
TIM3.Prescaler=6400
VP_LWIP_VS_Enabled.Mode=Enabled
VP_LWIP_VS_Enabled.Signal=LWIP_VS_Enabled
VP_SYS_VS_Systick.Mode=SysTick
VP_SYS_VS_Systick.Signal=SYS_VS_Systick
VP_TIM3_VS_ClockSourceINT.Mode=Internal
VP_TIM3_VS_ClockSourceINT.Signal=TIM3_VS_ClockSourceINT
board=custom