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