Add example USART driver
This commit is contained in:
18
os/delay.c
Normal file
18
os/delay.c
Normal file
@@ -0,0 +1,18 @@
|
||||
#define CLOCK_FREQUENCY 120000
|
||||
|
||||
unsigned int ms_to_ticks(unsigned int ms) {
|
||||
return ms * (CLOCK_FREQUENCY/3);
|
||||
}
|
||||
|
||||
void delay_routine(unsigned int delay_counter) {
|
||||
asm("mov r1, %[input]\n"
|
||||
"loop_2:\n"
|
||||
"subs r1, #1\n"
|
||||
"cmp r1, #0\n"
|
||||
"bne loop_2\n"
|
||||
: [input] "=r" (delay_counter));
|
||||
}
|
||||
|
||||
void delay_ms(unsigned int seconds) {
|
||||
delay_routine(ms_to_ticks(seconds));
|
||||
}
|
||||
2
os/delay.h
Normal file
2
os/delay.h
Normal file
@@ -0,0 +1,2 @@
|
||||
void delay_routine(unsigned int delay_counter);
|
||||
void delay_ms(unsigned int ms);
|
||||
57
os/driver/usart.c
Normal file
57
os/driver/usart.c
Normal file
@@ -0,0 +1,57 @@
|
||||
#include "usart.h"
|
||||
#include "os/delay.h"
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#define USART_SR_TXE (1 << 7)
|
||||
|
||||
typedef struct uart_s {
|
||||
uint32_t USART_SR;
|
||||
uint32_t USART_DR;
|
||||
uint32_t USART_BRR;
|
||||
uint32_t USART_CR1;
|
||||
uint32_t USART_CR2;
|
||||
uint32_t USART_CR3;
|
||||
uint32_t USART_GTPR;
|
||||
} uart_c;
|
||||
|
||||
volatile uart_c *const USART1 = (uart_c *)0x40011000;
|
||||
|
||||
static uint32_t usart_tx_ready(void) {
|
||||
return (USART1->USART_SR & USART_SR_TXE) >> 7;
|
||||
}
|
||||
|
||||
uint32_t usart_tx_write(const uint8_t *data_bytes, uint32_t n_bytes) {
|
||||
if (data_bytes == NULL) return USART_TX_ERROR;
|
||||
|
||||
for (int i = 0; i < n_bytes; i++) {
|
||||
unsigned int timeout = TIMEOUT;
|
||||
while (!usart_tx_ready()) {
|
||||
delay_ms(1);
|
||||
timeout--;
|
||||
if (timeout == 0) return USART_TX_BUSY;
|
||||
}
|
||||
|
||||
USART1->USART_DR = *data_bytes;
|
||||
|
||||
data_bytes++;
|
||||
}
|
||||
return USART_TX_COMPLETE;
|
||||
}
|
||||
|
||||
uint32_t usart_tx_write_string(const uint8_t *data_bytes) {
|
||||
if (data_bytes == NULL) return USART_TX_ERROR;
|
||||
|
||||
while (*data_bytes != '\0') {
|
||||
unsigned int timeout = TIMEOUT;
|
||||
while (!usart_tx_ready()) {
|
||||
delay_ms(1);
|
||||
timeout--;
|
||||
if (timeout == 0) return USART_TX_BUSY;
|
||||
}
|
||||
|
||||
USART1->USART_DR = *data_bytes;
|
||||
|
||||
data_bytes++;
|
||||
}
|
||||
return USART_TX_COMPLETE;
|
||||
}
|
||||
12
os/driver/usart.h
Normal file
12
os/driver/usart.h
Normal file
@@ -0,0 +1,12 @@
|
||||
#include <stdint.h>
|
||||
#define TIMEOUT 1000
|
||||
|
||||
enum UartErrorCode {
|
||||
USART_TX_ERROR,
|
||||
USART_TX_BUSY,
|
||||
USART_TX_COMPLETE
|
||||
};
|
||||
|
||||
static uint32_t usart_tx_ready(void);
|
||||
uint32_t usart_tx_write(const uint8_t *data_bytes, uint32_t n_bytes);
|
||||
uint32_t usart_tx_write_string(const uint8_t *data_bytes);
|
||||
Reference in New Issue
Block a user