-
Notifications
You must be signed in to change notification settings - Fork 0
/
uart.c
72 lines (63 loc) · 1.38 KB
/
uart.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include <avr/interrupt.h>
#include <stdio.h>
#include "m128_hal.h"
#include "uart.h"
#include "timer.h"
/*
ISR(USART_TX_vect)
{
PORTC = 0xFF;
}*/
/*
* Static function declarations
*/
/*
* Public function declarations
*/
int printk (const char *fmt, ...)
{
va_list args;
char printbuffer[RS232_PRINT_BUFFER] = {0};
if (strlen(fmt) + SYSTIME_STR_LEN > RS232_PRINT_BUFFER) {
goto error;
}
va_start (args, fmt);
vsprintf (printbuffer, fmt, args);
va_end (args);
if (strlen(printbuffer) + SYSTIME_STR_LEN <= RS232_PRINT_BUFFER)
{
memmove(printbuffer+SYSTIME_STR_LEN, printbuffer, strlen(printbuffer));
memcpy(printbuffer, get_timestamp_str(), SYSTIME_STR_LEN);
print(printbuffer);
}
return 0;
error:
printk("printk error! Output too long.\n");
return -1;
}
void uart_init(void)
{
/*
* If interrupt is enabled an interrupt vector must be present!
*/
UART_BAUD_RATE_REG_LOW = 16;
UCSR2A |= (1<<U2X2);
UCSR2B = (1<<RXEN2) | (1<<TXEN2) | (1<<RXCIE2);/* | (1<<TXCIE2)*/
//print("\x1B[2J");
//print("\x1B[0;0H");
}
void print(char *c)
{
while(*c)
{
/* Wait for hardware */
while ( !( UCSR2A & (1<<UDRE2)) )
;
/* Put data into buffer, sends the data */
UART_DATA_REG = *c;
c++;
}
}