SingingCat 0
application
constants.c
Go to the documentation of this file.
1#include "constants.h"
2#include "checksums.h"
7static int CRC_SIZE;
8
13extern uint32_t permaramadr;
14
15struct consts *constants() {
16 return (struct consts *)&permaramadr;
17}
18
19static void set_checksum() {
20 uint32_t c = calc_crc32((void *)constants(), CRC_SIZE, 4);
21
22 constants()->crc32 = c;
23}
24
25// returns true if constants were reset
26int constants_init() {
27 CRC_SIZE = offsetof(struct consts, end_of_checksum);
28 void *v = (void *)constants();
29
30 if (constants()->magic == CONSTANTS_MAGIC) {
31 uint32_t c = calc_crc32(v, CRC_SIZE, 4);
32 if (c == constants()->crc32) {
33 return 0;
34 }
35 }
36 memset(v, 0, sizeof(struct consts));
37 constants()->magic = CONSTANTS_MAGIC;
38 set_checksum();
39 return 1;
40}
41
42// after each update to constants you *must* call this
43void constants_validate() {
44 if (constants()->magic != CONSTANTS_MAGIC) {
45 // if it isn't magic, then just rewrite it out
46 constants_init();
47 return;
48 }
49 set_checksum();
50}
51
52void constants_print() {
53 struct consts *c = constants();
54
55 printf("Constants (checksum size: %i:\r\n", CRC_SIZE);
56 if (constants()->magic != CONSTANTS_MAGIC) {
57 printf("invalid constants magic\r\n");
58 return;
59 }
60 printf("last_return_address: %p\r\n", c->last_return_address);
61 printf("failing_address : %p\r\n", c->failing_address);
62 printf("CFSR : %p\r\n", c->CFSR);
63 printf("SHCSR : %p\r\n", c->SHCSR);
64 printf("MMFAR : %p\r\n", c->MMFAR);
65 printf("HFSR : %p\r\n", c->HFSR);
66 printf("BFAR : %p\r\n", c->BFAR);
67 printf("reset_counter : %i\r\n", c->reset_counter);
68 printf("in_user_app : %i\r\n", c->in_user_app);
69 uint32_t ch = calc_crc32((uint8_t *)c, CRC_SIZE, 4);
70
71 if (ch == c->crc32) {
72 printf("Checksum : OK\r\n");
73 } else {
74 printf("Checksum : FAIL\r\n");
75 }
76}