-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathsoftuart.c
324 lines (274 loc) · 6.89 KB
/
softuart.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*
Software Uart For Stm32
By Liyanboy74
https://github.com/liyanboy74
*/
#include "softuart.h"
// Some internal define
#if(SoftUart_PARITY)
#define SoftUart_IDEF_LEN_C1 (SoftUart_DATA_LEN+2)
#else
#define SoftUart_IDEF_LEN_C1 (SoftUart_DATA_LEN+1)
#endif
#define SoftUart_IDEF_LEN_C2 (SoftUart_IDEF_LEN_C1 + SoftUart_STOP_Bit)
// All Soft Uart Config and State
SoftUart_S SUart [Number_Of_SoftUarts];
// TX RX Data Buffer
SoftUartBuffer_S SUBuffer[Number_Of_SoftUarts];
// For timing division
__IO uint8_t SU_Timer=0;
// Parity var
static uint8_t DV,PCount;
// Read RX single Pin Value
GPIO_PinState SoftUartGpioReadPin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
{
return HAL_GPIO_ReadPin(GPIOx,GPIO_Pin);
}
// Write TX single Pin Value
void SoftUartGpioWritePin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState)
{
HAL_GPIO_WritePin(GPIOx,GPIO_Pin,PinState);
}
// Initial Soft Uart
SoftUartState_E SoftUartInit(uint8_t SoftUartNumber,GPIO_TypeDef *TxPort,uint16_t TxPin,GPIO_TypeDef *RxPort,uint16_t RxPin)
{
if(SoftUartNumber>=Number_Of_SoftUarts)return SoftUart_Error;
SUart[SoftUartNumber].TxNComplated=0;
SUart[SoftUartNumber].RxBitCounter=0;
SUart[SoftUartNumber].RxBitShift=0;
SUart[SoftUartNumber].RxIndex=0;
SUart[SoftUartNumber].TxEnable=0;
SUart[SoftUartNumber].RxEnable=0;
SUart[SoftUartNumber].TxBitCounter=0;
SUart[SoftUartNumber].TxBitShift=0;
SUart[SoftUartNumber].TxIndex=0;
SUart[SoftUartNumber].TxSize=0;
SUart[SoftUartNumber].Buffer=&SUBuffer[SoftUartNumber];
SUart[SoftUartNumber].RxPort=RxPort;
SUart[SoftUartNumber].RxPin=RxPin;
SUart[SoftUartNumber].TxPort=TxPort;
SUart[SoftUartNumber].TxPin=TxPin;
SUart[SoftUartNumber].RxTimingFlag=0;
SUart[SoftUartNumber].RxBitOffset=0;
return SoftUart_OK;
}
// Send one bit to TX pin
void SoftUartTransmitBit(SoftUart_S *SU,uint8_t Bit0_1)
{
SoftUartGpioWritePin(SU->TxPort,SU->TxPin,(GPIO_PinState)Bit0_1);
}
// Enable Soft Uart Receiving
SoftUartState_E SoftUartEnableRx(uint8_t SoftUartNumber)
{
if(SoftUartNumber>=Number_Of_SoftUarts)return SoftUart_Error;
SUart[SoftUartNumber].RxEnable=1;
return SoftUart_OK;
}
// Disable Soft Uart Receiving
SoftUartState_E SoftUartDisableRx(uint8_t SoftUartNumber)
{
if(SoftUartNumber>=Number_Of_SoftUarts)return SoftUart_Error;
SUart[SoftUartNumber].RxEnable=0;
return SoftUart_OK;
}
// Read Size of Received Data in buffer
uint8_t SoftUartRxAlavailable(uint8_t SoftUartNumber)
{
return SUart[SoftUartNumber].RxIndex;
}
// Move Received Data to Another Buffer
SoftUartState_E SoftUartReadRxBuffer(uint8_t SoftUartNumber,uint8_t *Buffer,uint8_t Len)
{
int i;
if(SoftUartNumber>=Number_Of_SoftUarts)return SoftUart_Error;
for(i=0;i<Len;i++)
{
Buffer[i]=SUart[SoftUartNumber].Buffer->Rx[i];
}
for(i=0;i<SUart[SoftUartNumber].RxIndex;i++)
{
SUart[SoftUartNumber].Buffer->Rx[i]=SUart[SoftUartNumber].Buffer->Rx[i+Len];
}
SUart[SoftUartNumber].RxIndex-=Len;
return SoftUart_OK;
}
// Soft Uart Transmit Data Process
void SoftUartTxProcess(SoftUart_S *SU)
{
if(SU->TxEnable)
{
// Start
if(SU->TxBitCounter==0)
{
SU->TxNComplated=1;
SU->TxBitShift=0;
SoftUartTransmitBit(SU,0);
SU->TxBitCounter++;
PCount=0;
}
// Data
else if(SU->TxBitCounter<(SoftUart_DATA_LEN+1))
{
DV=((SU->Buffer->Tx[SU->TxIndex])>>(SU->TxBitShift))&0x01;
SoftUartTransmitBit(SU,DV);
SU->TxBitCounter++;
SU->TxBitShift++;
if(DV)PCount++;
}
// Parity
else if(SU->TxBitCounter<SoftUart_IDEF_LEN_C1)
{
// Check Even or Odd
DV=PCount%2;
// if Odd Parity
if(SoftUart_PARITY==1)DV=!DV;
SoftUartTransmitBit(SU,DV);
SU->TxBitCounter++;
}
// Stop
else if(SU->TxBitCounter<SoftUart_IDEF_LEN_C2)
{
SoftUartTransmitBit(SU,1);
SU->TxBitCounter++;
}
//Complete
else if(SU->TxBitCounter==SoftUart_IDEF_LEN_C2)
{
// Reset Bit Counter
SU->TxBitCounter=0;
// Ready To Send Another Data
SU->TxIndex++;
// Check Size of Data
if(SU->TxSize > SU->TxIndex)
{
// Continue Sending
SU->TxNComplated=1;
SU->TxEnable=1;
}
else
{
// Finish
SU->TxNComplated=0;
SU->TxEnable=0;
}
}
}
}
// Soft Uart Receive Data Process
void SoftUartRxDataBitProcess(SoftUart_S *SU,uint8_t B0_1)
{
if(SU->RxEnable)
{
// Start
if(SU->RxBitCounter==0)
{
// Start Bit is 0
if(B0_1)return;
SU->RxBitShift=0;
SU->RxBitCounter++;
SU->Buffer->Rx[SU->RxIndex]=0;
}
// Data
else if(SU->RxBitCounter<(SoftUart_DATA_LEN+1))
{
SU->Buffer->Rx[SU->RxIndex]|=((B0_1&0x01)<<SU->RxBitShift);
SU->RxBitCounter++;
SU->RxBitShift++;
}
// Parity
else if(SU->RxBitCounter<SoftUart_IDEF_LEN_C1)
{
// Need to be check
// B0_1;
SU->RxBitCounter++;
}
// Stop & Complete
else if(SU->RxBitCounter<SoftUart_IDEF_LEN_C2)
{
SU->RxBitCounter=0;
SU->RxTimingFlag=0;
//Stop Bit must be 1
if(B0_1)
{
// Received successfully
// Change RX Buffer Index
if((SU->RxIndex)<(SoftUartRxBufferSize-1))(SU->RxIndex)++;
}
// if not : ERROR -> Overwrite data
}
}
}
// Wait Until Transmit Completed
// You do not usually need to use this function!
void SoftUartWaitUntilTxComplate(uint8_t SoftUartNumber)
{
while(SUart[SoftUartNumber].TxNComplated);
}
// Copy Data to Transmit Buffer and Start Sending
SoftUartState_E SoftUartPuts(uint8_t SoftUartNumber,uint8_t *Data,uint8_t Len)
{
int i;
if(SoftUartNumber>=Number_Of_SoftUarts)return SoftUart_Error;
if(SUart[SoftUartNumber].TxNComplated) return SoftUart_Error;
SUart[SoftUartNumber].TxIndex=0;
SUart[SoftUartNumber].TxSize=Len;
for(i=0;i<Len;i++)
{
SUart[SoftUartNumber].Buffer->Tx[i]= Data[i];
}
SUart[SoftUartNumber].TxNComplated=1;
SUart[SoftUartNumber].TxEnable=1;
return SoftUart_OK;
}
// Capture RX and Get BitOffset
uint8_t SoftUartScanRxPorts(void)
{
int i;
uint8_t Buffer=0x00,Bit;
for(i=0;i<Number_Of_SoftUarts;i++)
{
// Read RX GPIO Value
Bit=SoftUartGpioReadPin(SUart[i].RxPort,SUart[i].RxPin);
// Starting conditions
if(!SUart[i].RxBitCounter && !SUart[i].RxTimingFlag && !Bit)
{
// Save RX Bit Offset
// Calculate middle position of data puls
SUart[i].RxBitOffset=((SU_Timer+2)%5);
// Timing Offset is Set
SUart[i].RxTimingFlag=1;
}
// Add all RX GPIO State to Buffer
Buffer|=((Bit&0x01)<<i);
}
return Buffer;
}
// SoftUartHandler must call in interrupt every 0.2*(1/BR)
// if BR=9600 then 0.2*(1/9600)=20.8333333 uS
void SoftUartHandler(void)
{
int i;
uint8_t SU_DBuffer;
// Capture RX and Get BitOffset
SU_DBuffer = SoftUartScanRxPorts();
for(i=0;i < Number_Of_SoftUarts;i++)
{
// Receive Data if we in middle data pulse position
if(SUart[i].RxBitOffset == SU_Timer)
{
SoftUartRxDataBitProcess(&SUart[i],((SU_DBuffer>>i)&0x01));
}
}
// Sending always happens in the first time slot
if(SU_Timer==0)
{
// Transmit Data
for(i=0;i < Number_Of_SoftUarts;i++)
{
SoftUartTxProcess(&SUart[i]);
}
}
// Timing process
SU_Timer++;
if(SU_Timer >= 5)SU_Timer=0;
}