lib_adt7461.c

00001 #include "edtinc.h"
00002 
00003 #include "lib_adt7461.h"
00004 
00005 
00006 
00007 /* IO to chip pins*/
00008 
00009 
00010 /* int temp_clock = 0, temp_data = 1, temp_den = 0;*/
00011 
00012 void
00013 edt_adt7461_set_clock(EdtDev *edt_p, int clock)
00014 {
00015         int val = edt_reg_read(edt_p, EDT_ADT7461_REG) & ~EDT_ADT7461_CLK;
00016         edt_reg_write(edt_p, EDT_ADT7461_REG, (val | ((clock)?EDT_ADT7461_CLK:0)));
00017 }
00018 
00019 void
00020 edt_adt7461_set_data(EdtDev *edt_p, int data)
00021 {
00022         int val = edt_reg_read(edt_p, EDT_ADT7461_REG) & ~EDT_ADT7461_DATA;
00023         edt_reg_write(edt_p, EDT_ADT7461_REG, (val | ((data)?EDT_ADT7461_DATA:0)));
00024 }
00025 
00026 void
00027 edt_adt7461_set_den(EdtDev *edt_p, int enable)
00028 {
00029         int val = edt_reg_read(edt_p, EDT_ADT7461_REG) & ~EDT_ADT7461_TRISTATE;
00030         edt_reg_write(edt_p, EDT_ADT7461_REG, (val | ((!enable)?EDT_ADT7461_TRISTATE:0)));
00031 }
00032 
00033 void
00034 edt_adt7461_show_bits(EdtDev *edt_p)
00035 
00036 {
00037         int val = edt_reg_read(edt_p, EDT_ADT7461_REG);
00038 
00039         printf("%s %s %s\n", 
00040                 (val & EDT_ADT7461_TRISTATE)? "DEN":"   ",
00041                 (val & EDT_ADT7461_DATA)?"DATA":"    ",
00042                 (val & EDT_ADT7461_CLK)?"CLK":"   ");
00043 
00044 }
00045 
00046 void
00047 edt_adt7461_set(EdtDev *edt_p, int enable, int data, int clock )
00048 {
00049         int rval = edt_reg_read(edt_p, EDT_ADT7461_REG) & 
00050                 ~(EDT_ADT7461_TRISTATE|EDT_ADT7461_CLK|EDT_ADT7461_DATA) ;
00051 
00052         int val  = ((clock)  ? EDT_ADT7461_CLK : 0)
00053                 | ((data) ? EDT_ADT7461_DATA : 0) | ((enable) ? 0 : EDT_ADT7461_TRISTATE);
00054 
00055 
00056         edt_reg_write(edt_p, EDT_ADT7461_REG, val | rval);
00057 
00058 
00059 /*      edt_adt7461_show_bits(edt_p);*/
00060 
00061 
00062 }
00063 
00064 
00065 int
00066 edt_adt7461_get_therm_reset(EdtDev *edt_p)
00067 
00068 {
00069         int val;
00070         val = edt_reg_read(edt_p, EDT_ADT7461_REG) & EDT_ADT7461_THERM_RESET;
00071         return (val != 0);
00072 }
00073 
00074 void
00075 edt_adt7461_set_therm_reset(EdtDev *edt_p, int enable)
00076 
00077 {
00078         int val = edt_reg_read(edt_p, EDT_ADT7461_REG) & ~EDT_ADT7461_THERM_RESET;
00079         edt_reg_write(edt_p, EDT_ADT7461_REG, val | (enable)?EDT_ADT7461_THERM_RESET:0);
00080 }
00081 
00082 int
00083 edt_adt7461_read_alert_pin(EdtDev *edt_p)
00084 {
00085         if ((edt_reg_read(edt_p, EDT_ADT7461_REG) & EDT_ADT7461_ALERT) != 0) {
00086                 return 1;
00087         } else {
00088                 return 0;
00089         }
00090 }
00091 
00092 
00093 int
00094 edt_adt7461_read_data_pin(EdtDev *edt_p)
00095 {
00096         int val;
00097         val = edt_reg_read(edt_p, EDT_ADT7461_REG) & EDT_ADT7461_DATA;
00098         edt_msg(EDTLIB_MSG_INFO_2, "read_data_pin %x\n", val);
00099         return (val != 0);
00100 }
00101 
00102 int
00103 edt_adt7461_read_therm_pin(EdtDev *edt_p)
00104 {
00105         int val;
00106         val = edt_reg_read(edt_p, EDT_ADT7461_REG) & EDT_ADT7461_THERM;
00107         edt_msg(EDTLIB_MSG_INFO_2, "read_data_pin %x\n", val);
00108         return (val != 0);
00109 }
00110 
00111 
00112 /* Basic communication parts*/
00113 
00114 
00115 void
00116 edt_adt7461_send_bus_reset(EdtDev *edt_p) 
00117 {
00118         /*send low to hi data transition while clock is hi*/
00119         edt_msg(EDTLIB_MSG_INFO_2, "Send Bus Reset\n");
00120         edt_adt7461_set_den(edt_p, 1);
00121         edt_adt7461_set_clock(edt_p,1);
00122         edt_adt7461_set_data(edt_p,1);
00123         edt_adt7461_set_data(edt_p,0);
00124 
00125 
00126 
00127 }
00128 
00129 void
00130 edt_adt7461_send_bus_start(EdtDev *edt_p)
00131 
00132 {
00133         /*assuming clock starts hi*/
00134         /*send hi to low data transition while clock is hi*/
00135         edt_msg(EDTLIB_MSG_INFO_2, "Send Bus Start\n");
00136 
00137 
00138         edt_adt7461_set_den(edt_p,1);
00139         edt_adt7461_set_data(edt_p,1);
00140 
00141         edt_adt7461_set_data(edt_p,0);
00142 
00143         edt_adt7461_set_clock(edt_p,0);
00144 
00145 
00146 
00147 }
00148 
00149 void
00150 edt_adt7461_send_bus_stop(EdtDev *edt_p)
00151 
00152 {
00153         /*assuming clock starts low*/
00154         /*send low to hi data transition while clock is hi*/
00155         edt_msg(EDTLIB_MSG_INFO_2, "Send Bus Stop\n");
00156         edt_adt7461_set_clock(edt_p,1);
00157 
00158         edt_adt7461_set_data(edt_p,0);
00159         edt_adt7461_set_data(edt_p,1);
00160 
00161 
00162 }
00163 
00164 int
00165 edt_adt7461_get_ack(EdtDev *edt_p)
00166 
00167 {
00168     int rc = 1;
00169         edt_msg(EDTLIB_MSG_INFO_2, "Get Ack\n");
00170         /*check for acknowledge from slave (pulls data line low)*/
00171 
00172         edt_adt7461_set_den(edt_p,0);
00173         edt_adt7461_set_clock(edt_p, 0);
00174         edt_adt7461_set_clock(edt_p, 1);
00175 
00176 /*    edt_adt7461_set(edt_p,0,0,0); //disable data output*/
00177 
00178    /* edt_adt7461_set(edt_p,0,0,1); //clock high*/
00179         if (edt_adt7461_read_data_pin(edt_p)) {
00180                 rc = 0; /*data is hi - no acknowledge*/
00181         }
00182 
00183         edt_adt7461_set_clock(edt_p, 0);
00184 
00185  /*   edt_adt7461_set(edt_p,0,0,0); //clock low*/
00186 
00187     return rc;
00188 }
00189 
00190 
00191 int
00192 edt_adt7461_send_data_byte(EdtDev *edt_p, int data)
00193 
00194 {
00195         /*assuming clock starts low*/
00196         int i;
00197         /*write out one bit x8*/
00198     int dbit;
00199         edt_msg(EDTLIB_MSG_INFO_2, "Send Data Byte %02x\n", data);
00200 
00201         edt_adt7461_set_den(edt_p, 1);
00202         edt_adt7461_set_clock(edt_p, 0);
00203 
00204         for (i = 0; i < 8; i++) {
00205                 dbit  = data & 0x80;
00206         edt_adt7461_set_data(edt_p, dbit);;
00207         edt_adt7461_set_clock(edt_p, 1);
00208                 edt_adt7461_set_clock(edt_p, 0);
00209 
00210                 data = data << 1;
00211         }
00212 
00213     if (!edt_adt7461_get_ack(edt_p))
00214         return -1;
00215 
00216         return 0;
00217 }
00218 
00219 int
00220 edt_adt7461_get_data_byte(EdtDev *edt_p, int doAcknowledge)
00221 
00222 {
00223         /*assuming clock starts low*/
00224         int i;
00225         int data = 0;
00226         /*read in one bit x8*/
00227         edt_msg(EDTLIB_MSG_INFO_2, "Get Data Byte %s\n", (doAcknowledge)?"Ack" : "");
00228 
00229         edt_adt7461_set_den(edt_p,0);
00230         edt_adt7461_set_clock(edt_p, 0);
00231 
00232         for (i = 0; i < 8; i++) {
00233                 edt_adt7461_set_clock(edt_p,1); /*clock goes high*/
00234                 data = (data << 1) | ( edt_adt7461_read_data_pin(edt_p) ? 0x01 : 0x00 ); /*data bit is read*/
00235                 edt_adt7461_set_clock(edt_p,0); /*clock goes high*/
00236         }
00237         if (doAcknowledge) {
00238 
00239         if (!edt_adt7461_get_ack(edt_p))
00240             return -1;
00241         else
00242                     return data;
00243         } else {
00244                 edt_adt7461_set_den(edt_p,1);
00245                 edt_adt7461_set_data(edt_p, 1);
00246                 /*send no acknowledge to slave (pull data line hi)*/
00247                 edt_adt7461_set_clock(edt_p,1); /*clock goes high*/
00248                 edt_adt7461_set_clock(edt_p,0); /*clock goes low*/
00249 
00250                 return data;
00251         }
00252 }
00253 
00254 
00255 /* Full register read/write commands*/
00256 
00257 
00258 int
00259 edt_adt7461_reg_read(EdtDev *edt_p, int address)
00260 
00261 {
00262         int status;
00263         int data;
00264         switch (0) {
00265                 case 0:
00266                 /*send start condition*/
00267                 edt_adt7461_send_bus_start(edt_p);
00268                 /*send address of temperature controller*/
00269                 status = edt_adt7461_send_data_byte(edt_p, (EDT_ADT7461_DEVICE_ADDR << 1) | 0x00);
00270                 if (status == -1) break;
00271                 /*send register address for read*/
00272                 status = edt_adt7461_send_data_byte(edt_p, address);
00273                 if (status == -1) break;
00274                 /*send stop condition*/
00275                 edt_adt7461_send_bus_stop(edt_p);
00276                 /*send start condition*/
00277                 edt_adt7461_send_bus_start(edt_p);
00278                 /*send address of temperature controller*/
00279                 status = edt_adt7461_send_data_byte(edt_p, (EDT_ADT7461_DEVICE_ADDR << 1) | 0x01);
00280                 if (status == -1) break;
00281                 /*read data for read*/
00282                 data = edt_adt7461_get_data_byte(edt_p, 0);
00283                 if (data == -1) break;
00284                 /*send stop condition*/
00285                 edt_adt7461_send_bus_stop(edt_p);
00286                 return data;
00287         }
00288         /*reset bus*/
00289         edt_adt7461_send_bus_reset(edt_p);
00290         return -1;      /*transaction failed*/
00291 }
00292 
00293 int
00294 edt_adt7461_reg_write(EdtDev *edt_p, int address, int data)
00295 
00296 {
00297         int status;
00298         switch (0) {
00299                 case 0:
00300                 /*send start condition*/
00301                 edt_adt7461_send_bus_start(edt_p);
00302                 /*send address of temperature controller*/
00303                 status = edt_adt7461_send_data_byte(edt_p, (EDT_ADT7461_DEVICE_ADDR << 1) | 0x00);
00304                 if (status == -1) break;
00305                 /*send register address for write*/
00306                 status = edt_adt7461_send_data_byte(edt_p, address);
00307                 if (status == -1) break;
00308                 /*send data for write*/
00309                 status = edt_adt7461_send_data_byte(edt_p, data);
00310                 if (status == -1) break;
00311                 /*send stop condition*/
00312                 edt_adt7461_send_bus_stop(edt_p);
00313                 return 0;
00314         }
00315         /*reset bus*/
00316         edt_adt7461_send_bus_reset(edt_p);
00317         return -1;      /*transaction failed*/
00318 }
00319 
00320 int
00321 edt_adt7461_read_alert_address(EdtDev *edt_p)
00322 
00323 {
00324         int status;
00325         int deviceAddress;
00326         switch (0) {
00327                 case 0:
00328                 /*send start condition*/
00329                 edt_adt7461_send_bus_start(edt_p);
00330                 /*send address of temperature controller*/
00331                 status = edt_adt7461_send_data_byte(edt_p, (EDT_ADT7461_ALERT_RESPONSE_ADDR << 1) | 0x01);
00332                 if (status == -1) break;
00333                 /*read address of alert-causing device*/
00334                 deviceAddress = edt_adt7461_get_data_byte(edt_p, 0);
00335                 if (deviceAddress == -1) break;
00336                 /*send stop condition*/
00337                 edt_adt7461_send_bus_stop(edt_p);
00338                 return deviceAddress & 0xFE;
00339         }
00340         /*reset bus*/
00341         edt_adt7461_send_bus_reset(edt_p);
00342         return -1;      /*transaction failed*/
00343 }
00344 
00345 /* Main*/
00346 
00347 
00348 
00349 /* returns 10-bit temperature*/
00350 /* with two bits for fractional degree*/
00351 int
00352 edt_adt7461_read_extern(EdtDev *edt_p, int tenbits)
00353 
00354 {
00355 
00356     int low, high;
00357 
00358     low = edt_adt7461_reg_read(edt_p,EDT_ADT7461_EXT_TEMP_LOW_R);
00359     high = edt_adt7461_reg_read(edt_p,EDT_ADT7461_EXT_TEMP_R);
00360 
00361     if (tenbits)
00362                 return (high << 2) | (low >> 6);
00363         else
00364                 return high;
00365 }
00366 
00367 int
00368 edt_adt7461_read_intern(EdtDev *edt_p)
00369 
00370 {
00371     return edt_adt7461_reg_read(edt_p,EDT_ADT7461_INTERN_TEMP_R);
00372 }
00373 
00374 void
00375 edt_adt7461_set_alert_high(EdtDev *edt_p, int alert_high)
00376 
00377 {       
00378         edt_adt7461_reg_write(edt_p, EDT_ADT7461_EXT_TEMP_HIGH_W, alert_high);
00379 }
00380 
00381 int
00382 edt_adt7461_get_alert_high(EdtDev *edt_p)
00383 
00384 {       
00385         return edt_adt7461_reg_read(edt_p, EDT_ADT7461_EXT_TEMP_HIGH_R);
00386 }
00387 void
00388 edt_adt7461_set_alert_low(EdtDev *edt_p, int alert_low)
00389 
00390 {       
00391         edt_adt7461_reg_write(edt_p, EDT_ADT7461_EXT_TEMP_LOW_W, alert_low);
00392 }
00393 
00394 int
00395 edt_adt7461_get_alert_low(EdtDev *edt_p)
00396 
00397 {       
00398         return edt_adt7461_reg_read(edt_p, EDT_ADT7461_EXT_TEMP_LOW_R);
00399 }
00400 
00401 void
00402 edt_adt7461_set_therm(EdtDev *edt_p, int therm_high)
00403 
00404 {
00405                 
00406         edt_adt7461_reg_write(edt_p, EDT_ADT7461_EXT_THERM_W, therm_high);
00407 
00408 
00409 }
00410 
00411 int
00412 edt_adt7461_get_therm(EdtDev *edt_p)
00413 
00414 {
00415                 
00416         return edt_adt7461_reg_read(edt_p, EDT_ADT7461_EXT_THERM_W);
00417 
00418 
00419 }
00420 
00421 void
00422 edt_adt7461_set_therm_hysteresis(EdtDev *edt_p, int hysteresis)
00423 
00424 {
00425 
00426                 edt_adt7461_reg_write(edt_p, EDT_ADT7461_THERM_HYSTERESIS, hysteresis);
00427 
00428 }
00429 
00430 int
00431 edt_adt7461_get_therm_hysteresis(EdtDev *edt_p)
00432 
00433 {
00434 
00435         return  edt_adt7461_reg_read(edt_p, EDT_ADT7461_THERM_HYSTERESIS);
00436 
00437 }
00438 
00439 int
00440 edt_adt7461_get_alert(EdtDev *edt_p)
00441 
00442 {
00443         int alert;
00444 
00445         alert = edt_adt7461_read_alert_pin(edt_p);
00446                 
00447         /* clear */
00448 
00449         if (alert)
00450                 edt_adt7461_read_alert_address(edt_p);
00451 
00452         return alert;
00453 }
00454 
00455 void
00456 edt_adt4761_print_status(EdtDev *edt_p, int full)
00457 
00458 {
00459 
00460         int alert;
00461         int external;
00462         int internal;
00463         int therm;
00464         
00465 
00466         alert = edt_adt7461_get_alert(edt_p);
00467         therm = edt_adt7461_read_therm_pin(edt_p);
00468         external = edt_adt7461_read_extern(edt_p, TRUE);
00469         internal = edt_adt7461_read_intern(edt_p);
00470         printf("Ext %d.%02d Int %i", external >> 2, (external & 3) * 25, internal);
00471 
00472         if (full)
00473         {
00474 
00475 
00476                 printf(" Alert min %i max %i",
00477                         edt_adt7461_get_alert_low(edt_p),
00478                         edt_adt7461_get_alert_high(edt_p));
00479                 
00480                 printf(" Therm %d Hyster %d", 
00481                         edt_adt7461_get_therm(edt_p),
00482                         edt_adt7461_get_therm_hysteresis(edt_p));
00483 
00484                 printf(" Stat 0x%02x T Reset %s", 
00485                         edt_adt7461_reg_read(edt_p, EDT_ADT7461_STATUS_REG_R),
00486                         (edt_adt7461_get_therm_reset(edt_p)) ? "On ":"Off"
00487                         );
00488         
00489         
00490                 
00491         }
00492         if (alert) {
00493                 printf(" ALERT");                       
00494         }
00495         if (therm) {
00496                 printf(" THERM");                       
00497         }
00498 
00499         printf("\n");
00500 }
00501 

Generated on Mon May 12 16:38:53 2008 by  doxygen 1.5.1