2018-01-05 02:19:51 -04:00
|
|
|
/*
|
|
|
|
* This file 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 file 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, see <http://www.gnu.org/licenses/>.
|
2019-10-20 10:31:12 -03:00
|
|
|
*
|
2018-01-05 02:19:51 -04:00
|
|
|
* Code by Andrew Tridgell and Siddharth Bharat Purohit
|
|
|
|
*/
|
|
|
|
#include <AP_HAL/AP_HAL.h>
|
|
|
|
#include <AP_Math/AP_Math.h>
|
|
|
|
|
|
|
|
#include "Util.h"
|
2018-06-08 13:05:48 -03:00
|
|
|
#include <ch.h>
|
2018-04-14 00:55:03 -03:00
|
|
|
#include "RCOutput.h"
|
2020-12-05 15:16:27 -04:00
|
|
|
#include "UARTDriver.h"
|
2018-06-02 00:27:02 -03:00
|
|
|
#include "hwdef/common/stm32_util.h"
|
2019-04-19 22:25:18 -03:00
|
|
|
#include "hwdef/common/watchdog.h"
|
2018-06-27 02:59:57 -03:00
|
|
|
#include "hwdef/common/flash.h"
|
|
|
|
#include <AP_ROMFS/AP_ROMFS.h>
|
2020-12-30 02:44:19 -04:00
|
|
|
#include <AP_Common/ExpandingString.h>
|
2018-12-27 21:05:06 -04:00
|
|
|
#include "sdcard.h"
|
2020-11-21 12:00:11 -04:00
|
|
|
#include "shared_dma.h"
|
2021-01-16 01:01:04 -04:00
|
|
|
#include <AP_Common/ExpandingString.h>
|
|
|
|
|
|
|
|
#if HAL_ENABLE_SAVE_PERSISTENT_PARAMS
|
|
|
|
#include <AP_InertialSensor/AP_InertialSensor.h>
|
|
|
|
#endif
|
2018-01-05 02:19:51 -04:00
|
|
|
|
|
|
|
#if HAL_WITH_IO_MCU
|
2018-01-05 04:55:01 -04:00
|
|
|
#include <AP_BoardConfig/AP_BoardConfig.h>
|
2018-01-05 02:19:51 -04:00
|
|
|
#include <AP_IOMCU/AP_IOMCU.h>
|
|
|
|
extern AP_IOMCU iomcu;
|
|
|
|
#endif
|
|
|
|
|
2018-01-28 14:31:21 -04:00
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
2018-01-05 02:19:51 -04:00
|
|
|
using namespace ChibiOS;
|
2018-03-06 18:41:03 -04:00
|
|
|
#if CH_CFG_USE_HEAP == TRUE
|
|
|
|
|
2018-01-05 02:19:51 -04:00
|
|
|
/**
|
|
|
|
how much free memory do we have in bytes.
|
|
|
|
*/
|
2018-01-13 00:02:05 -04:00
|
|
|
uint32_t Util::available_memory(void)
|
2018-01-05 02:19:51 -04:00
|
|
|
{
|
2018-01-09 17:18:28 -04:00
|
|
|
// from malloc.c in hwdef
|
|
|
|
return mem_available();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Special Allocation Routines
|
|
|
|
*/
|
|
|
|
|
2018-01-13 00:02:05 -04:00
|
|
|
void* Util::malloc_type(size_t size, AP_HAL::Util::Memory_Type mem_type)
|
2018-01-09 17:18:28 -04:00
|
|
|
{
|
2018-05-31 22:09:05 -03:00
|
|
|
if (mem_type == AP_HAL::Util::MEM_DMA_SAFE) {
|
2018-06-02 00:27:02 -03:00
|
|
|
return malloc_dma(size);
|
|
|
|
} else if (mem_type == AP_HAL::Util::MEM_FAST) {
|
2019-02-17 07:23:34 -04:00
|
|
|
return malloc_fastmem(size);
|
2018-01-09 17:18:28 -04:00
|
|
|
} else {
|
2018-01-16 17:03:26 -04:00
|
|
|
return calloc(1, size);
|
2018-01-09 17:18:28 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-13 00:02:05 -04:00
|
|
|
void Util::free_type(void *ptr, size_t size, AP_HAL::Util::Memory_Type mem_type)
|
2018-01-09 17:18:28 -04:00
|
|
|
{
|
|
|
|
if (ptr != NULL) {
|
2020-11-13 01:10:45 -04:00
|
|
|
free(ptr);
|
2018-01-09 17:18:28 -04:00
|
|
|
}
|
|
|
|
}
|
2018-01-05 02:19:51 -04:00
|
|
|
|
|
|
|
|
2018-12-08 22:34:39 -04:00
|
|
|
#ifdef ENABLE_HEAP
|
|
|
|
|
|
|
|
void *Util::allocate_heap_memory(size_t size)
|
|
|
|
{
|
|
|
|
void *buf = malloc(size);
|
|
|
|
if (buf == nullptr) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
memory_heap_t *heap = (memory_heap_t *)malloc(sizeof(memory_heap_t));
|
|
|
|
if (heap != nullptr) {
|
|
|
|
chHeapObjectInit(heap, buf, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
return heap;
|
|
|
|
}
|
|
|
|
|
2020-03-16 04:22:18 -03:00
|
|
|
/*
|
|
|
|
realloc implementation thanks to wolfssl, used by AP_Scripting
|
|
|
|
*/
|
|
|
|
void *Util::std_realloc(void *addr, size_t size)
|
|
|
|
{
|
|
|
|
if (size == 0) {
|
|
|
|
free(addr);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (addr == nullptr) {
|
|
|
|
return malloc(size);
|
|
|
|
}
|
|
|
|
void *new_mem = malloc(size);
|
|
|
|
if (new_mem != nullptr) {
|
|
|
|
memcpy(new_mem, addr, chHeapGetSize(addr) > size ? size : chHeapGetSize(addr));
|
|
|
|
free(addr);
|
|
|
|
}
|
|
|
|
return new_mem;
|
|
|
|
}
|
|
|
|
|
2018-12-08 22:34:39 -04:00
|
|
|
void *Util::heap_realloc(void *heap, void *ptr, size_t new_size)
|
|
|
|
{
|
|
|
|
if (heap == nullptr) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (new_size == 0) {
|
|
|
|
if (ptr != nullptr) {
|
|
|
|
chHeapFree(ptr);
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (ptr == nullptr) {
|
|
|
|
return chHeapAlloc((memory_heap_t *)heap, new_size);
|
|
|
|
}
|
|
|
|
void *new_mem = chHeapAlloc((memory_heap_t *)heap, new_size);
|
|
|
|
if (new_mem != nullptr) {
|
|
|
|
memcpy(new_mem, ptr, chHeapGetSize(ptr) > new_size ? new_size : chHeapGetSize(ptr));
|
|
|
|
chHeapFree(ptr);
|
|
|
|
}
|
|
|
|
return new_mem;
|
|
|
|
}
|
|
|
|
#endif // ENABLE_HEAP
|
|
|
|
|
2018-03-06 18:41:03 -04:00
|
|
|
#endif // CH_CFG_USE_HEAP
|
|
|
|
|
2018-01-05 02:19:51 -04:00
|
|
|
/*
|
|
|
|
get safety switch state
|
|
|
|
*/
|
2018-01-13 00:02:05 -04:00
|
|
|
Util::safety_state Util::safety_switch_state(void)
|
2018-01-05 02:19:51 -04:00
|
|
|
{
|
2018-04-14 00:55:03 -03:00
|
|
|
#if HAL_USE_PWM == TRUE
|
|
|
|
return ((RCOutput *)hal.rcout)->_safety_switch_state();
|
|
|
|
#else
|
2018-01-05 04:55:01 -04:00
|
|
|
return SAFETY_NONE;
|
2018-04-14 00:55:03 -03:00
|
|
|
#endif
|
2018-01-05 02:19:51 -04:00
|
|
|
}
|
|
|
|
|
2018-02-09 23:55:22 -04:00
|
|
|
#ifdef HAL_PWM_ALARM
|
2018-07-24 22:29:44 -03:00
|
|
|
struct Util::ToneAlarmPwmGroup Util::_toneAlarm_pwm_group = HAL_PWM_ALARM;
|
2018-02-09 23:55:22 -04:00
|
|
|
|
2018-01-28 14:31:21 -04:00
|
|
|
bool Util::toneAlarm_init()
|
|
|
|
{
|
2018-07-24 22:29:44 -03:00
|
|
|
_toneAlarm_pwm_group.pwm_cfg.period = 1000;
|
|
|
|
pwmStart(_toneAlarm_pwm_group.pwm_drv, &_toneAlarm_pwm_group.pwm_cfg);
|
2018-01-28 14:31:21 -04:00
|
|
|
|
2018-07-24 22:29:44 -03:00
|
|
|
return true;
|
2018-07-18 21:38:13 -03:00
|
|
|
}
|
|
|
|
|
2018-07-27 04:12:26 -03:00
|
|
|
void Util::toneAlarm_set_buzzer_tone(float frequency, float volume, uint32_t duration_ms)
|
|
|
|
{
|
2018-07-24 22:29:44 -03:00
|
|
|
if (is_zero(frequency) || is_zero(volume)) {
|
|
|
|
pwmDisableChannel(_toneAlarm_pwm_group.pwm_drv, _toneAlarm_pwm_group.chan);
|
|
|
|
} else {
|
|
|
|
pwmChangePeriod(_toneAlarm_pwm_group.pwm_drv,
|
|
|
|
roundf(_toneAlarm_pwm_group.pwm_cfg.frequency/frequency));
|
2018-01-28 14:31:21 -04:00
|
|
|
|
2018-07-24 22:29:44 -03:00
|
|
|
pwmEnableChannel(_toneAlarm_pwm_group.pwm_drv, _toneAlarm_pwm_group.chan, roundf(volume*_toneAlarm_pwm_group.pwm_cfg.frequency/frequency)/2);
|
2018-01-28 14:31:21 -04:00
|
|
|
}
|
|
|
|
}
|
2018-02-09 23:55:22 -04:00
|
|
|
#endif // HAL_PWM_ALARM
|
2018-03-06 18:41:03 -04:00
|
|
|
|
2018-06-14 02:31:33 -03:00
|
|
|
/*
|
|
|
|
set HW RTC in UTC microseconds
|
|
|
|
*/
|
|
|
|
void Util::set_hw_rtc(uint64_t time_utc_usec)
|
|
|
|
{
|
|
|
|
stm32_set_utc_usec(time_utc_usec);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
get system clock in UTC microseconds
|
|
|
|
*/
|
|
|
|
uint64_t Util::get_hw_rtc() const
|
|
|
|
{
|
|
|
|
return stm32_get_utc_usec();
|
|
|
|
}
|
2018-06-27 02:59:57 -03:00
|
|
|
|
2019-05-26 22:45:30 -03:00
|
|
|
#if !defined(HAL_NO_FLASH_SUPPORT) && !defined(HAL_NO_ROMFS_SUPPORT)
|
2018-08-29 10:16:17 -03:00
|
|
|
|
2020-03-10 22:21:33 -03:00
|
|
|
#if defined(HAL_NO_GCS) || defined(HAL_BOOTLOADER_BUILD)
|
|
|
|
#define Debug(fmt, args ...) do { hal.console->printf(fmt, ## args); } while (0)
|
|
|
|
#else
|
|
|
|
#include <GCS_MAVLink/GCS.h>
|
|
|
|
#define Debug(fmt, args ...) do { gcs().send_text(MAV_SEVERITY_INFO, fmt, ## args); } while (0)
|
|
|
|
#endif
|
|
|
|
|
2019-10-24 23:15:25 -03:00
|
|
|
Util::FlashBootloader Util::flash_bootloader()
|
2018-06-27 02:59:57 -03:00
|
|
|
{
|
|
|
|
uint32_t fw_size;
|
|
|
|
const char *fw_name = "bootloader.bin";
|
|
|
|
|
2019-05-15 01:09:30 -03:00
|
|
|
EXPECT_DELAY_MS(11000);
|
2019-04-20 03:11:41 -03:00
|
|
|
|
2019-10-23 06:54:10 -03:00
|
|
|
const uint8_t *fw = AP_ROMFS::find_decompress(fw_name, fw_size);
|
2018-06-27 02:59:57 -03:00
|
|
|
if (!fw) {
|
2020-03-10 22:21:33 -03:00
|
|
|
Debug("failed to find %s\n", fw_name);
|
2019-10-24 23:15:25 -03:00
|
|
|
return FlashBootloader::NOT_AVAILABLE;
|
2018-06-27 02:59:57 -03:00
|
|
|
}
|
2019-10-25 06:11:59 -03:00
|
|
|
// make sure size is multiple of 32
|
|
|
|
fw_size = (fw_size + 31U) & ~31U;
|
2018-06-27 02:59:57 -03:00
|
|
|
|
2021-01-16 01:01:04 -04:00
|
|
|
bool uptodate = true;
|
2019-03-25 21:21:53 -03:00
|
|
|
const uint32_t addr = hal.flash->getpageaddr(0);
|
2021-01-16 01:01:04 -04:00
|
|
|
|
|
|
|
if (memcmp(fw, (const void*)addr, fw_size) != 0) {
|
|
|
|
uptodate = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if HAL_ENABLE_SAVE_PERSISTENT_PARAMS
|
|
|
|
// see if we should store persistent parameters along with the
|
|
|
|
// bootloader. We only do this on boards using a single sector for
|
|
|
|
// the bootloader. The persistent parameters are stored as text at
|
|
|
|
// the end of the sector
|
|
|
|
const int32_t space_available = hal.flash->getpagesize(0) - int32_t(fw_size);
|
|
|
|
ExpandingString persistent_params {}, old_persistent_params {};
|
|
|
|
if (get_persistent_params(persistent_params) &&
|
|
|
|
space_available >= persistent_params.get_length() &&
|
|
|
|
(!load_persistent_params(old_persistent_params) ||
|
|
|
|
strcmp(persistent_params.get_string(),
|
|
|
|
old_persistent_params.get_string()) != 0)) {
|
|
|
|
// persistent parameters have changed, we will update
|
|
|
|
// bootloader to allow storage of the params
|
|
|
|
uptodate = false;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (uptodate) {
|
2020-03-10 22:21:33 -03:00
|
|
|
Debug("Bootloader up-to-date\n");
|
2019-10-23 06:54:10 -03:00
|
|
|
AP_ROMFS::free(fw);
|
2019-10-24 23:15:25 -03:00
|
|
|
return FlashBootloader::NO_CHANGE;
|
2018-06-27 02:59:57 -03:00
|
|
|
}
|
|
|
|
|
2020-03-10 22:21:33 -03:00
|
|
|
Debug("Erasing\n");
|
2019-10-24 23:15:25 -03:00
|
|
|
uint32_t erased_size = 0;
|
|
|
|
uint8_t erase_page = 0;
|
|
|
|
while (erased_size < fw_size) {
|
2019-10-25 06:11:59 -03:00
|
|
|
uint32_t page_size = hal.flash->getpagesize(erase_page);
|
|
|
|
if (page_size == 0) {
|
2019-10-24 23:15:25 -03:00
|
|
|
AP_ROMFS::free(fw);
|
|
|
|
return FlashBootloader::FAIL;
|
|
|
|
}
|
2019-10-25 06:11:59 -03:00
|
|
|
hal.scheduler->expect_delay_ms(1000);
|
|
|
|
if (!hal.flash->erasepage(erase_page)) {
|
2020-03-10 22:21:33 -03:00
|
|
|
Debug("Erase %u failed\n", erase_page);
|
2019-10-24 23:15:25 -03:00
|
|
|
AP_ROMFS::free(fw);
|
|
|
|
return FlashBootloader::FAIL;
|
|
|
|
}
|
|
|
|
erased_size += page_size;
|
|
|
|
erase_page++;
|
2018-06-27 02:59:57 -03:00
|
|
|
}
|
2019-10-24 23:15:25 -03:00
|
|
|
|
2020-03-10 22:21:33 -03:00
|
|
|
Debug("Flashing %s @%08x\n", fw_name, (unsigned int)addr);
|
2018-06-27 02:59:57 -03:00
|
|
|
const uint8_t max_attempts = 10;
|
2019-10-25 06:11:59 -03:00
|
|
|
hal.flash->keep_unlocked(true);
|
2018-06-27 02:59:57 -03:00
|
|
|
for (uint8_t i=0; i<max_attempts; i++) {
|
2019-10-25 06:11:59 -03:00
|
|
|
hal.scheduler->expect_delay_ms(1000);
|
2019-03-25 21:21:53 -03:00
|
|
|
bool ok = hal.flash->write(addr, fw, fw_size);
|
2019-02-03 21:41:45 -04:00
|
|
|
if (!ok) {
|
2020-03-10 22:21:33 -03:00
|
|
|
Debug("Flash failed! (attempt=%u/%u)\n",
|
2018-06-27 02:59:57 -03:00
|
|
|
i+1,
|
|
|
|
max_attempts);
|
2019-10-25 06:11:59 -03:00
|
|
|
hal.scheduler->delay(100);
|
2018-06-27 02:59:57 -03:00
|
|
|
continue;
|
|
|
|
}
|
2020-03-10 22:21:33 -03:00
|
|
|
Debug("Flash OK\n");
|
2021-01-16 01:01:04 -04:00
|
|
|
#if HAL_ENABLE_SAVE_PERSISTENT_PARAMS
|
|
|
|
if (persistent_params.get_length()) {
|
|
|
|
const uint32_t ofs = hal.flash->getpagesize(0) - persistent_params.get_length();
|
|
|
|
hal.flash->write(addr+ofs, persistent_params.get_string(), persistent_params.get_length());
|
|
|
|
}
|
|
|
|
#endif
|
2019-10-25 06:11:59 -03:00
|
|
|
hal.flash->keep_unlocked(false);
|
2019-10-23 06:54:10 -03:00
|
|
|
AP_ROMFS::free(fw);
|
2019-10-24 23:15:25 -03:00
|
|
|
return FlashBootloader::OK;
|
2018-06-27 02:59:57 -03:00
|
|
|
}
|
|
|
|
|
2019-10-25 06:11:59 -03:00
|
|
|
hal.flash->keep_unlocked(false);
|
2020-03-10 22:21:33 -03:00
|
|
|
Debug("Flash failed after %u attempts\n", max_attempts);
|
2019-10-23 06:54:10 -03:00
|
|
|
AP_ROMFS::free(fw);
|
2019-10-24 23:15:25 -03:00
|
|
|
return FlashBootloader::FAIL;
|
2018-06-27 02:59:57 -03:00
|
|
|
}
|
2019-05-26 22:45:30 -03:00
|
|
|
#endif // !HAL_NO_FLASH_SUPPORT && !HAL_NO_ROMFS_SUPPORT
|
2018-06-27 02:59:57 -03:00
|
|
|
|
2018-06-29 04:54:51 -03:00
|
|
|
/*
|
|
|
|
display system identifer - board type and serial number
|
|
|
|
*/
|
|
|
|
bool Util::get_system_id(char buf[40])
|
|
|
|
{
|
|
|
|
uint8_t serialid[12];
|
|
|
|
char board_name[14];
|
2019-10-20 10:31:12 -03:00
|
|
|
|
2018-06-29 04:54:51 -03:00
|
|
|
memcpy(serialid, (const void *)UDID_START, 12);
|
|
|
|
strncpy(board_name, CHIBIOS_SHORT_BOARD_NAME, 13);
|
|
|
|
board_name[13] = 0;
|
2019-10-20 10:31:12 -03:00
|
|
|
|
2018-07-05 18:53:31 -03:00
|
|
|
// this format is chosen to match the format used by HAL_PX4
|
2018-06-29 04:54:51 -03:00
|
|
|
snprintf(buf, 40, "%s %02X%02X%02X%02X %02X%02X%02X%02X %02X%02X%02X%02X",
|
|
|
|
board_name,
|
2019-10-20 10:31:12 -03:00
|
|
|
(unsigned)serialid[3], (unsigned)serialid[2], (unsigned)serialid[1], (unsigned)serialid[0],
|
|
|
|
(unsigned)serialid[7], (unsigned)serialid[6], (unsigned)serialid[5], (unsigned)serialid[4],
|
2018-07-10 01:41:16 -03:00
|
|
|
(unsigned)serialid[11], (unsigned)serialid[10], (unsigned)serialid[9],(unsigned)serialid[8]);
|
|
|
|
buf[39] = 0;
|
2018-06-29 04:54:51 -03:00
|
|
|
return true;
|
|
|
|
}
|
2018-09-28 12:30:48 -03:00
|
|
|
|
|
|
|
bool Util::get_system_id_unformatted(uint8_t buf[], uint8_t &len)
|
|
|
|
{
|
|
|
|
len = MIN(12, len);
|
|
|
|
memcpy(buf, (const void *)UDID_START, len);
|
|
|
|
return true;
|
|
|
|
}
|
2018-12-27 21:05:06 -04:00
|
|
|
|
2019-04-19 22:25:18 -03:00
|
|
|
// return true if the reason for the reboot was a watchdog reset
|
|
|
|
bool Util::was_watchdog_reset() const
|
|
|
|
{
|
|
|
|
return stm32_was_watchdog_reset();
|
|
|
|
}
|
2020-03-27 17:29:12 -03:00
|
|
|
|
2020-03-29 19:27:34 -03:00
|
|
|
#if CH_DBG_ENABLE_STACK_CHECK == TRUE
|
2020-03-27 17:29:12 -03:00
|
|
|
/*
|
|
|
|
display stack usage as text buffer for @SYS/threads.txt
|
|
|
|
*/
|
2020-12-30 02:44:19 -04:00
|
|
|
void Util::thread_info(ExpandingString &str)
|
2020-03-27 17:29:12 -03:00
|
|
|
{
|
|
|
|
// a header to allow for machine parsers to determine format
|
2020-11-29 18:10:21 -04:00
|
|
|
const uint32_t isr_stack_size = uint32_t((const uint8_t *)&__main_stack_end__ - (const uint8_t *)&__main_stack_base__);
|
2020-12-30 02:44:19 -04:00
|
|
|
str.printf("ThreadsV2\nISR PRI=255 sp=%p STACK=%u/%u\n",
|
|
|
|
&__main_stack_base__,
|
|
|
|
unsigned(stack_free(&__main_stack_base__)),
|
|
|
|
unsigned(isr_stack_size));
|
2020-03-27 17:29:12 -03:00
|
|
|
|
2020-11-29 18:10:21 -04:00
|
|
|
for (thread_t *tp = chRegFirstThread(); tp; tp = chRegNextThread(tp)) {
|
|
|
|
uint32_t total_stack;
|
|
|
|
if (tp->wabase == (void*)&__main_thread_stack_base__) {
|
|
|
|
// main thread has its stack separated from the thread context
|
|
|
|
total_stack = uint32_t((const uint8_t *)&__main_thread_stack_end__ - (const uint8_t *)&__main_thread_stack_base__);
|
|
|
|
} else {
|
|
|
|
// all other threads have their thread context pointer
|
|
|
|
// above the stack top
|
|
|
|
total_stack = uint32_t(tp) - uint32_t(tp->wabase);
|
|
|
|
}
|
2020-12-19 18:43:49 -04:00
|
|
|
#if HAL_ENABLE_THREAD_STATISTICS
|
2020-12-30 02:44:19 -04:00
|
|
|
str.printf("%-13.13s PRI=%3u sp=%p STACK=%4u/%4u MIN=%4u AVG=%4u MAX=%4u\n",
|
2021-02-18 17:52:58 -04:00
|
|
|
tp->name, unsigned(tp->realprio), tp->wabase,
|
2020-12-05 15:16:27 -04:00
|
|
|
unsigned(stack_free(tp->wabase)), unsigned(total_stack), unsigned(RTC2US(STM32_HSECLK, tp->stats.best)),
|
|
|
|
unsigned(RTC2US(STM32_HSECLK, uint32_t(tp->stats.cumulative / uint64_t(tp->stats.n)))),
|
|
|
|
unsigned(RTC2US(STM32_HSECLK, tp->stats.worst)));
|
2020-12-30 02:44:19 -04:00
|
|
|
chTMObjectInit(&tp->stats); // reset counters to zero
|
2020-12-19 18:43:49 -04:00
|
|
|
#else
|
2020-12-30 02:44:19 -04:00
|
|
|
str.printf("%-13.13s PRI=%3u sp=%p STACK=%u/%u\n",
|
2021-02-18 17:52:58 -04:00
|
|
|
tp->name, unsigned(tp->realprio), tp->wabase,
|
2020-12-30 02:44:19 -04:00
|
|
|
unsigned(stack_free(tp->wabase)), unsigned(total_stack));
|
2020-12-19 18:43:49 -04:00
|
|
|
#endif
|
2020-11-29 18:10:21 -04:00
|
|
|
}
|
2020-03-27 17:29:12 -03:00
|
|
|
}
|
2020-03-29 19:27:34 -03:00
|
|
|
#endif // CH_DBG_ENABLE_STACK_CHECK == TRUE
|
|
|
|
|
2020-11-21 12:00:11 -04:00
|
|
|
#if CH_CFG_USE_SEMAPHORES
|
|
|
|
// request information on dma contention
|
2020-12-30 02:44:19 -04:00
|
|
|
void Util::dma_info(ExpandingString &str)
|
|
|
|
{
|
2021-02-23 00:07:53 -04:00
|
|
|
#ifndef HAL_NO_SHARED_DMA
|
2020-12-30 02:44:19 -04:00
|
|
|
ChibiOS::Shared_DMA::dma_info(str);
|
2021-02-23 00:07:53 -04:00
|
|
|
#endif
|
2020-11-21 12:00:11 -04:00
|
|
|
}
|
|
|
|
#endif
|
2021-01-16 01:01:04 -04:00
|
|
|
|
2021-01-22 06:32:30 -04:00
|
|
|
#if CH_CFG_USE_HEAP == TRUE
|
|
|
|
/*
|
|
|
|
return information on heap usage
|
|
|
|
*/
|
|
|
|
void Util::mem_info(ExpandingString &str)
|
|
|
|
{
|
|
|
|
memory_heap_t *heaps;
|
|
|
|
const struct memory_region *regions;
|
|
|
|
uint8_t num_heaps = malloc_get_heaps(&heaps, ®ions);
|
|
|
|
|
|
|
|
str.printf("MemInfoV1\n");
|
|
|
|
for (uint8_t i=0; i<num_heaps; i++) {
|
|
|
|
size_t totalp=0, largest=0;
|
|
|
|
// get memory available on main heap
|
|
|
|
chHeapStatus(i == 0 ? nullptr : &heaps[i], &totalp, &largest);
|
|
|
|
str.printf("START=0x%08x LEN=%3uk FREE=%6u LRG=%6u TYPE=%1u\n",
|
|
|
|
unsigned(regions[i].address), unsigned(regions[i].size/1024),
|
|
|
|
unsigned(totalp), unsigned(largest), unsigned(regions[i].flags));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-01-16 01:01:04 -04:00
|
|
|
#if HAL_ENABLE_SAVE_PERSISTENT_PARAMS
|
|
|
|
|
|
|
|
static const char *persistent_header = "{{PERSISTENT_START_V1}}\n";
|
|
|
|
|
|
|
|
/*
|
|
|
|
create a set of persistent parameters in string form
|
|
|
|
*/
|
|
|
|
bool Util::get_persistent_params(ExpandingString &str) const
|
|
|
|
{
|
|
|
|
str.printf("%s", persistent_header);
|
|
|
|
#if HAL_INS_TEMPERATURE_CAL_ENABLE
|
|
|
|
const auto *ins = AP_InertialSensor::get_singleton();
|
|
|
|
if (ins) {
|
|
|
|
ins->get_persistent_params(str);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (str.has_failed_allocation() || str.get_length() <= strlen(persistent_header)) {
|
|
|
|
// no data
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// ensure that the length is a multiple of 32 to meet flash alignment requirements
|
|
|
|
while (!str.has_failed_allocation() && str.get_length() % 32 != 0) {
|
2021-01-16 18:22:29 -04:00
|
|
|
str.append(" ", 1);
|
2021-01-16 01:01:04 -04:00
|
|
|
}
|
|
|
|
return !str.has_failed_allocation();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
load a set of persistent parameters in string form from the bootloader sector
|
|
|
|
*/
|
|
|
|
bool Util::load_persistent_params(ExpandingString &str) const
|
|
|
|
{
|
|
|
|
const uint32_t addr = hal.flash->getpageaddr(0);
|
|
|
|
const uint32_t size = hal.flash->getpagesize(0);
|
|
|
|
const char *s = (const char *)memmem((void*)addr, size,
|
|
|
|
persistent_header,
|
|
|
|
strlen(persistent_header));
|
|
|
|
if (s) {
|
|
|
|
str.append(s, (addr+size) - uint32_t(s));
|
|
|
|
return !str.has_failed_allocation();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
apply persistent parameters from the bootloader sector to AP_Param
|
|
|
|
*/
|
|
|
|
void Util::apply_persistent_params(void) const
|
|
|
|
{
|
|
|
|
ExpandingString str {};
|
|
|
|
if (!load_persistent_params(str)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
char *s = str.get_writeable_string();
|
|
|
|
char *saveptr;
|
|
|
|
s += strlen(persistent_header);
|
|
|
|
uint32_t count = 0;
|
2021-01-21 16:14:33 -04:00
|
|
|
uint32_t errors = 0;
|
2021-01-16 01:01:04 -04:00
|
|
|
for (char *p = strtok_r(s, "\n", &saveptr);
|
|
|
|
p; p = strtok_r(nullptr, "\n", &saveptr)) {
|
|
|
|
char *eq = strchr(p, int('='));
|
|
|
|
if (eq) {
|
|
|
|
*eq = 0;
|
2021-01-21 16:14:33 -04:00
|
|
|
const char *pname = p;
|
|
|
|
const float value = atof(eq+1);
|
|
|
|
if (AP_Param::set_default_by_name(pname, value)) {
|
2021-01-16 01:01:04 -04:00
|
|
|
count++;
|
2021-01-21 16:14:33 -04:00
|
|
|
/*
|
|
|
|
we now have a special case for INS_ACC*_ID. To
|
|
|
|
support factory accelerometer calibration we need to
|
|
|
|
do a save() on the ID parameters if they are not
|
|
|
|
already in storage. This is needed as
|
|
|
|
AP_InertialSensor determines if a calibration has
|
|
|
|
been done by whether the IDs are configured in
|
|
|
|
storage
|
|
|
|
*/
|
|
|
|
if (strncmp(pname, "INS_ACC", 7) == 0 &&
|
|
|
|
strcmp(pname+strlen(pname)-3, "_ID") == 0) {
|
|
|
|
enum ap_var_type ptype;
|
|
|
|
AP_Int32 *ap = (AP_Int32 *)AP_Param::find(pname, &ptype);
|
|
|
|
if (ap && ptype == AP_PARAM_INT32) {
|
|
|
|
if (ap->get() != int32_t(value)) {
|
|
|
|
// the accelerometer ID has changed since
|
|
|
|
// this persistent data was saved. Stop
|
|
|
|
// loading persistent parameters as it is
|
|
|
|
// no longer valid for this board. This
|
|
|
|
// can happen if the user has set
|
|
|
|
// parameters to prevent loading of
|
|
|
|
// specific IMU drivers, or if they have
|
|
|
|
// setup an external IMU
|
|
|
|
errors++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!ap->configured_in_storage()) {
|
|
|
|
ap->save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-16 01:01:04 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (count) {
|
|
|
|
AP_Param::invalidate_count();
|
2021-01-21 16:14:33 -04:00
|
|
|
GCS_SEND_TEXT(MAV_SEVERITY_INFO, "Loaded %u persistent parameters (%u errors)",
|
|
|
|
unsigned(count), unsigned(errors));
|
2021-01-16 01:01:04 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif // HAL_ENABLE_SAVE_PERSISTENT_PARAMS
|
2020-12-05 15:16:27 -04:00
|
|
|
|
|
|
|
// request information on uart I/O
|
|
|
|
void Util::uart_info(ExpandingString &str)
|
|
|
|
{
|
|
|
|
#if !defined(HAL_NO_UARTDRIVER)
|
|
|
|
ChibiOS::UARTDriver::uart_info(str);
|
|
|
|
#endif
|
|
|
|
}
|