mirror of
https://github.com/mytechnotalent/Embedded-Hacking.git
synced 2026-04-02 17:41:05 +02:00
0x0001_hello-world code
This commit is contained in:
5
0x0001_hello-world/.vscode/settings.json
vendored
Normal file
5
0x0001_hello-world/.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"files.associations": {
|
||||
"stm32f4xx.h": "c"
|
||||
}
|
||||
}
|
||||
60
0x0001_hello-world/include/usart.h
Normal file
60
0x0001_hello-world/include/usart.h
Normal file
@@ -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 <stdint.h>
|
||||
|
||||
#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
|
||||
@@ -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 <stdio.h>
|
||||
|
||||
#include "usart.h"
|
||||
|
||||
int main(void)
|
||||
{
|
||||
usart2_tx_init();
|
||||
|
||||
while (1)
|
||||
{
|
||||
printf("hello, world\r\n");
|
||||
}
|
||||
}
|
||||
|
||||
75
0x0001_hello-world/src/usart.c
Normal file
75
0x0001_hello-world/src/usart.c
Normal file
@@ -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);
|
||||
}
|
||||
Reference in New Issue
Block a user