xref: /btstack/port/archive/msp-exp430f5438-cc2564b/src/main.c (revision 1664436fd643daf66517dc309e6cc72448e8a86d)
1*1664436fSMatthias Ringwald /*
2*1664436fSMatthias Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
3*1664436fSMatthias Ringwald  *
4*1664436fSMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
5*1664436fSMatthias Ringwald  * modification, are permitted provided that the following conditions
6*1664436fSMatthias Ringwald  * are met:
7*1664436fSMatthias Ringwald  *
8*1664436fSMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
9*1664436fSMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
10*1664436fSMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
11*1664436fSMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
12*1664436fSMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
13*1664436fSMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
14*1664436fSMatthias Ringwald  *    contributors may be used to endorse or promote products derived
15*1664436fSMatthias Ringwald  *    from this software without specific prior written permission.
16*1664436fSMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
17*1664436fSMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
18*1664436fSMatthias Ringwald  *    monetary gain.
19*1664436fSMatthias Ringwald  *
20*1664436fSMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21*1664436fSMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22*1664436fSMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23*1664436fSMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24*1664436fSMatthias Ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25*1664436fSMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26*1664436fSMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27*1664436fSMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28*1664436fSMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29*1664436fSMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30*1664436fSMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31*1664436fSMatthias Ringwald  * SUCH DAMAGE.
32*1664436fSMatthias Ringwald  *
33*1664436fSMatthias Ringwald  * Please inquire about commercial licensing options at
34*1664436fSMatthias Ringwald  * [email protected]
35*1664436fSMatthias Ringwald  *
36*1664436fSMatthias Ringwald  */
37*1664436fSMatthias Ringwald 
38*1664436fSMatthias Ringwald // *****************************************************************************
39*1664436fSMatthias Ringwald //
40*1664436fSMatthias Ringwald // spp_counter demo - it provides an SPP and sends a counter every second
41*1664436fSMatthias Ringwald //
42*1664436fSMatthias Ringwald // it doesn't use the LCD to get down to a minimal memory footprint
43*1664436fSMatthias Ringwald //
44*1664436fSMatthias Ringwald // *****************************************************************************
45*1664436fSMatthias Ringwald 
46*1664436fSMatthias Ringwald #include <stdint.h>
47*1664436fSMatthias Ringwald #include <stdio.h>
48*1664436fSMatthias Ringwald #include <stdlib.h>
49*1664436fSMatthias Ringwald #include <string.h>
50*1664436fSMatthias Ringwald 
51*1664436fSMatthias Ringwald #include <msp430x54x.h>
52*1664436fSMatthias Ringwald 
53*1664436fSMatthias Ringwald #include "btstack_chipset_cc256x.h"
54*1664436fSMatthias Ringwald #include "btstack_config.h"
55*1664436fSMatthias Ringwald #include "btstack_event.h"
56*1664436fSMatthias Ringwald #include "btstack_memory.h"
57*1664436fSMatthias Ringwald #include "btstack_run_loop.h"
58*1664436fSMatthias Ringwald #include "btstack_run_loop_embedded.h"
59*1664436fSMatthias Ringwald #include "classic/btstack_link_key_db.h"
60*1664436fSMatthias Ringwald #include "bluetooth_company_id.h"
61*1664436fSMatthias Ringwald #include "hal_board.h"
62*1664436fSMatthias Ringwald #include "hal_compat.h"
63*1664436fSMatthias Ringwald #include "hal_usb.h"
64*1664436fSMatthias Ringwald #include "hci.h"
65*1664436fSMatthias Ringwald 
66*1664436fSMatthias Ringwald static void hw_setup(void){
67*1664436fSMatthias Ringwald     // stop watchdog timer
68*1664436fSMatthias Ringwald     WDTCTL = WDTPW + WDTHOLD;
69*1664436fSMatthias Ringwald 
70*1664436fSMatthias Ringwald     //Initialize clock and peripherals
71*1664436fSMatthias Ringwald     halBoardInit();
72*1664436fSMatthias Ringwald     halBoardStartXT1();
73*1664436fSMatthias Ringwald     halBoardSetSystemClock(SYSCLK_16MHZ);
74*1664436fSMatthias Ringwald 
75*1664436fSMatthias Ringwald     // init debug UART
76*1664436fSMatthias Ringwald     halUsbInit();
77*1664436fSMatthias Ringwald 
78*1664436fSMatthias Ringwald     // init LEDs
79*1664436fSMatthias Ringwald     LED1_OUT |= LED1_PIN;
80*1664436fSMatthias Ringwald     LED1_DIR |= LED1_PIN;
81*1664436fSMatthias Ringwald     LED2_OUT |= LED2_PIN;
82*1664436fSMatthias Ringwald     LED2_DIR |= LED2_PIN;
83*1664436fSMatthias Ringwald 
84*1664436fSMatthias Ringwald     // ready - enable irq used in h4 task
85*1664436fSMatthias Ringwald     __enable_interrupt();
86*1664436fSMatthias Ringwald }
87*1664436fSMatthias Ringwald 
88*1664436fSMatthias Ringwald static hci_transport_config_uart_t config = {
89*1664436fSMatthias Ringwald     HCI_TRANSPORT_CONFIG_UART,
90*1664436fSMatthias Ringwald     115200,
91*1664436fSMatthias Ringwald     1000000,  // main baudrate
92*1664436fSMatthias Ringwald     1,        // flow control
93*1664436fSMatthias Ringwald     NULL,
94*1664436fSMatthias Ringwald };
95*1664436fSMatthias Ringwald 
96*1664436fSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
97*1664436fSMatthias Ringwald 
98*1664436fSMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
99*1664436fSMatthias Ringwald     if (packet_type != HCI_EVENT_PACKET) return;
100*1664436fSMatthias Ringwald     switch(hci_event_packet_get_type(packet)){
101*1664436fSMatthias Ringwald         case BTSTACK_EVENT_STATE:
102*1664436fSMatthias Ringwald             if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) return;
103*1664436fSMatthias Ringwald             printf("BTstack up and running.\n");
104*1664436fSMatthias Ringwald             break;
105*1664436fSMatthias Ringwald         case HCI_EVENT_COMMAND_COMPLETE:
106*1664436fSMatthias Ringwald             if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_version_information)){
107*1664436fSMatthias Ringwald                 uint16_t manufacturer   = little_endian_read_16(packet, 10);
108*1664436fSMatthias Ringwald                 uint16_t lmp_subversion = little_endian_read_16(packet, 12);
109*1664436fSMatthias Ringwald                 // assert manufacturer is TI
110*1664436fSMatthias Ringwald                 if (manufacturer != BLUETOOTH_COMPANY_ID_TEXAS_INSTRUMENTS_INC){
111*1664436fSMatthias Ringwald                     printf("ERROR: Expected Bluetooth Chipset from TI but got manufacturer 0x%04x\n", manufacturer);
112*1664436fSMatthias Ringwald                     break;
113*1664436fSMatthias Ringwald                 }
114*1664436fSMatthias Ringwald                 // assert correct init script is used based on expected lmp_subversion
115*1664436fSMatthias Ringwald                 if (lmp_subversion != btstack_chipset_cc256x_lmp_subversion()){
116*1664436fSMatthias Ringwald                     printf("Error: LMP Subversion does not match initscript! ");
117*1664436fSMatthias Ringwald                     printf("Your initscripts is for %s chipset\n", btstack_chipset_cc256x_lmp_subversion() < lmp_subversion ? "an older" : "a newer");
118*1664436fSMatthias Ringwald                     printf("Please update Makefile to include the appropriate bluetooth_init_cc256???.c file\n");
119*1664436fSMatthias Ringwald                     break;
120*1664436fSMatthias Ringwald                 }
121*1664436fSMatthias Ringwald             }
122*1664436fSMatthias Ringwald             break;
123*1664436fSMatthias Ringwald         default:
124*1664436fSMatthias Ringwald             break;
125*1664436fSMatthias Ringwald     }
126*1664436fSMatthias Ringwald }
127*1664436fSMatthias Ringwald 
128*1664436fSMatthias Ringwald static void btstack_setup(void){
129*1664436fSMatthias Ringwald     /// GET STARTED with BTstack ///
130*1664436fSMatthias Ringwald     btstack_memory_init();
131*1664436fSMatthias Ringwald     btstack_run_loop_init(btstack_run_loop_embedded_get_instance());
132*1664436fSMatthias Ringwald 
133*1664436fSMatthias Ringwald     // init HCI
134*1664436fSMatthias Ringwald     hci_init(hci_transport_h4_instance(btstack_uart_block_embedded_instance()), &config);
135*1664436fSMatthias Ringwald     hci_set_link_key_db(btstack_link_key_db_memory_instance());
136*1664436fSMatthias Ringwald     hci_set_chipset(btstack_chipset_cc256x_instance());
137*1664436fSMatthias Ringwald 
138*1664436fSMatthias Ringwald     // inform about BTstack state
139*1664436fSMatthias Ringwald     hci_event_callback_registration.callback = &packet_handler;
140*1664436fSMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
141*1664436fSMatthias Ringwald }
142*1664436fSMatthias Ringwald 
143*1664436fSMatthias Ringwald int btstack_main(int argc, const char * argv[]);
144*1664436fSMatthias Ringwald 
145*1664436fSMatthias Ringwald // main
146*1664436fSMatthias Ringwald int main(void){
147*1664436fSMatthias Ringwald 
148*1664436fSMatthias Ringwald     hw_setup();
149*1664436fSMatthias Ringwald 
150*1664436fSMatthias Ringwald     btstack_setup();
151*1664436fSMatthias Ringwald     btstack_main(0, NULL);
152*1664436fSMatthias Ringwald 
153*1664436fSMatthias Ringwald     btstack_run_loop_execute();
154*1664436fSMatthias Ringwald 
155*1664436fSMatthias Ringwald     return 0;
156*1664436fSMatthias Ringwald }
157*1664436fSMatthias Ringwald 
158