xref: /btstack/port/stm32-f4discovery-usb/port/port.c (revision d20c764cf7101fbf842ceeacaa4e9dd0eed2991d)
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     __asm__("wfe");	// go to sleep if event flag isn't set. if set, just clear it. IRQs set event flag
89 }
90 
91 #define HAL_FLASH_BANK_SIZE (128 * 1024)
92 #define HAL_FLASH_BANK_0_ADDR 0x080C0000
93 #define HAL_FLASH_BANK_1_ADDR 0x080E0000
94 #define HAL_FLASH_BANK_0_SECTOR FLASH_SECTOR_10
95 #define HAL_FLASH_BANK_1_SECTOR FLASH_SECTOR_11
96 
97 int btstack_main(int argc, char ** argv);
98 
99 // main.c
100 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
101     UNUSED(size);
102     UNUSED(channel);
103     bd_addr_t local_addr;
104     if (packet_type != HCI_EVENT_PACKET) return;
105     switch(hci_event_packet_get_type(packet)){
106         case BTSTACK_EVENT_STATE:
107             if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) return;
108             gap_local_bd_addr(local_addr);
109             printf("BTstack up and running on %s.\n", bd_addr_to_str(local_addr));
110             break;
111         default:
112             break;
113     }
114 }
115 
116 void btstack_assert_failed(const char * file, uint16_t line_nr){
117     printf("ASSERT in %s, line %u failed - HALT\n", file, line_nr);
118     while(1);
119 }
120 
121 void port_main(void){
122 
123     printf("BTstack on STM32 F4 Discovery with USB support starting...\n");
124 
125     // start with BTstack init - especially configure HCI Transport
126     btstack_memory_init();
127     btstack_run_loop_init(btstack_run_loop_embedded_get_instance());
128 
129     // uncomment for packet log
130     hci_dump_open( NULL, HCI_DUMP_STDOUT );
131 
132     // init HCI
133     hci_init(hci_transport_h2_stm32_instance(), NULL);
134 
135     // setup TLV Flash Sector implementation
136     const hal_flash_bank_t * hal_flash_bank_impl = hal_flash_bank_stm32_init_instance(
137             &hal_flash_bank_context,
138             HAL_FLASH_BANK_SIZE,
139             HAL_FLASH_BANK_0_SECTOR,
140             HAL_FLASH_BANK_1_SECTOR,
141             HAL_FLASH_BANK_0_ADDR,
142             HAL_FLASH_BANK_1_ADDR);
143     const btstack_tlv_t * btstack_tlv_impl = btstack_tlv_flash_bank_init_instance(
144             &btstack_tlv_flash_bank_context,
145             hal_flash_bank_impl,
146             &hal_flash_bank_context);
147 
148     // setup global tlv
149     btstack_tlv_set_instance(btstack_tlv_impl, &btstack_tlv_flash_bank_context);
150 
151     // setup Link Key DB using TLV
152     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     hci_set_link_key_db(btstack_link_key_db);
154 
155     // setup LE Device DB using TLV
156     le_device_db_tlv_configure(btstack_tlv_impl, &btstack_tlv_flash_bank_context);
157 
158 #if  0
159 #ifdef HAVE_HAL_AUDIO
160     // setup audio
161    	btstack_audio_sink_set_instance(btstack_audio_embedded_sink_get_instance());
162     btstack_audio_source_set_instance(btstack_audio_embedded_source_get_instance());
163 #endif
164 #endif
165 
166     // inform about BTstack state
167     hci_event_callback_registration.callback = &packet_handler;
168     hci_add_event_handler(&hci_event_callback_registration);
169 
170     // hand over to btstack embedded code
171     btstack_main(0, NULL);
172 
173     // go
174     btstack_run_loop_execute();
175 }
176