board.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. * This file is free software: you can redistribute it and/or modify it
  3. * under the terms of the GNU General Public License as published by the
  4. * Free Software Foundation, either version 3 of the License, or
  5. * (at your option) any later version.
  6. *
  7. * This file is distributed in the hope that it will be useful, but
  8. * WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. * See the GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License along
  13. * with this program. If not, see <http://www.gnu.org/licenses/>.
  14. *
  15. * Modified for use in AP_HAL by Andrew Tridgell and Siddharth Bharat Purohit
  16. */
  17. #include "hal.h"
  18. #include "usbcfg.h"
  19. #include "stm32_util.h"
  20. #include "watchdog.h"
  21. /*===========================================================================*/
  22. /* Driver local definitions. */
  23. /*===========================================================================*/
  24. /*===========================================================================*/
  25. /* Driver exported variables. */
  26. /*===========================================================================*/
  27. /*===========================================================================*/
  28. /* Driver local variables and types. */
  29. /*===========================================================================*/
  30. /**
  31. * @brief STM32 GPIO static initialization data.
  32. */
  33. #if defined(STM32F100_MCUCONF) || defined(STM32F103_MCUCONF)
  34. const PALConfig pal_default_config =
  35. {
  36. {VAL_GPIOA_ODR, VAL_GPIOA_CRL, VAL_GPIOA_CRH},
  37. {VAL_GPIOB_ODR, VAL_GPIOB_CRL, VAL_GPIOB_CRH},
  38. {VAL_GPIOC_ODR, VAL_GPIOC_CRL, VAL_GPIOC_CRH},
  39. {VAL_GPIOD_ODR, VAL_GPIOD_CRL, VAL_GPIOD_CRH},
  40. {VAL_GPIOE_ODR, VAL_GPIOE_CRL, VAL_GPIOE_CRH},
  41. };
  42. #else //Other than STM32F1 series
  43. /**
  44. * @brief Type of STM32 GPIO port setup.
  45. */
  46. typedef struct {
  47. uint32_t moder;
  48. uint32_t otyper;
  49. uint32_t ospeedr;
  50. uint32_t pupdr;
  51. uint32_t odr;
  52. uint32_t afrl;
  53. uint32_t afrh;
  54. } gpio_setup_t;
  55. /**
  56. * @brief Type of STM32 GPIO initialization data.
  57. */
  58. typedef struct {
  59. #if STM32_HAS_GPIOA || defined(__DOXYGEN__)
  60. gpio_setup_t PAData;
  61. #endif
  62. #if STM32_HAS_GPIOB || defined(__DOXYGEN__)
  63. gpio_setup_t PBData;
  64. #endif
  65. #if STM32_HAS_GPIOC || defined(__DOXYGEN__)
  66. gpio_setup_t PCData;
  67. #endif
  68. #if STM32_HAS_GPIOD || defined(__DOXYGEN__)
  69. gpio_setup_t PDData;
  70. #endif
  71. #if STM32_HAS_GPIOE || defined(__DOXYGEN__)
  72. gpio_setup_t PEData;
  73. #endif
  74. #if STM32_HAS_GPIOF || defined(__DOXYGEN__)
  75. gpio_setup_t PFData;
  76. #endif
  77. #if STM32_HAS_GPIOG || defined(__DOXYGEN__)
  78. gpio_setup_t PGData;
  79. #endif
  80. #if STM32_HAS_GPIOH || defined(__DOXYGEN__)
  81. gpio_setup_t PHData;
  82. #endif
  83. #if STM32_HAS_GPIOI || defined(__DOXYGEN__)
  84. gpio_setup_t PIData;
  85. #endif
  86. #if STM32_HAS_GPIOJ || defined(__DOXYGEN__)
  87. gpio_setup_t PJData;
  88. #endif
  89. #if STM32_HAS_GPIOK || defined(__DOXYGEN__)
  90. gpio_setup_t PKData;
  91. #endif
  92. } gpio_config_t;
  93. /**
  94. * @brief STM32 GPIO static initialization data.
  95. */
  96. static const gpio_config_t gpio_default_config = {
  97. #if STM32_HAS_GPIOA
  98. {VAL_GPIOA_MODER, VAL_GPIOA_OTYPER, VAL_GPIOA_OSPEEDR, VAL_GPIOA_PUPDR,
  99. VAL_GPIOA_ODR, VAL_GPIOA_AFRL, VAL_GPIOA_AFRH},
  100. #endif
  101. #if STM32_HAS_GPIOB
  102. {VAL_GPIOB_MODER, VAL_GPIOB_OTYPER, VAL_GPIOB_OSPEEDR, VAL_GPIOB_PUPDR,
  103. VAL_GPIOB_ODR, VAL_GPIOB_AFRL, VAL_GPIOB_AFRH},
  104. #endif
  105. #if STM32_HAS_GPIOC
  106. {VAL_GPIOC_MODER, VAL_GPIOC_OTYPER, VAL_GPIOC_OSPEEDR, VAL_GPIOC_PUPDR,
  107. VAL_GPIOC_ODR, VAL_GPIOC_AFRL, VAL_GPIOC_AFRH},
  108. #endif
  109. #if STM32_HAS_GPIOD
  110. {VAL_GPIOD_MODER, VAL_GPIOD_OTYPER, VAL_GPIOD_OSPEEDR, VAL_GPIOD_PUPDR,
  111. VAL_GPIOD_ODR, VAL_GPIOD_AFRL, VAL_GPIOD_AFRH},
  112. #endif
  113. #if STM32_HAS_GPIOE
  114. {VAL_GPIOE_MODER, VAL_GPIOE_OTYPER, VAL_GPIOE_OSPEEDR, VAL_GPIOE_PUPDR,
  115. VAL_GPIOE_ODR, VAL_GPIOE_AFRL, VAL_GPIOE_AFRH},
  116. #endif
  117. #if STM32_HAS_GPIOF
  118. {VAL_GPIOF_MODER, VAL_GPIOF_OTYPER, VAL_GPIOF_OSPEEDR, VAL_GPIOF_PUPDR,
  119. VAL_GPIOF_ODR, VAL_GPIOF_AFRL, VAL_GPIOF_AFRH},
  120. #endif
  121. #if STM32_HAS_GPIOG
  122. {VAL_GPIOG_MODER, VAL_GPIOG_OTYPER, VAL_GPIOG_OSPEEDR, VAL_GPIOG_PUPDR,
  123. VAL_GPIOG_ODR, VAL_GPIOG_AFRL, VAL_GPIOG_AFRH},
  124. #endif
  125. #if STM32_HAS_GPIOH
  126. {VAL_GPIOH_MODER, VAL_GPIOH_OTYPER, VAL_GPIOH_OSPEEDR, VAL_GPIOH_PUPDR,
  127. VAL_GPIOH_ODR, VAL_GPIOH_AFRL, VAL_GPIOH_AFRH},
  128. #endif
  129. #if STM32_HAS_GPIOI
  130. {VAL_GPIOI_MODER, VAL_GPIOI_OTYPER, VAL_GPIOI_OSPEEDR, VAL_GPIOI_PUPDR,
  131. VAL_GPIOI_ODR, VAL_GPIOI_AFRL, VAL_GPIOI_AFRH},
  132. #endif
  133. #if STM32_HAS_GPIOJ
  134. {VAL_GPIOJ_MODER, VAL_GPIOJ_OTYPER, VAL_GPIOJ_OSPEEDR, VAL_GPIOJ_PUPDR,
  135. VAL_GPIOJ_ODR, VAL_GPIOJ_AFRL, VAL_GPIOJ_AFRH},
  136. #endif
  137. #if STM32_HAS_GPIOK
  138. {VAL_GPIOK_MODER, VAL_GPIOK_OTYPER, VAL_GPIOK_OSPEEDR, VAL_GPIOK_PUPDR,
  139. VAL_GPIOK_ODR, VAL_GPIOK_AFRL, VAL_GPIOK_AFRH}
  140. #endif
  141. };
  142. /*===========================================================================*/
  143. /* Driver local functions. */
  144. /*===========================================================================*/
  145. static void gpio_init(stm32_gpio_t *gpiop, const gpio_setup_t *config) {
  146. gpiop->OTYPER = config->otyper;
  147. gpiop->OSPEEDR = config->ospeedr;
  148. gpiop->PUPDR = config->pupdr;
  149. gpiop->ODR = config->odr;
  150. gpiop->AFRL = config->afrl;
  151. gpiop->AFRH = config->afrh;
  152. gpiop->MODER = config->moder;
  153. }
  154. static void stm32_gpio_init(void) {
  155. /* Enabling GPIO-related clocks, the mask comes from the
  156. registry header file.*/
  157. #if defined(STM32H7)
  158. rccResetAHB4(STM32_GPIO_EN_MASK);
  159. rccEnableAHB4(STM32_GPIO_EN_MASK, true);
  160. #else
  161. rccResetAHB1(STM32_GPIO_EN_MASK);
  162. rccEnableAHB1(STM32_GPIO_EN_MASK, true);
  163. #endif
  164. /* Initializing all the defined GPIO ports.*/
  165. #if STM32_HAS_GPIOA
  166. gpio_init(GPIOA, &gpio_default_config.PAData);
  167. #endif
  168. #if STM32_HAS_GPIOB
  169. gpio_init(GPIOB, &gpio_default_config.PBData);
  170. #endif
  171. #if STM32_HAS_GPIOC
  172. gpio_init(GPIOC, &gpio_default_config.PCData);
  173. #endif
  174. #if STM32_HAS_GPIOD
  175. gpio_init(GPIOD, &gpio_default_config.PDData);
  176. #endif
  177. #if STM32_HAS_GPIOE
  178. gpio_init(GPIOE, &gpio_default_config.PEData);
  179. #endif
  180. #if STM32_HAS_GPIOF
  181. gpio_init(GPIOF, &gpio_default_config.PFData);
  182. #endif
  183. #if STM32_HAS_GPIOG
  184. gpio_init(GPIOG, &gpio_default_config.PGData);
  185. #endif
  186. #if STM32_HAS_GPIOH
  187. gpio_init(GPIOH, &gpio_default_config.PHData);
  188. #endif
  189. #if STM32_HAS_GPIOI
  190. gpio_init(GPIOI, &gpio_default_config.PIData);
  191. #endif
  192. #if STM32_HAS_GPIOJ
  193. gpio_init(GPIOJ, &gpio_default_config.PJData);
  194. #endif
  195. #if STM32_HAS_GPIOK
  196. gpio_init(GPIOK, &gpio_default_config.PKData);
  197. #endif
  198. }
  199. #endif //!STM32F100_MCUCONF
  200. /**
  201. * @brief Early initialization code.
  202. * @details This initialization must be performed just after stack setup
  203. * and before any other initialization.
  204. */
  205. void __early_init(void) {
  206. #if !defined(STM32F100_MCUCONF) && !defined(STM32F103_MCUCONF)
  207. stm32_gpio_init();
  208. #endif
  209. stm32_clock_init();
  210. #if defined(HAL_DISABLE_DCACHE)
  211. SCB_DisableDCache();
  212. #endif
  213. }
  214. void __late_init(void) {
  215. halInit();
  216. chSysInit();
  217. stm32_watchdog_save_reason();
  218. #ifndef HAL_BOOTLOADER_BUILD
  219. stm32_watchdog_clear_reason();
  220. #endif
  221. #if CH_CFG_USE_HEAP == TRUE
  222. malloc_init();
  223. #endif
  224. #ifdef HAL_USB_PRODUCT_ID
  225. setup_usb_strings();
  226. #endif
  227. }
  228. #if HAL_USE_SDC || defined(__DOXYGEN__)
  229. /**
  230. * @brief SDC card detection.
  231. */
  232. bool sdc_lld_is_card_inserted(SDCDriver *sdcp) {
  233. (void)sdcp;
  234. return true;
  235. }
  236. /**
  237. * @brief SDC card write protection detection.
  238. */
  239. bool sdc_lld_is_write_protected(SDCDriver *sdcp) {
  240. (void)sdcp;
  241. return false;
  242. }
  243. #endif /* HAL_USE_SDC */
  244. #if HAL_USE_MMC_SPI || defined(__DOXYGEN__)
  245. /**
  246. * @brief MMC_SPI card detection.
  247. */
  248. bool mmc_lld_is_card_inserted(MMCDriver *mmcp) {
  249. (void)mmcp;
  250. /* TODO: Fill the implementation.*/
  251. return true;
  252. }
  253. /**
  254. * @brief MMC_SPI card write protection detection.
  255. */
  256. bool mmc_lld_is_write_protected(MMCDriver *mmcp) {
  257. (void)mmcp;
  258. /* TODO: Fill the implementation.*/
  259. return false;
  260. }
  261. #endif
  262. /**
  263. * @brief Board-specific initialization code.
  264. * @todo Add your board-specific code, if any.
  265. */
  266. void boardInit(void) {
  267. HAL_BOARD_INIT_HOOK_CALL
  268. }