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.

353 lines
14 KiB

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