-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuart.c
More file actions
63 lines (45 loc) · 1.51 KB
/
uart.c
File metadata and controls
63 lines (45 loc) · 1.51 KB
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
#include "uart.h"
#include <stdlib.h>
void uart_init(){
/* Pin 24 is TXD Pin 25 is RXD */
GPIO->DIRSET |= (1 << 24); //Set as output
GPIO->DIR &= ~(1 << 25); //Set as input //krasjer alt
UART->PSELTXD = 24; //Set as TXD
UART->PSELRXD = 25; //Set as RXD
UART->BAUDRATE = 0x00275000; //Set baud rate to 9600 (actual rate 9598)
UART->PSELCTS = 0xFFFFFFFF; //Disable CTS
UART->PSELRTS = 0xFFFFFFFF; //Disable RTS
UART->ENABLE = 4; //Enable uart;
UART->STARTRX = 1; //Start uart reciever
}
void uart_send(char letter){
UART->STARTTX = 1; //Start uart reciever
UART->TXDRDY = 0; //Set interrupt register for UART low
UART->TXD = letter; //Put character to be sendt into transfer register
while (!(UART->TXDRDY)){ //Interrupt on TXDRDY is triggered when transmission is complete. wait until this trigger
//Wait until transmission completed
}
UART->STOPTX = 1; //Trigger STOPTX task to stop UART transmission
}
char uart_read(){
if (UART->RXDRDY){
/*A byte has been moved to the RXD register*/
UART->RXDRDY = 0;
UART->STARTTX = 1; //UART reception squance is started
char RXD_cont = UART->RXD;
//UART->STOPRX = 1;
return RXD_cont;
}
else{
/*No byte in the RXD register*/
return '\0';
}
}
ssize_t _write(int fd , const void *buf, size_t count){
char * letter = (char *)(buf);
for(int i = 0; i < count; i++){
uart_send(*letter);
letter++;
}
return count;
}