spi_sample.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #include"rtconfig.h"
  2. #if defined(BSP_USING_SPI_LAYER)
  3. #include"rtconfig.h"
  4. #include <rtthread.h>
  5. #include <rtdevice.h>
  6. #include "auto_test.h"
  7. #include "interrupt.h"
  8. #if defined(BSP_USING_SPI)
  9. #define LOG_TAG "spi_drv"
  10. #elif defined(BSP_USING_SPI_MSG)
  11. #define LOG_TAG "spi_msg_drv"
  12. #endif
  13. #include "drv_log.h"
  14. #include <string.h>
  15. #include "fparameters.h"
  16. #include "fcpu_info.h"
  17. #include "fkernel.h"
  18. #include "ftypes.h"
  19. #include <dfs_file.h>
  20. #if defined(BSP_USING_SPI)
  21. #include "fspim.h"
  22. #include "fspim_hw.h" /* include low-level header file for internal probe */
  23. #include "drv_spi.h"
  24. #elif defined(BSP_USING_SPI_MSG)
  25. #include "fspim_msg.h"
  26. #include "fspim_msg_hw.h" /* include low-level header file for internal probe */
  27. #include "drv_spi_msg.h"
  28. #endif
  29. static struct rt_spi_device spi_flash_device;
  30. /* spi test example */
  31. rt_err_t fspim_test_sample()
  32. {
  33. static struct rt_spi_device *spi_device = RT_NULL;
  34. static struct rt_spi_device *spi_bus = RT_NULL;
  35. rt_err_t res = RT_EOK;
  36. #if defined(E2000D_DEMO_BOARD)||defined(E2000Q_DEMO_BOARD)
  37. spi_bus = (struct rt_spi_device *)rt_device_find("SPI2");
  38. rt_spi_bus_attach_device(&spi_flash_device, "flash", "SPI2", spi_bus);
  39. #endif
  40. #if defined(FIREFLY_DEMO_BOARD)||defined(CUS_DEMO_BOARD)
  41. spi_bus = (struct rt_spi_device *)rt_device_find("SPI0");
  42. rt_spi_bus_attach_device(&spi_flash_device, "flash", "SPI0", spi_bus);
  43. #endif
  44. rt_uint8_t send_to_flash_id = 0x9f; /* Flash cmd */
  45. rt_uint8_t recv_from_falsh_id1[5] = {0};
  46. rt_uint8_t recv_from_falsh_id2[5] = {0};
  47. /* find the spi device to get the device handle */
  48. spi_device = (struct rt_spi_device *)rt_device_find("flash");
  49. if (!spi_device)
  50. {
  51. rt_kprintf("fspim_test_sample run failed! can't find flash device!\n");
  52. }
  53. else
  54. {
  55. static struct rt_spi_message msg1, msg2;
  56. msg1.send_buf = &send_to_flash_id;
  57. msg1.recv_buf = RT_NULL;
  58. msg1.length = 1;
  59. msg1.cs_take = 1;
  60. msg1.cs_release = 0;
  61. msg1.next = &msg2;
  62. msg2.send_buf = RT_NULL;
  63. msg2.recv_buf = recv_from_falsh_id2;
  64. msg2.length = 5;
  65. msg2.cs_take = 0;
  66. msg2.cs_release = 1;
  67. msg2.next = RT_NULL;
  68. /* send the command to read the ID using rt_spi_send_then_recv() */
  69. rt_spi_send_then_recv(spi_device, &send_to_flash_id, 1, recv_from_falsh_id1, 5);
  70. rt_kprintf("use rt_spi_send_then_recv() read flash ID is:0x%x 0x%x 0x%x 0x%x 0x%x\n", recv_from_falsh_id1[0], recv_from_falsh_id1[1], recv_from_falsh_id1[2], recv_from_falsh_id1[3], recv_from_falsh_id1[4]);
  71. /* send the command to read the ID using rt_spi_transfer_message() */
  72. rt_spi_transfer_message(spi_device, &msg1);
  73. rt_kprintf("use rt_spi_transfer_message() read flash ID is:0x%x 0x%x 0x%x 0x%x 0x%x\n", recv_from_falsh_id2[0], recv_from_falsh_id2[1], recv_from_falsh_id2[2], recv_from_falsh_id2[3], recv_from_falsh_id2[4]);
  74. for (int i = 0; i < 5; i++)
  75. {
  76. if(recv_from_falsh_id1[i] == 0xff)
  77. {
  78. res = RT_ERROR;
  79. goto exit;
  80. }
  81. }
  82. for (int j = 0; j < 5; j++)
  83. {
  84. if(recv_from_falsh_id2[j] == 0xff)
  85. {
  86. res = RT_ERROR;
  87. goto exit;
  88. }
  89. }
  90. }
  91. exit:
  92. /* print message on example run result */
  93. if (res == 0)
  94. {
  95. rt_kprintf("%s@%d:rtthread spi test example [success].\r\n", __func__, __LINE__);
  96. }
  97. else
  98. {
  99. rt_kprintf("%s@%d:rtthread spi test example [failure], res = %d\r\n", __func__, __LINE__, res);
  100. }
  101. return res;
  102. }
  103. MSH_CMD_EXPORT(fspim_test_sample, "fspim test sample");
  104. #endif