Commit afd828c0 authored by Dean Camera's avatar Dean Camera
Browse files

Removed SerialStream module, rolled functionality into the base USART Serial...

Removed SerialStream module, rolled functionality into the base USART Serial peripheral driver instead through the new Serial_CreateStream() and Serial_CreateBlockingStream() methods.

Renamed the Serial byte send/receive functions to remain consistent with the CDC driver's byte functions.

Altered the serial byte receive function to make it non-blocking.
parent 30f6d2bf
......@@ -51,7 +51,7 @@
#include <LUFA/Version.h>
#include <LUFA/Drivers/Misc/TerminalCodes.h>
#include <LUFA/Drivers/USB/USB.h>
#include <LUFA/Drivers/Peripheral/SerialStream.h>
#include <LUFA/Drivers/Peripheral/Serial.h>
#include <LUFA/Drivers/Board/LEDs.h>
/* Macros: */
......
......@@ -129,8 +129,7 @@ SRC = $(TARGET).c \
ConfigDescriptor.c \
Lib/PrinterCommands.c \
$(LUFA_SRC_USB) \
$(LUFA_SRC_SERIAL) \
$(LUFA_SRC_SERIALSTREAM)
$(LUFA_SRC_SERIAL)
# List C++ source files here. (C dependencies are automatically generated.)
......
......@@ -66,9 +66,12 @@ void SetupHardware(void)
clock_prescale_set(clock_div_1);
/* Hardware Initialization */
SerialStream_Init(9600, false);
Serial_Init(9600, false);
LEDs_Init();
USB_Init();
/* Create a stdio stream for the serial port for stdin and stdout */
Serial_CreateStream(NULL);
}
/** Event handler for the USB_DeviceAttached event. This indicates that a device has been attached to the host, and
......
......@@ -47,7 +47,7 @@
#include <LUFA/Version.h>
#include <LUFA/Drivers/Misc/TerminalCodes.h>
#include <LUFA/Drivers/USB/USB.h>
#include <LUFA/Drivers/Peripheral/SerialStream.h>
#include <LUFA/Drivers/Peripheral/Serial.h>
#include <LUFA/Drivers/Board/LEDs.h>
#include "Lib/RNDISCommands.h"
......
......@@ -129,8 +129,7 @@ SRC = $(TARGET).c \
ConfigDescriptor.c \
Lib/RNDISCommands.c \
$(LUFA_SRC_USB) \
$(LUFA_SRC_SERIAL) \
$(LUFA_SRC_SERIALSTREAM)
$(LUFA_SRC_SERIAL)
# List C++ source files here. (C dependencies are automatically generated.)
......
......@@ -67,9 +67,12 @@ void SetupHardware(void)
CLKPR = 0;
/* Hardware Initialization */
SerialStream_Init(9600, false);
Serial_Init(9600, false);
LEDs_Init();
USB_Init();
/* Create a stdio stream for the serial port for stdin and stdout */
Serial_CreateStream(NULL);
}
/** Event handler for the USB_DeviceAttached event. This indicates that a device has been attached to the host, and
......
......@@ -49,7 +49,7 @@
#include <LUFA/Drivers/Misc/TerminalCodes.h>
#include <LUFA/Drivers/USB/USB.h>
#include <LUFA/Drivers/Peripheral/SerialStream.h>
#include <LUFA/Drivers/Peripheral/Serial.h>
#include <LUFA/Drivers/Board/LEDs.h>
/* Macros: */
......
......@@ -129,8 +129,7 @@ SRC = $(TARGET).c \
ConfigDescriptor.c \
Lib/StillImageCommands.c \
$(LUFA_SRC_USB) \
$(LUFA_SRC_SERIAL) \
$(LUFA_SRC_SERIALSTREAM)
$(LUFA_SRC_SERIAL)
# List C++ source files here. (C dependencies are automatically generated.)
......
......@@ -66,9 +66,12 @@ void SetupHardware(void)
clock_prescale_set(clock_div_1);
/* Hardware Initialization */
SerialStream_Init(9600, false);
Serial_Init(9600, false);
LEDs_Init();
USB_Init();
/* Create a stdio stream for the serial port for stdin and stdout */
Serial_CreateStream(NULL);
}
/** Event handler for the USB_DeviceAttached event. This indicates that a device has been attached to the host, and
......
......@@ -47,7 +47,7 @@
#include <LUFA/Version.h>
#include <LUFA/Drivers/Misc/TerminalCodes.h>
#include <LUFA/Drivers/USB/USB.h>
#include <LUFA/Drivers/Peripheral/SerialStream.h>
#include <LUFA/Drivers/Peripheral/Serial.h>
#include <LUFA/Drivers/Board/LEDs.h>
#include "ConfigDescriptor.h"
......
......@@ -128,8 +128,7 @@ include $(LUFA_PATH)/LUFA/makefile
SRC = $(TARGET).c \
ConfigDescriptor.c \
$(LUFA_SRC_USB) \
$(LUFA_SRC_SERIAL) \
$(LUFA_SRC_SERIALSTREAM)
$(LUFA_SRC_SERIAL)
# List C++ source files here. (C dependencies are automatically generated.)
......
This diff is collapsed.
......@@ -30,24 +30,53 @@
#include "Serial.h"
void Serial_TxString_P(const char* FlashStringPtr)
FILE USARTSerialStream;
int Serial_putchar(char DataByte,
FILE *Stream)
{
(void)Stream;
Serial_SendByte(DataByte);
return 0;
}
int Serial_getchar(FILE *Stream)
{
(void)Stream;
if (!(Serial_IsCharReceived()))
return _FDEV_EOF;
return Serial_ReceiveByte();
}
int Serial_getchar_Blocking(FILE *Stream)
{
(void)Stream;
while (!(Serial_IsCharReceived()));
return Serial_ReceiveByte();
}
void Serial_SendString_P(const char* FlashStringPtr)
{
uint8_t CurrByte;
while ((CurrByte = pgm_read_byte(FlashStringPtr)) != 0x00)
{
Serial_TxByte(CurrByte);
Serial_SendByte(CurrByte);
FlashStringPtr++;
}
}
void Serial_TxString(const char* StringPtr)
void Serial_SendString(const char* StringPtr)
{
uint8_t CurrByte;
while ((CurrByte = *StringPtr) != 0x00)
{
Serial_TxByte(CurrByte);
Serial_SendByte(CurrByte);
StringPtr++;
}
}
......
......@@ -70,6 +70,7 @@
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <stdbool.h>
#include <stdio.h>
#include "../../Common/Common.h"
#include "../Misc/TerminalCodes.h"
......@@ -79,6 +80,18 @@
extern "C" {
#endif
/* Private Interface - For use in library only: */
#if !defined(__DOXYGEN__)
/* External Variables: */
extern FILE USARTSerialStream;
/* Function Prototypes: */
int Serial_putchar(char DataByte,
FILE *Stream);
int Serial_getchar(FILE *Stream);
int Serial_getchar_Blocking(FILE *Stream);
#endif
/* Public Interface - May be used in end-application: */
/* Macros: */
/** Macro for calculating the baud value from a given baud rate when the U2X (double speed) bit is
......@@ -96,14 +109,14 @@
*
* \param[in] FlashStringPtr Pointer to a string located in program space.
*/
void Serial_TxString_P(const char* FlashStringPtr) ATTR_NON_NULL_PTR_ARG(1);
void Serial_SendString_P(const char* FlashStringPtr) ATTR_NON_NULL_PTR_ARG(1);
/** Transmits a given string located in SRAM memory through the USART.
*
* \param[in] StringPtr Pointer to a string located in SRAM space.
*/
void Serial_TxString(const char* StringPtr) ATTR_NON_NULL_PTR_ARG(1);
void Serial_SendString(const char* StringPtr) ATTR_NON_NULL_PTR_ARG(1);
/* Inline Functions: */
/** Initializes the USART, ready for serial data transmission and reception. This initializes the interface to
* standard 8-bit, no parity, 1 stop bit settings suitable for most applications.
......@@ -137,6 +150,52 @@
PORTD &= ~(1 << 2);
}
/** Creates a standard character stream from the USART so that it can be used with all the regular functions
* in the avr-libc \c <stdio.h> library that accept a \c FILE stream as a destination (e.g. \c fprintf). The created
* stream is bidirectional and can be used for both input and output functions.
*
* Reading data from this stream is non-blocking, i.e. in most instances, complete strings cannot be read in by a single
* fetch, as the endpoint will not be ready at some point in the transmission, aborting the transfer. However, this may
* be used when the read data is processed byte-per-bye (via \c getc()) or when the user application will implement its own
* line buffering.
*
* \param[in,out] Stream Pointer to a FILE structure where the created stream should be placed, if \c NULL stdio
* and stdin will be configured to use the USART.
*
* \pre The USART must first be configured via a call to \ref Serial_Init() before the stream is used.
*/
static inline void Serial_CreateStream(FILE* Stream)
{
if (!(Stream))
{
Stream = &USARTSerialStream;
stdin = Stream;
stdout = Stream;
}
*Stream = (FILE)FDEV_SETUP_STREAM(Serial_putchar, Serial_getchar, _FDEV_SETUP_RW);
}
/** Identical to \ref Serial_CreateStream(), except that reads are blocking until the calling stream function terminates
* the transfer.
*
* \param[in,out] Stream Pointer to a FILE structure where the created stream should be placed, if \c NULL stdio
* and stdin will be configured to use the USART.
*
* \pre The USART must first be configured via a call to \ref Serial_Init() before the stream is used.
*/
static inline void Serial_CreateBlockingStream(FILE* Stream)
{
if (!(Stream))
{
Stream = &USARTSerialStream;
stdin = Stream;
stdout = Stream;
}
*Stream = (FILE)FDEV_SETUP_STREAM(Serial_putchar, Serial_getchar_Blocking, _FDEV_SETUP_RW);
}
/** Indicates whether a character has been received through the USART.
*
* \return Boolean \c true if a character has been received, \c false otherwise.
......@@ -151,23 +210,23 @@
*
* \param[in] DataByte Byte to transmit through the USART.
*/
static inline void Serial_TxByte(const char DataByte) ATTR_ALWAYS_INLINE;
static inline void Serial_TxByte(const char DataByte)
static inline void Serial_SendByte(const char DataByte) ATTR_ALWAYS_INLINE;
static inline void Serial_SendByte(const char DataByte)
{
while (!(UCSR1A & (1 << UDRE1)));
UDR1 = DataByte;
}
/** Receives a byte from the USART. This function blocks until a byte has been
* received; if non-blocking behaviour is required, test for a received character
* beforehand with \ref Serial_IsCharReceived().
/** Receives the next byte from the USART.
*
* \return Byte received from the USART.
* \return Next byte received from the USART, or a negative value if no byte has been received.
*/
static inline char Serial_RxByte(void) ATTR_ALWAYS_INLINE;
static inline char Serial_RxByte(void)
static inline int16_t Serial_ReceiveByte(void) ATTR_ALWAYS_INLINE;
static inline int16_t Serial_ReceiveByte(void)
{
while (!(UCSR1A & (1 << RXC1)));
if (!(Serial_IsCharReceived()))
return -1;
return UDR1;
}
......
/*
LUFA Library
Copyright (C) Dean Camera, 2011.
dean [at] fourwalledcubicle [dot] com
www.lufa-lib.org
*/
/*
Copyright 2011 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.
*/
#define __INCLUDE_FROM_SERIALSTREAM_C
#include "SerialStream.h"
FILE USARTStream = FDEV_SETUP_STREAM(SerialStream_TxByte, SerialStream_RxByte, _FDEV_SETUP_RW);
static int SerialStream_TxByte(char DataByte,
FILE *Stream)
{
(void)Stream;
Serial_TxByte(DataByte);
return 0;
}
static int SerialStream_RxByte(FILE *Stream)
{
(void)Stream;
if (!(Serial_IsCharReceived()))
return _FDEV_EOF;
return Serial_RxByte();
}
/*
LUFA Library
Copyright (C) Dean Camera, 2011.
dean [at] fourwalledcubicle [dot] com
www.lufa-lib.org
*/
/*
Copyright 2011 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
* \brief Standard avr-libc character stream driver for the USART.
*
* Serial stream driver for the USART subsystem on supported USB AVRs. This makes use of the functions in the
* regular USART driver (see \ref Group_Serial), but allows the avr-libc standard stream functions (\c printf,
* \c puts, etc.) to work with the
* USART.
*/
/** \ingroup Group_PeripheralDrivers
* @defgroup Group_SerialStream Serial Stream Driver - LUFA/Drivers/Peripheral/SerialStream.h
*
* \section Sec_Dependencies Module Source Dependencies
* The following files must be built with any user project that uses this module:
* - LUFA/Drivers/Peripheral/SerialStream.c <i>(Makefile source module name: LUFA_SRC_SERIALSTREAM)</i>
*
* \section Sec_ModDescription Module Description
* Serial stream driver for the USART subsystem on supported USB AVRs. This makes use of the functions in the
* regular USART driver (see \ref Group_Serial), but allows the avr-libc standard stream functions (\c printf,
* \c puts, etc.) to work with the USART. Upon configuration, this will redirect the \c stdin standard input
* and \c stdout output streams to the USART.
*
* \section Sec_ExampleUsage Example Usage
* The following snippet is an example of how this module may be used within a typical
* application.
*
* \code
* // Initialise the Serial Stream driver before first use, with 9600 baud (and no double-speed mode)
* SerialStream_Init(9600, false);
*
* // Write a string to the USART via the implicit stdout stream
* printf("Test String using stdout\r\n");
*
* // Write a string to the USART via the explicit USART stream
* fprintf(&USARTStream, "Test String using explicit stream handle\r\n");
*
* // Read in an integer from the USART using the implicit stdin stream
* uint16_t TestValue;
* scanf("%d", &TestValue);
* \endcode
*
* @{
*/
#ifndef __SERIAL_STREAM_H__
#define __SERIAL_STREAM_H__
/* Includes: */
#include <avr/io.h>
#include <stdio.h>
#include "Serial.h"
/* Enable C linkage for C++ Compilers: */
#if defined(__cplusplus)
extern "C" {
#endif
/* Private Interface - For use in library only: */
#if !defined(__DOXYGEN__)
/* Function Prototypes: */
#if defined(__INCLUDE_FROM_SERIALSTREAM_C)
static int SerialStream_TxByte(char DataByte,
FILE *Stream) ATTR_NON_NULL_PTR_ARG(2);
static int SerialStream_RxByte(FILE *Stream) ATTR_NON_NULL_PTR_ARG(1);
#endif
#endif
/* Public Interface - May be used in end-application: */
/* External Variables: */
/** Named stream for the USART, once \ref SerialStream_Init() has been called. This may be used with the
* file based stream functions (fprintf, fscanf, etc.) that require a handle to the stream rather than
* using the stdin and stdout named streams.
*/
extern FILE USARTStream;
/* Inline Functions: */
/** Initialises the serial stream (and regular USART driver) so that both the stream and regular
* USART driver functions can be used. Must be called before any stream or regular USART functions.
*
* \param[in] BaudRate Baud rate to configure the USART to.
* \param[in] DoubleSpeed Enables double speed mode when set, halving the sample time to double the baud rate.
*/
static inline void SerialStream_Init(const uint32_t BaudRate,
const bool DoubleSpeed)
{
Serial_Init(BaudRate, DoubleSpeed);
stdout = &USARTStream;
stdin = &USARTStream;
}
/** Turns off the serial stream (and regular USART driver), disabling and returning used hardware to
* their default configuration.
*/
static inline void SerialStream_ShutDown(void)
{
Serial_ShutDown();
}
/* Disable C linkage for C++ Compilers: */
#if defined(__cplusplus)
}
#endif
#endif
/** @} */
......@@ -343,11 +343,11 @@
* Where \c uintA_t is a type large enough to hold one bit per button, and \c intB_t is a type large enough to hold the
* ranges of the signed \c MinAxisVal and \c MaxAxisVal values.
*
* \param[in] MinAxisVal Minimum X/Y logical axis value
* \param[in] MaxAxisVal Maximum X/Y logical axis value
* \param[in] MinPhysicalVal Minimum X/Y physical axis value, for movement resolution calculations
* \param[in] MaxPhysicalVal Maximum X/Y physical axis value, for movement resolution calculations
* \param[in] Buttons Total number of buttons in the device
* \param[in] MinAxisVal Minimum X/Y logical axis value.
* \param[in] MaxAxisVal Maximum X/Y logical axis value.
* \param[in] MinPhysicalVal Minimum X/Y physical axis value, for movement resolution calculations.
* \param[in] MaxPhysicalVal Maximum X/Y physical axis value, for movement resolution calculations.
* \param[in] Buttons Total number of buttons in the device.
*/
#define HID_DESCRIPTOR_JOYSTICK(MinAxisVal, MaxAxisVal, MinPhysicalVal, MaxPhysicalVal, Buttons) \
HID_RI_USAGE_PAGE(8, 0x01), \
......@@ -393,7 +393,7 @@
* \endcode
*
* \param[in] MaxKeys Number of simultaneous keys that can be reported at the one time (a value between 1 and
* (ENDPOINT_SIZE - 2) )
* (ENDPOINT_SIZE - 2) ).
*/
#define HID_DESCRIPTOR_KEYBOARD(MaxKeys) \
HID_RI_USAGE_PAGE(8, 0x01), \
......@@ -446,12 +446,12 @@
* Where \c intA_t is a type large enough to hold one bit per button, and \c intB_t is a type large enough to hold the
* ranges of the signed \c MinAxisVal and \c MaxAxisVal values.
*
* \param[in] MinAxisVal Minimum X/Y logical axis value
* \param[in] MaxAxisVal Maximum X/Y logical axis value
* \param[in] MinPhysicalVal Minimum X/Y physical axis value, for movement resolution calculations
* \param[in] MaxPhysicalVal Maximum X/Y physical axis value, for movement resolution calculations
* \param[in] Buttons Total number of buttons in the device
* \param[in] AbsoluteCoords Boolean true to use absolute X/Y coordinates (e.g. touchscreen)
* \param[in] MinAxisVal Minimum X/Y logical axis value.
* \param[in] MaxAxisVal Maximum X/Y logical axis value.
* \param[in] MinPhysicalVal Minimum X/Y physical axis value, for movement resolution calculations.
* \param[in] MaxPhysicalVal Maximum X/Y physical axis value, for movement resolution calculations.
* \param[in] Buttons Total number of buttons in the device.
* \param[in] AbsoluteCoords Boolean true to use absolute X/Y coordinates (e.g. touchscreen).
*/
#define HID_DESCRIPTOR_MOUSE(MinAxisVal, MaxAxisVal, MinPhysicalVal, MaxPhysicalVal, Buttons, AbsoluteCoords) \
HID_RI_USAGE_PAGE(8, 0x01), \
......@@ -488,11 +488,11 @@
* used for transporting abitrary data between the USB host and device via HID reports. The resulting report should be
* a uint8_t byte array of the specified length in both Device to Host (IN) and Host to Device (OUT) directions.
*
* \param[in] VendorPageNum Vendor Defined HID Usage Page index, ranging from 0x00 to 0xFF
* \param[in] CollectionUsage Vendor Usage for the encompasing report IN and OUT collection, ranging from 0x00 to 0xFF
* \param[in] DataINUsage Vendor Usage for the IN report data, ranging from 0x00 to 0xFF
* \param[in] DataOUTUsage Vendor Usage for the OUT report data, ranging from 0x00 to 0xFF
* \param[in] NumBytes Length of the data IN and OUT reports
* \param[in] VendorPageNum Vendor Defined HID Usage Page index, ranging from 0x00 to 0xFF.
* \param[in] CollectionUsage Vendor Usage for the encompasing report IN and OUT collection, ranging from 0x00 to 0xFF.
* \param[in] DataINUsage Vendor Usage for the IN report data, ranging from 0x00 to 0xFF.
* \param[in] DataOUTUsage Vendor Usage for the OUT report data, ranging from 0x00 to 0xFF.
* \param[in] NumBytes Length of the data IN and OUT reports.
*/
#define HID_DESCRIPTOR_VENDOR(VendorPageNum, CollectionUsage, DataINUsage, DataOUTUsage, NumBytes) \
HID_RI_USAGE_PAGE(16, (0xFF00 | VendorPageNum)), \
......
......@@ -44,13 +44,20 @@
* - LUFA/Drivers/USB/Class/Host/HIDParser.c <i>(Makefile source module name: LUFA_SRC_USB)</i>
*
* \section Sec_ModDescription Module Description
* Functions, macros, variables, enums and types related to the parsing of HID class device report descriptors.
* Human Interface Device (HID) class report descriptor parser. This module implements a parser than is
* capable of processing a complete HID report descriptor, and outputting a flat structure containing the
* contents of the report in an a more friendly format. The parsed data may then be further processed and used
* within an application to process sent and received HID reports to and from an attached HID device.
*
* The processed HID report is presented back to the user application as a flat structure containing each report
* item's IN, OUT and FEATURE items along with each item's attributes.
* A HID report descriptor consists of a set of HID report items, which describe the function and layout
* of data exchanged between a HID device and a host, including both the physical encoding of each item
* (such as a button, key press or joystick axis) in the sent and received data packets - known as "reports" -
* as well as other information about each item such as the usages, data range, physical location and other
* characterstics. In this way a HID device can retain a high degree of flexibility in its capabilities, as it
* is not forced to comply with a given report layout or featureset.
*
* This library portion also allows for easy setting and retrieval of data from a HID report, including devices
* with multiple reports on the one HID interface.
* This module also contains routines for the processing of data in an actual HID report, using the parsed report
* descritor data as a guide for the encoding.
*
* @{
*/
......@@ -159,8 +166,8 @@
*/
typedef struct
{
uint32_t Minimum; /**< Minimum value for the attribute. */
uint32_t Maximum; /**< Maximum value for the attribute. */
uint32_t Minimum; /**< Minimum value for the attribute. */
uint32_t Maximum; /**< Maximum value for the attribute. */
} HID_MinMax_t;
/** \brief HID Parser Report Item Unit Structure.
......@@ -169,8 +176,8 @@
*/
typedef struct
{
uint32_t Type; /**< Unit type (refer to HID specifications for details). */
uint8_t Exponent; /**< Unit exponent (refer to HID specifications for details). */
uint32_t Type; /**< Unit type (refer to HID specifications for details). */