From 24167015705ae831692b95735968b04a876f935e Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Fri, 4 Apr 2014 23:34:48 -0700 Subject: altos: Rename 'core' to 'kernel' core remains a bad name to use -- dirvish skips files (and directories, it seems) with that name. Signed-off-by: Keith Packard --- src/kernel/ao_log.c | 291 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 291 insertions(+) create mode 100644 src/kernel/ao_log.c (limited to 'src/kernel/ao_log.c') diff --git a/src/kernel/ao_log.c b/src/kernel/ao_log.c new file mode 100644 index 00000000..20febefe --- /dev/null +++ b/src/kernel/ao_log.c @@ -0,0 +1,291 @@ +/* + * Copyright © 2009 Keith Packard + * + * 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; version 2 of the License. + * + * 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 "ao.h" +#include +#include + +__pdata uint32_t ao_log_current_pos; +__pdata uint32_t ao_log_end_pos; +__pdata uint32_t ao_log_start_pos; +__xdata uint8_t ao_log_running; +__pdata enum ao_flight_state ao_log_state; +__xdata uint16_t ao_flight_number; + +void +ao_log_flush(void) +{ + ao_storage_flush(); +} + +/* + * When erasing a flight log, make sure the config block + * has an up-to-date version of the current flight number + */ + +struct ao_log_erase { + uint8_t unused; + uint16_t flight; +}; + +static __xdata struct ao_log_erase erase; + +#define LOG_MAX_ERASE 16 + +static uint32_t +ao_log_erase_pos(uint8_t i) +{ + return i * sizeof (struct ao_log_erase) + AO_CONFIG_MAX_SIZE; +} + +void +ao_log_write_erase(uint8_t pos) +{ + erase.unused = 0x00; + erase.flight = ao_flight_number; + ao_config_write(ao_log_erase_pos(pos), &erase, sizeof (erase)); + ao_config_flush(); +} + +static void +ao_log_read_erase(uint8_t pos) +{ + ao_config_read(ao_log_erase_pos(pos), &erase, sizeof (erase)); +} + + +static void +ao_log_erase_mark(void) +{ + uint8_t i; + + for (i = 0; i < LOG_MAX_ERASE; i++) { + ao_log_read_erase(i); + if (erase.unused == 0 && erase.flight == ao_flight_number) + return; + if (erase.unused == 0xff) { + ao_log_write_erase(i); + return; + } + } + ao_config_put(); +} + +static uint8_t +ao_log_slots() +{ + return (uint8_t) (ao_storage_log_max / ao_config.flight_log_max); +} + +uint32_t +ao_log_pos(uint8_t slot) +{ + return ((slot) * ao_config.flight_log_max); +} + +static uint16_t +ao_log_max_flight(void) +{ + uint8_t log_slot; + uint8_t log_slots; + uint16_t log_flight; + uint16_t max_flight = 0; + + /* Scan the log space looking for the biggest flight number */ + log_slots = ao_log_slots(); + for (log_slot = 0; log_slot < log_slots; log_slot++) { + log_flight = ao_log_flight(log_slot); + if (!log_flight) + continue; + if (max_flight == 0 || (int16_t) (log_flight - max_flight) > 0) + max_flight = log_flight; + } + return max_flight; +} + +void +ao_log_scan(void) __reentrant +{ + uint8_t log_slot; + uint8_t log_slots; + uint8_t log_want; + + ao_config_get(); + + ao_flight_number = ao_log_max_flight(); + if (ao_flight_number) + if (++ao_flight_number == 0) + ao_flight_number = 1; + + /* Now look through the log of flight numbers from erase operations and + * see if the last one is bigger than what we found above + */ + for (log_slot = LOG_MAX_ERASE; log_slot-- > 0;) { + ao_log_read_erase(log_slot); + if (erase.unused == 0) { + if (ao_flight_number == 0 || + (int16_t) (erase.flight - ao_flight_number) > 0) + ao_flight_number = erase.flight; + break; + } + } + if (ao_flight_number == 0) + ao_flight_number = 1; + + /* With a flight number in hand, find a place to write a new log, + * use the target flight number to index the available log slots so + * that we write logs to each spot about the same number of times. + */ + + /* Find a log slot for the next flight, if available */ + ao_log_current_pos = ao_log_end_pos = 0; + log_slots = ao_log_slots(); + log_want = (ao_flight_number - 1) % log_slots; + log_slot = log_want; + do { + if (ao_log_flight(log_slot) == 0) { + ao_log_current_pos = ao_log_pos(log_slot); + ao_log_end_pos = ao_log_current_pos + ao_config.flight_log_max; + break; + } + if (++log_slot >= log_slots) + log_slot = 0; + } while (log_slot != log_want); + + ao_wakeup(&ao_flight_number); +} + +void +ao_log_start(void) +{ + /* start logging */ + ao_log_running = 1; + ao_wakeup(&ao_log_running); +} + +void +ao_log_stop(void) +{ + ao_log_running = 0; + ao_log_flush(); +} + +uint8_t +ao_log_present(void) +{ + return ao_log_max_flight() != 0; +} + +uint8_t +ao_log_full(void) +{ + return ao_log_current_pos == ao_log_end_pos; +} + +#if HAS_ADC +static __xdata struct ao_task ao_log_task; +#endif + +void +ao_log_list(void) __reentrant +{ + uint8_t slot; + uint8_t slots; + uint16_t flight; + + slots = ao_log_slots(); + for (slot = 0; slot < slots; slot++) + { + flight = ao_log_flight(slot); + if (flight) + printf ("flight %d start %x end %x\n", + flight, + (uint16_t) (ao_log_pos(slot) >> 8), + (uint16_t) (ao_log_pos(slot+1) >> 8)); + } + printf ("done\n"); +} + +void +ao_log_delete(void) __reentrant +{ + uint8_t slot; + uint8_t slots; + + ao_cmd_decimal(); + if (ao_cmd_status != ao_cmd_success) + return; + + slots = ao_log_slots(); + /* Look for the flight log matching the requested flight */ + if (ao_cmd_lex_i) { + for (slot = 0; slot < slots; slot++) { + if (ao_log_flight(slot) == ao_cmd_lex_i) { + ao_log_erase_mark(); + ao_log_current_pos = ao_log_pos(slot); + ao_log_end_pos = ao_log_current_pos + ao_config.flight_log_max; + while (ao_log_current_pos < ao_log_end_pos) { + uint8_t i; + static __xdata uint8_t b; + + /* + * Check to see if we've reached the end of + * the used memory to avoid re-erasing the same + * memory over and over again + */ + for (i = 0; i < 16; i++) { + if (ao_storage_read(ao_log_current_pos + i, &b, 1)) + if (b != 0xff) + break; + } + if (i == 16) + break; + ao_storage_erase(ao_log_current_pos); + ao_log_current_pos += ao_storage_block; + } + puts("Erased"); + return; + } + } + } + printf("No such flight: %d\n", ao_cmd_lex_i); +} + +__code struct ao_cmds ao_log_cmds[] = { + { ao_log_list, "l\0List logs" }, + { ao_log_delete, "d \0Delete flight" }, + { 0, NULL }, +}; + +void +ao_log_init(void) +{ + ao_log_running = 0; + + /* For now, just log the flight starting at the begining of eeprom */ + ao_log_state = ao_flight_invalid; + + ao_cmd_register(&ao_log_cmds[0]); + +#ifndef HAS_ADC +#error Define HAS_ADC for ao_log.c +#endif +#if HAS_ADC + /* Create a task to log events to eeprom */ + ao_add_task(&ao_log_task, ao_log, "log"); +#endif +} -- cgit v1.2.3 From b8201bc9ba4a5f5f0522b68493cd5e7f013fd4bb Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Thu, 5 Jun 2014 17:14:28 -0700 Subject: altos: Include sensor logging task only on flight boards This lets TeleGPS use the logging infrastructure without wasting a task to log sensor data Signed-off-by: Keith Packard --- src/kernel/ao_log.c | 8 ++++++-- src/kernel/ao_log_mega.c | 6 +++--- 2 files changed, 9 insertions(+), 5 deletions(-) (limited to 'src/kernel/ao_log.c') diff --git a/src/kernel/ao_log.c b/src/kernel/ao_log.c index 20febefe..d60485e0 100644 --- a/src/kernel/ao_log.c +++ b/src/kernel/ao_log.c @@ -196,7 +196,11 @@ ao_log_full(void) return ao_log_current_pos == ao_log_end_pos; } -#if HAS_ADC +#ifndef LOG_ADC +#define LOG_ADC HAS_ADC +#endif + +#if LOG_ADC static __xdata struct ao_task ao_log_task; #endif @@ -284,7 +288,7 @@ ao_log_init(void) #ifndef HAS_ADC #error Define HAS_ADC for ao_log.c #endif -#if HAS_ADC +#if LOG_ADC /* Create a task to log events to eeprom */ ao_add_task(&ao_log_task, ao_log, "log"); #endif diff --git a/src/kernel/ao_log_mega.c b/src/kernel/ao_log_mega.c index 768947d5..8997fd05 100644 --- a/src/kernel/ao_log_mega.c +++ b/src/kernel/ao_log_mega.c @@ -65,7 +65,7 @@ ao_log_dump_check_data(void) return 1; } -#if HAS_ADC +#if HAS_FLIGHT static __data uint8_t ao_log_data_pos; /* a hack to make sure that ao_log_megas fill the eeprom block in even units */ @@ -100,9 +100,9 @@ ao_log(void) log.u.flight.ground_accel_along = ao_ground_accel_along; log.u.flight.ground_accel_across = ao_ground_accel_across; log.u.flight.ground_accel_through = ao_ground_accel_through; + log.u.flight.ground_roll = ao_ground_roll; log.u.flight.ground_pitch = ao_ground_pitch; log.u.flight.ground_yaw = ao_ground_yaw; - log.u.flight.ground_roll = ao_ground_roll; #endif log.u.flight.ground_pres = ao_ground_pres; log.u.flight.flight = ao_flight_number; @@ -183,7 +183,7 @@ ao_log(void) ao_sleep(&ao_log_running); } } -#endif +#endif /* HAS_FLIGHT */ uint16_t ao_log_flight(uint8_t slot) -- cgit v1.2.3 From 1d6ca536c688d35b3cba0a829b04b93c5124b328 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Sat, 7 Jun 2014 10:09:51 -0700 Subject: altos: Allow value other than 0 for marking erased flights on-chip eeprom doesn't erase to 0xff, so let TeleMega use a different value. Signed-off-by: Keith Packard --- src/kernel/ao_log.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) (limited to 'src/kernel/ao_log.c') diff --git a/src/kernel/ao_log.c b/src/kernel/ao_log.c index d60485e0..76e0e68c 100644 --- a/src/kernel/ao_log.c +++ b/src/kernel/ao_log.c @@ -38,13 +38,22 @@ ao_log_flush(void) */ struct ao_log_erase { - uint8_t unused; + uint8_t mark; uint16_t flight; }; static __xdata struct ao_log_erase erase; +#ifndef LOG_MAX_ERASE #define LOG_MAX_ERASE 16 +#endif + +#ifndef LOG_ERASE_MARK +#if USE_EEPROM_CONFIG +#error "Must define LOG_ERASE_MARK with USE_EEPROM_CONFIG" +#endif +#define LOG_ERASE_MARK 0x00 +#endif static uint32_t ao_log_erase_pos(uint8_t i) @@ -55,7 +64,7 @@ ao_log_erase_pos(uint8_t i) void ao_log_write_erase(uint8_t pos) { - erase.unused = 0x00; + erase.mark = LOG_ERASE_MARK; erase.flight = ao_flight_number; ao_config_write(ao_log_erase_pos(pos), &erase, sizeof (erase)); ao_config_flush(); @@ -75,9 +84,9 @@ ao_log_erase_mark(void) for (i = 0; i < LOG_MAX_ERASE; i++) { ao_log_read_erase(i); - if (erase.unused == 0 && erase.flight == ao_flight_number) + if (erase.mark == LOG_ERASE_MARK && erase.flight == ao_flight_number) return; - if (erase.unused == 0xff) { + if (erase.mark != LOG_ERASE_MARK) { ao_log_write_erase(i); return; } @@ -136,7 +145,7 @@ ao_log_scan(void) __reentrant */ for (log_slot = LOG_MAX_ERASE; log_slot-- > 0;) { ao_log_read_erase(log_slot); - if (erase.unused == 0) { + if (erase.mark == LOG_ERASE_MARK) { if (ao_flight_number == 0 || (int16_t) (erase.flight - ao_flight_number) > 0) ao_flight_number = erase.flight; -- cgit v1.2.3 From 5d973570ef2324b21a64477eecb0a292652ff467 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Sat, 7 Jun 2014 10:54:14 -0700 Subject: altos: Clear out eeprom erase records when writing entry 0 When writing config/erase to eeprom, there's no 'erase' operation as on-chip eeprom is writable at a byte level. As such, we can't tell when the erase blocks get reset when the config gets written. When this happens, erase block 0 gets written explicitly, so just use that call to trigger explicit erasing of the data. Signed-off-by: Keith Packard --- src/kernel/ao_log.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'src/kernel/ao_log.c') diff --git a/src/kernel/ao_log.c b/src/kernel/ao_log.c index 76e0e68c..3cf85a33 100644 --- a/src/kernel/ao_log.c +++ b/src/kernel/ao_log.c @@ -67,6 +67,18 @@ ao_log_write_erase(uint8_t pos) erase.mark = LOG_ERASE_MARK; erase.flight = ao_flight_number; ao_config_write(ao_log_erase_pos(pos), &erase, sizeof (erase)); + +#if USE_EEPROM_CONFIG + if (pos == 0) { + uint8_t i; + for (i = 1; i < LOG_MAX_ERASE; i++) { + erase.mark = ~LOG_ERASE_MARK; + erase.flight = 0; + ao_config_write(ao_log_erase_pos(i), &erase, sizeof (erase)); + } + } +#endif + ao_config_flush(); } -- cgit v1.2.3 From 8117ba3553789a2bae9beb92fbe9e14e3cc79389 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Thu, 12 Jun 2014 23:56:07 -0700 Subject: altos: Define ao_log_mutex in ao_log.c rather than every log product Signed-off-by: Keith Packard --- src/kernel/ao_log.c | 1 + src/kernel/ao_log.h | 2 +- src/kernel/ao_log_big.c | 1 - src/kernel/ao_log_gps.c | 1 - src/kernel/ao_log_mega.c | 1 - src/kernel/ao_log_metrum.c | 1 - src/kernel/ao_log_mini.c | 1 - 7 files changed, 2 insertions(+), 6 deletions(-) (limited to 'src/kernel/ao_log.c') diff --git a/src/kernel/ao_log.c b/src/kernel/ao_log.c index 3cf85a33..d9c3e00f 100644 --- a/src/kernel/ao_log.c +++ b/src/kernel/ao_log.c @@ -19,6 +19,7 @@ #include #include +__xdata uint8_t ao_log_mutex; __pdata uint32_t ao_log_current_pos; __pdata uint32_t ao_log_end_pos; __pdata uint32_t ao_log_start_pos; diff --git a/src/kernel/ao_log.h b/src/kernel/ao_log.h index 87fa0d4d..33cda3eb 100644 --- a/src/kernel/ao_log.h +++ b/src/kernel/ao_log.h @@ -29,7 +29,7 @@ * by sleeping on this variable. */ extern __xdata uint16_t ao_flight_number; - +extern __xdata uint8_t ao_log_mutex; extern __pdata uint32_t ao_log_current_pos; extern __pdata uint32_t ao_log_end_pos; extern __pdata uint32_t ao_log_start_pos; diff --git a/src/kernel/ao_log_big.c b/src/kernel/ao_log_big.c index db01f46c..8f57bf75 100644 --- a/src/kernel/ao_log_big.c +++ b/src/kernel/ao_log_big.c @@ -17,7 +17,6 @@ #include "ao.h" -static __xdata uint8_t ao_log_mutex; static __xdata struct ao_log_record log; __code uint8_t ao_log_format = AO_LOG_FORMAT_FULL; diff --git a/src/kernel/ao_log_gps.c b/src/kernel/ao_log_gps.c index 8bf529f4..3b728c25 100644 --- a/src/kernel/ao_log_gps.c +++ b/src/kernel/ao_log_gps.c @@ -23,7 +23,6 @@ #include #include -static __xdata uint8_t ao_log_mutex; static __xdata struct ao_log_gps log; __code uint8_t ao_log_format = AO_LOG_FORMAT_TELEGPS; diff --git a/src/kernel/ao_log_mega.c b/src/kernel/ao_log_mega.c index 8997fd05..cb83be4b 100644 --- a/src/kernel/ao_log_mega.c +++ b/src/kernel/ao_log_mega.c @@ -20,7 +20,6 @@ #include #include -static __xdata uint8_t ao_log_mutex; static __xdata struct ao_log_mega log; __code uint8_t ao_log_format = AO_LOG_FORMAT_TELEMEGA; diff --git a/src/kernel/ao_log_metrum.c b/src/kernel/ao_log_metrum.c index 9b17adc2..08e7b8c4 100644 --- a/src/kernel/ao_log_metrum.c +++ b/src/kernel/ao_log_metrum.c @@ -20,7 +20,6 @@ #include #include -static __xdata uint8_t ao_log_mutex; static __xdata struct ao_log_metrum log; __code uint8_t ao_log_format = AO_LOG_FORMAT_TELEMETRUM; diff --git a/src/kernel/ao_log_mini.c b/src/kernel/ao_log_mini.c index 29e3bd9f..0ca3ed06 100644 --- a/src/kernel/ao_log_mini.c +++ b/src/kernel/ao_log_mini.c @@ -20,7 +20,6 @@ #include #include -static __xdata uint8_t ao_log_mutex; static __xdata struct ao_log_mini log; __code uint8_t ao_log_format = AO_LOG_FORMAT; -- cgit v1.2.3 From f49540acd48292bd9f68ded647561d0e800c619d Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Thu, 12 Jun 2014 23:59:37 -0700 Subject: altos/telegps: Create new flight if current flight is erased telegps is unique in that USB may be connected while a flight is active and sensible things should happen. If a flight is being recorded and gets erased, then a new flight should be started. This is done by hooking in the flight erase code and calling out to the tracker code to figure out whether to switch to a new flight or not. Signed-off-by: Keith Packard --- src/kernel/ao_log.c | 22 ++++++++++++++++------ src/kernel/ao_tracker.c | 43 ++++++++++++++++++++++++++++++++++++------- src/kernel/ao_tracker.h | 6 ++++++ 3 files changed, 58 insertions(+), 13 deletions(-) (limited to 'src/kernel/ao_log.c') diff --git a/src/kernel/ao_log.c b/src/kernel/ao_log.c index d9c3e00f..91617d93 100644 --- a/src/kernel/ao_log.c +++ b/src/kernel/ao_log.c @@ -18,6 +18,9 @@ #include "ao.h" #include #include +#if HAS_TRACKER +#include +#endif __xdata uint8_t ao_log_mutex; __pdata uint32_t ao_log_current_pos; @@ -251,6 +254,7 @@ ao_log_delete(void) __reentrant { uint8_t slot; uint8_t slots; + uint32_t log_current_pos, log_end_pos; ao_cmd_decimal(); if (ao_cmd_status != ao_cmd_success) @@ -261,10 +265,13 @@ ao_log_delete(void) __reentrant if (ao_cmd_lex_i) { for (slot = 0; slot < slots; slot++) { if (ao_log_flight(slot) == ao_cmd_lex_i) { +#if HAS_TRACKER + ao_tracker_erase_start(ao_cmd_lex_i); +#endif ao_log_erase_mark(); - ao_log_current_pos = ao_log_pos(slot); - ao_log_end_pos = ao_log_current_pos + ao_config.flight_log_max; - while (ao_log_current_pos < ao_log_end_pos) { + log_current_pos = ao_log_pos(slot); + log_end_pos = log_current_pos + ao_config.flight_log_max; + while (log_current_pos < log_end_pos) { uint8_t i; static __xdata uint8_t b; @@ -274,15 +281,18 @@ ao_log_delete(void) __reentrant * memory over and over again */ for (i = 0; i < 16; i++) { - if (ao_storage_read(ao_log_current_pos + i, &b, 1)) + if (ao_storage_read(log_current_pos + i, &b, 1)) if (b != 0xff) break; } if (i == 16) break; - ao_storage_erase(ao_log_current_pos); - ao_log_current_pos += ao_storage_block; + ao_storage_erase(log_current_pos); + log_current_pos += ao_storage_block; } +#if HAS_TRACKER + ao_tracker_erase_end(); +#endif puts("Erased"); return; } diff --git a/src/kernel/ao_tracker.c b/src/kernel/ao_tracker.c index a6ad87a3..9febc7f0 100644 --- a/src/kernel/ao_tracker.c +++ b/src/kernel/ao_tracker.c @@ -50,6 +50,7 @@ struct gps_position gps_position[GPS_RING]; static uint8_t gps_head; +static uint8_t tracker_mutex; static uint8_t log_started; static struct ao_telemetry_location gps_data; static uint8_t tracker_running; @@ -100,17 +101,19 @@ ao_tracker(void) tracker_running = 0; } - if (new_tracker_running && !tracker_running) { + if (new_tracker_running && !tracker_running) ao_telemetry_set_interval(AO_SEC_TO_TICKS(tracker_interval)); - ao_log_start(); - } else if (!new_tracker_running && tracker_running) { + else if (!new_tracker_running && tracker_running) ao_telemetry_set_interval(0); - ao_log_stop(); - } tracker_running = new_tracker_running; - if (!tracker_running) + if (new_tracker_running && !ao_log_running) + ao_log_start(); + else if (!new_tracker_running && ao_log_running) + ao_log_stop(); + + if (!ao_log_running) continue; if (new & AO_GPS_NEW_DATA) { @@ -138,10 +141,11 @@ ao_tracker(void) } } if (ao_tracker_force_telem) { - printf ("moving %d\n", moving); + printf ("moving %d started %d\n", moving, log_started); flush(); } if (moving) { + ao_mutex_get(&tracker_mutex); if (!log_started) { ao_log_gps_flight(); log_started = 1; @@ -151,12 +155,37 @@ ao_tracker(void) gps_position[gps_head].longitude = gps_data.longitude; gps_position[gps_head].altitude = gps_data.altitude; gps_head = ao_gps_ring_next(gps_head); + ao_mutex_put(&tracker_mutex); } } } } } +static uint8_t erasing_current; + +void +ao_tracker_erase_start(uint16_t flight) +{ + erasing_current = flight == ao_flight_number; + if (erasing_current) { + ao_mutex_get(&tracker_mutex); + ao_log_stop(); + if (++ao_flight_number == 0) + ao_flight_number = 1; + } +} + +void +ao_tracker_erase_end(void) +{ + if (erasing_current) { + ao_log_scan(); + log_started = 0; + ao_mutex_put(&tracker_mutex); + } +} + static struct ao_task ao_tracker_task; static void diff --git a/src/kernel/ao_tracker.h b/src/kernel/ao_tracker.h index a0fd2f49..78c40cb4 100644 --- a/src/kernel/ao_tracker.h +++ b/src/kernel/ao_tracker.h @@ -28,4 +28,10 @@ void ao_tracker_init(void); +void +ao_tracker_erase_start(uint16_t flight); + +void +ao_tracker_erase_end(void); + #endif /* _AO_TRACKER_H_ */ -- cgit v1.2.3