diff --git a/port/msp432p401lp-cc256x/Makefile b/port/msp432p401lp-cc256x/Makefile index 241311b1f..6be2213cc 100755 --- a/port/msp432p401lp-cc256x/Makefile +++ b/port/msp432p401lp-cc256x/Makefile @@ -61,6 +61,7 @@ IPATH += ${BTSTACK_ROOT}/chipset/cc256x IPATH += ${BTSTACK_ROOT}/3rd-party/bluedroid/decoder/include IPATH += ${BTSTACK_ROOT}/3rd-party/bluedroid/encoder/include IPATH += . +IPATH += ${COMPILER} BTSTACK_SOURCES = \ ad_parser.c \ @@ -135,12 +136,14 @@ pbap_client.c \ PORT_SOURCES = \ btstack_chipset_cc256x.o \ bluetooth_init_cc2564B_1.6_BT_Spec_4.1.o \ + hal_flash_bank_msp432.o \ main.o \ startup_msp432p401r_${COMPILER}.o \ system_msp432p401r.o \ SEGGER_RTT.o \ SEGGER_RTT_Syscalls_GCC.o \ SEGGER_RTT_printf.o \ + le_counter.o \ include ${BTSTACK_ROOT}/3rd-party/bluedroid/decoder/Makefile.inc include ${BTSTACK_ROOT}/3rd-party/bluedroid/encoder/Makefile.inc @@ -148,6 +151,10 @@ include ${BTSTACK_ROOT}/3rd-party/bluedroid/encoder/Makefile.inc BTSTACK_SOURCES += ${SBC_ENCODER} BTSTACK_SOURCES += ${SBC_DECODER} +# compile .gatt descriptions +${COMPILER}/%.h: %.gatt + python ${BTSTACK_ROOT}/tool/compile_gatt.py $< $@ + OBJECTS = $(BTSTACK_SOURCES:%.c=$(COMPILER)/%.o) $(PORT_SOURCES:%.o=$(COMPILER)/%.o) # ${COMPILER}/startup_msp432p401r_${COMPILER}.o \ @@ -174,6 +181,7 @@ OBJECTS = $(BTSTACK_SOURCES:%.c=$(COMPILER)/%.o) $(PORT_SOURCES:%.o=$(COMPILER)/ # # Rules for building the uart_loopback_24mhz_brclk example. # +${COMPILER}/uart_loopback_24mhz_brclk.axf: ${COMPILER}/le_counter.h ${COMPILER}/uart_loopback_24mhz_brclk.axf: ${OBJECTS} ${COMPILER}/uart_loopback_24mhz_brclk.axf: ${ROOT}/driverlib/MSP432P4xx/${COMPILER}/msp432p4xx_driverlib.a ${COMPILER}/uart_loopback_24mhz_brclk.axf: linker_script_gcc.ld diff --git a/port/msp432p401lp-cc256x/hal_flash_bank_msp432.c b/port/msp432p401lp-cc256x/hal_flash_bank_msp432.c new file mode 100644 index 000000000..27e436949 --- /dev/null +++ b/port/msp432p401lp-cc256x/hal_flash_bank_msp432.c @@ -0,0 +1,121 @@ +/* + * Copyright (C) 2017 BlueKitchen GmbH + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holders nor the names of + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS + * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF + * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + */ + +/* + * hal_flash_bank_stm32.c + * + * HAL abstraction for Flash memory that can be written anywhere + * after being erased + */ + +#include +#include // memcpy + +#include "hal_flash_bank_msp432.h" +#include "driverlib.h" +#include "btstack_defines.h" + +static uint32_t hal_flash_bank_msp432_get_size(void * context){ + hal_flash_bank_msp432_t * self = (hal_flash_bank_msp432_t *) context; + return self->sector_size; +} + +static uint32_t hal_flash_bank_memory_msp432_get_alignment(void * context){ + UNUSED(context); + return 4; +} + +static void hal_flash_bank_msp432_erase(void * context, int bank){ + + hal_flash_bank_msp432_t * self = (hal_flash_bank_msp432_t *) context; + if (bank > 1) return; + + /* Unprotecting sector */ + MAP_FlashCtl_unprotectSector(FLASH_MAIN_MEMORY_SPACE_BANK1,self->sectors[bank]); + + /* Trying to erase the sector. Within this function, the API will + automatically try to erase the maximum number of tries. If it fails, + trap in an infinite loop */ + if(!MAP_FlashCtl_eraseSector(self->banks[bank])) + while(1); + + /* Setting the sector back to protected */ + MAP_FlashCtl_protectSector(FLASH_MAIN_MEMORY_SPACE_BANK1,self->sectors[bank]); +} + +static void hal_flash_bank_msp432_read(void * context, int bank, uint32_t offset, uint8_t * buffer, uint32_t size){ + hal_flash_bank_msp432_t * self = (hal_flash_bank_msp432_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_bank_msp432_write(void * context, int bank, uint32_t offset, const uint8_t * data, uint32_t size){ + hal_flash_bank_msp432_t * self = (hal_flash_bank_msp432_t *) context; + + if (bank > 1) return; + if (offset > self->sector_size) return; + if ((offset + size) > self->sector_size) return; + + /* Unprotecting sector */ + MAP_FlashCtl_unprotectSector(FLASH_MAIN_MEMORY_SPACE_BANK1,self->sectors[bank]); + + /* Trying to program the memory. Within this function, the API will + automatically try to program the maximum number of tries. If it fails, + trap inside an infinite loop */ + if(!MAP_FlashCtl_programMemory( (void *) data, + (void*) ((uint8_t *) self->banks[bank]) + offset, size)) + while(1); + + /* Setting the sector back to protected */ + MAP_FlashCtl_protectSector(FLASH_MAIN_MEMORY_SPACE_BANK1,self->sectors[bank]); +} + +static const hal_flash_bank_t hal_flash_bank_msp432_impl = { + /* uint32_t (*get_size)() */ &hal_flash_bank_msp432_get_size, + /* uint32_t (*get_alignment)(..); */ &hal_flash_bank_memory_msp432_get_alignment, + /* void (*erase)(..); */ &hal_flash_bank_msp432_erase, + /* void (*read)(..); */ &hal_flash_bank_msp432_read, + /* void (*write)(..); */ &hal_flash_bank_msp432_write, +}; + +const hal_flash_bank_t * hal_flash_bank_msp432_init_instance(hal_flash_bank_msp432_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_bank_msp432_impl; +} diff --git a/port/msp432p401lp-cc256x/hal_flash_bank_msp432.h b/port/msp432p401lp-cc256x/hal_flash_bank_msp432.h new file mode 100644 index 000000000..7cf0e3043 --- /dev/null +++ b/port/msp432p401lp-cc256x/hal_flash_bank_msp432.h @@ -0,0 +1,72 @@ +/* + * Copyright (C) 2017 BlueKitchen GmbH + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holders nor the names of + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS + * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF + * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + */ + +/* + * hal_flash_bank_msp432.h + * + * HAL abstraction for Flash memory that can be written anywhere + * after being erased + */ + +#ifndef __HAL_FLASH_BANK_MSP432_H +#define __HAL_FLASH_BANK_MSP432_H + +#include +#include "hal_flash_bank.h" + +#if defined __cplusplus +extern "C" { +#endif + +typedef struct { + uint32_t sector_size; + uint32_t sectors[2]; + uintptr_t banks[2]; +} hal_flash_bank_msp432_t; + +/** + * Configure MSP432 HAL Flash Implementation + * + * @param context of hal_flash_bank_msp432_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_bank_t * hal_flash_bank_msp432_init_instance(hal_flash_bank_msp432_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 +} +#endif +#endif diff --git a/port/msp432p401lp-cc256x/main.c b/port/msp432p401lp-cc256x/main.c index 350198639..b2c50ec6c 100755 --- a/port/msp432p401lp-cc256x/main.c +++ b/port/msp432p401lp-cc256x/main.c @@ -15,9 +15,14 @@ #include "btstack_defines.h" #include "btstack_debug.h" #include "btstack_memory.h" +#include "btstack_tlv.h" #include "btstack_run_loop.h" #include "btstack_run_loop_embedded.h" #include "hci_dump.h" +#include "btstack_tlv_flash_bank.h" +#include "hal_flash_bank_msp432.h" +#include "classic/btstack_link_key_db_tlv.h" +#include "ble/le_device_db_tlv.h" static hci_transport_config_uart_t config = { HCI_TRANSPORT_CONFIG_UART, @@ -27,6 +32,9 @@ static hci_transport_config_uart_t config = { NULL, }; +static hal_flash_bank_msp432_t hal_flash_bank_context; +static btstack_tlv_flash_bank_t btstack_tlv_flash_bank_context; + #ifndef ENABLE_SEGGER_RTT /** @@ -378,6 +386,15 @@ static void rc_callback(void){ #include "SEGGER_RTT.h" +// HAL FLASH MSP432 Configuration - use two last 4kB sectors +#define HAL_FLASH_BANK_SIZE 4096 +#define HAL_FLASH_BANK_0_SECTOR FLASH_SECTOR30 +#define HAL_FLASH_BANK_1_SECTOR FLASH_SECTOR31 +#define HAL_FLASH_BANK_0_ADDR 0x3E000 +#define HAL_FLASH_BANK_1_ADDR 0x3F000 + +int btstack_main(const int argc, const char * argvp[]); + int main(void) { volatile uint32_t ii; @@ -397,15 +414,15 @@ int main(void) hci_init(hci_transport_h4_instance(btstack_uart_block_embedded_instance()), (void*) &config); hci_set_chipset(btstack_chipset_cc256x_instance()); -#if 0 // setup TLV Flash Sector implementation - const hal_flash_bank_t * hal_flash_bank_impl = hal_flash_bank_stm32_init_instance( + const hal_flash_bank_t * hal_flash_bank_impl = hal_flash_bank_msp432_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, @@ -421,13 +438,15 @@ int main(void) // setup LE Device DB using TLV le_device_db_tlv_configure(btstack_tlv_impl, &btstack_tlv_flash_bank_context); +#if 0 // inform about BTstack state hci_event_callback_registration.callback = &packet_handler; hci_add_event_handler(&hci_event_callback_registration); +#endif + // hand over to btstack embedded code btstack_main(0, NULL); -#endif hci_power_control(HCI_POWER_ON); @@ -451,19 +470,6 @@ int main(void) delay_ms(10); } #endif - - /* Configuring P1.0 as output */ - MAP_GPIO_setAsOutputPin(GPIO_PORT_P1, GPIO_PIN0); - - while (1) - { - /* Delay Loop */ - for(ii=0;ii<5000;ii++) - { - } - // SEGGER_RTT_printf(0, "Hi! tick %u\n", (int) systick / 1000); - MAP_GPIO_toggleOutputOnPin(GPIO_PORT_P1, GPIO_PIN0); - } }