Răsfoiți Sursa

update:常用spi外设增加锁操作

alienwalker 10 luni în urmă
părinte
comite
0b05470452

+ 11 - 0
components/fatfs/diskio_spitf.c

@@ -29,6 +29,8 @@
 #define LUAT_LOG_TAG "SPI_TF"
 #include "luat_log.h"
 
+//#define luat_spi_lock(x)
+//#define luat_spi_unlock(x)
 
 #define __SDHC_BLOCK_LEN__	(512)
 
@@ -911,7 +913,9 @@ static uint8_t luat_spitf_is_ready(luat_spitf_ctrl_t *spitf)
 static DSTATUS luat_spitf_initialize(void* userdata)
 {
 	luat_mutex_lock(g_s_spitf.locker);
+	luat_spi_lock(g_s_spitf.SpiID);
 	luat_spitf_init(&g_s_spitf);
+	luat_spi_unlock(g_s_spitf.SpiID);
 	luat_mutex_unlock(g_s_spitf.locker);
 	return luat_spitf_is_ready(&g_s_spitf)?0:STA_NOINIT;
 }
@@ -919,6 +923,7 @@ static DSTATUS luat_spitf_initialize(void* userdata)
 static DSTATUS luat_spitf_status(void* userdata)
 {
 	return luat_spitf_is_ready(&g_s_spitf)?0:STA_NOINIT;
+
 }
 
 static DRESULT luat_spitf_read(void* userdata, uint8_t* buff, LBA_t sector, UINT count)
@@ -929,7 +934,9 @@ static DRESULT luat_spitf_read(void* userdata, uint8_t* buff, LBA_t sector, UINT
 		luat_mutex_unlock(g_s_spitf.locker);
 		return RES_NOTRDY;
 	}
+	luat_spi_lock(g_s_spitf.SpiID);
 	luat_spitf_read_blocks(&g_s_spitf, buff, sector, count);
+	luat_spi_unlock(g_s_spitf.SpiID);
 	luat_mutex_unlock(g_s_spitf.locker);
 	return luat_spitf_is_ready(&g_s_spitf)?RES_OK:RES_ERROR;
 }
@@ -942,7 +949,9 @@ static DRESULT luat_spitf_write(void* userdata, const uint8_t* buff, LBA_t secto
 		luat_mutex_unlock(g_s_spitf.locker);
 		return RES_NOTRDY;
 	}
+	luat_spi_lock(g_s_spitf.SpiID);
 	luat_spitf_write_blocks(&g_s_spitf, buff, sector, count);
+	luat_spi_unlock(g_s_spitf.SpiID);
 	luat_mutex_unlock(g_s_spitf.locker);
 	return luat_spitf_is_ready(&g_s_spitf)?RES_OK:RES_ERROR;
 }
@@ -955,7 +964,9 @@ static DRESULT luat_spitf_ioctl(void* userdata, uint8_t ctrl, void* buff)
 		luat_mutex_unlock(g_s_spitf.locker);
 		return RES_NOTRDY;
 	}
+	luat_spi_lock(g_s_spitf.SpiID);
 	luat_spitf_read_config(&g_s_spitf);
+	luat_spi_unlock(g_s_spitf.SpiID);
 	luat_mutex_unlock(g_s_spitf.locker);
 	switch (ctrl) {
 		case CTRL_SYNC :		/* Make sure that no pending write process */

+ 4 - 0
components/network/netdrv/src/ch390h_api.c

@@ -18,6 +18,7 @@
 
 int luat_ch390h_read(ch390h_t* ch, uint8_t addr, uint16_t count, uint8_t* buff) {
     char tmp[1] = {0};
+    luat_spi_lock(ch->spiid);
     luat_gpio_set(ch->cspin, 0);
     if (addr == 0x72) {
         tmp[0] = addr;
@@ -45,6 +46,7 @@ int luat_ch390h_read(ch390h_t* ch, uint8_t addr, uint16_t count, uint8_t* buff)
     }
 
     luat_gpio_set(ch->cspin, 1);
+    luat_spi_unlock(ch->spiid);
     return 0;
 }
 
@@ -54,6 +56,7 @@ int luat_ch390h_write(ch390h_t* ch, uint8_t addr, uint16_t count, uint8_t* buff)
         LLOGI("分配txtmp缓冲区 4k");
         txtmp = luat_heap_malloc(4096);
     }
+    luat_spi_lock(ch->spiid);
     luat_gpio_set(ch->cspin, 0);
     if (count > 1600) {
         return 0; // 直接不发送
@@ -73,6 +76,7 @@ int luat_ch390h_write(ch390h_t* ch, uint8_t addr, uint16_t count, uint8_t* buff)
     }
 
     luat_gpio_set(ch->cspin, 1);
+    luat_spi_unlock(ch->spiid);
     return 0;
 }
 

+ 2 - 0
components/sfud/sfud_port.c

@@ -50,6 +50,7 @@ static sfud_err spi_write_read(const sfud_spi *spi, const uint8_t *write_buf, si
     int type = (*(luat_sfud_flash_t*)(spi->user_data)).luat_spi;
     if ( type == LUAT_TYPE_SPI ) {
         luat_spi_t* spi_flash = (luat_spi_t*) ((*(luat_sfud_flash_t*)(spi->user_data)).user_data);
+        luat_spi_lock(spi_flash -> id);
         if (write_size && read_size) {
             if (luat_spi_transfer(spi_flash -> id, (const char*)write_buf, write_size, (char*)read_buf, read_size) <= 0) {
                 result = SFUD_ERR_TIMEOUT;
@@ -63,6 +64,7 @@ static sfud_err spi_write_read(const sfud_spi *spi, const uint8_t *write_buf, si
                 result = SFUD_ERR_READ;
             }
         }
+        luat_spi_unlock(spi_flash -> id);
     }
     else if ( type == LUAT_TYPE_SPI_DEVICE ) {
         luat_spi_device_t* spi_dev = (luat_spi_device_t*) ((*(luat_sfud_flash_t*)(spi->user_data)).user_data);

+ 17 - 0
luat/include/luat_i2c.h

@@ -127,6 +127,23 @@ int luat_i2c_no_block_transfer(int id, int addr, uint8_t is_read, uint8_t *reg,
 int luat_i2c_set_iomux(int id, uint8_t value);
 
 int luat_i2c_set_polling_mode(int id, uint8_t on_off);
+
+/**
+ * @brief 锁定I2C,只有主模式下才能使用,多个设备挂载在同一条总线上需要使用
+ *
+ * @param id i2c总线id
+ * @return int 成功返回0,其他-1
+ */
+int luat_i2c_lock(int id);
+
+/**
+ * @brief 释放I2C,只有主模式下才能使用,多个设备挂载在同一条总线上需要使用
+ *
+ * @param id i2c总线id
+ * @return int 成功返回0,其他-1
+ */
+int luat_i2c_unlock(int id);
+/**@}*/
 /** @}*/
 
 #define LUAT_EI2C_TYPE "EI2C*"

+ 15 - 0
luat/include/luat_spi.h

@@ -253,5 +253,20 @@ int luat_spi_device_recv(luat_spi_device_t* spi_dev, char* recv_buf, size_t leng
  */
 int luat_spi_device_send(luat_spi_device_t* spi_dev, const char* send_buf, size_t length);
 
+/**
+ * @brief 锁定SPI,只有主模式下才能使用,多个设备挂载在同一条总线上需要使用
+ *
+ * @param spi_id spi id
+ * @return int 成功返回0,其他-1
+ */
+int luat_spi_lock(int spi_id);
+
+/**
+ * @brief 释放SPI,只有主模式下才能使用,多个设备挂载在同一条总线上需要使用
+ *
+ * @param spi_id spi id
+ * @return int 成功返回0,其他-1
+ */
+int luat_spi_unlock(int spi_id);
 /**@}*/
 #endif

+ 15 - 1
luat/weak/luat_spi_device.c

@@ -5,7 +5,6 @@
 #define LUAT_SPI_CS_SELECT 0
 #define LUAT_SPI_CS_CLEAR  1
 
-
 // luat_spi_device_t* 在lua层看到的是一个userdata
 LUAT_WEAK int luat_spi_device_setup(luat_spi_device_t* spi_dev) {
     luat_spi_bus_setup(spi_dev);
@@ -22,6 +21,7 @@ LUAT_WEAK int luat_spi_device_close(luat_spi_device_t* spi_dev) {
 //收发SPI数据,返回接收字节数
 LUAT_WEAK int luat_spi_device_transfer(luat_spi_device_t* spi_dev, const char* send_buf, size_t send_length, char* recv_buf, size_t recv_length) {
     int ret = -1;
+    luat_spi_lock(spi_dev->bus_id);
     luat_spi_device_config(spi_dev);
     if (spi_dev->spi_config.cs != 255)
         luat_gpio_set(spi_dev->spi_config.cs, LUAT_SPI_CS_SELECT);
@@ -42,28 +42,42 @@ LUAT_WEAK int luat_spi_device_transfer(luat_spi_device_t* spi_dev, const char* s
     }
     if (spi_dev->spi_config.cs != 255)
         luat_gpio_set(spi_dev->spi_config.cs, LUAT_SPI_CS_CLEAR);
+    luat_spi_unlock(spi_dev->bus_id);
     return ret;
 }
 
 //收SPI数据,返回接收字节数
 LUAT_WEAK int luat_spi_device_recv(luat_spi_device_t* spi_dev, char* recv_buf, size_t length) {
+	luat_spi_lock(spi_dev->bus_id);
     luat_spi_device_config(spi_dev);
     if (spi_dev->spi_config.cs != 255)
         luat_gpio_set(spi_dev->spi_config.cs, LUAT_SPI_CS_SELECT);
     int ret = luat_spi_recv(spi_dev->bus_id, recv_buf, length);
     if (spi_dev->spi_config.cs != 255)
         luat_gpio_set(spi_dev->spi_config.cs, LUAT_SPI_CS_CLEAR);
+    luat_spi_unlock(spi_dev->bus_id);
     return ret;
 }
 
 //发SPI数据,返回发送字节数
 LUAT_WEAK int luat_spi_device_send(luat_spi_device_t* spi_dev, const char* send_buf, size_t length) {
+	luat_spi_lock(spi_dev->bus_id);
     luat_spi_device_config(spi_dev);
     if (spi_dev->spi_config.cs != 255)
         luat_gpio_set(spi_dev->spi_config.cs, LUAT_SPI_CS_SELECT);
     int ret = luat_spi_send(spi_dev->bus_id, send_buf, length);
     if (spi_dev->spi_config.cs != 255)
         luat_gpio_set(spi_dev->spi_config.cs, LUAT_SPI_CS_CLEAR);
+    luat_spi_unlock(spi_dev->bus_id);
     return ret;
 }
 
+LUAT_WEAK int luat_spi_lock(int spi_id)
+{
+    return -1;
+}
+
+LUAT_WEAK int luat_spi_unlock(int spi_id)
+{
+	return -1;
+}