UoS³ Flight Computer Firmware
 All Data Structures Files Functions Groups Pages
buffer.h
1 
11 #ifndef __BUFFER_H__
12 #define __BUFFER_H__
13 
14 #define BUFFER_SLOT_SIZE (uint32_t)848
15 
16 #if(FRAM_SIZE == 0x20000)
17  #define BUFFER_SLOTS 1210
18 #elif(FRAM_SIZE == 0x80000)
19  #define BUFFER_SLOTS 4840
20 #else
21  #error "FRAM Size not coded for."
22 #endif
23 
24 #define BUFFER_FRAM_SIZE_INITIALISED sizeof(bool)
25 #define BUFFER_FRAM_SIZE_LAST_INDEX sizeof(uint16_t)
26 #define BUFFER_FRAM_SIZE_LAST_SLOT sizeof(uint16_t)
27 #define BUFFER_FRAM_SIZE_OCCUPANCY ROUND_UP(BUFFER_SLOTS/8, 1)
28 #define BUFFER_FRAM_SIZE_INDEXES (BUFFER_SLOTS * sizeof(uint16_t))
29 #define BUFFER_FRAM_SIZE_CRC sizeof(uint16_t)
30 
31 #define BUFFER_FRAM_ADDRESS_INITIALISED 0x000000
32 #define BUFFER_FRAM_ADDRESS_LAST_INDEX (BUFFER_FRAM_ADDRESS_INITIALISED + BUFFER_FRAM_SIZE_INITIALISED)
33 #define BUFFER_FRAM_ADDRESS_LAST_SLOT (BUFFER_FRAM_ADDRESS_LAST_INDEX + BUFFER_FRAM_SIZE_LAST_INDEX)
34 #define BUFFER_FRAM_ADDRESS_OCCUPANCY (BUFFER_FRAM_ADDRESS_LAST_SLOT + BUFFER_FRAM_SIZE_LAST_SLOT)
35 #define BUFFER_FRAM_ADDRESS_INDEXES (BUFFER_FRAM_ADDRESS_OCCUPANCY + BUFFER_FRAM_SIZE_OCCUPANCY)
36 #define BUFFER_FRAM_ADDRESS_CRC (BUFFER_FRAM_ADDRESS_INDEXES + BUFFER_FRAM_SIZE_INDEXES)
37 #define BUFFER_FRAM_ADDRESS_SLOTS (BUFFER_FRAM_ADDRESS_CRC + BUFFER_FRAM_SIZE_CRC)
38 
39 /* Illustrative struct of FRAM data layout:
40 typedef struct buffer_data {
41  uint16_t last_index;
42  uint8_t occupancy[ROUND_UP(BUFFER_SLOTS/8, 1)];
43  uint16_t indexes[BUFFER_SLOTS];
44  buffer_data_slot slots[BUFFER_SLOTS];
45 } buffer_data;
46 */
47 
48 typedef struct buffer_cache_t {
49  bool initialised;
50  uint16_t last_index_stored;
51  uint16_t last_slot_transmitted;
52  uint8_t occupancy[ROUND_UP(BUFFER_SLOTS/8, 1)]; // bitmap of occupancy
53  uint16_t indexes[BUFFER_SLOTS]; // indexes[slot] = index
54  uint16_t crc;
56 
57 void Buffer_init(void);
58 void Buffer_reset(void);
59 bool Buffer_verify_cache(void);
60 
61 void Buffer_store_new_data(uint8_t *data_payload);
62 bool Buffer_get_next_data(uint8_t *data_payload);
63 void Buffer_remove_index(uint16_t index);
64 uint16_t Buffer_count_occupied(void);
65 
66 
67 bool Buffer_get_next_slot(uint16_t *slot);
68 bool Buffer_find_index(uint16_t index, uint16_t *slot);
69 void Buffer_find_new_slot(uint16_t *slot);
70 void Buffer_find_oldest_slot(uint16_t *slot);
71 bool Buffer_get_occupancy(uint16_t slot);
72 void Buffer_set_occupancy(uint16_t slot, bool value);
73 void Buffer_set_index(uint16_t slot, uint16_t index);
74 
75 bool Buffer_FRAM_cache_read(buffer_cache_t *buffer);
76 void Buffer_FRAM_read_data(uint16_t slot, uint8_t *data);
77 
78 void Buffer_FRAM_write_last_index_stored(buffer_cache_t *buffer);
79 void Buffer_FRAM_write_last_slot_transmitted(buffer_cache_t *buffer);
80 void Buffer_FRAM_write_occupancy(buffer_cache_t *buffer);
81 void Buffer_FRAM_write_indexes(buffer_cache_t *buffer);
82 void Buffer_FRAM_write_data(uint16_t slot, uint8_t *data);
83 
84 #endif /* __BUFFER_H__ */