#include "fsl_debug_console.h"
#include "board.h"
#include "app.h"
#include "fsl_tpm.h"
#include "fsl_rgpio.h"
#include "fsl_lpuart.h"
/*******************************************************************************
* Definitions
******************************************************************************/
/*******************************************************************************
* Prototypes
******************************************************************************/
/*******************************************************************************
* Variables
******************************************************************************/
volatile bool tpmIsrFlag = false;
volatile uint32_t risingTime = 0;
volatile uint32_t fallingTime = 0;
volatile bool gotRisingEdge = false;
/*******************************************************************************
* Code
******************************************************************************/
void Init_LPUART(void)
{
lpuart_config_t config;
/* Get default UART config (8N1, no parity, no flow control) */
LPUART_GetDefaultConfig(&config);
/* Modify parameters as needed */
config.baudRate_Bps = 115200U;
config.enableTx = true;
config.enableRx = true;
/* Initialize the UART with the specified parameters */
LPUART_Init(BOARD_LPUART, &config, BOARD_LPUART_CLK_FREQ);
}
float CalculateDistance(void)
{
uint32_t ticks;
if (fallingTime >= risingTime)
ticks = fallingTime - risingTime;
else
ticks = (TPM_MAX_COUNTER_VALUE(DEMO_TPM_BASEADDR) - risingTime) + fallingTime; /* Overflow handled */
float tpmClk = CLOCK_GetIpFreq(LPTPM_CLOCK_ROOT); /* You may adjust to TPM clock */
float timeUs = (ticks * 1.0f / tpmClk) * 1e6;
PRINTF("TPM Clock : %.2f\r\n",tpmClk);
float distanceCm = timeUs / 58.0f;
return distanceCm;
}
void SetUp(void)
{
rgpio_pin_config_t out_config ={
kRGPIO_DigitalOutput,
0,
};
/* Trigger pin */
RGPIO_PinInit(BOARD_RGPIO,TRIG_PIN,&out_config);
RGPIO_PinWrite(BOARD_RGPIO,TRIG_PIN,0);
}
void SendTrigPulse(void)
{
RGPIO_PinWrite(BOARD_RGPIO, TRIG_PIN, 1);
SDK_DelayAtLeastUs(10, SystemCoreClock);
RGPIO_PinWrite(BOARD_RGPIO, TRIG_PIN, 0);
}
void TPM_INPUT_CAPTURE_HANDLER(void)
{
uint32_t status = TPM_GetStatusFlags(DEMO_TPM_BASEADDR);
if (status & TPM_CHANNEL_FLAG)
{
uint32_t capturedValue = TPM_GetChannelValue(DEMO_TPM_BASEADDR, BOARD_TPM_INPUT_CAPTURE_CHANNEL);
if (!gotRisingEdge)
{
risingTime = capturedValue;
gotRisingEdge = true;
// Switch to falling edge detection
TPM_SetupInputCapture(DEMO_TPM_BASEADDR,BOARD_TPM_INPUT_CAPTURE_CHANNEL,kTPM_FallingEdge);
}
else
{
fallingTime = capturedValue;
tpmIsrFlag = true;
gotRisingEdge = false;
// Switch back to rising edge detection for next measurement
TPM_SetupInputCapture(DEMO_TPM_BASEADDR,BOARD_TPM_INPUT_CAPTURE_CHANNEL,kTPM_RisingEdge);
}
// Clear interrupt flag for this channel
TPM_ClearStatusFlags(DEMO_TPM_BASEADDR, TPM_CHANNEL_FLAG);
}
SDK_ISR_EXIT_BARRIER;
// tpmIsrFlag = true;
// /* Clear interrupt flag.*/
// TPM_ClearStatusFlags(DEMO_TPM_BASEADDR, TPM_CHANNEL_FLAG);
// SDK_ISR_EXIT_BARRIER;
}
/*!
* @brief Main function
*/
int main(void)
{
tpm_config_t tpmInfo;
/* Board pin, clock, debug console init */
BOARD_InitHardware();
SetUp(); /* Set up the GPIO */
// Init_LPUART(); /* Initialise one more UART */
/* Print a note to terminal */
PRINTF("\r\nTPM input capture example\r\n");
PRINTF("\r\nOnce the input signal is received the input capture value is printed\r\n");
TPM_GetDefaultConfig(&tpmInfo);
/* Initialize TPM module */
TPM_Init(DEMO_TPM_BASEADDR, &tpmInfo);
/* Setup input capture on a TPM channel */
TPM_SetupInputCapture(DEMO_TPM_BASEADDR, BOARD_TPM_INPUT_CAPTURE_CHANNEL, kTPM_RisingEdge);
/* Set the timer to be in free-running mode */
TPM_SetTimerPeriod(DEMO_TPM_BASEADDR, TPM_MAX_COUNTER_VALUE(DEMO_TPM_BASEADDR));
/* Enable channel interrupt when the second edge is detected */
TPM_EnableInterrupts(DEMO_TPM_BASEADDR, TPM_CHANNEL_INTERRUPT_ENABLE);
/* Enable at the NVIC */
EnableIRQ(TPM_INTERRUPT_NUMBER);
TPM_StartTimer(DEMO_TPM_BASEADDR, kTPM_SystemClock);
while (1)
{
tpmIsrFlag = false;
SendTrigPulse();
while (!tpmIsrFlag)
{
__NOP(); // Wait for capture
}
float distance = CalculateDistance();
PRINTF("RisingTime : %u\r\n",risingTime);
PRINTF("FallingTime : %u\r\n",fallingTime);
PRINTF("Distance: %.2f cm\r\n", distance);
SDK_DelayAtLeastUs(60000,SystemCoreClock);
}
}