spi_sample.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Copyright (C) 2018 Shanghai Eastsoft Microelectronics Co., Ltd.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the License); you may
  7. * not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  14. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. * Change Logs:
  19. * Date Author Notes
  20. * 2020-12-15 liuhy first implementation.
  21. */
  22. /*
  23. * 程序清单:这是一个 SPI 设备使用例程
  24. * 例程导出了 spi_io_sample 命令到控制终端
  25. * 命令调用格式:spi_io_sample
  26. * 程序功能:通过SPI设备先读取数据,然后每个字符加1后输出。
  27. */
  28. #include <rtthread.h>
  29. #include <rtdevice.h>
  30. #ifdef RT_USING_SPI
  31. #define SPI_DEVICE_NAME "spi00"
  32. #define BUF_LEN 16
  33. static void spi_io_sample(int argc, char *argv[])
  34. {
  35. struct rt_spi_device * spi_dev; /* spi设备的句柄 */
  36. rt_uint8_t i,buffer[BUF_LEN] = { 0U };
  37. rt_err_t s_stat;
  38. rt_err_t result;
  39. /* 查找 spi设备 获取spi设备句柄 */
  40. spi_dev = (struct rt_spi_device *)rt_device_find(SPI_DEVICE_NAME);
  41. if (spi_dev == RT_NULL)
  42. {
  43. rt_kprintf("spi sample run failed! can't find %s device!\n", SPI_DEVICE_NAME);
  44. return;
  45. }
  46. /* 配置SPI设备 */
  47. s_stat = rt_spi_configure(spi_dev,&(spi_dev->config));
  48. if(s_stat != RT_EOK)
  49. {
  50. rt_kprintf(" spi config fail !\n ");
  51. return;
  52. }
  53. /* 获取总线 ,防止总线被多个线程同时使用 */
  54. result = rt_spi_take_bus(spi_dev);
  55. if (result != RT_EOK)
  56. {
  57. rt_kprintf(" %s take spi bus failed! \n", SPI_DEVICE_NAME);
  58. return;
  59. }
  60. /* 选中片选 */
  61. result = rt_spi_take(spi_dev);
  62. if (result != RT_EOK)
  63. {
  64. rt_kprintf(" %s take spi cs failed! \n", SPI_DEVICE_NAME);
  65. return;
  66. }
  67. /*接收一次数据*/
  68. result = rt_spi_recv(spi_dev,buffer,BUF_LEN);
  69. if(result != BUF_LEN)
  70. {
  71. rt_kprintf("receive fail. \n buffer is : %s \n:",buffer);
  72. for( i = 0 ; i < BUF_LEN ; i++)
  73. rt_kprintf(" %x",(unsigned int)buffer[i]);
  74. rt_kprintf("\n");
  75. return;
  76. }
  77. rt_kprintf("receive successful. \n buffer is : %s \n:",buffer);
  78. for( i = 0 ; i < BUF_LEN ; i++)
  79. rt_kprintf(" %x",(unsigned int)buffer[i]);
  80. rt_kprintf("\n");
  81. /* 将接收到的数据加1 */
  82. for( i = 0 ; i < BUF_LEN ; i++)
  83. buffer[i]++;
  84. /*发送数据*/
  85. result = rt_spi_send(spi_dev,buffer,BUF_LEN);
  86. if(result != BUF_LEN)
  87. {
  88. rt_kprintf("send fail. \n buffer is : %s \n:",buffer);
  89. for( i = 0 ; i < BUF_LEN ; i++)
  90. rt_kprintf(" %x",(unsigned int)buffer[i]);
  91. rt_kprintf("\n");
  92. return;
  93. }
  94. rt_kprintf("send successful. \n buffer is : %s \n:",buffer);
  95. for( i = 0 ; i < BUF_LEN ; i++)
  96. rt_kprintf(" %x",(unsigned int)buffer[i]);
  97. rt_kprintf("\n");
  98. /* 释放片选 */
  99. result = rt_spi_release(spi_dev);
  100. if (result != RT_EOK)
  101. {
  102. rt_kprintf(" %s release spi cs failed! \n", SPI_DEVICE_NAME);
  103. return;
  104. }
  105. /* 释放总线 */
  106. result = rt_spi_release_bus(spi_dev);
  107. if (result != RT_EOK)
  108. {
  109. rt_kprintf(" %s release spi bus failed! \n", SPI_DEVICE_NAME);
  110. return;
  111. }
  112. }
  113. /* 导出到 msh 命令列表中 */
  114. MSH_CMD_EXPORT(spi_io_sample, spi sample);
  115. #endif