From cf67ed35455ab247841e38f50dde536793136c45 Mon Sep 17 00:00:00 2001
From: Luiz Fernando Lavado Villa <lflavado@laas.fr>
Date: Thu, 3 Nov 2022 08:50:37 +0000
Subject: [PATCH] One of the four communication methods uses a DAC for analog
 signal sharing....

One of the four communication methods uses a DAC for analog signal sharing. This commit implements a first non-tested version of this feature.

Four files were modified to implement this feature:
- The low layer dac_configuration.h and .cpp: These files allow access to the dac module and can set any dac to constant value mode. The constant value can also be set.
- The HardwareConfiguration API: They give access to the low-leve configuration file to the user.
---
 .../public_api/HardwareConfiguration.cpp      |  9 ++++
 .../zephyr/public_api/HardwareConfiguration.h |  2 +
 .../zephyr/src/dac_configuration.cpp          | 50 +++++++++++++++++++
 .../zephyr/src/dac_configuration.h            |  6 +++
 4 files changed, 67 insertions(+)

diff --git a/zephyr/modules/owntech_hardware_configuration/zephyr/public_api/HardwareConfiguration.cpp b/zephyr/modules/owntech_hardware_configuration/zephyr/public_api/HardwareConfiguration.cpp
index 19ef299..e3984c0 100644
--- a/zephyr/modules/owntech_hardware_configuration/zephyr/public_api/HardwareConfiguration.cpp
+++ b/zephyr/modules/owntech_hardware_configuration/zephyr/public_api/HardwareConfiguration.cpp
@@ -94,6 +94,15 @@ void HardwareConfiguration::initDac1Dac3CurrentMode()
 	dac_config_dac1_dac3_current_mode_init();
 }
 
+void HardwareConfiguration::initDacConstValue(uint8_t dac_number)
+{
+	dac_config_const_value_init(dac_number);
+}
+
+void HardwareConfiguration::setDacConstValue(uint8_t dac_number, uint8_t channel, uint32_t const_value)
+{
+	dac_set_const_value(dac_number, channel, const_value);
+}
 
 /////
 // NGND
diff --git a/zephyr/modules/owntech_hardware_configuration/zephyr/public_api/HardwareConfiguration.h b/zephyr/modules/owntech_hardware_configuration/zephyr/public_api/HardwareConfiguration.h
index 6272e61..283edf6 100644
--- a/zephyr/modules/owntech_hardware_configuration/zephyr/public_api/HardwareConfiguration.h
+++ b/zephyr/modules/owntech_hardware_configuration/zephyr/public_api/HardwareConfiguration.h
@@ -82,6 +82,8 @@ public:
 
 	// DAC
 	static void initDac1Dac3CurrentMode();
+	static void initDacConstValue(uint8_t dac_number);
+	static void setDacConstValue(uint8_t dac_number, uint8_t channel, uint32_t const_value);
 
 	// NGND
 	static void setNgndOn();
diff --git a/zephyr/modules/owntech_hardware_configuration/zephyr/src/dac_configuration.cpp b/zephyr/modules/owntech_hardware_configuration/zephyr/src/dac_configuration.cpp
index b213ff5..0ae089b 100644
--- a/zephyr/modules/owntech_hardware_configuration/zephyr/src/dac_configuration.cpp
+++ b/zephyr/modules/owntech_hardware_configuration/zephyr/src/dac_configuration.cpp
@@ -21,6 +21,7 @@
  * @date   2022
  *
  * @author Clément Foucher <clement.foucher@laas.fr>
+ * @author Luiz Villa <luiz.villa@laas.fr>
  */
 
 
@@ -32,6 +33,7 @@
 
 
 static const struct device* dac1 = DEVICE_DT_GET(DAC1_DEVICE);
+static const struct device* dac2 = DEVICE_DT_GET(DAC2_DEVICE);
 static const struct device* dac3 = DEVICE_DT_GET(DAC3_DEVICE);
 
 
@@ -63,3 +65,51 @@ void dac_config_dac1_dac3_current_mode_init()
 		dac_start(dac3, 1);
 	}
 }
+
+void dac_config_const_value_init(uint8_t dac_number)
+{
+	const struct device* dac_dev;
+
+	if (dac_number == 1)
+	{
+		dac_dev = dac1;
+	}
+	else if (dac_number == 3)
+	{
+		dac_dev = dac3;
+	}
+	else
+	{
+		dac_dev = dac2; //sets the dac 2 as default
+	}
+
+	if (device_is_ready(dac_dev) == true)
+	{
+		dac_set_const_value(dac_dev,1,0);
+		dac_pin_configure(dac_dev, 1, dac_pin_external);
+		dac_start(dac_dev, 1);
+	}
+}
+
+void dac_set_const_value(uint8_t dac_number, uint8_t channel, uint32_t const_value)
+{
+	const struct device* dac_dev;
+
+	if (dac_number == 1)
+	{
+		dac_dev = dac1;
+	}
+	else if (dac_number == 3)
+	{
+		dac_dev = dac3;
+	}
+	else
+	{
+		dac_dev = dac2; //sets the dac 2 as default
+	}
+
+	if (device_is_ready(dac_dev) == true)
+	{
+		dac_set_const_value(dac_dev,channel,const_value);
+	}
+}
diff --git a/zephyr/modules/owntech_hardware_configuration/zephyr/src/dac_configuration.h b/zephyr/modules/owntech_hardware_configuration/zephyr/src/dac_configuration.h
index fcd827e..fff576e 100644
--- a/zephyr/modules/owntech_hardware_configuration/zephyr/src/dac_configuration.h
+++ b/zephyr/modules/owntech_hardware_configuration/zephyr/src/dac_configuration.h
@@ -20,14 +20,20 @@
 /**
  * @date   2022
  * @author Clément Foucher <clement.foucher@laas.fr>
+ * @author Luiz Villa <luiz.villa@laas.fr>
  */
 
 
 #ifndef DAC_CONFIGURATION_H_
 #define DAC_CONFIGURATION_H_
 
+// Stdlib
+#include <stdint.h>
+
 
 void dac_config_dac1_dac3_current_mode_init();
+void dac_config_const_value_init(uint8_t dac_number);
+void dac_set_const_value(uint8_t dac_number, uint8_t channel, uint32_t const_value);
 
 
 #endif // DAC_CONFIGURATION_H_
-- 
GitLab