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: http://learn.iotcommunity.io/t/updated-4-led-blinky-on-s3a7-fast-prototyping-board/849 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.
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 */
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.
//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)
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: http://learn.iotcommunity.io/t/updated-4-led-blinky-on-s3a7-fast-prototyping-board/849
It is definitely worth playing with the timing to see how that works.