|
|
@@ -8,8 +8,10 @@
|
|
|
#include "thread.h"
|
|
|
#include "datadef.h"
|
|
|
|
|
|
-#define BRD_NUM 3 //BRD_MAX
|
|
|
-#define BRD_TIMEOUT 100
|
|
|
+
|
|
|
+#define BRD_NUM 3 //BRD_MAX
|
|
|
+#define BRD_TIMEOUT 200
|
|
|
+#define RETRY_TIMES 3
|
|
|
|
|
|
#define LIMIT_HOF(x) (x*1.1f)
|
|
|
#define LIMIT_LOF(x) (x*0.9f)
|
|
|
@@ -30,9 +32,25 @@ typedef struct {
|
|
|
power_total_t ttl;
|
|
|
|
|
|
product_data_t *prod;
|
|
|
+ power_data_t pdat;
|
|
|
}power_handle_t;
|
|
|
power_handle_t pwrHandle={0};
|
|
|
|
|
|
+
|
|
|
+
|
|
|
+static int alarm_evt_handle(power_handle_t *h, power_ch_t *pch, uint32_t flag)
|
|
|
+{
|
|
|
+ alarm_data_t ad;
|
|
|
+
|
|
|
+ ad.ch = pch->info.ch;
|
|
|
+ ad.alarm = pch->alarm;
|
|
|
+ ad.time = pch->time;
|
|
|
+ web_post(PKT_TYPE_ALARM, &ad, sizeof(ad));
|
|
|
+
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
static void memswap(uint8_t *buf, int len)
|
|
|
{
|
|
|
int i;
|
|
|
@@ -45,11 +63,21 @@ static void memswap(uint8_t *buf, int len)
|
|
|
}
|
|
|
static int read_reg(power_handle_t *h, uint8_t addr, uint16_t reg, uint16_t *data, int cnt)
|
|
|
{
|
|
|
- return mb_read(h->mb, addr, reg, data, cnt, BRD_TIMEOUT);
|
|
|
+ int i,r;
|
|
|
+ for(i=0; i<RETRY_TIMES; i++) {
|
|
|
+ r = mb_read(h->mb, addr, reg, data, cnt, BRD_TIMEOUT);
|
|
|
+ if(r==0) break;
|
|
|
+ }
|
|
|
+ return r;
|
|
|
}
|
|
|
static int write_reg(power_handle_t *h, uint8_t addr, uint16_t reg, uint16_t *data, int cnt)
|
|
|
{
|
|
|
- return mb_write(h->mb, addr, reg, data, cnt);
|
|
|
+ int i,r;
|
|
|
+ for(i=0; i<RETRY_TIMES; i++) {
|
|
|
+ r = mb_write(h->mb, addr, reg, data, cnt);
|
|
|
+ if(r==0) break;
|
|
|
+ }
|
|
|
+ return r;
|
|
|
}
|
|
|
////////////////////////////////////////////////////////////////////
|
|
|
static int get_key(power_handle_t *h, uint8_t addr, board_key_t *key)
|
|
|
@@ -57,17 +85,20 @@ static int get_key(power_handle_t *h, uint8_t addr, board_key_t *key)
|
|
|
int i,r=-1;
|
|
|
uint16_t tmp[2];
|
|
|
|
|
|
- r = read_reg(h, addr, POWER_DC_INFO, tmp, 2);
|
|
|
- if(r==0) {
|
|
|
- key->type = (tmp[0]>>8)&0xFF;
|
|
|
- key->chs = tmp[0]&0xFF;
|
|
|
- return 0;
|
|
|
+ if(h->prod->pwr_type==PDU_AC_I1O1 || h->prod->pwr_type==PDU_AC_I3O1_H) {
|
|
|
+ r = read_reg(h, addr, POWER_AC_GET_INFO, tmp, 1);
|
|
|
+ if(r==0) {
|
|
|
+ key->type = (tmp[0]>>8)&0xFF;
|
|
|
+ key->chs = tmp[0]&0xFF;
|
|
|
+ }
|
|
|
}
|
|
|
-
|
|
|
- r = read_reg(h, addr, POWER_AC_GET_INFO, tmp, 2);
|
|
|
- if(r==0) {
|
|
|
- key->type = (tmp[0]>>8)&0xFF;
|
|
|
- key->chs = tmp[0]&0xFF;
|
|
|
+ else {
|
|
|
+ r = read_reg(h, addr, POWER_DC_INFO, tmp, 2);
|
|
|
+ if(r==0) {
|
|
|
+ key->type = (tmp[0]>>8)&0xFF;
|
|
|
+ key->chs = tmp[0]&0xFF;
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
return r;
|
|
|
@@ -215,7 +246,7 @@ static int threshold_proc(power_handle_t *h, board_data_t *pbrd)
|
|
|
|
|
|
//设置为输出三相且三相有缺失则报警
|
|
|
if(pch->info.ph_val && pbrd->ph_loss) {
|
|
|
- //trigger alarm event here ...
|
|
|
+ alarm_evt_handle(h, pch, THRESHOLD_V_UPPER);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -224,13 +255,13 @@ static int threshold_proc(power_handle_t *h, board_data_t *pbrd)
|
|
|
if(pch->power[j].voltage>pch->thr.v_upper.val) {
|
|
|
if(pch->alarm.v_upper==0) {
|
|
|
pch->alarm.v_upper = 1;
|
|
|
- //trigger alarm event here ...
|
|
|
+ alarm_evt_handle(h, pch, THRESHOLD_V_UPPER);
|
|
|
}
|
|
|
}
|
|
|
else if(pch->power[j].voltage<pch->thr.v_lower.val) {
|
|
|
if(pch->alarm.v_lower==0) {
|
|
|
pch->alarm.v_lower = 1;
|
|
|
- //trigger alarm event here ...
|
|
|
+ alarm_evt_handle(h, pch, THRESHOLD_V_LOWER);
|
|
|
}
|
|
|
}
|
|
|
else {
|
|
|
@@ -243,7 +274,7 @@ static int threshold_proc(power_handle_t *h, board_data_t *pbrd)
|
|
|
if(pch->power[j].current>pch->thr.c_upper.val) {
|
|
|
if(pch->alarm.c_upper==0) {
|
|
|
pch->alarm.c_upper = 1;
|
|
|
- //trigger alarm event here ...
|
|
|
+ alarm_evt_handle(h, pch, THRESHOLD_C_UPPER);
|
|
|
}
|
|
|
}
|
|
|
else {
|
|
|
@@ -255,7 +286,7 @@ static int threshold_proc(power_handle_t *h, board_data_t *pbrd)
|
|
|
if(pch->power[j].power>pch->thr.p_upper.val) {
|
|
|
if(pch->alarm.p_upper==0) {
|
|
|
pch->alarm.p_upper = 1;
|
|
|
- //trigger alarm event here ...
|
|
|
+ alarm_evt_handle(h, pch, THRESHOLD_P_UPPER);
|
|
|
}
|
|
|
}
|
|
|
else {
|
|
|
@@ -267,7 +298,7 @@ static int threshold_proc(power_handle_t *h, board_data_t *pbrd)
|
|
|
if(pch->power[j].current>pch->thr.w_upper.val) {
|
|
|
if(pch->alarm.w_upper==0) {
|
|
|
pch->alarm.w_upper = 1;
|
|
|
- //trigger alarm event here ...
|
|
|
+ alarm_evt_handle(h, pch, THRESHOLD_W_UPPER);
|
|
|
}
|
|
|
}
|
|
|
else {
|
|
|
@@ -286,7 +317,7 @@ static int total_proc(power_handle_t *h)
|
|
|
power_ch_t *pch=NULL;
|
|
|
board_data_t *pbrd=NULL;
|
|
|
|
|
|
- lock_d_hold(h->lck);
|
|
|
+ lock_on(h->lck);
|
|
|
if(h->prod->pwr_type==PDU_AC_I3O3) times = 3;
|
|
|
for(i=0; i<h->brd_max; i++) {
|
|
|
if(h->pbrd[i]) {
|
|
|
@@ -317,7 +348,7 @@ static int total_proc(power_handle_t *h)
|
|
|
|
|
|
h->ttl.type = h->prod->pwr_type;
|
|
|
memcpy(h->ttl.total, tmp, sizeof(tmp));
|
|
|
- lock_d_release(h->lck);
|
|
|
+ lock_off(h->lck);
|
|
|
|
|
|
return 0;
|
|
|
}
|
|
|
@@ -328,16 +359,18 @@ static int board_read(power_handle_t *h, board_data_t *pbrd)
|
|
|
uint16_t offset,tmp[144];
|
|
|
power_ch_t *pch=NULL;
|
|
|
|
|
|
- lock_d_hold(h->lck);
|
|
|
+ lock_on(h->lck);
|
|
|
if(pbrd) {
|
|
|
switch(pbrd->type) {
|
|
|
case AC_SINGLE_S_TYPE:
|
|
|
case AC_SINGLE_B_TYPE:
|
|
|
{
|
|
|
uint32_t val;
|
|
|
- r = read_reg(h, pbrd->addr, POWER_AC_CUR_INFO_L, tmp, pbrd->chs);
|
|
|
+
|
|
|
+ offset = POWER_AC_CUR_INFO_L;
|
|
|
+ r = read_reg(h, pbrd->addr, offset, tmp, pbrd->chs*12);
|
|
|
if (r<0) {
|
|
|
- LOGE("get_board, addr:%d offset:%d r=%d\n", pbrd->addr, offset, r);
|
|
|
+ LOGE("read_reg failed, addr:%d reg:0x%04x/%d cnt:%d\n", pbrd->addr, offset, offset, pbrd->chs*12);
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
@@ -363,10 +396,11 @@ static int board_read(power_handle_t *h, board_data_t *pbrd)
|
|
|
val = (tmp[11 + idx] << 16) | tmp[10 + idx];
|
|
|
pwr->factor = val / 1000.0;
|
|
|
}
|
|
|
-
|
|
|
- r = read_reg(h, pbrd->addr, POWER_AC_STAT_INFO_L, tmp, pbrd->chs);
|
|
|
+
|
|
|
+ offset = POWER_AC_STAT_INFO_L;
|
|
|
+ r = read_reg(h, pbrd->addr, offset, tmp, pbrd->chs);
|
|
|
if (r<0) {
|
|
|
- LOGE("get_board, addr:%d offset:%d r=%d\n", pbrd->addr, offset, r);
|
|
|
+ LOGE("read_reg failed, addr:%d reg:0x%04x/%d cnt:%d\n", pbrd->addr, offset, offset, pbrd->chs);
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
@@ -386,11 +420,12 @@ static int board_read(power_handle_t *h, board_data_t *pbrd)
|
|
|
case DCPDU_TYPE:
|
|
|
{
|
|
|
uint32_t flag;
|
|
|
- offset = POWER_DC_OUT_INFO + 16;
|
|
|
uint16_t *ptmp = tmp + 32;
|
|
|
+
|
|
|
+ offset = POWER_DC_OUT_INFO + 16;
|
|
|
r = read_reg(h, pbrd->addr, offset, ptmp, 32);
|
|
|
if (r<0) {
|
|
|
- LOGE("get_board, addr:%d offset:%d r=%d\n", pbrd->addr, offset, r);
|
|
|
+ LOGE("read_reg failed, addr:%d reg:0x%04x/%d cnt:%d\n", pbrd->addr, offset, offset, 32);
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
@@ -417,7 +452,7 @@ static int board_read(power_handle_t *h, board_data_t *pbrd)
|
|
|
offset = POWER_DC_STAT_INFO;
|
|
|
r = read_reg(h, pbrd->addr, offset, tmp, 2);
|
|
|
if (r<0) {
|
|
|
- LOGE("get_board, addr:%d offset:%d r=%d\n", pbrd->addr, offset, r);
|
|
|
+ LOGE("read_reg failed, addr:%d reg:0x%04x/%d cnt:%d\n", pbrd->addr, offset, offset, 2);
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
@@ -430,7 +465,7 @@ static int board_read(power_handle_t *h, board_data_t *pbrd)
|
|
|
offset = POWER_DC_WARNING;
|
|
|
r = read_reg(h, pbrd->addr, offset, tmp, 16);
|
|
|
if (r<0) {
|
|
|
- LOGE("get_board, addr:%d offset:%d r=%d\n", pbrd->addr, offset, r);
|
|
|
+ LOGE("read_reg failed, addr:%d reg:0x%04x/%d cnt:%d\n", pbrd->addr, offset, offset, 16);
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
@@ -452,7 +487,7 @@ static int board_read(power_handle_t *h, board_data_t *pbrd)
|
|
|
offset = POWER_AC3_OUT_INFO;
|
|
|
r = read_reg(h, pbrd->addr, offset, tmp, 80);
|
|
|
if (r < 0) {
|
|
|
- LOGE("get_board, addr:%d offset:%d r=%d\n", pbrd->addr, offset, r);
|
|
|
+ LOGE("read_reg failed, addr:%d reg:0x%04x/%d cnt:%d\n", pbrd->addr, offset, offset, 80);
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
@@ -460,7 +495,7 @@ static int board_read(power_handle_t *h, board_data_t *pbrd)
|
|
|
uint16_t* ptmp=tmp+80;
|
|
|
r = read_reg(h, pbrd->addr, offset, ptmp, 64);
|
|
|
if (r < 0) {
|
|
|
- LOGE("get_board, addr:%d offset:%d r=%d\n", pbrd->addr, offset, r);
|
|
|
+ LOGE("read_reg failed, addr:%d reg:0x%04x/%d cnt:%d\n", pbrd->addr, offset, offset, 64);
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
@@ -503,7 +538,7 @@ static int board_read(power_handle_t *h, board_data_t *pbrd)
|
|
|
offset = POWER_AC3_OUT_ENABLE;
|
|
|
r = read_reg(h, pbrd->addr, offset, tmp, 20);
|
|
|
if (r < 0) {
|
|
|
- LOGE("get_board, addr:%d offset:%d r=%d\n", pbrd->addr, offset, r);
|
|
|
+ LOGE("read_reg failed, addr:%d reg:0x%04x/%d cnt:%d\n", pbrd->addr, offset, offset, 20);
|
|
|
break;
|
|
|
}
|
|
|
for (i=0; i<pbrd->chs; i++) {
|
|
|
@@ -516,7 +551,7 @@ static int board_read(power_handle_t *h, board_data_t *pbrd)
|
|
|
offset = POWER_AC3_OUT_ERROR;
|
|
|
r = read_reg(h, pbrd->addr, offset, tmp, 18);
|
|
|
if (r < 0) {
|
|
|
- LOGE("get_board, addr:%d offset:%d r=%d\n", pbrd->addr, offset, r);
|
|
|
+ LOGE("read_reg failed, addr:%d reg:0x%04x/%d cnt:%d\n", pbrd->addr, offset, offset, 18);
|
|
|
break;
|
|
|
}
|
|
|
for (i=0; i<pbrd->chs; i++) {
|
|
|
@@ -532,7 +567,7 @@ static int board_read(power_handle_t *h, board_data_t *pbrd)
|
|
|
offset = POWER_AC3_ALARM_MISSING_PH;
|
|
|
r = read_reg(h, pbrd->addr, offset, tmp, pbrd->chs);
|
|
|
if (r < 0) {
|
|
|
- LOGE("get_board, addr:%d offset:%d r=%d\n", pbrd->addr, offset, r);
|
|
|
+ LOGE("read_reg failed, addr:%d reg:0x%04x/%d cnt:%d\n", pbrd->addr, offset, offset, pbrd->chs);
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
@@ -559,10 +594,33 @@ static int board_read(power_handle_t *h, board_data_t *pbrd)
|
|
|
threshold_proc(h, pbrd);
|
|
|
}
|
|
|
}
|
|
|
- lock_d_release(h->lck);
|
|
|
+ lock_off(h->lck);
|
|
|
|
|
|
return r;
|
|
|
}
|
|
|
+static int power_copy(power_handle_t *h)
|
|
|
+{
|
|
|
+ int i,r=-1;
|
|
|
+ power_data_t *pd=&h->pdat;
|
|
|
+
|
|
|
+ if(h->chs>0) {
|
|
|
+ if(!pd->pch || pd->chs!=h->chs) {
|
|
|
+ if(pd->pch) free(pd->pch);
|
|
|
+ pd->chs = 0;
|
|
|
+ pd->pch = malloc(sizeof(power_ch_t)*h->chs);
|
|
|
+ }
|
|
|
+
|
|
|
+ if(pd->pch) {
|
|
|
+ pd->chs = h->chs;
|
|
|
+ for(i=0; i<pd->chs; i++) {
|
|
|
+ pd->pch[i] = *h->pch[i];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ pd->ttl = h->ttl;
|
|
|
+
|
|
|
+ return 0;
|
|
|
+}
|
|
|
static int board_query(power_handle_t *h)
|
|
|
{
|
|
|
int i,r;
|
|
|
@@ -571,22 +629,24 @@ static int board_query(power_handle_t *h)
|
|
|
r = board_read(h, h->pbrd[i]);
|
|
|
}
|
|
|
total_proc(h);
|
|
|
+ power_copy(h);
|
|
|
|
|
|
return r;
|
|
|
}
|
|
|
-static void *power_thread(void *arg)
|
|
|
+static void power_thread(void *arg)
|
|
|
{
|
|
|
int r;
|
|
|
board_data_t *pbrd=NULL;
|
|
|
thread_handle_t *th=(thread_handle_t*)arg;
|
|
|
- power_handle_t *h=(power_handle_t*)th->arg;;
|
|
|
+ power_handle_t *h=(power_handle_t*)th->attr->arg;;
|
|
|
|
|
|
- power_scan();
|
|
|
while(th->quit==0) {
|
|
|
+ LOGD("___ board_query 111\n");
|
|
|
board_query(h);
|
|
|
+ LOGD("___ board_query 222\n");
|
|
|
sleep(1);
|
|
|
+ LOGD("___ board_query 333\n");
|
|
|
}
|
|
|
- pthread_exit(NULL);
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -609,7 +669,7 @@ int power_init(void)
|
|
|
};
|
|
|
|
|
|
memset(h, 0, sizeof(power_handle_t));
|
|
|
- h->lck = lock_d_init();
|
|
|
+ h->lck = lock_init();
|
|
|
h->mb = mb_init(¶);
|
|
|
if(!h->mb) {
|
|
|
return -1;
|
|
|
@@ -617,6 +677,9 @@ int power_init(void)
|
|
|
h->cur_addr = 0;
|
|
|
h->brd_max = BRD_NUM;
|
|
|
h->prod = ¶s_get()->prod;
|
|
|
+
|
|
|
+ power_scan();
|
|
|
+ power_set_all_sw(1);
|
|
|
thread_start(THREAD_ID_POWER, power_thread, h);
|
|
|
|
|
|
return 0;
|
|
|
@@ -627,40 +690,44 @@ int power_deinit(void)
|
|
|
{
|
|
|
power_handle_t *h=&pwrHandle;
|
|
|
|
|
|
- lock_d_deinit(h->lck);
|
|
|
+ lock_deinit(h->lck);
|
|
|
mb_deinit(h->mb);
|
|
|
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-int power_get_ch(int ch, power_ch_t *pch)
|
|
|
+static int get_ch(power_handle_t *h, uint8_t ch, power_ch_t *pch)
|
|
|
{
|
|
|
- power_handle_t *h=&pwrHandle;
|
|
|
-
|
|
|
- lock_d_hold(h->lck);
|
|
|
if(!pch || !h->pch || !h->chs || !h->pch[ch]) {
|
|
|
- lock_d_release(h->lck);
|
|
|
return -1;
|
|
|
}
|
|
|
*pch = *h->pch[ch];
|
|
|
- lock_d_release(h->lck);
|
|
|
-
|
|
|
return 0;
|
|
|
}
|
|
|
+int power_get_ch(uint8_t ch, power_ch_t *pch)
|
|
|
+{
|
|
|
+ int r;
|
|
|
+ power_handle_t *h=&pwrHandle;
|
|
|
+
|
|
|
+ lock_on(h->lck);
|
|
|
+ r = get_ch(h, ch, pch);
|
|
|
+ lock_off(h->lck);
|
|
|
+
|
|
|
+ return r;
|
|
|
+}
|
|
|
|
|
|
|
|
|
int power_get_board(board_data_t *pb)
|
|
|
{
|
|
|
power_handle_t *h=&pwrHandle;
|
|
|
|
|
|
- lock_d_hold(h->lck);
|
|
|
+ lock_on(h->lck);
|
|
|
if(!pb || !h->pch || !h->cnt || !h->pbrd[pb->addr]) {
|
|
|
- lock_d_release(h->lck);
|
|
|
+ lock_off(h->lck);
|
|
|
return -1;
|
|
|
}
|
|
|
*pb = *h->pbrd[pb->addr];
|
|
|
- lock_d_release(h->lck);
|
|
|
+ lock_off(h->lck);
|
|
|
|
|
|
return 0;
|
|
|
}
|
|
|
@@ -670,18 +737,18 @@ int power_set(int ch, power_ch_t *pch)
|
|
|
{
|
|
|
power_handle_t *h=&pwrHandle;
|
|
|
|
|
|
- lock_d_hold(h->lck);
|
|
|
+ lock_on(h->lck);
|
|
|
if(!pch || !h->pch || !h->chs || !h->pch[pch->info.ch]) {
|
|
|
- lock_d_release(h->lck);
|
|
|
+ lock_off(h->lck);
|
|
|
return -1;
|
|
|
}
|
|
|
*h->pch[pch->info.ch] = *pch;
|
|
|
- lock_d_release(h->lck);
|
|
|
+ lock_off(h->lck);
|
|
|
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
-static int pch_map(power_handle_t *h, int chs)
|
|
|
+static int power_map(power_handle_t *h, int chs)
|
|
|
{
|
|
|
int i,j,r,idx=0;
|
|
|
board_data_t *pbrd=NULL;
|
|
|
@@ -706,22 +773,46 @@ static int pch_map(power_handle_t *h, int chs)
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
+static int power_clear(power_handle_t *h)
|
|
|
+{
|
|
|
+ int i,j;
|
|
|
+ for(i=0; i<=h->brd_max; i++) {
|
|
|
+ if(h->pbrd[i]) {
|
|
|
+ for(j=0; j<h->pbrd[i]->chs; j++) {
|
|
|
+ if(h->pbrd[i]->pch) {
|
|
|
+ free(h->pbrd[i]->pch);
|
|
|
+ h->pbrd[i]->pch = NULL;
|
|
|
+ }
|
|
|
+ h->pbrd[i]->chs = 0;
|
|
|
+ }
|
|
|
+ free(h->pbrd[i]);
|
|
|
+ h->pbrd[i] = NULL;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ memset(h->key, 0, sizeof(h->key));
|
|
|
+ h->cnt = 0;
|
|
|
+ h->cur_addr = 0;
|
|
|
+
|
|
|
+ return 0;
|
|
|
+}
|
|
|
int power_scan(void)
|
|
|
{
|
|
|
- int r,i,j,chs=0;
|
|
|
+ int r,i,j,total_chs=0;
|
|
|
int ch_idx=0,brd_idx=0;
|
|
|
- power_handle_t *h=&pwrHandle;
|
|
|
+ power_ch_t *pch=NULL;
|
|
|
board_key_t *pkey=NULL;
|
|
|
+ board_data_t *pbrd=NULL;
|
|
|
+ power_handle_t *h=&pwrHandle;
|
|
|
uint16_t times,nGroups=h->prod->ch_delay;
|
|
|
|
|
|
- power_clear();
|
|
|
+ lock_on(h->lck);
|
|
|
+ power_clear(h);
|
|
|
|
|
|
- lock_d_hold(h->lck);
|
|
|
- h->cur_addr = 0;
|
|
|
for(i=1; i<=h->brd_max; i++) {
|
|
|
r = get_key(h, i, &h->key[i]);
|
|
|
if(r==0) {
|
|
|
LOGD("___ power_scan addr %d ok, type: %d, chs: %d\n", i, h->key[i].type, h->key[i].chs);
|
|
|
+ h->cnt++;
|
|
|
}
|
|
|
else {
|
|
|
LOGE("___ power_scan addr %d failed\n", i);
|
|
|
@@ -731,73 +822,55 @@ int power_scan(void)
|
|
|
for(i=1; i<h->brd_max; i++) {
|
|
|
pkey = &h->key[i];
|
|
|
if(pkey->chs>0) {
|
|
|
- h->pbrd[i] = (board_data_t*)calloc(1, sizeof(board_data_t));
|
|
|
- if(h->pbrd[i]) {
|
|
|
- h->pbrd[i]->type = pkey->type;
|
|
|
- h->pbrd[i]->chs = pkey->chs;
|
|
|
- h->pbrd[i]->addr = i;
|
|
|
- h->pbrd[i]->ch0 = ch_idx;
|
|
|
- h->pbrd[i]->pch = (power_ch_t*)calloc(1, sizeof(power_ch_t)*pkey->chs);
|
|
|
- if(h->pbrd[i]->pch) {
|
|
|
- times = (ch_idx+1)%nGroups?(ch_idx+1):nGroups;
|
|
|
- for(j=0; j<pkey->chs; j++) {
|
|
|
- h->pbrd[i]->pch[j].info.addr = i;
|
|
|
- h->pbrd[i]->pch[j].info.sch = j; //序号从0开始
|
|
|
- h->pbrd[i]->pch[j].info.type = pkey->type;
|
|
|
- h->pbrd[i]->pch[j].info.start_delay = 1000*times;
|
|
|
- h->pbrd[i]->pch[j].info.stop_delay = 1000*times;
|
|
|
-
|
|
|
- if(h->prod->pwr_type==PDU_AC_I3O3) {
|
|
|
- h->pbrd[i]->pch[j].info.ch = ch_idx+j/3; //序号程序从0开始
|
|
|
- h->pbrd[i]->pch[j].info.ph_id = j%3;
|
|
|
- }
|
|
|
- else {
|
|
|
- h->pbrd[i]->pch[j].info.ch = ch_idx+j; //序号程序从0开始
|
|
|
- h->pbrd[i]->pch[j].info.ph_id = 0;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if(h->prod->pwr_type==PDU_AC_I3O3) {
|
|
|
- ch_idx += pkey->chs/3;
|
|
|
- }
|
|
|
- else {
|
|
|
- ch_idx += pkey->chs;
|
|
|
- }
|
|
|
- }
|
|
|
- brd_idx++;
|
|
|
+ pbrd = (board_data_t*)calloc(1, sizeof(board_data_t));
|
|
|
+ if(!pbrd) {
|
|
|
+ LOGE("___ power_scan, calloc pbrd %d failed\n", i);
|
|
|
+ return -1;
|
|
|
}
|
|
|
- chs += pkey->chs;
|
|
|
- }
|
|
|
- }
|
|
|
- pch_map(h, chs);
|
|
|
- lock_d_release(h->lck);
|
|
|
-
|
|
|
- return 0;
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-int power_clear(void)
|
|
|
-{
|
|
|
- int i,j;
|
|
|
- power_handle_t *h=&pwrHandle;
|
|
|
-
|
|
|
- lock_d_hold(h->lck);
|
|
|
- for(i=0; i<=h->brd_max; i++) {
|
|
|
- if(h->pbrd[i]) {
|
|
|
- for(j=0; j<h->pbrd[i]->chs; j++) {
|
|
|
- if(h->pbrd[i]->pch) {
|
|
|
- free(h->pbrd[i]->pch);
|
|
|
- h->pbrd[i]->pch = NULL;
|
|
|
+
|
|
|
+ pbrd->type = pkey->type;
|
|
|
+ pbrd->chs = pkey->chs;
|
|
|
+ pbrd->addr = i;
|
|
|
+ pbrd->ch0 = ch_idx;
|
|
|
+ pch = (power_ch_t*)calloc(1, sizeof(power_ch_t)*pkey->chs);
|
|
|
+ if(!pch) {
|
|
|
+ LOGE("___ power_scan, calloc pch failed\n");
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ times = (ch_idx+1)%nGroups?(ch_idx+1):nGroups;
|
|
|
+ for(j=0; j<pkey->chs; j++) {
|
|
|
+ pch[j].info.addr = i;
|
|
|
+ pch[j].info.sch = j; //序号从0开始
|
|
|
+ pch[j].info.type = pkey->type;
|
|
|
+ pch[j].info.start_delay = 1000*times;
|
|
|
+ pch[j].info.stop_delay = 1000*times;
|
|
|
+
|
|
|
+ if(h->prod->pwr_type==PDU_AC_I3O3) {
|
|
|
+ pch[j].info.ch = ch_idx+j/3; //序号程序从0开始
|
|
|
+ pch[j].info.ph_id = j%3;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ pch[j].info.ch = ch_idx+j; //序号程序从0开始
|
|
|
+ pch[j].info.ph_id = 0;
|
|
|
}
|
|
|
- h->pbrd[i]->chs = 0;
|
|
|
}
|
|
|
- free(h->pbrd[i]);
|
|
|
- h->pbrd[i] = NULL;
|
|
|
+
|
|
|
+ if(h->prod->pwr_type==PDU_AC_I3O3) {
|
|
|
+ ch_idx += pkey->chs/3;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ ch_idx += pkey->chs;
|
|
|
+ }
|
|
|
+ brd_idx++;
|
|
|
+
|
|
|
+ pbrd->pch = pch;
|
|
|
+ h->pbrd[i] = pbrd;
|
|
|
+ total_chs += pkey->chs;
|
|
|
}
|
|
|
}
|
|
|
- memset(h->key, 0, sizeof(h->key));
|
|
|
- h->cnt = 0;
|
|
|
- lock_d_release(h->lck);
|
|
|
+ power_map(h, total_chs);
|
|
|
+ lock_off(h->lck);
|
|
|
|
|
|
return 0;
|
|
|
}
|
|
|
@@ -810,7 +883,7 @@ int power_reset(void)
|
|
|
power_handle_t *h=&pwrHandle;
|
|
|
board_data_t *pbrd=NULL;
|
|
|
|
|
|
- lock_d_hold(h->lck);
|
|
|
+ lock_on(h->lck);
|
|
|
for(i=0; i<=h->brd_max; i++) {
|
|
|
pbrd = h->pbrd[i];
|
|
|
if(pbrd) {
|
|
|
@@ -885,18 +958,26 @@ int power_reset(void)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- lock_d_release(h->lck);
|
|
|
+ lock_off(h->lck);
|
|
|
|
|
|
return r;
|
|
|
}
|
|
|
|
|
|
|
|
|
-int power_set_sw(power_ch_t *pch)
|
|
|
+int power_set_ch_sw(uint8_t ch, uint8_t on)
|
|
|
{
|
|
|
int r;
|
|
|
- power_ch_t pc;
|
|
|
+ power_ch_t pc,*pch;
|
|
|
power_handle_t *h=&pwrHandle;
|
|
|
- uint16_t offset,tmp[2]={0},st=pch->power[0].status;
|
|
|
+ uint16_t offset,tmp[2]={0},st=on;
|
|
|
+
|
|
|
+ lock_on(h->lck);
|
|
|
+ r = get_ch(h, ch, &pc);
|
|
|
+ if(r) {
|
|
|
+ lock_off(h->lck);
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ pch = &pc;
|
|
|
|
|
|
if (pch->thr.v_upper.en == 1)
|
|
|
st |= ENABLE_AC3_V_UP;
|
|
|
@@ -909,13 +990,12 @@ int power_set_sw(power_ch_t *pch)
|
|
|
if (pch->thr.w_upper.en == 1)
|
|
|
st |= ENABLE_AC3_W_UP;
|
|
|
|
|
|
- lock_d_hold(h->lck);
|
|
|
switch(pch->info.type) {
|
|
|
case AC_SINGLE_S_TYPE:
|
|
|
case AC_SINGLE_B_TYPE:
|
|
|
{
|
|
|
offset = POWER_AC_CH_STAT_L + pch->info.ch;
|
|
|
- tmp[0] = pch->power[0].status; tmp[1] = 0;
|
|
|
+ tmp[0] = st; tmp[1] = 0;
|
|
|
r = write_reg(h, pc.info.addr, offset, tmp, 2);
|
|
|
}
|
|
|
break;
|
|
|
@@ -949,11 +1029,49 @@ int power_set_sw(power_ch_t *pch)
|
|
|
}
|
|
|
break;
|
|
|
}
|
|
|
- lock_d_release(h->lck);
|
|
|
+ lock_off(h->lck);
|
|
|
|
|
|
return r;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+static int set_board_sw(board_data_t *pbrd, uint8_t on)
|
|
|
+{
|
|
|
+ int i,r;
|
|
|
+ power_ch_t pc;
|
|
|
+ power_handle_t *h=&pwrHandle;
|
|
|
+
|
|
|
+ if(!pbrd) {
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ for(i=0; i<pbrd->chs; i++) {
|
|
|
+ r = get_ch(h, i, &pc);
|
|
|
+ if(r==0) {
|
|
|
+ power_set_ch_sw(pc.info.ch, on);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+int power_set_all_sw(uint8_t on)
|
|
|
+{
|
|
|
+ int i,r;
|
|
|
+
|
|
|
+ power_handle_t *h=&pwrHandle;
|
|
|
+
|
|
|
+ for(i=0; i<h->brd_max; i++) {
|
|
|
+ set_board_sw(h->pbrd[i], on);
|
|
|
+ }
|
|
|
+
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
int power_set_alarm(power_ch_t *pch)
|
|
|
{
|
|
|
int r=0;
|
|
|
@@ -961,7 +1079,7 @@ int power_set_alarm(power_ch_t *pch)
|
|
|
uint16_t nStatus = 0;
|
|
|
power_handle_t *h=&pwrHandle;
|
|
|
|
|
|
- lock_d_hold(h->lck);
|
|
|
+ lock_on(h->lck);
|
|
|
switch(pch->info.type) {
|
|
|
case AC_SINGLE_S_TYPE:
|
|
|
case AC_SINGLE_B_TYPE:
|
|
|
@@ -1009,7 +1127,7 @@ int power_set_alarm(power_ch_t *pch)
|
|
|
if(pch->thr.w_upper.act==1) nStatus |= BIT(4);
|
|
|
r = write_reg(h, pch->info.addr, offset, &nStatus, 1);
|
|
|
}
|
|
|
- lock_d_release(h->lck);
|
|
|
+ lock_off(h->lck);
|
|
|
|
|
|
return r;
|
|
|
}
|
|
|
@@ -1021,7 +1139,7 @@ int power_set_threshold(power_ch_t *pch)
|
|
|
uint16_t offset;
|
|
|
power_handle_t *h=&pwrHandle;
|
|
|
|
|
|
- lock_d_hold(h->lck);
|
|
|
+ lock_on(h->lck);
|
|
|
switch(pch->info.type) {
|
|
|
case AC_SINGLE_S_TYPE:
|
|
|
case AC_SINGLE_B_TYPE:
|
|
|
@@ -1186,7 +1304,7 @@ int power_set_threshold(power_ch_t *pch)
|
|
|
r = -1;
|
|
|
break;
|
|
|
}
|
|
|
- lock_d_release(h->lck);
|
|
|
+ lock_off(h->lck);
|
|
|
|
|
|
return r;
|
|
|
}
|
|
|
@@ -1198,7 +1316,7 @@ int power_set_start_delay(power_ch_t *pch)
|
|
|
uint16_t tmp[2],reg,offset;
|
|
|
power_handle_t *h=&pwrHandle;
|
|
|
|
|
|
- lock_d_hold(h->lck);
|
|
|
+ lock_on(h->lck);
|
|
|
switch(pch->info.type) {
|
|
|
case AC_SINGLE_S_TYPE:
|
|
|
case AC_SINGLE_B_TYPE:
|
|
|
@@ -1247,7 +1365,7 @@ int power_set_start_delay(power_ch_t *pch)
|
|
|
default:
|
|
|
r = -1;
|
|
|
}
|
|
|
- lock_d_release(h->lck);
|
|
|
+ lock_off(h->lck);
|
|
|
|
|
|
return r;
|
|
|
}
|
|
|
@@ -1257,7 +1375,7 @@ int power_set_stop_delay(power_ch_t *pch)
|
|
|
uint16_t tmp[2],reg,offset;
|
|
|
power_handle_t *h=&pwrHandle;
|
|
|
|
|
|
- lock_d_hold(h->lck);
|
|
|
+ lock_off(h->lck);
|
|
|
switch(pch->info.type) {
|
|
|
case AC_SINGLE_S_TYPE:
|
|
|
case AC_SINGLE_B_TYPE:
|
|
|
@@ -1305,44 +1423,17 @@ int power_set_stop_delay(power_ch_t *pch)
|
|
|
r = -1;
|
|
|
break;
|
|
|
}
|
|
|
- lock_d_release(h->lck);
|
|
|
+ lock_on(h->lck);
|
|
|
|
|
|
return r;
|
|
|
}
|
|
|
|
|
|
|
|
|
-int power_data_copy(power_data_t *pd, int flag)
|
|
|
+power_data_t power_data_get(void)
|
|
|
{
|
|
|
- int i,r=-1;
|
|
|
power_handle_t *h=&pwrHandle;
|
|
|
|
|
|
- lock_d_hold(h->lck);
|
|
|
- if(flag&1) {
|
|
|
- if(h->chs>0) {
|
|
|
- if(!pd->pch || pd->chs!=h->chs) {
|
|
|
- if(pd->pch) free(pd->pch);
|
|
|
- pd->chs = 0;
|
|
|
- pd->pch = malloc(sizeof(power_ch_t)*h->chs);
|
|
|
- }
|
|
|
-
|
|
|
- if(pd->pch) {
|
|
|
- pd->chs = h->chs;
|
|
|
- for(i=0; i<pd->chs; i++) {
|
|
|
- pd->pch[i] = *h->pch[i];
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if(flag&2) {
|
|
|
- pd->ttl = h->ttl;
|
|
|
- }
|
|
|
- lock_d_release(h->lck);
|
|
|
-
|
|
|
- return 0;
|
|
|
+ return h->pdat;
|
|
|
}
|
|
|
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-
|