Mahesh Phalke / Mbed OS EVAL-AD7606

Dependencies:   platform_drivers

Revision:
6:75e922b3859a
Parent:
3:83b3133f544a
Child:
7:94f36455773c
--- a/app/ad7606_data_capture.c	Mon Oct 19 07:54:56 2020 +0000
+++ b/app/ad7606_data_capture.c	Thu Jul 15 13:21:42 2021 +0530
@@ -87,7 +87,7 @@
 static volatile uint32_t active_channels = 0;
 
 /* Number of active channels */
-static uint8_t num_of_active_channels = 0;
+static volatile uint8_t num_of_active_channels = 0;
 
 /* Minimum number of bytes to be read from device over digital interface */
 static volatile uint8_t min_bytes_to_read = 0;
@@ -96,10 +96,10 @@
 static volatile uint8_t chn_read_cnt = 0;
 
 /* Number of samples requested by IIO client */
-static uint16_t num_of_samples = 0;
+static volatile uint16_t num_of_samples = 0;
 
 /* Max available size of buffer */
-static uint16_t max_available_buffer_size = 0;
+static volatile uint16_t max_available_buffer_size = 0;
 
 /******************************************************************************/
 /************************ Functions Declarations ******************************/
@@ -117,16 +117,9 @@
  * @param	device[in]- Device instance
  * @return	none
  */
-int32_t iio_data_capture_init(struct ad7606_dev *device)
+int32_t iio_data_capture_init(struct ad7606_dev **device)
 {
-	/* Save the device structure for background conversion */
-	dev = malloc(sizeof(struct ad7606_dev *));
-	if (!dev) {
-		return FAILURE;
-	}
-
-	memcpy(&dev, &device, sizeof(dev));
-
+	dev = *device;
 	return SUCCESS;
 }
 
@@ -191,33 +184,32 @@
  * @param	samples_to_read[in] - Number of samples to read
  * @return	none
  */
-static void wait_and_read_new_samples(volatile acq_buf_t *input_buffer,
-				      char *output_buffer,
+static void wait_and_read_new_samples(char *output_buffer,
 				      size_t samples_to_read)
 {
 	int32_t buff_rd_wr_indx_offset; 			// Offset b/w buffer read and write indexes
-	uint32_t timeout = BUF_READ_TIMEOUT;  		// Buffer new data read timeout count
+	uint32_t timeout = BUF_READ_TIMEOUT;   		// Buffer new data read timeout count
 	size_t bytes = samples_to_read * BYTES_PER_SAMPLE;
 
 	/* Copy the bytes into buffer provided there is enough offset b/w read and write counts.
 	* If there is overlap b/w read and write indexes (read is faster than write), empty buffer
 	* should be returned to IIO client to avoid IIO request timeout */
 	do {
-		buff_rd_wr_indx_offset = (int32_t)input_buffer->wr_indx -
-					 (int32_t)input_buffer->rd_indx;
+		buff_rd_wr_indx_offset = acq_buffer.wr_indx - acq_buffer.rd_indx;
 		timeout--;
 	} while (((buff_rd_wr_indx_offset < (int32_t)(samples_to_read))
-		  || (input_buffer->wr_indx < input_buffer->rd_indx)) && (timeout > 0));
+		  || (acq_buffer.wr_indx < acq_buffer.rd_indx)) && (timeout > 0)
+		 && start_adc_data_capture);
 
-	if ((timeout == 0) || (input_buffer->wr_indx <= 0)) {
+	if ((timeout == 0) || (acq_buffer.wr_indx <= 0)) {
 		/* This returns the empty buffer */
 		return;
 	}
 
 	memcpy(output_buffer,
-	       (void const *)&input_buffer->data[input_buffer->rd_indx],
+	       (void const *)&acq_buffer.data[acq_buffer.rd_indx],
 	       bytes);
-	input_buffer->rd_indx += samples_to_read;
+	acq_buffer.rd_indx += samples_to_read;
 }
 
 
@@ -241,7 +233,7 @@
 		return bytes;
 	}
 
-	wait_and_read_new_samples(&acq_buffer, pbuf, samples_to_read);
+	wait_and_read_new_samples(pbuf, samples_to_read);
 
 	/* Make buffer available again once read completely */
 	if (acq_buffer.rd_indx >= max_available_buffer_size) {
@@ -264,7 +256,7 @@
  *			results are read into acquisition buffer and next conversion is triggered.
  *			This continues until background conversion is stopped.
  */
-void do_conversion_callback(void)
+void do_conversion_callback(void *ctx, uint32_t event, void *extra)
 {
 #if (AD7606X_ADC_RESOLUTION == 18)
 	uint8_t arr_indx = 0;
@@ -275,10 +267,8 @@
 #endif
 
 	if (start_adc_data_capture == true) {
-
 		/* Read the conversion result for required number of bytes */
 		if (ad7606_read_conversion_data(dev, min_bytes_to_read) == SUCCESS) {
-
 			/* Extract the data based on the active channels selected in IIO client.
 			 * Note: The extraction of data based on active channel needs
 			 * to be done since its not possible to capture individual