furi_hal_spi_config.c 11 KB

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