furi_hal_resources.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. #include <furi_hal_resources.h>
  2. #include <furi.h>
  3. #include <stm32wbxx_ll_rcc.h>
  4. #include <stm32wbxx_ll_pwr.h>
  5. #define TAG "FuriHalResources"
  6. const GpioPin gpio_swdio = {.port = GPIOA, .pin = LL_GPIO_PIN_13};
  7. const GpioPin gpio_swclk = {.port = GPIOA, .pin = LL_GPIO_PIN_14};
  8. const GpioPin gpio_vibro = {.port = GPIOA, .pin = LL_GPIO_PIN_8};
  9. const GpioPin gpio_ibutton = {.port = GPIOB, .pin = LL_GPIO_PIN_14};
  10. const GpioPin gpio_display_cs = {.port = GPIOC, .pin = LL_GPIO_PIN_11};
  11. const GpioPin gpio_display_rst_n = {.port = GPIOB, .pin = LL_GPIO_PIN_0};
  12. const GpioPin gpio_display_di = {.port = GPIOB, .pin = LL_GPIO_PIN_1};
  13. const GpioPin gpio_sdcard_cs = {.port = GPIOC, .pin = LL_GPIO_PIN_12};
  14. const GpioPin gpio_sdcard_cd = {.port = GPIOC, .pin = LL_GPIO_PIN_10};
  15. const GpioPin gpio_button_up = {.port = GPIOB, .pin = LL_GPIO_PIN_10};
  16. const GpioPin gpio_button_down = {.port = GPIOC, .pin = LL_GPIO_PIN_6};
  17. const GpioPin gpio_button_right = {.port = GPIOB, .pin = LL_GPIO_PIN_12};
  18. const GpioPin gpio_button_left = {.port = GPIOB, .pin = LL_GPIO_PIN_11};
  19. const GpioPin gpio_button_ok = {.port = GPIOH, .pin = LL_GPIO_PIN_3};
  20. const GpioPin gpio_button_back = {.port = GPIOC, .pin = LL_GPIO_PIN_13};
  21. const GpioPin gpio_spi_d_miso = {.port = GPIOC, .pin = LL_GPIO_PIN_2};
  22. const GpioPin gpio_spi_d_mosi = {.port = GPIOB, .pin = LL_GPIO_PIN_15};
  23. const GpioPin gpio_spi_d_sck = {.port = GPIOD, .pin = LL_GPIO_PIN_1};
  24. const GpioPin gpio_ext_pc0 = {.port = GPIOC, .pin = LL_GPIO_PIN_0};
  25. const GpioPin gpio_ext_pc1 = {.port = GPIOC, .pin = LL_GPIO_PIN_1};
  26. const GpioPin gpio_ext_pc3 = {.port = GPIOC, .pin = LL_GPIO_PIN_3};
  27. const GpioPin gpio_ext_pb2 = {.port = GPIOB, .pin = LL_GPIO_PIN_2};
  28. const GpioPin gpio_ext_pb3 = {.port = GPIOB, .pin = LL_GPIO_PIN_3};
  29. const GpioPin gpio_ext_pa4 = {.port = GPIOA, .pin = LL_GPIO_PIN_4};
  30. const GpioPin gpio_ext_pa6 = {.port = GPIOA, .pin = LL_GPIO_PIN_6};
  31. const GpioPin gpio_ext_pa7 = {.port = GPIOA, .pin = LL_GPIO_PIN_7};
  32. const GpioPin gpio_ext_pc5 = {.port = GPIOC, .pin = LL_GPIO_PIN_5};
  33. const GpioPin gpio_ext_pc4 = {.port = GPIOC, .pin = LL_GPIO_PIN_4};
  34. const GpioPin gpio_ext_pa5 = {.port = GPIOA, .pin = LL_GPIO_PIN_5};
  35. const GpioPin gpio_ext_pb9 = {.port = GPIOB, .pin = LL_GPIO_PIN_9};
  36. const GpioPin gpio_ext_pa0 = {.port = GPIOA, .pin = LL_GPIO_PIN_0};
  37. const GpioPin gpio_ext_pa1 = {.port = GPIOA, .pin = LL_GPIO_PIN_1};
  38. const GpioPin gpio_ext_pa15 = {.port = GPIOA, .pin = LL_GPIO_PIN_15};
  39. const GpioPin gpio_ext_pe4 = {.port = GPIOE, .pin = LL_GPIO_PIN_4};
  40. const GpioPin gpio_ext_pa2 = {.port = GPIOA, .pin = LL_GPIO_PIN_2};
  41. const GpioPin gpio_ext_pb4 = {.port = GPIOB, .pin = LL_GPIO_PIN_4};
  42. const GpioPin gpio_ext_pb5 = {.port = GPIOB, .pin = LL_GPIO_PIN_5};
  43. const GpioPin gpio_ext_pd0 = {.port = GPIOD, .pin = LL_GPIO_PIN_0};
  44. const GpioPin gpio_ext_pb13 = {.port = GPIOB, .pin = LL_GPIO_PIN_13};
  45. const GpioPin gpio_usart_tx = {.port = GPIOB, .pin = LL_GPIO_PIN_6};
  46. const GpioPin gpio_usart_rx = {.port = GPIOB, .pin = LL_GPIO_PIN_7};
  47. const GpioPin gpio_i2c_power_sda = {.port = GPIOA, .pin = LL_GPIO_PIN_10};
  48. const GpioPin gpio_i2c_power_scl = {.port = GPIOA, .pin = LL_GPIO_PIN_9};
  49. const GpioPin gpio_speaker = {.port = GPIOB, .pin = LL_GPIO_PIN_8};
  50. const GpioPin gpio_periph_power = {.port = GPIOA, .pin = LL_GPIO_PIN_3};
  51. const GpioPin gpio_usb_dm = {.port = GPIOA, .pin = LL_GPIO_PIN_11};
  52. const GpioPin gpio_usb_dp = {.port = GPIOA, .pin = LL_GPIO_PIN_12};
  53. const GpioPinRecord gpio_pins[] = {
  54. {.pin = &gpio_ext_pa7, .name = "PA7", .debug = false},
  55. {.pin = &gpio_ext_pa6, .name = "PA6", .debug = false},
  56. {.pin = &gpio_ext_pa4, .name = "PA4", .debug = false},
  57. {.pin = &gpio_ext_pb3, .name = "PB3", .debug = false},
  58. {.pin = &gpio_ext_pb2, .name = "PB2", .debug = false},
  59. {.pin = &gpio_ext_pc3, .name = "PC3", .debug = false},
  60. {.pin = &gpio_ext_pc1, .name = "PC1", .debug = false},
  61. {.pin = &gpio_ext_pc0, .name = "PC0", .debug = false},
  62. {.pin = &gpio_ext_pc5, .name = "PC5", .debug = false},
  63. {.pin = &gpio_ext_pc4, .name = "PC4", .debug = false},
  64. {.pin = &gpio_ext_pa5, .name = "PA5", .debug = false},
  65. {.pin = &gpio_ext_pb9, .name = "PB9", .debug = false},
  66. {.pin = &gpio_ext_pa0, .name = "PA0", .debug = false},
  67. {.pin = &gpio_ext_pa1, .name = "PA1", .debug = false},
  68. {.pin = &gpio_ext_pa15, .name = "PA15", .debug = false},
  69. {.pin = &gpio_ext_pe4, .name = "PE4", .debug = false},
  70. {.pin = &gpio_ext_pa2, .name = "PA2", .debug = false},
  71. {.pin = &gpio_ext_pb4, .name = "PB4", .debug = false},
  72. {.pin = &gpio_ext_pb5, .name = "PB5", .debug = false},
  73. {.pin = &gpio_ext_pd0, .name = "PD0", .debug = false},
  74. {.pin = &gpio_ext_pb13, .name = "PB13", .debug = false},
  75. /* Dangerous pins, may damage hardware */
  76. {.pin = &gpio_usart_rx, .name = "PB7", .debug = true},
  77. {.pin = &gpio_speaker, .name = "PB8", .debug = true},
  78. };
  79. const size_t gpio_pins_count = sizeof(gpio_pins) / sizeof(GpioPinRecord);
  80. const InputPin input_pins[] = {
  81. {.gpio = &gpio_button_up, .key = InputKeyUp, .inverted = true, .name = "Up"},
  82. {.gpio = &gpio_button_down, .key = InputKeyDown, .inverted = true, .name = "Down"},
  83. {.gpio = &gpio_button_right, .key = InputKeyRight, .inverted = true, .name = "Right"},
  84. {.gpio = &gpio_button_left, .key = InputKeyLeft, .inverted = true, .name = "Left"},
  85. {.gpio = &gpio_button_ok, .key = InputKeyOk, .inverted = false, .name = "OK"},
  86. {.gpio = &gpio_button_back, .key = InputKeyBack, .inverted = true, .name = "Back"},
  87. };
  88. const size_t input_pins_count = sizeof(input_pins) / sizeof(InputPin);
  89. static void furi_hal_resources_init_input_pins(GpioMode mode) {
  90. for(size_t i = 0; i < input_pins_count; i++) {
  91. furi_hal_gpio_init(
  92. input_pins[i].gpio,
  93. mode,
  94. (input_pins[i].inverted) ? GpioPullUp : GpioPullDown,
  95. GpioSpeedLow);
  96. }
  97. }
  98. void furi_hal_resources_init_early() {
  99. furi_hal_resources_init_input_pins(GpioModeInput);
  100. // SD Card stepdown control
  101. furi_hal_gpio_write(&gpio_periph_power, 1);
  102. furi_hal_gpio_init(&gpio_periph_power, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedLow);
  103. // Display pins
  104. furi_hal_gpio_write(&gpio_display_rst_n, 1);
  105. furi_hal_gpio_init_simple(&gpio_display_rst_n, GpioModeOutputPushPull);
  106. furi_hal_gpio_init(&gpio_display_di, GpioModeOutputPushPull, GpioPullNo, GpioSpeedLow);
  107. // Pullup display reset pin for shutdown
  108. SET_BIT(PWR->PUCRB, gpio_display_rst_n.pin);
  109. CLEAR_BIT(PWR->PDCRB, gpio_display_rst_n.pin);
  110. SET_BIT(PWR->CR3, PWR_CR3_APC);
  111. // Hard reset USB
  112. furi_hal_gpio_write(&gpio_usb_dm, 1);
  113. furi_hal_gpio_write(&gpio_usb_dp, 1);
  114. furi_hal_gpio_init_simple(&gpio_usb_dm, GpioModeOutputOpenDrain);
  115. furi_hal_gpio_init_simple(&gpio_usb_dp, GpioModeOutputOpenDrain);
  116. furi_hal_gpio_write(&gpio_usb_dm, 0);
  117. furi_hal_gpio_write(&gpio_usb_dp, 0);
  118. furi_delay_us(5); // Device Driven disconnect: 2.5us + extra to compensate cables
  119. furi_hal_gpio_write(&gpio_usb_dm, 1);
  120. furi_hal_gpio_write(&gpio_usb_dp, 1);
  121. furi_hal_gpio_init_simple(&gpio_usb_dm, GpioModeAnalog);
  122. furi_hal_gpio_init_simple(&gpio_usb_dp, GpioModeAnalog);
  123. furi_hal_gpio_write(&gpio_usb_dm, 0);
  124. furi_hal_gpio_write(&gpio_usb_dp, 0);
  125. // External header pins
  126. furi_hal_gpio_init(&gpio_ext_pc0, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
  127. furi_hal_gpio_init(&gpio_ext_pc1, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
  128. furi_hal_gpio_init(&gpio_ext_pc3, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
  129. furi_hal_gpio_init(&gpio_ext_pb2, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
  130. furi_hal_gpio_init(&gpio_ext_pb3, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
  131. furi_hal_gpio_init(&gpio_ext_pa4, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
  132. furi_hal_gpio_init(&gpio_ext_pa6, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
  133. furi_hal_gpio_init(&gpio_ext_pa7, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
  134. }
  135. void furi_hal_resources_deinit_early() {
  136. furi_hal_resources_init_input_pins(GpioModeAnalog);
  137. }
  138. void furi_hal_resources_init() {
  139. // Button pins
  140. furi_hal_resources_init_input_pins(GpioModeInterruptRiseFall);
  141. // Explicit pulls pins
  142. LL_PWR_EnablePUPDCfg();
  143. LL_PWR_EnableGPIOPullDown(LL_PWR_GPIO_B, LL_PWR_GPIO_BIT_8); // gpio_speaker
  144. LL_PWR_EnableGPIOPullDown(LL_PWR_GPIO_A, LL_PWR_GPIO_BIT_8); // gpio_vibro
  145. // Display pins
  146. furi_hal_gpio_init(&gpio_display_rst_n, GpioModeOutputPushPull, GpioPullNo, GpioSpeedLow);
  147. furi_hal_gpio_write(&gpio_display_rst_n, 0);
  148. furi_hal_gpio_init(&gpio_display_di, GpioModeOutputPushPull, GpioPullNo, GpioSpeedLow);
  149. furi_hal_gpio_write(&gpio_display_di, 0);
  150. // SD pins
  151. furi_hal_gpio_init(&gpio_sdcard_cd, GpioModeInput, GpioPullNo, GpioSpeedLow);
  152. furi_hal_gpio_write(&gpio_sdcard_cd, 0);
  153. furi_hal_gpio_init(&gpio_ibutton, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
  154. NVIC_SetPriority(EXTI0_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 5, 0));
  155. NVIC_EnableIRQ(EXTI0_IRQn);
  156. NVIC_SetPriority(EXTI1_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 5, 0));
  157. NVIC_EnableIRQ(EXTI1_IRQn);
  158. NVIC_SetPriority(EXTI2_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 5, 0));
  159. NVIC_EnableIRQ(EXTI2_IRQn);
  160. NVIC_SetPriority(EXTI3_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 5, 0));
  161. NVIC_EnableIRQ(EXTI3_IRQn);
  162. NVIC_SetPriority(EXTI4_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 5, 0));
  163. NVIC_EnableIRQ(EXTI4_IRQn);
  164. NVIC_SetPriority(EXTI9_5_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 5, 0));
  165. NVIC_EnableIRQ(EXTI9_5_IRQn);
  166. NVIC_SetPriority(EXTI15_10_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 5, 0));
  167. NVIC_EnableIRQ(EXTI15_10_IRQn);
  168. FURI_LOG_I(TAG, "Init OK");
  169. }
  170. int32_t furi_hal_resources_get_ext_pin_number(const GpioPin* gpio) {
  171. // TODO: describe second ROW
  172. if(gpio == &gpio_ext_pa7)
  173. return 2;
  174. else if(gpio == &gpio_ext_pa6)
  175. return 3;
  176. else if(gpio == &gpio_ext_pa4)
  177. return 4;
  178. else if(gpio == &gpio_ext_pb3)
  179. return 5;
  180. else if(gpio == &gpio_ext_pb2)
  181. return 6;
  182. else if(gpio == &gpio_ext_pc3)
  183. return 7;
  184. else if(gpio == &gpio_ext_pc1)
  185. return 15;
  186. else if(gpio == &gpio_ext_pc0)
  187. return 16;
  188. else if(gpio == &gpio_ibutton)
  189. return 17;
  190. else
  191. return -1;
  192. }