2 ******************************************************************************
3 * @addtogroup OpenPilotModules OpenPilot Modules
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
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
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"
43 #define MAX_QUEUE_SIZE 200
44 #define STACK_SIZE_BYTES 512
45 #define TASK_PRIORITY (tskIDLE_PRIORITY + 0)
50 static xQueueHandle queue
;
51 static UAVTalkConnection uavTalkCon
;
52 static xTaskHandle overoSyncTaskHandle
;
53 static bool overoEnabled
;
56 static void overoSyncTask(void *parameters
);
57 static int32_t packData(uint8_t *data
, int32_t length
);
58 static void registerObject(UAVObjHandle obj
);
61 extern uint32_t pios_com_overo_id
;
62 extern uint32_t pios_overo_id
;
66 uint32_t sent_objects
;
67 uint32_t failed_objects
;
68 uint32_t received_objects
;
71 struct overosync
*overosync
;
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
84 uint8_t optionalModules
[HWSETTINGS_OPTIONALMODULES_NUMELEM
];
85 HwSettingsOptionalModulesGet(optionalModules
);
87 if (optionalModules
[HWSETTINGS_OPTIONALMODULES_OVERO
] == HWSETTINGS_OPTIONALMODULES_ENABLED
) {
90 // Create object queues
91 queue
= xQueueCreate(MAX_QUEUE_SIZE
, sizeof(UAVObjEvent
));
99 OveroSyncStatsInitialize();
102 // Initialise UAVTalk
103 uavTalkCon
= UAVTalkInitialize(&packData
);
109 * Initialise the overosync module
110 * \return -1 if initialisation failed
111 * \return 0 on success
113 int32_t OveroSyncStart(void)
115 // Check if module is enabled or not
116 if (overoEnabled
== false) {
120 overosync
= (struct overosync
*)pios_malloc(sizeof(*overosync
));
121 if (overosync
== NULL
) {
125 overosync
->sent_bytes
= 0;
127 // Process all registered objects and connect queue for updates
128 UAVObjIterate(®isterObject
);
130 // Start overosync tasks
131 xTaskCreate(overoSyncTask
, (signed char *)"OveroSync", STACK_SIZE_BYTES
/ 4, NULL
, TASK_PRIORITY
, &overoSyncTaskHandle
);
133 PIOS_TASK_MONITOR_RegisterTask(TASKINFO_RUNNING_OVEROSYNC
, overoSyncTaskHandle
);
138 MODULE_INITCALL(OveroSyncInitialize
, OveroSyncStart
);
141 * Register a new object, adds object to local list and connects the queue depending on the object's
142 * telemetry settings.
143 * \param[in] obj Object to connect
145 static void registerObject(UAVObjHandle obj
)
149 eventMask
= EV_UPDATED
| EV_UPDATED_MANUAL
| EV_UPDATE_REQ
;
150 if (UAVObjIsMetaobject(obj
)) {
151 eventMask
|= EV_UNPACKED
; // we also need to act on remote updates (unpack events)
153 UAVObjConnectQueue(obj
, queue
, eventMask
);
157 * Telemetry transmit task, regular priority
159 * Logic: We need to double buffer the DMA transfers. Pack the buffer until either
160 * 1) it is full (and then we should record the number of missed events then)
161 * 2) the current transaction is done (we should immediately schedule since we are slave)
162 * when done packing the buffer we should call PIOS_SPI_TransferBlock, change the active buffer
163 * and then take the semaphrore
165 static void overoSyncTask(__attribute__((unused
)) void *parameters
)
169 // Kick off SPI transfers (once one is completed another will automatically transmit)
170 overosync
->sent_objects
= 0;
171 overosync
->failed_objects
= 0;
172 overosync
->received_objects
= 0;
174 portTickType lastUpdateTime
= xTaskGetTickCount();
175 portTickType updateTime
;
179 // Wait for queue message
180 if (xQueueReceive(queue
, &ev
, portMAX_DELAY
) == pdTRUE
) {
181 // Process event. This calls transmitData
182 UAVTalkSendObjectTimestamped(uavTalkCon
, ev
.obj
, ev
.instId
, false, 0);
184 updateTime
= xTaskGetTickCount();
185 if (((portTickType
)(updateTime
- lastUpdateTime
)) > 1000) {
186 // Update stats. This will trigger a local send event too
187 OveroSyncStatsData syncStats
;
188 syncStats
.Send
= overosync
->sent_bytes
;
189 syncStats
.Connected
= syncStats
.Send
> 500 ? OVEROSYNCSTATS_CONNECTED_TRUE
: OVEROSYNCSTATS_CONNECTED_FALSE
;
190 syncStats
.DroppedUpdates
= overosync
->failed_objects
;
191 syncStats
.Packets
= PIOS_OVERO_GetPacketCount(pios_overo_id
);
192 OveroSyncStatsSet(&syncStats
);
193 overosync
->failed_objects
= 0;
194 overosync
->sent_bytes
= 0;
195 lastUpdateTime
= updateTime
;
198 // TODO: Check the receive buffer
204 * Transmit data buffer to the modem or USB port.
205 * \param[in] data Data buffer to send
206 * \param[in] length Length of buffer
207 * \return -1 on failure
208 * \return number of bytes transmitted on success
210 static int32_t packData(uint8_t *data
, int32_t length
)
212 if (PIOS_COM_SendBufferNonBlocking(pios_com_overo_id
, data
, length
) < 0) {
216 overosync
->sent_bytes
+= length
;
221 overosync
->failed_objects
++;