mirror of
https://github.com/qmk/qmk_firmware.git
synced 2025-06-06 16:32:49 +00:00
Replace ring buffer library in Benito and USBtoSerial projects with the new lightweight ring buffer header to improve reliability.
This commit is contained in:
parent
885170f5b4
commit
8d993afc53
File diff suppressed because one or more lines are too long
@ -11,8 +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>
|
||||||
* - The RingBuff library code has been replaced in the XPLAINBridge project with an ultra lightweight buffer to help
|
* - The RingBuff library code has been replaced in the XPLAINBridge, Benito and USBtoSerial projects with an ultra lightweight
|
||||||
* improve the reliability of the bridge
|
* ring buffer to help improve the reliability of the projects
|
||||||
* - The EEPROM stream read/write functions now use eeprom_update_byte() instead of eeprom_write_byte(), so that only
|
* - The EEPROM stream read/write functions now use eeprom_update_byte() instead of eeprom_write_byte(), so that only
|
||||||
* changed bytes are written to EEPROM to preserve its lifespan
|
* changed bytes are written to EEPROM to preserve its lifespan
|
||||||
* - Changed over the AVRISP-MKII and TemperatureDataLogger projects to use eeprom_update_byte() when writing non-volatile
|
* - Changed over the AVRISP-MKII and TemperatureDataLogger projects to use eeprom_update_byte() when writing non-volatile
|
||||||
|
@ -82,7 +82,7 @@ int main(void)
|
|||||||
{
|
{
|
||||||
SetupHardware();
|
SetupHardware();
|
||||||
|
|
||||||
Buffer_Initialize(&Tx_Buffer);
|
RingBuffer_InitBuffer(&Tx_Buffer);
|
||||||
|
|
||||||
sei();
|
sei();
|
||||||
|
|
||||||
@ -98,9 +98,9 @@ int main(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Echo bytes from the target to the host via the virtual serial port */
|
/* Echo bytes from the target to the host via the virtual serial port */
|
||||||
while (Tx_Buffer.Elements > 0)
|
while (Tx_Buffer.Count)
|
||||||
{
|
{
|
||||||
CDC_Device_SendByte(&VirtualSerial_CDC_Interface, Buffer_GetElement(&Tx_Buffer));
|
CDC_Device_SendByte(&VirtualSerial_CDC_Interface, RingBuffer_Remove(&Tx_Buffer));
|
||||||
|
|
||||||
LEDs_TurnOnLEDs(LEDMASK_RX);
|
LEDs_TurnOnLEDs(LEDMASK_RX);
|
||||||
PulseMSRemaining.RxLEDPulse = TX_RX_LED_PULSE_MS;
|
PulseMSRemaining.RxLEDPulse = TX_RX_LED_PULSE_MS;
|
||||||
@ -240,7 +240,7 @@ ISR(USART1_RX_vect, ISR_BLOCK)
|
|||||||
uint8_t ReceivedByte = UDR1;
|
uint8_t ReceivedByte = UDR1;
|
||||||
|
|
||||||
if (USB_DeviceState == DEVICE_STATE_Configured)
|
if (USB_DeviceState == DEVICE_STATE_Configured)
|
||||||
Buffer_StoreElement(&Tx_Buffer, ReceivedByte);
|
RingBuffer_Insert(&Tx_Buffer, ReceivedByte);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Event handler for the CDC Class driver Host-to-Device Line Encoding Changed event.
|
/** Event handler for the CDC Class driver Host-to-Device Line Encoding Changed event.
|
||||||
|
@ -42,7 +42,7 @@
|
|||||||
#include <avr/interrupt.h>
|
#include <avr/interrupt.h>
|
||||||
|
|
||||||
#include "Descriptors.h"
|
#include "Descriptors.h"
|
||||||
#include "Lib/RingBuff.h"
|
#include "Lib/LightweightRingBuff.h"
|
||||||
|
|
||||||
#include <LUFA/Version.h>
|
#include <LUFA/Version.h>
|
||||||
#include <LUFA/Drivers/Board/LEDs.h>
|
#include <LUFA/Drivers/Board/LEDs.h>
|
||||||
|
90
Projects/Benito/Lib/LightweightRingBuff.h
Normal file
90
Projects/Benito/Lib/LightweightRingBuff.h
Normal file
@ -0,0 +1,90 @@
|
|||||||
|
/*
|
||||||
|
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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/** \file
|
||||||
|
*
|
||||||
|
* Ultra lightweight ring buffer, for fast insertion/deletion.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _ULW_RING_BUFF_H_
|
||||||
|
#define _ULW_RING_BUFF_H_
|
||||||
|
|
||||||
|
/* Includes: */
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
/* Defines: */
|
||||||
|
/** Size of each ring buffer, in bytes */
|
||||||
|
#define BUFFER_SIZE 128
|
||||||
|
|
||||||
|
/** Type of data to store into the buffer */
|
||||||
|
#define RingBuff_Data_t uint8_t
|
||||||
|
|
||||||
|
/* Type Defines: */
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
RingBuff_Data_t Buffer[BUFFER_SIZE];
|
||||||
|
RingBuff_Data_t* In;
|
||||||
|
RingBuff_Data_t* Out;
|
||||||
|
uint8_t Count;
|
||||||
|
} RingBuff_t;
|
||||||
|
|
||||||
|
/* Inline Functions: */
|
||||||
|
static inline void RingBuffer_InitBuffer(RingBuff_t* const Buffer)
|
||||||
|
{
|
||||||
|
Buffer->In = Buffer->Buffer;
|
||||||
|
Buffer->Out = Buffer->Buffer;
|
||||||
|
Buffer->Count = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void RingBuffer_Insert(RingBuff_t* const Buffer, RingBuff_Data_t Data)
|
||||||
|
{
|
||||||
|
*Buffer->In = Data;
|
||||||
|
|
||||||
|
if (++Buffer->In == &Buffer->Buffer[BUFFER_SIZE])
|
||||||
|
Buffer->In = Buffer->Buffer;
|
||||||
|
|
||||||
|
Buffer->Count++;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline RingBuff_Data_t RingBuffer_Remove(RingBuff_t* const Buffer)
|
||||||
|
{
|
||||||
|
RingBuff_Data_t Data = *Buffer->Out;
|
||||||
|
|
||||||
|
if (++Buffer->Out == &Buffer->Buffer[BUFFER_SIZE])
|
||||||
|
Buffer->Out = Buffer->Buffer;
|
||||||
|
|
||||||
|
Buffer->Count--;
|
||||||
|
|
||||||
|
return Data;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
@ -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 255 // 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
|
|
@ -126,7 +126,6 @@ LUFA_OPTS += -D USE_STATIC_OPTIONS="(USB_DEVICE_OPT_FULLSPEED | USB_OPT_REG_ENAB
|
|||||||
# List C source files here. (C dependencies are automatically generated.)
|
# List C source files here. (C dependencies are automatically generated.)
|
||||||
SRC = $(TARGET).c \
|
SRC = $(TARGET).c \
|
||||||
Descriptors.c \
|
Descriptors.c \
|
||||||
Lib/RingBuff.c \
|
|
||||||
$(LUFA_PATH)/LUFA/Drivers/USB/LowLevel/DevChapter9.c \
|
$(LUFA_PATH)/LUFA/Drivers/USB/LowLevel/DevChapter9.c \
|
||||||
$(LUFA_PATH)/LUFA/Drivers/USB/LowLevel/Endpoint.c \
|
$(LUFA_PATH)/LUFA/Drivers/USB/LowLevel/Endpoint.c \
|
||||||
$(LUFA_PATH)/LUFA/Drivers/USB/LowLevel/Host.c \
|
$(LUFA_PATH)/LUFA/Drivers/USB/LowLevel/Host.c \
|
||||||
|
90
Projects/USBtoSerial/Lib/LightweightRingBuff.h
Normal file
90
Projects/USBtoSerial/Lib/LightweightRingBuff.h
Normal file
@ -0,0 +1,90 @@
|
|||||||
|
/*
|
||||||
|
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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/** \file
|
||||||
|
*
|
||||||
|
* Ultra lightweight ring buffer, for fast insertion/deletion.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _ULW_RING_BUFF_H_
|
||||||
|
#define _ULW_RING_BUFF_H_
|
||||||
|
|
||||||
|
/* Includes: */
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
/* Defines: */
|
||||||
|
/** Size of each ring buffer, in bytes */
|
||||||
|
#define BUFFER_SIZE 128
|
||||||
|
|
||||||
|
/** Type of data to store into the buffer */
|
||||||
|
#define RingBuff_Data_t uint8_t
|
||||||
|
|
||||||
|
/* Type Defines: */
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
RingBuff_Data_t Buffer[BUFFER_SIZE];
|
||||||
|
RingBuff_Data_t* In;
|
||||||
|
RingBuff_Data_t* Out;
|
||||||
|
uint8_t Count;
|
||||||
|
} RingBuff_t;
|
||||||
|
|
||||||
|
/* Inline Functions: */
|
||||||
|
static inline void RingBuffer_InitBuffer(RingBuff_t* const Buffer)
|
||||||
|
{
|
||||||
|
Buffer->In = Buffer->Buffer;
|
||||||
|
Buffer->Out = Buffer->Buffer;
|
||||||
|
Buffer->Count = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void RingBuffer_Insert(RingBuff_t* const Buffer, RingBuff_Data_t Data)
|
||||||
|
{
|
||||||
|
*Buffer->In = Data;
|
||||||
|
|
||||||
|
if (++Buffer->In == &Buffer->Buffer[BUFFER_SIZE])
|
||||||
|
Buffer->In = Buffer->Buffer;
|
||||||
|
|
||||||
|
Buffer->Count++;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline RingBuff_Data_t RingBuffer_Remove(RingBuff_t* const Buffer)
|
||||||
|
{
|
||||||
|
RingBuff_Data_t Data = *Buffer->Out;
|
||||||
|
|
||||||
|
if (++Buffer->Out == &Buffer->Buffer[BUFFER_SIZE])
|
||||||
|
Buffer->Out = Buffer->Buffer;
|
||||||
|
|
||||||
|
Buffer->Count--;
|
||||||
|
|
||||||
|
return Data;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
@ -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
|
|
@ -73,8 +73,8 @@ int main(void)
|
|||||||
{
|
{
|
||||||
SetupHardware();
|
SetupHardware();
|
||||||
|
|
||||||
Buffer_Initialize(&USBtoUSART_Buffer);
|
RingBuffer_InitBuffer(&USBtoUSART_Buffer);
|
||||||
Buffer_Initialize(&USARTtoUSB_Buffer);
|
RingBuffer_InitBuffer(&USARTtoUSB_Buffer);
|
||||||
|
|
||||||
LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
|
LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
|
||||||
sei();
|
sei();
|
||||||
@ -84,19 +84,19 @@ int main(void)
|
|||||||
/* Read bytes from the USB OUT endpoint into the USART transmit buffer */
|
/* Read bytes from the USB OUT endpoint into the USART 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 - USBtoUSART_Buffer.Elements))
|
if (!(BUFFER_SIZE - USBtoUSART_Buffer.Count))
|
||||||
break;
|
break;
|
||||||
|
|
||||||
Buffer_StoreElement(&USBtoUSART_Buffer, CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface));
|
RingBuffer_Insert(&USBtoUSART_Buffer, CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface));
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Read bytes from the USART receive buffer into the USB IN endpoint */
|
/* Read bytes from the USART receive buffer into the USB IN endpoint */
|
||||||
while (USARTtoUSB_Buffer.Elements)
|
while (USARTtoUSB_Buffer.Count)
|
||||||
CDC_Device_SendByte(&VirtualSerial_CDC_Interface, Buffer_GetElement(&USARTtoUSB_Buffer));
|
CDC_Device_SendByte(&VirtualSerial_CDC_Interface, RingBuffer_Remove(&USARTtoUSB_Buffer));
|
||||||
|
|
||||||
/* Load bytes from the USART transmit buffer into the USART */
|
/* Load bytes from the USART transmit buffer into the USART */
|
||||||
while (USBtoUSART_Buffer.Elements)
|
while (USBtoUSART_Buffer.Count)
|
||||||
Serial_TxByte(Buffer_GetElement(&USBtoUSART_Buffer));
|
Serial_TxByte(RingBuffer_Remove(&USBtoUSART_Buffer));
|
||||||
|
|
||||||
CDC_Device_USBTask(&VirtualSerial_CDC_Interface);
|
CDC_Device_USBTask(&VirtualSerial_CDC_Interface);
|
||||||
USB_USBTask();
|
USB_USBTask();
|
||||||
@ -154,7 +154,7 @@ ISR(USART1_RX_vect, ISR_BLOCK)
|
|||||||
uint8_t ReceivedByte = UDR1;
|
uint8_t ReceivedByte = UDR1;
|
||||||
|
|
||||||
if (USB_DeviceState == DEVICE_STATE_Configured)
|
if (USB_DeviceState == DEVICE_STATE_Configured)
|
||||||
Buffer_StoreElement(&USARTtoUSB_Buffer, ReceivedByte);
|
RingBuffer_Insert(&USARTtoUSB_Buffer, ReceivedByte);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Event handler for the CDC Class driver Line Encoding Changed event.
|
/** Event handler for the CDC Class driver Line Encoding Changed event.
|
||||||
|
@ -44,7 +44,7 @@
|
|||||||
|
|
||||||
#include "Descriptors.h"
|
#include "Descriptors.h"
|
||||||
|
|
||||||
#include "Lib/RingBuff.h"
|
#include "Lib/LightweightRingBuff.h"
|
||||||
|
|
||||||
#include <LUFA/Version.h>
|
#include <LUFA/Version.h>
|
||||||
#include <LUFA/Drivers/Board/LEDs.h>
|
#include <LUFA/Drivers/Board/LEDs.h>
|
||||||
|
@ -126,7 +126,6 @@ LUFA_OPTS += -D USE_STATIC_OPTIONS="(USB_DEVICE_OPT_FULLSPEED | USB_OPT_REG_ENAB
|
|||||||
# List C source files here. (C dependencies are automatically generated.)
|
# List C source files here. (C dependencies are automatically generated.)
|
||||||
SRC = $(TARGET).c \
|
SRC = $(TARGET).c \
|
||||||
Descriptors.c \
|
Descriptors.c \
|
||||||
Lib/RingBuff.c \
|
|
||||||
$(LUFA_PATH)/LUFA/Drivers/USB/LowLevel/DevChapter9.c \
|
$(LUFA_PATH)/LUFA/Drivers/USB/LowLevel/DevChapter9.c \
|
||||||
$(LUFA_PATH)/LUFA/Drivers/USB/LowLevel/Endpoint.c \
|
$(LUFA_PATH)/LUFA/Drivers/USB/LowLevel/Endpoint.c \
|
||||||
$(LUFA_PATH)/LUFA/Drivers/USB/LowLevel/Host.c \
|
$(LUFA_PATH)/LUFA/Drivers/USB/LowLevel/Host.c \
|
||||||
|
@ -40,7 +40,7 @@
|
|||||||
#include "SoftUART.h"
|
#include "SoftUART.h"
|
||||||
|
|
||||||
/** Total number of bits remaining to be sent in the current frame */
|
/** Total number of bits remaining to be sent in the current frame */
|
||||||
static uint8_t TX_BitsRemaining
|
static uint8_t TX_BitsRemaining;
|
||||||
|
|
||||||
/** Temporary data variable to hold the byte being transmitted as it is shifted out */
|
/** Temporary data variable to hold the byte being transmitted as it is shifted out */
|
||||||
static uint8_t TX_Data;
|
static uint8_t TX_Data;
|
||||||
@ -76,7 +76,7 @@ void SoftUART_Init(void)
|
|||||||
ISR(INT0_vect)
|
ISR(INT0_vect)
|
||||||
{
|
{
|
||||||
/* Set reception channel to fire 1.5 bits past the beginning of the start bit */
|
/* Set reception channel to fire 1.5 bits past the beginning of the start bit */
|
||||||
OCR1A = TCNT1 + ((BIT_TIME * 3) / 2) - 1;
|
OCR1A = TCNT1 + ((BIT_TIME * 3) / 2);
|
||||||
|
|
||||||
/* Clear the received data temporary variable, reset the current received bit position mask */
|
/* Clear the received data temporary variable, reset the current received bit position mask */
|
||||||
RX_Data = 0;
|
RX_Data = 0;
|
||||||
|
@ -121,13 +121,13 @@ void USARTBridge_Task(void)
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
/* 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--)
|
if (CDC_Device_BytesReceived(&VirtualSerial_CDC_Interface))
|
||||||
RingBuffer_Insert(&USBtoUART_Buffer, CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface));
|
RingBuffer_Insert(&USBtoUART_Buffer, CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface));
|
||||||
|
|
||||||
/* 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.Count)
|
if (UARTtoUSB_Buffer.Count)
|
||||||
CDC_Device_SendByte(&VirtualSerial_CDC_Interface, RingBuffer_Remove(&UARTtoUSB_Buffer));
|
CDC_Device_SendByte(&VirtualSerial_CDC_Interface, RingBuffer_Remove(&UARTtoUSB_Buffer));
|
||||||
|
|
||||||
CDC_Device_USBTask(&VirtualSerial_CDC_Interface);
|
CDC_Device_USBTask(&VirtualSerial_CDC_Interface);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -147,6 +147,7 @@ void SetupHardware(void)
|
|||||||
USB_Init();
|
USB_Init();
|
||||||
V2Protocol_Init();
|
V2Protocol_Init();
|
||||||
|
|
||||||
|
#if 0
|
||||||
/* Disable JTAG debugging */
|
/* Disable JTAG debugging */
|
||||||
MCUCR |= (1 << JTD);
|
MCUCR |= (1 << JTD);
|
||||||
MCUCR |= (1 << JTD);
|
MCUCR |= (1 << JTD);
|
||||||
@ -161,6 +162,9 @@ void SetupHardware(void)
|
|||||||
/* Re-enable JTAG debugging */
|
/* Re-enable JTAG debugging */
|
||||||
MCUCR &= ~(1 << JTD);
|
MCUCR &= ~(1 << JTD);
|
||||||
MCUCR &= ~(1 << JTD);
|
MCUCR &= ~(1 << JTD);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
CurrentFirmwareMode = MODE_USART_BRIDGE;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Event handler for the library USB Configuration Changed event. */
|
/** Event handler for the library USB Configuration Changed event. */
|
||||||
|
Loading…
Reference in New Issue
Block a user