aboutsummaryrefslogtreecommitdiffstats
path: root/Projects/XPLAINBridge/Lib
diff options
context:
space:
mode:
authorDean Camera <dean@fourwalledcubicle.com>2009-11-25 00:36:18 +0000
committerDean Camera <dean@fourwalledcubicle.com>2009-11-25 00:36:18 +0000
commitc05c7c7df46a0377db8a72cb32f06aa40153d3e1 (patch)
tree0e51bc1ef2a4e32ce29f448299a62ca16c46c3a8 /Projects/XPLAINBridge/Lib
parentd3f11eb52809353d7f103cea8b4332f335c26697 (diff)
downloadlufa-c05c7c7df46a0377db8a72cb32f06aa40153d3e1.tar.gz
lufa-c05c7c7df46a0377db8a72cb32f06aa40153d3e1.tar.bz2
lufa-c05c7c7df46a0377db8a72cb32f06aa40153d3e1.zip
Added new XPLAIN serial Bridge project (thanks to John Steggall for the software UART code).
Diffstat (limited to 'Projects/XPLAINBridge/Lib')
-rw-r--r--Projects/XPLAINBridge/Lib/RingBuff.c120
-rw-r--r--Projects/XPLAINBridge/Lib/RingBuff.h116
-rw-r--r--Projects/XPLAINBridge/Lib/SoftUART.S389
-rw-r--r--Projects/XPLAINBridge/Lib/SoftUART.h59
4 files changed, 684 insertions, 0 deletions
diff --git a/Projects/XPLAINBridge/Lib/RingBuff.c b/Projects/XPLAINBridge/Lib/RingBuff.c
new file mode 100644
index 000000000..0dd4a06bb
--- /dev/null
+++ b/Projects/XPLAINBridge/Lib/RingBuff.c
@@ -0,0 +1,120 @@
+/*
+ LUFA Library
+ Copyright (C) Dean Camera, 2009.
+
+ dean [at] fourwalledcubicle [dot] com
+ www.fourwalledcubicle.com
+*/
+
+/*
+ Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] 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 "RingBuff.h"
+
+void Buffer_Initialize(RingBuff_t* Buffer)
+{
+ BUFF_ATOMIC_BLOCK
+ {
+ Buffer->InPtr = (RingBuff_Data_t*)&Buffer->Buffer;
+ Buffer->OutPtr = (RingBuff_Data_t*)&Buffer->Buffer;
+ Buffer->Elements = 0;
+ }
+}
+
+void Buffer_StoreElement(RingBuff_t* Buffer, RingBuff_Data_t Data)
+{
+ BUFF_ATOMIC_BLOCK
+ {
+ #if defined(BUFF_DROPOLD)
+ if (Buffer->Elements == BUFF_LENGTH)
+ {
+ Buffer->OutPtr++;
+
+ if (Buffer->OutPtr == &Buffer->Buffer[BUFF_LENGTH])
+ Buffer->OutPtr = (RingBuff_Data_t*)&Buffer->Buffer;
+ }
+ else
+ {
+ Buffer->Elements++;
+ }
+ #elif defined(BUFF_DROPNEW)
+ if (Buffer->Elements == BUFF_LENGTH)
+ return;
+
+ Buffer->Elements++;
+ #elif defined(BUFF_NODROPCHECK)
+ Buffer->Elements++;
+ #endif
+
+ *(Buffer->InPtr) = Data;
+ Buffer->InPtr++;
+
+ if (Buffer->InPtr == &Buffer->Buffer[BUFF_LENGTH])
+ Buffer->InPtr = (RingBuff_Data_t*)&Buffer->Buffer;
+ }
+}
+
+RingBuff_Data_t Buffer_GetElement(RingBuff_t* Buffer)
+{
+ RingBuff_Data_t BuffData;
+
+ BUFF_ATOMIC_BLOCK
+ {
+#if defined(BUFF_EMPTYRETURNSZERO)
+ if (!(Buffer->Elements))
+ return 0;
+#elif !defined(BUFF_NOEMPTYCHECK)
+ #error No empty buffer check behavior specified.
+#endif
+
+ BuffData = *(Buffer->OutPtr);
+
+ Buffer->OutPtr++;
+ Buffer->Elements--;
+
+ if (Buffer->OutPtr == &Buffer->Buffer[BUFF_LENGTH])
+ Buffer->OutPtr = (RingBuff_Data_t*)&Buffer->Buffer;
+ }
+
+ return BuffData;
+}
+
+#if defined(BUFF_USEPEEK)
+RingBuff_Data_t Buffer_PeekElement(const RingBuff_t* Buffer)
+{
+ RingBuff_Data_t BuffData;
+
+ BUFF_ATOMIC_BLOCK
+ {
+#if defined(BUFF_EMPTYRETURNSZERO)
+ if (!(Buffer->Elements))
+ return 0;
+#elif !defined(BUFF_NOEMPTYCHECK)
+ #error No empty buffer check behavior specified.
+#endif
+
+ BuffData = *(Buffer->OutPtr);
+ }
+
+ return BuffData;
+}
+#endif
diff --git a/Projects/XPLAINBridge/Lib/RingBuff.h b/Projects/XPLAINBridge/Lib/RingBuff.h
new file mode 100644
index 000000000..23288d87a
--- /dev/null
+++ b/Projects/XPLAINBridge/Lib/RingBuff.h
@@ -0,0 +1,116 @@
+/*
+ LUFA Library
+ Copyright (C) Dean Camera, 2009.
+
+ dean [at] fourwalledcubicle [dot] com
+ www.fourwalledcubicle.com
+*/
+
+/*
+ Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] 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.
+*/
+
+/* Buffer Configuration: */
+ /* Buffer length - select static size of created ring buffers: */
+ #define BUFF_STATICSIZE 128 // Set to the static ring buffer size for all ring buffers (place size after define)
+
+ /* Volatile mode - uncomment to make buffers volatile, for use in ISRs, etc: */
+ #define BUFF_VOLATILE // Uncomment to cause all ring buffers to become volatile (and atomic if multi-byte) in access
+
+ /* Drop mode - select behaviour when Buffer_StoreElement called on a full buffer: */
+ #define BUFF_DROPOLD // Uncomment to cause full ring buffers to drop the oldest character to make space when full
+ // #define BUFF_DROPNEW // Uncomment to cause full ring buffers to drop the new character when full
+ // #define BUFF_NODROPCHECK // Uncomment to ignore full ring buffer checks - checking left to user!
+
+ /* Underflow behaviour - select behaviour when Buffer_GetElement is called with an empty ring buffer: */
+ //#define BUFF_EMPTYRETURNSZERO // Uncomment to return 0 when an empty ring buffer is read
+ #define BUFF_NOEMPTYCHECK // Uncomment to disable checking of empty ring buffers - checking left to user!
+
+ /* Buffer storage type - set the datatype for the stored data */
+ #define BUFF_DATATYPE uint8_t // Change to the data type that is going to be stored into the buffer
+
+ /* Peek routine - uncomment to include the peek routine (fetches next byte without removing it from the buffer */
+ //#define BUFF_USEPEEK
+
+#ifndef _RINGBUFF_H_
+#define _RINGBUFF_H_
+
+ /* Includes: */
+ #include <avr/io.h>
+ #include <avr/interrupt.h>
+ #include <util/atomic.h>
+ #include <limits.h>
+
+ #include <LUFA/Common/Common.h>
+
+ /* Defines and checks: */
+ #if defined(BUFF_STATICSIZE)
+ #define BUFF_LENGTH BUFF_STATICSIZE
+ #else
+ #error No buffer length specified!
+ #endif
+
+ #if !(defined(BUFF_DROPOLD) || defined(BUFF_DROPNEW) || defined(BUFF_NODROPCHECK))
+ #error No buffer drop mode specified.
+ #endif
+
+ #if !defined(BUFF_DATATYPE)
+ #error Ringbuffer storage data type not specified.
+ #endif
+
+ #if defined(BUFF_VOLATILE)
+ #define BUFF_MODE volatile
+ #define BUFF_ATOMIC_BLOCK ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
+ #else
+ #define BUFF_MODE
+ #define BUFF_ATOMIC_BLOCK
+ #endif
+
+ #if (BUFF_STATICSIZE > LONG_MAX)
+ #define RingBuff_Elements_t uint64_t
+ #elif (BUFF_STATICSIZE > INT_MAX)
+ #define RingBuff_Elements_t uint32_t
+ #elif (BUFF_STATICSIZE > CHAR_MAX)
+ #define RingBuff_Elements_t uint16_t
+ #else
+ #define RingBuff_Elements_t uint8_t
+ #endif
+
+ /* Type Defines: */
+ typedef BUFF_DATATYPE RingBuff_Data_t;
+
+ typedef BUFF_MODE struct
+ {
+ RingBuff_Data_t Buffer[BUFF_LENGTH];
+ RingBuff_Data_t* InPtr;
+ RingBuff_Data_t* OutPtr;
+ RingBuff_Elements_t Elements;
+ } RingBuff_t;
+
+ /* Function Prototypes: */
+ void Buffer_Initialize(RingBuff_t* Buff);
+ void Buffer_StoreElement(RingBuff_t* Buffer, RingBuff_Data_t Data);
+ RingBuff_Data_t Buffer_GetElement(RingBuff_t* Buffer);
+ #if defined(BUFF_USEPEEK)
+ RingBuff_Data_t Buffer_PeekElement(const RingBuff_t* Buffer);
+ #endif
+
+#endif
diff --git a/Projects/XPLAINBridge/Lib/SoftUART.S b/Projects/XPLAINBridge/Lib/SoftUART.S
new file mode 100644
index 000000000..8951387e9
--- /dev/null
+++ b/Projects/XPLAINBridge/Lib/SoftUART.S
@@ -0,0 +1,389 @@
+/*
+
+ uart_soft
+
+ 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>
+
+/* BITLENGTH is the time for a bit cycle worked out at F_CPU / BAUD. Gives a rough but usable figure. Wouldn't like to try
+ * anything faster than 9600! */
+#define BITLENGTH 833
+
+#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
+status:
+ .byte 0
+
+ .section .text
+
+/*********************************************
+ * External interrupt
+ *
+ * RX pin has gone low.
+ */
+ .global INT0_vect
+
+INT0_vect:
+ push r16
+ lds r16,SREG
+ push r16
+
+ lds r16,PIND
+ sbrc r16,0 // anti glitch
+ rjmp ignore
+ nop
+ nop
+ nop
+ nop
+ lds r16,PIND
+ sbrc r16,0 // just make sure
+ rjmp ignore
+
+ push r17
+
+ // grab timer value
+ lds r16,TCNT3L
+ lds r17,TCNT3H
+
+ // set trigger for RX timer (will need to add a little more though)
+ sts OCR3CH,r17
+ sts OCR3CL,r16
+
+ pop r17
+
+ // set bitcount to 0
+ ldi r16,0
+ sts rxBitcount,r16
+
+
+ // turn off interrupt, will get annoying.
+ cbi 0x1D,0
+
+ // turn on interrupt on compare match
+ sbi 0x18,OCF3C
+ lds r16,TIMSK3
+ ori r16,(1<<OCIE3C)
+ sts TIMSK3,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
+
+ lds r17, PORTD
+
+ sbrs r16,0
+ andi r17,0xFD
+ sbrc r16,0
+ ori r17,0x02
+
+ sts PORTD,r17
+
+ lsr r16
+ ori r16,0x80
+
+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,TIMSK3
+ andi r16,~(1<<OCIE3B)
+ sts TIMSK3,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
+
+// cbi 0x0B,1 // marker
+
+loopGetBit:
+ lds r16,PIND
+ sbrc r16,0
+ inc r17
+ dec r18
+ nop
+ nop
+ nop
+ nop
+ brne loopGetBit
+
+// sbi 0x0B,1 // marker
+
+ 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
+ lds r16,PIND // get status of stop bit
+ sbrc r16,0
+ 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 0x1C,0 // clear interrupt flag
+ sbi 0x1D,0 // enable external interrupt 0 (RX)
+
+ // switch off rx bit timer
+ lds r16,TIMSK3
+ andi r16,~(1<<OCIE3C)
+ sts TIMSK3,r16
+
+ rjmp lastBitOut // loud and clear
+
+rx1stbit:
+ lds r16,TCNT3L
+ lds r17,TCNT3H
+
+ subi r16,lo8(BITLENGTH / 2)
+ sbci r17,hi8(BITLENGTH / 2)
+ brcc skipOverflow
+
+ subi r16,lo8(0xFFFF - BITLENGTH)
+ sbci r17,hi8(0xFFFF - BITLENGTH)
+
+skipOverflow:
+ sts OCR3CH,r17
+ sts OCR3CL,r17
+ rjmp lastBitOut
+
+
+/*********************************************
+ * void SoftUART_Init(void)
+ *
+ * initialises software uart and enables transmit
+ */
+ .global SoftUART_Init
+
+SoftUART_Init:
+
+ lds r18,PORTD
+ ori r18,0x02
+ sts PORTD,r18
+ lds r18,DDRD
+ ori r18,0x02
+ sts DDRD,r18
+
+ 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 TCCR3B,r18
+
+ // Interrupt on low level INT0
+ sbi 0x1C,0
+ sbi 0x1D,0
+
+ ret
+
+
+/*********************************************
+ * char SoftUART_TxByte(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 uart_putchar_end
+
+ andi r18,0xFE // clear tx empty flag
+ sts status,r18
+
+ sts txShifter,r24
+
+ ldi r18,0
+ sts txBitcount,r18
+
+ // grab timer value
+
+ lds r18,TCNT3L
+ lds r19,TCNT3H
+
+ // drop down tx line for start bit
+ lds r20, PORTD
+ andi r20,0xFD
+ sts PORTD,r20
+
+ // set trigger for tx timer
+ cli
+ sts OCR3BH,r19
+ sts OCR3BL,r18
+ sei
+
+ // clear interrupt flag and enable tx interrupt
+ sbi 0x18,OCF3B
+ lds r18,TIMSK3
+ ori r18,(1<<OCIE3B)
+ sts TIMSK3,r18
+
+uart_putchar_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
diff --git a/Projects/XPLAINBridge/Lib/SoftUART.h b/Projects/XPLAINBridge/Lib/SoftUART.h
new file mode 100644
index 000000000..d992966c0
--- /dev/null
+++ b/Projects/XPLAINBridge/Lib/SoftUART.h
@@ -0,0 +1,59 @@
+/*
+
+ soft_uart
+
+ 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.
+*/
+
+/*
+ Specifically designed for the xplain board, other uses could be made by furkling through the code
+ and replacing the port and pin assignments. Also relies on and external interupt to detect the low
+ level of the start bit, in this case INT0.
+ Always configured to 9600baud. Can be changed by setting the BITLENGTH define to F_CPU/(desired baud)
+ code may need optimising when getting any faster to sample the bit in the correct place. No
+ compensation has been made for the response time of the int routine.
+ */
+
+#ifndef UART_SOFT_H
+#define UART_SOFT_H
+
+ /* initialises software uart and enables transmit */
+ extern void SoftUART_Init(void);
+
+ /* checks if there is a byte in the receive buffer*/
+ extern unsigned char SoftUART_IsReceived(void);
+
+ /* returns the received byte */
+ extern unsigned char SoftUART_RxByte(void);
+
+ /* starts a byte send and returns the byte to be sent */
+ extern unsigned char SoftUART_TxByte(unsigned char data);
+
+ /* Simulates polling UDRE to see if tx buffer is empty and ready */
+ extern unsigned char SoftUART_IsReady(void);
+
+#endif