From 72ee0299ffb6b6c73efb48beb612d3425a0791a7 Mon Sep 17 00:00:00 2001 From: Kevin Thomas Date: Fri, 6 Oct 2023 16:52:28 -0400 Subject: [PATCH] 0x0001_hello-world code --- 0x0001_hello-world/.vscode/settings.json | 5 ++ 0x0001_hello-world/include/usart.h | 60 +++++++++++++++++++ 0x0001_hello-world/src/main.c | 16 +++++ 0x0001_hello-world/src/usart.c | 75 ++++++++++++++++++++++++ 4 files changed, 156 insertions(+) create mode 100644 0x0001_hello-world/.vscode/settings.json create mode 100644 0x0001_hello-world/include/usart.h create mode 100644 0x0001_hello-world/src/usart.c diff --git a/0x0001_hello-world/.vscode/settings.json b/0x0001_hello-world/.vscode/settings.json new file mode 100644 index 0000000..0b9e0c6 --- /dev/null +++ b/0x0001_hello-world/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "files.associations": { + "stm32f4xx.h": "c" + } +} \ No newline at end of file diff --git a/0x0001_hello-world/include/usart.h b/0x0001_hello-world/include/usart.h new file mode 100644 index 0000000..83b44cd --- /dev/null +++ b/0x0001_hello-world/include/usart.h @@ -0,0 +1,60 @@ +/** + * FILE: usart.h + * + * DESCRIPTION: This file contains the usart function definitions utilizing the STM32F401CC6 microcontroller. + * + * AUTHOR: Kevin Thomas + * DATE: October 6, 2023 + */ + +#ifndef USART_H +#define USART_H + +#include + +#include "stm32f4xx.h" + +#define DEFAULT_CLK_SPEED 16000000U + +extern int __io_putchar(int ch) __attribute__((weak)); + +/** + * @brief Sets the baud rate for a given USART peripheral. + * + * This function calculates and sets the baud rate register (BRR) for + * the specified USART peripheral based on the provided peripheral clock + * speed and desired baud rate. + * + * @param USARTx: Pointer to the USART peripheral (e.g., USART1, USART2, etc.). + * @param periph_clk: The frequency of the peripheral clock in Hertz. + * @param baud_rate: The desired baud rate for communication. + * @retval None + */ +static void set_baud_rate(USART_TypeDef *USARTx, uint32_t periph_clk, uint32_t baud_rate); + +/** + * @brief Initializes USART2 for transmission on PA2 pin. + * + * This function configures the necessary settings for USART2 to enable + * transmission on pin PA2. It includes configuring GPIO, setting + * alternate function mode, configuring the USART clock, setting the + * baud rate, and enabling the transmitter. + * + * @param None + * @retval None + */ +void usart2_tx_init(void); + +/** + * @brief Writes a single byte to USART2 for transmission. + * + * This function writes a single byte to the USART2 transmit data + * register for transmission. It ensures that the transmit data register + * is empty before writing the byte. + * + * @param ch: The byte to be transmitted. + * @retval None + */ +void usart2_write(uint8_t ch); + +#endif // USART_H diff --git a/0x0001_hello-world/src/main.c b/0x0001_hello-world/src/main.c index f89ee09..331797b 100644 --- a/0x0001_hello-world/src/main.c +++ b/0x0001_hello-world/src/main.c @@ -1,6 +1,22 @@ +/** + * FILE: main.s + * + * DESCRIPTION: This file contains the main function for the hello-world program utilizing the STM32F401CC6 microcontroller. + * + * AUTHOR: Kevin Thomas + * DATE: October 6, 2023 + */ + #include +#include "usart.h" + int main(void) { + usart2_tx_init(); + while (1) + { + printf("hello, world\r\n"); + } } diff --git a/0x0001_hello-world/src/usart.c b/0x0001_hello-world/src/usart.c new file mode 100644 index 0000000..3e77647 --- /dev/null +++ b/0x0001_hello-world/src/usart.c @@ -0,0 +1,75 @@ +/** + * FILE: usart.s + * + * DESCRIPTION: This file contains the usart functions utilizing the STM32F401CC6 microcontroller. + * + * AUTHOR: Kevin Thomas + * DATE: October 6, 2023 + */ + + +#include "usart.h" + +__attribute__((weak)) int _write(int file, char *ptr, int len) +{ + (void)file; + int data_idx; + + for (data_idx = 0; data_idx < len; data_idx++) + { + __io_putchar(*ptr++); + } + + return len; +} + +int __io_putchar(int ch) +{ + // write a single char + usart2_write(ch); + + return ch; +} + +static void set_baud_rate(USART_TypeDef *USARTx, uint32_t periph_clk, uint32_t baud_rate) +{ + // set baud rate register + USARTx->BRR = ((periph_clk + (baud_rate / 2)) / baud_rate); +} + +void usart2_tx_init(void) +{ + // enable GPIOA clock + RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN; + + // set PA2 to output alternate function mode + GPIOA->MODER |= GPIO_MODER_MODE2_1; + GPIOA->MODER &= ~GPIO_MODER_MODE2_0; + + // set PA2 alternate function type to UART_TX (AF07) + GPIOA->AFR[0] &= ~GPIO_AFRL_AFRL2_3; + GPIOA->AFR[0] |= GPIO_AFRL_AFRL2_2; + GPIOA->AFR[0] |= GPIO_AFRL_AFRL2_1; + GPIOA->AFR[0] |= GPIO_AFRL_AFRL2_0; + + // enable USART2 clock + RCC->APB1ENR |= RCC_APB1ENR_USART2EN; + + // configure baud rate to 9600 + set_baud_rate(USART2, DEFAULT_CLK_SPEED, 9600); + + // configure TX transmitter enable + USART2->CR1 |= USART_CR1_TE; + + // enable UART2 + USART2->CR1 |= USART_CR1_UE; +} + +void usart2_write(uint8_t ch) +{ + // verify transmit data register is empty, will return true if the bit is set + while(!(USART2->SR & USART_SR_TXE)) {}; + + // write a single char to the transmit data register + USART2->DR = (ch & 0xFF); +}