stm32_serDes_protobuf_project/bsp/bsp.c

160 lines
4.3 KiB
C
Raw Normal View History

2024-04-30 13:10:11 +03:00
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_rcc.h"
2024-05-02 17:19:55 +03:00
#include "stm32f4xx_dma.h"
2024-04-30 13:10:11 +03:00
#include "misc.h"
2024-04-10 19:45:21 +03:00
#include "bsp.h"
2024-05-02 17:19:55 +03:00
#include "lwip.h"
2024-04-30 13:10:11 +03:00
#define MAX_DELAY 0xFFFFFFFU
#define INTERVAL 500
2024-04-11 21:53:16 +03:00
2024-04-10 19:45:21 +03:00
static uint32_t EthStatus = 0;
2024-04-30 13:10:11 +03:00
static volatile uint32_t sysTick = 0;
static volatile uint32_t cnt = 0;
2024-04-02 12:47:04 +03:00
void SysTick_Handler(void)
{
2024-04-30 13:10:11 +03:00
if (cnt == INTERVAL) {
GPIO_ToggleBits(GPIOB, GPIO_Pin_14);
cnt = 0;
}
sysTick++;
cnt++;
2024-04-02 12:47:04 +03:00
}
2024-04-30 13:10:11 +03:00
uint32_t getSysTick() {
return sysTick;
}
void delay(uint32_t delay) {
uint32_t tickStart = getSysTick();
uint32_t wait = delay;
if (wait < MAX_DELAY) {
wait++;
}
while((getSysTick() - tickStart) < wait) {}
}
2024-04-02 12:47:04 +03:00
void USART2_IRQHandler()
{
}
void gpio_init() {
const uint16_t led_pins = GPIO_Pin_0 | GPIO_Pin_7 | GPIO_Pin_14;
2024-04-10 19:45:21 +03:00
RCC_AHB1PeriphClockCmd(
RCC_AHB1Periph_GPIOA |
RCC_AHB1Periph_GPIOB |
RCC_AHB1Periph_GPIOC |
RCC_AHB1Periph_GPIOD |
RCC_AHB1Periph_GPIOG,
ENABLE
);
2024-03-28 17:51:39 +03:00
GPIO_InitTypeDef gpio;
2024-04-02 12:47:04 +03:00
2024-03-28 17:51:39 +03:00
GPIO_StructInit(&gpio);
gpio.GPIO_Mode = GPIO_Mode_OUT;
2024-04-02 12:47:04 +03:00
gpio.GPIO_Pin = led_pins;
2024-03-28 17:51:39 +03:00
GPIO_Init(GPIOB, &gpio);
2024-04-02 12:47:04 +03:00
GPIO_ResetBits(GPIOB, led_pins);
2024-05-03 12:10:38 +03:00
}
void usart_init() {
GPIO_InitTypeDef gpio;
const uint16_t usart_pins = GPIO_Pin_5 | GPIO_Pin_6;
2024-04-02 12:47:04 +03:00
gpio.GPIO_Mode = GPIO_Mode_AF;
gpio.GPIO_Pin = usart_pins;
gpio.GPIO_Speed = GPIO_Speed_50MHz;
gpio.GPIO_OType = GPIO_OType_PP;
gpio.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOD, &gpio);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource5, GPIO_AF_USART2);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource6, GPIO_AF_USART2);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
USART_InitTypeDef usart;
usart.USART_BaudRate = 115200;
usart.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART2, &usart);
2024-05-02 17:19:55 +03:00
USART_Cmd(USART2, ENABLE);
2024-04-02 12:47:04 +03:00
}
2024-05-02 17:19:55 +03:00
void dma_init() {
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);
DMA_InitTypeDef dma;
dma.DMA_Channel = DMA_Channel_0;
dma.DMA_DIR = DMA_DIR_MemoryToMemory;
dma.DMA_PeripheralInc = DMA_PeripheralInc_Enable;
dma.DMA_MemoryInc = DMA_MemoryInc_Enable;
dma.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
dma.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
dma.DMA_Mode = DMA_Mode_Normal;
dma.DMA_Priority = DMA_Priority_Low;
dma.DMA_FIFOMode = DMA_FIFOMode_Enable;
dma.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
dma.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
dma.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_Init(DMA2_Stream0, &dma);
2024-04-08 14:21:04 +03:00
}
2024-04-02 12:47:04 +03:00
2024-05-03 12:10:38 +03:00
void eth_init() {
GPIO_InitTypeDef gpio;
gpio.GPIO_Mode = GPIO_Mode_AF;
gpio.GPIO_Speed = GPIO_Speed_100MHz;
gpio.GPIO_OType = GPIO_OType_PP;
gpio.GPIO_PuPd = GPIO_PuPd_NOPULL;
gpio.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_7;
GPIO_Init(GPIOA, &gpio);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource1, GPIO_AF_ETH); //RMII ref clock
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_ETH); //RMII MDIO
GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_ETH); //RMII RX Data Valid
gpio.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_4 | GPIO_Pin_5;
GPIO_Init(GPIOC, &gpio);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource1, GPIO_AF_ETH); //RMII MDC
GPIO_PinAFConfig(GPIOC, GPIO_PinSource4, GPIO_AF_ETH); //RMII RXD0
GPIO_PinAFConfig(GPIOC, GPIO_PinSource5, GPIO_AF_ETH); //RMII RXD1
gpio.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_13;
GPIO_Init(GPIOG, &gpio);
GPIO_PinAFConfig(GPIOG, GPIO_PinSource11, GPIO_AF_ETH); //RMII TX enable
GPIO_PinAFConfig(GPIOG, GPIO_PinSource13, GPIO_AF_ETH); //RMII TXD0
gpio.GPIO_Pin = GPIO_Pin_13;
GPIO_Init(GPIOB, &gpio);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF_ETH); //RMII TXD1
RCC_AHB1PeriphClockCmd(RCC_AHB1ENR_ETHMACEN, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1ENR_ETHMACRXEN, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1ENR_ETHMACTXEN, ENABLE);
NVIC_SetPriority(ETH_IRQn, 0);
NVIC_EnableIRQ(ETH_IRQn);
}
2024-04-08 14:21:04 +03:00
void board_init() {
2024-04-30 13:10:11 +03:00
uint32_t tick = SystemCoreClock/1000;
2024-04-08 14:21:04 +03:00
SysTick_Config(tick);
NVIC_EnableIRQ(SysTick_IRQn);
__enable_irq();
2024-04-02 12:47:04 +03:00
gpio_init();
usart_init();
2024-05-03 12:10:38 +03:00
// dma_init();
eth_init();
2024-05-02 17:19:55 +03:00
init_LWIP();
2024-04-30 13:10:11 +03:00
delay(50); //wait until periph init
printf("Controller is started...\r\n");
2024-03-28 17:51:39 +03:00
}
2024-04-24 15:06:06 +03:00