samv71-xplained-atwilc3000: add BTstack files

This commit is contained in:
Matthias Ringwald 2017-07-26 19:19:33 +02:00
parent d344c38bba
commit 58a1b1bbd7
2 changed files with 586 additions and 437 deletions

View File

@ -52,6 +52,7 @@ TARGET_SRAM = getting-started_sram.elf
# List of C source files. # List of C source files.
CSRCS = \ CSRCS = \
../main \
common/services/clock/samv71/sysclk.c \ common/services/clock/samv71/sysclk.c \
common/services/serial/usart_serial.c \ common/services/serial/usart_serial.c \
common/utils/interrupt/interrupt_sam_nvic.c \ common/utils/interrupt/interrupt_sam_nvic.c \
@ -70,7 +71,6 @@ CSRCS = \
sam/utils/cmsis/samv71/source/templates/gcc/startup_samv71.c \ sam/utils/cmsis/samv71/source/templates/gcc/startup_samv71.c \
sam/utils/cmsis/samv71/source/templates/system_samv71.c \ sam/utils/cmsis/samv71/source/templates/system_samv71.c \
sam/utils/syscalls/gcc/syscalls.c \ sam/utils/syscalls/gcc/syscalls.c \
../main \
# List of assembler source files. # List of assembler source files.
ASSRCS = ASSRCS =
@ -81,6 +81,7 @@ INC_PATH = \
common/services/clock \ common/services/clock \
common/services/gpio \ common/services/gpio \
common/services/ioport \ common/services/ioport \
common/services/delay \
common/services/serial \ common/services/serial \
common/services/serial/sam_uart \ common/services/serial/sam_uart \
common/utils \ common/utils \
@ -96,6 +97,7 @@ INC_PATH = \
sam/drivers/tc \ sam/drivers/tc \
sam/drivers/uart \ sam/drivers/uart \
sam/drivers/usart \ sam/drivers/usart \
sam/drivers/xdmac \
sam/utils \ sam/utils \
sam/utils/cmsis/samv71/include \ sam/utils/cmsis/samv71/include \
sam/utils/cmsis/samv71/source/templates \ sam/utils/cmsis/samv71/source/templates \
@ -106,6 +108,51 @@ INC_PATH = \
thirdparty/CMSIS/Lib/GCC \ thirdparty/CMSIS/Lib/GCC \
.. ..
BTSTACK_ROOT = ../../..
INC_PATH += ${BTSTACK_ROOT}/src/ble
INC_PATH += ${BTSTACK_ROOT}/src/ble/gatt-service
INC_PATH += ${BTSTACK_ROOT}/src/classic
INC_PATH += ${BTSTACK_ROOT}/src
INC_PATH += ${BTSTACK_ROOT}/3rd-party/micro-ecc
INC_PATH += ${BTSTACK_ROOT}/platform/embedded
INC_PATH += ${BTSTACK_ROOT}/chipset/atwilc3000
# VPATH += ${BTSTACK_ROOT}/src
# VPATH += ${BTSTACK_ROOT}/src/ble
# VPATH += ${BTSTACK_ROOT}/src/ble/gatt-service
# VPATH += ${BTSTACK_ROOT}/src/classic
# VPATH += ${BTSTACK_ROOT}/platform/embedded
# VPATH += ${BTSTACK_ROOT}/example
# VPATH += ${BTSTACK_ROOT}/3rd-party/micro-ecc
CSRCS += \
${BTSTACK_ROOT}/src/ad_parser.c \
${BTSTACK_ROOT}/src/ble/ancs_client.c \
${BTSTACK_ROOT}/src/ble/att_db.c \
${BTSTACK_ROOT}/src/ble/att_dispatch.c \
${BTSTACK_ROOT}/src/ble/att_server.c \
${BTSTACK_ROOT}/src/ble/gatt-service/battery_service_server.c \
${BTSTACK_ROOT}/src/btstack_linked_list.c \
${BTSTACK_ROOT}/src/btstack_memory.c \
${BTSTACK_ROOT}/src/btstack_memory_pool.c \
${BTSTACK_ROOT}/src/btstack_ring_buffer.c \
${BTSTACK_ROOT}/src/btstack_run_loop.c \
${BTSTACK_ROOT}/platform/embedded/btstack_run_loop_embedded.c \
${BTSTACK_ROOT}/platform/embedded/btstack_uart_block_embedded.c \
${BTSTACK_ROOT}/src/btstack_util.c \
${BTSTACK_ROOT}/src/ble/gatt-service/device_information_service_server.c \
${BTSTACK_ROOT}/src/ble/gatt_client.c \
${BTSTACK_ROOT}/src/hci.c \
${BTSTACK_ROOT}/src/hci_cmd.c \
${BTSTACK_ROOT}/src/hci_dump.c \
${BTSTACK_ROOT}/src/hci_transport_h4.c \
${BTSTACK_ROOT}/src/l2cap.c \
${BTSTACK_ROOT}/src/l2cap_signaling.c \
${BTSTACK_ROOT}/src/ble/le_device_db_memory.c \
${BTSTACK_ROOT}/src/ble/sm.c \
${BTSTACK_ROOT}/3rd-party/micro-ecc/uECC.c \
${BTSTACK_ROOT}/example/led_counter.c \
# Additional search paths for libraries. # Additional search paths for libraries.
LIB_PATH = \ LIB_PATH = \
thirdparty/CMSIS/Lib/GCC thirdparty/CMSIS/Lib/GCC

View File

@ -1,436 +1,538 @@
/** /**
* \file * \file
* *
* \brief Getting Started Application. * \brief Getting Started Application.
* *
* Copyright (c) 2011-2016 Atmel Corporation. All rights reserved. * Copyright (c) 2011-2015 Atmel Corporation. All rights reserved.
* *
* \asf_license_start * \asf_license_start
* *
* \page License * \page License
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met: * modification, are permitted provided that the following conditions are met:
* *
* 1. Redistributions of source code must retain the above copyright notice, * 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer. * this list of conditions and the following disclaimer.
* *
* 2. Redistributions in binary form must reproduce the above copyright notice, * 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation * this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution. * and/or other materials provided with the distribution.
* *
* 3. The name of Atmel may not be used to endorse or promote products derived * 3. The name of Atmel may not be used to endorse or promote products derived
* from this software without specific prior written permission. * from this software without specific prior written permission.
* *
* 4. This software may only be redistributed and used in connection with an * 4. This software may only be redistributed and used in connection with an
* Atmel microcontroller product. * Atmel microcontroller product.
* *
* THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
* EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE. * POSSIBILITY OF SUCH DAMAGE.
* *
* \asf_license_stop * \asf_license_stop
* *
*/ */
/** /**
* \mainpage Getting Started Application * \mainpage Getting Started Application
* *
* \section Purpose * \section Purpose
* *
* The Getting Started example will help new users get familiar with Atmel's * BTstack port for SAM MCUs
* SAM family of microcontrollers. This basic application shows the startup *
* sequence of a chip and how to use its core peripherals. * \section Requirements
* *
* \section Requirements * This package can be used with SAM evaluation kits.
* *
* This package can be used with SAM evaluation kits. * \section Description
* *
* \section Description *
* * \section Usage
* The demonstration program makes the LED(s) on the board blink at a fixed rate. *
* This rate is generated by using Time tick timer. The blinking can be stopped * -# Build the program and download it inside the evaluation board.
* using the push button. * -# On the computer, open and configure a terminal application
* * (e.g. HyperTerminal on Microsoft Windows) with these settings:
* \section Usage * - 115200 bauds
* * - 8 bits of data
* -# Build the program and download it inside the evaluation board. * - No parity
* -# On the computer, open and configure a terminal application * - 1 stop bit
* (e.g. HyperTerminal on Microsoft Windows) with these settings: * - No flow control
* - 115200 bauds * -# Start the application.
* - 8 bits of data *
* - No parity */
* - 1 stop bit
* - No flow control #include "asf.h"
* -# Start the application. #include "stdio_serial.h"
* -# The LED(s) should start blinking on the board. In the terminal window, the #include "conf_board.h"
* following text should appear (values depend on the board and chip used): #include "conf_clock.h"
* \code
-- Getting Started Example xxx -- // BTstack
-- xxxxxx-xx #include "btstack_run_loop.h"
-- Compiled: xxx xx xxxx xx:xx:xx -- #include "btstack_run_loop_embedded.h"
\endcode #include "btstack_debug.h"
* -# Pressing and release button 1 should make one LED stop & restart #include "hci.h"
* blinking. #include "hci_dump.h"
* -# If the button 2 available, pressing button 2 should make the other LED #include "btstack_chipset_atwilc3000.h"
* stop & restart blinking. #include "btstack_memory.h"
* #include "classic/btstack_link_key_db.h"
*/
/* #include "hal_uart_dma.h"
* Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a> #include "hal_cpu.h"
*/ #include "hal_tick.h"
#include "asf.h" extern int btstack_main(int argc, const char * argv[]);
#include "stdio_serial.h"
#include "conf_board.h" #define USE_XDMAC_FOR_USART
#include "conf_clock.h" #define XDMA_CH_UART_TX 0
#define XDMA_CH_UART_RX 1
/** IRQ priority for PIO (The lower the value, the greater the priority) */
// [main_def_pio_irq_prior] #define STRING_EOL "\r"
#define IRQ_PRIOR_PIO 0 #define STRING_HEADER "-- Getting Started Example --\r\n" \
// [main_def_pio_irq_prior] "-- "BOARD_NAME" --\r\n" \
"-- Compiled: "__DATE__" "__TIME__" --"STRING_EOL
/** LED0 blink time, LED1 blink half this time, in ms */
#define BLINK_PERIOD 1000 /** All interrupt mask. */
#define ALL_INTERRUPT_MASK 0xffffffff
#define STRING_EOL "\r"
#define STRING_HEADER "-- Getting Started Example --\r\n" \ static void dummy_handler(void){}
"-- "BOARD_NAME" --\r\n" \ static void (*tick_handler)(void) = &dummy_handler;
"-- Compiled: "__DATE__" "__TIME__" --"STRING_EOL
/** when porting to different setup, please
/** LED0 blinking control. */ * disable baudrate change (use 0 instead of 4000000)
// [main_var_led0_control] * and don't enable eHCILL mode (comment line below)
volatile bool g_b_led0_active = true; */
// [main_var_led0_control]
// after HCI Reset, use 115200. Then increase baud rate to X.
#ifdef LED1_GPIO static hci_transport_config_uart_t hci_transport_config = {
/** LED1 blinking control. */ HCI_TRANSPORT_CONFIG_UART,
// [main_var_led1_control] 115200,
volatile bool g_b_led1_active = true; 4000000, // use 0 to skip baud rate change from 115200 to X for debugging purposes
// [main_var_led1_control] 1, // flow control
#endif NULL,
};
/** Global g_ul_ms_ticks in milliseconds since start of application */ /// @cond 0
// [main_var_ticks] /**INDENT-OFF**/
volatile uint32_t g_ul_ms_ticks = 0; #ifdef __cplusplus
// [main_var_ticks] extern "C" {
#endif
/// @cond 0 /**INDENT-ON**/
/**INDENT-OFF**/ /// @endcond
#ifdef __cplusplus
extern "C" { /**
#endif * \brief Handler for System Tick interrupt.
/**INDENT-ON**/ */
/// @endcond void SysTick_Handler(void)
{
/** tick_handler();
* \brief Process Buttons Events }
*
* Change active states of LEDs when corresponding button events happened. /**
*/ * Configure UART console.
static void ProcessButtonEvt(uint8_t uc_button) */
{ // [main_console_configure]
// [main_button1_evnt_process] static void configure_console(void)
if (uc_button == 0) { {
g_b_led0_active = !g_b_led0_active; const usart_serial_options_t uart_serial_options = {
if (!g_b_led0_active) { .baudrate = CONF_UART_BAUDRATE,
ioport_set_pin_level(LED0_GPIO, IOPORT_PIN_LEVEL_HIGH); #ifdef CONF_UART_CHAR_LENGTH
} .charlength = CONF_UART_CHAR_LENGTH,
} #endif
// [main_button1_evnt_process] .paritytype = CONF_UART_PARITY,
#ifdef LED1_GPIO #ifdef CONF_UART_STOP_BITS
else { .stopbits = CONF_UART_STOP_BITS,
// [main_button2_evnt_process] #endif
g_b_led1_active = !g_b_led1_active; };
/* Enable LED#2 and TC if they were enabled */ /* Configure console UART. */
if (g_b_led1_active) { sysclk_enable_peripheral_clock(CONSOLE_UART_ID);
ioport_set_pin_level(LED1_GPIO, IOPORT_PIN_LEVEL_LOW); stdio_serial_init(CONF_UART, &uart_serial_options);
tc_start(TC0, 0); }
}
/* Disable LED#2 and TC if they were disabled */ // [main_console_configure]
else {
ioport_set_pin_level(LED1_GPIO, IOPORT_PIN_LEVEL_HIGH); /**
tc_stop(TC0, 0); * \brief Wait for the given number of milliseconds (ticks
} * generated by the SAM's microcontrollers's system tick).
// [main_button2_evnt_process] *
} * \param ul_dly_ticks Delay to wait for, in milliseconds.
#endif */
} // [main_ms_delay]
static void mdelay(uint32_t delay_in_ms)
/** {
* \brief Handler for System Tick interrupt. // delay_ms(delay_in_ms);
* uint32_t time_to_wait = btstack_run_loop_get_time_ms() + delay_in_ms;
* Process System Tick Event while (btstack_run_loop_get_time_ms() < time_to_wait);
* Increments the g_ul_ms_ticks counter. }
*/ // [main_ms_delay]
// [main_systick_handler]
void SysTick_Handler(void) ////////////////////////////////////////////////////////////////////////////////
{ // hal_cpu.h implementation
g_ul_ms_ticks++; ////////////////////////////////////////////////////////////////////////////////
} // hal_led.h implementation
// [main_systick_handler] #include "hal_led.h"
void hal_led_off(void);
/** void hal_led_on(void);
* \brief Handler for Button 1 rising edge interrupt.
* void hal_led_off(void){
* Handle process led1 status change. // gpio_set_pin_low(GPIOA, GPIO_LED2);
*/ }
// [main_button1_handler] void hal_led_on(void){
static void Button1_Handler(uint32_t id, uint32_t mask) // gpio_set_pin_high(GPIOA, GPIO_LED2);
{ }
if (PIN_PUSHBUTTON_1_ID == id && PIN_PUSHBUTTON_1_MASK == mask) { void hal_led_toggle(void){
ProcessButtonEvt(0); // gpio_toggle_pin(GPIOA, GPIO_LED2);
} }
}
// [main_button1_handler] // hal_cpu.h implementation
#include "hal_cpu.h"
#ifndef BOARD_NO_PUSHBUTTON_2
/** void hal_cpu_disable_irqs(void){
* \brief Handler for Button 2 falling edge interrupt. //__disable_irq();
* }
* Handle process led2 status change.
*/ void hal_cpu_enable_irqs(void){
// [main_button2_handler] // __enable_irq();
static void Button2_Handler(uint32_t id, uint32_t mask) }
{
if (PIN_PUSHBUTTON_2_ID == id && PIN_PUSHBUTTON_2_MASK == mask) { void hal_cpu_enable_irqs_and_sleep(void){
ProcessButtonEvt(1); hal_led_off();
} // __enable_irq();
} // __asm__("wfe"); // go to sleep if event flag isn't set. if set, just clear it. IRQs set event flag
// [main_button2_handler]
#endif // note: hal_uart_needed_during_sleep can be used to disable peripheral clock if it's not needed for a timer
hal_led_on();
/** }
* \brief Configure the Pushbuttons
*
* Configure the PIO as inputs and generate corresponding interrupt when #ifndef USE_XDMAC_FOR_USART
* pressed or released. // RX state
*/ static volatile uint16_t bytes_to_read = 0;
static void configure_buttons(void) static volatile uint8_t * rx_buffer_ptr = 0;
{
// [main_button1_configure] // TX state
/* Configure Pushbutton 1 */ static volatile uint16_t bytes_to_write = 0;
pmc_enable_periph_clk(PIN_PUSHBUTTON_1_ID); static volatile uint8_t * tx_buffer_ptr = 0;
pio_set_debounce_filter(PIN_PUSHBUTTON_1_PIO, PIN_PUSHBUTTON_1_MASK, 10); #endif
/* Interrupt on rising edge */
pio_handler_set(PIN_PUSHBUTTON_1_PIO, PIN_PUSHBUTTON_1_ID, // handlers
PIN_PUSHBUTTON_1_MASK, PIN_PUSHBUTTON_1_ATTR, Button1_Handler); static void (*rx_done_handler)(void) = dummy_handler;
NVIC_EnableIRQ((IRQn_Type) PIN_PUSHBUTTON_1_ID); static void (*tx_done_handler)(void) = dummy_handler;
pio_handler_set_priority(PIN_PUSHBUTTON_1_PIO, static void (*cts_irq_handler)(void) = dummy_handler;
(IRQn_Type) PIN_PUSHBUTTON_1_ID, IRQ_PRIOR_PIO);
pio_enable_interrupt(PIN_PUSHBUTTON_1_PIO, PIN_PUSHBUTTON_1_MASK); // @note While the Atmel SAM S7x data sheet states
// [main_button1_configure] // "The hardware handshaking feature enables an out-of-band flow control by automatic management
#ifndef BOARD_NO_PUSHBUTTON_2 // of the pins RTS and CTS.",
// [main_button2_configure] // I didn't see RTS going up automatically up, ever. So, at least for RTS, the automatic management
/* Configure Pushbutton 2 */ // is just a glorified GPIO pin control feature, which provides no benefit, but irritates a lot
pmc_enable_periph_clk(PIN_PUSHBUTTON_2_ID);
pio_set_debounce_filter(PIN_PUSHBUTTON_2_PIO, PIN_PUSHBUTTON_2_MASK, 10); static void hal_uart_rts_high(void){
/* Interrupt on falling edge */ BOARD_USART->US_CR = US_CR_RTSEN;
pio_handler_set(PIN_PUSHBUTTON_2_PIO, PIN_PUSHBUTTON_2_ID, }
PIN_PUSHBUTTON_2_MASK, PIN_PUSHBUTTON_2_ATTR, Button2_Handler); static void hal_uart_rts_low(void){
NVIC_EnableIRQ((IRQn_Type) PIN_PUSHBUTTON_2_ID); BOARD_USART->US_CR = US_CR_RTSDIS;
pio_handler_set_priority(PIN_PUSHBUTTON_2_PIO, }
(IRQn_Type) PIN_PUSHBUTTON_2_ID, IRQ_PRIOR_PIO);
pio_enable_interrupt(PIN_PUSHBUTTON_2_PIO, PIN_PUSHBUTTON_2_MASK); /**
// [main_button2_configure] */
#endif void hal_uart_dma_init(void)
} {
// configure n_shutdown pin, and reset Bluetooth
/** ioport_set_pin_dir(N_SHUTDOWN, IOPORT_DIR_OUTPUT);
* Interrupt handler for TC0 interrupt. Toggles the state of LED\#2. ioport_set_pin_level(N_SHUTDOWN, IOPORT_PIN_LEVEL_LOW);
*/ mdelay(100);
// [main_tc0_handler] ioport_set_pin_level(N_SHUTDOWN, IOPORT_PIN_LEVEL_HIGH);
#ifndef BOARD_NO_LED_1 mdelay(500);
void TC0_Handler(void)
{ // configure Bluetooth USART
volatile uint32_t ul_dummy; const sam_usart_opt_t bluetooth_settings = {
115200,
/* Clear status bit to acknowledge interrupt */ US_MR_CHRL_8_BIT,
ul_dummy = tc_get_status(TC0, 0); US_MR_PAR_NO,
US_MR_NBSTOP_1_BIT,
/* Avoid compiler warning */ US_MR_CHMODE_NORMAL,
UNUSED(ul_dummy); /* This field is only used in IrDA mode. */
0
#ifdef LED1_GPIO };
/** Toggle LED state. */
ioport_toggle_pin_level(LED1_GPIO); /* Enable the peripheral clock in the PMC. */
#endif sysclk_enable_peripheral_clock(BOARD_ID_USART);
printf("2 "); /* Configure USART mode. */
} usart_init_hw_handshaking(BOARD_USART, &bluetooth_settings, sysclk_get_peripheral_hz());
// [main_tc0_handler]
/* Disable all the interrupts. */
/** usart_disable_interrupt(BOARD_USART, ALL_INTERRUPT_MASK);
* Configure Timer Counter 0 to generate an interrupt every 250ms.
*/ // RX not ready yet
// [main_tc_configure] // usart_drive_RTS_pin_high(BOARD_USART);
static void configure_tc(void)
{ /* Enable TX & RX function. */
uint32_t ul_div; usart_enable_tx(BOARD_USART);
uint32_t ul_tcclks; usart_enable_rx(BOARD_USART);
uint32_t ul_sysclk = sysclk_get_cpu_hz();
/* Configure and enable interrupt of USART. */
/* Configure PMC */ NVIC_EnableIRQ(USART_IRQn);
pmc_enable_periph_clk(ID_TC0);
#if SAMG55 #ifdef USE_XDMAC_FOR_USART
/* Enable PCK output */
pmc_disable_pck(PMC_PCK_3); // setup XDMAC
pmc_switch_pck_to_sclk(PMC_PCK_3, PMC_PCK_PRES(0));
pmc_enable_pck(PMC_PCK_3); /* Initialize and enable DMA controller */
#endif pmc_enable_periph_clk(ID_XDMAC);
/** Configure TC for a 4Hz frequency and trigger on RC compare. */ /* Enable XDMA interrupt */
tc_find_mck_divisor(4, ul_sysclk, &ul_div, &ul_tcclks, ul_sysclk); NVIC_ClearPendingIRQ(XDMAC_IRQn);
tc_init(TC0, 0, ul_tcclks | TC_CMR_CPCTRG); NVIC_SetPriority( XDMAC_IRQn ,1);
tc_write_rc(TC0, 0, (ul_sysclk / ul_div) / 4); NVIC_EnableIRQ(XDMAC_IRQn);
/* Configure and enable interrupt on RC compare */ // Setup XDMA Channel for USART TX
NVIC_EnableIRQ((IRQn_Type) ID_TC0); xdmac_channel_set_destination_addr(XDMAC, XDMA_CH_UART_TX, (uint32_t)&BOARD_USART->US_THR);
tc_enable_interrupt(TC0, 0, TC_IER_CPCS); xdmac_channel_set_config(XDMAC, XDMA_CH_UART_TX,
XDMAC_CC_TYPE_PER_TRAN |
#ifdef LED1_GPIO XDMAC_CC_DSYNC_MEM2PER |
/** Start the counter if LED1 is enabled. */ XDMAC_CC_MEMSET_NORMAL_MODE |
if (g_b_led1_active) { XDMAC_CC_MBSIZE_SINGLE |
tc_start(TC0, 0); XDMAC_CC_DWIDTH_BYTE |
} XDMAC_CC_SIF_AHB_IF0 |
#else XDMAC_CC_DIF_AHB_IF1 |
tc_start(TC0, 0); XDMAC_CC_SAM_INCREMENTED_AM |
#endif XDMAC_CC_DAM_FIXED_AM |
} XDMAC_CC_CSIZE_CHK_1 |
#endif XDMAC_CC_PERID(XDAMC_CHANNEL_HWID_USART0_TX)
// [main_tc_configure] );
xdmac_channel_set_descriptor_control(XDMAC, XDMA_CH_UART_TX, 0);
/** xdmac_channel_set_source_microblock_stride(XDMAC, XDMA_CH_UART_TX, 0);
* Configure UART console. xdmac_channel_set_destination_microblock_stride(XDMAC, XDMA_CH_UART_TX, 0);
*/ xdmac_channel_set_datastride_mempattern(XDMAC, XDMA_CH_UART_TX, 0);
// [main_console_configure] xdmac_channel_set_block_control(XDMAC, XDMA_CH_UART_TX, 0);
static void configure_console(void) xdmac_enable_interrupt(XDMAC, XDMA_CH_UART_TX);
{ xdmac_channel_enable_interrupt(XDMAC, XDMA_CH_UART_TX, XDMAC_CIE_BIE);
const usart_serial_options_t uart_serial_options = {
.baudrate = CONF_UART_BAUDRATE, // Setup XDMA Channel for USART RX
#ifdef CONF_UART_CHAR_LENGTH xdmac_channel_set_source_addr(XDMAC, XDMA_CH_UART_RX, (uint32_t)&BOARD_USART->US_RHR);
.charlength = CONF_UART_CHAR_LENGTH, xdmac_channel_set_config(XDMAC, XDMA_CH_UART_RX,
#endif XDMAC_CC_TYPE_PER_TRAN |
.paritytype = CONF_UART_PARITY, XDMAC_CC_DSYNC_PER2MEM |
#ifdef CONF_UART_STOP_BITS XDMAC_CC_MEMSET_NORMAL_MODE |
.stopbits = CONF_UART_STOP_BITS, XDMAC_CC_MBSIZE_SINGLE |
#endif XDMAC_CC_DWIDTH_BYTE |
}; XDMAC_CC_SIF_AHB_IF1 |
XDMAC_CC_DIF_AHB_IF0 |
/* Configure console UART. */ XDMAC_CC_SAM_FIXED_AM |
sysclk_enable_peripheral_clock(CONSOLE_UART_ID); XDMAC_CC_DAM_INCREMENTED_AM |
stdio_serial_init(CONF_UART, &uart_serial_options); XDMAC_CC_CSIZE_CHK_1 |
} XDMAC_CC_PERID(XDAMC_CHANNEL_HWID_USART0_RX)
);
// [main_console_configure] xdmac_channel_set_descriptor_control(XDMAC, XDMA_CH_UART_RX, 0);
xdmac_channel_set_source_microblock_stride(XDMAC, XDMA_CH_UART_RX, 0);
/** xdmac_channel_set_destination_microblock_stride(XDMAC, XDMA_CH_UART_RX, 0);
* \brief Wait for the given number of milliseconds (using the g_ul_ms_ticks xdmac_channel_set_datastride_mempattern(XDMAC, XDMA_CH_UART_RX, 0);
* generated by the SAM's microcontrollers's system tick). xdmac_channel_set_block_control(XDMAC, XDMA_CH_UART_RX, 0);
* xdmac_enable_interrupt(XDMAC, XDMA_CH_UART_RX);
* \param ul_dly_ticks Delay to wait for, in milliseconds. xdmac_channel_enable_interrupt(XDMAC, XDMA_CH_UART_RX, XDMAC_CIE_BIE);
*/ #endif
// [main_ms_delay] }
static void mdelay(uint32_t ul_dly_ticks)
{ void hal_uart_dma_set_sleep(uint8_t sleep){
uint32_t ul_cur_ticks; }
ul_cur_ticks = g_ul_ms_ticks; void hal_uart_dma_set_block_received( void (*the_block_handler)(void)){
while ((g_ul_ms_ticks - ul_cur_ticks) < ul_dly_ticks); rx_done_handler = the_block_handler;
} }
// [main_ms_delay]
void hal_uart_dma_set_block_sent( void (*the_block_handler)(void)){
/** tx_done_handler = the_block_handler;
* \brief getting-started Application entry point. }
*
* \return Unused (ANSI-C compatibility). void hal_uart_dma_set_csr_irq_handler( void (*the_irq_handler)(void)){
*/ cts_irq_handler = the_irq_handler;
// [main] }
int main(void)
{ int hal_uart_dma_set_baud(uint32_t baud){
//! [main_step_sys_init] /* Disable TX & RX function. */
/* Initialize the SAM system */ usart_disable_tx(BOARD_USART);
sysclk_init(); usart_disable_rx(BOARD_USART);
board_init(); uint32_t res = usart_set_async_baudrate(BOARD_USART, baud, sysclk_get_peripheral_hz());
//! [main_step_sys_init] if (res){
log_error("hal_uart_dma_set_baud library call failed");
#ifndef BOARD_NO_PUSHBUTTON_2 }
#if (SAMV71 || SAMV70 || SAMS70 || SAME70)
if (GPIO_PUSH_BUTTON_2 == PIO_PB12_IDX) { /* Enable TX & RX function. */
matrix_set_system_io(matrix_get_system_io() | CCFG_SYSIO_SYSIO12); usart_enable_tx(BOARD_USART);
} usart_enable_rx(BOARD_USART);
ioport_set_pin_dir(GPIO_PUSH_BUTTON_2, IOPORT_DIR_INPUT);
ioport_set_pin_mode(GPIO_PUSH_BUTTON_2, GPIO_PUSH_BUTTON_2_FLAGS); log_info("Bump baud rate");
ioport_set_pin_sense_mode(GPIO_PUSH_BUTTON_2, GPIO_PUSH_BUTTON_2_SENSE);
#endif return 0;
#endif }
//! [main_step_console_init]
/* Initialize the console uart */ void hal_uart_dma_send_block(const uint8_t *data, uint16_t size){
configure_console();
//! [main_step_console_init] #ifdef USE_XDMAC_FOR_USART
xdmac_channel_get_interrupt_status( XDMAC, XDMA_CH_UART_TX);
/* Output example information */ xdmac_channel_set_source_addr(XDMAC, XDMA_CH_UART_TX, (uint32_t)data);
puts(STRING_HEADER); xdmac_channel_set_microblock_control(XDMAC, XDMA_CH_UART_TX, size);
xdmac_channel_enable(XDMAC, XDMA_CH_UART_TX);
/* Configure systick for 1 ms */ #else
puts("Configure system tick to get 1ms tick period.\r"); tx_buffer_ptr = (uint8_t *) data;
//! [main_step_systick_init] bytes_to_write = size;
if (SysTick_Config(sysclk_get_cpu_hz() / 1000)) { usart_enable_interrupt(BOARD_USART, US_IER_TXRDY);
puts("-F- Systick configuration error\r"); #endif
while (1); }
}
//! [main_step_systick_init] void hal_uart_dma_receive_block(uint8_t *data, uint16_t size){
#ifndef BOARD_NO_LED_1 hal_uart_rts_low();
puts("Configure TC.\r");
//! [main_step_tc_init] #ifdef USE_XDMAC_FOR_USART
configure_tc(); xdmac_channel_get_interrupt_status( XDMAC, XDMA_CH_UART_RX);
//! [main_step_tc_init] xdmac_channel_set_destination_addr(XDMAC, XDMA_CH_UART_RX, (uint32_t)data);
#endif xdmac_channel_set_microblock_control(XDMAC, XDMA_CH_UART_RX, size);
xdmac_channel_enable(XDMAC, XDMA_CH_UART_RX);
puts("Configure buttons with debouncing.\r"); #else
//! [main_step_btn_init] rx_buffer_ptr = data;
configure_buttons(); bytes_to_read = size;
//! [main_step_btn_init] usart_enable_interrupt(BOARD_USART, US_IER_RXRDY);
#endif
printf("Press %s to Start/Stop the %s blinking.\r\n", }
PUSHBUTTON_1_NAME, LED_0_NAME);
#ifdef USE_XDMAC_FOR_USART
#ifndef BOARD_NO_PUSHBUTTON_2 void XDMAC_Handler(void)
printf("Press %s to Start/Stop the %s blinking.\r\n", {
PUSHBUTTON_2_NAME, LED_1_NAME); uint32_t dma_status;
#endif dma_status = xdmac_channel_get_interrupt_status(XDMAC, XDMA_CH_UART_TX);
if (dma_status & XDMAC_CIS_BIS) {
//! [main_step_loop] tx_done_handler();
while (1) { }
/* Wait for LED to be active */ dma_status = xdmac_channel_get_interrupt_status(XDMAC, XDMA_CH_UART_RX);
while (!g_b_led0_active); if (dma_status & XDMAC_CIS_BIS) {
hal_uart_rts_high();
/* Toggle LED state if active */ rx_done_handler();
if (g_b_led0_active) { }
ioport_toggle_pin_level(LED0_GPIO); }
printf("1 "); #else
} void USART_Handler(void)
{
/* Wait for 500ms */ uint32_t ul_status;
mdelay(300); uint8_t uc_char;
}
//! [main_step_loop] /* Read USART status. */
} ul_status = usart_get_status(BOARD_USART);
// [main]
/// @cond 0 // handle ready to send
/**INDENT-OFF**/ if(ul_status & US_IER_TXRDY) {
#ifdef __cplusplus if (bytes_to_write){
} // send next byte
#endif usart_write(BOARD_USART, *tx_buffer_ptr);
/**INDENT-ON**/ tx_buffer_ptr++;
/// @endcond bytes_to_write--;
} else {
// done. disable tx ready interrupt to avoid starvation here
usart_disable_interrupt(BOARD_USART, US_IER_TXRDY);
tx_done_handler();
}
}
// handle byte available for read
if (ul_status & US_IER_RXRDY) {
uint32_t ch;
usart_read(BOARD_USART, (uint32_t *)&ch);
*rx_buffer_ptr++ = ch;
bytes_to_read--;
if (bytes_to_read == 0){
// done. disable rx ready interrupt, raise RTS
hal_uart_rts_high();
usart_disable_interrupt(BOARD_USART, US_IER_RXRDY);
rx_done_handler();
}
}
}
#endif
void hal_tick_init()
{
/* Configure systick for 1 ms */
puts("Configure system tick to get 1ms tick period.\r");
if (SysTick_Config(sysclk_get_cpu_hz() / 1000)) {
puts("-F- Systick configuration error\r");
while (1);
}
}
void hal_tick_set_handler(void (*handler)(void)){
if (handler == NULL){
tick_handler = &dummy_handler;
return;
}
tick_handler = handler;
}
int hal_tick_get_tick_period_in_ms(void){
return 1;
}
/**
* \brief getting-started Application entry point.
*
* \return Unused (ANSI-C compatibility).
*/
// [main]
int main(void)
{
//! [main_step_sys_init]
/* Initialize the SAM system */
sysclk_init();
board_init();
//! [main_step_sys_init]
//! [main_step_console_init]
/* Initialize the console uart */
configure_console();
//! [main_step_console_init]
/* Output example information */
puts(STRING_HEADER);
printf("CPU %lu hz, peripheral clock %lu hz\n", sysclk_get_cpu_hz(), sysclk_get_peripheral_hz());
// start with BTstack init - especially configure HCI Transport
btstack_memory_init();
btstack_run_loop_init(btstack_run_loop_embedded_get_instance());
// enable full log output while porting
// hci_dump_open(NULL, HCI_DUMP_STDOUT);
// init HCI
// hci_init(hci_transport_h4_instance(), (void*) &hci_transport_config);
// hci_set_chipset(btstack_chipset_cc256x_instance());
// hci_set_link_key_db(btstack_link_key_db_memory_instance());
// enable eHCILL
// bt_control_cc256x_enable_ehcill(1);
// hand over to btstack embedded code
btstack_main(0, NULL);
// go
btstack_run_loop_execute();
// compiler happy
while(1);
}
// [main]
/// @cond 0
/**INDENT-OFF**/
#ifdef __cplusplus
}
#endif
/**INDENT-ON**/
/// @endcond