xref: /btstack/port/renesas-tb-s1ja-cc256x/template/btstack_example/src/hal_entry.c (revision d39264f239eb026fd486651e238a9ef65f8504ab)
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 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__ "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 
timer_1ms(timer_callback_args_t * p_args)47 void timer_1ms(timer_callback_args_t *p_args){
48     (void) p_args;
49     time_ms++;
50 }
51 
hal_time_ms(void)52 uint32_t hal_time_ms(void){
53     return time_ms;
54 }
55 
56 // hal_cpu.h implementation
57 #include "hal_cpu.h"
58 
hal_cpu_disable_irqs(void)59 void hal_cpu_disable_irqs(void){
60     __disable_irq();
61 }
62 
hal_cpu_enable_irqs(void)63 void hal_cpu_enable_irqs(void){
64     __enable_irq();
65 }
66 
hal_cpu_enable_irqs_and_sleep(void)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 
hal_uart_dma_set_sleep(uint8_t sleep)93 void hal_uart_dma_set_sleep(uint8_t sleep){
94     // TODO: configure RTS as GPIO and raise
95     (void) sleep;
96 }
97 
nShutdown_low(void)98 static void nShutdown_low(void){
99     g_ioport.p_api->pinWrite(nShutdown_pin, IOPORT_LEVEL_LOW);
100 }
101 
nShutdown_high(void)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
bluetooth_power_cycle(void)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 
user_uart_callback(uart_callback_args_t * p_args)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 
hal_uart_dma_init(void)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 
hal_uart_dma_set_block_received(void (* the_block_handler)(void))146 void hal_uart_dma_set_block_received( void (*the_block_handler)(void)){
147     rx_done_handler = the_block_handler;
148 }
149 
hal_uart_dma_set_block_sent(void (* the_block_handler)(void))150 void hal_uart_dma_set_block_sent( void (*the_block_handler)(void)){
151     tx_done_handler = the_block_handler;
152 }
153 
hal_uart_dma_set_csr_irq_handler(void (* the_irq_handler)(void))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 
hal_uart_dma_set_baud(uint32_t baud)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 
hal_uart_dma_send_block(const uint8_t * data,uint16_t size)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 
hal_uart_dma_receive_block(uint8_t * data,uint16_t size)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 "hci_transport_h4.h"
204 #include "btstack_memory.h"
205 #include "ble/le_device_db_tlv.h"
206 #include "classic/btstack_link_key_db_tlv.h"
207 #include "hal_flash_bank_synergy.h"
208 
packet_handler(uint8_t packet_type,uint16_t channel,uint8_t * packet,uint16_t size)209 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
210     UNUSED(size);
211     UNUSED(channel);
212     bd_addr_t local_addr;
213     if (packet_type != HCI_EVENT_PACKET) return;
214     switch(hci_event_packet_get_type(packet)){
215         case BTSTACK_EVENT_STATE:
216             if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) return;
217             gap_local_bd_addr(local_addr);
218             printf("BTstack up and running on %s.\n", bd_addr_to_str(local_addr));
219             break;
220         case HCI_EVENT_COMMAND_COMPLETE:
221             if (hci_event_command_complete_get_command_opcode(packet) == HCI_OPCODE_HCI_READ_LOCAL_VERSION_INFORMATION){
222                 uint16_t manufacturer   = little_endian_read_16(packet, 10);
223                 uint16_t lmp_subversion = little_endian_read_16(packet, 12);
224                 // assert manufacturer is TI
225                 if (manufacturer != BLUETOOTH_COMPANY_ID_TEXAS_INSTRUMENTS_INC){
226                     printf("ERROR: Expected Bluetooth Chipset from TI but got manufacturer 0x%04x\n", manufacturer);
227                     break;
228                 }
229                 // assert correct init script is used based on expected lmp_subversion
230                 if (lmp_subversion != btstack_chipset_cc256x_lmp_subversion()){
231                     printf("Error: LMP Subversion does not match initscript! ");
232                     printf("Your initscripts is for %s chipset\n", btstack_chipset_cc256x_lmp_subversion() < lmp_subversion ? "an older" : "a newer");
233                     printf("Please update Makefile to include the appropriate bluetooth_init_cc256???.c file\n");
234                     break;
235                 }
236             }
237             break;
238         default:
239             break;
240     }
241 }
242 
243 // port.c
244 static btstack_packet_callback_registration_t hci_event_callback_registration;
245 static btstack_tlv_flash_bank_t btstack_tlv_flash_bank_context;
246 
247 static hal_flash_bank_synergy_t  hal_flash_bank_context;
248 #define HAL_FLASH_BANK_SIZE     ( 10224 )
249 #define HAL_FLASH_BANK_0_ADDR   ( 0x40100000 )
250 #define HAL_FLASH_BANK_1_ADDR   ( 0x40100400 )
251 
252 static const hci_transport_config_uart_t config = {
253     HCI_TRANSPORT_CONFIG_UART,
254     115200,
255     2000000,
256     1,
257     NULL
258 };
259 
260 int btstack_main(int argc, const char * argv[]);
hal_entry(void)261 void hal_entry(void) {
262 
263     // init hal
264     g_hal_init();
265 
266     // open uart, timer, flash
267     g_uart0.p_api->open(g_uart0.p_ctrl, g_uart0.p_cfg);
268     g_timer0.p_api->open(g_timer0.p_ctrl, g_timer0.p_cfg);
269     g_flash0.p_api->open(g_flash0.p_ctrl, g_flash0.p_cfg);
270 
271     // start with BTstack init - especially configure HCI Transport
272     btstack_memory_init();
273     btstack_run_loop_init(btstack_run_loop_embedded_get_instance());
274 
275     // enable HCI logging
276     // hci_dump_init(hci_dump_embedded_stdout_get_instance());
277 
278     // init HCI
279     hci_init(hci_transport_h4_instance(btstack_uart_block_embedded_instance()), (void*) &config);
280     hci_set_chipset(btstack_chipset_cc256x_instance());
281 
282     // setup TLV Flash Sector implementation
283     const hal_flash_bank_t * hal_flash_bank_impl = hal_flash_bank_synergy_init_instance(
284             &hal_flash_bank_context,
285             HAL_FLASH_BANK_SIZE,
286             HAL_FLASH_BANK_0_ADDR,
287             HAL_FLASH_BANK_1_ADDR);
288 
289     const btstack_tlv_t * btstack_tlv_impl = btstack_tlv_flash_bank_init_instance(
290             &btstack_tlv_flash_bank_context,
291             hal_flash_bank_impl,
292             &hal_flash_bank_context);
293 
294     // setup global tlv
295     btstack_tlv_set_instance(btstack_tlv_impl, &btstack_tlv_flash_bank_context);
296 
297     // setup Link Key DB using TLV
298     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);
299     hci_set_link_key_db(btstack_link_key_db);
300 
301     // setup LE Device DB using TLV
302     le_device_db_tlv_configure(btstack_tlv_impl, &btstack_tlv_flash_bank_context);
303 
304 #ifdef HAVE_HAL_AUDIO
305     // setup audio
306     btstack_audio_sink_set_instance(btstack_audio_embedded_sink_get_instance());
307     btstack_audio_source_set_instance(btstack_audio_embedded_source_get_instance());
308 #endif
309 
310     // inform about BTstack state
311     hci_event_callback_registration.callback = &packet_handler;
312     hci_add_event_handler(&hci_event_callback_registration);
313 
314     // hand over to btstack embedded code
315     btstack_main(0, NULL);
316 
317     // go
318     btstack_run_loop_execute();
319 }
320