ISSDK  1.7
IoT Sensing Software Development Kit
dp5004_demo.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_demo.c
37  * @brief The dp5004_demo.c file implements the ISSDK MPXVDP5004 analog sensor
38  * driver demo demonstration in interrupt mode with FRDM-KE15Z.
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 
50 //-----------------------------------------------------------------------
51 // CMSIS Includes
52 //-----------------------------------------------------------------------
53 #include "Driver_USART.h"
54 
55 //-----------------------------------------------------------------------
56 // ISSDK Includes
57 //-----------------------------------------------------------------------
58 #include "issdk_hal.h"
59 #include "gpio_driver.h"
60 #include "host_io_uart.h"
61 #include "systick_utils.h"
62 #include "auto_detection_service.h"
63 
64 //-----------------------------------------------------------------------
65 // Macros
66 //-----------------------------------------------------------------------
67 /* Timer timeout Callback. */
68 #define dp5004_odr_callback PWT_LPTMR0_IRQHandler
69 /* ADC completion Callback. */
70 #define adc12_irq_callback ADC0_IRQHandler
71 
72 /* The desired ODR in milli seconds for the Pressure output */
73 #define MPXV5004DP_ODR_ms 100U
74 /* The size of Streaming Data in Host Message. */
75 #define MPXV5004DP_STREAM_DATA_SIZE 6U
76 
77 /*! @brief Unique Name for this application which should match the target GUI pkg name. */
78 #define APPLICATION_NAME "Analog Pressure Sensor Demo (MPXV5004DP)"
79 /*! @brief Version to distinguish between instances the same application based on target Shield and updates. */
80 #define APPLICATION_VERSION "2.5"
81 
82 //-----------------------------------------------------------------------
83 // Data Types
84 //-----------------------------------------------------------------------
85 /*! @brief This structure defines the mpxv5004dp data buffer.*/
86 typedef struct
87 {
88  uint32_t timestamp; /*!< Time stamp value in micro-seconds. */
89  uint16_t pressure; /*!< Sensor pressure output: unsigned 16-bits. */
91 
92 //-----------------------------------------------------------------------
93 // Global Variables
94 //-----------------------------------------------------------------------
97 volatile bool bAdc12ConversionCompletedFlag = false, bStreamingEnabled = false, bDp5004Ready = false;
98 adc12_channel_config_t gAdcChannelConfigStruct;
99 volatile uint32_t gAdcConversionValue;
100 uint8_t gStreamID; /* The auto assigned Stream ID. */
103 
104 //-----------------------------------------------------------------------
105 // Functions
106 //-----------------------------------------------------------------------
107 /* LPTMR based ODR control Timer Callback function. */
109 {
110  LPTMR_ClearStatusFlags(LPTMR0, kLPTMR_TimerCompareFlag);
111  /* Trigger ADC Conversion */
112  ADC12_SetChannelConfig(ADC0, MPXVDP5004_ADC12_CHANNEL_GROUP, &gAdcChannelConfigStruct);
113 }
114 
115 /* ADC12 based voltage conversion completion IRQ Callback function. */
117 {
118  /* Read conversion result to clear the conversion completed flag. */
119  gAdcConversionValue = ADC12_GetChannelConversionValue(ADC0, MPXVDP5004_ADC12_CHANNEL_GROUP);
121 }
122 
124 {
125  lptmr_config_t lptmrConfig;
126  adc12_config_t adc12ConfigStruct;
127 
128  /* Configure ODR Timer. */
129  LPTMR_GetDefaultConfig(&lptmrConfig);
130  LPTMR_Init(LPTMR0, &lptmrConfig);
131  LPTMR_SetTimerPeriod(LPTMR0, MSEC_TO_COUNT(MPXV5004DP_ODR_ms, CLOCK_GetFreq(kCLOCK_LpoClk)));
132  LPTMR_EnableInterrupts(LPTMR0, kLPTMR_TimerInterruptEnable);
133  EnableIRQ(PWT_LPTMR0_IRQn);
134 
135  /* Configure ADC. */
136  /* Set ADC12's clock source to be Slow IRC async clock. */
137  CLOCK_SetIpSrc(kCLOCK_Adc0, kCLOCK_IpSrcSircAsync);
138  EnableIRQ(ADC0_IRQn);
139  ADC12_GetDefaultConfig(&adc12ConfigStruct);
140  adc12ConfigStruct.clockSource = kADC12_ClockSourceAlt0;
141  adc12ConfigStruct.resolution = kADC12_Resolution12Bit;
142  ADC12_Init(ADC0, &adc12ConfigStruct);
143  /* Set to software trigger mode. */
144  ADC12_EnableHardwareTrigger(ADC0, false);
145  /* Calibrate ADC. */
146  if (kStatus_Success == ADC12_DoAutoCalibration(ADC0))
147  {
148  bDp5004Ready = true;
149  }
151  /* Enable the interrupt. */
152  gAdcChannelConfigStruct.enableInterruptOnConversionCompleted = true;
153 }
154 
155 /* Handler for Device Info and Streaming Control Commands. */
157  uint8_t tag, uint8_t *hostCommand, uint8_t *hostResponse, size_t *hostMsgSize, size_t respBufferSize)
158 {
159  bool success = false;
160 
161  /* If it is Host requesting Device Info, send Board Name and Shield Name. */
162  if (tag == HOST_PRO_INT_DEV_TAG)
163  { /* Byte 1 : Payload - Length of APPLICATION_NAME (b)
164  * Bytes=b : Payload Application Name
165  * Byte b+1 : Payload - Length of BOARD_NAME (s)
166  * Bytes=s : Payload Board Name
167  * Byte b+s+2 : Payload - Length of SHIELD_NAME (v)
168  * Bytes=v : Payload Shield Name */
169 
170  size_t appNameLen = strlen(embAppName);
171  size_t boardNameLen = strlen(boardString);
172  size_t shieldNameLen = strlen(shieldString);
173 
174  if (respBufferSize >= boardNameLen + shieldNameLen + appNameLen + 3)
175  { /* We have sufficient buffer. */
176  *hostMsgSize = 0;
177  }
178  else
179  {
180  return false;
181  }
182 
183  hostResponse[*hostMsgSize] = appNameLen;
184  *hostMsgSize += 1;
185 
186  memcpy(hostResponse + *hostMsgSize, embAppName, appNameLen);
187  *hostMsgSize += appNameLen;
188 
189  hostResponse[*hostMsgSize] = boardNameLen;
190  *hostMsgSize += 1;
191 
192  memcpy(hostResponse + *hostMsgSize, boardString, boardNameLen);
193  *hostMsgSize += boardNameLen;
194 
195  hostResponse[*hostMsgSize] = shieldNameLen;
196  *hostMsgSize += 1;
197 
198  memcpy(hostResponse + *hostMsgSize, shieldString, shieldNameLen);
199  *hostMsgSize += shieldNameLen;
200 
201  return true;
202  }
203 
204  /* If it is Host sending Streaming Commands, take necessary actions. */
205  if ((tag == (HOST_PRO_INT_CMD_TAG | HOST_PRO_CMD_W_CFG_TAG)) &&
207  { /* Byte 1 : Payload - Operation Code (Start/Stop Operation Code)
208  * Byte 2 : Payload - Stream ID (Target Stream for carrying out operation) */
209  switch (hostCommand[0]) /* Execute desired operation (Start/Stop) on the requested Stream. */
210  {
211  case HOST_CMD_START:
212  if (hostCommand[1] == gStreamID && bDp5004Ready && bStreamingEnabled == false)
213  {
215  bStreamingEnabled = true;
216  LPTMR_StartTimer(LPTMR0);
217  success = true;
218  }
219  break;
220  case HOST_CMD_STOP:
221  if (hostCommand[1] == gStreamID && bDp5004Ready && bStreamingEnabled == true)
222  {
223  pGpioDriver->clr_pin(&GREEN_LED);
224  bStreamingEnabled = false;
225  LPTMR_StopTimer(LPTMR0);
226  success = true;
227  }
228  break;
229  default:
230  break;
231  }
232  *hostMsgSize = 0; /* Zero payload in response. */
233  }
234 
235  return success;
236 }
237 
238 /*!
239  * @brief Main function
240  */
241 int main(void)
242 {
243  int32_t status;
246 
247  ARM_DRIVER_USART *pUartDriver = &HOST_S_DRIVER;
248 
251 
252  /* Create the Short Application Name String for ADS. */
253  sprintf(embAppName, "%s:%s", APPLICATION_NAME, APPLICATION_VERSION);
254 
255  /* Run ADS. */
257 
258  /* Create the Full Application Name String for Device Info Response. */
260 
261  /*! Initialize GREEN LED pin used by FRDM board */
262  pGpioDriver->pin_init(&GREEN_LED, GPIO_DIRECTION_OUT, NULL, NULL, NULL);
263 
264  /*! Initialize the UART driver. */
265  status = pUartDriver->Initialize(HOST_S_SIGNAL_EVENT);
266  if (ARM_DRIVER_OK != status)
267  {
268  return -1;
269  }
270 
271  /*! Set the UART Power mode. */
272  status = pUartDriver->PowerControl(ARM_POWER_FULL);
273  if (ARM_DRIVER_OK != status)
274  {
275  return -1;
276  }
277 
278  /*! Set UART Baud Rate. */
279  status = pUartDriver->Control(ARM_USART_MODE_ASYNCHRONOUS, BOARD_DEBUG_UART_BAUDRATE);
280  if (ARM_DRIVER_OK != status)
281  {
282  return -1;
283  }
284 
285  /* Initialize LPTMR and ADC framework */
287 
288  /*! Initialize streaming and assign a Stream ID. */
289  gStreamID = Host_IO_Init(pUartDriver, NULL, NULL, NULL, 0);
290  /* Confirm if a valid Stream ID has been allocated for this stream. */
291  if (0 == gStreamID)
292  {
293  bDp5004Ready = false;
294  }
295  else
296  {
297  /*! Populate streaming header. */
299  pGpioDriver->clr_pin(&GREEN_LED); /* Set LED to indicate application is ready. */
300  }
301 
302  for (;;) /* Forever loop */
303  { /* Call UART Non-Blocking Receive. */
305 
306  if (false == bAdc12ConversionCompletedFlag)
307  {
309  continue;
310  }
311  else
312  {
314  pGpioDriver->toggle_pin(&GREEN_LED);
315  }
316 
317  /* Update timestamp from Systick framework. */
319 
321 
322  /* Copy samples to Streaming Buffer. */
323  memcpy(streamingPacket + STREAMING_HEADER_LEN, &rawData, MPXV5004DP_STREAM_DATA_SIZE);
324 
325  /* Send streaming packet to Host. */
326  Host_IO_Send(streamingPacket, sizeof(streamingPacket), HOST_FORMAT_HDLC);
327  }
328 }
uint32_t BOARD_SystickElapsedTime_us(int32_t *pStart)
Function to compute the Elapsed Time.
Definition: systick_utils.c:99
uint8_t gStreamID
Definition: dp5004_demo.c:100
volatile bool bDp5004Ready
Definition: dp5004_demo.c:97
#define HOST_PRO_CMD_W_CFG_TAG
Definition: host_io_uart.h:89
#define dp5004_odr_callback
Definition: dp5004_demo.c:68
#define APPLICATION_VERSION
Version to distinguish between instances the same application based on target Shield and updates...
Definition: dp5004_demo.c:80
#define HOST_S_SIGNAL_EVENT
Definition: frdm_k64f.h:120
char embAppName[ADS_MAX_STRING_LENGTH]
Definition: dp5004_demo.c:96
void Host_IO_Receive(host_cmd_proc_fn_t process_host_command, uint8_t encoding)
Definition: host_io_uart.c:233
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
int32_t status
void(* toggle_pin)(pinID_t aPinId)
Definition: Driver_GPIO.h:74
#define SHIELD_NAME
char shieldString[ADS_MAX_STRING_LENGTH]
Definition: dp5004_demo.c:95
#define ADS_MAX_STRING_LENGTH
#define HOST_PRO_INT_DEV_TAG
Definition: host_io_uart.h:85
uint8_t streamingPacket[STREAMING_HEADER_LEN+FXLS8962_STREAM_DATA_SIZE]
#define HOST_PRO_INT_CMD_TAG
Bit aligned values for Host Protocol Interface IDs (Bits 5-6).
Definition: host_io_uart.h:83
#define MPXV5004DP_STREAM_DATA_SIZE
Definition: dp5004_demo.c:75
bool process_host_command(uint8_t tag, uint8_t *hostCommand, uint8_t *hostResponse, size_t *hostMsgSize, size_t respBufferSize)
Definition: dp5004_demo.c:156
int32_t gSystick
Definition: dp5004_demo.c:101
GENERIC_DRIVER_GPIO * pGpioDriver
Definition: dp5004_demo.c:102
gpioHandleKSDK_t GREEN_LED
Definition: frdm_k64f.c:214
volatile uint32_t gAdcConversionValue
Definition: dp5004_demo.c:99
#define MPXVDP5004_ADC12_CHANNEL
char boardString[ADS_MAX_STRING_LENGTH]
Definition: dp5004_demo.c:95
void Host_IO_Add_ISO_Header(uint8_t streamID, uint8_t *pStreamingPacket, size_t sizePayload)
Definition: host_io_uart.c:112
void Host_IO_Send(uint8_t *pMsg, size_t size, uint8_t encoding)
Definition: host_io_uart.c:162
void(* clr_pin)(pinID_t aPinId)
Definition: Driver_GPIO.h:73
#define adc12_irq_callback
Definition: dp5004_demo.c:70
#define APPLICATION_NAME
Unique Name for this application which should match the target GUI pkg name.
Definition: dp5004_demo.c:78
void BOARD_SystickEnable(void)
Function to enable systicks framework.
Definition: systick_utils.c:70
#define MPXVDP5004_ADC12_CHANNEL_GROUP
GENERIC_DRIVER_GPIO Driver_GPIO_KSDK
Definition: gpio_driver.c:203
typedef int32_t(DATA_FORMAT_Append_t))(void *pData
The interface function to append the data on the formated stream.
#define BOARD_DEBUG_UART_BAUDRATE
Definition: board.h:57
volatile bool bStreamingEnabled
Definition: dp5004_demo.c:97
adc12_channel_config_t gAdcChannelConfigStruct
Definition: dp5004_demo.c:98
Access structure of the GPIO Driver.
Definition: Driver_GPIO.h:64
void BOARD_SystickStart(int32_t *pStart)
Function to Record the Start systick.
Definition: systick_utils.c:79
#define MPXV5004DP_ODR_ms
Definition: dp5004_demo.c:73
ARM Systick Utilities.
void BOARD_RunADS(const char *appName, char *boardString, char *shieldString, size_t bufferLength)
The function to register Application Name and initialte ADS.
#define MPXV5004DP_PRESSURE_FROM_ADC_VALUE(x)
fxls8962_acceldataUser_t rawData
This structure defines the mpxv5004dp data buffer.
Definition: dp5004_demo.c:86
int main(void)
Main function.
Definition: dp5004_demo.c:241
uint8_t Host_IO_Init(ARM_DRIVER_USART *pDrv, void *pBus, void *pDevInfo, void *spiSlaveParams, uint16_t slaveAddress)
Definition: host_io_uart.c:126
#define SMC
Definition: lpc54114.h:144
The host_io_uart.h file contains the Host Protocol interface definitions and configuration.
#define STREAMING_HEADER_LEN
Definition: host_io_uart.h:51
volatile bool bAdc12ConversionCompletedFlag
Definition: dp5004_demo.c:97
void KE15Z_LPTMR0_ADC0_Initialize(void)
Definition: dp5004_demo.c:123
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
#define HOST_S_DRIVER
Definition: frdm_k64f.h:119