MikroElektronika Learn

Let's Make

RN52 Media Station

10 Nov , 2016  

Bluetooth is one of the easiest and most common ways to connect your embedded project over a wireless signal. Sometimes bluetooth can be used to communicate between two modules in an IoT environment or a simple controller for an embedded hobbyist, but in the case of BT Audio click it makes implementing streaming audio over bluetooth easier than ever. In this article we will go over how BT Audio is capable of making this typically difficult task very easy for us, as well as the many features and how to implement them in your own example!

How Is It So Easy?

Interfacing with the RN52 by Microchip takes some string commands to configure and a bluetooth device to connect with. With a few commands, your device is ready to connect and begin streaming audio or whatever else you have in mind. If you’re familiar with streaming audio of any kind you know that this is not an easy task, but with the RN52 on the BT Audio click it takes the user a few string commands and the RN52 can decode SBC, AAC and aptX audio. With its on-board Audio DSP 16-bit Stereo CODEC and great firmware, the module takes UART commands to configure the settings, and after that the magic of outputting clean and clear audio is done by the RN52. After configuration, the device can be put into “data” mode to convert all incoming audio signals accordingly and then sends them through the integrated amplifier to the audio jacks on the click board.



What Can It Do?

Besides the initial setup and configuration like audio routing, module name, authentication pin and bluetooth profile type settings, this little chip is capable of many cool features that make developing a connected media device very effortless. Here is a list of the capabilities:

  • Speaker / Mic gain
  • Tone gain
  • Battery Status
  • Toggle Discoverable
  • Voice Call Dialing
  • Track Metadata
  • Redial
  • Increase / Decrease Volume
  • Next / Previous Track
  • Pause / Play
  • Accept / Reject Incoming Calls
  • Voice Command ( if host device is capable )
  • Mute Caller
  • Caller ID

Every one of these features are easily accessed by the library already developed by MikroE, with an awesome example on the Mikromedia Plus for STM32 ARM with the Mikromedia+ shield. Let’s take a look at how that example is made.

Media Station Example

We have created a great example that shows how to use the Mikromedia Plus for STM32 ARM development platform, Visual TFT, MikroC and the BT Audio click to create an interface for connecting your phone or other device to the click, and playing music or making phone calls. If you’d like to just use our code, download that example from Libstock, otherwise follow these steps to create your own example.

For this example we have used the Mikromedia Plus for STM development platform on which we added a shield. An RN52 click board was added to the mikroBUS slot 1 on the shield.


To setup your workspace for developing with the Mikromedia Plus display, start up Visual TFT and choose Mikromedia_Plus_for_STM32_ARM as the platform, and mikroC PRO for ARM for your Target Compiler like so:



After your work environment is setup for this board, you can go ahead and start designing your screens to display anything you’d like. The example code is provided on Libstock, but if you’d like you can create your own buttons and text with the tools provided in TFT. To create a button, simply drag a “Button” from the Components palette to your screen…


Drag as many buttons or labels you want to the screen, and when you are finished making it look nice, you can begin to create functions for the events that can happen with your objects. In our example we use “OnClick” events for our buttons to sense when a user has pushed the button. By implementing these events, you can include any code you want inside of the functions created, therefore making a button do anything you want. To find your buttons “OnClick” event, click on your button on the screen you’re working with in Visual TFT, and then head over to the left side of the screen under “Events“. Here, you can click the “+” next to the “OnClick” tag, and double click on the empty box next to “Action” in the drop down menu to create an empty function for that event.



Once this is finished, you must Generate Code and open the project in MikroC PRO for ARM. The Project Settings will be wrong for this MCU and board, and you must upload this Scheme from Libstock and use it for this project. Once you have downloaded the scheme, navigate to Project->Edit Project… to load the scheme. On the right side of this window, click the Load Scheme button and find your scheme, in our case the scheme was downloaded into the path under /Mikroelektronika/mikroC PRO for ARM/Schemes/:


BT Audio Library Functions

You now have a working Mikromedia Plus project in MikroC. Now, you must implement some button events with the BT Audio library. You can either download the source code from Github or install the package from Libstock and include the library in your project that way. Once you’ve included the library, the first step would be to setup the environment for the click board. You can either copy and paste it from the already made example, or from right here:

It doesn’t matter if you used our example or are writing one yourselfYou will need to create a new “Module_Struct” for the UART on the shield.

This is how to create a new Module_Struct:

  1. The file is called __Lib_GPIO_32F7xx_Defs.c but the fastest way to find it is to right click on an already existing Module_Struct like  _GPIO_MODULE_UART1_PA9_10 and choose Find Declaration.
  2. As you can see, there are many other definitions for these GPIO modules. Simply paste this code between any of the modules:

Event Functions

When you were in Visual TFT and created the “Events” functions, it created a pointer to these functions in your “project_name_events_code.c” file. You can include the “bt_audio_hw.h” file in this file to access functions for controlling the BT Audio. The names for the functions that handle the events should directly relate to the button they are connected to, unless you decided to change the name of them at the time of creation before clicking “Generate Code”. Let’s take a look at one of the event functions in the example project:

In this case, there is a little bit of extra code. The  connection_status_t  is a struct that is used to determine the connection status through the function  bt_audio_query_connection_status() . This code is here to make sure the program is not attempting to do things with the BT Audio click that it is not prepared to do yet. With that said, first this function checks the connection status, and if there is not a connection, it does nothing. Since the BT Audio is only capable of toggling play/pause, we must implement a check to determine if the music is playing or not. It does this by checking an enum that is used to define the current state of the music at all times ( these types of enums are described in more detail later ). It then uses the   bt_audio_pause_play_track()  function to either play or pause the current track playing over the BT Audio click. One last flag is implemented to tell the check_state()  function whether this button push was the first. Lastly, the get_track_data()  function is called to display the new track meta data onto the TFT.

Here is one more example of an event function:

This function is a very simple example that will lead us into the next topic. When the “Info” button is pressed, this function is called. All that it does is change the screen_state_t  enum to INFO . Later on, when the check_state()  function is called again, it will poll this variable and react accordingly!

Small State Machine

To make implementing thread-like functionality a little bit easier, I implemented a small and simple state machine. In most cases state machines would also implement some kind of transition control, but in our case the states are controlled by buttons in every possible case. Enumerated types are great for defining what the current state of something is, and in this case there are four different variables that we must watch: the current screen being displayed, call state, connection state, and music state. The enums used for determining these states look like this:

Let’s take a look at the screen_state_t  enum. There are four screens, so therefore there are four different possibilities for the current state of the screen. To poll these states, a function is called from the main while loop that checks each state in a switch statement, and handles each different case accordingly. This makes the readability of code much easier to walk through by giving each state a descriptive name. The switch statement makes it even easier to control each individual state accordingly by giving each enum value its own space for user-defined code. Here is what that function looks like:

There is a static variable for each unique state machine, to determine the “old” or “previous” state of the object. The variables to hold these “old” states are static so that they stick around and can be remembered whenever this function is called. The next important thing to realize is that the function is split up into two switch statements. One switch statement is used when the screen state is the same as it was in the last call of the function, and the other is used for when the screen state has changed and the display must be changed before continuing. This also provides protection from trying to modify things from other screens that are not being displayed currently.


Our main function should only be used to setup the BT Audio and anything else needed before starting the TFT display and touch functions. Here is our implementation:

Other than  system_setup()  there is not much going on here. Everything needed to setup is setup through the system_setup()  function, then the BT Audio is set to data mode, some default settings are initialized with assign_default_settings()  and finally the TFT is initialized. Inside of our while loop, the TFT is checked, and a call to the check_state()  function that we reviewed earlier is made.


With the BT Audio click, you can make wireless connectivity with audio streaming and easy control as simple as a few string commands. Take a moment to play with some of the cool features in Visual TFT and other features you can implement with the RN52 and make your own examples! Don’t forget to post your example code on Libstock so everyone can see what you’ve done and build onto it.


Products mentioned

, , , , , , , , , , , ,

Firmware Engineer at MikroElektronika from Oregon with a passion for learning and programming.