From 94007010446b75548a41b3979fa0bdc4bf66a08c Mon Sep 17 00:00:00 2001 From: gdisirio Date: Wed, 19 Aug 2009 11:59:36 +0000 Subject: git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@1080 35acf78f-673a-0410-8e92-d51de3d6d3f4 --- os/io/serial.c | 101 ++++++++++++++++++++++++++----------------- os/io/serial.h | 50 ++++++++------------- os/io/templates/serial_lld.c | 21 +++++++++ os/io/templates/serial_lld.h | 24 ++-------- 4 files changed, 103 insertions(+), 93 deletions(-) diff --git a/os/io/serial.c b/os/io/serial.c index b513938b8..abc099eea 100644 --- a/os/io/serial.c +++ b/os/io/serial.c @@ -25,47 +25,72 @@ */ #include - -#if CH_USE_SERIAL_FULLDUPLEX +#include /* * Interface implementation, the following functions just invoke the equivalent * 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(&((SerialDriver *)ip)->d2.iqueue, buffer, n); +} + +static void start(void *ip, const SerialDriverConfig *config) { + SerialDriver *sdp = (SerialDriver *)ip; + + chSysLock(); + sd_lld_start(sdp, config); + chSysUnlock(); +} - return chIQRead(&((FullDuplexDriver *)instance)->d2.iqueue, buffer, n); +/** + * @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 FullDuplexDriverVMT vmt = { +static const struct SerialDriverVMT vmt = { {putwouldblock, getwouldblock, put, get}, {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 * 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] isize size of the Input Queue buffer * @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 * @p NULL. */ -void chFDDInit(FullDuplexDriver *sd, - uint8_t *ib, size_t isize, qnotify_t inotify, - uint8_t *ob, size_t osize, qnotify_t onotify) { +void sdInit(SerialDriver *sdp, qnotify_t inotify, qnotify_t onotify) { - chDbgCheck((sd != NULL) && (ib != NULL) && (ob != NULL) && - (isize > 0) && (osize > 0), "chFDDInit"); + chDbgCheck(sdp != NULL, "sdInit"); - sd->vmt = &vmt; - chEvtInit(&sd->d1.ievent); - chEvtInit(&sd->d1.oevent); - chEvtInit(&sd->d2.sevent); - sd->d2.flags = SD_NO_ERROR; - chIQInit(&sd->d2.iqueue, ib, isize, inotify); - chOQInit(&sd->d2.oqueue, ob, osize, onotify); + sdp->vmt = &vmt; + chEvtInit(&sdp->d1.ievent); + chEvtInit(&sdp->d1.oevent); + chEvtInit(&sdp->d2.sevent); + sdp->d2.flags = SD_NO_ERROR; + chIQInit(&sdp->d2.iqueue, sdp->d2.ib, SERIAL_BUFFERS_SIZE, inotify); + 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 * routine in order to enqueue incoming data and generate the * 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 */ -void sdIncomingDataI(FullDuplexDriver *sd, uint8_t b) { +void sdIncomingDataI(SerialDriver *sd, uint8_t b) { if (chIQPutI(&sd->d2.iqueue, b) < Q_OK) - chFDDAddFlagsI(sd, SD_OVERRUN_ERROR); + sdAddFlagsI(sd, SD_OVERRUN_ERROR); else 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 * 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. * @retval Q_EMPTY if the queue is empty (the lower driver usually disables * the interrupt source when this happens). */ -msg_t sdRequestDataI(FullDuplexDriver *sd) { +msg_t sdRequestDataI(SerialDriver *sd) { msg_t b = chOQGetI(&sd->d2.oqueue); 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 * 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 */ -void sdAddFlagsI(FullDuplexDriver *sd, dflags_t mask) { +void sdAddFlagsI(SerialDriver *sd, sdflags_t mask) { sd->d2.flags |= mask; 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. * - * @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 * invoked. */ -dflags_t sdGetAndClearFlags(FullDuplexDriver *sd) { - dflags_t mask; +sdflags_t sdGetAndClearFlags(SerialDriver *sd) { + sdflags_t mask; mask = sd->d2.flags; sd->d2.flags = SD_NO_ERROR; return mask; } -#endif /* CH_USE_SERIAL_FULLDUPLEX */ /** @} */ diff --git a/os/io/serial.h b/os/io/serial.h index ef6b7f742..aa18ebe7b 100644 --- a/os/io/serial.h +++ b/os/io/serial.h @@ -42,42 +42,30 @@ /** Break detected.*/ #define SD_BREAK_DETECTED 32 -#include "serial_lld.h" +typedef struct _SerialDriver SerialDriver; -/** Serial Driver condition flags type.*/ -typedef uint8_t sdflags_t; +#include "serial_lld.h" /** - * @brief @p FullDuplexDriver specific methods. + * @brief @p SerialDriver specific methods. */ struct _serial_driver_methods { - void (*start)(SerialDriver *sd, const SerialDriverConfig *config); - void (*stop)(SerialDriver *sd); -}; - -/** - * @brief @p SerialDriver specific data. - */ -struct _serial_driver_data { /** - * Input queue, incoming data can be read from this input queue by - * using the queues APIs. + * @brief Configures and starts the driver. + * + * @param[in] ip pointer to a @p SerialDriver or derived class + * @param[in] config The configuration record. */ - 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; + void (*start)(void *ip, const SerialDriverConfig *config); + /** - * I/O driver status flags. + * @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 */ - sdflags_t flags; + void (*stop)(void *ip); }; /** @@ -105,7 +93,7 @@ struct SerialDriverVMT { * @details This class extends @p BaseAsynchronousChannel by adding physical * I/O queues. */ -typedef struct { +struct _SerialDriver { /** * Virtual Methods Table. */ @@ -122,14 +110,12 @@ typedef struct { * @p SerialDriver specific data. */ struct _serial_driver_data d2; -} SerialDriver; +}; #ifdef __cplusplus extern "C" { #endif - void sdInit(SerialDriver *sd, - uint8_t *ib, size_t isize, qnotify_t inotify, - uint8_t *ob, size_t osize, qnotify_t onotify); + void sdInit(SerialDriver *sd, qnotify_t inotify, qnotify_t onotify); void sdIncomingDataI(SerialDriver *sd, uint8_t b); msg_t sdRequestDataI(SerialDriver *sd); void sdAddFlagsI(SerialDriver *sd, sdflags_t mask); diff --git a/os/io/templates/serial_lld.c b/os/io/templates/serial_lld.c index e99c70385..0fbaa3900 100644 --- a/os/io/templates/serial_lld.c +++ b/os/io/templates/serial_lld.c @@ -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) { + +} + /** @} */ diff --git a/os/io/templates/serial_lld.h b/os/io/templates/serial_lld.h index edb10fb85..2c133f91c 100644 --- a/os/io/templates/serial_lld.h +++ b/os/io/templates/serial_lld.h @@ -77,10 +77,6 @@ struct _serial_driver_data { uint8_t ob[SERIAL_BUFFERS_SIZE]; }; -/*===========================================================================*/ -/* I/O Ports Types and constants. */ -/*===========================================================================*/ - /** * @brief Generic Serial Driver static initializer. * @details An instance of this structure must be passed to @p sdStart() @@ -95,28 +91,14 @@ typedef struct { } SerialDriverConfig; /*===========================================================================*/ -/* Implementation, some of the following macros could be implemented as */ -/* real functions, if so please put them in the file serial_lld.c. */ +/* External declarations. */ /*===========================================================================*/ -/** - * @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 extern "C" { void sd_lld_init(void); + void sd_lld_start(SerialDriver *sdp, const SerialDriverConfig *config); + void sd_lld_stop(SerialDriver *sdp); #endif #ifdef __cplusplus } -- cgit v1.2.3