/* ChibiOS/RT - Copyright (C) 2006-2007 Giovanni Di Sirio. 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 . */ /** * @file chqueues.c * @brief I/O Queues code. * @addtogroup io_queues * @{ */ #include #if CH_USE_QUEUES /** * @brief Initializes an input queue. * @details A Semaphore is internally initialized and works as a counter of * the bytes contained in the queue. * * @param[out] iqp pointer to an @p InputQueue structure * @param[in] buffer pointer to a memory area allocated as queue buffer * @param[in] size size of the queue buffer * @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. * * @note The callback is invoked from within the S-Locked system state, * see @ref system_states. */ void chIQInit(InputQueue *iqp, uint8_t *buffer, size_t size, qnotify_t inotify) { iqp->q_buffer = iqp->q_rdptr = iqp->q_wrptr = buffer; iqp->q_top = buffer + size; chSemInit(&iqp->q_sem, 0); iqp->q_notify = inotify; } /** * @brief Resets an input queue. * @details All the data in the input queue is erased and lost, any waiting * thread is resumed with status @p Q_RESET. * * @param[in] iqp pointer to an @p InputQueue structure * * @note A reset operation can be used by a low level driver in order to obtain * immediate attention from the high level layers. */ void chIQResetI(InputQueue *iqp) { iqp->q_rdptr = iqp->q_wrptr = iqp->q_buffer; chSemResetI(&iqp->q_sem, 0); } /** * @brief Input queue write. * @details A byte value is written into the low end of an input queue. * * @param[in] iqp pointer to an @p InputQueue structure * @param[in] b the byte value to be written in the queue * @return The operation status, it can be one of: * @retval Q_OK if the operation has been completed with success. * @retval Q_FULL if the queue is full and the operation cannot be completed. */ msg_t chIQPutI(InputQueue *iqp, uint8_t b) { if (chIQIsFull(iqp)) return Q_FULL; *iqp->q_wrptr++ = b; if (iqp->q_wrptr >= iqp->q_top) iqp->q_wrptr = iqp->q_buffer; chSemSignalI(&iqp->q_sem); return Q_OK; } /** * @brief Input queue read with timeout. * @details This function reads a byte value from an input queue. If the queue * is empty then the calling thread is suspended until a byte arrives * in the queue or a timeout occurs. * * @param[in] iqp pointer to an @p InputQueue structure * @param[in] timeout the number of ticks before the operation timeouts, * the following special values are allowed: * - @a TIME_IMMEDIATE immediate timeout. * - @a TIME_INFINITE no timeout. * . * @return A byte value from the queue or: * @retval Q_TIMEOUT if the specified time expired. * @retval Q_RESET if the queue was reset. */ msg_t chIQGetTimeout(InputQueue *iqp, systime_t timeout) { uint8_t b; msg_t msg; chSysLock(); if ((msg = chSemWaitTimeoutS(&iqp->q_sem, timeout)) < RDY_OK) { chSysUnlock(); return msg; } b = *iqp->q_rdptr++; if (iqp->q_rdptr >= iqp->q_top) iqp->q_rdptr = iqp->q_buffer; if (iqp->q_notify) iqp->q_notify(); chSysUnlock(); return b; } /** * @brief Non-blocking read. * @details The function reads data from an input queue into a buffer. The * transfer is non-blocking and can return zero if the queue is * empty. * * @param[in] iqp pointer to an @p InputQueue structure * @param[out] buffer pointer to the buffer where the input data is copied * @param[in] n the maximum amount of data to be transferred * @return The number of bytes transferred. * * @note The function is not atomic, if you need atomic
/*
             LUFA Library
     Copyright (C) Dean Camera, 2017.

  dean [at] fourwalledcubicle [dot] com
           www.lufa-lib.org
*/

/*
  Copyright 2017  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 disclaims 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.
*/

#include <LUFA/Common/Common.h>
#include <LUFA/Drivers/USB/USB.h>