Interface Driver for Maxim DS2482 1Wire-to-I2C bridge IC. Includes access functions for DS1820 temperature sensors. Can easily be ported to other hardware by using hardware abstraction layer.

Usage Example - main.cpp

#include "mbed.h"
#include "ds2482.h"

#define MAX_TEMP_SENSORS        16
#define CONNECTED_DS2482_HUBS   2

struct sDS1820_t
{    
    struct sDS2482_t *hub;
    uint8_t u8RomNr[8];
};

struct sDS1820_t sDS1820[MAX_TEMP_SENSORS];
struct sDS2482_t sDS2482[CONNECTED_DS2482_HUBS];

Serial console(USBTX, USBRX);
I2C i2c (p9, p10);

int8_t i8SetupTempSensors(void)
{
    int x=0;    
    
    sDS2482[0].u8Addr = DS2482_ADDR1;     
    sDS2482[1].u8Addr = DS2482_ADDR2;
    
    for(int loop=0; loop<2; loop++)
    {   
        int8_t i8Tmp = i8DS2482Reset(&sDS2482[loop]);
        if(i8Tmp)
            return i8Tmp;
        
        i8Tmp = i8DS2482SetControlBits(&sDS2482[loop], APU | SPU );
        if(i8Tmp)
            return i8Tmp;
        
        i8Tmp = i8DS2482_OWReset(&sDS2482[loop]);        
        if(i8Tmp) 
            return i8Tmp;
            
        while(i16DS2482_OWSearch(&sDS2482[loop]) > 0)
        {            
            sDS1820[x].hub = &sDS2482[loop];
            for(int z=0; z<8; z++)
                sDS1820[x].u8RomNr[z] = sDS2482[loop].u8RomNr[z];                        
            x++;
        }
    }  
    return x;
}

int main(void)
{
    uint8_t u8SensorCount;
    
    mbed_i2c = &i2c; 
    
    console.baud(115200);    
    
    int8_t i8Ret = i8SetupTempSensors();    
    
    if(i8Ret < 0)
    {
        console.printf("Error -i8Ret\n");    
        while(1);       // error occured
    }
    
    u8SensorCount = i8Ret;
    
    while(1)
    {
        // Start Temperature Conversion on all DS1820
        for(uint8_t loop = 0; loop < CONNECTED_DS2482_HUBS; loop++)
        {            
            i8Ret = i8DS2482_OWStartAllDS1820(&sDS2482[loop], 0);
                if(i8Ret) 
                {
                    console.printf("Error %i\n", -i8Ret);
                    while(1);   // error!            
                }
        }
        
        // Wait until all DS1820 have completed the conversion
        for(uint8_t loop = 0; loop < CONNECTED_DS2482_HUBS; loop++)        
            while(!i8DS2482_OWCheckDeviceReady(&sDS2482[loop]));                        
        
        // Get temperature values and display them
        for(uint8_t z=0; z<u8SensorCount; z++)
        {
            int16_t i16Tmp = i16DS2482_OWReadDS1820(sDS1820[z].hub, sDS1820[z].u8RomNr, 0);
            if(i16Tmp < 0)    
            {                
                console.printf("Error %i\n", -i16Tmp);
                while(1);           // error                    
            }
            else
            {
                uint8_t u8Tmp = (i16Tmp-109)/2;
                uint8_t u8Tmp2;
                if((int16_t)u8Tmp*2+109 != i16Tmp)
                    u8Tmp2=5;
                else
                    u8Tmp2=0;
                console.printf("[%02i] %02i", z+1, u8Tmp);         
                console.printf(",%iC | ", u8Tmp2);
            }
            if((z+1)%8==0)
                console.printf("\n");
        }                                        
    }
}
Committer:
stefangun
Date:
Thu Jul 24 14:14:40 2014 +0000
Revision:
0:39243a42bc87
Driver for Maxim DS2482 1Wire-to-I2C bridge IC.; Includes interface functions for DS1820 temperature sensors.; Offers HAL (Hardware Abstraction Layer) for easy porting to other hardware environment.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
stefangun 0:39243a42bc87 1 #include "i2c_api.h"
stefangun 0:39243a42bc87 2 #include "wait_api.h"
stefangun 0:39243a42bc87 3
stefangun 0:39243a42bc87 4 /* Driver Object Definition */
stefangun 0:39243a42bc87 5 struct sDS2482_t
stefangun 0:39243a42bc87 6 {
stefangun 0:39243a42bc87 7 uint32_t u32Flags;
stefangun 0:39243a42bc87 8 uint8_t u8Addr;
stefangun 0:39243a42bc87 9
stefangun 0:39243a42bc87 10 uint8_t u8RomNr[8];
stefangun 0:39243a42bc87 11 int16_t i16LastDiscrepancy;
stefangun 0:39243a42bc87 12 int16_t i16LastFamilyDiscrepancy;
stefangun 0:39243a42bc87 13 int16_t i16LastDeviceFlag;
stefangun 0:39243a42bc87 14 uint8_t u8CRC8;
stefangun 0:39243a42bc87 15 } ;
stefangun 0:39243a42bc87 16
stefangun 0:39243a42bc87 17 /* DS2482 Low-Level-Interface */
stefangun 0:39243a42bc87 18 int8_t i8DS2482Reset (struct sDS2482_t *dev);
stefangun 0:39243a42bc87 19 int16_t i16DS2482GetStatus (struct sDS2482_t *dev);
stefangun 0:39243a42bc87 20 int8_t i8DS2482SetControlBits (struct sDS2482_t *dev, uint8_t u8Flags);
stefangun 0:39243a42bc87 21 // TODO: extract basic write/read function from higher-level functions below
stefangun 0:39243a42bc87 22
stefangun 0:39243a42bc87 23 /* 1Wire Interface Functions */
stefangun 0:39243a42bc87 24 int8_t i8DS2482_OWWait (struct sDS2482_t *dev);
stefangun 0:39243a42bc87 25 int8_t i8DS2482_OWReset (struct sDS2482_t *dev);
stefangun 0:39243a42bc87 26 int8_t i8DS2482_OWTouchBit (struct sDS2482_t *dev, uint8_t sendbit);
stefangun 0:39243a42bc87 27 int8_t i8DS2482_OWReadBit (struct sDS2482_t *dev);
stefangun 0:39243a42bc87 28 int8_t i8DS2482_OWWriteBit (struct sDS2482_t *dev, uint8_t sendbit);
stefangun 0:39243a42bc87 29 int8_t i8DS2482_OWWriteByte (struct sDS2482_t *dev, uint8_t sendbyte);
stefangun 0:39243a42bc87 30 int16_t i16DS2482_OWReadByte (struct sDS2482_t *dev);
stefangun 0:39243a42bc87 31 int8_t i8DS2482_OWBlock (struct sDS2482_t *dev, uint8_t *tran_buf, uint8_t tran_len);
stefangun 0:39243a42bc87 32 int16_t i16DS2482_OWTouchByte (struct sDS2482_t *dev, uint8_t sendbyte);
stefangun 0:39243a42bc87 33 int16_t i16DS2482_OWFirst (struct sDS2482_t *dev);
stefangun 0:39243a42bc87 34 int16_t i16DS2482_OWNext (struct sDS2482_t *dev);
stefangun 0:39243a42bc87 35 int16_t i16DS2482_OWSearch (struct sDS2482_t *dev);
stefangun 0:39243a42bc87 36 int16_t i16DS2482_search_triplet (struct sDS2482_t *dev, int search_direction);
stefangun 0:39243a42bc87 37 int8_t i8DS2482_OWSelectDevice (struct sDS2482_t *dev, uint8_t *u8SN);
stefangun 0:39243a42bc87 38 int8_t i8DS2482_OWCheckDeviceReady (struct sDS2482_t *dev);
stefangun 0:39243a42bc87 39
stefangun 0:39243a42bc87 40 /* DS1820-Specific High-Level-Functions */
stefangun 0:39243a42bc87 41 int8_t i8DS2482_OWStartAllDS1820 (struct sDS2482_t *dev, uint8_t u8WaitForCompletion);
stefangun 0:39243a42bc87 42 int16_t i16DS2482_OWReadDS1820 (struct sDS2482_t *dev, uint8_t *u8SN, uint8_t u8ManualStart);
stefangun 0:39243a42bc87 43 int8_t i8DS2482_OWReadDS1820Precise(struct sDS2482_t *dev, uint8_t *u8SN, uint8_t u8ManualStart, int16_t *i16Temperature);
stefangun 0:39243a42bc87 44
stefangun 0:39243a42bc87 45 /* Helper Function */
stefangun 0:39243a42bc87 46 uint8_t calc_crc8 (uint8_t *data_in, int number_of_bytes_to_read);
stefangun 0:39243a42bc87 47
stefangun 0:39243a42bc87 48 /* =============== USAGE EXAMPLE ===============
stefangun 0:39243a42bc87 49 #include "mbed.h"
stefangun 0:39243a42bc87 50 #include "ds2482.h"
stefangun 0:39243a42bc87 51
stefangun 0:39243a42bc87 52 #define MAX_TEMP_SENSORS 16
stefangun 0:39243a42bc87 53 #define CONNECTED_DS2482_HUBS 2
stefangun 0:39243a42bc87 54
stefangun 0:39243a42bc87 55 struct sDS1820_t
stefangun 0:39243a42bc87 56 {
stefangun 0:39243a42bc87 57 struct sDS2482_t *hub;
stefangun 0:39243a42bc87 58 uint8_t u8RomNr[8];
stefangun 0:39243a42bc87 59 };
stefangun 0:39243a42bc87 60
stefangun 0:39243a42bc87 61 struct sDS1820_t sDS1820[MAX_TEMP_SENSORS];
stefangun 0:39243a42bc87 62 struct sDS2482_t sDS2482[CONNECTED_DS2482_HUBS];
stefangun 0:39243a42bc87 63
stefangun 0:39243a42bc87 64 Serial console(USBTX, USBRX);
stefangun 0:39243a42bc87 65 I2C i2c (p9, p10);
stefangun 0:39243a42bc87 66
stefangun 0:39243a42bc87 67 int8_t i8SetupTempSensors(void)
stefangun 0:39243a42bc87 68 {
stefangun 0:39243a42bc87 69 int x=0;
stefangun 0:39243a42bc87 70
stefangun 0:39243a42bc87 71 sDS2482[0].u8Addr = DS2482_ADDR1;
stefangun 0:39243a42bc87 72 sDS2482[1].u8Addr = DS2482_ADDR2;
stefangun 0:39243a42bc87 73
stefangun 0:39243a42bc87 74 for(int loop=0; loop<2; loop++)
stefangun 0:39243a42bc87 75 {
stefangun 0:39243a42bc87 76 int8_t i8Tmp = i8DS2482Reset(&sDS2482[loop]);
stefangun 0:39243a42bc87 77 if(i8Tmp)
stefangun 0:39243a42bc87 78 return i8Tmp;
stefangun 0:39243a42bc87 79
stefangun 0:39243a42bc87 80 i8Tmp = i8DS2482SetControlBits(&sDS2482[loop], APU | SPU );
stefangun 0:39243a42bc87 81 if(i8Tmp)
stefangun 0:39243a42bc87 82 return i8Tmp;
stefangun 0:39243a42bc87 83
stefangun 0:39243a42bc87 84 i8Tmp = i8DS2482_OWReset(&sDS2482[loop]);
stefangun 0:39243a42bc87 85 if(i8Tmp)
stefangun 0:39243a42bc87 86 return i8Tmp;
stefangun 0:39243a42bc87 87
stefangun 0:39243a42bc87 88 while(i16DS2482_OWSearch(&sDS2482[loop]) > 0)
stefangun 0:39243a42bc87 89 {
stefangun 0:39243a42bc87 90 sDS1820[x].hub = &sDS2482[loop];
stefangun 0:39243a42bc87 91 for(int z=0; z<8; z++)
stefangun 0:39243a42bc87 92 sDS1820[x].u8RomNr[z] = sDS2482[loop].u8RomNr[z];
stefangun 0:39243a42bc87 93 x++;
stefangun 0:39243a42bc87 94 }
stefangun 0:39243a42bc87 95 }
stefangun 0:39243a42bc87 96 return x;
stefangun 0:39243a42bc87 97 }
stefangun 0:39243a42bc87 98
stefangun 0:39243a42bc87 99 int main(void)
stefangun 0:39243a42bc87 100 {
stefangun 0:39243a42bc87 101 uint8_t u8SensorCount;
stefangun 0:39243a42bc87 102
stefangun 0:39243a42bc87 103 mbed_i2c = &i2c;
stefangun 0:39243a42bc87 104
stefangun 0:39243a42bc87 105 console.baud(115200);
stefangun 0:39243a42bc87 106
stefangun 0:39243a42bc87 107 int8_t i8Ret = i8SetupTempSensors();
stefangun 0:39243a42bc87 108
stefangun 0:39243a42bc87 109 if(i8Ret < 0)
stefangun 0:39243a42bc87 110 {
stefangun 0:39243a42bc87 111 console.printf("Error -i8Ret\n");
stefangun 0:39243a42bc87 112 while(1); // error occured
stefangun 0:39243a42bc87 113 }
stefangun 0:39243a42bc87 114
stefangun 0:39243a42bc87 115 u8SensorCount = i8Ret;
stefangun 0:39243a42bc87 116
stefangun 0:39243a42bc87 117 while(1)
stefangun 0:39243a42bc87 118 {
stefangun 0:39243a42bc87 119 // Start Temperature Conversion on all DS1820
stefangun 0:39243a42bc87 120 for(uint8_t loop = 0; loop < CONNECTED_DS2482_HUBS; loop++)
stefangun 0:39243a42bc87 121 {
stefangun 0:39243a42bc87 122 i8Ret = i8DS2482_OWStartAllDS1820(&sDS2482[loop], 0);
stefangun 0:39243a42bc87 123 if(i8Ret)
stefangun 0:39243a42bc87 124 {
stefangun 0:39243a42bc87 125 console.printf("Error %i\n", -i8Ret);
stefangun 0:39243a42bc87 126 while(1); // error!
stefangun 0:39243a42bc87 127 }
stefangun 0:39243a42bc87 128 }
stefangun 0:39243a42bc87 129
stefangun 0:39243a42bc87 130 // Wait until all DS1820 have completed the conversion
stefangun 0:39243a42bc87 131 for(uint8_t loop = 0; loop < CONNECTED_DS2482_HUBS; loop++)
stefangun 0:39243a42bc87 132 while(!i8DS2482_OWCheckDeviceReady(&sDS2482[loop]));
stefangun 0:39243a42bc87 133
stefangun 0:39243a42bc87 134 // Get temperature values and display them
stefangun 0:39243a42bc87 135 for(uint8_t z=0; z<u8SensorCount; z++)
stefangun 0:39243a42bc87 136 {
stefangun 0:39243a42bc87 137 int16_t i16Tmp = i16DS2482_OWReadDS1820(sDS1820[z].hub, sDS1820[z].u8RomNr, 0);
stefangun 0:39243a42bc87 138 if(i16Tmp < 0)
stefangun 0:39243a42bc87 139 {
stefangun 0:39243a42bc87 140 console.printf("Error %i\n", -i16Tmp);
stefangun 0:39243a42bc87 141 while(1); // error
stefangun 0:39243a42bc87 142 }
stefangun 0:39243a42bc87 143 else
stefangun 0:39243a42bc87 144 {
stefangun 0:39243a42bc87 145 uint8_t u8Tmp = (i16Tmp-109)/2;
stefangun 0:39243a42bc87 146 uint8_t u8Tmp2;
stefangun 0:39243a42bc87 147 if((int16_t)u8Tmp*2+109 != i16Tmp)
stefangun 0:39243a42bc87 148 u8Tmp2=5;
stefangun 0:39243a42bc87 149 else
stefangun 0:39243a42bc87 150 u8Tmp2=0;
stefangun 0:39243a42bc87 151 console.printf("[%02i] %02i", z+1, u8Tmp);
stefangun 0:39243a42bc87 152 console.printf(",%iC | ", u8Tmp2);
stefangun 0:39243a42bc87 153 }
stefangun 0:39243a42bc87 154 if((z+1)%8==0)
stefangun 0:39243a42bc87 155 console.printf("\n");
stefangun 0:39243a42bc87 156 }
stefangun 0:39243a42bc87 157 }
stefangun 0:39243a42bc87 158 }
stefangun 0:39243a42bc87 159 */
stefangun 0:39243a42bc87 160
stefangun 0:39243a42bc87 161 /* HW Setup */
stefangun 0:39243a42bc87 162 #define DS2482_ADDR1 0x30
stefangun 0:39243a42bc87 163 #define DS2482_ADDR2 0x32
stefangun 0:39243a42bc87 164
stefangun 0:39243a42bc87 165 #define POLL_LIMIT 50
stefangun 0:39243a42bc87 166
stefangun 0:39243a42bc87 167 #define ACK 1
stefangun 0:39243a42bc87 168 #define NAK 0
stefangun 0:39243a42bc87 169
stefangun 0:39243a42bc87 170 /* DS2482 Status Register Bit Definitions */
stefangun 0:39243a42bc87 171 #define STATUS_1WB (1<<0)
stefangun 0:39243a42bc87 172 #define STATUS_PPD (1<<1)
stefangun 0:39243a42bc87 173 #define STATUS_SD (1<<2)
stefangun 0:39243a42bc87 174 #define STATUS_LL (1<<3)
stefangun 0:39243a42bc87 175 #define STATUS_RST (1<<4)
stefangun 0:39243a42bc87 176 #define STATUS_SBR (1<<5)
stefangun 0:39243a42bc87 177 #define STATUS_TSB (1<<6)
stefangun 0:39243a42bc87 178 #define STATUS_DIR (1<<7)
stefangun 0:39243a42bc87 179
stefangun 0:39243a42bc87 180 /* DS2482 Config Register Definitions */
stefangun 0:39243a42bc87 181 #define APU 0x1 // active pullup
stefangun 0:39243a42bc87 182 #define SPU 0x4 // strong pullup
stefangun 0:39243a42bc87 183 #define OWS 0x8 // 1wire speed
stefangun 0:39243a42bc87 184
stefangun 0:39243a42bc87 185 /* Flag Definition */
stefangun 0:39243a42bc87 186 #define FLAG_SHORT 0x00000001
stefangun 0:39243a42bc87 187
stefangun 0:39243a42bc87 188 /* Error Codes */
stefangun 0:39243a42bc87 189 #define DS2482_ERR_NOPRESENCE 2
stefangun 0:39243a42bc87 190 #define DS2482_ERR_TIMEOUT 3
stefangun 0:39243a42bc87 191 #define DS2482_ERR_I2CWRITE 4
stefangun 0:39243a42bc87 192 #define DS2482_ERR_I2CREAD 5
stefangun 0:39243a42bc87 193 #define DS2482_ERR_CONFIGMISSMATCH 6
stefangun 0:39243a42bc87 194 #define DS2482_ERR_CHECKSUM 7
stefangun 0:39243a42bc87 195
stefangun 0:39243a42bc87 196 /* DS2482 Commands */
stefangun 0:39243a42bc87 197 #define DRST 0xF0
stefangun 0:39243a42bc87 198 #define SRP 0xE1
stefangun 0:39243a42bc87 199 #define WCFG 0xD2
stefangun 0:39243a42bc87 200 #define OWRS 0xB4
stefangun 0:39243a42bc87 201 #define OWSB 0x87
stefangun 0:39243a42bc87 202 #define OWWB 0xA5
stefangun 0:39243a42bc87 203 #define OWRB 0x96
stefangun 0:39243a42bc87 204 #define OWT 0x78
stefangun 0:39243a42bc87 205
stefangun 0:39243a42bc87 206 /* 1Wire CRC Definitions */
stefangun 0:39243a42bc87 207 #define CRC8INIT 0x00
stefangun 0:39243a42bc87 208 #define CRC8POLY 0x18
stefangun 0:39243a42bc87 209