Skip to content

Commit fa43b95

Browse files
committed
stop hardcoding everything except for clock
1 parent 379667a commit fa43b95

File tree

3 files changed

+26
-23
lines changed

3 files changed

+26
-23
lines changed

BLINKY/G4HELLO/Core/Src/main.c

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,28 @@ int main(void)
117117
USARTConfig config = {
118118
.instance = USART1,
119119
.tx_queue_length = 16,
120-
.baud_rate = 115200,
121120
.on_rx_byte = received_byte_callback,
121+
.gpio_port = GPIOB,
122+
.ll_gpio =
123+
&(LL_GPIO_InitTypeDef){
124+
.Pin = LL_GPIO_PIN_6 | LL_GPIO_PIN_7,
125+
.Mode = LL_GPIO_MODE_ALTERNATE,
126+
.Alternate = LL_GPIO_AF_7,
127+
.OutputType = LL_GPIO_OUTPUT_PUSHPULL,
128+
.Pull = LL_GPIO_PULL_UP,
129+
.Speed = LL_GPIO_SPEED_FREQ_HIGH,
130+
},
131+
.ll_usart =
132+
&(LL_USART_InitTypeDef){
133+
.PrescalerValue = LL_USART_PRESCALER_DIV1,
134+
.BaudRate = 115200,
135+
.DataWidth = LL_USART_DATAWIDTH_8B,
136+
.StopBits = LL_USART_STOPBITS_1,
137+
.Parity = LL_USART_PARITY_NONE,
138+
.TransferDirection = LL_USART_DIRECTION_TX_RX,
139+
.HardwareFlowControl = LL_USART_HWCONTROL_NONE,
140+
.OverSampling = LL_USART_OVERSAMPLING_16,
141+
},
122142
};
123143
USARTHandle *handle = usart_init_peripheral(&config);
124144
for (uint8_t i = 0; i < 30; i++) {

Lib/Peripherals/USART/usart.c

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -97,27 +97,8 @@ void usart_init_hardware(USARTConfig *config, USARTHandle *handle)
9797
// Select PCLK2 as USART1 clock source
9898
LL_RCC_SetUSARTClockSource(LL_RCC_USART1_CLKSOURCE_PCLK2);
9999

100-
// Configure PB6 = TX, PB7 = RX
101-
LL_GPIO_InitTypeDef gpio = {0};
102-
gpio.Pin = LL_GPIO_PIN_6 | LL_GPIO_PIN_7;
103-
gpio.Mode = LL_GPIO_MODE_ALTERNATE;
104-
gpio.Alternate = LL_GPIO_AF_7;
105-
gpio.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
106-
gpio.Pull = LL_GPIO_PULL_UP;
107-
gpio.Speed = LL_GPIO_SPEED_FREQ_HIGH;
108-
LL_GPIO_Init(GPIOB, &gpio);
109-
110-
// Configure USART
111-
LL_USART_InitTypeDef us = {0};
112-
us.PrescalerValue = LL_USART_PRESCALER_DIV1;
113-
us.BaudRate = config->baud_rate;
114-
us.DataWidth = LL_USART_DATAWIDTH_8B;
115-
us.StopBits = LL_USART_STOPBITS_1;
116-
us.Parity = LL_USART_PARITY_NONE;
117-
us.TransferDirection = LL_USART_DIRECTION_TX_RX;
118-
us.HardwareFlowControl = LL_USART_HWCONTROL_NONE;
119-
us.OverSampling = LL_USART_OVERSAMPLING_16;
120-
LL_USART_Init(handle->instance, &us);
100+
LL_GPIO_Init(config->gpio_port, config->ll_gpio);
101+
LL_USART_Init(handle->instance, config->ll_usart);
121102

122103
LL_USART_ConfigAsyncMode(handle->instance);
123104

Lib/Peripherals/USART/usart.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ typedef void (*USART_RxByteCallback)(uint8_t byte);
66
typedef struct {
77
USART_TypeDef *instance; // i.e. USART1, USART2, etc.
88
uint32_t tx_queue_length; // in # of messages
9-
uint32_t baud_rate;
109
USART_RxByteCallback on_rx_byte;
10+
GPIO_TypeDef *gpio_port; // i.e. GPIOA, GPIOB, etc.
11+
LL_GPIO_InitTypeDef *ll_gpio;
12+
LL_USART_InitTypeDef *ll_usart;
1113
} USARTConfig;
1214

1315
typedef struct usart_handle_st USARTHandle;

0 commit comments

Comments
 (0)