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