xref: /btstack/port/archive/msp430f5229lp-cc2564b/src/main.c (revision d39264f239eb026fd486651e238a9ef65f8504ab)
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 // main.c for msp430f5529-cc256x
411664436fSMatthias Ringwald //
421664436fSMatthias Ringwald // *****************************************************************************
431664436fSMatthias Ringwald 
441664436fSMatthias Ringwald #include <stdint.h>
451664436fSMatthias Ringwald #include <stdio.h>
461664436fSMatthias Ringwald #include <stdlib.h>
471664436fSMatthias Ringwald #include <string.h>
481664436fSMatthias Ringwald 
491664436fSMatthias Ringwald #include <msp430.h>
501664436fSMatthias Ringwald 
511664436fSMatthias Ringwald #include "btstack_chipset_cc256x.h"
521664436fSMatthias Ringwald #include "btstack_config.h"
531664436fSMatthias Ringwald #include "btstack_event.h"
541664436fSMatthias Ringwald #include "btstack_memory.h"
551664436fSMatthias Ringwald #include "btstack_run_loop.h"
561664436fSMatthias Ringwald #include "btstack_run_loop_embedded.h"
571664436fSMatthias Ringwald #include "bluetooth_company_id.h"
581664436fSMatthias Ringwald #include "classic/btstack_link_key_db.h"
591664436fSMatthias Ringwald #include "hal_board.h"
601664436fSMatthias Ringwald #include "hal_compat.h"
611664436fSMatthias Ringwald #include "hal_cpu.h"
621664436fSMatthias Ringwald #include "hal_tick.h"
631664436fSMatthias Ringwald #include "hal_usb.h"
641664436fSMatthias Ringwald #include "hci.h"
651664436fSMatthias Ringwald #include "hci_dump.h"
661664436fSMatthias Ringwald 
hw_setup(void)671664436fSMatthias Ringwald static void hw_setup(void){
681664436fSMatthias Ringwald 
691664436fSMatthias Ringwald     // stop watchdog timer
701664436fSMatthias Ringwald     WDTCTL = WDTPW + WDTHOLD;
711664436fSMatthias Ringwald 
721664436fSMatthias Ringwald     //Initialize clock and peripherals
731664436fSMatthias Ringwald     halBoardInit();
741664436fSMatthias Ringwald     halBoardStartXT1();
751664436fSMatthias Ringwald     halBoardSetSystemClock(SYSCLK_16MHZ);
761664436fSMatthias Ringwald 
771664436fSMatthias Ringwald     // // init debug UART
781664436fSMatthias Ringwald     halUsbInit();
791664436fSMatthias Ringwald 
801664436fSMatthias Ringwald     hal_tick_init();
811664436fSMatthias Ringwald 
821664436fSMatthias Ringwald     // init LEDs
831664436fSMatthias Ringwald     LED1_OUT |= LED1_PIN;
841664436fSMatthias Ringwald     LED1_DIR |= LED1_PIN;
851664436fSMatthias Ringwald     LED2_OUT |= LED2_PIN;
861664436fSMatthias Ringwald     LED2_DIR |= LED2_PIN;
871664436fSMatthias Ringwald 
881664436fSMatthias Ringwald     // ready - enable irq used in h4 task
891664436fSMatthias Ringwald     __enable_interrupt();
901664436fSMatthias Ringwald }
911664436fSMatthias Ringwald 
921664436fSMatthias Ringwald static hci_transport_config_uart_t config = {
931664436fSMatthias Ringwald     HCI_TRANSPORT_CONFIG_UART,
941664436fSMatthias Ringwald     115200,
951664436fSMatthias Ringwald     1000000,  // main baudrate
961664436fSMatthias Ringwald     1,        // flow control
971664436fSMatthias Ringwald     NULL,
981664436fSMatthias Ringwald };
991664436fSMatthias Ringwald 
1001664436fSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
1011664436fSMatthias Ringwald 
packet_handler(uint8_t packet_type,uint16_t channel,uint8_t * packet,uint16_t size)1021664436fSMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1031664436fSMatthias Ringwald     if (packet_type != HCI_EVENT_PACKET) return;
1041664436fSMatthias Ringwald     switch(hci_event_packet_get_type(packet)){
1051664436fSMatthias Ringwald         case BTSTACK_EVENT_STATE:
1061664436fSMatthias Ringwald             if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) return;
1071664436fSMatthias Ringwald             printf("BTstack up and running.\n");
1081664436fSMatthias Ringwald             break;
1091664436fSMatthias Ringwald         case HCI_EVENT_COMMAND_COMPLETE:
110*d39264f2SMatthias Ringwald             if (hci_event_command_complete_get_command_opcode(packet) == HCI_OPCODE_HCI_READ_LOCAL_VERSION_INFORMATION){
1111664436fSMatthias Ringwald                 uint16_t manufacturer   = little_endian_read_16(packet, 10);
1121664436fSMatthias Ringwald                 uint16_t lmp_subversion = little_endian_read_16(packet, 12);
1131664436fSMatthias Ringwald                 // assert manufacturer is TI
1141664436fSMatthias Ringwald                 if (manufacturer != BLUETOOTH_COMPANY_ID_TEXAS_INSTRUMENTS_INC){
1151664436fSMatthias Ringwald                     printf("ERROR: Expected Bluetooth Chipset from TI but got manufacturer 0x%04x\n", manufacturer);
1161664436fSMatthias Ringwald                     break;
1171664436fSMatthias Ringwald                 }
1181664436fSMatthias Ringwald                 // assert correct init script is used based on expected lmp_subversion
1191664436fSMatthias Ringwald                 if (lmp_subversion != btstack_chipset_cc256x_lmp_subversion()){
1201664436fSMatthias Ringwald                     printf("Error: LMP Subversion does not match initscript! ");
1211664436fSMatthias Ringwald                     printf("Your initscripts is for %s chipset\n", btstack_chipset_cc256x_lmp_subversion() < lmp_subversion ? "an older" : "a newer");
1221664436fSMatthias Ringwald                     printf("Please update Makefile to include the appropriate bluetooth_init_cc256???.c file\n");
1231664436fSMatthias Ringwald                     break;
1241664436fSMatthias Ringwald                 }
1251664436fSMatthias Ringwald             }
1261664436fSMatthias Ringwald             break;
1271664436fSMatthias Ringwald         default:
1281664436fSMatthias Ringwald             break;
1291664436fSMatthias Ringwald     }
1301664436fSMatthias Ringwald }
1311664436fSMatthias Ringwald 
btstack_setup(void)1321664436fSMatthias Ringwald static void btstack_setup(void){
1331664436fSMatthias Ringwald 
13498451c7bSMatthias Ringwald     // hci_dump_open(NULL, HCI_DUMP_STDOUT);
1351664436fSMatthias Ringwald 
1361664436fSMatthias Ringwald     /// GET STARTED with BTstack ///
1371664436fSMatthias Ringwald     btstack_memory_init();
1381664436fSMatthias Ringwald     btstack_run_loop_init(btstack_run_loop_embedded_get_instance());
1391664436fSMatthias Ringwald 
1401664436fSMatthias Ringwald     // init HCI
1411664436fSMatthias Ringwald     hci_init(hci_transport_h4_instance(btstack_uart_block_embedded_instance()), &config);
1421664436fSMatthias Ringwald     hci_set_link_key_db(btstack_link_key_db_memory_instance());
1431664436fSMatthias Ringwald     hci_set_chipset(btstack_chipset_cc256x_instance());
1441664436fSMatthias Ringwald 
1451664436fSMatthias Ringwald     // inform about BTstack state
1461664436fSMatthias Ringwald     hci_event_callback_registration.callback = &packet_handler;
1471664436fSMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
1481664436fSMatthias Ringwald }
1491664436fSMatthias Ringwald 
1501664436fSMatthias Ringwald int btstack_main(int argc, const char * argv[]);
1511664436fSMatthias Ringwald 
1521664436fSMatthias Ringwald // main
main(void)1531664436fSMatthias Ringwald int main(void){
1541664436fSMatthias Ringwald 
1551664436fSMatthias Ringwald     hw_setup();
1561664436fSMatthias Ringwald 
1571664436fSMatthias Ringwald     printf("Hardware setup done\n");
1581664436fSMatthias Ringwald 
1591664436fSMatthias Ringwald     btstack_setup();
1601664436fSMatthias Ringwald     btstack_main(0, NULL);
1611664436fSMatthias Ringwald 
1621664436fSMatthias Ringwald     btstack_run_loop_execute();
1631664436fSMatthias Ringwald     return 0;
1641664436fSMatthias Ringwald }
1651664436fSMatthias Ringwald 
166