2009-08-16 13:07:24 +00:00
|
|
|
/*
|
2011-03-18 18:38:08 +00:00
|
|
|
ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010,
|
2013-02-02 10:58:09 +00:00
|
|
|
2011,2012,2013 Giovanni Di Sirio.
|
2009-08-16 13:07:24 +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-27 08:54:22 +00:00
|
|
|
* @file chmboxes.h
|
2010-02-06 12:31:09 +00:00
|
|
|
* @brief Mailboxes macros and structures.
|
|
|
|
*
|
2009-08-30 06:59:43 +00:00
|
|
|
* @addtogroup mailboxes
|
2009-08-16 13:07:24 +00:00
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2010-02-27 08:54:22 +00:00
|
|
|
#ifndef _CHMBOXES_H_
|
|
|
|
#define _CHMBOXES_H_
|
2009-08-16 13:07:24 +00:00
|
|
|
|
2013-07-20 10:12:44 +00:00
|
|
|
#if CH_CFG_USE_MAILBOXES || defined(__DOXYGEN__)
|
2009-08-16 13:07:24 +00:00
|
|
|
|
2013-07-19 14:51:35 +00:00
|
|
|
/*===========================================================================*/
|
|
|
|
/* Module constants. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Module pre-compile time settings. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Derived constants and error checks. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
2013-07-20 10:12:44 +00:00
|
|
|
#if !CH_CFG_USE_SEMAPHORES
|
|
|
|
#error "CH_CFG_USE_MAILBOXES requires CH_CFG_USE_SEMAPHORES"
|
2009-10-17 09:40:49 +00:00
|
|
|
#endif
|
|
|
|
|
2013-07-19 14:51:35 +00:00
|
|
|
/*===========================================================================*/
|
|
|
|
/* Module data structures and types. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
2010-09-18 06:48:56 +00:00
|
|
|
/**
|
2010-12-12 07:36:51 +00:00
|
|
|
* @brief Structure representing a mailbox object.
|
2010-09-18 06:48:56 +00:00
|
|
|
*/
|
2009-08-16 13:07:24 +00:00
|
|
|
typedef struct {
|
2010-02-06 12:31:09 +00:00
|
|
|
msg_t *mb_buffer; /**< @brief Pointer to the mailbox
|
|
|
|
buffer. */
|
|
|
|
msg_t *mb_top; /**< @brief Pointer to the location
|
|
|
|
after the buffer. */
|
|
|
|
msg_t *mb_wrptr; /**< @brief Write pointer. */
|
|
|
|
msg_t *mb_rdptr; /**< @brief Read pointer. */
|
2013-07-19 13:17:42 +00:00
|
|
|
semaphore_t mb_fullsem; /**< @brief Full counter
|
|
|
|
@p semaphore_t. */
|
|
|
|
semaphore_t mb_emptysem; /**< @brief Empty counter
|
|
|
|
@p semaphore_t. */
|
2013-07-19 14:51:35 +00:00
|
|
|
} mailbox_t;
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Module macros. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Data part of a static mailbox initializer.
|
|
|
|
* @details This macro should be used when statically initializing a
|
|
|
|
* mailbox that is part of a bigger structure.
|
|
|
|
*
|
|
|
|
* @param[in] name the name of the mailbox variable
|
|
|
|
* @param[in] buffer pointer to the mailbox buffer area
|
|
|
|
* @param[in] size size of the mailbox buffer area
|
|
|
|
*/
|
|
|
|
#define _MAILBOX_DATA(name, buffer, size) { \
|
|
|
|
(msg_t *)(buffer), \
|
|
|
|
(msg_t *)(buffer) + size, \
|
|
|
|
(msg_t *)(buffer), \
|
|
|
|
(msg_t *)(buffer), \
|
|
|
|
_SEMAPHORE_DATA(name.mb_fullsem, 0), \
|
|
|
|
_SEMAPHORE_DATA(name.mb_emptysem, size), \
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Static mailbox initializer.
|
|
|
|
* @details Statically initialized mailboxes require no explicit
|
|
|
|
* initialization using @p chMBInit().
|
|
|
|
*
|
|
|
|
* @param[in] name the name of the mailbox variable
|
|
|
|
* @param[in] buffer pointer to the mailbox buffer area
|
|
|
|
* @param[in] size size of the mailbox buffer area
|
|
|
|
*/
|
|
|
|
#define MAILBOX_DECL(name, buffer, size) \
|
|
|
|
mailbox_t name = _MAILBOX_DATA(name, buffer, size)
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* External declarations. */
|
|
|
|
/*===========================================================================*/
|
2009-08-16 13:07:24 +00:00
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
2013-07-20 07:24:12 +00:00
|
|
|
void chMBObjectInit(mailbox_t *mbp, msg_t *buf, cnt_t n);
|
2013-07-19 14:51:35 +00:00
|
|
|
void chMBReset(mailbox_t *mbp);
|
|
|
|
msg_t chMBPost(mailbox_t *mbp, msg_t msg, systime_t timeout);
|
|
|
|
msg_t chMBPostS(mailbox_t *mbp, msg_t msg, systime_t timeout);
|
|
|
|
msg_t chMBPostI(mailbox_t *mbp, msg_t msg);
|
|
|
|
msg_t chMBPostAhead(mailbox_t *mbp, msg_t msg, systime_t timeout);
|
|
|
|
msg_t chMBPostAheadS(mailbox_t *mbp, msg_t msg, systime_t timeout);
|
|
|
|
msg_t chMBPostAheadI(mailbox_t *mbp, msg_t msg);
|
|
|
|
msg_t chMBFetch(mailbox_t *mbp, msg_t *msgp, systime_t timeout);
|
|
|
|
msg_t chMBFetchS(mailbox_t *mbp, msg_t *msgp, systime_t timeout);
|
|
|
|
msg_t chMBFetchI(mailbox_t *mbp, msg_t *msgp);
|
2009-08-16 13:07:24 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-07-19 14:51:35 +00:00
|
|
|
/*===========================================================================*/
|
|
|
|
/* Module inline functions. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
2009-08-16 13:07:24 +00:00
|
|
|
/**
|
2010-02-06 12:31:09 +00:00
|
|
|
* @brief Returns the mailbox buffer size.
|
|
|
|
*
|
2013-07-19 14:51:35 +00:00
|
|
|
* @param[in] mbp the pointer to an initialized mailbox_t object
|
2010-09-21 10:22:06 +00:00
|
|
|
*
|
|
|
|
* @iclass
|
2009-08-16 13:07:24 +00:00
|
|
|
*/
|
2013-07-19 14:51:35 +00:00
|
|
|
static inline size_t chMBSizeI(mailbox_t *mbp) {
|
|
|
|
|
|
|
|
return (size_t)(mbp->mb_top - mbp->mb_buffer);
|
|
|
|
}
|
2009-08-16 13:07:24 +00:00
|
|
|
|
|
|
|
/**
|
2010-10-07 13:32:58 +00:00
|
|
|
* @brief Returns the number of free message slots into a mailbox.
|
2010-02-06 12:31:09 +00:00
|
|
|
* @note Can be invoked in any system state but if invoked out of a locked
|
|
|
|
* state then the returned value may change after reading.
|
|
|
|
* @note The returned value can be less than zero when there are waiting
|
|
|
|
* threads on the internal semaphore.
|
|
|
|
*
|
2013-07-19 14:51:35 +00:00
|
|
|
* @param[in] mbp the pointer to an initialized mailbox_t object
|
2010-02-06 12:31:09 +00:00
|
|
|
* @return The number of empty message slots.
|
2010-09-21 10:22:06 +00:00
|
|
|
*
|
|
|
|
* @iclass
|
2009-08-16 13:07:24 +00:00
|
|
|
*/
|
2013-07-19 14:51:35 +00:00
|
|
|
static inline cnt_t chMBGetFreeCountI(mailbox_t *mbp) {
|
|
|
|
|
|
|
|
chDbgCheckClassI();
|
|
|
|
|
|
|
|
return chSemGetCounterI(&mbp->mb_emptysem);
|
|
|
|
}
|
2009-08-16 13:07:24 +00:00
|
|
|
|
|
|
|
/**
|
2010-10-08 18:16:38 +00:00
|
|
|
* @brief Returns the number of used message slots into a mailbox.
|
2010-02-06 12:31:09 +00:00
|
|
|
* @note Can be invoked in any system state but if invoked out of a locked
|
|
|
|
* state then the returned value may change after reading.
|
|
|
|
* @note The returned value can be less than zero when there are waiting
|
|
|
|
* threads on the internal semaphore.
|
|
|
|
*
|
2013-07-19 14:51:35 +00:00
|
|
|
* @param[in] mbp the pointer to an initialized mailbox_t object
|
2010-02-06 12:31:09 +00:00
|
|
|
* @return The number of queued messages.
|
2010-09-21 10:22:06 +00:00
|
|
|
*
|
|
|
|
* @iclass
|
2009-08-16 13:07:24 +00:00
|
|
|
*/
|
2013-07-19 14:51:35 +00:00
|
|
|
static inline cnt_t chMBGetUsedCountI(mailbox_t *mbp) {
|
|
|
|
|
|
|
|
chDbgCheckClassI();
|
|
|
|
|
|
|
|
return chSemGetCounterI(&mbp->mb_fullsem);
|
|
|
|
}
|
2009-08-16 13:07:24 +00:00
|
|
|
|
|
|
|
/**
|
2010-02-06 12:31:09 +00:00
|
|
|
* @brief Returns the next message in the queue without removing it.
|
2010-09-18 06:48:56 +00:00
|
|
|
* @pre A message must be waiting in the queue for this function to work
|
|
|
|
* or it would return garbage. The correct way to use this macro is
|
2010-10-07 13:32:58 +00:00
|
|
|
* to use @p chMBGetFullCountI() and then use this macro, all within
|
|
|
|
* a lock state.
|
2010-09-21 10:22:06 +00:00
|
|
|
*
|
|
|
|
* @iclass
|
2009-08-16 13:07:24 +00:00
|
|
|
*/
|
2013-07-19 14:51:35 +00:00
|
|
|
static inline cnt_t chMBPeekI(mailbox_t *mbp) {
|
2009-08-16 13:07:24 +00:00
|
|
|
|
2013-07-19 14:51:35 +00:00
|
|
|
chDbgCheckClassI();
|
2009-08-16 13:07:24 +00:00
|
|
|
|
2013-07-19 14:51:35 +00:00
|
|
|
return *mbp->mb_rdptr;
|
|
|
|
}
|
2009-08-16 13:07:24 +00:00
|
|
|
|
2013-07-20 10:12:44 +00:00
|
|
|
#endif /* CH_CFG_USE_MAILBOXES */
|
2009-08-16 13:07:24 +00:00
|
|
|
|
2010-02-27 08:54:22 +00:00
|
|
|
#endif /* _CHMBOXES_H_ */
|
2009-08-16 13:07:24 +00:00
|
|
|
|
|
|
|
/** @} */
|