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
16 #include <app_demo_i2c.h>
17
18 /* Es8311 initialization array */
19 hi_u8 g_es8311_reg_array[I2C_REG_ARRAY_LEN] = {
20 0x1F, 0x00, 0x00, 0x10, 0x10, 0x00, 0x03, 0x00, /* 0x00 ~ 0x0f */
21 0xff, 0x00, 0x00, 0x00, 0x20, 0xfc, 0x6a, 0x00,
22 0x13, 0x7c, 0x02, 0x40, 0x10, 0x00, 0x04, 0x00, /* 0x10 ~ 0x1f */
23 0x00, 0x00, 0x00, 0x0b, 0x4b, 0x00, 0x00, 0x00,
24 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x20 ~ 0x2f */
25 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
26 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, /* 0x30 ~ 0x3f */
27 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
28 };
29
30 hi_u8 g_send_data[I2C_REG_ARRAY_LEN] = { 0 };
31
i2c_demo_send_data_init(hi_void)32 hi_void i2c_demo_send_data_init(hi_void)
33 {
34 hi_u8 i;
35
36 hi_u8 reg_addr = ES8311_REG_ADDR;
37
38 g_send_data[0] = reg_addr;
39 g_send_data[1] = g_es8311_reg_array[reg_addr];
40
41 reg_addr++;
42 g_send_data[2] = reg_addr; /* size 2 */
43 g_send_data[3] = g_es8311_reg_array[reg_addr]; /* size 3 */
44
45 for (i = 0; i < I2C_SEND_LEN_2; i++) {
46 printf("----- data init: send_buf[%d] = 0x%x! -----*\r\n", i, g_send_data[i]);
47 }
48
49 printf("\n");
50 }
51
i2c_demo_write(hi_i2c_idx id,hi_u16 device_addr,hi_u32 send_len)52 hi_u32 i2c_demo_write(hi_i2c_idx id, hi_u16 device_addr, hi_u32 send_len)
53 {
54 hi_u32 status;
55 hi_i2c_data es8311_i2c_data = { 0 };
56
57 es8311_i2c_data.send_buf = g_send_data;
58 es8311_i2c_data.send_len = send_len;
59 status = hi_i2c_write(id, device_addr, &es8311_i2c_data);
60 if (status != HI_ERR_SUCCESS) {
61 printf("===== Error: I2C write status = 0x%x! =====\r\n", status);
62 return status;
63 }
64
65 return HI_ERR_SUCCESS;
66 }
67
i2c_demo_writeread(hi_i2c_idx id,hi_u16 device_addr,hi_u32 recv_len)68 hi_u32 i2c_demo_writeread(hi_i2c_idx id, hi_u16 device_addr, hi_u32 recv_len)
69 {
70 hi_u8 i;
71 hi_u32 status;
72 hi_u8 recv_data[I2C_REG_ARRAY_LEN] = { 0 };
73 hi_i2c_data es8311_i2c_data = { 0 };
74
75 printf("----- reg addr[0] = 0x%x! -----\r\n", g_send_data[0]);
76
77 /* Request memory space */
78 memset_s(recv_data, I2C_REG_ARRAY_LEN, 0x0, sizeof(recv_data));
79 memset_s(&es8311_i2c_data, sizeof(hi_i2c_data), 0x0, sizeof(hi_i2c_data));
80
81 es8311_i2c_data.send_buf = g_send_data;
82 es8311_i2c_data.send_len = 1;
83 es8311_i2c_data.receive_buf = recv_data;
84 es8311_i2c_data.receive_len = recv_len;
85 status = hi_i2c_writeread(id, device_addr, &es8311_i2c_data);
86 if (status != HI_ERR_SUCCESS) {
87 printf("===== Error: I2C read status = 0x%x! =====\r\n", status);
88 return status;
89 }
90
91 for (i = 0; i < recv_len; i++) {
92 printf("----- revData[%d] = 0x%x! -----\r\n", i, recv_data[i]);
93 }
94
95 return HI_ERR_SUCCESS;
96 }
97
i2c_demo(hi_i2c_idx id)98 hi_void i2c_demo(hi_i2c_idx id)
99 {
100 hi_u32 ret;
101
102 printf("----- I2C Start -----\r\n");
103
104 i2c_demo_send_data_init();
105 hi_i2c_init(id, 100000); /* baudrate: 100000 */
106
107 ret = i2c_demo_write(id, ES8311_DEV_ADDR, I2C_SEND_LEN_2);
108 printf("----- (ES8311)i2c_demo_write %d : %s! -----\r\n\n", __LINE__,
109 ret == HI_ERR_SUCCESS ? "SUCCESS!" : "FAILURE!");
110
111 ret = i2c_demo_writeread(id, ES8311_DEV_ADDR, I2C_RECV_LEN_1);
112 printf("----- (ES8311)i2c_demo_writeread %d : %s! -----\r\n\n", __LINE__,
113 ret == HI_ERR_SUCCESS ? "SUCCESS!" : "FAILURE!");
114
115 hi_i2c_deinit(id);
116
117 printf("I2C Test End!\r\n");
118 }
119
app_demo_custom_i2c(hi_void)120 hi_void app_demo_custom_i2c(hi_void)
121 {
122 /* Default use I2C 0 */
123 i2c_demo(HI_I2C_IDX_0);
124 }
125
126