2012-01-13 01:53:52 +01:00
|
|
|
/**
|
|
|
|
******************************************************************************
|
|
|
|
* @addtogroup PIOS PIOS Core hardware abstraction layer
|
|
|
|
* @{
|
|
|
|
* @addtogroup PIOS_COM COM MSG layer functions
|
|
|
|
* @brief Hardware communication layer
|
|
|
|
* @{
|
|
|
|
*
|
2013-05-19 16:37:30 +02:00
|
|
|
* @file pios_com_msg.c
|
2012-01-13 01:53:52 +01:00
|
|
|
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
|
|
|
|
* @brief COM MSG layer functions
|
|
|
|
* @see The GNU Public License (GPL) Version 3
|
2013-05-19 16:37:30 +02:00
|
|
|
*
|
2012-01-13 01:53:52 +01:00
|
|
|
*****************************************************************************/
|
2013-05-19 16:37:30 +02:00
|
|
|
/*
|
|
|
|
* This program 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
|
2012-01-13 01:53:52 +01:00
|
|
|
* (at your option) any later version.
|
2013-05-19 16:37:30 +02:00
|
|
|
*
|
|
|
|
* This program 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
|
2012-01-13 01:53:52 +01:00
|
|
|
* for more details.
|
2013-05-19 16:37:30 +02:00
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
2012-01-13 01:53:52 +01:00
|
|
|
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "pios.h"
|
|
|
|
|
2013-03-15 15:28:53 +01:00
|
|
|
#ifdef PIOS_INCLUDE_COM_MSG
|
2012-01-13 01:53:52 +01:00
|
|
|
|
|
|
|
#include "pios_com.h"
|
|
|
|
|
|
|
|
#define PIOS_COM_MSG_MAX_LEN 63
|
|
|
|
|
|
|
|
struct pios_com_msg_dev {
|
2013-05-19 16:37:30 +02:00
|
|
|
uint32_t lower_id;
|
|
|
|
const struct pios_com_driver *driver;
|
2012-01-13 01:53:52 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
uint8_t rx_msg_buffer[PIOS_COM_MSG_MAX_LEN];
|
|
|
|
volatile bool rx_msg_full;
|
2012-01-13 01:53:52 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
uint8_t tx_msg_buffer[PIOS_COM_MSG_MAX_LEN];
|
|
|
|
volatile bool tx_msg_full;
|
2012-01-13 01:53:52 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct pios_com_msg_dev com_msg_dev;
|
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
static uint16_t PIOS_COM_MSG_TxOutCallback(uint32_t context, uint8_t *buf, uint16_t buf_len, uint16_t *headroom, bool *need_yield);
|
|
|
|
static uint16_t PIOS_COM_MSG_RxInCallback(uint32_t context, uint8_t *buf, uint16_t buf_len, uint16_t *headroom, bool *need_yield);
|
2012-01-13 01:53:52 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
int32_t PIOS_COM_MSG_Init(uint32_t *com_id, const struct pios_com_driver *driver, uint32_t lower_id)
|
2012-01-13 01:53:52 +01:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_Assert(com_id);
|
|
|
|
PIOS_Assert(driver);
|
2012-01-13 01:53:52 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_Assert(driver->bind_tx_cb);
|
|
|
|
PIOS_Assert(driver->bind_rx_cb);
|
2012-01-13 01:53:52 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
struct pios_com_msg_dev *com_dev = &com_msg_dev;
|
2012-01-13 01:53:52 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
com_dev->driver = driver;
|
|
|
|
com_dev->lower_id = lower_id;
|
2012-01-13 01:53:52 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
com_dev->rx_msg_full = false;
|
|
|
|
(com_dev->driver->bind_rx_cb)(lower_id, PIOS_COM_MSG_RxInCallback, (uint32_t)com_dev);
|
|
|
|
(com_dev->driver->rx_start)(com_dev->lower_id, sizeof(com_dev->rx_msg_buffer));
|
2012-01-13 01:53:52 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
com_dev->tx_msg_full = false;
|
|
|
|
(com_dev->driver->bind_tx_cb)(lower_id, PIOS_COM_MSG_TxOutCallback, (uint32_t)com_dev);
|
2012-01-13 01:53:52 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
*com_id = (uint32_t)com_dev;
|
|
|
|
return 0;
|
2012-01-13 01:53:52 +01:00
|
|
|
}
|
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
static uint16_t PIOS_COM_MSG_TxOutCallback(uint32_t context, uint8_t *buf, uint16_t buf_len, uint16_t *headroom, __attribute__((unused)) bool *need_yield)
|
2012-01-13 01:53:52 +01:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
struct pios_com_msg_dev *com_dev = (struct pios_com_msg_dev *)context;
|
2012-01-13 01:53:52 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_Assert(buf);
|
|
|
|
PIOS_Assert(buf_len);
|
2012-01-13 01:53:52 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
uint16_t bytes_from_fifo = 0;
|
2012-01-13 01:53:52 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
if (com_dev->tx_msg_full && (buf_len >= sizeof(com_dev->tx_msg_buffer))) {
|
|
|
|
/* Room for an entire message, send it */
|
|
|
|
memcpy(buf, com_dev->tx_msg_buffer, sizeof(com_dev->tx_msg_buffer));
|
|
|
|
bytes_from_fifo = sizeof(com_dev->tx_msg_buffer);
|
|
|
|
com_dev->tx_msg_full = false;
|
|
|
|
}
|
2012-01-13 01:53:52 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
if (headroom) {
|
|
|
|
if (com_dev->tx_msg_full) {
|
|
|
|
*headroom = sizeof(com_dev->tx_msg_buffer);
|
|
|
|
} else {
|
|
|
|
*headroom = 0;
|
|
|
|
}
|
|
|
|
}
|
2012-01-13 01:53:52 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
return bytes_from_fifo;
|
2012-01-13 01:53:52 +01:00
|
|
|
}
|
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
static uint16_t PIOS_COM_MSG_RxInCallback(uint32_t context, uint8_t *buf, uint16_t buf_len, uint16_t *headroom, __attribute__((unused)) bool *need_yield)
|
2012-01-13 01:53:52 +01:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
struct pios_com_msg_dev *com_dev = (struct pios_com_msg_dev *)context;
|
2012-01-13 01:53:52 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
uint16_t bytes_into_fifo = 0;
|
2012-01-13 01:53:52 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
if (!com_dev->rx_msg_full && (buf_len >= sizeof(com_dev->rx_msg_buffer))) {
|
|
|
|
memcpy(com_dev->rx_msg_buffer, buf, sizeof(com_dev->rx_msg_buffer));
|
|
|
|
bytes_into_fifo = sizeof(com_dev->rx_msg_buffer);
|
|
|
|
com_dev->rx_msg_full = true;
|
|
|
|
}
|
2012-01-13 01:53:52 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
if (headroom) {
|
|
|
|
if (!com_dev->rx_msg_full) {
|
|
|
|
*headroom = sizeof(com_dev->rx_msg_buffer);
|
|
|
|
} else {
|
|
|
|
*headroom = 0;
|
|
|
|
}
|
|
|
|
}
|
2012-01-13 01:53:52 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
return bytes_into_fifo;
|
2012-01-13 01:53:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int32_t PIOS_COM_MSG_Send(uint32_t com_id, const uint8_t *msg, uint16_t msg_len)
|
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_Assert(msg);
|
|
|
|
PIOS_Assert(msg_len);
|
2012-01-13 01:53:52 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
struct pios_com_msg_dev *com_dev = (struct pios_com_msg_dev *)com_id;
|
2012-01-13 01:53:52 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_Assert(msg_len == sizeof(com_dev->tx_msg_buffer));
|
2012-01-13 01:53:52 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
/* Wait forever for room in the tx buffer */
|
|
|
|
while (com_dev->tx_msg_full) {
|
|
|
|
/* Kick the transmitter while we wait */
|
|
|
|
if (com_dev->driver->tx_start) {
|
|
|
|
(com_dev->driver->tx_start)(com_dev->lower_id, sizeof(com_dev->tx_msg_buffer));
|
|
|
|
}
|
|
|
|
}
|
2012-01-13 01:53:52 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
memcpy((void *)com_dev->tx_msg_buffer, msg, msg_len);
|
|
|
|
com_dev->tx_msg_full = true;
|
2012-01-13 01:53:52 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
/* Kick the transmitter now that we've queued our message */
|
|
|
|
if (com_dev->driver->tx_start) {
|
|
|
|
(com_dev->driver->tx_start)(com_dev->lower_id, sizeof(com_dev->tx_msg_buffer));
|
|
|
|
}
|
2012-01-13 01:53:52 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
return 0;
|
2012-01-13 01:53:52 +01:00
|
|
|
}
|
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
uint16_t PIOS_COM_MSG_Receive(uint32_t com_id, uint8_t *msg, uint16_t msg_len)
|
2012-01-13 01:53:52 +01:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_Assert(msg);
|
|
|
|
PIOS_Assert(msg_len);
|
2012-01-13 01:53:52 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
struct pios_com_msg_dev *com_dev = (struct pios_com_msg_dev *)com_id;
|
2012-01-13 01:53:52 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_Assert(msg_len == sizeof(com_dev->rx_msg_buffer));
|
2012-01-13 01:53:52 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
if (!com_dev->rx_msg_full) {
|
|
|
|
/* There's room in our buffer, kick the receiver */
|
|
|
|
(com_dev->driver->rx_start)(com_dev->lower_id, sizeof(com_dev->rx_msg_buffer));
|
|
|
|
} else {
|
|
|
|
memcpy(msg, com_dev->rx_msg_buffer, msg_len);
|
|
|
|
com_dev->rx_msg_full = false;
|
2012-01-13 01:53:52 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
return msg_len;
|
|
|
|
}
|
2012-01-13 01:53:52 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
return 0;
|
2012-01-13 01:53:52 +01:00
|
|
|
}
|
|
|
|
|
2014-09-07 21:51:11 +02:00
|
|
|
/**
|
|
|
|
* Change the port speed without re-initializing
|
|
|
|
* \param[in] port COM port
|
|
|
|
* \param[in] baud Requested baud rate
|
|
|
|
* \return -1 if port not available
|
|
|
|
* \return 0 on success
|
|
|
|
*/
|
|
|
|
int32_t PIOS_COM_MSG_ChangeBaud(uint32_t com_id, uint32_t baud)
|
|
|
|
{
|
|
|
|
struct pios_com_msg_dev *com_dev = (struct pios_com_msg_dev *)com_id;
|
|
|
|
|
|
|
|
if (!com_dev) {
|
|
|
|
/* Undefined COM port for this board (see pios_board.c) */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Invoke the driver function if it exists */
|
|
|
|
if (com_dev->driver->set_baud) {
|
|
|
|
com_dev->driver->set_baud(com_dev->lower_id, baud);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
#endif /* PIOS_INCLUDE_COM_MSG */
|
2012-01-13 01:53:52 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @}
|
|
|
|
* @}
|
|
|
|
*/
|