1 /* I2C-WIFI - Test*/
2 
3 #include <string.h>
4 #include <stdio.h>
5 #include <string.h>
6 #include "freertos/FreeRTOS.h"
7 #include "freertos/task.h"
8 #include "esp_system.h"
9 #include "esp_wifi.h"
10 #include "esp_event.h"
11 #include "esp_log.h"
12 #include "nvs_flash.h"
13 
14 #include "esp_timer.h"
15 #include "driver/i2c.h"
16 
17 #define WAIT_TIMER_PERIOD               (1 * 1000)
18 
19 #define I2C_SDA_GPIO                    CONFIG_TEST_I2C_SDA_NUM
20 #define I2C_SCL_GPIO                    CONFIG_TEST_I2C_SCL_NUM
21 #define I2C_SLAVE_ADDR                  CONFIG_TEST_I2C_SLAVE_ADDR
22 
23 #if CONFIG_TEST_I2C_MASTER_MODE
24 #define I2C_TEST_WIFI_AP_ENABLE         CONFIG_TEST_I2C_WIFI_AP_ENABLE
25 #define I2C_CLK_FREQUENCY               CONFIG_TEST_I2C_MASTER_FREQUENCY
26 #define I2C_MASTER_NUM_PORT             CONFIG_TEST_I2C_PORT_NUM
27 #define I2C_MASTER_TX_BUF_DISABLE       0
28 #define I2C_MASTER_RX_BUF_DISABLE       0
29 #define I2C_ACK_CHECK_EN                0x1
30 #define I2C_ACK_CHECK_DIS               0x0
31 #else
32 #define I2C_SLAVE_NUM_PORT              CONFIG_TEST_I2C_PORT_NUM
33 #define I2C_SLAVE_TX_BUF_LEN            256
34 #define I2C_SLAVE_RX_BUF_LEN            256
35 #endif
36 
37 /* The tests use WiFi configuration that you can set via project configuration menu.
38 
39    If you'd rather not, just change the below entries to strings with
40    the config you want - ie #define TEST_WIFI_SSID "mywifissid"
41 */
42 static const char* TAG = "I2C-wifi test";
43 
44 #if I2C_TEST_WIFI_AP_ENABLE
45 #define TEST_ESP_WIFI_SSID      CONFIG_TEST_WIFI_SSID
46 #define TEST_ESP_WIFI_PASS      CONFIG_TEST_WIFI_PASSWORD
47 #define TEST_ESP_WIFI_CHANNEL   CONFIG_TEST_WIFI_CHANNEL
48 #define TEST_MAX_STA_CONN       CONFIG_TEST_MAX_STA_CONN
49 
50 
wifi_event_handler(void * arg,esp_event_base_t event_base,int32_t event_id,void * event_data)51 static void wifi_event_handler(void *arg, esp_event_base_t event_base,
52                                int32_t event_id, void *event_data)
53 {
54     if (event_id == WIFI_EVENT_AP_STACONNECTED) {
55         wifi_event_ap_staconnected_t *event = (wifi_event_ap_staconnected_t *) event_data;
56         ESP_LOGI(TAG, "station "MACSTR" join, AID=%d", MAC2STR(event->mac), event->aid);
57     } else if (event_id == WIFI_EVENT_AP_STADISCONNECTED) {
58         wifi_event_ap_stadisconnected_t *event = (wifi_event_ap_stadisconnected_t *) event_data;
59         ESP_LOGI(TAG, "station "MACSTR" leave, AID=%d", MAC2STR(event->mac), event->aid);
60     }
61 }
62 
wifi_init_softap(void)63 void wifi_init_softap(void)
64 {
65     ESP_ERROR_CHECK(esp_netif_init());
66     ESP_ERROR_CHECK(esp_event_loop_create_default());
67     esp_netif_create_default_wifi_ap();
68 
69     wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
70     ESP_ERROR_CHECK(esp_wifi_init(&cfg));
71 
72     ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL, NULL));
73 
74     wifi_config_t wifi_config = {
75         .ap = {
76             .ssid = TEST_ESP_WIFI_SSID,
77             .ssid_len = strlen(TEST_ESP_WIFI_SSID),
78             .channel = TEST_ESP_WIFI_CHANNEL,
79             .password = TEST_ESP_WIFI_PASS,
80             .max_connection = TEST_MAX_STA_CONN,
81             .authmode = WIFI_AUTH_WPA_WPA2_PSK
82         },
83     };
84     if (strlen(TEST_ESP_WIFI_PASS) == 0) {
85         wifi_config.ap.authmode = WIFI_AUTH_OPEN;
86     }
87 
88     ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP));
89     ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &wifi_config));
90     ESP_ERROR_CHECK(esp_wifi_start());
91 
92     ESP_LOGI(TAG, "wifi_init_softap finished. SSID:%s password:%s channel:%d", TEST_ESP_WIFI_SSID, TEST_ESP_WIFI_PASS, TEST_ESP_WIFI_CHANNEL);
93 }
94 
95 #endif //I2C_TEST_WIFI_AP_ENABLE
96 
97 #if CONFIG_TEST_I2C_MASTER_MODE
98 
99 static esp_timer_handle_t wait_timer;
100 static uint32_t timer_count = 1000 * 1000;
101 
i2c_master_init(void)102 static esp_err_t i2c_master_init(void)
103 {
104     i2c_port_t i2c_master_port = I2C_MASTER_NUM_PORT;
105 
106     i2c_config_t conf_master = {
107         .mode = I2C_MODE_MASTER,
108         .sda_io_num = I2C_SDA_GPIO,
109         .sda_pullup_en = GPIO_PULLUP_ENABLE,
110         .scl_io_num = I2C_SCL_GPIO,
111         .scl_pullup_en = GPIO_PULLUP_ENABLE,
112         .master.clk_speed = I2C_CLK_FREQUENCY,
113     };
114 
115     esp_err_t err = i2c_param_config(i2c_master_port, &conf_master);
116     if (err != ESP_OK) {
117         return err;
118     }
119 
120     return i2c_driver_install(i2c_master_port, conf_master.mode, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0);
121 }
122 
123 // i2c master write
i2c_master_write_to_slave(uint8_t * data,uint32_t size)124 static esp_err_t i2c_master_write_to_slave(uint8_t *data, uint32_t size)
125 {
126     i2c_cmd_handle_t cmd = i2c_cmd_link_create();
127     i2c_master_start(cmd);
128     i2c_master_write_byte(cmd, (I2C_SLAVE_ADDR << 1) | I2C_MASTER_WRITE, I2C_ACK_CHECK_DIS);
129     i2c_master_write(cmd, data, size, I2C_ACK_CHECK_DIS);
130 
131     i2c_master_stop(cmd);
132     esp_err_t ret = i2c_master_cmd_begin(I2C_MASTER_NUM_PORT, cmd, 1000 / portTICK_RATE_MS);
133     i2c_cmd_link_delete(cmd);
134     return ret;
135 }
136 
i2c_data_write(void)137 static esp_err_t i2c_data_write(void)
138 {
139     uint8_t data[] = {0x6a, 0x1b, 0x05, 0x1f, 0x1f, 0x08, 0x01, 0x20, 0x19, 0x03, 0x27};
140 
141     esp_err_t ret = i2c_master_write_to_slave(data, sizeof(data));
142     if(ret != ESP_OK) {
143         ESP_LOGW(TAG,"I2C timeout");
144     }
145 
146     return ret;
147 }
148 
wait_timer_callback(void * arg)149 static void wait_timer_callback(void *arg)
150 {
151     // i2c write
152     i2c_data_write();
153     /* For local test, please remove this part and give long time testing. */
154     timer_count--;
155     if (timer_count == 0) {
156         esp_timer_stop(wait_timer);
157         return;
158     }
159 
160     // restart timer
161     esp_timer_start_once(wait_timer, WAIT_TIMER_PERIOD);
162 }
163 
164 static const esp_timer_create_args_t wait_timer_args = {
165     .callback = &wait_timer_callback,
166     .arg = NULL,
167     .name = "wait_timer",
168 };
169 
170 #else
171 
i2c_slave_init(void)172 static esp_err_t i2c_slave_init(void)
173 {
174     i2c_port_t i2c_slave_port = I2C_SLAVE_NUM_PORT;
175 
176     i2c_config_t conf_slave = {
177         .mode = I2C_MODE_SLAVE,
178         .sda_io_num = I2C_SDA_GPIO,
179         .sda_pullup_en = GPIO_PULLUP_ENABLE,
180         .scl_io_num = I2C_SCL_GPIO,
181         .scl_pullup_en = GPIO_PULLUP_ENABLE,
182         .slave.addr_10bit_en = 0,
183         .slave.slave_addr = I2C_SLAVE_ADDR,
184 
185     };
186 
187     i2c_param_config(i2c_slave_port, &conf_slave);
188     return i2c_driver_install(i2c_slave_port, conf_slave.mode, I2C_SLAVE_RX_BUF_LEN, I2C_SLAVE_TX_BUF_LEN, 0);
189 }
190 
i2c_data_read(void * arg)191 static void i2c_data_read(void *arg)
192 {
193     printf("slave mode.read thread start...\r\n");
194     while (1) {
195         uint8_t data[11] = {0};
196         i2c_slave_read_buffer(I2C_SLAVE_NUM_PORT, data, sizeof(data), 1000 / portTICK_RATE_MS);
197         ESP_LOG_BUFFER_HEX(TAG, data, sizeof(data));
198         vTaskDelay(1);
199     }
200 }
201 #endif
202 
203 // i2c init function
i2c_init(void)204 static esp_err_t i2c_init(void)
205 {
206     esp_err_t err = ESP_OK;
207 #if CONFIG_TEST_I2C_MASTER_MODE
208     err = i2c_master_init();
209 #else
210     err = i2c_slave_init();
211 #endif
212     return err;
213 }
214 
app_main(void)215 void app_main(void)
216 {
217     esp_err_t ret = i2c_init();
218 
219 #if I2C_TEST_WIFI_AP_ENABLE
220     //Initialize NVS
221     ret = nvs_flash_init();
222     if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
223         ESP_ERROR_CHECK(nvs_flash_erase());
224         ret = nvs_flash_init();
225     }
226     ESP_ERROR_CHECK(ret);
227 
228     ESP_LOGI(TAG, "ESP_WIFI_MODE_AP");
229     wifi_init_softap();
230 #endif
231 
232 #if CONFIG_TEST_I2C_MASTER_MODE
233     ret = esp_timer_create(&wait_timer_args, &wait_timer);
234     if (ret != ESP_OK) {
235         ESP_LOGE(TAG, "esp_timer_create fail!");
236     }
237 
238     ret = esp_timer_start_once(wait_timer, WAIT_TIMER_PERIOD);
239     if (ret != ESP_OK) {
240         ESP_LOGE(TAG, "esp_timer_start_once fail!");
241     }
242 #else
243     xTaskCreatePinnedToCore(i2c_data_read, "i2c_data_read", (3 * 1024), NULL, 1, NULL, 1);
244 #endif
245     ESP_ERROR_CHECK(ret);
246     printf("I2C-WIFI test success\n");
247 }
248