pin_write() & pin_read() through register manipulation

I wish to implement Pin_write() & pin_read() by register manipulation. Can ayone help?

  • I use these instructions for example on P000:

    R_IOPORT0->PCNTR1 = 0x00000001; //P000 = 1


    R_IOPORT0->PCNTR1 = 0x00000000; //P000 = 0

  • Hello Jeremy, 

    I tried this, however the high  & low time were not 1us but around 3.5 us. Also i would require making that same pin as input and reading the pin. How do i do that again on same register manipulations.??
     R_IOPORT5->PCNTR3 |= 0x00800000;//Making p5.7 low/high

    R_IOPORT5->PCNTR3 |= 0x00000080;//Making p5.7 low/high

  • For an accurate delay I would suggest you use a hardware timer.

    Have you read the I/O Port chapter of the relevant Synergy hardware manual, for the details of the registers used to configure, and read from the pins of a Synergy device?

  • Dear jeremy,

    I used a GPT Timer with period  1 Microseconds. Still Didn't get the low time & high time as 1 us, it comes roughly around 2.5 us (Checked on the scope). I am posting my code for the same below here. Moreover when I checked by changing the timer period to 10 us, it toggled the pin successfully. Any thoughts on why this must be happening?Why i am able to get 10us time and not 1us even after using hardware timer.

    //timer start

    mirosecond_timer.p_api->open(mirosecond_timer.p_ctrl, mirosecond_timer.p_cfg);

    //handler function

    void mirosecond_timer_callback(timer_callback_args_t *p_args)
    if (TIMER_EVENT_EXPIRED == p_args->event)
    if(miros_counter ==1)
    R_IOPORT5->PCNTR3 |= 0x00800000;
    R_IOPORT5->PCNTR3 |= 0x00000080;

  • Use the HW timer to toggle the pins directly.

  • What is HW Timer? I am using GPT timer here. Could you please help how?

  • Configure the GPT timer to output to a timer pin :-

    configure the pin for the GPT channel used :-

    Start the open the timer driver (auto start is enabled in this example, so no need to call start ) :-

        ssp_err_t err;

        err = g_timer0.p_api->open(g_timer0.p_ctrl, g_timer0.p_cfg);
        if (SSP_SUCCESS != err)

    and the GPT timer will toggle the pin directly.

  • Dear Jeremy,

    This wont be of any use to me as I do not have just toggle a pin with low/high time of 1 us. That isnt my requirement. I am writing a 1wire Driver wherein  i have to make the pin low (followed by n us delay) as well as configure it as i/p. This will be done repeatedly in  different communication sequences. Now 1 wire has strict delays in each communication sequences varying from 1 us to 1600 us. which is why i was trying to toggle a pin at every 1us for demonstration. 

    What i observed is neither the R_BSP_SoftwareDelay(5, BSP_DELAY_UNITS_MICROSECONDS) or hardware timers were able to generate  precise us delay. This is the problem i need help on.


    Himanshu M.

  • You are probably not going to get the accuracy by using the BSP_software delay function or by writing to the PCNTR3 in an ISR.  The ISR latency within the SSP is relatively high and access to the port pin via register access is again relatively slow.

    However, you can still use a GPT timer to set and clear an I/O pin via the ELC (event link controller) and PCNTR4.

    In the attached example I use GPT0 to generate a Compare Match A interrupt and a Compare Match B interrupt.  These interrupts also generate events that can be used be the ELC to control an I/O pin.

    The ELC and Port control is set so that on a compare match A event, the pin is set low.

    The Compare Match A value = 1.  Therefore, when the GPT starts counting, on the fist GPT clock the i/o pin will go low.  At the same time the Compare Match A ISR is generated and in software the ELC and Port Control is set so that on a compare match B event, the pin is set high.

    The compare match B value = 120.  With the GPT running at 120Mhz, this equates to a time of 1us.

    Therefore, 1us later the i/o pin is set high.

    The compare match B ISR is called and the ELC and Port control is set back for compare match A event.

    At the same time, the ELC B event will stop the GPT from counting.

    In this example, written for the SK_S7G2 I use the push buttons to 1. generate the pulse by pressing S4 and 2. changing the pulse width by pressing S5.

    You will see that to minimize interrupt latency time I create my own ISRs, rather than using the ISR created by the SSP.

    I also set the GPT register values directly to get the required use case.

    It isn't clear at which point you wish to change the I/O port from an output to input, or how many output pulses you need to make at any one time.  This is all possible by using the ISR and if required, using other GPT timers to count pulse generated etc.

    Hope this helps


    1us delay: