| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800 |
- #include "hlw8110_handle.h"
- #include <fcntl.h>
- #include "unistd.h"
- #include <termios.h>
- #include <string.h>
- #include "stdio.h"
- #include "elog.h"
- union IntData
- {
- unsigned int inte;
- unsigned char byte[2];
- };
- union LongData
- {
- unsigned long int word;
- unsigned int inte[2];
- unsigned char byte[4];
- };
- static uint8_t hlw8110_checksum_write(LowConf *conf)
- {
- uint32_t a = 0x0000;
- uint8_t check_sum = 0;
- for(int i =0; i < (conf->tx_lenth-1);i++)
- a += conf->serial_tx_buf[i];
- a = ~a;
- check_sum = a & 0xff;
- return check_sum;
- }
- static uint8_t hlw8110_checksum_read(LowConf *conf)
- {
- uint32_t a = 0x0000;
- uint8_t check_sum = 0;
- for(int i =0; i < (conf->rx_lenth-1);i++)
- a += (conf->serial_rx_buf[i]);
- a = a + conf->serial_tx_buf[0] + conf->serial_tx_buf[1];
- a = ~a;
- check_sum = a & 0xff;
- return check_sum;
- }
- int hlw8110_write_reg_en(LowConf *conf)
- {
- if(!conf)
- return -1;
- conf->tx_lenth = 4;
- conf->rx_lenth = 0;
- conf->serial_tx_buf[0] = 0xA5;
- conf->serial_tx_buf[1] = 0xEA;
- conf->serial_tx_buf[2] = 0xE5;
- conf->serial_tx_buf[3] =hlw8110_checksum_write(conf);
-
- int size = write(conf->h_hlw8110_fd,conf->serial_tx_buf,conf->tx_lenth);
- _DEBUG(SET,conf->serial_tx_buf,size);
- if(size == conf->tx_lenth)
- {
- return 0;
- }
- return -1;
- }
- int hlw8110_write_reg_dis(LowConf *conf)
- {
- if(!conf)
- return -1;
-
-
- conf->tx_lenth = 4;
- conf->rx_lenth = 0;
- conf->serial_tx_buf[0] = 0xA5;
- conf->serial_tx_buf[1] = 0xEA;
- conf->serial_tx_buf[2] = 0xDC;
- conf->serial_tx_buf[3] =hlw8110_checksum_write(conf);
-
- int size = write(conf->h_hlw8110_fd,conf->serial_tx_buf,conf->tx_lenth);
- _DEBUG(SET,conf->serial_tx_buf,size);
- if(size == conf->tx_lenth)
- {
- return 0;
- }
- printf("%s: %x\n",__func__,conf->serial_tx_buf[3]);
- return -1;
- }
- int hlw8110_set_channel(LowConf *conf,uint8_t channel)
- {
- if(channel == CHANEL_A || channel == CHANEL_B)
- {
- conf->tx_lenth = 4;
- conf->rx_lenth = 0;
- conf->serial_tx_buf[0] = 0xA5;
- conf->serial_tx_buf[1] = 0xEA;
- conf->serial_tx_buf[2] = (channel == CHANEL_A) ? 0x5a : 0xa5;
- conf->serial_tx_buf[3] =hlw8110_checksum_write(conf);
- int size = write(conf->h_hlw8110_fd,conf->serial_tx_buf,conf->tx_lenth);
- _DEBUG(SET,conf->serial_tx_buf,size);
- if(size == conf->tx_lenth)
- {
- conf->current_channel = channel;
- return 0;
- }
- printf("%s: %x\n",__func__,conf->serial_tx_buf[3]);
- return -1;
- }
- printf("set channel %d error!!!",channel);
- return -1;
- }
- int hlw8110_read_reg(LowConf *conf,uint8_t ADDR_Reg,int reg_lenth)
- {
- if(conf)
- {
- conf->serial_tx_buf[0] = 0xa5;
- conf->serial_tx_buf[1] = ADDR_Reg;
- conf->tx_lenth = 2;
- conf->rx_lenth = reg_lenth+1;
- int size = write(conf->h_hlw8110_fd,conf->serial_tx_buf,conf->tx_lenth);
- _DEBUG(SET,conf->serial_tx_buf,size);
- if(size == conf->tx_lenth)
- {
- return 0;
- }
- printf("%s: %x\n",__func__,conf->serial_tx_buf[3]);
- return -1;
- }
- printf("read reg %X faile!!!",ADDR_Reg);
- return -1;
- }
- int hlw8110_write_reg(LowConf *conf,uint8_t ADDR_Reg,int reg_lenth,uint32_t _data)
- {
- if(conf)
- {
- union LongData Temp_u32_a;
- conf->serial_tx_buf[0] = 0xa5;
- conf->serial_tx_buf[1] = ADDR_Reg|0x80;
- Temp_u32_a.word = _data;
- for(int i = 0; i < reg_lenth;i++)
- {
- conf->serial_tx_buf[i+2] = Temp_u32_a.byte[reg_lenth-1-i];
- }
- conf->tx_lenth = 3 + reg_lenth;
- conf->rx_lenth = 0;
- conf->serial_tx_buf[conf->tx_lenth-1] = hlw8110_checksum_write(conf);
- int size = write(conf->h_hlw8110_fd,conf->serial_tx_buf,conf->tx_lenth);
- _DEBUG(SET,conf->serial_tx_buf,size);
- if(size == conf->tx_lenth)
- {
- return 0;
- }
- }
- return -1;
- }
- int hlw8110_reset(LowConf *conf)
- {
- if(conf)
- {
- conf->tx_lenth = 4;
- conf->rx_lenth = 0;
- conf->serial_tx_buf[0] = 0xa5;
- conf->serial_tx_buf[1] = 0xea;
- conf->serial_tx_buf[2] = 0x96;
- conf->serial_tx_buf[3] = hlw8110_checksum_write(conf);
- int size = write(conf->h_hlw8110_fd,conf->serial_tx_buf,conf->tx_lenth);
- _DEBUG(SET,conf->serial_tx_buf,size);
- if(size == conf->tx_lenth)
- {
- return 0;
- }
- }
- return -1;
- }
- uint8_t hlw8110_judge_chechsum_calfac(LowConf *conf)
- {
- uint32_t a;
- uint32_t d;
- int size = 0;
- //read RmsIAC RmsIBC RmsPAC PowerPAC PowrPBC PowerSC EnergBc EnergAc
- if(conf)
- {
- if(hlw8110_read_reg(conf,REG_RMS_IAC_ADDR,2))printf("%s: read reg filed\n",__func__);
- usleep(D_TIME_50MS);
- memset(conf->serial_rx_buf,0,10);
- size = read(conf->h_hlw8110_fd,conf->serial_rx_buf,conf->rx_lenth);
- _DEBUG(GET,conf->serial_rx_buf,size);
- if ( conf->serial_rx_buf[conf->rx_lenth-1] == hlw8110_checksum_read(conf))
- {
- conf->U16_RMSIAC_RegData = (conf->serial_rx_buf[0] << 8) + conf->serial_rx_buf[1];
- }else
- printf("read U16_RMSIAC_RegData filed\n");
-
- if(hlw8110_read_reg(conf,REG_RMS_IBC_ADDR,2))printf("%s: read reg filed\n",__func__);
- usleep(D_TIME_50MS);
- memset(conf->serial_rx_buf,0,10);
- size = read(conf->h_hlw8110_fd,conf->serial_rx_buf,conf->rx_lenth);
- _DEBUG(GET,conf->serial_rx_buf,size);
- if ( conf->serial_rx_buf[conf->rx_lenth-1] == hlw8110_checksum_read(conf))
- {
- conf->U16_RMSIBC_RegData = (conf->serial_rx_buf[0] << 8) + conf->serial_rx_buf[1];
- }else
- printf("read U16_RMSIBC_RegData filed\n");
-
- if(hlw8110_read_reg(conf,REG_RMS_UC_ADDR,2))printf("%s: read reg filed\n",__func__);
- usleep(D_TIME_50MS);
- memset(conf->serial_rx_buf,0,10);
- size = read(conf->h_hlw8110_fd,conf->serial_rx_buf,conf->rx_lenth);
- _DEBUG(GET,conf->serial_rx_buf,size);
- if ( conf->serial_rx_buf[conf->rx_lenth-1] == hlw8110_checksum_read(conf))
- {
- conf->U16_RMSUC_RegData = (conf->serial_rx_buf[0] << 8) + conf->serial_rx_buf[1];
- }else
- printf("read U16_RMSUC_RegData filed\n");
- if(hlw8110_read_reg(conf,REG_POWER_PAC_ADDR,2))printf("%s: read reg filed\n",__func__);
- usleep(D_TIME_50MS);
- memset(conf->serial_rx_buf,0,10);
- size = read(conf->h_hlw8110_fd,conf->serial_rx_buf,conf->rx_lenth);
- _DEBUG(GET,conf->serial_rx_buf,size);
- if ( conf->serial_rx_buf[conf->rx_lenth-1] == hlw8110_checksum_read(conf))
- {
- conf->U16_PowerPAC_RegData = (conf->serial_rx_buf[0] << 8) + conf->serial_rx_buf[1];
- }else
- printf("read U16_PowerPAC_RegData filed\n");
- if(hlw8110_read_reg(conf,REG_POWER_PBC_ADDR,2))printf("%s: read reg filed\n",__func__);
- usleep(D_TIME_50MS);
- memset(conf->serial_rx_buf,0,10);
- size = read(conf->h_hlw8110_fd,conf->serial_rx_buf,conf->rx_lenth);
- _DEBUG(GET,conf->serial_rx_buf,size);
- if ( conf->serial_rx_buf[conf->rx_lenth-1] == hlw8110_checksum_read(conf))
- {
- conf->U16_PowerPBC_RegData = (conf->serial_rx_buf[0] << 8) + conf->serial_rx_buf[1];
- }else
- printf("read U16_PowerPBC_RegData filed\n");
- if(hlw8110_read_reg(conf,REG_POWER_SC_ADDR,2))printf("%s: read reg filed\n",__func__);
- usleep(D_TIME_50MS);
- memset(conf->serial_rx_buf,0,10);
- size = read(conf->h_hlw8110_fd,conf->serial_rx_buf,conf->rx_lenth);
- _DEBUG(GET,conf->serial_rx_buf,size);
- if ( conf->serial_rx_buf[conf->rx_lenth-1] == hlw8110_checksum_read(conf))
- {
- conf->U16_PowerSC_RegData = (conf->serial_rx_buf[0] << 8) + conf->serial_rx_buf[1];
- }else
- printf("read U16_PowerSC_RegData filed\n");
- if(hlw8110_read_reg(conf,REG_ENERGY_AC_ADDR,2))printf("%s: read reg filed\n",__func__);
- usleep(D_TIME_50MS);
- memset(conf->serial_rx_buf,0,10);
- size = read(conf->h_hlw8110_fd,conf->serial_rx_buf,conf->rx_lenth);
- _DEBUG(GET,conf->serial_rx_buf,size);
- if ( conf->serial_rx_buf[conf->rx_lenth-1] == hlw8110_checksum_read(conf))
- {
- conf->U16_EnergyAC_RegData = (conf->serial_rx_buf[0] << 8) + conf->serial_rx_buf[1];
- }else
- printf("read U16_PowerSC_RegData filed\n");
- if(hlw8110_read_reg(conf,REG_ENERGY_BC_ADDR,2))printf("%s: read reg filed\n",__func__);
- usleep(D_TIME_50MS);
- memset(conf->serial_rx_buf,0,10);
- size = read(conf->h_hlw8110_fd,conf->serial_rx_buf,conf->rx_lenth);
- _DEBUG(GET,conf->serial_rx_buf,size);
- if ( conf->serial_rx_buf[conf->rx_lenth-1] == hlw8110_checksum_read(conf))
- {
- conf->U16_EnergyBC_RegData = (conf->serial_rx_buf[0] << 8) + conf->serial_rx_buf[1];
- }else
- printf("read U16_EnergyBC_RegData filed\n");
- if(hlw8110_read_reg(conf,REG_CHECKSUM_ADDR,2))printf("%s: read reg filed\n",__func__);
- usleep(D_TIME_50MS);
- memset(conf->serial_rx_buf,0,10);
- size = read(conf->h_hlw8110_fd,conf->serial_rx_buf,conf->rx_lenth);
- _DEBUG(GET,conf->serial_rx_buf,size);
- if ( conf->serial_rx_buf[conf->rx_lenth-1] == hlw8110_checksum_read(conf))
- {
- conf->U16_CheckSUM_RegData = (conf->serial_rx_buf[0] << 8) + conf->serial_rx_buf[1];
- }else
- printf("read U16_CheckSUM_RegData filed\n");
- a=0;
- a=~(
- 0xffff +
- conf->U16_RMSIAC_RegData +
- conf->U16_RMSIBC_RegData +
- conf->U16_RMSUC_RegData +
- conf->U16_PowerPAC_RegData +
- conf->U16_PowerPBC_RegData +
- conf->U16_PowerSC_RegData +
- conf->U16_EnergyAC_RegData +
- conf->U16_EnergyBC_RegData
- );
- conf->U16_CheckSUM_Data = a & 0xffff;
- if(conf->U16_CheckSUM_Data == conf->U16_CheckSUM_RegData)
- {
- d = 1;
- }else
- d = 0;
- return d;
- }
- return 0;
- }
- int hlw8110_write_reg_sucess(LowConf *conf)
- {
- if(conf)
- {
- hlw8110_read_reg(conf,REG_SYSCON_ADDR,2);
- usleep(D_TIME_10MS);
- memset(conf->serial_rx_buf,0,10);
- int size = read(conf->h_hlw8110_fd,conf->serial_rx_buf,conf->rx_lenth);
- _DEBUG(GET,conf->serial_rx_buf,size);
- if ( conf->serial_rx_buf[conf->rx_lenth-1] == hlw8110_checksum_read(conf))
- {
- conf->U16_Check_SysconReg_Data = (conf->serial_rx_buf[0] << 8) + conf->serial_rx_buf[1];
- }else
- printf("read U16_Check_SysconReg_Data filed\n");
- hlw8110_read_reg(conf,REG_EMUCON_ADDR,2);
- usleep(D_TIME_10MS);
- memset(conf->serial_rx_buf,0,10);
- size = read(conf->h_hlw8110_fd,conf->serial_rx_buf,conf->rx_lenth);
- _DEBUG(GET,conf->serial_rx_buf,size);
- if ( conf->serial_rx_buf[conf->rx_lenth-1] == hlw8110_checksum_read(conf))
- {
- conf->U16_Check_EmuconReg_Data = (conf->serial_rx_buf[0] << 8) + conf->serial_rx_buf[1];
- }else
- printf("read U16_Check_EmuconReg_Data filed\n");
- hlw8110_read_reg(conf,REG_EMUCON2_ADDR,2);
- usleep(D_TIME_10MS);
- memset(conf->serial_rx_buf,0,10);
- size = read(conf->h_hlw8110_fd,conf->serial_rx_buf,conf->rx_lenth);
- _DEBUG(GET,conf->serial_rx_buf,size);
- if ( conf->serial_rx_buf[conf->rx_lenth-1] == hlw8110_checksum_read(conf))
- {
- conf->U16_Check_Emucon2Reg_Data = (conf->serial_rx_buf[0] << 8) + conf->serial_rx_buf[1];
- }else
- printf("read U16_Check_Emucon2Reg_Data filed\n");
-
- return 0;
- }
- return -1;
- }
- static int uart_setup(LowConf *conf)
- {
- if(tcgetattr(conf->h_hlw8110_fd,&(conf->bak_opt)) < 0) {
- return -1;
- }
- memcpy(&(conf->new_opt),&(conf->bak_opt),sizeof(struct termios));
- // 修改控制模式,保证程序不会占用串口
- conf->new_opt.c_cflag |= CLOCAL;
- // 修改控制模式,能够从串口读取数据
- conf->new_opt.c_cflag |= CREAD;
- // 不使用流控制
- conf->new_opt.c_cflag &= ~CRTSCTS;
- // 设置数据位
- conf->new_opt.c_cflag &= ~CSIZE;
- conf->new_opt.c_cflag |= CS8;
- // 设置奇偶校验位
- conf->new_opt.c_cflag |= PARENB;
- conf->new_opt.c_cflag &= ~PARODD;
- conf->new_opt.c_iflag &= ~ICRNL;
- conf->new_opt.c_iflag |= INPCK;
- // 设置停止位
- conf->new_opt.c_cflag &= ~CSTOPB;
-
- // 设置最少字符和等待时间
- conf->new_opt.c_cc[VMIN] = 1; // 读数据的最小字节数
- conf->new_opt.c_cc[VTIME] = 0; //等待第1个数据,单位是10s
-
- // 修改输出模式,原始数据输出
- conf->new_opt.c_oflag &= ~OPOST;
- conf->new_opt.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
- conf->new_opt.c_iflag &= ~ (IXON | IXOFF | IXANY);
- // 设置波特率
- cfsetispeed(&(conf->new_opt), B9600);
- cfsetospeed(&(conf->new_opt), B9600);
- // 清空终端未完成的数据
- tcflush(conf->h_hlw8110_fd, TCIFLUSH);
- // 设置新属性
- if(tcsetattr(conf->h_hlw8110_fd, TCSANOW, &(conf->new_opt)) < 0) {
- return -1;
- }
- return 0;
- }
- float hlw8110_read_UV(LowConf *conf)
- {
- float a;
- hlw8110_read_reg(conf,REG_RMSU_ADDR,3);
- usleep(D_TIME_50MS);
- memset(conf->serial_rx_buf,0,10);
- int size = read(conf->h_hlw8110_fd,conf->serial_rx_buf,conf->rx_lenth);
- _DEBUG(GET,conf->serial_rx_buf,size);
- if ( conf->serial_rx_buf[conf->rx_lenth-1] == hlw8110_checksum_read(conf))
- {
- conf->U32_RMSU_RegData =(uint32_t)(conf->serial_rx_buf[0]<<16) +
- (uint32_t)(conf->serial_rx_buf[1]<<8) +
- (uint32_t)(conf->serial_rx_buf[2]);
- }else {
- log_e("read U32_RMSU_RegData filed\n");
- }
-
- if ((conf->U32_RMSU_RegData &0x800000) == 0x800000)
- {
- a = 0;
- }else
- {
- a = (float)conf->U32_RMSU_RegData;
- a = a*(conf->U16_RMSUC_RegData);
- a = a/0x400000;
- a = a/0.47;
- a = a/100;
- a = a*D_CAL_U;
- conf->F_AC_V = a;
- }
- return conf->F_AC_V;
-
- }
- float hlw8110_read_IA(LowConf *conf)
- {
- float a;
- hlw8110_read_reg(conf,REG_RMSIA_ADDR,3);
- usleep(D_TIME_50MS);
- memset(conf->serial_rx_buf,0,10);
- int size = read(conf->h_hlw8110_fd,conf->serial_rx_buf,conf->rx_lenth);
- _DEBUG(GET,conf->serial_rx_buf,size);
- if ( conf->serial_rx_buf[conf->rx_lenth-1] == hlw8110_checksum_read(conf))
- {
- conf->U32_RMSIA_RegData =(uint32_t)(conf->serial_rx_buf[0]<<16) +
- (uint32_t)(conf->serial_rx_buf[1]<<8) +
- (uint32_t)(conf->serial_rx_buf[2]);
- }else {
- log_e("read U32_RMSIA_RegData filed\n");
- }
- if ((conf->U32_RMSIA_RegData &0x800000) == 0x800000)
- {
- a = 0;
- }else
- {
- a = (float)conf->U32_RMSIA_RegData;
- a = a*(conf->U16_RMSIAC_RegData);
- a = a/0x800000;
- a = a/1;
- a = a /500;
- a = a*D_CAL_U;
- conf->F_AC_I = a;
- }
- return conf->F_AC_I;
- }
- float hwl8110_read_PA(LowConf *conf)
- {
- float a;
- float b;
- hlw8110_read_reg(conf,REG_POWER_PA_ADDR,4);
- usleep(D_TIME_50MS);
- memset(conf->serial_rx_buf,0,10);
- int size = read(conf->h_hlw8110_fd,conf->serial_rx_buf,conf->rx_lenth);
- _DEBUG(GET,conf->serial_rx_buf,size);
- if ( conf->serial_rx_buf[conf->rx_lenth-1] == hlw8110_checksum_read(conf))
- {
- conf->U32_POWERPA_RegData =(uint32_t)(conf->serial_rx_buf[0]<<24) +
- (uint32_t)(conf->serial_rx_buf[1]<<16) +
- (uint32_t)(conf->serial_rx_buf[2]<<8) +
- (uint32_t)(conf->serial_rx_buf[3]);
- }else {
- log_e("read U32_POWERPA_RegData filed\n");
- }
- if (conf->U32_POWERPA_RegData > 0x40000000)
- {
- b = ~(conf->U32_POWERPA_RegData);
- a = (float)b;
- }else
- {
- a = (float)conf->U32_POWERPA_RegData;
- }
- a = a*(conf->U16_PowerPAC_RegData);
- a = a/0x40000000;
- a = a/0.47;
- a = a/1.0;
- a = a*D_CAL_U;
- conf->F_AC_P = a;
- return a;
- }
- float hwl8110_read_EA(LowConf *conf)
- {
- float a;
- hlw8110_read_reg(conf,REG_ENERGY_PA_ADDR,3);
- usleep(D_TIME_50MS);
- int size = read(conf->h_hlw8110_fd,conf->serial_rx_buf,conf->rx_lenth);
- _DEBUG(GET,conf->serial_rx_buf,size);
- if ( conf->serial_rx_buf[conf->rx_lenth-1] == hlw8110_checksum_read(conf))
- {
- conf->U32_ENERGY_PA_RegData =(uint32_t)(conf->serial_rx_buf[0]<<16) +
- (uint32_t)(conf->serial_rx_buf[1]<<8) +
- (uint32_t)(conf->serial_rx_buf[2]);
- }else {
- log_e("read U32_RMSIA_RegData filed\n");
- }
- hlw8110_read_reg(conf,REG_HFCONST_ADDR,2);
- usleep(D_TIME_50MS);
- size = read(conf->h_hlw8110_fd,conf->serial_rx_buf,conf->rx_lenth);
- _DEBUG(GET,conf->serial_rx_buf,size);
- if ( conf->serial_rx_buf[conf->rx_lenth-1] == hlw8110_checksum_read(conf))
- {
- conf->U16_HFConst_RegData = (uint32_t)(conf->serial_rx_buf[0]<<8) +
- (uint32_t)(conf->serial_rx_buf[1]);
- }else {
- log_e("read U16_HFConst_RegData filed\n");
- }
- a = (float)conf->U32_ENERGY_PA_RegData;
- a = a*(conf->U16_EnergyAC_RegData);
- a = a/0x20000000;
- a = a/0.47;
- a = a/1;
- a = a * D_CAL_A_E;
- conf->F_AC_E = a;
- conf->F_AC_BACKUP_E = conf->F_AC_E;
- return conf->F_AC_E;
- }
- float hwl8110_read_LineFreq(LowConf *conf)
- {
- float a = 0.0;
- uint16_t b = 0;
- hlw8110_read_reg(conf,REG_UFREQ_ADDR,2);
- usleep(D_TIME_50MS);
- int size = read(conf->h_hlw8110_fd,conf->serial_rx_buf,conf->rx_lenth);
- _DEBUG(GET,conf->serial_rx_buf,size);
- if ( conf->serial_rx_buf[conf->rx_lenth-1] == hlw8110_checksum_read(conf))
- {
- b = (uint16_t)(conf->serial_rx_buf[0]<<8) +
- (uint16_t)(conf->serial_rx_buf[1]);
- }else {
- log_e("read U16_HFConst_RegData filed\n");
- }
-
- //char *buf = (char *)&(b);
- //printf("b[0]=%x b[1]=%x\n",buf[0],buf[1]);
- a = (float)b;
- if (a!=0)
- {
- a = 3579545/(8*a);
- }
- conf->F_AC_LINE_Freq = a;
- return conf->F_AC_LINE_Freq;
- }
- float hwl8110_read_PF(LowConf *conf)
- {
- float a;
- uint32_t b;
- hlw8110_read_reg(conf,REG_PF_ADDR,3);
- usleep(D_TIME_50MS);
- int size = read(conf->h_hlw8110_fd,conf->serial_rx_buf,conf->rx_lenth);
- _DEBUG(GET,conf->serial_rx_buf,size);
- if ( conf->serial_rx_buf[conf->rx_lenth-1] == hlw8110_checksum_read(conf))
- {
- b = (uint32_t)(conf->serial_rx_buf[0]<<16) +
- (uint32_t)(conf->serial_rx_buf[1]<< 8) +
- (uint32_t)(conf->serial_rx_buf[2]);
- }else {
- log_e("read U16_HFConst_RegData filed\n");
- }
- if (b>0x800000)
- {
- a = (float)(0xffffff-b + 1)/0x7fffff;
- }
- else
- {
- a = (float)b/0x7fffff;
- }
- if (conf->F_AC_P < 0.3)
- a = 0;
-
- conf->F_AC_P = a;
- return a;
- }
- float hlw8110_read_Angle(LowConf *conf)
- {
- float a;
- uint16_t b= 0;
- hlw8110_read_reg(conf,REG_ANGLE_ADDR,2);
- usleep(D_TIME_50MS);
- int size = read(conf->h_hlw8110_fd,conf->serial_rx_buf,conf->rx_lenth);
- _DEBUG(GET,conf->serial_rx_buf,size);
- if ( conf->serial_rx_buf[conf->rx_lenth-1] == hlw8110_checksum_read(conf))
- {
- b = (uint32_t)(conf->serial_rx_buf[0]<<8) +
- (uint32_t)(conf->serial_rx_buf[1]);
- }else {
- log_e("read REG_ANGLE_ADDR filed\n");
- }
- if ( conf->F_AC_PF < 55)
- {
- a = b;
- a = a * 0.0805;
- conf->F_Angle = a;
- }
- else
- {
-
- a = b;
- a = a * 0.0965;
- conf->F_Angle = a;
- }
- if (conf->F_AC_P < 0.5)
- {
- conf->F_Angle = 0;
- }
- if (conf->F_Angle < 90)
- {
- a = conf->F_Angle;
- }
- else if (conf->F_Angle < 180)
- {
- a = 180-conf->F_Angle;
- }
- else if (conf->F_Angle < 360)
- {
- a = 360 - conf->F_Angle;
- }
- else
- {
- a = conf->F_Angle -360;
- }
- return a;
- }
- int hlw8110_read_Status(LowConf* conf,int* nStatus)
- {
- char value_path[64];
- char buf[3]; // 用于存储读取到的GPIO值,通常为'0'或'1'
- ssize_t bytesRead;
- int fd;
- // 构造GPIO值的路径
- snprintf(value_path, sizeof(value_path), GPIO_VALUE_PATH, GPIO_PE3);
-
- // 打开GPIO值文件以读取
- fd = open(value_path, O_RDONLY);
- if (fd == -1) {
- perror("Failed to open GPIO value for reading");
- return 1;
- }
-
- // 读取GPIO的值
- bytesRead = read(fd, buf, sizeof(buf) - 1);
- if (bytesRead == -1) {
- perror("Failed to read from GPIO value");
- close(fd);
- return 1;
- }
-
- // 确保字符串以null终止
- buf[bytesRead] = '\0';
-
- // 打印读取到的GPIO值
- // printf("GPIO %d value: %s\n", GPIO_PE3, buf);
- if (buf[0]=='0')
- {
- *nStatus=1;
- }else{
- *nStatus=0;
- }
-
- // 关闭文件描述符
- close(fd);
-
- return 0;
- }
- LowConf * hlw8110_init(void)
- {
- // int ret;
-
- static LowConf conf;
- memset(&conf,0,sizeof(LowConf));
- conf.h_hlw8110_fd = open("/dev/ttyS4", O_RDWR | O_NOCTTY | O_NDELAY);
- if(conf.h_hlw8110_fd < 0)
- return NULL;
- else
- fcntl(conf.h_hlw8110_fd, F_SETFL, O_NONBLOCK);
- uart_setup(&conf);
- hlw8110_write_reg_en(&conf);
- usleep(D_TIME_10MS);
-
- hlw8110_set_channel(&conf,CHANEL_A);
- usleep(D_TIME_10MS);
- hlw8110_write_reg(&conf,REG_SYSCON_ADDR,2,0x0a04);
- usleep(D_TIME_10MS);
- hlw8110_write_reg(&conf,REG_EMUCON_ADDR,2,0x0001);
- usleep(D_TIME_10MS);
- //hlw8110_write_reg(&conf,REG_EMUCON_ADDR,2,0x0018);
- hlw8110_write_reg(&conf,REG_EMUCON2_ADDR,2,0x0065);
- usleep(D_TIME_10MS);
- hlw8110_write_reg_dis(&conf);
- usleep(D_TIME_10MS);
- hlw8110_judge_chechsum_calfac(&conf);
- usleep(D_TIME_10MS);
- hlw8110_write_reg_sucess(&conf);
- conf.opreation = 1;
- return &conf;
- }
- void hlw8110_deinit(LowConf *conf)
- {
- tcsetattr(conf->h_hlw8110_fd, TCSANOW, &(conf->bak_opt));
- close(conf->h_hlw8110_fd);
- memset(conf,0,sizeof(LowConf));
- }
-
- LowConf* hlw8110_Reset(LowConf *conf)
- {
- hlw8110_reset(conf);
- usleep(10000);
- hlw8110_deinit(conf);
- usleep(10000);
- conf = hlw8110_init();
- return conf;
- }
|