luat_spi_device.c 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #include "luat_base.h"
  2. #include "luat_gpio.h"
  3. #include "luat_spi.h"
  4. #define LUAT_SPI_CS_SELECT 0
  5. #define LUAT_SPI_CS_CLEAR 1
  6. // luat_spi_device_t* 在lua层看到的是一个userdata
  7. LUAT_WEAK int luat_spi_device_setup(luat_spi_device_t* spi_dev) {
  8. luat_spi_bus_setup(spi_dev);
  9. if (spi_dev->spi_config.cs != 255)
  10. luat_gpio_mode(spi_dev->spi_config.cs, Luat_GPIO_OUTPUT, Luat_GPIO_DEFAULT, Luat_GPIO_HIGH); // CS
  11. return 0;
  12. }
  13. //关闭SPI设备,成功返回0
  14. LUAT_WEAK int luat_spi_device_close(luat_spi_device_t* spi_dev) {
  15. return luat_spi_close(spi_dev->bus_id);
  16. }
  17. //收发SPI数据,返回接收字节数
  18. 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) {
  19. int ret = -1;
  20. luat_spi_lock(spi_dev->bus_id);
  21. luat_spi_device_config(spi_dev);
  22. if (spi_dev->spi_config.cs != 255)
  23. luat_gpio_set(spi_dev->spi_config.cs, LUAT_SPI_CS_SELECT);
  24. if (spi_dev->spi_config.mode){
  25. if (send_length>recv_length){
  26. recv_length = send_length;
  27. }else{
  28. send_length = recv_length;
  29. }
  30. ret = luat_spi_transfer(spi_dev->bus_id, send_buf, send_length, recv_buf, recv_length);
  31. }else{
  32. if (send_length){
  33. ret = luat_spi_send(spi_dev->bus_id, send_buf, send_length);
  34. }
  35. if (recv_length){
  36. ret = luat_spi_recv(spi_dev->bus_id, recv_buf, recv_length);
  37. }
  38. }
  39. if (spi_dev->spi_config.cs != 255)
  40. luat_gpio_set(spi_dev->spi_config.cs, LUAT_SPI_CS_CLEAR);
  41. luat_spi_unlock(spi_dev->bus_id);
  42. return ret;
  43. }
  44. //收SPI数据,返回接收字节数
  45. LUAT_WEAK int luat_spi_device_recv(luat_spi_device_t* spi_dev, char* recv_buf, size_t length) {
  46. luat_spi_lock(spi_dev->bus_id);
  47. luat_spi_device_config(spi_dev);
  48. if (spi_dev->spi_config.cs != 255)
  49. luat_gpio_set(spi_dev->spi_config.cs, LUAT_SPI_CS_SELECT);
  50. int ret = luat_spi_recv(spi_dev->bus_id, recv_buf, length);
  51. if (spi_dev->spi_config.cs != 255)
  52. luat_gpio_set(spi_dev->spi_config.cs, LUAT_SPI_CS_CLEAR);
  53. luat_spi_unlock(spi_dev->bus_id);
  54. return ret;
  55. }
  56. //发SPI数据,返回发送字节数
  57. LUAT_WEAK int luat_spi_device_send(luat_spi_device_t* spi_dev, const char* send_buf, size_t length) {
  58. luat_spi_lock(spi_dev->bus_id);
  59. luat_spi_device_config(spi_dev);
  60. if (spi_dev->spi_config.cs != 255)
  61. luat_gpio_set(spi_dev->spi_config.cs, LUAT_SPI_CS_SELECT);
  62. int ret = luat_spi_send(spi_dev->bus_id, send_buf, length);
  63. if (spi_dev->spi_config.cs != 255)
  64. luat_gpio_set(spi_dev->spi_config.cs, LUAT_SPI_CS_CLEAR);
  65. luat_spi_unlock(spi_dev->bus_id);
  66. return ret;
  67. }
  68. LUAT_WEAK int luat_spi_lock(int spi_id)
  69. {
  70. return -1;
  71. }
  72. LUAT_WEAK int luat_spi_unlock(int spi_id)
  73. {
  74. return -1;
  75. }