Files
kunlun/app/smart_grid/sta/iot_sg_sta_zc.c

102 lines
3.2 KiB
C
Raw Normal View History

2024-09-28 14:24:04 +08:00
/****************************************************************************
Copyright(c) 2019 by Aerospace C.Power (Chongqing) Microelectronics. ALL RIGHTS RESERVED.
This Information is proprietary to Aerospace C.Power (Chongqing) Microelectronics and MAY NOT
be copied by any method or incorporated into another program without
the express written consent of Aerospace C.Power. This Information or any portion
thereof remains the property of Aerospace C.Power. The Information contained herein
is believed to be accurate and Aerospace C.Power assumes no responsibility or
liability for its use in any way and conveys no license or title under
any patent or copyright and makes no representation or warranty that this
Information is free from patent or copyright infringement.
****************************************************************************/
#include "iot_sg.h"
#include "iot_sg_sta.h"
#include "iot_io_api.h"
#include "iot_gpio_api.h"
#include "iot_board_api.h"
#include "iot_sg_sta_zc.h"
static uint8_t iot_sg_sta_get_zc_phase(uint8_t index)
{
const uint8_t phase_buf[] = {GPIO_PA_ZC, GPIO_PB_ZC, GPIO_PC_ZC};
if (index >= sizeof(phase_buf)) {
return GPIO_NO_VALID;
}
return phase_buf[index];
}
static void iot_sg_sta_zc_gpio_handler(int arg)
{
uint32_t cur_counter;
uint8_t index = (uint8_t)arg, gpio;
iot_sg_sta_zc_check_t *check = &p_sg_glb->desc.sta->check;
if (arg >= IOT_PLC_PHASE_CNT) {
IOT_ASSERT(0);
goto err;
}
gpio = iot_board_get_gpio(iot_sg_sta_get_zc_phase(index));
cur_counter = iot_gpio_get_trigger_ticks(gpio);
if (!(check->started_zc_bm & (1 << index))) {
check->started_zc_bm |= 1 << index;
goto out;
}
check->total_time[index] +=
(uint32_t)(cur_counter - check->pre_tick[index]);
check->trig_cnt[index]++;
out:
check->pre_tick[index] = cur_counter;
err:
return;
}
void iot_sg_sta_zc_start()
{
uint8_t gpio = 0, index, reason = 0;
os_mem_set(&p_sg_glb->desc.sta->check, 0,
sizeof(p_sg_glb->desc.sta->check));
for (index = 0; index < IOT_PLC_PHASE_CNT; index++) {
gpio = iot_board_get_gpio(iot_sg_sta_get_zc_phase(index));
if (gpio == GPIO_NO_VALID) {
reason = 1;
goto next;
}
iot_gpio_close(gpio);
if (iot_gpio_open_as_interrupt(gpio)) {
reason = 2;
goto next;
}
if (iot_gpio_interrupt_config(gpio, GPIO_INT_EDGE_RAISING,
iot_sg_sta_zc_gpio_handler, index, GPIO_INT_FUNC_GET_TICKS)) {
reason = 3;
goto next;
}
if (iot_gpio_interrupt_enable(gpio, 1)) {
reason = 4;
goto next;
}
next:
if (reason) {
iot_printf("%s index %d, reason %d\n",
__FUNCTION__, index, reason);
}
reason = 0;
}
}
void iot_sg_sta_zc_stop()
{
uint8_t gpio = 0, index;
for (index = 0; index < IOT_PLC_PHASE_CNT; index++) {
gpio = iot_board_get_gpio(iot_sg_sta_get_zc_phase(index));
if (gpio == GPIO_NO_VALID) {
}
iot_gpio_interrupt_enable(gpio, 0);
}
}