2009-11-29 08:50:13 +00:00
|
|
|
/*
|
2010-02-21 07:24:53 +00:00
|
|
|
ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010 Giovanni Di Sirio.
|
2009-11-29 08:50:13 +00:00
|
|
|
|
|
|
|
This file is part of ChibiOS/RT.
|
|
|
|
|
|
|
|
ChibiOS/RT is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
ChibiOS/RT is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-02-06 16:17:30 +00:00
|
|
|
* @file serial.c
|
|
|
|
* @brief Serial Driver code.
|
|
|
|
*
|
2009-11-29 08:50:13 +00:00
|
|
|
* @addtogroup SERIAL
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ch.h"
|
|
|
|
#include "hal.h"
|
|
|
|
|
2009-11-29 10:37:31 +00:00
|
|
|
#if CH_HAL_USE_SERIAL || defined(__DOXYGEN__)
|
2009-11-29 08:50:13 +00:00
|
|
|
|
2009-12-29 11:12:05 +00:00
|
|
|
/*===========================================================================*/
|
|
|
|
/* Driver exported variables. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Driver local variables. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Driver local functions. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
2009-11-29 08:50:13 +00:00
|
|
|
/*
|
|
|
|
* Interface implementation, the following functions just invoke the equivalent
|
2010-01-25 18:50:35 +00:00
|
|
|
* queue-level function or macro.
|
2009-11-29 08:50:13 +00:00
|
|
|
*/
|
2010-01-05 17:14:09 +00:00
|
|
|
|
|
|
|
static size_t writes(void *ip, const uint8_t *bp, size_t n) {
|
|
|
|
|
2010-01-22 14:51:54 +00:00
|
|
|
return chOQWriteTimeout(&((SerialDriver *)ip)->oqueue, bp,
|
2010-01-05 17:14:09 +00:00
|
|
|
n, TIME_INFINITE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t reads(void *ip, uint8_t *bp, size_t n) {
|
|
|
|
|
2010-01-22 14:51:54 +00:00
|
|
|
return chIQReadTimeout(&((SerialDriver *)ip)->iqueue, bp,
|
2010-01-05 17:14:09 +00:00
|
|
|
n, TIME_INFINITE);
|
|
|
|
}
|
|
|
|
|
2009-11-29 08:50:13 +00:00
|
|
|
static bool_t putwouldblock(void *ip) {
|
|
|
|
|
2010-09-21 10:22:06 +00:00
|
|
|
return chOQIsFullI(&((SerialDriver *)ip)->oqueue);
|
2009-11-29 08:50:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool_t getwouldblock(void *ip) {
|
|
|
|
|
2010-09-21 10:22:06 +00:00
|
|
|
return chIQIsEmptyI(&((SerialDriver *)ip)->iqueue);
|
2009-11-29 08:50:13 +00:00
|
|
|
}
|
|
|
|
|
2010-01-05 17:14:09 +00:00
|
|
|
static msg_t putt(void *ip, uint8_t b, systime_t timeout) {
|
2009-11-29 08:50:13 +00:00
|
|
|
|
2010-01-22 14:51:54 +00:00
|
|
|
return chOQPutTimeout(&((SerialDriver *)ip)->oqueue, b, timeout);
|
2009-11-29 08:50:13 +00:00
|
|
|
}
|
|
|
|
|
2010-01-05 17:14:09 +00:00
|
|
|
static msg_t gett(void *ip, systime_t timeout) {
|
2009-11-29 08:50:13 +00:00
|
|
|
|
2010-01-22 14:51:54 +00:00
|
|
|
return chIQGetTimeout(&((SerialDriver *)ip)->iqueue, timeout);
|
2009-11-29 08:50:13 +00:00
|
|
|
}
|
|
|
|
|
2010-01-05 17:14:09 +00:00
|
|
|
static size_t writet(void *ip, const uint8_t *bp, size_t n, systime_t time) {
|
2009-11-29 08:50:13 +00:00
|
|
|
|
2010-01-22 14:51:54 +00:00
|
|
|
return chOQWriteTimeout(&((SerialDriver *)ip)->oqueue, bp, n, time);
|
2009-11-29 08:50:13 +00:00
|
|
|
}
|
|
|
|
|
2010-01-05 17:14:09 +00:00
|
|
|
static size_t readt(void *ip, uint8_t *bp, size_t n, systime_t time) {
|
2009-11-29 08:50:13 +00:00
|
|
|
|
2010-01-22 14:51:54 +00:00
|
|
|
return chIQReadTimeout(&((SerialDriver *)ip)->iqueue, bp, n, time);
|
2009-11-29 08:50:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct SerialDriverVMT vmt = {
|
2010-01-22 14:51:54 +00:00
|
|
|
writes, reads, putwouldblock, getwouldblock, putt, gett, writet, readt
|
2009-11-29 08:50:13 +00:00
|
|
|
};
|
|
|
|
|
2009-12-29 11:12:05 +00:00
|
|
|
/*===========================================================================*/
|
|
|
|
/* Driver exported functions. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
2009-11-29 08:50:13 +00:00
|
|
|
/**
|
2010-02-06 16:17:30 +00:00
|
|
|
* @brief Serial Driver initialization.
|
2010-10-04 17:16:18 +00:00
|
|
|
*
|
|
|
|
* @init
|
2009-11-29 08:50:13 +00:00
|
|
|
*/
|
|
|
|
void sdInit(void) {
|
|
|
|
|
|
|
|
sd_lld_init();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-02-06 16:17:30 +00:00
|
|
|
* @brief Initializes a generic full duplex driver object.
|
2009-11-29 08:50:13 +00:00
|
|
|
* @details The HW dependent part of the initialization has to be performed
|
|
|
|
* outside, usually in the hardware initialization code.
|
|
|
|
*
|
2010-02-06 16:17:30 +00:00
|
|
|
* @param[out] sdp pointer to a @p SerialDriver structure
|
|
|
|
* @param[in] inotify pointer to a callback function that is invoked when
|
|
|
|
* some data is read from the Queue. The value can be
|
|
|
|
* @p NULL.
|
|
|
|
* @param[in] onotify pointer to a callback function that is invoked when
|
|
|
|
* some data is written in the Queue. The value can be
|
|
|
|
* @p NULL.
|
2010-10-04 17:16:18 +00:00
|
|
|
*
|
|
|
|
* @init
|
2009-11-29 08:50:13 +00:00
|
|
|
*/
|
|
|
|
void sdObjectInit(SerialDriver *sdp, qnotify_t inotify, qnotify_t onotify) {
|
|
|
|
|
|
|
|
sdp->vmt = &vmt;
|
2010-01-22 14:51:54 +00:00
|
|
|
chEvtInit(&sdp->ievent);
|
|
|
|
chEvtInit(&sdp->oevent);
|
|
|
|
chEvtInit(&sdp->sevent);
|
|
|
|
sdp->state = SD_STOP;
|
|
|
|
sdp->flags = SD_NO_ERROR;
|
|
|
|
chIQInit(&sdp->iqueue, sdp->ib, SERIAL_BUFFERS_SIZE, inotify);
|
|
|
|
chOQInit(&sdp->oqueue, sdp->ob, SERIAL_BUFFERS_SIZE, onotify);
|
2009-11-29 08:50:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-02-06 16:17:30 +00:00
|
|
|
* @brief Configures and starts the driver.
|
2009-11-29 08:50:13 +00:00
|
|
|
*
|
2010-02-06 16:17:30 +00:00
|
|
|
* @param[in] sdp pointer to a @p SerialDriver object
|
|
|
|
* @param[in] config the architecture-dependent serial driver configuration.
|
|
|
|
* If this parameter is set to @p NULL then a default
|
|
|
|
* configuration is used.
|
2010-10-04 17:16:18 +00:00
|
|
|
*
|
|
|
|
* @api
|
2009-11-29 08:50:13 +00:00
|
|
|
*/
|
2010-01-01 13:41:31 +00:00
|
|
|
void sdStart(SerialDriver *sdp, const SerialConfig *config) {
|
|
|
|
|
2010-01-15 15:22:36 +00:00
|
|
|
chDbgCheck(sdp != NULL, "sdStart");
|
2009-11-29 08:50:13 +00:00
|
|
|
|
|
|
|
chSysLock();
|
2010-01-22 14:51:54 +00:00
|
|
|
chDbgAssert((sdp->state == SD_STOP) || (sdp->state == SD_READY),
|
2010-01-01 13:41:31 +00:00
|
|
|
"sdStart(), #1",
|
|
|
|
"invalid state");
|
2010-04-19 15:29:44 +00:00
|
|
|
sd_lld_start(sdp, config);
|
2010-01-22 14:51:54 +00:00
|
|
|
sdp->state = SD_READY;
|
2009-11-29 08:50:13 +00:00
|
|
|
chSysUnlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-02-06 16:17:30 +00:00
|
|
|
* @brief Stops the driver.
|
2009-11-29 08:50:13 +00:00
|
|
|
* @details Any thread waiting on the driver's queues will be awakened with
|
|
|
|
* the message @p Q_RESET.
|
|
|
|
*
|
2010-02-06 16:17:30 +00:00
|
|
|
* @param[in] sdp pointer to a @p SerialDrive object
|
2010-10-04 17:16:18 +00:00
|
|
|
*
|
|
|
|
* @api
|
2009-11-29 08:50:13 +00:00
|
|
|
*/
|
|
|
|
void sdStop(SerialDriver *sdp) {
|
|
|
|
|
2010-01-01 13:41:31 +00:00
|
|
|
chDbgCheck(sdp != NULL, "sdStop");
|
|
|
|
|
2009-11-29 08:50:13 +00:00
|
|
|
chSysLock();
|
2010-01-22 14:51:54 +00:00
|
|
|
chDbgAssert((sdp->state == SD_STOP) || (sdp->state == SD_READY),
|
2010-01-01 13:41:31 +00:00
|
|
|
"sdStop(), #1",
|
|
|
|
"invalid state");
|
2009-11-29 08:50:13 +00:00
|
|
|
sd_lld_stop(sdp);
|
2010-01-22 14:51:54 +00:00
|
|
|
sdp->state = SD_STOP;
|
|
|
|
chOQResetI(&sdp->oqueue);
|
|
|
|
chIQResetI(&sdp->iqueue);
|
2009-11-29 08:50:13 +00:00
|
|
|
chSchRescheduleS();
|
|
|
|
chSysUnlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-02-06 16:17:30 +00:00
|
|
|
* @brief Handles incoming data.
|
2009-11-29 08:50:13 +00:00
|
|
|
* @details This function must be called from the input interrupt service
|
|
|
|
* routine in order to enqueue incoming data and generate the
|
|
|
|
* related events.
|
2010-02-06 16:17:30 +00:00
|
|
|
* @note The incoming data event is only generated when the input queue
|
|
|
|
* becomes non-empty.
|
|
|
|
* @note In order to gain some performance it is suggested to not use
|
|
|
|
* this function directly but copy this code directly into the
|
|
|
|
* interrupt service routine.
|
2010-01-01 13:41:31 +00:00
|
|
|
*
|
2010-02-06 16:17:30 +00:00
|
|
|
* @param[in] sdp pointer to a @p SerialDriver structure
|
|
|
|
* @param[in] b the byte to be written in the driver's Input Queue
|
2010-10-04 17:16:18 +00:00
|
|
|
*
|
|
|
|
* @iclass
|
2009-11-29 08:50:13 +00:00
|
|
|
*/
|
2010-01-01 13:41:31 +00:00
|
|
|
void sdIncomingDataI(SerialDriver *sdp, uint8_t b) {
|
|
|
|
|
|
|
|
chDbgCheck(sdp != NULL, "sdIncomingDataI");
|
2009-11-29 08:50:13 +00:00
|
|
|
|
2010-09-21 10:22:06 +00:00
|
|
|
if (chIQIsEmptyI(&sdp->iqueue))
|
2010-01-22 14:51:54 +00:00
|
|
|
chEvtBroadcastI(&sdp->ievent);
|
|
|
|
if (chIQPutI(&sdp->iqueue, b) < Q_OK)
|
2010-01-01 13:41:31 +00:00
|
|
|
sdAddFlagsI(sdp, SD_OVERRUN_ERROR);
|
2009-11-29 08:50:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-02-06 16:17:30 +00:00
|
|
|
* @brief Handles outgoing data.
|
2009-11-29 08:50:13 +00:00
|
|
|
* @details Must be called from the output interrupt service routine in order
|
|
|
|
* to get the next byte to be transmitted.
|
2010-02-06 16:17:30 +00:00
|
|
|
* @note In order to gain some performance it is suggested to not use
|
|
|
|
* this function directly but copy this code directly into the
|
|
|
|
* interrupt service routine.
|
2009-11-29 08:50:13 +00:00
|
|
|
*
|
2010-02-06 16:17:30 +00:00
|
|
|
* @param[in] sdp 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).
|
2010-10-04 17:16:18 +00:00
|
|
|
*
|
|
|
|
* @iclass
|
2009-11-29 08:50:13 +00:00
|
|
|
*/
|
2010-01-01 13:41:31 +00:00
|
|
|
msg_t sdRequestDataI(SerialDriver *sdp) {
|
2010-01-25 18:50:35 +00:00
|
|
|
msg_t b;
|
2009-11-29 08:50:13 +00:00
|
|
|
|
2010-01-01 13:41:31 +00:00
|
|
|
chDbgCheck(sdp != NULL, "sdRequestDataI");
|
|
|
|
|
2010-01-25 18:50:35 +00:00
|
|
|
b = chOQGetI(&sdp->oqueue);
|
2009-11-29 08:50:13 +00:00
|
|
|
if (b < Q_OK)
|
2010-01-22 14:51:54 +00:00
|
|
|
chEvtBroadcastI(&sdp->oevent);
|
2009-11-29 08:50:13 +00:00
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-02-06 16:17:30 +00:00
|
|
|
* @brief Handles communication events/errors.
|
2009-11-29 08:50:13 +00:00
|
|
|
* @details Must be called from the I/O interrupt service routine in order to
|
|
|
|
* notify I/O conditions as errors, signals change etc.
|
|
|
|
*
|
2010-02-06 16:17:30 +00:00
|
|
|
* @param[in] sdp pointer to a @p SerialDriver structure
|
|
|
|
* @param[in] mask condition flags to be added to the mask
|
2010-10-04 17:16:18 +00:00
|
|
|
*
|
|
|
|
* @iclass
|
2009-11-29 08:50:13 +00:00
|
|
|
*/
|
2010-01-01 13:41:31 +00:00
|
|
|
void sdAddFlagsI(SerialDriver *sdp, sdflags_t mask) {
|
|
|
|
|
|
|
|
chDbgCheck(sdp != NULL, "sdAddFlagsI");
|
2009-11-29 08:50:13 +00:00
|
|
|
|
2010-01-22 14:51:54 +00:00
|
|
|
sdp->flags |= mask;
|
|
|
|
chEvtBroadcastI(&sdp->sevent);
|
2009-11-29 08:50:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-02-06 16:17:30 +00:00
|
|
|
* @brief Returns and clears the errors mask associated to the driver.
|
2009-11-29 08:50:13 +00:00
|
|
|
*
|
2010-02-06 16:17:30 +00:00
|
|
|
* @param[in] sdp pointer to a @p SerialDriver structure
|
|
|
|
* @return The condition flags modified since last time this
|
|
|
|
* function was invoked.
|
2010-10-04 17:16:18 +00:00
|
|
|
*
|
|
|
|
* @api
|
2009-11-29 08:50:13 +00:00
|
|
|
*/
|
2010-01-01 13:41:31 +00:00
|
|
|
sdflags_t sdGetAndClearFlags(SerialDriver *sdp) {
|
2009-11-29 08:50:13 +00:00
|
|
|
sdflags_t mask;
|
|
|
|
|
2010-01-01 13:41:31 +00:00
|
|
|
chDbgCheck(sdp != NULL, "sdGetAndClearFlags");
|
|
|
|
|
2010-01-15 15:22:36 +00:00
|
|
|
chSysLock();
|
2010-01-22 14:51:54 +00:00
|
|
|
mask = sdp->flags;
|
|
|
|
sdp->flags = SD_NO_ERROR;
|
2010-01-15 15:22:36 +00:00
|
|
|
chSysUnlock();
|
2009-11-29 08:50:13 +00:00
|
|
|
return mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CH_HAL_USE_SERIAL */
|
|
|
|
|
|
|
|
/** @} */
|