BeRTOS
|
00001 00039 #include "lm75.h" 00040 00041 #include "hw/hw_lm75.h" 00042 00043 #include "cfg/cfg_lm75.h" 00044 00045 #include <cfg/debug.h> 00046 #include <cfg/module.h> 00047 00048 // Define logging setting (for cfg/log.h module). 00049 #define LOG_LEVEL LM75_LOG_LEVEL 00050 #define LOG_FORMAT LM75_LOG_FORMAT 00051 00052 #include <cfg/log.h> 00053 00054 #include <drv/i2c.h> 00055 #include <drv/ntc.h> // Macro and data type to manage celsius degree 00056 00057 #define SELECT_ADDRESS(addr) LM75_ADDRESS_BYTE | (addr << 1) 00058 #define LM75_ADDRESS_BYTE 0x91 00059 #define LM75_PAD_BYTE 0x0 00060 00061 00062 #if !CONFIG_I2C_DISABLE_OLD_API 00063 00064 deg_t lm75_read_1(uint8_t sens_addr) 00065 { 00066 return lm75_read_2(&local_i2c_old_api, sens_addr); 00067 } 00068 #endif /* !CONFIG_I2C_DISABLE_OLD_API */ 00069 00070 00071 /* 00072 * New API 00073 */ 00074 deg_t lm75_read_2(I2c *i2c, uint8_t sens_addr) 00075 { 00076 uint8_t data[2]; 00077 int16_t degree; 00078 int16_t deci_degree; 00079 00080 i2c_start_w(i2c, SELECT_ADDRESS(sens_addr), 1, I2C_NOSTOP); 00081 i2c_putc(i2c, LM75_PAD_BYTE); 00082 i2c_start_r(i2c, SELECT_ADDRESS(sens_addr), sizeof(data), I2C_STOP); 00083 i2c_read(i2c, data, sizeof(data)); 00084 00085 if (i2c_error(i2c)) 00086 return EOF; 00087 00088 degree = (int16_t)data[0]; 00089 deci_degree = (int16_t)(((data[1] >> 7) & 1 ) * 5); 00090 00091 LOG_INFO("[%d.%d C]\n", degree, deci_degree); 00092 00093 return degree * 10 + deci_degree; 00094 }