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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
|
/*
uart_soft
v0.2
copyright John Steggall 2009
*/
/*
Copyright 2009 John Steggall (steggall.j@gmail.com)
Permission to use, copy, modify, and distribute this software
and its documentation for any purpose and without fee is hereby
granted, provided that the above copyright notice appear in all
copies and that both that the copyright notice and this
permission notice and warranty disclaimer appear in supporting
documentation, and that the name of the author not be used in
advertising or publicity pertaining to distribution of the
software without specific, written prior permission.
The author disclaim all warranties with regard to this
software, including all implied warranties of merchantability
and fitness. In no event shall the author be liable for any
special, indirect or consequential damages or any damages
whatsoever resulting from loss of use, data or profits, whether
in an action of contract, negligence or other tortious action,
arising out of or in connection with the use or performance of
this software.
*/
#include <avr/io.h>
#include "SoftUARTConf.h"
#define SFT_TX_EN 7
#define SF_UART_TX 1
#define SF_UART_RX 2
.section .data
rxdata:
.byte 0
txShifter:
.byte 0
txBitcount:
.byte 0
rxShifter:
.byte 0
rxBitcount:
.byte 0
.global status
status:
.byte 0
.section .text
.global RX_PIN_INT
/*********************************************
* External interrupt
*
* RX pin has gone low.
*/
RX_PIN_INT:
push r16
lds r16,SREG
push r16
#if (RXPORT>=32)
lds r16,RXPORT
sbrc r16,0 // anti glitch
#else
sbic RXPORT,0
#endif
rjmp ignore
nop
nop
nop
nop
#if (RXPORT>=32)
lds r16,RXPORT
sbrc r16,0 // anti glitch
#else
sbic RXPORT,0
#endif
rjmp ignore
push r17
// grab timer value
lds r16,TC_COUNTL
lds r17,TC_COUNTH
// set trigger for RX timer (will need to add a little more though)
sts TC_RX_COMPH,r17
sts TC_RX_COMPL,r16
pop r17
// set bitcount to 0
ldi r16,0
sts rxBitcount,r16
// turn off interrupt, will get annoying.
cbi EXTI_MASK_REG,EXTI_MASK_BIT
// turn on interrupt on compare match
sbi TC_INTFLAG_REG,TC_RX_IF_BIT
lds r16,TC_INT_MASK_REG
ori r16,(1<<TC_RX_COMPEN)
sts TC_INT_MASK_REG,r16
ignore:
pop r16
sts SREG,r16
pop r16
reti
/*********************************************
* interrupt routine, timer compare match.
*
* TX bit rate timing
*/
.global TIMER3_COMPB_vect
TIMER3_COMPB_vect:
push r16
lds r16,SREG
push r16
push r17
push r18
// check if the last bit was sent
lds r17,txBitcount
inc r17
cpi r17,0x0A
sts txBitcount,r17
breq lastBitTX
lds r16,txShifter
#if (TXPORT>=32)
lds r17, TXPORT
sbrs r16,0
andi r17,~(1<<TXPIN)
sbrc r16,0
ori r17,(1<<TXPIN)
sts TXPORT,r17
#else
sbrs r16,0
cbi TXPORT,TXPIN
sbrc r16,0
sbi TXPORT,TXPIN
#endif
sec
ror r16
txout:
sts txShifter,r16
lastBitOut:
pop r18
pop r17
pop r16
sts SREG,r16
pop r16
reti
// section handles the last bit (stop bit sent/received and sets the flag to say done //
lastBitTX:
lds r17,status // get status
ori r17,SF_UART_TX // set TXC/DRE flag
sts status,r17
lds r16,TC_INT_MASK_REG
andi r16,~(1<<TC_TX_COMPEN)
sts TC_INT_MASK_REG,r16
rjmp lastBitOut // over and out
/*********************************************
* interrupt routine, timer compare match.
*
* RX bit rate timing
*/
.global TIMER3_COMPC_vect
TIMER3_COMPC_vect:
push r16
lds r16,SREG
push r16
push r17
push r18
// check if the last bit has been recieved
lds r17,rxBitcount
inc r17
cpi r17,0x0A
sts rxBitcount,r17
breq lastBitRX
cpi r17,0x01
breq rx1stbit
ldi r18,3 // set counter to 3
ldi r17,0
#ifdef DEBUG
#if RXPORT>64
lds r16,RXPORT
andi r16,~(1<<TXPIN)
sts TXPORT,r16
#else
cbi TXPORT,TXPIN // marker
#endif
#endif
loopGetBit:
#if (RXPORT>=32)
lds r16,RXPORT
sbrs r16,RXPIN
#else
sbic RXPORT,RXPIN
#endif
inc r17
dec r18
nop
nop
nop
nop
brne loopGetBit
#ifdef DEBUG
#if RXPORT>64
lds r16,RXPORT
ori r16,1<<TXPIN
sts r16
#else
sbi TXPORT,TXPIN // marker
#endif
#endif
lds r16,rxShifter
lsr r16
cpi r17,2
brlo skipBitSet
ori r16,0x80
skipBitSet:
sts rxShifter,r16
rjmp lastBitOut
lastBitRX:
lds r17,status // store status
#if (RXPORT>=32)
lds r16,RXPORT
sbrc r16,RXPIN
#else
sbic RXPORT,RXPIN
#endif
ori r17,0x02 // set flag if stop bit was high
sts status,r17
lds r16,rxShifter // get contents of shifter
sbrc r17,1 // check if we just received a valid byte
sts rxdata,r16 // if valid rxdata = shifter
// switch interrupt back on to get another go
sbi EXTI_FLAG_REG,EXTI_MASK_BIT // clear interrupt flag
sbi EXTI_MASK_REG,EXTI_MASK_BIT // enable external interrupt 0 (RX)
// switch off rx bit timer
lds r16,TC_INT_MASK_REG
andi r16,~(1<<TC_RX_COMPEN)
sts TC_INT_MASK_REG,r16
rjmp lastBitOut // loud and clear
rx1stbit:
lds r16,TC_COUNTL
lds r17,TC_COUNTH
subi r16,lo8(BITLENGTH / 2)
sbci r17,hi8(BITLENGTH / 2)
brcc skipOverflow
subi r16,lo8(0xFFFF - BITLENGTH)
sbci r17,hi8(0xFFFF - BITLENGTH)
skipOverflow:
sts TC_RX_COMPH,r17
sts TC_RX_COMPL,r17
rjmp lastBitOut
/*********************************************
* void SoftUART_Init(void)
*
* initialises software uart and enables transmit
*/
.global SoftUART_Init
SoftUART_Init:
#if (TXPORT>=32)
lds r18,TXPORT
ori r18,0x02
sts TXPORT,r18
lds r18,TXDIR_REG
ori r18,0x02
sts TXDIR_REG,r18
#else
sbi TXPORT,TXPIN
sbi TXDIR_REG,TXPIN
#endif
ldi r18,(1<<SFT_TX_EN)|SF_UART_TX
sts status,r18
ldi r18,lo8(BITLENGTH)
ldi r19,hi8(BITLENGTH)
sts OCR3AH,r19
sts OCR3AL,r18
// Start timer 3
ldi r18,0b00001001 // ctc count mode, clock div 1
sts TC_CTRLB,r18
// Interrupt on pin change INT0
sbi EXTI_FLAG_REG,EXTI_MASK_BIT
sbi EXTI_MASK_REG,EXTI_MASK_BIT
ret
/*********************************************
* char SoftUART_RxByte(char)
*
* starts a byte send and returns the byte to be sent
*/
.global SoftUART_TxByte
SoftUART_TxByte:
lds r18,status
sbrs r18,SFT_TX_EN
rjmp SoftUART_TxByte_end
andi r18,0xFE // clear tx empty flag
sts status,r18
sts txShifter,r24
ldi r18,0
sts txBitcount,r18
// grab timer value
cli // Atomic section start
lds r18,TC_COUNTL
lds r19,TC_COUNTH
// drop down tx line for start bit
#if (TXPORT>=32)
lds r20, TXPORT
andi r20,~(1<<TXPIN)
sts TXPORT,r20
#else
cbi TXPORT,TXPIN
#endif
// set trigger for tx timer
sts TC_TX_COMPH,r19
sts TC_TX_COMPL,r18
sei // Atomic section end
// clear interrupt flag and enable tx interrupt
sbi TC_INTFLAG_REG,TC_TX_IF_BIT
lds r18,TC_INT_MASK_REG
ori r18,(1<<TC_TX_COMPEN)
sts TC_INT_MASK_REG,r18
SoftUART_TxByte_end:
ret
/*********************************************
* char SoftUART_RxByte(void)
*
* returns the received byte
*/
.global SoftUART_RxByte
SoftUART_RxByte:
lds r24,rxdata
lds r18,status
andi r18,0xFD
sts status,r18
ret
/*********************************************
* char SoftUART_IsReceived(void)
*
* checks if there is a byte in the receive buffer
*/
.global SoftUART_IsReceived
SoftUART_IsReceived:
lds r24,status
andi r24,SF_UART_RX
lsr r24
ret
/*********************************************
* char SoftUART_IsReady(void)
*
* Simulates polling UDRE to see if tx buffer is empty and ready
*
* returns 1 if empty 0 if not
*/
.global SoftUART_IsReady
SoftUART_IsReady:
lds r24,status
andi r24,SF_UART_TX
ret
|