You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by ma...@apache.org on 2016/03/31 02:27:56 UTC

incubator-mynewt-core git commit: a hal_dac (Digital To Analog Conversion) API for HAL.

Repository: incubator-mynewt-core
Updated Branches:
  refs/heads/develop ad0cbc548 -> a3821ef05


a hal_dac (Digital To Analog Conversion) API for HAL.

This looks similar to the hal ADC API except allows you to write a value
(intead of read).  It also allows you to read the current value from the DAC


Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/commit/a3821ef0
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/a3821ef0
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/a3821ef0

Branch: refs/heads/develop
Commit: a3821ef057b9bbae5b20e59b57079c87b9eab65e
Parents: ad0cbc5
Author: Paul Dietrich <pa...@yahoo.com>
Authored: Wed Mar 30 17:01:15 2016 -0700
Committer: Paul Dietrich <pa...@yahoo.com>
Committed: Wed Mar 30 17:11:00 2016 -0700

----------------------------------------------------------------------
 hw/bsp/native/include/bsp/bsp_sysid.h |  23 +++---
 hw/bsp/native/src/hal_bsp.c           |  22 ++++++
 hw/hal/include/hal/hal_dac.h          |  77 ++++++++++++++++++++
 hw/hal/include/hal/hal_dac_int.h      |  63 ++++++++++++++++
 hw/hal/src/hal_dac.c                  |  88 ++++++++++++++++++++++
 hw/mcu/native/include/mcu/hal_dac.h   |  36 +++++++++
 hw/mcu/native/src/hal_dac.c           | 113 +++++++++++++++++++++++++++++
 7 files changed, 408 insertions(+), 14 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/a3821ef0/hw/bsp/native/include/bsp/bsp_sysid.h
----------------------------------------------------------------------
diff --git a/hw/bsp/native/include/bsp/bsp_sysid.h b/hw/bsp/native/include/bsp/bsp_sysid.h
index f1eb61d..b9094c2 100644
--- a/hw/bsp/native/include/bsp/bsp_sysid.h
+++ b/hw/bsp/native/include/bsp/bsp_sysid.h
@@ -16,16 +16,12 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-#ifndef __BSP_SYSID_H
-#define __BSP_SYSID_H
+#ifndef __NATIVE_BSP_SYSID_H
+#define __NATIVE_BSP_SYSID_H
 
 /* This file defines the system device descriptors for this BSP.
  * System device descriptors are any HAL devies */
 
-#ifdef __cplusplus
-extern "C" {
-#endif
-
 /* this is a native build and these are just simulated */
 enum system_device_id {
     NATIVE_A0,
@@ -41,13 +37,12 @@ enum system_device_id {
     NATIVE_BSP_PWM_4,
     NATIVE_BSP_PWM_5,
     NATIVE_BSP_PWM_6,
-    NATIVE_BSP_PWM_7,    
-    /* TODO -- add other hals here */
-    
+    NATIVE_BSP_PWM_7, 
+    NATIVE_BSP_DAC_0,
+    NATIVE_BSP_DAC_1,
+    NATIVE_BSP_DAC_2,
+    NATIVE_BSP_DAC_3,
+    /* TODO -- add other hals here */    
 };
 
-#ifdef __cplusplus
-}
-#endif
-
-#endif  /* __BSP_SYSID_H */
+#endif  /* __NATIVE_BSP_SYSID_H */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/a3821ef0/hw/bsp/native/src/hal_bsp.c
----------------------------------------------------------------------
diff --git a/hw/bsp/native/src/hal_bsp.c b/hw/bsp/native/src/hal_bsp.c
index 2a1db85..dadee14 100644
--- a/hw/bsp/native/src/hal_bsp.c
+++ b/hw/bsp/native/src/hal_bsp.c
@@ -29,6 +29,8 @@
 #include <bsp/bsp.h>
 #include <bsp/bsp_sysid.h>
 #include <mcu/hal_pwm.h>
+#include <mcu/hal_dac.h>
+
 
 const struct hal_flash *
 bsp_flash_dev(uint8_t id)
@@ -94,3 +96,23 @@ bsp_get_hal_pwm_driver(enum system_device_id sysid)
     }
     return NULL;
 }
+
+
+struct hal_dac *
+bsp_get_hal_dac(enum system_device_id sysid) 
+{
+    switch(sysid) 
+    {
+        case NATIVE_BSP_DAC_0:
+            return native_dac_create(NATIVE_MCU_DAC0);
+        case NATIVE_BSP_DAC_1:
+            return native_dac_create(NATIVE_MCU_DAC1);
+        case NATIVE_BSP_DAC_2:
+            return native_dac_create(NATIVE_MCU_DAC2);
+        case NATIVE_BSP_DAC_3:
+            return native_dac_create(NATIVE_MCU_DAC3);
+        default:
+            break;
+    }
+    return NULL;   
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/a3821ef0/hw/hal/include/hal/hal_dac.h
----------------------------------------------------------------------
diff --git a/hw/hal/include/hal/hal_dac.h b/hw/hal/include/hal/hal_dac.h
new file mode 100644
index 0000000..ab765de
--- /dev/null
+++ b/hw/hal/include/hal/hal_dac.h
@@ -0,0 +1,77 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#ifndef HAL_DAC_H
+#define HAL_DAC_H
+
+/* for the pin descriptor enum */
+#include <bsp/bsp_sysid.h>    
+
+/* This is the device for a Digital to Analog Converter (DAC). 
+ * The application using the DAC device
+ * does not need to know the definition of this device and can operate 
+ * with a pointer to this device.  you can get/build device pointers in the 
+ * BSP 
+ *
+ * NOTE: You can also use PWM devices to simulate analog output.
+ * These are defined in hal_pwm.h */
+struct hal_dac; 
+
+/* initialize the DAC on the corresponding BSP device. Returns a pointer
+ * to the DAC object to use for the methods below. Returns NULL on 
+ * error */
+struct hal_dac *
+hal_dac_init(enum system_device_id sysid);
+
+/*
+ * write the DAC corresponding to sysid in your system.  
+ * Return 0 on success negative on failures. If you 
+ * write a value larger than the DAC size, it will 
+ * get truncated to the maximum DAC value but the write
+ * will succeed.
+ */
+int 
+hal_dac_write(struct hal_dac *pdac, int val);
+
+/* gets the current value that is output on the DAC .
+ * Return the current value on success negative on failures.
+ */
+int 
+hal_dac_get_current(struct hal_dac *pdac);
+
+/* returns the number of bit of resolution in this DAC.  
+ * For example if the system has an 8-bit DAC reporting 
+ * values from 0= to 255 (2^8-1), this function would return
+ * the value 8. returns negative or zero on error */
+int 
+hal_dac_get_bits(struct hal_dac *pdac);
+
+/* Returns the positive reference voltage for a maximum DAC reading.
+ * This API assumes the negative reference voltage is zero volt.
+ * Returns negative or zero on error.  
+ */
+int 
+hal_dac_get_ref_mv(struct hal_dac *pdac);
+
+/* Converts a value in millivolts to a DAC value for this DAC */
+int 
+hal_dac_to_val(struct hal_dac *pdac, int mvolts);
+
+
+#endif /* HAL_DAC_H */  
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/a3821ef0/hw/hal/include/hal/hal_dac_int.h
----------------------------------------------------------------------
diff --git a/hw/hal/include/hal/hal_dac_int.h b/hw/hal/include/hal/hal_dac_int.h
new file mode 100644
index 0000000..fa679d7
--- /dev/null
+++ b/hw/hal/include/hal/hal_dac_int.h
@@ -0,0 +1,63 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#ifndef HAL_DAC_INT_H
+#define HAL_DAC_INT_H
+
+#include <inttypes.h>
+#include <bsp/bsp_sysid.h>
+
+
+struct hal_dac;
+
+/* These functions make up the driver API for DAC devices.  All 
+ * DAC devices with Mynewt support implement this interface */
+struct hal_dac_funcs {
+    int (*hdac_write)            (struct hal_dac *pdac, int val);
+    int (*hdac_current)          (struct hal_dac *pdac);
+    int (*hdac_get_bits)         (struct hal_dac *pdac);
+    int (*hdac_get_ref_mv)       (struct hal_dac *pdac);    
+};
+
+/* This is the internal device representation for a hal_dac device.
+ * 
+ * Its main goal is to wrap the const drivers in a non-const structure.
+ * Thus these can be made on the stack and wrapped with other non-const
+ * structures. 
+ * 
+ * For example, if you are creating a dac driver you can use
+ * 
+ * struct my_dac_driver {
+ *     struct hal_dac   parent;
+ *     int              my_stuff 1;
+ *     char            *mybuff;
+ * };
+ * 
+ * See the native MCU and BSP for examples 
+ */
+struct hal_dac {
+    const struct hal_dac_funcs  *driver_api;
+};
+
+/* The  BSP must implement this factory to get devices for the 
+ * application.  */
+extern struct hal_dac *
+bsp_get_hal_dac(enum system_device_id sysid);
+
+#endif /* HAL_DAC_INT_H */ 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/a3821ef0/hw/hal/src/hal_dac.c
----------------------------------------------------------------------
diff --git a/hw/hal/src/hal_dac.c b/hw/hal/src/hal_dac.c
new file mode 100644
index 0000000..3e334a3
--- /dev/null
+++ b/hw/hal/src/hal_dac.c
@@ -0,0 +1,88 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+#include <inttypes.h>
+#include <hal/hal_dac.h>
+#include <hal/hal_dac_int.h>
+
+struct hal_dac *
+hal_dac_init(enum system_device_id pin) 
+{
+    return bsp_get_hal_dac(pin);
+}
+
+int 
+hal_dac_write(struct hal_dac *pdac, int val) 
+{
+    if (pdac && pdac->driver_api && pdac->driver_api->hdac_write) {
+        return pdac->driver_api->hdac_write(pdac, val);
+    }
+    return -1;
+}
+
+int 
+hal_dac_get_bits(struct hal_dac *pdac) 
+{
+    if (pdac && pdac->driver_api && pdac->driver_api->hdac_get_bits) {
+        return pdac->driver_api->hdac_get_bits(pdac);
+    }
+    return -1;
+}
+
+int 
+hal_dac_get_ref_mv(struct hal_dac *pdac) 
+{
+    if (pdac && pdac->driver_api && pdac->driver_api->hdac_get_ref_mv) {
+        return pdac->driver_api->hdac_get_ref_mv(pdac);
+    }
+    return -1;
+}
+
+/* gets the current DAC value */
+int 
+hal_dac_get_current(struct hal_dac *pdac) 
+{
+    if (pdac && pdac->driver_api && pdac->driver_api->hdac_get_ref_mv) {
+        return pdac->driver_api->hdac_current(pdac);
+    }
+    return -1;
+}
+
+/* Converts a millivolt value to the right DAC settings for this DAC.
+ */
+int 
+hal_dac_to_val(struct hal_dac *pdac, int mvolts) 
+{
+    int rc = -1;
+    int bits = hal_dac_get_bits(pdac);
+    int ref = hal_dac_get_ref_mv(pdac);
+            
+    if ((bits > 0) && (ref >= 0)) 
+    {
+        /* assume that 2^N -1 is full scale. This line multiples by 2^N-1 
+         * and then rounds up */
+        rc = (mvolts << bits) - mvolts + (ref >> 1);
+        rc /= ref;
+        
+        /* don't let this exceed the DAC max */
+        if(rc >= (1 << bits)) {
+            rc = (1 << bits) - 1;
+        }
+    }
+    return rc;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/a3821ef0/hw/mcu/native/include/mcu/hal_dac.h
----------------------------------------------------------------------
diff --git a/hw/mcu/native/include/mcu/hal_dac.h b/hw/mcu/native/include/mcu/hal_dac.h
new file mode 100644
index 0000000..0fb1a47
--- /dev/null
+++ b/hw/mcu/native/include/mcu/hal_dac.h
@@ -0,0 +1,36 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#ifndef _NATIVE_HAL_DAC_H
+#define _NATIVE_HAL_DAC_H
+
+enum native_dac_channel 
+{
+    NATIVE_MCU_DAC0 = 0,
+    NATIVE_MCU_DAC1,
+    NATIVE_MCU_DAC2,
+    NATIVE_MCU_DAC3,
+};
+ 
+/* to create a native dac driver */
+struct hal_dac *
+native_dac_create (enum native_dac_channel);
+
+#endif /* _NATIVE_HAL_PWM_H */
+

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/a3821ef0/hw/mcu/native/src/hal_dac.c
----------------------------------------------------------------------
diff --git a/hw/mcu/native/src/hal_dac.c b/hw/mcu/native/src/hal_dac.c
new file mode 100644
index 0000000..9f60879
--- /dev/null
+++ b/hw/mcu/native/src/hal_dac.c
@@ -0,0 +1,113 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <assert.h>
+#include <hal/hal_dac.h>
+#include <hal/hal_dac_int.h>
+#include <mcu/hal_dac.h>
+
+/* forwards for the const structure below */
+static int native_dac_write(struct hal_dac *pdac, int val);
+static int native_dac_bits(struct hal_dac *pdac);
+static int native_dac_current(struct hal_dac *pdac);
+static int native_dac_refmv(struct hal_dac *pdac);
+
+/* This DAC had four channels that print out the value  
+ * random numbers */
+const struct hal_dac_funcs native_dac_funcs = {
+    .hdac_current = &native_dac_current,
+    .hdac_get_ref_mv = &native_dac_refmv,
+    .hdac_get_bits = &native_dac_bits,
+    .hdac_write = &native_dac_write,
+};
+
+struct native_hal_dac {
+    struct hal_dac parent;
+    uint8_t        channel;
+    uint8_t        last_written_val;
+};
+
+struct hal_dac * 
+native_dac_create(enum native_dac_channel chan) 
+{
+    struct native_hal_dac *pdac = NULL;
+    
+    switch(chan) {
+        case NATIVE_MCU_DAC0:
+        case NATIVE_MCU_DAC1:
+        case NATIVE_MCU_DAC2:
+        case NATIVE_MCU_DAC3:
+            pdac = malloc(sizeof(struct native_hal_dac));
+            if (pdac) {
+                pdac->parent.driver_api = &native_dac_funcs;
+                pdac->channel = chan;
+                pdac->last_written_val = 0;
+            }
+    }    
+    return &pdac->parent;
+}
+
+int 
+native_dac_bits(struct hal_dac *pdac) 
+{
+    struct native_hal_dac *pn = (struct native_hal_dac *) pdac;
+    if (pn && pn->parent.driver_api == &native_dac_funcs) {
+         return 8;        
+    }
+    return -1;
+}
+
+int 
+native_dac_refmv(struct hal_dac *pdac) 
+{
+    struct native_hal_dac *pn = (struct native_hal_dac *) pdac;
+    if (pn && pn->parent.driver_api == &native_dac_funcs) {
+         return 5000;        
+    }
+    return -1;
+}
+
+
+int 
+native_dac_current(struct hal_dac *pdac) 
+{
+    struct native_hal_dac *pn = (struct native_hal_dac *) pdac;
+    if (pn && pn->parent.driver_api == &native_dac_funcs) {
+         return (int) pn->last_written_val;        
+    }
+    return -1;
+}
+
+int 
+native_dac_write(struct hal_dac *pdac, int val) 
+{
+    struct native_hal_dac *pn = (struct native_hal_dac *) pdac;
+    
+    if (val > 255) {
+        val = 255;
+    }
+        
+    if (pn && pn->parent.driver_api == &native_dac_funcs) {
+        pn->last_written_val = val;
+        return 0;
+    }
+    return -1;    
+}