system.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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 5 // PA5
  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 9 /* GPIOB P9 */
  24. #define I2C1_SCL 8 /* GPIOB P8 */
  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 TIM4_APB1_CLOCK_ER_VAL (1 << 2)
  31. #define PWR_APB1_CLOCK_ER_VAL (1 << 28)
  32. #define SPI2_APB1_CLOCK_ER_VAL (1 << 14)
  33. #define APB2_CLOCK_ER (*(volatile uint32_t *)(0x40023844))
  34. #define APB2_CLOCK_RST (*(volatile uint32_t *)(0x40023824))
  35. #define SYSCFG_APB2_CLOCK_ER (1 << 14)
  36. #define SPI1_APB2_CLOCK_ER_VAL (1 << 12)
  37. #define SDIO_APB2_CLOCK_ER_VAL (1 << 11)
  38. #define ADC1_APB2_CLOCK_ER_VAL (1 << 8)
  39. #define RCC_BACKUP (*(volatile uint32_t *)(0x40023870))
  40. #define RCC_BACKUP_RESET (1 << 16)
  41. #define RCC_BACKUP_RTCEN (1 << 15)
  42. #define RCC_BACKUP_RTCSEL_SHIFT 8
  43. #define RCC_BACKUP_RTCSEL_MASK 0x3
  44. #define RCC_BACKUP_RTCSEL_NONE 0
  45. #define RCC_BACKUP_RTCSEL_LSE 1
  46. #define RCC_BACKUP_RTCSEL_LSI 2
  47. #define RCC_BACKUP_RTCSEL_HSE 3
  48. #define RCC_BACKUP_LSEMOD (1 << 3)
  49. #define RCC_BACKUP_LSEBYP (1 << 2)
  50. #define RCC_BACKUP_LSERDY (1 << 1)
  51. #define RCC_BACKUP_LSEON (1 << 0)
  52. #define RCC_CSR_LSION (1 << 0)
  53. #define RCC_CSR_LSIRDY (1 << 1)
  54. /* EXTI */
  55. #define EXTI_CR_BASE (0x40013808)
  56. #define EXTI_CR0 (*(volatile uint32_t *)(EXTI_CR_BASE + 0x00))
  57. #define EXTI_CR_EXTI0_MASK (0xFFFF)
  58. #define EXTI_IMR (*(volatile uint32_t *)(EXTI_BASE + 0x00))
  59. #define EXTI_EMR (*(volatile uint32_t *)(EXTI_BASE + 0x04))
  60. #define EXTI_RTSR (*(volatile uint32_t *)(EXTI_BASE + 0x08))
  61. #define EXTI_FTSR (*(volatile uint32_t *)(EXTI_BASE + 0x0c))
  62. #define EXTI_SWIER (*(volatile uint32_t *)(EXTI_BASE + 0x10))
  63. #define EXTI_PR (*(volatile uint32_t *)(EXTI_BASE + 0x14))
  64. /* HW RNG */
  65. #define RNG_BASE (0x50060800)
  66. #define RNG_CR (*(volatile uint32_t *)(RNG_BASE + 0x00))
  67. #define RNG_SR (*(volatile uint32_t *)(RNG_BASE + 0x04))
  68. #define RNG_DR (*(volatile uint32_t *)(RNG_BASE + 0x08))
  69. #define RNG_CR_IE (1 << 3)
  70. #define RNG_CR_RNGEN (1 << 2)
  71. #define RNG_SR_DRDY (1 << 0)
  72. #define RNG_SR_CECS (1 << 1)
  73. #define RNG_SR_SECS (1 << 2)
  74. /* SCB for sleep configuration */
  75. #define SCB_SCR (*(volatile uint32_t *)(0xE000ED10))
  76. #define SCB_SCR_SEVONPEND (1 << 4)
  77. #define SCB_SCR_SLEEPDEEP (1 << 2)
  78. #define SCB_SCR_SLEEPONEXIT (1 << 1)
  79. /* Assembly helpers */
  80. #define DMB() __asm__ volatile ("dmb")
  81. #define WFI() __asm__ volatile ("wfi")
  82. #define WFE() __asm__ volatile ("wfe")
  83. #define SEV() __asm__ volatile ("sev")
  84. /* Master clock setting */
  85. void clock_pll_on(void);
  86. void clock_pll_off(void);
  87. /* NVIC */
  88. /* NVIC ISER Base register (Cortex-M) */
  89. #define NVIC_RTC_IRQ (3)
  90. #define NVIC_TIM2_IRQN (28)
  91. #define NVIC_ISER_BASE (0xE000E100)
  92. #define NVIC_ICER_BASE (0xE000E180)
  93. #define NVIC_ICPR_BASE (0xE000E280)
  94. #define NVIC_IPRI_BASE (0xE000E400)
  95. #define NVIC_EXTI0_IRQ (6)
  96. #define NVIC_EXTI9_5_IRQ (23)
  97. #define NVIC_EXTI15_10_IRQ (40)
  98. static inline void nvic_irq_enable(uint8_t n)
  99. {
  100. int i = n / 32;
  101. volatile uint32_t *nvic_iser = ((volatile uint32_t *)(NVIC_ISER_BASE + 4 * i));
  102. *nvic_iser |= (1 << (n % 32));
  103. }
  104. static inline void nvic_irq_disable(uint8_t n)
  105. {
  106. int i = n / 32;
  107. volatile uint32_t *nvic_icer = ((volatile uint32_t *)(NVIC_ICER_BASE + 4 * i));
  108. *nvic_icer |= (1 << (n % 32));
  109. }
  110. static inline void nvic_irq_setprio(uint8_t n, uint8_t prio)
  111. {
  112. volatile uint8_t *nvic_ipri = ((volatile uint8_t *)(NVIC_IPRI_BASE + n));
  113. *nvic_ipri = prio;
  114. }
  115. static inline void nvic_irq_clear(uint8_t n)
  116. {
  117. int i = n / 32;
  118. volatile uint8_t *nvic_icpr = ((volatile uint8_t *)(NVIC_ICPR_BASE + 4 * i));
  119. *nvic_icpr = (1 << (n % 32));
  120. }
  121. /*** FLASH ***/
  122. #define FLASH_BASE (0x40023C00)
  123. #define FLASH_ACR (*(volatile uint32_t *)(FLASH_BASE + 0x00))
  124. #define FLASH_ACR_ENABLE_DATA_CACHE (1 << 10)
  125. #define FLASH_ACR_ENABLE_INST_CACHE (1 << 9)
  126. /*** SPI ***/
  127. #define SPI1 (0x40013000)
  128. #define SPI1_CR1 (*(volatile uint32_t *)(SPI1))
  129. #define SPI1_CR2 (*(volatile uint32_t *)(SPI1 + 0x04))
  130. #define SPI1_SR (*(volatile uint32_t *)(SPI1 + 0x08))
  131. #define SPI1_DR (*(volatile uint32_t *)(SPI1 + 0x0c))
  132. #define SPI2 (0x40003800)
  133. #define SPI2_CR1 (*(volatile uint32_t *)(SPI2))
  134. #define SPI2_CR2 (*(volatile uint32_t *)(SPI2 + 0x04))
  135. #define SPI2_SR (*(volatile uint32_t *)(SPI2 + 0x08))
  136. #define SPI2_DR (*(volatile uint32_t *)(SPI2 + 0x0c))
  137. #define SPI_CR1_CLOCK_PHASE (1 << 0)
  138. #define SPI_CR1_CLOCK_POLARITY (1 << 1)
  139. #define SPI_CR1_MASTER (1 << 2)
  140. #define SPI_CR1_BAUDRATE (0x07 << 3)
  141. #define SPI_CR1_SPI_EN (1 << 6)
  142. #define SPI_CR1_LSBFIRST (1 << 7)
  143. #define SPI_CR1_SSI (1 << 8)
  144. #define SPI_CR1_SSM (1 << 9)
  145. #define SPI_CR1_16BIT_FORMAT (1 << 11)
  146. #define SPI_CR1_TX_CRC_NEXT (1 << 12)
  147. #define SPI_CR1_HW_CRC_EN (1 << 13)
  148. #define SPI_CR1_BIDIOE (1 << 14)
  149. #define SPI_CR2_SSOE (1 << 2)
  150. #define SPI_SR_RX_NOTEMPTY (1 << 0)
  151. #define SPI_SR_TX_EMPTY (1 << 1)
  152. #define SPI_SR_BUSY (1 << 7)
  153. /*** RCC ***/
  154. #define RCC_CR_PLLRDY (1 << 25)
  155. #define RCC_CR_PLLON (1 << 24)
  156. #define RCC_CR_HSERDY (1 << 17)
  157. #define RCC_CR_HSEON (1 << 16)
  158. #define RCC_CR_HSIRDY (1 << 1)
  159. #define RCC_CR_HSION (1 << 0)
  160. #define RCC_CFGR_SW_HSI 0x0
  161. #define RCC_CFGR_SW_HSE 0x1
  162. #define RCC_CFGR_SW_PLL 0x2
  163. #define RCC_PRESCALER_DIV_NONE 0
  164. #define RCC_PRESCALER_DIV_2 8
  165. #define RCC_PRESCALER_DIV_4 9
  166. #define RCC_PLLCFGR_PLLSRC (1 << 22)
  167. #define AHB1_CLOCK_ER (*(volatile uint32_t *)(0x40023830))
  168. #define GPIOA_AHB1_CLOCK_ER (1 << 0)
  169. #define GPIOB_AHB1_CLOCK_ER (1 << 1)
  170. #define GPIOC_AHB1_CLOCK_ER (1 << 2)
  171. #define GPIOD_AHB1_CLOCK_ER (1 << 3)
  172. #define GPIOE_AHB1_CLOCK_ER (1 << 4)
  173. #define GPIOA_BASE 0x40020000
  174. #define GPIOB_BASE 0x40020400
  175. #define GPIOC_BASE 0x40020800
  176. #define GPIOD_BASE 0x40020C00
  177. #define GPIOE_BASE 0x40021000
  178. #define AHB2_CLOCK_ER (*(volatile uint32_t *)(0x40023834))
  179. #define RNG_AHB2_CLOCK_ER (1 << 6)
  180. /* POWER CONTROL REGISTER */
  181. #define POW_BASE (0x40007000)
  182. #define POW_CR (*(volatile uint32_t *)(POW_BASE + 0x00))
  183. #define POW_SCR (*(volatile uint32_t *)(POW_BASE + 0x04))
  184. #define POW_CR_VOS (1 << 14)
  185. #define POW_CR_FPDS (1 << 9)
  186. #define POW_CR_DPB (1 << 8)
  187. #define POW_CR_CSBF (1 << 3)
  188. #define POW_CR_CWUF (1 << 2)
  189. #define POW_CR_PDDS (1 << 1)
  190. #define POW_CR_LPDS (1 << 0)
  191. #define POW_SCR_BRE (1 << 9)
  192. #define POW_SCR_EWUP (1 << 4)
  193. #define POW_SCR_BRR (1 << 3)
  194. #define POW_SCR_WUF (1 << 0)
  195. /* GPIOS */
  196. #define GPIOA_MODE (*(volatile uint32_t *)(GPIOA_BASE + 0x00))
  197. #define GPIOA_AFL (*(volatile uint32_t *)(GPIOA_BASE + 0x20))
  198. #define GPIOA_AFH (*(volatile uint32_t *)(GPIOA_BASE + 0x24))
  199. #define GPIOA_OSPD (*(volatile uint32_t *)(GPIOA_BASE + 0x08))
  200. #define GPIOA_PUPD (*(volatile uint32_t *)(GPIOA_BASE + 0x0c))
  201. #define GPIOA_BSRR (*(volatile uint32_t *)(GPIOA_BASE + 0x18))
  202. #define GPIOA_ODR (*(volatile uint32_t *)(GPIOA_BASE + 0x14))
  203. #define GPIOA_IDR (*(volatile uint32_t *)(GPIOA_BASE + 0x10))
  204. #define GPIOB_MODE (*(volatile uint32_t *)(GPIOB_BASE + 0x00))
  205. #define GPIOB_AFL (*(volatile uint32_t *)(GPIOB_BASE + 0x20))
  206. #define GPIOB_AFH (*(volatile uint32_t *)(GPIOB_BASE + 0x24))
  207. #define GPIOB_OSPD (*(volatile uint32_t *)(GPIOB_BASE + 0x08))
  208. #define GPIOB_PUPD (*(volatile uint32_t *)(GPIOB_BASE + 0x0c))
  209. #define GPIOB_BSRR (*(volatile uint32_t *)(GPIOB_BASE + 0x18))
  210. #define GPIOB_ODR (*(volatile uint32_t *)(GPIOB_BASE + 0x14))
  211. #define GPIOC_MODE (*(volatile uint32_t *)(GPIOC_BASE + 0x00))
  212. #define GPIOC_OTYPE (*(volatile uint32_t *)(GPIOC_BASE + 0x04))
  213. #define GPIOC_OSPEED (*(volatile uint32_t *)(GPIOC_BASE + 0x08))
  214. #define GPIOC_AFL (*(volatile uint32_t *)(GPIOC_BASE + 0x20))
  215. #define GPIOC_AFH (*(volatile uint32_t *)(GPIOC_BASE + 0x24))
  216. #define GPIOC_OSPD (*(volatile uint32_t *)(GPIOC_BASE + 0x08))
  217. #define GPIOC_PUPD (*(volatile uint32_t *)(GPIOC_BASE + 0x0c))
  218. #define GPIOC_BSRR (*(volatile uint32_t *)(GPIOC_BASE + 0x18))
  219. #define GPIOC_ODR (*(volatile uint32_t *)(GPIOC_BASE + 0x14))
  220. #define GPIOD_MODE (*(volatile uint32_t *)(GPIOD_BASE + 0x00))
  221. #define GPIOD_OTYPE (*(volatile uint32_t *)(GPIOD_BASE + 0x04))
  222. #define GPIOD_OSPEED (*(volatile uint32_t *)(GPIOD_BASE + 0x08))
  223. #define GPIOD_AFL (*(volatile uint32_t *)(GPIOD_BASE + 0x20))
  224. #define GPIOD_AFH (*(volatile uint32_t *)(GPIOD_BASE + 0x24))
  225. #define GPIOD_OSPD (*(volatile uint32_t *)(GPIOD_BASE + 0x08))
  226. #define GPIOD_PUPD (*(volatile uint32_t *)(GPIOD_BASE + 0x0c))
  227. #define GPIOD_BSRR (*(volatile uint32_t *)(GPIOD_BASE + 0x18))
  228. #define GPIOD_ODR (*(volatile uint32_t *)(GPIOD_BASE + 0x14))
  229. #define GPIOE_MODE (*(volatile uint32_t *)(GPIOE_BASE + 0x00))
  230. #define GPIOE_AFL (*(volatile uint32_t *)(GPIOE_BASE + 0x20))
  231. #define GPIOE_AFH (*(volatile uint32_t *)(GPIOE_BASE + 0x24))
  232. #define GPIOE_OSPD (*(volatile uint32_t *)(GPIOE_BASE + 0x08))
  233. #define GPIOE_PUPD (*(volatile uint32_t *)(GPIOE_BASE + 0x0c))
  234. #define GPIOE_BSRR (*(volatile uint32_t *)(GPIOE_BASE + 0x18))
  235. #define GPIOE_ODR (*(volatile uint32_t *)(GPIOE_BASE + 0x14))
  236. #define GPIO_MODE_AF (2)
  237. /* SDIO */
  238. #define SDIO_BASE (0x40012C00)
  239. #define SDIO_POWER (*(volatile uint32_t *)((SDIO_BASE) + 0x00))
  240. #define SDIO_CLKCR (*(volatile uint32_t *)((SDIO_BASE) + 0x04))
  241. #define SDIO_ARG (*(volatile uint32_t *)((SDIO_BASE) + 0x08))
  242. #define SDIO_CMD (*(volatile uint32_t *)((SDIO_BASE) + 0x0C))
  243. #define SDIO_RESPCMD (*(volatile uint32_t *)((SDIO_BASE) + 0x10))
  244. #define SDIO_RESP1 (*(volatile uint32_t *)((SDIO_BASE) + 0x14))
  245. #define SDIO_RESP2 (*(volatile uint32_t *)((SDIO_BASE) + 0x18))
  246. #define SDIO_RESP3 (*(volatile uint32_t *)((SDIO_BASE) + 0x1C))
  247. #define SDIO_RESP4 (*(volatile uint32_t *)((SDIO_BASE) + 0x20))
  248. #define SDIO_DTIMER (*(volatile uint32_t *)((SDIO_BASE) + 0x24))
  249. #define SDIO_DLEN (*(volatile uint32_t *)((SDIO_BASE) + 0x28))
  250. #define SDIO_DCTRL (*(volatile uint32_t *)((SDIO_BASE) + 0x2C))
  251. #define SDIO_DCOUNT (*(volatile uint32_t *)((SDIO_BASE) + 0x30))
  252. #define SDIO_STA (*(volatile uint32_t *)((SDIO_BASE) + 0x34))
  253. #define SDIO_ICR (*(volatile uint32_t *)((SDIO_BASE) + 0x38))
  254. #define SDIO_MASK (*(volatile uint32_t *)((SDIO_BASE) + 0x3C))
  255. #define SDIO_FIFOCNT (*(volatile uint32_t *)((SDIO_BASE) + 0x48))
  256. #define SDIO_FIFO (*(volatile uint32_t *)((SDIO_BASE) + 0x80))
  257. #define SDIO_POWER_PWRCTRL_SHIFT 0
  258. #define SDIO_POWER_PWRCTRL_PWROFF (0x0 << SDIO_POWER_PWRCTRL_SHIFT)
  259. #define SDIO_POWER_PWRCTRL_RSVPWRUP (0x2 << SDIO_POWER_PWRCTRL_SHIFT)
  260. #define SDIO_POWER_PWRCTRL_PWRON (0x3 << SDIO_POWER_PWRCTRL_SHIFT)
  261. #define SDIO_CLKCR_HWFC_EN (1 << 14)
  262. #define SDIO_CLKCR_NEGEDGE (1 << 13)
  263. #define SDIO_CLKCR_WIDBUS_SHIFT 11
  264. #define SDIO_CLKCR_WIDBUS_1 (0x0 << SDIO_CLKCR_WIDBUS_SHIFT)
  265. #define SDIO_CLKCR_WIDBUS_4 (0x1 << SDIO_CLKCR_WIDBUS_SHIFT)
  266. #define SDIO_CLKCR_WIDBUS_8 (0x2 << SDIO_CLKCR_WIDBUS_SHIFT)
  267. #define SDIO_CLKCR_BYPASS (1 << 10)
  268. #define SDIO_CLKCR_PWRSAV (1 << 9)
  269. #define SDIO_CLKCR_CLKEN (1 << 8)
  270. #define SDIO_CLKCR_CLKDIV_SHIFT 0
  271. #define SDIO_CLKCR_CLKDIV_MSK (0xFF << SDIO_CLKCR_CLKDIV_SHIFT)
  272. #define SDIO_CMD_ATACMD (1 << 14)
  273. #define SDIO_CMD_NIEN (1 << 13)
  274. #define SDIO_CMD_ENCMDCOMPL (1 << 12)
  275. #define SDIO_CMD_SDIOSUSPEND (1 << 11)
  276. #define SDIO_CMD_CPSMEN (1 << 10)
  277. #define SDIO_CMD_WAITPEND (1 << 9)
  278. #define SDIO_CMD_WAITINT (1 << 8)
  279. #define SDIO_CMD_WAITRESP_SHIFT 6
  280. #define SDIO_CMD_WAITRESP_NO_0 (0x0 << SDIO_CMD_WAITRESP_SHIFT)
  281. #define SDIO_CMD_WAITRESP_SHORT (0x1 << SDIO_CMD_WAITRESP_SHIFT)
  282. #define SDIO_CMD_WAITRESP_NO_2 (0x2 << SDIO_CMD_WAITRESP_SHIFT)
  283. #define SDIO_CMD_WAITRESP_LONG (0x3 << SDIO_CMD_WAITRESP_SHIFT)
  284. #define SDIO_CMD_CMDINDEX_SHIFT 0
  285. #define SDIO_CMD_CMDINDEX_MSK (0x3F << SDIO_CMD_CMDINDEX_SHIFT)
  286. #define SDIO_RESPCMD_SHIFT 0
  287. #define SDIO_RESPCMD_MSK (0x3F << SDIO_RESPCMD_SHIFT)
  288. #define SDIO_DCTRL_SDIOEN (1 << 11)
  289. #define SDIO_DCTRL_RWMOD (1 << 10)
  290. #define SDIO_DCTRL_RWSTOP (1 << 9)
  291. #define SDIO_DCTRL_RWSTART (1 << 8)
  292. #define SDIO_DCTRL_DBLOCKSIZE_SHIFT 4
  293. #define SDIO_DCTRL_DBLOCKSIZE_0 (0x0 << SDIO_DCTRL_DBLOCKSIZE_SHIFT)
  294. #define SDIO_DCTRL_DBLOCKSIZE_1 (0x1 << SDIO_DCTRL_DBLOCKSIZE_SHIFT)
  295. #define SDIO_DCTRL_DBLOCKSIZE_2 (0x2 << SDIO_DCTRL_DBLOCKSIZE_SHIFT)
  296. #define SDIO_DCTRL_DBLOCKSIZE_3 (0x3 << SDIO_DCTRL_DBLOCKSIZE_SHIFT)
  297. #define SDIO_DCTRL_DBLOCKSIZE_4 (0x4 << SDIO_DCTRL_DBLOCKSIZE_SHIFT)
  298. #define SDIO_DCTRL_DBLOCKSIZE_5 (0x5 << SDIO_DCTRL_DBLOCKSIZE_SHIFT)
  299. #define SDIO_DCTRL_DBLOCKSIZE_6 (0x6 << SDIO_DCTRL_DBLOCKSIZE_SHIFT)
  300. #define SDIO_DCTRL_DBLOCKSIZE_7 (0x7 << SDIO_DCTRL_DBLOCKSIZE_SHIFT)
  301. #define SDIO_DCTRL_DBLOCKSIZE_8 (0x8 << SDIO_DCTRL_DBLOCKSIZE_SHIFT)
  302. #define SDIO_DCTRL_DBLOCKSIZE_9 (0x9 << SDIO_DCTRL_DBLOCKSIZE_SHIFT)
  303. #define SDIO_DCTRL_DBLOCKSIZE_10 (0xA << SDIO_DCTRL_DBLOCKSIZE_SHIFT)
  304. #define SDIO_DCTRL_DBLOCKSIZE_11 (0xB << SDIO_DCTRL_DBLOCKSIZE_SHIFT)
  305. #define SDIO_DCTRL_DBLOCKSIZE_12 (0xC << SDIO_DCTRL_DBLOCKSIZE_SHIFT)
  306. #define SDIO_DCTRL_DBLOCKSIZE_13 (0xD << SDIO_DCTRL_DBLOCKSIZE_SHIFT)
  307. #define SDIO_DCTRL_DBLOCKSIZE_14 (0xE << SDIO_DCTRL_DBLOCKSIZE_SHIFT)
  308. #define SDIO_DCTRL_DMAEN (1 << 3)
  309. #define SDIO_DCTRL_DTMODE (1 << 2)
  310. #define SDIO_DCTRL_DTDIR (1 << 1)
  311. #define SDIO_DCTRL_DTEN (1 << 0)
  312. #define SDIO_STA_CEATAEND (1 << 23)
  313. #define SDIO_STA_SDIOIT (1 << 22)
  314. #define SDIO_STA_RXDAVL (1 << 21)
  315. #define SDIO_STA_TXDAVL (1 << 20)
  316. #define SDIO_STA_RXFIFOE (1 << 19)
  317. #define SDIO_STA_TXFIFOE (1 << 18)
  318. #define SDIO_STA_RXFIFOF (1 << 17)
  319. #define SDIO_STA_TXFIFOF (1 << 16)
  320. #define SDIO_STA_RXFIFOHF (1 << 15)
  321. #define SDIO_STA_TXFIFOHE (1 << 14)
  322. #define SDIO_STA_RXACT (1 << 13)
  323. #define SDIO_STA_TXACT (1 << 12)
  324. #define SDIO_STA_CMDACT (1 << 11)
  325. #define SDIO_STA_DBCKEND (1 << 10)
  326. #define SDIO_STA_STBITERR (1 << 9)
  327. #define SDIO_STA_DATAEND (1 << 8)
  328. #define SDIO_STA_CMDSENT (1 << 7)
  329. #define SDIO_STA_CMDREND (1 << 6)
  330. #define SDIO_STA_RXOVERR (1 << 5)
  331. #define SDIO_STA_TXUNDERR (1 << 4)
  332. #define SDIO_STA_DTIMEOUT (1 << 3)
  333. #define SDIO_STA_CTIMEOUT (1 << 2)
  334. #define SDIO_STA_DCRCFAIL (1 << 1)
  335. #define SDIO_STA_CCRCFAIL (1 << 0)
  336. #define SDIO_ICR_CEATAENDC (1 << 23)
  337. #define SDIO_ICR_SDIOITC (1 << 22)
  338. #define SDIO_ICR_DBCKENDC (1 << 10)
  339. #define SDIO_ICR_STBITERRC (1 << 9)
  340. #define SDIO_ICR_DATAENDC (1 << 8)
  341. #define SDIO_ICR_CMDSENTC (1 << 7)
  342. #define SDIO_ICR_CMDRENDC (1 << 6)
  343. #define SDIO_ICR_RXOVERRC (1 << 5)
  344. #define SDIO_ICR_TXUNDERRC (1 << 4)
  345. #define SDIO_ICR_DTIMEOUTC (1 << 3)
  346. #define SDIO_ICR_CTIMEOUTC (1 << 2)
  347. #define SDIO_ICR_DCRCFAILC (1 << 1)
  348. #define SDIO_ICR_CCRCFAILC (1 << 0)
  349. #define SDIO_MASK_CEATAENDIE (1 << 23)
  350. #define SDIO_MASK_SDIOITIE (1 << 22)
  351. #define SDIO_MASK_RXDAVLIE (1 << 21)
  352. #define SDIO_MASK_TXDAVLIE (1 << 20)
  353. #define SDIO_MASK_RXFIFOEIE (1 << 19)
  354. #define SDIO_MASK_TXFIFOEIE (1 << 18)
  355. #define SDIO_MASK_RXFIFOFIE (1 << 17)
  356. #define SDIO_MASK_TXFIFOFIE (1 << 16)
  357. #define SDIO_MASK_RXFIFOHFIE (1 << 15)
  358. #define SDIO_MASK_TXFIFOHEIE (1 << 14)
  359. #define SDIO_MASK_RXACTIE (1 << 13)
  360. #define SDIO_MASK_TXACTIE (1 << 12)
  361. #define SDIO_MASK_CMDACTIE (1 << 11)
  362. #define SDIO_MASK_DBCKENDIE (1 << 10)
  363. #define SDIO_MASK_STBITERRIE (1 << 9)
  364. #define SDIO_MASK_DATAENDIE (1 << 8)
  365. #define SDIO_MASK_CMDSENTIE (1 << 7)
  366. #define SDIO_MASK_CMDRENDIE (1 << 6)
  367. #define SDIO_MASK_RXOVERRIE (1 << 5)
  368. #define SDIO_MASK_TXUNDERRIE (1 << 4)
  369. #define SDIO_MASK_DTIMEOUTIE (1 << 3)
  370. #define SDIO_MASK_CTIMEOUTIE (1 << 2)
  371. #define SDIO_MASK_DCRCFAILIE (1 << 1)
  372. #define SDIO_MASK_CCRCFAILIE (1 << 0)
  373. /* Timers */
  374. #define TIM2_BASE (0x40000000)
  375. #define TIM2_CR1 (*(volatile uint32_t *)(TIM2_BASE + 0x00))
  376. #define TIM2_DIER (*(volatile uint32_t *)(TIM2_BASE + 0x0c))
  377. #define TIM2_SR (*(volatile uint32_t *)(TIM2_BASE + 0x10))
  378. #define TIM2_CNT (*(volatile uint32_t *)(TIM2_BASE + 0x24))
  379. #define TIM2_PSC (*(volatile uint32_t *)(TIM2_BASE + 0x28))
  380. #define TIM2_ARR (*(volatile uint32_t *)(TIM2_BASE + 0x2c))
  381. #define TIM4_BASE (0x40000800)
  382. #define TIM4_CR1 (*(volatile uint32_t *)(TIM4_BASE + 0x00))
  383. #define TIM4_DIER (*(volatile uint32_t *)(TIM4_BASE + 0x0c))
  384. #define TIM4_SR (*(volatile uint32_t *)(TIM4_BASE + 0x10))
  385. #define TIM4_CCMR1 (*(volatile uint32_t *)(TIM4_BASE + 0x18))
  386. #define TIM4_CCMR2 (*(volatile uint32_t *)(TIM4_BASE + 0x1c))
  387. #define TIM4_CCER (*(volatile uint32_t *)(TIM4_BASE + 0x20))
  388. #define TIM4_PSC (*(volatile uint32_t *)(TIM4_BASE + 0x28))
  389. #define TIM4_ARR (*(volatile uint32_t *)(TIM4_BASE + 0x2c))
  390. #define TIM4_CCR4 (*(volatile uint32_t *)(TIM4_BASE + 0x40))
  391. #define TIM_DIER_UIE (1 << 0)
  392. #define TIM_SR_UIF (1 << 0)
  393. #define TIM_CR1_CLOCK_ENABLE (1 << 0)
  394. #define TIM_CR1_UPD_RS (1 << 2)
  395. #define TIM_CR1_ARPE (1 << 7)
  396. #define TIM_CCER_CC4_ENABLE (1 << 12)
  397. #define TIM_CCMR1_OC1M_PWM1 (0x06 << 4)
  398. #define TIM_CCMR2_OC4M_PWM1 (0x06 << 12)
  399. #define AHB1_CLOCK_ER (*(volatile uint32_t *)(0x40023830))
  400. #define GPIOD_AHB1_CLOCK_ER (1 << 3)
  401. #define GPIOD_BASE 0x40020c00
  402. #define GPIOD_MODE (*(volatile uint32_t *)(GPIOD_BASE + 0x00))
  403. #define GPIOD_OTYPE (*(volatile uint32_t *)(GPIOD_BASE + 0x04))
  404. #define GPIOD_PUPD (*(volatile uint32_t *)(GPIOD_BASE + 0x0c))
  405. #define GPIOD_ODR (*(volatile uint32_t *)(GPIOD_BASE + 0x14))
  406. /* ADC */
  407. #define ADC1_BASE (0x40012000)
  408. #define ADC_COM_BASE (0x40012300)
  409. #define ADC_COM_CCR (*(volatile uint32_t *)(ADC_COM_BASE + 0x04))
  410. #define ADC1_SR (*(volatile uint32_t *)(ADC1_BASE + 0x00))
  411. #define ADC1_CR1 (*(volatile uint32_t *)(ADC1_BASE + 0x04))
  412. #define ADC1_CR2 (*(volatile uint32_t *)(ADC1_BASE + 0x08))
  413. #define ADC1_SMPR1 (*(volatile uint32_t *)(ADC1_BASE + 0x0c))
  414. #define ADC1_SMPR2 (*(volatile uint32_t *)(ADC1_BASE + 0x10))
  415. #define ADC1_SQR3 (*(volatile uint32_t *)(ADC1_BASE + 0x34))
  416. #define ADC1_DR (*(volatile uint32_t *)(ADC1_BASE + 0x4c))
  417. #define ADC_CR1_SCAN (1 << 8)
  418. #define ADC_CR2_EN (1 << 0)
  419. #define ADC_CR2_CONT (1 << 1)
  420. #define ADC_CR2_SWSTART (1 << 30)
  421. #define ADC_SR_EOC (1 << 1)
  422. #define ADC_SMPR_SMP_480CYC (0x7)
  423. /* Reboot */
  424. #define AIRCR *(volatile uint32_t *)(0xE000ED0C)
  425. #define AIRCR_VKEY (0x05FA << 16)
  426. # define AIRCR_SYSRESETREQ (1 << 2)
  427. static inline void reboot(void)
  428. {
  429. AIRCR = AIRCR_SYSRESETREQ | AIRCR_VKEY;
  430. }
  431. #endif