1c894dca1SMatthias Ringwald /*******************************************************************************
2c894dca1SMatthias Ringwald * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
3c894dca1SMatthias Ringwald * Author: Ismail H. Kose <[email protected]>
4c894dca1SMatthias Ringwald *
5c894dca1SMatthias Ringwald * Permission is hereby granted, free of charge, to any person obtaining a
6c894dca1SMatthias Ringwald * copy of this software and associated documentation files (the "Software"),
7c894dca1SMatthias Ringwald * to deal in the Software without restriction, including without limitation
8c894dca1SMatthias Ringwald * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9c894dca1SMatthias Ringwald * and/or sell copies of the Software, and to permit persons to whom the
10c894dca1SMatthias Ringwald * Software is furnished to do so, subject to the following conditions:
11c894dca1SMatthias Ringwald *
12c894dca1SMatthias Ringwald * The above copyright notice and this permission notice shall be included
13c894dca1SMatthias Ringwald * in all copies or substantial portions of the Software.
14c894dca1SMatthias Ringwald *
15c894dca1SMatthias Ringwald * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16c894dca1SMatthias Ringwald * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17c894dca1SMatthias Ringwald * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18c894dca1SMatthias Ringwald * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
19c894dca1SMatthias Ringwald * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20c894dca1SMatthias Ringwald * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21c894dca1SMatthias Ringwald * OTHER DEALINGS IN THE SOFTWARE.
22c894dca1SMatthias Ringwald *
23c894dca1SMatthias Ringwald * Except as contained in this notice, the name of Maxim Integrated
24c894dca1SMatthias Ringwald * Products, Inc. shall not be used except as stated in the Maxim Integrated
25c894dca1SMatthias Ringwald * Products, Inc. Branding Policy.
26c894dca1SMatthias Ringwald *
27c894dca1SMatthias Ringwald * The mere transfer of this software does not imply any licenses
28c894dca1SMatthias Ringwald * of trade secrets, proprietary technology, copyrights, patents,
29c894dca1SMatthias Ringwald * trademarks, maskwork rights, or any other form of intellectual
30c894dca1SMatthias Ringwald * property whatsoever. Maxim Integrated Products, Inc. retains all
31c894dca1SMatthias Ringwald * ownership rights.
32c894dca1SMatthias Ringwald *******************************************************************************
33c894dca1SMatthias Ringwald */
34c894dca1SMatthias Ringwald
35c894dca1SMatthias Ringwald #include <stdio.h>
36c894dca1SMatthias Ringwald #include <string.h>
3784e26812SMatthias Ringwald
3884e26812SMatthias Ringwald // MXC
39c894dca1SMatthias Ringwald #include "lp.h"
40c894dca1SMatthias Ringwald #include "uart.h"
41c894dca1SMatthias Ringwald #include "board.h"
4224efff18SMatthias Ringwald #include "led.h"
43c894dca1SMatthias Ringwald
4484e26812SMatthias Ringwald // BTstack Core
4584e26812SMatthias Ringwald #include "btstack_debug.h"
46c894dca1SMatthias Ringwald #include "btstack.h"
47c894dca1SMatthias Ringwald #include "btstack_config.h"
48c894dca1SMatthias Ringwald #include "btstack_run_loop_embedded.h"
49c894dca1SMatthias Ringwald #include "btstack_chipset_cc256x.h"
50cccba290SMatthias Ringwald #include "hci_dump_embedded_stdout.h"
51c8dfe071SMatthias Ringwald #include "hci_transport.h"
52c8dfe071SMatthias Ringwald #include "hci_transport_h4.h"
5384e26812SMatthias Ringwald
5484e26812SMatthias Ringwald // BTstack HALs
5584e26812SMatthias Ringwald #include "hal_tick.h"
5684e26812SMatthias Ringwald #include "hal_stdin.h"
5784e26812SMatthias Ringwald
58c894dca1SMatthias Ringwald #include "btstack_port.h"
59c894dca1SMatthias Ringwald
60c894dca1SMatthias Ringwald #define CC256X_UART_ID 0
61c894dca1SMatthias Ringwald #define UART_RXFIFO_USABLE (MXC_UART_FIFO_DEPTH-3)
62c894dca1SMatthias Ringwald
63c894dca1SMatthias Ringwald static uint32_t baud_rate;
64c894dca1SMatthias Ringwald
65c894dca1SMatthias Ringwald // rx state
66c894dca1SMatthias Ringwald static int bytes_to_read = 0;
67c894dca1SMatthias Ringwald static uint8_t * rx_buffer_ptr = 0;
68c894dca1SMatthias Ringwald
69c894dca1SMatthias Ringwald // tx state
70c894dca1SMatthias Ringwald static int bytes_to_write = 0;
71c894dca1SMatthias Ringwald static uint8_t * tx_buffer_ptr = 0;
72c894dca1SMatthias Ringwald
73c894dca1SMatthias Ringwald const gpio_cfg_t PAN1326_SLOW_CLK = { PORT_1, PIN_7, GPIO_FUNC_GPIO,
74c894dca1SMatthias Ringwald GPIO_PAD_NORMAL };
75c894dca1SMatthias Ringwald const gpio_cfg_t PAN1326_nSHUTD = { PORT_1, PIN_6, GPIO_FUNC_GPIO,
76c894dca1SMatthias Ringwald GPIO_PAD_NORMAL };
77c894dca1SMatthias Ringwald const gpio_cfg_t PAN1326_HCIRTS = { PORT_0, PIN_3, GPIO_FUNC_GPIO,
788959f869SMatthias Ringwald GPIO_PAD_INPUT_PULLUP };
798959f869SMatthias Ringwald const gpio_cfg_t PAN1326_HCICTS = { PORT_0, PIN_2, GPIO_FUNC_GPIO,
80c894dca1SMatthias Ringwald GPIO_PAD_NORMAL };
81c894dca1SMatthias Ringwald
dummy_handler(void)82c894dca1SMatthias Ringwald static void dummy_handler(void) {};
83c894dca1SMatthias Ringwald static void (*rx_done_handler)(void) = dummy_handler;
84c894dca1SMatthias Ringwald static void (*tx_done_handler)(void) = dummy_handler;
85c894dca1SMatthias Ringwald
8624efff18SMatthias Ringwald
8724efff18SMatthias Ringwald
hal_cpu_disable_irqs(void)88c894dca1SMatthias Ringwald void hal_cpu_disable_irqs(void)
89c894dca1SMatthias Ringwald {
90c894dca1SMatthias Ringwald __disable_irq();
91c894dca1SMatthias Ringwald }
92c894dca1SMatthias Ringwald
hal_cpu_enable_irqs(void)93c894dca1SMatthias Ringwald void hal_cpu_enable_irqs(void)
94c894dca1SMatthias Ringwald {
95c894dca1SMatthias Ringwald __enable_irq();
96c894dca1SMatthias Ringwald }
hal_cpu_enable_irqs_and_sleep(void)97c894dca1SMatthias Ringwald void hal_cpu_enable_irqs_and_sleep(void)
98c894dca1SMatthias Ringwald {
9984e26812SMatthias Ringwald __enable_irq();
10084e26812SMatthias Ringwald /* TODO: Add sleep mode */
101c894dca1SMatthias Ringwald }
102c894dca1SMatthias Ringwald
hal_uart_dma_send_block(const uint8_t * buffer,uint16_t len)103c894dca1SMatthias Ringwald void hal_uart_dma_send_block(const uint8_t *buffer, uint16_t len)
104c894dca1SMatthias Ringwald {
105c894dca1SMatthias Ringwald tx_buffer_ptr = (uint8_t *)buffer;
106c894dca1SMatthias Ringwald bytes_to_write = len;
107c894dca1SMatthias Ringwald }
108c894dca1SMatthias Ringwald
hal_uart_dma_receive_block(uint8_t * buffer,uint16_t len)109c894dca1SMatthias Ringwald void hal_uart_dma_receive_block(uint8_t *buffer, uint16_t len)
110c894dca1SMatthias Ringwald {
111c894dca1SMatthias Ringwald rx_buffer_ptr = buffer;
112c894dca1SMatthias Ringwald bytes_to_read = len;
113c894dca1SMatthias Ringwald }
114c894dca1SMatthias Ringwald
hal_btstack_run_loop_execute_once(void)115c894dca1SMatthias Ringwald void hal_btstack_run_loop_execute_once(void)
116c894dca1SMatthias Ringwald {
117c894dca1SMatthias Ringwald int rx_avail;
118c894dca1SMatthias Ringwald int num_rx_bytes;
119c894dca1SMatthias Ringwald int tx_avail;
120c894dca1SMatthias Ringwald int rx_bytes;
121c894dca1SMatthias Ringwald int tx_bytes;
122c894dca1SMatthias Ringwald int ret;
123c894dca1SMatthias Ringwald
124c894dca1SMatthias Ringwald while (bytes_to_read) {
125c894dca1SMatthias Ringwald rx_avail = UART_NumReadAvail(MXC_UART_GET_UART(CC256X_UART_ID));
126c894dca1SMatthias Ringwald if (!rx_avail)
127c894dca1SMatthias Ringwald break;
128c894dca1SMatthias Ringwald
129c894dca1SMatthias Ringwald if (bytes_to_read > rx_avail)
130c894dca1SMatthias Ringwald num_rx_bytes = rx_avail;
131c894dca1SMatthias Ringwald else
132c894dca1SMatthias Ringwald num_rx_bytes = bytes_to_read;
133c894dca1SMatthias Ringwald
134c894dca1SMatthias Ringwald ret = UART_Read(MXC_UART_GET_UART(CC256X_UART_ID), rx_buffer_ptr, num_rx_bytes, &rx_bytes);
135c894dca1SMatthias Ringwald if (ret < 0)
136c894dca1SMatthias Ringwald break;
137c894dca1SMatthias Ringwald
138c894dca1SMatthias Ringwald rx_buffer_ptr += rx_bytes;
139c894dca1SMatthias Ringwald bytes_to_read -= rx_bytes;
140c894dca1SMatthias Ringwald
141c894dca1SMatthias Ringwald if (bytes_to_read < 0) {
142c894dca1SMatthias Ringwald bytes_to_read = 0;
143c894dca1SMatthias Ringwald }
144c894dca1SMatthias Ringwald
145c894dca1SMatthias Ringwald if (bytes_to_read == 0){
146c894dca1SMatthias Ringwald (*rx_done_handler)();
147c894dca1SMatthias Ringwald }
148c894dca1SMatthias Ringwald }
149c894dca1SMatthias Ringwald
150c894dca1SMatthias Ringwald while (bytes_to_write) {
151c894dca1SMatthias Ringwald tx_avail = UART_NumWriteAvail(MXC_UART_GET_UART(CC256X_UART_ID));
152c894dca1SMatthias Ringwald if (!tx_avail)
153c894dca1SMatthias Ringwald break;
154c894dca1SMatthias Ringwald
155c894dca1SMatthias Ringwald if (bytes_to_write > tx_avail)
156c894dca1SMatthias Ringwald tx_bytes = tx_avail;
157c894dca1SMatthias Ringwald else
158c894dca1SMatthias Ringwald tx_bytes = bytes_to_write;
159c894dca1SMatthias Ringwald
160c894dca1SMatthias Ringwald ret = UART_Write(MXC_UART_GET_UART(CC256X_UART_ID), tx_buffer_ptr, tx_bytes);
161c894dca1SMatthias Ringwald if (ret < 0)
162c894dca1SMatthias Ringwald break;
163c894dca1SMatthias Ringwald bytes_to_write -= tx_bytes;
164c894dca1SMatthias Ringwald tx_buffer_ptr += tx_bytes;
165c894dca1SMatthias Ringwald if (bytes_to_write < 0) {
166c894dca1SMatthias Ringwald bytes_to_write = 0;
167c894dca1SMatthias Ringwald }
168c894dca1SMatthias Ringwald
169c894dca1SMatthias Ringwald if (bytes_to_write == 0){
170c894dca1SMatthias Ringwald (*tx_done_handler)();
171c894dca1SMatthias Ringwald }
172c894dca1SMatthias Ringwald }
173c894dca1SMatthias Ringwald
174c894dca1SMatthias Ringwald btstack_run_loop_embedded_execute_once();
175c894dca1SMatthias Ringwald }
176c894dca1SMatthias Ringwald
hal_uart_init(void)177c894dca1SMatthias Ringwald void hal_uart_init(void)
178c894dca1SMatthias Ringwald {
179c894dca1SMatthias Ringwald int error = 0;
180c894dca1SMatthias Ringwald uart_cfg_t cfg;
181c894dca1SMatthias Ringwald
182c894dca1SMatthias Ringwald cfg.parity = UART_PARITY_DISABLE;
183c894dca1SMatthias Ringwald cfg.size = UART_DATA_SIZE_8_BITS;
184c894dca1SMatthias Ringwald cfg.extra_stop = 0;
185c894dca1SMatthias Ringwald cfg.cts = 1;
186c894dca1SMatthias Ringwald cfg.rts = 1;
187c894dca1SMatthias Ringwald
188c894dca1SMatthias Ringwald cfg.baud = baud_rate;
189c894dca1SMatthias Ringwald
190c894dca1SMatthias Ringwald sys_cfg_uart_t sys_cfg;
191c894dca1SMatthias Ringwald sys_cfg.clk_scale = CLKMAN_SCALE_AUTO;
192c894dca1SMatthias Ringwald
193c894dca1SMatthias Ringwald sys_cfg.io_cfg = (ioman_cfg_t )IOMAN_UART(0,
194c894dca1SMatthias Ringwald IOMAN_MAP_B, // io_map
195c894dca1SMatthias Ringwald IOMAN_MAP_B, // cts_map
196c894dca1SMatthias Ringwald IOMAN_MAP_B, // rts_map
197c894dca1SMatthias Ringwald 1, // io_en
198c894dca1SMatthias Ringwald 1, // cts_en
199c894dca1SMatthias Ringwald 1); //rts_en
200c894dca1SMatthias Ringwald
201c894dca1SMatthias Ringwald if ((error = UART_Init(MXC_UART_GET_UART(CC256X_UART_ID), &cfg, &sys_cfg)) != E_NO_ERROR) {
202c894dca1SMatthias Ringwald printf("Error initializing UART %d\n", error);
203c894dca1SMatthias Ringwald while (1);
204c894dca1SMatthias Ringwald } else {
205c894dca1SMatthias Ringwald printf("BTSTACK UART Initialized\n");
206c894dca1SMatthias Ringwald }
207c894dca1SMatthias Ringwald
208c894dca1SMatthias Ringwald MXC_UART_GET_UART(CC256X_UART_ID)->ctrl |= MXC_F_UART_CTRL_CTS_POLARITY | MXC_F_UART_CTRL_RTS_POLARITY;
209c894dca1SMatthias Ringwald MXC_UART_GET_UART(CC256X_UART_ID)->ctrl &= ~((MXC_UART_FIFO_DEPTH - 4) << (MXC_F_UART_CTRL_RTS_LEVEL_POS));
210c894dca1SMatthias Ringwald MXC_UART_GET_UART(CC256X_UART_ID)->ctrl |= ((UART_RXFIFO_USABLE) << MXC_F_UART_CTRL_RTS_LEVEL_POS);
211c894dca1SMatthias Ringwald }
212c894dca1SMatthias Ringwald
hal_uart_dma_set_baud(uint32_t baud)213c894dca1SMatthias Ringwald int hal_uart_dma_set_baud(uint32_t baud){
214c894dca1SMatthias Ringwald baud_rate = baud;
215c894dca1SMatthias Ringwald printf("BAUD RATE IS = %d \n", baud);
216c894dca1SMatthias Ringwald hal_uart_init();
217c894dca1SMatthias Ringwald return baud_rate;
218c894dca1SMatthias Ringwald }
219c894dca1SMatthias Ringwald
hal_uart_dma_init(void)220c894dca1SMatthias Ringwald void hal_uart_dma_init(void){
221c894dca1SMatthias Ringwald bytes_to_write = 0;
222c894dca1SMatthias Ringwald bytes_to_read = 0;
223c894dca1SMatthias Ringwald hal_uart_dma_set_baud(115200);
224c894dca1SMatthias Ringwald }
225c894dca1SMatthias Ringwald
hal_uart_dma_set_block_received(void (* block_handler)(void))226c894dca1SMatthias Ringwald void hal_uart_dma_set_block_received( void (*block_handler)(void)){
227c894dca1SMatthias Ringwald rx_done_handler = block_handler;
228c894dca1SMatthias Ringwald }
229c894dca1SMatthias Ringwald
hal_uart_dma_set_block_sent(void (* block_handler)(void))230c894dca1SMatthias Ringwald void hal_uart_dma_set_block_sent( void (*block_handler)(void)){
231c894dca1SMatthias Ringwald
232c894dca1SMatthias Ringwald tx_done_handler = block_handler;
233c894dca1SMatthias Ringwald }
234c894dca1SMatthias Ringwald
hal_uart_dma_set_csr_irq_handler(void (* csr_irq_handler)(void))235c894dca1SMatthias Ringwald void hal_uart_dma_set_csr_irq_handler( void (*csr_irq_handler)(void)){
236c894dca1SMatthias Ringwald
237c894dca1SMatthias Ringwald }
238c894dca1SMatthias Ringwald
hal_uart_dma_set_sleep(uint8_t sleep)239c894dca1SMatthias Ringwald void hal_uart_dma_set_sleep(uint8_t sleep){
240c894dca1SMatthias Ringwald
241c894dca1SMatthias Ringwald }
242c894dca1SMatthias Ringwald
init_slow_clock(void)243c894dca1SMatthias Ringwald void init_slow_clock(void)
244c894dca1SMatthias Ringwald {
245c894dca1SMatthias Ringwald MXC_PWRSEQ->reg0 &= ~(MXC_F_PWRSEQ_REG0_PWR_RTCEN_RUN | MXC_F_PWRSEQ_REG0_PWR_RTCEN_SLP);
246c894dca1SMatthias Ringwald MXC_PWRSEQ->reg4 &= ~MXC_F_PWRSEQ_REG4_PWR_PSEQ_32K_EN;
247c894dca1SMatthias Ringwald MXC_PWRSEQ->reg0 |= MXC_F_PWRSEQ_REG0_PWR_RTCEN_RUN | MXC_F_PWRSEQ_REG0_PWR_RTCEN_SLP; // Enable RTC
248c894dca1SMatthias Ringwald hal_delay_us(1);
249c894dca1SMatthias Ringwald MXC_PWRSEQ->reg4 |= MXC_F_PWRSEQ_REG4_PWR_PSEQ_32K_EN; // Enable the RTC out of P1.7
250c894dca1SMatthias Ringwald }
251c894dca1SMatthias Ringwald
bt_comm_init()252c894dca1SMatthias Ringwald int bt_comm_init() {
253c894dca1SMatthias Ringwald int error = 0;
254c894dca1SMatthias Ringwald int cnt = 0;
255c894dca1SMatthias Ringwald
256c894dca1SMatthias Ringwald hal_tick_init();
257c894dca1SMatthias Ringwald hal_delay_us(1);
2588959f869SMatthias Ringwald
2598959f869SMatthias Ringwald /* HCI module RTS as input with 25k pullup */
260c894dca1SMatthias Ringwald if ((error = GPIO_Config(&PAN1326_HCIRTS)) != E_NO_ERROR) {
261c894dca1SMatthias Ringwald printf("Error setting PAN1326_HCIRTS %d\n", error);
262c894dca1SMatthias Ringwald }
263c894dca1SMatthias Ringwald GPIO_OutSet(&PAN1326_HCIRTS);
2648959f869SMatthias Ringwald
265c894dca1SMatthias Ringwald init_slow_clock();
266c894dca1SMatthias Ringwald /*
267c894dca1SMatthias Ringwald * when enabling the P1.7 RTC output, P1.6 will be hardcoded to an input with 25k pullup enabled.
268c894dca1SMatthias Ringwald * There is an internal pullup, so when it is set as an input, it will float high.
269c894dca1SMatthias Ringwald * The PAN1326B data sheet says the NSHUTD pin is pulled down, but the input impedance is stated at 1Meg Ohm,
270c894dca1SMatthias Ringwald * The so the 25k pullup should be enough to reach the minimum 1.42V to enable the device.
271c894dca1SMatthias Ringwald * */
2728959f869SMatthias Ringwald
2738959f869SMatthias Ringwald /* Force PAN1326 shutdown to be output and take it out of reset */
2748959f869SMatthias Ringwald if ((error = GPIO_Config(&PAN1326_nSHUTD)) != E_NO_ERROR) {
2758959f869SMatthias Ringwald printf("Error setting PAN1326_nSHUTD %d\n", error);
2768959f869SMatthias Ringwald }
2778959f869SMatthias Ringwald GPIO_OutSet(&PAN1326_nSHUTD);
2788959f869SMatthias Ringwald
2798959f869SMatthias Ringwald /*Check the module is ready to receive data */
280c894dca1SMatthias Ringwald while (GPIO_InGet(&PAN1326_HCIRTS)) {
281c894dca1SMatthias Ringwald cnt++;
282c894dca1SMatthias Ringwald }
283c894dca1SMatthias Ringwald
284c894dca1SMatthias Ringwald printf("%s CC256X init completed. cnt: %d \n", __func__, cnt);
285c894dca1SMatthias Ringwald return 0;
286c894dca1SMatthias Ringwald }
287c894dca1SMatthias Ringwald
288c894dca1SMatthias Ringwald static hci_transport_config_uart_t config = {
289c894dca1SMatthias Ringwald HCI_TRANSPORT_CONFIG_UART,
290c894dca1SMatthias Ringwald 115200,
291c894dca1SMatthias Ringwald 4000000,
292c894dca1SMatthias Ringwald 1, // flow control
293c894dca1SMatthias Ringwald "max32630fthr",
294c894dca1SMatthias Ringwald };
295c894dca1SMatthias Ringwald
296183c110aSMatthias Ringwald // hal_led.h implementation
297183c110aSMatthias Ringwald #include "hal_led.h"
hal_led_off(void)298183c110aSMatthias Ringwald void hal_led_off(void){
29924efff18SMatthias Ringwald LED_Off(LED_BLUE);
300183c110aSMatthias Ringwald }
30124efff18SMatthias Ringwald
hal_led_on(void)302183c110aSMatthias Ringwald void hal_led_on(void){
30324efff18SMatthias Ringwald LED_On(LED_BLUE);
304183c110aSMatthias Ringwald }
30524efff18SMatthias Ringwald
hal_led_toggle(void)306183c110aSMatthias Ringwald void hal_led_toggle(void){
30724efff18SMatthias Ringwald LED_Toggle(LED_BLUE);
308183c110aSMatthias Ringwald }
309183c110aSMatthias Ringwald
31084e26812SMatthias Ringwald // hal_stdin.h
31184e26812SMatthias Ringwald static uint8_t stdin_buffer[1];
31284e26812SMatthias Ringwald static void (*stdin_handler)(char c);
31384e26812SMatthias Ringwald
31484e26812SMatthias Ringwald static uart_req_t uart_byte_request;
31584e26812SMatthias Ringwald
uart_rx_handler(uart_req_t * request,int error)31684e26812SMatthias Ringwald static void uart_rx_handler(uart_req_t *request, int error)
31784e26812SMatthias Ringwald {
31884e26812SMatthias Ringwald if (stdin_handler){
31984e26812SMatthias Ringwald (*stdin_handler)(stdin_buffer[0]);
32084e26812SMatthias Ringwald }
32184e26812SMatthias Ringwald UART_ReadAsync(MXC_UART_GET_UART(CONSOLE_UART), &uart_byte_request);
32284e26812SMatthias Ringwald }
32384e26812SMatthias Ringwald
hal_stdin_setup(void (* handler)(char c))32484e26812SMatthias Ringwald void hal_stdin_setup(void (*handler)(char c)){
32584e26812SMatthias Ringwald // set handler
32684e26812SMatthias Ringwald stdin_handler = handler;
32784e26812SMatthias Ringwald
32884e26812SMatthias Ringwald /* set input handler */
32984e26812SMatthias Ringwald uart_byte_request.callback = uart_rx_handler;
33084e26812SMatthias Ringwald uart_byte_request.data = stdin_buffer;
33184e26812SMatthias Ringwald uart_byte_request.len = sizeof(uint8_t);
33284e26812SMatthias Ringwald UART_ReadAsync(MXC_UART_GET_UART(CONSOLE_UART), &uart_byte_request);
33384e26812SMatthias Ringwald }
33484e26812SMatthias Ringwald
33584e26812SMatthias Ringwald #if 0
33684e26812SMatthias Ringwald
33784e26812SMatthias Ringwald #include "btstack_stdin.h"
33884e26812SMatthias Ringwald
33984e26812SMatthias Ringwald static btstack_data_source_t stdin_data_source;
34084e26812SMatthias Ringwald static void (*stdin_handler)(char c);
34184e26812SMatthias Ringwald
34284e26812SMatthias Ringwald static uart_req_t uart_byte_request;
34384e26812SMatthias Ringwald static volatile int stdin_character_received;
34484e26812SMatthias Ringwald static uint8_t stdin_buffer[1];
34584e26812SMatthias Ringwald
34684e26812SMatthias Ringwald static void stdin_rx_complete(void) {
34784e26812SMatthias Ringwald stdin_character_received = 1;
34884e26812SMatthias Ringwald }
34984e26812SMatthias Ringwald
35084e26812SMatthias Ringwald static void uart_rx_handler(uart_req_t *request, int error)
35184e26812SMatthias Ringwald {
35284e26812SMatthias Ringwald stdin_rx_complete();
35384e26812SMatthias Ringwald }
35484e26812SMatthias Ringwald
35584e26812SMatthias Ringwald static void stdin_process(struct btstack_data_source *ds, btstack_data_source_callback_type_t callback_type){
35684e26812SMatthias Ringwald if (!stdin_character_received) return;
35784e26812SMatthias Ringwald if (stdin_handler){
35884e26812SMatthias Ringwald (*stdin_handler)(stdin_buffer[0]);
35984e26812SMatthias Ringwald }
36084e26812SMatthias Ringwald stdin_character_received = 0;
36184e26812SMatthias Ringwald UART_ReadAsync(MXC_UART_GET_UART(CONSOLE_UART), &uart_byte_request);
36284e26812SMatthias Ringwald }
36384e26812SMatthias Ringwald
36484e26812SMatthias Ringwald static void btstack_stdin_handler(char c){
36584e26812SMatthias Ringwald stdin_character_received = 1;
366*d3384105SMatthias Ringwald btstack_run_loop_poll_data_sources_from_irq();
36784e26812SMatthias Ringwald printf("Received: %c\n", c);
36884e26812SMatthias Ringwald }
36984e26812SMatthias Ringwald
37084e26812SMatthias Ringwald void btstack_stdin_setup(void (*handler)(char c)){
37184e26812SMatthias Ringwald // set handler
37284e26812SMatthias Ringwald stdin_handler = handler;
37384e26812SMatthias Ringwald
37484e26812SMatthias Ringwald // set up polling data_source
37584e26812SMatthias Ringwald btstack_run_loop_set_data_source_handler(&stdin_data_source, &stdin_process);
37684e26812SMatthias Ringwald btstack_run_loop_enable_data_source_callbacks(&stdin_data_source, DATA_SOURCE_CALLBACK_POLL);
37784e26812SMatthias Ringwald btstack_run_loop_add_data_source(&stdin_data_source);
37884e26812SMatthias Ringwald
37984e26812SMatthias Ringwald /* set input handler */
38084e26812SMatthias Ringwald uart_byte_request.callback = uart_rx_handler;
38184e26812SMatthias Ringwald uart_byte_request.data = stdin_buffer;
38284e26812SMatthias Ringwald uart_byte_request.len = sizeof(uint8_t);
38384e26812SMatthias Ringwald UART_ReadAsync(MXC_UART_GET_UART(CONSOLE_UART), &uart_byte_request);
38484e26812SMatthias Ringwald }
38584e26812SMatthias Ringwald #endif
38684e26812SMatthias Ringwald
38717ae5bc4SMatthias Ringwald #include "hal_flash_bank_mxc.h"
3885efdcd7dSMatthias Ringwald #include "btstack_tlv.h"
38917ae5bc4SMatthias Ringwald #include "btstack_tlv_flash_bank.h"
3905efdcd7dSMatthias Ringwald #include "btstack_link_key_db_tlv.h"
3915efdcd7dSMatthias Ringwald #include "le_device_db_tlv.h"
3925efdcd7dSMatthias Ringwald
39317ae5bc4SMatthias Ringwald #define HAL_FLASH_BANK_SIZE 0x2000
39417ae5bc4SMatthias Ringwald #define HAL_FLASH_BANK_0_ADDR 0x1FC000
39517ae5bc4SMatthias Ringwald #define HAL_FLASH_BANK_1_ADDR 0x1FE000
3965efdcd7dSMatthias Ringwald
39717ae5bc4SMatthias Ringwald static hal_flash_bank_mxc_t hal_flash_bank_context;
39817ae5bc4SMatthias Ringwald static btstack_tlv_flash_bank_t btstack_tlv_flash_bank_context;
3995efdcd7dSMatthias Ringwald
40084e26812SMatthias Ringwald
40184e26812SMatthias Ringwald /******************************************************************************/
bluetooth_main(void)402c894dca1SMatthias Ringwald int bluetooth_main(void)
403c894dca1SMatthias Ringwald {
40424efff18SMatthias Ringwald LED_Off(LED_GREEN);
40524efff18SMatthias Ringwald LED_On(LED_RED);
40624efff18SMatthias Ringwald LED_Off(LED_BLUE);
40724efff18SMatthias Ringwald
408c894dca1SMatthias Ringwald bt_comm_init();
409c894dca1SMatthias Ringwald /* BT Stack Initialization */
410c894dca1SMatthias Ringwald btstack_memory_init();
411c894dca1SMatthias Ringwald btstack_run_loop_init(btstack_run_loop_embedded_get_instance());
412c894dca1SMatthias Ringwald
41384e26812SMatthias Ringwald // enable packet logger
414cccba290SMatthias Ringwald // hci_dump_init(hci_dump_embedded_stdout_get_instance());
41584e26812SMatthias Ringwald
416c894dca1SMatthias Ringwald /* Init HCI */
417c894dca1SMatthias Ringwald const hci_transport_t * transport = hci_transport_h4_instance(btstack_uart_block_embedded_instance());
418c894dca1SMatthias Ringwald hci_init(transport, &config);
419c894dca1SMatthias Ringwald hci_set_chipset(btstack_chipset_cc256x_instance());
4205efdcd7dSMatthias Ringwald
42117ae5bc4SMatthias Ringwald // setup TLV Flash Bank implementation
42217ae5bc4SMatthias Ringwald const hal_flash_bank_t * hal_flash_bank_impl = hal_flash_bank_mxc_init_instance(
42317ae5bc4SMatthias Ringwald &hal_flash_bank_context,
42417ae5bc4SMatthias Ringwald HAL_FLASH_BANK_SIZE,
42517ae5bc4SMatthias Ringwald HAL_FLASH_BANK_0_ADDR,
42617ae5bc4SMatthias Ringwald HAL_FLASH_BANK_1_ADDR);
42717ae5bc4SMatthias Ringwald const btstack_tlv_t * btstack_tlv_impl = btstack_tlv_flash_bank_init_instance(
42817ae5bc4SMatthias Ringwald &btstack_tlv_flash_bank_context,
42917ae5bc4SMatthias Ringwald hal_flash_bank_impl,
43017ae5bc4SMatthias Ringwald &hal_flash_bank_context);
4315efdcd7dSMatthias Ringwald
4325efdcd7dSMatthias Ringwald // setup Link Key DB using TLV
43317ae5bc4SMatthias Ringwald 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);
4345efdcd7dSMatthias Ringwald hci_set_link_key_db(btstack_link_key_db);
4355efdcd7dSMatthias Ringwald
4365efdcd7dSMatthias Ringwald // setup LE Device DB using TLV
43717ae5bc4SMatthias Ringwald le_device_db_tlv_configure(btstack_tlv_impl, &btstack_tlv_flash_bank_context);
4385efdcd7dSMatthias Ringwald
4395efdcd7dSMatthias Ringwald // go
440c894dca1SMatthias Ringwald btstack_main(0, (void *)NULL);
441c894dca1SMatthias Ringwald return 0;
442c894dca1SMatthias Ringwald }
443