system.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. #ifndef SYSTEM_H_INCLUDED
  2. #define SYSTEM_H_INCLUDED
  3. #include <stdint.h>
  4. /* System specific: PLL with 8 MHz external oscillator, CPU at 168MHz */
  5. #define PLL_FULL_MASK (0x7F037FFF)
  6. extern uint32_t cpu_freq;
  7. void panic(void);
  8. void printbin(const uint8_t *buf, int len); /* Defined in uart.c */
  9. extern int _mutex_lock(void *); /* defined in mutex.S */
  10. extern int _mutex_unlock(void *);
  11. /* PIN CONFIG TARGET */
  12. #define LED 0 // PE0
  13. #define SPI_FLASH_PIN 4 /* Flash CS connected to GPIOA4 */
  14. #define SPI1_PIN_AF 5
  15. #define SPI1_CLOCK_PIN 5
  16. #define SPI1_MISO_PIN 6
  17. #define SPI1_MOSI_PIN 7
  18. #define SPI2_PIN_AF 5
  19. #define SPI2_CLOCK_PIN 13
  20. #define SPI2_MISO_PIN 14
  21. #define SPI2_MOSI_PIN 15
  22. #define I2C1_PIN_AF 4
  23. #define I2C1_SDA 7 /* GPIOB P7 */
  24. #define I2C1_SCL 6 /* GPIOB P6 */
  25. #define GPIO_MODE_AF (2)
  26. /* STM32 specific defines */
  27. #define APB1_CLOCK_ER (*(volatile uint32_t *)(0x40023840))
  28. #define APB1_CLOCK_RST (*(volatile uint32_t *)(0x40023820))
  29. #define TIM2_APB1_CLOCK_ER_VAL (1 << 0)
  30. #define TIM3_APB1_CLOCK_ER_VAL (1 << 1)
  31. #define TIM4_APB1_CLOCK_ER_VAL (1 << 2)
  32. #define PWR_APB1_CLOCK_ER_VAL (1 << 28)
  33. #define SPI2_APB1_CLOCK_ER_VAL (1 << 14)
  34. #define APB2_CLOCK_ER (*(volatile uint32_t *)(0x40023844))
  35. #define APB2_CLOCK_RST (*(volatile uint32_t *)(0x40023824))
  36. #define SYSCFG_APB2_CLOCK_ER (1 << 14)
  37. #define SPI1_APB2_CLOCK_ER_VAL (1 << 12)
  38. #define SDIO_APB2_CLOCK_ER_VAL (1 << 11)
  39. #define ADC1_APB2_CLOCK_ER_VAL (1 << 8)
  40. #define TIM1_APB2_CLOCK_ER_VAL (1 << 0)
  41. #define RCC_BACKUP (*(volatile uint32_t *)(0x40023870))
  42. #define RCC_BACKUP_RESET (1 << 16)
  43. #define RCC_BACKUP_RTCEN (1 << 15)
  44. #define RCC_BACKUP_RTCSEL_SHIFT 8
  45. #define RCC_BACKUP_RTCSEL_MASK 0x3
  46. #define RCC_BACKUP_RTCSEL_NONE 0
  47. #define RCC_BACKUP_RTCSEL_LSE 1
  48. #define RCC_BACKUP_RTCSEL_LSI 2
  49. #define RCC_BACKUP_RTCSEL_HSE 3
  50. #define RCC_BACKUP_LSEMOD (1 << 3)
  51. #define RCC_BACKUP_LSEBYP (1 << 2)
  52. #define RCC_BACKUP_LSERDY (1 << 1)
  53. #define RCC_BACKUP_LSEON (1 << 0)
  54. #define RCC_CSR_LSION (1 << 0)
  55. #define RCC_CSR_LSIRDY (1 << 1)
  56. /* EXTI */
  57. #define EXTI_CR_BASE (0x40013808)
  58. #define EXTI_CR0 (*(volatile uint32_t *)(EXTI_CR_BASE + 0x00))
  59. #define EXTI_CR_EXTI0_MASK (0xFFFF)
  60. #define EXTI_IMR (*(volatile uint32_t *)(EXTI_BASE + 0x00))
  61. #define EXTI_EMR (*(volatile uint32_t *)(EXTI_BASE + 0x04))
  62. #define EXTI_RTSR (*(volatile uint32_t *)(EXTI_BASE + 0x08))
  63. #define EXTI_FTSR (*(volatile uint32_t *)(EXTI_BASE + 0x0c))
  64. #define EXTI_SWIER (*(volatile uint32_t *)(EXTI_BASE + 0x10))
  65. #define EXTI_PR (*(volatile uint32_t *)(EXTI_BASE + 0x14))
  66. /* HW RNG */
  67. #define RNG_BASE (0x50060800)
  68. #define RNG_CR (*(volatile uint32_t *)(RNG_BASE + 0x00))
  69. #define RNG_SR (*(volatile uint32_t *)(RNG_BASE + 0x04))
  70. #define RNG_DR (*(volatile uint32_t *)(RNG_BASE + 0x08))
  71. #define RNG_CR_IE (1 << 3)
  72. #define RNG_CR_RNGEN (1 << 2)
  73. #define RNG_SR_DRDY (1 << 0)
  74. #define RNG_SR_CECS (1 << 1)
  75. #define RNG_SR_SECS (1 << 2)
  76. /* SCB for sleep configuration */
  77. #define SCB_SCR (*(volatile uint32_t *)(0xE000ED10))
  78. #define SCB_SCR_SEVONPEND (1 << 4)
  79. #define SCB_SCR_SLEEPDEEP (1 << 2)
  80. #define SCB_SCR_SLEEPONEXIT (1 << 1)
  81. /* Assembly helpers */
  82. #define DMB() __asm__ volatile ("dmb")
  83. #define WFI() __asm__ volatile ("wfi")
  84. #define WFE() __asm__ volatile ("wfe")
  85. #define SEV() __asm__ volatile ("sev")
  86. /* Master clock setting */
  87. void clock_pll_on(void);
  88. void clock_pll_off(void);
  89. /* NVIC */
  90. /* NVIC ISER Base register (Cortex-M) */
  91. #define NVIC_RTC_IRQ (3)
  92. #define NVIC_TIM1_IRQN (27)
  93. #define NVIC_TIM2_IRQN (28)
  94. #define NVIC_TIM3_IRQN (29)
  95. #define NVIC_TIM4_IRQN (30)
  96. #define NVIC_ISER_BASE (0xE000E100)
  97. #define NVIC_ICER_BASE (0xE000E180)
  98. #define NVIC_ICPR_BASE (0xE000E280)
  99. #define NVIC_IPRI_BASE (0xE000E400)
  100. #define NVIC_EXTI0_IRQ (6)
  101. #define NVIC_EXTI9_5_IRQ (23)
  102. #define NVIC_EXTI15_10_IRQ (40)
  103. static inline void nvic_irq_enable(uint8_t n)
  104. {
  105. int i = n / 32;
  106. volatile uint32_t *nvic_iser = ((volatile uint32_t *)(NVIC_ISER_BASE + 4 * i));
  107. *nvic_iser |= (1 << (n % 32));
  108. }
  109. static inline void nvic_irq_disable(uint8_t n)
  110. {
  111. int i = n / 32;
  112. volatile uint32_t *nvic_icer = ((volatile uint32_t *)(NVIC_ICER_BASE + 4 * i));
  113. *nvic_icer |= (1 << (n % 32));
  114. }
  115. static inline void nvic_irq_setprio(uint8_t n, uint8_t prio)
  116. {
  117. volatile uint8_t *nvic_ipri = ((volatile uint8_t *)(NVIC_IPRI_BASE + n));
  118. *nvic_ipri = prio;
  119. }
  120. static inline void nvic_irq_clear(uint8_t n)
  121. {
  122. int i = n / 32;
  123. volatile uint8_t *nvic_icpr = ((volatile uint8_t *)(NVIC_ICPR_BASE + 4 * i));
  124. *nvic_icpr = (1 << (n % 32));
  125. }
  126. /*** FLASH ***/
  127. #define FLASH_BASE (0x40023C00)
  128. #define FLASH_ACR (*(volatile uint32_t *)(FLASH_BASE + 0x00))
  129. #define FLASH_KEYR (*(volatile uint32_t *)(FLASH_BASE + 0x04))
  130. #define FLASH_SR (*(volatile uint32_t *)(FLASH_BASE + 0x0C))
  131. #define FLASH_CR (*(volatile uint32_t *)(FLASH_BASE + 0x10))
  132. #define FLASH_ACR_ENABLE_DATA_CACHE (1 << 10)
  133. #define FLASH_ACR_ENABLE_INST_CACHE (1 << 9)
  134. /* Register values */
  135. #define FLASH_ACR_RESET_DATA_CACHE (1 << 12)
  136. #define FLASH_ACR_RESET_INST_CACHE (1 << 11)
  137. #define FLASH_ACR_ENABLE_DATA_CACHE (1 << 10)
  138. #define FLASH_ACR_ENABLE_INST_CACHE (1 << 9)
  139. #define FLASH_ACR_ENABLE_PRFT (1 << 8)
  140. #define FLASH_SR_BSY (1 << 16)
  141. #define FLASH_SR_PGSERR (1 << 7)
  142. #define FLASH_SR_PGPERR (1 << 6)
  143. #define FLASH_SR_PGAERR (1 << 5)
  144. #define FLASH_SR_WRPERR (1 << 4)
  145. #define FLASH_SR_OPERR (1 << 1)
  146. #define FLASH_SR_EOP (1 << 0)
  147. #define FLASH_CR_LOCK (1 << 31)
  148. #define FLASH_CR_ERRIE (1 << 25)
  149. #define FLASH_CR_EOPIE (1 << 24)
  150. #define FLASH_CR_STRT (1 << 16)
  151. #define FLASH_CR_MER (1 << 2)
  152. #define FLASH_CR_SER (1 << 1)
  153. #define FLASH_CR_PG (1 << 0)
  154. #define FLASH_CR_SNB_SHIFT 3
  155. #define FLASH_CR_SNB_MASK 0x1f
  156. #define FLASH_CR_PROGRAM_X8 (0 << 8)
  157. #define FLASH_CR_PROGRAM_X16 (1 << 8)
  158. #define FLASH_CR_PROGRAM_X32 (2 << 8)
  159. #define FLASH_CR_PROGRAM_X64 (3 << 8)
  160. #define FLASH_KEY1 (0x45670123)
  161. #define FLASH_KEY2 (0xCDEF89AB)
  162. /*** SPI ***/
  163. #define SPI1 (0x40013000)
  164. #define SPI1_CR1 (*(volatile uint32_t *)(SPI1))
  165. #define SPI1_CR2 (*(volatile uint32_t *)(SPI1 + 0x04))
  166. #define SPI1_SR (*(volatile uint32_t *)(SPI1 + 0x08))
  167. #define SPI1_DR (*(volatile uint32_t *)(SPI1 + 0x0c))
  168. #define SPI2 (0x40003800)
  169. #define SPI2_CR1 (*(volatile uint32_t *)(SPI2))
  170. #define SPI2_CR2 (*(volatile uint32_t *)(SPI2 + 0x04))
  171. #define SPI2_SR (*(volatile uint32_t *)(SPI2 + 0x08))
  172. #define SPI2_DR (*(volatile uint32_t *)(SPI2 + 0x0c))
  173. #define SPI_CR1_CLOCK_PHASE (1 << 0)
  174. #define SPI_CR1_CLOCK_POLARITY (1 << 1)
  175. #define SPI_CR1_MASTER (1 << 2)
  176. #define SPI_CR1_BAUDRATE (0x07 << 3)
  177. #define SPI_CR1_SPI_EN (1 << 6)
  178. #define SPI_CR1_LSBFIRST (1 << 7)
  179. #define SPI_CR1_SSI (1 << 8)
  180. #define SPI_CR1_SSM (1 << 9)
  181. #define SPI_CR1_16BIT_FORMAT (1 << 11)
  182. #define SPI_CR1_TX_CRC_NEXT (1 << 12)
  183. #define SPI_CR1_HW_CRC_EN (1 << 13)
  184. #define SPI_CR1_BIDIOE (1 << 14)
  185. #define SPI_CR2_SSOE (1 << 2)
  186. #define SPI_SR_RX_NOTEMPTY (1 << 0)
  187. #define SPI_SR_TX_EMPTY (1 << 1)
  188. #define SPI_SR_BUSY (1 << 7)
  189. /*** RCC ***/
  190. #define RCC_CR_PLLRDY (1 << 25)
  191. #define RCC_CR_PLLON (1 << 24)
  192. #define RCC_CR_HSERDY (1 << 17)
  193. #define RCC_CR_HSEON (1 << 16)
  194. #define RCC_CR_HSIRDY (1 << 1)
  195. #define RCC_CR_HSION (1 << 0)
  196. #define RCC_CFGR_SW_HSI 0x0
  197. #define RCC_CFGR_SW_HSE 0x1
  198. #define RCC_CFGR_SW_PLL 0x2
  199. #define RCC_PRESCALER_DIV_NONE 0
  200. #define RCC_PRESCALER_DIV_2 8
  201. #define RCC_PRESCALER_DIV_4 9
  202. #define RCC_PLLCFGR_PLLSRC (1 << 22)
  203. #define AHB1_CLOCK_ER (*(volatile uint32_t *)(0x40023830))
  204. #define GPIOA_AHB1_CLOCK_ER (1 << 0)
  205. #define GPIOB_AHB1_CLOCK_ER (1 << 1)
  206. #define GPIOC_AHB1_CLOCK_ER (1 << 2)
  207. #define GPIOD_AHB1_CLOCK_ER (1 << 3)
  208. #define GPIOE_AHB1_CLOCK_ER (1 << 4)
  209. #define GPIOA_BASE 0x40020000
  210. #define GPIOB_BASE 0x40020400
  211. #define GPIOC_BASE 0x40020800
  212. #define GPIOD_BASE 0x40020C00
  213. #define GPIOE_BASE 0x40021000
  214. #define GPIOA GPIOA_BASE
  215. #define GPIOB GPIOB_BASE
  216. #define GPIOC GPIOC_BASE
  217. #define GPIOD GPIOD_BASE
  218. #define GPIOE GPIOE_BASE
  219. #define AHB2_CLOCK_ER (*(volatile uint32_t *)(0x40023834))
  220. #define RNG_AHB2_CLOCK_ER (1 << 6)
  221. /* POWER CONTROL REGISTER */
  222. #define POW_BASE (0x40007000)
  223. #define POW_CR (*(volatile uint32_t *)(POW_BASE + 0x00))
  224. #define POW_SCR (*(volatile uint32_t *)(POW_BASE + 0x04))
  225. #define POW_CR_VOS (1 << 14)
  226. #define POW_CR_FPDS (1 << 9)
  227. #define POW_CR_DPB (1 << 8)
  228. #define POW_CR_CSBF (1 << 3)
  229. #define POW_CR_CWUF (1 << 2)
  230. #define POW_CR_PDDS (1 << 1)
  231. #define POW_CR_LPDS (1 << 0)
  232. #define POW_SCR_BRE (1 << 9)
  233. #define POW_SCR_EWUP (1 << 4)
  234. #define POW_SCR_BRR (1 << 3)
  235. #define POW_SCR_WUF (1 << 0)
  236. /* GPIOS */
  237. #define GPIOA_MODE (*(volatile uint32_t *)(GPIOA_BASE + 0x00))
  238. #define GPIOA_AFL (*(volatile uint32_t *)(GPIOA_BASE + 0x20))
  239. #define GPIOA_AFH (*(volatile uint32_t *)(GPIOA_BASE + 0x24))
  240. #define GPIOA_OSPD (*(volatile uint32_t *)(GPIOA_BASE + 0x08))
  241. #define GPIOA_PUPD (*(volatile uint32_t *)(GPIOA_BASE + 0x0c))
  242. #define GPIOA_BSRR (*(volatile uint32_t *)(GPIOA_BASE + 0x18))
  243. #define GPIOA_ODR (*(volatile uint32_t *)(GPIOA_BASE + 0x14))
  244. #define GPIOA_IDR (*(volatile uint32_t *)(GPIOA_BASE + 0x10))
  245. #define GPIOB_MODE (*(volatile uint32_t *)(GPIOB_BASE + 0x00))
  246. #define GPIOB_AFL (*(volatile uint32_t *)(GPIOB_BASE + 0x20))
  247. #define GPIOB_AFH (*(volatile uint32_t *)(GPIOB_BASE + 0x24))
  248. #define GPIOB_OSPD (*(volatile uint32_t *)(GPIOB_BASE + 0x08))
  249. #define GPIOB_PUPD (*(volatile uint32_t *)(GPIOB_BASE + 0x0c))
  250. #define GPIOB_BSRR (*(volatile uint32_t *)(GPIOB_BASE + 0x18))
  251. #define GPIOB_ODR (*(volatile uint32_t *)(GPIOB_BASE + 0x14))
  252. #define GPIOC_MODE (*(volatile uint32_t *)(GPIOC_BASE + 0x00))
  253. #define GPIOC_OTYPE (*(volatile uint32_t *)(GPIOC_BASE + 0x04))
  254. #define GPIOC_OSPEED (*(volatile uint32_t *)(GPIOC_BASE + 0x08))
  255. #define GPIOC_AFL (*(volatile uint32_t *)(GPIOC_BASE + 0x20))
  256. #define GPIOC_AFH (*(volatile uint32_t *)(GPIOC_BASE + 0x24))
  257. #define GPIOC_OSPD (*(volatile uint32_t *)(GPIOC_BASE + 0x08))
  258. #define GPIOC_PUPD (*(volatile uint32_t *)(GPIOC_BASE + 0x0c))
  259. #define GPIOC_BSRR (*(volatile uint32_t *)(GPIOC_BASE + 0x18))
  260. #define GPIOC_ODR (*(volatile uint32_t *)(GPIOC_BASE + 0x14))
  261. #define GPIOD_MODE (*(volatile uint32_t *)(GPIOD_BASE + 0x00))
  262. #define GPIOD_OTYPE (*(volatile uint32_t *)(GPIOD_BASE + 0x04))
  263. #define GPIOD_OSPEED (*(volatile uint32_t *)(GPIOD_BASE + 0x08))
  264. #define GPIOD_AFL (*(volatile uint32_t *)(GPIOD_BASE + 0x20))
  265. #define GPIOD_AFH (*(volatile uint32_t *)(GPIOD_BASE + 0x24))
  266. #define GPIOD_OSPD (*(volatile uint32_t *)(GPIOD_BASE + 0x08))
  267. #define GPIOD_PUPD (*(volatile uint32_t *)(GPIOD_BASE + 0x0c))
  268. #define GPIOD_BSRR (*(volatile uint32_t *)(GPIOD_BASE + 0x18))
  269. #define GPIOD_ODR (*(volatile uint32_t *)(GPIOD_BASE + 0x14))
  270. #define GPIOD_MODE (*(volatile uint32_t *)(GPIOD_BASE + 0x00))
  271. #define GPIOD_OTYPE (*(volatile uint32_t *)(GPIOD_BASE + 0x04))
  272. #define GPIOD_PUPD (*(volatile uint32_t *)(GPIOD_BASE + 0x0c))
  273. #define GPIOD_ODR (*(volatile uint32_t *)(GPIOD_BASE + 0x14))
  274. #define GPIOE_MODE (*(volatile uint32_t *)(GPIOE_BASE + 0x00))
  275. #define GPIOE_AFL (*(volatile uint32_t *)(GPIOE_BASE + 0x20))
  276. #define GPIOE_AFH (*(volatile uint32_t *)(GPIOE_BASE + 0x24))
  277. #define GPIOE_OSPD (*(volatile uint32_t *)(GPIOE_BASE + 0x08))
  278. #define GPIOE_PUPD (*(volatile uint32_t *)(GPIOE_BASE + 0x0c))
  279. #define GPIOE_BSRR (*(volatile uint32_t *)(GPIOE_BASE + 0x18))
  280. #define GPIOE_ODR (*(volatile uint32_t *)(GPIOE_BASE + 0x14))
  281. #define GPIO_MODE_AF (2)
  282. /* SDIO */
  283. #define SDIO_BASE (0x40012C00)
  284. #define SDIO_POWER (*(volatile uint32_t *)((SDIO_BASE) + 0x00))
  285. #define SDIO_CLKCR (*(volatile uint32_t *)((SDIO_BASE) + 0x04))
  286. #define SDIO_ARG (*(volatile uint32_t *)((SDIO_BASE) + 0x08))
  287. #define SDIO_CMD (*(volatile uint32_t *)((SDIO_BASE) + 0x0C))
  288. #define SDIO_RESPCMD (*(volatile uint32_t *)((SDIO_BASE) + 0x10))
  289. #define SDIO_RESP1 (*(volatile uint32_t *)((SDIO_BASE) + 0x14))
  290. #define SDIO_RESP2 (*(volatile uint32_t *)((SDIO_BASE) + 0x18))
  291. #define SDIO_RESP3 (*(volatile uint32_t *)((SDIO_BASE) + 0x1C))
  292. #define SDIO_RESP4 (*(volatile uint32_t *)((SDIO_BASE) + 0x20))
  293. #define SDIO_DTIMER (*(volatile uint32_t *)((SDIO_BASE) + 0x24))
  294. #define SDIO_DLEN (*(volatile uint32_t *)((SDIO_BASE) + 0x28))
  295. #define SDIO_DCTRL (*(volatile uint32_t *)((SDIO_BASE) + 0x2C))
  296. #define SDIO_DCOUNT (*(volatile uint32_t *)((SDIO_BASE) + 0x30))
  297. #define SDIO_STA (*(volatile uint32_t *)((SDIO_BASE) + 0x34))
  298. #define SDIO_ICR (*(volatile uint32_t *)((SDIO_BASE) + 0x38))
  299. #define SDIO_MASK (*(volatile uint32_t *)((SDIO_BASE) + 0x3C))
  300. #define SDIO_FIFOCNT (*(volatile uint32_t *)((SDIO_BASE) + 0x48))
  301. #define SDIO_FIFO (*(volatile uint32_t *)((SDIO_BASE) + 0x80))
  302. #define SDIO_POWER_PWRCTRL_SHIFT 0
  303. #define SDIO_POWER_PWRCTRL_PWROFF (0x0 << SDIO_POWER_PWRCTRL_SHIFT)
  304. #define SDIO_POWER_PWRCTRL_RSVPWRUP (0x2 << SDIO_POWER_PWRCTRL_SHIFT)
  305. #define SDIO_POWER_PWRCTRL_PWRON (0x3 << SDIO_POWER_PWRCTRL_SHIFT)
  306. #define SDIO_CLKCR_HWFC_EN (1 << 14)
  307. #define SDIO_CLKCR_NEGEDGE (1 << 13)
  308. #define SDIO_CLKCR_WIDBUS_SHIFT 11
  309. #define SDIO_CLKCR_WIDBUS_1 (0x0 << SDIO_CLKCR_WIDBUS_SHIFT)
  310. #define SDIO_CLKCR_WIDBUS_4 (0x1 << SDIO_CLKCR_WIDBUS_SHIFT)
  311. #define SDIO_CLKCR_WIDBUS_8 (0x2 << SDIO_CLKCR_WIDBUS_SHIFT)
  312. #define SDIO_CLKCR_BYPASS (1 << 10)
  313. #define SDIO_CLKCR_PWRSAV (1 << 9)
  314. #define SDIO_CLKCR_CLKEN (1 << 8)
  315. #define SDIO_CLKCR_CLKDIV_SHIFT 0
  316. #define SDIO_CLKCR_CLKDIV_MSK (0xFF << SDIO_CLKCR_CLKDIV_SHIFT)
  317. #define SDIO_CMD_ATACMD (1 << 14)
  318. #define SDIO_CMD_NIEN (1 << 13)
  319. #define SDIO_CMD_ENCMDCOMPL (1 << 12)
  320. #define SDIO_CMD_SDIOSUSPEND (1 << 11)
  321. #define SDIO_CMD_CPSMEN (1 << 10)
  322. #define SDIO_CMD_WAITPEND (1 << 9)
  323. #define SDIO_CMD_WAITINT (1 << 8)
  324. #define SDIO_CMD_WAITRESP_SHIFT 6
  325. #define SDIO_CMD_WAITRESP_NO_0 (0x0 << SDIO_CMD_WAITRESP_SHIFT)
  326. #define SDIO_CMD_WAITRESP_SHORT (0x1 << SDIO_CMD_WAITRESP_SHIFT)
  327. #define SDIO_CMD_WAITRESP_NO_2 (0x2 << SDIO_CMD_WAITRESP_SHIFT)
  328. #define SDIO_CMD_WAITRESP_LONG (0x3 << SDIO_CMD_WAITRESP_SHIFT)
  329. #define SDIO_CMD_CMDINDEX_SHIFT 0
  330. #define SDIO_CMD_CMDINDEX_MSK (0x3F << SDIO_CMD_CMDINDEX_SHIFT)
  331. #define SDIO_RESPCMD_SHIFT 0
  332. #define SDIO_RESPCMD_MSK (0x3F << SDIO_RESPCMD_SHIFT)
  333. #define SDIO_DCTRL_SDIOEN (1 << 11)
  334. #define SDIO_DCTRL_RWMOD (1 << 10)
  335. #define SDIO_DCTRL_RWSTOP (1 << 9)
  336. #define SDIO_DCTRL_RWSTART (1 << 8)
  337. #define SDIO_DCTRL_DBLOCKSIZE_SHIFT 4
  338. #define SDIO_DCTRL_DBLOCKSIZE_0 (0x0 << SDIO_DCTRL_DBLOCKSIZE_SHIFT)
  339. #define SDIO_DCTRL_DBLOCKSIZE_1 (0x1 << SDIO_DCTRL_DBLOCKSIZE_SHIFT)
  340. #define SDIO_DCTRL_DBLOCKSIZE_2 (0x2 << SDIO_DCTRL_DBLOCKSIZE_SHIFT)
  341. #define SDIO_DCTRL_DBLOCKSIZE_3 (0x3 << SDIO_DCTRL_DBLOCKSIZE_SHIFT)
  342. #define SDIO_DCTRL_DBLOCKSIZE_4 (0x4 << SDIO_DCTRL_DBLOCKSIZE_SHIFT)
  343. #define SDIO_DCTRL_DBLOCKSIZE_5 (0x5 << SDIO_DCTRL_DBLOCKSIZE_SHIFT)
  344. #define SDIO_DCTRL_DBLOCKSIZE_6 (0x6 << SDIO_DCTRL_DBLOCKSIZE_SHIFT)
  345. #define SDIO_DCTRL_DBLOCKSIZE_7 (0x7 << SDIO_DCTRL_DBLOCKSIZE_SHIFT)
  346. #define SDIO_DCTRL_DBLOCKSIZE_8 (0x8 << SDIO_DCTRL_DBLOCKSIZE_SHIFT)
  347. #define SDIO_DCTRL_DBLOCKSIZE_9 (0x9 << SDIO_DCTRL_DBLOCKSIZE_SHIFT)
  348. #define SDIO_DCTRL_DBLOCKSIZE_10 (0xA << SDIO_DCTRL_DBLOCKSIZE_SHIFT)
  349. #define SDIO_DCTRL_DBLOCKSIZE_11 (0xB << SDIO_DCTRL_DBLOCKSIZE_SHIFT)
  350. #define SDIO_DCTRL_DBLOCKSIZE_12 (0xC << SDIO_DCTRL_DBLOCKSIZE_SHIFT)
  351. #define SDIO_DCTRL_DBLOCKSIZE_13 (0xD << SDIO_DCTRL_DBLOCKSIZE_SHIFT)
  352. #define SDIO_DCTRL_DBLOCKSIZE_14 (0xE << SDIO_DCTRL_DBLOCKSIZE_SHIFT)
  353. #define SDIO_DCTRL_DMAEN (1 << 3)
  354. #define SDIO_DCTRL_DTMODE (1 << 2)
  355. #define SDIO_DCTRL_DTDIR (1 << 1)
  356. #define SDIO_DCTRL_DTEN (1 << 0)
  357. #define SDIO_STA_CEATAEND (1 << 23)
  358. #define SDIO_STA_SDIOIT (1 << 22)
  359. #define SDIO_STA_RXDAVL (1 << 21)
  360. #define SDIO_STA_TXDAVL (1 << 20)
  361. #define SDIO_STA_RXFIFOE (1 << 19)
  362. #define SDIO_STA_TXFIFOE (1 << 18)
  363. #define SDIO_STA_RXFIFOF (1 << 17)
  364. #define SDIO_STA_TXFIFOF (1 << 16)
  365. #define SDIO_STA_RXFIFOHF (1 << 15)
  366. #define SDIO_STA_TXFIFOHE (1 << 14)
  367. #define SDIO_STA_RXACT (1 << 13)
  368. #define SDIO_STA_TXACT (1 << 12)
  369. #define SDIO_STA_CMDACT (1 << 11)
  370. #define SDIO_STA_DBCKEND (1 << 10)
  371. #define SDIO_STA_STBITERR (1 << 9)
  372. #define SDIO_STA_DATAEND (1 << 8)
  373. #define SDIO_STA_CMDSENT (1 << 7)
  374. #define SDIO_STA_CMDREND (1 << 6)
  375. #define SDIO_STA_RXOVERR (1 << 5)
  376. #define SDIO_STA_TXUNDERR (1 << 4)
  377. #define SDIO_STA_DTIMEOUT (1 << 3)
  378. #define SDIO_STA_CTIMEOUT (1 << 2)
  379. #define SDIO_STA_DCRCFAIL (1 << 1)
  380. #define SDIO_STA_CCRCFAIL (1 << 0)
  381. #define SDIO_ICR_CEATAENDC (1 << 23)
  382. #define SDIO_ICR_SDIOITC (1 << 22)
  383. #define SDIO_ICR_DBCKENDC (1 << 10)
  384. #define SDIO_ICR_STBITERRC (1 << 9)
  385. #define SDIO_ICR_DATAENDC (1 << 8)
  386. #define SDIO_ICR_CMDSENTC (1 << 7)
  387. #define SDIO_ICR_CMDRENDC (1 << 6)
  388. #define SDIO_ICR_RXOVERRC (1 << 5)
  389. #define SDIO_ICR_TXUNDERRC (1 << 4)
  390. #define SDIO_ICR_DTIMEOUTC (1 << 3)
  391. #define SDIO_ICR_CTIMEOUTC (1 << 2)
  392. #define SDIO_ICR_DCRCFAILC (1 << 1)
  393. #define SDIO_ICR_CCRCFAILC (1 << 0)
  394. #define SDIO_MASK_CEATAENDIE (1 << 23)
  395. #define SDIO_MASK_SDIOITIE (1 << 22)
  396. #define SDIO_MASK_RXDAVLIE (1 << 21)
  397. #define SDIO_MASK_TXDAVLIE (1 << 20)
  398. #define SDIO_MASK_RXFIFOEIE (1 << 19)
  399. #define SDIO_MASK_TXFIFOEIE (1 << 18)
  400. #define SDIO_MASK_RXFIFOFIE (1 << 17)
  401. #define SDIO_MASK_TXFIFOFIE (1 << 16)
  402. #define SDIO_MASK_RXFIFOHFIE (1 << 15)
  403. #define SDIO_MASK_TXFIFOHEIE (1 << 14)
  404. #define SDIO_MASK_RXACTIE (1 << 13)
  405. #define SDIO_MASK_TXACTIE (1 << 12)
  406. #define SDIO_MASK_CMDACTIE (1 << 11)
  407. #define SDIO_MASK_DBCKENDIE (1 << 10)
  408. #define SDIO_MASK_STBITERRIE (1 << 9)
  409. #define SDIO_MASK_DATAENDIE (1 << 8)
  410. #define SDIO_MASK_CMDSENTIE (1 << 7)
  411. #define SDIO_MASK_CMDRENDIE (1 << 6)
  412. #define SDIO_MASK_RXOVERRIE (1 << 5)
  413. #define SDIO_MASK_TXUNDERRIE (1 << 4)
  414. #define SDIO_MASK_DTIMEOUTIE (1 << 3)
  415. #define SDIO_MASK_CTIMEOUTIE (1 << 2)
  416. #define SDIO_MASK_DCRCFAILIE (1 << 1)
  417. #define SDIO_MASK_CCRCFAILIE (1 << 0)
  418. /* Timers */
  419. #define TIM1_BASE (0x40010000)
  420. #define TIM1_CR1 (*(volatile uint32_t *)(TIM1_BASE + 0x00))
  421. #define TIM1_DIER (*(volatile uint32_t *)(TIM1_BASE + 0x0c))
  422. #define TIM1_SR (*(volatile uint32_t *)(TIM1_BASE + 0x10))
  423. #define TIM1_EGR (*(volatile uint16_t *)(TIM1_BASE + 0x14))
  424. #define TIM1_CCMR1 (*(volatile uint16_t *)(TIM1_BASE + 0x18))
  425. #define TIM1_CCMR2 (*(volatile uint16_t *)(TIM1_BASE + 0x1c))
  426. #define TIM1_CCER (*(volatile uint16_t *)(TIM1_BASE + 0x20))
  427. #define TIM1_CNT (*(volatile uint16_t *)(TIM1_BASE + 0x24))
  428. #define TIM1_PSC (*(volatile uint16_t *)(TIM1_BASE + 0x28))
  429. #define TIM1_ARR (*(volatile uint16_t *)(TIM1_BASE + 0x2c))
  430. #define TIM1_CCR1 (*(volatile uint32_t *)(TIM1_BASE + 0x34))
  431. #define TIM1_CCR2 (*(volatile uint32_t *)(TIM1_BASE + 0x3C))
  432. #define TIM1_CCR3 (*(volatile uint32_t *)(TIM1_BASE + 0x38))
  433. #define TIM1_CCR4 (*(volatile uint32_t *)(TIM1_BASE + 0x40))
  434. #define TIM2_BASE (0x40000000)
  435. #define TIM2_CR1 (*(volatile uint32_t *)(TIM2_BASE + 0x00))
  436. #define TIM2_DIER (*(volatile uint32_t *)(TIM2_BASE + 0x0c))
  437. #define TIM2_SR (*(volatile uint32_t *)(TIM2_BASE + 0x10))
  438. #define TIM2_EGR (*(volatile uint16_t *)(TIM2_BASE + 0x14))
  439. #define TIM2_CCMR1 (*(volatile uint16_t *)(TIM2_BASE + 0x18))
  440. #define TIM2_CCMR2 (*(volatile uint16_t *)(TIM2_BASE + 0x1c))
  441. #define TIM2_CCER (*(volatile uint16_t *)(TIM2_BASE + 0x20))
  442. #define TIM2_CNT (*(volatile uint32_t *)(TIM2_BASE + 0x24))
  443. #define TIM2_PSC (*(volatile uint32_t *)(TIM2_BASE + 0x28))
  444. #define TIM2_ARR (*(volatile uint32_t *)(TIM2_BASE + 0x2c))
  445. #define TIM2_CCR1 (*(volatile uint32_t *)(TIM2_BASE + 0x34))
  446. #define TIM2_CCR2 (*(volatile uint32_t *)(TIM2_BASE + 0x3C))
  447. #define TIM2_CCR3 (*(volatile uint32_t *)(TIM2_BASE + 0x38))
  448. #define TIM2_CCR4 (*(volatile uint32_t *)(TIM2_BASE + 0x40))
  449. #define TIM3_BASE (0x40000400)
  450. #define TIM3_CR1 (*(volatile uint32_t *)(TIM3_BASE + 0x00))
  451. #define TIM3_DIER (*(volatile uint32_t *)(TIM3_BASE + 0x0c))
  452. #define TIM3_SR (*(volatile uint32_t *)(TIM3_BASE + 0x10))
  453. #define TIM3_EGR (*(volatile uint16_t *)(TIM3_BASE + 0x14))
  454. #define TIM3_CCMR1 (*(volatile uint16_t *)(TIM3_BASE + 0x18))
  455. #define TIM3_CCMR2 (*(volatile uint16_t *)(TIM3_BASE + 0x1c))
  456. #define TIM3_CCER (*(volatile uint16_t *)(TIM3_BASE + 0x20))
  457. #define TIM3_CNT (*(volatile uint16_t *)(TIM3_BASE + 0x24))
  458. #define TIM3_PSC (*(volatile uint16_t *)(TIM3_BASE + 0x28))
  459. #define TIM3_ARR (*(volatile uint16_t *)(TIM3_BASE + 0x2c))
  460. #define TIM3_CCR1 (*(volatile uint32_t *)(TIM3_BASE + 0x34))
  461. #define TIM3_CCR2 (*(volatile uint32_t *)(TIM3_BASE + 0x3C))
  462. #define TIM3_CCR3 (*(volatile uint32_t *)(TIM3_BASE + 0x38))
  463. #define TIM3_CCR4 (*(volatile uint32_t *)(TIM3_BASE + 0x40))
  464. #define TIM4_BASE (0x40000800)
  465. #define TIM4_CR1 (*(volatile uint32_t *)(TIM4_BASE + 0x00))
  466. #define TIM4_DIER (*(volatile uint32_t *)(TIM4_BASE + 0x0c))
  467. #define TIM4_SR (*(volatile uint32_t *)(TIM4_BASE + 0x10))
  468. #define TIM4_CCMR1 (*(volatile uint16_t *)(TIM4_BASE + 0x18))
  469. #define TIM4_CCMR2 (*(volatile uint16_t *)(TIM4_BASE + 0x1c))
  470. #define TIM4_CCER (*(volatile uint16_t *)(TIM4_BASE + 0x20))
  471. #define TIM4_CNT (*(volatile uint16_t *)(TIM4_BASE + 0x24))
  472. #define TIM4_PSC (*(volatile uint16_t *)(TIM4_BASE + 0x28))
  473. #define TIM4_ARR (*(volatile uint16_t *)(TIM4_BASE + 0x2c))
  474. #define TIM_DIER_UIE (1 << 0)
  475. #define TIM_SR_UIF (1 << 0)
  476. #define TIM_CR1_CLOCK_ENABLE (1 << 0)
  477. #define TIM_CR1_UPD_RS (1 << 2)
  478. #define TIM_CR1_ARPE (1 << 7)
  479. #define TIM_CCER_CC1_ENABLE (1 << 0)
  480. #define TIM_CCER_CC2_ENABLE (1 << 4)
  481. #define TIM_CCER_CC3_ENABLE (1 << 8)
  482. #define TIM_CCER_CC4_ENABLE (1 << 12)
  483. #define TIM_CCMR1_OC1M_PWM1 (0x06 << 4) | (1 << 3)
  484. #define TIM_CCMR1_OC2M_PWM1 (0x06 << 12) | (1 << 11)
  485. #define TIM_CCMR2_OC3M_PWM1 (0x06 << 4) | (1 << 3)
  486. #define TIM_CCMR2_OC4M_PWM1 (0x06 << 12) | (1 << 11)
  487. #define AHB1_CLOCK_ER (*(volatile uint32_t *)(0x40023830))
  488. #define GPIOD_AHB1_CLOCK_ER (1 << 3)
  489. /* ADC */
  490. #define ADC1_BASE (0x40012000)
  491. #define ADC_COM_BASE (0x40012300)
  492. #define ADC_COM_CCR (*(volatile uint32_t *)(ADC_COM_BASE + 0x04))
  493. #define ADC1_SR (*(volatile uint32_t *)(ADC1_BASE + 0x00))
  494. #define ADC1_CR1 (*(volatile uint32_t *)(ADC1_BASE + 0x04))
  495. #define ADC1_CR2 (*(volatile uint32_t *)(ADC1_BASE + 0x08))
  496. #define ADC1_SMPR1 (*(volatile uint32_t *)(ADC1_BASE + 0x0c))
  497. #define ADC1_SMPR2 (*(volatile uint32_t *)(ADC1_BASE + 0x10))
  498. #define ADC1_SQR3 (*(volatile uint32_t *)(ADC1_BASE + 0x34))
  499. #define ADC1_DR (*(volatile uint32_t *)(ADC1_BASE + 0x4c))
  500. #define ADC_CR1_SCAN (1 << 8)
  501. #define ADC_CR2_EN (1 << 0)
  502. #define ADC_CR2_CONT (1 << 1)
  503. #define ADC_CR2_SWSTART (1 << 30)
  504. #define ADC_SR_EOC (1 << 1)
  505. #define ADC_SMPR_SMP_480CYC (0x7)
  506. /* Reboot */
  507. #define AIRCR *(volatile uint32_t *)(0xE000ED0C)
  508. #define AIRCR_VKEY (0x05FA << 16)
  509. # define AIRCR_SYSRESETREQ (1 << 2)
  510. static inline void reboot(void)
  511. {
  512. AIRCR = AIRCR_SYSRESETREQ | AIRCR_VKEY;
  513. }
  514. #endif