i am using 2 gpt timer in hal layer 1timer for 1ms and 2nd one is for 1microsecond and i also use timer callback routine also..

now the thing is when i start the timer 2  for one micro second in timer 1 callback routine so my code is not work and curser goes in bsp_cfg handelr error 

  • Hi Niku-
    Have you tried not starting Timer 2 from the callback? It is best to get out of the callback quickly- perhaps by setting a flag or semaphore that can be used to start the timer from the application..

  • In reply to WarrenM:

    yes , i tried and it will work proper.
    semaphore and that type of thing i have not much knowledge so can you give me reference code if you have base on my requirement
  • In reply to niku:

    Hi Niku-
    If you are using HAL only (No ThreadX) you could just use a global flag. A semaphore is only needed in a threaded application.

    Check out this article on semaphores if you want to learn about them.

  • In reply to WarrenM:

    i have doubt in timer what is the functionality in gpt timer GTIOCA AND GTIOCB PIN ?
    is it decide raising edge and falling edge or something else?
    please clear my doubt
    and one more thing i am using gpt timer and generate square wave pulse for 1 ms period but i can not able to identified my pulse genereate raising edge or falling edge how i can seen please help this two things and clear my doubt.
  • In reply to niku:


    The GTIOCA and GTIOCB pins are the timer I/O pins associated to a particular GPT channel.
    For example, the S7G2 has 14 channels of GPT.
    GPT0 has pins GTIOC0A & GTIOC0B
    GPT1 has pins GTIOC1A & GTIOC1B
    GPT2 has pins GTIOC2A & GTIOC2B
    These pins can be used to output (generate) wavefroms or be used to capture (time) input waveforms.

    The GPT is a complex, but extremely powerful timer unit. Please refer to the Hardware User's Manual of the applicable Synergy device you are using for a detailed explanation of the GPT functionality. For example, if you are using an S7G2 device the Hardware Users Manual can be downloaded from:


    Please refer to 23. General PWM Timer (GPT)

    You we see that the GPT driver within the SSP has extremely limited functionality compared to what the GPT peripheral can actually do. A lot of the GPT functionality has to be achieved by you writing your own driver code. This is easily doe, but it does rely on a good understanding of the peripheral.

    You mention that you are trying to generate a 1ms square wave but cannot view the output?
    Which channel of GPT are you using?
    In the GPT timer configuration have you set the "GTIOCA Output Enabled" / "GTIOCB Output Enabled"to "True"?
    In the pin configuration tab, have you enabled the I/O pins of the corresponding GPT.
    For example, if you are using GPT0, then P511 & P512 have to be enabled in the Peripherals -> GPT -> GPT0 section

  • In reply to Richard:

    thanks for replay..
    i am using custom board
    Actually I am using GPT timer in HAl DRIVER and generate square wave 1ms using pwm mode channel no 6 and GTIOCB Output is true this is my configuration according to my custom board pin and generate sqare wave one particular pin in controller and i seen sqw pulse on cro.

    but my requirement is i want to collect adc data when my square wave pulse is high period time only so i could not able to understand my squre wave pulse start at raising edge or falling edge so how i can check my pulse is start raising is or falling edge?

    and you mentioned in your answer
    "These pins can be used to output (generate) wavefroms or be used to capture (time) input waveforms. "

    my confusion is if my GTIOCA or GTIOCB pin if i select as pin level low so my square wave pulse start low to high(raising edge )
    if i select pin level is high my square wave pulse start high to low(falling edge)

    And one more thing is i used one more gpt timer 1microsecond as periodic mode in hal driver for purpose only my square wave pulse is while high only that time my 1 microsecond timer will start and collect the adc data and i use callback routine also but my two timer not synchronize proper and cursor goes internal bsp cfg error so give me some solution and please overcome to this problem

    i used external adc in my project hardware so i did not use any adc driver in code just took data and collect
  • In reply to niku:


    I have uploaded a project (S7G2_SK_timer.zip) to the media gallery that may help. I have used GPT0 and GPT1, but the ideas can be ported to the GPT channels that you are using.

    GPT0 is generating a 1ms PWM, 50% duty.
    GPT1 is triggered from the PWM rising edge.
    GPT1 generates a 1us interrupt, handled by the callback function
    In the callback, GPT1 stops itself from counting and resets itself, waiting for the next PWM rising edge.
    PWM is on GPT0 GTIOCB0 - P511
    GPT1 callback toggles an I/O pin in the callback, P411

    I have added a Powerpoint with more explanation to the S7G2_SK_timer.zip

  • In reply to Richard:

    Hello Richards,

    I was refer your S7G2_SK_timer code and many things is useful of my requirement..
    i just ask one question about your timer code you did 1microsecond timer start only once per 1ms pulse cycle. right..

    but my requirement is my 1 microsecond timer will start only my square wave pulse high time . not only once but continue run until my square wave pulse is not low . while my pulse is low then 1 microsecond timer will be stop. so how i can do this ?
    i made in your code some changes and stop 1 microsecond so that timer run continuously whether my pulse high and low peroid
  • In reply to niku:

    Hello Niku

    So you want something like this:

    Where the purple trace is being generated by the 1us interrupt callback function.

    The code below does this.   You will see that it is very similar to previous code example.

    In the previous code example version I started GPT1 on an ELC event but I used an interrupt to stop the timer. 


    This version uses the same ELC event to start the timer GPT1 (the rising edge of GPT0 - yellow trace )

    but I now use another ELC event to stop the timer (falling edge of GPT0) 


    Therefore, GPT1 will now run for GPT0 high period. 


    Hope this helps and is of interest.




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

    ssp_err_t ssp_err;

    void hal_entry(void)
        /* TODO: add your own code here */

        /* Open GPT0 - configured to generate 1ms PWN @ 50% duty */
        ssp_err = g_gpt0.p_api->open(g_gpt0.p_ctrl, g_gpt0.p_cfg);
        if(SSP_SUCCESS != ssp_err)

        /* GPT0 GTIOCB0 has been enabled
         * Set the starting condition of this pin
         * Refer to Hardware Users Manual
         * 23.2.14 General PWM Timer I/O Control Register (GTIOR)
         * Table 23.5 Settings of GTIOA[4:0] and GTIOB[4:0] bits
        R_GPTA0->GTIOR_b.GTIOB = 0x6;       /* Set initial output low - Output high at GTCCRB compare match - Output low at cycle end */

        /* Open GPT1 - 1us interrupt generated on rising edge of PWM */
        ssp_err = g_gpt1.p_api->open(g_gpt1.p_ctrl, g_gpt1.p_cfg);
        if(SSP_SUCCESS != ssp_err)

        /* Configure GPT1 to START on ELC_GPTB event.  We will link GPT0 compare match B to ELC_GPTB */
        R_GPTA1->GTSSR_b.SSELCB = 1;    /* GPT1 can be started by ELC event */

        /* Configure GPT1 to STOP on ELC_GPTH event. We will link GPT0 overflow to ELC_GPTB */
        R_GPTA1->GTPSR_b.PSELCH = 1;    /* GPT1 can be stoped by ELC event */

        /* Link ELC_GPTB to GPT0 Compare Match B */

        /* Link ELC_GPTH to GPT0 Overflow */

        /* Start GPT0 - PWM timer
         * GPT1 will be started automatically be GPT0

    void cb_gpt1(timer_callback_args_t *p_args)

        /* As an example, toggle an I/O pin to show interrupt occuring 1us
         * after the GPT0 rising edge
        static uint8_t toggle_p411 = 0;

        if( 0 == toggle_p411 )
            R_IOPORT4->PCNTR1 = 0x08000800;     /* Set pin high / pin is output */
            R_IOPORT4->PCNTR1 = 0x00000800;    /* Clear pin low / pin is output */
        toggle_p411 = (uint8_t)(1 - toggle_p411);