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.

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