9 #include "../firmware.h"
11 static buffer_cache_t buffer_cache = {
false, 0, 0, {0}, {0}, 0x0000 };
13 void Buffer_init(
void)
15 if(buffer_cache.initialised && !Buffer_verify_cache())
20 else if(!buffer_cache.initialised)
22 if(!Buffer_FRAM_cache_read(&buffer_cache))
29 buffer_cache.initialised =
true;
33 bool Buffer_verify_cache(
void)
38 void Buffer_reset(
void)
40 buffer_cache.last_index_stored = 0;
41 Buffer_FRAM_write_last_index_stored(&buffer_cache);
43 buffer_cache.last_slot_transmitted = (BUFFER_SLOTS-1);
44 Buffer_FRAM_write_last_slot_transmitted(&buffer_cache);
46 memset(buffer_cache.occupancy, 0x00, BUFFER_FRAM_SIZE_OCCUPANCY);
47 Buffer_FRAM_write_occupancy(&buffer_cache);
49 memset(buffer_cache.indexes, 0x00, BUFFER_FRAM_SIZE_INDEXES);
50 Buffer_FRAM_write_indexes(&buffer_cache);
55 void Buffer_store_new_data(uint8_t *data_payload)
62 new_index = (uint16_t)(buffer_cache.last_index_stored + 1);
64 Buffer_find_new_slot(&new_slot);
65 Buffer_FRAM_write_data(new_slot, data_payload);
66 Buffer_set_index(new_slot, new_index);
67 Buffer_set_occupancy(new_slot,
true);
70 void Buffer_set_index(uint16_t slot, uint16_t index)
72 buffer_cache.indexes[slot] = index;
73 Buffer_FRAM_write_indexes(&buffer_cache);
76 bool Buffer_get_next_data(uint8_t *data_payload)
80 if(Buffer_get_next_slot(&(buffer_cache.last_slot_transmitted)))
82 Buffer_FRAM_read_data(buffer_cache.last_slot_transmitted, data_payload);
88 bool Buffer_get_next_slot(uint16_t *slot)
90 uint16_t new_slot = *slot;
92 if(++new_slot >= BUFFER_SLOTS)
98 while(new_slot != *slot)
101 if(0x00 != (buffer_cache.occupancy[new_slot>>3] & (0x80 >> (new_slot & 0x07))))
107 if(++new_slot >= BUFFER_SLOTS)
114 if(0x00 != (buffer_cache.occupancy[new_slot>>3] & (0x80 >> (new_slot & 0x07))))
123 void Buffer_remove_index(uint16_t index)
126 if(Buffer_find_index(index, &slot))
128 Buffer_set_occupancy(slot,
false);
132 bool Buffer_find_index(uint16_t index, uint16_t *slot)
135 while(buffer_cache.indexes[*slot] != index)
137 if(++*slot >= BUFFER_SLOTS)
145 void Buffer_find_new_slot(uint16_t *slot)
148 while (buffer_cache.occupancy[*slot>>3] & (0x80 >> (*slot & 0x07)))
150 if(++*slot >= BUFFER_SLOTS)
153 Buffer_find_oldest_slot(slot);
159 void Buffer_find_oldest_slot(uint16_t *slot)
161 uint16_t oldest_slot, oldest_index = buffer_cache.last_index_stored;
164 while(++*slot < BUFFER_SLOTS)
166 if(oldest_index <= buffer_cache.last_index_stored)
168 if(buffer_cache.indexes[*slot] < oldest_index)
170 oldest_index = buffer_cache.indexes[*slot];
173 else if(buffer_cache.indexes[*slot] > buffer_cache.last_index_stored)
175 oldest_index = buffer_cache.indexes[*slot];
179 else if(buffer_cache.indexes[*slot] > buffer_cache.last_index_stored)
181 if(buffer_cache.indexes[*slot] < oldest_index)
183 oldest_index = buffer_cache.indexes[*slot];
193 bool Buffer_get_occupancy(uint16_t slot)
195 return buffer_cache.occupancy[slot>>3] & (0x80 >> (slot & 0x07));
198 void Buffer_set_occupancy(uint16_t slot,
bool value)
203 buffer_cache.occupancy[slot>>3] = (uint8_t)(buffer_cache.occupancy[slot>>3] | (0x80 >> (slot & 0x07)));
208 buffer_cache.occupancy[slot>>3] = (uint8_t)(buffer_cache.occupancy[slot>>3] & ~(0x80 >> (slot & 0x07)));
210 Buffer_FRAM_write_occupancy(&buffer_cache);
213 uint16_t Buffer_count_occupied(
void)
220 for(i=0; i<BUFFER_SLOTS; i++)
222 if(buffer_cache.occupancy[i>>3] & (0x80 >> (i & 0x07)))
uint16_t Util_crc16(const uint8_t *buf, uint32_t size)