-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUart.c
67 lines (46 loc) · 2.14 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
#include "Uart.h"
void UART0_Transmit(uint8_t data)
{
//Punem in asteptare pana cand registrul de transmisie a datelor nu este gol
while(!(UART0->S1 & UART0_S1_TDRE_MASK));
UART0->D = data;
}
void initUART0(uint32_t baud_rate)
{
//Setarea sursei de ceas pentru modulul UART
SIM->SOPT2 |= SIM_SOPT2_UART0SRC(01);
//Activarea semnalului de ceas pentru modulul UART
SIM->SCGC4 |= SIM_SCGC4_UART0_MASK;
//Activarea semnalului de ceas pentru portul A
//intrucat dorim sa folosim pinii PTA1, respectiv PTA2 pentru comunicarea UART
SIM->SCGC5 |= SIM_SCGC5_PORTA_MASK;
//Fiecare pin pune la dispozitie mai multe functionalitati
//la care avem acces prin intermediul multiplexarii
PORTA->PCR[1] = ~PORT_PCR_MUX_MASK;
PORTA->PCR[1] = PORT_PCR_ISF_MASK | PORT_PCR_MUX(2); // Configurare RX pentru UART0
PORTA->PCR[2] = ~PORT_PCR_MUX_MASK;
PORTA->PCR[2] = PORT_PCR_ISF_MASK | PORT_PCR_MUX(2); // Configurare TX pentru UART0
UART0->C2 &= ~((UART0_C2_RE_MASK) | (UART0_C2_TE_MASK));
//Configurare Baud Rate
uint32_t osr = 15; // Over-Sampling Rate (numarul de esantioane luate per bit-time)
//SBR - vom retine valoarea baud rate-ului calculat pe baza frecventei ceasului de sistem
// SBR - b16 b15 b14 [b13 b12 b11 b10 b09 b08 b07 b06 b05 b04 b03 b02 b01] &
// 0x1F00 - 0 0 0 1 1 1 1 1 0 0 0 0 0 0 0 0
// 0 0 0 b13 b12 b11 b10 b09 0 0 0 0 0 0 0 0 >> 8
// BDH - 0 0 0 b13 b12 b11 b10 b09
// BDL - b08 b07 b06 b05 b04 b03 b02 b01
uint32_t sbr32 = (uint32_t)((DEFAULT_SYSTEM_CLOCK)/(baud_rate * (osr+1)));
uint16_t sbr = (uint16_t)(sbr32);
uint8_t temp = UART0->BDH & ~(UART0_BDH_SBR(0x1F));
UART0->BDH = temp | UART0_BDH_SBR(((sbr & 0x1F00)>> 8));
UART0->BDL = (uint8_t)(sbr & UART_BDL_SBR_MASK);
UART0->C4 |= UART0_C4_OSR(osr);
//Setare numarul de biti de date la 8 si fara bit de paritate
UART0->C1 = 0;
//Dezactivare intreruperi la transmisie
UART0->C2 |= UART0_C2_TIE(0);
UART0->C2 |= UART0_C2_TCIE(0);
//Dezactivare intreruperi la receptie
UART0->C2 |= UART0_C2_RIE(0);
UART0->C2 |= ((UART_C2_RE_MASK) | (UART_C2_TE_MASK));
}