ardupilot/libraries/DataFlash/examples/DataFlash_test/DataFlash_test.pde

123 lines
3.3 KiB
Plaintext
Raw Normal View History

2013-01-12 02:21:21 -04:00
/// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
/*
* Example of DataFlash library.
* Code by Jordi MuÒoz and Jose Julio. DIYDrones.com
*/
// Libraries
2013-03-10 23:02:08 -03:00
#include <AP_HAL.h>
#include <AP_HAL_AVR.h>
#include <AP_HAL_AVR_SITL.h>
#include <AP_HAL_Empty.h>
#include <AP_HAL_PX4.h>
#include <AP_Common.h>
2012-12-12 18:21:04 -04:00
#include <AP_Param.h>
#include <AP_Progmem.h>
#include <AP_Math.h> // ArduPilot Mega Vector/Matrix math Library
#include <DataFlash.h>
2012-12-12 18:21:04 -04:00
const AP_HAL::HAL& hal = AP_HAL_BOARD_DRIVER;
2012-12-12 18:21:04 -04:00
#if CONFIG_HAL_BOARD == HAL_BOARD_APM2
DataFlash_APM2 DataFlash;
#elif CONFIG_HAL_BOARD == HAL_BOARD_APM1
DataFlash_APM1 DataFlash;
#else
DataFlash_Empty DataFlash;
2012-12-12 18:21:04 -04:00
#endif
2012-01-09 00:26:56 -04:00
2013-01-12 02:21:21 -04:00
struct test_packet {
LOG_PACKET_HEADER;
2013-01-12 02:21:21 -04:00
uint16_t v1, v2, v3, v4;
int32_t l1, l2;
uint8_t dummy[80];
2013-01-12 02:21:21 -04:00
};
#define NUM_PACKETS 20
2012-01-09 00:26:56 -04:00
static uint16_t log_num;
void setup()
{
DataFlash.Init(); // DataFlash initialization
2012-12-12 18:21:04 -04:00
hal.console->println("Dataflash Log Test 1.0");
// Test
2012-12-12 18:21:04 -04:00
hal.scheduler->delay(20);
DataFlash.ReadManufacturerID();
2012-12-12 18:21:04 -04:00
hal.scheduler->delay(10);
2013-03-10 23:02:08 -03:00
DataFlash.ShowDeviceInfo(hal.console);
2013-01-12 02:21:21 -04:00
if (DataFlash.NeedErase()) {
hal.console->println("Erasing...");
DataFlash.EraseAll();
}
// We start to write some info (sequentialy) starting from page 1
// This is similar to what we will do...
log_num = DataFlash.start_new_log();
hal.console->printf("Using log number %u\n", log_num);
2012-12-12 18:21:04 -04:00
hal.console->println("After testing perform erase before using DataFlash for logging!");
hal.console->println("");
hal.console->println("Writing to flash... wait...");
2013-01-12 02:21:21 -04:00
uint32_t total_micros = 0;
uint16_t i;
for (i = 0; i < NUM_PACKETS; i++) {
uint32_t start = hal.scheduler->micros();
// note that we use g++ style initialisers to make larger
// structures easier to follow
struct test_packet pkt = {
LOG_PACKET_HEADER_INIT(7),
2013-01-12 02:21:21 -04:00
v1 : 2000 + i,
v2 : 2001 + i,
v3 : 2002 + i,
v4 : 2003 + i,
l1 : (long)i * 5000,
l2 : (long)i * 16268
2013-01-12 02:21:21 -04:00
};
DataFlash.WriteBlock(&pkt, sizeof(pkt));
total_micros += hal.scheduler->micros() - start;
hal.scheduler->delay(20);
}
2013-01-12 02:21:21 -04:00
hal.console->printf("Average write time %.1f usec/byte\n",
(double)total_micros/((float)i*sizeof(struct test_packet)));
2012-12-12 18:21:04 -04:00
hal.scheduler->delay(100);
}
static void callback(uint8_t msgid)
{
struct test_packet pkt;
DataFlash.ReadPacket(&pkt, sizeof(pkt));
hal.console->printf("PACKET: %02x,%u,%u,%u,%u,%ld,%ld\n",
(unsigned)msgid,
(unsigned)pkt.v1,
(unsigned)pkt.v2,
(unsigned)pkt.v3,
(unsigned)pkt.v4,
(long)pkt.l1,
(long)pkt.l2);
}
void loop()
{
uint16_t start, end;
hal.console->printf("Start read of log %u\n", log_num);
DataFlash.get_log_boundaries(log_num, start, end);
DataFlash.log_read_process(log_num, start, end, callback);
2013-01-12 02:21:21 -04:00
hal.console->printf("\nTest complete. Test will repeat in 20 seconds\n");
2012-12-12 18:21:04 -04:00
hal.scheduler->delay(20000);
}
2012-12-12 18:21:04 -04:00
AP_HAL_MAIN();