Andrew Boyson / lpc1768

Dependents:   test-lpc1768 oldheating gps motorhome ... more

Committer:
andrewboyson
Date:
Wed May 08 12:13:37 2019 +0000
Revision:
49:d056e2c5c4ee
Child:
50:e90c6aaa2645
Sorted out reset module to properly read the RSID register

Who changed what in which revision?

UserRevisionLine numberNew contents of line
andrewboyson 49:d056e2c5c4ee 1 #include <stdio.h>
andrewboyson 49:d056e2c5c4ee 2 #include <string.h>
andrewboyson 49:d056e2c5c4ee 3 #include "restart.h"
andrewboyson 49:d056e2c5c4ee 4 #include "semihost.h"
andrewboyson 49:d056e2c5c4ee 5
andrewboyson 49:d056e2c5c4ee 6 #define PERSIST (*((volatile unsigned *) 0x20083FFC)) //Make sure the startup scatter leaves a 4 byte space at this address
andrewboyson 49:d056e2c5c4ee 7
andrewboyson 49:d056e2c5c4ee 8 #define MAGIC 51 //0101 0001
andrewboyson 49:d056e2c5c4ee 9
andrewboyson 49:d056e2c5c4ee 10 //Continually set during the program then saved by the relevant fault handler if that fault occurs.
andrewboyson 49:d056e2c5c4ee 11 volatile int RestartZone = 0; //0 to FF
andrewboyson 49:d056e2c5c4ee 12 volatile int RestartPoint = 0; //0 to FFFF
andrewboyson 49:d056e2c5c4ee 13
andrewboyson 49:d056e2c5c4ee 14 void Restart(int type)
andrewboyson 49:d056e2c5c4ee 15 {
andrewboyson 49:d056e2c5c4ee 16 PERSIST = MAGIC << 24 | (type & 0xF) << 20 | (RestartZone & 0xFF) << 12 | (RestartPoint & 0xFFF);
andrewboyson 49:d056e2c5c4ee 17 SemihostReset();
andrewboyson 49:d056e2c5c4ee 18 }
andrewboyson 49:d056e2c5c4ee 19
andrewboyson 49:d056e2c5c4ee 20 //Recorded during initialisation
andrewboyson 49:d056e2c5c4ee 21 int lastCause = 0;
andrewboyson 49:d056e2c5c4ee 22 int lastZone = 0;
andrewboyson 49:d056e2c5c4ee 23 int lastPoint = 0;
andrewboyson 49:d056e2c5c4ee 24
andrewboyson 49:d056e2c5c4ee 25 //Called by the user interface
andrewboyson 49:d056e2c5c4ee 26 int RestartGetLastCause () { return lastCause; } //Cause can be 0 to 15
andrewboyson 49:d056e2c5c4ee 27 int RestartGetLastZone () { return lastZone; } //Zone can be 0 to 255
andrewboyson 49:d056e2c5c4ee 28 int RestartGetLastPoint () { return lastPoint; } //Point can be 0 to 4095
andrewboyson 49:d056e2c5c4ee 29
andrewboyson 49:d056e2c5c4ee 30 void RestartInit()
andrewboyson 49:d056e2c5c4ee 31 {
andrewboyson 49:d056e2c5c4ee 32 int magic = PERSIST >> 24 & 0x00FF; //Magic number if valid data
andrewboyson 49:d056e2c5c4ee 33 if (magic == MAGIC) //Data is valid
andrewboyson 49:d056e2c5c4ee 34 {
andrewboyson 49:d056e2c5c4ee 35 lastCause = PERSIST >> 20 & 0x000F; //Type can be 0 to 15
andrewboyson 49:d056e2c5c4ee 36 lastZone = PERSIST >> 12 & 0x00FF; //Zone can be 0 to 255
andrewboyson 49:d056e2c5c4ee 37 lastPoint = PERSIST & 0x0FFF; //Point can be 0 to 4095
andrewboyson 49:d056e2c5c4ee 38 }
andrewboyson 49:d056e2c5c4ee 39 else //Data is invalid
andrewboyson 49:d056e2c5c4ee 40 {
andrewboyson 49:d056e2c5c4ee 41 lastCause = RESTART_CAUSE_INVALID_DATA;
andrewboyson 49:d056e2c5c4ee 42 lastZone = 0;
andrewboyson 49:d056e2c5c4ee 43 lastPoint = 0;
andrewboyson 49:d056e2c5c4ee 44 }
andrewboyson 49:d056e2c5c4ee 45
andrewboyson 49:d056e2c5c4ee 46 //If no routine overwrites the cause then we know it must be the push button.
andrewboyson 49:d056e2c5c4ee 47 PERSIST = MAGIC << 24 | RESTART_CAUSE_RESET_BUTTON << 20;
andrewboyson 49:d056e2c5c4ee 48 }
andrewboyson 49:d056e2c5c4ee 49
andrewboyson 49:d056e2c5c4ee 50 void RestartCauseToString(int value, int size, char* text)
andrewboyson 49:d056e2c5c4ee 51 {
andrewboyson 49:d056e2c5c4ee 52 switch (value)
andrewboyson 49:d056e2c5c4ee 53 {
andrewboyson 49:d056e2c5c4ee 54 case RESTART_CAUSE_RESET_BUTTON: strncpy(text, "Reset button" , size); break;
andrewboyson 49:d056e2c5c4ee 55 case RESTART_CAUSE_SOFTWARE_RESET: strncpy(text, "Software reset" , size); break;
andrewboyson 49:d056e2c5c4ee 56 case RESTART_CAUSE_HARD_FAULT: strncpy(text, "Hard fault" , size); break;
andrewboyson 49:d056e2c5c4ee 57 case RESTART_CAUSE_DEFAULT_HANDLER: strncpy(text, "Default handler" , size); break;
andrewboyson 49:d056e2c5c4ee 58 case RESTART_CAUSE_INVALID_DATA: strncpy(text, "Invalid magic number", size); break;
andrewboyson 49:d056e2c5c4ee 59 default: snprintf(text, size, "%d", value ); break;
andrewboyson 49:d056e2c5c4ee 60 }
andrewboyson 49:d056e2c5c4ee 61 }
andrewboyson 49:d056e2c5c4ee 62 void RestartZoneToString(int value, int size, char* text)
andrewboyson 49:d056e2c5c4ee 63 {
andrewboyson 49:d056e2c5c4ee 64 switch (value)
andrewboyson 49:d056e2c5c4ee 65 {
andrewboyson 49:d056e2c5c4ee 66 case RESTART_ZONE_NONE: strncpy(text, "None" , size); break;
andrewboyson 49:d056e2c5c4ee 67 case RESTART_ZONE_INIT: strncpy(text, "Init" , size); break;
andrewboyson 49:d056e2c5c4ee 68 case RESTART_ZONE_NET: strncpy(text, "Net" , size); break;
andrewboyson 49:d056e2c5c4ee 69 case RESTART_ZONE_CLOCK: strncpy(text, "Clock" , size); break;
andrewboyson 49:d056e2c5c4ee 70 default: snprintf(text, size, "%d", value); break;
andrewboyson 49:d056e2c5c4ee 71 }
andrewboyson 49:d056e2c5c4ee 72 }