2016-11-17 01:24:16 -04:00
|
|
|
//
|
|
|
|
// Unit tests for the AP_Math rotations code
|
|
|
|
//
|
|
|
|
|
|
|
|
#include <AP_HAL/AP_HAL.h>
|
|
|
|
#include <AP_Math/AP_Math.h>
|
|
|
|
#include <AP_FlashStorage/AP_FlashStorage.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
const AP_HAL::HAL& hal = AP_HAL::get_HAL();
|
|
|
|
|
|
|
|
class FlashTest : public AP_HAL::HAL::Callbacks {
|
|
|
|
public:
|
|
|
|
// HAL::Callbacks implementation.
|
|
|
|
void setup() override;
|
|
|
|
void loop() override;
|
|
|
|
|
|
|
|
private:
|
2016-11-17 14:54:47 -04:00
|
|
|
static const uint32_t flash_sector_size = 32U * 1024U;
|
2016-11-17 01:24:16 -04:00
|
|
|
|
|
|
|
uint8_t mem_buffer[AP_FlashStorage::storage_size];
|
|
|
|
uint8_t mem_mirror[AP_FlashStorage::storage_size];
|
|
|
|
|
|
|
|
// flash buffer
|
2018-07-10 21:07:30 -03:00
|
|
|
uint8_t *flash[2];
|
2016-11-17 01:24:16 -04:00
|
|
|
|
|
|
|
bool flash_write(uint8_t sector, uint32_t offset, const uint8_t *data, uint16_t length);
|
|
|
|
bool flash_read(uint8_t sector, uint32_t offset, uint8_t *data, uint16_t length);
|
|
|
|
bool flash_erase(uint8_t sector);
|
2016-11-18 19:22:58 -04:00
|
|
|
bool flash_erase_ok(void);
|
2016-11-17 01:24:16 -04:00
|
|
|
|
|
|
|
AP_FlashStorage storage{mem_buffer,
|
|
|
|
flash_sector_size,
|
|
|
|
FUNCTOR_BIND_MEMBER(&FlashTest::flash_write, bool, uint8_t, uint32_t, const uint8_t *, uint16_t),
|
|
|
|
FUNCTOR_BIND_MEMBER(&FlashTest::flash_read, bool, uint8_t, uint32_t, uint8_t *, uint16_t),
|
2016-11-18 19:22:58 -04:00
|
|
|
FUNCTOR_BIND_MEMBER(&FlashTest::flash_erase, bool, uint8_t),
|
|
|
|
FUNCTOR_BIND_MEMBER(&FlashTest::flash_erase_ok, bool)};
|
2016-11-17 01:24:16 -04:00
|
|
|
|
|
|
|
// write to storage and mem_mirror
|
|
|
|
void write(uint16_t offset, const uint8_t *data, uint16_t length);
|
2016-11-18 19:22:58 -04:00
|
|
|
|
|
|
|
bool erase_ok;
|
2016-11-17 01:24:16 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
bool FlashTest::flash_write(uint8_t sector, uint32_t offset, const uint8_t *data, uint16_t length)
|
|
|
|
{
|
|
|
|
if (sector > 1) {
|
|
|
|
AP_HAL::panic("FATAL: write to sector %u\n", (unsigned)sector);
|
|
|
|
}
|
|
|
|
if (offset + length > flash_sector_size) {
|
|
|
|
AP_HAL::panic("FATAL: write to sector %u at offset %u length %u\n",
|
|
|
|
(unsigned)sector,
|
|
|
|
(unsigned)offset,
|
|
|
|
(unsigned)length);
|
|
|
|
}
|
|
|
|
uint8_t *b = &flash[sector][offset];
|
|
|
|
for (uint16_t i=0; i<length; i++) {
|
|
|
|
b[i] &= data[i];
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FlashTest::flash_read(uint8_t sector, uint32_t offset, uint8_t *data, uint16_t length)
|
|
|
|
{
|
|
|
|
if (sector > 1) {
|
|
|
|
AP_HAL::panic("FATAL: read from sector %u\n", (unsigned)sector);
|
|
|
|
}
|
|
|
|
if (offset + length > flash_sector_size) {
|
|
|
|
AP_HAL::panic("FATAL: read from sector %u at offset %u length %u\n",
|
|
|
|
(unsigned)sector,
|
|
|
|
(unsigned)offset,
|
|
|
|
(unsigned)length);
|
|
|
|
}
|
|
|
|
memcpy(data, &flash[sector][offset], length);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FlashTest::flash_erase(uint8_t sector)
|
|
|
|
{
|
|
|
|
if (sector > 1) {
|
|
|
|
AP_HAL::panic("FATAL: erase sector %u\n", (unsigned)sector);
|
|
|
|
}
|
|
|
|
memset(&flash[sector][0], 0xFF, flash_sector_size);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-11-18 19:22:58 -04:00
|
|
|
bool FlashTest::flash_erase_ok(void)
|
|
|
|
{
|
|
|
|
return erase_ok;
|
|
|
|
}
|
|
|
|
|
2016-11-17 01:24:16 -04:00
|
|
|
void FlashTest::write(uint16_t offset, const uint8_t *data, uint16_t length)
|
|
|
|
{
|
|
|
|
memcpy(&mem_mirror[offset], data, length);
|
|
|
|
memcpy(&mem_buffer[offset], data, length);
|
|
|
|
if (!storage.write(offset, length)) {
|
2016-11-18 19:22:58 -04:00
|
|
|
if (erase_ok) {
|
|
|
|
printf("Failed to write at %u for %u\n", offset, length);
|
2016-11-17 01:24:16 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* test flash storage
|
|
|
|
*/
|
|
|
|
void FlashTest::setup(void)
|
|
|
|
{
|
2018-07-10 21:07:30 -03:00
|
|
|
flash[0] = (uint8_t *)malloc(flash_sector_size);
|
|
|
|
flash[1] = (uint8_t *)malloc(flash_sector_size);
|
2016-11-17 01:24:16 -04:00
|
|
|
flash_erase(0);
|
|
|
|
flash_erase(1);
|
|
|
|
hal.console->printf("AP_FlashStorage test\n");
|
|
|
|
|
|
|
|
if (!storage.init()) {
|
|
|
|
AP_HAL::panic("Failed first init()");
|
|
|
|
}
|
|
|
|
|
|
|
|
// fill with 10k random writes
|
2016-11-18 19:22:58 -04:00
|
|
|
for (uint32_t i=0; i<5000000; i++) {
|
2016-11-17 01:24:16 -04:00
|
|
|
uint16_t ofs = get_random16() % sizeof(mem_buffer);
|
|
|
|
uint16_t length = get_random16() & 0x1F;
|
|
|
|
length = MIN(length, sizeof(mem_buffer) - ofs);
|
|
|
|
uint8_t data[length];
|
|
|
|
for (uint8_t j=0; j<length; j++) {
|
|
|
|
data[j] = get_random16() & 0xFF;
|
|
|
|
}
|
2016-11-18 19:22:58 -04:00
|
|
|
|
|
|
|
erase_ok = (i % 1000 == 0);
|
2016-11-17 01:24:16 -04:00
|
|
|
write(ofs, data, length);
|
|
|
|
|
2016-11-18 19:22:58 -04:00
|
|
|
if (erase_ok) {
|
2016-11-17 01:24:16 -04:00
|
|
|
if (memcmp(mem_buffer, mem_mirror, sizeof(mem_buffer)) != 0) {
|
2018-05-03 22:41:27 -03:00
|
|
|
AP_HAL::panic("FATAL: data mis-match at i=%u", (unsigned)i);
|
2016-11-17 01:24:16 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-18 19:22:58 -04:00
|
|
|
// force final write to allow for flush with erase_ok
|
|
|
|
erase_ok = true;
|
|
|
|
uint8_t b = 42;
|
|
|
|
write(37, &b, 1);
|
|
|
|
|
2016-11-17 01:24:16 -04:00
|
|
|
if (memcmp(mem_buffer, mem_mirror, sizeof(mem_buffer)) != 0) {
|
|
|
|
AP_HAL::panic("FATAL: data mis-match before re-init");
|
|
|
|
}
|
|
|
|
|
|
|
|
// re-init
|
|
|
|
printf("re-init\n");
|
|
|
|
memset(mem_buffer, 0, sizeof(mem_buffer));
|
|
|
|
if (!storage.init()) {
|
|
|
|
AP_HAL::panic("Failed second init()");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (memcmp(mem_buffer, mem_mirror, sizeof(mem_buffer)) != 0) {
|
|
|
|
AP_HAL::panic("FATAL: data mis-match");
|
|
|
|
}
|
|
|
|
AP_HAL::panic("TEST PASSED");
|
|
|
|
}
|
|
|
|
|
|
|
|
void FlashTest::loop(void)
|
|
|
|
{
|
|
|
|
hal.console->printf("loop\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
FlashTest flashtest;
|
|
|
|
|
|
|
|
AP_HAL_MAIN_CALLBACKS(&flashtest);
|