xref: /btstack/platform/embedded/btstack_uart_block_embedded.c (revision 084ad01c1a9e12f0da3d2691f48405bf30ddf87a)
1 /*
2  * Copyright (C) 2016 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 /*
39  *  btstack_uart_block_embedded.c
40  *
41  *  Common code to access UART via asynchronous block read/write commands on top of hal_uart_dma.h
42  *
43  */
44 
45 #include "btstack_debug.h"
46 #include "btstack_uart_block.h"
47 #include "btstack_run_loop_embedded.h"
48 #include "hal_uart_dma.h"
49 
50 // uart config
51 static const btstack_uart_config_t * uart_config;
52 
53 // data source for integration with BTstack Runloop
54 static btstack_data_source_t transport_data_source;
55 
56 static int send_complete;
57 static int receive_complete;
58 
59 // callbacks
60 static void (*block_sent)(void);
61 static void (*block_received)(void);
62 
63 
64 static void btstack_uart_block_received(void){
65     receive_complete = 1;
66     btstack_run_loop_embedded_trigger();
67 }
68 
69 static void btstack_uart_block_sent(void){
70     send_complete = 1;
71     btstack_run_loop_embedded_trigger();
72 }
73 
74 static int btstack_uart_embedded_init(const btstack_uart_config_t * config){
75     uart_config = config;
76     hal_uart_dma_set_block_received(&btstack_uart_block_received);
77     hal_uart_dma_set_block_sent(&btstack_uart_block_sent);
78     return 0;
79 }
80 
81 static void btstack_uart_embedded_process(btstack_data_source_t *ds, btstack_data_source_callback_type_t callback_type) {
82     switch (callback_type){
83         case DATA_SOURCE_CALLBACK_POLL:
84             if (send_complete){
85                 send_complete = 0;
86                 block_sent();
87             }
88             if (block_received){
89                 block_received = 0;
90                 block_received();
91             }
92             break;
93         default:
94             break;
95     }
96 }
97 
98 static int btstack_uart_embedded_open(void){
99     hal_uart_dma_init();
100     hal_uart_dma_set_baud(uart_config->baudrate);
101 
102     // set up polling data_source
103     btstack_run_loop_set_data_source_handler(&transport_data_source, &btstack_uart_embedded_process);
104     btstack_run_loop_enable_data_source_callbacks(&transport_data_source, DATA_SOURCE_CALLBACK_POLL);
105     btstack_run_loop_add_data_source(&transport_data_source);
106     return 0;
107 }
108 
109 static int btstack_uart_embedded_close(void){
110     return 0;
111 }
112 
113 static void btstack_uart_embedded_set_block_received( void (*block_handler)(void)){
114     block_received = block_handler;
115 }
116 
117 static void btstack_uart_embedded_set_block_sent( void (*block_handler)(void)){
118     block_sent = block_handler;
119 }
120 
121 static int btstack_uart_embedded_set_parity(int parity){
122     return 0;
123 }
124 
125 static void btstack_uart_embedded_send_block(const uint8_t *data, uint16_t size){
126     hal_uart_dma_send_block(data, size);
127 }
128 
129 static void btstack_uart_embedded_receive_block(uint8_t *buffer, uint16_t len){
130     hal_uart_dma_receive_block(buffer, len);
131 }
132 
133 // static void btstack_uart_embedded_set_sleep(uint8_t sleep){
134 // }
135 
136 // static void btstack_uart_embedded_set_csr_irq_handler( void (*csr_irq_handler)(void)){
137 // }
138 
139 static const btstack_uart_block_t btstack_uart_embedded = {
140     /* int  (*init)(hci_transport_config_uart_t * config); */         &btstack_uart_embedded_init,
141     /* int  (*open)(void); */                                         &btstack_uart_embedded_open,
142     /* int  (*close)(void); */                                        &btstack_uart_embedded_close,
143     /* void (*set_block_received)(void (*handler)(void)); */          &btstack_uart_embedded_set_block_received,
144     /* void (*set_block_sent)(void (*handler)(void)); */              &btstack_uart_embedded_set_block_sent,
145     /* int  (*set_baudrate)(uint32_t baudrate); */                    &hal_uart_dma_set_baud,
146     /* int  (*set_parity)(int parity); */                             &btstack_uart_embedded_set_parity,
147     /* void (*receive_block)(uint8_t *buffer, uint16_t len); */       &btstack_uart_embedded_receive_block,
148     /* void (*send_block)(const uint8_t *buffer, uint16_t length); */ &btstack_uart_embedded_send_block
149 };
150 
151 const btstack_uart_block_t * btstack_uart_block_embedded_instance(void){
152 	return &btstack_uart_embedded;
153 }