-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathuart_core_lib.c
182 lines (155 loc) · 4.67 KB
/
uart_core_lib.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include <stdio.h> // printf setvbuf
#include <stdlib.h> // free y malloc
#include <math.h> // round
#include <string.h> // strlen
#include "socal/socal.h" // alt_write_hword alt_read_hword
#include "socal/hps.h" // para BASEADD
#include "uart_core_lib.h"
// INITIAL CONFIG
UART_T uart_init(uint32_t baseadd)
{
UART_T uart;
uart.BASEADD = baseadd;
uart.RXDATA = baseadd + (uint32_t)(0x00);
uart.TXDATA = baseadd + (uint32_t)(0x04);
uart.STATUS = baseadd + (uint32_t)(0x08);
uart.CONTROL = baseadd + (uint32_t)(0x0c);
uart.DIVISOR = baseadd + (uint32_t)(0x10);
uart.ENDOFPACK = baseadd + (uint32_t)(0x14);
// default buffersize value
// 10MB
uart.buffer_size = 10000000;
return uart;
}
void setBaud(UART_T * uart_p, uint32_t clock_frequency, uint32_t baudrate)
{
// Formula segun
// Embedded Peripherals IP User Guide - UART Core
float divisor = round((float)clock_frequency/(float)baudrate + 0.5f);
alt_write_hword(uart_p->DIVISOR, (uint16_t)divisor);
}
void setBufferSize(UART_T * uart_p, uint32_t size)
{
uart_p->buffer_size = size;
uart_p->buffer_count = 0;
uart_p->buffer_pointer_init = malloc(size*sizeof(uint8_t));
uart_p->buffer_pointer_writing = uart_p->buffer_pointer_init;
uart_p->buffer_pointer_reading = uart_p->buffer_pointer_init;
}
void freeBuffer(UART_T * uart_p)
{
free(uart_p->buffer_pointer_init);
}
// UART CORE REGISTER ACCESS
uint16_t uartRegister(uint32_t address)
{
return alt_read_hword(address);
}
void checkUartRegisters(UART_T * uart_p)
{
printf("%s\n", "*******************************");
printf("%s ", "RX Data:");
printf("%i\n", uartRegister(uart_p->RXDATA));
printf("%s ", "TX Data:");
printf("%i\n", uartRegister(uart_p->TXDATA));
printf("%s ", "Status:");
uint16_t status = uartRegister(uart_p->STATUS);
printf("%i\n", status);
printf("%s ", "Exception:");
printf("%i\n", BIT(status, 8));
printf("%s ", "ROE:");
printf("%i\n", BIT(status, 3));
printf("%s ", "TOE:");
printf("%i\n", BIT(status, 4));
printf("%s ", "Break:");
printf("%i\n", BIT(status, 2));
printf("%s ", "Frame Error:");
printf("%i\n", BIT(status, 1));
printf("%s\n", "*******************************");
// printf("%s ", "control:");
// printUartAdd(uart_p->CONTROL);
// printf("%s ", "divisor:");
// printUartAdd(uart_p->DIVISOR);
// printf("%s ", "end of packet:");
// printUartAdd(uart_p->ENDOFPACK);
}
void resetStatus(UART_T * uart_p)
{
alt_write_hword(uart_p->STATUS, 0);
}
// PROCESO DE ENVIO DE DATOS
// Checkea si el registro esta listo para enviar nuevo caracter
// true si esta listo
bool checkTxdata(UART_T * uart_p)
{
uint16_t status = alt_read_hword(uart_p->STATUS);
return BIT(status, 6);
}
void enviarChar(UART_T * uart_p, char character)
{
while(checkTxdata(uart_p) == 0) {}; // txready es 1 cuando
// la transmision anterios se completo
alt_write_hword(uart_p->TXDATA, (uint16_t)character);
}
void enviarString(UART_T * uart_p, char * str, int num)
{
for (int i = 0; i < num; ++i) {
enviarChar(uart_p, *str);
str++;
}
}
// PROCESO DE RECEPCION DE DATOS
// Checkea si hay algun dato en espera a ser leido
// true si hay un dato en espera
bool checkRxdata(UART_T * uart_p)
{
uint16_t status = alt_read_hword(uart_p->STATUS);
return BIT(status, 7);
}
uint8_t recibirChar(UART_T * uart_p)
{
return (uint8_t)alt_read_hword(uart_p->RXDATA);
}
// PROCESO DE RECEPCION AUTOMATICA DE DATOS
void activarRecepcion(UART_T * uart_p)
{
uart_p->thread_run = true;
pthread_t thread = uart_p->thread;
pthread_create(&thread, NULL, activarRecepcion_aux, uart_p);
}
void * activarRecepcion_aux(void * ptr)
{
UART_T * uart_p = (UART_T*)ptr;
setBufferSize(uart_p, uart_p->buffer_size); // crea malloc
// si el usuario no lo ha hecho
uart_p->buffer_count = 0;
uint8_t * posicionInicial = uart_p->buffer_pointer_writing;
while(uart_p->thread_run) {
if(checkRxdata(uart_p)) {
*(uart_p->buffer_pointer_writing) = recibirChar(uart_p);
uart_p->buffer_pointer_writing++;
}
}
uart_p->buffer_count =
(uint32_t)(uart_p->buffer_pointer_writing) - (uint32_t)posicionInicial;
return NULL;
}
void terminarRecepcion(UART_T * uart_p)
{
uart_p->thread_run = false;
}
uint8_t * vaciarBuffer(UART_T * uart_p, uint32_t * num)
{
*num = uart_p->buffer_count;
uint8_t * data_array = malloc(*num * sizeof(uint8_t));
uint8_t * data_array_init = data_array;
for(int i = 0; i<*num; i++) {
*data_array = *(uart_p->buffer_pointer_reading);
uart_p->buffer_pointer_reading++;
data_array++;
}
uart_p->buffer_pointer_reading = uart_p->buffer_pointer_init;
uart_p->buffer_pointer_writing = uart_p->buffer_pointer_init;
uart_p->buffer_count = 0;
return data_array_init;
}