Merge branch 'development' of github.com:arendst/Tasmota into pr_tm1638

This commit is contained in:
Ajith Vasudevan 2021-03-05 17:18:48 +05:30
commit 3653ca5a7e
5 changed files with 152 additions and 107 deletions

View File

@ -115,19 +115,19 @@ int Epd42::Init(void) {
height = EPD_HEIGHT42; height = EPD_HEIGHT42;
Reset(); Reset();
SendCommand(POWER_SETTING); SendCommand(EPD_42_POWER_SETTING);
SendData(0x03); // VDS_EN, VDG_EN SendData(0x03); // VDS_EN, VDG_EN
SendData(0x00); // VCOM_HV, VGHL_LV[1], VGHL_LV[0] SendData(0x00); // VCOM_HV, VGHL_LV[1], VGHL_LV[0]
SendData(0x2b); // VDH SendData(0x2b); // VDH
SendData(0x2b); // VDL SendData(0x2b); // VDL
SendData(0xff); // VDHR SendData(0xff); // VDHR
SendCommand(BOOSTER_SOFT_START); SendCommand(EPD_42_BOOSTER_SOFT_START);
SendData(0x17); SendData(0x17);
SendData(0x17); SendData(0x17);
SendData(0x17); //07 0f 17 1f 27 2F 37 2f SendData(0x17); //07 0f 17 1f 27 2F 37 2f
SendCommand(POWER_ON); SendCommand(EPD_42_POWER_ON);
WaitUntilIdle(); WaitUntilIdle();
SendCommand(PANEL_SETTING); SendCommand(EPD_42_PANEL_SETTING);
// SendData(0xbf); // KW-BF KWR-AF BWROTP 0f // SendData(0xbf); // KW-BF KWR-AF BWROTP 0f
// SendData(0x0b); // SendData(0x0b);
// SendData(0x0F); //300x400 Red mode, LUT from OTP // SendData(0x0F); //300x400 Red mode, LUT from OTP
@ -135,7 +135,7 @@ int Epd42::Init(void) {
SendData(0x3F); //300x400 B/W mode, LUT set by register SendData(0x3F); //300x400 B/W mode, LUT set by register
// SendData(0x2F); //300x400 Red mode, LUT set by register // SendData(0x2F); //300x400 Red mode, LUT set by register
SendCommand(PLL_CONTROL); SendCommand(EPD_42_PLL_CONTROL);
SendData(0x3C); // 3A 100Hz 29 150Hz 39 200Hz 31 171Hz 3C 50Hz (default) 0B 10Hz SendData(0x3C); // 3A 100Hz 29 150Hz 39 200Hz 31 171Hz 3C 50Hz (default) 0B 10Hz
//SendData(0x0B); //0B is 10Hz //SendData(0x0B); //0B is 10Hz
/* EPD hardware init end */ /* EPD hardware init end */
@ -184,8 +184,8 @@ void Epd42::Reset(void) {
* @brief: transmit partial data to the SRAM. The final parameter chooses between dtm=1 and dtm=2 * @brief: transmit partial data to the SRAM. The final parameter chooses between dtm=1 and dtm=2
*/ */
void Epd42::SetPartialWindow(const unsigned char* buffer_black, int x, int y, int w, int l, int dtm) { void Epd42::SetPartialWindow(const unsigned char* buffer_black, int x, int y, int w, int l, int dtm) {
SendCommand(PARTIAL_IN); SendCommand(EPD_42_PARTIAL_IN);
SendCommand(PARTIAL_WINDOW); SendCommand(EPD_42_PARTIAL_WINDOW);
SendData(x >> 8); SendData(x >> 8);
SendData(x & 0xf8); // x should be the multiple of 8, the last 3 bit will always be ignored SendData(x & 0xf8); // x should be the multiple of 8, the last 3 bit will always be ignored
SendData(((x & 0xf8) + w - 1) >> 8); SendData(((x & 0xf8) + w - 1) >> 8);
@ -196,7 +196,7 @@ void Epd42::SetPartialWindow(const unsigned char* buffer_black, int x, int y, in
SendData((y + l - 1) & 0xff); SendData((y + l - 1) & 0xff);
SendData(0x01); // Gates scan both inside and outside of the partial window. (default) SendData(0x01); // Gates scan both inside and outside of the partial window. (default)
// DelayMs(2); // DelayMs(2);
SendCommand((dtm == 1) ? DATA_START_TRANSMISSION_1 : DATA_START_TRANSMISSION_2); SendCommand((dtm == 1) ? EPD_42_DATA_START_TRANSMISSION_1 : EPD_42_DATA_START_TRANSMISSION_2);
if (buffer_black != NULL) { if (buffer_black != NULL) {
for(int i = 0; i < w / 8 * l; i++) { for(int i = 0; i < w / 8 * l; i++) {
SendData(buffer_black[i]^0xff); SendData(buffer_black[i]^0xff);
@ -207,7 +207,7 @@ void Epd42::SetPartialWindow(const unsigned char* buffer_black, int x, int y, in
} }
} }
// DelayMs(2); // DelayMs(2);
SendCommand(PARTIAL_OUT); SendCommand(EPD_42_PARTIAL_OUT);
} }
@ -217,27 +217,27 @@ void Epd42::SetPartialWindow(const unsigned char* buffer_black, int x, int y, in
*/ */
void Epd42::SetLut(void) { void Epd42::SetLut(void) {
unsigned int count; unsigned int count;
SendCommand(LUT_FOR_VCOM); //vcom SendCommand(EPD_42_LUT_FOR_VCOM); //vcom
for(count = 0; count < 44; count++) { for(count = 0; count < 44; count++) {
SendData(pgm_read_byte(&lut_vcom0[count])); SendData(pgm_read_byte(&lut_vcom0[count]));
} }
SendCommand(LUT_WHITE_TO_WHITE); //ww -- SendCommand(EPD_42_LUT_WHITE_TO_WHITE); //ww --
for(count = 0; count < 42; count++) { for(count = 0; count < 42; count++) {
SendData(pgm_read_byte(&lut_ww[count])); SendData(pgm_read_byte(&lut_ww[count]));
} }
SendCommand(LUT_BLACK_TO_WHITE); //bw r SendCommand(EPD_42_LUT_BLACK_TO_WHITE); //bw r
for(count = 0; count < 42; count++) { for(count = 0; count < 42; count++) {
SendData(pgm_read_byte(&lut_bw[count])); SendData(pgm_read_byte(&lut_bw[count]));
} }
SendCommand(LUT_WHITE_TO_BLACK); //wb w SendCommand(EPD_42_LUT_WHITE_TO_BLACK); //wb w
for(count = 0; count < 42; count++) { for(count = 0; count < 42; count++) {
SendData(pgm_read_byte(&lut_wb[count])); SendData(pgm_read_byte(&lut_wb[count]));
} }
SendCommand(LUT_BLACK_TO_BLACK); //bb b SendCommand(EPD_42_LUT_BLACK_TO_BLACK); //bb b
for(count = 0; count < 42; count++) { for(count = 0; count < 42; count++) {
SendData(pgm_read_byte(&lut_bb[count])); SendData(pgm_read_byte(&lut_bb[count]));
} }
@ -250,27 +250,27 @@ void Epd42::SetLut(void) {
void Epd42::SetLutQuick(void) { void Epd42::SetLutQuick(void) {
unsigned int count; unsigned int count;
SendCommand(LUT_FOR_VCOM); //vcom SendCommand(EPD_42_LUT_FOR_VCOM); //vcom
for(count = 0; count < 44; count++) { for(count = 0; count < 44; count++) {
SendData(pgm_read_byte(&lut_vcom0_quick[count])); SendData(pgm_read_byte(&lut_vcom0_quick[count]));
} }
SendCommand(LUT_WHITE_TO_WHITE); //ww -- SendCommand(EPD_42_LUT_WHITE_TO_WHITE); //ww --
for(count = 0; count < 42; count++) { for(count = 0; count < 42; count++) {
SendData(pgm_read_byte(&lut_ww_quick[count])); SendData(pgm_read_byte(&lut_ww_quick[count]));
} }
SendCommand(LUT_BLACK_TO_WHITE); //bw r SendCommand(EPD_42_LUT_BLACK_TO_WHITE); //bw r
for(count = 0; count < 42; count++) { for(count = 0; count < 42; count++) {
SendData(pgm_read_byte(&lut_bw_quick[count])); SendData(pgm_read_byte(&lut_bw_quick[count]));
} }
SendCommand(LUT_WHITE_TO_BLACK); //wb w SendCommand(EPD_42_LUT_WHITE_TO_BLACK); //wb w
for(count = 0; count < 42; count++) { for(count = 0; count < 42; count++) {
SendData(pgm_read_byte(&lut_wb_quick[count])); SendData(pgm_read_byte(&lut_wb_quick[count]));
} }
SendCommand(LUT_BLACK_TO_BLACK); //bb b SendCommand(EPD_42_LUT_BLACK_TO_BLACK); //bb b
for(count = 0; count < 42; count++) { for(count = 0; count < 42; count++) {
SendData(pgm_read_byte(&lut_bb_quick[count])); SendData(pgm_read_byte(&lut_bb_quick[count]));
} }
@ -281,25 +281,25 @@ void Epd42::SetLutQuick(void) {
* @brief: refresh and displays the frame * @brief: refresh and displays the frame
*/ */
void Epd42::DisplayFrame(const unsigned char* frame_buffer) { void Epd42::DisplayFrame(const unsigned char* frame_buffer) {
SendCommand(RESOLUTION_SETTING); SendCommand(EPD_42_RESOLUTION_SETTING);
SendData(width >> 8); SendData(width >> 8);
SendData(width & 0xff); SendData(width & 0xff);
SendData(height >> 8); SendData(height >> 8);
SendData(height & 0xff); SendData(height & 0xff);
SendCommand(VCM_DC_SETTING); SendCommand(EPD_42_VCM_DC_SETTING);
SendData(0x12); SendData(0x12);
SendCommand(VCOM_AND_DATA_INTERVAL_SETTING); SendCommand(EPD_42_VCOM_AND_DATA_INTERVAL_SETTING);
SendCommand(0x97); //VBDF 17|D7 VBDW 97 VBDB 57 VBDF F7 VBDW 77 VBDB 37 VBDR B7 SendCommand(0x97); //VBDF 17|D7 VBDW 97 VBDB 57 VBDF F7 VBDW 77 VBDB 37 VBDR B7
if (frame_buffer != NULL) { if (frame_buffer != NULL) {
SendCommand(DATA_START_TRANSMISSION_1); SendCommand(EPD_42_DATA_START_TRANSMISSION_1);
for(int i = 0; i < width / 8 * height; i++) { for(int i = 0; i < width / 8 * height; i++) {
SendData(0xFF); // bit set: white, bit reset: black SendData(0xFF); // bit set: white, bit reset: black
} }
delay(2); delay(2);
SendCommand(DATA_START_TRANSMISSION_2); SendCommand(EPD_42_DATA_START_TRANSMISSION_2);
for(int i = 0; i < width / 8 * height; i++) { for(int i = 0; i < width / 8 * height; i++) {
SendData(pgm_read_byte(&frame_buffer[i])); SendData(pgm_read_byte(&frame_buffer[i]));
} }
@ -308,7 +308,7 @@ void Epd42::DisplayFrame(const unsigned char* frame_buffer) {
SetLut(); SetLut();
SendCommand(DISPLAY_REFRESH); SendCommand(EPD_42_DISPLAY_REFRESH);
delay(100); delay(100);
WaitUntilIdle(); WaitUntilIdle();
} }
@ -320,19 +320,19 @@ void Epd42::DisplayFrame(const unsigned char* frame_buffer) {
* @brief: clear the frame data from the SRAM, this won't refresh the display * @brief: clear the frame data from the SRAM, this won't refresh the display
*/ */
void Epd42::ClearFrame(void) { void Epd42::ClearFrame(void) {
SendCommand(RESOLUTION_SETTING); SendCommand(EPD_42_RESOLUTION_SETTING);
SendData(width >> 8); SendData(width >> 8);
SendData(width & 0xff); SendData(width & 0xff);
SendData(height >> 8); SendData(height >> 8);
SendData(height & 0xff); SendData(height & 0xff);
SendCommand(DATA_START_TRANSMISSION_1); SendCommand(EPD_42_DATA_START_TRANSMISSION_1);
delay(2); delay(2);
for(int i = 0; i < width / 8 * height; i++) { for(int i = 0; i < width / 8 * height; i++) {
SendData(0xFF); SendData(0xFF);
} }
delay(2); delay(2);
SendCommand(DATA_START_TRANSMISSION_2); SendCommand(EPD_42_DATA_START_TRANSMISSION_2);
delay(2); delay(2);
for(int i = 0; i < width / 8 * height; i++) { for(int i = 0; i < width / 8 * height; i++) {
SendData(0xFF); SendData(0xFF);
@ -347,14 +347,14 @@ void Epd42::ClearFrame(void) {
*/ */
void Epd42::DisplayFrame(void) { void Epd42::DisplayFrame(void) {
SetLut(); SetLut();
SendCommand(DISPLAY_REFRESH); SendCommand(EPD_42_DISPLAY_REFRESH);
delay(100); delay(100);
WaitUntilIdle(); WaitUntilIdle();
} }
void Epd42::DisplayFrameQuick(void) { void Epd42::DisplayFrameQuick(void) {
SetLutQuick(); SetLutQuick();
SendCommand(DISPLAY_REFRESH); SendCommand(EPD_42_DISPLAY_REFRESH);
// DelayMs(100); // DelayMs(100);
// WaitUntilIdle(); // WaitUntilIdle();
} }
@ -367,13 +367,13 @@ void Epd42::DisplayFrameQuick(void) {
* You can use Epd::Reset() to awaken and use Epd::Init() to initialize. * You can use Epd::Reset() to awaken and use Epd::Init() to initialize.
*/ */
void Epd42::Sleep() { void Epd42::Sleep() {
SendCommand(VCOM_AND_DATA_INTERVAL_SETTING); SendCommand(EPD_42_VCOM_AND_DATA_INTERVAL_SETTING);
SendData(0x17); //border floating SendData(0x17); //border floating
SendCommand(VCM_DC_SETTING); //VCOM to 0V SendCommand(EPD_42_VCM_DC_SETTING); //VCOM to 0V
SendCommand(PANEL_SETTING); SendCommand(EPD_42_PANEL_SETTING);
delay(100); delay(100);
SendCommand(POWER_SETTING); //VG&VS to 0V fast SendCommand(EPD_42_POWER_SETTING); //VG&VS to 0V fast
SendData(0x00); SendData(0x00);
SendData(0x00); SendData(0x00);
SendData(0x00); SendData(0x00);
@ -381,9 +381,9 @@ void Epd42::Sleep() {
SendData(0x00); SendData(0x00);
delay(100); delay(100);
SendCommand(POWER_OFF); //power off SendCommand(EPD_42_POWER_OFF); //power off
WaitUntilIdle(); WaitUntilIdle();
SendCommand(DEEP_SLEEP); //deep sleep SendCommand(EPD_42_DEEP_SLEEP); //deep sleep
SendData(0xA5); SendData(0xA5);
} }

View File

@ -34,44 +34,44 @@
#define EPD_HEIGHT42 300 #define EPD_HEIGHT42 300
// EPD4IN2 commands // EPD4IN2 commands
#define PANEL_SETTING 0x00 #define EPD_42_PANEL_SETTING 0x00
#define POWER_SETTING 0x01 #define EPD_42_POWER_SETTING 0x01
#define POWER_OFF 0x02 #define EPD_42_POWER_OFF 0x02
#define POWER_OFF_SEQUENCE_SETTING 0x03 #define EPD_42_POWER_OFF_SEQUENCE_SETTING 0x03
#define POWER_ON 0x04 #define EPD_42_POWER_ON 0x04
#define POWER_ON_MEASURE 0x05 #define EPD_42_POWER_ON_MEASURE 0x05
#define BOOSTER_SOFT_START 0x06 #define EPD_42_BOOSTER_SOFT_START 0x06
#define DEEP_SLEEP 0x07 #define EPD_42_DEEP_SLEEP 0x07
#define DATA_START_TRANSMISSION_1 0x10 #define EPD_42_DATA_START_TRANSMISSION_1 0x10
#define DATA_STOP 0x11 #define EPD_42_DATA_STOP 0x11
#define DISPLAY_REFRESH 0x12 #define EPD_42_DISPLAY_REFRESH 0x12
#define DATA_START_TRANSMISSION_2 0x13 #define EPD_42_DATA_START_TRANSMISSION_2 0x13
#define LUT_FOR_VCOM 0x20 #define EPD_42_LUT_FOR_VCOM 0x20
#define LUT_WHITE_TO_WHITE 0x21 #define EPD_42_LUT_WHITE_TO_WHITE 0x21
#define LUT_BLACK_TO_WHITE 0x22 #define EPD_42_LUT_BLACK_TO_WHITE 0x22
#define LUT_WHITE_TO_BLACK 0x23 #define EPD_42_LUT_WHITE_TO_BLACK 0x23
#define LUT_BLACK_TO_BLACK 0x24 #define EPD_42_LUT_BLACK_TO_BLACK 0x24
#define PLL_CONTROL 0x30 #define EPD_42_PLL_CONTROL 0x30
#define TEMPERATURE_SENSOR_COMMAND 0x40 #define EPD_42_TEMPERATURE_SENSOR_COMMAND 0x40
#define TEMPERATURE_SENSOR_SELECTION 0x41 #define EPD_42_TEMPERATURE_SENSOR_SELECTION 0x41
#define TEMPERATURE_SENSOR_WRITE 0x42 #define EPD_42_TEMPERATURE_SENSOR_WRITE 0x42
#define TEMPERATURE_SENSOR_READ 0x43 #define EPD_42_TEMPERATURE_SENSOR_READ 0x43
#define VCOM_AND_DATA_INTERVAL_SETTING 0x50 #define EPD_42_VCOM_AND_DATA_INTERVAL_SETTING 0x50
#define LOW_POWER_DETECTION 0x51 #define EPD_42_LOW_POWER_DETECTION 0x51
#define TCON_SETTING 0x60 #define EPD_42_TCON_SETTING 0x60
#define RESOLUTION_SETTING 0x61 #define EPD_42_RESOLUTION_SETTING 0x61
#define GSST_SETTING 0x65 #define EPD_42_GSST_SETTING 0x65
#define GET_STATUS 0x71 #define EPD_42_GET_STATUS 0x71
#define AUTO_MEASUREMENT_VCOM 0x80 #define EPD_42_AUTO_MEASUREMENT_VCOM 0x80
#define READ_VCOM_VALUE 0x81 #define EPD_42_READ_VCOM_VALUE 0x81
#define VCM_DC_SETTING 0x82 #define EPD_42_VCM_DC_SETTING 0x82
#define PARTIAL_WINDOW 0x90 #define EPD_42_PARTIAL_WINDOW 0x90
#define PARTIAL_IN 0x91 #define EPD_42_PARTIAL_IN 0x91
#define PARTIAL_OUT 0x92 #define EPD_42_PARTIAL_OUT 0x92
#define PROGRAM_MODE 0xA0 #define EPD_42_PROGRAM_MODE 0xA0
#define ACTIVE_PROGRAMMING 0xA1 #define EPD_42_ACTIVE_PROGRAMMING 0xA1
#define READ_OTP 0xA2 #define EPD_42_READ_OTP 0xA2
#define POWER_SAVING 0xE3 #define EPD_42_POWER_SAVING 0xE3
extern const unsigned char lut_vcom0[]; extern const unsigned char lut_vcom0[];
extern const unsigned char lut_ww[]; extern const unsigned char lut_ww[];

View File

@ -688,7 +688,8 @@ void CmndPowerCal(void)
{ {
Energy.command_code = CMND_POWERCAL; Energy.command_code = CMND_POWERCAL;
if (XnrgCall(FUNC_COMMAND)) { // microseconds if (XnrgCall(FUNC_COMMAND)) { // microseconds
if ((XdrvMailbox.payload > 999) && (XdrvMailbox.payload < 32001)) { // if ((XdrvMailbox.payload > 999) && (XdrvMailbox.payload < 32001)) {
if (XdrvMailbox.payload > 999) {
Settings.energy_power_calibration = XdrvMailbox.payload; Settings.energy_power_calibration = XdrvMailbox.payload;
} }
ResponseCmndNumber(Settings.energy_power_calibration); ResponseCmndNumber(Settings.energy_power_calibration);
@ -699,7 +700,8 @@ void CmndVoltageCal(void)
{ {
Energy.command_code = CMND_VOLTAGECAL; Energy.command_code = CMND_VOLTAGECAL;
if (XnrgCall(FUNC_COMMAND)) { // microseconds if (XnrgCall(FUNC_COMMAND)) { // microseconds
if ((XdrvMailbox.payload > 999) && (XdrvMailbox.payload < 32001)) { // if ((XdrvMailbox.payload > 999) && (XdrvMailbox.payload < 32001)) {
if (XdrvMailbox.payload > 999) {
Settings.energy_voltage_calibration = XdrvMailbox.payload; Settings.energy_voltage_calibration = XdrvMailbox.payload;
} }
ResponseCmndNumber(Settings.energy_voltage_calibration); ResponseCmndNumber(Settings.energy_voltage_calibration);
@ -710,7 +712,8 @@ void CmndCurrentCal(void)
{ {
Energy.command_code = CMND_CURRENTCAL; Energy.command_code = CMND_CURRENTCAL;
if (XnrgCall(FUNC_COMMAND)) { // microseconds if (XnrgCall(FUNC_COMMAND)) { // microseconds
if ((XdrvMailbox.payload > 999) && (XdrvMailbox.payload < 32001)) { // if ((XdrvMailbox.payload > 999) && (XdrvMailbox.payload < 32001)) {
if (XdrvMailbox.payload > 999) {
Settings.energy_current_calibration = XdrvMailbox.payload; Settings.energy_current_calibration = XdrvMailbox.payload;
} }
ResponseCmndNumber(Settings.energy_current_calibration); ResponseCmndNumber(Settings.energy_current_calibration);

View File

@ -127,9 +127,9 @@ void DeepSleepPrepare(void)
// uint32_t deepsleep_sleeptime = DEEPSLEEP_MAX_CYCLE < (RtcSettings.nextwakeup - UtcTime()) ? (uint32_t)DEEPSLEEP_MAX_CYCLE : RtcSettings.nextwakeup - UtcTime(); // uint32_t deepsleep_sleeptime = DEEPSLEEP_MAX_CYCLE < (RtcSettings.nextwakeup - UtcTime()) ? (uint32_t)DEEPSLEEP_MAX_CYCLE : RtcSettings.nextwakeup - UtcTime();
deepsleep_sleeptime = tmin((uint32_t)DEEPSLEEP_MAX_CYCLE ,RtcSettings.nextwakeup - UtcTime()); deepsleep_sleeptime = tmin((uint32_t)DEEPSLEEP_MAX_CYCLE ,RtcSettings.nextwakeup - UtcTime());
// stat/tasmota/STATUS = {"DeepSleep":{"Time":"2019-11-12T21:33:45","Epoch":1573590825}} // stat/tasmota/DEEPSLEEP = {"DeepSleep":{"Time":"2019-11-12T21:33:45","Epoch":1573590825}}
Response_P(PSTR("{\"" D_PRFX_DEEPSLEEP "\":{\"" D_JSON_TIME "\":\"%s\",\"Epoch\":%d}}"), (char*)dt.c_str(), RtcSettings.nextwakeup); Response_P(PSTR("{\"" D_PRFX_DEEPSLEEP "\":{\"" D_JSON_TIME "\":\"%s\",\"Epoch\":%d}}"), (char*)dt.c_str(), RtcSettings.nextwakeup);
MqttPublishPrefixTopic_P(RESULT_OR_STAT, PSTR(D_CMND_STATUS)); MqttPublishPrefixTopic_P(RESULT_OR_STAT, PSTR(D_PRFX_DEEPSLEEP));
// Response_P(S_LWT_OFFLINE); // Response_P(S_LWT_OFFLINE);
// MqttPublishPrefixTopic_P(TELE, PSTR(D_LWT), true); // Offline or remove previous retained topic // MqttPublishPrefixTopic_P(TELE, PSTR(D_LWT), true); // Offline or remove previous retained topic

View File

@ -27,13 +27,15 @@
#define XNRG_19 19 #define XNRG_19 19
//#define CSE7761_SIMULATE
#define CSE7761_DUAL_K1 1 // Current channel sampling resistance in milli Ohm #define CSE7761_DUAL_K1 1 // Current channel sampling resistance in milli Ohm
#define CSE7761_DUAL_K2 1 // Voltage divider resistance in 1k/1M #define CSE7761_DUAL_K2 1 // Voltage divider resistance in 1k/1M
#define CSE7761_DUAL_CLK1 3579545 // System clock (3.579545MHz) used in frequency calculation #define CSE7761_DUAL_CLK1 3579545 // System clock (3.579545MHz) used in frequency calculation
#define CSE7761_UREF 4194304 // 2^22 #define CSE7761_UREF 10000 // Gain 1 * 10000 in V
#define CSE7761_IREF 8388608 // 2^23 #define CSE7761_IREF 160000 // Gain 16 * 10000 in A
#define CSE7761_PREF 2147483648 // 2^31 #define CSE7761_PREF 50000 // in W
#define CSE7761_REG_SYSCON 0x00 // System Control Register #define CSE7761_REG_SYSCON 0x00 // System Control Register
#define CSE7761_REG_EMUCON 0x01 // Metering control register #define CSE7761_REG_EMUCON 0x01 // Metering control register
@ -171,15 +173,18 @@ bool Cse7761ChipInit(void) {
coefficient[PowerPAC] = 0xADE1; coefficient[PowerPAC] = 0xADE1;
} }
if (HLW_PREF_PULSE == Settings.energy_power_calibration) { if (HLW_PREF_PULSE == Settings.energy_power_calibration) {
Settings.energy_voltage_calibration = 1000; // Gain 1 * 1000 // Settings.energy_frequency_calibration = 2750;
Settings.energy_frequency_calibration = 2750; Settings.energy_voltage_calibration = CSE7761_UREF;
Settings.energy_current_calibration = 160; // Gain 16 * 10 Settings.energy_current_calibration = CSE7761_IREF;
Settings.energy_power_calibration = 50000; Settings.energy_power_calibration = CSE7761_PREF;
} }
Cse7761Write(CSE7761_SPECIAL_COMMAND, CSE7761_CMD_ENABLE_WRITE); Cse7761Write(CSE7761_SPECIAL_COMMAND, CSE7761_CMD_ENABLE_WRITE);
delay(8); delay(8);
uint8_t sys_status = Cse7761Read(CSE7761_REG_SYSSTATUS); uint8_t sys_status = Cse7761Read(CSE7761_REG_SYSSTATUS);
#ifdef CSE7761_SIMULATE
sys_status = 0x11;
#endif
if (sys_status & 0x10) { // Write enable to protected registers (WREN) if (sys_status & 0x10) { // Write enable to protected registers (WREN)
/* /*
System Control Register (SYSCON) Addr:0x00 Default value: 0x0A04 System Control Register (SYSCON) Addr:0x00 Default value: 0x0A04
@ -196,11 +201,11 @@ bool Cse7761ChipInit(void) {
=001, PGA of current channel B=2 =001, PGA of current channel B=2
=000, PGA of current channel B=1 =000, PGA of current channel B=1
5-3 PGAU[2:0] Highest bit of voltage channel analog gain selection 5-3 PGAU[2:0] Highest bit of voltage channel analog gain selection
=1XX, PGA of current channel U=16 =1XX, PGA of voltage U=16
=011, PGA of current channel U=8 =011, PGA of voltage U=8
=010, PGA of current channel U=4 =010, PGA of voltage U=4
=001, PGA of current channel U=2 =001, PGA of voltage U=2
=000, PGA of current channel U=1 (Sonoff Dual R3 Pow) =000, PGA of voltage U=1 (Sonoff Dual R3 Pow)
2-0 PGAIA[2:0] Current channel A analog gain selection highest bit 2-0 PGAIA[2:0] Current channel A analog gain selection highest bit
=1XX, PGA of current channel A=16 (Sonoff Dual R3 Pow) =1XX, PGA of current channel A=16 (Sonoff Dual R3 Pow)
=011, PGA of current channel A=8 =011, PGA of current channel A=8
@ -309,19 +314,39 @@ bool Cse7761ChipInit(void) {
void Cse7761GetData(void) { void Cse7761GetData(void) {
CSE7761Data.frequency = Cse7761Read(CSE7761_REG_UFREQ); CSE7761Data.frequency = Cse7761Read(CSE7761_REG_UFREQ);
uint32_t value = Cse7761Read(CSE7761_REG_RMSU); #ifdef CSE7761_SIMULATE
CSE7761Data.frequency = 0;
#endif
// The effective value of current and voltage Rms is a 24-bit signed number, the highest bit is 0 for valid data, // The effective value of current and voltage Rms is a 24-bit signed number, the highest bit is 0 for valid data,
// and when the highest bit is 1, the reading will be processed as zero // and when the highest bit is 1, the reading will be processed as zero
CSE7761Data.voltage_rms = (value >= 0x800000) ? 0 : value;
value = Cse7761Read(CSE7761_REG_RMSIA);
CSE7761Data.current_rms[0] = (value >= 0x800000) ? 0 : value;
value = Cse7761Read(CSE7761_REG_RMSIB);
CSE7761Data.current_rms[1] = (value >= 0x800000) ? 0 : value;
// The active power parameter PowerA/B is in twos complement format, 32-bit data, the highest bit is Sign bit. // The active power parameter PowerA/B is in twos complement format, 32-bit data, the highest bit is Sign bit.
uint32_t value = Cse7761Read(CSE7761_REG_RMSU);
#ifdef CSE7761_SIMULATE
value = 2342160;
#endif
CSE7761Data.voltage_rms = (value >= 0x800000) ? 0 : value;
value = Cse7761Read(CSE7761_REG_RMSIA);
#ifdef CSE7761_SIMULATE
value = 455;
#endif
CSE7761Data.current_rms[0] = ((value >= 0x800000) || (value < 1600)) ? 0 : value; // No load threshold of 10mA
value = Cse7761Read(CSE7761_REG_POWERPA); value = Cse7761Read(CSE7761_REG_POWERPA);
CSE7761Data.active_power[0] = (value & 0x80000000) ? (~value) + 1 : value; #ifdef CSE7761_SIMULATE
value = 217;
#endif
CSE7761Data.active_power[0] = (0 == CSE7761Data.current_rms[0]) ? 0 : (value & 0x80000000) ? (~value) + 1 : value;
value = Cse7761Read(CSE7761_REG_RMSIB);
#ifdef CSE7761_SIMULATE
value = 29760;
#endif
CSE7761Data.current_rms[1] = ((value >= 0x800000) || (value < 1600)) ? 0 : value; // No load threshold of 10mA
value = Cse7761Read(CSE7761_REG_POWERPB); value = Cse7761Read(CSE7761_REG_POWERPB);
CSE7761Data.active_power[1] = (value & 0x80000000) ? (~value) + 1 : value; #ifdef CSE7761_SIMULATE
value = 2126641;
#endif
CSE7761Data.active_power[1] = (0 == CSE7761Data.current_rms[1]) ? 0 : (value & 0x80000000) ? (~value) + 1 : value;
AddLog(LOG_LEVEL_DEBUG_MORE, PSTR("C61: U%d, F%d, I%d/%d, P%d/%d"), AddLog(LOG_LEVEL_DEBUG_MORE, PSTR("C61: U%d, F%d, I%d/%d, P%d/%d"),
CSE7761Data.voltage_rms, CSE7761Data.frequency, CSE7761Data.voltage_rms, CSE7761Data.frequency,
@ -330,7 +355,7 @@ void Cse7761GetData(void) {
if (Energy.power_on) { // Powered on if (Energy.power_on) { // Powered on
Energy.voltage[0] = ((float)CSE7761Data.voltage_rms / Settings.energy_voltage_calibration); // V Energy.voltage[0] = ((float)CSE7761Data.voltage_rms / Settings.energy_voltage_calibration); // V
Energy.frequency[0] = (float)Settings.energy_frequency_calibration / ((float)CSE7761Data.frequency + 1); // Hz // Energy.frequency[0] = (float)Settings.energy_frequency_calibration / ((float)CSE7761Data.frequency + 1); // Hz
for (uint32_t channel = 0; channel < 2; channel++) { for (uint32_t channel = 0; channel < 2; channel++) {
Energy.data_valid[channel] = 0; Energy.data_valid[channel] = 0;
@ -338,7 +363,7 @@ void Cse7761GetData(void) {
if (0 == Energy.active_power[channel]) { if (0 == Energy.active_power[channel]) {
Energy.current[channel] = 0; Energy.current[channel] = 0;
} else { } else {
Energy.current[channel] = ((float)CSE7761Data.current_rms[channel] / Settings.energy_current_calibration) / 10; // mA Energy.current[channel] = (float)CSE7761Data.current_rms[channel] / Settings.energy_current_calibration; // A
CSE7761Data.energy[channel] += Energy.active_power[channel]; CSE7761Data.energy[channel] += Energy.active_power[channel];
} }
} }
@ -363,6 +388,9 @@ void Cse7761EverySecond(void) {
} }
else if (2 == CSE7761Data.init) { else if (2 == CSE7761Data.init) {
uint16_t syscon = Cse7761Read(0x00); // Default 0x0A04 uint16_t syscon = Cse7761Read(0x00); // Default 0x0A04
#ifdef CSE7761_SIMULATE
syscon = 0x0A04;
#endif
if ((0x0A04 == syscon) && Cse7761ChipInit()) { if ((0x0A04 == syscon) && Cse7761ChipInit()) {
CSE7761Data.ready = 1; CSE7761Data.ready = 1;
} }
@ -419,7 +447,19 @@ bool Cse7761Command(void) {
uint32_t channel = (2 == XdrvMailbox.index) ? 1 : 0; uint32_t channel = (2 == XdrvMailbox.index) ? 1 : 0;
uint32_t value = (uint32_t)(CharToFloat(XdrvMailbox.data) * 100); // 1.23 = 123 uint32_t value = (uint32_t)(CharToFloat(XdrvMailbox.data) * 100); // 1.23 = 123
if (CMND_POWERSET == Energy.command_code) { if (CMND_POWERCAL == Energy.command_code) {
if (1 == XdrvMailbox.payload) { XdrvMailbox.payload = CSE7761_PREF; }
// Service in xdrv_03_energy.ino
}
else if (CMND_VOLTAGECAL == Energy.command_code) {
if (1 == XdrvMailbox.payload) { XdrvMailbox.payload = CSE7761_UREF; }
// Service in xdrv_03_energy.ino
}
else if (CMND_CURRENTCAL == Energy.command_code) {
if (1 == XdrvMailbox.payload) { XdrvMailbox.payload = CSE7761_IREF; }
// Service in xdrv_03_energy.ino
}
else if (CMND_POWERSET == Energy.command_code) {
if (XdrvMailbox.data_len && CSE7761Data.active_power[channel]) { if (XdrvMailbox.data_len && CSE7761Data.active_power[channel]) {
if ((value > 100) && (value < 200000)) { // Between 1W and 2000W if ((value > 100) && (value < 200000)) { // Between 1W and 2000W
Settings.energy_power_calibration = (CSE7761Data.active_power[channel] * 100) / value; Settings.energy_power_calibration = (CSE7761Data.active_power[channel] * 100) / value;
@ -435,11 +475,12 @@ bool Cse7761Command(void) {
} }
else if (CMND_CURRENTSET == Energy.command_code) { else if (CMND_CURRENTSET == Energy.command_code) {
if (XdrvMailbox.data_len && CSE7761Data.current_rms[channel]) { if (XdrvMailbox.data_len && CSE7761Data.current_rms[channel]) {
if ((value > 2000) && (value < 1000000)) { // Between 20mA and 10A if ((value > 1000) && (value < 1000000)) { // Between 10mA and 10A
Settings.energy_current_calibration = (CSE7761Data.current_rms[channel] * 100) / value; Settings.energy_current_calibration = ((CSE7761Data.current_rms[channel] * 100) / value) * 1000;
} }
} }
} }
/*
else if (CMND_FREQUENCYSET == Energy.command_code) { else if (CMND_FREQUENCYSET == Energy.command_code) {
if (XdrvMailbox.data_len && CSE7761Data.frequency) { if (XdrvMailbox.data_len && CSE7761Data.frequency) {
if ((value > 4500) && (value < 6500)) { // Between 45Hz and 65Hz if ((value > 4500) && (value < 6500)) { // Between 45Hz and 65Hz
@ -447,6 +488,7 @@ bool Cse7761Command(void) {
} }
} }
} }
*/
else serviced = false; // Unknown command else serviced = false; // Unknown command
return serviced; return serviced;