From d8d09fc96082d3cdaa4414c4fa621c6ab192150c Mon Sep 17 00:00:00 2001 From: Richard Meadows Date: Fri, 12 Aug 2016 20:08:28 +0100 Subject: [PATCH] [loader] add testcase for loader, but disable testcases from loader build --- loader/Makefile | 2 +- loader/inc/flash.h | 11 ++++++ loader/src/flash.c | 34 ++---------------- loader/test/tc/__init__.py | 9 +++++ loader/test/tc/repair_test.h | 49 ++++++++++++++++++++++++++ loader/test/tc/repair_test.py | 65 +++++++++++++++++++++++++++++++++++ loader/test/tmain.c | 1 + 7 files changed, 138 insertions(+), 33 deletions(-) create mode 100644 loader/test/tc/__init__.py create mode 100644 loader/test/tc/repair_test.h create mode 100644 loader/test/tc/repair_test.py diff --git a/loader/Makefile b/loader/Makefile index 09faaf3..232cbc2 100644 --- a/loader/Makefile +++ b/loader/Makefile @@ -115,7 +115,7 @@ INCLUDE_PATH += chip/ chip/cmsis/ samd20/ samd20/component/ test/tc/ # Verification suite code # # -SYSTEM += test/tmain.c +#SYSTEM += test/tmain.c # Linker Scripts # diff --git a/loader/inc/flash.h b/loader/inc/flash.h index b3e7784..80fd224 100644 --- a/loader/inc/flash.h +++ b/loader/inc/flash.h @@ -25,6 +25,17 @@ #ifndef FLASH_H #define FLASH_H +#include "samd20.h" + +#define APPLICATION_BASE (0x00004000) /* 16K */ +#define APPLICATION_LENGTH (112*1024) /* 112K */ + +#define D1_START (APPLICATION_BASE) +#define D1_SECTORS (APPLICATION_LENGTH/256) +#define D2_START (APPLICATION_BASE+APPLICATION_LENGTH) +#define D2_SECTORS (D1_SECTORS) + + uint32_t check_and_repair_memory(void); #endif /* FLASH_H */ diff --git a/loader/src/flash.c b/loader/src/flash.c index bcd610c..5574222 100644 --- a/loader/src/flash.c +++ b/loader/src/flash.c @@ -30,14 +30,6 @@ #include "flash.h" #include "watchdog.h" -#define APPLICATION_BASE (0x00004000) /* 16K */ -#define APPLICATION_LENGTH (112*1024) /* 112K */ - -#define D1_START (APPLICATION_BASE) -#define D1_SECTORS (APPLICATION_LENGTH/256) -#define D2_START (APPLICATION_BASE+APPLICATION_LENGTH) -#define D2_SECTORS (D1_SECTORS) - /* Check these are multiples of 64 */ #if (D1_SECTORS & 0x3F) #error D1_SECTORS _must_ be a mul 64, so checksums fill integer no. of sectors @@ -123,30 +115,6 @@ uint32_t checksum_sector(unsigned int* sector) SECTOR_SIZE); /* 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; */ -/* } */ -/* } */ -/* } */ - - /** * updates checksum records in nvm */ @@ -166,6 +134,8 @@ void update_checksums(const uint32_t* nvm, uint32_t* ram, int sectors) /** * Checks and repairs application memory space + * + * returns the number of errors successfully corrected */ uint32_t check_and_repair_memory(void) { diff --git a/loader/test/tc/__init__.py b/loader/test/tc/__init__.py new file mode 100644 index 0000000..592ded6 --- /dev/null +++ b/loader/test/tc/__init__.py @@ -0,0 +1,9 @@ +# Import every module in this subdirectory +import os +import glob +modules = glob.glob(os.path.dirname(__file__)+"/*.py") +__all__ = [ os.path.basename(f)[:-3] for f in modules if not f.endswith('__init__.py')] +del os +del glob +del f +del modules diff --git a/loader/test/tc/repair_test.h b/loader/test/tc/repair_test.h new file mode 100644 index 0000000..67c8a02 --- /dev/null +++ b/loader/test/tc/repair_test.h @@ -0,0 +1,49 @@ +#ifndef __verification__ +#define __verification__ +#endif + +/****************************//* repair_test_tc *//****************************/ +/** + * Write a description of your test case here + */ +#include + +#include "flash.h" +#include "memory.h" + +/* Parameters in */ +struct repair_test_tc_params { + + /* Input paramters to your test case go here */ + uint32_t address_to_corrupt; + +} repair_test_tc_params; +/* Results out */ +struct repair_test_tc_results { + + /* Result values should be populated here */ + uint32_t errors_corrected; + int memcmp_result; + +} repair_test_tc_results; +/* Function */ +__verification__ void repair_test_tc(void) { + + /** + * The main body of the test case goes here. + * + * Use the input parameters to run the test case. Populate the + * results structure at the end + */ + + if (repair_test_tc_params.address_to_corrupt) { + mem_write_word((unsigned int*)repair_test_tc_params.address_to_corrupt, 0); + } + + /* repair */ + repair_test_tc_results.errors_corrected = check_and_repair_memory(); + + /* check memory */ + repair_test_tc_results.memcmp_result = + memcmp((void*)D1_START, (void*)D2_START, APPLICATION_LENGTH); +} diff --git a/loader/test/tc/repair_test.py b/loader/test/tc/repair_test.py new file mode 100644 index 0000000..ffa2e3b --- /dev/null +++ b/loader/test/tc/repair_test.py @@ -0,0 +1,65 @@ +#!/usr/bin/env python +# coding=utf-8 + +# ------------------------------------------------------------------------------ +# Imports +# ------------------------------------------------------------------------------ + +import sys +sys.path.append("./test") +import main + +from random import randint + +# ------------------------------------------------------------------------------ +# Test Script +# ------------------------------------------------------------------------------ + +class repair_test_tc: + def __init__(self): + self.name = self.__class__.__name__ + + self.index = 0 + self.addresses = [0, # Do nothing + 0x00004000, # D1 start + 0x00020000, # D2 start + 0x0003C000, # EEPROM + ]; + self.errors_expected = [0, + 1, + 1, + 0]; + + def get_test(self): + """Returns some suitable test parameters""" + params = main.struct_repair_test_tc_params() + + if self.index >= len(self.addresses): + return None + + params.address_to_corrupt = self.addresses[self.index] + + return params + + def is_correct(self, params, result, print_info): + """Returns if a result is correct for the given parameters""" + + errors_corrected = int(result['errors_corrected']) + memcmp_result = int(result['memcmp_result']) + errors_expected = self.errors_expected[self.index] + self.index +=1 + + # Check errors + if errors_corrected != errors_expected: + print_info("Expected {:d} errors, actually repaired {:d} errors!" + .format(errors_expected, errors_corrected)) + return False + + if memcmp_result != 0: + print_info("D1 and D2 regions differ! memcmp = {d}" + .format(memcmp_result)) + return False + + print_info("Case {:d} passed ✓".format(self.index)) + + return True diff --git a/loader/test/tmain.c b/loader/test/tmain.c index 7db9750..c22e61e 100644 --- a/loader/test/tmain.c +++ b/loader/test/tmain.c @@ -37,6 +37,7 @@ /***************************** test cases *******************************/ #include "times_two.h" +#include "repair_test.h" /* [new_tc] */