xref: /btstack/port/stm32-f4discovery-usb/port/port.c (revision a2d541cc52e811a0fbc8671b731632bc146b53da)
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
232fca4dadSMilanka Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
242fca4dadSMilanka Ringwald  * GMBH 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"
59c8dfe071SMatthias Ringwald #include "hci_transport.h"
604317f7c8SMatthias Ringwald #include "hci_transport_h2_stm32.h"
614317f7c8SMatthias Ringwald 
624317f7c8SMatthias Ringwald #ifdef ENABLE_SEGGER_RTT
634317f7c8SMatthias Ringwald #include "SEGGER_RTT.h"
6409df40bdSMatthias Ringwald #include "hci_dump_segger_rtt_stdout.h"
6509df40bdSMatthias Ringwald #else
6609df40bdSMatthias Ringwald #include "hci_dump_embedded_stdout.h"
674317f7c8SMatthias Ringwald #endif
684317f7c8SMatthias Ringwald 
694317f7c8SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
704317f7c8SMatthias Ringwald static btstack_tlv_flash_bank_t btstack_tlv_flash_bank_context;
714317f7c8SMatthias Ringwald static hal_flash_bank_stm32_t   hal_flash_bank_context;
724317f7c8SMatthias Ringwald 
734317f7c8SMatthias Ringwald // hal_time_ms.h
744317f7c8SMatthias Ringwald #include "hal_time_ms.h"
754317f7c8SMatthias Ringwald uint32_t hal_time_ms(void){
764317f7c8SMatthias Ringwald     return HAL_GetTick();
774317f7c8SMatthias Ringwald }
784317f7c8SMatthias Ringwald 
794317f7c8SMatthias Ringwald // hal_cpu.h implementation
804317f7c8SMatthias Ringwald #include "hal_cpu.h"
814317f7c8SMatthias Ringwald 
824317f7c8SMatthias Ringwald void hal_cpu_disable_irqs(void){
834317f7c8SMatthias Ringwald     __disable_irq();
844317f7c8SMatthias Ringwald }
854317f7c8SMatthias Ringwald 
864317f7c8SMatthias Ringwald void hal_cpu_enable_irqs(void){
874317f7c8SMatthias Ringwald     __enable_irq();
884317f7c8SMatthias Ringwald }
894317f7c8SMatthias Ringwald 
904317f7c8SMatthias Ringwald void hal_cpu_enable_irqs_and_sleep(void){
914317f7c8SMatthias Ringwald     __enable_irq();
928b703f3dSMatthias Ringwald #if 0
938b703f3dSMatthias Ringwald     // temp disable until effect on RTT is clear
948b703f3dSMatthias Ringwald     // go to sleep if event flag isn't set. if set, just clear it. IRQs set event flag
958b703f3dSMatthias Ringwald     //  __asm__("wfe");
968b703f3dSMatthias Ringwald #endif
974317f7c8SMatthias Ringwald }
984317f7c8SMatthias Ringwald 
994317f7c8SMatthias Ringwald #define HAL_FLASH_BANK_SIZE (128 * 1024)
1004317f7c8SMatthias Ringwald #define HAL_FLASH_BANK_0_ADDR 0x080C0000
1014317f7c8SMatthias Ringwald #define HAL_FLASH_BANK_1_ADDR 0x080E0000
1024317f7c8SMatthias Ringwald #define HAL_FLASH_BANK_0_SECTOR FLASH_SECTOR_10
1034317f7c8SMatthias Ringwald #define HAL_FLASH_BANK_1_SECTOR FLASH_SECTOR_11
1044317f7c8SMatthias Ringwald 
1054317f7c8SMatthias Ringwald int btstack_main(int argc, char ** argv);
1064317f7c8SMatthias Ringwald 
1074317f7c8SMatthias Ringwald // main.c
1084317f7c8SMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1094317f7c8SMatthias Ringwald     UNUSED(size);
1104317f7c8SMatthias Ringwald     UNUSED(channel);
1114317f7c8SMatthias Ringwald     bd_addr_t local_addr;
1124317f7c8SMatthias Ringwald     if (packet_type != HCI_EVENT_PACKET) return;
113*a2d541ccSMatthias Ringwald     switch(hci_event_packet_get_type(packet)){
1144317f7c8SMatthias Ringwald         case BTSTACK_EVENT_STATE:
1154317f7c8SMatthias Ringwald             if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) return;
1164317f7c8SMatthias Ringwald             gap_local_bd_addr(local_addr);
1174317f7c8SMatthias Ringwald             printf("BTstack up and running on %s.\n", bd_addr_to_str(local_addr));
1184317f7c8SMatthias Ringwald             break;
1194317f7c8SMatthias Ringwald         default:
1204317f7c8SMatthias Ringwald             break;
1214317f7c8SMatthias Ringwald     }
1224317f7c8SMatthias Ringwald }
1234317f7c8SMatthias Ringwald 
1244317f7c8SMatthias Ringwald void btstack_assert_failed(const char * file, uint16_t line_nr){
1254317f7c8SMatthias Ringwald     printf("ASSERT in %s, line %u failed - HALT\n", file, line_nr);
1264317f7c8SMatthias Ringwald     while(1);
1274317f7c8SMatthias Ringwald }
1284317f7c8SMatthias Ringwald 
1294317f7c8SMatthias Ringwald void port_main(void){
1304317f7c8SMatthias Ringwald 
1314317f7c8SMatthias Ringwald     printf("BTstack on STM32 F4 Discovery with USB support starting...\n");
1324317f7c8SMatthias Ringwald 
1334317f7c8SMatthias Ringwald     // start with BTstack init - especially configure HCI Transport
1344317f7c8SMatthias Ringwald     btstack_memory_init();
1354317f7c8SMatthias Ringwald     btstack_run_loop_init(btstack_run_loop_embedded_get_instance());
1364317f7c8SMatthias Ringwald 
13709df40bdSMatthias Ringwald     // uncomment to enable packet logger
13809df40bdSMatthias Ringwald #ifdef ENABLE_SEGGER_RTT
13909df40bdSMatthias Ringwald     // hci_dump_init(hci_dump_segger_rtt_stdout_get_instance());
14009df40bdSMatthias Ringwald #else
14109df40bdSMatthias Ringwald     // hci_dump_init(hci_dump_embedded_stdout_get_instance());
14209df40bdSMatthias Ringwald #endif
1434317f7c8SMatthias Ringwald 
1444317f7c8SMatthias Ringwald     // init HCI
1454317f7c8SMatthias Ringwald     hci_init(hci_transport_h2_stm32_instance(), NULL);
1464317f7c8SMatthias Ringwald 
1474317f7c8SMatthias Ringwald     // setup TLV Flash Sector implementation
1484317f7c8SMatthias Ringwald     const hal_flash_bank_t * hal_flash_bank_impl = hal_flash_bank_stm32_init_instance(
1494317f7c8SMatthias Ringwald             &hal_flash_bank_context,
1504317f7c8SMatthias Ringwald             HAL_FLASH_BANK_SIZE,
1514317f7c8SMatthias Ringwald             HAL_FLASH_BANK_0_SECTOR,
1524317f7c8SMatthias Ringwald             HAL_FLASH_BANK_1_SECTOR,
1534317f7c8SMatthias Ringwald             HAL_FLASH_BANK_0_ADDR,
1544317f7c8SMatthias Ringwald             HAL_FLASH_BANK_1_ADDR);
1554317f7c8SMatthias Ringwald     const btstack_tlv_t * btstack_tlv_impl = btstack_tlv_flash_bank_init_instance(
1564317f7c8SMatthias Ringwald             &btstack_tlv_flash_bank_context,
1574317f7c8SMatthias Ringwald             hal_flash_bank_impl,
1584317f7c8SMatthias Ringwald             &hal_flash_bank_context);
1594317f7c8SMatthias Ringwald 
1604317f7c8SMatthias Ringwald     // setup global tlv
1614317f7c8SMatthias Ringwald     btstack_tlv_set_instance(btstack_tlv_impl, &btstack_tlv_flash_bank_context);
1624317f7c8SMatthias Ringwald 
1634317f7c8SMatthias Ringwald     // setup Link Key DB using TLV
1644317f7c8SMatthias 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);
1654317f7c8SMatthias Ringwald     hci_set_link_key_db(btstack_link_key_db);
1664317f7c8SMatthias Ringwald 
1674317f7c8SMatthias Ringwald     // setup LE Device DB using TLV
1684317f7c8SMatthias Ringwald     le_device_db_tlv_configure(btstack_tlv_impl, &btstack_tlv_flash_bank_context);
1694317f7c8SMatthias Ringwald 
1704317f7c8SMatthias Ringwald #ifdef HAVE_HAL_AUDIO
1714317f7c8SMatthias Ringwald     // setup audio
1724317f7c8SMatthias Ringwald    	btstack_audio_sink_set_instance(btstack_audio_embedded_sink_get_instance());
1734317f7c8SMatthias Ringwald     btstack_audio_source_set_instance(btstack_audio_embedded_source_get_instance());
1744317f7c8SMatthias Ringwald #endif
1754317f7c8SMatthias Ringwald 
1764317f7c8SMatthias Ringwald     // inform about BTstack state
1774317f7c8SMatthias Ringwald     hci_event_callback_registration.callback = &packet_handler;
1784317f7c8SMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
1794317f7c8SMatthias Ringwald 
1804317f7c8SMatthias Ringwald     // hand over to btstack embedded code
1814317f7c8SMatthias Ringwald     btstack_main(0, NULL);
1824317f7c8SMatthias Ringwald 
1834317f7c8SMatthias Ringwald     // go
1844317f7c8SMatthias Ringwald     btstack_run_loop_execute();
1854317f7c8SMatthias Ringwald }
186