xref: /btstack/port/archive/msp-exp430f5438-cc2564b/src/main.c (revision b29e92f97ffd81bc8a1057634c8d1552fc963a6a)
1 /*
2  * Copyright (C) 2014 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 BLUEKITCHEN
24  * GMBH 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 //
40 // spp_counter demo - it provides an SPP and sends a counter every second
41 //
42 // it doesn't use the LCD to get down to a minimal memory footprint
43 //
44 // *****************************************************************************
45 
46 #include <stdint.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 
51 #include <msp430x54x.h>
52 
53 #include "btstack_chipset_cc256x.h"
54 #include "btstack_config.h"
55 #include "btstack_event.h"
56 #include "btstack_memory.h"
57 #include "btstack_run_loop.h"
58 #include "btstack_run_loop_embedded.h"
59 #include "classic/btstack_link_key_db.h"
60 #include "bluetooth_company_id.h"
61 #include "hal_board.h"
62 #include "hal_compat.h"
63 #include "hal_usb.h"
64 #include "hci.h"
65 
66 static void hw_setup(void){
67     // stop watchdog timer
68     WDTCTL = WDTPW + WDTHOLD;
69 
70     //Initialize clock and peripherals
71     halBoardInit();
72     halBoardStartXT1();
73     halBoardSetSystemClock(SYSCLK_16MHZ);
74 
75     // init debug UART
76     halUsbInit();
77 
78     // init LEDs
79     LED1_OUT |= LED1_PIN;
80     LED1_DIR |= LED1_PIN;
81     LED2_OUT |= LED2_PIN;
82     LED2_DIR |= LED2_PIN;
83 
84     // ready - enable irq used in h4 task
85     __enable_interrupt();
86 }
87 
88 static hci_transport_config_uart_t config = {
89     HCI_TRANSPORT_CONFIG_UART,
90     115200,
91     1000000,  // main baudrate
92     1,        // flow control
93     NULL,
94 };
95 
96 static btstack_packet_callback_registration_t hci_event_callback_registration;
97 
98 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
99     if (packet_type != HCI_EVENT_PACKET) return;
100     switch(hci_event_packet_get_type(packet)){
101         case BTSTACK_EVENT_STATE:
102             if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) return;
103             printf("BTstack up and running.\n");
104             break;
105         case HCI_EVENT_COMMAND_COMPLETE:
106             if (hci_event_command_complete_get_command_opcode(packet) == HCI_OPCODE_HCI_READ_LOCAL_VERSION_INFORMATION){
107                 uint16_t manufacturer   = little_endian_read_16(packet, 10);
108                 uint16_t lmp_subversion = little_endian_read_16(packet, 12);
109                 // assert manufacturer is TI
110                 if (manufacturer != BLUETOOTH_COMPANY_ID_TEXAS_INSTRUMENTS_INC){
111                     printf("ERROR: Expected Bluetooth Chipset from TI but got manufacturer 0x%04x\n", manufacturer);
112                     break;
113                 }
114                 // assert correct init script is used based on expected lmp_subversion
115                 if (lmp_subversion != btstack_chipset_cc256x_lmp_subversion()){
116                     printf("Error: LMP Subversion does not match initscript! ");
117                     printf("Your initscripts is for %s chipset\n", btstack_chipset_cc256x_lmp_subversion() < lmp_subversion ? "an older" : "a newer");
118                     printf("Please update Makefile to include the appropriate bluetooth_init_cc256???.c file\n");
119                     break;
120                 }
121             }
122             break;
123         default:
124             break;
125     }
126 }
127 
128 static void btstack_setup(void){
129     /// GET STARTED with BTstack ///
130     btstack_memory_init();
131     btstack_run_loop_init(btstack_run_loop_embedded_get_instance());
132 
133     // init HCI
134     hci_init(hci_transport_h4_instance(btstack_uart_block_embedded_instance()), &config);
135     hci_set_link_key_db(btstack_link_key_db_memory_instance());
136     hci_set_chipset(btstack_chipset_cc256x_instance());
137 
138     // inform about BTstack state
139     hci_event_callback_registration.callback = &packet_handler;
140     hci_add_event_handler(&hci_event_callback_registration);
141 }
142 
143 int btstack_main(int argc, const char * argv[]);
144 
145 // main
146 int main(void){
147 
148     hw_setup();
149 
150     btstack_setup();
151     btstack_main(0, NULL);
152 
153     btstack_run_loop_execute();
154 
155     return 0;
156 }
157 
158