2025-07-07 09:57:18 +08:00

511 lines
15 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// trs55d.c
#include "system/includes.h"
#include "app_config.h"
#include "app_action.h"
#include "app_main.h"
#include "update.h"
#include "update_loader_download.h"
#include "app_charge.h"
#include "app_power_manage.h"
#include "asm/charge.h"
#include "app_temperature.h"
#include "typedef.h"
#include "le_common.h"
#include "cpu.h"
#include "timer.h"
#include "os/os_api.h"
#include "system/includes.h"
#include "gatt_common/le_gatt_common.h"
#include "examples/trans_data/ble_trans_profile.h"
#include "asm/iic_soft.h"
#include "trs55d.h"
#define IIC_CHANNEL_1 0
const struct soft_iic_config soft_iic_cfg[] = {
//iic0 data
{
.scl = IO_PORTA_07, //IIC CLK脚
.sda = IO_PORTA_08, //IIC DAT脚
.delay = 50, //软件IIC延时参数影响通讯时钟频率
.io_pu = 1, //是否打开上拉电阻如果外部电路没有焊接上拉电阻需要置1
},
};
#if 1
//如果无reg_addr:reg_addr=NULL,reg_len=0
//return: <0:error, =read_len:ok
int soft_i2c_master_read_nbytes_from_device_reg(soft_iic_dev iic, //iic索引
unsigned char dev_addr, //设备地址
int reg_addr, unsigned char reg_len,//设备寄存器地址,长度
unsigned char *read_buf, int read_len)//缓存buf读取长度
{
u8 ack;
int ret = 0;
// 发送起始条件
soft_iic_start(iic);
// 1. 发送设备地址(写模式)
ack = soft_iic_tx_byte(iic, dev_addr & 0xFE);
if (!ack) {
printf("dev_addr no ack!\n");
ret = -1;
goto exit;
}
// 2. 发送寄存器地址(支持多字节地址)
if (reg_len > 0) {
// 处理多字节地址(高位在前)
uint8_t addr_buf[4];
for (int i = reg_len - 1; i >= 0; i--) {
addr_buf[i] = (reg_addr >> (8 * (reg_len - 1 - i))) & 0xFF;
}
for (u8 i = 0; i < reg_len; i++) {
ack = soft_iic_tx_byte(iic, addr_buf[i]);
if (!ack) {
printf("reg_addr no ack at byte %d!\n", i);
ret = -1;
goto exit;
}
}
}
// 3. 发送重复起始条件
soft_iic_start(iic);
// 4. 发送设备地址(读模式)
ack = soft_iic_tx_byte(iic, dev_addr | 0x01);
if (!ack) {
printf("dev_addr (read) no ack!\n");
ret = -1;
goto exit;
}
// 5. 读取数据
ret = soft_iic_read_buf(iic, read_buf, read_len);
exit:
// 发送停止条件
soft_iic_stop(iic);
return ret;
/*
u8 ack;
int ret = 0;
//local_irq_disable();
//if (soft_iic_check_busy(iic) == IIC_ERROR_BUSY) { //busy
// ret = IIC_ERROR_BUSY; //busy
// goto _read_exit2;
//}
if ((reg_addr != 0) && (reg_len != 0)) {
soft_iic_start(iic);
ack = soft_iic_tx_byte(iic, dev_addr);
if (ack == 0) {
printf("dev_addr no ack!");
//ret = IIC_ERROR_DEV_ADDR_ACK_ERROR; //无应答
ret = -1;
goto _read_exit1;
}
for (u8 i = 0; i < reg_len; i++) {
ack = soft_iic_tx_byte(iic, reg_addr);
if (ack == 0) {
printf("reg_addr no ack!");
//ret = IIC_ERROR_REG_ADDR_ACK_ERROR; //无应答
ret = -1;
goto _read_exit1;
}
}
}
soft_iic_start(iic);
ack = soft_iic_tx_byte(iic, dev_addr | BIT(0));
if (ack == 0) {
printf("dev_addr no ack!");
//ret = IIC_ERROR_DEV_ADDR_ACK_ERROR; //无应答
goto _read_exit1;
}
ret = soft_iic_read_buf(iic, read_buf, read_len);
_read_exit1:
soft_iic_stop(iic);
return ret;*/
}
//如果无reg_addr:reg_addr=NULL,reg_len=0
//return: =write_len:ok, other:error
int soft_i2c_master_write_nbytes_to_device_reg(soft_iic_dev iic,
unsigned char dev_addr, //设备地址
int reg_addr, unsigned char reg_len,//设备寄存器地址,长度
unsigned char *write_buf, int write_len)//数据buf, 写入长度
{
int res;
u8 ack;
//local_irq_disable();//软件iic不可被中断
//if (soft_iic_check_busy(iic) == IIC_ERROR_BUSY) { //busy
// res = IIC_ERROR_BUSY; //busy
// goto _write_exit2;
//}
soft_iic_start(iic);
ack = soft_iic_tx_byte(iic, dev_addr);
if (ack == 0) {
printf("dev_addr no ack!");
//res = IIC_ERROR_DEV_ADDR_ACK_ERROR; //无应答
res = -1; //无应答
goto _write_exit1;
}
if ((reg_addr !=0 ) && (reg_len != 0)) {
for (u8 i = 0; i < reg_len; i++) {
ack = soft_iic_tx_byte(iic, reg_addr);
if (ack == 0) {
printf("reg_addr no ack!");
//res = IIC_ERROR_REG_ADDR_ACK_ERROR; //无应答
res = -1;
goto _write_exit1;
}
}
}
for (res = 0; res < write_len; res++) {
if (0 == soft_iic_tx_byte(iic, write_buf[res])) {
printf("write data no ack!");
goto _write_exit1;
}
}
_write_exit1:
soft_iic_stop(iic);
return res;
}
#endif
static void udelay(u32 usec)
{
/* if (set_to_close_timer0_delay) { */
/* JL_MCPWM->MCPWM_CON0 &= ~BIT(8 + 3); */
/* JL_MCPWM->TMR3_CNT = 0; */
/* JL_MCPWM->TMR3_PR = clk_get("lsb") / 1000000 * usec; */
/* JL_MCPWM->TMR3_CON = BIT(10) | BIT(0); */
/* JL_MCPWM->MCPWM_CON0 |= BIT(8 + 3); */
/* while (!(JL_MCPWM->TMR3_CON & BIT(12))); */
/* JL_MCPWM->TMR3_CON = BIT(10); */
/* JL_MCPWM->MCPWM_CON0 &= ~BIT(8 + 3); */
/* } else { */
JL_TIMER0->CON = BIT(14);
JL_TIMER0->CNT = 0;
JL_TIMER0->PRD = 16 * 1000000L / 1000000L * usec; //1us
JL_TIMER0->CON = BIT(0); //sys clk
while ((JL_TIMER0->CON & BIT(15)) == 0);
JL_TIMER0->CON = BIT(14);
/* } */
}
void TRS55D_Init(){
#if 0
//LCD VDD3.3
pmu_set_aldo_voltage(PMU_ALDO_MODE_NORMAL,PMU_ALDO_VOL_3_3);
//LCD RESET
system_set_port_mux(GPIO_PORT_A,GPIO_BIT_4,PORTA4_FUNC_A4);
gpio_set_dir(GPIO_PORT_A, GPIO_BIT_4, GPIO_DIR_OUT);
gpio_set_pin_value(GPIO_PORT_A,GPIO_BIT_4,1);
co_delay_100us(1000);
system_set_port_mux(GPIO_PORT_A, GPIO_BIT_6, PORTA6_FUNC_I2C1_CLK);//Pa6
system_set_port_mux(GPIO_PORT_A, GPIO_BIT_7, PORTA7_FUNC_I2C1_DAT);//Pa7
system_set_port_pull( (GPIO_PA6|GPIO_PA7), true);
//IIC0, 1M hz. slave addr: 0xd6
iic_init(IIC_CHANNEL_1,100,TRS_ADDR_AD);
soft_iic_init(0);
#endif
//打开电源
gpio_set_direction(IO_PORTA_02,0);
gpio_direction_output(IO_PORTA_02, 1);
//初始化IIC
soft_iic_init(IIC_CHANNEL_1);
}
uint8_t TRS55D_IIC_Read(uint8_t addr_dev, uint8_t addr_reg, uint8_t *buf, uint16_t count)
{
#if 0
uint8_t ret;
uint8_t ackflag;
uint16_t i = 0;
/*
drv_i2c_start();
drv_i2c_select_dev(addr_dev,DRV_I2C_OPWR);
drv_i2c_writebyte(addr_reg);
drv_i2c_start();
drv_i2c_select_dev(addr_dev,DRV_I2C_OPRD);
for(i = 0; i < count; i ++) {
ackflag = (i < (count-1)) ? 1:0; buf[i] = drv_i2c_readbyte(ackflag);
}
drv_i2c_stop();
*/
iic_read_bytes(IIC_CHANNEL_1, addr_dev, addr_reg, buf, count);
return ret;
#endif
//soft_iic_read_buf(IIC_CHANNEL_1, buf, count);
soft_i2c_master_read_nbytes_from_device_reg(IIC_CHANNEL_1,
addr_dev, //设备地址
addr_reg, 1,//设备寄存器地址,长度
buf, count);//数据buf, 写入长度
return 1;
}
void TRS55D_IIC_Write(uint8_t addr_dev, uint8_t addr_reg, uint8_t *buf, uint16_t count)
{
uint16_t i = 0;
/*
drv_i2c_start();
drv_i2c_select_dev(addr_dev,DRV_I2C_OPWR);
drv_i2c_writebyte(addr_reg);
for (i = 0; i < count; i ++)
{
drv_i2c_writebyte(buf[i]);
}
drv_i2c_stop();
*/
// iic_write_bytes(IIC_CHANNEL_1,addr_dev,addr_reg,buf,count);
//soft_iic_write_buf(IIC_CHANNEL_1, buf, count);
soft_i2c_master_write_nbytes_to_device_reg(IIC_CHANNEL_1,
addr_dev, //设备地址
addr_reg, 1,//设备寄存器地址,长度
buf, count);//数据buf, 写入长度
}
static char buffer[264];
uu32_t obj,tmp_3, vtp_cor;
float tamb_temp ,vtp_cor_uv ,Tobj_temp ;
//#include "FW_TRS_CAL.h"
uint16_t t_objx10_u16;
int16_t Tambx10_u16;
uint16_t Trs_Read(uint16_t *t_body, uint16_t *t_ntc)
{
uint8_t rbuf[4];
uint8_t raddr, rdat;
uint8_t waddr = 0x0, wdat = 0x0;
int timeout = 0;
waddr = 0x30; // CMD
wdat = 0x09; // 0x0000 1 FSM 001 单通道连续转换
TRS55D_IIC_Write(TRS_ADDR_AD, waddr, &wdat, 1); // start conversion 模式使能
udelay(100);
raddr = 0x03; // 原始数据地址
do
{
TRS55D_IIC_Read(TRS_ADDR_AD, raddr, &rdat, 1);
} while (((rdat == 0xFF) || (!(rdat & 0x30))) && timeout++ < 100); // 全真为真
// flag置1 结果为真 取反为假跳出循环
raddr = 0x02; // 经过校准后的数据地址
do
{
TRS55D_IIC_Read(TRS_ADDR_AD, raddr, &rdat, 1);
} while (((rdat == 0xFF) || (!(rdat & 0x0B))) && timeout++ < 200);
// flag置1 结果为真 取反为假跳出循环
udelay(500);
// raddr = NORMAL_DATA1_MSB_R;//0x19 通道1校准后adc数据
//iic_read_byte(IIC_CHANNEL_1, TRS_ADDR_AD, 0x19, &rbuf[0]);
//iic_read_byte(IIC_CHANNEL_1, TRS_ADDR_AD, 0x1A, &rbuf[1]);
//iic_read_byte(IIC_CHANNEL_1, TRS_ADDR_AD, 0x1B, &rbuf[2]);
soft_i2c_master_read_nbytes_from_device_reg(IIC_CHANNEL_1,TRS_ADDR_AD,0x19, 1,&rbuf[0], 1);
soft_i2c_master_read_nbytes_from_device_reg(IIC_CHANNEL_1,TRS_ADDR_AD,0x1A, 1,&rbuf[1], 1);
soft_i2c_master_read_nbytes_from_device_reg(IIC_CHANNEL_1,TRS_ADDR_AD,0x1B, 1,&rbuf[2], 1);
//printf("0x19:%02X 0x1A:%02X 0x1B:%02X \r\n", rbuf[0], rbuf[1], rbuf[2]);
vtp_cor.un.u8b2 = rbuf[0];
vtp_cor.un.u8b1 = rbuf[1];
vtp_cor.un.u8b0 = rbuf[2];
if (vtp_cor.un.u8b2 & 0x80)
{
vtp_cor.un.u8b3 = 0xFF;
}
else
{
vtp_cor.un.u8b3 = 0x00;
}
float vt_cor = 524288;
vtp_cor_uv = (float)vtp_cor.i32 / vt_cor; // vtp_cor_uv=DATA1/2^19±16mV=vtp_cor.i32/vt_cor
vtp_cor_uv *= 1000.0;
// raddr = NORMAL_TEMP_MSB_R;//0x16环境校准后温度
// TRS55D_IIC_Read (TRS_ADDR_AD, raddr, &rbuf[0],3);
// uint8_t iic_read_byte(enum iic_channel_t channel, uint8_t slave_addr, uint8_t reg_addr, uint8_t *buffer)
//iic_read_byte(IIC_CHANNEL_1, TRS_ADDR_AD, 0x16, &rbuf[0]);
//iic_read_byte(IIC_CHANNEL_1, TRS_ADDR_AD, 0x17, &rbuf[1]);
//iic_read_byte(IIC_CHANNEL_1, TRS_ADDR_AD, 0x18, &rbuf[2]);
soft_i2c_master_read_nbytes_from_device_reg(IIC_CHANNEL_1,TRS_ADDR_AD,0x16, 1,&rbuf[0], 1);
soft_i2c_master_read_nbytes_from_device_reg(IIC_CHANNEL_1,TRS_ADDR_AD,0x17, 1,&rbuf[1], 1);
soft_i2c_master_read_nbytes_from_device_reg(IIC_CHANNEL_1,TRS_ADDR_AD,0x18, 1,&rbuf[2], 1);
//printf("0x16:%02X 0x17:%02X 0x18:%02X \r\n", rbuf[0], rbuf[1], rbuf[2]);
tmp_3.un.u8b2 = rbuf[0];
tmp_3.un.u8b1 = rbuf[1];
tmp_3.un.u8b0 = rbuf[2];
if (tmp_3.un.u8b2 & 0x80)
{
tmp_3.un.u8b3 = 0xFF;
}
else
{
tmp_3.un.u8b3 = 0x00;
}
// raddr = NORMAL_Tobj_MSB_R;//0x10 dsp处理数据据
// TRS55D_IIC_Read (TRS_ADDR_AD, raddr, &rbuf[0],3);
//iic_read_byte(IIC_CHANNEL_1, TRS_ADDR_AD, 0x10, &rbuf[0]);
//iic_read_byte(IIC_CHANNEL_1, TRS_ADDR_AD, 0x11, &rbuf[1]);
//iic_read_byte(IIC_CHANNEL_1, TRS_ADDR_AD, 0x12, &rbuf[2]);
soft_i2c_master_read_nbytes_from_device_reg(IIC_CHANNEL_1,TRS_ADDR_AD,0x10, 1,&rbuf[0], 1);
soft_i2c_master_read_nbytes_from_device_reg(IIC_CHANNEL_1,TRS_ADDR_AD,0x11, 1,&rbuf[1], 1);
soft_i2c_master_read_nbytes_from_device_reg(IIC_CHANNEL_1,TRS_ADDR_AD,0x12, 1,&rbuf[2], 1);
//printf("0x10:%02X 0x11:%02X 0x12:%02X \r\n", rbuf[0], rbuf[1], rbuf[2]);
obj.un.u8b2 = rbuf[0];
obj.un.u8b1 = rbuf[1];
obj.un.u8b0 = rbuf[2];
if (obj.un.u8b2 & 0x80)
{
obj.un.u8b3 = 0xFF;
}
else
{
obj.un.u8b3 = 0x00;
}
// co_printf("NORMAL_Tobj_MSB_R :%02X %02X %02X\r\n",rbuf[0],rbuf[1],rbuf[2]);
//printf("tmp_3.i32: %d obj.i32: %d \r\n", tmp_3.i32, obj.i32);
Cal_get_temp(tmp_3.i32, 1000, obj.i32, 1000, &Tambx10_u16, &t_objx10_u16);
Tobj_temp = (float)t_objx10_u16 / 10;
tamb_temp = (float)Tambx10_u16 / 10;
*t_body = (uint16_t)(Tobj_temp * 100);
*t_ntc = (uint16_t)(tamb_temp * 100);
return 1;
}
/*
static char buffer[264];
uu32_t tobj, tamb_cal;
uu32_t vtp_cor;
float vtp_uv_f = 0.0;
float vtp_cor_f = 0.0;
float tambf = 0.0;
float tobjf = 0.0;
float tbdyf = 0.0;
void TRS55D_read(void){
co_printf("TRS55D_read !! \r\n");
uint8_t rbuf[4];
uint8_t raddr,rdat;
uint8_t waddr = 0x0, wdat = 0x0;
int timeout=0;
iic_read_byte(IIC_CHANNEL_1, ADDR_TRS55D, 0x97, &rdat);
co_printf("read raddr :%02X rdat %02X\r\n",0x97,rdat);
co_delay_100us(1000);
waddr = 0x30; wdat = 0x09;
TRS55D_IIC_Write(ADDR_TRS55D, waddr, &wdat,1); //start conversion
//delay_ms(100);
co_delay_100us(1000);
// waite for conversion over
// co_printf("waite for conversion over !! \r\n");
raddr = 0x03;
do {
TRS55D_IIC_Read (ADDR_TRS55D, raddr, &rdat,1);
co_printf("read raddr :%02X rdat %02X\r\n",raddr,rdat);
} while(((rdat == 0xFF) || (!(rdat & 0x30))) && timeout++ < 200);
raddr = 0x02;
do {
TRS55D_IIC_Read (ADDR_TRS55D, raddr, &rdat,1);
co_printf("read raddr :%02X rdat %02X\r\n",raddr,rdat);
} while(((rdat == 0xFF) || (!(rdat & 0x0B))) && timeout++ < 200);
// voltage value after calibration
raddr = TRS55D_NORMAL_DATA1_MSB_R;
TRS55D_IIC_Read (ADDR_TRS55D, raddr,&rbuf[0],3);
co_printf("NORMAL_DATA1_MSB_R :%02X %02X %02X\r\n",rbuf[0],rbuf[1],rbuf[2]);
vtp_cor.un.u8b2 = rbuf[0];
vtp_cor.un.u8b1 = rbuf[1];
vtp_cor.un.u8b0 =rbuf[2];
if (vtp_cor.un.u8b2 & 0x80)
{
vtp_cor.un.u8b3 = 0xFF;
}else {
vtp_cor.un.u8b3 = 0x00;// vtp after corrected
}
vtp_cor_f = (float)vtp_cor.i32/524288.0;
vtp_cor_f *= 1000;
// ambient temperature value after calibration
raddr = TRS55D_NORMAL_TEMP_MSB_R;
TRS55D_IIC_Read (ADDR_TRS55D,raddr,&rbuf[0],3);
co_printf("NORMAL_TEMP_MSB_R :%02X %02X %02X\r\n",rbuf[0],rbuf[1],rbuf[2]);
tamb_cal.un.u8b2 = rbuf[0];
tamb_cal.un.u8b1 =rbuf[1];
tamb_cal.un.u8b0 =rbuf[2];
tambf = (float)tamb_cal.i32 / 16384.0;
// ambient temperature
// object(surface) temperature after calibration
raddr = TRS55D_NORMAL_Tobj_MSB_R;
TRS55D_IIC_Read (ADDR_TRS55D,raddr,&rbuf[0],3);
tobj.un.u8b2 = rbuf[0];
tobj.un.u8b1 = rbuf[1];
tobj.un.u8b0 =rbuf[2];
tobjf = (float)tobj.i32 / 16384.0;
sprintf(buffer, "tambf = %.2f, vtp_cor = %.2f, tobj = %.2f, tbdy = %.2f\r\n",tambf,vtp_cor_f,tobjf,tbdyf);
co_printf("%s \r\n",buffer);
//co_printf("tambf:%.2f vtp_cor_f:%.2f tobjf:%.2f \r\n",tambf,vtp_cor_f,tobjf);
// object(surface) temperature
// get bodytemp
//tbdyf = get_body_temp(tambf,tobjf);
// get bodytemp
// display on uart
// sprintf(buffer, "tamb = %.2f, vtp_cor = %.2f, tobj = %.2f, tbdy = %.2f\r\n",\tambf,vtp_cor_f,tobjf,tbdyf);
//uartSendString(buffer);
// display on oled
/*
float vals[4] = {tambf, vtp_cor_f, tobjf, tbdyf+0.05};
OLED_ShowBNum(64, 0, vals[0], 4, 16, 1); // Tamb
OLED_ShowBNum(48, 16, vals[1], 6, 16, 1); // vtp
OLED_ShowString(112,16,"uv",16, 1);
OLED_ShowBNum(64, 32, vals[2], 4, 16, 1); // Tobj
OLED_ShowBNum(64, 48, vals[3], 4, 16, 1); // Tbdy
*/
//}*/