/*
 * File:		LPTMR_pulse.c
 * Purpose:		Pulse counting Examples
 *
 */


#include "common.h"
#include "demo_lp_timer.h"

/*
 * Counts pulses found on LPTMR0_ALT1, LPTMR0_ALT2, or LPTMR0_ALT3
 *
 *  LPTMR0_ALT1 is pin PORTA19 (ALT6)
 *   On TWR-K60N512, PORTA19 is conected to pin 18 on J5
 * LPTMR0_ALT2 is pin PORTC5 (ALT3).
 *   On K70 Daughter Card, use A70 on the Elevator Board
 * LPTMR0_ALT3 is pin PORTE17 (ALT6)
 *   However this pin is associated with UART2, and setting it to LPTMR_ALT3
 *    interfers with UART communication, so it is not used in this example.
 *
 *
 *
 */
void lptmr_pulse_counter(char pin_select)
{
    unsigned int compare_value=1000;
    char input;
    printf("\n\n****************************\n");
    printf("LPTMR Pulse Counting Example on LPTMR_ALT%d\n\n",pin_select);
    
    //Reset LPTMR module
    lptmr_clear_registers();
    
    //Set up GPIO
    if(pin_select==LPTMR_ALT1)
    {
        SIM_SCGC5 |= SIM_SCGC5_PORTA_MASK; //Turn on PORTA clock
        PORTA_PCR19=PORT_PCR_MUX(0x6); //Use ALT6 on PTA19
        
        printf("Testing ALT1 pin on PORTA19\n");
        printf("\tTWR-K70F120M: ALT1 is conected to pin 18 on J15\n");
    }
    else if(pin_select==LPTMR_ALT2)
    {
        SIM_SCGC5 |= SIM_SCGC5_PORTC_MASK; //Turn on PORTC clock
        PORTC_PCR5=PORT_PCR_MUX(0x4); //Use ALT4 on PTC5
        
        printf("Testing ALT2 pin on PORTC5\n");
        printf("\tTWR-K70F120M: ALT2 is conected A70 on TWR-ELEV\n");
    }
    else
    {
        printf("Invalid pin selected\n");
        printf("****************************\n");
        return;
    }
    
    /* Test requires external hardware. Need to confirm if want to run test or not */
    printf("\nThis test requires a function generator, or another way of producing a pulse signal on the pin specified above. ");
    printf("Please connect that device to the specified pin\n\n");
    printf("If you would like to continue with this example, press \"y\". To skip press any other key\n");
    input=in_char(); //wait for keyboard press
    printf("\n");
    if(input!='y' && input!='Y')
    {
        printf("Exiting LPTMR Pulse Counting Example on LPTMR_ALT%d\n",pin_select);
        printf("****************************\n");
        return;
    }
    
    LPTMR0_PSR=LPTMR_PSR_PCS(0x1)|LPTMR_PSR_PBYP_MASK; //Use LPO clock but bypass glitch filter
    LPTMR0_CMR=LPTMR_CMR_COMPARE(compare_value);  //Set compare value
    LPTMR0_CSR=LPTMR_CSR_TPS(pin_select)|LPTMR_CSR_TMS_MASK; //Set LPT to use the pin selected, and put in pulse count mode, on rising edge (default)
    
    printf("Press any key to start pulse counter\n");
    in_char(); //wait for keyboard press
    
    LPTMR0_CSR|=LPTMR_CSR_TEN_MASK; //Turn on LPT
    
    //Wait for compare flag to be set
    while((LPTMR0_CSR&LPTMR_CSR_TCF_MASK)==0)
    {
        //This may not get proper counter data if the CNR is read at the same time it is incremented
        printf("Current value of pulse count register CNR is %d\n",get_counter_value());
    }
    
    printf("Detected %d pulses on LPTMR_ALT%d\n",compare_value,pin_select);
    printf("End of Pulse Counting Example\n");
    printf("****************************\n");
}
