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 <stdio.h>
41 #include <unistd.h>
42 #include <signal.h>
43
44 // include STM32 first to avoid warning about redefinition of UNUSED
45 #include "stm32f4xx_hal.h"
46 #include "main.h"
47
48 #include "port.h"
49
50 #include <stdio.h>
51 #include <stddef.h>
52
53 #include "port.h"
54 #include "btstack.h"
55 #include "btstack_debug.h"
56 #include "btstack_audio.h"
57 #include "btstack_run_loop_embedded.h"
58 #include "btstack_tlv.h"
59 #include "btstack_tlv_flash_bank.h"
60 #include "ble/le_device_db_tlv.h"
61 #include "classic/btstack_link_key_db_tlv.h"
62 #include "hal_flash_bank_stm32.h"
63 #include "hci_transport.h"
64 #include "hci_transport_h2_stm32.h"
65
66 #ifdef ENABLE_SEGGER_RTT
67 #include "SEGGER_RTT.h"
68 #include "hci_dump_segger_rtt_stdout.h"
69 #else
70 #include "hci_dump_embedded_stdout.h"
71 #endif
72
73 static btstack_packet_callback_registration_t hci_event_callback_registration;
74 static btstack_tlv_flash_bank_t btstack_tlv_flash_bank_context;
75 static hal_flash_bank_stm32_t hal_flash_bank_context;
76
77 // hal_time_ms.h
78 #include "hal_time_ms.h"
hal_time_ms(void)79 uint32_t hal_time_ms(void){
80 return HAL_GetTick();
81 }
82
83 // hal_cpu.h implementation
84 #include "hal_cpu.h"
85
hal_cpu_disable_irqs(void)86 void hal_cpu_disable_irqs(void){
87 __disable_irq();
88 }
89
hal_cpu_enable_irqs(void)90 void hal_cpu_enable_irqs(void){
91 __enable_irq();
92 }
93
hal_cpu_enable_irqs_and_sleep(void)94 void hal_cpu_enable_irqs_and_sleep(void){
95 __enable_irq();
96 #if 0
97 // temp disable until effect on RTT is clear
98 // go to sleep if event flag isn't set. if set, just clear it. IRQs set event flag
99 // __asm__("wfe");
100 #endif
101 }
102
103 #define HAL_FLASH_BANK_SIZE (128 * 1024)
104 #define HAL_FLASH_BANK_0_ADDR 0x080C0000
105 #define HAL_FLASH_BANK_1_ADDR 0x080E0000
106 #define HAL_FLASH_BANK_0_SECTOR FLASH_SECTOR_10
107 #define HAL_FLASH_BANK_1_SECTOR FLASH_SECTOR_11
108
109 int btstack_main(int argc, char ** argv);
110
_read(int fd,void * buf,size_t count)111 ssize_t _read(int fd, void * buf, size_t count){
112 UNUSED(fd);
113 UNUSED(buf);
114 UNUSED(count);
115 return -1;
116 }
117
_close(int file)118 int _close(int file){
119 UNUSED(file);
120 return -1;
121 }
122
_isatty(int file)123 int _isatty(int file){
124 UNUSED(file);
125 return -1;
126 }
127
_lseek(int file)128 int _lseek(int file){
129 UNUSED(file);
130 return -1;
131 }
132
_fstat(int file)133 int _fstat(int file){
134 UNUSED(file);
135 return -1;
136 }
137
_kill(pid_t pid,int sig)138 int _kill (pid_t pid, int sig) {
139 UNUSED(pid);
140 UNUSED(sig);
141 return -1;
142 }
143
_getpid(void)144 pid_t _getpid (void) {
145 return 0;
146 }
147
148 // main.c
packet_handler(uint8_t packet_type,uint16_t channel,uint8_t * packet,uint16_t size)149 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
150 UNUSED(size);
151 UNUSED(channel);
152 bd_addr_t local_addr;
153 if (packet_type != HCI_EVENT_PACKET) return;
154 switch(hci_event_packet_get_type(packet)){
155 case BTSTACK_EVENT_STATE:
156 if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) return;
157 gap_local_bd_addr(local_addr);
158 printf("BTstack up and running on %s.\n", bd_addr_to_str(local_addr));
159 break;
160 default:
161 break;
162 }
163 }
164
btstack_assert_failed(const char * file,uint16_t line_nr)165 void btstack_assert_failed(const char * file, uint16_t line_nr){
166 printf("ASSERT in %s, line %u failed - HALT\n", file, line_nr);
167 while(1);
168 }
169
port_main(void)170 void port_main(void){
171
172 printf("BTstack on STM32 F4 Discovery with USB support starting...\n");
173
174 // start with BTstack init - especially configure HCI Transport
175 btstack_memory_init();
176 btstack_run_loop_init(btstack_run_loop_embedded_get_instance());
177
178 // uncomment to enable packet logger
179 #ifdef ENABLE_SEGGER_RTT
180 // hci_dump_init(hci_dump_segger_rtt_stdout_get_instance());
181 #else
182 // hci_dump_init(hci_dump_embedded_stdout_get_instance());
183 #endif
184
185 // init HCI
186 hci_init(hci_transport_h2_stm32_instance(), NULL);
187
188 // setup TLV Flash Sector implementation
189 const hal_flash_bank_t * hal_flash_bank_impl = hal_flash_bank_stm32_init_instance(
190 &hal_flash_bank_context,
191 HAL_FLASH_BANK_SIZE,
192 HAL_FLASH_BANK_0_SECTOR,
193 HAL_FLASH_BANK_1_SECTOR,
194 HAL_FLASH_BANK_0_ADDR,
195 HAL_FLASH_BANK_1_ADDR);
196 const btstack_tlv_t * btstack_tlv_impl = btstack_tlv_flash_bank_init_instance(
197 &btstack_tlv_flash_bank_context,
198 hal_flash_bank_impl,
199 &hal_flash_bank_context);
200
201 // setup global tlv
202 btstack_tlv_set_instance(btstack_tlv_impl, &btstack_tlv_flash_bank_context);
203
204 // setup Link Key DB using TLV
205 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);
206 hci_set_link_key_db(btstack_link_key_db);
207
208 // setup LE Device DB using TLV
209 le_device_db_tlv_configure(btstack_tlv_impl, &btstack_tlv_flash_bank_context);
210
211 #ifdef HAVE_HAL_AUDIO
212 // setup audio
213 btstack_audio_sink_set_instance(btstack_audio_embedded_sink_get_instance());
214 btstack_audio_source_set_instance(btstack_audio_embedded_source_get_instance());
215 #endif
216
217 // inform about BTstack state
218 hci_event_callback_registration.callback = &packet_handler;
219 hci_add_event_handler(&hci_event_callback_registration);
220
221 // hand over to btstack embedded code
222 btstack_main(0, NULL);
223
224 // go
225 btstack_run_loop_execute();
226 }
227