Browse Source

add:添加i2c支持

Dozingfiretruck 3 years ago
parent
commit
ed8cb10e2b

+ 3 - 2
luatos/components/luat/port/luat_base_idf5.c

@@ -204,10 +204,11 @@ void luat_openlibs(lua_State *L) {
     }
 }
 
+#include "sys/unistd.h"
 #include "esp_system.h"
 
-void luat_os_reboot(int code) {
-    esp_restart();
+void luat_timer_us_delay(size_t us){
+    usleep(us);
 }
 
 const char* luat_os_bsp(void) {

+ 72 - 0
luatos/components/luat/port/luat_i2c_idf5.c

@@ -0,0 +1,72 @@
+
+#include "luat_base.h"
+#include "luat_i2c.h"
+
+#include "driver/i2c.h"
+
+#define LUAT_LOG_TAG "i2c"
+#include "luat_log.h"
+
+#define ACK_CHECK_EN 0x1           /*!< I2C master will check ack from slave*/
+
+#define I2C_CHECK(i2cid) ((i2cid<0 || i2cid>=SOC_I2C_NUM) ? -1:0)
+
+int luat_i2c_exist(int id){
+    return (I2C_CHECK(id)==0);
+}
+
+int luat_i2c_setup(int id, int speed, int slaveaddr){
+    if (I2C_CHECK(id)){
+        return -1;
+    }
+    i2c_config_t conf = {0};
+    conf.mode = I2C_MODE_MASTER;
+    conf.sda_io_num = 4;
+    conf.scl_io_num = 5;
+    conf.sda_pullup_en = GPIO_PULLUP_ENABLE;
+    conf.scl_pullup_en = GPIO_PULLUP_ENABLE;
+    if (speed == 0)
+        conf.master.clk_speed = 100 * 1000;
+    else if (speed == 1)
+        conf.master.clk_speed = 400 * 1000;
+    i2c_param_config(id, &conf);
+    i2c_driver_install(id, conf.mode, 0, 0, 0);
+    return 0;
+}
+
+int luat_i2c_close(int id){
+    if (I2C_CHECK(id)){
+        return -1;
+    }
+    i2c_driver_delete(id);
+    return 0;
+}
+
+int luat_i2c_send(int id, int addr, void *buff, size_t len, uint8_t stop){
+    if (I2C_CHECK(id)){
+        return -1;
+    }
+    i2c_cmd_handle_t cmd = i2c_cmd_link_create();
+    i2c_master_start(cmd);
+    i2c_master_write_byte(cmd, addr, ACK_CHECK_EN);
+    i2c_master_write(cmd, (const uint8_t *)buff, len, ACK_CHECK_EN);
+    if (stop)
+        i2c_master_stop(cmd);
+    i2c_master_cmd_begin(id, cmd, 1000 / portTICK_PERIOD_MS);
+    i2c_cmd_link_delete(cmd);
+    return 0;
+}
+
+int luat_i2c_recv(int id, int addr, void *buff, size_t len){
+    if (I2C_CHECK(id)){
+        return -1;
+    }
+    i2c_cmd_handle_t cmd = i2c_cmd_link_create();
+    i2c_master_start(cmd);
+    i2c_master_write_byte(cmd, addr, ACK_CHECK_EN);
+    i2c_master_read(cmd, (uint8_t *)buff, len, I2C_MASTER_LAST_NACK);
+    i2c_master_stop(cmd);
+    i2c_master_cmd_begin(id, cmd, 1000 / portTICK_PERIOD_MS);
+    i2c_cmd_link_delete(cmd);
+    return 0;
+}

+ 1 - 1
luatos/include/luat_conf_bsp.h

@@ -23,7 +23,7 @@
 // 外设,按需启用, 最起码启用uart和wdt库
 #define LUAT_USE_UART 1
 #define LUAT_USE_GPIO 1
-// #define LUAT_USE_I2C  1
+#define LUAT_USE_I2C  1
 // #define LUAT_USE_SPI  1
 // #define LUAT_USE_ADC  1
 // #define LUAT_USE_PWM  1