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.

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