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