spi_sample.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-07-19 Rbbb666 first version
  9. * 2024-03-30 xhackerustc 2nd version for FRDM-MCXN947
  10. */
  11. #include "board.h"
  12. #include <drv_spi.h>
  13. #define SPI_NAME "spi60"
  14. #define CS_PIN (3*32+23)
  15. static struct rt_spi_device *spi_dev;
  16. /* attach spi device */
  17. static int rt_spi_device_init(void)
  18. {
  19. struct rt_spi_configuration cfg;
  20. rt_hw_spi_device_attach("spi6", SPI_NAME, CS_PIN);
  21. cfg.data_width = 8;
  22. cfg.mode = RT_SPI_MASTER | RT_SPI_MODE_0 | RT_SPI_MSB | RT_SPI_NO_CS;
  23. cfg.max_hz = 1 *1000 *1000;
  24. spi_dev = (struct rt_spi_device *)rt_device_find(SPI_NAME);
  25. if (RT_NULL == spi_dev)
  26. {
  27. rt_kprintf("spi sample run failed! can't find %s device!\n", SPI_NAME);
  28. return -RT_ERROR;
  29. }
  30. rt_spi_configure(spi_dev, &cfg);
  31. return RT_EOK;
  32. }
  33. INIT_APP_EXPORT(rt_spi_device_init);
  34. /* spi loopback mode test case */
  35. static int spi_sample(int argc, char **argv)
  36. {
  37. rt_uint8_t t_buf[32], r_buf[32];
  38. int i = 0;
  39. static struct rt_spi_message msg1;
  40. for (i = 0; i < sizeof(t_buf); i++)
  41. {
  42. t_buf[i] = i;
  43. }
  44. msg1.send_buf = &t_buf;
  45. msg1.recv_buf = &r_buf;
  46. msg1.length = sizeof(t_buf);
  47. msg1.cs_take = 1;
  48. msg1.cs_release = 1;
  49. msg1.next = RT_NULL;
  50. rt_spi_transfer_message(spi_dev, &msg1);
  51. rt_kprintf("spi rbuf : ");
  52. for (i = 0; i < sizeof(r_buf); i++)
  53. {
  54. rt_kprintf("%x ", r_buf[i]);
  55. }
  56. rt_kprintf("\nspi loopback mode test over!\n");
  57. return RT_EOK;
  58. }
  59. MSH_CMD_EXPORT(spi_sample, spi loopback test);