Getting Started With Renesas RA Family/FSP - Part 1: Kick Off Your First Project

by Panos_V in Circuits > Microcontrollers

15 Views, 0 Favorites, 0 Comments

Getting Started With Renesas RA Family/FSP - Part 1: Kick Off Your First Project

Part1.JPG

In Part 0 we gave you the big‑picture intro to the Renesas RA MCU family and the Flexible Software Package (FSP).

Now we’re rolling up our sleeves, firing up the tool chain, and creating a working project that actually makes an LED blink using an timer interrupt instead of a simple busy‑wait loop.

Supplies

While this tutorial works with any Renesas RA microcontroller, we'll be walking through the steps using the Evaluation Kit for RA6M4 as our example.

Setting Up the Dev Environment

To follow along, we will need:

  1. e2 Studio (IDE)
  2. FSP
  3. A supported RA development board. Specifically, we will use the EK-RA6M4.

Once these are in place, fire up the IDE and choose your workspace folder.

In case of any trouble, please refer to Part 0.

Create the Brand New Project

Screenshot_1.png
Screenshot_2.png
Screenshot_3.png

We will not deep dive to these steps since describes more these steps

We will not re-walk every click-by-click here that Part 0 covered in details. Just follow the following short checklist:

  1. Create the Renesas C/C++ Project.
  2. Give a name of your desire.
  3. Configure the device and tool.
  4. Select Flat Project.
  5. Choose the Executable build artifact without RTOS.
  6. Select the Minimal option, not the Blinky template.

That's it! Your project skeleton is now created and ready for the next sections, where we will add the timer interrupt, configure the LED pin ad write code that will flash them.

Blue Led for the Win

Screenshot_4.png

Let's find out where is this LED located on the board.

  1. Open the EK-RA6M4 Users Manual and find the LED section.
  2. Assuming your favorite color is blue, like mine, we will use LED1 for our project. Please keep in mind the MCU port, which is P415, meaning that blue LED is connect to port 4, pin 15.

Configure FSP to Drive This LED

Screenshot_5.png

We previously found that P415 of our MCU is connected to LED1, don't forget it is the blue one.

Now it's time to set-up the well-known accordingly.

  1. Open the Pins tab and locate the Port4 and then P415.
  2. The fields will be predefined because we have previously selected the evaluation kit board.
  3. If not, select the Output Mode.

Stack-it-Up

Screenshot_6.png

What's an FSP stack? Simply, a pre-written, board specific foundation(BSP + drivers) access through clean HAL API, so we can write only the application code without bothering about the register-level headaches.

When you first create your project you will notice that the stack r_ioport is already there. FYI, this one is responsible for basic pin configuration and IOs.

Now, let's add the timer stack. On the Stacks tab in the FSP Configuration window, click on New Stack-> Timers-> Timer,General Purpose PWM r_gpt

Basic Configuration of R_GPT

Screenshot_11.png

Each stacks consists of several properties, based on the peripheral they are related to. This step will guide us how to configure the r_gpt for our application.

On the Properties Tab, we will set the General Parameters.

Let’s give it a name that actually tells us what it does, instead of leaving it as “g_timer0” or some other generic name. So, rename the instance to coolTimer. This way you’ll know at a glance it’s your timer.

Since we are humans and not machines, the period unit makes more sense to be expressed as milliseconds. 100ms will be fine.

Channel and mode could be remained unchanged.


Hooking Up the Timer Callback

Screenshot_12.png

Okay, so our coolTimer is ticking every 100 ms, but right now it’s just... ticking quietly in the corner. Let’s make it do something when that happens! We’ll set up a callback a little function that gets called automatically every time the timer period expires.

Find the Interrupts section. Then give Callback a name and select the Priority Level of the timer overflow. Lucky 7 for me. Remember, the lower the number, the higher the priority.

Don't waste time, just Generate the Project Content,

Coding

Untitled.png
Screenshot_14.png
Screenshot_15.png

Now, we have set the coolTimer, it's time to bring it to life in code.

  1. Locate the src/hal_enty.c: Where our app starts running.
  2. For error busting, we will use the macro shown on 2nd picture.
  3. Start the timer: Using the FSP API, we will start the coding using the Developer Assistance. Expand it and find the peripheral we want to use, the coolTimer. A wild bunch of functions related to this r_gpt will be appeared, don't panic. We just want to Open and Start the timer. So, just drag and drop them in the hal_entry.
  4. Did you notice the parameters with the coolTimer are already prefilled? Now, lets add a status variable fsp_err_t status, which will lead us to the status of each FSP process status.
  5. Use the same drag and drop approach to get the callback. Add the code shown on third picture.
  6. Next, we will add the error checking, after the timer Open and Start APIs.
  7. Finally, add the well known while(true) loop, using only the __WFI() instruction, which gets the MCU to sleep until an interrupt kicks.


Now, the hal_entry.c should look like this

#include "hal_data.h"
/* Application error */
#define APP_ASSERT(a) \
{ \
if ((a)) \
{ \
/* Break */ \
__BKPT(0); \
} \
}
/* Callback function */
void coolTimerCallback(timer_callback_args_t *p_args)
{
/* TODO: add your own code here */
FSP_PARAMETER_NOT_USED(p_args);
static bsp_io_level_t level = BSP_IO_LEVEL_LOW;
R_IOPORT_PinWrite (&g_ioport_ctrl, LED1, level);
level = (level) ? BSP_IO_LEVEL_LOW : BSP_IO_LEVEL_HIGH;
}

/*******************************************************************************************************************//**
* main() is generated by the RA Configuration editor and is used to generate threads if an RTOS is used. This function
* is called by main() when no RTOS is used.
**********************************************************************************************************************/
void hal_entry(void)
{
/* TODO: add your own code here */
fsp_err_t status;
status=R_GPT_Open(&coolTimer_ctrl, &coolTimer_cfg);
APP_ASSERT(status);
status = R_GPT_Start (&coolTimer_ctrl);
APP_ASSERT(status);
while (1)
{ /* Wait for interrupt */
__WFI(); /* Increment counter */
}
#if BSP_TZ_SECURE_BUILD
/* Enter non-secure code */
R_BSP_NonSecureEnter();
#endif
}

#if BSP_TZ_SECURE_BUILD

FSP_CPP_HEADER
BSP_CMSE_NONSECURE_ENTRY void template_nonsecure_callable ();

/* Trustzone Secure Projects require at least one nonsecure callable function in order to build (Remove this if it is not required to build). */
BSP_CMSE_NONSECURE_ENTRY void template_nonsecure_callable ()
{

}
FSP_CPP_FOOTER

#endif


Downloads

Implementing and Testing the Timer Callback

Untitled.png

Back in Part 0 , we learned how to build and debug a project, now’s your chance to put that into practice!

Go ahead and build your project, then start a debug session to flash it onto the board.

Set a breakpoint at line 17, where the LED write instruction is located, so you can confirm that the callback is actually being triggered.

If everything works as expected, congratulations, you’ve just completed your first project, and your coolTimer is running happily on its own!

Oh, and don’t forget to remove the breakpoint afterward so the code can run continuously.


As an optional step, you can also hook up a logic analyzer to check the timer frequency. I will share a capture of mine, just for verification. You'll see a neat toggle every 100ms.