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