11b2596b5SMatthias Ringwald #include "asf.h" 21b2596b5SMatthias Ringwald #include "stdio_serial.h" 31b2596b5SMatthias Ringwald #include "conf_board.h" 41b2596b5SMatthias Ringwald #include "conf_clock.h" 51b2596b5SMatthias Ringwald 658a1b1bbSMatthias Ringwald // BTstack 713b8e9b1SMatthias Ringwald #include "btstack_chipset_atwilc3000.h" 813b8e9b1SMatthias Ringwald #include "btstack_debug.h" 913b8e9b1SMatthias Ringwald #include "btstack_memory.h" 1058a1b1bbSMatthias Ringwald #include "btstack_run_loop.h" 1158a1b1bbSMatthias Ringwald #include "btstack_run_loop_embedded.h" 1258a1b1bbSMatthias Ringwald #include "classic/btstack_link_key_db.h" 1358a1b1bbSMatthias Ringwald #include "hal_uart_dma.h" 1458a1b1bbSMatthias Ringwald #include "hal_cpu.h" 1558a1b1bbSMatthias Ringwald #include "hal_tick.h" 1613b8e9b1SMatthias Ringwald #include "hci.h" 1713b8e9b1SMatthias Ringwald #include "hci_dump.h" 1813b8e9b1SMatthias Ringwald #include "wilc3000_bt_firmware.h" 1958a1b1bbSMatthias Ringwald 2013b8e9b1SMatthias Ringwald // #define USE_XDMAC_FOR_USART 2158a1b1bbSMatthias Ringwald #define XDMA_CH_UART_TX 0 2258a1b1bbSMatthias Ringwald #define XDMA_CH_UART_RX 1 231b2596b5SMatthias Ringwald 2458a1b1bbSMatthias Ringwald /** All interrupt mask. */ 2558a1b1bbSMatthias Ringwald #define ALL_INTERRUPT_MASK 0xffffffff 261b2596b5SMatthias Ringwald 271b2596b5SMatthias Ringwald #ifdef __cplusplus 281b2596b5SMatthias Ringwald extern "C" { 291b2596b5SMatthias Ringwald #endif 3013b8e9b1SMatthias Ringwald 3113b8e9b1SMatthias Ringwald extern int btstack_main(int argc, const char * argv[]); 3213b8e9b1SMatthias Ringwald 3313b8e9b1SMatthias Ringwald static void dummy_handler(void){} 3413b8e9b1SMatthias Ringwald static void (*tick_handler)(void) = &dummy_handler; 3513b8e9b1SMatthias Ringwald 3613b8e9b1SMatthias Ringwald static btstack_uart_config_t uart_config; 3713b8e9b1SMatthias Ringwald 3813b8e9b1SMatthias Ringwald static hci_transport_config_uart_t transport_config = { 3913b8e9b1SMatthias Ringwald HCI_TRANSPORT_CONFIG_UART, 4013b8e9b1SMatthias Ringwald 115200, 4113b8e9b1SMatthias Ringwald 921600, // use 0 to skip baud rate change from 115200 to X for debugging purposes 4213b8e9b1SMatthias Ringwald 1, // flow control 4313b8e9b1SMatthias Ringwald NULL, 4413b8e9b1SMatthias Ringwald }; 451b2596b5SMatthias Ringwald 461b2596b5SMatthias Ringwald /** 471b2596b5SMatthias Ringwald * \brief Handler for System Tick interrupt. 481b2596b5SMatthias Ringwald */ 491b2596b5SMatthias Ringwald void SysTick_Handler(void) 501b2596b5SMatthias Ringwald { 5158a1b1bbSMatthias Ringwald tick_handler(); 521b2596b5SMatthias Ringwald } 531b2596b5SMatthias Ringwald 541b2596b5SMatthias Ringwald /** 551b2596b5SMatthias Ringwald * Configure UART console. 561b2596b5SMatthias Ringwald */ 571b2596b5SMatthias Ringwald // [main_console_configure] 581b2596b5SMatthias Ringwald static void configure_console(void) 591b2596b5SMatthias Ringwald { 601b2596b5SMatthias Ringwald const usart_serial_options_t uart_serial_options = { 611b2596b5SMatthias Ringwald .baudrate = CONF_UART_BAUDRATE, 621b2596b5SMatthias Ringwald #ifdef CONF_UART_CHAR_LENGTH 631b2596b5SMatthias Ringwald .charlength = CONF_UART_CHAR_LENGTH, 641b2596b5SMatthias Ringwald #endif 651b2596b5SMatthias Ringwald .paritytype = CONF_UART_PARITY, 661b2596b5SMatthias Ringwald #ifdef CONF_UART_STOP_BITS 671b2596b5SMatthias Ringwald .stopbits = CONF_UART_STOP_BITS, 681b2596b5SMatthias Ringwald #endif 691b2596b5SMatthias Ringwald }; 701b2596b5SMatthias Ringwald 711b2596b5SMatthias Ringwald /* Configure console UART. */ 721b2596b5SMatthias Ringwald sysclk_enable_peripheral_clock(CONSOLE_UART_ID); 731b2596b5SMatthias Ringwald stdio_serial_init(CONF_UART, &uart_serial_options); 741b2596b5SMatthias Ringwald } 751b2596b5SMatthias Ringwald 761b2596b5SMatthias Ringwald // [main_console_configure] 771b2596b5SMatthias Ringwald 781b2596b5SMatthias Ringwald /** 7958a1b1bbSMatthias Ringwald * \brief Wait for the given number of milliseconds (ticks 801b2596b5SMatthias Ringwald * generated by the SAM's microcontrollers's system tick). 811b2596b5SMatthias Ringwald * 821b2596b5SMatthias Ringwald * \param ul_dly_ticks Delay to wait for, in milliseconds. 831b2596b5SMatthias Ringwald */ 841b2596b5SMatthias Ringwald // [main_ms_delay] 8558a1b1bbSMatthias Ringwald static void mdelay(uint32_t delay_in_ms) 861b2596b5SMatthias Ringwald { 8758a1b1bbSMatthias Ringwald // delay_ms(delay_in_ms); 8858a1b1bbSMatthias Ringwald uint32_t time_to_wait = btstack_run_loop_get_time_ms() + delay_in_ms; 8958a1b1bbSMatthias Ringwald while (btstack_run_loop_get_time_ms() < time_to_wait); 901b2596b5SMatthias Ringwald } 911b2596b5SMatthias Ringwald // [main_ms_delay] 921b2596b5SMatthias Ringwald 9358a1b1bbSMatthias Ringwald //////////////////////////////////////////////////////////////////////////////// 9458a1b1bbSMatthias Ringwald // hal_cpu.h implementation 9558a1b1bbSMatthias Ringwald //////////////////////////////////////////////////////////////////////////////// 9658a1b1bbSMatthias Ringwald // hal_led.h implementation 9758a1b1bbSMatthias Ringwald #include "hal_led.h" 9858a1b1bbSMatthias Ringwald void hal_led_off(void); 9958a1b1bbSMatthias Ringwald void hal_led_on(void); 10058a1b1bbSMatthias Ringwald 10158a1b1bbSMatthias Ringwald void hal_led_off(void){ 10258a1b1bbSMatthias Ringwald // gpio_set_pin_low(GPIOA, GPIO_LED2); 10358a1b1bbSMatthias Ringwald } 10458a1b1bbSMatthias Ringwald void hal_led_on(void){ 10558a1b1bbSMatthias Ringwald // gpio_set_pin_high(GPIOA, GPIO_LED2); 10658a1b1bbSMatthias Ringwald } 10758a1b1bbSMatthias Ringwald void hal_led_toggle(void){ 10858a1b1bbSMatthias Ringwald // gpio_toggle_pin(GPIOA, GPIO_LED2); 10958a1b1bbSMatthias Ringwald } 11058a1b1bbSMatthias Ringwald 11158a1b1bbSMatthias Ringwald // hal_cpu.h implementation 11258a1b1bbSMatthias Ringwald #include "hal_cpu.h" 11358a1b1bbSMatthias Ringwald 11458a1b1bbSMatthias Ringwald void hal_cpu_disable_irqs(void){ 11558a1b1bbSMatthias Ringwald //__disable_irq(); 11658a1b1bbSMatthias Ringwald } 11758a1b1bbSMatthias Ringwald 11858a1b1bbSMatthias Ringwald void hal_cpu_enable_irqs(void){ 11958a1b1bbSMatthias Ringwald // __enable_irq(); 12058a1b1bbSMatthias Ringwald } 12158a1b1bbSMatthias Ringwald 12258a1b1bbSMatthias Ringwald void hal_cpu_enable_irqs_and_sleep(void){ 12358a1b1bbSMatthias Ringwald hal_led_off(); 12458a1b1bbSMatthias Ringwald // __enable_irq(); 12558a1b1bbSMatthias Ringwald // __asm__("wfe"); // go to sleep if event flag isn't set. if set, just clear it. IRQs set event flag 12658a1b1bbSMatthias Ringwald 12758a1b1bbSMatthias Ringwald // note: hal_uart_needed_during_sleep can be used to disable peripheral clock if it's not needed for a timer 12858a1b1bbSMatthias Ringwald hal_led_on(); 12958a1b1bbSMatthias Ringwald } 13058a1b1bbSMatthias Ringwald 13158a1b1bbSMatthias Ringwald 13258a1b1bbSMatthias Ringwald #ifndef USE_XDMAC_FOR_USART 13358a1b1bbSMatthias Ringwald // RX state 13458a1b1bbSMatthias Ringwald static volatile uint16_t bytes_to_read = 0; 13558a1b1bbSMatthias Ringwald static volatile uint8_t * rx_buffer_ptr = 0; 13658a1b1bbSMatthias Ringwald 13758a1b1bbSMatthias Ringwald // TX state 13858a1b1bbSMatthias Ringwald static volatile uint16_t bytes_to_write = 0; 13958a1b1bbSMatthias Ringwald static volatile uint8_t * tx_buffer_ptr = 0; 14058a1b1bbSMatthias Ringwald #endif 14158a1b1bbSMatthias Ringwald 142*c267d18bSMatthias Ringwald static int simulate_flowcontrol; 143*c267d18bSMatthias Ringwald 14458a1b1bbSMatthias Ringwald // handlers 14558a1b1bbSMatthias Ringwald static void (*rx_done_handler)(void) = dummy_handler; 14658a1b1bbSMatthias Ringwald static void (*tx_done_handler)(void) = dummy_handler; 14758a1b1bbSMatthias Ringwald static void (*cts_irq_handler)(void) = dummy_handler; 14858a1b1bbSMatthias Ringwald 14958a1b1bbSMatthias Ringwald // @note While the Atmel SAM S7x data sheet states 15058a1b1bbSMatthias Ringwald // "The hardware handshaking feature enables an out-of-band flow control by automatic management 15158a1b1bbSMatthias Ringwald // of the pins RTS and CTS.", 15258a1b1bbSMatthias Ringwald // I didn't see RTS going up automatically up, ever. So, at least for RTS, the automatic management 15358a1b1bbSMatthias Ringwald // is just a glorified GPIO pin control feature, which provides no benefit, but irritates a lot 15458a1b1bbSMatthias Ringwald 155*c267d18bSMatthias Ringwald static inline void hal_uart_rts_high(void){ 156*c267d18bSMatthias Ringwald if (!simulate_flowcontrol) return; 15758a1b1bbSMatthias Ringwald BOARD_USART->US_CR = US_CR_RTSEN; 15858a1b1bbSMatthias Ringwald } 159*c267d18bSMatthias Ringwald static inline void hal_uart_rts_low(void){ 160*c267d18bSMatthias Ringwald if (!simulate_flowcontrol) return; 16158a1b1bbSMatthias Ringwald BOARD_USART->US_CR = US_CR_RTSDIS; 16258a1b1bbSMatthias Ringwald } 16358a1b1bbSMatthias Ringwald 16458a1b1bbSMatthias Ringwald /** 16558a1b1bbSMatthias Ringwald */ 16658a1b1bbSMatthias Ringwald void hal_uart_dma_init(void) 16758a1b1bbSMatthias Ringwald { 16813b8e9b1SMatthias Ringwald // power on 16913b8e9b1SMatthias Ringwald ioport_set_pin_dir(BLUETOOTH_CHP_EN, IOPORT_DIR_OUTPUT); 17013b8e9b1SMatthias Ringwald ioport_set_pin_level(BLUETOOTH_CHP_EN, IOPORT_PIN_LEVEL_HIGH); 17113b8e9b1SMatthias Ringwald 17213b8e9b1SMatthias Ringwald // reset 17313b8e9b1SMatthias Ringwald ioport_set_pin_dir(BLUETOOTH_RESET, IOPORT_DIR_OUTPUT); 17413b8e9b1SMatthias Ringwald ioport_set_pin_level(BLUETOOTH_RESET, IOPORT_PIN_LEVEL_LOW); 17513b8e9b1SMatthias Ringwald mdelay(250); 17613b8e9b1SMatthias Ringwald ioport_set_pin_level(BLUETOOTH_RESET, IOPORT_PIN_LEVEL_HIGH); 17713b8e9b1SMatthias Ringwald mdelay(250); 17813b8e9b1SMatthias Ringwald 17913b8e9b1SMatthias Ringwald /* Enable the peripheral clock in the PMC. */ 18013b8e9b1SMatthias Ringwald sysclk_enable_peripheral_clock(BOARD_ID_USART); 18158a1b1bbSMatthias Ringwald 18258a1b1bbSMatthias Ringwald // configure Bluetooth USART 18358a1b1bbSMatthias Ringwald const sam_usart_opt_t bluetooth_settings = { 18458a1b1bbSMatthias Ringwald 115200, 18558a1b1bbSMatthias Ringwald US_MR_CHRL_8_BIT, 18658a1b1bbSMatthias Ringwald US_MR_PAR_NO, 18758a1b1bbSMatthias Ringwald US_MR_NBSTOP_1_BIT, 18858a1b1bbSMatthias Ringwald US_MR_CHMODE_NORMAL, 18958a1b1bbSMatthias Ringwald /* This field is only used in IrDA mode. */ 19058a1b1bbSMatthias Ringwald 0 19158a1b1bbSMatthias Ringwald }; 19258a1b1bbSMatthias Ringwald 19358a1b1bbSMatthias Ringwald /* Configure USART mode. */ 194*c267d18bSMatthias Ringwald simulate_flowcontrol = 0; 19513b8e9b1SMatthias Ringwald usart_init_rs232(BOARD_USART, &bluetooth_settings, sysclk_get_peripheral_hz()); 196*c267d18bSMatthias Ringwald // Set RTS = 0 (normal mode) 197*c267d18bSMatthias Ringwald BOARD_USART->US_CR = US_CR_RTSEN; 19858a1b1bbSMatthias Ringwald 19958a1b1bbSMatthias Ringwald /* Disable all the interrupts. */ 20058a1b1bbSMatthias Ringwald usart_disable_interrupt(BOARD_USART, ALL_INTERRUPT_MASK); 20158a1b1bbSMatthias Ringwald 20258a1b1bbSMatthias Ringwald /* Enable TX & RX function. */ 20358a1b1bbSMatthias Ringwald usart_enable_tx(BOARD_USART); 20458a1b1bbSMatthias Ringwald usart_enable_rx(BOARD_USART); 20558a1b1bbSMatthias Ringwald 20658a1b1bbSMatthias Ringwald /* Configure and enable interrupt of USART. */ 20758a1b1bbSMatthias Ringwald NVIC_EnableIRQ(USART_IRQn); 20858a1b1bbSMatthias Ringwald 20958a1b1bbSMatthias Ringwald #ifdef USE_XDMAC_FOR_USART 21058a1b1bbSMatthias Ringwald 21158a1b1bbSMatthias Ringwald // setup XDMAC 21258a1b1bbSMatthias Ringwald 21358a1b1bbSMatthias Ringwald /* Initialize and enable DMA controller */ 21458a1b1bbSMatthias Ringwald pmc_enable_periph_clk(ID_XDMAC); 21558a1b1bbSMatthias Ringwald 21658a1b1bbSMatthias Ringwald /* Enable XDMA interrupt */ 21758a1b1bbSMatthias Ringwald NVIC_ClearPendingIRQ(XDMAC_IRQn); 21858a1b1bbSMatthias Ringwald NVIC_SetPriority( XDMAC_IRQn ,1); 21958a1b1bbSMatthias Ringwald NVIC_EnableIRQ(XDMAC_IRQn); 22058a1b1bbSMatthias Ringwald 22158a1b1bbSMatthias Ringwald // Setup XDMA Channel for USART TX 22258a1b1bbSMatthias Ringwald xdmac_channel_set_destination_addr(XDMAC, XDMA_CH_UART_TX, (uint32_t)&BOARD_USART->US_THR); 22358a1b1bbSMatthias Ringwald xdmac_channel_set_config(XDMAC, XDMA_CH_UART_TX, 22458a1b1bbSMatthias Ringwald XDMAC_CC_TYPE_PER_TRAN | 22558a1b1bbSMatthias Ringwald XDMAC_CC_DSYNC_MEM2PER | 22658a1b1bbSMatthias Ringwald XDMAC_CC_MEMSET_NORMAL_MODE | 22758a1b1bbSMatthias Ringwald XDMAC_CC_MBSIZE_SINGLE | 22858a1b1bbSMatthias Ringwald XDMAC_CC_DWIDTH_BYTE | 22958a1b1bbSMatthias Ringwald XDMAC_CC_SIF_AHB_IF0 | 23058a1b1bbSMatthias Ringwald XDMAC_CC_DIF_AHB_IF1 | 23158a1b1bbSMatthias Ringwald XDMAC_CC_SAM_INCREMENTED_AM | 23258a1b1bbSMatthias Ringwald XDMAC_CC_DAM_FIXED_AM | 23358a1b1bbSMatthias Ringwald XDMAC_CC_CSIZE_CHK_1 | 23458a1b1bbSMatthias Ringwald XDMAC_CC_PERID(XDAMC_CHANNEL_HWID_USART0_TX) 23558a1b1bbSMatthias Ringwald ); 23658a1b1bbSMatthias Ringwald xdmac_channel_set_descriptor_control(XDMAC, XDMA_CH_UART_TX, 0); 23758a1b1bbSMatthias Ringwald xdmac_channel_set_source_microblock_stride(XDMAC, XDMA_CH_UART_TX, 0); 23858a1b1bbSMatthias Ringwald xdmac_channel_set_destination_microblock_stride(XDMAC, XDMA_CH_UART_TX, 0); 23958a1b1bbSMatthias Ringwald xdmac_channel_set_datastride_mempattern(XDMAC, XDMA_CH_UART_TX, 0); 24058a1b1bbSMatthias Ringwald xdmac_channel_set_block_control(XDMAC, XDMA_CH_UART_TX, 0); 24158a1b1bbSMatthias Ringwald xdmac_enable_interrupt(XDMAC, XDMA_CH_UART_TX); 24258a1b1bbSMatthias Ringwald xdmac_channel_enable_interrupt(XDMAC, XDMA_CH_UART_TX, XDMAC_CIE_BIE); 24358a1b1bbSMatthias Ringwald 24458a1b1bbSMatthias Ringwald // Setup XDMA Channel for USART RX 24558a1b1bbSMatthias Ringwald xdmac_channel_set_source_addr(XDMAC, XDMA_CH_UART_RX, (uint32_t)&BOARD_USART->US_RHR); 24658a1b1bbSMatthias Ringwald xdmac_channel_set_config(XDMAC, XDMA_CH_UART_RX, 24758a1b1bbSMatthias Ringwald XDMAC_CC_TYPE_PER_TRAN | 24858a1b1bbSMatthias Ringwald XDMAC_CC_DSYNC_PER2MEM | 24958a1b1bbSMatthias Ringwald XDMAC_CC_MEMSET_NORMAL_MODE | 25058a1b1bbSMatthias Ringwald XDMAC_CC_MBSIZE_SINGLE | 25158a1b1bbSMatthias Ringwald XDMAC_CC_DWIDTH_BYTE | 25258a1b1bbSMatthias Ringwald XDMAC_CC_SIF_AHB_IF1 | 25358a1b1bbSMatthias Ringwald XDMAC_CC_DIF_AHB_IF0 | 25458a1b1bbSMatthias Ringwald XDMAC_CC_SAM_FIXED_AM | 25558a1b1bbSMatthias Ringwald XDMAC_CC_DAM_INCREMENTED_AM | 25658a1b1bbSMatthias Ringwald XDMAC_CC_CSIZE_CHK_1 | 25758a1b1bbSMatthias Ringwald XDMAC_CC_PERID(XDAMC_CHANNEL_HWID_USART0_RX) 25858a1b1bbSMatthias Ringwald ); 25958a1b1bbSMatthias Ringwald xdmac_channel_set_descriptor_control(XDMAC, XDMA_CH_UART_RX, 0); 26058a1b1bbSMatthias Ringwald xdmac_channel_set_source_microblock_stride(XDMAC, XDMA_CH_UART_RX, 0); 26158a1b1bbSMatthias Ringwald xdmac_channel_set_destination_microblock_stride(XDMAC, XDMA_CH_UART_RX, 0); 26258a1b1bbSMatthias Ringwald xdmac_channel_set_datastride_mempattern(XDMAC, XDMA_CH_UART_RX, 0); 26358a1b1bbSMatthias Ringwald xdmac_channel_set_block_control(XDMAC, XDMA_CH_UART_RX, 0); 26458a1b1bbSMatthias Ringwald xdmac_enable_interrupt(XDMAC, XDMA_CH_UART_RX); 26558a1b1bbSMatthias Ringwald xdmac_channel_enable_interrupt(XDMAC, XDMA_CH_UART_RX, XDMAC_CIE_BIE); 26658a1b1bbSMatthias Ringwald #endif 26758a1b1bbSMatthias Ringwald } 26858a1b1bbSMatthias Ringwald 26958a1b1bbSMatthias Ringwald void hal_uart_dma_set_sleep(uint8_t sleep){ 27058a1b1bbSMatthias Ringwald } 27158a1b1bbSMatthias Ringwald 27258a1b1bbSMatthias Ringwald void hal_uart_dma_set_block_received( void (*the_block_handler)(void)){ 27358a1b1bbSMatthias Ringwald rx_done_handler = the_block_handler; 27458a1b1bbSMatthias Ringwald } 27558a1b1bbSMatthias Ringwald 27658a1b1bbSMatthias Ringwald void hal_uart_dma_set_block_sent( void (*the_block_handler)(void)){ 27758a1b1bbSMatthias Ringwald tx_done_handler = the_block_handler; 27858a1b1bbSMatthias Ringwald } 27958a1b1bbSMatthias Ringwald 28058a1b1bbSMatthias Ringwald void hal_uart_dma_set_csr_irq_handler( void (*the_irq_handler)(void)){ 28158a1b1bbSMatthias Ringwald cts_irq_handler = the_irq_handler; 28258a1b1bbSMatthias Ringwald } 28358a1b1bbSMatthias Ringwald 28458a1b1bbSMatthias Ringwald int hal_uart_dma_set_baud(uint32_t baud){ 28558a1b1bbSMatthias Ringwald /* Disable TX & RX function. */ 28658a1b1bbSMatthias Ringwald usart_disable_tx(BOARD_USART); 28758a1b1bbSMatthias Ringwald usart_disable_rx(BOARD_USART); 28858a1b1bbSMatthias Ringwald uint32_t res = usart_set_async_baudrate(BOARD_USART, baud, sysclk_get_peripheral_hz()); 28958a1b1bbSMatthias Ringwald if (res){ 29058a1b1bbSMatthias Ringwald log_error("hal_uart_dma_set_baud library call failed"); 29158a1b1bbSMatthias Ringwald } 29258a1b1bbSMatthias Ringwald 29358a1b1bbSMatthias Ringwald /* Enable TX & RX function. */ 29458a1b1bbSMatthias Ringwald usart_enable_tx(BOARD_USART); 29558a1b1bbSMatthias Ringwald usart_enable_rx(BOARD_USART); 29658a1b1bbSMatthias Ringwald 29713b8e9b1SMatthias Ringwald log_info("set baud rate %u", (int) baud); 29813b8e9b1SMatthias Ringwald return 0; 29913b8e9b1SMatthias Ringwald } 30058a1b1bbSMatthias Ringwald 301*c267d18bSMatthias Ringwald int hal_uart_dma_set_flowcontrol(int flowcontrol){ 302*c267d18bSMatthias Ringwald simulate_flowcontrol = flowcontrol; 303*c267d18bSMatthias Ringwald if (flowcontrol){ 304*c267d18bSMatthias Ringwald /* Set hardware handshaking mode. */ 305*c267d18bSMatthias Ringwald BOARD_USART->US_MR = (BOARD_USART->US_MR & ~US_MR_USART_MODE_Msk) | US_MR_USART_MODE_HW_HANDSHAKING; 306*c267d18bSMatthias Ringwald hal_uart_rts_low(); 307*c267d18bSMatthias Ringwald } else { 308*c267d18bSMatthias Ringwald /* Set nomal mode. */ 309*c267d18bSMatthias Ringwald BOARD_USART->US_MR = (BOARD_USART->US_MR & ~US_MR_USART_MODE_Msk) | US_MR_USART_MODE_NORMAL; 310*c267d18bSMatthias Ringwald // Set RTS = 0 (normal mode) 311*c267d18bSMatthias Ringwald BOARD_USART->US_CR = US_CR_RTSEN; 312*c267d18bSMatthias Ringwald } 31358a1b1bbSMatthias Ringwald return 0; 31458a1b1bbSMatthias Ringwald } 31558a1b1bbSMatthias Ringwald 31658a1b1bbSMatthias Ringwald void hal_uart_dma_send_block(const uint8_t *data, uint16_t size){ 31713b8e9b1SMatthias Ringwald // log_info("send %u", size); 31813b8e9b1SMatthias Ringwald // log_info_hexdump(data, size); 31958a1b1bbSMatthias Ringwald #ifdef USE_XDMAC_FOR_USART 32058a1b1bbSMatthias Ringwald xdmac_channel_get_interrupt_status( XDMAC, XDMA_CH_UART_TX); 32158a1b1bbSMatthias Ringwald xdmac_channel_set_source_addr(XDMAC, XDMA_CH_UART_TX, (uint32_t)data); 32258a1b1bbSMatthias Ringwald xdmac_channel_set_microblock_control(XDMAC, XDMA_CH_UART_TX, size); 32358a1b1bbSMatthias Ringwald xdmac_channel_enable(XDMAC, XDMA_CH_UART_TX); 32458a1b1bbSMatthias Ringwald #else 32558a1b1bbSMatthias Ringwald tx_buffer_ptr = (uint8_t *) data; 32658a1b1bbSMatthias Ringwald bytes_to_write = size; 32758a1b1bbSMatthias Ringwald usart_enable_interrupt(BOARD_USART, US_IER_TXRDY); 32858a1b1bbSMatthias Ringwald #endif 32958a1b1bbSMatthias Ringwald } 33058a1b1bbSMatthias Ringwald 33158a1b1bbSMatthias Ringwald void hal_uart_dma_receive_block(uint8_t *data, uint16_t size){ 33258a1b1bbSMatthias Ringwald 33358a1b1bbSMatthias Ringwald hal_uart_rts_low(); 33458a1b1bbSMatthias Ringwald 33558a1b1bbSMatthias Ringwald #ifdef USE_XDMAC_FOR_USART 33658a1b1bbSMatthias Ringwald xdmac_channel_get_interrupt_status( XDMAC, XDMA_CH_UART_RX); 33758a1b1bbSMatthias Ringwald xdmac_channel_set_destination_addr(XDMAC, XDMA_CH_UART_RX, (uint32_t)data); 33858a1b1bbSMatthias Ringwald xdmac_channel_set_microblock_control(XDMAC, XDMA_CH_UART_RX, size); 33958a1b1bbSMatthias Ringwald xdmac_channel_enable(XDMAC, XDMA_CH_UART_RX); 34058a1b1bbSMatthias Ringwald #else 34158a1b1bbSMatthias Ringwald rx_buffer_ptr = data; 34258a1b1bbSMatthias Ringwald bytes_to_read = size; 34358a1b1bbSMatthias Ringwald usart_enable_interrupt(BOARD_USART, US_IER_RXRDY); 34458a1b1bbSMatthias Ringwald #endif 34558a1b1bbSMatthias Ringwald } 34658a1b1bbSMatthias Ringwald 34758a1b1bbSMatthias Ringwald #ifdef USE_XDMAC_FOR_USART 34858a1b1bbSMatthias Ringwald void XDMAC_Handler(void) 34958a1b1bbSMatthias Ringwald { 35058a1b1bbSMatthias Ringwald uint32_t dma_status; 35158a1b1bbSMatthias Ringwald dma_status = xdmac_channel_get_interrupt_status(XDMAC, XDMA_CH_UART_TX); 35258a1b1bbSMatthias Ringwald if (dma_status & XDMAC_CIS_BIS) { 35358a1b1bbSMatthias Ringwald tx_done_handler(); 35458a1b1bbSMatthias Ringwald } 35558a1b1bbSMatthias Ringwald dma_status = xdmac_channel_get_interrupt_status(XDMAC, XDMA_CH_UART_RX); 35658a1b1bbSMatthias Ringwald if (dma_status & XDMAC_CIS_BIS) { 35758a1b1bbSMatthias Ringwald hal_uart_rts_high(); 35858a1b1bbSMatthias Ringwald rx_done_handler(); 35958a1b1bbSMatthias Ringwald } 36058a1b1bbSMatthias Ringwald } 36158a1b1bbSMatthias Ringwald #else 36258a1b1bbSMatthias Ringwald void USART_Handler(void) 36358a1b1bbSMatthias Ringwald { 36458a1b1bbSMatthias Ringwald uint32_t ul_status; 36558a1b1bbSMatthias Ringwald 36658a1b1bbSMatthias Ringwald /* Read USART status. */ 36758a1b1bbSMatthias Ringwald ul_status = usart_get_status(BOARD_USART); 36858a1b1bbSMatthias Ringwald 36958a1b1bbSMatthias Ringwald // handle ready to send 37058a1b1bbSMatthias Ringwald if(ul_status & US_IER_TXRDY) { 37158a1b1bbSMatthias Ringwald if (bytes_to_write){ 37258a1b1bbSMatthias Ringwald // send next byte 37358a1b1bbSMatthias Ringwald usart_write(BOARD_USART, *tx_buffer_ptr); 37458a1b1bbSMatthias Ringwald tx_buffer_ptr++; 37558a1b1bbSMatthias Ringwald bytes_to_write--; 37658a1b1bbSMatthias Ringwald } else { 37758a1b1bbSMatthias Ringwald // done. disable tx ready interrupt to avoid starvation here 37858a1b1bbSMatthias Ringwald usart_disable_interrupt(BOARD_USART, US_IER_TXRDY); 37958a1b1bbSMatthias Ringwald tx_done_handler(); 38058a1b1bbSMatthias Ringwald } 38158a1b1bbSMatthias Ringwald } 38258a1b1bbSMatthias Ringwald 38358a1b1bbSMatthias Ringwald // handle byte available for read 38458a1b1bbSMatthias Ringwald if (ul_status & US_IER_RXRDY) { 38558a1b1bbSMatthias Ringwald uint32_t ch; 38658a1b1bbSMatthias Ringwald usart_read(BOARD_USART, (uint32_t *)&ch); 38758a1b1bbSMatthias Ringwald *rx_buffer_ptr++ = ch; 38858a1b1bbSMatthias Ringwald bytes_to_read--; 38958a1b1bbSMatthias Ringwald if (bytes_to_read == 0){ 39058a1b1bbSMatthias Ringwald // done. disable rx ready interrupt, raise RTS 39158a1b1bbSMatthias Ringwald hal_uart_rts_high(); 39258a1b1bbSMatthias Ringwald usart_disable_interrupt(BOARD_USART, US_IER_RXRDY); 39358a1b1bbSMatthias Ringwald rx_done_handler(); 39458a1b1bbSMatthias Ringwald } 39558a1b1bbSMatthias Ringwald } 39658a1b1bbSMatthias Ringwald } 39758a1b1bbSMatthias Ringwald #endif 39858a1b1bbSMatthias Ringwald 39958a1b1bbSMatthias Ringwald void hal_tick_init() 40058a1b1bbSMatthias Ringwald { 40158a1b1bbSMatthias Ringwald /* Configure systick for 1 ms */ 40258a1b1bbSMatthias Ringwald puts("Configure system tick to get 1ms tick period.\r"); 40358a1b1bbSMatthias Ringwald if (SysTick_Config(sysclk_get_cpu_hz() / 1000)) { 40458a1b1bbSMatthias Ringwald puts("-F- Systick configuration error\r"); 40558a1b1bbSMatthias Ringwald while (1); 40658a1b1bbSMatthias Ringwald } 40758a1b1bbSMatthias Ringwald } 40858a1b1bbSMatthias Ringwald 40958a1b1bbSMatthias Ringwald void hal_tick_set_handler(void (*handler)(void)){ 41058a1b1bbSMatthias Ringwald if (handler == NULL){ 41158a1b1bbSMatthias Ringwald tick_handler = &dummy_handler; 41258a1b1bbSMatthias Ringwald return; 41358a1b1bbSMatthias Ringwald } 41458a1b1bbSMatthias Ringwald tick_handler = handler; 41558a1b1bbSMatthias Ringwald } 41658a1b1bbSMatthias Ringwald 41758a1b1bbSMatthias Ringwald int hal_tick_get_tick_period_in_ms(void){ 41858a1b1bbSMatthias Ringwald return 1; 41958a1b1bbSMatthias Ringwald } 42058a1b1bbSMatthias Ringwald 42113b8e9b1SMatthias Ringwald static const btstack_uart_block_t * uart_driver; 42213b8e9b1SMatthias Ringwald 42313b8e9b1SMatthias Ringwald static void phase2(int status){ 42413b8e9b1SMatthias Ringwald 42513b8e9b1SMatthias Ringwald if (status){ 42613b8e9b1SMatthias Ringwald printf("Download firmware failed\n"); 42713b8e9b1SMatthias Ringwald return; 42813b8e9b1SMatthias Ringwald } 42913b8e9b1SMatthias Ringwald 43013b8e9b1SMatthias Ringwald printf("Phase 2: Main app\n"); 43113b8e9b1SMatthias Ringwald 43213b8e9b1SMatthias Ringwald // init HCI 43313b8e9b1SMatthias Ringwald const hci_transport_t * transport = hci_transport_h4_instance(uart_driver); 43413b8e9b1SMatthias Ringwald // const btstack_link_key_db_t * link_key_db = btstack_link_key_db_fs_instance(); 43513b8e9b1SMatthias Ringwald hci_init(transport, (void*) &transport_config); 43613b8e9b1SMatthias Ringwald hci_set_chipset(btstack_chipset_atwilc3000_instance()); 43713b8e9b1SMatthias Ringwald // hci_set_link_key_db(link_key_db); 43813b8e9b1SMatthias Ringwald 43913b8e9b1SMatthias Ringwald // setup app 44013b8e9b1SMatthias Ringwald btstack_main(0, NULL); 44113b8e9b1SMatthias Ringwald } 44258a1b1bbSMatthias Ringwald 4431b2596b5SMatthias Ringwald /** 4441b2596b5SMatthias Ringwald * \brief getting-started Application entry point. 4451b2596b5SMatthias Ringwald * 4461b2596b5SMatthias Ringwald * \return Unused (ANSI-C compatibility). 4471b2596b5SMatthias Ringwald */ 4481b2596b5SMatthias Ringwald // [main] 4491b2596b5SMatthias Ringwald int main(void) 4501b2596b5SMatthias Ringwald { 4511b2596b5SMatthias Ringwald /* Initialize the SAM system */ 4521b2596b5SMatthias Ringwald sysclk_init(); 4531b2596b5SMatthias Ringwald board_init(); 4541b2596b5SMatthias Ringwald 4551b2596b5SMatthias Ringwald /* Initialize the console uart */ 4561b2596b5SMatthias Ringwald configure_console(); 4571b2596b5SMatthias Ringwald 45813b8e9b1SMatthias Ringwald /* Output boot info */ 45913b8e9b1SMatthias Ringwald printf("BTstack on SAMV71 Xplained Ultra with ATWILC3000\n"); 46058a1b1bbSMatthias Ringwald printf("CPU %lu hz, peripheral clock %lu hz\n", sysclk_get_cpu_hz(), sysclk_get_peripheral_hz()); 46113b8e9b1SMatthias Ringwald #ifdef USE_XDMAC_FOR_USART 46213b8e9b1SMatthias Ringwald printf("Using XDMA for Bluetooth UART\n"); 46313b8e9b1SMatthias Ringwald #else 46413b8e9b1SMatthias Ringwald printf("Using IRQ driver for Bluetooth UART\n"); 46513b8e9b1SMatthias Ringwald #endif 46613b8e9b1SMatthias Ringwald printf("--\n"); 46758a1b1bbSMatthias Ringwald 46858a1b1bbSMatthias Ringwald // start with BTstack init - especially configure HCI Transport 46958a1b1bbSMatthias Ringwald btstack_memory_init(); 47058a1b1bbSMatthias Ringwald btstack_run_loop_init(btstack_run_loop_embedded_get_instance()); 47158a1b1bbSMatthias Ringwald 47258a1b1bbSMatthias Ringwald // enable full log output while porting 47313b8e9b1SMatthias Ringwald hci_dump_open(NULL, HCI_DUMP_STDOUT); 47458a1b1bbSMatthias Ringwald 47513b8e9b1SMatthias Ringwald // setup UART HAL + Run Loop integration 47613b8e9b1SMatthias Ringwald uart_driver = btstack_uart_block_embedded_instance(); 47758a1b1bbSMatthias Ringwald 47813b8e9b1SMatthias Ringwald // extract UART config from transport config, but disable flow control 47913b8e9b1SMatthias Ringwald uart_config.baudrate = transport_config.baudrate_init; 48013b8e9b1SMatthias Ringwald uart_config.flowcontrol = 0; 48113b8e9b1SMatthias Ringwald uart_config.device_name = transport_config.device_name; 48213b8e9b1SMatthias Ringwald uart_driver->init(&uart_config); 48358a1b1bbSMatthias Ringwald 48413b8e9b1SMatthias Ringwald // phase #1 download firmware 48513b8e9b1SMatthias Ringwald printf("Phase 1: Download firmware\n"); 48613b8e9b1SMatthias Ringwald 48713b8e9b1SMatthias Ringwald // phase #2 start main app 48813b8e9b1SMatthias Ringwald btstack_chipset_atwilc3000_download_firmware(uart_driver, transport_config.baudrate_main, transport_config.flowcontrol, atwilc3000_fw_data, atwilc3000_fw_size, &phase2); 48958a1b1bbSMatthias Ringwald 49058a1b1bbSMatthias Ringwald // go 49158a1b1bbSMatthias Ringwald btstack_run_loop_execute(); 49258a1b1bbSMatthias Ringwald 49358a1b1bbSMatthias Ringwald // compiler happy 4941b2596b5SMatthias Ringwald while(1); 4951b2596b5SMatthias Ringwald } 4961b2596b5SMatthias Ringwald #ifdef __cplusplus 4971b2596b5SMatthias Ringwald } 4981b2596b5SMatthias Ringwald #endif 499