// Built with BFTC Rev. 1.0.0, lun 25 agosto 2014 17:00:44

/*

    Copyright 2014 Digital Technology Art SRL

    This file is part of Blackfin Toolchain (BFTC) project.

 

    BFTC is free software: you can redistribute it and/or modify

    it under the terms of the GNU General Public License as published by

    the Free Software Foundation, either version 3 of the License, or

    (at your option) any later version.

 

    BFTC is distributed in the hope that it will be useful,

    but WITHOUT ANY WARRANTY; without even the implied warranty of

    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the

    GNU General Public License for more details.

 

    You should have received a copy of the GNU General Public License

    along with Nome-Programma.  If not, see <http://www.gnu.org/licenses/>.

*/

 

// Test of reflexes speed

// Connect on PG0 a switch in this mode:

//            10K Resistor

// Y14.1 ---+----[====]---+

//          |             |

//           [-( BUTTON   |

//          |             |

// Y12.4 ---+             +--- Y12.6

//

// Run the program and when the string START is displayed press the button

// at your maximum speed.

// Repeat the test for an average value.

//

#include "main.h"

 

#define ST st = Get_TimerCounter(TIMER_0)

#define ET et = Get_TimerCounter(TIMER_0)

 

#define TIMEBASE 0xFFFFFFFF

 

unsigned int st, et;

 

void display()

{

    double tx;

    if(st > et) tx = TIMEBASE - st + et;

    else tx = et - st;

    printf("Elapsed time: %10.3f ms\n", tx / 100000.0);

}

 

// Interrupt service procedure

//

void PortCallback()

{

    ET;

   display();

    // Clear the interrupt request

    *pPORTGIO_CLEAR = PG0;

}

 

unsigned randx(unsigned val)

{

    return Get_TimerCounter(TIMER_0) * 0x1961957 + val;

}

 

int main(void)

{

    Set_PLL(16, 4);     // CORE: 25MHz * 16 = 400MHz, SCLK: 400MHz / 4 = 100MHz

    Set_Port();         // Set the port according to project set

    Set_Uart0(115200);  // printf is redirected to UART0

   

    // Set an interrupt on the rising edge of the PG0 port

    Set_PortGInterruptService(PG0, PortCallback);

    Set_PortGInterruptEnable(PG0, true);

   Set_TimerCounter(TIMER_0);

    Set_TimerEnable(TIMER_0, true);

    while(1)            // Main loop

    {

       printf("START!\n");

       ST;

       DelayUs(500000 + randx(et) % 1000000);

    }

    return 0;

}