2024-10-16 23:51:49 -07:00
|
|
|
/*
|
|
|
|
* port_pwr.c
|
|
|
|
*
|
|
|
|
* Created Oct 16, 2024
|
|
|
|
*
|
|
|
|
* power control for GAT and USB ports.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <ch32v20x.h>
|
2024-11-06 19:15:40 -08:00
|
|
|
|
|
|
|
#include "periph/port_pwr.h"
|
2024-10-16 23:51:49 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define PORT_PWR_STATE_DR BKP_DR3
|
|
|
|
|
|
|
|
#define GAT_ON_FLAG (1 << 0)
|
|
|
|
#define USB2_ON_FLAG (1 << 1)
|
|
|
|
|
2024-10-22 23:06:18 -07:00
|
|
|
#define INIT_FLAG (0xaf << 8)
|
2024-10-16 23:51:49 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
2024-10-22 23:06:18 -07:00
|
|
|
static uint16_t port_pwr_state;
|
2024-10-16 23:51:49 -07:00
|
|
|
static uint8_t gat_oc_state_latch = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-10-23 04:19:13 -07:00
|
|
|
void port_pwr_state_commit_bkp()
|
2024-10-22 23:06:18 -07:00
|
|
|
{
|
|
|
|
BKP_WriteBackupRegister(PORT_PWR_STATE_DR, port_pwr_state);
|
|
|
|
RTC_WaitForLastTask();
|
|
|
|
}
|
|
|
|
|
2024-10-16 23:51:49 -07:00
|
|
|
void gat_on()
|
|
|
|
{
|
|
|
|
if (gat_oc_state_latch) return;
|
|
|
|
|
|
|
|
GAT_EN_PORT->BSHR = GAT_EN_PIN;
|
|
|
|
|
|
|
|
port_pwr_state |= GAT_ON_FLAG;
|
2024-10-23 04:19:13 -07:00
|
|
|
port_pwr_state_commit_bkp();
|
2024-10-16 23:51:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void gat_off()
|
|
|
|
{
|
|
|
|
GAT_EN_PORT->BCR = GAT_EN_PIN;
|
|
|
|
|
|
|
|
port_pwr_state &= ~GAT_ON_FLAG;
|
2024-10-23 04:19:13 -07:00
|
|
|
port_pwr_state_commit_bkp();
|
2024-10-16 23:51:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t gat_pwr_state()
|
|
|
|
{
|
|
|
|
return (GAT_EN_PORT->OUTDR & GAT_EN_PIN) ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t gat_oc_state()
|
|
|
|
{
|
|
|
|
if (!(GAT_OC_PORT->INDR & GAT_OC_PIN)) {
|
|
|
|
gat_oc_state_latch = 1;
|
|
|
|
gat_off();
|
|
|
|
}
|
|
|
|
|
|
|
|
return gat_oc_state_latch;
|
|
|
|
}
|
|
|
|
|
|
|
|
void gat_toggle()
|
|
|
|
{
|
|
|
|
if (gat_pwr_state()) gat_off();
|
|
|
|
else gat_on();
|
|
|
|
}
|
|
|
|
|
2024-10-22 23:06:18 -07:00
|
|
|
|
2024-10-16 23:51:49 -07:00
|
|
|
void usb2_on()
|
|
|
|
{
|
2024-10-22 23:06:18 -07:00
|
|
|
USB2_EN_PORT->BSHR = USB2_EN_PIN;
|
|
|
|
|
2024-10-16 23:51:49 -07:00
|
|
|
port_pwr_state |= USB2_ON_FLAG;
|
2024-10-23 04:19:13 -07:00
|
|
|
port_pwr_state_commit_bkp();
|
2024-10-16 23:51:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void usb2_off()
|
|
|
|
{
|
2024-10-22 23:06:18 -07:00
|
|
|
USB2_EN_PORT->BCR = USB2_EN_PIN;
|
|
|
|
|
2024-10-16 23:51:49 -07:00
|
|
|
port_pwr_state &= ~USB2_ON_FLAG;
|
2024-10-23 04:19:13 -07:00
|
|
|
port_pwr_state_commit_bkp();
|
2024-10-16 23:51:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t usb2_pwr_state()
|
|
|
|
{
|
|
|
|
return (USB2_EN_PORT->OUTDR & USB2_EN_PIN) ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void usb2_toggle()
|
|
|
|
{
|
2024-10-22 23:06:18 -07:00
|
|
|
if (usb2_pwr_state()) usb2_off();
|
|
|
|
else usb2_on();
|
2024-10-16 23:51:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void port_pwr_init()
|
|
|
|
{
|
|
|
|
port_pwr_state = BKP_ReadBackupRegister(PORT_PWR_STATE_DR);
|
2024-10-22 23:06:18 -07:00
|
|
|
RTC_WaitForLastTask();
|
2024-10-16 23:51:49 -07:00
|
|
|
|
|
|
|
if ((port_pwr_state & INIT_FLAG) != INIT_FLAG) {
|
|
|
|
// no battery retention.
|
|
|
|
// automatically turn on all outputs
|
2024-10-22 23:06:18 -07:00
|
|
|
port_pwr_state = INIT_FLAG | USB2_ON_FLAG | GAT_ON_FLAG;
|
|
|
|
|
|
|
|
// and commit state flags
|
2024-10-23 04:19:13 -07:00
|
|
|
port_pwr_state_commit_bkp();
|
2024-10-16 23:51:49 -07:00
|
|
|
}
|
2024-10-22 23:06:18 -07:00
|
|
|
|
|
|
|
if (port_pwr_state & GAT_ON_FLAG) gat_on();
|
|
|
|
if (port_pwr_state & USB2_ON_FLAG) usb2_on();
|
2024-10-16 23:51:49 -07:00
|
|
|
}
|