ntc.c
Go to the documentation of this file.00001
00044 #include "hw/hw_ntc.h"
00045 #include "hw/ntc_map.h"
00046
00047 #include "cfg/cfg_ntc.h"
00048
00049
00050 #define LOG_LEVEL CONFIG_NTC_LOG_LEVEL
00051 #define LOG_VERBOSITY CONFIG_NTC_LOG_FORMAT
00052 #include <cfg/log.h>
00053 #include <cfg/debug.h>
00054
00055 #include <drv/ntc.h>
00056
00057 DB(bool ntc_initialized;)
00058
00067 static size_t upper_bound(const res_t *orig_table, size_t size, res_t val)
00068 {
00069 const res_t *table = orig_table;
00070
00071 while (size)
00072 {
00073 size_t pos = size / 2;
00074 if (val > table[pos])
00075 size = pos;
00076 else
00077 {
00078 table += pos+1;
00079 size -= pos+1;
00080 }
00081 }
00082
00083 return table - orig_table;
00084 }
00085
00086
00104 deg_t ntc_read(NtcDev dev)
00105 {
00106 const NtcHwInfo* hw = ntc_hw_getInfo(dev);
00107 const res_t* r = hw->resistances;
00108
00109 res_t rx;
00110 size_t i;
00111 deg_t degrees = 0;
00112
00113 rx = ntc_hw_read(dev);
00114
00115
00116 i = upper_bound(r, hw->num_resistances, rx);
00117 ASSERT(i <= hw->num_resistances);
00118
00119 if (i >= hw->num_resistances)
00120 return NTC_SHORT_CIRCUIT;
00121 else if (i == 0)
00122 return NTC_OPEN_CIRCUIT;
00123
00124
00125
00126
00127
00128
00129
00130 float tmp;
00131 tmp = 10 * hw->degrees_step * (rx - r[i]) / (r[i - 1] - r[i]);
00132
00133
00134
00135
00136
00137
00138 degrees = (i * hw->degrees_step + hw->degrees_min) * 10 - (int)(tmp);
00139
00140
00141
00142 return degrees;
00143 }
00144
00145
00149 void ntc_init(void)
00150 {
00151 NTC_HW_INIT;
00152 DB(ntc_initialized = true;)
00153 }
00154