LP-500 HoTT Telemetry added device definitions
[librepilot.git] / flight / modules / OveroSync / overosync.c
blob38e0be1c3d285ac0622862e48cd0cdd1109d433b
1 /**
2 ******************************************************************************
3 * @addtogroup OpenPilotModules OpenPilot Modules
4 * @{
5 * @addtogroup Overo Sync Module
6 * @brief Overo sync module
7 * Starts a sync tasks that watch event queues
8 * and push to Overo spi port UAVobjects
9 * @{
11 * @file overosync.c
12 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
13 * @brief Telemetry module, handles telemetry and UAVObject updates
14 * @see The GNU Public License (GPL) Version 3
16 *****************************************************************************/
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 3 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful, but
24 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
25 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
26 * for more details.
28 * You should have received a copy of the GNU General Public License along
29 * with this program; if not, write to the Free Software Foundation, Inc.,
30 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33 #include <openpilot.h>
35 #include "overosync.h"
37 #include "hwsettings.h"
38 #include "overosyncstats.h"
39 #include "systemstats.h"
40 #include "taskinfo.h"
42 // Private constants
43 #define MAX_QUEUE_SIZE 200
44 #define STACK_SIZE_BYTES 512
45 #define TASK_PRIORITY (tskIDLE_PRIORITY + 0)
47 // Private types
49 // Private variables
50 static xQueueHandle queue;
51 static UAVTalkConnection uavTalkCon;
52 static xTaskHandle overoSyncTaskHandle;
53 static bool overoEnabled;
55 // Private functions
56 static void overoSyncTask(void *parameters);
57 static int32_t packData(uint8_t *data, int32_t length);
58 static void registerObject(UAVObjHandle obj);
60 // External variables
61 extern uint32_t pios_com_overo_id;
62 extern uint32_t pios_overo_id;
64 struct overosync {
65 uint32_t sent_bytes;
66 uint32_t sent_objects;
67 uint32_t failed_objects;
68 uint32_t received_objects;
71 struct overosync *overosync;
73 /**
74 * Initialise the overosync module
75 * \return -1 if initialisation failed
76 * \return 0 on success
78 int32_t OveroSyncInitialize(void)
80 #ifdef MODULE_OVEROSYNC_BUILTIN
81 overoEnabled = true;
82 #else
84 HwSettingsInitialize();
85 uint8_t optionalModules[HWSETTINGS_OPTIONALMODULES_NUMELEM];
86 HwSettingsOptionalModulesGet(optionalModules);
88 if (optionalModules[HWSETTINGS_OPTIONALMODULES_OVERO] == HWSETTINGS_OPTIONALMODULES_ENABLED) {
89 overoEnabled = true;
91 // Create object queues
92 queue = xQueueCreate(MAX_QUEUE_SIZE, sizeof(UAVObjEvent));
93 } else {
94 overoEnabled = false;
95 return -1;
97 #endif
100 OveroSyncStatsInitialize();
103 // Initialise UAVTalk
104 uavTalkCon = UAVTalkInitialize(&packData);
106 return 0;
110 * Initialise the overosync module
111 * \return -1 if initialisation failed
112 * \return 0 on success
114 int32_t OveroSyncStart(void)
116 // Check if module is enabled or not
117 if (overoEnabled == false) {
118 return -1;
121 overosync = (struct overosync *)pios_malloc(sizeof(*overosync));
122 if (overosync == NULL) {
123 return -1;
126 overosync->sent_bytes = 0;
128 // Process all registered objects and connect queue for updates
129 UAVObjIterate(&registerObject);
131 // Start overosync tasks
132 xTaskCreate(overoSyncTask, (signed char *)"OveroSync", STACK_SIZE_BYTES / 4, NULL, TASK_PRIORITY, &overoSyncTaskHandle);
134 PIOS_TASK_MONITOR_RegisterTask(TASKINFO_RUNNING_OVEROSYNC, overoSyncTaskHandle);
136 return 0;
139 MODULE_INITCALL(OveroSyncInitialize, OveroSyncStart);
142 * Register a new object, adds object to local list and connects the queue depending on the object's
143 * telemetry settings.
144 * \param[in] obj Object to connect
146 static void registerObject(UAVObjHandle obj)
148 int32_t eventMask;
150 eventMask = EV_UPDATED | EV_UPDATED_MANUAL | EV_UPDATE_REQ;
151 if (UAVObjIsMetaobject(obj)) {
152 eventMask |= EV_UNPACKED; // we also need to act on remote updates (unpack events)
154 UAVObjConnectQueue(obj, queue, eventMask);
158 * Telemetry transmit task, regular priority
160 * Logic: We need to double buffer the DMA transfers. Pack the buffer until either
161 * 1) it is full (and then we should record the number of missed events then)
162 * 2) the current transaction is done (we should immediately schedule since we are slave)
163 * when done packing the buffer we should call PIOS_SPI_TransferBlock, change the active buffer
164 * and then take the semaphrore
166 static void overoSyncTask(__attribute__((unused)) void *parameters)
168 UAVObjEvent ev;
170 // Kick off SPI transfers (once one is completed another will automatically transmit)
171 overosync->sent_objects = 0;
172 overosync->failed_objects = 0;
173 overosync->received_objects = 0;
175 portTickType lastUpdateTime = xTaskGetTickCount();
176 portTickType updateTime;
178 // Loop forever
179 while (1) {
180 // Wait for queue message
181 if (xQueueReceive(queue, &ev, portMAX_DELAY) == pdTRUE) {
182 // Process event. This calls transmitData
183 UAVTalkSendObjectTimestamped(uavTalkCon, ev.obj, ev.instId, false, 0);
185 updateTime = xTaskGetTickCount();
186 if (((portTickType)(updateTime - lastUpdateTime)) > 1000) {
187 // Update stats. This will trigger a local send event too
188 OveroSyncStatsData syncStats;
189 syncStats.Send = overosync->sent_bytes;
190 syncStats.Connected = syncStats.Send > 500 ? OVEROSYNCSTATS_CONNECTED_TRUE : OVEROSYNCSTATS_CONNECTED_FALSE;
191 syncStats.DroppedUpdates = overosync->failed_objects;
192 syncStats.Packets = PIOS_OVERO_GetPacketCount(pios_overo_id);
193 OveroSyncStatsSet(&syncStats);
194 overosync->failed_objects = 0;
195 overosync->sent_bytes = 0;
196 lastUpdateTime = updateTime;
199 // TODO: Check the receive buffer
205 * Transmit data buffer to the modem or USB port.
206 * \param[in] data Data buffer to send
207 * \param[in] length Length of buffer
208 * \return -1 on failure
209 * \return number of bytes transmitted on success
211 static int32_t packData(uint8_t *data, int32_t length)
213 if (PIOS_COM_SendBufferNonBlocking(pios_com_overo_id, data, length) < 0) {
214 goto fail;
217 overosync->sent_bytes += length;
219 return length;
221 fail:
222 overosync->failed_objects++;
223 return -1;
227 * @}
228 * @}