Here's a simple example setup that you can examine and craft to your needs.
The attached code snippet uses a SK-S7G2 for the platform. SSP 1.1.1 is used.
Pulse input is P100. This is CH0 of the AGT on the AGTIO0 channel.
There is a 1 second 'tick' that is output from P400. This is an output from a GPT timer at a 1 second interval.
In the Synergy Configurator, you can see that initially the AGT is configured just to get the module added into the code.
All the code happens in EventCount.c
Here's the basic steps:
- Open the GPT (for the 1 second interval)
- Open the AGT to get the module up and running
- Directly manipulate the AGT registers to setup rising edge trigger captures. SSP currently does not implement Event Capture in the configuration
- Start both the AGT and GPT
- In the callback, the AGT is sampled and the number of pulses is calculated and stored in a global
- In the e2Studio Expressions window, you can see the result of the calculations when the 1 second GPT expires
Good luck!
#include "hal_data.h"
// declares
void TimerSetup(void);
// global variables
uint16_t PreviousCount = 0xFFFF; // initialized to start value of timer
uint16_t CurrentCount = 0;
void TimerSetup(void)
{
// open our timer counter for period (1 second intervals)
g_timer0_EventRead.p_api->open(g_timer0_EventRead.p_ctrl, g_timer0_EventRead.p_cfg);
g_timer0_EventRead.p_api->start(g_timer0_EventRead.p_ctrl);
// open the AGT timer, channel 0 here for pulse counting
// assumes that the GPIO port is setup to accept the incoming pulses
// note not typical of usage since timer mode takes no inputs typically. but open is needed here
g_timer1_EventCount.p_api->open(g_timer1_EventCount.p_ctrl, g_timer1_EventCount.p_cfg);
//stop the timer from counting
AGT_CH(0)->AGTCR_b.TSTART = (uint8_t)(0x00 & 0x01);
// put the timer into event count mode
AGT_CH(0)->AGTMR1_b.TMOD = (uint8_t)(AGT_MODE_EVENT_COUNTER & 0x07);
// select the edge for timer decrement
AGT_CH(0)->AGTIOC_b.TEDGSEL = (uint8_t)(0x00 & 0x01);
// clear out mode register
AGT_CH(0)->AGTCR = 0x00;
// set the AGT timer register count start value
AGT_CH(0)->AGT = (uint16_t)(0xFFFF & 0x0FFFF);
//Start the timer counting pulses
AGT_CH(0)->AGTCR_b.TSTART = (uint8_t)(0x01 & 0x01);
}
// declare contained in hal_data.h
void g_timer0_EventRead_Callback(timer_callback_args_t * p_args)
{
// simple quick toggle of IO port for 1 second period
g_ioport.p_api->pinWrite(IOPORT_PORT_04_PIN_00, IOPORT_LEVEL_HIGH);
R_BSP_SoftwareDelay(1, BSP_DELAY_UNITS_MICROSECONDS);
g_ioport.p_api->pinWrite(IOPORT_PORT_04_PIN_00, IOPORT_LEVEL_LOW);
// events are calculate by: Event number = initial value of counter [AGT register] - counter value of active event end
CurrentCount = (uint16_t)(PreviousCount - AGT_CH(0)->AGT);
PreviousCount = AGT_CH(0)->AGT;
}