git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@1080 35acf78f-673a-0410-8e92-d51de3d6d3f4

This commit is contained in:
gdisirio 2009-08-19 11:59:36 +00:00
parent f6b7b4d844
commit 9400701044
4 changed files with 104 additions and 94 deletions

View File

@ -25,47 +25,72 @@
*/ */
#include <ch.h> #include <ch.h>
#include <serial.h>
#if CH_USE_SERIAL_FULLDUPLEX
/* /*
* Interface implementation, the following functions just invoke the equivalent * Interface implementation, the following functions just invoke the equivalent
* queue-level function or macro. * queue-level function or macro.
*/ */
static bool_t putwouldblock(void *instance) { static bool_t putwouldblock(void *ip) {
return chOQIsFull(&((FullDuplexDriver *)instance)->d2.oqueue); return chOQIsFull(&((SerialDriver *)ip)->d2.oqueue);
} }
static bool_t getwouldblock(void *instance) { static bool_t getwouldblock(void *ip) {
return chIQIsEmpty(&((FullDuplexDriver *)instance)->d2.iqueue); return chIQIsEmpty(&((SerialDriver *)ip)->d2.iqueue);
} }
static msg_t put(void *instance, uint8_t b, systime_t timeout) { static msg_t put(void *ip, uint8_t b, systime_t timeout) {
return chOQPutTimeout(&((FullDuplexDriver *)instance)->d2.oqueue, b, timeout); return chOQPutTimeout(&((SerialDriver *)ip)->d2.oqueue, b, timeout);
} }
static msg_t get(void *instance, systime_t timeout) { static msg_t get(void *ip, systime_t timeout) {
return chIQGetTimeout(&((FullDuplexDriver *)instance)->d2.iqueue, timeout); return chIQGetTimeout(&((SerialDriver *)ip)->d2.iqueue, timeout);
} }
static size_t write(void *instance, uint8_t *buffer, size_t n) { static size_t write(void *ip, uint8_t *buffer, size_t n) {
return chOQWrite(&((FullDuplexDriver *)instance)->d2.oqueue, buffer, n); return chOQWrite(&((SerialDriver *)ip)->d2.oqueue, buffer, n);
} }
static size_t read(void *instance, uint8_t *buffer, size_t n) { static size_t read(void *ip, uint8_t *buffer, size_t n) {
return chIQRead(&((FullDuplexDriver *)instance)->d2.iqueue, buffer, n); return chIQRead(&((SerialDriver *)ip)->d2.iqueue, buffer, n);
} }
static const struct FullDuplexDriverVMT vmt = { static void start(void *ip, const SerialDriverConfig *config) {
SerialDriver *sdp = (SerialDriver *)ip;
chSysLock();
sd_lld_start(sdp, config);
chSysUnlock();
}
/**
* @brief Stops the driver.
* @Details Any thread waiting on the driver's queues will be awakened with
* the message @p Q_RESET.
*
* @param sd The @p SerialDriver to be stopped.
*/
static void stop(void *ip) {
SerialDriver *sdp = (SerialDriver *)ip;
chSysLock();
sd_lld_stop(sdp);
chOQResetI(&sdp->d2.oqueue);
chIQResetI(&sdp->d2.iqueue);
chSchRescheduleS();
chSysUnlock();
}
static const struct SerialDriverVMT vmt = {
{putwouldblock, getwouldblock, put, get}, {putwouldblock, getwouldblock, put, get},
{write, read}, {write, read},
{} {start, stop}
}; };
/** /**
@ -73,7 +98,7 @@ static const struct FullDuplexDriverVMT vmt = {
* @details The HW dependent part of the initialization has to be performed * @details The HW dependent part of the initialization has to be performed
* outside, usually in the hardware initialization code. * outside, usually in the hardware initialization code.
* *
* @param[out] sd pointer to a @p FullDuplexDriver structure * @param[out] sd pointer to a @p SerialDriver structure
* @param[in] ib pointer to a memory area allocated for the Input Queue buffer * @param[in] ib pointer to a memory area allocated for the Input Queue buffer
* @param[in] isize size of the Input Queue buffer * @param[in] isize size of the Input Queue buffer
* @param[in] inotify pointer to a callback function that is invoked when * @param[in] inotify pointer to a callback function that is invoked when
@ -85,20 +110,17 @@ static const struct FullDuplexDriverVMT vmt = {
* some data is written in the Queue. The value can be * some data is written in the Queue. The value can be
* @p NULL. * @p NULL.
*/ */
void chFDDInit(FullDuplexDriver *sd, void sdInit(SerialDriver *sdp, qnotify_t inotify, qnotify_t onotify) {
uint8_t *ib, size_t isize, qnotify_t inotify,
uint8_t *ob, size_t osize, qnotify_t onotify) {
chDbgCheck((sd != NULL) && (ib != NULL) && (ob != NULL) && chDbgCheck(sdp != NULL, "sdInit");
(isize > 0) && (osize > 0), "chFDDInit");
sd->vmt = &vmt; sdp->vmt = &vmt;
chEvtInit(&sd->d1.ievent); chEvtInit(&sdp->d1.ievent);
chEvtInit(&sd->d1.oevent); chEvtInit(&sdp->d1.oevent);
chEvtInit(&sd->d2.sevent); chEvtInit(&sdp->d2.sevent);
sd->d2.flags = SD_NO_ERROR; sdp->d2.flags = SD_NO_ERROR;
chIQInit(&sd->d2.iqueue, ib, isize, inotify); chIQInit(&sdp->d2.iqueue, sdp->d2.ib, SERIAL_BUFFERS_SIZE, inotify);
chOQInit(&sd->d2.oqueue, ob, osize, onotify); chOQInit(&sdp->d2.oqueue, sdp->d2.ob, SERIAL_BUFFERS_SIZE, onotify);
} }
/** /**
@ -106,13 +128,13 @@ void chFDDInit(FullDuplexDriver *sd,
* @details This function must be called from the input interrupt service * @details This function must be called from the input interrupt service
* routine in order to enqueue incoming data and generate the * routine in order to enqueue incoming data and generate the
* related events. * related events.
* @param[in] sd pointer to a @p FullDuplexDriver structure * @param[in] sd pointer to a @p SerialDriver structure
* @param[in] b the byte to be written in the driver's Input Queue * @param[in] b the byte to be written in the driver's Input Queue
*/ */
void sdIncomingDataI(FullDuplexDriver *sd, uint8_t b) { void sdIncomingDataI(SerialDriver *sd, uint8_t b) {
if (chIQPutI(&sd->d2.iqueue, b) < Q_OK) if (chIQPutI(&sd->d2.iqueue, b) < Q_OK)
chFDDAddFlagsI(sd, SD_OVERRUN_ERROR); sdAddFlagsI(sd, SD_OVERRUN_ERROR);
else else
chEvtBroadcastI(&sd->d1.ievent); chEvtBroadcastI(&sd->d1.ievent);
} }
@ -122,12 +144,12 @@ void sdIncomingDataI(FullDuplexDriver *sd, uint8_t b) {
* @details Must be called from the output interrupt service routine in order * @details Must be called from the output interrupt service routine in order
* to get the next byte to be transmitted. * to get the next byte to be transmitted.
* *
* @param[in] sd pointer to a @p FullDuplexDriver structure * @param[in] sd pointer to a @p SerialDriver structure
* @return The byte value read from the driver's output queue. * @return The byte value read from the driver's output queue.
* @retval Q_EMPTY if the queue is empty (the lower driver usually disables * @retval Q_EMPTY if the queue is empty (the lower driver usually disables
* the interrupt source when this happens). * the interrupt source when this happens).
*/ */
msg_t sdRequestDataI(FullDuplexDriver *sd) { msg_t sdRequestDataI(SerialDriver *sd) {
msg_t b = chOQGetI(&sd->d2.oqueue); msg_t b = chOQGetI(&sd->d2.oqueue);
if (b < Q_OK) if (b < Q_OK)
@ -140,10 +162,10 @@ msg_t sdRequestDataI(FullDuplexDriver *sd) {
* @details Must be called from the I/O interrupt service routine in order to * @details Must be called from the I/O interrupt service routine in order to
* notify I/O conditions as errors, signals change etc. * notify I/O conditions as errors, signals change etc.
* *
* @param[in] sd pointer to a @p FullDuplexDriver structure * @param[in] sd pointer to a @p SerialDriver structure
* @param[in] mask condition flags to be added to the mask * @param[in] mask condition flags to be added to the mask
*/ */
void sdAddFlagsI(FullDuplexDriver *sd, dflags_t mask) { void sdAddFlagsI(SerialDriver *sd, sdflags_t mask) {
sd->d2.flags |= mask; sd->d2.flags |= mask;
chEvtBroadcastI(&sd->d2.sevent); chEvtBroadcastI(&sd->d2.sevent);
@ -152,17 +174,16 @@ void sdAddFlagsI(FullDuplexDriver *sd, dflags_t mask) {
/** /**
* @brief Returns and clears the errors mask associated to the driver. * @brief Returns and clears the errors mask associated to the driver.
* *
* @param[in] sd pointer to a @p FullDuplexDriver structure * @param[in] sd pointer to a @p SerialDriver structure
* @return The condition flags modified since last time this function was * @return The condition flags modified since last time this function was
* invoked. * invoked.
*/ */
dflags_t sdGetAndClearFlags(FullDuplexDriver *sd) { sdflags_t sdGetAndClearFlags(SerialDriver *sd) {
dflags_t mask; sdflags_t mask;
mask = sd->d2.flags; mask = sd->d2.flags;
sd->d2.flags = SD_NO_ERROR; sd->d2.flags = SD_NO_ERROR;
return mask; return mask;
} }
#endif /* CH_USE_SERIAL_FULLDUPLEX */
/** @} */ /** @} */

View File

@ -42,42 +42,30 @@
/** Break detected.*/ /** Break detected.*/
#define SD_BREAK_DETECTED 32 #define SD_BREAK_DETECTED 32
typedef struct _SerialDriver SerialDriver;
#include "serial_lld.h" #include "serial_lld.h"
/** Serial Driver condition flags type.*/
typedef uint8_t sdflags_t;
/** /**
* @brief @p FullDuplexDriver specific methods. * @brief @p SerialDriver specific methods.
*/ */
struct _serial_driver_methods { struct _serial_driver_methods {
void (*start)(SerialDriver *sd, const SerialDriverConfig *config); /**
void (*stop)(SerialDriver *sd); * @brief Configures and starts the driver.
}; *
* @param[in] ip pointer to a @p SerialDriver or derived class
* @param[in] config The configuration record.
*/
void (*start)(void *ip, const SerialDriverConfig *config);
/** /**
* @brief @p SerialDriver specific data. * @brief Stops the driver.
* @Details Any thread waiting on the driver's queues will be awakened with
* the message @p Q_RESET.
*
* @param[in] ip pointer to a @p SerialDriver or derived class
*/ */
struct _serial_driver_data { void (*stop)(void *ip);
/**
* Input queue, incoming data can be read from this input queue by
* using the queues APIs.
*/
InputQueue iqueue;
/**
* Output queue, outgoing data can be written to this output queue by
* using the queues APIs.
*/
OutputQueue oqueue;
/**
* Status Change @p EventSource. This event is generated when one or more
* condition flags change.
*/
EventSource sevent;
/**
* I/O driver status flags.
*/
sdflags_t flags;
}; };
/** /**
@ -105,7 +93,7 @@ struct SerialDriverVMT {
* @details This class extends @p BaseAsynchronousChannel by adding physical * @details This class extends @p BaseAsynchronousChannel by adding physical
* I/O queues. * I/O queues.
*/ */
typedef struct { struct _SerialDriver {
/** /**
* Virtual Methods Table. * Virtual Methods Table.
*/ */
@ -122,14 +110,12 @@ typedef struct {
* @p SerialDriver specific data. * @p SerialDriver specific data.
*/ */
struct _serial_driver_data d2; struct _serial_driver_data d2;
} SerialDriver; };
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
void sdInit(SerialDriver *sd, void sdInit(SerialDriver *sd, qnotify_t inotify, qnotify_t onotify);
uint8_t *ib, size_t isize, qnotify_t inotify,
uint8_t *ob, size_t osize, qnotify_t onotify);
void sdIncomingDataI(SerialDriver *sd, uint8_t b); void sdIncomingDataI(SerialDriver *sd, uint8_t b);
msg_t sdRequestDataI(SerialDriver *sd); msg_t sdRequestDataI(SerialDriver *sd);
void sdAddFlagsI(SerialDriver *sd, sdflags_t mask); void sdAddFlagsI(SerialDriver *sd, sdflags_t mask);

View File

@ -45,4 +45,25 @@ void sd_lld_init(void) {
} }
/**
* @brief Low level serial driver configuration and (re)start.
*
* @param[in] sdp pointer to a @p SerialDriver object
* @param[in] config the architecture-dependent serial driver configuration
*/
void sd_lld_start(SerialDriver *sdp, const SerialDriverConfig *config) {
}
/**
* @brief Low level serial driver stop.
* @details De-initializes the USART, stops the associated clock, resets the
* interrupt vector.
*
* @param[in] sd pointer to a @p SerialDriver object
*/
void sd_lld_stop(SerialDriver *sdp) {
}
/** @} */ /** @} */

View File

@ -77,10 +77,6 @@ struct _serial_driver_data {
uint8_t ob[SERIAL_BUFFERS_SIZE]; uint8_t ob[SERIAL_BUFFERS_SIZE];
}; };
/*===========================================================================*/
/* I/O Ports Types and constants. */
/*===========================================================================*/
/** /**
* @brief Generic Serial Driver static initializer. * @brief Generic Serial Driver static initializer.
* @details An instance of this structure must be passed to @p sdStart() * @details An instance of this structure must be passed to @p sdStart()
@ -95,28 +91,14 @@ typedef struct {
} SerialDriverConfig; } SerialDriverConfig;
/*===========================================================================*/ /*===========================================================================*/
/* Implementation, some of the following macros could be implemented as */ /* External declarations. */
/* real functions, if so please put them in the file serial_lld.c. */
/*===========================================================================*/ /*===========================================================================*/
/**
* @brief Low level serial driver configuration and (re)start.
*
* @param[in] sd pointer to a @p SerialDriver object
* @param[in] config the architecture-dependent serial driver configuration
*/
#define sd_lld_start(sd, config)
/**
* @brief Low level serial driver stop.
*
* @param[in] sd pointer to a @p SerialDriver object
*/
#define sd_lld_stop(sd)
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
void sd_lld_init(void); void sd_lld_init(void);
void sd_lld_start(SerialDriver *sdp, const SerialDriverConfig *config);
void sd_lld_stop(SerialDriver *sdp);
#endif #endif
#ifdef __cplusplus #ifdef __cplusplus
} }