stm32_serDes_protobuf_project/bsp/bsp.c

129 lines
3.2 KiB
C
Raw Normal View History

2024-03-28 17:51:39 +03:00
#include "bsp.h"
2024-04-02 12:47:04 +03:00
#define PLL_M 4
#define PLL_N 168
/* SYSCLK = PLL_VCO / PLL_P */
#define PLL_P 2
/* USB OTG FS, SDIO and RNG Clock = PLL_VCO / PLLQ */
#define PLL_Q 7
2024-04-02 13:56:52 +03:00
// #ifdef __GNUC__
// #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
// #else
// #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
// #endif /* __GNUC__ */
// PUTCHAR_PROTOTYPE
// {
// for(uint8_t i = 0; i < len; ++i) {
// USART_SendData(USART2, ptr[i]);
// }
// return ch;
// }
int _write(int fd, char* ptr, int len) {
int i= 0;
while(i < len) {
while (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET); // Ждем, пока буфер передатчика не освободится
USART_SendData(USART2, *ptr++); // Отправляем символ
i++;
}
return len;
}
void UART_SendString(const char* str) {
while (*str) {
}
}
2024-04-02 12:47:04 +03:00
void SysTick_Handler(void)
{
//GPIO_ToggleBits(GPIOB, GPIO_Pin_0);
}
void USART2_IRQHandler()
{
}
void sysTick_init(void) {
uint32_t tick = SystemCoreClock/2 - 1;
SysTick->LOAD = tick;
SysTick->VAL = tick;
SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk | SysTick_CTRL_TICKINT_Msk | SysTick_CTRL_ENABLE_Msk;
}
void systemClock_init(void) {
RCC_DeInit(); // Reset RCC configuration to default values
RCC_HSEConfig(RCC_HSE_ON); // Enable HSE (High Speed External) clock
if (RCC_WaitForHSEStartUp() == SUCCESS) { // Wait for HSE clock to stabilize
RCC_PLLConfig(RCC_PLLSource_HSE, PLL_M, PLL_N, PLL_P, PLL_Q); // Configure PLL
RCC_PLLCmd(ENABLE); // Enable PLL
while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET); // Wait for PLL to stabilize
FLASH->ACR = FLASH_ACR_ICEN |FLASH_ACR_DCEN |FLASH_ACR_LATENCY_5WS;
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); // Set PLL as system clock
while (RCC_GetSYSCLKSource() != RCC_CFGR_SWS_PLL); // Wait for PLL to become system clock
}
}
void gpio_init() {
const uint16_t led_pins = GPIO_Pin_0 | GPIO_Pin_7 | GPIO_Pin_14;
const uint16_t usart_pins = GPIO_Pin_5 | GPIO_Pin_6;
2024-03-28 17:51:39 +03:00
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
2024-04-02 12:47:04 +03:00
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, 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);
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);
}
void usart_init() {
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);
USART_Cmd(USART2, ENABLE);
//NVIC_EnableIRQ(USART2_IRQn);
}
void board_init() {
SystemInit();
//systemClock_init();
sysTick_init();
//__enable_irq();
gpio_init();
usart_init();
2024-03-28 17:51:39 +03:00
}