Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

362 lines
14 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. // -----------------------------------------------------------------------------
  2. // ADS1X15-based Energy Monitor Sensor over I2C
  3. // Copyright (C) 2017-2018 by Xose Pérez <xose dot perez at gmail dot com>
  4. // -----------------------------------------------------------------------------
  5. #if SENSOR_SUPPORT && EMON_ADS1X15_SUPPORT
  6. #pragma once
  7. #include "Arduino.h"
  8. #include "EmonSensor.h"
  9. #define ADS1X15_CHANNELS (4)
  10. #define ADS1X15_CHIP_ADS1015 (0)
  11. #define ADS1X15_CHIP_ADS1115 (1)
  12. #define ADS1X15_RESOLUTION (16)
  13. #define ADS1015_CONVERSIONDELAY (1)
  14. #define ADS1115_CONVERSIONDELAY (8)
  15. #define ADS1015_BIT_SHIFT (4)
  16. #define ADS1115_BIT_SHIFT (0)
  17. #define ADS1X15_REG_POINTER_MASK (0x03)
  18. #define ADS1X15_REG_POINTER_CONVERT (0x00)
  19. #define ADS1X15_REG_POINTER_CONFIG (0x01)
  20. #define ADS1X15_REG_POINTER_LOWTHRESH (0x02)
  21. #define ADS1X15_REG_POINTER_HITHRESH (0x03)
  22. #define ADS1X15_REG_CONFIG_OS_MASK (0x8000)
  23. #define ADS1X15_REG_CONFIG_OS_SINGLE (0x8000) // Write: Set to start a single-conversion
  24. #define ADS1X15_REG_CONFIG_OS_BUSY (0x0000) // Read: Bit = 0 when conversion is in progress
  25. #define ADS1X15_REG_CONFIG_OS_NOTBUSY (0x8000) // Read: Bit = 1 when device is not performing a conversion
  26. #define ADS1X15_REG_CONFIG_MUX_MASK (0x7000)
  27. #define ADS1X15_REG_CONFIG_MUX_DIFF_0_1 (0x0000) // Differential P = AIN0, N = AIN1 (default)
  28. #define ADS1X15_REG_CONFIG_MUX_DIFF_0_3 (0x1000) // Differential P = AIN0, N = AIN3
  29. #define ADS1X15_REG_CONFIG_MUX_DIFF_1_3 (0x2000) // Differential P = AIN1, N = AIN3
  30. #define ADS1X15_REG_CONFIG_MUX_DIFF_2_3 (0x3000) // Differential P = AIN2, N = AIN3
  31. #define ADS1X15_REG_CONFIG_MUX_SINGLE_0 (0x4000) // Single-ended AIN0
  32. #define ADS1X15_REG_CONFIG_MUX_SINGLE_1 (0x5000) // Single-ended AIN1
  33. #define ADS1X15_REG_CONFIG_MUX_SINGLE_2 (0x6000) // Single-ended AIN2
  34. #define ADS1X15_REG_CONFIG_MUX_SINGLE_3 (0x7000) // Single-ended AIN3
  35. #define ADS1X15_REG_CONFIG_PGA_MASK (0x0E00)
  36. #define ADS1X15_REG_CONFIG_PGA_6_144V (0x0000) // +/-6.144V range = Gain 2/3
  37. #define ADS1X15_REG_CONFIG_PGA_4_096V (0x0200) // +/-4.096V range = Gain 1
  38. #define ADS1X15_REG_CONFIG_PGA_2_048V (0x0400) // +/-2.048V range = Gain 2 (default)
  39. #define ADS1X15_REG_CONFIG_PGA_1_024V (0x0600) // +/-1.024V range = Gain 4
  40. #define ADS1X15_REG_CONFIG_PGA_0_512V (0x0800) // +/-0.512V range = Gain 8
  41. #define ADS1X15_REG_CONFIG_PGA_0_256V (0x0A00) // +/-0.256V range = Gain 16
  42. #define ADS1X15_REG_CONFIG_MODE_MASK (0x0100)
  43. #define ADS1X15_REG_CONFIG_MODE_CONTIN (0x0000) // Continuous conversion mode
  44. #define ADS1X15_REG_CONFIG_MODE_SINGLE (0x0100) // Power-down single-shot mode (default)
  45. #define ADS1X15_REG_CONFIG_DR_MASK (0x00E0)
  46. #define ADS1015_REG_CONFIG_DR_128SPS (0x0000) // 128 samples per second
  47. #define ADS1015_REG_CONFIG_DR_250SPS (0x0020) // 250 samples per second
  48. #define ADS1015_REG_CONFIG_DR_490SPS (0x0040) // 490 samples per second
  49. #define ADS1015_REG_CONFIG_DR_920SPS (0x0060) // 920 samples per second
  50. #define ADS1015_REG_CONFIG_DR_1600SPS (0x0080) // 1600 samples per second (default)
  51. #define ADS1015_REG_CONFIG_DR_2400SPS (0x00A0) // 2400 samples per second
  52. #define ADS1015_REG_CONFIG_DR_3300SPS (0x00C0) // 3300 samples per second
  53. #define ADS1115_REG_CONFIG_DR_8SPS (0x0000) // 8 samples per second
  54. #define ADS1115_REG_CONFIG_DR_16SPS (0x0020) // 16 samples per second
  55. #define ADS1115_REG_CONFIG_DR_32SPS (0x0040) // 32 samples per second
  56. #define ADS1115_REG_CONFIG_DR_64SPS (0x0060) // 64 samples per second
  57. #define ADS1115_REG_CONFIG_DR_128SPS (0x0080) // 128 samples per second (default)
  58. #define ADS1115_REG_CONFIG_DR_250SPS (0x00A0) // 250 samples per second
  59. #define ADS1115_REG_CONFIG_DR_475SPS (0x00C0) // 475 samples per second
  60. #define ADS1115_REG_CONFIG_DR_860SPS (0x00E0) // 860 samples per second
  61. #define ADS1X15_REG_CONFIG_CMODE_MASK (0x0010)
  62. #define ADS1X15_REG_CONFIG_CMODE_TRAD (0x0000) // Traditional comparator with hysteresis (default)
  63. #define ADS1X15_REG_CONFIG_CMODE_WINDOW (0x0010) // Window comparator
  64. #define ADS1X15_REG_CONFIG_CPOL_MASK (0x0008)
  65. #define ADS1X15_REG_CONFIG_CPOL_ACTVLOW (0x0000) // ALERT/RDY pin is low when active (default)
  66. #define ADS1X15_REG_CONFIG_CPOL_ACTVHI (0x0008) // ALERT/RDY pin is high when active
  67. #define ADS1X15_REG_CONFIG_CLAT_MASK (0x0004) // Determines if ALERT/RDY pin latches once asserted
  68. #define ADS1X15_REG_CONFIG_CLAT_NONLAT (0x0000) // Non-latching comparator (default)
  69. #define ADS1X15_REG_CONFIG_CLAT_LATCH (0x0004) // Latching comparator
  70. #define ADS1X15_REG_CONFIG_CQUE_MASK (0x0003)
  71. #define ADS1X15_REG_CONFIG_CQUE_1CONV (0x0000) // Assert ALERT/RDY after one conversions
  72. #define ADS1X15_REG_CONFIG_CQUE_2CONV (0x0001) // Assert ALERT/RDY after two conversions
  73. #define ADS1X15_REG_CONFIG_CQUE_4CONV (0x0002) // Assert ALERT/RDY after four conversions
  74. #define ADS1X15_REG_CONFIG_CQUE_NONE (0x0003) // Disable the comparator and put ALERT/RDY in high state (default)
  75. class EmonADS1X15Sensor : public EmonSensor {
  76. public:
  77. // ---------------------------------------------------------------------
  78. // Public
  79. // ---------------------------------------------------------------------
  80. EmonADS1X15Sensor(): EmonSensor() {
  81. _channels = ADS1X15_CHANNELS;
  82. _sensor_id = SENSOR_EMON_ADS1X15_ID;
  83. init();
  84. }
  85. // ---------------------------------------------------------------------
  86. void setType(unsigned char type) {
  87. if (_type == type) return;
  88. _type = type;
  89. _dirty = true;
  90. }
  91. void setMask(unsigned char mask) {
  92. if (_mask == mask) return;
  93. _mask = mask;
  94. _dirty = true;
  95. }
  96. void setGain(unsigned int gain) {
  97. if (_gain == gain) return;
  98. _gain = gain;
  99. _dirty = true;
  100. }
  101. // ---------------------------------------------------------------------
  102. unsigned char getType() {
  103. return _type;
  104. }
  105. unsigned char getMask() {
  106. return _mask;
  107. }
  108. unsigned char getGain() {
  109. return _gain;
  110. }
  111. // ---------------------------------------------------------------------
  112. // Sensor API
  113. // ---------------------------------------------------------------------
  114. // Initialization method, must be idempotent
  115. void begin() {
  116. if (!_dirty) return;
  117. _dirty = false;
  118. // Discover
  119. unsigned char addresses[] = {0x48, 0x49, 0x4A, 0x4B};
  120. _address = _begin_i2c(_address, sizeof(addresses), addresses);
  121. if (_address == 0) return;
  122. // Calculate ports
  123. _ports = 0;
  124. unsigned char mask = _mask;
  125. while (mask) {
  126. if (mask & 0x01) ++_ports;
  127. mask = mask >> 1;
  128. }
  129. _count = _ports * _magnitudes;
  130. // Bit depth
  131. _resolution = ADS1X15_RESOLUTION;
  132. // Reference based on gain
  133. if (_gain == ADS1X15_REG_CONFIG_PGA_6_144V) _reference = 12.288;
  134. if (_gain == ADS1X15_REG_CONFIG_PGA_4_096V) _reference = 8.192;
  135. if (_gain == ADS1X15_REG_CONFIG_PGA_2_048V) _reference = 4.096;
  136. if (_gain == ADS1X15_REG_CONFIG_PGA_1_024V) _reference = 2.048;
  137. if (_gain == ADS1X15_REG_CONFIG_PGA_0_512V) _reference = 1.024;
  138. if (_gain == ADS1X15_REG_CONFIG_PGA_0_256V) _reference = 0.512;
  139. // Call the parent class method
  140. EmonSensor::begin();
  141. // warmup all channels
  142. warmup();
  143. }
  144. // Descriptive name of the sensor
  145. String description() {
  146. char buffer[30];
  147. snprintf(buffer, sizeof(buffer), "EMON @ ADS1%d15 @ I2C (0x%02X)", _type == ADS1X15_CHIP_ADS1015 ? 0 : 1, _address);
  148. return String(buffer);
  149. }
  150. // Descriptive name of the slot # index
  151. String slot(unsigned char index) {
  152. char buffer[35];
  153. unsigned char channel = getChannel(index % _ports);
  154. snprintf(buffer, sizeof(buffer), "EMON @ ADS1%d15 (A%d) @ I2C (0x%02X)", _type == ADS1X15_CHIP_ADS1015 ? 0 : 1, channel, _address);
  155. return String(buffer);
  156. }
  157. // Address of the sensor (it could be the GPIO or I2C address)
  158. String address(unsigned char index) {
  159. char buffer[10];
  160. unsigned char channel = getChannel(index % _ports);
  161. snprintf(buffer, sizeof(buffer), "0x%02X:%u", _address, channel);
  162. return String(buffer);
  163. }
  164. // Type for slot # index
  165. unsigned char type(unsigned char index) {
  166. if (index < _count) {
  167. _error = SENSOR_ERROR_OK;
  168. unsigned char magnitude = index / _ports;
  169. unsigned char i=0;
  170. #if EMON_REPORT_CURRENT
  171. if (magnitude == i++) return MAGNITUDE_CURRENT;
  172. #endif
  173. #if EMON_REPORT_POWER
  174. if (magnitude == i++) return MAGNITUDE_POWER_APPARENT;
  175. #endif
  176. #if EMON_REPORT_ENERGY
  177. if (magnitude == i) return MAGNITUDE_ENERGY;
  178. #endif
  179. }
  180. _error = SENSOR_ERROR_OUT_OF_RANGE;
  181. return MAGNITUDE_NONE;
  182. }
  183. void pre() {
  184. static unsigned long last = 0;
  185. for (unsigned char port=0; port<_ports; port++) {
  186. unsigned char channel = getChannel(port);
  187. _current[port] = getCurrent(channel);
  188. #if EMON_REPORT_ENERGY
  189. _energy[port] += (_current[port] * _voltage * (millis() - last) / 1000);
  190. #endif
  191. }
  192. last = millis();
  193. _error = SENSOR_ERROR_OK;
  194. }
  195. // Current value for slot # index
  196. double value(unsigned char index) {
  197. if (index < _count) {
  198. _error = SENSOR_ERROR_OK;
  199. unsigned char port = index % _ports;
  200. unsigned char magnitude = index / _ports;
  201. unsigned char i=0;
  202. #if EMON_REPORT_CURRENT
  203. if (magnitude == i++) return _current[port];
  204. #endif
  205. #if EMON_REPORT_POWER
  206. if (magnitude == i++) return _current[port] * _voltage;
  207. #endif
  208. #if EMON_REPORT_ENERGY
  209. if (magnitude == i) return _energy[port];
  210. #endif
  211. }
  212. _error = SENSOR_ERROR_OUT_OF_RANGE;
  213. return 0;
  214. }
  215. protected:
  216. //----------------------------------------------------------------------
  217. // Protected
  218. //----------------------------------------------------------------------
  219. unsigned char getChannel(unsigned char port) {
  220. unsigned char count = 0;
  221. unsigned char bit = 1;
  222. for (unsigned char channel=0; channel<ADS1X15_CHANNELS; channel++) {
  223. if ((_mask & bit) == bit) {
  224. if (count == port) return channel;
  225. ++count;
  226. }
  227. bit <<= 1;
  228. }
  229. return 0;
  230. }
  231. void warmup() {
  232. for (unsigned char port=0; port<_ports; port++) {
  233. unsigned char channel = getChannel(port);
  234. _pivot[channel] = _adc_counts >> 1;
  235. getCurrent(channel);
  236. }
  237. }
  238. //----------------------------------------------------------------------
  239. // I2C
  240. //----------------------------------------------------------------------
  241. void setConfigRegistry(unsigned char channel, bool continuous, bool start) {
  242. // Start with default values
  243. uint16_t config = 0;
  244. config |= _gain; // Set PGA/voltage range (0x0200)
  245. config |= ADS1X15_REG_CONFIG_DR_MASK; // Always at max speed (0x00E0)
  246. //config |= ADS1X15_REG_CONFIG_CMODE_TRAD; // Traditional comparator (default val) (0x0000)
  247. //config |= ADS1X15_REG_CONFIG_CPOL_ACTVLOW; // Alert/Rdy active low (default val) (0x0000)
  248. //config |= ADS1X15_REG_CONFIG_CLAT_NONLAT; // Non-latching (default val) (0x0000)
  249. config |= ADS1X15_REG_CONFIG_CQUE_NONE; // Disable the comparator (default val) (0x0003)
  250. if (start) {
  251. config |= ADS1X15_REG_CONFIG_OS_SINGLE; // Start a single-conversion (0x8000)
  252. }
  253. if (continuous) {
  254. //config |= ADS1X15_REG_CONFIG_MODE_CONTIN; // Continuous mode (default) (0x0000)
  255. } else {
  256. config |= ADS1X15_REG_CONFIG_MODE_SINGLE; // Single-shot mode (0x0100)
  257. }
  258. config |= ((channel + 4) << 12); // Set single-ended input channel (0x4000 - 0x7000)
  259. #if SENSOR_DEBUG
  260. //Serial.printf("[EMON] ADS1X115 Config Registry: %04X\n", config);
  261. #endif
  262. // Write config register to the ADC
  263. i2c_write_uint16(_address, ADS1X15_REG_POINTER_CONFIG, config);
  264. }
  265. double getCurrent(unsigned char channel) {
  266. // Force stop by setting single mode and back to continuous
  267. static unsigned char previous = 9;
  268. if (previous != channel) {
  269. setConfigRegistry(channel, true, false);
  270. setConfigRegistry(channel, false, false);
  271. setConfigRegistry(channel, false, true);
  272. delay(10);
  273. readADC(channel);
  274. previous = channel;
  275. }
  276. setConfigRegistry(channel, true, true);
  277. return read(channel);
  278. }
  279. unsigned int readADC(unsigned char channel) {
  280. (void) channel;
  281. unsigned int value = i2c_read_uint16(_address, ADS1X15_REG_POINTER_CONVERT);
  282. if (_type = ADS1X15_CHIP_ADS1015) value >>= ADS1015_BIT_SHIFT;
  283. delayMicroseconds(500);
  284. return value;
  285. }
  286. unsigned char _type = ADS1X15_CHIP_ADS1115;
  287. unsigned char _mask = 0x0F;
  288. unsigned int _gain = ADS1X15_REG_CONFIG_PGA_4_096V;
  289. unsigned char _ports;
  290. };
  291. #endif // SENSOR_SUPPORT && EMON_ADS1X15_SUPPORT