From 06beadfe683809d68f30655b5caed25550df912e Mon Sep 17 00:00:00 2001 From: "Sebastian H. Gabrielli" Date: Wed, 6 Mar 2024 15:36:24 +0100 Subject: [PATCH 1/2] Add UART functionality --- .gitignore | 4 ++- prosjekt.X/main.c | 19 +++++++++----- prosjekt.X/nbproject/configurations.xml | 4 ++- prosjekt.X/uart.c | 25 ++++++++++++++++++ prosjekt.X/uart.h | 35 +++++++++++++++++++++++++ 5 files changed, 78 insertions(+), 9 deletions(-) create mode 100644 prosjekt.X/uart.c create mode 100644 prosjekt.X/uart.h diff --git a/.gitignore b/.gitignore index d14efc5..f31b37b 100644 --- a/.gitignore +++ b/.gitignore @@ -26,4 +26,6 @@ # Large Files *.exe *.zip -*.pdf \ No newline at end of file +*.pdf +/prosjekt.X/build/ +/prosjekt.X/dist/ \ No newline at end of file diff --git a/prosjekt.X/main.c b/prosjekt.X/main.c index 5f964b3..b42afa9 100644 --- a/prosjekt.X/main.c +++ b/prosjekt.X/main.c @@ -5,14 +5,19 @@ * Created on March 6, 2024, 12:34 PM */ +#define F_CPU 4E6 + #include #include +#include "uart.h" +#include -/* - * - */ -int main(int argc, char** argv) { - - return (EXIT_SUCCESS); -} +int main() { + init_uart((uint16_t)9600); + stdout = &USART_stream; + while (1) { + printf("Hello, world!\n"); + _delay_ms(500); + } +} \ No newline at end of file diff --git a/prosjekt.X/nbproject/configurations.xml b/prosjekt.X/nbproject/configurations.xml index d7f88a4..e8e1b9c 100644 --- a/prosjekt.X/nbproject/configurations.xml +++ b/prosjekt.X/nbproject/configurations.xml @@ -4,6 +4,7 @@ + uart.h main.c + uart.c AVR128DB48 - noID + nEdbgTool XC8 2.45 2 diff --git a/prosjekt.X/uart.c b/prosjekt.X/uart.c new file mode 100644 index 0000000..67648d1 --- /dev/null +++ b/prosjekt.X/uart.c @@ -0,0 +1,25 @@ + +#include "uart.h" + +void init_uart(uint16_t baud) { + // Configure UART pin directions + PORTB.DIR &= ~PIN1_bm; + PORTB.DIR |= PIN0_bm; + // Set the baudrate + USART3.BAUD = (uint16_t)USART3_BAUD_RATE(baud); + // Enable UART TX & RX + USART3.CTRLB |= USART_TXEN_bm; +} + +void USART3_sendChar(char c) { + // Hold the code while the UART is not ready to send + while (!(USART3.STATUS & USART_DREIF_bm)) { ; } + // UART is ready, send the character. + USART3.TXDATAL = c; + +} + +int USART3_printChar(char c, FILE *stream) { + USART3_sendChar(c); + return 0; +} \ No newline at end of file diff --git a/prosjekt.X/uart.h b/prosjekt.X/uart.h new file mode 100644 index 0000000..6259eee --- /dev/null +++ b/prosjekt.X/uart.h @@ -0,0 +1,35 @@ +/* + * File: uart.h + * Author: Sebastian H. Gabrielli + * + * Created on March 6, 2024, 3:19 PM + */ + +#ifndef UART_H +#define UART_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include + +#ifndef F_CPU + #define F_CPU 4E6 +#endif + +#define USART3_BAUD_RATE(BAUD_RATE) ((float)(F_CPU * 64 / (16 * (float)BAUD_RATE)) + 0.5) + +void init_uart(uint16_t baud); +void USART3_sendChar(char c); +int USART3_printChar(char c, FILE *stream); + +static FILE USART_stream = FDEV_SETUP_STREAM(USART3_printChar, NULL, _FDEV_SETUP_WRITE); + +#ifdef __cplusplus +} +#endif + +#endif /* UART_H */ + From 7dd8b67378f74c1834ec0da7b3572298d1450869 Mon Sep 17 00:00:00 2001 From: "Sebastian H. Gabrielli" Date: Wed, 6 Mar 2024 15:55:56 +0100 Subject: [PATCH 2/2] Add comments --- prosjekt.X/uart.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/prosjekt.X/uart.h b/prosjekt.X/uart.h index 6259eee..7284552 100644 --- a/prosjekt.X/uart.h +++ b/prosjekt.X/uart.h @@ -20,9 +20,14 @@ extern "C" { #endif #define USART3_BAUD_RATE(BAUD_RATE) ((float)(F_CPU * 64 / (16 * (float)BAUD_RATE)) + 0.5) - + +// Initialize the USART3 controller void init_uart(uint16_t baud); + +// Send a single character over UART void USART3_sendChar(char c); + +// Send a string of characters over UART int USART3_printChar(char c, FILE *stream); static FILE USART_stream = FDEV_SETUP_STREAM(USART3_printChar, NULL, _FDEV_SETUP_WRITE);