KM3NeT CLB  2.0
KM3NeT CLB v2 Embedded Software
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
blockstore.c
1 /*
2  * KM3NeT CLB v2 Firmware
3  * ----------------------
4  *
5  * Copyright 2012-2014 KM3NeT Collaboration
6  *
7  * All Rights Reserved.
8  *
9  *
10  * File : blockstore.c
11  * Created : 26 nov. 2014
12  * Author : Vincent van Beveren
13  */
14 
15 #include "kernel/blockstore.h"
16 #include "cfg_soc.h"
17 
18 #include <stdint.h>
19 #include <assert.h>
20 #include "util/log.h"
21 #include "kernel/err.h"
22 #include "drv/spi/sflash.h"
23 
24 #define _MAGIC 0xAC55
25 #define _CLEAR 0xFFFF
26 
27 LOG_DEF("BlockStore")
28 
29 typedef struct {
30  uint16_t magic;
31  uint16_t len;
32 } BlockHeader;
33 
34 static inline uint32_t toAddress(unsigned int block) {
35  return BLOCKS_OFFSET + block * BLOCKS_SPACING;
36 }
37 
38 #define _CHK_ERROR -1 // There was an error
39 #define _CHK_EQUAL 0 // Data to be written equals data, no write required
40 #define _CHK_ERASE 1 // Block must be erased prior to writing
41 #define _CHK_WRITE 2 // Block is already empty and can be written
42 
43 #define _BUF_SIZE 16
44 
45 
46 bool bsSave(unsigned int block, void * data, size_t len)
47 {
48  uint32_t addr = toAddress(block);
49 
50  BlockHeader hdr = {
51  .magic = _MAGIC,
52  .len = len
53  };
54 
55 
56  if (!sfErase(addr)) return false;
57  if (!sfProg(addr, (void *)&hdr, sizeof(BlockHeader))) return false;
58  if (!sfProgXPage(addr + sizeof(BlockHeader), data, len)) return false;
59 
60  return true;
61 }
62 
63 bool bsCheck(unsigned int block, size_t len, bool * hasValidData)
64 {
65  uint32_t addr = toAddress(block);
66  BlockHeader hdr;
67  if (!sfRead(addr, (void *)&hdr, sizeof(hdr))) return errRebase(_logModInfo.name);
68 
69  if (hdr.magic != _MAGIC || (len > 0 && hdr.len != len))
70  {
71  *hasValidData = false;
72  } else {
73  *hasValidData = true;
74  }
75 
76  return true;
77 }
78 
79 bool bsLoad(unsigned int block, void * data, size_t len)
80 {
81  uint32_t addr = toAddress(block);
82  BlockHeader hdr;
83  if (!sfRead(addr,(void *) &hdr, sizeof(hdr))) return errRebase(_logModInfo.name);
84 
85  if (hdr.magic != _MAGIC) return errSet(ERROR_CTX(E_BS_INVALID));
86  if (hdr.len != len) return errSet(ERROR_CTX(E_BS_LEN_MISMATCH));
87 
88  return errCondRebase(sfRead(addr + sizeof(hdr), data, len), _logModInfo.name);
89 }
90 
91 bool bsErase(unsigned int block)
92 {
93  uint32_t addr = toAddress(block);
94  return sfErase(addr);
95 }
Defines the configuration of the LM32 SOC for the CLBv2.
Allows for storage of persistent information in flash.
#define E_BS_INVALID
Block storage is not valid. Must erase.
Definition: blockstore.h:34
bool bsCheck(unsigned int block, size_t len, bool *hasValidData)
Checks the contents of a block.
Definition: blockstore.c:63
static bool errCondRebase(bool err, const char *name)
Transparent conditional error rebase.
Definition: err.h:117
bool bsLoad(unsigned int block, void *data, size_t len)
Loads a block of data from the flash.
Definition: blockstore.c:79
Manages the global system error.
bool sfErase(uint32_t address)
Erase a sector in flash.
Definition: sflash.c:564
bool bsSave(unsigned int block, void *data, size_t len)
Saves a block of data on the internal flash.
Definition: blockstore.c:46
#define BLOCKS_OFFSET
The block space offset.
Definition: cfg_soc.h:164
bool bsErase(unsigned int block)
Erases all data.
Definition: blockstore.c:91
bool sfProgXPage(uint32_t address, uint8_t *data, uint32_t count)
Program cross pages.
Definition: sflash.c:547
#define LOG_DEF(NAME,...)
Define a logger for a module.
Definition: log.h:129
static bool errRebase(const char *name)
Rebases the cause of the error message.
Definition: err.h:104
bool sfRead(uint32_t address, uint8_t *data, uint32_t count)
Read from a specific address in flash.
Definition: sflash.c:491
bool errSet(uint32_t code, const char *error, const char *name)
Sets an error.
This driver implements access to the Serial Flash.
Implements a generic logger facility.
bool sfProg(uint32_t address, uint8_t *data, uint32_t count)
Program an page in flash.
Definition: sflash.c:519
#define E_BS_LEN_MISMATCH
Buffer does not match the size of the allocated area.
Definition: blockstore.h:38