xnrg_04_mcp39f501.ino 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. /*
  2. xnrg_04_mcp39f501.ino - MCP39F501 energy sensor support for Sonoff-Tasmota
  3. Copyright (C) 2018 Theo Arends
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #ifdef USE_ENERGY_SENSOR
  16. #ifdef USE_MCP39F501
  17. /*********************************************************************************************\
  18. * MCP39F501 - Energy (Shelly 2)
  19. *
  20. * Based on datasheet from https://www.microchip.com/wwwproducts/en/MCP39F501
  21. * and https://github.com/OLIMEX/olimex-iot-firmware-esp8266/blob/7a7f9bb56d4b72770dba8d0f18eaa9d956dd0baf/olimex/user/modules/mod_emtr.c
  22. \*********************************************************************************************/
  23. #define XNRG_04 4
  24. #define MCP_TIMEOUT 4
  25. #define MCP_CALIBRATION_TIMEOUT 2
  26. #define MCP_CALIBRATE_POWER 0x001
  27. #define MCP_CALIBRATE_VOLTAGE 0x002
  28. #define MCP_CALIBRATE_CURRENT 0x004
  29. #define MCP_CALIBRATE_FREQUENCY 0x008
  30. #define MCP_SINGLE_WIRE_FLAG 0x100
  31. #define MCP_START_FRAME 0xA5
  32. #define MCP_ACK_FRAME 0x06
  33. #define MCP_ERROR_NAK 0x15
  34. #define MCP_ERROR_CRC 0x51
  35. #define MCP_SINGLE_WIRE 0xAB
  36. #define MCP_SET_ADDRESS 0x41
  37. #define MCP_READ 0x4E
  38. #define MCP_READ_16 0x52
  39. #define MCP_READ_32 0x44
  40. #define MCP_WRITE 0x4D
  41. #define MCP_WRITE_16 0x57
  42. #define MCP_WRITE_32 0x45
  43. #define MCP_SAVE_REGISTERS 0x53
  44. #define MCP_CALIBRATION_BASE 0x0028
  45. #define MCP_CALIBRATION_LEN 52
  46. #define MCP_FREQUENCY_REF_BASE 0x0094
  47. #define MCP_FREQUENCY_GAIN_BASE 0x00AE
  48. #define MCP_FREQUENCY_LEN 4
  49. typedef struct mcp_cal_registers_type {
  50. uint16_t gain_current_rms;
  51. uint16_t gain_voltage_rms;
  52. uint16_t gain_active_power;
  53. uint16_t gain_reactive_power;
  54. sint32_t offset_current_rms;
  55. sint32_t offset_active_power;
  56. sint32_t offset_reactive_power;
  57. sint16_t dc_offset_current;
  58. sint16_t phase_compensation;
  59. uint16_t apparent_power_divisor;
  60. uint32_t system_configuration;
  61. uint16_t dio_configuration;
  62. uint32_t range;
  63. uint32_t calibration_current;
  64. uint16_t calibration_voltage;
  65. uint32_t calibration_active_power;
  66. uint32_t calibration_reactive_power;
  67. uint16_t accumulation_interval;
  68. } mcp_cal_registers_type;
  69. unsigned long mcp_kWhcounter = 0;
  70. uint32_t mcp_system_configuration = 0x03000000;
  71. uint32_t mcp_active_power;
  72. //uint32_t mcp_reactive_power;
  73. //uint32_t mcp_apparent_power;
  74. uint32_t mcp_current_rms;
  75. uint16_t mcp_voltage_rms;
  76. uint16_t mcp_line_frequency;
  77. //sint16_t mcp_power_factor;
  78. uint8_t mcp_address = 0;
  79. uint8_t mcp_calibration_active = 0;
  80. uint8_t mcp_init = 0;
  81. uint8_t mcp_timeout = 0;
  82. uint8_t mcp_calibrate = 0;
  83. /*********************************************************************************************\
  84. * Olimex tools
  85. * https://github.com/OLIMEX/olimex-iot-firmware-esp8266/blob/7a7f9bb56d4b72770dba8d0f18eaa9d956dd0baf/olimex/user/modules/mod_emtr.c
  86. \*********************************************************************************************/
  87. uint8_t McpChecksum(uint8_t *data)
  88. {
  89. uint8_t checksum = 0;
  90. uint8_t offset = 0;
  91. uint8_t len = data[1] -1;
  92. for (byte i = offset; i < len; i++) { checksum += data[i]; }
  93. return checksum;
  94. }
  95. unsigned long McpExtractInt(char *data, uint8_t offset, uint8_t size)
  96. {
  97. unsigned long result = 0;
  98. unsigned long pow = 1;
  99. for (byte i = 0; i < size; i++) {
  100. result = result + (uint8_t)data[offset + i] * pow;
  101. pow = pow * 256;
  102. }
  103. return result;
  104. }
  105. void McpSetInt(unsigned long value, uint8_t *data, uint8_t offset, size_t size)
  106. {
  107. for (byte i = 0; i < size; i++) {
  108. data[offset + i] = ((value >> (i * 8)) & 0xFF);
  109. }
  110. }
  111. void McpSend(uint8_t *data)
  112. {
  113. if (mcp_timeout) { return; }
  114. mcp_timeout = MCP_TIMEOUT;
  115. data[0] = MCP_START_FRAME;
  116. data[data[1] -1] = McpChecksum(data);
  117. // AddLogSerial(LOG_LEVEL_DEBUG_MORE, data, data[1]);
  118. for (byte i = 0; i < data[1]; i++) {
  119. Serial.write(data[i]);
  120. }
  121. }
  122. /********************************************************************************************/
  123. void McpGetAddress(void)
  124. {
  125. uint8_t data[] = { MCP_START_FRAME, 7, MCP_SET_ADDRESS, 0x00, 0x26, MCP_READ_16, 0x00 };
  126. McpSend(data);
  127. }
  128. void McpAddressReceive(void)
  129. {
  130. // 06 05 004D 58
  131. mcp_address = serial_in_buffer[3];
  132. }
  133. /********************************************************************************************/
  134. void McpGetCalibration(void)
  135. {
  136. if (mcp_calibration_active) { return; }
  137. mcp_calibration_active = MCP_CALIBRATION_TIMEOUT;
  138. uint8_t data[] = { MCP_START_FRAME, 8, MCP_SET_ADDRESS, (MCP_CALIBRATION_BASE >> 8) & 0xFF, MCP_CALIBRATION_BASE & 0xFF, MCP_READ, MCP_CALIBRATION_LEN, 0x00 };
  139. McpSend(data);
  140. }
  141. void McpParseCalibration(void)
  142. {
  143. bool action = false;
  144. mcp_cal_registers_type cal_registers;
  145. // 06 37 C882 B6AD 0781 9273 06000000 00000000 00000000 0000 D3FF 0300 00000003 9204 120C1300 204E0000 9808 E0AB0000 D9940000 0200 24
  146. cal_registers.gain_current_rms = McpExtractInt(serial_in_buffer, 2, 2);
  147. cal_registers.gain_voltage_rms = McpExtractInt(serial_in_buffer, 4, 2);
  148. cal_registers.gain_active_power = McpExtractInt(serial_in_buffer, 6, 2);
  149. cal_registers.gain_reactive_power = McpExtractInt(serial_in_buffer, 8, 2);
  150. cal_registers.offset_current_rms = McpExtractInt(serial_in_buffer, 10, 4);
  151. cal_registers.offset_active_power = McpExtractInt(serial_in_buffer, 14, 4);
  152. cal_registers.offset_reactive_power = McpExtractInt(serial_in_buffer, 18, 4);
  153. cal_registers.dc_offset_current = McpExtractInt(serial_in_buffer, 22, 2);
  154. cal_registers.phase_compensation = McpExtractInt(serial_in_buffer, 24, 2);
  155. cal_registers.apparent_power_divisor = McpExtractInt(serial_in_buffer, 26, 2);
  156. cal_registers.system_configuration = McpExtractInt(serial_in_buffer, 28, 4);
  157. cal_registers.dio_configuration = McpExtractInt(serial_in_buffer, 32, 2);
  158. cal_registers.range = McpExtractInt(serial_in_buffer, 34, 4);
  159. cal_registers.calibration_current = McpExtractInt(serial_in_buffer, 38, 4);
  160. cal_registers.calibration_voltage = McpExtractInt(serial_in_buffer, 42, 2);
  161. cal_registers.calibration_active_power = McpExtractInt(serial_in_buffer, 44, 4);
  162. cal_registers.calibration_reactive_power = McpExtractInt(serial_in_buffer, 48, 4);
  163. cal_registers.accumulation_interval = McpExtractInt(serial_in_buffer, 52, 2);
  164. if (mcp_calibrate & MCP_CALIBRATE_POWER) {
  165. cal_registers.calibration_active_power = Settings.energy_power_calibration;
  166. if (McpCalibrationCalc(&cal_registers, 16)) { action = true; }
  167. }
  168. if (mcp_calibrate & MCP_CALIBRATE_VOLTAGE) {
  169. cal_registers.calibration_voltage = Settings.energy_voltage_calibration;
  170. if (McpCalibrationCalc(&cal_registers, 0)) { action = true; }
  171. }
  172. if (mcp_calibrate & MCP_CALIBRATE_CURRENT) {
  173. cal_registers.calibration_current = Settings.energy_current_calibration;
  174. if (McpCalibrationCalc(&cal_registers, 8)) { action = true; }
  175. }
  176. mcp_timeout = 0;
  177. if (action) { McpSetCalibration(&cal_registers); }
  178. mcp_calibrate = 0;
  179. Settings.energy_power_calibration = cal_registers.calibration_active_power;
  180. Settings.energy_voltage_calibration = cal_registers.calibration_voltage;
  181. Settings.energy_current_calibration = cal_registers.calibration_current;
  182. mcp_system_configuration = cal_registers.system_configuration;
  183. if (mcp_system_configuration & MCP_SINGLE_WIRE_FLAG) {
  184. mcp_system_configuration &= ~MCP_SINGLE_WIRE_FLAG; // Reset SingleWire flag
  185. McpSetSystemConfiguration(2);
  186. }
  187. }
  188. bool McpCalibrationCalc(struct mcp_cal_registers_type *cal_registers, uint8_t range_shift)
  189. {
  190. uint32_t measured;
  191. uint32_t expected;
  192. uint16_t *gain;
  193. uint32_t new_gain;
  194. if (range_shift == 0) {
  195. measured = mcp_voltage_rms;
  196. expected = cal_registers->calibration_voltage;
  197. gain = &(cal_registers->gain_voltage_rms);
  198. } else if (range_shift == 8) {
  199. measured = mcp_current_rms;
  200. expected = cal_registers->calibration_current;
  201. gain = &(cal_registers->gain_current_rms);
  202. } else if (range_shift == 16) {
  203. measured = mcp_active_power;
  204. expected = cal_registers->calibration_active_power;
  205. gain = &(cal_registers->gain_active_power);
  206. } else {
  207. return false;
  208. }
  209. if (measured == 0) {
  210. return false;
  211. }
  212. uint32_t range = (cal_registers->range >> range_shift) & 0xFF;
  213. calc:
  214. new_gain = (*gain) * expected / measured;
  215. if (new_gain < 25000) {
  216. range++;
  217. if (measured > 6) {
  218. measured = measured / 2;
  219. goto calc;
  220. }
  221. }
  222. if (new_gain > 55000) {
  223. range--;
  224. measured = measured * 2;
  225. goto calc;
  226. }
  227. *gain = new_gain;
  228. uint32_t old_range = (cal_registers->range >> range_shift) & 0xFF;
  229. cal_registers->range = cal_registers->range ^ (old_range << range_shift);
  230. cal_registers->range = cal_registers->range | (range << range_shift);
  231. return true;
  232. }
  233. /*
  234. void McpCalibrationReactivePower(void)
  235. {
  236. cal_registers.gain_reactive_power = cal_registers.gain_reactive_power * cal_registers.calibration_reactive_power / mcp_reactive_power;
  237. }
  238. */
  239. void McpSetCalibration(struct mcp_cal_registers_type *cal_registers)
  240. {
  241. uint8_t data[7 + MCP_CALIBRATION_LEN + 2 + 1];
  242. data[1] = sizeof(data);
  243. data[2] = MCP_SET_ADDRESS; // Set address pointer
  244. data[3] = (MCP_CALIBRATION_BASE >> 8) & 0xFF; // address
  245. data[4] = (MCP_CALIBRATION_BASE >> 0) & 0xFF; // address
  246. data[5] = MCP_WRITE; // Write N bytes
  247. data[6] = MCP_CALIBRATION_LEN;
  248. McpSetInt(cal_registers->gain_current_rms, data, 0+7, 2);
  249. McpSetInt(cal_registers->gain_voltage_rms, data, 2+7, 2);
  250. McpSetInt(cal_registers->gain_active_power, data, 4+7, 2);
  251. McpSetInt(cal_registers->gain_reactive_power, data, 6+7, 2);
  252. McpSetInt(cal_registers->offset_current_rms, data, 8+7, 4);
  253. McpSetInt(cal_registers->offset_active_power, data, 12+7, 4);
  254. McpSetInt(cal_registers->offset_reactive_power, data, 16+7, 4);
  255. McpSetInt(cal_registers->dc_offset_current, data, 20+7, 2);
  256. McpSetInt(cal_registers->phase_compensation, data, 22+7, 2);
  257. McpSetInt(cal_registers->apparent_power_divisor, data, 24+7, 2);
  258. McpSetInt(cal_registers->system_configuration, data, 26+7, 4);
  259. McpSetInt(cal_registers->dio_configuration, data, 30+7, 2);
  260. McpSetInt(cal_registers->range, data, 32+7, 4);
  261. McpSetInt(cal_registers->calibration_current, data, 36+7, 4);
  262. McpSetInt(cal_registers->calibration_voltage, data, 40+7, 2);
  263. McpSetInt(cal_registers->calibration_active_power, data, 42+7, 4);
  264. McpSetInt(cal_registers->calibration_reactive_power, data, 46+7, 4);
  265. McpSetInt(cal_registers->accumulation_interval, data, 50+7, 2);
  266. data[MCP_CALIBRATION_LEN+7] = MCP_SAVE_REGISTERS; // Save registers to flash
  267. data[MCP_CALIBRATION_LEN+8] = mcp_address; // Device address
  268. McpSend(data);
  269. }
  270. /********************************************************************************************/
  271. void McpSetSystemConfiguration(uint16 interval)
  272. {
  273. // A5 11 41 00 42 45 03 00 01 00 41 00 5A 57 00 06 7A
  274. uint8_t data[17];
  275. data[ 1] = sizeof(data);
  276. data[ 2] = MCP_SET_ADDRESS; // Set address pointer
  277. data[ 3] = 0x00; // address
  278. data[ 4] = 0x42; // address
  279. data[ 5] = MCP_WRITE_32; // Write 4 bytes
  280. data[ 6] = (mcp_system_configuration >> 24) & 0xFF; // system_configuration
  281. data[ 7] = (mcp_system_configuration >> 16) & 0xFF; // system_configuration
  282. data[ 8] = (mcp_system_configuration >> 8) & 0xFF; // system_configuration
  283. data[ 9] = (mcp_system_configuration >> 0) & 0xFF; // system_configuration
  284. data[10] = MCP_SET_ADDRESS; // Set address pointer
  285. data[11] = 0x00; // address
  286. data[12] = 0x5A; // address
  287. data[13] = MCP_WRITE_16; // Write 2 bytes
  288. data[14] = (interval >> 8) & 0xFF; // interval
  289. data[15] = (interval >> 0) & 0xFF; // interval
  290. McpSend(data);
  291. }
  292. /********************************************************************************************/
  293. void McpGetFrequency(void)
  294. {
  295. if (mcp_calibration_active) { return; }
  296. mcp_calibration_active = MCP_CALIBRATION_TIMEOUT;
  297. uint8_t data[] = { MCP_START_FRAME, 11, MCP_SET_ADDRESS, (MCP_FREQUENCY_REF_BASE >> 8) & 0xFF, MCP_FREQUENCY_REF_BASE & 0xFF, MCP_READ_16,
  298. MCP_SET_ADDRESS, (MCP_FREQUENCY_GAIN_BASE >> 8) & 0xFF, MCP_FREQUENCY_GAIN_BASE & 0xFF, MCP_READ_16, 0x00 };
  299. McpSend(data);
  300. }
  301. void McpParseFrequency(void)
  302. {
  303. // 06 07 C350 8000 A0
  304. uint16_t line_frequency_ref = serial_in_buffer[2] * 256 + serial_in_buffer[3];
  305. uint16_t gain_line_frequency = serial_in_buffer[4] * 256 + serial_in_buffer[5];
  306. if (mcp_calibrate & MCP_CALIBRATE_FREQUENCY) {
  307. line_frequency_ref = Settings.energy_frequency_calibration;
  308. if ((0xFFFF == mcp_line_frequency) || (0 == gain_line_frequency)) { // Reset values to 50Hz
  309. mcp_line_frequency = 50000;
  310. gain_line_frequency = 0x8000;
  311. }
  312. gain_line_frequency = gain_line_frequency * line_frequency_ref / mcp_line_frequency;
  313. mcp_timeout = 0;
  314. McpSetFrequency(line_frequency_ref, gain_line_frequency);
  315. }
  316. Settings.energy_frequency_calibration = line_frequency_ref;
  317. mcp_calibrate = 0;
  318. }
  319. void McpSetFrequency(uint16_t line_frequency_ref, uint16_t gain_line_frequency)
  320. {
  321. // A5 11 41 00 94 57 C3 B4 41 00 AE 57 7E 46 53 4D 03
  322. uint8_t data[17];
  323. data[ 1] = sizeof(data);
  324. data[ 2] = MCP_SET_ADDRESS; // Set address pointer
  325. data[ 3] = (MCP_FREQUENCY_REF_BASE >> 8) & 0xFF; // address
  326. data[ 4] = (MCP_FREQUENCY_REF_BASE >> 0) & 0xFF; // address
  327. data[ 5] = MCP_WRITE_16; // Write register
  328. data[ 6] = (line_frequency_ref >> 8) & 0xFF; // line_frequency_ref high
  329. data[ 7] = (line_frequency_ref >> 0) & 0xFF; // line_frequency_ref low
  330. data[ 8] = MCP_SET_ADDRESS; // Set address pointer
  331. data[ 9] = (MCP_FREQUENCY_GAIN_BASE >> 8) & 0xFF; // address
  332. data[10] = (MCP_FREQUENCY_GAIN_BASE >> 0) & 0xFF; // address
  333. data[11] = MCP_WRITE_16; // Write register
  334. data[12] = (gain_line_frequency >> 8) & 0xFF; // gain_line_frequency high
  335. data[13] = (gain_line_frequency >> 0) & 0xFF; // gain_line_frequency low
  336. data[14] = MCP_SAVE_REGISTERS; // Save registers to flash
  337. data[15] = mcp_address; // Device address
  338. McpSend(data);
  339. }
  340. /********************************************************************************************/
  341. void McpGetData(void)
  342. {
  343. uint8_t data[] = { MCP_START_FRAME, 8, MCP_SET_ADDRESS, 0x00, 0x04, MCP_READ, 22, 0x00 };
  344. McpSend(data);
  345. }
  346. void McpParseData(void)
  347. {
  348. // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
  349. // 06 19 61 06 00 00 FE 08 9B 0E 00 00 0B 00 00 00 97 0E 00 00 FF 7F 0C C6 35
  350. // 06 19 CE 18 00 00 F2 08 3A 38 00 00 66 00 00 00 93 38 00 00 36 7F 9A C6 B7
  351. // Ak Ln Current---- Volt- ActivePower ReActivePow ApparentPow Factr Frequ Ck
  352. mcp_current_rms = McpExtractInt(serial_in_buffer, 2, 4);
  353. mcp_voltage_rms = McpExtractInt(serial_in_buffer, 6, 2);
  354. mcp_active_power = McpExtractInt(serial_in_buffer, 8, 4);
  355. // mcp_reactive_power = McpExtractInt(serial_in_buffer, 12, 4);
  356. // mcp_power_factor = McpExtractInt(serial_in_buffer, 20, 2);
  357. mcp_line_frequency = McpExtractInt(serial_in_buffer, 22, 2);
  358. if (energy_power_on) { // Powered on
  359. energy_frequency = (float)mcp_line_frequency / 1000;
  360. energy_voltage = (float)mcp_voltage_rms / 10;
  361. energy_active_power = (float)mcp_active_power / 100;
  362. if (0 == energy_active_power) {
  363. energy_current = 0;
  364. } else {
  365. energy_current = (float)mcp_current_rms / 10000;
  366. }
  367. } else { // Powered off
  368. energy_frequency = 0;
  369. energy_voltage = 0;
  370. energy_active_power = 0;
  371. energy_current = 0;
  372. }
  373. }
  374. /********************************************************************************************/
  375. bool McpSerialInput(void)
  376. {
  377. serial_in_buffer[serial_in_byte_counter++] = serial_in_byte;
  378. unsigned long start = millis();
  379. while (millis() - start < 20) {
  380. yield();
  381. if (Serial.available()) {
  382. serial_in_buffer[serial_in_byte_counter++] = Serial.read();
  383. start = millis();
  384. }
  385. }
  386. AddLogSerial(LOG_LEVEL_DEBUG_MORE);
  387. if (1 == serial_in_byte_counter) {
  388. if (MCP_ERROR_CRC == serial_in_buffer[0]) {
  389. // AddLog_P(LOG_LEVEL_DEBUG, PSTR("MCP: Send " D_CHECKSUM_FAILURE));
  390. mcp_timeout = 0;
  391. }
  392. else if (MCP_ERROR_NAK == serial_in_buffer[0]) {
  393. // AddLog_P(LOG_LEVEL_DEBUG, PSTR("MCP: NAck"));
  394. mcp_timeout = 0;
  395. }
  396. }
  397. else if (MCP_ACK_FRAME == serial_in_buffer[0]) {
  398. if (serial_in_byte_counter == serial_in_buffer[1]) {
  399. if (McpChecksum((uint8_t *)serial_in_buffer) != serial_in_buffer[serial_in_byte_counter -1]) {
  400. AddLog_P(LOG_LEVEL_DEBUG, PSTR("MCP: " D_CHECKSUM_FAILURE));
  401. } else {
  402. if (5 == serial_in_buffer[1]) { McpAddressReceive(); }
  403. if (25 == serial_in_buffer[1]) { McpParseData(); }
  404. if (MCP_CALIBRATION_LEN + 3 == serial_in_buffer[1]) { McpParseCalibration(); }
  405. if (MCP_FREQUENCY_LEN + 3 == serial_in_buffer[1]) { McpParseFrequency(); }
  406. }
  407. }
  408. mcp_timeout = 0;
  409. }
  410. else if (MCP_SINGLE_WIRE == serial_in_buffer[0]) {
  411. mcp_timeout = 0;
  412. }
  413. return 1;
  414. }
  415. /********************************************************************************************/
  416. void McpEverySecond(void)
  417. {
  418. if (mcp_active_power) {
  419. energy_kWhtoday_delta += ((mcp_active_power * 10) / 36);
  420. EnergyUpdateToday();
  421. }
  422. if (mcp_timeout) {
  423. mcp_timeout--;
  424. }
  425. else if (mcp_calibration_active) {
  426. mcp_calibration_active--;
  427. }
  428. else if (mcp_init) {
  429. if (2 == mcp_init) {
  430. McpGetCalibration(); // Get calibration parameters and disable SingleWire mode if enabled
  431. }
  432. else if (1 == mcp_init) {
  433. McpGetFrequency(); // Get calibration parameter
  434. }
  435. mcp_init--;
  436. }
  437. else if (!mcp_address) {
  438. McpGetAddress(); // Get device address for future calibration changes
  439. }
  440. else {
  441. McpGetData(); // Get energy data
  442. }
  443. }
  444. void McpSnsInit(void)
  445. {
  446. SetSeriallog(LOG_LEVEL_NONE); // Free serial interface from logging interference
  447. digitalWrite(15, 1); // GPIO15 - MCP enable
  448. }
  449. void McpDrvInit(void)
  450. {
  451. if (!energy_flg) {
  452. if (SHELLY2 == Settings.module) {
  453. pinMode(15, OUTPUT);
  454. digitalWrite(15, 0); // GPIO15 - MCP disable - Reset Delta Sigma ADC's
  455. baudrate = 4800;
  456. mcp_calibrate = 0;
  457. mcp_timeout = 2; // Initial wait
  458. mcp_init = 2; // Initial setup steps
  459. energy_flg = XNRG_04;
  460. }
  461. }
  462. }
  463. boolean McpCommand(void)
  464. {
  465. boolean serviced = true;
  466. unsigned long value = 0;
  467. if (CMND_POWERSET == energy_command_code) {
  468. if (XdrvMailbox.data_len && mcp_active_power) {
  469. value = (unsigned long)(CharToDouble(XdrvMailbox.data) * 100);
  470. if ((value > 100) && (value < 200000)) { // Between 1W and 2000W
  471. Settings.energy_power_calibration = value;
  472. mcp_calibrate |= MCP_CALIBRATE_POWER;
  473. McpGetCalibration();
  474. }
  475. }
  476. }
  477. else if (CMND_VOLTAGESET == energy_command_code) {
  478. if (XdrvMailbox.data_len && mcp_voltage_rms) {
  479. value = (unsigned long)(CharToDouble(XdrvMailbox.data) * 10);
  480. if ((value > 1000) && (value < 2600)) { // Between 100V and 260V
  481. Settings.energy_voltage_calibration = value;
  482. mcp_calibrate |= MCP_CALIBRATE_VOLTAGE;
  483. McpGetCalibration();
  484. }
  485. }
  486. }
  487. else if (CMND_CURRENTSET == energy_command_code) {
  488. if (XdrvMailbox.data_len && mcp_current_rms) {
  489. value = (unsigned long)(CharToDouble(XdrvMailbox.data) * 10);
  490. if ((value > 100) && (value < 80000)) { // Between 10mA and 8A
  491. Settings.energy_current_calibration = value;
  492. mcp_calibrate |= MCP_CALIBRATE_CURRENT;
  493. McpGetCalibration();
  494. }
  495. }
  496. }
  497. else if (CMND_FREQUENCYSET == energy_command_code) {
  498. if (XdrvMailbox.data_len && mcp_line_frequency) {
  499. value = (unsigned long)(CharToDouble(XdrvMailbox.data) * 1000);
  500. if ((value > 45000) && (value < 65000)) { // Between 45Hz and 65Hz
  501. Settings.energy_frequency_calibration = value;
  502. mcp_calibrate |= MCP_CALIBRATE_FREQUENCY;
  503. McpGetFrequency();
  504. }
  505. }
  506. }
  507. else serviced = false; // Unknown command
  508. return serviced;
  509. }
  510. /*********************************************************************************************\
  511. * Interface
  512. \*********************************************************************************************/
  513. int Xnrg04(byte function)
  514. {
  515. int result = 0;
  516. if (FUNC_PRE_INIT == function) {
  517. McpDrvInit();
  518. }
  519. else if (XNRG_04 == energy_flg) {
  520. switch (function) {
  521. case FUNC_INIT:
  522. McpSnsInit();
  523. break;
  524. case FUNC_EVERY_SECOND:
  525. McpEverySecond();
  526. break;
  527. case FUNC_COMMAND:
  528. result = McpCommand();
  529. break;
  530. case FUNC_SERIAL:
  531. result = McpSerialInput();
  532. break;
  533. }
  534. }
  535. return result;
  536. }
  537. #endif // USE_MCP39F501
  538. #endif // USE_ENERGY_SENSOR