aboutsummaryrefslogtreecommitdiffstats
path: root/tmk_core/protocol/serial.h
blob: 96913c86755fad15b5681fe53d89c194f383a221 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
Copyright 2012 Jun WAKO <wakojun@gmail.com>

This software is licensed with a Modified BSD License.
All of this is supposed to be Free Software, Open Source, DFSG-free,
GPL-compatible, and OK to use in both free and proprietary applications.
Additions and corrections to this file are welcome.


Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright
  notice, this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright
  notice, this list of conditions and the following disclaimer in
  the documentation and/or other materials provided with the
  distribution.

* Neither the name of the copyright holders nor the names of
  contributors may be used to endorse or promote products derived
  from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef SERIAL_H
#define SERIAL_H

/* host role */
void serial_init(void);
uint8_t serial_recv(void);
int16_t serial_recv2(void);
void serial_send(uint8_t data);

#endif
5px 0 5px; } .highlight .hll { background-color: #ffffcc } .highlight { background: #ffffff; } .highlight .c { color: #888888 } /* Comment */ .highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */ .highlight .k { color: #008800; font-weight: bold } /* Keyword */ .highlight .ch { color: #888888 } /* Comment.Hashbang */ .highlight .cm { color: #888888 } /* Comment.Multiline */ .highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */ .highlight .cpf { color: #888888 } /* Comment.PreprocFile */ .highlight .c1 { color: #888888 } /* Comment.Single */ .highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */ .highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */ .highlight .ge { font-style: italic } /* Generic.Emph */ .highlight .gr { color: #aa0000 } /* Generic.Error */ .highlight .gh { color: #333333 } /* Generic.Heading */ .highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */ .highlight .go { color: #888888 } /* Generic.Output */ .highlight .gp { color: #555555 } /* Generic.Prompt */ .highlight .gs { font-weight: bold } /* Generic.Strong */ .highlight .gu { color: #666666 } /* Generic.Subheading */ .highlight .gt { color: #aa0000 } /* Generic.Traceback */ .highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */ .highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */ .highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */ .highlight .kp { color: #008800 } /* Keyword.Pseudo */ .highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */ .highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */ .highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */ .highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */ .highlight .na { color: #336699 } /* Name.Attribute */ .highlight .nb { color: #003388 } /* Name.Builtin */ .highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */ .highlight .no { color: #003366; font-weight: bold } /* Name.Constant */ .highlight .nd { color: #555555 } /* Name.Decorator */ .highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */ .highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */ .highlight .nl { color: #336699; font-style: italic } /* Name.Label */ .highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
/*
    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 <http://www.gnu.org/licenses/>.
*/

/**
 * @file chqueues.c
 * @brief I/O Queues code.
 * @addtogroup IOQueues
 * @{
 */

#include <ch.h>

#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 atomicity it is suggested
 *       to use a semaphore or a mutex for mutual exclusion.
 */
size_t chIQRead(InputQueue *iqp, uint8_t *buffer, size_t n) {
  size_t r = 0;

  while (n--) {
    chSysLock();
    if (chIQIsEmpty(iqp)) {
      chSysUnlock();
      break;
    }
    chSemFastWaitI(&iqp->q_sem);
    *buffer++ = *iqp->q_rdptr++;
    if (iqp->q_rdptr >= iqp->q_top)
      iqp->q_rdptr = iqp->q_buffer;
    chSysUnlock();
    r++;
  }
  if (r && iqp->q_notify) {
    chSysLock();
    iqp->q_notify();
    chSysUnlock();
  }
  return r;
}

/**
 * @brief Initializes an output queue.
 * @details A Semaphore is internally initialized and works as a counter of
 *          the free bytes in the queue.
 *
 * @param[out] oqp pointer to an @p OutputQueue structure
 * @param[in] buffer pointer to a memory area allocated as queue buffer
 * @param[in] size size of the queue buffer
 * @param[in] onotify pointer to a callback function that is invoked when
 *                    some data is written to 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 chOQInit(OutputQueue *oqp, uint8_t *buffer,
              size_t size, qnotify_t onotify) {

  oqp->q_buffer = oqp->q_rdptr = oqp->q_wrptr = buffer;
  oqp->q_top = buffer + size;
  chSemInit(&oqp->q_sem, size);
  oqp->q_notify = onotify;
}

/**
 * @brief Resets an output queue.
 * @details All the data in the output queue is erased and lost, any waiting
 *          thread is resumed with status @p Q_RESET.
 *
 * @param[in] oqp pointer to an @p OutputQueue 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 chOQResetI(OutputQueue *oqp) {

  oqp->q_rdptr = oqp->q_wrptr = oqp->q_buffer;
  chSemResetI(&oqp->q_sem, (cnt_t)(oqp->q_top - oqp->q_buffer));
}

/**
 * @brief Output queue write with timeout.
 * @details This function writes a byte value to an output queue. If the queue
 *          is full then the calling thread is suspended until there is space
 *          in the queue or a timeout occurs.
 *
 * @param[in] oqp pointer to an @p OutputQueue structure
 * @param[in] b the byte value to be written in the queue
 * @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 The operation status:
 * @retval Q_OK if the operation succeeded.
 * @retval Q_TIMEOUT if the specified time expired.
 * @retval Q_RESET if the queue was reset.
 */
msg_t chOQPutTimeout(OutputQueue *oqp, uint8_t b, systime_t timeout) {
  msg_t msg;

  chSysLock();
  if ((msg = chSemWaitTimeoutS(&oqp->q_sem, timeout)) < RDY_OK) {
    chSysUnlock();
    return msg;
  }
  *oqp->q_wrptr++ = b;
  if (oqp->q_wrptr >= oqp->q_top)
    oqp->q_wrptr = oqp->q_buffer;

  if (oqp->q_notify)
    oqp->q_notify();

  chSysUnlock();
  return Q_OK;
}

/**
 * @brief Output queue read.
 * @details A byte value is read from the low end of an output queue.
 *
 * @param[in] oqp pointer to an @p OutputQueue structure
 * @return The byte value from the queue or:
 * @retval Q_EMPTY if the queue is empty.
 */
msg_t chOQGetI(OutputQueue *oqp) {
  uint8_t b;

  if (chOQIsEmpty(oqp))
    return Q_EMPTY;

  b = *oqp->q_rdptr++;
  if (oqp->q_rdptr >= oqp->q_top)
    oqp->q_rdptr = oqp->q_buffer;
  chSemSignalI(&oqp->q_sem);
  return b;
}

/**
 * @brief Non-blocking write.
 * @details The function writes data from a buffer to an output queue. The
 *          transfer is non-blocking and can return zero if the queue is
 *          already full.
 *
 * @param[in] oqp pointer to an @p OutputQueue structure
 * @param[out] buffer pointer to the buffer where the output data is stored
 * @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 atomicity it is suggested
 *       to use a semaphore or a mutex for mutual exclusion.
 */
size_t chOQWrite(OutputQueue *oqp, uint8_t *buffer, size_t n) {

  size_t w = 0;
  while (n--) {
    chSysLock();
    if (chOQIsFull(oqp)) {
      chSysUnlock();
      break;
    }
    chSemFastWaitI(&oqp->q_sem);
    *oqp->q_wrptr++ = *buffer++;
    if (oqp->q_wrptr >= oqp->q_top)
      oqp->q_wrptr = oqp->q_buffer;
    chSysUnlock();
    w++;
  }
  if (w && oqp->q_notify) {
    chSysLock();
    oqp->q_notify();
    chSysUnlock();
  }
  return w;
}
#endif  /* CH_USE_QUEUES */

/** @} */