test_spi.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * Copyright (c) 2022 OpenLuat & AirM2M
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  5. * this software and associated documentation files (the "Software"), to deal in
  6. * the Software without restriction, including without limitation the rights to
  7. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  8. * the Software, and to permit persons to whom the Software is furnished to do so,
  9. * subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in all
  12. * copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  16. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  17. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  18. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  19. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. */
  21. #include "user.h"
  22. static uint8_t *prvTxBuf;
  23. static uint8_t *prvRxBuf;
  24. static uint32_t prvLen;
  25. static int32_t prvSPI_CB(void *pData, void *pParam)
  26. {
  27. uint32_t i;
  28. if (!memcmp(prvTxBuf, prvRxBuf, prvLen))
  29. {
  30. DBG("spi test ok");
  31. }
  32. for(i = 0; i < prvLen; i++)
  33. {
  34. if (prvTxBuf[i] != prvRxBuf[i])
  35. {
  36. DBG("spi test fail %u, %x, %x", i, prvTxBuf[i], prvRxBuf[i]);
  37. break;
  38. }
  39. }
  40. }
  41. void SPI_TestInit(uint8_t SpiID, uint32_t Speed, uint32_t TestLen)
  42. {
  43. GPIO_Iomux(GPIOC_12, 3);
  44. GPIO_Iomux(GPIOC_13, 3);
  45. GPIO_Iomux(GPIOB_14, 0);
  46. GPIO_Iomux(GPIOB_15, 0);
  47. SPI_MasterInit(SpiID, 8, SPI_MODE_0, Speed, prvSPI_CB, NULL);
  48. SPI_DMATxInit(SpiID, DMA1_STREAM_6, 0);
  49. SPI_DMARxInit(SpiID, DMA1_STREAM_7, 0);
  50. prvTxBuf = malloc(TestLen);
  51. prvRxBuf = malloc(TestLen);
  52. }
  53. void SPI_TestOnce(uint8_t SpiID, uint32_t TestLen)
  54. {
  55. uint32_t i;
  56. for(i = 0; i < TestLen; i++)
  57. {
  58. prvTxBuf[i] = i + 2;
  59. }
  60. memset(prvRxBuf, 0xff, TestLen);
  61. if (memcmp(prvTxBuf, prvRxBuf, TestLen))
  62. {
  63. DBG("spi test ready");
  64. }
  65. else
  66. {
  67. DBG("spi test not ready");
  68. }
  69. prvLen = TestLen;
  70. SPI_Transfer(SpiID, prvTxBuf, prvRxBuf, TestLen, 1);
  71. }