spi_sample.c 3.3 KB

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