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 BLUEKITCHEN 24 * GMBH 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.h" 60 #include "hci_transport_h2_stm32.h" 61 62 #ifdef ENABLE_SEGGER_RTT 63 #include "SEGGER_RTT.h" 64 #include "hci_dump_segger_rtt_stdout.h" 65 #else 66 #include "hci_dump_embedded_stdout.h" 67 #endif 68 69 static btstack_packet_callback_registration_t hci_event_callback_registration; 70 static btstack_tlv_flash_bank_t btstack_tlv_flash_bank_context; 71 static hal_flash_bank_stm32_t hal_flash_bank_context; 72 73 // hal_time_ms.h 74 #include "hal_time_ms.h" 75 uint32_t hal_time_ms(void){ 76 return HAL_GetTick(); 77 } 78 79 // hal_cpu.h implementation 80 #include "hal_cpu.h" 81 82 void hal_cpu_disable_irqs(void){ 83 __disable_irq(); 84 } 85 86 void hal_cpu_enable_irqs(void){ 87 __enable_irq(); 88 } 89 90 void hal_cpu_enable_irqs_and_sleep(void){ 91 __enable_irq(); 92 #if 0 93 // temp disable until effect on RTT is clear 94 // go to sleep if event flag isn't set. if set, just clear it. IRQs set event flag 95 // __asm__("wfe"); 96 #endif 97 } 98 99 #define HAL_FLASH_BANK_SIZE (128 * 1024) 100 #define HAL_FLASH_BANK_0_ADDR 0x080C0000 101 #define HAL_FLASH_BANK_1_ADDR 0x080E0000 102 #define HAL_FLASH_BANK_0_SECTOR FLASH_SECTOR_10 103 #define HAL_FLASH_BANK_1_SECTOR FLASH_SECTOR_11 104 105 int btstack_main(int argc, char ** argv); 106 107 // main.c 108 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 109 UNUSED(size); 110 UNUSED(channel); 111 bd_addr_t local_addr; 112 if (packet_type != HCI_EVENT_PACKET) return; 113 switch(hci_event_packet_get_type(packet)){ 114 case BTSTACK_EVENT_STATE: 115 if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) return; 116 gap_local_bd_addr(local_addr); 117 printf("BTstack up and running on %s.\n", bd_addr_to_str(local_addr)); 118 break; 119 default: 120 break; 121 } 122 } 123 124 void btstack_assert_failed(const char * file, uint16_t line_nr){ 125 printf("ASSERT in %s, line %u failed - HALT\n", file, line_nr); 126 while(1); 127 } 128 129 void port_main(void){ 130 131 printf("BTstack on STM32 F4 Discovery with USB support starting...\n"); 132 133 // start with BTstack init - especially configure HCI Transport 134 btstack_memory_init(); 135 btstack_run_loop_init(btstack_run_loop_embedded_get_instance()); 136 137 // uncomment to enable packet logger 138 #ifdef ENABLE_SEGGER_RTT 139 // hci_dump_init(hci_dump_segger_rtt_stdout_get_instance()); 140 #else 141 // hci_dump_init(hci_dump_embedded_stdout_get_instance()); 142 #endif 143 144 // init HCI 145 hci_init(hci_transport_h2_stm32_instance(), NULL); 146 147 // setup TLV Flash Sector implementation 148 const hal_flash_bank_t * hal_flash_bank_impl = hal_flash_bank_stm32_init_instance( 149 &hal_flash_bank_context, 150 HAL_FLASH_BANK_SIZE, 151 HAL_FLASH_BANK_0_SECTOR, 152 HAL_FLASH_BANK_1_SECTOR, 153 HAL_FLASH_BANK_0_ADDR, 154 HAL_FLASH_BANK_1_ADDR); 155 const btstack_tlv_t * btstack_tlv_impl = btstack_tlv_flash_bank_init_instance( 156 &btstack_tlv_flash_bank_context, 157 hal_flash_bank_impl, 158 &hal_flash_bank_context); 159 160 // setup global tlv 161 btstack_tlv_set_instance(btstack_tlv_impl, &btstack_tlv_flash_bank_context); 162 163 // setup Link Key DB using TLV 164 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); 165 hci_set_link_key_db(btstack_link_key_db); 166 167 // setup LE Device DB using TLV 168 le_device_db_tlv_configure(btstack_tlv_impl, &btstack_tlv_flash_bank_context); 169 170 #ifdef HAVE_HAL_AUDIO 171 // setup audio 172 btstack_audio_sink_set_instance(btstack_audio_embedded_sink_get_instance()); 173 btstack_audio_source_set_instance(btstack_audio_embedded_source_get_instance()); 174 #endif 175 176 // inform about BTstack state 177 hci_event_callback_registration.callback = &packet_handler; 178 hci_add_event_handler(&hci_event_callback_registration); 179 180 // hand over to btstack embedded code 181 btstack_main(0, NULL); 182 183 // go 184 btstack_run_loop_execute(); 185 } 186