]> jspc29.x-matter.uni-frankfurt.de Git - avr.git/commitdiff
split the DS28E17 from the MLX90393 library and go over to a more general implementation
authorAdrian Weber <adrian.a.weber@exp2.physik.uni-giessen.de>
Thu, 11 Feb 2021 16:34:27 +0000 (17:34 +0100)
committerAdrian Weber <adrian.a.weber@exp2.physik.uni-giessen.de>
Thu, 11 Feb 2021 16:34:27 +0000 (17:34 +0100)
atmega32u4/CbmRich_sensoring/DS28E17/DS28E17.c
atmega32u4/CbmRich_sensoring/DS28E17/DS28E17.h
atmega32u4/CbmRich_sensoring/MLX90393/MLX90393.c [new file with mode: 0644]
atmega32u4/CbmRich_sensoring/MLX90393/MLX90393.h [new file with mode: 0644]
atmega32u4/CbmRich_sensoring/Makefile
atmega32u4/CbmRich_sensoring/main.c

index dd4fb731acd14ff6aa48e7001e92f59cfe14f2d5..f07f280890e972cc8f23453446caa67358d478cd 100644 (file)
@@ -1,10 +1,11 @@
 #include "DS28E17.h"
 
-int ds28e17_Write_with_Stop(owu_struct_t *wire, uint8_t *OW_addr, uint8_t I2C_Addr, uint8_t *status)
+int ds28e17_Write_with_Stop(owu_struct_t *wire, uint8_t *OW_addr, uint8_t I2C_Addr, uint8_t wleng, uint8_t *wdata, uint8_t *status)
 {
        int wire_status;
     status[0] = 0xd;
-    uint8_t CRC_data_buf[] = {0,0,0,0};
+    const uint8_t CRC16bufSize = 3 + wleng;
+    uint8_t CRC_data_buf[CRC16bufSize];
        wire_status = owu_reset(wire);
        if (wire_status != OW_OK) return wire_status;
 
@@ -24,85 +25,19 @@ int ds28e17_Write_with_Stop(owu_struct_t *wire, uint8_t *OW_addr, uint8_t I2C_Ad
     CRC_data_buf[1] = i2c_add_shftd; // check if address has to be shifted or not
 
     //Length of Data
-    wire_status = owu_write_byte(wire, 0x01);
+    wire_status = owu_write_byte(wire, wleng);
        if (wire_status != OW_OK) return wire_status;
-    CRC_data_buf[2] = 0x01;
+    CRC_data_buf[2] = wleng;
 
     //Data
-    wire_status = owu_write_byte(wire, 0x80);
-       if (wire_status != OW_OK) return wire_status;
-    CRC_data_buf[3] = 0x80;
-
-    //CRC16
-    uint16_t CRC16 = ds28e17_crc16(CRC_data_buf,4);
-    wire_status = owu_write_byte(wire, (CRC16&0xFF));
-       if (wire_status != OW_OK) return wire_status;
-
-    //CRC16
-    uint16_t CRC16_2 = ((CRC16>>8)&0xFF);
-    wire_status = owu_write_byte(wire, CRC16_2);
-       if (wire_status != OW_OK) return wire_status;
-
-    // read status: read bit until 0;
-    uint8_t bit;
-    do {
-        wire_status = read_bit(wire->driver, &bit);
+    for (uint8_t i_data = 0; i_data < wleng; i_data++){
+        wire_status = owu_write_byte(wire, wdata[i_data]); // 0x80
         if (wire_status != OW_OK) return wire_status;
-    } while(bit != 0);
-
-    // read status
-    uint8_t r_status;
-    wire_status = owu_read_byte(wire, &r_status);
-    if (wire_status != OW_OK) return wire_status;
-    status[0] = r_status;
-    // read status write
-    wire_status = owu_read_byte(wire, &r_status);
-    if (wire_status != OW_OK) return wire_status;    
-    status[1] = CRC16;//r_status;
-
-       return wire_status;
-}
-
-int ds28e17_Write_Read_with_Stop(owu_struct_t *wire, uint8_t *OW_addr, uint8_t I2C_Addr, uint8_t *status, uint8_t *answr)
-{
-       int wire_status;
-    status[0] = 0xd;
-    uint8_t CRC_data_buf[] = {0,0,0,0,0};
-       wire_status = owu_reset(wire);
-       if (wire_status != OW_OK) return wire_status;
-
-       wire_status = owu_select_device(wire, OW_addr);
-       if (wire_status != OW_OK) return wire_status;
-
-       wire_status = owu_write_byte(wire, CMD_WRwS);
-       if (wire_status != OW_OK) return wire_status;
-    CRC_data_buf[0] = CMD_WRwS;
-
-    //Send I2C Address
-    //uint8_t addr_i2c = (I2C_Addr << 1) | 0x1;
-    // not sure if Address has to be shifted or not
-    uint8_t i2c_add_shftd = ((I2C_Addr << 1) | 0x0);
-    wire_status = owu_write_byte(wire, i2c_add_shftd); // 0x0: write
-       if (wire_status != OW_OK) return wire_status;
-    CRC_data_buf[1] = i2c_add_shftd; // check if address has to be shifted or not
-
-    //Length of Data
-    wire_status = owu_write_byte(wire, 0x01);
-       if (wire_status != OW_OK) return wire_status;
-    CRC_data_buf[2] = 0x01;
-
-    //Data
-    wire_status = owu_write_byte(wire, 0x4F);
-       if (wire_status != OW_OK) return wire_status;
-    CRC_data_buf[3] = 0x4F;
-
-    //#bytes to read
-    wire_status = owu_write_byte(wire, 9);
-       if (wire_status != OW_OK) return wire_status;
-    CRC_data_buf[4] = 9;
+        CRC_data_buf[3+i_data] = wdata[i_data];
+    }
 
     //CRC16
-    uint16_t CRC16 = ds28e17_crc16(CRC_data_buf,5);
+    uint16_t CRC16 = ds28e17_crc16(CRC_data_buf,CRC16bufSize);
     wire_status = owu_write_byte(wire, (CRC16&0xFF));
        if (wire_status != OW_OK) return wire_status;
 
@@ -128,51 +63,15 @@ int ds28e17_Write_Read_with_Stop(owu_struct_t *wire, uint8_t *OW_addr, uint8_t I
     if (wire_status != OW_OK) return wire_status;    
     status[1] = CRC16;//r_status;
 
-    uint8_t value;
-    wire_status = owu_read_byte(wire, &value);
-    if (wire_status != OW_OK) return wire_status;    
-    answr[0] = value;
-
-    wire_status = owu_read_byte(wire, &value);
-    if (wire_status != OW_OK) return wire_status;    
-    answr[1] = value;
-
-    wire_status = owu_read_byte(wire, &value);
-    if (wire_status != OW_OK) return wire_status;    
-    answr[2] = value;
-
-    wire_status = owu_read_byte(wire, &value);
-    if (wire_status != OW_OK) return wire_status;    
-    answr[3] = value;
-
-    wire_status = owu_read_byte(wire, &value);
-    if (wire_status != OW_OK) return wire_status;    
-    answr[4] = value;
-
-    wire_status = owu_read_byte(wire, &value);
-    if (wire_status != OW_OK) return wire_status;    
-    answr[5] = value;
-
-    wire_status = owu_read_byte(wire, &value);
-    if (wire_status != OW_OK) return wire_status;    
-    answr[6] = value;        
-
-    wire_status = owu_read_byte(wire, &value);
-    if (wire_status != OW_OK) return wire_status;    
-    answr[7] = value;
-
-    wire_status = owu_read_byte(wire, &value);
-    if (wire_status != OW_OK) return wire_status;    
-    answr[8] = value;
-
        return wire_status;
 }
 
-int ds28e17_SM(owu_struct_t *wire, uint8_t *OW_addr, uint8_t I2C_Addr, uint8_t *status, uint8_t *answr)
+int ds28e17_Write_Read_with_Stop(owu_struct_t *wire, uint8_t *OW_addr, uint8_t I2C_Addr, uint8_t wleng, uint8_t *wdata, uint8_t rleng, uint8_t *rdata, uint8_t *status)
 {
        int wire_status;
     status[0] = 0xd;
-    uint8_t CRC_data_buf[] = {0,0,0,0,0};
+    const uint8_t crc_buf_size = 4+wleng;
+    uint8_t CRC_data_buf[crc_buf_size];
        wire_status = owu_reset(wire);
        if (wire_status != OW_OK) return wire_status;
 
@@ -192,22 +91,24 @@ int ds28e17_SM(owu_struct_t *wire, uint8_t *OW_addr, uint8_t I2C_Addr, uint8_t *
     CRC_data_buf[1] = i2c_add_shftd; // check if address has to be shifted or not
 
     //Length of Data
-    wire_status = owu_write_byte(wire, 0x01);
+    wire_status = owu_write_byte(wire, wleng);
        if (wire_status != OW_OK) return wire_status;
-    CRC_data_buf[2] = 0x01;
+    CRC_data_buf[2] = wleng;
 
     //Data
-    wire_status = owu_write_byte(wire, 0x3F);
-       if (wire_status != OW_OK) return wire_status;
-    CRC_data_buf[3] = 0x3F;
+    for (uint8_t i_wdata = 0; i_wdata < wleng; i_wdata++) {
+      wire_status = owu_write_byte(wire, wdata[i_wdata]);
+         if (wire_status != OW_OK) return wire_status;
+      CRC_data_buf[3+i_wdata] = wdata[i_wdata];
+    }
 
     //#bytes to read
-    wire_status = owu_write_byte(wire, 1);
+    wire_status = owu_write_byte(wire, rleng);
        if (wire_status != OW_OK) return wire_status;
-    CRC_data_buf[4] = 1;
+    CRC_data_buf[3+wleng] = rleng;
 
     //CRC16
-    uint16_t CRC16 = ds28e17_crc16(CRC_data_buf,5);
+    uint16_t CRC16 = ds28e17_crc16(CRC_data_buf,crc_buf_size);
     wire_status = owu_write_byte(wire, (CRC16&0xFF));
        if (wire_status != OW_OK) return wire_status;
 
@@ -233,10 +134,11 @@ int ds28e17_SM(owu_struct_t *wire, uint8_t *OW_addr, uint8_t I2C_Addr, uint8_t *
     if (wire_status != OW_OK) return wire_status;    
     status[1] = CRC16;//r_status;
 
-    uint8_t value;
-    wire_status = owu_read_byte(wire, &value);
-    if (wire_status != OW_OK) return wire_status;    
-    answr[0] = value;
+    // read data
+    for (uint8_t i_rdata = 0; i_rdata < rleng; i_rdata++){
+      wire_status = owu_read_byte(wire, &rdata[i_rdata]);
+      if (wire_status != OW_OK) return wire_status;
+    }
 
        return wire_status;
 }
index 105823c438693d6c03bcc0435eb43ba987441333..bd5e1d4f02cb5e5b5a66f3b84960aa9731ac7ea8 100644 (file)
 extern "C" {
 #endif
 
-int ds28e17_Write_Read_with_Stop(owu_struct_t *wire, uint8_t *OW_addr, uint8_t I2C_Addr, uint8_t *status, uint8_t *answr);
+int ds28e17_Write_Read_with_Stop(owu_struct_t *wire, uint8_t *OW_addr, uint8_t I2C_Addr, uint8_t wleng, uint8_t *wdata,uint8_t rleng, uint8_t *rdata, uint8_t *status);
 
-int ds28e17_Write_with_Stop(owu_struct_t *wire, uint8_t *addr, uint8_t I2C_Addr, uint8_t *status);
-
-int ds28e17_SM(owu_struct_t *wire, uint8_t *OW_addr, uint8_t I2C_Addr, uint8_t *status, uint8_t *answr);
+int ds28e17_Write_with_Stop(owu_struct_t *wire, uint8_t *addr, uint8_t I2C_Addr, uint8_t wleng, uint8_t *wdata, uint8_t *status);
 
 uint16_t ds28e17_crc16(uint8_t*, uint32_t);
 
diff --git a/atmega32u4/CbmRich_sensoring/MLX90393/MLX90393.c b/atmega32u4/CbmRich_sensoring/MLX90393/MLX90393.c
new file mode 100644 (file)
index 0000000..737be1b
--- /dev/null
@@ -0,0 +1,11 @@
+#include "MLX90393.h"
+
+int MLX90393_convert_TXYZ_SM(owu_struct_t *wire, uint8_t *OW_addr, uint8_t I2C_Addr, uint8_t rleng, uint8_t *rdata, uint8_t *status){
+    uint8_t wdata_SM[1] = {0x3F};
+    return ds28e17_Write_Read_with_Stop(wire,OW_addr,I2C_Addr,0x1,wdata_SM,rleng,rdata,status);
+}
+
+int MLX90393_read_TXYZ(owu_struct_t *wire, uint8_t *OW_addr, uint8_t I2C_Addr, uint8_t rleng, uint8_t *rdata, uint8_t *status){
+    uint8_t wdata_SM[1] = {0x4F};
+    return ds28e17_Write_Read_with_Stop(wire,OW_addr,I2C_Addr,0x1,wdata_SM,rleng,rdata,status);
+}
\ No newline at end of file
diff --git a/atmega32u4/CbmRich_sensoring/MLX90393/MLX90393.h b/atmega32u4/CbmRich_sensoring/MLX90393/MLX90393.h
new file mode 100644 (file)
index 0000000..6a43b0d
--- /dev/null
@@ -0,0 +1,20 @@
+#ifndef INCLUDE_MLX90393_H_
+#define INCLUDE_MLX90393_H_
+
+
+#include <stdint.h>
+#include "DS28E17.h"
+
+#if defined(__cplusplus)
+extern "C" {
+#endif
+
+int MLX90393_convert_TXYZ_SM(owu_struct_t *wire, uint8_t *OW_addr, uint8_t I2C_Addr, uint8_t rleng, uint8_t *rdata, uint8_t *status);
+
+int MLX90393_read_TXYZ(owu_struct_t *wire, uint8_t *OW_addr, uint8_t I2C_Addr, uint8_t rleng, uint8_t *rdata, uint8_t *status);
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif /* INCLUDE_MLX90393_H_ */
\ No newline at end of file
index 59ebbc2c181e8b58d11c2e906cff2ebb0685b90e..3c8a62d3badaf6a64c0e809c9716466813e0f4d7 100644 (file)
@@ -8,7 +8,7 @@
 MCU = atmega32u4
 FORMAT = ihex
 TARGET = main
-SRC = $(TARGET).c usb_serial.c lib/1wire/drivers/ow_driver_avr_gpio.c lib/1wire/dallas/dallas.c lib/1wire/onewire/onewire.c DS28E17/DS28E17.c
+SRC = $(TARGET).c usb_serial.c lib/1wire/drivers/ow_driver_avr_gpio.c lib/1wire/dallas/dallas.c lib/1wire/onewire/onewire.c DS28E17/DS28E17.c MLX90393/MLX90393.c
 ASRC = 
 OPT = 2
 PORT=/dev/ttyACM0
@@ -33,7 +33,7 @@ CDEFS = -DF_CPU=8000000
 
 # Place -I options here
 #CINCS =
-CINCS = -I. -I./lib/1wire/dallas -I./lib/1wire/onewire -I./lib/1wire/drivers -I./DS28E17
+CINCS = -I. -I./lib/1wire/dallas -I./lib/1wire/onewire -I./lib/1wire/drivers -I./DS28E17 -I./MLX90393
 
 
 CDEBUG = -g$(DEBUG)
index 7660fa91ed454c5e2bb30dfd0c19864534dfeffd..aa1f98737757d04a69450231efbb0525ae72758e 100644 (file)
@@ -12,6 +12,7 @@
 #include "onewire.h"
 #include "dallas.h"
 #include "DS28E17.h"
+#include "MLX90393.h"
 
 #include <stdio.h>
 
@@ -249,7 +250,8 @@ int search_devices(owu_struct_t ow_Q[]){
 
         //uint8_t lbuf[20];
         //uint8_t stat[2] = {0xe,0xa};
-        //int nbytes2 = sprintf((char*) lbuf, "Reset MLX: %d\n", ds28e17_Write_with_Stop(&ow_Q[ow_line], dev_addr[ow_line][dev_count[ow_line]-1],0x0C,stat));
+        //uint wdata[1] = 0x80;
+        //int nbytes2 = sprintf((char*) lbuf, "Reset MLX: %d\n", ds28e17_Write_with_Stop(&ow_Q[ow_line], dev_addr[ow_line][dev_count[ow_line]-1],0x0C,1,wdata,stat));
         //usb_serial_write(lbuf,nbytes2);
         //nbytes2 = sprintf((char*) lbuf, "DS28E17, status: %02x  %02x \n", stat[0], stat[1]);
         //usb_serial_write(lbuf,nbytes2);
@@ -302,10 +304,10 @@ int init_OW_measurements(owu_struct_t ow_Q[]){
 
       } else if (dev_addr[ow_line][dev_nbr][0] == 0x19) { // Device is DS28E17
         
-        uint8_t lbuf[20];
         uint8_t stat[2] = {0xe,0xa};
         uint8_t answer = 0;
-        ds28e17_SM(&ow_Q[ow_line], dev_addr[ow_line][dev_nbr],0x0C,stat, &answer);
+        MLX90393_convert_TXYZ_SM(&ow_Q[ow_line], dev_addr[ow_line][dev_nbr],0x0C,0x1,&answer,stat);
+        //uint8_t lbuf[20];
         //int nbytes = sprintf((char*) lbuf, "INIT SM on MLX: %d\n", ds28e17_SM(&ow_Q[ow_line], dev_addr[ow_line][dev_nbr],0x0C,stat, &answer));
         //usb_serial_write(lbuf,nbytes);
         //nbytes = sprintf((char*) lbuf, "DS28E17, status: %02x  %02x  %02x\n", stat[0], stat[1], answer);
@@ -317,13 +319,13 @@ int init_OW_measurements(owu_struct_t ow_Q[]){
 
       #ifdef DEBUG_USB_OUT
         int nbytes = sprintf((char*) tx_test_buf, "\t Start Measurement:  %x%x%x%x%x%x%x%x\r\n", dev_addr[ow_line][dev_nbr][0],
-                                                                                             dev_addr[ow_line][dev_nbr][1],
-                                                                                             dev_addr[ow_line][dev_nbr][2],
-                                                                                             dev_addr[ow_line][dev_nbr][3],
-                                                                                             dev_addr[ow_line][dev_nbr][4],
-                                                                                             dev_addr[ow_line][dev_nbr][5],
-                                                                                             dev_addr[ow_line][dev_nbr][6],
-                                                                                             dev_addr[ow_line][dev_nbr][7]); 
+                                                                                                 dev_addr[ow_line][dev_nbr][1],
+                                                                                                 dev_addr[ow_line][dev_nbr][2],
+                                                                                                 dev_addr[ow_line][dev_nbr][3],
+                                                                                                 dev_addr[ow_line][dev_nbr][4],
+                                                                                                 dev_addr[ow_line][dev_nbr][5],
+                                                                                                 dev_addr[ow_line][dev_nbr][6],
+                                                                                                 dev_addr[ow_line][dev_nbr][7]); 
         usb_serial_write(tx_test_buf,nbytes);
       #endif
     }
@@ -369,9 +371,21 @@ int OW_measurements(owu_struct_t ow_Q[]){
     } else if (dev_addr[cur_meas_line][cur_meas_devNbr][0] == 0x19) { // Device is DS28E17
 
       uint8_t lbuf[50];
-      uint8_t stat[2] = {0xe,0xa};
+      uint8_t stat[2]   = {0xe,0xa};
       uint8_t value[10] = {0,0,0,0,0,0,0,0,0,0};
-      int nbytes = sprintf((char*) lbuf, "DS28E17: %d\n", ds28e17_Write_Read_with_Stop(&ow_Q[cur_meas_line], dev_addr[cur_meas_line][cur_meas_devNbr],0x0C,stat, value));
+      //uint8_t wdata[1]  = {0x4F};
+      int nbytes = sprintf((char*) lbuf, "DS28E17: %d\n", MLX90393_read_TXYZ(&ow_Q[cur_meas_line],
+                                                                             dev_addr[cur_meas_line][cur_meas_devNbr],
+                                                                             0x0C,
+                                                                             0x9,
+                                                                             value,
+                                                                             stat));
+
+      send_Values(cur_meas_line,cur_meas_devNbr,'T',(uint16_t) ((value[1] << 8) | value[2]));
+      send_Values(cur_meas_line,cur_meas_devNbr,'X',(uint16_t) ((value[3] << 8) | value[4]));
+      send_Values(cur_meas_line,cur_meas_devNbr,'Y',(uint16_t) ((value[5] << 8) | value[6]));
+      send_Values(cur_meas_line,cur_meas_devNbr,'Z',(uint16_t) ((value[7] << 8) | value[8]));
+
       usb_serial_write(lbuf,nbytes);
       nbytes = sprintf((char*) lbuf, "DS28E17, status: %02x  %02x %02x  %02x%02x  %02x%02x  %02x%02x  %02x%02x\n", stat[0], stat[1], value[0],
                                                                                                                                      value[1],
@@ -384,13 +398,15 @@ int OW_measurements(owu_struct_t ow_Q[]){
                                                                                                                                      value[8]);
       usb_serial_write(lbuf,nbytes);
 
-      send_Values(cur_meas_line,cur_meas_devNbr,'T',0x0000);
-      send_Values(cur_meas_line,cur_meas_devNbr,'X',0x0000);
-      send_Values(cur_meas_line,cur_meas_devNbr,'Y',0x0000);
-      send_Values(cur_meas_line,cur_meas_devNbr,'Z',0x0000);
+
 
       // Init new Single measurement (SM)
-      ds28e17_SM(&ow_Q[cur_meas_line], dev_addr[cur_meas_line][cur_meas_devNbr],0x0C,stat, value);
+      MLX90393_convert_TXYZ_SM(&ow_Q[cur_meas_line],
+                               dev_addr[cur_meas_line][cur_meas_devNbr],
+                               0x0C,
+                               0x1,
+                               value,
+                               stat);                                 
 
     } else {
       // error: Action to be defined
@@ -453,7 +469,7 @@ void send_Values(uint8_t line , uint8_t devNbr, uint8_t valType, uint16_t value)
     delay_us(5); // TO DO: change to ASM command
   }
 
-  uint8_t nbytes = sprintf((char*) txbuf, "V%02x%02x%c%x\n",dev_addr[line][devNbr][0],
+  uint8_t nbytes = sprintf((char*) txbuf, "V%02x%02x%c%04x\n",dev_addr[line][devNbr][0],
                                                      dev_int_addr[line][devNbr],
                                                      (char) valType,
                                                      value);