ISSDK  1.8
IoT Sensing Software Development Kit
fxps7250a4_interrupt.c
Go to the documentation of this file.
1 /*
2  * Copyright 2019 NXP
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 
8 /**
9  * @file fxps7250a4_interrupt.c
10  * @brief The fxps7250a4_interrupt.c file implements the ISSDK FXPS7250A4 analog sensor
11  * driver example demonstration with interrupt mode.
12  */
13 
14 //-----------------------------------------------------------------------
15 // SDK Includes
16 //-----------------------------------------------------------------------
17 #include "pin_mux.h"
18 #include "clock_config.h"
19 #include "board.h"
20 #include "fsl_lptmr.h"
21 #include "fsl_adc12.h"
22 #include "fsl_debug_console.h"
23 
24 //-----------------------------------------------------------------------
25 // ISSDK Includes
26 //-----------------------------------------------------------------------
27 #include "issdk_hal.h"
28 #include "gpio_driver.h"
29 
30 //-----------------------------------------------------------------------
31 // Macros
32 //-----------------------------------------------------------------------
33 /* Timer timeout Callback. */
34 #define pa7250_odr_callback PWT_LPTMR0_IRQHandler
35 /* ADC completion Callback. */
36 #define adc12_irq_callback ADC0_IRQHandler
37 
38 /* The desired ODR in milli seconds for the Pressure output */
39 #define FXPS7250A4_ODR_ms 1000U
40 
41 //-----------------------------------------------------------------------
42 // Global Variables
43 //-----------------------------------------------------------------------
44 volatile uint32_t gAdcConversionValue;
45 volatile bool bAdcCompletedFlag = false;
46 adc12_channel_config_t gAdcChannelConfigStruct;
47 
48 //-----------------------------------------------------------------------
49 // Functions Prototypes
50 //-----------------------------------------------------------------------
52 
53 float analog_output_transfer_fn(float volt);
54 
55 //-----------------------------------------------------------------------
56 // Functions
57 //-----------------------------------------------------------------------
58 /* LPTMR based ODR control Timer Callback function. */
60 {
61  LPTMR_ClearStatusFlags(LPTMR0, kLPTMR_TimerCompareFlag);
62  /* Trigger ADC Conversion */
63  ADC12_SetChannelConfig(ADC0, FXPS7250A4_ADC12_CHANNEL_GROUP, &gAdcChannelConfigStruct);
64 }
65 
66 /* ADC12 based voltage conversion completion IRQ Callback function. */
68 {
69  /* Read conversion result to clear the conversion completed flag. */
70  gAdcConversionValue = ADC12_GetChannelConversionValue(ADC0, FXPS7250A4_ADC12_CHANNEL_GROUP);
71  bAdcCompletedFlag = true;
72 }
73 
75 {
76  lptmr_config_t lptmrConfig;
77  adc12_config_t adc12ConfigStruct;
78 
79  /* Configure ODR Timer. */
80  LPTMR_GetDefaultConfig(&lptmrConfig);
81  LPTMR_Init(LPTMR0, &lptmrConfig);
82  LPTMR_SetTimerPeriod(LPTMR0, MSEC_TO_COUNT(FXPS7250A4_ODR_ms, CLOCK_GetFreq(kCLOCK_LpoClk)));
83  LPTMR_EnableInterrupts(LPTMR0, kLPTMR_TimerInterruptEnable);
84  EnableIRQ(PWT_LPTMR0_IRQn);
85  LPTMR_StartTimer(LPTMR0);
86 
87  /* Configure ADC. */
88  /* Set ADC12's clock source to be Slow IRC async clock. */
89  CLOCK_SetIpSrc(kCLOCK_Adc0, kCLOCK_IpSrcSircAsync);
90  EnableIRQ(ADC0_IRQn);
91  ADC12_GetDefaultConfig(&adc12ConfigStruct);
92  adc12ConfigStruct.clockSource = kADC12_ClockSourceAlt0;
93  adc12ConfigStruct.resolution = kADC12_Resolution12Bit;
94  ADC12_Init(ADC0, &adc12ConfigStruct);
95  /* Set to software trigger mode. */
96  ADC12_EnableHardwareTrigger(ADC0, false);
97  /* Calibrate ADC. */
98  if (kStatus_Success != ADC12_DoAutoCalibration(ADC0))
99  {
100  PRINTF("ADC calibration failed!\r\n");
101  }
103  /* Enable the interrupt. */
104  gAdcChannelConfigStruct.enableInterruptOnConversionCompleted = true;
105 }
106 
107 float analog_output_transfer_fn(float volt)
108 {
109  float pressure = 0.0;
110  pressure = ((volt - 0.2)/0.02) + FXPS7250A4_OFFSET;
111 
112  return pressure;
113 }
114 
115 /*!
116  * @brief Main function
117  */
118 int main(void)
119 {
120  float outVolt = 0.0;
121  float pressurekPa = 0.0;
122 
123  GENERIC_DRIVER_GPIO *gpioDriver = &Driver_GPIO_KSDK;
124 
125  BOARD_InitPins();
128 
129  PRINTF("\r\n FXPS7250A4 ADC based Interrupt Example\r\n");
130 
131  /*! Initialize GREEN LED pin used by FRDM board */
132  gpioDriver->pin_init(&GREEN_LED, GPIO_DIRECTION_OUT, NULL, NULL, NULL);
133 
134  /* Initialize LPTMR and ADC framework */
136 
137  /* Forever loop */
138  while (true)
139  {
140  if (false == bAdcCompletedFlag)
141  {
143  continue;
144  }
145  else
146  {
147  bAdcCompletedFlag = false;
148  gpioDriver->toggle_pin(&GREEN_LED);
149  }
150 
151  outVolt = (gAdcConversionValue * VCC)/BIT12RES;
152  pressurekPa = analog_output_transfer_fn(outVolt);
153  PRINTF("\r\n Raw Data Value: %d", gAdcConversionValue);
154  PRINTF("\r\n Calculated Voltage Value: %0.2f Volts", outVolt);
155  PRINTF("\r\n Analog Pressure Value: %0.2f kPa \n", pressurekPa);
156  ASK_USER_TO_RESUME(10); /* Ask for user input after processing 10 samples. */
157  }
158 }
#define FXPS7250A4_ADC12_CHANNEL_GROUP
void KE15Z_LPTMR0_ADC0_Initialize(void)
#define ASK_USER_TO_RESUME(x)
Definition: frdm_k64f.h:106
Access structure of the GPIO Driver.
Definition: Driver_GPIO.h:38
#define SMC
Definition: lpc54114.h:118
#define BIT12RES
volatile bool bAdcCompletedFlag
#define BOARD_BootClockRUN
Definition: clock_config.h:19
GENERIC_DRIVER_GPIO Driver_GPIO_KSDK
Definition: gpio_driver.c:177
#define FXPS7250A4_OFFSET
#define pa7250_odr_callback
#define FXPS7250A4_ODR_ms
#define VCC
adc12_channel_config_t gAdcChannelConfigStruct
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
int main(void)
Main function.
void(* pin_init)(pinID_t aPinId, gpio_direction_t dir, void *apPinConfig, gpio_isr_handler_t aIsrHandler, void *apUserData)
Definition: Driver_GPIO.h:41
volatile uint32_t gAdcConversionValue
#define FXPS7250A4_ADC12_CHANNEL
float analog_output_transfer_fn(float volt)
gpioHandleKSDK_t GREEN_LED
Definition: frdm_k64f.c:188
#define adc12_irq_callback
void BOARD_InitDebugConsole(void)
Definition: board.c:15
void BOARD_InitPins(void)
Configures pin routing and optionally pin electrical features.
Definition: pin_mux.c:47