1*4317f7c8SMatthias Ringwald /* 2*4317f7c8SMatthias Ringwald * Copyright (C) 2020 BlueKitchen GmbH 3*4317f7c8SMatthias Ringwald * 4*4317f7c8SMatthias Ringwald * Redistribution and use in source and binary forms, with or without 5*4317f7c8SMatthias Ringwald * modification, are permitted provided that the following conditions 6*4317f7c8SMatthias Ringwald * are met: 7*4317f7c8SMatthias Ringwald * 8*4317f7c8SMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 9*4317f7c8SMatthias Ringwald * notice, this list of conditions and the following disclaimer. 10*4317f7c8SMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 11*4317f7c8SMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 12*4317f7c8SMatthias Ringwald * documentation and/or other materials provided with the distribution. 13*4317f7c8SMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 14*4317f7c8SMatthias Ringwald * contributors may be used to endorse or promote products derived 15*4317f7c8SMatthias Ringwald * from this software without specific prior written permission. 16*4317f7c8SMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 17*4317f7c8SMatthias Ringwald * personal benefit and not for any commercial purpose or for 18*4317f7c8SMatthias Ringwald * monetary gain. 19*4317f7c8SMatthias Ringwald * 20*4317f7c8SMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21*4317f7c8SMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22*4317f7c8SMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23*4317f7c8SMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 24*4317f7c8SMatthias Ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25*4317f7c8SMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26*4317f7c8SMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27*4317f7c8SMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28*4317f7c8SMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29*4317f7c8SMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30*4317f7c8SMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31*4317f7c8SMatthias Ringwald * SUCH DAMAGE. 32*4317f7c8SMatthias Ringwald * 33*4317f7c8SMatthias Ringwald * Please inquire about commercial licensing options at 34*4317f7c8SMatthias Ringwald * [email protected] 35*4317f7c8SMatthias Ringwald * 36*4317f7c8SMatthias Ringwald */ 37*4317f7c8SMatthias Ringwald 38*4317f7c8SMatthias Ringwald #define __BTSTACK_FILE__ "port.c" 39*4317f7c8SMatthias Ringwald 40*4317f7c8SMatthias Ringwald // include STM32 first to avoid warning about redefinition of UNUSED 41*4317f7c8SMatthias Ringwald #include "stm32f4xx_hal.h" 42*4317f7c8SMatthias Ringwald #include "main.h" 43*4317f7c8SMatthias Ringwald 44*4317f7c8SMatthias Ringwald #include "port.h" 45*4317f7c8SMatthias Ringwald 46*4317f7c8SMatthias Ringwald #include <stdio.h> 47*4317f7c8SMatthias Ringwald #include <stddef.h> 48*4317f7c8SMatthias Ringwald 49*4317f7c8SMatthias Ringwald #include "port.h" 50*4317f7c8SMatthias Ringwald #include "btstack.h" 51*4317f7c8SMatthias Ringwald #include "btstack_debug.h" 52*4317f7c8SMatthias Ringwald #include "btstack_audio.h" 53*4317f7c8SMatthias Ringwald #include "btstack_run_loop_embedded.h" 54*4317f7c8SMatthias Ringwald #include "btstack_tlv.h" 55*4317f7c8SMatthias Ringwald #include "btstack_tlv_flash_bank.h" 56*4317f7c8SMatthias Ringwald #include "ble/le_device_db_tlv.h" 57*4317f7c8SMatthias Ringwald #include "classic/btstack_link_key_db_tlv.h" 58*4317f7c8SMatthias Ringwald #include "hal_flash_bank_stm32.h" 59*4317f7c8SMatthias Ringwald #include "hci_transport_h2_stm32.h" 60*4317f7c8SMatthias Ringwald 61*4317f7c8SMatthias Ringwald #ifdef ENABLE_SEGGER_RTT 62*4317f7c8SMatthias Ringwald #include "SEGGER_RTT.h" 63*4317f7c8SMatthias Ringwald #endif 64*4317f7c8SMatthias Ringwald 65*4317f7c8SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 66*4317f7c8SMatthias Ringwald static btstack_tlv_flash_bank_t btstack_tlv_flash_bank_context; 67*4317f7c8SMatthias Ringwald static hal_flash_bank_stm32_t hal_flash_bank_context; 68*4317f7c8SMatthias Ringwald 69*4317f7c8SMatthias Ringwald // hal_time_ms.h 70*4317f7c8SMatthias Ringwald #include "hal_time_ms.h" 71*4317f7c8SMatthias Ringwald uint32_t hal_time_ms(void){ 72*4317f7c8SMatthias Ringwald return HAL_GetTick(); 73*4317f7c8SMatthias Ringwald } 74*4317f7c8SMatthias Ringwald 75*4317f7c8SMatthias Ringwald // hal_cpu.h implementation 76*4317f7c8SMatthias Ringwald #include "hal_cpu.h" 77*4317f7c8SMatthias Ringwald 78*4317f7c8SMatthias Ringwald void hal_cpu_disable_irqs(void){ 79*4317f7c8SMatthias Ringwald __disable_irq(); 80*4317f7c8SMatthias Ringwald } 81*4317f7c8SMatthias Ringwald 82*4317f7c8SMatthias Ringwald void hal_cpu_enable_irqs(void){ 83*4317f7c8SMatthias Ringwald __enable_irq(); 84*4317f7c8SMatthias Ringwald } 85*4317f7c8SMatthias Ringwald 86*4317f7c8SMatthias Ringwald void hal_cpu_enable_irqs_and_sleep(void){ 87*4317f7c8SMatthias Ringwald __enable_irq(); 88*4317f7c8SMatthias Ringwald __asm__("wfe"); // go to sleep if event flag isn't set. if set, just clear it. IRQs set event flag 89*4317f7c8SMatthias Ringwald } 90*4317f7c8SMatthias Ringwald 91*4317f7c8SMatthias Ringwald #define HAL_FLASH_BANK_SIZE (128 * 1024) 92*4317f7c8SMatthias Ringwald #define HAL_FLASH_BANK_0_ADDR 0x080C0000 93*4317f7c8SMatthias Ringwald #define HAL_FLASH_BANK_1_ADDR 0x080E0000 94*4317f7c8SMatthias Ringwald #define HAL_FLASH_BANK_0_SECTOR FLASH_SECTOR_10 95*4317f7c8SMatthias Ringwald #define HAL_FLASH_BANK_1_SECTOR FLASH_SECTOR_11 96*4317f7c8SMatthias Ringwald 97*4317f7c8SMatthias Ringwald int btstack_main(int argc, char ** argv); 98*4317f7c8SMatthias Ringwald 99*4317f7c8SMatthias Ringwald // main.c 100*4317f7c8SMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 101*4317f7c8SMatthias Ringwald UNUSED(size); 102*4317f7c8SMatthias Ringwald UNUSED(channel); 103*4317f7c8SMatthias Ringwald bd_addr_t local_addr; 104*4317f7c8SMatthias Ringwald if (packet_type != HCI_EVENT_PACKET) return; 105*4317f7c8SMatthias Ringwald switch(hci_event_packet_get_type(packet)){ 106*4317f7c8SMatthias Ringwald case BTSTACK_EVENT_STATE: 107*4317f7c8SMatthias Ringwald if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) return; 108*4317f7c8SMatthias Ringwald gap_local_bd_addr(local_addr); 109*4317f7c8SMatthias Ringwald printf("BTstack up and running on %s.\n", bd_addr_to_str(local_addr)); 110*4317f7c8SMatthias Ringwald break; 111*4317f7c8SMatthias Ringwald default: 112*4317f7c8SMatthias Ringwald break; 113*4317f7c8SMatthias Ringwald } 114*4317f7c8SMatthias Ringwald } 115*4317f7c8SMatthias Ringwald 116*4317f7c8SMatthias Ringwald void btstack_assert_failed(const char * file, uint16_t line_nr){ 117*4317f7c8SMatthias Ringwald printf("ASSERT in %s, line %u failed - HALT\n", file, line_nr); 118*4317f7c8SMatthias Ringwald while(1); 119*4317f7c8SMatthias Ringwald } 120*4317f7c8SMatthias Ringwald 121*4317f7c8SMatthias Ringwald void port_main(void){ 122*4317f7c8SMatthias Ringwald 123*4317f7c8SMatthias Ringwald printf("BTstack on STM32 F4 Discovery with USB support starting...\n"); 124*4317f7c8SMatthias Ringwald 125*4317f7c8SMatthias Ringwald // start with BTstack init - especially configure HCI Transport 126*4317f7c8SMatthias Ringwald btstack_memory_init(); 127*4317f7c8SMatthias Ringwald btstack_run_loop_init(btstack_run_loop_embedded_get_instance()); 128*4317f7c8SMatthias Ringwald 129*4317f7c8SMatthias Ringwald // uncomment for packet log 130*4317f7c8SMatthias Ringwald hci_dump_open( NULL, HCI_DUMP_STDOUT ); 131*4317f7c8SMatthias Ringwald 132*4317f7c8SMatthias Ringwald // init HCI 133*4317f7c8SMatthias Ringwald hci_init(hci_transport_h2_stm32_instance(), NULL); 134*4317f7c8SMatthias Ringwald 135*4317f7c8SMatthias Ringwald // setup TLV Flash Sector implementation 136*4317f7c8SMatthias Ringwald const hal_flash_bank_t * hal_flash_bank_impl = hal_flash_bank_stm32_init_instance( 137*4317f7c8SMatthias Ringwald &hal_flash_bank_context, 138*4317f7c8SMatthias Ringwald HAL_FLASH_BANK_SIZE, 139*4317f7c8SMatthias Ringwald HAL_FLASH_BANK_0_SECTOR, 140*4317f7c8SMatthias Ringwald HAL_FLASH_BANK_1_SECTOR, 141*4317f7c8SMatthias Ringwald HAL_FLASH_BANK_0_ADDR, 142*4317f7c8SMatthias Ringwald HAL_FLASH_BANK_1_ADDR); 143*4317f7c8SMatthias Ringwald const btstack_tlv_t * btstack_tlv_impl = btstack_tlv_flash_bank_init_instance( 144*4317f7c8SMatthias Ringwald &btstack_tlv_flash_bank_context, 145*4317f7c8SMatthias Ringwald hal_flash_bank_impl, 146*4317f7c8SMatthias Ringwald &hal_flash_bank_context); 147*4317f7c8SMatthias Ringwald 148*4317f7c8SMatthias Ringwald // setup global tlv 149*4317f7c8SMatthias Ringwald btstack_tlv_set_instance(btstack_tlv_impl, &btstack_tlv_flash_bank_context); 150*4317f7c8SMatthias Ringwald 151*4317f7c8SMatthias Ringwald // setup Link Key DB using TLV 152*4317f7c8SMatthias 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); 153*4317f7c8SMatthias Ringwald hci_set_link_key_db(btstack_link_key_db); 154*4317f7c8SMatthias Ringwald 155*4317f7c8SMatthias Ringwald // setup LE Device DB using TLV 156*4317f7c8SMatthias Ringwald le_device_db_tlv_configure(btstack_tlv_impl, &btstack_tlv_flash_bank_context); 157*4317f7c8SMatthias Ringwald 158*4317f7c8SMatthias Ringwald #if 0 159*4317f7c8SMatthias Ringwald #ifdef HAVE_HAL_AUDIO 160*4317f7c8SMatthias Ringwald // setup audio 161*4317f7c8SMatthias Ringwald btstack_audio_sink_set_instance(btstack_audio_embedded_sink_get_instance()); 162*4317f7c8SMatthias Ringwald btstack_audio_source_set_instance(btstack_audio_embedded_source_get_instance()); 163*4317f7c8SMatthias Ringwald #endif 164*4317f7c8SMatthias Ringwald #endif 165*4317f7c8SMatthias Ringwald 166*4317f7c8SMatthias Ringwald // inform about BTstack state 167*4317f7c8SMatthias Ringwald hci_event_callback_registration.callback = &packet_handler; 168*4317f7c8SMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 169*4317f7c8SMatthias Ringwald 170*4317f7c8SMatthias Ringwald // hand over to btstack embedded code 171*4317f7c8SMatthias Ringwald btstack_main(0, NULL); 172*4317f7c8SMatthias Ringwald 173*4317f7c8SMatthias Ringwald // go 174*4317f7c8SMatthias Ringwald btstack_run_loop_execute(); 175*4317f7c8SMatthias Ringwald } 176