Skip to content

Commit ea61956

Browse files
committed
working synchronous usart
1 parent 2cd857f commit ea61956

File tree

3 files changed

+50
-39
lines changed

3 files changed

+50
-39
lines changed

ECU/Core/Src/main.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ int main(void)
117117

118118
/* Infinite loop */
119119
/* USER CODE BEGIN WHILE */
120-
uint8_t buffer[128];
121120
USARTConfig config;
122121
USARTHandle handle = usart_init_peripheral(&config);
123122
while (1) {

Lib/Inhouse/Peripherals/USART/usart.c

Lines changed: 44 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,55 @@
11
#include "usart.h"
2+
#include "Logomatic.h"
23
#include "string.h"
34

45
#include "usart_ll_platform_deps.h"
56

67
USARTHandle usart_init_peripheral(USARTConfig *config)
78
{
89
USARTHandle handle;
9-
handle.baud_rate = config->baud_rate;
1010

11-
LL_USART_InitTypeDef USART_InitStruct = {0};
12-
LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
13-
14-
// TODO: this should come from USARTConfig
15-
/**USART1 GPIO Configuration
16-
PB6 ------> USART1_TX
17-
PB7 ------> USART1_RX
18-
*/
19-
GPIO_InitStruct.Pin = LL_GPIO_PIN_6 | LL_GPIO_PIN_7;
20-
GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
21-
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
22-
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
23-
GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
24-
GPIO_InitStruct.Alternate = LL_GPIO_AF_7;
25-
LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
26-
27-
NVIC_SetPriority(USART1_IRQn,
28-
NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 0, 0));
29-
NVIC_EnableIRQ(USART1_IRQn);
30-
31-
USART_InitStruct.PrescalerValue = LL_USART_PRESCALER_DIV1;
32-
USART_InitStruct.BaudRate = config->baud_rate;
33-
USART_InitStruct.DataWidth = LL_USART_DATAWIDTH_8B;
34-
USART_InitStruct.StopBits = LL_USART_STOPBITS_1;
35-
USART_InitStruct.Parity = LL_USART_PARITY_NONE;
36-
USART_InitStruct.TransferDirection = LL_USART_DIRECTION_TX_RX;
37-
USART_InitStruct.HardwareFlowControl = LL_USART_HWCONTROL_NONE;
38-
USART_InitStruct.OverSampling = LL_USART_OVERSAMPLING_16;
39-
40-
LL_USART_Init(USART1, &USART_InitStruct);
41-
LL_USART_SetTXFIFOThreshold(USART1, LL_USART_FIFOTHRESHOLD_1_8);
42-
LL_USART_SetRXFIFOThreshold(USART1, LL_USART_FIFOTHRESHOLD_1_8);
43-
LL_USART_DisableFIFO(USART1);
11+
// Enable GPIOB and USART1 clocks
12+
LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_GPIOB);
13+
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_USART1);
14+
15+
// Select PCLK2 as USART1 clock source
16+
LL_RCC_SetUSARTClockSource(LL_RCC_USART1_CLKSOURCE_PCLK2);
17+
18+
// Configure PB6 = TX, PB7 = RX
19+
LL_GPIO_InitTypeDef gpio = {0};
20+
gpio.Pin = LL_GPIO_PIN_6 | LL_GPIO_PIN_7;
21+
gpio.Mode = LL_GPIO_MODE_ALTERNATE;
22+
gpio.Alternate = LL_GPIO_AF_7;
23+
gpio.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
24+
gpio.Pull = LL_GPIO_PULL_UP;
25+
gpio.Speed = LL_GPIO_SPEED_FREQ_HIGH;
26+
LL_GPIO_Init(GPIOB, &gpio);
27+
28+
// Configure USART
29+
LL_USART_InitTypeDef us = {0};
30+
us.PrescalerValue = LL_USART_PRESCALER_DIV1;
31+
us.BaudRate = config->baud_rate;
32+
us.DataWidth = LL_USART_DATAWIDTH_8B;
33+
us.StopBits = LL_USART_STOPBITS_1;
34+
us.Parity = LL_USART_PARITY_NONE;
35+
us.TransferDirection = LL_USART_DIRECTION_TX_RX;
36+
us.HardwareFlowControl = LL_USART_HWCONTROL_NONE;
37+
us.OverSampling = LL_USART_OVERSAMPLING_16;
38+
LL_USART_Init(USART1, &us);
39+
4440
LL_USART_ConfigAsyncMode(USART1);
4541

42+
// Enable TX/RX explicitly and enable USART
43+
LL_USART_EnableDirectionTx(USART1);
44+
LL_USART_EnableDirectionRx(USART1);
4645
LL_USART_Enable(USART1);
4746

48-
while ((!(LL_USART_IsActiveFlag_TEACK(USART1))) ||
49-
(!(LL_USART_IsActiveFlag_REACK(USART1)))) {
47+
// Wait for TEACK/REACK
48+
while (!LL_USART_IsActiveFlag_TEACK(USART1) ||
49+
!LL_USART_IsActiveFlag_REACK(USART1)) {
5050
}
5151

52+
handle.baud_rate = config->baud_rate;
5253
return handle;
5354
}
5455

@@ -62,10 +63,15 @@ void usart_send(USARTHandle *handle, uint8_t *data, uint32_t size)
6263
size = strlen((char *)data);
6364
}
6465
for (uint32_t i = 0; i < size; i++) {
65-
while (!LL_USART_IsActiveFlag_TXE(USART1))
66-
;
66+
while (!LL_USART_IsActiveFlag_TXE_TXFNF(USART1)) {
67+
LOGOMATIC("Waiting for TXE flag...\n");
68+
}
6769
LL_USART_TransmitData8(USART1, data[i]);
6870
}
71+
72+
// wait for final byte to transmit
73+
while (!LL_USART_IsActiveFlag_TXE_TXFNF(USART1))
74+
;
6975
}
7076

7177
uint32_t usart_receive(USARTHandle *handle, uint8_t *buffer, uint32_t size,

Lib/Inhouse/Peripherals/USART/usart_ll_platform_deps.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
#ifdef STM32L476xx
2+
#include "stm32l4xx_ll_bus.h"
23
#include "stm32l4xx_ll_gpio.h"
4+
#include "stm32l4xx_ll_rcc.h"
35
#include "stm32l4xx_ll_usart.h"
46
#elif defined(STM32G474xx)
7+
#include "stm32g4xx_ll_bus.h"
58
#include "stm32g4xx_ll_gpio.h"
9+
#include "stm32g4xx_ll_rcc.h"
610
#include "stm32g4xx_ll_usart.h"
711
#elif defined(STM32U5A9xx)
12+
#include "stm32u5xx_ll_bus.h"
813
#include "stm32u5xx_ll_gpio.h"
14+
#include "stm32u5xx_ll_rcc.h"
915
#include "stm32u5xx_ll_usart.h"
1016
#else
1117
#error "Unsupported STM32 family"

0 commit comments

Comments
 (0)