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.

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ds2482.h Source File

ds2482.h

00001 #include "i2c_api.h"
00002 #include "wait_api.h"
00003 
00004 /* Driver Object Definition */       
00005 struct sDS2482_t
00006 {    
00007     uint32_t u32Flags;
00008     uint8_t u8Addr;
00009     
00010     uint8_t u8RomNr[8];
00011     int16_t i16LastDiscrepancy;
00012     int16_t i16LastFamilyDiscrepancy;
00013     int16_t i16LastDeviceFlag;
00014     uint8_t u8CRC8;
00015 } ;
00016 
00017 /* DS2482 Low-Level-Interface */
00018 int8_t  i8DS2482Reset               (struct sDS2482_t *dev);
00019 int16_t i16DS2482GetStatus          (struct sDS2482_t *dev);
00020 int8_t  i8DS2482SetControlBits      (struct sDS2482_t *dev, uint8_t u8Flags);
00021 // TODO: extract basic write/read function from higher-level functions below
00022 
00023 /* 1Wire Interface Functions */
00024 int8_t  i8DS2482_OWWait             (struct sDS2482_t *dev);
00025 int8_t  i8DS2482_OWReset            (struct sDS2482_t *dev);
00026 int8_t  i8DS2482_OWTouchBit         (struct sDS2482_t *dev, uint8_t sendbit);
00027 int8_t  i8DS2482_OWReadBit          (struct sDS2482_t *dev);
00028 int8_t  i8DS2482_OWWriteBit         (struct sDS2482_t *dev, uint8_t sendbit);
00029 int8_t  i8DS2482_OWWriteByte        (struct sDS2482_t *dev, uint8_t sendbyte);
00030 int16_t i16DS2482_OWReadByte        (struct sDS2482_t *dev);
00031 int8_t  i8DS2482_OWBlock            (struct sDS2482_t *dev, uint8_t *tran_buf, uint8_t tran_len);
00032 int16_t i16DS2482_OWTouchByte       (struct sDS2482_t *dev, uint8_t sendbyte);
00033 int16_t i16DS2482_OWFirst           (struct sDS2482_t *dev);
00034 int16_t i16DS2482_OWNext            (struct sDS2482_t *dev);
00035 int16_t i16DS2482_OWSearch          (struct sDS2482_t *dev);
00036 int16_t i16DS2482_search_triplet    (struct sDS2482_t *dev, int search_direction);
00037 int8_t  i8DS2482_OWSelectDevice     (struct sDS2482_t *dev, uint8_t *u8SN);
00038 int8_t  i8DS2482_OWCheckDeviceReady (struct sDS2482_t *dev);
00039 
00040 /* DS1820-Specific High-Level-Functions */
00041 int8_t  i8DS2482_OWStartAllDS1820   (struct sDS2482_t *dev, uint8_t u8WaitForCompletion);
00042 int16_t i16DS2482_OWReadDS1820      (struct sDS2482_t *dev, uint8_t *u8SN, uint8_t u8ManualStart);
00043 int8_t  i8DS2482_OWReadDS1820Precise(struct sDS2482_t *dev, uint8_t *u8SN, uint8_t u8ManualStart, int16_t *i16Temperature);
00044 
00045 /* Helper Function */
00046 uint8_t calc_crc8 (uint8_t *data_in, int number_of_bytes_to_read);
00047 
00048 /* =============== USAGE EXAMPLE =============== 
00049 #include "mbed.h"
00050 #include "ds2482.h"
00051 
00052 #define MAX_TEMP_SENSORS        16
00053 #define CONNECTED_DS2482_HUBS   2
00054 
00055 struct sDS1820_t
00056 {    
00057     struct sDS2482_t *hub;
00058     uint8_t u8RomNr[8];
00059 };
00060 
00061 struct sDS1820_t sDS1820[MAX_TEMP_SENSORS];
00062 struct sDS2482_t sDS2482[CONNECTED_DS2482_HUBS];
00063 
00064 Serial console(USBTX, USBRX);
00065 I2C i2c (p9, p10);
00066 
00067 int8_t i8SetupTempSensors(void)
00068 {
00069     int x=0;    
00070     
00071     sDS2482[0].u8Addr = DS2482_ADDR1;     
00072     sDS2482[1].u8Addr = DS2482_ADDR2;
00073     
00074     for(int loop=0; loop<2; loop++)
00075     {   
00076         int8_t i8Tmp = i8DS2482Reset(&sDS2482[loop]);
00077         if(i8Tmp)
00078             return i8Tmp;
00079         
00080         i8Tmp = i8DS2482SetControlBits(&sDS2482[loop], APU | SPU );
00081         if(i8Tmp)
00082             return i8Tmp;
00083         
00084         i8Tmp = i8DS2482_OWReset(&sDS2482[loop]);        
00085         if(i8Tmp) 
00086             return i8Tmp;
00087             
00088         while(i16DS2482_OWSearch(&sDS2482[loop]) > 0)
00089         {            
00090             sDS1820[x].hub = &sDS2482[loop];
00091             for(int z=0; z<8; z++)
00092                 sDS1820[x].u8RomNr[z] = sDS2482[loop].u8RomNr[z];                        
00093             x++;
00094         }
00095     }  
00096     return x;
00097 }
00098 
00099 int main(void)
00100 {
00101     uint8_t u8SensorCount;
00102     
00103     mbed_i2c = &i2c; 
00104     
00105     console.baud(115200);    
00106     
00107     int8_t i8Ret = i8SetupTempSensors();    
00108     
00109     if(i8Ret < 0)
00110     {
00111         console.printf("Error -i8Ret\n");    
00112         while(1);       // error occured
00113     }
00114     
00115     u8SensorCount = i8Ret;
00116     
00117     while(1)
00118     {
00119         // Start Temperature Conversion on all DS1820
00120         for(uint8_t loop = 0; loop < CONNECTED_DS2482_HUBS; loop++)
00121         {            
00122             i8Ret = i8DS2482_OWStartAllDS1820(&sDS2482[loop], 0);
00123                 if(i8Ret) 
00124                 {
00125                     console.printf("Error %i\n", -i8Ret);
00126                     while(1);   // error!            
00127                 }
00128         }
00129         
00130         // Wait until all DS1820 have completed the conversion
00131         for(uint8_t loop = 0; loop < CONNECTED_DS2482_HUBS; loop++)        
00132             while(!i8DS2482_OWCheckDeviceReady(&sDS2482[loop]));                        
00133         
00134         // Get temperature values and display them
00135         for(uint8_t z=0; z<u8SensorCount; z++)
00136         {
00137             int16_t i16Tmp = i16DS2482_OWReadDS1820(sDS1820[z].hub, sDS1820[z].u8RomNr, 0);
00138             if(i16Tmp < 0)    
00139             {                
00140                 console.printf("Error %i\n", -i16Tmp);
00141                 while(1);           // error                    
00142             }
00143             else
00144             {
00145                 uint8_t u8Tmp = (i16Tmp-109)/2;
00146                 uint8_t u8Tmp2;
00147                 if((int16_t)u8Tmp*2+109 != i16Tmp)
00148                     u8Tmp2=5;
00149                 else
00150                     u8Tmp2=0;
00151                 console.printf("[%02i] %02i", z+1, u8Tmp);         
00152                 console.printf(",%iC | ", u8Tmp2);
00153             }
00154             if((z+1)%8==0)
00155                 console.printf("\n");
00156         }                                        
00157     }
00158 }
00159 */
00160 
00161 /* HW Setup */
00162 #define DS2482_ADDR1    0x30
00163 #define DS2482_ADDR2    0x32
00164 
00165 #define POLL_LIMIT      50
00166 
00167 #define ACK     1
00168 #define NAK     0
00169 
00170 /* DS2482 Status Register Bit Definitions */
00171 #define STATUS_1WB      (1<<0)
00172 #define STATUS_PPD      (1<<1)
00173 #define STATUS_SD       (1<<2)
00174 #define STATUS_LL       (1<<3)
00175 #define STATUS_RST      (1<<4)
00176 #define STATUS_SBR      (1<<5)
00177 #define STATUS_TSB      (1<<6)
00178 #define STATUS_DIR      (1<<7)
00179 
00180 /* DS2482 Config Register Definitions */
00181 #define APU     0x1             // active pullup
00182 #define SPU     0x4             // strong pullup
00183 #define OWS     0x8             // 1wire speed
00184 
00185 /* Flag Definition */
00186 #define FLAG_SHORT      0x00000001
00187 
00188 /* Error Codes */
00189 #define DS2482_ERR_NOPRESENCE       2
00190 #define DS2482_ERR_TIMEOUT          3
00191 #define DS2482_ERR_I2CWRITE         4
00192 #define DS2482_ERR_I2CREAD          5
00193 #define DS2482_ERR_CONFIGMISSMATCH  6
00194 #define DS2482_ERR_CHECKSUM         7
00195 
00196 /* DS2482 Commands */
00197 #define DRST    0xF0
00198 #define SRP     0xE1
00199 #define WCFG    0xD2
00200 #define OWRS    0xB4
00201 #define OWSB    0x87
00202 #define OWWB    0xA5
00203 #define OWRB    0x96
00204 #define OWT     0x78
00205 
00206 /* 1Wire CRC Definitions */
00207 #define CRC8INIT    0x00
00208 #define CRC8POLY    0x18  
00209