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