]> git.karo-electronics.de Git - oswald.git/blob - metawatch/mw_bt.c
d5c7d26bbd29821fa7ebff3adc7b6b3de8077699
[oswald.git] / metawatch / mw_bt.c
1 #include <msp430.h>
2 #include <msp430xgeneric.h>
3 #include <stdint.h>
4 #include <stdio.h>
5 #include <string.h>
6
7 #include "mw_main.h"
8
9 #include "mw_uart.h"
10 #include "mw_bt.h"
11 #include "bt_hci.h"
12 #include "bt_l2cap.h"
13
14 #include "bluetooth_init_cc256x.h"
15
16
17 static char bt_rx_buf[BT_RX_MAX_SIZE];
18 static unsigned char bt_rx_buf_wpos = 0;
19 static unsigned char bt_rx_buf_rpos = 0;
20 static uint8_t mw_bt_enabled = 0;
21
22 int mw_bt_get_rxbuf_len(void)
23 {
24         if (bt_rx_buf_rpos > bt_rx_buf_wpos)
25                 return (BT_RX_MAX_SIZE - bt_rx_buf_rpos) + bt_rx_buf_wpos;
26         else
27                 return bt_rx_buf_wpos - bt_rx_buf_rpos;
28 }
29
30 const unsigned char *mw_bt_get_rx_buf(unsigned char **rpos, unsigned char *len)
31 {
32         *rpos = &bt_rx_buf_rpos;
33
34         if (bt_rx_buf_rpos > bt_rx_buf_wpos)
35                 *len = (BT_RX_MAX_SIZE - bt_rx_buf_rpos) + bt_rx_buf_wpos;
36         else
37                 *len = bt_rx_buf_wpos - bt_rx_buf_rpos;
38
39         // if we reach high water mark raise RTS to stop more data
40         if (*len > (BT_RX_MAX_SIZE-(BT_RX_MAX_SIZE/10))) {
41                 debug_uart_tx("BT UART RTS\n");
42                 BT_IO_POUT |= BT_IO_RTS; // low == ready, high == !ready
43         } else {
44                 BT_IO_POUT &= ~BT_IO_RTS; // low == ready, high == !ready
45         }
46
47         return (unsigned char *)bt_rx_buf;
48 }
49
50 #pragma vector=USCI_A1_VECTOR
51 __interrupt void UCA1_ISR (void)
52 {
53         switch (UCA1IV) {
54                 case 2: // RXIFG
55                         /* clear IRQ flag */
56                         //UCA1IFG  &= ~UCRXIFG;
57                         /* wake up to handle the received char */
58                         if (UCA1STAT & UCRXERR) {
59                                 debug_uart_tx("BT UART RXERR: ");
60                                 if (UCA1STAT & UCOE)
61                                         debug_uart_tx("overrun\n");
62                                 if (UCA1STAT & UCPE)
63                                         debug_uart_tx("parity err\n");
64                                 if (UCA1STAT & UCFE)
65                                         debug_uart_tx("frm-err\n");
66                         }
67                         bt_rx_buf[bt_rx_buf_wpos++] = UCA1RXBUF;
68                         bt_rx_buf_wpos %= BT_RX_MAX_SIZE;
69                         LPM3_EXIT;
70                         _event_src |= BT_UART_RCV_EVENT;
71                         break;
72                 case 4: // TXIFG
73                         debug_uart_tx("BT UART TX IRQ - huh?\n");
74                         break;
75                 default:
76                         break;
77         };
78 }
79
80 void mw_init_bt_uart(const bt_uart_baud_t baud)
81 {
82         UCA1CTL1 = UCSWRST;
83
84         UCA1CTL1 |= UCSSEL__SMCLK;
85
86         switch (baud) {
87                 case BT_UART_BD115200:
88                 default:
89                         UCA1BR0 = 138;
90                         UCA1MCTL = UCBRS_7 + UCBRF_0;
91                 break;
92         };
93         UCA1STAT = 0;
94         // UCA1CTL0 = UCMODE_0; // UART mode
95         // UCA1CTL0 &= ~UC7BIT; // 8bit char
96         //UCA1CTL0 |= UCRXEIE;
97
98         UCA1CTL1 &= ~UCSWRST;
99         /* clear interrup flags */
100         UCA1IE = UCRXIE;
101         UCA1IFG = 0;
102 }
103
104 #if 0 // Does never finish, presumably trigger does not trigger, unknown :(
105 void mw_bt_uart_tx(const void *buf, const unsigned int len)
106 {
107         UCA1IE &= UCTXIE;
108
109         DMACTL0 = DMA0TSEL_21;
110
111         DMA0DA  = (unsigned int) &UCA1TXBUF;
112         DMA0SA  = (uint32_t) buf;
113         DMA0SZ  = len;
114
115         //DMA0CTL = 0x03F0;
116         DMA0CTL = DMADT_1 | DMASRCINCR_3 | DMASBDB | DMALEVEL | DMAIE;
117         UCA1IFG &= ~UCTXIFG;
118         DMA0CTL |= DMAEN;
119
120         while ((DMA0CTL & DMAIFG) == 0 && (DMA0CTL & DMAABORT) == 0)
121                 nop();
122 }
123 #else
124 void mw_bt_uart_tx(const void *buf, const unsigned int len)
125 {
126         unsigned int pos;
127         // char txstr[8];
128
129         pos = 0;
130         // debug_uart_tx("BT tx: ");
131         while (pos < len) {
132                 // wait for CTS to go low
133                 while ((BT_IO_PIN & BT_IO_CTS))
134                         nop();
135
136                 // do not start a transfer if UART is busy, e.g. rx-ing
137                 while (UCA1STAT & UCBUSY)
138                         nop();
139
140                 UCA1TXBUF = *(unsigned char *) (buf+pos);
141                 // debug_uart_tx_char(*(unsigned char *) (buf+pos));
142                 // snprintf(txstr, 8, "0x%02x ", *(unsigned char *) (buf+pos));
143                 // debug_uart_tx(txstr);
144                 pos++;
145                 while ((UCA1IFG & UCTXIFG) == 0)
146                         nop();
147         }
148         while (UCA1STAT & UCBUSY)
149                 nop();
150 }
151 #endif
152
153 static void load_cc256x_init_script(void)
154 {
155         uint32_t pos;
156         unsigned char *tptr;
157
158         pos = 0;
159         while (pos < cc256x_init_script_size) {
160                 if (_event_src != 0)
161                         handle_event();
162                 tptr = (unsigned char *)(cc256x_init_script + pos);
163                 mw_bt_uart_tx(tptr, 4 + tptr[3]);
164                 pos += 4 + tptr[3];
165                 // each init script part is one HCI command so wait for reply
166                 if (_event_src != 0)
167                         handle_event();
168         }
169 }
170
171 void mw_enable_bt(void)
172 {
173         int i;
174
175         /* make sure it resets */
176         BT_SHUTDOWN();
177
178         /* enable 32kHz ACLK output to BT module */
179         P11DIR |= BIT0;
180         P11SEL |= BIT0;
181
182         // wait for clock to stabilize
183         __delay_cycles(16000);
184
185         BT_IO_PDIR &= ~(BT_IO_CTS | BT_IO_PIN1 | BT_IO_PIN2 | BT_IO_CLKREQ);
186         BT_IO_PDIR |= BT_IO_RTS;
187         BT_IO_POUT &= ~(BT_IO_CTS | BT_IO_PIN1 | BT_IO_PIN2 | BT_IO_CLKREQ);
188         BT_IO_POUT &= ~BT_IO_RTS; // low == ready, high == !ready
189
190         /* setup UART pins */
191         BT_UART_PSEL |= BT_UART_TX_PIN | BT_UART_RX_PIN;
192         // P5OUT |= BT_UART_TX_PIN | BT_UART_RX_PIN;
193         // P5REN |= BT_UART_TX_PIN | BT_UART_RX_PIN;
194
195         mw_init_bt_uart(BT_UART_BD115200);
196
197         bt_rx_buf_wpos = 0;
198         bt_rx_buf_rpos = 0;
199
200         /* release BT reset pin */
201         BT_ENABLE();
202
203         for (i=0; i<1000; i++) {
204                 __delay_cycles(16000);
205                 if ((BT_IO_PIN & BT_IO_CTS) == 0) // when CTS goes low module is ready
206                         break;
207         }
208         if (i>=1000) {
209                 debug_uart_tx("Timeout waiting for CC256x to lower CTS\n");
210         } else {
211                 debug_uart_tx("CC256x CTS low - uploading init\n");
212                 for (i=0; i<100; i++) {
213                         __delay_cycles(16000); // give it some more before anyone sends data
214                 }
215                 load_cc256x_init_script();
216                 debug_uart_tx("init uploaded\n");
217         }
218
219         P1IE &= ~BT_IO_CTS;
220         P1IES &= ~BT_IO_CTS;
221
222         bt_hci_init();
223         init_l2cap();
224
225         mw_bt_enabled = 1;
226 }
227
228 void mw_disable_bt(void)
229 {
230         mw_bt_enabled = 0;
231
232         /* disable UART RX interrupt */
233         UCA1IE &= ~UCRXIE;
234
235         /* disable UART pins */
236         BT_UART_PSEL &= ~(BT_UART_TX_PIN | BT_UART_RX_PIN);
237
238         /* set BT reset pin */
239         BT_SHUTDOWN();
240
241         /* disable 32kHz ACLK output to BT module */
242         P11DIR &= ~BIT0;
243         P11SEL &= ~BIT0;
244
245         /* make all I/O Pins inputs so we do not drive against a "deaf" module */
246         BT_IO_PDIR &= ~(BT_IO_RTS | BT_IO_CTS | BT_IO_PIN1 | BT_IO_PIN2 | BT_IO_CLKREQ);
247         BT_IO_POUT &= ~(BT_IO_RTS | BT_IO_CTS | BT_IO_PIN1 | BT_IO_PIN2 | BT_IO_CLKREQ);
248 }
249
250 uint8_t mw_bt_is_enabled(void)
251 {
252         return mw_bt_enabled;
253 }
254