mirror of
https://github.com/bluekitchen/btstack.git
synced 2025-02-24 12:40:47 +00:00
stm32-f4discovery-cc256x: implement hal_flash_sector_t interface for STM32 F4
This commit is contained in:
parent
adcdded97f
commit
b6c1f7f5cc
@ -3,7 +3,11 @@
|
|||||||
#include "btstack_debug.h"
|
#include "btstack_debug.h"
|
||||||
#include "btstack_chipset_cc256x.h"
|
#include "btstack_chipset_cc256x.h"
|
||||||
#include "btstack_run_loop_embedded.h"
|
#include "btstack_run_loop_embedded.h"
|
||||||
#include "btstack_link_key_db_fixed.h"
|
#include "classic/btstack_link_key_db_fixed.h"
|
||||||
|
#include "classic/btstack_link_key_db_tlv.h"
|
||||||
|
#include "hal_flash_sector.h"
|
||||||
|
#include "btstack_tlv.h"
|
||||||
|
#include "btstack_tlv_flash_sector.h"
|
||||||
#include "stm32f4xx_hal.h"
|
#include "stm32f4xx_hal.h"
|
||||||
|
|
||||||
#define __BTSTACK_FILE__ "port.c"
|
#define __BTSTACK_FILE__ "port.c"
|
||||||
@ -244,6 +248,7 @@ int _fstat(int file){
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// main.c
|
// main.c
|
||||||
static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
|
static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
|
||||||
UNUSED(size);
|
UNUSED(size);
|
||||||
@ -277,6 +282,85 @@ static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *pack
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// hal_flash_sector.h
|
||||||
|
typedef struct {
|
||||||
|
uint32_t sector_size;
|
||||||
|
uint32_t sectors[2];
|
||||||
|
uintptr_t banks[2];
|
||||||
|
|
||||||
|
} hal_flash_sector_stm32_t;
|
||||||
|
|
||||||
|
static uint32_t hal_flash_sector_stm32_get_size(void * context){
|
||||||
|
hal_flash_sector_stm32_t * self = (hal_flash_sector_stm32_t *) context;
|
||||||
|
return self->sector_size;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void hal_flash_sector_stm32_erase(void * context, int bank){
|
||||||
|
hal_flash_sector_stm32_t * self = (hal_flash_sector_stm32_t *) context;
|
||||||
|
if (bank > 1) return;
|
||||||
|
FLASH_EraseInitTypeDef eraseInit;
|
||||||
|
eraseInit.TypeErase = FLASH_TYPEERASE_SECTORS;
|
||||||
|
eraseInit.Sector = self->sectors[bank];
|
||||||
|
eraseInit.NbSectors = 1;
|
||||||
|
eraseInit.VoltageRange = FLASH_VOLTAGE_RANGE_1; // safe value
|
||||||
|
uint32_t sectorError;
|
||||||
|
HAL_FLASH_Unlock();
|
||||||
|
HAL_FLASHEx_Erase(&eraseInit, §orError);
|
||||||
|
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;
|
||||||
|
|
||||||
|
if (bank > 1) return;
|
||||||
|
if (offset > self->sector_size) return;
|
||||||
|
if ((offset + size) > self->sector_size) return;
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
if (bank > 1) return;
|
||||||
|
if (offset > self->sector_size) return;
|
||||||
|
if ((offset + size) > self->sector_size) return;
|
||||||
|
|
||||||
|
unsigned int i;
|
||||||
|
HAL_FLASH_Unlock();
|
||||||
|
for (i=0;i<size;i++){
|
||||||
|
HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, self->banks[bank] + offset +i, data[i]);
|
||||||
|
}
|
||||||
|
HAL_FLASH_Lock();
|
||||||
|
}
|
||||||
|
|
||||||
|
static const hal_flash_sector_t hal_flash_sector_stm32_impl = {
|
||||||
|
/* uint32_t (*get_size)() */ &hal_flash_sector_stm32_get_size,
|
||||||
|
/* void (*erase)(int); */ &hal_flash_sector_stm32_erase,
|
||||||
|
/* void (*read)(..); */ &hal_flash_sector_stm32_read,
|
||||||
|
/* void (*write)(..); */ &hal_flash_sector_stm32_write,
|
||||||
|
};
|
||||||
|
|
||||||
|
static const hal_flash_sector_t * hal_flash_sector_stm32_init_instance(hal_flash_sector_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;
|
||||||
|
}
|
||||||
|
|
||||||
|
static btstack_tlv_flash_sector_t btstack_tlv_flash_sector_context;
|
||||||
|
static hal_flash_sector_stm32_t hal_flash_sector_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
|
||||||
|
|
||||||
|
//
|
||||||
int btstack_main(int argc, char ** argv);
|
int btstack_main(int argc, char ** argv);
|
||||||
void port_main(void){
|
void port_main(void){
|
||||||
|
|
||||||
@ -284,13 +368,27 @@ void port_main(void){
|
|||||||
btstack_memory_init();
|
btstack_memory_init();
|
||||||
btstack_run_loop_init(btstack_run_loop_embedded_get_instance());
|
btstack_run_loop_init(btstack_run_loop_embedded_get_instance());
|
||||||
|
|
||||||
// hci_dump_open( NULL, HCI_DUMP_STDOUT );
|
hci_dump_open( NULL, HCI_DUMP_STDOUT );
|
||||||
|
|
||||||
// init HCI
|
// init HCI
|
||||||
hci_init(hci_transport_h4_instance(btstack_uart_block_embedded_instance()), (void*) &config);
|
hci_init(hci_transport_h4_instance(btstack_uart_block_embedded_instance()), (void*) &config);
|
||||||
hci_set_link_key_db(btstack_link_key_db_fixed_instance());
|
|
||||||
hci_set_chipset(btstack_chipset_cc256x_instance());
|
hci_set_chipset(btstack_chipset_cc256x_instance());
|
||||||
|
|
||||||
|
// setup Link Key DB
|
||||||
|
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 btstack_link_key_db_t * btstack_link_key_db = btstack_link_key_db_tlv_get_instance(btstack_tlv_impl, &btstack_tlv_flash_sector_context);
|
||||||
|
hci_set_link_key_db(btstack_link_key_db);
|
||||||
|
|
||||||
// inform about BTstack state
|
// inform about BTstack state
|
||||||
hci_event_callback_registration.callback = &packet_handler;
|
hci_event_callback_registration.callback = &packet_handler;
|
||||||
hci_add_event_handler(&hci_event_callback_registration);
|
hci_add_event_handler(&hci_event_callback_registration);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user