furi-hal-spi-config.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. FURI_CRITICAL_ENTER();
  70. LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_SPI1);
  71. LL_APB2_GRP1_ForceReset(LL_APB2_GRP1_PERIPH_SPI1);
  72. FURI_CRITICAL_EXIT();
  73. bus->current_handle = NULL;
  74. } else if (event == FuriHalSpiBusEventDeinit) {
  75. furi_check(osMutexDelete(furi_hal_spi_bus_r_mutex));
  76. } else if (event == FuriHalSpiBusEventLock) {
  77. furi_check(osMutexAcquire(furi_hal_spi_bus_r_mutex, osWaitForever) == osOK);
  78. } else if (event == FuriHalSpiBusEventUnlock) {
  79. furi_check(osMutexRelease(furi_hal_spi_bus_r_mutex) == osOK);
  80. } else if (event == FuriHalSpiBusEventActivate) {
  81. FURI_CRITICAL_ENTER();
  82. LL_APB2_GRP1_ReleaseReset(LL_APB2_GRP1_PERIPH_SPI1);
  83. FURI_CRITICAL_EXIT();
  84. } else if (event == FuriHalSpiBusEventDeactivate) {
  85. FURI_CRITICAL_ENTER();
  86. LL_APB2_GRP1_ForceReset(LL_APB2_GRP1_PERIPH_SPI1);
  87. FURI_CRITICAL_EXIT();
  88. }
  89. }
  90. FuriHalSpiBus furi_hal_spi_bus_r = {
  91. .spi=SPI1,
  92. .callback = furi_hal_spi_bus_r_event_callback,
  93. };
  94. osMutexId_t furi_hal_spi_bus_d_mutex = NULL;
  95. static void furi_hal_spi_bus_d_event_callback(FuriHalSpiBus* bus, FuriHalSpiBusEvent event) {
  96. if (event == FuriHalSpiBusEventInit) {
  97. furi_hal_spi_bus_d_mutex = osMutexNew(NULL);
  98. FURI_CRITICAL_ENTER();
  99. LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_SPI2);
  100. LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_SPI2);
  101. FURI_CRITICAL_EXIT();
  102. bus->current_handle = NULL;
  103. } else if (event == FuriHalSpiBusEventDeinit) {
  104. furi_check(osMutexDelete(furi_hal_spi_bus_d_mutex));
  105. } else if (event == FuriHalSpiBusEventLock) {
  106. furi_check(osMutexAcquire(furi_hal_spi_bus_d_mutex, osWaitForever) == osOK);
  107. } else if (event == FuriHalSpiBusEventUnlock) {
  108. furi_check(osMutexRelease(furi_hal_spi_bus_d_mutex) == osOK);
  109. } else if (event == FuriHalSpiBusEventActivate) {
  110. FURI_CRITICAL_ENTER();
  111. LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_SPI2);
  112. FURI_CRITICAL_EXIT();
  113. } else if (event == FuriHalSpiBusEventDeactivate) {
  114. FURI_CRITICAL_ENTER();
  115. LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_SPI2);
  116. FURI_CRITICAL_EXIT();
  117. }
  118. }
  119. FuriHalSpiBus furi_hal_spi_bus_d = {
  120. .spi=SPI2,
  121. .callback = furi_hal_spi_bus_d_event_callback,
  122. };
  123. /* SPI Bus Handles */
  124. inline static void furi_hal_spi_bus_r_handle_event_callback(FuriHalSpiBusHandle* handle, FuriHalSpiBusHandleEvent event, const LL_SPI_InitTypeDef* preset) {
  125. if (event == FuriHalSpiBusHandleEventInit) {
  126. hal_gpio_write(handle->cs, true);
  127. hal_gpio_init(handle->cs, GpioModeOutputPushPull, GpioPullNo, GpioSpeedVeryHigh);
  128. } else if (event == FuriHalSpiBusHandleEventDeinit) {
  129. hal_gpio_write(handle->cs, true);
  130. hal_gpio_init(handle->cs, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
  131. } else if (event == FuriHalSpiBusHandleEventActivate) {
  132. LL_SPI_Init(handle->bus->spi, (LL_SPI_InitTypeDef*)preset);
  133. LL_SPI_SetRxFIFOThreshold(handle->bus->spi, LL_SPI_RX_FIFO_TH_QUARTER);
  134. LL_SPI_Enable(handle->bus->spi);
  135. hal_gpio_init_ex(handle->miso, GpioModeAltFunctionPushPull, GpioPullNo, GpioSpeedVeryHigh, GpioAltFn5SPI1);
  136. hal_gpio_init_ex(handle->mosi, GpioModeAltFunctionPushPull, GpioPullNo, GpioSpeedVeryHigh, GpioAltFn5SPI1);
  137. hal_gpio_init_ex(handle->sck, GpioModeAltFunctionPushPull, GpioPullNo, GpioSpeedVeryHigh, GpioAltFn5SPI1);
  138. hal_gpio_write(handle->cs, false);
  139. } else if (event == FuriHalSpiBusHandleEventDeactivate) {
  140. hal_gpio_write(handle->cs, true);
  141. hal_gpio_init(handle->miso, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
  142. hal_gpio_init(handle->mosi, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
  143. hal_gpio_init(handle->sck, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
  144. LL_SPI_Disable(handle->bus->spi);
  145. }
  146. }
  147. static void furi_hal_spi_bus_handle_subghz_event_callback(FuriHalSpiBusHandle* handle, FuriHalSpiBusHandleEvent event) {
  148. furi_hal_spi_bus_r_handle_event_callback(handle, event, &furi_hal_spi_preset_1edge_low_8m);
  149. }
  150. FuriHalSpiBusHandle furi_hal_spi_bus_handle_subghz = {
  151. .bus=&furi_hal_spi_bus_r,
  152. .callback=furi_hal_spi_bus_handle_subghz_event_callback,
  153. .miso=&gpio_spi_r_miso,
  154. .mosi=&gpio_spi_r_mosi,
  155. .sck=&gpio_spi_r_sck,
  156. .cs=&gpio_subghz_cs,
  157. };
  158. static void furi_hal_spi_bus_handle_nfc_event_callback(FuriHalSpiBusHandle* handle, FuriHalSpiBusHandleEvent event) {
  159. furi_hal_spi_bus_r_handle_event_callback(handle, event, &furi_hal_spi_preset_2edge_low_8m);
  160. }
  161. FuriHalSpiBusHandle furi_hal_spi_bus_handle_nfc = {
  162. .bus=&furi_hal_spi_bus_r,
  163. .callback=furi_hal_spi_bus_handle_nfc_event_callback,
  164. .miso=&gpio_spi_r_miso,
  165. .mosi=&gpio_spi_r_mosi,
  166. .sck=&gpio_spi_r_sck,
  167. .cs=&gpio_nfc_cs,
  168. };
  169. static void furi_hal_spi_bus_handle_external_event_callback(FuriHalSpiBusHandle* handle, FuriHalSpiBusHandleEvent event) {
  170. furi_hal_spi_bus_r_handle_event_callback(handle, event, &furi_hal_spi_preset_1edge_low_2m);
  171. }
  172. FuriHalSpiBusHandle furi_hal_spi_bus_handle_external = {
  173. .bus=&furi_hal_spi_bus_r,
  174. .callback=furi_hal_spi_bus_handle_external_event_callback,
  175. .miso=&gpio_ext_pa6,
  176. .mosi=&gpio_ext_pa7,
  177. .sck=&gpio_ext_pb3,
  178. .cs=&gpio_ext_pa4,
  179. };
  180. inline static void furi_hal_spi_bus_d_handle_event_callback(FuriHalSpiBusHandle* handle, FuriHalSpiBusHandleEvent event, const LL_SPI_InitTypeDef* preset) {
  181. if (event == FuriHalSpiBusHandleEventInit) {
  182. hal_gpio_write(handle->cs, true);
  183. hal_gpio_init(handle->cs, GpioModeOutputPushPull, GpioPullUp, GpioSpeedVeryHigh);
  184. hal_gpio_init_ex(handle->miso, GpioModeAltFunctionPushPull, GpioPullNo, GpioSpeedVeryHigh, GpioAltFn5SPI2);
  185. hal_gpio_init_ex(handle->mosi, GpioModeAltFunctionPushPull, GpioPullNo, GpioSpeedVeryHigh, GpioAltFn5SPI2);
  186. hal_gpio_init_ex(handle->sck, GpioModeAltFunctionPushPull, GpioPullNo, GpioSpeedVeryHigh, GpioAltFn5SPI2);
  187. } else if (event == FuriHalSpiBusHandleEventDeinit) {
  188. hal_gpio_write(handle->cs, true);
  189. hal_gpio_init(handle->cs, GpioModeAnalog, GpioPullUp, GpioSpeedLow);
  190. } else if (event == FuriHalSpiBusHandleEventActivate) {
  191. LL_SPI_Init(handle->bus->spi, (LL_SPI_InitTypeDef*)preset);
  192. LL_SPI_SetRxFIFOThreshold(handle->bus->spi, LL_SPI_RX_FIFO_TH_QUARTER);
  193. LL_SPI_Enable(handle->bus->spi);
  194. hal_gpio_write(handle->cs, false);
  195. } else if (event == FuriHalSpiBusHandleEventDeactivate) {
  196. hal_gpio_write(handle->cs, true);
  197. LL_SPI_Disable(handle->bus->spi);
  198. }
  199. }
  200. static void furi_hal_spi_bus_handle_display_event_callback(FuriHalSpiBusHandle* handle, FuriHalSpiBusHandleEvent event) {
  201. furi_hal_spi_bus_d_handle_event_callback(handle, event, &furi_hal_spi_preset_1edge_low_4m);
  202. }
  203. FuriHalSpiBusHandle furi_hal_spi_bus_handle_display = {
  204. .bus=&furi_hal_spi_bus_d,
  205. .callback=furi_hal_spi_bus_handle_display_event_callback,
  206. .miso=&gpio_spi_d_miso,
  207. .mosi=&gpio_spi_d_mosi,
  208. .sck=&gpio_spi_d_sck,
  209. .cs=&gpio_display_cs,
  210. };
  211. static void furi_hal_spi_bus_handle_sd_fast_event_callback(FuriHalSpiBusHandle* handle, FuriHalSpiBusHandleEvent event) {
  212. furi_hal_spi_bus_d_handle_event_callback(handle, event, &furi_hal_spi_preset_1edge_low_16m);
  213. }
  214. FuriHalSpiBusHandle furi_hal_spi_bus_handle_sd_fast = {
  215. .bus=&furi_hal_spi_bus_d,
  216. .callback=furi_hal_spi_bus_handle_sd_fast_event_callback,
  217. .miso=&gpio_spi_d_miso,
  218. .mosi=&gpio_spi_d_mosi,
  219. .sck=&gpio_spi_d_sck,
  220. .cs=&gpio_sdcard_cs,
  221. };
  222. static void furi_hal_spi_bus_handle_sd_slow_event_callback(FuriHalSpiBusHandle* handle, FuriHalSpiBusHandleEvent event) {
  223. furi_hal_spi_bus_d_handle_event_callback(handle, event, &furi_hal_spi_preset_1edge_low_2m);
  224. }
  225. FuriHalSpiBusHandle furi_hal_spi_bus_handle_sd_slow = {
  226. .bus=&furi_hal_spi_bus_d,
  227. .callback=furi_hal_spi_bus_handle_sd_slow_event_callback,
  228. .miso=&gpio_spi_d_miso,
  229. .mosi=&gpio_spi_d_mosi,
  230. .sck=&gpio_spi_d_sck,
  231. .cs=&gpio_sdcard_cs,
  232. };