ISSDK  1.7
IoT Sensing Software Development Kit
dp5004_interrupt.c
Go to the documentation of this file.
1 /*
2  * The Clear BSD License
3  * Copyright (c) 2017, Freescale Semiconductor, Inc.
4  * Copyright 2017 NXP
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without modification,
8  * are permitted (subject to the limitations in the disclaimer below) provided
9  * that the following conditions are met:
10  *
11  * o Redistributions of source code must retain the above copyright notice, this list
12  * of conditions and the following disclaimer.
13  *
14  * o Redistributions in binary form must reproduce the above copyright notice, this
15  * list of conditions and the following disclaimer in the documentation and/or
16  * other materials provided with the distribution.
17  *
18  * o Neither the name of the copyright holder nor the names of its
19  * contributors may be used to endorse or promote products derived from this
20  * software without specific prior written permission.
21  *
22  * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
27  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
30  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 /**
36  * @file dp5004_interrupt.c
37  * @brief The dp5004_interrupt.c file implements the ISSDK MPXVDP5004 analog sensor
38  * driver example demonstration with interrupt mode.
39  */
40 
41 //-----------------------------------------------------------------------
42 // SDK Includes
43 //-----------------------------------------------------------------------
44 #include "board.h"
45 #include "pin_mux.h"
46 #include "fsl_lptmr.h"
47 #include "fsl_adc12.h"
48 #include "clock_config.h"
49 #include "fsl_debug_console.h"
50 
51 //-----------------------------------------------------------------------
52 // ISSDK Includes
53 //-----------------------------------------------------------------------
54 #include "issdk_hal.h"
55 #include "gpio_driver.h"
56 
57 //-----------------------------------------------------------------------
58 // Macros
59 //-----------------------------------------------------------------------
60 /* Timer timeout Callback. */
61 #define dp5004_odr_callback PWT_LPTMR0_IRQHandler
62 /* ADC completion Callback. */
63 #define adc12_irq_callback ADC0_IRQHandler
64 
65 /* The desired ODR in milli seconds for the Pressure output */
66 #define MPXV5004DR_ODR_ms 1000U
67 
68 //-----------------------------------------------------------------------
69 // Global Variables
70 //-----------------------------------------------------------------------
71 volatile uint32_t gAdcConversionValue;
72 volatile bool bAdcCompletedFlag = false;
73 adc12_channel_config_t gAdcChannelConfigStruct;
74 
75 //-----------------------------------------------------------------------
76 // Functions
77 //-----------------------------------------------------------------------
78 /* LPTMR based ODR control Timer Callback function. */
80 {
81  LPTMR_ClearStatusFlags(LPTMR0, kLPTMR_TimerCompareFlag);
82  /* Trigger ADC Conversion */
83  ADC12_SetChannelConfig(ADC0, MPXVDP5004_ADC12_CHANNEL_GROUP, &gAdcChannelConfigStruct);
84 }
85 
86 /* ADC12 based voltage conversion completion IRQ Callback function. */
88 {
89  /* Read conversion result to clear the conversion completed flag. */
90  gAdcConversionValue = ADC12_GetChannelConversionValue(ADC0, MPXVDP5004_ADC12_CHANNEL_GROUP);
91  bAdcCompletedFlag = true;
92 }
93 
95 {
96  lptmr_config_t lptmrConfig;
97  adc12_config_t adc12ConfigStruct;
98 
99  /* Configure ODR Timer. */
100  LPTMR_GetDefaultConfig(&lptmrConfig);
101  LPTMR_Init(LPTMR0, &lptmrConfig);
102  LPTMR_SetTimerPeriod(LPTMR0, MSEC_TO_COUNT(MPXV5004DR_ODR_ms, CLOCK_GetFreq(kCLOCK_LpoClk)));
103  LPTMR_EnableInterrupts(LPTMR0, kLPTMR_TimerInterruptEnable);
104  EnableIRQ(PWT_LPTMR0_IRQn);
105  LPTMR_StartTimer(LPTMR0);
106 
107  /* Configure ADC. */
108  /* Set ADC12's clock source to be Slow IRC async clock. */
109  CLOCK_SetIpSrc(kCLOCK_Adc0, kCLOCK_IpSrcSircAsync);
110  EnableIRQ(ADC0_IRQn);
111  ADC12_GetDefaultConfig(&adc12ConfigStruct);
112  adc12ConfigStruct.clockSource = kADC12_ClockSourceAlt0;
113  adc12ConfigStruct.resolution = kADC12_Resolution12Bit;
114  ADC12_Init(ADC0, &adc12ConfigStruct);
115  /* Set to software trigger mode. */
116  ADC12_EnableHardwareTrigger(ADC0, false);
117  /* Calibrate ADC. */
118  if (kStatus_Success != ADC12_DoAutoCalibration(ADC0))
119  {
120  PRINTF("ADC calibration failed!\r\n");
121  }
123  /* Enable the interrupt. */
124  gAdcChannelConfigStruct.enableInterruptOnConversionCompleted = true;
125 }
126 
127 /*!
128  * @brief Main function
129  */
130 int main(void)
131 {
132  uint16_t pressurePascals;
133  GENERIC_DRIVER_GPIO *gpioDriver = &Driver_GPIO_KSDK;
134 
135  BOARD_InitPins();
138 
139  PRINTF("\r\n MPXV5004DP ADC based Interrupt Example\r\n");
140 
141  /*! Initialize GREEN LED pin used by FRDM board */
142  gpioDriver->pin_init(&GREEN_LED, GPIO_DIRECTION_OUT, NULL, NULL, NULL);
143 
144  /* Initialize LPTMR and ADC framework */
146 
147  /* Forever loop */
148  while (true)
149  {
150  if (false == bAdcCompletedFlag)
151  {
153  continue;
154  }
155  else
156  {
157  bAdcCompletedFlag = false;
158  gpioDriver->toggle_pin(&GREEN_LED);
159  }
160 
161  pressurePascals = (uint16_t)MPXV5004DP_PRESSURE_FROM_ADC_VALUE(gAdcConversionValue);
162  PRINTF("\r\n Differential Pressure Value: %dPa\r\n", pressurePascals);
163  ASK_USER_TO_RESUME(10); /* Ask for user input after processing 10 samples. */
164  }
165 }
#define adc12_irq_callback
void(* pin_init)(pinID_t aPinId, gpio_direction_t dir, void *apPinConfig, gpio_isr_handler_t aIsrHandler, void *apUserData)
Definition: Driver_GPIO.h:67
#define BOARD_BootClockRUN
Definition: clock_config.h:45
volatile bool bAdcCompletedFlag
void(* toggle_pin)(pinID_t aPinId)
Definition: Driver_GPIO.h:74
void KE15Z_LPTMR0_ADC0_Initialize(void)
void BOARD_InitDebugConsole(void)
Definition: board.c:41
#define MPXV5004DR_ODR_ms
adc12_channel_config_t gAdcChannelConfigStruct
volatile uint32_t gAdcConversionValue
gpioHandleKSDK_t GREEN_LED
Definition: frdm_k64f.c:214
#define ASK_USER_TO_RESUME(x)
Definition: frdm_k64f.h:132
#define MPXVDP5004_ADC12_CHANNEL
#define MPXVDP5004_ADC12_CHANNEL_GROUP
GENERIC_DRIVER_GPIO Driver_GPIO_KSDK
Definition: gpio_driver.c:203
#define dp5004_odr_callback
Access structure of the GPIO Driver.
Definition: Driver_GPIO.h:64
#define MPXV5004DP_PRESSURE_FROM_ADC_VALUE(x)
#define SMC
Definition: lpc54114.h:144
status_t SMC_SetPowerModeWait(void *arg)
Configures the system to WAIT power mode. API name used from Kinetis family to maintain compatibility...
Definition: lpc54114.c:181
int main(void)
Main function.
void BOARD_InitPins(void)
Configures pin routing and optionally pin electrical features.
Definition: pin_mux.c:73