xref: /btstack/port/max32630-fthr/src/btstack_port.c (revision c894dca1b72560abcf8bcddfb43ffd9285d8713c)
1*c894dca1SMatthias Ringwald /*******************************************************************************
2*c894dca1SMatthias Ringwald * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
3*c894dca1SMatthias Ringwald * Author: Ismail H. Kose <[email protected]>
4*c894dca1SMatthias Ringwald *
5*c894dca1SMatthias Ringwald * Permission is hereby granted, free of charge, to any person obtaining a
6*c894dca1SMatthias Ringwald * copy of this software and associated documentation files (the "Software"),
7*c894dca1SMatthias Ringwald * to deal in the Software without restriction, including without limitation
8*c894dca1SMatthias Ringwald * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9*c894dca1SMatthias Ringwald * and/or sell copies of the Software, and to permit persons to whom the
10*c894dca1SMatthias Ringwald * Software is furnished to do so, subject to the following conditions:
11*c894dca1SMatthias Ringwald *
12*c894dca1SMatthias Ringwald * The above copyright notice and this permission notice shall be included
13*c894dca1SMatthias Ringwald * in all copies or substantial portions of the Software.
14*c894dca1SMatthias Ringwald *
15*c894dca1SMatthias Ringwald * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16*c894dca1SMatthias Ringwald * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17*c894dca1SMatthias Ringwald * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18*c894dca1SMatthias Ringwald * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
19*c894dca1SMatthias Ringwald * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20*c894dca1SMatthias Ringwald * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21*c894dca1SMatthias Ringwald * OTHER DEALINGS IN THE SOFTWARE.
22*c894dca1SMatthias Ringwald *
23*c894dca1SMatthias Ringwald * Except as contained in this notice, the name of Maxim Integrated
24*c894dca1SMatthias Ringwald * Products, Inc. shall not be used except as stated in the Maxim Integrated
25*c894dca1SMatthias Ringwald * Products, Inc. Branding Policy.
26*c894dca1SMatthias Ringwald *
27*c894dca1SMatthias Ringwald * The mere transfer of this software does not imply any licenses
28*c894dca1SMatthias Ringwald * of trade secrets, proprietary technology, copyrights, patents,
29*c894dca1SMatthias Ringwald * trademarks, maskwork rights, or any other form of intellectual
30*c894dca1SMatthias Ringwald * property whatsoever. Maxim Integrated Products, Inc. retains all
31*c894dca1SMatthias Ringwald * ownership rights.
32*c894dca1SMatthias Ringwald *******************************************************************************
33*c894dca1SMatthias Ringwald */
34*c894dca1SMatthias Ringwald 
35*c894dca1SMatthias Ringwald #include "hal_tick.h"
36*c894dca1SMatthias Ringwald #include <stdio.h>
37*c894dca1SMatthias Ringwald #include <string.h>
38*c894dca1SMatthias Ringwald #include "lp.h"
39*c894dca1SMatthias Ringwald #include "uart.h"
40*c894dca1SMatthias Ringwald #include "board.h"
41*c894dca1SMatthias Ringwald #include "btstack_debug.h"
42*c894dca1SMatthias Ringwald 
43*c894dca1SMatthias Ringwald #include "btstack.h"
44*c894dca1SMatthias Ringwald #include "btstack_config.h"
45*c894dca1SMatthias Ringwald #include "btstack_run_loop_embedded.h"
46*c894dca1SMatthias Ringwald #include "btstack_chipset_cc256x.h"
47*c894dca1SMatthias Ringwald #include "btstack_port.h"
48*c894dca1SMatthias Ringwald 
49*c894dca1SMatthias Ringwald #define CC256X_UART_ID             0
50*c894dca1SMatthias Ringwald #define UART_RXFIFO_USABLE     (MXC_UART_FIFO_DEPTH-3)
51*c894dca1SMatthias Ringwald 
52*c894dca1SMatthias Ringwald static uint32_t baud_rate;
53*c894dca1SMatthias Ringwald 
54*c894dca1SMatthias Ringwald // rx state
55*c894dca1SMatthias Ringwald static int  bytes_to_read = 0;
56*c894dca1SMatthias Ringwald static uint8_t * rx_buffer_ptr = 0;
57*c894dca1SMatthias Ringwald 
58*c894dca1SMatthias Ringwald // tx state
59*c894dca1SMatthias Ringwald static int bytes_to_write = 0;
60*c894dca1SMatthias Ringwald static uint8_t * tx_buffer_ptr = 0;
61*c894dca1SMatthias Ringwald 
62*c894dca1SMatthias Ringwald const gpio_cfg_t PAN1326_SLOW_CLK = { PORT_1, PIN_7, GPIO_FUNC_GPIO,
63*c894dca1SMatthias Ringwald 		GPIO_PAD_NORMAL };
64*c894dca1SMatthias Ringwald const gpio_cfg_t PAN1326_nSHUTD = { PORT_1, PIN_6, GPIO_FUNC_GPIO,
65*c894dca1SMatthias Ringwald 		GPIO_PAD_NORMAL };
66*c894dca1SMatthias Ringwald const gpio_cfg_t PAN1326_HCIRTS = { PORT_0, PIN_3, GPIO_FUNC_GPIO,
67*c894dca1SMatthias Ringwald 		GPIO_PAD_NORMAL };
68*c894dca1SMatthias Ringwald 
69*c894dca1SMatthias Ringwald static void dummy_handler(void) {};
70*c894dca1SMatthias Ringwald static void (*rx_done_handler)(void) = dummy_handler;
71*c894dca1SMatthias Ringwald static void (*tx_done_handler)(void) = dummy_handler;
72*c894dca1SMatthias Ringwald 
73*c894dca1SMatthias Ringwald void hal_cpu_disable_irqs(void)
74*c894dca1SMatthias Ringwald {
75*c894dca1SMatthias Ringwald 	__disable_irq();
76*c894dca1SMatthias Ringwald }
77*c894dca1SMatthias Ringwald 
78*c894dca1SMatthias Ringwald void hal_cpu_enable_irqs(void)
79*c894dca1SMatthias Ringwald {
80*c894dca1SMatthias Ringwald 	__enable_irq();
81*c894dca1SMatthias Ringwald }
82*c894dca1SMatthias Ringwald void hal_cpu_enable_irqs_and_sleep(void)
83*c894dca1SMatthias Ringwald {
84*c894dca1SMatthias Ringwald 
85*c894dca1SMatthias Ringwald }
86*c894dca1SMatthias Ringwald 
87*c894dca1SMatthias Ringwald void hal_uart_dma_send_block(const uint8_t *buffer, uint16_t len)
88*c894dca1SMatthias Ringwald {
89*c894dca1SMatthias Ringwald 	tx_buffer_ptr = (uint8_t *)buffer;
90*c894dca1SMatthias Ringwald 	bytes_to_write = len;
91*c894dca1SMatthias Ringwald }
92*c894dca1SMatthias Ringwald 
93*c894dca1SMatthias Ringwald void hal_uart_dma_receive_block(uint8_t *buffer, uint16_t len)
94*c894dca1SMatthias Ringwald {
95*c894dca1SMatthias Ringwald 	rx_buffer_ptr = buffer;
96*c894dca1SMatthias Ringwald 	bytes_to_read = len;
97*c894dca1SMatthias Ringwald }
98*c894dca1SMatthias Ringwald 
99*c894dca1SMatthias Ringwald void hal_btstack_run_loop_execute_once(void)
100*c894dca1SMatthias Ringwald {
101*c894dca1SMatthias Ringwald 	int rx_avail;
102*c894dca1SMatthias Ringwald 	int num_rx_bytes;
103*c894dca1SMatthias Ringwald 	int tx_avail;
104*c894dca1SMatthias Ringwald 	int rx_bytes;
105*c894dca1SMatthias Ringwald 	int tx_bytes;
106*c894dca1SMatthias Ringwald 	int ret;
107*c894dca1SMatthias Ringwald 
108*c894dca1SMatthias Ringwald     while (bytes_to_read) {
109*c894dca1SMatthias Ringwald 		rx_avail = UART_NumReadAvail(MXC_UART_GET_UART(CC256X_UART_ID));
110*c894dca1SMatthias Ringwald 		if (!rx_avail)
111*c894dca1SMatthias Ringwald 			break;
112*c894dca1SMatthias Ringwald 
113*c894dca1SMatthias Ringwald 		if (bytes_to_read > rx_avail)
114*c894dca1SMatthias Ringwald 			num_rx_bytes = rx_avail;
115*c894dca1SMatthias Ringwald 		else
116*c894dca1SMatthias Ringwald 			num_rx_bytes = bytes_to_read;
117*c894dca1SMatthias Ringwald 
118*c894dca1SMatthias Ringwald 		ret = UART_Read(MXC_UART_GET_UART(CC256X_UART_ID), rx_buffer_ptr, num_rx_bytes, &rx_bytes);
119*c894dca1SMatthias Ringwald 		if (ret < 0)
120*c894dca1SMatthias Ringwald 			break;
121*c894dca1SMatthias Ringwald 
122*c894dca1SMatthias Ringwald 		rx_buffer_ptr += rx_bytes;
123*c894dca1SMatthias Ringwald         bytes_to_read -= rx_bytes;
124*c894dca1SMatthias Ringwald 
125*c894dca1SMatthias Ringwald 		 if (bytes_to_read < 0) {
126*c894dca1SMatthias Ringwald 			bytes_to_read = 0;
127*c894dca1SMatthias Ringwald 		}
128*c894dca1SMatthias Ringwald 
129*c894dca1SMatthias Ringwald          if (bytes_to_read == 0){
130*c894dca1SMatthias Ringwald              (*rx_done_handler)();
131*c894dca1SMatthias Ringwald          }
132*c894dca1SMatthias Ringwald      }
133*c894dca1SMatthias Ringwald 
134*c894dca1SMatthias Ringwald      while (bytes_to_write) {
135*c894dca1SMatthias Ringwald 		tx_avail = UART_NumWriteAvail(MXC_UART_GET_UART(CC256X_UART_ID));
136*c894dca1SMatthias Ringwald 		if (!tx_avail)
137*c894dca1SMatthias Ringwald 			break;
138*c894dca1SMatthias Ringwald 
139*c894dca1SMatthias Ringwald 		if (bytes_to_write > tx_avail)
140*c894dca1SMatthias Ringwald 			tx_bytes = tx_avail;
141*c894dca1SMatthias Ringwald 		else
142*c894dca1SMatthias Ringwald 			tx_bytes = bytes_to_write;
143*c894dca1SMatthias Ringwald 
144*c894dca1SMatthias Ringwald 		ret = UART_Write(MXC_UART_GET_UART(CC256X_UART_ID), tx_buffer_ptr, tx_bytes);
145*c894dca1SMatthias Ringwald 		if (ret < 0)
146*c894dca1SMatthias Ringwald 			break;
147*c894dca1SMatthias Ringwald 		bytes_to_write -= tx_bytes;
148*c894dca1SMatthias Ringwald 		tx_buffer_ptr += tx_bytes;
149*c894dca1SMatthias Ringwald 		if (bytes_to_write < 0) {
150*c894dca1SMatthias Ringwald 			bytes_to_write = 0;
151*c894dca1SMatthias Ringwald 		}
152*c894dca1SMatthias Ringwald 
153*c894dca1SMatthias Ringwald         if (bytes_to_write == 0){
154*c894dca1SMatthias Ringwald              (*tx_done_handler)();
155*c894dca1SMatthias Ringwald         }
156*c894dca1SMatthias Ringwald      }
157*c894dca1SMatthias Ringwald 
158*c894dca1SMatthias Ringwald 	btstack_run_loop_embedded_execute_once();
159*c894dca1SMatthias Ringwald }
160*c894dca1SMatthias Ringwald 
161*c894dca1SMatthias Ringwald void hal_uart_init(void)
162*c894dca1SMatthias Ringwald {
163*c894dca1SMatthias Ringwald 	int error = 0;
164*c894dca1SMatthias Ringwald 	uart_cfg_t cfg;
165*c894dca1SMatthias Ringwald 
166*c894dca1SMatthias Ringwald 	cfg.parity = UART_PARITY_DISABLE;
167*c894dca1SMatthias Ringwald 	cfg.size = UART_DATA_SIZE_8_BITS;
168*c894dca1SMatthias Ringwald 	cfg.extra_stop = 0;
169*c894dca1SMatthias Ringwald 	cfg.cts = 1;
170*c894dca1SMatthias Ringwald 	cfg.rts = 1;
171*c894dca1SMatthias Ringwald 
172*c894dca1SMatthias Ringwald 	cfg.baud = baud_rate;
173*c894dca1SMatthias Ringwald 
174*c894dca1SMatthias Ringwald 	sys_cfg_uart_t sys_cfg;
175*c894dca1SMatthias Ringwald 	sys_cfg.clk_scale = CLKMAN_SCALE_AUTO;
176*c894dca1SMatthias Ringwald 
177*c894dca1SMatthias Ringwald 	sys_cfg.io_cfg = (ioman_cfg_t )IOMAN_UART(0,
178*c894dca1SMatthias Ringwald 			IOMAN_MAP_B, // io_map
179*c894dca1SMatthias Ringwald 			IOMAN_MAP_B, // cts_map
180*c894dca1SMatthias Ringwald 			IOMAN_MAP_B, // rts_map
181*c894dca1SMatthias Ringwald 			1, // io_en
182*c894dca1SMatthias Ringwald 			1, // cts_en
183*c894dca1SMatthias Ringwald 			1); //rts_en
184*c894dca1SMatthias Ringwald 
185*c894dca1SMatthias Ringwald 	if ((error = UART_Init(MXC_UART_GET_UART(CC256X_UART_ID), &cfg, &sys_cfg)) != E_NO_ERROR) {
186*c894dca1SMatthias Ringwald 		printf("Error initializing UART %d\n", error);
187*c894dca1SMatthias Ringwald 		while (1);
188*c894dca1SMatthias Ringwald 	} else {
189*c894dca1SMatthias Ringwald 		printf("BTSTACK UART Initialized\n");
190*c894dca1SMatthias Ringwald 	}
191*c894dca1SMatthias Ringwald 
192*c894dca1SMatthias Ringwald 	MXC_UART_GET_UART(CC256X_UART_ID)->ctrl |= MXC_F_UART_CTRL_CTS_POLARITY | MXC_F_UART_CTRL_RTS_POLARITY;
193*c894dca1SMatthias Ringwald 	MXC_UART_GET_UART(CC256X_UART_ID)->ctrl &= ~((MXC_UART_FIFO_DEPTH - 4) << (MXC_F_UART_CTRL_RTS_LEVEL_POS));
194*c894dca1SMatthias Ringwald 	MXC_UART_GET_UART(CC256X_UART_ID)->ctrl |= ((UART_RXFIFO_USABLE) << MXC_F_UART_CTRL_RTS_LEVEL_POS);
195*c894dca1SMatthias Ringwald }
196*c894dca1SMatthias Ringwald 
197*c894dca1SMatthias Ringwald int hal_uart_dma_set_baud(uint32_t baud){
198*c894dca1SMatthias Ringwald 	baud_rate = baud;
199*c894dca1SMatthias Ringwald 	printf("BAUD RATE IS = %d \n", baud);
200*c894dca1SMatthias Ringwald 	hal_uart_init();
201*c894dca1SMatthias Ringwald 	return baud_rate;
202*c894dca1SMatthias Ringwald }
203*c894dca1SMatthias Ringwald 
204*c894dca1SMatthias Ringwald void hal_uart_dma_init(void){
205*c894dca1SMatthias Ringwald 	bytes_to_write = 0;
206*c894dca1SMatthias Ringwald 	bytes_to_read = 0;
207*c894dca1SMatthias Ringwald 	hal_uart_dma_set_baud(115200);
208*c894dca1SMatthias Ringwald }
209*c894dca1SMatthias Ringwald 
210*c894dca1SMatthias Ringwald void hal_uart_dma_set_block_received( void (*block_handler)(void)){
211*c894dca1SMatthias Ringwald 	rx_done_handler = block_handler;
212*c894dca1SMatthias Ringwald }
213*c894dca1SMatthias Ringwald 
214*c894dca1SMatthias Ringwald void hal_uart_dma_set_block_sent( void (*block_handler)(void)){
215*c894dca1SMatthias Ringwald 
216*c894dca1SMatthias Ringwald 	tx_done_handler = block_handler;
217*c894dca1SMatthias Ringwald }
218*c894dca1SMatthias Ringwald 
219*c894dca1SMatthias Ringwald void hal_uart_dma_set_csr_irq_handler( void (*csr_irq_handler)(void)){
220*c894dca1SMatthias Ringwald 
221*c894dca1SMatthias Ringwald }
222*c894dca1SMatthias Ringwald 
223*c894dca1SMatthias Ringwald void hal_uart_dma_set_sleep(uint8_t sleep){
224*c894dca1SMatthias Ringwald 
225*c894dca1SMatthias Ringwald }
226*c894dca1SMatthias Ringwald 
227*c894dca1SMatthias Ringwald void init_slow_clock(void)
228*c894dca1SMatthias Ringwald {
229*c894dca1SMatthias Ringwald 	MXC_PWRSEQ->reg0 &= ~(MXC_F_PWRSEQ_REG0_PWR_RTCEN_RUN | MXC_F_PWRSEQ_REG0_PWR_RTCEN_SLP);
230*c894dca1SMatthias Ringwald 	MXC_PWRSEQ->reg4 &= ~MXC_F_PWRSEQ_REG4_PWR_PSEQ_32K_EN;
231*c894dca1SMatthias Ringwald 	MXC_PWRSEQ->reg0 |= MXC_F_PWRSEQ_REG0_PWR_RTCEN_RUN | MXC_F_PWRSEQ_REG0_PWR_RTCEN_SLP; // Enable RTC
232*c894dca1SMatthias Ringwald 	hal_delay_us(1);
233*c894dca1SMatthias Ringwald 	MXC_PWRSEQ->reg4 |= MXC_F_PWRSEQ_REG4_PWR_PSEQ_32K_EN; // Enable the RTC out of P1.7
234*c894dca1SMatthias Ringwald }
235*c894dca1SMatthias Ringwald 
236*c894dca1SMatthias Ringwald int bt_comm_init() {
237*c894dca1SMatthias Ringwald 	int error = 0;
238*c894dca1SMatthias Ringwald 	int cnt = 0;
239*c894dca1SMatthias Ringwald 
240*c894dca1SMatthias Ringwald 	hal_tick_init();
241*c894dca1SMatthias Ringwald 	hal_delay_us(1);
242*c894dca1SMatthias Ringwald 	if ((error = GPIO_Config(&PAN1326_HCIRTS)) != E_NO_ERROR) {
243*c894dca1SMatthias Ringwald 		printf("Error setting PAN1326_HCIRTS %d\n", error);
244*c894dca1SMatthias Ringwald 	}
245*c894dca1SMatthias Ringwald 	GPIO_OutSet(&PAN1326_HCIRTS);
246*c894dca1SMatthias Ringwald 	init_slow_clock();
247*c894dca1SMatthias Ringwald 	/*
248*c894dca1SMatthias Ringwald 	 * when enabling the P1.7 RTC output, P1.6 will be hardcoded to an input with 25k pullup enabled.
249*c894dca1SMatthias Ringwald 	 * There is an internal pullup, so when it is set as an input, it will float high.
250*c894dca1SMatthias Ringwald 	 * The PAN1326B data sheet says the NSHUTD pin is pulled down, but the input impedance is stated at 1Meg Ohm,
251*c894dca1SMatthias Ringwald 	 * The so the 25k pullup should be enough to reach the minimum 1.42V to enable the device.
252*c894dca1SMatthias Ringwald 	 * */
253*c894dca1SMatthias Ringwald 	while (GPIO_InGet(&PAN1326_HCIRTS)) {
254*c894dca1SMatthias Ringwald 		cnt++;
255*c894dca1SMatthias Ringwald 	}
256*c894dca1SMatthias Ringwald 
257*c894dca1SMatthias Ringwald 	printf("%s CC256X init completed. cnt: %d \n", __func__, cnt);
258*c894dca1SMatthias Ringwald 	return 0;
259*c894dca1SMatthias Ringwald }
260*c894dca1SMatthias Ringwald 
261*c894dca1SMatthias Ringwald static hci_transport_config_uart_t config = {
262*c894dca1SMatthias Ringwald 	    HCI_TRANSPORT_CONFIG_UART,
263*c894dca1SMatthias Ringwald 	    115200,
264*c894dca1SMatthias Ringwald 	    4000000,
265*c894dca1SMatthias Ringwald 	    1, // flow control
266*c894dca1SMatthias Ringwald 	    "max32630fthr",
267*c894dca1SMatthias Ringwald 	};
268*c894dca1SMatthias Ringwald 
269*c894dca1SMatthias Ringwald int bluetooth_main(void)
270*c894dca1SMatthias Ringwald {
271*c894dca1SMatthias Ringwald 	bt_comm_init();
272*c894dca1SMatthias Ringwald 	/* BT Stack Initialization */
273*c894dca1SMatthias Ringwald 	btstack_memory_init();
274*c894dca1SMatthias Ringwald 	btstack_run_loop_init(btstack_run_loop_embedded_get_instance());
275*c894dca1SMatthias Ringwald 
276*c894dca1SMatthias Ringwald 	/* Init HCI */
277*c894dca1SMatthias Ringwald 	const hci_transport_t * transport = hci_transport_h4_instance(btstack_uart_block_embedded_instance());
278*c894dca1SMatthias Ringwald 	const btstack_link_key_db_t *link_key_db = NULL;
279*c894dca1SMatthias Ringwald 
280*c894dca1SMatthias Ringwald 	hci_init(transport, &config);
281*c894dca1SMatthias Ringwald 	hci_set_link_key_db(link_key_db);
282*c894dca1SMatthias Ringwald 
283*c894dca1SMatthias Ringwald 	hci_set_chipset(btstack_chipset_cc256x_instance());
284*c894dca1SMatthias Ringwald 	btstack_main(0, (void *)NULL);
285*c894dca1SMatthias Ringwald 
286*c894dca1SMatthias Ringwald 	return 0;
287*c894dca1SMatthias Ringwald }
288