2010-09-27 09:28:34 +02:00
|
|
|
/**
|
|
|
|
******************************************************************************
|
|
|
|
* @addtogroup OpenPilotModules OpenPilot Modules
|
|
|
|
* @brief The OpenPilot Modules do the majority of the control in OpenPilot. The
|
|
|
|
* @ref SystemModule "System Module" starts all the other modules that then take care
|
|
|
|
* of all the telemetry and control algorithms and such. This is done through the @ref PIOS
|
|
|
|
* "PIOS Hardware abstraction layer" which then contains hardware specific implementations
|
|
|
|
* (currently only STM32 supported)
|
|
|
|
*
|
|
|
|
* @{
|
|
|
|
* @addtogroup SystemModule System Module
|
|
|
|
* @brief Initializes PIOS and other modules runs monitoring
|
|
|
|
* After initializing all the modules (currently selected by Makefile but in
|
|
|
|
* future controlled by configuration on SD card) runs basic monitoring and
|
|
|
|
* alarms.
|
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* @file systemmod.c
|
|
|
|
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
|
|
|
|
* @brief System module
|
|
|
|
*
|
|
|
|
* @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 "systemmod.h"
|
|
|
|
#include "objectpersistence.h"
|
2011-05-03 18:04:44 +02:00
|
|
|
#include "flightstatus.h"
|
2010-09-27 09:28:34 +02:00
|
|
|
#include "systemstats.h"
|
2011-06-18 18:59:02 +02:00
|
|
|
#include "systemsettings.h"
|
2010-12-17 08:01:58 +01:00
|
|
|
#include "i2cstats.h"
|
2011-06-18 18:59:02 +02:00
|
|
|
#include "taskinfo.h"
|
2011-01-10 01:16:30 +01:00
|
|
|
#include "watchdogstatus.h"
|
2011-01-09 00:43:34 +01:00
|
|
|
#include "taskmonitor.h"
|
2010-09-27 09:28:34 +02:00
|
|
|
|
|
|
|
// Private constants
|
|
|
|
#define SYSTEM_UPDATE_PERIOD_MS 1000
|
2011-05-02 09:28:01 +02:00
|
|
|
#define LED_BLINK_RATE_HZ 5
|
2011-05-07 22:07:14 +02:00
|
|
|
|
|
|
|
#ifndef IDLE_COUNTS_PER_SEC_AT_NO_LOAD
|
2010-09-27 09:28:34 +02:00
|
|
|
#define IDLE_COUNTS_PER_SEC_AT_NO_LOAD 995998 // calibrated by running tests/test_cpuload.c
|
|
|
|
// must be updated if the FreeRTOS or compiler
|
|
|
|
// optimisation options are changed.
|
2011-05-07 22:07:14 +02:00
|
|
|
#endif
|
2011-04-30 10:56:52 +02:00
|
|
|
|
2011-05-12 00:35:52 +02:00
|
|
|
#if defined(PIOS_SYSTEM_STACK_SIZE)
|
|
|
|
#define STACK_SIZE_BYTES PIOS_SYSTEM_STACK_SIZE
|
2011-02-01 03:18:35 +01:00
|
|
|
#else
|
2011-01-09 01:03:19 +01:00
|
|
|
#define STACK_SIZE_BYTES 924
|
2011-02-01 03:18:35 +01:00
|
|
|
#endif
|
|
|
|
|
2012-06-11 18:48:05 +02:00
|
|
|
#define TASK_PRIORITY (tskIDLE_PRIORITY+1)
|
2010-09-27 09:28:34 +02:00
|
|
|
|
|
|
|
// Private types
|
|
|
|
|
|
|
|
// Private variables
|
|
|
|
static uint32_t idleCounter;
|
|
|
|
static uint32_t idleCounterClear;
|
|
|
|
static xTaskHandle systemTaskHandle;
|
2012-07-23 06:03:27 +02:00
|
|
|
static xQueueHandle objectPersistenceQueue;
|
2011-08-22 05:14:32 +02:00
|
|
|
static bool stackOverflow;
|
|
|
|
static bool mallocFailed;
|
2010-09-27 09:28:34 +02:00
|
|
|
|
|
|
|
// Private functions
|
|
|
|
static void objectUpdatedCb(UAVObjEvent * ev);
|
|
|
|
static void updateStats();
|
|
|
|
static void updateSystemAlarms();
|
|
|
|
static void systemTask(void *parameters);
|
2011-06-18 18:59:02 +02:00
|
|
|
#if defined(DIAGNOSTICS)
|
|
|
|
static void updateI2Cstats();
|
|
|
|
static void updateWDGstats();
|
|
|
|
#endif
|
2010-09-27 09:28:34 +02:00
|
|
|
/**
|
2011-06-20 07:35:40 +02:00
|
|
|
* Create the module task.
|
|
|
|
* \returns 0 on success or -1 if initialization failed
|
2010-09-27 09:28:34 +02:00
|
|
|
*/
|
2011-06-20 07:35:40 +02:00
|
|
|
int32_t SystemModStart(void)
|
2010-09-27 09:28:34 +02:00
|
|
|
{
|
|
|
|
// Initialize vars
|
2011-08-22 05:14:32 +02:00
|
|
|
stackOverflow = false;
|
|
|
|
mallocFailed = false;
|
2010-09-27 09:28:34 +02:00
|
|
|
// Create system task
|
2011-01-09 01:03:19 +01:00
|
|
|
xTaskCreate(systemTask, (signed char *)"System", STACK_SIZE_BYTES/4, NULL, TASK_PRIORITY, &systemTaskHandle);
|
2011-06-20 07:35:40 +02:00
|
|
|
// Register task
|
|
|
|
TaskMonitorAdd(TASKINFO_RUNNING_SYSTEM, systemTaskHandle);
|
|
|
|
|
2010-09-27 09:28:34 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-06-20 07:35:40 +02:00
|
|
|
* Initialize the module, called on startup.
|
|
|
|
* \returns 0 on success or -1 if initialization failed
|
2010-09-27 09:28:34 +02:00
|
|
|
*/
|
2011-06-20 07:35:40 +02:00
|
|
|
int32_t SystemModInitialize(void)
|
2010-09-27 09:28:34 +02:00
|
|
|
{
|
|
|
|
|
2011-06-18 18:59:02 +02:00
|
|
|
// Must registers objects here for system thread because ObjectManager started in OpenPilotInit
|
|
|
|
SystemSettingsInitialize();
|
|
|
|
SystemStatsInitialize();
|
2011-10-19 15:50:42 +02:00
|
|
|
FlightStatusInitialize();
|
2011-06-18 18:59:02 +02:00
|
|
|
ObjectPersistenceInitialize();
|
2011-12-30 18:49:27 +01:00
|
|
|
#if defined(DIAG_TASKS)
|
2011-06-18 18:59:02 +02:00
|
|
|
TaskInfoInitialize();
|
2011-12-30 18:49:27 +01:00
|
|
|
#endif
|
|
|
|
#if defined(DIAGNOSTICS)
|
2011-06-18 18:59:02 +02:00
|
|
|
I2CStatsInitialize();
|
|
|
|
WatchdogStatusInitialize();
|
|
|
|
#endif
|
|
|
|
|
2012-07-23 06:03:27 +02:00
|
|
|
objectPersistenceQueue = xQueueCreate(1, sizeof(UAVObjEvent));
|
2012-07-23 15:47:43 +02:00
|
|
|
if (objectPersistenceQueue == NULL)
|
|
|
|
return -1;
|
2012-07-23 06:03:27 +02:00
|
|
|
|
2011-06-20 07:35:40 +02:00
|
|
|
SystemModStart();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-07-13 05:44:32 +02:00
|
|
|
MODULE_INITCALL(SystemModInitialize, 0)
|
2010-09-27 09:28:34 +02:00
|
|
|
/**
|
|
|
|
* System task, periodically executes every SYSTEM_UPDATE_PERIOD_MS
|
|
|
|
*/
|
|
|
|
static void systemTask(void *parameters)
|
|
|
|
{
|
2011-06-20 07:35:40 +02:00
|
|
|
/* create all modules thread */
|
2011-12-30 19:26:54 +01:00
|
|
|
MODULE_TASKCREATE_ALL;
|
|
|
|
|
|
|
|
if (mallocFailed) {
|
|
|
|
/* We failed to malloc during task creation,
|
|
|
|
* system behaviour is undefined. Reset and let
|
|
|
|
* the BootFault code recover for us.
|
|
|
|
*/
|
|
|
|
PIOS_SYS_Reset();
|
|
|
|
}
|
|
|
|
|
2012-01-15 23:56:42 +01:00
|
|
|
#if defined(PIOS_INCLUDE_IAP)
|
2011-12-30 19:26:54 +01:00
|
|
|
/* Record a successful boot */
|
|
|
|
PIOS_IAP_WriteBootCount(0);
|
2012-01-15 23:56:42 +01:00
|
|
|
#endif
|
2011-01-09 00:43:34 +01:00
|
|
|
|
2010-09-27 09:28:34 +02:00
|
|
|
// Initialize vars
|
|
|
|
idleCounter = 0;
|
|
|
|
idleCounterClear = 0;
|
|
|
|
|
|
|
|
// Listen for SettingPersistance object updates, connect a callback function
|
2012-07-23 06:03:27 +02:00
|
|
|
ObjectPersistenceConnectQueue(objectPersistenceQueue);
|
2010-09-27 09:28:34 +02:00
|
|
|
|
|
|
|
// Main system loop
|
2011-06-13 05:23:00 +02:00
|
|
|
while (1) {
|
2010-09-27 09:28:34 +02:00
|
|
|
// Update the system statistics
|
|
|
|
updateStats();
|
|
|
|
|
|
|
|
// Update the system alarms
|
|
|
|
updateSystemAlarms();
|
2011-06-18 18:59:02 +02:00
|
|
|
#if defined(DIAGNOSTICS)
|
2010-12-17 08:01:58 +01:00
|
|
|
updateI2Cstats();
|
2011-01-10 01:16:30 +01:00
|
|
|
updateWDGstats();
|
2011-06-18 18:59:02 +02:00
|
|
|
#endif
|
2012-01-16 00:02:38 +01:00
|
|
|
|
|
|
|
#if defined(DIAG_TASKS)
|
2011-01-09 00:43:34 +01:00
|
|
|
// Update the task status object
|
|
|
|
TaskMonitorUpdateAll();
|
2012-01-16 00:02:38 +01:00
|
|
|
#endif
|
2011-01-09 00:43:34 +01:00
|
|
|
|
2010-09-27 09:28:34 +02:00
|
|
|
// Flash the heartbeat LED
|
2012-01-23 00:22:59 +01:00
|
|
|
#if defined(PIOS_LED_HEARTBEAT)
|
|
|
|
PIOS_LED_Toggle(PIOS_LED_HEARTBEAT);
|
|
|
|
#endif /* PIOS_LED_HEARTBEAT */
|
2010-09-27 09:28:34 +02:00
|
|
|
|
|
|
|
// Turn on the error LED if an alarm is set
|
2012-01-23 00:22:59 +01:00
|
|
|
#if defined (PIOS_LED_ALARM)
|
2010-09-27 09:28:34 +02:00
|
|
|
if (AlarmsHasWarnings()) {
|
2012-01-23 00:22:59 +01:00
|
|
|
PIOS_LED_On(PIOS_LED_ALARM);
|
2010-09-27 09:28:34 +02:00
|
|
|
} else {
|
2012-01-23 00:22:59 +01:00
|
|
|
PIOS_LED_Off(PIOS_LED_ALARM);
|
2010-09-27 09:28:34 +02:00
|
|
|
}
|
2012-01-23 00:22:59 +01:00
|
|
|
#endif /* PIOS_LED_ALARM */
|
2010-09-27 09:28:34 +02:00
|
|
|
|
2011-05-03 18:04:44 +02:00
|
|
|
FlightStatusData flightStatus;
|
|
|
|
FlightStatusGet(&flightStatus);
|
2010-10-20 07:31:24 +02:00
|
|
|
|
2012-07-23 06:03:27 +02:00
|
|
|
UAVObjEvent ev;
|
|
|
|
int delayTime = flightStatus.Armed == FLIGHTSTATUS_ARMED_ARMED ?
|
|
|
|
SYSTEM_UPDATE_PERIOD_MS / portTICK_RATE_MS / (LED_BLINK_RATE_HZ * 2) :
|
|
|
|
SYSTEM_UPDATE_PERIOD_MS / portTICK_RATE_MS;
|
|
|
|
|
|
|
|
if(xQueueReceive(objectPersistenceQueue, &ev, delayTime) == pdTRUE) {
|
|
|
|
// If object persistence is updated call the callback
|
|
|
|
objectUpdatedCb(&ev);
|
2010-10-20 07:31:24 +02:00
|
|
|
}
|
2010-09-27 09:28:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function called in response to object updates
|
|
|
|
*/
|
|
|
|
static void objectUpdatedCb(UAVObjEvent * ev)
|
|
|
|
{
|
|
|
|
ObjectPersistenceData objper;
|
|
|
|
UAVObjHandle obj;
|
|
|
|
|
|
|
|
// If the object updated was the ObjectPersistence execute requested action
|
|
|
|
if (ev->obj == ObjectPersistenceHandle()) {
|
|
|
|
// Get object data
|
|
|
|
ObjectPersistenceGet(&objper);
|
|
|
|
|
2012-06-02 17:23:27 +02:00
|
|
|
int retval = 1;
|
2012-07-20 04:51:31 +02:00
|
|
|
FlightStatusData flightStatus;
|
|
|
|
FlightStatusGet(&flightStatus);
|
|
|
|
|
2012-07-20 05:18:36 +02:00
|
|
|
// When this is called because of this method don't do anything
|
|
|
|
if (objper.Operation == OBJECTPERSISTENCE_OPERATION_ERROR ||
|
|
|
|
objper.Operation == OBJECTPERSISTENCE_OPERATION_COMPLETED) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-07-20 04:51:31 +02:00
|
|
|
// Execute action if disarmed
|
|
|
|
if(flightStatus.Armed != FLIGHTSTATUS_ARMED_DISARMED) {
|
|
|
|
retval = -1;
|
|
|
|
} else if (objper.Operation == OBJECTPERSISTENCE_OPERATION_LOAD) {
|
2010-09-27 09:28:34 +02:00
|
|
|
if (objper.Selection == OBJECTPERSISTENCE_SELECTION_SINGLEOBJECT) {
|
|
|
|
// Get selected object
|
|
|
|
obj = UAVObjGetByID(objper.ObjectID);
|
|
|
|
if (obj == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Load selected instance
|
2011-03-28 21:16:50 +02:00
|
|
|
retval = UAVObjLoad(obj, objper.InstanceID);
|
2010-09-27 09:28:34 +02:00
|
|
|
} else if (objper.Selection == OBJECTPERSISTENCE_SELECTION_ALLSETTINGS
|
|
|
|
|| objper.Selection == OBJECTPERSISTENCE_SELECTION_ALLOBJECTS) {
|
2011-03-28 21:16:50 +02:00
|
|
|
retval = UAVObjLoadSettings();
|
2010-09-27 09:28:34 +02:00
|
|
|
} else if (objper.Selection == OBJECTPERSISTENCE_SELECTION_ALLMETAOBJECTS
|
|
|
|
|| objper.Selection == OBJECTPERSISTENCE_SELECTION_ALLOBJECTS) {
|
2011-03-28 21:16:50 +02:00
|
|
|
retval = UAVObjLoadMetaobjects();
|
2010-09-27 09:28:34 +02:00
|
|
|
}
|
|
|
|
} else if (objper.Operation == OBJECTPERSISTENCE_OPERATION_SAVE) {
|
|
|
|
if (objper.Selection == OBJECTPERSISTENCE_SELECTION_SINGLEOBJECT) {
|
|
|
|
// Get selected object
|
|
|
|
obj = UAVObjGetByID(objper.ObjectID);
|
|
|
|
if (obj == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Save selected instance
|
2011-03-28 21:16:50 +02:00
|
|
|
retval = UAVObjSave(obj, objper.InstanceID);
|
2012-06-04 00:26:10 +02:00
|
|
|
|
|
|
|
// Not sure why this is needed
|
|
|
|
vTaskDelay(10);
|
|
|
|
|
2012-06-02 17:23:27 +02:00
|
|
|
// Verify saving worked
|
|
|
|
if (retval == 0)
|
|
|
|
retval = UAVObjLoad(obj, objper.InstanceID);
|
2010-09-27 09:28:34 +02:00
|
|
|
} else if (objper.Selection == OBJECTPERSISTENCE_SELECTION_ALLSETTINGS
|
|
|
|
|| objper.Selection == OBJECTPERSISTENCE_SELECTION_ALLOBJECTS) {
|
2011-03-28 21:16:50 +02:00
|
|
|
retval = UAVObjSaveSettings();
|
2010-09-27 09:28:34 +02:00
|
|
|
} else if (objper.Selection == OBJECTPERSISTENCE_SELECTION_ALLMETAOBJECTS
|
|
|
|
|| objper.Selection == OBJECTPERSISTENCE_SELECTION_ALLOBJECTS) {
|
2011-03-28 21:16:50 +02:00
|
|
|
retval = UAVObjSaveMetaobjects();
|
2010-09-27 09:28:34 +02:00
|
|
|
}
|
|
|
|
} else if (objper.Operation == OBJECTPERSISTENCE_OPERATION_DELETE) {
|
|
|
|
if (objper.Selection == OBJECTPERSISTENCE_SELECTION_SINGLEOBJECT) {
|
|
|
|
// Get selected object
|
|
|
|
obj = UAVObjGetByID(objper.ObjectID);
|
|
|
|
if (obj == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Delete selected instance
|
2011-03-28 21:16:50 +02:00
|
|
|
retval = UAVObjDelete(obj, objper.InstanceID);
|
2010-09-27 09:28:34 +02:00
|
|
|
} else if (objper.Selection == OBJECTPERSISTENCE_SELECTION_ALLSETTINGS
|
|
|
|
|| objper.Selection == OBJECTPERSISTENCE_SELECTION_ALLOBJECTS) {
|
2011-03-28 21:16:50 +02:00
|
|
|
retval = UAVObjDeleteSettings();
|
2010-09-27 09:28:34 +02:00
|
|
|
} else if (objper.Selection == OBJECTPERSISTENCE_SELECTION_ALLMETAOBJECTS
|
|
|
|
|| objper.Selection == OBJECTPERSISTENCE_SELECTION_ALLOBJECTS) {
|
2011-03-28 21:16:50 +02:00
|
|
|
retval = UAVObjDeleteMetaobjects();
|
2010-09-27 09:28:34 +02:00
|
|
|
}
|
2011-08-15 17:24:37 +02:00
|
|
|
} else if (objper.Operation == OBJECTPERSISTENCE_OPERATION_FULLERASE) {
|
|
|
|
retval = -1;
|
|
|
|
#if defined(PIOS_INCLUDE_FLASH_SECTOR_SETTINGS)
|
|
|
|
retval = PIOS_FLASHFS_Format();
|
|
|
|
#endif
|
2010-09-27 09:28:34 +02:00
|
|
|
}
|
2012-06-02 17:23:27 +02:00
|
|
|
switch(retval) {
|
|
|
|
case 0:
|
|
|
|
objper.Operation = OBJECTPERSISTENCE_OPERATION_COMPLETED;
|
|
|
|
ObjectPersistenceSet(&objper);
|
|
|
|
break;
|
|
|
|
case -1:
|
|
|
|
objper.Operation = OBJECTPERSISTENCE_OPERATION_ERROR;
|
|
|
|
ObjectPersistenceSet(&objper);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2011-03-28 21:16:50 +02:00
|
|
|
}
|
2010-09-27 09:28:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-17 08:01:58 +01:00
|
|
|
/**
|
|
|
|
* Called periodically to update the I2C statistics
|
|
|
|
*/
|
2011-06-18 18:59:02 +02:00
|
|
|
#if defined(DIAGNOSTICS)
|
2010-12-17 08:01:58 +01:00
|
|
|
static void updateI2Cstats()
|
|
|
|
{
|
2011-02-20 08:14:02 +01:00
|
|
|
#if defined(PIOS_INCLUDE_I2C)
|
2010-12-17 08:01:58 +01:00
|
|
|
I2CStatsData i2cStats;
|
|
|
|
I2CStatsGet(&i2cStats);
|
|
|
|
|
|
|
|
struct pios_i2c_fault_history history;
|
2010-12-18 08:09:28 +01:00
|
|
|
PIOS_I2C_GetDiagnostics(&history, &i2cStats.event_errors);
|
2010-12-17 08:01:58 +01:00
|
|
|
|
|
|
|
for(uint8_t i = 0; (i < I2C_LOG_DEPTH) && (i < I2CSTATS_EVENT_LOG_NUMELEM); i++) {
|
2011-01-10 06:42:53 +01:00
|
|
|
i2cStats.evirq_log[i] = history.evirq[i];
|
|
|
|
i2cStats.erirq_log[i] = history.erirq[i];
|
2010-12-17 08:01:58 +01:00
|
|
|
i2cStats.event_log[i] = history.event[i];
|
|
|
|
i2cStats.state_log[i] = history.state[i];
|
|
|
|
}
|
|
|
|
i2cStats.last_error_type = history.type;
|
|
|
|
I2CStatsSet(&i2cStats);
|
2011-02-20 08:14:02 +01:00
|
|
|
#endif
|
2010-12-17 08:01:58 +01:00
|
|
|
}
|
|
|
|
|
2011-01-10 01:16:30 +01:00
|
|
|
static void updateWDGstats()
|
|
|
|
{
|
|
|
|
WatchdogStatusData watchdogStatus;
|
|
|
|
watchdogStatus.BootupFlags = PIOS_WDG_GetBootupFlags();
|
|
|
|
watchdogStatus.ActiveFlags = PIOS_WDG_GetActiveFlags();
|
|
|
|
WatchdogStatusSet(&watchdogStatus);
|
|
|
|
}
|
2011-06-18 18:59:02 +02:00
|
|
|
#endif
|
|
|
|
|
2011-01-10 01:16:30 +01:00
|
|
|
|
2011-06-13 05:23:00 +02:00
|
|
|
/**
|
|
|
|
* Called periodically to update the system stats
|
|
|
|
*/
|
|
|
|
static uint16_t GetFreeIrqStackSize(void)
|
|
|
|
{
|
|
|
|
uint32_t i = 0x200;
|
|
|
|
|
|
|
|
#if !defined(ARCH_POSIX) && !defined(ARCH_WIN32) && defined(CHECK_IRQ_STACK)
|
|
|
|
extern uint32_t _irq_stack_top;
|
|
|
|
extern uint32_t _irq_stack_end;
|
|
|
|
uint32_t pattern = 0x0000A5A5;
|
|
|
|
uint32_t *ptr = &_irq_stack_end;
|
|
|
|
|
|
|
|
#if 1 /* the ugly way accurate but takes more time, useful for debugging */
|
|
|
|
uint32_t stack_size = (((uint32_t)&_irq_stack_top - (uint32_t)&_irq_stack_end) & ~3 ) / 4;
|
|
|
|
|
|
|
|
for (i=0; i< stack_size; i++)
|
|
|
|
{
|
|
|
|
if (ptr[i] != pattern)
|
|
|
|
{
|
|
|
|
i=i*4;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else /* faster way but not accurate */
|
|
|
|
if (*(volatile uint32_t *)((uint32_t)ptr + IRQSTACK_LIMIT_CRITICAL) != pattern)
|
|
|
|
{
|
|
|
|
i = IRQSTACK_LIMIT_CRITICAL - 1;
|
|
|
|
}
|
|
|
|
else if (*(volatile uint32_t *)((uint32_t)ptr + IRQSTACK_LIMIT_WARNING) != pattern)
|
|
|
|
{
|
|
|
|
i = IRQSTACK_LIMIT_WARNING - 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
i = IRQSTACK_LIMIT_WARNING;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2010-09-27 09:28:34 +02:00
|
|
|
/**
|
|
|
|
* Called periodically to update the system stats
|
|
|
|
*/
|
|
|
|
static void updateStats()
|
|
|
|
{
|
2011-03-06 12:50:54 +01:00
|
|
|
static portTickType lastTickCount = 0;
|
2010-09-27 09:28:34 +02:00
|
|
|
SystemStatsData stats;
|
|
|
|
|
|
|
|
// Get stats and update
|
|
|
|
SystemStatsGet(&stats);
|
|
|
|
stats.FlightTime = xTaskGetTickCount() * portTICK_RATE_MS;
|
|
|
|
#if defined(ARCH_POSIX) || defined(ARCH_WIN32)
|
|
|
|
// POSIX port of FreeRTOS doesn't have xPortGetFreeHeapSize()
|
|
|
|
stats.HeapRemaining = 10240;
|
|
|
|
#else
|
|
|
|
stats.HeapRemaining = xPortGetFreeHeapSize();
|
|
|
|
#endif
|
2011-02-21 22:50:10 +01:00
|
|
|
|
2011-06-13 05:23:00 +02:00
|
|
|
// Get Irq stack status
|
|
|
|
stats.IRQStackRemaining = GetFreeIrqStackSize();
|
|
|
|
|
2011-02-21 22:50:10 +01:00
|
|
|
// When idleCounterClear was not reset by the idle-task, it means the idle-task did not run
|
|
|
|
if (idleCounterClear) {
|
|
|
|
idleCounter = 0;
|
|
|
|
}
|
2011-03-06 12:50:54 +01:00
|
|
|
|
|
|
|
portTickType now = xTaskGetTickCount();
|
|
|
|
if (now > lastTickCount) {
|
|
|
|
uint32_t dT = (xTaskGetTickCount() - lastTickCount) * portTICK_RATE_MS; // in ms
|
|
|
|
stats.CPULoad =
|
2011-11-26 22:16:16 +01:00
|
|
|
100 - (uint8_t) roundf(100.0f * ((float)idleCounter / ((float)dT / 1000.0f)) / (float)IDLE_COUNTS_PER_SEC_AT_NO_LOAD);
|
2011-03-06 12:50:54 +01:00
|
|
|
} //else: TickCount has wrapped, do not calc now
|
|
|
|
lastTickCount = now;
|
2010-09-27 09:28:34 +02:00
|
|
|
idleCounterClear = 1;
|
2011-01-21 18:18:24 +01:00
|
|
|
|
|
|
|
#if defined(PIOS_INCLUDE_ADC) && defined(PIOS_ADC_USE_TEMP_SENSOR)
|
|
|
|
float temp_voltage = 3.3 * PIOS_ADC_PinGet(0) / ((1 << 12) - 1);
|
|
|
|
const float STM32_TEMP_V25 = 1.43; /* V */
|
|
|
|
const float STM32_TEMP_AVG_SLOPE = 4.3; /* mV/C */
|
|
|
|
stats.CPUTemp = (temp_voltage-STM32_TEMP_V25) * 1000 / STM32_TEMP_AVG_SLOPE + 25;
|
|
|
|
#endif
|
2010-09-27 09:28:34 +02:00
|
|
|
SystemStatsSet(&stats);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update system alarms
|
|
|
|
*/
|
|
|
|
static void updateSystemAlarms()
|
|
|
|
{
|
|
|
|
SystemStatsData stats;
|
|
|
|
UAVObjStats objStats;
|
|
|
|
EventStats evStats;
|
|
|
|
SystemStatsGet(&stats);
|
|
|
|
|
2011-11-07 16:42:04 +01:00
|
|
|
// Check heap, IRQ stack and malloc failures
|
|
|
|
if ( mallocFailed
|
|
|
|
|| (stats.HeapRemaining < HEAP_LIMIT_CRITICAL)
|
2011-06-13 05:23:00 +02:00
|
|
|
#if !defined(ARCH_POSIX) && !defined(ARCH_WIN32) && defined(CHECK_IRQ_STACK)
|
2011-11-07 16:42:04 +01:00
|
|
|
|| (stats.IRQStackRemaining < IRQSTACK_LIMIT_CRITICAL)
|
|
|
|
#endif
|
|
|
|
) {
|
2011-06-13 05:23:00 +02:00
|
|
|
AlarmsSet(SYSTEMALARMS_ALARM_OUTOFMEMORY, SYSTEMALARMS_ALARM_CRITICAL);
|
2011-11-07 16:42:04 +01:00
|
|
|
} else if (
|
|
|
|
(stats.HeapRemaining < HEAP_LIMIT_WARNING)
|
|
|
|
#if !defined(ARCH_POSIX) && !defined(ARCH_WIN32) && defined(CHECK_IRQ_STACK)
|
|
|
|
|| (stats.IRQStackRemaining < IRQSTACK_LIMIT_WARNING)
|
|
|
|
#endif
|
|
|
|
) {
|
2011-06-13 05:23:00 +02:00
|
|
|
AlarmsSet(SYSTEMALARMS_ALARM_OUTOFMEMORY, SYSTEMALARMS_ALARM_WARNING);
|
|
|
|
} else {
|
|
|
|
AlarmsClear(SYSTEMALARMS_ALARM_OUTOFMEMORY);
|
|
|
|
}
|
|
|
|
|
2010-09-27 09:28:34 +02:00
|
|
|
// Check CPU load
|
|
|
|
if (stats.CPULoad > CPULOAD_LIMIT_CRITICAL) {
|
|
|
|
AlarmsSet(SYSTEMALARMS_ALARM_CPUOVERLOAD, SYSTEMALARMS_ALARM_CRITICAL);
|
|
|
|
} else if (stats.CPULoad > CPULOAD_LIMIT_WARNING) {
|
|
|
|
AlarmsSet(SYSTEMALARMS_ALARM_CPUOVERLOAD, SYSTEMALARMS_ALARM_WARNING);
|
|
|
|
} else {
|
|
|
|
AlarmsClear(SYSTEMALARMS_ALARM_CPUOVERLOAD);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for stack overflow
|
2011-08-22 05:14:32 +02:00
|
|
|
if (stackOverflow) {
|
2010-09-27 09:28:34 +02:00
|
|
|
AlarmsSet(SYSTEMALARMS_ALARM_STACKOVERFLOW, SYSTEMALARMS_ALARM_CRITICAL);
|
|
|
|
} else {
|
|
|
|
AlarmsClear(SYSTEMALARMS_ALARM_STACKOVERFLOW);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for event errors
|
|
|
|
UAVObjGetStats(&objStats);
|
|
|
|
EventGetStats(&evStats);
|
|
|
|
UAVObjClearStats();
|
|
|
|
EventClearStats();
|
2012-03-11 00:02:38 +01:00
|
|
|
if (objStats.eventCallbackErrors > 0 || objStats.eventQueueErrors > 0 || evStats.eventErrors > 0) {
|
2010-09-27 09:28:34 +02:00
|
|
|
AlarmsSet(SYSTEMALARMS_ALARM_EVENTSYSTEM, SYSTEMALARMS_ALARM_WARNING);
|
|
|
|
} else {
|
|
|
|
AlarmsClear(SYSTEMALARMS_ALARM_EVENTSYSTEM);
|
|
|
|
}
|
2012-03-11 00:02:38 +01:00
|
|
|
|
|
|
|
if (objStats.lastCallbackErrorID || objStats.lastQueueErrorID || evStats.lastErrorID) {
|
|
|
|
SystemStatsData sysStats;
|
|
|
|
SystemStatsGet(&sysStats);
|
|
|
|
sysStats.EventSystemWarningID = evStats.lastErrorID;
|
|
|
|
sysStats.ObjectManagerCallbackID = objStats.lastCallbackErrorID;
|
|
|
|
sysStats.ObjectManagerQueueID = objStats.lastQueueErrorID;
|
|
|
|
SystemStatsSet(&sysStats);
|
|
|
|
}
|
|
|
|
|
2010-09-27 09:28:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called by the RTOS when the CPU is idle, used to measure the CPU idle time.
|
|
|
|
*/
|
|
|
|
void vApplicationIdleHook(void)
|
|
|
|
{
|
|
|
|
// Called when the scheduler has no tasks to run
|
|
|
|
if (idleCounterClear == 0) {
|
|
|
|
++idleCounter;
|
|
|
|
} else {
|
|
|
|
idleCounter = 0;
|
|
|
|
idleCounterClear = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called by the RTOS when a stack overflow is detected.
|
|
|
|
*/
|
2011-08-22 05:14:32 +02:00
|
|
|
#define DEBUG_STACK_OVERFLOW 0
|
2010-09-27 09:28:34 +02:00
|
|
|
void vApplicationStackOverflowHook(xTaskHandle * pxTask, signed portCHAR * pcTaskName)
|
|
|
|
{
|
2011-08-22 05:14:32 +02:00
|
|
|
stackOverflow = true;
|
|
|
|
#if DEBUG_STACK_OVERFLOW
|
|
|
|
static volatile bool wait_here = true;
|
|
|
|
while(wait_here);
|
|
|
|
wait_here = true;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called by the RTOS when a malloc call fails.
|
|
|
|
*/
|
|
|
|
#define DEBUG_MALLOC_FAILURES 0
|
|
|
|
void vApplicationMallocFailedHook(void)
|
|
|
|
{
|
|
|
|
mallocFailed = true;
|
|
|
|
#if DEBUG_MALLOC_FAILURES
|
|
|
|
static volatile bool wait_here = true;
|
|
|
|
while(wait_here);
|
|
|
|
wait_here = true;
|
|
|
|
#endif
|
2010-09-27 09:28:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @}
|
|
|
|
* @}
|
|
|
|
*/
|