xref: /btstack/port/msp432p401lp-cc256x/main.c (revision d39264f239eb026fd486651e238a9ef65f8504ab)
1f7529f1dSMatthias Ringwald /*
2f7529f1dSMatthias Ringwald  ******************************************************************************/
3f7529f1dSMatthias Ringwald /* DriverLib Includes */
4f7529f1dSMatthias Ringwald #include "driverlib.h"
5f7529f1dSMatthias Ringwald 
6f7529f1dSMatthias Ringwald /* Standard Includes */
7f7529f1dSMatthias Ringwald #include <stdint.h>
8f7529f1dSMatthias Ringwald 
9f7529f1dSMatthias Ringwald #include <stdbool.h>
10f7529f1dSMatthias Ringwald #include <string.h>
11f7529f1dSMatthias Ringwald #include <stdio.h>
1244ea2295SMatthias Ringwald 
13f7529f1dSMatthias Ringwald #include "btstack_config.h"
1444ea2295SMatthias Ringwald 
1544ea2295SMatthias Ringwald #include "bluetooth_company_id.h"
16f7529f1dSMatthias Ringwald #include "btstack_chipset_cc256x.h"
17f7529f1dSMatthias Ringwald #include "btstack_defines.h"
1844ea2295SMatthias Ringwald #include "btstack_event.h"
19f7529f1dSMatthias Ringwald #include "btstack_debug.h"
20f7529f1dSMatthias Ringwald #include "btstack_memory.h"
21fc456f6dSMatthias Ringwald #include "btstack_tlv.h"
22f7529f1dSMatthias Ringwald #include "btstack_run_loop.h"
23f7529f1dSMatthias Ringwald #include "btstack_run_loop_embedded.h"
24f7529f1dSMatthias Ringwald #include "hci_dump.h"
25c8dfe071SMatthias Ringwald #include "hci_transport.h"
26c8dfe071SMatthias Ringwald #include "hci_transport_h4.h"
27fc456f6dSMatthias Ringwald #include "btstack_tlv_flash_bank.h"
28fc456f6dSMatthias Ringwald #include "hal_flash_bank_msp432.h"
29fc456f6dSMatthias Ringwald #include "classic/btstack_link_key_db_tlv.h"
30fc456f6dSMatthias Ringwald #include "ble/le_device_db_tlv.h"
31f7529f1dSMatthias Ringwald 
325d255a7aSMatthias Ringwald #ifdef ENABLE_SEGGER_RTT
335d255a7aSMatthias Ringwald #include "hci_dump_segger_rtt_stdout.h"
345d255a7aSMatthias Ringwald #else
355d255a7aSMatthias Ringwald #include "hci_dump_embedded_stdout.h"
365d255a7aSMatthias Ringwald #endif
375d255a7aSMatthias Ringwald 
3809c40e12SMatthias Ringwald static void delay_ms(uint32_t ms);
3909c40e12SMatthias Ringwald 
40f7529f1dSMatthias Ringwald static hci_transport_config_uart_t config = {
41f7529f1dSMatthias Ringwald     HCI_TRANSPORT_CONFIG_UART,
42f7529f1dSMatthias Ringwald     115200,
43a2f998edSMatthias Ringwald     3000000,      // main baudrate
44f7529f1dSMatthias Ringwald     1,      // flow control
45f7529f1dSMatthias Ringwald     NULL,
46f7529f1dSMatthias Ringwald };
47f7529f1dSMatthias Ringwald 
48fc456f6dSMatthias Ringwald static hal_flash_bank_msp432_t   hal_flash_bank_context;
49fc456f6dSMatthias Ringwald static btstack_tlv_flash_bank_t btstack_tlv_flash_bank_context;
50fc456f6dSMatthias Ringwald 
51f7529f1dSMatthias Ringwald #ifndef ENABLE_SEGGER_RTT
52f7529f1dSMatthias Ringwald 
53f7529f1dSMatthias Ringwald /**
54f7529f1dSMatthias Ringwald  * Use USART_CONSOLE as a console.
55f7529f1dSMatthias Ringwald  * This is a syscall for newlib
56f7529f1dSMatthias Ringwald  * @param file
57f7529f1dSMatthias Ringwald  * @param ptr
58f7529f1dSMatthias Ringwald  * @param len
59f7529f1dSMatthias Ringwald  * @return
60f7529f1dSMatthias Ringwald  */
61f7529f1dSMatthias Ringwald #include <stdio.h>
62f7529f1dSMatthias Ringwald #include <unistd.h>
63f7529f1dSMatthias Ringwald #include <errno.h>
64f7529f1dSMatthias Ringwald int _write(int file, char *ptr, int len);
_write(int file,char * ptr,int len)65f7529f1dSMatthias Ringwald int _write(int file, char *ptr, int len){
66f7529f1dSMatthias Ringwald     uint8_t cr = '\r';
67f7529f1dSMatthias Ringwald     int i;
68f7529f1dSMatthias Ringwald 
69f7529f1dSMatthias Ringwald     if (file == STDOUT_FILENO || file == STDERR_FILENO) {
70f7529f1dSMatthias Ringwald         for (i = 0; i < len; i++) {
71f7529f1dSMatthias Ringwald             if (ptr[i] == '\n') {
72f7529f1dSMatthias Ringwald                 HAL_UART_Transmit( &huart2, &cr, 1, HAL_MAX_DELAY );
73f7529f1dSMatthias Ringwald             }
74f7529f1dSMatthias Ringwald             HAL_UART_Transmit( &huart2, (uint8_t *) &ptr[i], 1, HAL_MAX_DELAY );
75f7529f1dSMatthias Ringwald         }
76f7529f1dSMatthias Ringwald         return i;
77f7529f1dSMatthias Ringwald     }
78f7529f1dSMatthias Ringwald     errno = EIO;
79f7529f1dSMatthias Ringwald     return -1;
80f7529f1dSMatthias Ringwald }
81f7529f1dSMatthias Ringwald 
82f7529f1dSMatthias Ringwald #endif
83f7529f1dSMatthias Ringwald 
_read(int file,char * ptr,int len)84f7529f1dSMatthias Ringwald int _read(int file, char * ptr, int len){
85f7529f1dSMatthias Ringwald     UNUSED(file);
86f7529f1dSMatthias Ringwald     UNUSED(ptr);
87f7529f1dSMatthias Ringwald     UNUSED(len);
88f7529f1dSMatthias Ringwald     return -1;
89f7529f1dSMatthias Ringwald }
90f7529f1dSMatthias Ringwald 
91f7529f1dSMatthias Ringwald 
_close(int file)92f7529f1dSMatthias Ringwald int _close(int file){
93f7529f1dSMatthias Ringwald     UNUSED(file);
94f7529f1dSMatthias Ringwald     return -1;
95f7529f1dSMatthias Ringwald }
96f7529f1dSMatthias Ringwald 
_isatty(int file)97f7529f1dSMatthias Ringwald int _isatty(int file){
98f7529f1dSMatthias Ringwald     UNUSED(file);
99f7529f1dSMatthias Ringwald     return -1;
100f7529f1dSMatthias Ringwald }
101f7529f1dSMatthias Ringwald 
_lseek(int file)102f7529f1dSMatthias Ringwald int _lseek(int file){
103f7529f1dSMatthias Ringwald     UNUSED(file);
104f7529f1dSMatthias Ringwald     return -1;
105f7529f1dSMatthias Ringwald }
106f7529f1dSMatthias Ringwald 
_fstat(int file)107f7529f1dSMatthias Ringwald int _fstat(int file){
108f7529f1dSMatthias Ringwald     UNUSED(file);
109f7529f1dSMatthias Ringwald     return -1;
110f7529f1dSMatthias Ringwald }
111f7529f1dSMatthias Ringwald 
11249845b43SMatthias Ringwald // end of bss, start of heap
11349845b43SMatthias Ringwald extern int _end;
_sbrk(int incr)114f7529f1dSMatthias Ringwald void * _sbrk(int incr){
115ff348cd3SMatthias Ringwald     static unsigned char *heap = NULL;
116ff348cd3SMatthias Ringwald     unsigned char *prev_heap;
117ff348cd3SMatthias Ringwald 
118ff348cd3SMatthias Ringwald     if (heap == NULL) {
119ff348cd3SMatthias Ringwald         heap = (unsigned char *)&_end;
120ff348cd3SMatthias Ringwald     }
121ff348cd3SMatthias Ringwald     prev_heap = heap;
122ff348cd3SMatthias Ringwald     heap += incr;
123ff348cd3SMatthias Ringwald     return prev_heap;
124f7529f1dSMatthias Ringwald }
125f7529f1dSMatthias Ringwald 
abort(void)1266ea4c31dSMatthias Ringwald void abort(void){
1276ea4c31dSMatthias Ringwald     btstack_assert(false);
1286ea4c31dSMatthias Ringwald     while (1);
1296ea4c31dSMatthias Ringwald }
1306ea4c31dSMatthias Ringwald 
13149845b43SMatthias Ringwald #ifdef ENABLE_SEGGER_RTT
132f7529f1dSMatthias Ringwald 
133f7529f1dSMatthias Ringwald #include "SEGGER_RTT.h"
134f7529f1dSMatthias Ringwald 
135f7529f1dSMatthias Ringwald int SEGGER_RTT_vprintf(unsigned BufferIndex, const char * sFormat, va_list * pParamList);
136f7529f1dSMatthias Ringwald 
1373c367d7cSMatthias Ringwald // gcc/clang map printf to puts or putchar for printf("...\n"), or printf("%c", c) respectively
1383c367d7cSMatthias Ringwald 
puts(const char * s)1393c367d7cSMatthias Ringwald int puts(const char * s){
1403c367d7cSMatthias Ringwald     SEGGER_RTT_WriteString(0, s);
1413c367d7cSMatthias Ringwald     SEGGER_RTT_PutChar(0, '\n');
1423c367d7cSMatthias Ringwald }
1433c367d7cSMatthias Ringwald 
putchar(int c)1443c367d7cSMatthias Ringwald int putchar(int c){
1453c367d7cSMatthias Ringwald     SEGGER_RTT_PutChar(0, c);
1463c367d7cSMatthias Ringwald }
1473c367d7cSMatthias Ringwald 
printf(const char * format,...)148f7529f1dSMatthias Ringwald int printf(const char * format, ...){
149f7529f1dSMatthias Ringwald     va_list argptr;
150f7529f1dSMatthias Ringwald     va_start(argptr, format);
151f7529f1dSMatthias Ringwald     SEGGER_RTT_vprintf(0, format, &argptr);
152f7529f1dSMatthias Ringwald     va_end(argptr);
153f7529f1dSMatthias Ringwald }
154f7529f1dSMatthias Ringwald 
vprintf(const char * format,va_list argptr)155f7529f1dSMatthias Ringwald int vprintf(const char * format,  va_list argptr){
156f7529f1dSMatthias Ringwald     SEGGER_RTT_vprintf(0, format, &argptr);
157f7529f1dSMatthias Ringwald }
158f7529f1dSMatthias Ringwald #endif
159f7529f1dSMatthias Ringwald 
16049845b43SMatthias Ringwald 
16149845b43SMatthias Ringwald // HAL CPU
162f7529f1dSMatthias Ringwald #include "hal_cpu.h"
163f7529f1dSMatthias Ringwald 
hal_cpu_disable_irqs(void)164f7529f1dSMatthias Ringwald void hal_cpu_disable_irqs(void){
165f7529f1dSMatthias Ringwald     __disable_irq();
166f7529f1dSMatthias Ringwald }
167f7529f1dSMatthias Ringwald 
hal_cpu_enable_irqs(void)168f7529f1dSMatthias Ringwald void hal_cpu_enable_irqs(void){
169f7529f1dSMatthias Ringwald     __enable_irq();
170f7529f1dSMatthias Ringwald }
171f7529f1dSMatthias Ringwald 
hal_cpu_enable_irqs_and_sleep(void)172f7529f1dSMatthias Ringwald void hal_cpu_enable_irqs_and_sleep(void){
173f7529f1dSMatthias Ringwald     __enable_irq();
174f7529f1dSMatthias Ringwald     __asm__("wfe"); // go to sleep if event flag isn't set. if set, just clear it. IRQs set event flag
175f7529f1dSMatthias Ringwald }
176f7529f1dSMatthias Ringwald 
177d392ca88SMatthias Ringwald // HAL LED
178d392ca88SMatthias Ringwald #include "hal_led.h"
hal_led_toggle(void)179d392ca88SMatthias Ringwald void hal_led_toggle(void){
180d392ca88SMatthias Ringwald     static bool on = false;
181d392ca88SMatthias Ringwald     if (on){
182d392ca88SMatthias Ringwald         on = false;
183d392ca88SMatthias Ringwald         GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN0);
184d392ca88SMatthias Ringwald    } else {
185d392ca88SMatthias Ringwald         on = true;
186d392ca88SMatthias Ringwald         GPIO_setOutputHighOnPin(GPIO_PORT_P1, GPIO_PIN0);
187d392ca88SMatthias Ringwald     }
188d392ca88SMatthias Ringwald }
189d392ca88SMatthias Ringwald 
190f7529f1dSMatthias Ringwald // HAL UART DMA
1912171803eSMatthias Ringwald #include "hal_uart_dma.h"
19249845b43SMatthias Ringwald 
1938ba100b5SMatthias Ringwald // DMA Control Table
19420d2b70cSMatthias Ringwald // 8 channels are implemented => 16 channel control data structures (a 16 bytes) are needed
1958ba100b5SMatthias Ringwald // GCC
19620d2b70cSMatthias Ringwald __attribute__ ((aligned (256)))
19720d2b70cSMatthias Ringwald static DMA_ControlTable MSP_EXP432P401RLP_DMAControlTable[16];
1988ba100b5SMatthias Ringwald 
1998ba100b5SMatthias Ringwald // RX Ping Pong Buffer - similar to circular buffer on other MCUs
20009c40e12SMatthias Ringwald #define HAL_DMA_RX_BUFFER_SIZE 64
2018ba100b5SMatthias Ringwald static uint8_t hal_dma_rx_ping_pong_buffer[2 * HAL_DMA_RX_BUFFER_SIZE];
20209c40e12SMatthias Ringwald 
20309c40e12SMatthias Ringwald // active buffer and position to read from
20409c40e12SMatthias Ringwald static uint8_t  hal_dma_rx_active_buffer = 0;
20509c40e12SMatthias Ringwald static uint16_t hal_dma_rx_offset;
2068ba100b5SMatthias Ringwald 
207f7529f1dSMatthias Ringwald // rx state
208f7529f1dSMatthias Ringwald static uint16_t  bytes_to_read = 0;
209f7529f1dSMatthias Ringwald static uint8_t * rx_buffer_ptr = 0;
210f7529f1dSMatthias Ringwald 
211f7529f1dSMatthias Ringwald // tx state
212f7529f1dSMatthias Ringwald static uint16_t  bytes_to_write = 0;
213f7529f1dSMatthias Ringwald static uint8_t * tx_buffer_ptr = 0;
214f7529f1dSMatthias Ringwald 
215f7529f1dSMatthias Ringwald // handlers
216f7529f1dSMatthias Ringwald static void (*rx_done_handler)(void);
217f7529f1dSMatthias Ringwald static void (*tx_done_handler)(void);
218f7529f1dSMatthias Ringwald 
2190e19a6f0SMatthias Ringwald // #define BLUETOOTH_DEBUG_PORT     GPIO_PORT_P5
2200e19a6f0SMatthias Ringwald // #define BLUETOOTH_DEBUG_PIN      GPIO_PIN0
2210e19a6f0SMatthias Ringwald 
2229782e884SMatthias Ringwald #define BLUETOOTH_TX_PORT        GPIO_PORT_P3
2239782e884SMatthias Ringwald #define BLUETOOTH_TX_PIN         GPIO_PIN2
2249782e884SMatthias Ringwald #define BLUETOOTH_RX_PORT        GPIO_PORT_P3
2259782e884SMatthias Ringwald #define BLUETOOTH_RX_PIN         GPIO_PIN3
22664d059d5SMatthias Ringwald 
22764d059d5SMatthias Ringwald #if 0
22864d059d5SMatthias Ringwald // BOOST-CC2564MODA (CC2564B BoosterPack)
2299782e884SMatthias Ringwald #define BLUETOOTH_RTS_PORT       GPIO_PORT_P6
2309782e884SMatthias Ringwald #define BLUETOOTH_RTS_PIN        GPIO_PIN6
2319782e884SMatthias Ringwald #define BLUETOOTH_CTS_PORT       GPIO_PORT_P5
2329782e884SMatthias Ringwald #define BLUETOOTH_CTS_PIN        GPIO_PIN6
2339782e884SMatthias Ringwald #define BLUETOOTH_nSHUTDOWN_PORT GPIO_PORT_P2
2349782e884SMatthias Ringwald #define BLUETOOTH_nSHUTDOWN_PIN  GPIO_PIN5
2355ad984f6SMatthias Ringwald #else
2365ad984f6SMatthias Ringwald // EM Wireless BoosterPack with CC256x module
2375ad984f6SMatthias Ringwald #define BLUETOOTH_RTS_PORT       GPIO_PORT_P3
2385ad984f6SMatthias Ringwald #define BLUETOOTH_RTS_PIN        GPIO_PIN6
2395ad984f6SMatthias Ringwald #define BLUETOOTH_CTS_PORT       GPIO_PORT_P5
2405ad984f6SMatthias Ringwald #define BLUETOOTH_CTS_PIN        GPIO_PIN2
2415ad984f6SMatthias Ringwald #define BLUETOOTH_nSHUTDOWN_PORT GPIO_PORT_P6
2425ad984f6SMatthias Ringwald #define BLUETOOTH_nSHUTDOWN_PIN  GPIO_PIN4
2435ad984f6SMatthias Ringwald #endif
2449782e884SMatthias Ringwald 
245f7529f1dSMatthias Ringwald /* UART Configuration Parameter. These are the configuration parameters to
246f7529f1dSMatthias Ringwald  * make the eUSCI A UART module to operate with a 115200 baud rate. These
247f7529f1dSMatthias Ringwald  * values were calculated using the online calculator that TI provides
248f7529f1dSMatthias Ringwald  * at:
249f7529f1dSMatthias Ringwald  * http://software-dl.ti.com/msp430/msp430_public_sw/mcu/msp430/MSP430BaudRateConverter/index.html
250f7529f1dSMatthias Ringwald  */
2515fd0122aSMatthias Ringwald static eUSCI_UART_ConfigV1 uartConfig =
252f7529f1dSMatthias Ringwald {
253f7529f1dSMatthias Ringwald         EUSCI_A_UART_CLOCKSOURCE_SMCLK,          // SMCLK Clock Source
2540084ad25SMatthias Ringwald         0 ,                                      // BRDIV  (template)
2550084ad25SMatthias Ringwald         0,                                       // UCxBRF (template)
2560084ad25SMatthias Ringwald         0 ,                                      // UCxBRS (template)
257f7529f1dSMatthias Ringwald         EUSCI_A_UART_NO_PARITY,                  // No Parity
258f7529f1dSMatthias Ringwald         EUSCI_A_UART_LSB_FIRST,                  // MSB First
259f7529f1dSMatthias Ringwald         EUSCI_A_UART_ONE_STOP_BIT,               // One stop bit
2600084ad25SMatthias Ringwald         EUSCI_A_UART_MODE,                       // UART mode: normal
2610084ad25SMatthias Ringwald         0,                                       // Oversampling (template)
2625fd0122aSMatthias Ringwald         EUSCI_A_UART_8_BIT_LEN,                  // 8 bit
263f7529f1dSMatthias Ringwald };
264f7529f1dSMatthias Ringwald 
265f7529f1dSMatthias Ringwald // table
266f7529f1dSMatthias Ringwald static struct baudrate_config {
267f7529f1dSMatthias Ringwald     uint32_t baudrate;
268f7529f1dSMatthias Ringwald     uint8_t  clock_prescalar;
269f7529f1dSMatthias Ringwald     uint8_t  first_mod_reg;
270f7529f1dSMatthias Ringwald     uint8_t  second_mod_reg;
271f7529f1dSMatthias Ringwald     uint8_t  oversampling;
272f7529f1dSMatthias Ringwald } baudrate_configs[] = {
273acf554ecSMatthias Ringwald     // Config for 48 Mhz
2740084ad25SMatthias Ringwald     {   57600, 52,  1,   37, 1},
275acf554ecSMatthias Ringwald     {  115200, 26,  1,  111, 1},
276acf554ecSMatthias Ringwald     {  230400, 13,  0,   37, 1},
277acf554ecSMatthias Ringwald     {  460800,  6,  8,   32, 1},
278acf554ecSMatthias Ringwald     {  921600,  3,  4,    2, 1},
279acf554ecSMatthias Ringwald     { 1000000,  3,  0,    0, 1},
280acf554ecSMatthias Ringwald     { 2000000,  1,  8,    0, 1},
2810084ad25SMatthias Ringwald     { 3000000,  1,  0,    0, 1},
2820084ad25SMatthias Ringwald     { 4000000, 12,  0,    0, 0},
283f7529f1dSMatthias Ringwald };
284f7529f1dSMatthias Ringwald 
2853ae54f9dSMatthias Ringwald #ifdef TEST_LOOPBACK
2863ae54f9dSMatthias Ringwald static uint8_t test_tx[16];
2873ae54f9dSMatthias Ringwald static uint8_t test_rx[16];
2883ae54f9dSMatthias Ringwald static uint8_t test_rx_flag;
test_tx_complete(void)2893ae54f9dSMatthias Ringwald static void test_tx_complete(void){
2903ae54f9dSMatthias Ringwald }
test_rx_complete(void)2913ae54f9dSMatthias Ringwald static void test_rx_complete(void){
2923ae54f9dSMatthias Ringwald     test_rx_flag = 1;
2933ae54f9dSMatthias Ringwald }
2943ae54f9dSMatthias Ringwald #endif
2953ae54f9dSMatthias Ringwald 
2960084ad25SMatthias Ringwald // return true if ok
hal_uart_dma_config(uint32_t baud)2970084ad25SMatthias Ringwald static bool hal_uart_dma_config(uint32_t baud){
2980084ad25SMatthias Ringwald     int index = -1;
2990084ad25SMatthias Ringwald     int i;
3000084ad25SMatthias Ringwald     for (i=0;i<sizeof(baudrate_configs)/sizeof(struct baudrate_config);i++){
3010084ad25SMatthias Ringwald         if (baudrate_configs[i].baudrate == baud){
3020084ad25SMatthias Ringwald             index = i;
3030084ad25SMatthias Ringwald             break;
3040084ad25SMatthias Ringwald         }
3050084ad25SMatthias Ringwald     }
3060084ad25SMatthias Ringwald     if (index < 0) return false;
3070084ad25SMatthias Ringwald 
3080084ad25SMatthias Ringwald     uartConfig.clockPrescalar = baudrate_configs[index].clock_prescalar;
3090084ad25SMatthias Ringwald     uartConfig.firstModReg    = baudrate_configs[index].first_mod_reg;
3100084ad25SMatthias Ringwald     uartConfig.secondModReg   = baudrate_configs[index].second_mod_reg;
3110084ad25SMatthias Ringwald     uartConfig.overSampling   = baudrate_configs[index].oversampling ? EUSCI_A_UART_OVERSAMPLING_BAUDRATE_GENERATION : EUSCI_A_UART_LOW_FREQUENCY_BAUDRATE_GENERATION;
3120084ad25SMatthias Ringwald     return true;
3130084ad25SMatthias Ringwald }
3140084ad25SMatthias Ringwald 
hal_dma_rx_start_transfer(uint8_t buffer)31509c40e12SMatthias Ringwald static void hal_dma_rx_start_transfer(uint8_t buffer){
31609c40e12SMatthias Ringwald     uint32_t channel = DMA_CH5_EUSCIA2RX | (buffer == 0 ? UDMA_PRI_SELECT : UDMA_ALT_SELECT);
31709c40e12SMatthias Ringwald     MAP_DMA_setChannelTransfer(channel, UDMA_MODE_PINGPONG, (void *) UART_getReceiveBufferAddressForDMA(EUSCI_A2_BASE),
31809c40e12SMatthias Ringwald        (uint8_t *) &hal_dma_rx_ping_pong_buffer[buffer * HAL_DMA_RX_BUFFER_SIZE], HAL_DMA_RX_BUFFER_SIZE);
319f7529f1dSMatthias Ringwald }
320f7529f1dSMatthias Ringwald 
hal_dma_rx_bytes_avail(uint8_t buffer,uint16_t offset)32109c40e12SMatthias Ringwald static uint16_t hal_dma_rx_bytes_avail(uint8_t buffer, uint16_t offset){
32209c40e12SMatthias Ringwald     uint32_t channel = DMA_CH5_EUSCIA2RX | (buffer == 0 ? UDMA_PRI_SELECT : UDMA_ALT_SELECT);
32309c40e12SMatthias Ringwald     return HAL_DMA_RX_BUFFER_SIZE - MAP_DMA_getChannelSize(channel) - offset;
324f7529f1dSMatthias Ringwald }
325f7529f1dSMatthias Ringwald 
hal_uart_dma_update_rts(void)326992f9cc1SMatthias Ringwald static void hal_uart_dma_update_rts(void){
327992f9cc1SMatthias Ringwald     // get active transfer
328992f9cc1SMatthias Ringwald     uint32_t attribute = MAP_DMA_getChannelAttribute(DMA_CH5_EUSCIA2RX & 0x0F);
329992f9cc1SMatthias Ringwald     uint8_t  active_transfer_buffer = (attribute & UDMA_ATTR_ALTSELECT) ? 1 : 0;
330992f9cc1SMatthias Ringwald     if (hal_dma_rx_active_buffer == active_transfer_buffer){
331992f9cc1SMatthias Ringwald         GPIO_setOutputLowOnPin(BLUETOOTH_CTS_PORT, BLUETOOTH_CTS_PIN);
332992f9cc1SMatthias Ringwald     } else {
333992f9cc1SMatthias Ringwald         GPIO_setOutputHighOnPin(BLUETOOTH_CTS_PORT, BLUETOOTH_CTS_PIN);
334992f9cc1SMatthias Ringwald     }
335992f9cc1SMatthias Ringwald }
336992f9cc1SMatthias Ringwald 
3373ae54f9dSMatthias Ringwald // directly called from timer or similar interrupt. to call from non-isr context, interrupts must be disabled
hal_uart_dma_harvest(void)33809c40e12SMatthias Ringwald static void hal_uart_dma_harvest(void){
3393ae54f9dSMatthias Ringwald     if (bytes_to_read == 0) {
3403ae54f9dSMatthias Ringwald         return;
3413ae54f9dSMatthias Ringwald     }
3423ae54f9dSMatthias Ringwald 
34309c40e12SMatthias Ringwald     uint16_t bytes_avail = hal_dma_rx_bytes_avail(hal_dma_rx_active_buffer, hal_dma_rx_offset);
3443ae54f9dSMatthias Ringwald     if (bytes_avail == 0) {
3453ae54f9dSMatthias Ringwald         return;
3463ae54f9dSMatthias Ringwald     }
3473ae54f9dSMatthias Ringwald 
3483ae54f9dSMatthias Ringwald     // fetch bytes from current buffer
34909c40e12SMatthias Ringwald     uint16_t bytes_to_copy = btstack_min(bytes_avail, bytes_to_read);
35009c40e12SMatthias Ringwald     memcpy(rx_buffer_ptr, &hal_dma_rx_ping_pong_buffer[hal_dma_rx_active_buffer * HAL_DMA_RX_BUFFER_SIZE + hal_dma_rx_offset], bytes_to_copy);
35109c40e12SMatthias Ringwald     rx_buffer_ptr     += bytes_to_copy;
35209c40e12SMatthias Ringwald     hal_dma_rx_offset += bytes_to_copy;
3533ae54f9dSMatthias Ringwald     bytes_to_read     -= bytes_to_copy;
3543ae54f9dSMatthias Ringwald 
35509c40e12SMatthias Ringwald     // if current buffer fully processed, restart DMA transfer and switch to next buffer
35609c40e12SMatthias Ringwald     if (hal_dma_rx_offset == HAL_DMA_RX_BUFFER_SIZE){
35709c40e12SMatthias Ringwald         hal_dma_rx_offset = 0;
35809c40e12SMatthias Ringwald         hal_dma_rx_start_transfer(hal_dma_rx_active_buffer);
35909c40e12SMatthias Ringwald         hal_dma_rx_active_buffer = 1 - hal_dma_rx_active_buffer;
360992f9cc1SMatthias Ringwald         hal_uart_dma_update_rts();
361f7529f1dSMatthias Ringwald     }
3623ae54f9dSMatthias Ringwald 
363f7529f1dSMatthias Ringwald     if (bytes_to_read == 0){
364f7529f1dSMatthias Ringwald         (*rx_done_handler)();
365f7529f1dSMatthias Ringwald     }
366f7529f1dSMatthias Ringwald }
367f7529f1dSMatthias Ringwald 
DMA_INT1_IRQHandler(void)3680e19a6f0SMatthias Ringwald void DMA_INT1_IRQHandler(void){
3692171803eSMatthias Ringwald     MAP_DMA_clearInterruptFlag(DMA_CH4_EUSCIA2TX & 0x0F);
3702171803eSMatthias Ringwald     MAP_DMA_disableChannel(DMA_CH4_EUSCIA2TX & 0x0F);
3712171803eSMatthias Ringwald     (*tx_done_handler)();
3722171803eSMatthias Ringwald }
3732171803eSMatthias Ringwald 
DMA_INT2_IRQHandler(void)3740e19a6f0SMatthias Ringwald void DMA_INT2_IRQHandler(void){
3752171803eSMatthias Ringwald     MAP_DMA_clearInterruptFlag(DMA_CH5_EUSCIA2RX & 0x0F);
376992f9cc1SMatthias Ringwald     // update RTS
377992f9cc1SMatthias Ringwald     hal_uart_dma_update_rts();
378992f9cc1SMatthias Ringwald     // process data
3790e19a6f0SMatthias Ringwald #ifdef BLUETOOTH_DEBUG_PORT
3800e19a6f0SMatthias Ringwald     MAP_GPIO_setOutputHighOnPin(BLUETOOTH_DEBUG_PIN, BLUETOOTH_DEBUG_PORT);
3810e19a6f0SMatthias Ringwald #endif
3822171803eSMatthias Ringwald     hal_uart_dma_harvest();
3830e19a6f0SMatthias Ringwald #ifdef BLUETOOTH_DEBUG_PORT
3840e19a6f0SMatthias Ringwald     MAP_GPIO_setOutputLowOnPin(BLUETOOTH_DEBUG_PIN, BLUETOOTH_DEBUG_PORT);
3850e19a6f0SMatthias Ringwald #endif
3862171803eSMatthias Ringwald }
3872171803eSMatthias Ringwald 
3882171803eSMatthias Ringwald 
hal_uart_dma_init(void)389f7529f1dSMatthias Ringwald void hal_uart_dma_init(void){
390f7529f1dSMatthias Ringwald     // nShutdown
3919782e884SMatthias Ringwald     MAP_GPIO_setAsOutputPin(BLUETOOTH_nSHUTDOWN_PORT, BLUETOOTH_nSHUTDOWN_PIN);
392f7529f1dSMatthias Ringwald     // BT-CTS
3939782e884SMatthias Ringwald     MAP_GPIO_setAsOutputPin(BLUETOOTH_CTS_PORT, BLUETOOTH_CTS_PIN);
3949782e884SMatthias Ringwald     GPIO_setOutputHighOnPin(BLUETOOTH_CTS_PORT, BLUETOOTH_CTS_PIN);
395f7529f1dSMatthias Ringwald     // BT-RTS
3969782e884SMatthias Ringwald     MAP_GPIO_setAsInputPinWithPullDownResistor(BLUETOOTH_RTS_PORT, BLUETOOTH_RTS_PIN);
397f7529f1dSMatthias Ringwald     // UART pins
3989782e884SMatthias Ringwald     MAP_GPIO_setAsPeripheralModuleFunctionInputPin(BLUETOOTH_TX_PORT, BLUETOOTH_TX_PIN, GPIO_PRIMARY_MODULE_FUNCTION);
3999782e884SMatthias Ringwald     MAP_GPIO_setAsPeripheralModuleFunctionInputPin(BLUETOOTH_RX_PORT, BLUETOOTH_RX_PIN, GPIO_PRIMARY_MODULE_FUNCTION);
400f7529f1dSMatthias Ringwald 
4010e19a6f0SMatthias Ringwald #ifdef BLUETOOTH_DEBUG_PORT
4020e19a6f0SMatthias Ringwald     // debug pin
4030e19a6f0SMatthias Ringwald     MAP_GPIO_setAsOutputPin(BLUETOOTH_DEBUG_PIN, BLUETOOTH_DEBUG_PORT);
4040e19a6f0SMatthias Ringwald     MAP_GPIO_setOutputLowOnPin(BLUETOOTH_DEBUG_PIN, BLUETOOTH_DEBUG_PORT);
4050e19a6f0SMatthias Ringwald #endif
4060e19a6f0SMatthias Ringwald 
4078ba100b5SMatthias Ringwald     // UART
408f7529f1dSMatthias Ringwald 
4098ba100b5SMatthias Ringwald     /* Configuring and enable UART Module */
4102171803eSMatthias Ringwald     hal_uart_dma_config(115200);
4118ba100b5SMatthias Ringwald     MAP_UART_initModule(EUSCI_A2_BASE, &uartConfig);
412f7529f1dSMatthias Ringwald     MAP_UART_enableModule(EUSCI_A2_BASE);
413f7529f1dSMatthias Ringwald 
4148ba100b5SMatthias Ringwald     // DMA
4158ba100b5SMatthias Ringwald 
4168ba100b5SMatthias Ringwald     /* Configuring DMA module */
4178ba100b5SMatthias Ringwald     MAP_DMA_enableModule();
4188ba100b5SMatthias Ringwald     MAP_DMA_setControlBase(MSP_EXP432P401RLP_DMAControlTable);
4198ba100b5SMatthias Ringwald 
4208ba100b5SMatthias Ringwald     /* Assign DMA channel 4 to EUSCI_A2_TX, channel 5 to EUSCI_A2_RX */
4218ba100b5SMatthias Ringwald     MAP_DMA_assignChannel(DMA_CH4_EUSCIA2TX);
4228ba100b5SMatthias Ringwald     MAP_DMA_assignChannel(DMA_CH5_EUSCIA2RX);
4238ba100b5SMatthias Ringwald 
4248ba100b5SMatthias Ringwald     /* Setup the RX and TX transfer characteristics */
4258ba100b5SMatthias Ringwald     MAP_DMA_setChannelControl(DMA_CH4_EUSCIA2TX | UDMA_PRI_SELECT, UDMA_SIZE_8 | UDMA_SRC_INC_8 | UDMA_DST_INC_NONE | UDMA_ARB_1);
4268ba100b5SMatthias Ringwald     MAP_DMA_setChannelControl(DMA_CH5_EUSCIA2RX | UDMA_PRI_SELECT, UDMA_SIZE_8 | UDMA_SRC_INC_NONE | UDMA_DST_INC_8 | UDMA_ARB_1);
4272171803eSMatthias Ringwald     MAP_DMA_setChannelControl(DMA_CH5_EUSCIA2RX | UDMA_ALT_SELECT, UDMA_SIZE_8 | UDMA_SRC_INC_NONE | UDMA_DST_INC_8 | UDMA_ARB_1);
4288ba100b5SMatthias Ringwald 
4298ba100b5SMatthias Ringwald     /* Enable DMA interrupt for both channels */
4308ba100b5SMatthias Ringwald     MAP_DMA_assignInterrupt(INT_DMA_INT1, DMA_CH4_EUSCIA2TX & 0x0f);
4318ba100b5SMatthias Ringwald     MAP_DMA_assignInterrupt(INT_DMA_INT2, DMA_CH5_EUSCIA2RX & 0x0f);
4328ba100b5SMatthias Ringwald 
4338ba100b5SMatthias Ringwald     /* Clear interrupt flags */
4348ba100b5SMatthias Ringwald     MAP_DMA_clearInterruptFlag(DMA_CH4_EUSCIA2TX & 0x0F);
4358ba100b5SMatthias Ringwald     MAP_DMA_clearInterruptFlag(DMA_CH5_EUSCIA2RX & 0x0F);
4368ba100b5SMatthias Ringwald 
4378ba100b5SMatthias Ringwald     /* Enable Interrupts */
4388ba100b5SMatthias Ringwald     MAP_Interrupt_enableInterrupt(INT_DMA_INT1);
4398ba100b5SMatthias Ringwald     MAP_Interrupt_enableInterrupt(INT_DMA_INT2);
4408ba100b5SMatthias Ringwald     MAP_DMA_enableInterrupt(INT_DMA_INT1);
4418ba100b5SMatthias Ringwald     MAP_DMA_enableInterrupt(INT_DMA_INT2);
442f7529f1dSMatthias Ringwald 
443f7529f1dSMatthias Ringwald     // power cycle
4449782e884SMatthias Ringwald     MAP_GPIO_setOutputLowOnPin(BLUETOOTH_nSHUTDOWN_PORT, BLUETOOTH_nSHUTDOWN_PIN);
445f7529f1dSMatthias Ringwald     delay_ms(10);
4469782e884SMatthias Ringwald     MAP_GPIO_setOutputHighOnPin(BLUETOOTH_nSHUTDOWN_PORT, BLUETOOTH_nSHUTDOWN_PIN);
447f7529f1dSMatthias Ringwald     delay_ms(200);
4488ba100b5SMatthias Ringwald 
44909c40e12SMatthias Ringwald     // setup ping pong rx
45009c40e12SMatthias Ringwald     hal_dma_rx_start_transfer(0);
45109c40e12SMatthias Ringwald     hal_dma_rx_start_transfer(1);
4528ba100b5SMatthias Ringwald 
45309c40e12SMatthias Ringwald     hal_dma_rx_active_buffer = 0;
45409c40e12SMatthias Ringwald     hal_dma_rx_offset = 0;
4558ba100b5SMatthias Ringwald 
4568ba100b5SMatthias Ringwald     MAP_DMA_enableChannel(DMA_CH5_EUSCIA2RX & 0x0F);
45709c40e12SMatthias Ringwald 
45809c40e12SMatthias Ringwald     // ready
4599782e884SMatthias Ringwald     GPIO_setOutputLowOnPin(BLUETOOTH_CTS_PORT, BLUETOOTH_CTS_PIN);
4603ae54f9dSMatthias Ringwald 
4613ae54f9dSMatthias Ringwald #ifdef TEST_LOOPBACK
4623ae54f9dSMatthias Ringwald     // test code
4633ae54f9dSMatthias Ringwald     rx_done_handler = &test_rx_complete;
4643ae54f9dSMatthias Ringwald     tx_done_handler = &test_tx_complete;
4653ae54f9dSMatthias Ringwald     uint8_t value = 0;
4663ae54f9dSMatthias Ringwald     uint8_t block_size = 6;
4673ae54f9dSMatthias Ringwald     while(true){
4683ae54f9dSMatthias Ringwald         // prepare data
4693ae54f9dSMatthias Ringwald         uint8_t pos;
4703ae54f9dSMatthias Ringwald         for (pos=0;pos<block_size;pos++){
4713ae54f9dSMatthias Ringwald             test_tx[pos] = value++;
4723ae54f9dSMatthias Ringwald         }
4733ae54f9dSMatthias Ringwald         // trigger receive
4743ae54f9dSMatthias Ringwald         hal_uart_dma_receive_block(test_rx, block_size);
4753ae54f9dSMatthias Ringwald         // trigger send
4763ae54f9dSMatthias Ringwald         printf_hexdump(test_tx, block_size);
4773ae54f9dSMatthias Ringwald         hal_uart_dma_send_block(test_tx, block_size);
4783ae54f9dSMatthias Ringwald         while (test_rx_flag == 0){
4793ae54f9dSMatthias Ringwald             hal_cpu_disable_irqs();
4803ae54f9dSMatthias Ringwald             hal_uart_dma_harvest();
4813ae54f9dSMatthias Ringwald             hal_cpu_enable_irqs();
4823ae54f9dSMatthias Ringwald         };
4833ae54f9dSMatthias Ringwald         test_rx_flag = 0;
4843ae54f9dSMatthias Ringwald         printf_hexdump(test_rx, block_size);
4853ae54f9dSMatthias Ringwald         printf("\n");
4863ae54f9dSMatthias Ringwald         if (memcmp(test_rx, test_tx, block_size) != 0) break;
4873ae54f9dSMatthias Ringwald     }
4883ae54f9dSMatthias Ringwald     while (1);
4893ae54f9dSMatthias Ringwald #endif
490f7529f1dSMatthias Ringwald }
491f7529f1dSMatthias Ringwald 
hal_uart_dma_set_baud(uint32_t baud)492f7529f1dSMatthias Ringwald int  hal_uart_dma_set_baud(uint32_t baud){
4930084ad25SMatthias Ringwald     hal_uart_dma_config(baud);
494f7529f1dSMatthias Ringwald     MAP_UART_disableModule(EUSCI_A2_BASE);
4950084ad25SMatthias Ringwald     /* BaudRate Control Register */
4960084ad25SMatthias Ringwald     uint32_t moduleInstance = EUSCI_A2_BASE;
4970084ad25SMatthias Ringwald     const eUSCI_UART_ConfigV1 *config = &uartConfig;
4980084ad25SMatthias Ringwald     EUSCI_A_CMSIS(moduleInstance)->BRW = config->clockPrescalar;
4990084ad25SMatthias Ringwald     EUSCI_A_CMSIS(moduleInstance)->MCTLW = ((config->secondModReg << 8) + (config->firstModReg << 4) + config->overSampling);
500f7529f1dSMatthias Ringwald     MAP_UART_enableModule(EUSCI_A2_BASE);
501f7529f1dSMatthias Ringwald     return 0;
502f7529f1dSMatthias Ringwald }
503f7529f1dSMatthias Ringwald 
hal_uart_dma_set_sleep(uint8_t sleep)504f7529f1dSMatthias Ringwald void hal_uart_dma_set_sleep(uint8_t sleep){
505f7529f1dSMatthias Ringwald     UNUSED(sleep);
506f7529f1dSMatthias Ringwald }
507f7529f1dSMatthias Ringwald 
hal_uart_dma_set_csr_irq_handler(void (* the_irq_handler)(void))508f7529f1dSMatthias Ringwald void hal_uart_dma_set_csr_irq_handler( void (*the_irq_handler)(void)){
509f7529f1dSMatthias Ringwald     UNUSED(the_irq_handler);
510f7529f1dSMatthias Ringwald }
511f7529f1dSMatthias Ringwald 
hal_uart_dma_set_block_received(void (* the_block_handler)(void))512f7529f1dSMatthias Ringwald void hal_uart_dma_set_block_received( void (*the_block_handler)(void)){
513f7529f1dSMatthias Ringwald     rx_done_handler = the_block_handler;
514f7529f1dSMatthias Ringwald }
515f7529f1dSMatthias Ringwald 
hal_uart_dma_set_block_sent(void (* the_block_handler)(void))516f7529f1dSMatthias Ringwald void hal_uart_dma_set_block_sent( void (*the_block_handler)(void)){
517f7529f1dSMatthias Ringwald     tx_done_handler = the_block_handler;
518f7529f1dSMatthias Ringwald }
519f7529f1dSMatthias Ringwald 
hal_uart_dma_send_block(const uint8_t * data,uint16_t len)520f7529f1dSMatthias Ringwald void hal_uart_dma_send_block(const uint8_t * data, uint16_t len){
5218ba100b5SMatthias Ringwald     MAP_DMA_setChannelTransfer(DMA_CH4_EUSCIA2TX | UDMA_PRI_SELECT, UDMA_MODE_BASIC, (uint8_t *) data,
5228ba100b5SMatthias Ringwald                                (void *) MAP_UART_getTransmitBufferAddressForDMA(EUSCI_A2_BASE),
5238ba100b5SMatthias Ringwald                                len);
5248ba100b5SMatthias Ringwald     MAP_DMA_enableChannel(DMA_CH4_EUSCIA2TX & 0x0F);
525f7529f1dSMatthias Ringwald }
526f7529f1dSMatthias Ringwald 
527f7529f1dSMatthias Ringwald // int used to indicate a request for more new data
hal_uart_dma_receive_block(uint8_t * buffer,uint16_t len)528f7529f1dSMatthias Ringwald void hal_uart_dma_receive_block(uint8_t *buffer, uint16_t len){
529f7529f1dSMatthias Ringwald     rx_buffer_ptr = buffer;
530f7529f1dSMatthias Ringwald     bytes_to_read = len;
53109c40e12SMatthias Ringwald     hal_cpu_disable_irqs();
53209c40e12SMatthias Ringwald     hal_uart_dma_harvest();
53309c40e12SMatthias Ringwald     hal_cpu_enable_irqs();
534f7529f1dSMatthias Ringwald }
535f7529f1dSMatthias Ringwald 
53649845b43SMatthias Ringwald // HAL TIME MS Implementation
53749845b43SMatthias Ringwald #include "hal_time_ms.h"
53809c40e12SMatthias Ringwald static volatile uint32_t systick;
53909c40e12SMatthias Ringwald 
SysTick_Handler(void)54009c40e12SMatthias Ringwald void SysTick_Handler(void){
54109c40e12SMatthias Ringwald     systick++;
54209c40e12SMatthias Ringwald     // process received data
5430e19a6f0SMatthias Ringwald #ifdef BLUETOOTH_DEBUG_PORT
5440e19a6f0SMatthias Ringwald     MAP_GPIO_setOutputHighOnPin(BLUETOOTH_DEBUG_PIN, BLUETOOTH_DEBUG_PORT);
5450e19a6f0SMatthias Ringwald #endif
54609c40e12SMatthias Ringwald     hal_uart_dma_harvest();
5470e19a6f0SMatthias Ringwald #ifdef BLUETOOTH_DEBUG_PORT
5480e19a6f0SMatthias Ringwald     MAP_GPIO_setOutputLowOnPin(BLUETOOTH_DEBUG_PIN, BLUETOOTH_DEBUG_PORT);
5490e19a6f0SMatthias Ringwald #endif
55009c40e12SMatthias Ringwald }
55109c40e12SMatthias Ringwald 
55209c40e12SMatthias Ringwald 
init_systick(void)55309c40e12SMatthias Ringwald static void init_systick(void){
55409c40e12SMatthias Ringwald     // Configuring SysTick to trigger every ms (48 Mhz / 48000 = 1 ms)
55509c40e12SMatthias Ringwald     MAP_SysTick_enableModule();
55609c40e12SMatthias Ringwald     MAP_SysTick_setPeriod(48000);
55709c40e12SMatthias Ringwald     // MAP_Interrupt_enableSleepOnIsrExit();
55809c40e12SMatthias Ringwald     MAP_SysTick_enableInterrupt();
55909c40e12SMatthias Ringwald }
56009c40e12SMatthias Ringwald 
delay_ms(uint32_t ms)56109c40e12SMatthias Ringwald static void delay_ms(uint32_t ms){
56209c40e12SMatthias Ringwald     uint32_t delay_until = systick + ms;
5634472ec16SMatthias Ringwald     // assumes mcu runs fast enough to check once every ms
5644472ec16SMatthias Ringwald     while (systick != delay_until);
56509c40e12SMatthias Ringwald }
56609c40e12SMatthias Ringwald 
hal_time_ms(void)56709c40e12SMatthias Ringwald uint32_t hal_time_ms(void){
56809c40e12SMatthias Ringwald     return systick;
56909c40e12SMatthias Ringwald }
57009c40e12SMatthias Ringwald 
5710cec242bSMatthias Ringwald // btstack assert
btstack_assert_failed(const char * file,uint16_t line_nr)5720cec242bSMatthias Ringwald void btstack_assert_failed(const char * file, uint16_t line_nr){
5730cec242bSMatthias Ringwald     printf("ASSERT failed in %s, line %u\n", file, line_nr);
5740cec242bSMatthias Ringwald     while (1);
5750cec242bSMatthias Ringwald }
5760cec242bSMatthias Ringwald 
57744ea2295SMatthias Ringwald // main.c
578f7529f1dSMatthias Ringwald #include "SEGGER_RTT.h"
579f7529f1dSMatthias Ringwald 
580fc456f6dSMatthias Ringwald // HAL FLASH MSP432 Configuration - use two last 4kB sectors
581fc456f6dSMatthias Ringwald #define HAL_FLASH_BANK_SIZE      4096
582fc456f6dSMatthias Ringwald #define HAL_FLASH_BANK_0_SECTOR  FLASH_SECTOR30
583fc456f6dSMatthias Ringwald #define HAL_FLASH_BANK_1_SECTOR  FLASH_SECTOR31
584fc456f6dSMatthias Ringwald #define HAL_FLASH_BANK_0_ADDR    0x3E000
585fc456f6dSMatthias Ringwald #define HAL_FLASH_BANK_1_ADDR    0x3F000
586fc456f6dSMatthias Ringwald 
587fc456f6dSMatthias Ringwald int btstack_main(const int argc, const char * argvp[]);
588fc456f6dSMatthias Ringwald 
58944ea2295SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
59044ea2295SMatthias Ringwald 
packet_handler(uint8_t packet_type,uint16_t channel,uint8_t * packet,uint16_t size)59144ea2295SMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
59244ea2295SMatthias Ringwald     UNUSED(size);
59344ea2295SMatthias Ringwald     UNUSED(channel);
59444ea2295SMatthias Ringwald     bd_addr_t local_addr;
59544ea2295SMatthias Ringwald     if (packet_type != HCI_EVENT_PACKET) return;
59644ea2295SMatthias Ringwald     switch(hci_event_packet_get_type(packet)){
59744ea2295SMatthias Ringwald         case BTSTACK_EVENT_STATE:
59844ea2295SMatthias Ringwald             if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) return;
59944ea2295SMatthias Ringwald             gap_local_bd_addr(local_addr);
60044ea2295SMatthias Ringwald             printf("BTstack up and running on %s.\n", bd_addr_to_str(local_addr));
60144ea2295SMatthias Ringwald             break;
60244ea2295SMatthias Ringwald         case HCI_EVENT_COMMAND_COMPLETE:
603*d39264f2SMatthias Ringwald             if (hci_event_command_complete_get_command_opcode(packet) == HCI_OPCODE_HCI_READ_LOCAL_VERSION_INFORMATION){
60444ea2295SMatthias Ringwald                 uint16_t manufacturer   = little_endian_read_16(packet, 10);
60544ea2295SMatthias Ringwald                 uint16_t lmp_subversion = little_endian_read_16(packet, 12);
60644ea2295SMatthias Ringwald                 // assert manufacturer is TI
60744ea2295SMatthias Ringwald                 if (manufacturer != BLUETOOTH_COMPANY_ID_TEXAS_INSTRUMENTS_INC){
60844ea2295SMatthias Ringwald                     printf("ERROR: Expected Bluetooth Chipset from TI but got manufacturer 0x%04x\n", manufacturer);
60944ea2295SMatthias Ringwald                     break;
61044ea2295SMatthias Ringwald                 }
61144ea2295SMatthias Ringwald                 // assert correct init script is used based on expected lmp_subversion
61244ea2295SMatthias Ringwald                 if (lmp_subversion != btstack_chipset_cc256x_lmp_subversion()){
61364d059d5SMatthias Ringwald                     printf("Error: LMP Subversion does not match initscript!\n");
61444ea2295SMatthias Ringwald                     printf("Your initscripts is for %s chipset\n", btstack_chipset_cc256x_lmp_subversion() < lmp_subversion ? "an older" : "a newer");
61544ea2295SMatthias Ringwald                     printf("Please update Makefile to include the appropriate bluetooth_init_cc256???.c file\n");
61664d059d5SMatthias Ringwald                     btstack_assert(false);
61744ea2295SMatthias Ringwald                     break;
61844ea2295SMatthias Ringwald                 }
61944ea2295SMatthias Ringwald             }
62044ea2295SMatthias Ringwald             break;
62144ea2295SMatthias Ringwald         default:
62244ea2295SMatthias Ringwald             break;
62344ea2295SMatthias Ringwald     }
62444ea2295SMatthias Ringwald }
62544ea2295SMatthias Ringwald 
main(void)626f7529f1dSMatthias Ringwald int main(void)
627f7529f1dSMatthias Ringwald {
628f7529f1dSMatthias Ringwald     /* Halting the Watchdog */
629f7529f1dSMatthias Ringwald     MAP_WDT_A_holdTimer();
630f7529f1dSMatthias Ringwald 
631f7529f1dSMatthias Ringwald     init_systick();
632f7529f1dSMatthias Ringwald 
633d392ca88SMatthias Ringwald     // init led
634d392ca88SMatthias Ringwald     MAP_GPIO_setAsOutputPin(GPIO_PORT_P1, GPIO_PIN0);
635d392ca88SMatthias Ringwald     hal_led_toggle();
636d392ca88SMatthias Ringwald 
637f7529f1dSMatthias Ringwald     // start with BTstack init - especially configure HCI Transport
638f7529f1dSMatthias Ringwald     btstack_memory_init();
639f7529f1dSMatthias Ringwald     btstack_run_loop_init(btstack_run_loop_embedded_get_instance());
640f7529f1dSMatthias Ringwald 
641a2f998edSMatthias Ringwald     // uncomment to enable packet logger
6425d255a7aSMatthias Ringwald #ifdef ENABLE_SEGGER_RTT
6435d255a7aSMatthias Ringwald     // hci_dump_init(hci_dump_segger_rtt_stdout_get_instance());
6445d255a7aSMatthias Ringwald #else
6455d255a7aSMatthias Ringwald     // hci_dump_init(hci_dump_embedded_stdout_get_instance());
6465d255a7aSMatthias Ringwald #endif
647f7529f1dSMatthias Ringwald 
648f7529f1dSMatthias Ringwald     // init HCI
649f7529f1dSMatthias Ringwald     hci_init(hci_transport_h4_instance(btstack_uart_block_embedded_instance()), (void*) &config);
650f7529f1dSMatthias Ringwald     hci_set_chipset(btstack_chipset_cc256x_instance());
651f7529f1dSMatthias Ringwald 
652f7529f1dSMatthias Ringwald     // setup TLV Flash Sector implementation
653fc456f6dSMatthias Ringwald     const hal_flash_bank_t * hal_flash_bank_impl = hal_flash_bank_msp432_init_instance(
654f7529f1dSMatthias Ringwald             &hal_flash_bank_context,
655f7529f1dSMatthias Ringwald             HAL_FLASH_BANK_SIZE,
656f7529f1dSMatthias Ringwald             HAL_FLASH_BANK_0_SECTOR,
657f7529f1dSMatthias Ringwald             HAL_FLASH_BANK_1_SECTOR,
658f7529f1dSMatthias Ringwald             HAL_FLASH_BANK_0_ADDR,
659f7529f1dSMatthias Ringwald             HAL_FLASH_BANK_1_ADDR);
660fc456f6dSMatthias Ringwald 
661f7529f1dSMatthias Ringwald     const btstack_tlv_t * btstack_tlv_impl = btstack_tlv_flash_bank_init_instance(
662f7529f1dSMatthias Ringwald             &btstack_tlv_flash_bank_context,
663f7529f1dSMatthias Ringwald             hal_flash_bank_impl,
664f7529f1dSMatthias Ringwald             &hal_flash_bank_context);
665f7529f1dSMatthias Ringwald 
666f7529f1dSMatthias Ringwald     // setup global tlv
667f7529f1dSMatthias Ringwald     btstack_tlv_set_instance(btstack_tlv_impl, &btstack_tlv_flash_bank_context);
668f7529f1dSMatthias Ringwald 
669f7529f1dSMatthias Ringwald     // setup Link Key DB using TLV
670f7529f1dSMatthias Ringwald     const btstack_link_key_db_t * btstack_link_key_db = btstack_link_key_db_tlv_get_instance(btstack_tlv_impl, &btstack_tlv_flash_bank_context);
671f7529f1dSMatthias Ringwald     hci_set_link_key_db(btstack_link_key_db);
672f7529f1dSMatthias Ringwald 
673f7529f1dSMatthias Ringwald     // setup LE Device DB using TLV
674f7529f1dSMatthias Ringwald     le_device_db_tlv_configure(btstack_tlv_impl, &btstack_tlv_flash_bank_context);
675f7529f1dSMatthias Ringwald 
676f7529f1dSMatthias Ringwald     // inform about BTstack state
677f7529f1dSMatthias Ringwald     hci_event_callback_registration.callback = &packet_handler;
678f7529f1dSMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
679f7529f1dSMatthias Ringwald 
680f7529f1dSMatthias Ringwald     // hand over to btstack embedded code
681f7529f1dSMatthias Ringwald     btstack_main(0, NULL);
682f7529f1dSMatthias Ringwald 
683f7529f1dSMatthias Ringwald     // go
684f7529f1dSMatthias Ringwald     btstack_run_loop_execute();
685f7529f1dSMatthias Ringwald }
686f7529f1dSMatthias Ringwald 
687f7529f1dSMatthias Ringwald 
688