xref: /btstack/port/archive/ez430-rf2560/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 a SPP and sends a counter every second
41 //
42 // it doesn't use the LCD to get down to a minimal memory footpring
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 "bluetooth_company_id.h"
60 #include "classic/btstack_link_key_db.h"
61 #include "hal_board.h"
62 #include "hal_compat.h"
63 #include "hal_usb.h"
64 #include "hci.h"
65 #include "hci_cmd.h"
66 
67 int btstack_main(int argc, const char * argv[]);
68 
69 static hci_transport_config_uart_t config = {
70     HCI_TRANSPORT_CONFIG_UART,
71     115200,
72     1000000,  // main baudrate
73     1,        // flow control
74     NULL,
75 };
76 
77 static btstack_packet_callback_registration_t hci_event_callback_registration;
78 
79 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
80     if (packet_type != HCI_EVENT_PACKET) return;
81     switch(hci_event_packet_get_type(packet)){
82         case BTSTACK_EVENT_STATE:
83             if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) return;
84             printf("BTstack up and running.\n");
85             break;
86         case HCI_EVENT_COMMAND_COMPLETE:
87             if (hci_event_command_complete_get_command_opcode(packet) == HCI_OPCODE_HCI_READ_LOCAL_VERSION_INFORMATION){
88                 uint16_t manufacturer   = little_endian_read_16(packet, 10);
89                 uint16_t lmp_subversion = little_endian_read_16(packet, 12);
90                 // assert manufacturer is TI
91                 if (manufacturer != BLUETOOTH_COMPANY_ID_TEXAS_INSTRUMENTS_INC){
92                     printf("ERROR: Expected Bluetooth Chipset from TI but got manufacturer 0x%04x\n", manufacturer);
93                     break;
94                 }
95                 // assert correct init script is used based on expected lmp_subversion
96                 if (lmp_subversion != btstack_chipset_cc256x_lmp_subversion()){
97                     printf("Error: LMP Subversion does not match initscript! ");
98                     printf("Your initscripts is for %s chipset\n", btstack_chipset_cc256x_lmp_subversion() < lmp_subversion ? "an older" : "a newer");
99                     printf("Please update Makefile to include the appropriate bluetooth_init_cc256???.c file\n");
100                     break;
101                 }
102             }
103             break;
104         default:
105             break;
106     }
107 }
108 
109 // main
110 int main(void)
111 {
112     // stop watchdog timer
113     WDTCTL = WDTPW + WDTHOLD;
114 
115     //Initialize clock and peripherals
116     halBoardInit();
117     halBoardStartXT1();
118     halBoardSetSystemClock(SYSCLK_16MHZ);
119 
120     // init debug UART
121     halUsbInit();
122 
123     // init LEDs
124     LED1_OUT |= LED1_PIN;
125     LED1_DIR |= LED1_PIN;
126     LED2_OUT |= LED2_PIN;
127     LED2_DIR |= LED2_PIN;
128 
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     // ready - enable irq used in h4 task
143     __enable_interrupt();
144 
145     btstack_main(0, NULL);
146 
147     // go!
148     btstack_run_loop_execute();
149 
150     return 0;
151 }
152 
153