| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- #include "luat_base.h"
- #include "luat_gpio.h"
- #include "luat_spi.h"
- #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);
- if (spi_dev->spi_config.cs != 255)
- luat_gpio_mode(spi_dev->spi_config.cs, Luat_GPIO_OUTPUT, Luat_GPIO_DEFAULT, Luat_GPIO_HIGH); // CS
- return 0;
- }
- //关闭SPI设备,成功返回0
- LUAT_WEAK int luat_spi_device_close(luat_spi_device_t* spi_dev) {
- return luat_spi_close(spi_dev->bus_id);
- }
- //收发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);
- if (spi_dev->spi_config.mode){
- if (send_length>recv_length){
- recv_length = send_length;
- }else{
- send_length = recv_length;
- }
- ret = luat_spi_transfer(spi_dev->bus_id, send_buf, send_length, recv_buf, recv_length);
- }else{
- if (send_length){
- ret = luat_spi_send(spi_dev->bus_id, send_buf, send_length);
- }
- if (recv_length){
- ret = luat_spi_recv(spi_dev->bus_id, recv_buf, recv_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_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;
- }
|