[flash status] transfer flash status from bootloader to application using random RAM location

main-solar-only
Richard Meadows 2016-08-12 22:33:54 +01:00
rodzic f9fad06c80
commit 0f9ba452f2
8 zmienionych plików z 29 dodań i 169 usunięć

Wyświetl plik

@ -1,35 +0,0 @@
/*
* Function related to the flash memory
* Copyright (C) 2016 Richard Meadows <richardeoin>
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef FLASH_H
#define FLASH_H
enum flash_state {
FLASH_GOOD, /* checksum matches */
FLASH_BAD_CSUM, /* mismatch */
};
enum flash_state check_flash_state(void);
#endif /* FLASH_H */

Wyświetl plik

@ -34,6 +34,8 @@ enum init_type {
INIT_TESTCASE = 0xCC,
};
uint32_t loader_flash_state;
/**
* Turns the status LED on
*/

Wyświetl plik

@ -104,7 +104,8 @@ void encode_telemetry(char* str, tracker_datapoint* dp)
base91_encode(str+4, 2, ((dp->thermistor_temperature+273.2)*10)); /* Temp never > 526º! */
base91_encode(str+6, 2, ((dp->radio_die_temperature+273.2)*10)); /* Temp never > 526º! */
//base91_encode(str+6, 2, dp->satillite_count); /* Small! */
base91_encode(str+8, 2, dp->time_to_first_fix); /* Small! */
base91_encode(str+8, 2, dp->time_to_first_fix +
((dp->flash_status & 0x7) * 1000)); /* < 8000 */
}
/**

Wyświetl plik

@ -37,6 +37,7 @@
#include "thermistor.h"
#include "watchdog.h"
#include "rtc.h"
#include "init.h"
struct tracker_datapoint datapoint = {.time={0}};
@ -143,6 +144,11 @@ struct tracker_datapoint* collect_data(void)
datapoint.bmp180_temperature = (float)b->temperature;
#endif
/**
* ---- Misc ----
*/
datapoint.flash_status = loader_flash_state;
#ifdef GPS_TYPE_UBX
/**
* ---- GPS UBX ----

Wyświetl plik

@ -1,116 +0,0 @@
/*
* Function related to the flash memory
* Copyright (C) 2016 Richard Meadows <richardeoin>
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <stdlib.h>
#include "samd20.h"
#include "memory.h"
#include "flash.h"
/**
* we put our checksum at the last address in flash
*/
volatile unsigned int* flash_checksum =
(unsigned int*)(FLASH_ADDR + FLASH_SIZE - 4);
// ---------------------------- crc32cx --------------------------------
/* This is crc32b modified to load the message a fullword at a time.
It assumes the message is word aligned and consists of an integral
number of words before the 0-byte that marks the end of the message.
This works only on a little-endian machine.
Not counting the table setup (which would probably be a separate
function), this function should be doable in 3 + 22w instructions, where
w is the number of fullwords in the input message. This is equivalent to
3 + 5.5n instructions, where n is the number of bytes. 1.25 of those 5.5
instructions are loads.
This is Exercise 1 in the text. C.f. Christopher Dannemiller,
who got it from Linux Source base,
www.gelato.unsw.edu.au/lxr/source/lib/crc32.c, lines 105-111. */
unsigned int crc32cx(unsigned int *ptr, size_t length)
{
int j;
unsigned int byte, crc, mask;
unsigned int table[256];
length &= ~0x3; /* must be mutiple of 4 */
/* Set up the table */
for (byte = 0; byte <= 255; byte++) {
crc = byte;
for (j = 7; j >= 0; j--) { // Do eight times.
mask = -(crc & 1);
crc = (crc >> 1) ^ (0xEDB88320 & mask);
}
table[byte] = crc;
}
/* Through with table setup, now calculate the CRC. */
crc = 0xFFFFFFFF;
while (length != 0) {
crc = crc ^ *ptr;
crc = (crc >> 8) ^ table[crc & 0xFF];
crc = (crc >> 8) ^ table[crc & 0xFF];
crc = (crc >> 8) ^ table[crc & 0xFF];
crc = (crc >> 8) ^ table[crc & 0xFF];
ptr++; length -= 4;
}
return ~crc;
}
/**
* 32 bit checksum for the whole memory space
*/
unsigned int checksum_memory(void)
{
/* do crc */
return crc32cx((unsigned int)FLASH_ADDR, /* start */
FLASH_SIZE - 4); /* length */
}
/**
* checks if memory checksum is good
*/
enum flash_state check_flash_state(void)
{
unsigned int calculated = checksum_memory();
if (*flash_checksum == 0xFFFFFFFF) { /* not written */
/* write it */
mem_write_word((uint32_t)flash_checksum, calculated);
return FLASH_GOOD;
} else { /* written */
/* check it */
if (calculated == *flash_checksum) {
return FLASH_GOOD;
} else {
return FLASH_BAD_CSUM;
}
}
}

Wyświetl plik

@ -76,6 +76,9 @@ void init(enum init_type init_t)
*/
external_watchdog_safe();
/* Get flash state from loader. Oh so hacky */
loader_flash_state = *(uint32_t*)0x20006000; /* SRAM+24k */
/**
* OSC8M should be considered unstable due to the temperature range. Therefore
* we need to switch to a stable low frequency clock right away.

Wyświetl plik

@ -35,7 +35,6 @@
#include "accumulator.h"
#include "battery.h"
#include "rtc.h"
#include "flash.h"
void rtty_telemetry(struct tracker_datapoint* dp);
@ -166,9 +165,7 @@ void run_sequencer(uint32_t n, uint32_t cycle_time_s)
/* Battery */
update_battery(dp);
/* Flash */
if ((n % 100) == 1) {
dp->flash_status = check_flash_state();
}
}

Wyświetl plik

@ -44,24 +44,26 @@ int main(void)
/* Init */
init(INIT_NORMAL);
/* Stay in low power mode until power/temperature are high enough */
do {
led_off();
idle(IDLE_LOADER);
led_on();
/* /\* Stay in low power mode until power/temperature are high enough *\/ */
/* do { */
/* led_off(); */
/* idle(IDLE_LOADER); */
/* led_on(); */
/* Read sensors */
start_adc_sequence();
while (is_adc_sequence_done() == 0) {
idle(IDLE_LOADER);
}
/* /\* Read sensors *\/ */
/* start_adc_sequence(); */
/* while (is_adc_sequence_done() == 0) { */
/* idle(IDLE_LOADER); */
/* } */
/* Check battery */
} while (get_battery() < 3.0);
/* /\* Check battery *\/ */
/* } while (get_battery() < 3.0); */
/* Check and repair memory */
check_and_repair_memory();
uint32_t flash_status = check_and_repair_memory();
/* Write it to RAM for the application to use. Oh so hacky */
*(uint32_t*)0x20006000 = flash_status; /* SRAM+24k */
/* Transfer control to application */
transfer_to_application();