This commit is contained in:
huxi
2025-12-03 11:12:34 +08:00
parent c23ae4f24c
commit bc195654bf
8163 changed files with 3799544 additions and 92 deletions
@@ -0,0 +1,87 @@
#ifndef _GSENSOR_MANAGE_H
#define _GSENSOR_MANAGE_H
#include "printf.h"
#include "cpu.h"
#include "timer.h"
#include "app_config.h"
#include "event.h"
#include "system/includes.h"
#include "iic_api.h"
#define ACCEL_OF_GRAVITY 10
#define ACCEL_DATA_GAIN 10
enum {
GSENSOR_DISABLE = 0,
GSENSOR_RESET_INT,
GSENSOR_RESUME_INT,
GSENSOR_INT_DET,
READ_GSENSOR_DATA,
GET_ACCEL_DATA,
SEARCH_SENSOR,
};
typedef struct {
short x;
short y;
short z;
} axis_info_t;
typedef struct {
u8 logo[20];
u8(*gravity_sensor_init)(void);
char (*gravity_sensor_check)(void);
void (*gravity_sensor_ctl)(u8 cmd, void *arg);
} G_SENSOR_INTERFACE;
struct gsensor_platform_data {
u8 iic;
char gSensor_name[20];
u32 gSensor_int_io;
};
typedef struct {
u8 iic_hdl;
u8 iic_delay; //这个延时并非影响iic的时钟频率,而是2Byte数据之间的延时
u8 ctl_flag;
int init_flag;
int check_cnt;
int check_timer_hdl;
} G_SENSOR_INFO;
int gravity_sensor_init(void *_data);
int gsensor_enable(void);
int gsensor_disable(void);
u8 gravity_sensor_command(u8 w_chip_id, u8 register_address, u8 function_command);
u8 _gravity_sensor_get_ndata(u8 r_chip_id, u8 register_address, u8 *buf, u8 data_len);
int get_gSensor_data(short *buf);
int gSensor_read_data(u8 *buf, u8 buflen);
int read_gsensor_buf(short *buf);
int read_gsensor_nbuf(short *buf, short datalen);
extern G_SENSOR_INTERFACE gsensor_dev_begin[];
extern G_SENSOR_INTERFACE gsensor_dev_end[];
#define REGISTER_GRAVITY_SENSOR(gSensor) \
static G_SENSOR_INTERFACE gSensor SEC_USED(.gsensor_dev)
#define list_for_each_gsensor(c) \
for (c=gsensor_dev_begin; c<gsensor_dev_end; c++)
#define GSENSOR_PLATFORM_DATA_BEGIN(data) \
static const struct gsensor_platform_data data = {
#define GSENSOR_PLATFORM_DATA_END() \
};
/*
enum {
GSENSOR_EVENT_CLICK = 0,
GSENSOR_EVENT_DOUBLE_CLICK,
GSENSOR_EVENT_THREE_CLICK,
};
#define DEVICE_EVENT_GSENSOR (('G' << 24) | ('S' << 16) | ('R' << 8) | '\0')
*/
#endif
@@ -0,0 +1,203 @@
#ifdef SUPPORT_MS_EXTENSIONS
#pragma bss_seg(".ir_manage.data.bss")
#pragma data_seg(".ir_manage.data")
#pragma const_seg(".ir_manage.text.const")
#pragma code_seg(".ir_manage.text")
#endif
#include "app_config.h"
#include "ir_sensor/ir_manage.h"
#if(TCFG_IRSENSOR_ENABLE == 1)
#define LOG_TAG "[IRSENSOR]"
#define LOG_ERROR_ENABLE
#define LOG_DEBUG_ENABLE
#define LOG_INFO_ENABLE
/* #define LOG_DUMP_ENABLE */
#define LOG_CLI_ENABLE
#include "debug.h"
#if TCFG_SENOR_USER_IIC_TYPE
#define iic_init(iic) hw_iic_init(iic)
#define iic_deinit(iic) hw_iic_deinit(iic)
#define iic_start(iic) hw_iic_start(iic)
#define iic_stop(iic) hw_iic_stop(iic)
#define iic_tx_byte(iic, byte) hw_iic_tx_byte(iic, byte)
#define iic_rx_byte(iic, ack, err) hw_iic_rx_byte(iic, ack, err)
#define iic_read_buf(iic, buf, len) hw_iic_read_buf(iic, buf, len)
#define iic_write_buf(iic, buf, len) hw_iic_write_buf(iic, buf, len)
#define iic_suspend(iic) hw_iic_suspend(iic)
#define iic_resume(iic) hw_iic_resume(iic)
#else
#define iic_init(iic) soft_iic_init(iic)
#define iic_deinit(iic) soft_iic_deinit(iic)
#define iic_start(iic) soft_iic_start(iic)
#define iic_stop(iic) soft_iic_stop(iic)
#define iic_tx_byte(iic, byte) soft_iic_tx_byte(iic, byte)
#define iic_rx_byte(iic, ack, err) soft_iic_rx_byte(iic, ack, err)
#define iic_read_buf(iic, buf, len) soft_iic_read_buf(iic, buf, len)
#define iic_write_buf(iic, buf, len) soft_iic_write_buf(iic, buf, len)
#define iic_suspend(iic) soft_iic_suspend(iic)
#define iic_resume(iic) soft_iic_resume(iic)
#endif
static const struct irSensor_platform_data *platform_data;
IR_SENSOR_INTERFACE *irSensor_hdl = NULL;
IR_SENSOR_INFO __irSensor_info = {.iic_delay = 30, .ir_event = IR_SENSOR_EVENT_NULL};
#define irSensor_info (&__irSensor_info)
#define JSA_IIC_DELAY 50
u8 irSensor_write(u8 w_chip_id, u8 register_address, u8 function_command)
{
//spin_lock(&iic_lock);
u8 ret = 1;
iic_start(irSensor_info->iic_hdl);
if (0 == iic_tx_byte(irSensor_info->iic_hdl, w_chip_id)) {
ret = 0;
log_e("\n JSA iic wr err 0\n");
goto __gcend;
}
delay_nops(JSA_IIC_DELAY);
if (0 == iic_tx_byte(irSensor_info->iic_hdl, register_address)) {
ret = 0;
log_e("\n JSA iic wr err 1\n");
goto __gcend;
}
delay_nops(JSA_IIC_DELAY);
if (0 == iic_tx_byte(irSensor_info->iic_hdl, function_command)) {
ret = 0;
log_e("\n JSA iic wr err 2\n");
goto __gcend;
}
g_printf("JSA iic wr succ\n");
__gcend:
iic_stop(irSensor_info->iic_hdl);
//spin_unlock(&iic_lock);
return ret;
}
u8 irSensor_read(u8 r_chip_id, u8 register_address, u8 *buf, u8 data_len)
{
u8 read_len = 0;
//spin_lock(&iic_lock);
iic_start(irSensor_info->iic_hdl);
if (0 == iic_tx_byte(irSensor_info->iic_hdl, r_chip_id - 1)) {
log_e("\n JSA iic rd err 0\n");
read_len = 0;
goto __gdend;
}
delay_nops(JSA_IIC_DELAY);
if (0 == iic_tx_byte(irSensor_info->iic_hdl, register_address)) {
log_e("\n JSA iic rd err 1\n");
read_len = 0;
goto __gdend;
}
iic_start(irSensor_info->iic_hdl);
if (0 == iic_tx_byte(irSensor_info->iic_hdl, r_chip_id)) {
log_e("\n JSA iic rd err 2\n");
read_len = 0;
goto __gdend;
}
delay_nops(JSA_IIC_DELAY);
for (; data_len > 1; data_len--) {
*buf++ = iic_rx_byte(irSensor_info->iic_hdl, 1, NULL);
read_len ++;
}
*buf = iic_rx_byte(irSensor_info->iic_hdl, 0, NULL);
read_len ++;
__gdend:
iic_stop(irSensor_info->iic_hdl);
delay_nops(JSA_IIC_DELAY);
//spin_unlock(&iic_lock);
return read_len;
}
void irSensor_int_io_detect(void *priv)
{
struct sys_event e;
if (gpio_read(irSensor_info->int_io) == 0) {
irSensor_info->ir_event = irSensor_hdl->ir_sensor_check();
if (irSensor_info->ir_event != IR_SENSOR_EVENT_NULL) {
log_info("irSensor event trigger:%d", irSensor_info->ir_event);
e.type = SYS_IR_EVENT;
e.u.ir.event = irSensor_info->ir_event;
sys_event_notify(&e);
//Interface: initial state after reboot is near
}
}
}
enum irSensor_event get_irSensor_event(void)
{
return irSensor_info->ir_event;
}
int ir_sensor_init(void *_data)
{
int retval = 0;
platform_data = (const struct irSensor_platform_data *)_data;
irSensor_info->iic_hdl = platform_data->iic;
irSensor_info->int_io = platform_data->irSensor_int_io;
retval = iic_init(irSensor_info->iic_hdl);
if (retval < 0) {
log_e("\n open iic for gsensor err\n");
return retval;
} else {
log_info("\n iic open succ\n");
}
retval = -EINVAL;
list_for_each_irSensor(irSensor_hdl) {
if (!memcmp(irSensor_hdl->logo, platform_data->irSensor_name, strlen(platform_data->irSensor_name))) {
retval = 0;
break;
}
}
if (retval < 0) {
log_e(">>>irSensor_hdl logo err\n");
return retval;
} else {
}
g_printf("irSensor_init_io:%d", platform_data->irSensor_int_io);
gpio_set_pull_up(platform_data->irSensor_int_io, 0);
gpio_set_pull_down(platform_data->irSensor_int_io, 0);
gpio_set_direction(platform_data->irSensor_int_io, 1);
gpio_set_die(platform_data->irSensor_int_io, 1);
if (irSensor_hdl->ir_sensor_init(platform_data->pxs_low_th, platform_data->pxs_high_th)) {
log_info(">>>>irSensor_Int SUCC\n");
sys_s_hi_timer_add(NULL, irSensor_int_io_detect, 10);
} else {
log_e(">>>>irSensor_Int ERROR\n");
}
return 0;
}
#endif
@@ -0,0 +1,72 @@
#ifndef _IR_SENSOR_MANAGE_H
#define _IR_SENSOR_MANAGE_H
#include "printf.h"
#include "cpu.h"
#include "timer.h"
#include "app_config.h"
#include "event.h"
#include "system/includes.h"
#include "iic_api.h"
enum {
IR_SENSOR_RESET_INT = 0,
IR_SENSOR_RESUME_INT,
IR_SENSOR_INT_DET,
};
typedef struct {
u8 logo[20];
u8(*ir_sensor_init)(u16 low_th, u16 high_th);
char (*ir_sensor_check)(void);
void (*ir_sensor_ctl)(u8 cmd, void *arg);
} IR_SENSOR_INTERFACE;
struct irSensor_platform_data {
u8 iic;
char irSensor_name[20];
int irSensor_int_io;
u16 pxs_low_th;
u16 pxs_high_th;
};
enum irSensor_event {
IR_SENSOR_EVENT_NULL = 0,
IR_SENSOR_EVENT_FAR,
IR_SENSOR_EVENT_NEAR,
};
typedef struct {
u8 iic_hdl;
u8 iic_delay; //这个延时并非影响iic的时钟频率,而是2Byte数据之间的延时
int int_io;
int check_timer_hdl;
enum irSensor_event ir_event;
} IR_SENSOR_INFO;
int ir_sensor_init(void *_data);
u8 ir_sensor_command(u8 w_chip_id, u8 register_address, u8 function_command);
extern IR_SENSOR_INTERFACE irSensor_dev_begin[];
extern IR_SENSOR_INTERFACE irSensor_dev_end[];
#define REGISTER_IR_SENSOR(gSensor) \
static IR_SENSOR_INTERFACE irSensor SEC_USED(.irSensor_dev)
#define list_for_each_irSensor(c) \
for (c=irSensor_dev_begin; c<irSensor_dev_end; c++)
#define IR_SENSOR_PLATFORM_DATA_BEGIN(data) \
static const struct irSensor_platform_data data = {
#define IR_GSENSOR_PLATFORM_DATA_END() \
};
int ir_sensor_init(void *_data);
u8 irSensor_write(u8 w_chip_id, u8 register_address, u8 function_command);
u8 irSensor_read(u8 r_chip_id, u8 register_address, u8 *buf, u8 data_len);
enum irSensor_event get_irSensor_event(void);
#endif
@@ -0,0 +1,82 @@
#ifdef SUPPORT_MS_EXTENSIONS
#pragma bss_seg(".jsa1221.data.bss")
#pragma data_seg(".jsa1221.data")
#pragma const_seg(".jsa1221.text.const")
#pragma code_seg(".jsa1221.text")
#endif
#include "app_config.h"
#if(TCFG_JSA1221_ENABLE == 1)
#include "cpu/includes.h"
#include "irSensor/jsa1221.h"
#include "asm/iic_soft.h"
#include "asm/iic_hw.h"
#include "system/timer.h"
#include "system/includes.h"
#include "ir_manage.h"
static u32 jsa_high_th = 0; //红外穿戴中断的触发阈值
static u32 jsa_low_th = 0;
u8 jsa1221_pxs_int_flag(void)
{
u8 int_flag = 0;
irSensor_read(JSA_1221_READ_ADDR, JSA_INT_FLAG, &int_flag, 1);
if (int_flag & BIT(1)) {
return 1;
} else {
return 0;
}
}
u16 jsa1221_read_pxs_data(void)
{
u8 lower_data, upper_data = 0;
irSensor_read(JSA_1221_READ_ADDR, JSA_PXS_DATA1, &lower_data, 1);
irSensor_read(JSA_1221_READ_ADDR, JSA_PXS_DATA2, &upper_data, 1);
return (upper_data * 256 + lower_data);
}
enum irSensor_event jsa1221_check(void *_para)
{
if (jsa1221_pxs_int_flag()) {
u16 pxs_data = jsa1221_read_pxs_data();
g_printf("DATA:%d", pxs_data);
if (pxs_data < jsa_low_th) {
g_printf("FAR");
return IR_SENSOR_EVENT_FAR;
} else {
g_printf("NEAR");
return IR_SENSOR_EVENT_NEAR;
}
} else {
return IR_SENSOR_EVENT_NULL;
}
}
u8 jsa1221_init(u16 pxs_low_th, u16 pxs_high_th)
{
u32 ret = 1;
jsa_low_th = pxs_low_th;
jsa_high_th = pxs_high_th;
ret &= irSensor_write(JSA_1221_WRITE_ADDR, JSA_CONFIGURE, PXS_ACTIVE_MODE);
ret &= irSensor_write(JSA_1221_WRITE_ADDR, JSA_PXS_LOW_TH1, pxs_low_th & 0xff); // low 8 bit
ret &= irSensor_write(JSA_1221_WRITE_ADDR, JSA_PXS_LOW_TH2, pxs_low_th >> 8); // upper 8 bit
ret &= irSensor_write(JSA_1221_WRITE_ADDR, JSA_PXS_HIGH_TH1, pxs_high_th & 0xff); // low 8 bit
ret &= irSensor_write(JSA_1221_WRITE_ADDR, JSA_PXS_HIGH_TH2, pxs_high_th >> 8); // upper 8 bit
ret &= irSensor_write(JSA_1221_WRITE_ADDR, JSA_INT_CTRL, BIT(7)); //enable pxs int
return ret;
}
REGISTER_IR_SENSOR(irSensor) = {
.logo = "jsa1221",
.ir_sensor_init = jsa1221_init,
.ir_sensor_check = jsa1221_check,
.ir_sensor_ctl = NULL,
};
#endif
+241
View File
@@ -0,0 +1,241 @@
#ifdef SUPPORT_MS_EXTENSIONS
#pragma bss_seg(".ntc_det.data.bss")
#pragma data_seg(".ntc_det.data")
#pragma const_seg(".ntc_det.text.const")
#pragma code_seg(".ntc_det.text")
#endif
//*********************************************************************************//
// NTC det //
//*********************************************************************************//
//
//
#include "gpadc.h"
#include "ntc_det_api.h"
#include "system/includes.h"
#include "asm/charge.h"
#include "app_msg.h"
#include "asm/efuse.h"
#define NTC_USED_INPUT_100K 1//使用内部上拉
#if NTC_USED_INPUT_100K
static u32 efuse_pb00_100k;
#endif
#if NTC_DET_EN
#define NTC_DET_BAD_RES 0 //分压电阻损坏关闭检测
#ifndef NTC_DET_DUTY1
#define NTC_DET_DUTY1 5000 //检测周期
#endif
#ifndef NTC_DET_DUTY2
#define NTC_DET_DUTY2 10 //检测小周期
#endif
#ifndef NTC_DET_CNT
#define NTC_DET_CNT 3 //检测次数
#endif
#ifndef NTC_DET_UPPER
#define NTC_DET_UPPER 322*1000 //正常范围AD值上限,0度时电阻值
#endif
#ifndef NTC_DET_LOWER
#define NTC_DET_LOWER 44*1000 //正常范围AD值下限,45度时电阻值
#endif
#define NTC_REDCOEFF 10*1000 //温度恢复一定范围后才算正常,防止临界状态,默认设置10k的安全范围,参考100k的电阻
#define NTC_IS_NORMAL(value, offset) (value >= NTC_DET_LOWER+(offset) && value <= NTC_DET_UPPER-(offset))
#define NTC_IS_BAD_RES(value) (value >= 3273*1000 || value <= 2197) //大于142度或者低于-40
enum {
NTC_STATE_NORMAL = 0,
NTC_STATE_ABNORMAL,
};
struct ntc_det_t {
u16 normal_cnt : 4; //温度正常的次数
u16 cnt : 4; //温度检测的次数
u16 res_cnt : 4; //分压电阻脱落或损坏
u16 state : 1; //是否超出范围
u16 timer;
};
static struct ntc_det_t ntc_det = {0};
extern u8 get_charge_full_flag(void);
u16 ntc_det_working()
{
return ntc_det.timer;
}
#define NTC_AD_MAX 4096
static u32 ntc_ad_ohm(u32 adc)
{
return (u32)((adc * efuse_pb00_100k) / ((float)(NTC_AD_MAX - adc)));
}
static void ntc_det_timer_deal(void *priv)
{
u32 value;
#if NTC_DET_CNT
if (ntc_det.cnt == 0) {
sys_timer_modify(ntc_det.timer, NTC_DET_DUTY2);
}
#endif
value = adc_get_value(NTC_DET_AD_CH);
value = ntc_ad_ohm(value);
printf("adc %d ohm \n", value);
ntc_det.cnt++;
if (NTC_IS_NORMAL(value, (ntc_det.state * NTC_REDCOEFF))) { //温度恢复一定范围后才算正常,防止临界状态,默认设置10k的安全范围
ntc_det.normal_cnt++;
} else if (NTC_IS_BAD_RES(value)) {
ntc_det.res_cnt++;
}
if (ntc_det.cnt >= NTC_DET_CNT) {
if (ntc_det.normal_cnt > NTC_DET_CNT / 2) {
if (ntc_det.state == NTC_STATE_ABNORMAL) {
printf("temperature recover, start charge");
ntc_det.state = NTC_STATE_NORMAL;
charge_start();
}
}
#if NTC_DET_BAD_RES
else if (ntc_det.res_cnt > NTC_DET_CNT / 2) {
printf("bad res, stop det");
ntc_det_stop();
}
#endif
else {
if (ntc_det.state == NTC_STATE_NORMAL) {
printf("temperature is abnormall, stop charge");
ntc_det.state = NTC_STATE_ABNORMAL;
charge_close();
CHARGE_EN(0);
}
/* power_set_soft_poweroff(); */
}
ntc_det.cnt = 0;
ntc_det.res_cnt = 0;
ntc_det.normal_cnt = 0;
sys_timer_modify(ntc_det.timer, NTC_DET_DUTY1);
}
}
void ntc_det_start(void)
{
if (ntc_det.timer == 0) {
printf("ntc det start");
memset(&ntc_det, 0, sizeof(ntc_det));
gpio_write(NTC_POWER_IO, 1);
#if NTC_USED_INPUT_100K
if (!efuse_pb00_100k) {
u8 efuse_100k = efuse_get_io_pu_100k();
if (efuse_100k != 0xff) {
s8 offset = (efuse_100k & 0x80) ? -(efuse_100k & 0x7F) : efuse_100k;
efuse_pb00_100k = (u32)((3072 - (524 + offset) * 3) / 0.015f);
} else {
printf("PB0_100k not trim\n");
efuse_pb00_100k = 10 * 1000;
}
}
ASSERT(efuse_pb00_100k > 50 * 1000 && efuse_pb00_100k < 150 * 1000);
ASSERT(NTC_DETECT_IO == IO_PORTB_00);
gpio_set_mode(IO_PORT_SPILT(NTC_DETECT_IO), PORT_HIGHZ);
gpio_hw_set_pull_up(IO_PORT_SPILT(NTC_DETECT_IO), GPIO_PULLUP_100K);
#else
gpio_set_mode(IO_PORT_SPILT(NTC_DETECT_IO), PORT_HIGHZ);
#endif
adc_add_sample_ch(NTC_DET_AD_CH);
adc_delay_set(NTC_DET_AD_CH, 0, 9);//采用不准可以延长采样时间0~15
ntc_det.timer = sys_timer_add(NULL, ntc_det_timer_deal, NTC_DET_DUTY1);
}
}
void ntc_det_stop(void)
{
if (!get_charge_full_flag() && get_charge_online_flag() && ntc_det.state == NTC_STATE_ABNORMAL) {
printf("charge protecting, wait recover");
return;
}
if (ntc_det.timer) {
printf("ntc det stop");
sys_timer_del(ntc_det.timer);
ntc_det.timer = 0;
adc_delete_ch(NTC_DET_AD_CH);
gpio_set_mode(IO_PORT_SPILT(NTC_POWER_IO), PORT_HIGHZ);
}
}
#if TCFG_CHARGE_ENABLE
static int ntc_msg_entry(int *msg)
{
switch (msg[0]) {
case CHARGE_EVENT_CHARGE_START:
ntc_det_start();
break;
case CHARGE_EVENT_CHARGE_CLOSE:
ntc_det_stop();
break;
case CHARGE_EVENT_LDO5V_KEEP:
ntc_det_stop();
}
return 0;
}
APP_MSG_PROB_HANDLER(ntc_msg_entry) = {
.owner = 0xff,
.from = MSG_FROM_BATTERY,
.handler = ntc_event_handler,
};
#endif
/* --------------------------------------------------------------------------*/
/**
* @brief ntc否可进低功耗查询函数
*
* @return 0:不可进
* 1:可进
*/
/* ----------------------------------------------------------------------------*/
static u8 ntc_idle_query(void)
{
if (ntc_det.timer) {
return 0;
} else {
return 1;
}
}
/* REGISTER_LP_TARGET(ntc_driver_target) = { */
/* .name = "ntc", */
/* .is_idle = ntc_idle_query, */
/* }; */
#endif
@@ -0,0 +1,14 @@
#ifndef __NTC_DET_API_H__
#define __NTC_DET_API_H__
#include "typedef.h"
#include "app_config.h"
#if NTC_DET_EN
extern void ntc_det_start(void);
extern void ntc_det_stop(void);
extern u16 ntc_det_working();
#endif
#endif //__NTC_DET_API_H__