FLASH HAL: use 'bank' instead of 'sector'

This commit is contained in:
Matthias Ringwald 2017-08-16 15:58:03 +02:00
parent c7f4b25f7a
commit 17ae5bc4f4
17 changed files with 281 additions and 278 deletions

View File

@ -29,10 +29,10 @@
*
*/
#define __BTSTACK_FILE__ "btstack_tlv_flash_sector.c"
#define __BTSTACK_FILE__ "btstack_tlv_flash_bank.c"
#include "btstack_tlv.h"
#include "btstack_tlv_flash_sector.h"
#include "btstack_tlv_flash_bank.h"
#include "btstack_debug.h"
#include "btstack_util.h"
#include "btstack_debug.h"
@ -62,44 +62,44 @@ typedef struct {
uint32_t len;
} tlv_iterator_t;
static void btstack_tlv_flash_sector_iterator_fetch_tag_len(btstack_tlv_flash_sector_t * self, tlv_iterator_t * it){
static void btstack_tlv_flash_bank_iterator_fetch_tag_len(btstack_tlv_flash_bank_t * self, tlv_iterator_t * it){
uint8_t entry[8];
self->hal_flash_sector_impl->read(self->hal_flash_sector_context, it->bank, it->offset, entry, 8);
self->hal_flash_bank_impl->read(self->hal_flash_bank_context, it->bank, it->offset, entry, 8);
it->tag = big_endian_read_32(entry, 0);
it->len = big_endian_read_32(entry, 4);
}
static void btstack_tlv_flash_sector_iterator_init(btstack_tlv_flash_sector_t * self, tlv_iterator_t * it, int bank){
static void btstack_tlv_flash_bank_iterator_init(btstack_tlv_flash_bank_t * self, tlv_iterator_t * it, int bank){
memset(it, 0, sizeof(tlv_iterator_t));
it->bank = bank;
it->offset = BTSTACK_TLV_HEADER_LEN;
btstack_tlv_flash_sector_iterator_fetch_tag_len(self, it);
btstack_tlv_flash_bank_iterator_fetch_tag_len(self, it);
}
static int btstack_tlv_flash_sector_iterator_has_next(btstack_tlv_flash_sector_t * self, tlv_iterator_t * it){
static int btstack_tlv_flash_bank_iterator_has_next(btstack_tlv_flash_bank_t * self, tlv_iterator_t * it){
if (it->tag == 0xffffffff) return 0;
return 1;
}
static void tlv_iterator_fetch_next(btstack_tlv_flash_sector_t * self, tlv_iterator_t * it){
static void tlv_iterator_fetch_next(btstack_tlv_flash_bank_t * self, tlv_iterator_t * it){
it->offset += 8 + it->len;
if (it->offset >= self->hal_flash_sector_impl->get_size(self->hal_flash_sector_context)) {
if (it->offset >= self->hal_flash_bank_impl->get_size(self->hal_flash_bank_context)) {
it->tag = 0xffffffff;
it->len = 0;
return;
}
btstack_tlv_flash_sector_iterator_fetch_tag_len(self, it);
btstack_tlv_flash_bank_iterator_fetch_tag_len(self, it);
}
//
// check both banks for headers and pick the one with the higher epoch % 4
// @returns bank or -1 if something is invalid
static int btstack_tlv_flash_sector_get_latest_bank(btstack_tlv_flash_sector_t * self){
static int btstack_tlv_flash_bank_get_latest_bank(btstack_tlv_flash_bank_t * self){
uint8_t header0[BTSTACK_TLV_HEADER_LEN];
uint8_t header1[BTSTACK_TLV_HEADER_LEN];
self->hal_flash_sector_impl->read(self->hal_flash_sector_context, 0, 0, &header0[0], BTSTACK_TLV_HEADER_LEN);
self->hal_flash_sector_impl->read(self->hal_flash_sector_context, 1, 0, &header1[0], BTSTACK_TLV_HEADER_LEN);
self->hal_flash_bank_impl->read(self->hal_flash_bank_context, 0, 0, &header0[0], BTSTACK_TLV_HEADER_LEN);
self->hal_flash_bank_impl->read(self->hal_flash_bank_context, 1, 0, &header1[0], BTSTACK_TLV_HEADER_LEN);
int valid0 = memcmp(header0, btstack_tlv_header_magic, BTSTACK_TLV_HEADER_LEN-1) == 0;
int valid1 = memcmp(header1, btstack_tlv_header_magic, BTSTACK_TLV_HEADER_LEN-1) == 0;
if (!valid0 && !valid1) return -1;
@ -112,25 +112,25 @@ static int btstack_tlv_flash_sector_get_latest_bank(btstack_tlv_flash_sector_t *
return -1; // invalid, must not happen
}
static void btstack_tlv_flash_sector_write_header(btstack_tlv_flash_sector_t * self, int bank, int epoch){
static void btstack_tlv_flash_bank_write_header(btstack_tlv_flash_bank_t * self, int bank, int epoch){
uint8_t header[BTSTACK_TLV_HEADER_LEN];
memcpy(&header[0], btstack_tlv_header_magic, BTSTACK_TLV_HEADER_LEN-1);
header[BTSTACK_TLV_HEADER_LEN-1] = epoch;
self->hal_flash_sector_impl->write(self->hal_flash_sector_context, bank, 0, header, BTSTACK_TLV_HEADER_LEN);
self->hal_flash_bank_impl->write(self->hal_flash_bank_context, bank, 0, header, BTSTACK_TLV_HEADER_LEN);
}
/**
* @brief Check if erased from offset
*/
static int btstack_tlv_flash_sector_test_erased(btstack_tlv_flash_sector_t * self, int bank, uint32_t offset){
static int btstack_tlv_flash_bank_test_erased(btstack_tlv_flash_bank_t * self, int bank, uint32_t offset){
log_info("test erased: bank %u, offset %u", bank, offset);
uint32_t size = self->hal_flash_sector_impl->get_size(self->hal_flash_sector_context);
uint32_t size = self->hal_flash_bank_impl->get_size(self->hal_flash_bank_context);
uint8_t buffer[16];
uint8_t empty16[16];
memset(empty16, 0xff, sizeof(empty16));
while (offset < size){
uint32_t copy_size = (offset + sizeof(empty16) < size) ? sizeof(empty16) : (size - offset);
self->hal_flash_sector_impl->read(self->hal_flash_sector_context, bank, offset, buffer, copy_size);
self->hal_flash_bank_impl->read(self->hal_flash_bank_context, bank, offset, buffer, copy_size);
if (memcmp(buffer, empty16, copy_size)) {
log_info("not erased %x - %x", offset, offset + copy_size);
return 0;
@ -143,26 +143,26 @@ static int btstack_tlv_flash_sector_test_erased(btstack_tlv_flash_sector_t * sel
/**
* @brief erase bank (only if not already erased)
*/
static void btstack_tlv_flash_sector_erase_bank(btstack_tlv_flash_sector_t * self, int bank){
if (btstack_tlv_flash_sector_test_erased(self, bank, 0)){
static void btstack_tlv_flash_bank_erase_bank(btstack_tlv_flash_bank_t * self, int bank){
if (btstack_tlv_flash_bank_test_erased(self, bank, 0)){
log_info("bank %u already erased", bank);
} else {
log_info("bank %u not empty, erase bank", bank);
self->hal_flash_sector_impl->erase(self->hal_flash_sector_context, bank);
self->hal_flash_bank_impl->erase(self->hal_flash_bank_context, bank);
}
}
static void btstack_tlv_flash_sector_migrate(btstack_tlv_flash_sector_t * self){
static void btstack_tlv_flash_bank_migrate(btstack_tlv_flash_bank_t * self){
int next_bank = 1 - self->current_bank;
log_info("migrate bank %u -> bank %u", self->current_bank, next_bank);
// erase bank (if needed)
btstack_tlv_flash_sector_erase_bank(self, next_bank);
btstack_tlv_flash_bank_erase_bank(self, next_bank);
int next_write_pos = 8;
tlv_iterator_t it;
btstack_tlv_flash_sector_iterator_init(self, &it, self->current_bank);
while (btstack_tlv_flash_sector_iterator_has_next(self, &it)){
btstack_tlv_flash_bank_iterator_init(self, &it, self->current_bank);
while (btstack_tlv_flash_bank_iterator_has_next(self, &it)){
// skip deleted entries
if (it.tag) {
uint32_t tag_len = it.len;
@ -174,8 +174,8 @@ static void btstack_tlv_flash_sector_migrate(btstack_tlv_flash_sector_t * self){
uint8_t copy_buffer[32];
while (bytes_to_copy){
int bytes_this_iteration = btstack_min(bytes_to_copy, sizeof(copy_buffer));
self->hal_flash_sector_impl->read(self->hal_flash_sector_context, self->current_bank, tag_index, copy_buffer, bytes_this_iteration);
self->hal_flash_sector_impl->write(self->hal_flash_sector_context, next_bank, next_write_pos, copy_buffer, bytes_this_iteration);
self->hal_flash_bank_impl->read(self->hal_flash_bank_context, self->current_bank, tag_index, copy_buffer, bytes_this_iteration);
self->hal_flash_bank_impl->write(self->hal_flash_bank_context, next_bank, next_write_pos, copy_buffer, bytes_this_iteration);
tag_index += bytes_this_iteration;
next_write_pos += bytes_this_iteration;
bytes_to_copy -= bytes_this_iteration;
@ -186,15 +186,15 @@ static void btstack_tlv_flash_sector_migrate(btstack_tlv_flash_sector_t * self){
// prepare new one
uint8_t epoch_buffer;
self->hal_flash_sector_impl->read(self->hal_flash_sector_context, self->current_bank, BTSTACK_TLV_HEADER_LEN-1, &epoch_buffer, 1);
btstack_tlv_flash_sector_write_header(self, next_bank, (epoch_buffer + 1) & 3);
self->hal_flash_bank_impl->read(self->hal_flash_bank_context, self->current_bank, BTSTACK_TLV_HEADER_LEN-1, &epoch_buffer, 1);
btstack_tlv_flash_bank_write_header(self, next_bank, (epoch_buffer + 1) & 3);
self->current_bank = next_bank;
self->write_offset = next_write_pos;
}
// returns 1 == ok
static int btstack_tlv_flash_sector_verify_alignment(btstack_tlv_flash_sector_t * self, uint32_t value_size){
uint32_t aligment = self->hal_flash_sector_impl->get_alignment(self->hal_flash_sector_context);
static int btstack_tlv_flash_bank_verify_alignment(btstack_tlv_flash_bank_t * self, uint32_t value_size){
uint32_t aligment = self->hal_flash_bank_impl->get_alignment(self->hal_flash_bank_context);
if (value_size % aligment){
log_error("Value size %u not a multiply of flash alignment %u", value_size, aligment);
return 0;
@ -202,15 +202,15 @@ static int btstack_tlv_flash_sector_verify_alignment(btstack_tlv_flash_sector_t
return 1;
}
static void btstack_tlv_flash_sector_delete_tag_until_offset(btstack_tlv_flash_sector_t * self, uint32_t tag, uint32_t offset){
static void btstack_tlv_flash_bank_delete_tag_until_offset(btstack_tlv_flash_bank_t * self, uint32_t tag, uint32_t offset){
tlv_iterator_t it;
btstack_tlv_flash_sector_iterator_init(self, &it, self->current_bank);
while (btstack_tlv_flash_sector_iterator_has_next(self, &it) && it.offset < offset){
btstack_tlv_flash_bank_iterator_init(self, &it, self->current_bank);
while (btstack_tlv_flash_bank_iterator_has_next(self, &it) && it.offset < offset){
if (it.tag == tag){
log_info("Erase tag '%x' at position %u", tag, it.offset);
// overwrite tag with invalid tag
uint32_t zero_tag = 0;
self->hal_flash_sector_impl->write(self->hal_flash_sector_context, self->current_bank, it.offset, (uint8_t*) &zero_tag, sizeof(zero_tag));
self->hal_flash_bank_impl->write(self->hal_flash_bank_context, self->current_bank, it.offset, (uint8_t*) &zero_tag, sizeof(zero_tag));
}
tlv_iterator_fetch_next(self, &it);
}
@ -223,18 +223,18 @@ static void btstack_tlv_flash_sector_delete_tag_until_offset(btstack_tlv_flash_s
* @param buffer_size
* @returns size of value
*/
static int btstack_tlv_flash_sector_get_tag(void * context, uint32_t tag, uint8_t * buffer, uint32_t buffer_size){
static int btstack_tlv_flash_bank_get_tag(void * context, uint32_t tag, uint8_t * buffer, uint32_t buffer_size){
btstack_tlv_flash_sector_t * self = (btstack_tlv_flash_sector_t *) context;
btstack_tlv_flash_bank_t * self = (btstack_tlv_flash_bank_t *) context;
// abort if data size not aligned with flash requirements
if (!btstack_tlv_flash_sector_verify_alignment(self, buffer_size)) return 0;
if (!btstack_tlv_flash_bank_verify_alignment(self, buffer_size)) return 0;
uint32_t tag_index = 0;
uint32_t tag_len = 0;
tlv_iterator_t it;
btstack_tlv_flash_sector_iterator_init(self, &it, self->current_bank);
while (btstack_tlv_flash_sector_iterator_has_next(self, &it)){
btstack_tlv_flash_bank_iterator_init(self, &it, self->current_bank);
while (btstack_tlv_flash_bank_iterator_has_next(self, &it)){
if (it.tag == tag){
log_info("Found tag '%x' at position %u", tag, it.offset);
tag_index = it.offset;
@ -246,7 +246,7 @@ static int btstack_tlv_flash_sector_get_tag(void * context, uint32_t tag, uint8_
if (tag_index == 0) return 0;
if (!buffer) return tag_len;
int copy_size = btstack_min(buffer_size, tag_len);
self->hal_flash_sector_impl->read(self->hal_flash_sector_context, self->current_bank, tag_index + 8, buffer, copy_size);
self->hal_flash_bank_impl->read(self->hal_flash_bank_context, self->current_bank, tag_index + 8, buffer, copy_size);
return copy_size;
}
@ -256,19 +256,19 @@ static int btstack_tlv_flash_sector_get_tag(void * context, uint32_t tag, uint8_
* @param data
* @param data_size
*/
static int btstack_tlv_flash_sector_store_tag(void * context, uint32_t tag, const uint8_t * data, uint32_t data_size){
static int btstack_tlv_flash_bank_store_tag(void * context, uint32_t tag, const uint8_t * data, uint32_t data_size){
btstack_tlv_flash_sector_t * self = (btstack_tlv_flash_sector_t *) context;
btstack_tlv_flash_bank_t * self = (btstack_tlv_flash_bank_t *) context;
// abort if data size not aligned with flash requirements
if (!btstack_tlv_flash_sector_verify_alignment(self, data_size)) return 1;
if (!btstack_tlv_flash_bank_verify_alignment(self, data_size)) return 1;
// trigger migration if not enough space
if (self->write_offset + 8 + data_size > self->hal_flash_sector_impl->get_size(self->hal_flash_sector_context)){
btstack_tlv_flash_sector_migrate(self);
if (self->write_offset + 8 + data_size > self->hal_flash_bank_impl->get_size(self->hal_flash_bank_context)){
btstack_tlv_flash_bank_migrate(self);
}
if (self->write_offset + 8 + data_size > self->hal_flash_sector_impl->get_size(self->hal_flash_sector_context)){
if (self->write_offset + 8 + data_size > self->hal_flash_bank_impl->get_size(self->hal_flash_bank_context)){
log_error("couldn't write entry, not enough space left");
return 2;
}
@ -281,43 +281,45 @@ static int btstack_tlv_flash_sector_store_tag(void * context, uint32_t tag, cons
log_info("write '%x', len %u at %u", tag, data_size, self->write_offset);
// write value first
self->hal_flash_sector_impl->write(self->hal_flash_sector_context, self->current_bank, self->write_offset + 8, data, data_size);
self->hal_flash_bank_impl->write(self->hal_flash_bank_context, self->current_bank, self->write_offset + 8, data, data_size);
// then entry
self->hal_flash_sector_impl->write(self->hal_flash_sector_context, self->current_bank, self->write_offset, entry, sizeof(entry));
self->hal_flash_bank_impl->write(self->hal_flash_bank_context, self->current_bank, self->write_offset, entry, sizeof(entry));
// overwrite old entries (if exists)
btstack_tlv_flash_sector_delete_tag_until_offset(self, tag, self->write_offset);
btstack_tlv_flash_bank_delete_tag_until_offset(self, tag, self->write_offset);
// done
self->write_offset += sizeof(entry) + data_size;
return 0;
}
/**
* Delete Tag
* @param tag
*/
static void btstack_tlv_flash_sector_delete_tag(void * context, uint32_t tag){
btstack_tlv_flash_sector_t * self = (btstack_tlv_flash_sector_t *) context;
btstack_tlv_flash_sector_delete_tag_until_offset(self, tag, self->write_offset);
static void btstack_tlv_flash_bank_delete_tag(void * context, uint32_t tag){
btstack_tlv_flash_bank_t * self = (btstack_tlv_flash_bank_t *) context;
btstack_tlv_flash_bank_delete_tag_until_offset(self, tag, self->write_offset);
}
static const btstack_tlv_t btstack_tlv_flash_sector = {
/* int (*get_tag)(..); */ &btstack_tlv_flash_sector_get_tag,
/* int (*store_tag)(..); */ &btstack_tlv_flash_sector_store_tag,
/* void (*delete_tag)(v..); */ &btstack_tlv_flash_sector_delete_tag,
static const btstack_tlv_t btstack_tlv_flash_bank = {
/* int (*get_tag)(..); */ &btstack_tlv_flash_bank_get_tag,
/* int (*store_tag)(..); */ &btstack_tlv_flash_bank_store_tag,
/* void (*delete_tag)(v..); */ &btstack_tlv_flash_bank_delete_tag,
};
/**
* Init Tag Length Value Store
*/
const btstack_tlv_t * btstack_tlv_flash_sector_init_instance(btstack_tlv_flash_sector_t * self, const hal_flash_sector_t * hal_flash_sector_impl, void * hal_flash_sector_context){
const btstack_tlv_t * btstack_tlv_flash_bank_init_instance(btstack_tlv_flash_bank_t * self, const hal_flash_bank_t * hal_flash_bank_impl, void * hal_flash_bank_context){
self->hal_flash_sector_impl = hal_flash_sector_impl;
self->hal_flash_sector_context = hal_flash_sector_context;
self->hal_flash_bank_impl = hal_flash_bank_impl;
self->hal_flash_bank_context = hal_flash_bank_context;
// try to find current bank
self->current_bank = btstack_tlv_flash_sector_get_latest_bank(self);
self->current_bank = btstack_tlv_flash_bank_get_latest_bank(self);
log_info("found bank %d", self->current_bank);
if (self->current_bank >= 0){
@ -325,27 +327,27 @@ const btstack_tlv_t * btstack_tlv_flash_sector_init_instance(btstack_tlv_flash_s
tlv_iterator_t it;
uint32_t last_tag = 0;
uint32_t last_offset = 0;
btstack_tlv_flash_sector_iterator_init(self, &it, self->current_bank);
while (btstack_tlv_flash_sector_iterator_has_next(self, &it)){
btstack_tlv_flash_bank_iterator_init(self, &it, self->current_bank);
while (btstack_tlv_flash_bank_iterator_has_next(self, &it)){
last_tag = it.tag;
last_offset = it.offset;
tlv_iterator_fetch_next(self, &it);
}
self->write_offset = it.offset;
if (self->write_offset < self->hal_flash_sector_impl->get_size(self->hal_flash_sector_context)){
if (self->write_offset < self->hal_flash_bank_impl->get_size(self->hal_flash_bank_context)){
// delete older instances of last_tag
// this handles the unlikely case where MCU did reset after new value + header was written but before delete did complete
if (last_tag){
btstack_tlv_flash_sector_delete_tag_until_offset(self, last_tag, last_offset);
btstack_tlv_flash_bank_delete_tag_until_offset(self, last_tag, last_offset);
}
// verify that rest of bank is empty
// this handles the unlikely case where MCU did reset after new value was written, but not the tag
if (!btstack_tlv_flash_sector_test_erased(self, self->current_bank, self->write_offset)){
if (!btstack_tlv_flash_bank_test_erased(self, self->current_bank, self->write_offset)){
log_info("Flash not empty after last found tag -> migrate");
btstack_tlv_flash_sector_migrate(self);
btstack_tlv_flash_bank_migrate(self);
} else {
log_info("Flash clean after last found tag");
}
@ -356,13 +358,13 @@ const btstack_tlv_t * btstack_tlv_flash_sector_init_instance(btstack_tlv_flash_s
}
if (self->current_bank < 0) {
btstack_tlv_flash_sector_erase_bank(self, 0);
btstack_tlv_flash_bank_erase_bank(self, 0);
self->current_bank = 0;
btstack_tlv_flash_sector_write_header(self, self->current_bank, 0); // epoch = 0;
btstack_tlv_flash_bank_write_header(self, self->current_bank, 0); // epoch = 0;
self->write_offset = 8;
}
log_info("write offset %u", self->write_offset);
return &btstack_tlv_flash_sector;
return &btstack_tlv_flash_bank;
}

View File

@ -30,39 +30,39 @@
*/
/*
* btstack_tlv_flash_sector.h
* btstack_tlv_flash_bank.h
*
* Implementation for BTstack's Tag Value Length Persistent Storage implementations
* using hal_flash_sector storage
* using hal_flash_bank storage
*/
#ifndef __BTSTACK_TLV_FLASH_SECTOR_H
#define __BTSTACK_TLV_FLASH_SECTOR_H
#ifndef __BTSTACK_TLV_FLASH_BANK_H
#define __BTSTACK_TLV_FLASH_BANK_H
#include <stdint.h>
#include "btstack_tlv.h"
#include "hal_flash_sector.h"
#include "hal_flash_bank.h"
#if defined __cplusplus
extern "C" {
#endif
typedef struct {
const hal_flash_sector_t * hal_flash_sector_impl;
void * hal_flash_sector_context;
const hal_flash_bank_t * hal_flash_bank_impl;
void * hal_flash_bank_context;
int current_bank;
int write_offset;
} btstack_tlv_flash_sector_t;
} btstack_tlv_flash_bank_t;
/**
* Init Tag Length Value Store
* @param context btstack_tlv_flash_sector_t
* @param hal_flash_sector_impl of hal_flash_sector interface
* @Param hal_flash_sector_context of hal_flash_sector_interface
* @param context btstack_tlv_flash_bank_t
* @param hal_flash_bank_impl of hal_flash_bank interface
* @Param hal_flash_bank_context of hal_flash_bank_interface
*/
const btstack_tlv_t * btstack_tlv_flash_sector_init_instance(btstack_tlv_flash_sector_t * context, const hal_flash_sector_t * hal_flash_sector_impl, void * hal_flash_sector_context);
const btstack_tlv_t * btstack_tlv_flash_bank_init_instance(btstack_tlv_flash_bank_t * context, const hal_flash_bank_t * hal_flash_bank_impl, void * hal_flash_bank_context);
#if defined __cplusplus
}
#endif
#endif // __BTSTACK_TLV_FLASH_SECTOR_H
#endif // __BTSTACK_TLV_FLASH_BANK_H

View File

@ -30,14 +30,14 @@
*/
/*
* hal_flash_sector.h
* hal_flash_bank.h
*
* HAL abstraction for Flash memory that can be written anywhere
* after being erased
*/
#ifndef __HAL_FLASH_SECTOR_H
#define __HAL_FLASH_SECTOR_H
#ifndef __HAL_FLASH_BANK_H
#define __HAL_FLASH_BANK_H
#include <stdint.h>
@ -84,9 +84,9 @@ typedef struct {
*/
void (*write)(void * context, int bank, uint32_t offset, const uint8_t * data, uint32_t size);
} hal_flash_sector_t;
} hal_flash_bank_t;
#if defined __cplusplus
}
#endif
#endif // __HAL_FLASH_SECTOR_H
#endif // __HAL_FLASH_BANK_H

View File

@ -30,14 +30,14 @@
*/
/*
* hal_flash_sector_memory.c -- volatile test environment that provides just two memory banks
* hal_flash_bank_memory.c -- volatile test environment that provides just two memory banks
*
*/
#define __BTSTACK_FILE__ "hal_flash_sector_memory.c"
#define __BTSTACK_FILE__ "hal_flash_bank_memory.c"
#include "hal_flash_sector.h"
#include "hal_flash_sector_memory.h"
#include "hal_flash_bank.h"
#include "hal_flash_bank_memory.h"
#include "btstack_debug.h"
#include "stdint.h"
@ -48,24 +48,24 @@
#include <stdlib.h> // exit(..)
#endif
static uint32_t hal_flash_sector_memory_get_size(void * context){
hal_flash_sector_memory_t * self = (hal_flash_sector_memory_t *) context;
static uint32_t hal_flash_bank_memory_get_size(void * context){
hal_flash_bank_memory_t * self = (hal_flash_bank_memory_t *) context;
return self->bank_size;
}
static uint32_t hal_flash_sector_memory_get_alignment(void * context){
static uint32_t hal_flash_bank_memory_get_alignment(void * context){
UNUSED(context);
return 1;
}
static void hal_flash_sector_memory_erase(void * context, int bank){
hal_flash_sector_memory_t * self = (hal_flash_sector_memory_t *) context;
static void hal_flash_bank_memory_erase(void * context, int bank){
hal_flash_bank_memory_t * self = (hal_flash_bank_memory_t *) context;
if (bank > 1) return;
memset(self->banks[bank], 0xff, self->bank_size);
}
static void hal_flash_sector_memory_read(void * context, int bank, uint32_t offset, uint8_t * buffer, uint32_t size){
hal_flash_sector_memory_t * self = (hal_flash_sector_memory_t *) context;
static void hal_flash_bank_memory_read(void * context, int bank, uint32_t offset, uint8_t * buffer, uint32_t size){
hal_flash_bank_memory_t * self = (hal_flash_bank_memory_t *) context;
// log_info("read offset %u, len %u", offset, size);
@ -76,8 +76,8 @@ static void hal_flash_sector_memory_read(void * context, int bank, uint32_t offs
memcpy(buffer, &self->banks[bank][offset], size);
}
static void hal_flash_sector_memory_write(void * context, int bank, uint32_t offset, const uint8_t * data, uint32_t size){
hal_flash_sector_memory_t * self = (hal_flash_sector_memory_t *) context;
static void hal_flash_bank_memory_write(void * context, int bank, uint32_t offset, const uint8_t * data, uint32_t size){
hal_flash_bank_memory_t * self = (hal_flash_bank_memory_t *) context;
log_info("write offset %u, len %u", offset, size);
log_info_hexdump(data, size);
@ -100,22 +100,22 @@ static void hal_flash_sector_memory_write(void * context, int bank, uint32_t off
memcpy(&self->banks[bank][offset], data, size);
}
static const hal_flash_sector_t hal_flash_sector_memory_instance = {
/* uint32_t (*get_size)(..) */ &hal_flash_sector_memory_get_size,
/* uint32_t (*get_alignment)(..); */ &hal_flash_sector_memory_get_alignment,
/* void (*erase)(..); */ &hal_flash_sector_memory_erase,
/* void (*read)(..); */ &hal_flash_sector_memory_read,
/* void (*write)(..); */ &hal_flash_sector_memory_write,
static const hal_flash_bank_t hal_flash_bank_memory_instance = {
/* uint32_t (*get_size)(..) */ &hal_flash_bank_memory_get_size,
/* uint32_t (*get_alignment)(..); */ &hal_flash_bank_memory_get_alignment,
/* void (*erase)(..); */ &hal_flash_bank_memory_erase,
/* void (*read)(..); */ &hal_flash_bank_memory_read,
/* void (*write)(..); */ &hal_flash_bank_memory_write,
};
/**
* Initialize instance
*/
const hal_flash_sector_t * hal_flash_sector_memory_init_instance(hal_flash_sector_memory_t * self, uint8_t * storage, uint32_t storage_size){
const hal_flash_bank_t * hal_flash_bank_memory_init_instance(hal_flash_bank_memory_t * self, uint8_t * storage, uint32_t storage_size){
self->bank_size = storage_size / 2;
self->banks[0] = storage;
self->banks[1] = &storage[self->bank_size];
memset(storage, 0xff, storage_size);
return &hal_flash_sector_memory_instance;
return &hal_flash_bank_memory_instance;
}

View File

@ -30,17 +30,17 @@
*/
/*
* hal_flash_sector.h
* hal_flash_bank.h
*
* HAL abstraction for Flash memory that can be written anywhere
* after being erased implemented with memory
*/
#ifndef __HAL_FLASH_SECTOR_MEMORY_H
#define __HAL_FLASH_SECTOR_MEMORY_H
#ifndef __HAL_FLASH_BANK_MEMORY_H
#define __HAL_FLASH_BANK_MEMORY_H
#include <stdint.h>
#include "hal_flash_sector.h"
#include "hal_flash_bank.h"
#if defined __cplusplus
extern "C" {
@ -50,19 +50,19 @@ extern "C" {
typedef struct {
uint32_t bank_size;
uint8_t * banks[2];
} hal_flash_sector_memory_t;
} hal_flash_bank_memory_t;
// public
/**
* Init instance
* @param context hal_flash_sector_memory_t
* @param context hal_flash_bank_memory_t
* @param storage to use
* @param size of storage
*/
const hal_flash_sector_t * hal_flash_sector_memory_init_instance(hal_flash_sector_memory_t * context, uint8_t * storage, uint32_t storage_size);
const hal_flash_bank_t * hal_flash_bank_memory_init_instance(hal_flash_bank_memory_t * context, uint8_t * storage, uint32_t storage_size);
#if defined __cplusplus
}
#endif
#endif // __HAL_FLASH_SECTOR_MEMORY_H
#endif // __HAL_FLASH_BANK_MEMORY_H

View File

@ -139,8 +139,8 @@ COMMON = \
hci_cmd.o \
hci_dump.o \
btstack_uart_block_embedded.o \
hal_flash_sector_mxc.o \
btstack_tlv_flash_sector.o \
hal_flash_bank_mxc.o \
btstack_tlv_flash_bank.o \
CLASSIC = \
btstack_link_key_db_tlv.o \

View File

@ -275,18 +275,18 @@ void hal_led_on(void){
void hal_led_toggle(void){
}
#include "hal_flash_sector_mxc.h"
#include "hal_flash_bank_mxc.h"
#include "btstack_tlv.h"
#include "btstack_tlv_flash_sector.h"
#include "btstack_tlv_flash_bank.h"
#include "btstack_link_key_db_tlv.h"
#include "le_device_db_tlv.h"
#define HAL_FLASH_SECTOR_SIZE 0x2000
#define HAL_FLASH_SECTOR_BANK_0_ADDR 0x1FC000
#define HAL_FLASH_SECTOR_BANK_1_ADDR 0x1FE000
#define HAL_FLASH_BANK_SIZE 0x2000
#define HAL_FLASH_BANK_0_ADDR 0x1FC000
#define HAL_FLASH_BANK_1_ADDR 0x1FE000
static hal_flash_sector_mxc_t hal_flash_sector_context;
static btstack_tlv_flash_sector_t btstack_tlv_flash_sector_context;
static hal_flash_bank_mxc_t hal_flash_bank_context;
static btstack_tlv_flash_bank_t btstack_tlv_flash_bank_context;
int bluetooth_main(void)
{
@ -300,23 +300,23 @@ int bluetooth_main(void)
hci_init(transport, &config);
hci_set_chipset(btstack_chipset_cc256x_instance());
// setup TLV Flash Sector implementation
const hal_flash_sector_t * hal_flash_sector_impl = hal_flash_sector_mxc_init_instance(
&hal_flash_sector_context,
HAL_FLASH_SECTOR_SIZE,
HAL_FLASH_SECTOR_BANK_0_ADDR,
HAL_FLASH_SECTOR_BANK_1_ADDR);
const btstack_tlv_t * btstack_tlv_impl = btstack_tlv_flash_sector_init_instance(
&btstack_tlv_flash_sector_context,
hal_flash_sector_impl,
&hal_flash_sector_context);
// setup TLV Flash Bank implementation
const hal_flash_bank_t * hal_flash_bank_impl = hal_flash_bank_mxc_init_instance(
&hal_flash_bank_context,
HAL_FLASH_BANK_SIZE,
HAL_FLASH_BANK_0_ADDR,
HAL_FLASH_BANK_1_ADDR);
const btstack_tlv_t * btstack_tlv_impl = btstack_tlv_flash_bank_init_instance(
&btstack_tlv_flash_bank_context,
hal_flash_bank_impl,
&hal_flash_bank_context);
// setup Link Key DB using TLV
const btstack_link_key_db_t * btstack_link_key_db = btstack_link_key_db_tlv_get_instance(btstack_tlv_impl, &btstack_tlv_flash_sector_context);
const btstack_link_key_db_t * btstack_link_key_db = btstack_link_key_db_tlv_get_instance(btstack_tlv_impl, &btstack_tlv_flash_bank_context);
hci_set_link_key_db(btstack_link_key_db);
// setup LE Device DB using TLV
le_device_db_tlv_configure(btstack_tlv_impl, &btstack_tlv_flash_sector_context);
le_device_db_tlv_configure(btstack_tlv_impl, &btstack_tlv_flash_bank_context);
// go
btstack_main(0, (void *)NULL);

View File

@ -30,7 +30,7 @@
*/
/*
* hal_flash_sector_mxc.c
* hal_flash_bank_mxc.c
*
* HAL abstraction for Flash memory that can be written anywhere
* after being erased implemented with memory
@ -39,22 +39,22 @@
#include <stdint.h>
#include <string.h> // memcpy
#include "hal_flash_sector_mxc.h"
#include "hal_flash_bank_mxc.h"
#include "flc.h" // Maxim Flash Controller
static uint32_t hal_flash_sector_mxc_get_size(void * context){
hal_flash_sector_mxc_t * self = (hal_flash_sector_mxc_t *) context;
static uint32_t hal_flash_bank_mxc_get_size(void * context){
hal_flash_bank_mxc_t * self = (hal_flash_bank_mxc_t *) context;
return self->sector_size;
}
static uint32_t hal_flash_sector_mxc_get_alignment(void * context){
static uint32_t hal_flash_bank_mxc_get_alignment(void * context){
(void)(context);
return 4;
}
static void hal_flash_sector_mxc_erase(void * context, int bank){
hal_flash_sector_mxc_t * self = (hal_flash_sector_mxc_t *) context;
static void hal_flash_bank_mxc_erase(void * context, int bank){
hal_flash_bank_mxc_t * self = (hal_flash_bank_mxc_t *) context;
if (bank > 1) return;
@ -62,8 +62,8 @@ static void hal_flash_sector_mxc_erase(void * context, int bank){
FLC_PageErase(self->banks[bank], MXC_V_FLC_ERASE_CODE_PAGE_ERASE, MXC_V_FLC_FLSH_UNLOCK_KEY);
}
static void hal_flash_sector_mxc_read(void * context, int bank, uint32_t offset, uint8_t * buffer, uint32_t size){
hal_flash_sector_mxc_t * self = (hal_flash_sector_mxc_t *) context;
static void hal_flash_bank_mxc_read(void * context, int bank, uint32_t offset, uint8_t * buffer, uint32_t size){
hal_flash_bank_mxc_t * self = (hal_flash_bank_mxc_t *) context;
if (bank > 1) return;
if (offset > self->sector_size) return;
@ -72,8 +72,8 @@ static void hal_flash_sector_mxc_read(void * context, int bank, uint32_t offset,
memcpy(buffer, ((uint8_t *) self->banks[bank]) + offset, size);
}
static void hal_flash_sector_mxc_write(void * context, int bank, uint32_t offset, const uint8_t * data, uint32_t size){
hal_flash_sector_mxc_t * self = (hal_flash_sector_mxc_t *) context;
static void hal_flash_bank_mxc_write(void * context, int bank, uint32_t offset, const uint8_t * data, uint32_t size){
hal_flash_bank_mxc_t * self = (hal_flash_bank_mxc_t *) context;
if (bank > 1) return;
if (offset > self->sector_size) return;
@ -82,15 +82,15 @@ static void hal_flash_sector_mxc_write(void * context, int bank, uint32_t offset
FLC_Write(self->banks[bank] + offset, data, size, MXC_V_FLC_FLSH_UNLOCK_KEY);
}
static const hal_flash_sector_t hal_flash_sector_mxc_impl = {
/* uint32_t (*get_size)() */ &hal_flash_sector_mxc_get_size,
/* uint32_t (*get_alignment)(..); */ &hal_flash_sector_mxc_get_alignment,
/* void (*erase)(..); */ &hal_flash_sector_mxc_erase,
/* void (*read)(..); */ &hal_flash_sector_mxc_read,
/* void (*write)(..); */ &hal_flash_sector_mxc_write,
static const hal_flash_bank_t hal_flash_bank_mxc_impl = {
/* uint32_t (*get_size)() */ &hal_flash_bank_mxc_get_size,
/* uint32_t (*get_alignment)(..); */ &hal_flash_bank_mxc_get_alignment,
/* void (*erase)(..); */ &hal_flash_bank_mxc_erase,
/* void (*read)(..); */ &hal_flash_bank_mxc_read,
/* void (*write)(..); */ &hal_flash_bank_mxc_write,
};
const hal_flash_sector_t * hal_flash_sector_mxc_init_instance(hal_flash_sector_mxc_t * context, uint32_t sector_size,
const hal_flash_bank_t * hal_flash_bank_mxc_init_instance(hal_flash_bank_mxc_t * context, uint32_t sector_size,
uintptr_t bank_0_addr, uintptr_t bank_1_addr){
context->sector_size = sector_size;
context->banks[0] = bank_0_addr;
@ -99,6 +99,6 @@ const hal_flash_sector_t * hal_flash_sector_mxc_init_instance(hal_flash_sector_m
// prepare FLC
FLC_Init();
return &hal_flash_sector_mxc_impl;
return &hal_flash_bank_mxc_impl;
}

View File

@ -36,11 +36,11 @@
* after being erased implemented with memory
*/
#ifndef __HAL_FLASH_SECTOR_MAXIM_H
#define __HAL_FLASH_SECTOR_MAXIM_H
#ifndef __HAL_FLASH_BANK_MAXIM_H
#define __HAL_FLASH_BANK_MAXIM_H
#include <stdint.h>
#include "hal_flash_sector.h"
#include "hal_flash_bank.h"
#if defined __cplusplus
extern "C" {
@ -50,18 +50,18 @@ typedef struct {
uint32_t sector_size;
uintptr_t banks[2];
} hal_flash_sector_mxc_t;
} hal_flash_bank_mxc_t;
/**
* Configure MXC HAL Flash Implementation
*
* @param context of hal_flash_sector_mxc_t
* @param context of hal_flash_bank_mxc_t
* @param sector_size
* @param bank_0_addr
* @param bank_1_addr
* @return
*/
const hal_flash_sector_t * hal_flash_sector_mxc_init_instance(hal_flash_sector_mxc_t * context, uint32_t sector_size, uintptr_t bank_0_addr, uintptr_t bank_1_addr);
const hal_flash_bank_t * hal_flash_bank_mxc_init_instance(hal_flash_bank_mxc_t * context, uint32_t sector_size, uintptr_t bank_0_addr, uintptr_t bank_1_addr);
#if defined __cplusplus
}

View File

@ -73,6 +73,7 @@
<listOptionValue builtIn="false" value="../btstack/chipset/cc256x"/>
<listOptionValue builtIn="false" value="../btstack/platform/embedded"/>
<listOptionValue builtIn="false" value="../btstack/port/stm32-f4discovery-cc256x/src"/>
<listOptionValue builtIn="false" value="../btstack/port/stm32-f4discovery-cc256x/src/bsp"/>
<listOptionValue builtIn="false" value="../btstack/3rd-party/bluedroid/encoder/include"/>
<listOptionValue builtIn="false" value="../btstack/3rd-party/bluedroid/decoder/include"/>
<listOptionValue builtIn="false" value="../btstack/3rd-party/hxcmod-player/mods"/>

View File

@ -5,7 +5,7 @@
<provider copy-of="extension" id="org.eclipse.cdt.ui.UserLanguageSettingsProvider"/>
<provider-reference id="org.eclipse.cdt.core.ReferencedProjectsLanguageSettingsProvider" ref="shared-provider"/>
<provider-reference id="org.eclipse.cdt.managedbuilder.core.MBSLanguageSettingsProvider" ref="shared-provider"/>
<provider class="org.eclipse.cdt.managedbuilder.language.settings.providers.GCCBuiltinSpecsDetector" console="false" env-hash="-1707676697706075522" id="ilg.gnuarmeclipse.managedbuild.cross.GCCBuiltinSpecsDetector" keep-relative-paths="false" name="CDT GCC Built-in Compiler Settings Cross ARM" parameter="${COMMAND} ${FLAGS} ${cross_toolchain_flags} -E -P -v -dD &quot;${INPUTS}&quot;" prefer-non-shared="true">
<provider class="org.eclipse.cdt.managedbuilder.language.settings.providers.GCCBuiltinSpecsDetector" console="false" env-hash="-1695327955656721019" id="ilg.gnuarmeclipse.managedbuild.cross.GCCBuiltinSpecsDetector" keep-relative-paths="false" name="CDT GCC Built-in Compiler Settings Cross ARM" parameter="${COMMAND} ${FLAGS} ${cross_toolchain_flags} -E -P -v -dD &quot;${INPUTS}&quot;" prefer-non-shared="true">
<language-scope id="org.eclipse.cdt.core.gcc"/>
<language-scope id="org.eclipse.cdt.core.g++"/>
</provider>

View File

@ -30,7 +30,7 @@
*/
/*
* hal_flash_sector_stm32.c
* hal_flash_bank_stm32.c
*
* HAL abstraction for Flash memory that can be written anywhere
* after being erased
@ -39,21 +39,21 @@
#include <stdint.h>
#include <string.h> // memcpy
#include "hal_flash_sector_stm32.h"
#include "hal_flash_bank_stm32.h"
#include "stm32f4xx_hal.h"
static uint32_t hal_flash_sector_stm32_get_size(void * context){
hal_flash_sector_stm32_t * self = (hal_flash_sector_stm32_t *) context;
static uint32_t hal_flash_bank_stm32_get_size(void * context){
hal_flash_bank_stm32_t * self = (hal_flash_bank_stm32_t *) context;
return self->sector_size;
}
static uint32_t hal_flash_sector_memory_get_alignment(void * context){
static uint32_t hal_flash_bank_memory_get_alignment(void * context){
UNUSED(context);
return 1;
}
static void hal_flash_sector_stm32_erase(void * context, int bank){
hal_flash_sector_stm32_t * self = (hal_flash_sector_stm32_t *) context;
static void hal_flash_bank_stm32_erase(void * context, int bank){
hal_flash_bank_stm32_t * self = (hal_flash_bank_stm32_t *) context;
if (bank > 1) return;
FLASH_EraseInitTypeDef eraseInit;
eraseInit.TypeErase = FLASH_TYPEERASE_SECTORS;
@ -66,8 +66,8 @@ static void hal_flash_sector_stm32_erase(void * context, int bank){
HAL_FLASH_Lock();
}
static void hal_flash_sector_stm32_read(void * context, int bank, uint32_t offset, uint8_t * buffer, uint32_t size){
hal_flash_sector_stm32_t * self = (hal_flash_sector_stm32_t *) context;
static void hal_flash_bank_stm32_read(void * context, int bank, uint32_t offset, uint8_t * buffer, uint32_t size){
hal_flash_bank_stm32_t * self = (hal_flash_bank_stm32_t *) context;
if (bank > 1) return;
if (offset > self->sector_size) return;
@ -76,8 +76,8 @@ static void hal_flash_sector_stm32_read(void * context, int bank, uint32_t offse
memcpy(buffer, ((uint8_t *) self->banks[bank]) + offset, size);
}
static void hal_flash_sector_stm32_write(void * context, int bank, uint32_t offset, const uint8_t * data, uint32_t size){
hal_flash_sector_stm32_t * self = (hal_flash_sector_stm32_t *) context;
static void hal_flash_bank_stm32_write(void * context, int bank, uint32_t offset, const uint8_t * data, uint32_t size){
hal_flash_bank_stm32_t * self = (hal_flash_bank_stm32_t *) context;
if (bank > 1) return;
if (offset > self->sector_size) return;
@ -91,20 +91,20 @@ static void hal_flash_sector_stm32_write(void * context, int bank, uint32_t offs
HAL_FLASH_Lock();
}
static const hal_flash_sector_t hal_flash_sector_stm32_impl = {
/* uint32_t (*get_size)() */ &hal_flash_sector_stm32_get_size,
/* uint32_t (*get_alignment)(..); */ &hal_flash_sector_memory_get_alignment,
/* void (*erase)(..); */ &hal_flash_sector_stm32_erase,
/* void (*read)(..); */ &hal_flash_sector_stm32_read,
/* void (*write)(..); */ &hal_flash_sector_stm32_write,
static const hal_flash_bank_t hal_flash_bank_stm32_impl = {
/* uint32_t (*get_size)() */ &hal_flash_bank_stm32_get_size,
/* uint32_t (*get_alignment)(..); */ &hal_flash_bank_memory_get_alignment,
/* void (*erase)(..); */ &hal_flash_bank_stm32_erase,
/* void (*read)(..); */ &hal_flash_bank_stm32_read,
/* void (*write)(..); */ &hal_flash_bank_stm32_write,
};
const hal_flash_sector_t * hal_flash_sector_stm32_init_instance(hal_flash_sector_stm32_t * context, uint32_t sector_size,
const hal_flash_bank_t * hal_flash_bank_stm32_init_instance(hal_flash_bank_stm32_t * context, uint32_t sector_size,
uint32_t bank_0_sector, uint32_t bank_1_sector, uintptr_t bank_0_addr, uintptr_t bank_1_addr){
context->sector_size = sector_size;
context->sectors[0] = bank_0_sector;
context->sectors[1] = bank_1_sector;
context->banks[0] = bank_0_addr;
context->banks[1] = bank_1_addr;
return &hal_flash_sector_stm32_impl;
return &hal_flash_bank_stm32_impl;
}

View File

@ -30,17 +30,17 @@
*/
/*
* hal_flash_sector_stm32.h
* hal_flash_bank_stm32.h
*
* HAL abstraction for Flash memory that can be written anywhere
* after being erased
*/
#ifndef __HAL_FLASH_SECTOR_STM32_H
#define __HAL_FLASH_SECTOR_STM32_H
#ifndef __HAL_FLASH_BANK_STM32_H
#define __HAL_FLASH_BANK_STM32_H
#include <stdint.h>
#include "hal_flash_sector.h"
#include "hal_flash_bank.h"
#if defined __cplusplus
extern "C" {
@ -50,20 +50,20 @@ typedef struct {
uint32_t sector_size;
uint32_t sectors[2];
uintptr_t banks[2];
} hal_flash_sector_stm32_t;
} hal_flash_bank_stm32_t;
/**
* Configure STM32 HAL Flash Implementation
*
* @param context of hal_flash_sector_stm32_t
* @param sector_size
* @param context of hal_flash_bank_stm32_t
* @param bank_size
* @param bank_0_sector id
* @param bank_1_sector id
* @param bank_0_addr
* @param bank_1_addr
* @return
*/
const hal_flash_sector_t * hal_flash_sector_stm32_init_instance(hal_flash_sector_stm32_t * context, uint32_t sector_size,
const hal_flash_bank_t * hal_flash_bank_stm32_init_instance(hal_flash_bank_stm32_t * context, uint32_t bank_size,
uint32_t bank_0_sector, uint32_t bank_1_sector, uintptr_t bank_0_addr, uintptr_t bank_1_addr);
#if defined __cplusplus

View File

@ -6,11 +6,11 @@
#include "btstack_chipset_cc256x.h"
#include "btstack_run_loop_embedded.h"
#include "btstack_tlv.h"
#include "btstack_tlv_flash_sector.h"
#include "btstack_tlv_flash_bank.h"
#include "ble/le_device_db_tlv.h"
#include "classic/btstack_link_key_db_static.h"
#include "classic/btstack_link_key_db_tlv.h"
#include "hal_flash_sector_stm32.h"
#include "hal_flash_bank_stm32.h"
#include "stm32f4xx_hal.h"
//
@ -284,14 +284,14 @@ static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *pack
}
static btstack_tlv_flash_sector_t btstack_tlv_flash_sector_context;
static hal_flash_sector_stm32_t hal_flash_sector_context;
static btstack_tlv_flash_bank_t btstack_tlv_flash_bank_context;
static hal_flash_bank_stm32_t hal_flash_bank_context;
#define HAL_FLASH_SECTOR_SIZE (128 * 1024)
#define HAL_FLASH_SECTOR_BANK_0_ADDR 0x080C0000
#define HAL_FLASH_SECTOR_BANK_1_ADDR 0x080E0000
#define HAL_FLASH_SECTOR_BANK_0_SECTOR FLASH_SECTOR_10
#define HAL_FLASH_SECTOR_BANK_1_SECTOR FLASH_SECTOR_11
#define HAL_FLASH_BANK_SIZE (128 * 1024)
#define HAL_FLASH_BANK_0_ADDR 0x080C0000
#define HAL_FLASH_BANK_1_ADDR 0x080E0000
#define HAL_FLASH_BANK_0_SECTOR FLASH_SECTOR_10
#define HAL_FLASH_BANK_1_SECTOR FLASH_SECTOR_11
//
int btstack_main(int argc, char ** argv);
@ -308,24 +308,24 @@ void port_main(void){
hci_set_chipset(btstack_chipset_cc256x_instance());
// setup TLV Flash Sector implementation
const hal_flash_sector_t * hal_flash_sector_impl = hal_flash_sector_stm32_init_instance(
&hal_flash_sector_context,
HAL_FLASH_SECTOR_SIZE,
HAL_FLASH_SECTOR_BANK_0_SECTOR,
HAL_FLASH_SECTOR_BANK_1_SECTOR,
HAL_FLASH_SECTOR_BANK_0_ADDR,
HAL_FLASH_SECTOR_BANK_1_ADDR);
const btstack_tlv_t * btstack_tlv_impl = btstack_tlv_flash_sector_init_instance(
&btstack_tlv_flash_sector_context,
hal_flash_sector_impl,
&hal_flash_sector_context);
const hal_flash_bank_t * hal_flash_bank_impl = hal_flash_bank_stm32_init_instance(
&hal_flash_bank_context,
HAL_FLASH_BANK_SIZE,
HAL_FLASH_BANK_0_SECTOR,
HAL_FLASH_BANK_1_SECTOR,
HAL_FLASH_BANK_0_ADDR,
HAL_FLASH_BANK_1_ADDR);
const btstack_tlv_t * btstack_tlv_impl = btstack_tlv_flash_bank_init_instance(
&btstack_tlv_flash_bank_context,
hal_flash_bank_impl,
&hal_flash_bank_context);
// setup Link Key DB using TLV
const btstack_link_key_db_t * btstack_link_key_db = btstack_link_key_db_tlv_get_instance(btstack_tlv_impl, &btstack_tlv_flash_sector_context);
const btstack_link_key_db_t * btstack_link_key_db = btstack_link_key_db_tlv_get_instance(btstack_tlv_impl, &btstack_tlv_flash_bank_context);
hci_set_link_key_db(btstack_link_key_db);
// setup LE Device DB using TLV
le_device_db_tlv_configure(btstack_tlv_impl, &btstack_tlv_flash_sector_context);
le_device_db_tlv_configure(btstack_tlv_impl, &btstack_tlv_flash_bank_context);
// inform about BTstack state
hci_event_callback_registration.callback = &packet_handler;

View File

@ -4,9 +4,9 @@ BTSTACK_ROOT = ../..
CPPUTEST_HOME = ${BTSTACK_ROOT}/test/cpputest
COMMON_OBJ = \
btstack_tlv_flash_sector.o \
btstack_tlv_flash_bank.o \
btstack_util.o \
hal_flash_sector_memory.o \
hal_flash_bank_memory.o \
hci_dump.o \
VPATH = \

View File

@ -2,10 +2,10 @@
#include "CppUTest/TestHarness.h"
#include "CppUTest/CommandLineTestRunner.h"
#include "hal_flash_sector.h"
#include "hal_flash_sector_memory.h"
#include "hal_flash_bank.h"
#include "hal_flash_bank_memory.h"
#include "btstack_tlv.h"
#include "btstack_tlv_flash_sector.h"
#include "btstack_tlv_flash_bank.h"
#include "hci_dump.h"
#include "ble/le_device_db.h"
#include "ble/le_device_db_tlv.h"
@ -13,8 +13,8 @@
#include "btstack_config.h"
#include "btstack_debug.h"
#define HAL_FLASH_SECTOR_MEMORY_STORAGE_SIZE 512
static uint8_t hal_flash_sector_memory_storage[HAL_FLASH_SECTOR_MEMORY_STORAGE_SIZE];
#define HAL_FLASH_BANK_MEMORY_STORAGE_SIZE 512
static uint8_t hal_flash_bank_memory_storage[HAL_FLASH_BANK_MEMORY_STORAGE_SIZE];
static void CHECK_EQUAL_ARRAY(uint8_t * expected, uint8_t * actual, int size){
int i;
@ -31,22 +31,22 @@ static void CHECK_EQUAL_ARRAY(uint8_t * expected, uint8_t * actual, int size){
//
TEST_GROUP(LE_DEVICE_DB){
const hal_flash_sector_t * hal_flash_sector_impl;
hal_flash_sector_memory_t hal_flash_sector_context;
const hal_flash_bank_t * hal_flash_bank_impl;
hal_flash_bank_memory_t hal_flash_bank_context;
const btstack_tlv_t * btstack_tlv_impl;
btstack_tlv_flash_sector_t btstack_tlv_context;
btstack_tlv_flash_bank_t btstack_tlv_context;
bd_addr_t addr_aa, addr_bb, addr_cc;
sm_key_t sm_key_aa, sm_key_bb, sm_key_cc;
void setup(void){
// hal_flash_sector
hal_flash_sector_impl = hal_flash_sector_memory_init_instance(&hal_flash_sector_context, hal_flash_sector_memory_storage, HAL_FLASH_SECTOR_MEMORY_STORAGE_SIZE);
hal_flash_sector_impl->erase(&hal_flash_sector_context, 0);
hal_flash_sector_impl->erase(&hal_flash_sector_context, 1);
// hal_flash_bank
hal_flash_bank_impl = hal_flash_bank_memory_init_instance(&hal_flash_bank_context, hal_flash_bank_memory_storage, HAL_FLASH_BANK_MEMORY_STORAGE_SIZE);
hal_flash_bank_impl->erase(&hal_flash_bank_context, 0);
hal_flash_bank_impl->erase(&hal_flash_bank_context, 1);
// btstack_tlv
btstack_tlv_impl = btstack_tlv_flash_sector_init_instance(&btstack_tlv_context, hal_flash_sector_impl, &hal_flash_sector_context);
btstack_tlv_impl = btstack_tlv_flash_bank_init_instance(&btstack_tlv_context, hal_flash_bank_impl, &hal_flash_bank_context);
// le_device_db_tlv
le_device_db_tlv_configure(btstack_tlv_impl, &btstack_tlv_context);
le_device_db_init();

View File

@ -2,10 +2,10 @@
#include "CppUTest/TestHarness.h"
#include "CppUTest/CommandLineTestRunner.h"
#include "hal_flash_sector.h"
#include "hal_flash_sector_memory.h"
#include "hal_flash_bank.h"
#include "hal_flash_bank_memory.h"
#include "btstack_tlv.h"
#include "btstack_tlv_flash_sector.h"
#include "btstack_tlv_flash_bank.h"
#include "hci_dump.h"
#include "classic/btstack_link_key_db.h"
#include "classic/btstack_link_key_db_tlv.h"
@ -13,8 +13,8 @@
#include "btstack_config.h"
#include "btstack_debug.h"
#define HAL_FLASH_SECTOR_MEMORY_STORAGE_SIZE 256
static uint8_t hal_flash_sector_memory_storage[HAL_FLASH_SECTOR_MEMORY_STORAGE_SIZE];
#define HAL_FLASH_BANK_MEMORY_STORAGE_SIZE 256
static uint8_t hal_flash_bank_memory_storage[HAL_FLASH_BANK_MEMORY_STORAGE_SIZE];
static void CHECK_EQUAL_ARRAY(uint8_t * expected, uint8_t * actual, int size){
int i;
@ -28,30 +28,30 @@ static void CHECK_EQUAL_ARRAY(uint8_t * expected, uint8_t * actual, int size){
}
}
TEST_GROUP(HAL_FLASH_SECTOR){
const hal_flash_sector_t * hal_flash_sector_impl;
hal_flash_sector_memory_t hal_flash_sector_context;
TEST_GROUP(HAL_FLASH_bank){
const hal_flash_bank_t * hal_flash_bank_impl;
hal_flash_bank_memory_t hal_flash_bank_context;
void setup(void){
hal_flash_sector_impl = hal_flash_sector_memory_init_instance(&hal_flash_sector_context, hal_flash_sector_memory_storage, HAL_FLASH_SECTOR_MEMORY_STORAGE_SIZE);
hal_flash_sector_impl->erase(&hal_flash_sector_context, 0);
hal_flash_sector_impl->erase(&hal_flash_sector_context, 1);
hal_flash_bank_impl = hal_flash_bank_memory_init_instance(&hal_flash_bank_context, hal_flash_bank_memory_storage, HAL_FLASH_BANK_MEMORY_STORAGE_SIZE);
hal_flash_bank_impl->erase(&hal_flash_bank_context, 0);
hal_flash_bank_impl->erase(&hal_flash_bank_context, 1);
}
};
TEST(HAL_FLASH_SECTOR, TestErased){
TEST(HAL_FLASH_bank, TestErased){
uint8_t buffer;
int offsets[] = { 0, 10, 100};
int i;
for (i=0;i<sizeof(offsets)/sizeof(int);i++){
int bank;
for (bank=0;bank<2;bank++){
hal_flash_sector_impl->read(&hal_flash_sector_context, bank, offsets[i], &buffer, 1);
hal_flash_bank_impl->read(&hal_flash_bank_context, bank, offsets[i], &buffer, 1);
CHECK_EQUAL(buffer, 0xff);
}
}
}
TEST(HAL_FLASH_SECTOR, TestWrite){
TEST(HAL_FLASH_bank, TestWrite){
uint8_t buffer;
int offsets[] = { 0, 10, 100};
int i;
@ -59,13 +59,13 @@ TEST(HAL_FLASH_SECTOR, TestWrite){
int bank;
for (bank=0;bank<2;bank++){
buffer = i;
hal_flash_sector_impl->write(&hal_flash_sector_context, bank, offsets[i], &buffer, 1);
hal_flash_bank_impl->write(&hal_flash_bank_context, bank, offsets[i], &buffer, 1);
}
}
for (i=0;i<sizeof(offsets)/sizeof(int);i++){
int bank;
for (bank=0;bank<2;bank++){
hal_flash_sector_impl->read(&hal_flash_sector_context, bank, offsets[i], &buffer, 1);
hal_flash_bank_impl->read(&hal_flash_bank_context, bank, offsets[i], &buffer, 1);
CHECK_EQUAL(buffer, i);
}
}
@ -73,50 +73,50 @@ TEST(HAL_FLASH_SECTOR, TestWrite){
#if 0
// prints error and exits tests. maybe all functions need to return ok
TEST(HAL_FLASH_SECTOR, TestWriteTwice){
TEST(HAL_FLASH_bank, TestWriteTwice){
uint8_t buffer = 5;
hal_flash_sector_impl->write(&hal_flash_sector_context, 0, 5, &buffer, 1);
hal_flash_sector_impl->write(&hal_flash_sector_context, 0, 5, &buffer, 1);
hal_flash_bank_impl->write(&hal_flash_bank_context, 0, 5, &buffer, 1);
hal_flash_bank_impl->write(&hal_flash_bank_context, 0, 5, &buffer, 1);
}
#endif
TEST(HAL_FLASH_SECTOR, TestWriteErase){
TEST(HAL_FLASH_bank, TestWriteErase){
uint32_t offset = 7;
uint8_t value = 9;
uint8_t buffer = value;
hal_flash_sector_impl->write(&hal_flash_sector_context, 0, offset, &buffer, 1);
hal_flash_sector_impl->read(&hal_flash_sector_context, 0, offset, &buffer, 1);
hal_flash_bank_impl->write(&hal_flash_bank_context, 0, offset, &buffer, 1);
hal_flash_bank_impl->read(&hal_flash_bank_context, 0, offset, &buffer, 1);
CHECK_EQUAL(buffer, value);
hal_flash_sector_impl->erase(&hal_flash_sector_context, 0);
hal_flash_sector_impl->read(&hal_flash_sector_context, 0, offset, &buffer, 1);
hal_flash_bank_impl->erase(&hal_flash_bank_context, 0);
hal_flash_bank_impl->read(&hal_flash_bank_context, 0, offset, &buffer, 1);
CHECK_EQUAL(buffer, 0xff);
}
/// TLV
TEST_GROUP(BSTACK_TLV){
const hal_flash_sector_t * hal_flash_sector_impl;
hal_flash_sector_memory_t hal_flash_sector_context;
const hal_flash_bank_t * hal_flash_bank_impl;
hal_flash_bank_memory_t hal_flash_bank_context;
const btstack_tlv_t * btstack_tlv_impl;
btstack_tlv_flash_sector_t btstack_tlv_context;
const btstack_tlv_t * btstack_tlv_impl;
btstack_tlv_flash_bank_t btstack_tlv_context;
void setup(void){
hal_flash_sector_impl = hal_flash_sector_memory_init_instance(&hal_flash_sector_context, hal_flash_sector_memory_storage, HAL_FLASH_SECTOR_MEMORY_STORAGE_SIZE);
hal_flash_sector_impl->erase(&hal_flash_sector_context, 0);
hal_flash_sector_impl->erase(&hal_flash_sector_context, 1);
hal_flash_bank_impl = hal_flash_bank_memory_init_instance(&hal_flash_bank_context, hal_flash_bank_memory_storage, HAL_FLASH_BANK_MEMORY_STORAGE_SIZE);
hal_flash_bank_impl->erase(&hal_flash_bank_context, 0);
hal_flash_bank_impl->erase(&hal_flash_bank_context, 1);
}
};
TEST(BSTACK_TLV, TestMissingTag){
btstack_tlv_impl = btstack_tlv_flash_sector_init_instance(&btstack_tlv_context, hal_flash_sector_impl, &hal_flash_sector_context);
btstack_tlv_impl = btstack_tlv_flash_bank_init_instance(&btstack_tlv_context, hal_flash_bank_impl, &hal_flash_bank_context);
uint32_t tag = 'abcd';
int size = btstack_tlv_impl->get_tag(&btstack_tlv_context, tag, NULL, 0);
CHECK_EQUAL(size, 0);
}
TEST(BSTACK_TLV, TestWriteRead){
btstack_tlv_impl = btstack_tlv_flash_sector_init_instance(&btstack_tlv_context, hal_flash_sector_impl, &hal_flash_sector_context);
btstack_tlv_impl = btstack_tlv_flash_bank_init_instance(&btstack_tlv_context, hal_flash_bank_impl, &hal_flash_bank_context);
uint32_t tag = 'abcd';
uint8_t data = 7;
uint8_t buffer = data;
@ -128,7 +128,7 @@ TEST(BSTACK_TLV, TestWriteRead){
}
TEST(BSTACK_TLV, TestWriteWriteRead){
btstack_tlv_impl = btstack_tlv_flash_sector_init_instance(&btstack_tlv_context, hal_flash_sector_impl, &hal_flash_sector_context);
btstack_tlv_impl = btstack_tlv_flash_bank_init_instance(&btstack_tlv_context, hal_flash_bank_impl, &hal_flash_bank_context);
uint32_t tag = 'abcd';
uint8_t data = 7;
uint8_t buffer = data;
@ -143,7 +143,7 @@ TEST(BSTACK_TLV, TestWriteWriteRead){
}
TEST(BSTACK_TLV, TestWriteABARead){
btstack_tlv_impl = btstack_tlv_flash_sector_init_instance(&btstack_tlv_context, hal_flash_sector_impl, &hal_flash_sector_context);
btstack_tlv_impl = btstack_tlv_flash_bank_init_instance(&btstack_tlv_context, hal_flash_bank_impl, &hal_flash_bank_context);
uint32_t tag_a = 'aaaa';
uint32_t tag_b = 'bbbb';
uint8_t data = 7;
@ -162,7 +162,7 @@ TEST(BSTACK_TLV, TestWriteABARead){
}
TEST(BSTACK_TLV, TestWriteDeleteRead){
btstack_tlv_impl = btstack_tlv_flash_sector_init_instance(&btstack_tlv_context, hal_flash_sector_impl, &hal_flash_sector_context);
btstack_tlv_impl = btstack_tlv_flash_bank_init_instance(&btstack_tlv_context, hal_flash_bank_impl, &hal_flash_bank_context);
uint32_t tag = 'abcd';
uint8_t data = 7;
uint8_t buffer = data;
@ -177,7 +177,7 @@ TEST(BSTACK_TLV, TestWriteDeleteRead){
TEST(BSTACK_TLV, TestMigrate){
btstack_tlv_impl = btstack_tlv_flash_sector_init_instance(&btstack_tlv_context, hal_flash_sector_impl, &hal_flash_sector_context);
btstack_tlv_impl = btstack_tlv_flash_bank_init_instance(&btstack_tlv_context, hal_flash_bank_impl, &hal_flash_bank_context);
uint32_t tag = 'abcd';
uint8_t data[8];
@ -190,7 +190,7 @@ TEST(BSTACK_TLV, TestMigrate){
btstack_tlv_impl->store_tag(&btstack_tlv_context, tag, &data[0], 8);
}
btstack_tlv_impl = btstack_tlv_flash_sector_init_instance(&btstack_tlv_context, hal_flash_sector_impl, &hal_flash_sector_context);
btstack_tlv_impl = btstack_tlv_flash_bank_init_instance(&btstack_tlv_context, hal_flash_bank_impl, &hal_flash_bank_context);
uint8_t buffer[8];
btstack_tlv_impl->get_tag(&btstack_tlv_context, tag, &buffer[0], 1);
@ -199,7 +199,7 @@ TEST(BSTACK_TLV, TestMigrate){
TEST(BSTACK_TLV, TestMigrate2){
btstack_tlv_impl = btstack_tlv_flash_sector_init_instance(&btstack_tlv_context, hal_flash_sector_impl, &hal_flash_sector_context);
btstack_tlv_impl = btstack_tlv_flash_bank_init_instance(&btstack_tlv_context, hal_flash_bank_impl, &hal_flash_bank_context);
uint32_t tag1 = 0x11223344;
uint32_t tag2 = 0x44556677;
@ -217,7 +217,7 @@ TEST(BSTACK_TLV, TestMigrate2){
btstack_tlv_impl->store_tag(&btstack_tlv_context, tag2, data2, 8);
}
btstack_tlv_impl = btstack_tlv_flash_sector_init_instance(&btstack_tlv_context, hal_flash_sector_impl, &hal_flash_sector_context);
btstack_tlv_impl = btstack_tlv_flash_bank_init_instance(&btstack_tlv_context, hal_flash_bank_impl, &hal_flash_bank_context);
uint8_t buffer[8];
btstack_tlv_impl->get_tag(&btstack_tlv_context, tag1, &buffer[0], 1);
@ -227,12 +227,12 @@ TEST(BSTACK_TLV, TestMigrate2){
}
TEST(BSTACK_TLV, TestWriteResetRead){
btstack_tlv_impl = btstack_tlv_flash_sector_init_instance(&btstack_tlv_context, hal_flash_sector_impl, &hal_flash_sector_context);
btstack_tlv_impl = btstack_tlv_flash_bank_init_instance(&btstack_tlv_context, hal_flash_bank_impl, &hal_flash_bank_context);
uint32_t tag = 'abcd';
uint8_t data = 7;
uint8_t buffer = data;
btstack_tlv_impl->store_tag(&btstack_tlv_context, tag, &buffer, 1);
btstack_tlv_impl = btstack_tlv_flash_sector_init_instance(&btstack_tlv_context, hal_flash_sector_impl, &hal_flash_sector_context);
btstack_tlv_impl = btstack_tlv_flash_bank_init_instance(&btstack_tlv_context, hal_flash_bank_impl, &hal_flash_bank_context);
int size = btstack_tlv_impl->get_tag(&btstack_tlv_context, tag, NULL, 0);
CHECK_EQUAL(size, 1);
btstack_tlv_impl->get_tag(&btstack_tlv_context, tag, &buffer, 1);
@ -241,11 +241,11 @@ TEST(BSTACK_TLV, TestWriteResetRead){
//
TEST_GROUP(LINK_KEY_DB){
const hal_flash_sector_t * hal_flash_sector_impl;
hal_flash_sector_memory_t hal_flash_sector_context;
const hal_flash_bank_t * hal_flash_bank_impl;
hal_flash_bank_memory_t hal_flash_bank_context;
const btstack_tlv_t * btstack_tlv_impl;
btstack_tlv_flash_sector_t btstack_tlv_context;
btstack_tlv_flash_bank_t btstack_tlv_context;
const btstack_link_key_db_t * btstack_link_key_db;
@ -254,12 +254,12 @@ TEST_GROUP(LINK_KEY_DB){
link_key_type_t link_key_type;
void setup(void){
// hal_flash_sector
hal_flash_sector_impl = hal_flash_sector_memory_init_instance(&hal_flash_sector_context, hal_flash_sector_memory_storage, HAL_FLASH_SECTOR_MEMORY_STORAGE_SIZE);
hal_flash_sector_impl->erase(&hal_flash_sector_context, 0);
hal_flash_sector_impl->erase(&hal_flash_sector_context, 1);
// hal_flash_bank
hal_flash_bank_impl = hal_flash_bank_memory_init_instance(&hal_flash_bank_context, hal_flash_bank_memory_storage, HAL_FLASH_BANK_MEMORY_STORAGE_SIZE);
hal_flash_bank_impl->erase(&hal_flash_bank_context, 0);
hal_flash_bank_impl->erase(&hal_flash_bank_context, 1);
// btstack_tlv
btstack_tlv_impl = btstack_tlv_flash_sector_init_instance(&btstack_tlv_context, hal_flash_sector_impl, &hal_flash_sector_context);
btstack_tlv_impl = btstack_tlv_flash_bank_init_instance(&btstack_tlv_context, hal_flash_bank_impl, &hal_flash_bank_context);
// btstack_link_key_db
btstack_link_key_db = btstack_link_key_db_tlv_get_instance(btstack_tlv_impl, &btstack_tlv_context);