1 /* 2 * Copyright (c) 2020-2022 Huawei Device Co., Ltd. 3 * 4 * HDF is dual licensed: you can use it either under the terms of 5 * the GPL, or the BSD license, at your option. 6 * See the LICENSE file in the root of this repository for complete details. 7 */ 8 9 #ifndef SENSOR_PLATFORM_IF_H 10 #define SENSOR_PLATFORM_IF_H 11 12 #include "hdf_log.h" 13 #include "i2c_if.h" 14 #include "spi_if.h" 15 #include "osal_thread.h" 16 17 #define CHECK_NULL_PTR_RETURN_VALUE(ptr, ret) do { \ 18 if ((ptr) == NULL) { \ 19 HDF_LOGE("%s:line %d pointer is null and return ret", __func__, __LINE__); \ 20 return (ret); \ 21 } \ 22 } while (0) 23 24 #define CHECK_NULL_PTR_RETURN(ptr) do { \ 25 if ((ptr) == NULL) { \ 26 HDF_LOGE("%s:line %d pointer is null and return", __func__, __LINE__); \ 27 return; \ 28 } \ 29 } while (0) 30 31 #define CHECK_PARSER_RESULT_RETURN_VALUE(ret, str) do { \ 32 if ((ret) != HDF_SUCCESS) { \ 33 HDF_LOGE("%s:line %d %s fail, ret = %d!", __func__, __LINE__, (str), (ret)); \ 34 return HDF_FAILURE; \ 35 } \ 36 } while (0) 37 38 #define SENSOR_DATA_SHIFT_LEFT(d, s) ((d) << (s)) 39 #define SENSOR_DATA_SHIFT_RIGHT(d, s) ((d) >> (s)) 40 41 #define SENSOR_ADDR_WIDTH_1_BYTE 1 // 8 bit 42 #define SENSOR_ADDR_WIDTH_2_BYTE 2 // 16 bit 43 #define SENSOR_ADDR_WIDTH_4_BYTE 4 // 16 bit 44 #define SENSOR_DATA_WIDTH_8_BIT 8 // 8 bit 45 #define SENSOR_CONVERT_UNIT 1000 46 #define SENSOR_1K_UNIT 1024 47 #define SENSOR_SPI_MAX_SPEED 115200 48 #define SENSOR_SECOND_CONVERT_NANOSECOND (SENSOR_CONVERT_UNIT * SENSOR_CONVERT_UNIT * SENSOR_CONVERT_UNIT) 49 50 #define SENSOR_TIMER_MIN_TIME 20 51 52 enum SensorBusType { 53 SENSOR_BUS_I2C = 0, 54 SENSOR_BUS_SPI = 1, 55 SENSOR_BUS_GPIO = 2, 56 }; 57 58 enum SensorGpioNum { 59 SENSOR_GPIO_NUM1 = 0, 60 SENSOR_GPIO_NUM2 = 1, 61 SENSOR_GPIO_NUM_MAX = 2, 62 }; 63 64 struct SensorI2cCfg { 65 DevHandle handle; 66 uint16_t busNum; 67 uint16_t devAddr; // Address of the I2C device 68 uint16_t regWidth; // length of the register address 69 }; 70 71 struct SensorSpiCfg { 72 DevHandle handle; 73 uint32_t busNum; 74 uint32_t csNum; 75 struct SpiCfg spi; 76 }; 77 78 struct SensorBusCfg { 79 uint8_t busType; // enum SensorBusType 80 uint8_t regBigEndian; 81 union { 82 struct SensorI2cCfg i2cCfg; 83 struct SensorSpiCfg spiCfg; 84 uint32_t GpioNum[SENSOR_GPIO_NUM_MAX]; 85 }; 86 }; 87 88 enum SENSORConfigValueIndex { 89 SENSOR_ADDR_INDEX, 90 SENSOR_VALUE_INDEX, 91 SENSOR_VALUE_BUTT, 92 }; 93 94 enum SENSORConfigShortValueIndex { 95 SENSOR_SHORT_VALUE_INDEX0, 96 SENSOR_SHORT_VALUE_INDEX1, 97 SENSOR_SHORT_VALUE_INDEX2, 98 SENSOR_SHORT_VALUE_BUTT, 99 }; 100 101 int32_t ReadSensor(struct SensorBusCfg *busCfg, uint16_t regAddr, uint8_t *data, uint16_t dataLen); 102 int32_t WriteSensor(struct SensorBusCfg *busCfg, uint8_t *writeData, uint16_t len); 103 int32_t SetSensorPinMux(uint32_t regAddr, int32_t regSize, uint32_t regValue); 104 105 #endif /* SENSOR_PLATFORM_IF_H */