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.

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