11664436fSMatthias Ringwald /*
21664436fSMatthias Ringwald * Copyright (C) 2014 BlueKitchen GmbH
31664436fSMatthias Ringwald *
41664436fSMatthias Ringwald * Redistribution and use in source and binary forms, with or without
51664436fSMatthias Ringwald * modification, are permitted provided that the following conditions
61664436fSMatthias Ringwald * are met:
71664436fSMatthias Ringwald *
81664436fSMatthias Ringwald * 1. Redistributions of source code must retain the above copyright
91664436fSMatthias Ringwald * notice, this list of conditions and the following disclaimer.
101664436fSMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright
111664436fSMatthias Ringwald * notice, this list of conditions and the following disclaimer in the
121664436fSMatthias Ringwald * documentation and/or other materials provided with the distribution.
131664436fSMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of
141664436fSMatthias Ringwald * contributors may be used to endorse or promote products derived
151664436fSMatthias Ringwald * from this software without specific prior written permission.
161664436fSMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for
171664436fSMatthias Ringwald * personal benefit and not for any commercial purpose or for
181664436fSMatthias Ringwald * monetary gain.
191664436fSMatthias Ringwald *
201664436fSMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
211664436fSMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
221664436fSMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
232fca4dadSMilanka Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
242fca4dadSMilanka Ringwald * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
251664436fSMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
261664436fSMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
271664436fSMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
281664436fSMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
291664436fSMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
301664436fSMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311664436fSMatthias Ringwald * SUCH DAMAGE.
321664436fSMatthias Ringwald *
331664436fSMatthias Ringwald * Please inquire about commercial licensing options at
341664436fSMatthias Ringwald * [email protected]
351664436fSMatthias Ringwald *
361664436fSMatthias Ringwald */
371664436fSMatthias Ringwald
381664436fSMatthias Ringwald // *****************************************************************************
391664436fSMatthias Ringwald //
401664436fSMatthias Ringwald // spp_counter demo - it provides an SPP and sends a counter every second
411664436fSMatthias Ringwald //
421664436fSMatthias Ringwald // it doesn't use the LCD to get down to a minimal memory footprint
431664436fSMatthias Ringwald //
441664436fSMatthias Ringwald // *****************************************************************************
451664436fSMatthias Ringwald
461664436fSMatthias Ringwald #include <stdint.h>
471664436fSMatthias Ringwald #include <stdio.h>
481664436fSMatthias Ringwald #include <stdlib.h>
491664436fSMatthias Ringwald #include <string.h>
501664436fSMatthias Ringwald
511664436fSMatthias Ringwald #include <msp430x54x.h>
521664436fSMatthias Ringwald
531664436fSMatthias Ringwald #include "btstack_chipset_cc256x.h"
541664436fSMatthias Ringwald #include "btstack_config.h"
551664436fSMatthias Ringwald #include "btstack_event.h"
561664436fSMatthias Ringwald #include "btstack_memory.h"
571664436fSMatthias Ringwald #include "btstack_run_loop.h"
581664436fSMatthias Ringwald #include "btstack_run_loop_embedded.h"
591664436fSMatthias Ringwald #include "classic/btstack_link_key_db.h"
601664436fSMatthias Ringwald #include "bluetooth_company_id.h"
611664436fSMatthias Ringwald #include "hal_board.h"
621664436fSMatthias Ringwald #include "hal_compat.h"
631664436fSMatthias Ringwald #include "hal_usb.h"
641664436fSMatthias Ringwald #include "hci.h"
651664436fSMatthias Ringwald
hw_setup(void)661664436fSMatthias Ringwald static void hw_setup(void){
671664436fSMatthias Ringwald // stop watchdog timer
681664436fSMatthias Ringwald WDTCTL = WDTPW + WDTHOLD;
691664436fSMatthias Ringwald
701664436fSMatthias Ringwald //Initialize clock and peripherals
711664436fSMatthias Ringwald halBoardInit();
721664436fSMatthias Ringwald halBoardStartXT1();
731664436fSMatthias Ringwald halBoardSetSystemClock(SYSCLK_16MHZ);
741664436fSMatthias Ringwald
751664436fSMatthias Ringwald // init debug UART
761664436fSMatthias Ringwald halUsbInit();
771664436fSMatthias Ringwald
781664436fSMatthias Ringwald // init LEDs
791664436fSMatthias Ringwald LED1_OUT |= LED1_PIN;
801664436fSMatthias Ringwald LED1_DIR |= LED1_PIN;
811664436fSMatthias Ringwald LED2_OUT |= LED2_PIN;
821664436fSMatthias Ringwald LED2_DIR |= LED2_PIN;
831664436fSMatthias Ringwald
841664436fSMatthias Ringwald // ready - enable irq used in h4 task
851664436fSMatthias Ringwald __enable_interrupt();
861664436fSMatthias Ringwald }
871664436fSMatthias Ringwald
881664436fSMatthias Ringwald static hci_transport_config_uart_t config = {
891664436fSMatthias Ringwald HCI_TRANSPORT_CONFIG_UART,
901664436fSMatthias Ringwald 115200,
911664436fSMatthias Ringwald 1000000, // main baudrate
921664436fSMatthias Ringwald 1, // flow control
931664436fSMatthias Ringwald NULL,
941664436fSMatthias Ringwald };
951664436fSMatthias Ringwald
961664436fSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
971664436fSMatthias Ringwald
packet_handler(uint8_t packet_type,uint16_t channel,uint8_t * packet,uint16_t size)981664436fSMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
991664436fSMatthias Ringwald if (packet_type != HCI_EVENT_PACKET) return;
1001664436fSMatthias Ringwald switch(hci_event_packet_get_type(packet)){
1011664436fSMatthias Ringwald case BTSTACK_EVENT_STATE:
1021664436fSMatthias Ringwald if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) return;
1031664436fSMatthias Ringwald printf("BTstack up and running.\n");
1041664436fSMatthias Ringwald break;
1051664436fSMatthias Ringwald case HCI_EVENT_COMMAND_COMPLETE:
106*d39264f2SMatthias Ringwald if (hci_event_command_complete_get_command_opcode(packet) == HCI_OPCODE_HCI_READ_LOCAL_VERSION_INFORMATION){
1071664436fSMatthias Ringwald uint16_t manufacturer = little_endian_read_16(packet, 10);
1081664436fSMatthias Ringwald uint16_t lmp_subversion = little_endian_read_16(packet, 12);
1091664436fSMatthias Ringwald // assert manufacturer is TI
1101664436fSMatthias Ringwald if (manufacturer != BLUETOOTH_COMPANY_ID_TEXAS_INSTRUMENTS_INC){
1111664436fSMatthias Ringwald printf("ERROR: Expected Bluetooth Chipset from TI but got manufacturer 0x%04x\n", manufacturer);
1121664436fSMatthias Ringwald break;
1131664436fSMatthias Ringwald }
1141664436fSMatthias Ringwald // assert correct init script is used based on expected lmp_subversion
1151664436fSMatthias Ringwald if (lmp_subversion != btstack_chipset_cc256x_lmp_subversion()){
1161664436fSMatthias Ringwald printf("Error: LMP Subversion does not match initscript! ");
1171664436fSMatthias Ringwald printf("Your initscripts is for %s chipset\n", btstack_chipset_cc256x_lmp_subversion() < lmp_subversion ? "an older" : "a newer");
1181664436fSMatthias Ringwald printf("Please update Makefile to include the appropriate bluetooth_init_cc256???.c file\n");
1191664436fSMatthias Ringwald break;
1201664436fSMatthias Ringwald }
1211664436fSMatthias Ringwald }
1221664436fSMatthias Ringwald break;
1231664436fSMatthias Ringwald default:
1241664436fSMatthias Ringwald break;
1251664436fSMatthias Ringwald }
1261664436fSMatthias Ringwald }
1271664436fSMatthias Ringwald
btstack_setup(void)1281664436fSMatthias Ringwald static void btstack_setup(void){
1291664436fSMatthias Ringwald /// GET STARTED with BTstack ///
1301664436fSMatthias Ringwald btstack_memory_init();
1311664436fSMatthias Ringwald btstack_run_loop_init(btstack_run_loop_embedded_get_instance());
1321664436fSMatthias Ringwald
1331664436fSMatthias Ringwald // init HCI
1341664436fSMatthias Ringwald hci_init(hci_transport_h4_instance(btstack_uart_block_embedded_instance()), &config);
1351664436fSMatthias Ringwald hci_set_link_key_db(btstack_link_key_db_memory_instance());
1361664436fSMatthias Ringwald hci_set_chipset(btstack_chipset_cc256x_instance());
1371664436fSMatthias Ringwald
1381664436fSMatthias Ringwald // inform about BTstack state
1391664436fSMatthias Ringwald hci_event_callback_registration.callback = &packet_handler;
1401664436fSMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration);
1411664436fSMatthias Ringwald }
1421664436fSMatthias Ringwald
1431664436fSMatthias Ringwald int btstack_main(int argc, const char * argv[]);
1441664436fSMatthias Ringwald
1451664436fSMatthias Ringwald // main
main(void)1461664436fSMatthias Ringwald int main(void){
1471664436fSMatthias Ringwald
1481664436fSMatthias Ringwald hw_setup();
1491664436fSMatthias Ringwald
1501664436fSMatthias Ringwald btstack_setup();
1511664436fSMatthias Ringwald btstack_main(0, NULL);
1521664436fSMatthias Ringwald
1531664436fSMatthias Ringwald btstack_run_loop_execute();
1541664436fSMatthias Ringwald
1551664436fSMatthias Ringwald return 0;
1561664436fSMatthias Ringwald }
1571664436fSMatthias Ringwald
158