2012-02-02 09:37:48 +01:00
|
|
|
/**
|
|
|
|
******************************************************************************
|
|
|
|
* @addtogroup OpenPilotModules OpenPilot Modules
|
|
|
|
* @{
|
|
|
|
* @addtogroup TelemetryModule Telemetry Module
|
|
|
|
* @brief Main telemetry module
|
|
|
|
* Starts three tasks (RX, TX, and priority TX) that watch event queues
|
|
|
|
* and handle all the telemetry of the UAVobjects
|
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* @file telemetry.c
|
|
|
|
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
|
|
|
|
* @brief Telemetry module, handles telemetry and UAVObject updates
|
|
|
|
* @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
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* for more details.
|
|
|
|
*
|
|
|
|
* 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.,
|
|
|
|
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "openpilot.h"
|
|
|
|
#include "overosync.h"
|
2012-02-06 15:51:48 +01:00
|
|
|
#include "overosyncstats.h"
|
2012-02-02 09:37:48 +01:00
|
|
|
|
|
|
|
// Private constants
|
|
|
|
#define OVEROSYNC_PACKET_SIZE 256
|
2012-02-06 15:21:24 +01:00
|
|
|
#define MAX_QUEUE_SIZE 10
|
2012-02-02 09:37:48 +01:00
|
|
|
#define STACK_SIZE_BYTES 512
|
2012-02-05 03:10:56 +01:00
|
|
|
#define TASK_PRIORITY (tskIDLE_PRIORITY + 0)
|
2012-02-02 09:37:48 +01:00
|
|
|
|
|
|
|
// Private types
|
|
|
|
|
|
|
|
// Private variables
|
|
|
|
static xQueueHandle queue;
|
|
|
|
static UAVTalkConnection uavTalkCon;
|
|
|
|
static xTaskHandle overoSyncTaskHandle;
|
|
|
|
|
|
|
|
// Private functions
|
|
|
|
static void overoSyncTask(void *parameters);
|
2012-02-04 16:41:30 +01:00
|
|
|
static int32_t packData(uint8_t * data, int32_t length);
|
|
|
|
static int32_t transmitData();
|
2012-02-02 09:37:48 +01:00
|
|
|
static void transmitDataDone(bool crc_ok, uint8_t crc_val);
|
|
|
|
static void registerObject(UAVObjHandle obj);
|
|
|
|
|
|
|
|
// External variables
|
|
|
|
extern int32_t pios_spi_overo_id;
|
|
|
|
|
2012-02-04 16:41:30 +01:00
|
|
|
struct dma_transaction {
|
|
|
|
uint8_t tx_buffer[OVEROSYNC_PACKET_SIZE] __attribute__ ((aligned(4)));
|
|
|
|
uint8_t rx_buffer[OVEROSYNC_PACKET_SIZE] __attribute__ ((aligned(4)));
|
|
|
|
};
|
|
|
|
|
|
|
|
struct overosync {
|
|
|
|
struct dma_transaction transactions[2];
|
|
|
|
uint32_t active_transaction_id;
|
|
|
|
uint32_t loading_transaction_id;
|
|
|
|
xSemaphoreHandle transaction_lock;
|
|
|
|
xSemaphoreHandle buffer_lock;
|
2012-02-05 03:10:56 +01:00
|
|
|
volatile bool transaction_done;
|
2012-02-06 15:51:48 +01:00
|
|
|
uint32_t sent_bytes;
|
2012-02-04 16:41:30 +01:00
|
|
|
uint32_t write_pointer;
|
|
|
|
uint32_t sent_objects;
|
|
|
|
uint32_t failed_objects;
|
|
|
|
uint32_t received_objects;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct overosync *overosync;
|
|
|
|
|
2012-02-02 09:37:48 +01:00
|
|
|
/**
|
|
|
|
* Initialise the telemetry module
|
|
|
|
* \return -1 if initialisation failed
|
|
|
|
* \return 0 on success
|
|
|
|
*/
|
|
|
|
int32_t OveroSyncInitialize(void)
|
|
|
|
{
|
2012-02-06 15:51:48 +01:00
|
|
|
if(pios_spi_overo_id == 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
OveroSyncStatsInitialize();
|
|
|
|
|
2012-02-02 09:37:48 +01:00
|
|
|
// Create object queues
|
|
|
|
queue = xQueueCreate(MAX_QUEUE_SIZE, sizeof(UAVObjEvent));
|
|
|
|
|
|
|
|
// Initialise UAVTalk
|
2012-02-04 16:41:30 +01:00
|
|
|
uavTalkCon = UAVTalkInitialize(&packData);
|
2012-02-02 09:37:48 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialise the telemetry module
|
|
|
|
* \return -1 if initialisation failed
|
|
|
|
* \return 0 on success
|
|
|
|
*/
|
|
|
|
int32_t OveroSyncStart(void)
|
|
|
|
{
|
2012-02-06 15:51:48 +01:00
|
|
|
if(pios_spi_overo_id == 0)
|
|
|
|
return -1;
|
|
|
|
|
2012-02-05 03:10:56 +01:00
|
|
|
overosync = (struct overosync *) pvPortMalloc(sizeof(*overosync));
|
2012-02-04 16:41:30 +01:00
|
|
|
if(overosync == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
overosync->transaction_lock = xSemaphoreCreateMutex();
|
|
|
|
if(overosync->transaction_lock == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
overosync->buffer_lock = xSemaphoreCreateMutex();
|
|
|
|
if(overosync->buffer_lock == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
overosync->active_transaction_id = 0;
|
|
|
|
overosync->loading_transaction_id = 0;
|
2012-02-06 15:21:24 +01:00
|
|
|
overosync->write_pointer = 0;
|
2012-02-06 15:51:48 +01:00
|
|
|
overosync->sent_bytes = 0;
|
2012-02-04 16:41:30 +01:00
|
|
|
|
2012-02-02 09:37:48 +01:00
|
|
|
// Process all registered objects and connect queue for updates
|
|
|
|
UAVObjIterate(®isterObject);
|
|
|
|
|
|
|
|
// Start telemetry tasks
|
2012-02-06 15:51:48 +01:00
|
|
|
xTaskCreate(overoSyncTask, (signed char *)"OveroSync", STACK_SIZE_BYTES/4, NULL, TASK_PRIORITY, &overoSyncTaskHandle);
|
2012-02-02 09:37:48 +01:00
|
|
|
|
2012-02-04 16:41:30 +01:00
|
|
|
TaskMonitorAdd(TASKINFO_RUNNING_OVEROSYNC, overoSyncTaskHandle);
|
2012-02-02 09:37:48 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
MODULE_INITCALL(OveroSyncInitialize, OveroSyncStart)
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register a new object, adds object to local list and connects the queue depending on the object's
|
|
|
|
* telemetry settings.
|
|
|
|
* \param[in] obj Object to connect
|
|
|
|
*/
|
|
|
|
static void registerObject(UAVObjHandle obj)
|
|
|
|
{
|
|
|
|
int32_t eventMask;
|
|
|
|
eventMask = EV_UPDATED | EV_UPDATED_MANUAL | EV_UPDATE_REQ;
|
|
|
|
if (UAVObjIsMetaobject(obj)) {
|
|
|
|
eventMask |= EV_UNPACKED; // we also need to act on remote updates (unpack events)
|
|
|
|
}
|
|
|
|
UAVObjConnectQueue(obj, queue, eventMask);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int32_t overosync_transfers = 0;
|
|
|
|
/**
|
|
|
|
* Telemetry transmit task, regular priority
|
2012-02-04 16:41:30 +01:00
|
|
|
*
|
|
|
|
* Logic: We need to double buffer the DMA transfers. Pack the buffer until either
|
|
|
|
* 1) it is full (and then we should record the number of missed events then)
|
|
|
|
* 2) the current transaction is done (we should immediately schedule since we are slave)
|
|
|
|
* when done packing the buffer we should call PIOS_SPI_TransferBlock, change the active buffer
|
|
|
|
* and then take the semaphrore
|
2012-02-02 09:37:48 +01:00
|
|
|
*/
|
|
|
|
static void overoSyncTask(void *parameters)
|
|
|
|
{
|
|
|
|
UAVObjEvent ev;
|
|
|
|
|
2012-02-04 16:41:30 +01:00
|
|
|
// Kick off SPI transfers (once one is completed another will automatically transmit)
|
2012-02-05 03:10:56 +01:00
|
|
|
overosync->transaction_done = true;
|
2012-02-06 15:21:24 +01:00
|
|
|
overosync->sent_objects = 0;
|
|
|
|
overosync->failed_objects = 0;
|
|
|
|
overosync->received_objects = 0;
|
|
|
|
|
|
|
|
transmitData();
|
|
|
|
|
2012-02-06 15:51:48 +01:00
|
|
|
portTickType lastUpdateTime = xTaskGetTickCount();
|
|
|
|
portTickType updateTime;
|
|
|
|
|
2012-02-02 09:37:48 +01:00
|
|
|
// Loop forever
|
|
|
|
while (1) {
|
|
|
|
// Wait for queue message
|
|
|
|
if (xQueueReceive(queue, &ev, portMAX_DELAY) == pdTRUE) {
|
2012-02-04 16:41:30 +01:00
|
|
|
// Process event. This calls transmitData
|
2012-02-02 09:37:48 +01:00
|
|
|
UAVTalkSendObject(uavTalkCon, ev.obj, ev.instId, false, 0);
|
2012-02-05 03:10:56 +01:00
|
|
|
|
2012-02-06 15:21:24 +01:00
|
|
|
//if(overosync->transaction_done)
|
|
|
|
// transmitData();
|
2012-02-05 03:10:56 +01:00
|
|
|
|
2012-02-06 15:51:48 +01:00
|
|
|
updateTime = xTaskGetTickCount();
|
|
|
|
if(((portTickType) (updateTime - lastUpdateTime)) > 1000) {
|
|
|
|
// Update stats. This will trigger a local send event too
|
|
|
|
OveroSyncStatsData syncStats;
|
|
|
|
syncStats.Send = overosync->sent_bytes;
|
|
|
|
syncStats.Connected = syncStats.Send > 500 ? OVEROSYNCSTATS_CONNECTED_TRUE : OVEROSYNCSTATS_CONNECTED_FALSE;
|
|
|
|
OveroSyncStatsSet(&syncStats);
|
|
|
|
overosync->sent_bytes = 0;
|
|
|
|
lastUpdateTime = updateTime;
|
|
|
|
}
|
|
|
|
|
2012-02-02 09:37:48 +01:00
|
|
|
overosync_transfers++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t transactionsDone = 0;
|
|
|
|
static void transmitDataDone(bool crc_ok, uint8_t crc_val)
|
|
|
|
{
|
2012-02-04 16:41:30 +01:00
|
|
|
uint8_t *rx_buffer;
|
2012-02-05 03:10:56 +01:00
|
|
|
static signed portBASE_TYPE xHigherPriorityTaskWoken;
|
|
|
|
|
2012-02-02 09:37:48 +01:00
|
|
|
transactionsDone ++;
|
2012-02-04 16:41:30 +01:00
|
|
|
rx_buffer = overosync->transactions[overosync->active_transaction_id].rx_buffer;
|
|
|
|
|
|
|
|
// Release the semaphore and start another transaction (which grabs semaphore again but then
|
2012-02-05 03:10:56 +01:00
|
|
|
// returns instantly). Because this is called by the DMA ISR we need to be aware of context
|
|
|
|
// switches.
|
|
|
|
xSemaphoreGiveFromISR(overosync->transaction_lock, &xHigherPriorityTaskWoken);
|
|
|
|
portEND_SWITCHING_ISR(xHigherPriorityTaskWoken);
|
|
|
|
|
|
|
|
overosync->transaction_done = true;
|
2012-02-04 16:41:30 +01:00
|
|
|
|
2012-02-06 15:21:24 +01:00
|
|
|
transmitData();
|
|
|
|
|
2012-02-04 16:41:30 +01:00
|
|
|
// Parse the data from overo
|
|
|
|
for (uint32_t i = 0; rx_buffer[0] != 0 && i < sizeof(rx_buffer) ; i++)
|
|
|
|
UAVTalkProcessInputStream(uavTalkCon, rx_buffer[i]);
|
2012-02-02 09:37:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int32_t transactionsStarted = 0;
|
|
|
|
/**
|
|
|
|
* Transmit data buffer to the modem or USB port.
|
|
|
|
* \param[in] data Data buffer to send
|
|
|
|
* \param[in] length Length of buffer
|
|
|
|
* \return -1 on failure
|
|
|
|
* \return number of bytes transmitted on success
|
|
|
|
*/
|
2012-02-04 16:41:30 +01:00
|
|
|
static int32_t packData(uint8_t * data, int32_t length)
|
2012-02-02 09:37:48 +01:00
|
|
|
{
|
2012-02-04 16:41:30 +01:00
|
|
|
uint8_t *tx_buffer;
|
|
|
|
|
|
|
|
// Get the lock for manipulating the buffer
|
|
|
|
xSemaphoreTake(overosync->buffer_lock, portMAX_DELAY);
|
2012-02-05 03:10:56 +01:00
|
|
|
|
2012-02-04 16:41:30 +01:00
|
|
|
// Check this packet will fit
|
|
|
|
if ((overosync->write_pointer + length) > sizeof(overosync->transactions[overosync->loading_transaction_id].tx_buffer)) {
|
|
|
|
overosync->failed_objects ++;
|
|
|
|
xSemaphoreGive(overosync->buffer_lock);
|
|
|
|
return -1;
|
|
|
|
}
|
2012-02-05 03:10:56 +01:00
|
|
|
|
2012-02-04 16:41:30 +01:00
|
|
|
// Get offset into buffer and copy contents
|
|
|
|
tx_buffer = overosync->transactions[overosync->loading_transaction_id].tx_buffer +
|
|
|
|
overosync->write_pointer;
|
|
|
|
memcpy(tx_buffer,data,length);
|
|
|
|
overosync->write_pointer += length;
|
2012-02-06 15:51:48 +01:00
|
|
|
overosync->sent_bytes += length;
|
2012-02-04 16:41:30 +01:00
|
|
|
overosync->sent_objects++;
|
2012-02-02 09:37:48 +01:00
|
|
|
|
2012-02-04 16:41:30 +01:00
|
|
|
xSemaphoreGive(overosync->buffer_lock);
|
2012-02-05 03:10:56 +01:00
|
|
|
|
2012-02-04 16:41:30 +01:00
|
|
|
return length;
|
2012-02-02 09:37:48 +01:00
|
|
|
}
|
|
|
|
|
2012-02-04 16:41:30 +01:00
|
|
|
static int32_t transmitData()
|
|
|
|
{
|
|
|
|
uint8_t *tx_buffer, *rx_buffer;
|
|
|
|
|
|
|
|
// Get lock to manipulate buffers
|
2012-02-06 15:21:24 +01:00
|
|
|
/*if(xSemaphoreTake(overosync->buffer_lock, 1) == pdFALSE)
|
|
|
|
return -1;*/
|
2012-02-05 03:10:56 +01:00
|
|
|
|
|
|
|
overosync->transaction_done = false;
|
2012-02-04 16:41:30 +01:00
|
|
|
|
|
|
|
// Swap buffers
|
|
|
|
overosync->active_transaction_id = overosync->loading_transaction_id;
|
|
|
|
overosync->loading_transaction_id = (overosync->loading_transaction_id + 1) %
|
|
|
|
NELEMENTS(overosync->transactions);
|
|
|
|
|
|
|
|
tx_buffer = overosync->transactions[overosync->active_transaction_id].tx_buffer;
|
|
|
|
rx_buffer = overosync->transactions[overosync->active_transaction_id].rx_buffer;
|
|
|
|
|
|
|
|
// Prepare the new loading buffer
|
|
|
|
memset(overosync->transactions[overosync->loading_transaction_id].tx_buffer, 0xff,
|
|
|
|
sizeof(overosync->transactions[overosync->loading_transaction_id].tx_buffer));
|
|
|
|
overosync->write_pointer = 0;
|
|
|
|
|
2012-02-06 15:21:24 +01:00
|
|
|
//xSemaphoreGive(overosync->buffer_lock);
|
2012-02-04 16:41:30 +01:00
|
|
|
|
|
|
|
xSemaphoreTake(overosync->transaction_lock, portMAX_DELAY);
|
|
|
|
transactionsStarted++;
|
|
|
|
|
|
|
|
return PIOS_SPI_TransferBlock(pios_spi_overo_id, (uint8_t *) tx_buffer, (uint8_t *) rx_buffer, sizeof(overosync->transactions[overosync->active_transaction_id].tx_buffer), &transmitDataDone);
|
|
|
|
}
|
2012-02-02 09:37:48 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @}
|
|
|
|
* @}
|
|
|
|
*/
|