• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  *
15  * Description: Provides iot_i2c driver source \n
16  *
17  * History: \n
18  * 2023-10-18, Create file. \n
19  */
20 #include "iot_errno.h"
21 #include "i2c.h"
22 #include "iot_i2c.h"
23 
IoTI2cInit(unsigned int id,unsigned int baudrate)24 unsigned int IoTI2cInit(unsigned int id, unsigned int baudrate)
25 {
26     uint8_t hscode = 0x0;
27 #if defined(CONFIG_I2C_SUPPORT_DMA)
28     uapi_dma_init();
29     uapi_dma_open();
30 #endif
31     if (uapi_i2c_master_init((i2c_bus_t)id, (uint32_t)baudrate, hscode) != ERRCODE_SUCC) {
32         return IOT_FAILURE;
33     }
34 #if defined(CONFIG_I2C_SUPPORT_INT)
35     uapi_i2c_set_irq_mode((i2c_bus_t)id, 1);
36 #endif
37     return IOT_SUCCESS;
38 }
39 
IoTI2cDeinit(unsigned int id)40 unsigned int IoTI2cDeinit(unsigned int id)
41 {
42     if (uapi_i2c_deinit((i2c_bus_t)id) != ERRCODE_SUCC) {
43         return IOT_FAILURE;
44     }
45     return IOT_SUCCESS;
46 }
47 
IoTI2cWrite(unsigned int id,unsigned short deviceAddr,const unsigned char * data,unsigned int dataLen)48 unsigned int IoTI2cWrite(unsigned int id, unsigned short deviceAddr, const unsigned char *data, unsigned int dataLen)
49 {
50     if (data == NULL) {
51         return IOT_FAILURE;
52     }
53     i2c_data_t datas = { 0 };
54     datas.send_buf = (uint8_t *)data;
55     datas.send_len = (uint32_t)dataLen;
56 
57     if (uapi_i2c_master_write((i2c_bus_t)id, (uint16_t)deviceAddr, &datas) != ERRCODE_SUCC) {
58         return IOT_FAILURE;
59     }
60     return IOT_SUCCESS;
61 }
62 
IoTI2cRead(unsigned int id,unsigned short deviceAddr,unsigned char * data,unsigned int dataLen)63 unsigned int IoTI2cRead(unsigned int id, unsigned short deviceAddr, unsigned char *data, unsigned int dataLen)
64 {
65     if (data == NULL) {
66         return IOT_FAILURE;
67     }
68     i2c_data_t datas = { 0 };
69     datas.receive_buf = (uint8_t *)data;
70     datas.receive_len = (uint32_t)dataLen;
71 
72     if (uapi_i2c_master_read((i2c_bus_t)id, (uint16_t)deviceAddr, &datas) != ERRCODE_SUCC) {
73         return IOT_FAILURE;
74     }
75     return IOT_SUCCESS;
76 }
77 
IoTI2cSetBaudrate(unsigned int id,unsigned int baudrate)78 unsigned int IoTI2cSetBaudrate(unsigned int id, unsigned int baudrate)
79 {
80     if (uapi_i2c_set_baudrate((i2c_bus_t)id, (uint32_t)baudrate) != ERRCODE_SUCC) {
81         return IOT_FAILURE;
82     }
83     return IOT_SUCCESS;
84 }