1 /*
2 * Copyright (c) 2016, Freescale Semiconductor, Inc.
3 * Copyright 2017 NXP
4 * All rights reserved.
5 *
6 * SPDX-License-Identifier: BSD-3-Clause
7 */
8
9 /**
10 * @file dp5004_interrupt.c
11 * @brief The dp5004_interrupt.c file implements the ISSDK MPXVDP5004 analog sensor
12 * driver example demonstration with interrupt mode.
13 */
14
15 //-----------------------------------------------------------------------
16 // SDK Includes
17 //-----------------------------------------------------------------------
18 #include "pin_mux.h"
19 #include "clock_config.h"
20 #include "board.h"
21 #include "fsl_lptmr.h"
22 #include "fsl_adc12.h"
23 #include "fsl_debug_console.h"
24
25 //-----------------------------------------------------------------------
26 // ISSDK Includes
27 //-----------------------------------------------------------------------
28 #include "issdk_hal.h"
29 #include "gpio_driver.h"
30
31 //-----------------------------------------------------------------------
32 // Macros
33 //-----------------------------------------------------------------------
34 /* Timer timeout Callback. */
35 #define dp5004_odr_callback PWT_LPTMR0_IRQHandler
36 /* ADC completion Callback. */
37 #define adc12_irq_callback ADC0_IRQHandler
38
39 /* The desired ODR in milli seconds for the Pressure output */
40 #define MPXV5004DR_ODR_ms 1000U
41
42 //-----------------------------------------------------------------------
43 // Global Variables
44 //-----------------------------------------------------------------------
45 volatile uint32_t gAdcConversionValue;
46 volatile bool bAdcCompletedFlag = false;
47 adc12_channel_config_t gAdcChannelConfigStruct;
48
49 //-----------------------------------------------------------------------
50 // Functions
51 //-----------------------------------------------------------------------
52 /* LPTMR based ODR control Timer Callback function. */
dp5004_odr_callback(void)53 void dp5004_odr_callback(void)
54 {
55 LPTMR_ClearStatusFlags(LPTMR0, kLPTMR_TimerCompareFlag);
56 /* Trigger ADC Conversion */
57 ADC12_SetChannelConfig(ADC0, MPXVDP5004_ADC12_CHANNEL_GROUP, &gAdcChannelConfigStruct);
58 }
59
60 /* ADC12 based voltage conversion completion IRQ Callback function. */
adc12_irq_callback(void)61 void adc12_irq_callback(void)
62 {
63 /* Read conversion result to clear the conversion completed flag. */
64 gAdcConversionValue = ADC12_GetChannelConversionValue(ADC0, MPXVDP5004_ADC12_CHANNEL_GROUP);
65 bAdcCompletedFlag = true;
66 }
67
KE15Z_LPTMR0_ADC0_Initialize(void)68 void KE15Z_LPTMR0_ADC0_Initialize(void)
69 {
70 lptmr_config_t lptmrConfig;
71 adc12_config_t adc12ConfigStruct;
72
73 /* Configure ODR Timer. */
74 LPTMR_GetDefaultConfig(&lptmrConfig);
75 LPTMR_Init(LPTMR0, &lptmrConfig);
76 LPTMR_SetTimerPeriod(LPTMR0, MSEC_TO_COUNT(MPXV5004DR_ODR_ms, CLOCK_GetFreq(kCLOCK_LpoClk)));
77 LPTMR_EnableInterrupts(LPTMR0, kLPTMR_TimerInterruptEnable);
78 EnableIRQ(PWT_LPTMR0_IRQn);
79 LPTMR_StartTimer(LPTMR0);
80
81 /* Configure ADC. */
82 /* Set ADC12's clock source to be Slow IRC async clock. */
83 CLOCK_SetIpSrc(kCLOCK_Adc0, kCLOCK_IpSrcSircAsync);
84 EnableIRQ(ADC0_IRQn);
85 ADC12_GetDefaultConfig(&adc12ConfigStruct);
86 adc12ConfigStruct.clockSource = kADC12_ClockSourceAlt0;
87 adc12ConfigStruct.resolution = kADC12_Resolution12Bit;
88 ADC12_Init(ADC0, &adc12ConfigStruct);
89 /* Set to software trigger mode. */
90 ADC12_EnableHardwareTrigger(ADC0, false);
91 /* Calibrate ADC. */
92 if (kStatus_Success != ADC12_DoAutoCalibration(ADC0))
93 {
94 PRINTF("ADC calibration failed!\r\n");
95 }
96 gAdcChannelConfigStruct.channelNumber = MPXVDP5004_ADC12_CHANNEL;
97 /* Enable the interrupt. */
98 gAdcChannelConfigStruct.enableInterruptOnConversionCompleted = true;
99 }
100
101 /*!
102 * @brief Main function
103 */
main(void)104 int main(void)
105 {
106 uint16_t pressurePascals;
107 GENERIC_DRIVER_GPIO *gpioDriver = &Driver_GPIO_KSDK;
108
109 BOARD_InitPins();
110 BOARD_BootClockRUN();
111 BOARD_InitDebugConsole();
112
113 PRINTF("\r\n MPXV5004DP ADC based Interrupt Example\r\n");
114
115 /*! Initialize GREEN LED pin used by FRDM board */
116 gpioDriver->pin_init(&GREEN_LED, GPIO_DIRECTION_OUT, NULL, NULL, NULL);
117
118 /* Initialize LPTMR and ADC framework */
119 KE15Z_LPTMR0_ADC0_Initialize();
120
121 /* Forever loop */
122 while (true)
123 {
124 if (false == bAdcCompletedFlag)
125 {
126 SMC_SetPowerModeWait(SMC);
127 continue;
128 }
129 else
130 {
131 bAdcCompletedFlag = false;
132 gpioDriver->toggle_pin(&GREEN_LED);
133 }
134
135 pressurePascals = (uint16_t)MPXV5004DP_PRESSURE_FROM_ADC_VALUE(gAdcConversionValue);
136 PRINTF("\r\n Differential Pressure Value: %dPa\r\n", pressurePascals);
137 ASK_USER_TO_RESUME(10); /* Ask for user input after processing 10 samples. */
138 }
139 }
140