14317f7c8SMatthias Ringwald /* 24317f7c8SMatthias Ringwald * Copyright (C) 2020 BlueKitchen GmbH 34317f7c8SMatthias Ringwald * 44317f7c8SMatthias Ringwald * Redistribution and use in source and binary forms, with or without 54317f7c8SMatthias Ringwald * modification, are permitted provided that the following conditions 64317f7c8SMatthias Ringwald * are met: 74317f7c8SMatthias Ringwald * 84317f7c8SMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 94317f7c8SMatthias Ringwald * notice, this list of conditions and the following disclaimer. 104317f7c8SMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 114317f7c8SMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 124317f7c8SMatthias Ringwald * documentation and/or other materials provided with the distribution. 134317f7c8SMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 144317f7c8SMatthias Ringwald * contributors may be used to endorse or promote products derived 154317f7c8SMatthias Ringwald * from this software without specific prior written permission. 164317f7c8SMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 174317f7c8SMatthias Ringwald * personal benefit and not for any commercial purpose or for 184317f7c8SMatthias Ringwald * monetary gain. 194317f7c8SMatthias Ringwald * 204317f7c8SMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 214317f7c8SMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 224317f7c8SMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 234317f7c8SMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 244317f7c8SMatthias Ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 254317f7c8SMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 264317f7c8SMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 274317f7c8SMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 284317f7c8SMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 294317f7c8SMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 304317f7c8SMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 314317f7c8SMatthias Ringwald * SUCH DAMAGE. 324317f7c8SMatthias Ringwald * 334317f7c8SMatthias Ringwald * Please inquire about commercial licensing options at 344317f7c8SMatthias Ringwald * [email protected] 354317f7c8SMatthias Ringwald * 364317f7c8SMatthias Ringwald */ 374317f7c8SMatthias Ringwald 384317f7c8SMatthias Ringwald #define __BTSTACK_FILE__ "port.c" 394317f7c8SMatthias Ringwald 404317f7c8SMatthias Ringwald // include STM32 first to avoid warning about redefinition of UNUSED 414317f7c8SMatthias Ringwald #include "stm32f4xx_hal.h" 424317f7c8SMatthias Ringwald #include "main.h" 434317f7c8SMatthias Ringwald 444317f7c8SMatthias Ringwald #include "port.h" 454317f7c8SMatthias Ringwald 464317f7c8SMatthias Ringwald #include <stdio.h> 474317f7c8SMatthias Ringwald #include <stddef.h> 484317f7c8SMatthias Ringwald 494317f7c8SMatthias Ringwald #include "port.h" 504317f7c8SMatthias Ringwald #include "btstack.h" 514317f7c8SMatthias Ringwald #include "btstack_debug.h" 524317f7c8SMatthias Ringwald #include "btstack_audio.h" 534317f7c8SMatthias Ringwald #include "btstack_run_loop_embedded.h" 544317f7c8SMatthias Ringwald #include "btstack_tlv.h" 554317f7c8SMatthias Ringwald #include "btstack_tlv_flash_bank.h" 564317f7c8SMatthias Ringwald #include "ble/le_device_db_tlv.h" 574317f7c8SMatthias Ringwald #include "classic/btstack_link_key_db_tlv.h" 584317f7c8SMatthias Ringwald #include "hal_flash_bank_stm32.h" 594317f7c8SMatthias Ringwald #include "hci_transport_h2_stm32.h" 604317f7c8SMatthias Ringwald 614317f7c8SMatthias Ringwald #ifdef ENABLE_SEGGER_RTT 624317f7c8SMatthias Ringwald #include "SEGGER_RTT.h" 634317f7c8SMatthias Ringwald #endif 644317f7c8SMatthias Ringwald 654317f7c8SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 664317f7c8SMatthias Ringwald static btstack_tlv_flash_bank_t btstack_tlv_flash_bank_context; 674317f7c8SMatthias Ringwald static hal_flash_bank_stm32_t hal_flash_bank_context; 684317f7c8SMatthias Ringwald 694317f7c8SMatthias Ringwald // hal_time_ms.h 704317f7c8SMatthias Ringwald #include "hal_time_ms.h" 714317f7c8SMatthias Ringwald uint32_t hal_time_ms(void){ 724317f7c8SMatthias Ringwald return HAL_GetTick(); 734317f7c8SMatthias Ringwald } 744317f7c8SMatthias Ringwald 754317f7c8SMatthias Ringwald // hal_cpu.h implementation 764317f7c8SMatthias Ringwald #include "hal_cpu.h" 774317f7c8SMatthias Ringwald 784317f7c8SMatthias Ringwald void hal_cpu_disable_irqs(void){ 794317f7c8SMatthias Ringwald __disable_irq(); 804317f7c8SMatthias Ringwald } 814317f7c8SMatthias Ringwald 824317f7c8SMatthias Ringwald void hal_cpu_enable_irqs(void){ 834317f7c8SMatthias Ringwald __enable_irq(); 844317f7c8SMatthias Ringwald } 854317f7c8SMatthias Ringwald 864317f7c8SMatthias Ringwald void hal_cpu_enable_irqs_and_sleep(void){ 874317f7c8SMatthias Ringwald __enable_irq(); 88*8b703f3dSMatthias Ringwald #if 0 89*8b703f3dSMatthias Ringwald // temp disable until effect on RTT is clear 90*8b703f3dSMatthias Ringwald // go to sleep if event flag isn't set. if set, just clear it. IRQs set event flag 91*8b703f3dSMatthias Ringwald // __asm__("wfe"); 92*8b703f3dSMatthias Ringwald #endif 934317f7c8SMatthias Ringwald } 944317f7c8SMatthias Ringwald 954317f7c8SMatthias Ringwald #define HAL_FLASH_BANK_SIZE (128 * 1024) 964317f7c8SMatthias Ringwald #define HAL_FLASH_BANK_0_ADDR 0x080C0000 974317f7c8SMatthias Ringwald #define HAL_FLASH_BANK_1_ADDR 0x080E0000 984317f7c8SMatthias Ringwald #define HAL_FLASH_BANK_0_SECTOR FLASH_SECTOR_10 994317f7c8SMatthias Ringwald #define HAL_FLASH_BANK_1_SECTOR FLASH_SECTOR_11 1004317f7c8SMatthias Ringwald 1014317f7c8SMatthias Ringwald int btstack_main(int argc, char ** argv); 1024317f7c8SMatthias Ringwald 1034317f7c8SMatthias Ringwald // main.c 1044317f7c8SMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 1054317f7c8SMatthias Ringwald UNUSED(size); 1064317f7c8SMatthias Ringwald UNUSED(channel); 1074317f7c8SMatthias Ringwald bd_addr_t local_addr; 1084317f7c8SMatthias Ringwald if (packet_type != HCI_EVENT_PACKET) return; 1094317f7c8SMatthias Ringwald switch(hci_event_packet_get_type(packet)){ 1104317f7c8SMatthias Ringwald case BTSTACK_EVENT_STATE: 1114317f7c8SMatthias Ringwald if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) return; 1124317f7c8SMatthias Ringwald gap_local_bd_addr(local_addr); 1134317f7c8SMatthias Ringwald printf("BTstack up and running on %s.\n", bd_addr_to_str(local_addr)); 1144317f7c8SMatthias Ringwald break; 1154317f7c8SMatthias Ringwald default: 1164317f7c8SMatthias Ringwald break; 1174317f7c8SMatthias Ringwald } 1184317f7c8SMatthias Ringwald } 1194317f7c8SMatthias Ringwald 1204317f7c8SMatthias Ringwald void btstack_assert_failed(const char * file, uint16_t line_nr){ 1214317f7c8SMatthias Ringwald printf("ASSERT in %s, line %u failed - HALT\n", file, line_nr); 1224317f7c8SMatthias Ringwald while(1); 1234317f7c8SMatthias Ringwald } 1244317f7c8SMatthias Ringwald 1254317f7c8SMatthias Ringwald void port_main(void){ 1264317f7c8SMatthias Ringwald 1274317f7c8SMatthias Ringwald printf("BTstack on STM32 F4 Discovery with USB support starting...\n"); 1284317f7c8SMatthias Ringwald 1294317f7c8SMatthias Ringwald // start with BTstack init - especially configure HCI Transport 1304317f7c8SMatthias Ringwald btstack_memory_init(); 1314317f7c8SMatthias Ringwald btstack_run_loop_init(btstack_run_loop_embedded_get_instance()); 1324317f7c8SMatthias Ringwald 1334317f7c8SMatthias Ringwald // uncomment for packet log 1344317f7c8SMatthias Ringwald hci_dump_open( NULL, HCI_DUMP_STDOUT ); 1354317f7c8SMatthias Ringwald 1364317f7c8SMatthias Ringwald // init HCI 1374317f7c8SMatthias Ringwald hci_init(hci_transport_h2_stm32_instance(), NULL); 1384317f7c8SMatthias Ringwald 1394317f7c8SMatthias Ringwald // setup TLV Flash Sector implementation 1404317f7c8SMatthias Ringwald const hal_flash_bank_t * hal_flash_bank_impl = hal_flash_bank_stm32_init_instance( 1414317f7c8SMatthias Ringwald &hal_flash_bank_context, 1424317f7c8SMatthias Ringwald HAL_FLASH_BANK_SIZE, 1434317f7c8SMatthias Ringwald HAL_FLASH_BANK_0_SECTOR, 1444317f7c8SMatthias Ringwald HAL_FLASH_BANK_1_SECTOR, 1454317f7c8SMatthias Ringwald HAL_FLASH_BANK_0_ADDR, 1464317f7c8SMatthias Ringwald HAL_FLASH_BANK_1_ADDR); 1474317f7c8SMatthias Ringwald const btstack_tlv_t * btstack_tlv_impl = btstack_tlv_flash_bank_init_instance( 1484317f7c8SMatthias Ringwald &btstack_tlv_flash_bank_context, 1494317f7c8SMatthias Ringwald hal_flash_bank_impl, 1504317f7c8SMatthias Ringwald &hal_flash_bank_context); 1514317f7c8SMatthias Ringwald 1524317f7c8SMatthias Ringwald // setup global tlv 1534317f7c8SMatthias Ringwald btstack_tlv_set_instance(btstack_tlv_impl, &btstack_tlv_flash_bank_context); 1544317f7c8SMatthias Ringwald 1554317f7c8SMatthias Ringwald // setup Link Key DB using TLV 1564317f7c8SMatthias Ringwald 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); 1574317f7c8SMatthias Ringwald hci_set_link_key_db(btstack_link_key_db); 1584317f7c8SMatthias Ringwald 1594317f7c8SMatthias Ringwald // setup LE Device DB using TLV 1604317f7c8SMatthias Ringwald le_device_db_tlv_configure(btstack_tlv_impl, &btstack_tlv_flash_bank_context); 1614317f7c8SMatthias Ringwald 1624317f7c8SMatthias Ringwald #if 0 1634317f7c8SMatthias Ringwald #ifdef HAVE_HAL_AUDIO 1644317f7c8SMatthias Ringwald // setup audio 1654317f7c8SMatthias Ringwald btstack_audio_sink_set_instance(btstack_audio_embedded_sink_get_instance()); 1664317f7c8SMatthias Ringwald btstack_audio_source_set_instance(btstack_audio_embedded_source_get_instance()); 1674317f7c8SMatthias Ringwald #endif 1684317f7c8SMatthias Ringwald #endif 1694317f7c8SMatthias Ringwald 1704317f7c8SMatthias Ringwald // inform about BTstack state 1714317f7c8SMatthias Ringwald hci_event_callback_registration.callback = &packet_handler; 1724317f7c8SMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 1734317f7c8SMatthias Ringwald 1744317f7c8SMatthias Ringwald // hand over to btstack embedded code 1754317f7c8SMatthias Ringwald btstack_main(0, NULL); 1764317f7c8SMatthias Ringwald 1774317f7c8SMatthias Ringwald // go 1784317f7c8SMatthias Ringwald btstack_run_loop_execute(); 1794317f7c8SMatthias Ringwald } 180