diff --git a/Source/BSP/Inc/lidar.h b/Source/BSP/Inc/lidar.h index e69de29..9777017 100644 --- a/Source/BSP/Inc/lidar.h +++ b/Source/BSP/Inc/lidar.h @@ -0,0 +1,24 @@ +#pragma once + +#ifndef LIDAR_H +#define LIDAR_H + +#include "stm32g431xx.h" + +typedef struct +{ + uint8_t header1; // 0x59 + uint8_t header2; // 0x59 + uint8_t distance_l; // cm + uint8_t distance_h; // cm + uint8_t strength_l; + uint8_t strength_h; + uint8_t temp_l; + uint8_t temp_h; + uint8_t checksum; +} lidar_data; + +void lidar_init(); +void lidar_update(); + +#endif \ No newline at end of file diff --git a/Source/BSP/Src/lidar.c b/Source/BSP/Src/lidar.c index e69de29..d50b85b 100644 --- a/Source/BSP/Src/lidar.c +++ b/Source/BSP/Src/lidar.c @@ -0,0 +1,54 @@ +#include "lidar.h" + +void lidar_init() +{ + RCC->AHB2ENR |= RCC_AHB2ENR_GPIOBEN; + + // port 11 alt func mode + GPIOB->MODER &= ~(3 << (11 * 2)); + GPIOB->MODER |= 2 << (11 * 2); + + // set AF7 on AFRegister for GPIOB11 + GPIOB->AFR[1] &= ~(0xF << 12); + GPIOB->AFR[1] |= 7 << 12; + + // very high speed + GPIOB->OSPEEDR |= 3 << (11 * 2); + + // pull-up + GPIOB->PUPDR &= ~(3 << (11 * 2)); + GPIOB->PUPDR |= 1 << (11 * 2); + + // SYSCLK selected as USART3 clock + RCC->CCIPR &= ~(RCC_CCIPR_USART3SEL); + RCC->CCIPR |= 1 << RCC_CCIPR_USART3SEL_Pos; + RCC->APB1ENR1 |= RCC_APB1ENR1_USART3EN; + + USART3->CR1 = 0; + USART3->CR2 = 0; + USART3->CR3 = 0; + + USART3->BRR = 16000000UL / 115200UL; + + // parity control disable + USART3->CR1 &= ~(USART_CR1_PCE); + + // word length 8 bit + USART3->CR1 &= ~USART_CR1_M1 & ~USART_CR1_M0; + + // 1 stop bit + USART3->CR2 &= ~USART_CR2_STOP; + + // receiver enable + // interrupt generated whenever ORE = 1 or RXNE = 1 + USART3->CR1 |= USART_CR1_RE | USART_CR1_RXNEIE; + + // overrun disable + USART3->CR3 |= USART_CR3_OVRDIS; + + // USART3 enable + USART3->CR1 |= USART_CR1_UE; + + // Interrupt enable + NVIC_EnableIRQ(USART3_IRQn); +} \ No newline at end of file