furi_hal_spi_config.c 9.9 KB

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