Blinky with Timers and Interrupts on the S3A7


#1

This tutorial uses one of the S3A7 general purpose timers and CPU interrupts to precisely control the blink rate of the 4 LEDs on the S3A7 IoT development board. It builds on the earlier tutorial, “Updated 4 LED Blinky on S3A7 Fast Prototyping Board” located here: Updated 4 LED Blinky on S3A7 Fast Prototyping Board That tutorial used a “while(true)” loop and a simple “for” loop delay to roughly control the blink frequency of the LEDs. In this case, we’ll configure a general purpose timer in periodic mode to trigger a CPU interrupt every time it expires. The interrupt will then call a callback function which toggles the LEDs.

Once you have completed the first tutorial, it takes very little time to modify it by adding the timer. Please follow that tutorial through Step 10 where you configure Port 7, Pins 0-3 as outputs to drive the LEDs. From that point, starting below, we’ll add a timer driver and the callback function.

First, click on the ‘Threads’ tab at the bottom of the ‘Synergy Configuration’ tab. Then highlight the ‘HAL/Common’ thread in the ‘Threads’ area. This project has only one thread since we are not using ThreadX. When you highlight the thread, the 3 default drivers will appear to the right. These are GPIO, CGC, and ELC.

Now we need to add a gpt timer driver by clicking on the little green “+” sign in the upper right hand corner as shown below. You can read about the various timers in the SSP manual, but I based the decision to use gpt rather than agt on the fact that gpt is a 32 bit timer and agt is 16 bit, and would thus requre more work since at 48MHz it will time out multiple times during each LED cycle.

Highlight the g_timer0 module which should now appear in the drivers screen.

Once you highlight the driver, the g_timer0 ‘Properties’ tab will be populated with default values in the lower left corner of the e2 Studio environment. I’ve highlighted the 4 fields that we are going to change. The second screenshot shows the ‘Properties’ tab with the updated settings. We will enable the GPT0 COUNTER OVERFLOW interrupt. Every time the counter expires this interrupt will be triggered. We’ll change the name from g_timer0 to led_timer0. We’ll set the period to 250msec to create a 2Hz blink frequency. Finally, we’ll name the callback function that will execute every time the timer interrupt occurs.

DEFAULT:

MODIFIED FOR THIS PROJECT.

Finally, click the ‘Generate Project Content’ button, and we can now edit the usual ‘hal_entry.c’ file. Here is the new code with explanatory comments. Note that the name of the callback function must match the name you just entered in the ‘Properties Dialog.’

/* HAL-only entry function */
#include "hal_data.h"

bool OnOff = true;
bool OffOn = false;

//This is called first when the thread starts.  All we have to do here is open
//the timer.  It is configured to start automatically.  The syntax below
//tells the system to use the settings we configured using e2 Studio.
//Configuration settings can also be changed in code.

void hal_entry(void)
{
   led_timer0.p_api->open(led_timer0.p_ctrl, led_timer0.p_cfg);
}

//This is the interrupt callback function.  It returns a pointer to a
//structure which includes the event which triggered the callback. Ordinarily,
//you would use that information to decide what to do.  In this case, the only
//event we care about is TIMER_EVENT_EXPIRED.  Since nothing else is going on
//we don't need to bother checking the event.

void led_timer0_callback(timer_callback_args_t * p_args)
{
    g_ioport.p_api->pinWrite(IOPORT_PORT_07_PIN_00, OnOff);
    g_ioport.p_api->pinWrite(IOPORT_PORT_07_PIN_01, OffOn);
    g_ioport.p_api->pinWrite(IOPORT_PORT_07_PIN_02, OnOff);
    g_ioport.p_api->pinWrite(IOPORT_PORT_07_PIN_03, OffOn);

    OnOff = !OnOff; //Toggle
    OffOn = !OffOn; //Toggle
}

Now, build the project and program the board as usual. If you need to review how to do this, the earlier 4LED Blinky tutorial explains how: Updated 4 LED Blinky on S3A7 Fast Prototyping Board
It is definitely worth playing with the timing to see how that works.


Updated 4 LED Blinky on S3A7 Fast Prototyping Board
Blinky on the S3A7 IoT Fast Prototyping Kit
Blinky on the S3A7 IoT Fast Prototyping Kit
#2

Dan,

I appreciate your post!!! I tried to do it with Eric’s lesson 2 timer. His lesson 2 is wonderful. Because I am new to e2 studio, I did not understand what he meant about setting the timer period. I was struggling with the clock overflow (10 msec default timer’s period).

The manual does not mention much except for the api call. Your picture of the timer configuration is very helpful

You save me a lot of time! I am going to try it tomorrow. Thank you.

Michael


#3

Hi Michael,

I’m glad it was helpful. Thanks for going through it. I’m looking forward to going through your post on using the debugger. I’ve been meaning to dive into that, but I haven’t had the time.

Dan