How to run a Bluetooth® Low Energy P2P Server simultaneously with USB CDC on STM32WB part 1
1 INTRODUCTION:
Welcome! In this article, we will cover how to run a Bluetooth® Low Energy application simultaneously with USB CDC functionality on STM32WB55xx devices.
When trying to run both APIs together, we need to pay attention to some details regarding how the Bluetooth® Low Energy stack is implemented as that can interfere with the USB clock.
During the startup phase, the Bluetooth® Low Energy CPU2 carries out various initializations, including the initialization of shared resources such as the RNG and USB peripherals. The RNG is specifically prepared at CPU2 startup to provide prompt access to random numbers when needed. In this configuration, CPU2 activates the HSI48 oscillator as the RNG peripheral clock, subsequently deactivating it once a wireless stack is initiated.
Hardware semaphores (HSEM) are used to control access to shared resources between the two CPUs. The code on CPU2 tries to access the RNG if HSEM0 is available. If that succeeds, it attempts to get HSEM5 to control the HSI48 clock.
Getting the HSEM0 is required for the RNG peripheral access by CPU2 and for the USB peripheral access by CPU1, which can create a potential race condition.
The HSEM5 is used to control the RNG clock source, but it could interfere with the USB clock source if both the RNG and USB use the same clock source as we can see in the image below:
This means that if the CLK48 clock mux output is switched off, the USB API will not be able to work under these conditions.To easily work around this, we will make sure that the CPU2 will not get the HSEM5, so the clock cannot be switched off. Now, let us delve into the project setup and code implementation to demonstrate how easily this can be achieved. The project involves combining the Bluetooth® Low Energy P2P application with the USB CDC to open a Virtual Com Port.
2 REQUIRED HARDWARE AND SOFTWARE
The following HW/SW are required:HARDWARE: - P-NUCLEO-WB55 - Two USB cables type micro-B- Cell phone with the STBLESensor appSOFTWARE: - STM32CubeIDE - STM32CubeIDE - STMicroelectronics. The project settings can also be done directly on STM32CubeMX instead.
3 DEVELOPMENT
Following this video series from our YouTube page, you can update the STm32WB Stack version. This is an important step to guarantee that the Bluetooth® Low Energy stack is correctly updated.
- CREATING THE PROJECT FROM SCRATCH
Let us create a project for the P-NUCLEO-WB55 on STM32CubeIDE. Opening the software, click on File -> New -> STM32 Project:In the "Board Selector" tab, search for P-NUCLEO-WB55-NUCLEO as the Commercial part number, select it on the list, then hit Next.Give a name to the project, let the standard settings the same, then hit Finish.With this done, the STM32CubeMX screen shows up so we can set the necessary configurations!
3.1 PROJECT SETTINGS
PINOUT & CONFIGURATION TABFirst of all, we set the user Buttons and the onboard user LEDs. This allows us to have a visual interface to check what the code is executing in each part!Give it a left click on the pin (the user LEDs are available on PB0, PB1, and PB5) and select GPIO_Output.After that, right click, then select “Enter user Label” to give it a name.To set the user Button, we will follow similar steps.Left click on the PC4 pin and select GPIO_EXTI4. Do the same as before to give it a name.After that, go to System Core > GPIO tab > Select PC4 pin and set the following:Still, in the GPIO tab, go to NVIC and enable the EXTI line4 Interrupt.Go to System Core category and set “Trace Asynchronous Sw” as “Debug”, System wake-up 2 and “SysTick” as the “Timebase Source”.Now, let us set the HSE and the LSE in the RCC category. Select Crystal/Ceramic Resonator for both.Also activate the IPCC and set the IPCC Rx and Tx interrupts.We do the same to the HSEM peripheral.Activate the RF, in the connectivity category, as follows:Going to the USART1 tab, select it as in “Asynchronous mode” and let it use the standard parameters.
Set the DMA request for USART_TX as follows.
And activate the internal Pull-up for each Tx and Rx pin and the maximum output speed to “Very High”.
Also, in “Middleware and Software packs”, enable “Bluetooth® Low Energy” in the STM32_WPAN tab and make sure the Bluetooth® Low Energy application type is set to “Server Profile” and that “Custom P2P Server” is enabled. In LOCAL_NAME, a different name can be set. This is the name that appears in your phone when it is looking for the available Bluetooth connections.
In the Utilities category, make sure that the “Sequencer” and the “Tiny_LPM" are enabled.
Now, enable USB Device FS and its interrupts.
In the USB_DEVICE tab, select the USB Class as CDC.
The pinout configurations are totally set. Now, let’s go to the clock settings!
CLOCK CONFIGURATION TAB
After all these settings, we can see that the clock Configuration tab is marked with a small red circle with an “x” inside. When entering this tab, a message appears asking to run the automatic clock issues solver. Allow it.
If it does not appear, you can hit the “Resolve clock Issues” button.
After that, make sure that the selected clocks and its values are as follows:
PROJECT MANAGER TAB
In the Project Manager tab > Project, increase the Minimum Heap Size and the Minimum Stack Size to 0x600.
We start the coding in part 2!
See you soon.