7 copyright John Steggall 2009
13 Copyright 2009 John Steggall (steggall.j@gmail.com)
15 Permission to use, copy, modify, and distribute this software
16 and its documentation for any purpose and without fee is hereby
17 granted, provided that the above copyright notice appear in all
18 copies and that both that the copyright notice and this
19 permission notice and warranty disclaimer appear in supporting
20 documentation, and that the name of the author not be used in
21 advertising or publicity pertaining to distribution of the
22 software without specific, written prior permission.
24 The author disclaim all warranties with regard to this
25 software, including all implied warranties of merchantability
26 and fitness. In no event shall the author be liable for any
27 special, indirect or consequential damages or any damages
28 whatsoever resulting from loss of use, data or profits, whether
29 in an action of contract, negligence or other tortious action,
30 arising out of or in connection with the use or performance of
35 #include "SoftUARTConf.h"
65 /*********************************************
68 * RX pin has gone low.
77 sbrc r16,0 // anti glitch
91 sbrc r16,0 // anti glitch
105 // set trigger for RX timer (will need to add a little more though)
116 // turn off interrupt, will get annoying.
117 cbi EXTI_MASK_REG,EXTI_MASK_BIT
119 // turn on interrupt on compare match
121 sbi TC_INTFLAG_REG,TC_RX_IF_BIT
123 lds r16,TC_INT_MASK_REG
124 ori r16,(1<<TC_RX_COMPEN)
125 sts TC_INT_MASK_REG,r16
134 /*********************************************
135 * interrupt routine, timer compare match.
139 .global TIMER3_COMPB_vect
148 // check if the last bit was sent
185 // section handles the last bit (stop bit sent/received and sets the flag to say done //
187 lds r17,status // get status
188 ori r17,SF_UART_TX // set TXC/DRE flag
191 lds r16,TC_INT_MASK_REG
192 andi r16,~(1<<TC_TX_COMPEN)
193 sts TC_INT_MASK_REG,r16
195 rjmp lastBitOut // over and out
199 /*********************************************
200 * interrupt routine, timer compare match.
204 .global TIMER3_COMPC_vect
213 // check if the last bit has been recieved
223 ldi r18,3 // set counter to 3
233 cbi TXPORT,TXPIN // marker
263 sbi TXPORT,TXPIN // marker
280 lds r17,status // store status
291 ori r17,0x02 // set flag if stop bit was high
294 lds r16,rxShifter // get contents of shifter
295 sbrc r17,1 // check if we just received a valid byte
296 sts rxdata,r16 // if valid rxdata = shifter
298 // switch interrupt back on to get another go
300 sbi EXTI_FLAG_REG,EXTI_MASK_BIT // clear interrupt flag
301 sbi EXTI_MASK_REG,EXTI_MASK_BIT // enable external interrupt 0 (RX)
303 // switch off rx bit timer
304 lds r16,TC_INT_MASK_REG
305 andi r16,~(1<<TC_RX_COMPEN)
306 sts TC_INT_MASK_REG,r16
308 rjmp lastBitOut // loud and clear
314 subi r16,lo8(BITLENGTH / 2)
315 sbci r17,hi8(BITLENGTH / 2)
318 subi r16,lo8(0xFFFF - BITLENGTH)
319 sbci r17,hi8(0xFFFF - BITLENGTH)
327 /*********************************************
328 * void SoftUART_Init(void)
330 * initialises software uart and enables transmit
332 .global SoftUART_Init
351 ldi r18,(1<<SFT_TX_EN)|SF_UART_TX
354 ldi r18,lo8(BITLENGTH)
355 ldi r19,hi8(BITLENGTH)
360 ldi r18,0b00001001 // ctc count mode, clock div 1
363 // Interrupt on pin change INT0
364 sbi EXTI_FLAG_REG,EXTI_MASK_BIT
365 sbi EXTI_MASK_REG,EXTI_MASK_BIT
370 /*********************************************
371 * char SoftUART_RxByte(char)
373 * starts a byte send and returns the byte to be sent
375 .global SoftUART_TxByte
380 rjmp SoftUART_TxByte_end
382 andi r18,0xFE // clear tx empty flag
391 cli // Atomic section start
395 // drop down tx line for start bit
406 // set trigger for tx timer
409 sei // Atomic section end
411 // clear interrupt flag and enable tx interrupt
413 sbi TC_INTFLAG_REG,TC_TX_IF_BIT
415 lds r18,TC_INT_MASK_REG
416 ori r18,(1<<TC_TX_COMPEN)
417 sts TC_INT_MASK_REG,r18
423 /*********************************************
424 * char SoftUART_RxByte(void)
426 * returns the received byte
428 .global SoftUART_RxByte
438 /*********************************************
439 * char SoftUART_IsReceived(void)
441 * checks if there is a byte in the receive buffer
443 .global SoftUART_IsReceived
452 /*********************************************
453 * char SoftUART_IsReady(void)
455 * Simulates polling UDRE to see if tx buffer is empty and ready
457 * returns 1 if empty 0 if not
459 .global SoftUART_IsReady