ISSDK  1.8
IoT Sensing Software Development Kit
dp5004_interrupt.c
Go to the documentation of this file.
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. */
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. */
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 
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  }
97  /* Enable the interrupt. */
98  gAdcChannelConfigStruct.enableInterruptOnConversionCompleted = true;
99 }
100 
101 /*!
102  * @brief Main function
103  */
104 int main(void)
105 {
106  uint16_t pressurePascals;
107  GENERIC_DRIVER_GPIO *gpioDriver = &Driver_GPIO_KSDK;
108 
109  BOARD_InitPins();
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 */
120 
121  /* Forever loop */
122  while (true)
123  {
124  if (false == bAdcCompletedFlag)
125  {
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 }
#define ASK_USER_TO_RESUME(x)
Definition: frdm_k64f.h:106
volatile uint32_t gAdcConversionValue
#define MPXV5004DP_PRESSURE_FROM_ADC_VALUE(x)
#define adc12_irq_callback
Access structure of the GPIO Driver.
Definition: Driver_GPIO.h:38
#define MPXVDP5004_ADC12_CHANNEL
#define SMC
Definition: lpc54114.h:118
int main(void)
Main function.
adc12_channel_config_t gAdcChannelConfigStruct
#define MPXVDP5004_ADC12_CHANNEL_GROUP
#define dp5004_odr_callback
#define BOARD_BootClockRUN
Definition: clock_config.h:19
void KE15Z_LPTMR0_ADC0_Initialize(void)
GENERIC_DRIVER_GPIO Driver_GPIO_KSDK
Definition: gpio_driver.c:177
void(* toggle_pin)(pinID_t aPinId)
Definition: Driver_GPIO.h:48
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:155
void(* pin_init)(pinID_t aPinId, gpio_direction_t dir, void *apPinConfig, gpio_isr_handler_t aIsrHandler, void *apUserData)
Definition: Driver_GPIO.h:41
gpioHandleKSDK_t GREEN_LED
Definition: frdm_k64f.c:188
void BOARD_InitDebugConsole(void)
Definition: board.c:15
volatile bool bAdcCompletedFlag
void BOARD_InitPins(void)
Configures pin routing and optionally pin electrical features.
Definition: pin_mux.c:47
#define MPXV5004DR_ODR_ms