furi_hal_resources.c 9.8 KB

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