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/>.
|
|
|
|
*
|
|
|
|
* 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"
|
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>
|
2018-12-27 21:05:06 -04:00
|
|
|
#include "sdcard.h"
|
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) {
|
|
|
|
chHeapFree(ptr);
|
|
|
|
}
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-01-13 00:02:05 -04:00
|
|
|
void Util::set_imu_temp(float current)
|
2018-01-05 02:19:51 -04:00
|
|
|
{
|
2019-02-21 17:54:42 -04:00
|
|
|
#if HAL_HAVE_IMU_HEATER
|
|
|
|
if (!heater.target || *heater.target == -1) {
|
2018-01-05 02:19:51 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// average over temperatures to remove noise
|
|
|
|
heater.count++;
|
|
|
|
heater.sum += current;
|
|
|
|
|
|
|
|
// update once a second
|
|
|
|
uint32_t now = AP_HAL::millis();
|
|
|
|
if (now - heater.last_update_ms < 1000) {
|
2019-02-21 17:54:42 -04:00
|
|
|
#if defined(HAL_HEATER_GPIO_PIN)
|
|
|
|
// output as duty cycle to local pin
|
|
|
|
hal.gpio->write(HAL_HEATER_GPIO_PIN, heater.duty_counter < heater.output);
|
|
|
|
heater.duty_counter = (heater.duty_counter+1) % 100;
|
|
|
|
#endif
|
2018-01-05 02:19:51 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
heater.last_update_ms = now;
|
|
|
|
|
|
|
|
current = heater.sum / heater.count;
|
|
|
|
heater.sum = 0;
|
|
|
|
heater.count = 0;
|
|
|
|
|
|
|
|
// experimentally tweaked for Pixhawk2
|
|
|
|
const float kI = 0.3f;
|
|
|
|
const float kP = 200.0f;
|
|
|
|
float target = (float)(*heater.target);
|
|
|
|
|
|
|
|
// limit to 65 degrees to prevent damage
|
|
|
|
target = constrain_float(target, 0, 65);
|
|
|
|
|
|
|
|
float err = target - current;
|
|
|
|
|
|
|
|
heater.integrator += kI * err;
|
|
|
|
heater.integrator = constrain_float(heater.integrator, 0, 70);
|
|
|
|
|
2019-02-21 17:54:42 -04:00
|
|
|
heater.output = constrain_float(kP * err + heater.integrator, 0, 100);
|
2018-01-05 02:19:51 -04:00
|
|
|
|
2019-02-21 17:54:42 -04:00
|
|
|
//hal.console->printf("integrator %.1f out=%.1f temp=%.2f err=%.2f\n", heater.integrator, heater.output, current, err);
|
2018-01-05 02:19:51 -04:00
|
|
|
|
2019-02-21 17:54:42 -04:00
|
|
|
#if HAL_WITH_IO_MCU
|
|
|
|
if (AP_BoardConfig::io_enabled()) {
|
|
|
|
// tell IOMCU to setup heater
|
|
|
|
iomcu.set_heater_duty_cycle(heater.output);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif // HAL_HAVE_IMU_HEATER
|
2018-01-05 02:19:51 -04:00
|
|
|
}
|
|
|
|
|
2018-01-13 00:02:05 -04:00
|
|
|
void Util::set_imu_target_temp(int8_t *target)
|
2018-01-05 02:19:51 -04:00
|
|
|
{
|
2019-02-21 17:54:42 -04:00
|
|
|
#if HAL_HAVE_IMU_HEATER
|
2018-01-05 02:19:51 -04:00
|
|
|
heater.target = target;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2018-08-29 10:16:17 -03:00
|
|
|
#ifndef HAL_NO_FLASH_SUPPORT
|
|
|
|
|
2018-06-27 02:59:57 -03:00
|
|
|
bool Util::flash_bootloader()
|
|
|
|
{
|
|
|
|
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
|
|
|
|
2018-07-09 03:47:09 -03:00
|
|
|
uint8_t *fw = AP_ROMFS::find_decompress(fw_name, fw_size);
|
2018-06-27 02:59:57 -03:00
|
|
|
if (!fw) {
|
|
|
|
hal.console->printf("failed to find %s\n", fw_name);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-03-25 21:21:53 -03:00
|
|
|
const uint32_t addr = hal.flash->getpageaddr(0);
|
2018-06-27 02:59:57 -03:00
|
|
|
if (!memcmp(fw, (const void*)addr, fw_size)) {
|
|
|
|
hal.console->printf("Bootloader up-to-date\n");
|
2018-07-09 03:47:09 -03:00
|
|
|
free(fw);
|
2018-06-27 02:59:57 -03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
hal.console->printf("Erasing\n");
|
2019-03-25 21:21:53 -03:00
|
|
|
if (!hal.flash->erasepage(0)) {
|
2018-06-27 02:59:57 -03:00
|
|
|
hal.console->printf("Erase failed\n");
|
2018-07-09 03:47:09 -03:00
|
|
|
free(fw);
|
2018-06-27 02:59:57 -03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
hal.console->printf("Flashing %s @%08x\n", fw_name, (unsigned int)addr);
|
|
|
|
const uint8_t max_attempts = 10;
|
|
|
|
for (uint8_t i=0; i<max_attempts; i++) {
|
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) {
|
2018-06-27 02:59:57 -03:00
|
|
|
hal.console->printf("Flash failed! (attempt=%u/%u)\n",
|
|
|
|
i+1,
|
|
|
|
max_attempts);
|
|
|
|
hal.scheduler->delay(1000);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
hal.console->printf("Flash OK\n");
|
2018-07-09 03:47:09 -03:00
|
|
|
free(fw);
|
2018-06-27 02:59:57 -03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
hal.console->printf("Flash failed after %u attempts\n", max_attempts);
|
2018-07-09 03:47:09 -03:00
|
|
|
free(fw);
|
2018-06-27 02:59:57 -03:00
|
|
|
return false;
|
|
|
|
}
|
2018-08-29 10:16:17 -03:00
|
|
|
#endif //#ifndef HAL_NO_FLASH_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];
|
|
|
|
|
|
|
|
memcpy(serialid, (const void *)UDID_START, 12);
|
|
|
|
strncpy(board_name, CHIBIOS_SHORT_BOARD_NAME, 13);
|
|
|
|
board_name[13] = 0;
|
|
|
|
|
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,
|
2018-07-05 18:53:31 -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
|
|
|
|
|
|
|
#ifdef USE_POSIX
|
|
|
|
/*
|
|
|
|
initialise filesystem
|
|
|
|
*/
|
|
|
|
bool Util::fs_init(void)
|
|
|
|
{
|
2019-04-06 23:56:05 -03:00
|
|
|
return sdcard_retry();
|
2018-12-27 21:05:06 -04:00
|
|
|
}
|
|
|
|
#endif
|
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();
|
|
|
|
}
|