xref: /btstack/platform/chibios/hal_uart_dma_chibios.c (revision 065fc9a3d9ed4f0d223eff283968bd32564011a2)
1*065fc9a3SMatthias Ringwald /*
2*065fc9a3SMatthias Ringwald  * Copyright (C) 2022 BlueKitchen GmbH
3*065fc9a3SMatthias Ringwald  *
4*065fc9a3SMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
5*065fc9a3SMatthias Ringwald  * modification, are permitted provided that the following conditions
6*065fc9a3SMatthias Ringwald  * are met:
7*065fc9a3SMatthias Ringwald  *
8*065fc9a3SMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
9*065fc9a3SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
10*065fc9a3SMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
11*065fc9a3SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
12*065fc9a3SMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
13*065fc9a3SMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
14*065fc9a3SMatthias Ringwald  *    contributors may be used to endorse or promote products derived
15*065fc9a3SMatthias Ringwald  *    from this software without specific prior written permission.
16*065fc9a3SMatthias Ringwald  *
17*065fc9a3SMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
18*065fc9a3SMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19*065fc9a3SMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20*065fc9a3SMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
21*065fc9a3SMatthias Ringwald  * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22*065fc9a3SMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23*065fc9a3SMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
24*065fc9a3SMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25*065fc9a3SMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26*065fc9a3SMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27*065fc9a3SMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28*065fc9a3SMatthias Ringwald  * SUCH DAMAGE.
29*065fc9a3SMatthias Ringwald  *
30*065fc9a3SMatthias Ringwald  */
31*065fc9a3SMatthias Ringwald 
32*065fc9a3SMatthias Ringwald /*
33*065fc9a3SMatthias Ringwald  *  hal_uart_dma_chibios.c
34*065fc9a3SMatthias Ringwald  *
35*065fc9a3SMatthias Ringwald  *  hal_uart_dma implementation on top of ChibiOS/HAL
36*065fc9a3SMatthias Ringwald  */
37*065fc9a3SMatthias Ringwald 
38*065fc9a3SMatthias Ringwald #define BTSTACK_FILE__ "hal_uart_dma_chibios.c"
39*065fc9a3SMatthias Ringwald 
40*065fc9a3SMatthias Ringwald 
41*065fc9a3SMatthias Ringwald #include "btstack_config.h"
42*065fc9a3SMatthias Ringwald 
43*065fc9a3SMatthias Ringwald #ifdef HAL_CONTROLLER_UART
44*065fc9a3SMatthias Ringwald 
45*065fc9a3SMatthias Ringwald #include "btstack_debug.h"
46*065fc9a3SMatthias Ringwald #include "btstack_util.h"
47*065fc9a3SMatthias Ringwald 
48*065fc9a3SMatthias Ringwald #include "hal_uart_dma.h"
49*065fc9a3SMatthias Ringwald #include "hal.h"
50*065fc9a3SMatthias Ringwald 
51*065fc9a3SMatthias Ringwald #include <inttypes.h>
52*065fc9a3SMatthias Ringwald #include <string.h> // memcpy
53*065fc9a3SMatthias Ringwald 
54*065fc9a3SMatthias Ringwald 
55*065fc9a3SMatthias Ringwald static void dummy_handler(void);
56*065fc9a3SMatthias Ringwald static void tx_complete(UARTDriver *uartp);
57*065fc9a3SMatthias Ringwald static void rx_complete(UARTDriver *uartp);
58*065fc9a3SMatthias Ringwald 
59*065fc9a3SMatthias Ringwald // handlers
60*065fc9a3SMatthias Ringwald static void (*rx_done_handler)(void) = &dummy_handler;
61*065fc9a3SMatthias Ringwald static void (*tx_done_handler)(void) = &dummy_handler;
62*065fc9a3SMatthias Ringwald 
63*065fc9a3SMatthias Ringwald static UARTConfig uartConf = {
64*065fc9a3SMatthias Ringwald     NULL,               /* UART transmission buffer callback.           */
65*065fc9a3SMatthias Ringwald     &tx_complete,       /* UART physical end of transmission callback.  */
66*065fc9a3SMatthias Ringwald     &rx_complete,       /* UART Receiver receiver filled callback.      */
67*065fc9a3SMatthias Ringwald     NULL,               /* UART caracter received callback.             */
68*065fc9a3SMatthias Ringwald     NULL,               /* UART received error callback.                */
69*065fc9a3SMatthias Ringwald     NULL,               /* Receiver timeout callback */
70*065fc9a3SMatthias Ringwald     115200,             /* UART baudrate.                               */
71*065fc9a3SMatthias Ringwald     0,                  /* CR 1 */
72*065fc9a3SMatthias Ringwald     0,                  /* CR 2 */
73*065fc9a3SMatthias Ringwald     (1<<8) | (1<<9) ,   /* CR 3 - enable RTS/CTS */
74*065fc9a3SMatthias Ringwald };
75*065fc9a3SMatthias Ringwald 
dummy_handler(void)76*065fc9a3SMatthias Ringwald static void dummy_handler(void){};
77*065fc9a3SMatthias Ringwald 
78*065fc9a3SMatthias Ringwald // reset Bluetooth using n_shutdown
bluetooth_power_cycle(void)79*065fc9a3SMatthias Ringwald static void bluetooth_power_cycle(void){
80*065fc9a3SMatthias Ringwald #ifdef HAL_CONTROLLER_RESET_PIN
81*065fc9a3SMatthias Ringwald     log_info("Bluetooth power cycle");
82*065fc9a3SMatthias Ringwald     palClearPad( HAL_CONTROLLER_RESET_PORT, HAL_CONTROLLER_RESET_PIN);
83*065fc9a3SMatthias Ringwald     chThdSleepMilliseconds( 250 );
84*065fc9a3SMatthias Ringwald     palSetPad( HAL_CONTROLLER_RESET_PORT, HAL_CONTROLLER_RESET_PIN);
85*065fc9a3SMatthias Ringwald     chThdSleepMilliseconds( 500 );
86*065fc9a3SMatthias Ringwald #else
87*065fc9a3SMatthias Ringwald     log_info("Bluetooth power cycle skipped, HAL_CONTROLLER_RESET_PIN not defined");
88*065fc9a3SMatthias Ringwald #endif
89*065fc9a3SMatthias Ringwald }
90*065fc9a3SMatthias Ringwald 
rx_complete(UARTDriver * uartp)91*065fc9a3SMatthias Ringwald static void rx_complete(UARTDriver *uartp){
92*065fc9a3SMatthias Ringwald     if (uartp == &HAL_CONTROLLER_UART){
93*065fc9a3SMatthias Ringwald         (*rx_done_handler)();
94*065fc9a3SMatthias Ringwald     }
95*065fc9a3SMatthias Ringwald }
96*065fc9a3SMatthias Ringwald 
tx_complete(UARTDriver * uartp)97*065fc9a3SMatthias Ringwald static void tx_complete(UARTDriver *uartp){
98*065fc9a3SMatthias Ringwald     UNUSED(uartp);
99*065fc9a3SMatthias Ringwald     if (uartp == &HAL_CONTROLLER_UART){
100*065fc9a3SMatthias Ringwald         (*tx_done_handler)();
101*065fc9a3SMatthias Ringwald     }
102*065fc9a3SMatthias Ringwald }
103*065fc9a3SMatthias Ringwald 
hal_uart_dma_init(void)104*065fc9a3SMatthias Ringwald void hal_uart_dma_init(void){
105*065fc9a3SMatthias Ringwald     bluetooth_power_cycle();
106*065fc9a3SMatthias Ringwald     uartStart(&HAL_CONTROLLER_UART, &uartConf);
107*065fc9a3SMatthias Ringwald }
108*065fc9a3SMatthias Ringwald 
hal_uart_dma_set_block_received(void (* the_block_handler)(void))109*065fc9a3SMatthias Ringwald void hal_uart_dma_set_block_received( void (*the_block_handler)(void)){
110*065fc9a3SMatthias Ringwald     rx_done_handler = the_block_handler;
111*065fc9a3SMatthias Ringwald }
112*065fc9a3SMatthias Ringwald 
hal_uart_dma_set_block_sent(void (* the_block_handler)(void))113*065fc9a3SMatthias Ringwald void hal_uart_dma_set_block_sent( void (*the_block_handler)(void)){
114*065fc9a3SMatthias Ringwald     tx_done_handler = the_block_handler;
115*065fc9a3SMatthias Ringwald }
116*065fc9a3SMatthias Ringwald 
hal_uart_dma_set_sleep(uint8_t sleep)117*065fc9a3SMatthias Ringwald void hal_uart_dma_set_sleep(uint8_t sleep){
118*065fc9a3SMatthias Ringwald     UNUSED(sleep);
119*065fc9a3SMatthias Ringwald }
120*065fc9a3SMatthias Ringwald 
hal_uart_dma_set_csr_irq_handler(void (* the_irq_handler)(void))121*065fc9a3SMatthias Ringwald void hal_uart_dma_set_csr_irq_handler( void (*the_irq_handler)(void)){
122*065fc9a3SMatthias Ringwald     UNUSED(the_irq_handler);
123*065fc9a3SMatthias Ringwald }
124*065fc9a3SMatthias Ringwald 
hal_uart_dma_set_baud(uint32_t baud)125*065fc9a3SMatthias Ringwald int hal_uart_dma_set_baud(uint32_t baud){
126*065fc9a3SMatthias Ringwald     // driver supports 'hot restart'
127*065fc9a3SMatthias Ringwald     uartConf.speed = baud;
128*065fc9a3SMatthias Ringwald     uartStart(&HAL_CONTROLLER_UART, &uartConf);
129*065fc9a3SMatthias Ringwald     log_info("UART baud %" PRIu32, baud);
130*065fc9a3SMatthias Ringwald     return 0;
131*065fc9a3SMatthias Ringwald }
132*065fc9a3SMatthias Ringwald 
hal_uart_dma_send_block(const uint8_t * data,uint16_t size)133*065fc9a3SMatthias Ringwald void hal_uart_dma_send_block(const uint8_t *data, uint16_t size){
134*065fc9a3SMatthias Ringwald     uartStartSend(&HAL_CONTROLLER_UART, size, data);
135*065fc9a3SMatthias Ringwald }
136*065fc9a3SMatthias Ringwald 
hal_uart_dma_receive_block(uint8_t * data,uint16_t size)137*065fc9a3SMatthias Ringwald void hal_uart_dma_receive_block(uint8_t *data, uint16_t size){
138*065fc9a3SMatthias Ringwald     uartStartReceive(&HAL_CONTROLLER_UART, size, data);
139*065fc9a3SMatthias Ringwald }
140*065fc9a3SMatthias Ringwald 
141*065fc9a3SMatthias Ringwald #endif
142