furi_hal_spi_config.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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(
  125. FuriHalSpiBusHandle* handle,
  126. FuriHalSpiBusHandleEvent event,
  127. const LL_SPI_InitTypeDef* preset) {
  128. if(event == FuriHalSpiBusHandleEventInit) {
  129. hal_gpio_write(handle->cs, true);
  130. hal_gpio_init(handle->cs, GpioModeOutputPushPull, GpioPullNo, GpioSpeedVeryHigh);
  131. } else if(event == FuriHalSpiBusHandleEventDeinit) {
  132. hal_gpio_write(handle->cs, true);
  133. hal_gpio_init(handle->cs, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
  134. } else if(event == FuriHalSpiBusHandleEventActivate) {
  135. LL_SPI_Init(handle->bus->spi, (LL_SPI_InitTypeDef*)preset);
  136. LL_SPI_SetRxFIFOThreshold(handle->bus->spi, LL_SPI_RX_FIFO_TH_QUARTER);
  137. LL_SPI_Enable(handle->bus->spi);
  138. hal_gpio_init_ex(
  139. handle->miso,
  140. GpioModeAltFunctionPushPull,
  141. GpioPullNo,
  142. GpioSpeedVeryHigh,
  143. GpioAltFn5SPI1);
  144. hal_gpio_init_ex(
  145. handle->mosi,
  146. GpioModeAltFunctionPushPull,
  147. GpioPullNo,
  148. GpioSpeedVeryHigh,
  149. GpioAltFn5SPI1);
  150. hal_gpio_init_ex(
  151. handle->sck,
  152. GpioModeAltFunctionPushPull,
  153. GpioPullNo,
  154. GpioSpeedVeryHigh,
  155. GpioAltFn5SPI1);
  156. hal_gpio_write(handle->cs, false);
  157. } else if(event == FuriHalSpiBusHandleEventDeactivate) {
  158. hal_gpio_write(handle->cs, true);
  159. hal_gpio_init(handle->miso, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
  160. hal_gpio_init(handle->mosi, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
  161. hal_gpio_init(handle->sck, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
  162. LL_SPI_Disable(handle->bus->spi);
  163. }
  164. }
  165. static void furi_hal_spi_bus_handle_subghz_event_callback(
  166. FuriHalSpiBusHandle* handle,
  167. FuriHalSpiBusHandleEvent event) {
  168. furi_hal_spi_bus_r_handle_event_callback(handle, event, &furi_hal_spi_preset_1edge_low_8m);
  169. }
  170. FuriHalSpiBusHandle furi_hal_spi_bus_handle_subghz = {
  171. .bus = &furi_hal_spi_bus_r,
  172. .callback = furi_hal_spi_bus_handle_subghz_event_callback,
  173. .miso = &gpio_spi_r_miso,
  174. .mosi = &gpio_spi_r_mosi,
  175. .sck = &gpio_spi_r_sck,
  176. .cs = &gpio_subghz_cs,
  177. };
  178. static void furi_hal_spi_bus_handle_nfc_event_callback(
  179. FuriHalSpiBusHandle* handle,
  180. FuriHalSpiBusHandleEvent event) {
  181. furi_hal_spi_bus_r_handle_event_callback(handle, event, &furi_hal_spi_preset_2edge_low_8m);
  182. }
  183. FuriHalSpiBusHandle furi_hal_spi_bus_handle_nfc = {
  184. .bus = &furi_hal_spi_bus_r,
  185. .callback = furi_hal_spi_bus_handle_nfc_event_callback,
  186. .miso = &gpio_spi_r_miso,
  187. .mosi = &gpio_spi_r_mosi,
  188. .sck = &gpio_spi_r_sck,
  189. .cs = &gpio_nfc_cs,
  190. };
  191. static void furi_hal_spi_bus_handle_external_event_callback(
  192. FuriHalSpiBusHandle* handle,
  193. FuriHalSpiBusHandleEvent event) {
  194. furi_hal_spi_bus_r_handle_event_callback(handle, event, &furi_hal_spi_preset_1edge_low_2m);
  195. }
  196. FuriHalSpiBusHandle furi_hal_spi_bus_handle_external = {
  197. .bus = &furi_hal_spi_bus_r,
  198. .callback = furi_hal_spi_bus_handle_external_event_callback,
  199. .miso = &gpio_ext_pa6,
  200. .mosi = &gpio_ext_pa7,
  201. .sck = &gpio_ext_pb3,
  202. .cs = &gpio_ext_pa4,
  203. };
  204. inline static void furi_hal_spi_bus_d_handle_event_callback(
  205. FuriHalSpiBusHandle* handle,
  206. FuriHalSpiBusHandleEvent event,
  207. const LL_SPI_InitTypeDef* preset) {
  208. if(event == FuriHalSpiBusHandleEventInit) {
  209. hal_gpio_write(handle->cs, true);
  210. hal_gpio_init(handle->cs, GpioModeOutputPushPull, GpioPullUp, GpioSpeedVeryHigh);
  211. hal_gpio_init_ex(
  212. handle->miso,
  213. GpioModeAltFunctionPushPull,
  214. GpioPullNo,
  215. GpioSpeedVeryHigh,
  216. GpioAltFn5SPI2);
  217. hal_gpio_init_ex(
  218. handle->mosi,
  219. GpioModeAltFunctionPushPull,
  220. GpioPullNo,
  221. GpioSpeedVeryHigh,
  222. GpioAltFn5SPI2);
  223. hal_gpio_init_ex(
  224. handle->sck,
  225. GpioModeAltFunctionPushPull,
  226. GpioPullNo,
  227. GpioSpeedVeryHigh,
  228. GpioAltFn5SPI2);
  229. } else if(event == FuriHalSpiBusHandleEventDeinit) {
  230. hal_gpio_write(handle->cs, true);
  231. hal_gpio_init(handle->cs, GpioModeAnalog, GpioPullUp, GpioSpeedLow);
  232. } else if(event == FuriHalSpiBusHandleEventActivate) {
  233. LL_SPI_Init(handle->bus->spi, (LL_SPI_InitTypeDef*)preset);
  234. LL_SPI_SetRxFIFOThreshold(handle->bus->spi, LL_SPI_RX_FIFO_TH_QUARTER);
  235. LL_SPI_Enable(handle->bus->spi);
  236. hal_gpio_write(handle->cs, false);
  237. } else if(event == FuriHalSpiBusHandleEventDeactivate) {
  238. hal_gpio_write(handle->cs, true);
  239. LL_SPI_Disable(handle->bus->spi);
  240. }
  241. }
  242. static void furi_hal_spi_bus_handle_display_event_callback(
  243. FuriHalSpiBusHandle* handle,
  244. FuriHalSpiBusHandleEvent event) {
  245. furi_hal_spi_bus_d_handle_event_callback(handle, event, &furi_hal_spi_preset_1edge_low_4m);
  246. }
  247. FuriHalSpiBusHandle furi_hal_spi_bus_handle_display = {
  248. .bus = &furi_hal_spi_bus_d,
  249. .callback = furi_hal_spi_bus_handle_display_event_callback,
  250. .miso = &gpio_spi_d_miso,
  251. .mosi = &gpio_spi_d_mosi,
  252. .sck = &gpio_spi_d_sck,
  253. .cs = &gpio_display_cs,
  254. };
  255. static void furi_hal_spi_bus_handle_sd_fast_event_callback(
  256. FuriHalSpiBusHandle* handle,
  257. FuriHalSpiBusHandleEvent event) {
  258. furi_hal_spi_bus_d_handle_event_callback(handle, event, &furi_hal_spi_preset_1edge_low_16m);
  259. }
  260. FuriHalSpiBusHandle furi_hal_spi_bus_handle_sd_fast = {
  261. .bus = &furi_hal_spi_bus_d,
  262. .callback = furi_hal_spi_bus_handle_sd_fast_event_callback,
  263. .miso = &gpio_spi_d_miso,
  264. .mosi = &gpio_spi_d_mosi,
  265. .sck = &gpio_spi_d_sck,
  266. .cs = &gpio_sdcard_cs,
  267. };
  268. static void furi_hal_spi_bus_handle_sd_slow_event_callback(
  269. FuriHalSpiBusHandle* handle,
  270. FuriHalSpiBusHandleEvent event) {
  271. furi_hal_spi_bus_d_handle_event_callback(handle, event, &furi_hal_spi_preset_1edge_low_2m);
  272. }
  273. FuriHalSpiBusHandle furi_hal_spi_bus_handle_sd_slow = {
  274. .bus = &furi_hal_spi_bus_d,
  275. .callback = furi_hal_spi_bus_handle_sd_slow_event_callback,
  276. .miso = &gpio_spi_d_miso,
  277. .mosi = &gpio_spi_d_mosi,
  278. .sck = &gpio_spi_d_sck,
  279. .cs = &gpio_sdcard_cs,
  280. };