lt8920_trs.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #include "stm32l0xx_hal.h"
  2. #include "lt8920_trs.h"
  3. #include "stm32l0xx_nucleo.h"
  4. #include <stdio.h>
  5. SPI_HandleTypeDef SpiHandle;
  6. //
  7. void lt_init(void)
  8. {
  9. lt_gpio_init();
  10. lt_spi_gpio_init();
  11. lt_spi_init();
  12. }
  13. //
  14. void lt_spi_init(void)
  15. {
  16. __HAL_RCC_SPI2_CLK_ENABLE();
  17. SpiHandle.Instance = SPI2;
  18. SpiHandle.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256;
  19. SpiHandle.Init.Direction = SPI_DIRECTION_2LINES;
  20. SpiHandle.Init.CLKPhase = SPI_PHASE_1EDGE;
  21. SpiHandle.Init.CLKPolarity = SPI_POLARITY_HIGH;
  22. SpiHandle.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  23. SpiHandle.Init.CRCPolynomial = 7;
  24. SpiHandle.Init.DataSize = SPI_DATASIZE_8BIT;
  25. SpiHandle.Init.FirstBit = SPI_FIRSTBIT_MSB;
  26. SpiHandle.Init.NSS = SPI_NSS_SOFT;
  27. SpiHandle.Init.TIMode = SPI_TIMODE_DISABLE;
  28. SpiHandle.Init.Mode = SPI_MODE_MASTER;
  29. HAL_SPI_Init(&SpiHandle);
  30. }
  31. //
  32. void lt_gpio_init(void)
  33. {
  34. GPIO_InitTypeDef GPIO_InitStruct;
  35. __HAL_RCC_GPIOB_CLK_ENABLE();
  36. // LT8920 Reset pin PB2
  37. GPIO_InitStruct.Pin = GPIO_PIN_2;
  38. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  39. GPIO_InitStruct.Pull = GPIO_PULLUP;
  40. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH ;
  41. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  42. // LT8920 PKT pin PB12
  43. GPIO_InitStruct.Pin = GPIO_PIN_12;
  44. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  45. GPIO_InitStruct.Pull = GPIO_PULLUP;
  46. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH ;
  47. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  48. }
  49. //
  50. void lt_spi_gpio_init(void)
  51. {
  52. GPIO_InitTypeDef GPIO_InitStruct;
  53. __HAL_RCC_GPIOB_CLK_ENABLE();
  54. // SPI SCK PB_13
  55. GPIO_InitStruct.Pin = GPIO_PIN_13;
  56. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  57. GPIO_InitStruct.Pull = GPIO_PULLUP;
  58. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH ;
  59. GPIO_InitStruct.Alternate = GPIO_AF0_SPI2;
  60. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  61. // SPI MISO PB14
  62. GPIO_InitStruct.Pin = GPIO_PIN_14;
  63. GPIO_InitStruct.Alternate = GPIO_AF0_SPI2;
  64. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  65. // SPI MOSI PB15
  66. GPIO_InitStruct.Pin = GPIO_PIN_15;
  67. GPIO_InitStruct.Alternate = GPIO_AF0_SPI2;
  68. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  69. // SPI CS PB1
  70. GPIO_InitStruct.Pin = GPIO_PIN_1;
  71. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  72. GPIO_InitStruct.Pull = GPIO_PULLUP;
  73. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH ;
  74. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  75. }
  76. // -------------------------------------------------------------------------- //
  77. //
  78. void lt_spi_test(void)
  79. {
  80. uint8_t tx_buf[12] = "hello world";
  81. uint8_t rx_buf[12] = {0};
  82. if (HAL_SPI_TransmitReceive(&SpiHandle, tx_buf, rx_buf, 11, 1000) != HAL_OK)
  83. {
  84. printf("[SPI] tx/rx error\r\n");
  85. BSP_LED_On(LED2);
  86. }
  87. else
  88. {
  89. printf("[SPI] rx: %s\r\n", rx_buf);
  90. printf("[SPI] tx/rx OK\r\n");
  91. BSP_LED_Toggle(LED2);
  92. }
  93. }