xref: /btstack/port/renesas-tb-s1ja-cc256x/template/btstack_example/src/hal_entry.c (revision 09df40bd406fbaa7aefdc22d0a8cafbc30600db2)
1 /*
2  * Copyright (C) 2019 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__ "hal_entry.c"
39 
40 #include "../src/synergy_gen/hal_data.h"
41 
42 // hal_time_ms.h implementation
43 #include "hal_time_ms.h"
44 
45 volatile uint32_t time_ms;
46 
47 void timer_1ms(timer_callback_args_t *p_args){
48     (void) p_args;
49     time_ms++;
50 }
51 
52 uint32_t hal_time_ms(void){
53     return time_ms;
54 }
55 
56 // hal_cpu.h implementation
57 #include "hal_cpu.h"
58 
59 void hal_cpu_disable_irqs(void){
60     __disable_irq();
61 }
62 
63 void hal_cpu_enable_irqs(void){
64     __enable_irq();
65 }
66 
67 void hal_cpu_enable_irqs_and_sleep(void){
68     __enable_irq();
69     __asm__("wfe"); // go to sleep if event flag isn't set. if set, just clear it. IRQs set event flag
70 }
71 
72 // hal_uart_dma.h implementation
73 #include "hal_uart_dma.h"
74 #include "btstack_debug.h"
75 #include "btstack_ring_buffer.h"
76 #include "btstack_util.h"
77 
78 #define nShutdown_pin IOPORT_PORT_01_PIN_12
79 #define rts_pin       IOPORT_PORT_03_PIN_03
80 
81 // handlers
82 static void (*rx_done_handler)(void);
83 static void (*tx_done_handler)(void);
84 static void (*cts_irq_handler)(void);
85 
86 // ringbuffer to deal with eager fifo
87 static uint8_t rx_ring_buffer_storage[64];
88 static btstack_ring_buffer_t rx_ring_buffer;
89 
90 static volatile uint8_t * rx_buffer;
91 static volatile uint16_t rx_len;
92 
93 void hal_uart_dma_set_sleep(uint8_t sleep){
94     // TODO: configure RTS as GPIO and raise
95     (void) sleep;
96 }
97 
98 static void nShutdown_low(void){
99     g_ioport.p_api->pinWrite(nShutdown_pin, IOPORT_LEVEL_LOW);
100 }
101 
102 static void nShutdown_high(void){
103     g_ioport.p_api->pinWrite(nShutdown_pin, IOPORT_LEVEL_HIGH);
104 }
105 
106 // reset Bluetooth using n_shutdown
107 static void bluetooth_power_cycle(void){
108     nShutdown_low();
109     R_BSP_SoftwareDelay( 250, BSP_DELAY_UNITS_MILLISECONDS);
110     nShutdown_high();
111     R_BSP_SoftwareDelay( 250, BSP_DELAY_UNITS_MILLISECONDS);
112 }
113 
114 void user_uart_callback(uart_callback_args_t *p_args){
115     switch (p_args->event){
116         case UART_EVENT_TX_DATA_EMPTY:
117             (*tx_done_handler)();
118             break;
119         case UART_EVENT_RX_CHAR:
120             if (rx_len > 0){
121                 *rx_buffer++ = (uint8_t) p_args->data;
122                 rx_len--;
123                 if (rx_len == 0) {
124                     g_ioport.p_api->pinWrite(rts_pin, IOPORT_LEVEL_HIGH);
125                     (*rx_done_handler)();
126                 }
127             } else {
128                 // store in ring buffer
129                 uint8_t data = (uint8_t) p_args->data;
130                 btstack_ring_buffer_write(&rx_ring_buffer, &data, 1);
131             }
132             break;
133         case UART_EVENT_ERR_RXBUF_OVERFLOW:
134             log_info("UART_EVENT_ERR_RXBUF_OVERFLOW");
135             break;
136         default:
137             break;
138     }
139 }
140 
141 void hal_uart_dma_init(void){
142     bluetooth_power_cycle();
143     btstack_ring_buffer_init(&rx_ring_buffer, rx_ring_buffer_storage, sizeof(rx_ring_buffer_storage));
144 }
145 
146 void hal_uart_dma_set_block_received( void (*the_block_handler)(void)){
147     rx_done_handler = the_block_handler;
148 }
149 
150 void hal_uart_dma_set_block_sent( void (*the_block_handler)(void)){
151     tx_done_handler = the_block_handler;
152 }
153 
154 void hal_uart_dma_set_csr_irq_handler( void (*the_irq_handler)(void)){
155     // TODO: configure CTS GPIO as edge falling edge trigger
156     cts_irq_handler = the_irq_handler;
157 }
158 
159 int  hal_uart_dma_set_baud(uint32_t baud){
160     ssp_err_t error = g_uart0.p_api->baudSet(g_uart0.p_ctrl, baud);
161     if (error != SSP_SUCCESS){
162         log_error("hal_uart_dma_set_baud error 0x%x", error);
163     }
164     return 0;
165 }
166 
167 void hal_uart_dma_send_block(const uint8_t *data, uint16_t size){
168     g_uart0.p_api->write(g_uart0.p_ctrl, data, size);
169 }
170 
171 void hal_uart_dma_receive_block(uint8_t *data, uint16_t size){
172     // fill from  ring buffer
173     uint32_t number_of_bytes_read = 0;
174     btstack_ring_buffer_read(&rx_ring_buffer, data, size, &number_of_bytes_read);
175     size -= number_of_bytes_read;
176     data += number_of_bytes_read;
177     if (size == 0){
178         (*rx_done_handler)();
179         return;
180     }
181 
182     // Clear RTS and read from UART
183     rx_buffer = data;
184     rx_len = size;
185     g_ioport.p_api->pinWrite(rts_pin, IOPORT_LEVEL_LOW);
186 }
187 
188 // actual port
189 
190 #include "bluetooth.h"
191 #include "bluetooth_company_id.h"
192 #include "btstack_chipset_cc256x.h"
193 #include "btstack_defines.h"
194 #include "btstack_event.h"
195 #include "btstack_run_loop_embedded.h"
196 #include "btstack_tlv.h"
197 #include "btstack_tlv_flash_bank.h"
198 #include "hci.h"
199 #include "hci_cmd.h"
200 #include "hci_dump.h"
201 #include "hci_dump_embedded_stdout.h"
202 #include "hci_transport.h"
203 #include "btstack_memory.h"
204 #include "ble/le_device_db_tlv.h"
205 #include "classic/btstack_link_key_db_tlv.h"
206 #include "hal_flash_bank_synergy.h"
207 
208 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
209     UNUSED(size);
210     UNUSED(channel);
211     bd_addr_t local_addr;
212     if (packet_type != HCI_EVENT_PACKET) return;
213     switch(hci_event_packet_get_type(packet)){
214         case BTSTACK_EVENT_STATE:
215             if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) return;
216             gap_local_bd_addr(local_addr);
217             printf("BTstack up and running on %s.\n", bd_addr_to_str(local_addr));
218             break;
219         case HCI_EVENT_COMMAND_COMPLETE:
220             if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_version_information)){
221                 uint16_t manufacturer   = little_endian_read_16(packet, 10);
222                 uint16_t lmp_subversion = little_endian_read_16(packet, 12);
223                 // assert manufacturer is TI
224                 if (manufacturer != BLUETOOTH_COMPANY_ID_TEXAS_INSTRUMENTS_INC){
225                     printf("ERROR: Expected Bluetooth Chipset from TI but got manufacturer 0x%04x\n", manufacturer);
226                     break;
227                 }
228                 // assert correct init script is used based on expected lmp_subversion
229                 if (lmp_subversion != btstack_chipset_cc256x_lmp_subversion()){
230                     printf("Error: LMP Subversion does not match initscript! ");
231                     printf("Your initscripts is for %s chipset\n", btstack_chipset_cc256x_lmp_subversion() < lmp_subversion ? "an older" : "a newer");
232                     printf("Please update Makefile to include the appropriate bluetooth_init_cc256???.c file\n");
233                     break;
234                 }
235             }
236             break;
237         default:
238             break;
239     }
240 }
241 
242 // port.c
243 static btstack_packet_callback_registration_t hci_event_callback_registration;
244 static btstack_tlv_flash_bank_t btstack_tlv_flash_bank_context;
245 
246 static hal_flash_bank_synergy_t  hal_flash_bank_context;
247 #define HAL_FLASH_BANK_SIZE     ( 10224 )
248 #define HAL_FLASH_BANK_0_ADDR   ( 0x40100000 )
249 #define HAL_FLASH_BANK_1_ADDR   ( 0x40100400 )
250 
251 static const hci_transport_config_uart_t config = {
252     HCI_TRANSPORT_CONFIG_UART,
253     115200,
254     2000000,
255     1,
256     NULL
257 };
258 
259 int btstack_main(int argc, const char * argv[]);
260 void hal_entry(void) {
261 
262     // init hal
263     g_hal_init();
264 
265     // open uart, timer, flash
266     g_uart0.p_api->open(g_uart0.p_ctrl, g_uart0.p_cfg);
267     g_timer0.p_api->open(g_timer0.p_ctrl, g_timer0.p_cfg);
268     g_flash0.p_api->open(g_flash0.p_ctrl, g_flash0.p_cfg);
269 
270     // start with BTstack init - especially configure HCI Transport
271     btstack_memory_init();
272     btstack_run_loop_init(btstack_run_loop_embedded_get_instance());
273 
274     // enable HCI logging
275     // hci_dump_init(hci_dump_embedded_stdout_get_instance());
276 
277     // init HCI
278     hci_init(hci_transport_h4_instance(btstack_uart_block_embedded_instance()), (void*) &config);
279     hci_set_chipset(btstack_chipset_cc256x_instance());
280 
281     // setup TLV Flash Sector implementation
282     const hal_flash_bank_t * hal_flash_bank_impl = hal_flash_bank_synergy_init_instance(
283             &hal_flash_bank_context,
284             HAL_FLASH_BANK_SIZE,
285             HAL_FLASH_BANK_0_ADDR,
286             HAL_FLASH_BANK_1_ADDR);
287 
288     const btstack_tlv_t * btstack_tlv_impl = btstack_tlv_flash_bank_init_instance(
289             &btstack_tlv_flash_bank_context,
290             hal_flash_bank_impl,
291             &hal_flash_bank_context);
292 
293     // setup global tlv
294     btstack_tlv_set_instance(btstack_tlv_impl, &btstack_tlv_flash_bank_context);
295 
296     // setup Link Key DB using TLV
297     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);
298     hci_set_link_key_db(btstack_link_key_db);
299 
300     // setup LE Device DB using TLV
301     le_device_db_tlv_configure(btstack_tlv_impl, &btstack_tlv_flash_bank_context);
302 
303 #ifdef HAVE_HAL_AUDIO
304     // setup audio
305     btstack_audio_sink_set_instance(btstack_audio_embedded_sink_get_instance());
306     btstack_audio_source_set_instance(btstack_audio_embedded_source_get_instance());
307 #endif
308 
309     // inform about BTstack state
310     hci_event_callback_registration.callback = &packet_handler;
311     hci_add_event_handler(&hci_event_callback_registration);
312 
313     // hand over to btstack embedded code
314     btstack_main(0, NULL);
315 
316     // go
317     btstack_run_loop_execute();
318 }
319