2012-10-20 20:48:22 +02:00
|
|
|
/**
|
2013-05-19 16:37:30 +02:00
|
|
|
******************************************************************************
|
|
|
|
* @addtogroup PIOS PIOS Core hardware abstraction layer
|
|
|
|
* @{
|
|
|
|
* @addtogroup PIOS_RFM22B Radio Functions
|
|
|
|
* @brief PIOS COM interface for for the RFM22B radio
|
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* @file pios_rfm22b_com.c
|
|
|
|
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2012.
|
|
|
|
* @brief Implements a driver the the RFM22B driver
|
|
|
|
* @see The GNU Public License (GPL) Version 3
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
/*
|
|
|
|
* 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-10-20 20:48:22 +02: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-10-20 20:48:22 +02: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-10-20 20:48:22 +02:00
|
|
|
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <pios.h>
|
|
|
|
|
2013-03-16 10:48:03 +01:00
|
|
|
#ifdef PIOS_INCLUDE_RFM22B_COM
|
|
|
|
|
2012-10-20 20:48:22 +02:00
|
|
|
#include <pios_rfm22b_priv.h>
|
|
|
|
|
|
|
|
/* Provide a COM driver */
|
2012-10-26 23:25:41 +02:00
|
|
|
static void PIOS_RFM22B_COM_ChangeBaud(uint32_t rfm22b_id, uint32_t baud);
|
|
|
|
static void PIOS_RFM22B_COM_RegisterRxCallback(uint32_t rfm22b_id, pios_com_callback rx_in_cb, uint32_t context);
|
|
|
|
static void PIOS_RFM22B_COM_RegisterTxCallback(uint32_t rfm22b_id, pios_com_callback tx_out_cb, uint32_t context);
|
|
|
|
static void PIOS_RFM22B_COM_TxStart(uint32_t rfm22b_id, uint16_t tx_bytes_avail);
|
|
|
|
static void PIOS_RFM22B_COM_RxStart(uint32_t rfm22b_id, uint16_t rx_bytes_avail);
|
|
|
|
static bool PIOS_RFM22B_COM_Available(uint32_t rfm22b_com_id);
|
2012-10-20 20:48:22 +02:00
|
|
|
|
|
|
|
/* Local variables */
|
|
|
|
const struct pios_com_driver pios_rfm22b_com_driver = {
|
2013-04-13 18:43:50 +02:00
|
|
|
.set_baud = PIOS_RFM22B_COM_ChangeBaud,
|
|
|
|
.tx_start = PIOS_RFM22B_COM_TxStart,
|
|
|
|
.rx_start = PIOS_RFM22B_COM_RxStart,
|
|
|
|
.bind_tx_cb = PIOS_RFM22B_COM_RegisterTxCallback,
|
|
|
|
.bind_rx_cb = PIOS_RFM22B_COM_RegisterRxCallback,
|
|
|
|
.available = PIOS_RFM22B_COM_Available
|
2012-10-20 20:48:22 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Changes the baud rate of the RFM22B peripheral without re-initialising.
|
2013-04-13 18:43:50 +02:00
|
|
|
*
|
|
|
|
* @param[in] rfm22b_id RFM22B name (GPS, TELEM, AUX)
|
|
|
|
* @param[in] baud Requested baud rate
|
2012-10-20 20:48:22 +02:00
|
|
|
*/
|
2012-10-26 23:25:41 +02:00
|
|
|
static void PIOS_RFM22B_COM_ChangeBaud(uint32_t rfm22b_id, uint32_t baud)
|
2012-10-20 20:48:22 +02:00
|
|
|
{
|
2013-04-13 18:43:50 +02:00
|
|
|
struct pios_rfm22b_dev *rfm22b_dev = (struct pios_rfm22b_dev *)rfm22b_id;
|
2013-05-19 16:37:30 +02:00
|
|
|
|
2013-04-13 18:43:50 +02:00
|
|
|
if (!PIOS_RFM22B_Validate(rfm22b_dev)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Set the RF data rate on the modem to ~2X the selected buad rate because the modem is half duplex.
|
|
|
|
enum rfm22b_datarate datarate = RFM22_datarate_64000;
|
2013-05-19 16:37:30 +02:00
|
|
|
if (baud <= 1024) {
|
2013-04-13 18:43:50 +02:00
|
|
|
datarate = RFM22_datarate_500;
|
2013-05-19 16:37:30 +02:00
|
|
|
} else if (baud <= 2048) {
|
2013-04-13 18:43:50 +02:00
|
|
|
datarate = RFM22_datarate_1000;
|
2013-05-19 16:37:30 +02:00
|
|
|
} else if (baud <= 4096) {
|
2013-04-13 18:43:50 +02:00
|
|
|
datarate = RFM22_datarate_8000;
|
2013-05-19 16:37:30 +02:00
|
|
|
} else if (baud <= 9600) {
|
2013-04-13 18:43:50 +02:00
|
|
|
datarate = RFM22_datarate_16000;
|
2013-05-19 16:37:30 +02:00
|
|
|
} else if (baud <= 19200) {
|
2013-04-13 18:43:50 +02:00
|
|
|
datarate = RFM22_datarate_32000;
|
2013-05-19 16:37:30 +02:00
|
|
|
} else if (baud <= 38400) {
|
2013-04-13 18:43:50 +02:00
|
|
|
datarate = RFM22_datarate_57600;
|
2013-05-19 16:37:30 +02:00
|
|
|
} else if (baud <= 57600) {
|
2013-04-13 18:43:50 +02:00
|
|
|
datarate = RFM22_datarate_128000;
|
2013-05-19 16:37:30 +02:00
|
|
|
} else if (baud <= 115200) {
|
2013-04-13 18:43:50 +02:00
|
|
|
datarate = RFM22_datarate_192000;
|
2013-05-19 16:37:30 +02:00
|
|
|
}
|
2013-04-13 18:43:50 +02:00
|
|
|
rfm22b_dev->datarate = datarate;
|
2012-10-20 20:48:22 +02:00
|
|
|
}
|
|
|
|
|
2013-04-13 18:43:50 +02:00
|
|
|
/**
|
|
|
|
* Start a receive from the COM device
|
|
|
|
*
|
|
|
|
* @param[in] rfm22b_dev The device ID.
|
|
|
|
* @param[in] rx_bytes_available The number of bytes available to receive
|
|
|
|
*/
|
2013-05-05 09:02:24 +02:00
|
|
|
static void PIOS_RFM22B_COM_RxStart(__attribute__((unused)) uint32_t rfm22b_id,
|
2013-05-19 16:37:30 +02:00
|
|
|
__attribute__((unused)) uint16_t rx_bytes_avail)
|
|
|
|
{}
|
2012-10-20 20:48:22 +02:00
|
|
|
|
2013-04-13 18:43:50 +02:00
|
|
|
/**
|
|
|
|
* Start a transmit from the COM device
|
|
|
|
*
|
|
|
|
* @param[in] rfm22b_dev The device ID.
|
|
|
|
* @param[in] tx_bytes_available The number of bytes available to transmit
|
|
|
|
*/
|
2013-05-05 09:02:24 +02:00
|
|
|
static void PIOS_RFM22B_COM_TxStart(uint32_t rfm22b_id, __attribute__((unused)) uint16_t tx_bytes_avail)
|
2012-10-20 20:48:22 +02:00
|
|
|
{
|
2013-04-13 18:43:50 +02:00
|
|
|
struct pios_rfm22b_dev *rfm22b_dev = (struct pios_rfm22b_dev *)rfm22b_id;
|
2013-05-19 16:37:30 +02:00
|
|
|
|
2013-04-13 18:43:50 +02:00
|
|
|
if (!PIOS_RFM22B_Validate(rfm22b_dev)) {
|
|
|
|
return;
|
|
|
|
}
|
2012-10-20 20:48:22 +02:00
|
|
|
}
|
|
|
|
|
2013-04-13 18:43:50 +02:00
|
|
|
/**
|
2013-05-19 16:37:30 +02:00
|
|
|
* Register the callback to pass received data to
|
2013-04-13 18:43:50 +02:00
|
|
|
*
|
|
|
|
* @param[in] rfm22b_dev The device ID.
|
|
|
|
* @param[in] rx_in_cb The Rx callback function.
|
|
|
|
* @param[in] context The callback context.
|
|
|
|
*/
|
2012-10-26 23:25:41 +02:00
|
|
|
static void PIOS_RFM22B_COM_RegisterRxCallback(uint32_t rfm22b_id, pios_com_callback rx_in_cb, uint32_t context)
|
2012-10-20 20:48:22 +02:00
|
|
|
{
|
2013-04-13 18:43:50 +02:00
|
|
|
struct pios_rfm22b_dev *rfm22b_dev = (struct pios_rfm22b_dev *)rfm22b_id;
|
2013-05-19 16:37:30 +02:00
|
|
|
|
|
|
|
if (!PIOS_RFM22B_Validate(rfm22b_dev)) {
|
2013-04-13 18:43:50 +02:00
|
|
|
return;
|
2013-05-19 16:37:30 +02:00
|
|
|
}
|
2012-10-20 20:48:22 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
/*
|
2013-04-13 18:43:50 +02:00
|
|
|
* Order is important in these assignments since ISR uses _cb
|
|
|
|
* field to determine if it's ok to dereference _cb and _context
|
|
|
|
*/
|
|
|
|
rfm22b_dev->rx_in_context = context;
|
|
|
|
rfm22b_dev->rx_in_cb = rx_in_cb;
|
2012-10-20 20:48:22 +02:00
|
|
|
}
|
|
|
|
|
2013-04-13 18:43:50 +02:00
|
|
|
/**
|
|
|
|
* Register the callback to get data from.
|
|
|
|
*
|
|
|
|
* @param[in] rfm22b_dev The device ID.
|
|
|
|
* @param[in] rx_in_cb The Tx callback function.
|
|
|
|
* @param[in] context The callback context.
|
|
|
|
*/
|
2012-10-26 23:25:41 +02:00
|
|
|
static void PIOS_RFM22B_COM_RegisterTxCallback(uint32_t rfm22b_id, pios_com_callback tx_out_cb, uint32_t context)
|
2012-10-20 20:48:22 +02:00
|
|
|
{
|
2013-04-13 18:43:50 +02:00
|
|
|
struct pios_rfm22b_dev *rfm22b_dev = (struct pios_rfm22b_dev *)rfm22b_id;
|
2013-05-19 16:37:30 +02:00
|
|
|
|
2013-04-13 18:43:50 +02:00
|
|
|
if (!PIOS_RFM22B_Validate(rfm22b_dev)) {
|
|
|
|
return;
|
|
|
|
}
|
2012-10-20 20:48:22 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
/*
|
2013-04-13 18:43:50 +02:00
|
|
|
* Order is important in these assignments since ISR uses _cb
|
|
|
|
* field to determine if it's ok to dereference _cb and _context
|
|
|
|
*/
|
|
|
|
rfm22b_dev->tx_out_context = context;
|
|
|
|
rfm22b_dev->tx_out_cb = tx_out_cb;
|
2012-10-20 20:48:22 +02:00
|
|
|
}
|
2012-10-26 23:25:41 +02:00
|
|
|
|
2013-04-13 18:43:50 +02:00
|
|
|
/**
|
|
|
|
* See if the COM port is alive
|
|
|
|
*
|
|
|
|
* @param[in] rfm22b_dev The device ID.
|
|
|
|
* @return True of the device is available.
|
|
|
|
*/
|
2012-10-26 23:25:41 +02:00
|
|
|
static bool PIOS_RFM22B_COM_Available(uint32_t rfm22b_id)
|
|
|
|
{
|
2013-04-13 18:43:50 +02:00
|
|
|
return PIOS_RFM22B_LinkStatus(rfm22b_id);
|
2012-10-26 23:25:41 +02:00
|
|
|
}
|
2013-03-16 10:48:03 +01:00
|
|
|
|
|
|
|
#endif /* PIOS_INCLUDE_RFM22B_COM */
|