furi_hal_spi_config.c 11 KB

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