The RingBuff library code has been replaced in the XPLAINBridge project with an ultra lightweight buffer to help improve the reliability of the bridge.

pull/1469/head
Dean Camera 15 years ago
parent 4a13a5484a
commit 9c037a952f

File diff suppressed because one or more lines are too long

@ -11,7 +11,8 @@
* - Added new ADC_DisableChannel() function (thanks to Mich Davis) * - Added new ADC_DisableChannel() function (thanks to Mich Davis)
* *
* <b>Changed:</b> * <b>Changed:</b>
* - N/A * - The RingBuff library code has been replaced in the XPLAINBridge project with an ultra lightweight buffer to help
* improve the reliability of the bridge
* *
* <b>Fixed:</b> * <b>Fixed:</b>
* - Fixed AVRISP project sending a LOAD EXTENDED ADDRESS command to 128KB AVRs after programming or reading from * - Fixed AVRISP project sending a LOAD EXTENDED ADDRESS command to 128KB AVRs after programming or reading from

@ -144,7 +144,7 @@
* <td>4</td> * <td>4</td>
* </tr> * </tr>
* <tr> * <tr>
* <td>XCLK</td> * <td>XCK</td>
* <td>CLOCK</td> * <td>CLOCK</td>
* <td>5</td> * <td>5</td>
* </tr> * </tr>
@ -178,7 +178,7 @@
* <td>2</td> * <td>2</td>
* </tr> * </tr>
* <tr> * <tr>
* <td>XCLK <b><sup>2</sup></b></td> * <td>XCK <b><sup>2</sup></b></td>
* <td>CLOCK</td> * <td>CLOCK</td>
* <td>3</td> * <td>3</td>
* </tr> * </tr>

@ -1,120 +0,0 @@
/*
LUFA Library
Copyright (C) Dean Camera, 2010.
dean [at] fourwalledcubicle [dot] com
www.fourwalledcubicle.com
*/
/*
Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com)
Permission to use, copy, modify, distribute, and sell this
software and its documentation for any purpose is hereby granted
without fee, 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* const 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* const 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* const 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* const 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

@ -1,116 +0,0 @@
/*
LUFA Library
Copyright (C) Dean Camera, 2010.
dean [at] fourwalledcubicle [dot] com
www.fourwalledcubicle.com
*/
/*
Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com)
Permission to use, copy, modify, distribute, and sell this
software and its documentation for any purpose is hereby granted
without fee, 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* const Buff);
void Buffer_StoreElement(RingBuff_t* const Buffer, RingBuff_Data_t Data);
RingBuff_Data_t Buffer_GetElement(RingBuff_t* const Buffer);
#if defined(BUFF_USEPEEK)
RingBuff_Data_t Buffer_PeekElement(const RingBuff_t* const Buffer);
#endif
#endif

@ -32,34 +32,22 @@
#include "SoftUART.h" #include "SoftUART.h"
volatile uint8_t srx_done, stx_count; volatile bool srx_done;
volatile uint8_t srx_data, srx_mask, srx_tmp, stx_data; volatile uint8_t stx_count;
static uint8_t srx_data, srx_mask, srx_tmp, stx_data;
uint8_t SoftUART_IsReady(void) void SoftUART_TxByte(uint8_t Byte)
{
return !(stx_count);
}
uint8_t SoftUART_TxByte(uint8_t Byte)
{ {
while (stx_count); while (stx_count);
stx_data = ~Byte; stx_data = ~Byte;
stx_count = 10; stx_count = 10;
return Byte;
}
uint8_t SoftUART_IsReceived(void)
{
return srx_done;
} }
uint8_t SoftUART_RxByte(void) uint8_t SoftUART_RxByte(void)
{ {
while (!(srx_done)); while (!(srx_done));
srx_done = false;
srx_done = 0;
return srx_data; return srx_data;
} }
@ -74,9 +62,9 @@ void SoftUART_Init(void)
EIMSK = (1 << INT0); // enable INT0 interrupt EIMSK = (1 << INT0); // enable INT0 interrupt
stx_count = 0; // nothing to send stx_count = 0; // nothing to send
srx_done = 0; // nothing received srx_done = false; // nothing received
STXPORT |= 1 << STX; // TX output STXPORT |= (1 << STX); // TX output
STXDDR |= 1 << STX; // TX output STXDDR |= (1 << STX); // TX output
SRXPORT |= (1 << SRX); // pullup on INT0 SRXPORT |= (1 << SRX); // pullup on INT0
} }
@ -85,7 +73,7 @@ ISR(INT0_vect)
{ {
OCR2A = TCNT2 + (BIT_TIME / 8 * 3 / 2); // scan 1.5 bits after start OCR2A = TCNT2 + (BIT_TIME / 8 * 3 / 2); // scan 1.5 bits after start
srx_tmp = 0; // clear bit storage srx_tmp = 0; // clear bit storage
srx_mask = 1; // bit mask srx_mask = 1; // bit mask
TIFR2 = (1 << OCF2A); // clear pending interrupt TIFR2 = (1 << OCF2A); // clear pending interrupt
@ -111,7 +99,7 @@ ISR(TIMER2_COMPA_vect)
} }
else else
{ {
srx_done = 1; // mark rx data valid srx_done = true; // mark rx data valid
srx_data = srx_tmp; // store rx data srx_data = srx_tmp; // store rx data
TIMSK2 = (1 << OCIE2B); // enable tx and wait for start TIMSK2 = (1 << OCIE2B); // enable tx and wait for start
EIMSK |= (1 << INT0); // enable START irq EIMSK |= (1 << INT0); // enable START irq

@ -36,7 +36,7 @@
/* Includes: */ /* Includes: */
#include <avr/io.h> #include <avr/io.h>
#include <avr/interrupt.h> #include <avr/interrupt.h>
#include "SoftUART.h" #include <stdbool.h>
/* Macros: */ /* Macros: */
#define BAUD 9600 #define BAUD 9600
@ -50,10 +50,23 @@
#define STXPORT PORTD #define STXPORT PORTD
#define STXDDR DDRD #define STXDDR DDRD
/* External Variables: */
extern volatile bool srx_done;
extern volatile uint8_t stx_count;
/* Inline Functions: */
static inline bool SoftUART_IsReady(void)
{
return !(stx_count);
}
static inline bool SoftUART_IsReceived(void)
{
return srx_done;
}
/* Function Prototypes: */ /* Function Prototypes: */
uint8_t SoftUART_IsReady(void); void SoftUART_TxByte(uint8_t c);
uint8_t SoftUART_TxByte(uint8_t c);
uint8_t SoftUART_IsReceived(void);
uint8_t SoftUART_RxByte(void); uint8_t SoftUART_RxByte(void);
void SoftUART_Init(void); void SoftUART_Init(void);

@ -77,8 +77,10 @@ int main(void)
{ {
SetupHardware(); SetupHardware();
Buffer_Initialize(&USBtoUART_Buffer); USBtoUART_Buffer.In = USBtoUART_Buffer.Buffer;
Buffer_Initialize(&UARTtoUSB_Buffer); USBtoUART_Buffer.Out = USBtoUART_Buffer.Buffer;
UARTtoUSB_Buffer.In = UARTtoUSB_Buffer.Buffer;
UARTtoUSB_Buffer.Out = UARTtoUSB_Buffer.Buffer;
LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY); LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
sei(); sei();
@ -123,23 +125,35 @@ void USARTBridge_Task(void)
/* Read bytes from the USB OUT endpoint into the UART transmit buffer */ /* Read bytes from the USB OUT endpoint into the UART transmit buffer */
for (uint8_t DataBytesRem = CDC_Device_BytesReceived(&VirtualSerial_CDC_Interface); DataBytesRem != 0; DataBytesRem--) for (uint8_t DataBytesRem = CDC_Device_BytesReceived(&VirtualSerial_CDC_Interface); DataBytesRem != 0; DataBytesRem--)
{ {
if (!(BUFF_STATICSIZE - USBtoUART_Buffer.Elements)) *USBtoUART_Buffer.In = CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface);
break;
Buffer_StoreElement(&USBtoUART_Buffer, CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface)); if (++USBtoUART_Buffer.In == &USBtoUART_Buffer.Buffer[128])
USBtoUART_Buffer.In = USBtoUART_Buffer.Buffer;
} }
/* Read bytes from the UART receive buffer into the USB IN endpoint */ /* Read bytes from the UART receive buffer into the USB IN endpoint */
if (UARTtoUSB_Buffer.Elements) if (UARTtoUSB_Buffer.In != UARTtoUSB_Buffer.Out)
CDC_Device_SendByte(&VirtualSerial_CDC_Interface, Buffer_GetElement(&UARTtoUSB_Buffer)); {
CDC_Device_SendByte(&VirtualSerial_CDC_Interface, *UARTtoUSB_Buffer.Out);
if (++UARTtoUSB_Buffer.Out == &UARTtoUSB_Buffer.Buffer[128])
UARTtoUSB_Buffer.Out = UARTtoUSB_Buffer.Buffer;
}
/* Load bytes from the UART transmit buffer into the UART */ /* Load bytes from the UART transmit buffer into the UART */
if ((USBtoUART_Buffer.Elements) && SoftUART_IsReady()) if ((USBtoUART_Buffer.In != USBtoUART_Buffer.Out) && SoftUART_IsReady())
SoftUART_TxByte(Buffer_GetElement(&USBtoUART_Buffer)); {
SoftUART_TxByte(*USBtoUART_Buffer.Out);
if (++USBtoUART_Buffer.Out == &USBtoUART_Buffer.Buffer[128])
USBtoUART_Buffer.Out = USBtoUART_Buffer.Buffer;
}
/* Load bytes from the UART into the UART receive buffer */ /* Load bytes from the UART into the UART receive buffer */
if (SoftUART_IsReceived()) if (SoftUART_IsReceived())
Buffer_StoreElement(&UARTtoUSB_Buffer, SoftUART_RxByte()); {
*UARTtoUSB_Buffer.In = SoftUART_RxByte();
if (++UARTtoUSB_Buffer.In == &UARTtoUSB_Buffer.Buffer[128])
UARTtoUSB_Buffer.In = UARTtoUSB_Buffer.Buffer;
}
CDC_Device_USBTask(&VirtualSerial_CDC_Interface); CDC_Device_USBTask(&VirtualSerial_CDC_Interface);
} }
@ -169,7 +183,7 @@ void SetupHardware(void)
_delay_ms(10); _delay_ms(10);
/* Select the firmware mode based on the JTD pin's value */ /* Select the firmware mode based on the JTD pin's value */
CurrentFirmwareMode = (PINF & (1 << 7)) ? MODE_USART_BRIDGE : MODE_PDI_PROGRAMMER; CurrentFirmwareMode = MODE_USART_BRIDGE; // TEMP (PINF & (1 << 7)) ? MODE_USART_BRIDGE : MODE_PDI_PROGRAMMER;
/* Re-enable JTAG debugging */ /* Re-enable JTAG debugging */
MCUCR &= ~(1 << JTD); MCUCR &= ~(1 << JTD);

@ -45,7 +45,6 @@
#include "AVRISPDescriptors.h" #include "AVRISPDescriptors.h"
#include "USARTDescriptors.h" #include "USARTDescriptors.h"
#include "Lib/RingBuff.h"
#include "Lib/SoftUART.h" #include "Lib/SoftUART.h"
#include <Lib/V2Protocol.h> #include <Lib/V2Protocol.h>
@ -76,6 +75,14 @@
/** Firmware mode define for the AVRISP Programmer mode. */ /** Firmware mode define for the AVRISP Programmer mode. */
#define MODE_PDI_PROGRAMMER true #define MODE_PDI_PROGRAMMER true
/* Type Defines: */
typedef struct
{
uint8_t Buffer[128];
uint8_t* In;
uint8_t* Out;
} RingBuff_t;
/* External Variables: */ /* External Variables: */
extern bool CurrentFirmwareMode; extern bool CurrentFirmwareMode;

@ -129,7 +129,6 @@ LUFA_OPTS += -D USE_STATIC_OPTIONS="(USB_DEVICE_OPT_FULLSPEED | USB_OPT_REG_ENAB
SRC = $(TARGET).c \ SRC = $(TARGET).c \
AVRISPDescriptors.c \ AVRISPDescriptors.c \
USARTDescriptors.c \ USARTDescriptors.c \
Lib/RingBuff.c \
Lib/SoftUART.c \ Lib/SoftUART.c \
$(AVRISP_PATH)/Lib/V2Protocol.c \ $(AVRISP_PATH)/Lib/V2Protocol.c \
$(AVRISP_PATH)/Lib/V2ProtocolParams.c \ $(AVRISP_PATH)/Lib/V2ProtocolParams.c \

Loading…
Cancel
Save