From 2f634f5fd0e4b2f4766a7cf5df72d489204f2f29 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Cl=C3=A9ment=20Foucher?= <cfoucher@laas.fr>
Date: Wed, 25 Jan 2023 07:30:16 +0000
Subject: [PATCH] Reorganized data acquisition to prepare for future evolution.

This is a preliminary change to data acquisition intended at adapting its structure for future changes.
---
 .../zephyr/CMakeLists.txt                     |   4 +-
 .../data_dispatch.c                           |  84 ++++++--
 .../data_dispatch.h                           |  22 +-
 .../zephyr/adc_to_mem/dma.c                   | 132 ++++++++++++
 .../zephyr/{dma => adc_to_mem}/dma.h          |  32 ++-
 .../owntech_data_acquisition/zephyr/dma/dma.c | 191 ------------------
 .../zephyr/public_api/DataAcquisition.cpp     |  24 +--
 7 files changed, 236 insertions(+), 253 deletions(-)
 rename zephyr/modules/owntech_data_acquisition/zephyr/{data_dispatch => adc_to_mem}/data_dispatch.c (72%)
 rename zephyr/modules/owntech_data_acquisition/zephyr/{data_dispatch => adc_to_mem}/data_dispatch.h (84%)
 create mode 100644 zephyr/modules/owntech_data_acquisition/zephyr/adc_to_mem/dma.c
 rename zephyr/modules/owntech_data_acquisition/zephyr/{dma => adc_to_mem}/dma.h (50%)
 delete mode 100644 zephyr/modules/owntech_data_acquisition/zephyr/dma/dma.c

diff --git a/zephyr/modules/owntech_data_acquisition/zephyr/CMakeLists.txt b/zephyr/modules/owntech_data_acquisition/zephyr/CMakeLists.txt
index def8f47..315587d 100644
--- a/zephyr/modules/owntech_data_acquisition/zephyr/CMakeLists.txt
+++ b/zephyr/modules/owntech_data_acquisition/zephyr/CMakeLists.txt
@@ -7,8 +7,8 @@ if(CONFIG_OWNTECH_DATA_ACQUISITION)
 
   # Select source files to be compiled
   zephyr_library_sources(
-    ./dma/dma.c
-    ./data_dispatch/data_dispatch.c
+    ./adc_to_mem/dma.c
+    ./adc_to_mem/data_dispatch.c
 	./data_conversion/data_conversion.c
     ./public_api/DataAcquisition.cpp
    )
diff --git a/zephyr/modules/owntech_data_acquisition/zephyr/data_dispatch/data_dispatch.c b/zephyr/modules/owntech_data_acquisition/zephyr/adc_to_mem/data_dispatch.c
similarity index 72%
rename from zephyr/modules/owntech_data_acquisition/zephyr/data_dispatch/data_dispatch.c
rename to zephyr/modules/owntech_data_acquisition/zephyr/adc_to_mem/data_dispatch.c
index da12d03..5ed0ca7 100644
--- a/zephyr/modules/owntech_data_acquisition/zephyr/data_dispatch/data_dispatch.c
+++ b/zephyr/modules/owntech_data_acquisition/zephyr/adc_to_mem/data_dispatch.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2021-2022 LAAS-CNRS
+ * Copyright (c) 2021-2023 LAAS-CNRS
  *
  *   This program is free software: you can redistribute it and/or modify
  *   it under the terms of the GNU Lesser General Public License as published by
@@ -18,7 +18,7 @@
  */
 
 /**
- * @date   2022
+ * @date   2023
  *
  * @author Clément Foucher <clement.foucher@laas.fr>
  */
@@ -34,13 +34,15 @@
 // OwnTech API
 #include "adc.h"
 
+// Current module private functions
+#include "dma.h"
+
 
 /////
 // Local variables
 
 #define CHANNELS_BUFFERS_SIZE 32
-
-static uint8_t number_of_adcs = 0;
+#define ADC_COUNT 4
 
 // Number of channels in each ADC (cell i is ADC number i+1)
 static uint8_t* enabled_channels_count = NULL;
@@ -65,6 +67,14 @@ static uint8_t** current_buffer = NULL;
 // the peek() function after a buffer swap.
 static uint16_t** peek_memory = NULL;
 
+// DMA buffers: data from the ADC 1/2 are stored in these
+// buffers until dispatch is done (ADC 3/4 wwon't use DMA).
+// Main buffers are always used, while secondary buffers
+// will only be used when double-buffering is activated.
+static uint16_t* dma_main_buffers[ADC_COUNT]      = {NULL};
+static uint16_t* dma_secondary_buffers[ADC_COUNT] = {NULL};
+static uint8_t   current_dma_buffer[ADC_COUNT]    = {0};
+
 
 /////
 // Private functions
@@ -100,23 +110,42 @@ __STATIC_INLINE void _data_dispatch_swap_buffer(uint8_t adc_index, uint8_t chann
 /////
 // Public API
 
-void data_dispatch_init(uint8_t adc_count)
+void data_dispatch_init()
 {
-	// Store number on ADCs
-	number_of_adcs = adc_count;
-
 	// Prepare arrays for each ADC
-	enabled_channels_count = k_malloc(number_of_adcs * sizeof(uint8_t));
-	adc_channel_buffers    = k_calloc(number_of_adcs,  sizeof(uint16_t***));
-	buffers_data_count     = k_calloc(number_of_adcs,  sizeof(uint32_t*));
-	current_buffer         = k_calloc(number_of_adcs,  sizeof(uint8_t*));
-	peek_memory            = k_calloc(number_of_adcs,  sizeof(uint16_t*));
-
-	for (int adc_index = 0 ; adc_index < number_of_adcs ; adc_index++)
+	enabled_channels_count = k_malloc(ADC_COUNT * sizeof(uint8_t));
+	adc_channel_buffers    = k_calloc(ADC_COUNT,  sizeof(uint16_t***));
+	buffers_data_count     = k_calloc(ADC_COUNT,  sizeof(uint32_t*));
+	current_buffer         = k_calloc(ADC_COUNT,  sizeof(uint8_t*));
+	peek_memory            = k_calloc(ADC_COUNT,  sizeof(uint16_t*));
+
+	// DMA 1 & 2: use DMA
+	for (uint8_t adc_num = 1 ; adc_num <= ADC_COUNT ; adc_num++)
 	{
-		enabled_channels_count[adc_index] = adc_get_enabled_channels_count(adc_index+1);
+		uint8_t adc_index = adc_num-1;
+		enabled_channels_count[adc_index] = adc_get_enabled_channels_count(adc_num);
+
 		if (enabled_channels_count[adc_index] > 0)
 		{
+			// For now, stay on double buffering approach
+			bool enable_double_buffering = true;
+
+			// Prepare buffers for DMA
+			size_t dma_buffer_size = enabled_channels_count[adc_index] * sizeof(uint16_t);
+			if (enable_double_buffering == true)
+			{
+				dma_buffer_size = dma_buffer_size * 2;
+			}
+
+			dma_main_buffers[adc_index] = (uint16_t*)k_malloc(dma_buffer_size);
+			if (enable_double_buffering == true)
+			{
+				dma_secondary_buffers[adc_index] = dma_main_buffers[adc_index] + enabled_channels_count[adc_index];
+			}
+
+			// Initialize DMA
+			dma_configure_adc_acquisition(adc_num, enable_double_buffering, dma_main_buffers[adc_index], dma_buffer_size);
+
 			// Prepare arrays for each channel
 			adc_channel_buffers[adc_index] = k_malloc(enabled_channels_count[adc_index] * sizeof(uint16_t**));
 			buffers_data_count[adc_index]  = k_calloc(enabled_channels_count[adc_index],  sizeof(uint32_t));
@@ -133,9 +162,24 @@ void data_dispatch_init(uint8_t adc_count)
 	}
 }
 
-void data_dispatch_do_dispatch(uint8_t adc_num, uint16_t* dma_buffer)
+void data_dispatch_do_dispatch(uint8_t adc_num)
 {
-	uint8_t adc_index = adc_num-1;
+	uint8_t adc_index = adc_num - 1;
+
+	uint16_t* dma_buffer = dma_main_buffers[adc_index];
+	if (dma_secondary_buffers[adc_index] != NULL)
+	{
+		if (current_dma_buffer[adc_index] == 0)
+		{
+			current_dma_buffer[adc_index] = 1;
+		}
+		else
+		{
+			dma_buffer = dma_secondary_buffers[adc_index];
+			current_dma_buffer[adc_index] = 0;
+		}
+	}
+
 	for (int channel_index = 0 ; channel_index < enabled_channels_count[adc_index] ; channel_index++)
 	{
 		// Get info on buffer
@@ -157,7 +201,7 @@ void data_dispatch_do_dispatch(uint8_t adc_num, uint16_t* dma_buffer)
 uint16_t* data_dispatch_get_acquired_values(uint8_t adc_number, uint8_t channel_rank, uint32_t* number_of_values_acquired)
 {
 	uint8_t adc_index = adc_number-1;
-	if (adc_index < number_of_adcs)
+	if (adc_index < ADC_COUNT)
 	{
 		// Get info on buffer
 		uint16_t* active_buffer = _data_dispatch_get_buffer(adc_index, channel_rank);
@@ -186,7 +230,7 @@ uint16_t* data_dispatch_get_acquired_values(uint8_t adc_number, uint8_t channel_
 uint16_t data_dispatch_peek_acquired_value(uint8_t adc_number, uint8_t channel_rank)
 {
 	uint8_t adc_index = adc_number-1;
-	if (adc_index < number_of_adcs)
+	if (adc_index < ADC_COUNT)
 	{
 		// Get info on buffer
 		uint16_t* active_buffer = _data_dispatch_get_buffer(adc_index, channel_rank);
diff --git a/zephyr/modules/owntech_data_acquisition/zephyr/data_dispatch/data_dispatch.h b/zephyr/modules/owntech_data_acquisition/zephyr/adc_to_mem/data_dispatch.h
similarity index 84%
rename from zephyr/modules/owntech_data_acquisition/zephyr/data_dispatch/data_dispatch.h
rename to zephyr/modules/owntech_data_acquisition/zephyr/adc_to_mem/data_dispatch.h
index 8dafef6..ab953b2 100644
--- a/zephyr/modules/owntech_data_acquisition/zephyr/data_dispatch/data_dispatch.h
+++ b/zephyr/modules/owntech_data_acquisition/zephyr/adc_to_mem/data_dispatch.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2021-2022 LAAS-CNRS
+ * Copyright (c) 2021-2023 LAAS-CNRS
  *
  *   This program is free software: you can redistribute it and/or modify
  *   it under the terms of the GNU Lesser General Public License as published by
@@ -18,7 +18,7 @@
  */
 
 /**
- * @date   2022
+ * @date   2023
  *
  * @author Clément Foucher <clement.foucher@laas.fr>
  *
@@ -45,22 +45,17 @@ extern "C" {
 
 /**
  * Init function to be called first.
- *
- * @param adc_count Number of enabled ADCs
  */
-void data_dispatch_init(uint8_t adc_count);
+void data_dispatch_init();
 
 /**
- * Dispatch function: gets the readings and store
- * them in per-channel arrays. This functon is
- * called by DMA callback when the DMA has filled
- * one of its buffers.
+ * Dispatch function: gets the readings and store them
+ * in per-channel arrays. This functon is called by DMA
+ * callback when the DMA has filled one of its buffers.
  *
- * @param adc_num Number of the ADC from which data
- *        comes from.
- * @param dma_buffer Buffer in which data is stored.
+ * @param adc_number Number of the ADC from which data comes.
  */
-void data_dispatch_do_dispatch(uint8_t adc_num, uint16_t* dma_buffer);
+void data_dispatch_do_dispatch(uint8_t adc_number);
 
 /**
  * Obtain data for a specific channel.
@@ -82,7 +77,6 @@ void data_dispatch_do_dispatch(uint8_t adc_num, uint16_t* dma_buffer);
  */
 uint16_t* data_dispatch_get_acquired_values(uint8_t adc_number, uint8_t channel_rank, uint32_t* number_of_values_acquired);
 
-
 /**
  * Peek data for a specific channel:
  * obtain the latest value from the channel without
diff --git a/zephyr/modules/owntech_data_acquisition/zephyr/adc_to_mem/dma.c b/zephyr/modules/owntech_data_acquisition/zephyr/adc_to_mem/dma.c
new file mode 100644
index 0000000..2701d17
--- /dev/null
+++ b/zephyr/modules/owntech_data_acquisition/zephyr/adc_to_mem/dma.c
@@ -0,0 +1,132 @@
+/*
+ * Copyright (c) 2021-2023 LAAS-CNRS
+ *
+ *   This program is free software: you can redistribute it and/or modify
+ *   it under the terms of the GNU Lesser General Public License as published by
+ *   the Free Software Foundation, either version 2.1 of the License, or
+ *   (at your option) any later version.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU Lesser General Public License for more details.
+ *
+ *   You should have received a copy of the GNU Lesser General Public License
+ *   along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
+ * SPDX-License-Identifier: LGLPV2.1
+ */
+
+/**
+ * @date   2023
+ *
+ * @author Clément Foucher <clement.foucher@laas.fr>
+ */
+
+
+// Stdlib
+#include <stdint.h>
+
+// Zephyr
+#include <zephyr.h>
+#include <drivers/dma.h>
+
+// STM32
+#include <stm32_ll_dma.h>
+
+// OwnTech API
+#include "adc.h"
+
+// Current module private functions
+#include "data_dispatch.h"
+
+
+/////
+// DT definition
+
+static const struct device* dma1 = DEVICE_DT_GET(DT_NODELABEL(dma1));
+
+
+/////
+// LL definitions
+
+static uint32_t source_registers[4] =
+{
+	(uint32_t)(&(ADC1->DR)),
+	(uint32_t)(&(ADC2->DR)),
+	(uint32_t)(&(ADC3->DR)),
+	(uint32_t)(&(ADC4->DR))
+};
+
+static uint32_t source_triggers[4] =
+{
+	LL_DMAMUX_REQ_ADC1,
+	LL_DMAMUX_REQ_ADC2,
+	LL_DMAMUX_REQ_ADC3,
+	LL_DMAMUX_REQ_ADC4
+};
+
+
+/////
+// Private API
+
+/**
+ * DMA callback
+ * This callback is called on DMA interrupt.
+ * If double-buffering is enabled on the channel, is is
+ * called when buffer is half-filled and when buffer is filled.
+ * Otherwse is is only called once when buffer is filled.
+ */
+static void _dma_callback(const struct device* dev, void* user_data, uint32_t channel, int status)
+{
+	UNUSED(dev);
+	UNUSED(user_data);
+	UNUSED(status);
+
+	uint8_t adc_number = channel + 1;
+	data_dispatch_do_dispatch(adc_number);
+}
+
+
+/////
+// Public API
+
+void dma_configure_adc_acquisition(uint8_t adc_number, bool enable_double_buffering, uint16_t* buffer, size_t buffer_size)
+{
+	// Check environment
+	if (device_is_ready(dma1) == false)
+		return;
+
+	if (adc_get_enabled_channels_count(adc_number) == 0)
+		return;
+
+	uint8_t adc_index = adc_number - 1;
+
+	// Configure DMA
+	struct dma_block_config dma_block_config_s = {0};
+	dma_block_config_s.source_address   = source_registers[adc_index]; // Source: ADC DR register
+	dma_block_config_s.dest_address     = (uint32_t)buffer;            // Dest: buffer in memory
+	dma_block_config_s.block_size       = (uint32_t)buffer_size;       // Buffer size in bytes
+	dma_block_config_s.source_addr_adj  = DMA_ADDR_ADJ_NO_CHANGE;      // Source: no increment in ADC register
+	dma_block_config_s.dest_addr_adj    = DMA_ADDR_ADJ_INCREMENT;      // Dest: increment in memory
+	dma_block_config_s.dest_reload_en   = 1;                           // Reload destination address on block completion
+	if (enable_double_buffering == 1)
+	{
+		dma_block_config_s.source_reload_en = 1; // Reload source address on block completion; Enables Half-transfer interrupt
+	}
+
+	struct dma_config dma_config_s = {0};
+	dma_config_s.dma_slot            = source_triggers[adc_index]; // Trigger source: ADC
+	dma_config_s.channel_direction   = PERIPHERAL_TO_MEMORY;       // From periph to mem
+	dma_config_s.source_data_size    = 2;                          // Source: 2 bytes (uint16_t)
+	dma_config_s.dest_data_size      = 2;                          // Dest: 2 bytes (uint16_t)
+	dma_config_s.source_burst_length = 1;                          // Source: No burst
+	dma_config_s.dest_burst_length   = 1;                          // Dest: No burst
+	dma_config_s.block_count         = 1;                          // 1 block
+	dma_config_s.head_block          = &dma_block_config_s;        // Block config as defined above
+	dma_config_s.dma_callback        = _dma_callback;              // DMA interrupt callback
+
+	dma_config(dma1, adc_number, &dma_config_s);
+
+	dma_start(dma1, adc_number);
+}
diff --git a/zephyr/modules/owntech_data_acquisition/zephyr/dma/dma.h b/zephyr/modules/owntech_data_acquisition/zephyr/adc_to_mem/dma.h
similarity index 50%
rename from zephyr/modules/owntech_data_acquisition/zephyr/dma/dma.h
rename to zephyr/modules/owntech_data_acquisition/zephyr/adc_to_mem/dma.h
index c5deae8..385a86b 100644
--- a/zephyr/modules/owntech_data_acquisition/zephyr/dma/dma.h
+++ b/zephyr/modules/owntech_data_acquisition/zephyr/adc_to_mem/dma.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2021 LAAS-CNRS
+ * Copyright (c) 2021-2023 LAAS-CNRS
  *
  *   This program is free software: you can redistribute it and/or modify
  *   it under the terms of the GNU Lesser General Public License as published by
@@ -18,15 +18,21 @@
  */
 
 /**
+ * @date   2023
+ *
  * @author Clément Foucher <clement.foucher@laas.fr>
  *
- * @brief  This file provides DMA configuration.
+ * @brief  This file provides DMA configuration to automatically
+ *         store ADC acquisitions in a provided buffer.
+ *         DMA 1 is used for all acquisitions, with channel n
+ *         acquiring values from ADC n.
  */
 
 #ifndef DMA_H_
 #define DMA_H_
 
 
+// Stdlib
 #include <stdint.h>
 
 
@@ -36,20 +42,24 @@ extern "C" {
 
 
 /**
- * This function configures and starts the DMA.
+ * This function configures a channel from DMA 1
+ * to transfer measures  from an ADC to buffers,
+ * then starts the channels.
  * It must only be called after all the ADCs configuration
  * has been carried out, as it uses its channels
  * configuration to determine the size of the buffers.
  *
- * @param adc_count Number of configured ADCs.
+ * @param adc_number Number of the ADC to acquire measures from.
+ * @param enable_double_buffering Boolean indicating whether
+ *        double buffering is to be activated.
+ *        - If false, the buffer will be treated as a single buffer with
+ *          interrupt being triggered only when it is full.
+ *        - If true the buffer will be treated as two consecutive sub-buffers,
+ *          with interrupt being triggered as soon as a sub-buffer is full.
+ * @param buffer Pointer to buffer.
+ * @param buffer_size Size of the buffer in bytes.
  */
-void dma_configure_and_start(uint8_t adc_count);
-
-// For debug purpose
-uint16_t* dma_get_dma1_buffer();
-uint16_t* dma_get_dma2_buffer();
-uint16_t* dma_get_dma3_buffer();
-uint16_t* dma_get_dma4_buffer();
+void dma_configure_adc_acquisition(uint8_t adc_number, bool enable_double_buffering, uint16_t* buffer, size_t buffer_size);
 
 
 #ifdef __cplusplus
diff --git a/zephyr/modules/owntech_data_acquisition/zephyr/dma/dma.c b/zephyr/modules/owntech_data_acquisition/zephyr/dma/dma.c
deleted file mode 100644
index 8b24ea4..0000000
--- a/zephyr/modules/owntech_data_acquisition/zephyr/dma/dma.c
+++ /dev/null
@@ -1,191 +0,0 @@
-/*
- * Copyright (c) 2021-2022 LAAS-CNRS
- *
- *   This program is free software: you can redistribute it and/or modify
- *   it under the terms of the GNU Lesser General Public License as published by
- *   the Free Software Foundation, either version 2.1 of the License, or
- *   (at your option) any later version.
- *
- *   This program is distributed in the hope that it will be useful,
- *   but WITHOUT ANY WARRANTY; without even the implied warranty of
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *   GNU Lesser General Public License for more details.
- *
- *   You should have received a copy of the GNU Lesser General Public License
- *   along with this program.  If not, see <https://www.gnu.org/licenses/>.
- *
- * SPDX-License-Identifier: LGLPV2.1
- */
-
-/**
- * @date   2022
- *
- * @author Clément Foucher <clement.foucher@laas.fr>
- *
- * @brief  DMA configuration for OwnTech application.
- * One DMA channel is assigned per ADC. For each ADC, the DMA
- * has a buffer sized 2*(number of enabled channels in ADC),
- * which is subdivised in two half-buffers, so that when
- * the DMA is filling one half-buffer, the other one
- * is available to data dispatch.
- * DMA 1 is used for all acquisitions, with channel i
- * acquiring values from ADC i.
- */
-
-
-// Stdlib
-#include <stdint.h>
-
-// Zephyr
-#include <zephyr.h>
-#include <drivers/dma.h>
-
-// STM32
-#include <stm32_ll_dma.h>
-
-// OwnTech API
-#include "adc.h"
-#include "../data_dispatch/data_dispatch.h"
-
-
-/////
-// DT definitions
-
-static const struct device* dma1 = DEVICE_DT_GET(DT_NODELABEL(dma1));
-
-
-/////
-// LL definitions
-
-static uint32_t source_registers[4] =
-{
-	(uint32_t)(&(ADC1->DR)),
-	(uint32_t)(&(ADC2->DR)),
-	(uint32_t)(&(ADC3->DR)),
-	(uint32_t)(&(ADC4->DR))
-};
-
-static uint32_t source_triggers[4] =
-{
-	LL_DMAMUX_REQ_ADC1,
-	LL_DMAMUX_REQ_ADC2,
-	LL_DMAMUX_REQ_ADC3,
-	LL_DMAMUX_REQ_ADC4
-};
-
-
-/////
-// Arrays of buffers:
-// half_buffer_*[i][] is an array whose size matches
-// the number of enabled channels in ADC(i+1).
-
-static uint16_t** half_buffer_1;
-static uint16_t** half_buffer_2;
-
-
-/////
-// Private API
-
-/**
- * DMA callback
- * This callback is called twice per buffer filling:
- * when buffer is half-filled and when buffer is filled.
- */
-static void _dma_callback(const struct device* dev, void* user_data, uint32_t channel, int status)
-{
-	static uint8_t current_half_buffer[4] = {0};
-	uint8_t adc_number = channel + 1;
-
-	if (current_half_buffer[channel] == 0)
-	{
-		data_dispatch_do_dispatch(adc_number, half_buffer_1[channel]);
-		current_half_buffer[channel] = 1;
-	}
-	else
-	{
-		data_dispatch_do_dispatch(adc_number, half_buffer_2[channel]);
-		current_half_buffer[channel] = 0;
-	}
-}
-
-/**
- * DMA init function for one channel
- */
-static void _dma_channel_init(uint8_t adc_num, uint32_t source_address, uint32_t source_trigger)
-{
-	// Prepare buffers
-	uint8_t enabled_channels = adc_get_enabled_channels_count(adc_num);
-	size_t dma_buffer_size = enabled_channels * sizeof(uint16_t) * 2;
-	uint8_t adc_index = adc_num - 1;
-
-	uint16_t* dma_buffer = k_malloc(dma_buffer_size);
-	half_buffer_1[adc_index] = dma_buffer;
-	half_buffer_2[adc_index] = dma_buffer + enabled_channels;
-
-	// Configure DMA
-	struct dma_block_config dma_block_config_s = {0};
-	struct dma_config       dma_config_s       = {0};
-
-	dma_block_config_s.source_address   = source_address;         // Source: ADC DR register
-	dma_block_config_s.dest_address     = (uint32_t)dma_buffer;   // Dest: buffer in memory
-	dma_block_config_s.block_size       = dma_buffer_size;        // Buffer size in bytes
-	dma_block_config_s.source_addr_adj  = DMA_ADDR_ADJ_NO_CHANGE; // Source: no increment in ADC register
-	dma_block_config_s.dest_addr_adj    = DMA_ADDR_ADJ_INCREMENT; // Dest: increment in memory
-	dma_block_config_s.source_reload_en = 1;                      // Reload initial address after block; Enables Half-transfer interrupt
-	dma_block_config_s.dest_reload_en   = 1;                      // Reload initial address after block
-
-	dma_config_s.dma_slot            = source_trigger;       // Source: triggered from ADC
-	dma_config_s.channel_direction   = PERIPHERAL_TO_MEMORY; // From periph to mem
-	dma_config_s.source_data_size    = 2;                    // 2 bytes
-	dma_config_s.dest_data_size      = 2;                    // 2 bytes
-	dma_config_s.source_burst_length = 1;                    // No burst
-	dma_config_s.dest_burst_length   = 1;                    // No burst
-	dma_config_s.block_count         = 1;                    // 1 block
-	dma_config_s.head_block          = &dma_block_config_s;  // Above block config
-	dma_config_s.dma_callback        = _dma_callback;        // Callback
-
-	dma_config(dma1, adc_num, &dma_config_s);
-}
-
-
-/////
-// Public API
-
-void dma_configure_and_start(uint8_t adc_count)
-{
-	half_buffer_1 = k_malloc(adc_count * sizeof(uint16_t*));
-	half_buffer_2 = k_malloc(adc_count * sizeof(uint16_t*));
-
-	if (device_is_ready(dma1) == true)
-	{
-		for (uint8_t adc_index = 0 ; adc_index < adc_count ; adc_index++)
-		{
-			uint8_t adc_num = adc_index +1;
-			if (adc_get_enabled_channels_count(adc_num) > 0)
-			{
-				_dma_channel_init(adc_num, source_registers[adc_index], source_triggers[adc_index]);
-				dma_start(dma1, adc_num);
-			}
-		}
-	}
-}
-
-uint16_t* dma_get_dma1_buffer()
-{
-	return half_buffer_1[0];
-}
-
-uint16_t* dma_get_dma2_buffer()
-{
-	return half_buffer_1[1];
-}
-
-uint16_t* dma_get_dma3_buffer()
-{
-	return half_buffer_1[2];
-}
-
-uint16_t* dma_get_dma4_buffer()
-{
-	return half_buffer_1[3];
-}
diff --git a/zephyr/modules/owntech_data_acquisition/zephyr/public_api/DataAcquisition.cpp b/zephyr/modules/owntech_data_acquisition/zephyr/public_api/DataAcquisition.cpp
index 68b2c40..77580c4 100644
--- a/zephyr/modules/owntech_data_acquisition/zephyr/public_api/DataAcquisition.cpp
+++ b/zephyr/modules/owntech_data_acquisition/zephyr/public_api/DataAcquisition.cpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2022 LAAS-CNRS
+ * Copyright (c) 2022-2023 LAAS-CNRS
  *
  *   This program is free software: you can redistribute it and/or modify
  *   it under the terms of the GNU Lesser General Public License as published by
@@ -18,7 +18,7 @@
  */
 
 /**
- * @date   2022
+ * @date   2023
  *
  * @author Clément Foucher <clement.foucher@laas.fr>
  * @author Luiz Villa <luiz.villa@laas.fr>
@@ -28,16 +28,15 @@
 // Stdlib
 #include <string.h>
 
-// Current module private functions
-#include "../dma/dma.h"
-#include "../data_dispatch/data_dispatch.h"
-#include "../data_conversion/data_conversion.h"
+// Current class header
+#include "DataAcquisition.h"
 
 // OwnTech Power API
 #include "adc.h"
 
-// Current class header
-#include "DataAcquisition.h"
+// Current module private functions
+#include "../adc_to_mem/data_dispatch.h"
+#include "../data_conversion/data_conversion.h"
 
 
 /////
@@ -115,9 +114,7 @@ void DataAcquisition::setChannnelAssignment(uint8_t adc_number, const char* chan
 
 void DataAcquisition::start()
 {
-	uint8_t number_of_adcs = 4;
-
-	for (uint8_t adc_num = 1 ; adc_num <= number_of_adcs ; adc_num++)
+	for (uint8_t adc_num = 1 ; adc_num <= 4 ; adc_num++)
 	{
 		uint8_t channel_rank = 0;
 
@@ -138,11 +135,8 @@ void DataAcquisition::start()
 
 	}
 
-	// DMAs
-	dma_configure_and_start(number_of_adcs);
-
 	// Initialize data dispatch
-	data_dispatch_init(number_of_adcs);
+	data_dispatch_init();
 
 	// Launch ADC conversion
 	adc_start();
-- 
GitLab