-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathUart.c
131 lines (102 loc) · 2.86 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
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
/*
* Uart.c
*
* Created on: 05 îêò. 2014 ã.
* Author: lamazavr
*/
#include "Uart.h"
#include "modbus_slave.h"
// áóôåð äëÿ modbus
Uint16 UartBuffer[50];
// êîëè÷åñòâî äàííûõ â áóôåðå
Uint16 UartRxLen = 0;
void Uart_init()
{
Uart_init_gpio();
// Òàêòèðîâàíèå ìîäóëÿ áûëî âêëþ÷åíî â
// InitSysCtrl() --> InitPeripheralClocks()
SciaRegs.SCICCR.all =0x0007; // 1 ñòîï áèò, áåç loopback
// áåç ïðîâåðêè ÷åòíîñòè, 8 áèò,
// àñèíõðîííàÿ ïåðåäà÷à
SciaRegs.SCICTL1.all =0x0043; // âêë TX, RX, internal SCICLK,
// îòêë RX ERR, SLEEP, TXWAKE
// RX interrupt
SciaRegs.SCICTL2.bit.TXINTENA = 1;
SciaRegs.SCICTL2.bit.RXBKINTENA = 1;
SciaRegs.SCIFFTX.all=0xC022;
SciaRegs.SCIFFRX.all=0x0022;
SciaRegs.SCIFFCT.all=0x00;
// SCI BRR = LSPCLK/(SCI BAUDx8) - 1
// 9600 áîä
// 194 = 15000000 / (9600*8) - 1
// 256000 áîä
// 6 = 15000000 / (256000*8) - 1
SciaRegs.SCIHBAUD = 0x0000;
SciaRegs.SCILBAUD = 0x0006; // äëÿ 256000 áîä
SciaRegs.SCICTL1.all = 0x0023; // Relinquish SCI from Reset
SciaRegs.SCIFFTX.bit.TXFIFOXRESET=1;
SciaRegs.SCIFFRX.bit.RXFIFORESET=1;
// ðàçðåøàåì ïðåðûâàíèå ïî ïðèåìó äàííûõ ïî uart
PieCtrlRegs.PIECTRL.bit.ENPIE = 1;
PieCtrlRegs.PIEIER9.bit.INTx1 = 1;
IER |= M_INT9; // Ðàçðåøàåì ïðåðûâàíèå ÿäðà ïî ëèíèè 9
Uart_timer_init();
}
void Uart_init_gpio()
{
EALLOW;
GpioCtrlRegs.GPAPUD.bit.GPIO28 = 0; // ïîäòÿæêà íà GPIO28 (SCIRXDA)
GpioCtrlRegs.GPAPUD.bit.GPIO29 = 1; // îòêë ïîäòÿæêè íà GPIO29 (SCITXDA)
GpioCtrlRegs.GPAQSEL2.bit.GPIO28 = 3; // àñèíõðîííûé âõîä GPIO28 (SCIRXDA)
GpioCtrlRegs.GPAMUX2.bit.GPIO28 = 1; // GPIO28 --> SCIRXDA
GpioCtrlRegs.GPAMUX2.bit.GPIO29 = 1; // GPIO29 --> SCITXDA
EDIS;
}
void Uart_send(Uint16 a)
{
while (SciaRegs.SCIFFTX.bit.TXFFST != 0) {}
SciaRegs.SCITXBUF = a;
}
void Uart_send_msg(char *msg)
{
while(*msg)
{
Uart_send(*msg++);
}
}
void Uart_timer_init()
{
EALLOW;
PieVectTable.TINT0 = &cpu_timer0_isr;
EDIS;
InitCpuTimers();
ConfigCpuTimer(&CpuTimer0, 60, 30000); // 30ms
CpuTimer0Regs.TCR.all = 0x4001;
IER |= M_INT1;
// Enable TINT0 in the PIE: Group 1 interrupt 7
PieCtrlRegs.PIEIER1.bit.INTx7 = 1;
}
interrupt void cpu_timer0_isr(void)
{
// stop timer
CpuTimer0.RegsAddr->TCR.bit.TSS = 1;
// çàïðåùàåì ïðåðûâàíèÿ íà âðåìÿ îáðàáîòêè è ïåðåäà÷è
DINT;
if (UartRxLen > 0)
{
Uint16 len = modbus_func(UartBuffer, UartRxLen, 2);
Uint16 i = 0;
//TODO: èçìåíèòü íà ïðåðûâàíèå
for (i = 0; i < len; i++)
{
Uart_send(UartBuffer[i]);
}
UartRxLen = 0;
}
// ðàçðåøàåì ïðåðûâàíèÿ
EINT;
// start timer
CpuTimer0.RegsAddr->TCR.bit.TSS = 0;
// Acknowledge this interrupt to receive more interrupts from group 1
PieCtrlRegs.PIEACK.all = PIEACK_GROUP1;
}