return ret;
}
+#include "tyhx_hrs_custom.h"
+
+void get_ppg_id(void)
+{
+ tyhx_poweron();
+ hx3695_chip_check();
+ tyhx_poweroff();
+}
+
+#include "hal_i2c.h"
+#include "i2c.h"
+#define CUSTOM_I2C_TIME_MAX 3
+
+static uint8_t i2c_installed = 0;
+static uint8_t i2c_mutex_installed = 0;
+static osal_mutex i2c_mutex;
+
+void custom_i2c_reset(void)
+{
+ errcode_t result;
+ if(i2c_mutex_installed == 0)
+ {
+ osal_mutex_init(&i2c_mutex);
+ i2c_mutex_installed = 1;
+ }
+
+ result = osal_mutex_lock(&i2c_mutex);
+ if (result != ERRCODE_SUCC)
+ {
+ PRINT(" i2c lock fail! ret=0x%x\n", result);
+ return ;
+ }
+
+ if(i2c_installed == 1)
+ {
+ result = uapi_i2c_deinit(I2C_BUS_2);
+ if (result != ERRCODE_SUCC)
+ {
+ PRINT(" i2c deinit fail! ret=0x%x\n", result);
+ return ;
+ }
+ i2c_installed = 0;
+ }
+
+ LOS_Mdelay(5);
+
+ if(i2c_installed == 0)
+ {
+ result = uapi_i2c_master_init(I2C_BUS_2, 400000, I2C_SPEED_MODE_FS); /* 400000: 400KHz */
+ if (result != ERRCODE_SUCC) {
+ PRINT(" i2c init fail! ret=0x%x\n", result);
+ return ;
+ }
+ i2c_installed = 1;
+ }
+
+ LOS_Mdelay(5);
+
+ osal_mutex_unlock(&i2c_mutex);
+}
+
+errcode_t custom_iic_init(void)
+{
+ errcode_t result;
+ if(i2c_installed == 0)
+ {
+ result = uapi_i2c_master_init(I2C_BUS_2, 400000, I2C_SPEED_MODE_FS); /* 400000: 400KHz */
+
+ if (result != ERRCODE_SUCC) {
+ PRINT("cywee i2c init fail! ret=0x%x\n", result);
+ return ERRCODE_FAIL;
+ }
+
+ i2c_installed = 1;
+ return ERRCODE_SUCC;
+ }
+ else
+ return ERRCODE_FAIL;
+}
+
+errcode_t custom_i2c_read_regs(uint16_t slaveAddr, uint16_t reg, int regLength, uint8_t *readData, int readDataSize, int busIndex)
+{
+ errcode_t ret;
+ uint32_t retry;
+ i2c_data_t data;
+ uint8_t *write_data = NULL;
+ // custom_iic_init();
+
+ write_data = (uint8_t *)osal_kmalloc(regLength, OSAL_GFP_KERNEL);
+ if (write_data == NULL) {
+ PRINT("send buff malloc fail! %d\n",regLength);
+ goto error;
+ }
+
+ if( regLength == 2)
+ {
+ write_data[0] = ((uint8_t*)®)[0];
+ write_data[1] = ((uint8_t*)®)[1];
+ }else{
+ write_data[0] = (uint8_t)reg;
+ }
+
+
+ data.send_buf = write_data;
+ data.send_len = regLength;
+ data.receive_buf = readData;
+ data.receive_len = readDataSize;
+
+ for (retry = 0; retry < CUSTOM_I2C_TIME_MAX; retry++) {
+ ret = uapi_i2c_master_writeread(busIndex, slaveAddr, &data);
+ if (ret == ERRCODE_SUCC) {
+ break;
+ }
+ }
+ if (ret != ERRCODE_SUCC) {
+ PRINT("i2cRead data read fail! busIndex %d, slaveAddr 0x%x, regaddr 0x%X,reglength %d, ret=0x%x\n",busIndex, slaveAddr,reg,regLength, ret);
+ }
+ osal_kfree(write_data);
+error:
+ return (ret>0) ? ret*-1: ret;
+}
+
+errcode_t custom_i2c_write_reg(uint16_t slaveAddr, uint16_t reg, int regLength, uint8_t *writeData, int writeDataSize, int busIndex)
+{
+ errcode_t ret;
+ uint32_t retry;
+ i2c_data_t data;
+ uint8_t *write_data = NULL;
+ uint16_t send_len;
+ uint8_t *data_buf=writeData;
+ uint16_t data_len=writeDataSize;
+
+ //custom_iic_init();
+ send_len = data_len + regLength;
+ write_data = (uint8_t *)osal_kmalloc(send_len, OSAL_GFP_KERNEL);
+ if (write_data == NULL) {
+ PRINT("i2cWrite send buff malloc fail %d!\n",send_len);
+ goto error;
+ }
+
+ if( regLength == 2)
+ {
+ write_data[0] = ((uint8_t*)®)[0];
+ write_data[1] = ((uint8_t*)®)[1];
+ }else{
+ write_data[0] = (uint8_t)reg;
+ }
+ ret = (uint32_t)memcpy_s(write_data + regLength, send_len - regLength, data_buf, data_len);
+ if (ret != EOK) {
+ PRINT("send_buf content copy fail! ret=0x%x\n", ret);
+ goto free_mem;
+ }
+
+ data.send_buf = write_data;
+ data.send_len = send_len;
+ data.receive_buf = NULL;
+ data.receive_len = 0;
+
+ for (retry = 0; retry < CUSTOM_I2C_TIME_MAX; retry++) {
+ ret = uapi_i2c_master_write(busIndex, slaveAddr, &data);
+ if (ret == ERRCODE_SUCC) {
+ break;
+ }
+ }
+ if (ret != ERRCODE_SUCC) {
+ PRINT("i2cWrite cywee data write fail! ret=0x%x\n", ret);
+ }
+free_mem:
+ osal_kfree(write_data);
+error:
+ return (ret>0) ? ret*-1: ret;
+}
+
+errcode_t custom_i2c_transfer(uint16_t slaveAddr, uint16_t reg, int regLength, uint8_t *writeData, int writeDataSize, uint8_t *readData, int readDataSize, int busIndex)
+{
+ errcode_t ret;
+ uint32_t retry;
+ i2c_data_t data;
+ uint8_t *write_data = NULL;
+ int16_t write_len = 0;
+
+ //custom_iic_init();
+ write_len = regLength + writeDataSize;
+ write_data = (uint8_t *)osal_kmalloc(write_len, OSAL_GFP_KERNEL);
+ if (write_data == NULL) {
+ PRINT(" send buff malloc fail!");
+ goto error;
+ }
+
+ if( regLength == 2)
+ {
+ write_data[0] = ((uint8_t*)®)[0];
+ write_data[1] = ((uint8_t*)®)[1];
+ }else{
+ write_data[0] = (uint8_t)reg;
+ }
+
+ ret = (uint32_t)memcpy_s(write_data + regLength, writeDataSize, writeData, writeDataSize);
+ if (ret != EOK) {
+ PRINT("send_buf content copy fail! ret=0x%x\n", ret);
+ goto free_mem;
+ }
+ data.send_buf = write_data;
+ data.send_len = write_len;
+ data.receive_buf = readData;
+ data.receive_len = readDataSize;
+
+ for (retry = 0; retry < CUSTOM_I2C_TIME_MAX; retry++) {
+ ret = uapi_i2c_master_writeread(busIndex, slaveAddr, &data);
+ if (ret == ERRCODE_SUCC) {
+ break;
+ }
+ }
+ if (ret != ERRCODE_SUCC) {
+ PRINT("data read fail! ret=0x%x\n", ret);
+ }
+
+free_mem:
+ osal_kfree(write_data);
+error:
+ return (ret>0) ? ret*-1: ret;
+}
+
+uint32_t custom_i2c_suspend(void)
+{
+ i2c_save_reg(I2C_BUS_2);
+ return 0;
+}
+uint32_t custom_i2c_resume(void)
+{
+ i2c_recovery_reg(I2C_BUS_2);
+ return 0;
+}
++
+ void uapi_enter_power_saving_mode(void)
+ {
+ PRINT("Enter Power Saving Mode !!! \r\n");
+ //关闭蓝牙
+ stop_custom_ble_service();
+
+ //关闭cat1
+ uapi_cat1_power_on(0);
+ uapi_cat1_reset(1);
+
+ //停止心率周期检测
+
+ //进入normal模式
+ do_cywee_service_check_msg(CYWEE_SERVICE_CMD_NORMAL_MODE,NULL);
+
+ //关闭音乐
+ }
+
+ void uapi_exit_power_saving_mode(void)
+ {
+ PRINT("Exit Power Saving Mode !!! \r\n");
+ //打开蓝牙
+ start_custom_ble_service();
+
+ //打开cat1
+ uapi_cat1_power_on(1);
+ uapi_cat1_reset(0);
+
+ //开启心率周期检测
++
+ }