furi-hal-spi-config.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. #include <furi-hal-spi-config.h>
  2. #include <furi-hal-resources.h>
  3. /* SPI Presets */
  4. const LL_SPI_InitTypeDef furi_hal_spi_preset_2edge_low_8m = {
  5. .Mode = LL_SPI_MODE_MASTER,
  6. .TransferDirection = LL_SPI_FULL_DUPLEX,
  7. .DataWidth = LL_SPI_DATAWIDTH_8BIT,
  8. .ClockPolarity = LL_SPI_POLARITY_LOW,
  9. .ClockPhase = LL_SPI_PHASE_2EDGE,
  10. .NSS = LL_SPI_NSS_SOFT,
  11. .BaudRate = LL_SPI_BAUDRATEPRESCALER_DIV8,
  12. .BitOrder = LL_SPI_MSB_FIRST,
  13. .CRCCalculation = LL_SPI_CRCCALCULATION_DISABLE,
  14. .CRCPoly = 7,
  15. };
  16. const LL_SPI_InitTypeDef furi_hal_spi_preset_1edge_low_8m = {
  17. .Mode = LL_SPI_MODE_MASTER,
  18. .TransferDirection = LL_SPI_FULL_DUPLEX,
  19. .DataWidth = LL_SPI_DATAWIDTH_8BIT,
  20. .ClockPolarity = LL_SPI_POLARITY_LOW,
  21. .ClockPhase = LL_SPI_PHASE_1EDGE,
  22. .NSS = LL_SPI_NSS_SOFT,
  23. .BaudRate = LL_SPI_BAUDRATEPRESCALER_DIV8,
  24. .BitOrder = LL_SPI_MSB_FIRST,
  25. .CRCCalculation = LL_SPI_CRCCALCULATION_DISABLE,
  26. .CRCPoly = 7,
  27. };
  28. const LL_SPI_InitTypeDef furi_hal_spi_preset_1edge_low_4m = {
  29. .Mode = LL_SPI_MODE_MASTER,
  30. .TransferDirection = LL_SPI_FULL_DUPLEX,
  31. .DataWidth = LL_SPI_DATAWIDTH_8BIT,
  32. .ClockPolarity = LL_SPI_POLARITY_LOW,
  33. .ClockPhase = LL_SPI_PHASE_1EDGE,
  34. .NSS = LL_SPI_NSS_SOFT,
  35. .BaudRate = LL_SPI_BAUDRATEPRESCALER_DIV16,
  36. .BitOrder = LL_SPI_MSB_FIRST,
  37. .CRCCalculation = LL_SPI_CRCCALCULATION_DISABLE,
  38. .CRCPoly = 7,
  39. };
  40. const LL_SPI_InitTypeDef furi_hal_spi_preset_1edge_low_16m = {
  41. .Mode = LL_SPI_MODE_MASTER,
  42. .TransferDirection = LL_SPI_FULL_DUPLEX,
  43. .DataWidth = LL_SPI_DATAWIDTH_8BIT,
  44. .ClockPolarity = LL_SPI_POLARITY_LOW,
  45. .ClockPhase = LL_SPI_PHASE_1EDGE,
  46. .NSS = LL_SPI_NSS_SOFT,
  47. .BaudRate = LL_SPI_BAUDRATEPRESCALER_DIV2,
  48. .BitOrder = LL_SPI_MSB_FIRST,
  49. .CRCCalculation = LL_SPI_CRCCALCULATION_DISABLE,
  50. .CRCPoly = 7,
  51. };
  52. const LL_SPI_InitTypeDef furi_hal_spi_preset_1edge_low_2m = {
  53. .Mode = LL_SPI_MODE_MASTER,
  54. .TransferDirection = LL_SPI_FULL_DUPLEX,
  55. .DataWidth = LL_SPI_DATAWIDTH_8BIT,
  56. .ClockPolarity = LL_SPI_POLARITY_LOW,
  57. .ClockPhase = LL_SPI_PHASE_1EDGE,
  58. .NSS = LL_SPI_NSS_SOFT,
  59. .BaudRate = LL_SPI_BAUDRATEPRESCALER_DIV32,
  60. .BitOrder = LL_SPI_MSB_FIRST,
  61. .CRCCalculation = LL_SPI_CRCCALCULATION_DISABLE,
  62. .CRCPoly = 7,
  63. };
  64. /* SPI Buses */
  65. osMutexId_t furi_hal_spi_bus_r_mutex = NULL;
  66. static void furi_hal_spi_bus_r_event_callback(FuriHalSpiBus* bus, FuriHalSpiBusEvent event) {
  67. if (event == FuriHalSpiBusEventInit) {
  68. furi_hal_spi_bus_r_mutex = osMutexNew(NULL);
  69. LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_SPI1);
  70. LL_APB2_GRP1_ForceReset(LL_APB2_GRP1_PERIPH_SPI1);
  71. bus->current_handle = NULL;
  72. } else if (event == FuriHalSpiBusEventDeinit) {
  73. furi_check(osMutexDelete(furi_hal_spi_bus_r_mutex));
  74. } else if (event == FuriHalSpiBusEventLock) {
  75. furi_check(osMutexAcquire(furi_hal_spi_bus_r_mutex, osWaitForever) == osOK);
  76. } else if (event == FuriHalSpiBusEventUnlock) {
  77. furi_check(osMutexRelease(furi_hal_spi_bus_r_mutex) == osOK);
  78. } else if (event == FuriHalSpiBusEventActivate) {
  79. LL_APB2_GRP1_ReleaseReset(LL_APB2_GRP1_PERIPH_SPI1);
  80. } else if (event == FuriHalSpiBusEventDeactivate) {
  81. LL_APB2_GRP1_ForceReset(LL_APB2_GRP1_PERIPH_SPI1);
  82. }
  83. }
  84. FuriHalSpiBus furi_hal_spi_bus_r = {
  85. .spi=SPI1,
  86. .callback = furi_hal_spi_bus_r_event_callback,
  87. };
  88. osMutexId_t furi_hal_spi_bus_d_mutex = NULL;
  89. static void furi_hal_spi_bus_d_event_callback(FuriHalSpiBus* bus, FuriHalSpiBusEvent event) {
  90. if (event == FuriHalSpiBusEventInit) {
  91. furi_hal_spi_bus_d_mutex = osMutexNew(NULL);
  92. LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_SPI2);
  93. LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_SPI2);
  94. bus->current_handle = NULL;
  95. } else if (event == FuriHalSpiBusEventDeinit) {
  96. furi_check(osMutexDelete(furi_hal_spi_bus_d_mutex));
  97. } else if (event == FuriHalSpiBusEventLock) {
  98. furi_check(osMutexAcquire(furi_hal_spi_bus_d_mutex, osWaitForever) == osOK);
  99. } else if (event == FuriHalSpiBusEventUnlock) {
  100. furi_check(osMutexRelease(furi_hal_spi_bus_d_mutex) == osOK);
  101. } else if (event == FuriHalSpiBusEventActivate) {
  102. LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_SPI2);
  103. } else if (event == FuriHalSpiBusEventDeactivate) {
  104. LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_SPI2);
  105. }
  106. }
  107. FuriHalSpiBus furi_hal_spi_bus_d = {
  108. .spi=SPI2,
  109. .callback = furi_hal_spi_bus_d_event_callback,
  110. };
  111. /* SPI Bus Handles */
  112. inline static void furi_hal_spi_bus_r_handle_event_callback(FuriHalSpiBusHandle* handle, FuriHalSpiBusHandleEvent event, const LL_SPI_InitTypeDef* preset) {
  113. if (event == FuriHalSpiBusHandleEventInit) {
  114. hal_gpio_write(handle->cs, true);
  115. hal_gpio_init(handle->cs, GpioModeOutputPushPull, GpioPullNo, GpioSpeedVeryHigh);
  116. } else if (event == FuriHalSpiBusHandleEventDeinit) {
  117. hal_gpio_write(handle->cs, true);
  118. hal_gpio_init(handle->cs, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
  119. } else if (event == FuriHalSpiBusHandleEventActivate) {
  120. LL_SPI_Init(handle->bus->spi, (LL_SPI_InitTypeDef*)preset);
  121. LL_SPI_SetRxFIFOThreshold(handle->bus->spi, LL_SPI_RX_FIFO_TH_QUARTER);
  122. LL_SPI_Enable(handle->bus->spi);
  123. hal_gpio_init_ex(handle->miso, GpioModeAltFunctionPushPull, GpioPullNo, GpioSpeedVeryHigh, GpioAltFn5SPI1);
  124. hal_gpio_init_ex(handle->mosi, GpioModeAltFunctionPushPull, GpioPullNo, GpioSpeedVeryHigh, GpioAltFn5SPI1);
  125. hal_gpio_init_ex(handle->sck, GpioModeAltFunctionPushPull, GpioPullNo, GpioSpeedVeryHigh, GpioAltFn5SPI1);
  126. hal_gpio_write(handle->cs, false);
  127. } else if (event == FuriHalSpiBusHandleEventDeactivate) {
  128. hal_gpio_write(handle->cs, true);
  129. hal_gpio_init(handle->miso, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
  130. hal_gpio_init(handle->mosi, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
  131. hal_gpio_init(handle->sck, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
  132. LL_SPI_Disable(handle->bus->spi);
  133. }
  134. }
  135. static void furi_hal_spi_bus_handle_subghz_event_callback(FuriHalSpiBusHandle* handle, FuriHalSpiBusHandleEvent event) {
  136. furi_hal_spi_bus_r_handle_event_callback(handle, event, &furi_hal_spi_preset_1edge_low_8m);
  137. }
  138. FuriHalSpiBusHandle furi_hal_spi_bus_handle_subghz = {
  139. .bus=&furi_hal_spi_bus_r,
  140. .callback=furi_hal_spi_bus_handle_subghz_event_callback,
  141. .miso=&gpio_spi_r_miso,
  142. .mosi=&gpio_spi_r_mosi,
  143. .sck=&gpio_spi_r_sck,
  144. .cs=&gpio_subghz_cs,
  145. };
  146. static void furi_hal_spi_bus_handle_nfc_event_callback(FuriHalSpiBusHandle* handle, FuriHalSpiBusHandleEvent event) {
  147. furi_hal_spi_bus_r_handle_event_callback(handle, event, &furi_hal_spi_preset_2edge_low_8m);
  148. }
  149. FuriHalSpiBusHandle furi_hal_spi_bus_handle_nfc = {
  150. .bus=&furi_hal_spi_bus_r,
  151. .callback=furi_hal_spi_bus_handle_nfc_event_callback,
  152. .miso=&gpio_spi_r_miso,
  153. .mosi=&gpio_spi_r_mosi,
  154. .sck=&gpio_spi_r_sck,
  155. .cs=&gpio_nfc_cs,
  156. };
  157. static void furi_hal_spi_bus_handle_external_event_callback(FuriHalSpiBusHandle* handle, FuriHalSpiBusHandleEvent event) {
  158. furi_hal_spi_bus_r_handle_event_callback(handle, event, &furi_hal_spi_preset_1edge_low_2m);
  159. }
  160. FuriHalSpiBusHandle furi_hal_spi_bus_handle_external = {
  161. .bus=&furi_hal_spi_bus_r,
  162. .callback=furi_hal_spi_bus_handle_external_event_callback,
  163. .miso=&gpio_ext_pa6,
  164. .mosi=&gpio_ext_pa7,
  165. .sck=&gpio_ext_pb3,
  166. .cs=&gpio_ext_pa4,
  167. };
  168. inline static void furi_hal_spi_bus_d_handle_event_callback(FuriHalSpiBusHandle* handle, FuriHalSpiBusHandleEvent event, const LL_SPI_InitTypeDef* preset) {
  169. if (event == FuriHalSpiBusHandleEventInit) {
  170. hal_gpio_write(handle->cs, true);
  171. hal_gpio_init(handle->cs, GpioModeOutputPushPull, GpioPullUp, GpioSpeedVeryHigh);
  172. hal_gpio_init_ex(handle->miso, GpioModeAltFunctionPushPull, GpioPullNo, GpioSpeedVeryHigh, GpioAltFn5SPI2);
  173. hal_gpio_init_ex(handle->mosi, GpioModeAltFunctionPushPull, GpioPullNo, GpioSpeedVeryHigh, GpioAltFn5SPI2);
  174. hal_gpio_init_ex(handle->sck, GpioModeAltFunctionPushPull, GpioPullNo, GpioSpeedVeryHigh, GpioAltFn5SPI2);
  175. } else if (event == FuriHalSpiBusHandleEventDeinit) {
  176. hal_gpio_write(handle->cs, true);
  177. hal_gpio_init(handle->cs, GpioModeAnalog, GpioPullUp, GpioSpeedLow);
  178. } else if (event == FuriHalSpiBusHandleEventActivate) {
  179. LL_SPI_Init(handle->bus->spi, (LL_SPI_InitTypeDef*)preset);
  180. LL_SPI_SetRxFIFOThreshold(handle->bus->spi, LL_SPI_RX_FIFO_TH_QUARTER);
  181. LL_SPI_Enable(handle->bus->spi);
  182. hal_gpio_write(handle->cs, false);
  183. } else if (event == FuriHalSpiBusHandleEventDeactivate) {
  184. hal_gpio_write(handle->cs, true);
  185. LL_SPI_Disable(handle->bus->spi);
  186. }
  187. }
  188. static void furi_hal_spi_bus_handle_display_event_callback(FuriHalSpiBusHandle* handle, FuriHalSpiBusHandleEvent event) {
  189. furi_hal_spi_bus_d_handle_event_callback(handle, event, &furi_hal_spi_preset_1edge_low_4m);
  190. }
  191. FuriHalSpiBusHandle furi_hal_spi_bus_handle_display = {
  192. .bus=&furi_hal_spi_bus_d,
  193. .callback=furi_hal_spi_bus_handle_display_event_callback,
  194. .miso=&gpio_spi_d_miso,
  195. .mosi=&gpio_spi_d_mosi,
  196. .sck=&gpio_spi_d_sck,
  197. .cs=&gpio_display_cs,
  198. };
  199. static void furi_hal_spi_bus_handle_sd_fast_event_callback(FuriHalSpiBusHandle* handle, FuriHalSpiBusHandleEvent event) {
  200. furi_hal_spi_bus_d_handle_event_callback(handle, event, &furi_hal_spi_preset_1edge_low_16m);
  201. }
  202. FuriHalSpiBusHandle furi_hal_spi_bus_handle_sd_fast = {
  203. .bus=&furi_hal_spi_bus_d,
  204. .callback=furi_hal_spi_bus_handle_sd_fast_event_callback,
  205. .miso=&gpio_spi_d_miso,
  206. .mosi=&gpio_spi_d_mosi,
  207. .sck=&gpio_spi_d_sck,
  208. .cs=&gpio_sdcard_cs,
  209. };
  210. static void furi_hal_spi_bus_handle_sd_slow_event_callback(FuriHalSpiBusHandle* handle, FuriHalSpiBusHandleEvent event) {
  211. furi_hal_spi_bus_d_handle_event_callback(handle, event, &furi_hal_spi_preset_1edge_low_2m);
  212. }
  213. FuriHalSpiBusHandle furi_hal_spi_bus_handle_sd_slow = {
  214. .bus=&furi_hal_spi_bus_d,
  215. .callback=furi_hal_spi_bus_handle_sd_slow_event_callback,
  216. .miso=&gpio_spi_d_miso,
  217. .mosi=&gpio_spi_d_mosi,
  218. .sck=&gpio_spi_d_sck,
  219. .cs=&gpio_sdcard_cs,
  220. };