KBA_BT_1011: How can I extend the Bluetooth examples with UART functionality?
06/169/2016 | 02:17 PM
Question
How can I extend the Bluetooth examples with UART functionality?
Answer
There are 3 options:
1. Use the standard printf and get functions which are retargeted to one USART or LEUART peripheral.
This option is very easy to use but you cannot send binary data with this.
The easiest way to use printf is through the use of our retargetserial driver. To get started,
copy the following files from the hardware\kit\common\drivers\ folder in the SDK to your project
retargetio.c, retargetserial.h and retargetserial.c
include retargetserial.h in your main.c
call RETARGET_SerialInit() before the first call to printf()
open hal-config.h and set the definition of HAL_VCOM_ENABLE as shown below
#define HAL_VCOM_ENABLE (1)
Now you can call printf() anywhere in the application and the output will be directed through the virtual COMPORT. Please remember that printf() can take somewhat long to execute so care should be taken in using it so that Bluetooth connections are not interrupted.
2. Use the em_usart or the em_leuart driver.
These drivers are part of the emlib. They provide an API for the USART and LEUART MCU peripherals.You can find more about the API here:
This driver is the most flexible from the available options. It is built on the top of emlib. The UART driver support both USART and LEUART peripherals.
The driver is fully reentrant and multiple driver instances can coexist. The driver does not buffer or queue data, but it queues UART transmit and receive operations. Both blocking and non-blocking transfer functions are available. Non-blocking transfer functions report transfer completion with callback functions. Transfers are done using DMA. Simple direct/forced transmit and receive functions are also available.
UART hardware flow control (CTS/RTS) is fully supported by the driver. UART software flow control (XON/XOFF) is partially supported.
In this article we going to set up the uartdrv to use the USART peripheral in few steps. The attached example is demonstrating the initialization and the using of non-blocking RX/TX functions with implementing a simple echo functionality.
Setting up the uartdrv
Step 1 – Add the necessary emlib source files to the project from the gecko SDK. These are the followings:
platform\emlib\src\em_cmu.c
platform\emlib\src\em_core.c
platform\emlib\src\em_dma.c
platform\emlib\src\em_emu.c
platform\emlib\src\em_gpio.c
platform\emlib\src\em_int.c
platform\emlib\src\em_ldma.c
platform\emlib\src\em_leuart.c
platform\emlib\src\em_usart.c
Then add the necessary emdrv source files to the project from the gecko SDK. These are the followings:
platform\emdrv\dmadrv\src\dmadrv.c
platform\emdrv\gpiointerrupt\src\gpiointerrupt.c
platform\emdrv\uartdrv\src\uartdrv.c
Step 2 – Update the include paths of the project.
You need to add the emlib and emdrv include paths as listed below:
platform\emlib\inc
platform\emdrv\common\inc
platform\emdrv\dmadrv\config
platform\emdrv\dmadrv\inc
platform\emdrv\gpiointerrupt\inc
platform\emdrv\gpiointerrupt\inc
platform\emdrv\uartdrv\inc
platform\emdrv\uartdrv\config
In case using UART over J-Link CDC-on Silicon Labs boards you need the include path for the board and kit abstraction headers also. These headers containing the Virtual COM pin settings. The path for these are below:
hardware\kit\\common
hardware\kit\common\bsp
hardware\kit\common\halconfig
Step 3 – Once the necessary files and include paths are added you need to switch off the automatic sleeping feature of the Bluetooth stack. This is needed because the USART peripheral is not working in EM2. It can be done by setting the gecko_configuration_t.sleep.flags to 0 as shown below.
/* Gecko configuration parameters (see gecko_configuration.h) */
static const gecko_configuration_t config = {
.config_flags = 0,
.sleep.flags = 0, // <- DISABLE THE SLEEP HERE
..
}
Step 4 – On Silicon Labs kits you can use the UART over the J-Link CDC. For that you need to route the UART signals from the MCU to the board controller of the kit.
This can be done by setting VCOM enable pin high. Most of the kits it can be done by setting a GPIO by software. But on some kits a 0 Ohm resistor must soldered additionally.
The attached uart_echo.c and uart_echo.h implements terminal echo. It can be used in any Bluetooth SDK project once the steps described above are completed.
Calling the UART_EchoInit(uint8_t vcom) sets the VCOM enable pin, configures the uartdrv then start to receive.
Once a byte received the UART_rx_callback called by the uartdrv. The callback immediately transmits the received byte back and start to receive the next byte.
Thanks for posting this. I downloaded the zip file you posted, imported into Simplicity and I get the following build error:
Errors occurred during the build. Errors running builder 'CDT Builder' on project 'bleDemoSoc-basic-server-uart-example_2'. A resource exists with a different case: '/bleDemoSoc-basic-server-uart-example_2/IAR ARM - Debug'. A resource exists with a different case: '/bleDemoSoc-basic-server-uart-example_2/IAR ARM - Debug'.
The '_2' is the name that Simplicity gave to the project when I imported it. Any idea why that is happening?
0
Hi,
I think something went wrong during the import. Extract the zip then
File->Import->Existing Project into the Workspace. This import method should work.
Do you have IAR and the C SDK 1.0.4 installed?
Cheers,
Balázs
0
I'm using SDK 1.0.4 and IAR v7.60. Although I was not able to import the project successfully I was able to use the code (copy/paste) in my test application so I'm set at this point.
Thanks,
Robert
0
HI,
Just for sharing.
in uart_echo.c following lines. This works only when Tx/Rx ports are PA0/PA1, but this is not good example.
To run the sample, I also regenerated with bleDemoSoc-basic-server sample application and put uart-echo.h and uart-echo.c files in the project then call UART_Init(1); from emberAfMainEfr32AppInitCallback() in ble-soc-callback-stubs.c.
Best regards,
Aki
0
Hello,
I'm interested in using the WSTK (6101B) to send the input byte stream received from connected devices out the serial port. I'm assuming there is already a sample program - what's the simplest way to achieve this?
Thanks.
0
I have imported the project in to my workspace but I cannot build, debug or gernerate. When I click on the .isc file a pop up opens
Select Stack Directory
No Approprate stack was found. Please point to a stack that you would like to use.
Selecting the directory of the downloaded file or workspace directory does not work... any ideas?
0
This project working with BLE SDK 1.0.4.
For newer versions it fails.
Which SDK version are you using?
0
baadamff-
I am using Blue tooth smart sdk 2.0.1 should I and how do I get an older one?
KBA_BT_1011: How can I extend the Bluetooth examples with UART functionality?
Question
How can I extend the Bluetooth examples with UART functionality?
Answer
There are 3 options:
1. Use the standard printf and get functions which are retargeted to one USART or LEUART peripheral.
This option is very easy to use but you cannot send binary data with this.
The easiest way to use printf is through the use of our retargetserial driver. To get started,
Now you can call printf() anywhere in the application and the output will be directed through the virtual COMPORT. Please remember that printf() can take somewhat long to execute so care should be taken in using it so that Bluetooth connections are not interrupted.
2. Use the em_usart or the em_leuart driver.
These drivers are part of the emlib. They provide an API for the USART and LEUART MCU peripherals.You can find more about the API here:
https://siliconlabs.github.io/Gecko_SDK_Doc/efr32bg1/html/group__USART.html
https://siliconlabs.github.io/Gecko_SDK_Doc/efr32bg1/html/group__LEUART.html
3. Use the uartdrv driver.
This driver is the most flexible from the available options. It is built on the top of emlib. The UART driver support both USART and LEUART peripherals.
The driver is fully reentrant and multiple driver instances can coexist. The driver does not buffer or queue data, but it queues UART transmit and receive operations. Both blocking and non-blocking transfer functions are available. Non-blocking transfer functions report transfer completion with callback functions. Transfers are done using DMA. Simple direct/forced transmit and receive functions are also available.
UART hardware flow control (CTS/RTS) is fully supported by the driver. UART software flow control (XON/XOFF) is partially supported.
In this article we going to set up the uartdrv to use the USART peripheral in few steps. The attached example is demonstrating the initialization and the using of non-blocking RX/TX functions with implementing a simple echo functionality.
Setting up the uartdrv
Step 1 – Add the necessary emlib source files to the project from the gecko SDK. These are the followings:
platform\emlib\src\em_cmu.c
platform\emlib\src\em_core.c
platform\emlib\src\em_dma.c
platform\emlib\src\em_emu.c
platform\emlib\src\em_gpio.c
platform\emlib\src\em_int.c
platform\emlib\src\em_ldma.c
platform\emlib\src\em_leuart.c
platform\emlib\src\em_usart.c
Then add the necessary emdrv source files to the project from the gecko SDK. These are the followings:
platform\emdrv\dmadrv\src\dmadrv.c
platform\emdrv\gpiointerrupt\src\gpiointerrupt.c
platform\emdrv\uartdrv\src\uartdrv.c
Step 2 – Update the include paths of the project.
You need to add the emlib and emdrv include paths as listed below:
platform\emlib\inc
platform\emdrv\common\inc
platform\emdrv\dmadrv\config
platform\emdrv\dmadrv\inc
platform\emdrv\gpiointerrupt\inc
platform\emdrv\gpiointerrupt\inc
platform\emdrv\uartdrv\inc
platform\emdrv\uartdrv\config
In case using UART over J-Link CDC-on Silicon Labs boards you need the include path for the board and kit abstraction headers also. These headers containing the Virtual COM pin settings. The path for these are below:
hardware\kit\\common
hardware\kit\common\bsp
hardware\kit\common\halconfig
Step 3 – Once the necessary files and include paths are added you need to switch off the automatic sleeping feature of the Bluetooth stack. This is needed because the USART peripheral is not working in EM2. It can be done by setting the gecko_configuration_t.sleep.flags to 0 as shown below.
Step 4 – On Silicon Labs kits you can use the UART over the J-Link CDC. For that you need to route the UART signals from the MCU to the board controller of the kit.
This can be done by setting VCOM enable pin high. Most of the kits it can be done by setting a GPIO by software. But on some kits a 0 Ohm resistor must soldered additionally.
Here is how you can enable VCOM by software.
Example
The attached uart_echo.c and uart_echo.h implements terminal echo. It can be used in any Bluetooth SDK project once the steps described above are completed.
Calling the UART_EchoInit(uint8_t vcom) sets the VCOM enable pin, configures the uartdrv then start to receive.
Once a byte received the UART_rx_callback called by the uartdrv. The callback immediately transmits the received byte back and start to receive the next byte.
Hello,
Thanks for posting this. I downloaded the zip file you posted, imported into Simplicity and I get the following build error:
Errors occurred during the build.
Errors running builder 'CDT Builder' on project 'bleDemoSoc-basic-server-uart-example_2'.
A resource exists with a different case: '/bleDemoSoc-basic-server-uart-example_2/IAR ARM - Debug'.
A resource exists with a different case: '/bleDemoSoc-basic-server-uart-example_2/IAR ARM - Debug'.
The '_2' is the name that Simplicity gave to the project when I imported it. Any idea why that is happening?
I'm using SDK 1.0.4 and IAR v7.60. Although I was not able to import the project successfully I was able to use the code (copy/paste) in my test application so I'm set at this point.
Thanks,
Robert
HI,
Just for sharing.
in uart_echo.c following lines. This works only when Tx/Rx ports are PA0/PA1, but this is not good example.
initData.portLocationTx = USART_ROUTELOC0_TXLOC_LOC0;
initData.portLocationRx = USART_ROUTELOC0_RXLOC_LOC0;
This is better to be below, otherwize Tx will not be set as expected.
initData.portLocationTx = _USART_ROUTELOC0_TXLOC_LOC0;
initData.portLocationRx = _USART_ROUTELOC0_RXLOC_LOC0;
To run the sample, I also regenerated with bleDemoSoc-basic-server sample application and put uart-echo.h and uart-echo.c files in the project then call UART_Init(1); from emberAfMainEfr32AppInitCallback() in ble-soc-callback-stubs.c.
Best regards,
Aki
Hello,
I'm interested in using the WSTK (6101B) to send the input byte stream received from connected devices out the serial port. I'm assuming there is already a sample program - what's the simplest way to achieve this?
Thanks.
I have imported the project in to my workspace but I cannot build, debug or gernerate. When I click on the .isc file a pop up opens
Select Stack Directory
No Approprate stack was found. Please point to a stack that you would like to use.
Selecting the directory of the downloaded file or workspace directory does not work... any ideas?
baadamff-
I am using Blue tooth smart sdk 2.0.1 should I and how do I get an older one?
Hi, Here is the SSv3: http://www.silabs.com/products/development-tools/software/simplicity-studio-version3 And here is the old SDK: https://www.silabs.com/pages/docDownload.aspx?client=silabs&entsp=a__en_biasing_policy&exclude_apps=1&FileTitle=SiliconLabs-BluetoothSmartSDK-1.0.4.0-GA.exe&FILEURL=http%3A%2F%2Fwww.silabs.com%2FSupport+Documents%2FRegisteredDocs%2FSiliconLabs-BluetoothSmartSDK-1.0.4.0-GA.exe&ie=UTF-8&oe=UTF-8&proxystylesheet=silabs&RefererURL=http%3A%2F%2Fsearch.silabs.com%2Fsearch%3Fq%3DBGM111&site=products&sort=date%3AD%3AL%3Ad1&ud=1&wc=200&wc_mc=1 However it is recommended to use the newer SDK. Here is a KB about adding UART retarget to those projects: http://community.silabs.com/t5/Bluetooth-Wi-Fi-Knowledge-Base/Retarget-stdio-to-UART-in-BLE-SDK-2-0-0-examples/ta-p/178710 Cheers, Balázs
2. Include stdio.h and retargetserial.h files to your file.
do I just #include or add then to the workspace as well?
Where is the retargetserialconfig.h file located?
also does this KB just redirect printf to uart rather than the consel?
Not sure what it is doing but i'll give it a try