hw2 done
This commit is contained in:
38
lib/uart.c
38
lib/uart.c
@@ -1,5 +1,6 @@
|
||||
#include <uart.h>
|
||||
#include <gpio.h>
|
||||
#include <stddef.h>
|
||||
|
||||
/* Auxilary mini UART registers */
|
||||
#define AUX_ENABLE ((volatile unsigned int*)(MMIO_BASE+0x00215004))
|
||||
@@ -15,6 +16,8 @@
|
||||
#define AUX_MU_STAT ((volatile unsigned int*)(MMIO_BASE+0x00215064))
|
||||
#define AUX_MU_BAUD ((volatile unsigned int*)(MMIO_BASE+0x00215068))
|
||||
|
||||
int is_uart_init = 0;
|
||||
|
||||
/**
|
||||
* Set baud rate and characteristics (115200 8N1) and map to GPIO
|
||||
*/
|
||||
@@ -42,6 +45,8 @@ void uart_init()
|
||||
r=150; while(r--) { asm volatile("nop"); }
|
||||
*GPPUDCLK0 = 0; // flush GPIO setup
|
||||
*AUX_MU_CNTL = 3; // enable Tx, Rx
|
||||
|
||||
is_uart_init = 1;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -54,13 +59,13 @@ void uart_send(unsigned int c) {
|
||||
*AUX_MU_IO=c;
|
||||
}
|
||||
|
||||
byte_t uart_getb()
|
||||
uint8_t uart_getb()
|
||||
{
|
||||
byte_t r;
|
||||
uint8_t r;
|
||||
/* wait until something is in the buffer */
|
||||
do{asm volatile("nop");}while(!(*AUX_MU_LSR&0x01));
|
||||
/* read it and return */
|
||||
r=(byte_t)(*AUX_MU_IO);
|
||||
r=(uint8_t)(*AUX_MU_IO);
|
||||
|
||||
return r;
|
||||
}
|
||||
@@ -78,7 +83,7 @@ char uart_getc() {
|
||||
/**
|
||||
* Display a string
|
||||
*/
|
||||
void uart_puts(char *s) {
|
||||
void uart_puts(const char *s) {
|
||||
while(*s) {
|
||||
/* convert newline to carrige return + newline */
|
||||
if(*s=='\n')
|
||||
@@ -90,7 +95,7 @@ void uart_puts(char *s) {
|
||||
/**
|
||||
* Display a binary value in hexadecimal
|
||||
*/
|
||||
void uart_hex(unsigned int d) {
|
||||
void uart_hex(const unsigned int d) {
|
||||
unsigned int n;
|
||||
int c;
|
||||
for(c=28;c>=0;c-=4) {
|
||||
@@ -101,3 +106,26 @@ void uart_hex(unsigned int d) {
|
||||
uart_send(n);
|
||||
}
|
||||
}
|
||||
|
||||
void uart_int(int d)
|
||||
{
|
||||
unsigned int buf[11];
|
||||
int size = 0;
|
||||
|
||||
if (d == 0) {
|
||||
uart_send((unsigned int)'0');
|
||||
return;
|
||||
}
|
||||
|
||||
int is_neg = (d < 0);
|
||||
|
||||
while (d != 0) {
|
||||
buf[size++] = '0' + d % 10;
|
||||
d /= 10;
|
||||
}
|
||||
|
||||
if (is_neg)
|
||||
uart_send((unsigned int)'-');
|
||||
while (size--)
|
||||
uart_send(buf[size]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user