f1_regs.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /*
  2. * stm32/f1_regs.h
  3. *
  4. * Core and peripheral register definitions.
  5. *
  6. * Written & released by Keir Fraser <keir.xen@gmail.com>
  7. *
  8. * This is free and unencumbered software released into the public domain.
  9. * See the file COPYING for more details, or visit <http://unlicense.org>.
  10. */
  11. struct dbg {
  12. uint32_t mcu_idcode; /* 00: MCU ID code */
  13. uint32_t mcu_cr; /* 04: Debug MCU configuration */
  14. };
  15. #define DBG_BASE 0xe0042000
  16. /* Flash memory interface */
  17. struct flash {
  18. uint32_t acr; /* 00: Flash access control */
  19. uint32_t keyr; /* 04: FPEC key */
  20. uint32_t optkeyr; /* 08: Flash OPTKEY */
  21. uint32_t sr; /* 0C: Flash status */
  22. uint32_t cr; /* 10: Flash control */
  23. uint32_t ar; /* 14: Flash address */
  24. uint32_t rsvd; /* 18: - */
  25. uint32_t obr; /* 1C: Option byte */
  26. uint32_t wrpr; /* 20: Write protection */
  27. };
  28. #define FLASH_UNLOCK_KEY1 0x45670123
  29. #define FLASH_UNLOCK_KEY2 0xcdef89ab
  30. #define FLASH_ACR_PRFTBS (1u<< 5)
  31. #define FLASH_ACR_PRFTBE (1u<< 4)
  32. #define FLASH_ACR_HLFCYA (1u<< 3)
  33. #define FLASH_ACR_LATENCY(w) ((w)<<0) /* wait states */
  34. #define FLASH_SR_EOP (1u<< 5)
  35. #define FLASH_SR_WRPRTERR (1u<< 4)
  36. #define FLASH_SR_PGERR (1u<< 2)
  37. #define FLASH_SR_BSY (1u<< 0)
  38. #define FLASH_CR_EOPIE (1u<<12)
  39. #define FLASH_CR_ERRIE (1u<<10)
  40. #define FLASH_CR_OPTWRE (1u<< 9)
  41. #define FLASH_CR_LOCK (1u<< 7)
  42. #define FLASH_CR_STRT (1u<< 6)
  43. #define FLASH_CR_OPTER (1u<< 5)
  44. #define FLASH_CR_OPTPG (1u<< 4)
  45. #define FLASH_CR_MER (1u<< 2)
  46. #define FLASH_CR_PER (1u<< 1)
  47. #define FLASH_CR_PG (1u<< 0)
  48. #define FLASH_BASE 0x40022000
  49. /* Power control */
  50. struct pwr {
  51. uint32_t cr; /* 00: Power control */
  52. uint32_t csr; /* 04: Power control/status */
  53. };
  54. #define PWR_CR_DBP (1u<< 8)
  55. #define PWR_BASE 0x40007000
  56. /* Backup */
  57. struct bkp {
  58. uint32_t _0[1]; /* 00: - */
  59. uint32_t dr1[10]; /* 04-28: Data block #1 */
  60. uint32_t rtccr; /* 2C: RTC clock calibration */
  61. uint32_t cr; /* 30: Control */
  62. uint32_t csr; /* 34: Control/status */
  63. uint32_t _1[2]; /* 38-3C: - */
  64. uint32_t dr2[32]; /* 40-BC: Data block #2 */
  65. };
  66. #define BKP_BASE 0x40006c00
  67. /* Reset and clock control */
  68. struct rcc {
  69. uint32_t cr; /* 00: Clock control */
  70. uint32_t cfgr; /* 04: Clock configuration */
  71. uint32_t cir; /* 08: Clock interrupt */
  72. uint32_t apb2rstr; /* 0C: APB2 peripheral reset */
  73. uint32_t apb1rstr; /* 10: APB1 peripheral reset */
  74. uint32_t ahbenr; /* 14: AHB periphernal clock enable */
  75. uint32_t apb2enr; /* 18: APB2 peripheral clock enable */
  76. uint32_t apb1enr; /* 1C: APB1 peripheral clock enable */
  77. uint32_t bdcr; /* 20: Backup domain control */
  78. uint32_t csr; /* 24: Control/status */
  79. uint32_t ahbrstr; /* 28: AHB peripheral clock reset */
  80. uint32_t cfgr2; /* 2C: Clock configuration 2 */
  81. };
  82. #define RCC_CR_PLL3RDY (1u<<29)
  83. #define RCC_CR_PLL3ON (1u<<28)
  84. #define RCC_CR_PLL2RDY (1u<<27)
  85. #define RCC_CR_PLL2ON (1u<<26)
  86. #define RCC_CR_PLLRDY (1u<<25)
  87. #define RCC_CR_PLLON (1u<<24)
  88. #define RCC_CR_CSSON (1u<<19)
  89. #define RCC_CR_HSEBYP (1u<<18)
  90. #define RCC_CR_HSERDY (1u<<17)
  91. #define RCC_CR_HSEON (1u<<16)
  92. #define RCC_CR_HSIRDY (1u<<1)
  93. #define RCC_CR_HSION (1u<<0)
  94. #define RCC_CFGR_PLLMUL(x) (((x)-2)<<18)
  95. #define RCC_CFGR_PLLXTPRE (1u<<17)
  96. #define RCC_CFGR_PLLSRC_HSI (0u<<16)
  97. #define RCC_CFGR_PLLSRC_PREDIV1 (1u<<16)
  98. #define RCC_CFGR_ADCPRE_DIV8 (3u<<14)
  99. #define RCC_CFGR_PPRE1_DIV2 (4u<<8)
  100. #define RCC_CFGR_SWS_HSI (0u<<2)
  101. #define RCC_CFGR_SWS_HSE (1u<<2)
  102. #define RCC_CFGR_SWS_PLL (2u<<2)
  103. #define RCC_CFGR_SWS_MASK (3u<<2)
  104. #define RCC_CFGR_SW_HSI (0u<<0)
  105. #define RCC_CFGR_SW_HSE (1u<<0)
  106. #define RCC_CFGR_SW_PLL (2u<<0)
  107. #define RCC_CFGR_SW_MASK (3u<<0)
  108. #define RCC_AHBENR_ETHMACRXEN (1u<<16)
  109. #define RCC_AHBENR_ETHMACTXEN (1u<<15)
  110. #define RCC_AHBENR_ETHMACEN (1u<<14)
  111. #define RCC_AHBENR_OTGFSEN (1u<<12)
  112. #define RCC_AHBENR_CRCEN (1u<< 6)
  113. #define RCC_AHBENR_FLITFEN (1u<< 4)
  114. #define RCC_AHBENR_SRAMEN (1u<< 2)
  115. #define RCC_AHBENR_DMA2EN (1u<< 1)
  116. #define RCC_AHBENR_DMA1EN (1u<< 0)
  117. #define RCC_APB1ENR_DACEN (1u<<29)
  118. #define RCC_APB1ENR_PWREN (1u<<28)
  119. #define RCC_APB1ENR_BKPEN (1u<<27)
  120. #define RCC_APB1ENR_CAN2EN (1u<<26)
  121. #define RCC_APB1ENR_CAN1EN (1u<<25)
  122. #define RCC_APB1ENR_USBEN (1u<<23)
  123. #define RCC_APB1ENR_I2C2EN (1u<<22)
  124. #define RCC_APB1ENR_I2C1EN (1u<<21)
  125. #define RCC_APB1ENR_USART5EN (1u<<20)
  126. #define RCC_APB1ENR_USART4EN (1u<<19)
  127. #define RCC_APB1ENR_USART3EN (1u<<18)
  128. #define RCC_APB1ENR_USART2EN (1u<<17)
  129. #define RCC_APB1ENR_SPI3EN (1u<<15)
  130. #define RCC_APB1ENR_SPI2EN (1u<<14)
  131. #define RCC_APB1ENR_WWDGEN (1u<<11)
  132. #define RCC_APB1ENR_TIM7EN (1u<< 5)
  133. #define RCC_APB1ENR_TIM6EN (1u<< 4)
  134. #define RCC_APB1ENR_TIM5EN (1u<< 3)
  135. #define RCC_APB1ENR_TIM4EN (1u<< 2)
  136. #define RCC_APB1ENR_TIM3EN (1u<< 1)
  137. #define RCC_APB1ENR_TIM2EN (1u<< 0)
  138. #define RCC_APB2ENR_USART1EN (1u<<14)
  139. #define RCC_APB2ENR_SPI1EN (1u<<12)
  140. #define RCC_APB2ENR_TIM1EN (1u<<11)
  141. #define RCC_APB2ENR_ADC2EN (1u<<10)
  142. #define RCC_APB2ENR_ADC1EN (1u<< 9)
  143. #define RCC_APB2ENR_IOPFEN (1u<< 7)
  144. #define RCC_APB2ENR_IOPEEN (1u<< 6)
  145. #define RCC_APB2ENR_IOPDEN (1u<< 5)
  146. #define RCC_APB2ENR_IOPCEN (1u<< 4)
  147. #define RCC_APB2ENR_IOPBEN (1u<< 3)
  148. #define RCC_APB2ENR_IOPAEN (1u<< 2)
  149. #define RCC_APB2ENR_AFIOEN (1u<< 0)
  150. #define RCC_CSR_LPWRRSTF (1u<<31)
  151. #define RCC_CSR_WWDGRSTF (1u<<30)
  152. #define RCC_CSR_IWDGRSTF (1u<<29)
  153. #define RCC_CSR_SFTRSTF (1u<<28)
  154. #define RCC_CSR_PORRSTF (1u<<27)
  155. #define RCC_CSR_PINRSTF (1u<<26)
  156. #define RCC_CSR_RMVF (1u<<24)
  157. #define RCC_CSR_LSIRDY (1u<< 1)
  158. #define RCC_CSR_LSION (1u<< 0)
  159. #define RCC_AHBRSTR_ETHMACRST (1u<<14)
  160. #define RCC_AHBRSTR_OTGFSRST (1u<<12)
  161. #define RCC_BASE 0x40021000
  162. /* General-purpose I/O */
  163. struct gpio {
  164. uint32_t crl; /* 00: Port configuration low */
  165. uint32_t crh; /* 04: Port configuration high */
  166. uint32_t idr; /* 08: Port input data */
  167. uint32_t odr; /* 0C: Port output data */
  168. uint32_t bsrr; /* 10: Port bit set/reset */
  169. uint32_t brr; /* 14: Port bit reset */
  170. uint32_t lckr; /* 18: Port configuration lock */
  171. };
  172. #define _GPI_pulled(level) (0x8u|((level)<<4))
  173. #define GPI_analog 0x0u
  174. #define GPI_floating 0x4u
  175. #define GPI_pull_down _GPI_pulled(LOW)
  176. #define GPI_pull_up _GPI_pulled(HIGH)
  177. #define GPO_pushpull(speed,level) (0x0u|(speed)|((level)<<4))
  178. #define GPO_opendrain(speed,level) (0x4u|(speed)|((level)<<4))
  179. #define AFO_pushpull(speed) (0x8u|(speed))
  180. #define AFO_opendrain(speed) (0xcu|(speed))
  181. #define _2MHz 2
  182. #define _10MHz 1
  183. #define _50MHz 3
  184. #define IOSPD_LOW _2MHz
  185. #define IOSPD_MED _10MHz
  186. #define IOSPD_HIGH _50MHz
  187. #define LOW 0
  188. #define HIGH 1
  189. #define GPIOA_BASE 0x40010800
  190. #define GPIOB_BASE 0x40010c00
  191. #define GPIOC_BASE 0x40011000
  192. #define GPIOD_BASE 0x40011400
  193. #define GPIOE_BASE 0x40011800
  194. #define GPIOF_BASE 0x40011c00
  195. #define GPIOG_BASE 0x40012000
  196. /* Alternative-function I/O */
  197. struct afio {
  198. uint32_t evcr; /* 00: Event control */
  199. uint32_t mapr; /* 04: AF remap and debug I/O configuration */
  200. uint32_t exticr1; /* 08: External interrupt configuration #1 */
  201. uint32_t exticr2; /* 0C: External interrupt configuration #2 */
  202. uint32_t exticr3; /* 10: External interrupt configuration #3 */
  203. uint32_t exticr4; /* 14: External interrupt configuration #4 */
  204. uint32_t rsvd; /* 18: - */
  205. uint32_t mapr2; /* 1C: AF remap and debug I/O configuration #2 */
  206. };
  207. #define AFIO_MAPR_SWJ_ON_JTAG_OFF (2u<<24)
  208. #define AFIO_MAPR_SWJ_CFG_DISABLED (4u<<24)
  209. #define AFIO_MAPR_TIM4_REMAP_FULL (1u<<12)
  210. #define AFIO_MAPR_TIM3_REMAP_FULL (3u<<10)
  211. #define AFIO_MAPR_TIM3_REMAP_PARTIAL (2u<<10)
  212. #define AFIO_MAPR_TIM2_REMAP_FULL (3u<< 8)
  213. #define AFIO_MAPR_TIM2_REMAP_PARTIAL_1 (1u<< 8)
  214. #define AFIO_MAPR_TIM2_REMAP_PARTIAL_2 (2u<< 8)
  215. #define AFIO_MAPR_TIM1_REMAP_FULL (3u<< 6)
  216. #define AFIO_MAPR_TIM1_REMAP_PARTIAL (1u<< 6)
  217. #define AFIO_MAPR_USART3_REMAP_FULL (3u<< 4)
  218. #define AFIO_MAPR_USART3_REMAP_PARTIAL (1u<< 4)
  219. #define AFIO_BASE 0x40010000
  220. #define EXTI_BASE 0x40010400
  221. /* DMA */
  222. struct dma_chn {
  223. uint32_t cr; /* +00: Configuration */
  224. uint32_t ndtr; /* +04: Number of data */
  225. uint32_t par; /* +08: Peripheral address */
  226. uint32_t mar; /* +0C: Memory address */
  227. uint32_t rsvd; /* +10: - */
  228. };
  229. struct dma {
  230. uint32_t isr; /* 00: Interrupt status */
  231. uint32_t ifcr; /* 04: Interrupt flag clear */
  232. struct dma_chn ch1; /* 08: Channel 1 */
  233. struct dma_chn ch2; /* 1C: Channel 2 */
  234. struct dma_chn ch3; /* 30: Channel 3 */
  235. struct dma_chn ch4; /* 44: Channel 4 */
  236. struct dma_chn ch5; /* 58: Channel 5 */
  237. struct dma_chn ch6; /* 6C: Channel 6 */
  238. struct dma_chn ch7; /* 80: Channel 7 */
  239. };
  240. /* n=1..7 */
  241. #define DMA_ISR_TEIF(n) (8u<<(((n)-1)*4))
  242. #define DMA_ISR_HTIF(n) (4u<<(((n)-1)*4))
  243. #define DMA_ISR_TCIF(n) (2u<<(((n)-1)*4))
  244. #define DMA_ISR_GIF(n) (1u<<(((n)-1)*4))
  245. /* n=1..7 */
  246. #define DMA_IFCR_CTEIF(n) (8u<<(((n)-1)*4))
  247. #define DMA_IFCR_CHTIF(n) (4u<<(((n)-1)*4))
  248. #define DMA_IFCR_CTCIF(n) (2u<<(((n)-1)*4))
  249. #define DMA_IFCR_CGIF(n) (1u<<(((n)-1)*4))
  250. #define DMA_CR_MEM2MEM (1u<<14)
  251. #define DMA_CR_PL_LOW (0u<<12)
  252. #define DMA_CR_PL_MEDIUM (1u<<12)
  253. #define DMA_CR_PL_HIGH (2u<<12)
  254. #define DMA_CR_PL_V_HIGH (3u<<12)
  255. #define DMA_CR_MSIZE_8BIT (0u<<10)
  256. #define DMA_CR_MSIZE_16BIT (1u<<10)
  257. #define DMA_CR_MSIZE_32BIT (2u<<10)
  258. #define DMA_CR_PSIZE_8BIT (0u<< 8)
  259. #define DMA_CR_PSIZE_16BIT (1u<< 8)
  260. #define DMA_CR_PSIZE_32BIT (2u<< 8)
  261. #define DMA_CR_MINC (1u<< 7)
  262. #define DMA_CR_PINC (1u<< 6)
  263. #define DMA_CR_CIRC (1u<< 5)
  264. #define DMA_CR_DIR_P2M (0u<< 4)
  265. #define DMA_CR_DIR_M2P (1u<< 4)
  266. #define DMA_CR_TEIE (1u<< 3)
  267. #define DMA_CR_HTIE (1u<< 2)
  268. #define DMA_CR_TCIE (1u<< 1)
  269. #define DMA_CR_EN (1u<< 0)
  270. #define DMA1_BASE 0x40020000
  271. #define DMA2_BASE 0x40020400
  272. #define TIM1_BASE 0x40012c00
  273. #define TIM2_BASE 0x40000000
  274. #define TIM3_BASE 0x40000400
  275. #define TIM4_BASE 0x40000800
  276. #define TIM5_BASE 0x40000c00
  277. #define TIM6_BASE 0x40001000
  278. #define TIM7_BASE 0x40001400
  279. /* I2C */
  280. struct i2c {
  281. uint32_t cr1; /* 00: Control 1 */
  282. uint32_t cr2; /* 04: Control 2 */
  283. uint32_t oar1; /* 08: Own address 1 */
  284. uint32_t oar2; /* 0C: Own address 2 */
  285. uint32_t dr; /* 10: Data */
  286. uint32_t sr1; /* 14: Status 1 */
  287. uint32_t sr2; /* 18: Status 2 */
  288. uint32_t ccr; /* 1C: Clock control */
  289. uint32_t trise; /* 20: Rise time */
  290. };
  291. #define I2C_CR1_SWRST (1u<<15)
  292. #define I2C_CR1_ALERT (1u<<13)
  293. #define I2C_CR1_PEC (1u<<12)
  294. #define I2C_CR1_POS (1u<<11)
  295. #define I2C_CR1_ACK (1u<<10)
  296. #define I2C_CR1_STOP (1u<< 9)
  297. #define I2C_CR1_START (1u<< 8)
  298. #define I2C_CR1_NOSTRETCH (1u<< 7)
  299. #define I2C_CR1_ENGC (1u<< 6)
  300. #define I2C_CR1_ENPEC (1u<< 5)
  301. #define I2C_CR1_ENARP (1u<< 4)
  302. #define I2C_CR1_SMBTYPE (1u<< 3)
  303. #define I2C_CR1_SMBUS (1u<< 1)
  304. #define I2C_CR1_PE (1u<< 0)
  305. #define I2C_CR2_LAST (1u<<12)
  306. #define I2C_CR2_DMAEN (1u<<11)
  307. #define I2C_CR2_ITBUFEN (1u<<10)
  308. #define I2C_CR2_ITEVTEN (1u<< 9)
  309. #define I2C_CR2_ITERREN (1u<< 8)
  310. #define I2C_CR2_FREQ(x) (x)
  311. #define I2C_SR1_SMBALERT (1u<<15)
  312. #define I2C_SR1_TIMEOUT (1u<<14)
  313. #define I2C_SR1_PECERR (1u<<12)
  314. #define I2C_SR1_OVR (1u<<11)
  315. #define I2C_SR1_AF (1u<<10)
  316. #define I2C_SR1_ARLO (1u<< 9)
  317. #define I2C_SR1_BERR (1u<< 8)
  318. #define I2C_SR1_ERRORS 0xdf00
  319. #define I2C_SR1_TXE (1u<< 7)
  320. #define I2C_SR1_RXNE (1u<< 6)
  321. #define I2C_SR1_STOPF (1u<< 4)
  322. #define I2C_SR1_ADD10 (1u<< 3)
  323. #define I2C_SR1_BTF (1u<< 2)
  324. #define I2C_SR1_ADDR (1u<< 1)
  325. #define I2C_SR1_SB (1u<< 0)
  326. #define I2C_SR1_EVENTS 0x001f
  327. #define I2C_SR2_PEC(x) ((x)<<15)
  328. #define I2C_SR2_DUALF (1u<< 7)
  329. #define I2C_SR2_SMBHOST (1u<< 6)
  330. #define I2C_SR2_SMBDEFAULT (1u<< 5)
  331. #define I2C_SR2_GENCALL (1u<< 4)
  332. #define I2C_SR2_TRA (1u<< 2)
  333. #define I2C_SR2_BUSY (1u<< 1)
  334. #define I2C_SR2_MSL (1u<< 0)
  335. #define I2C_CCR_FS (1u<<15)
  336. #define I2C_CCR_DUTY (1u<<14)
  337. #define I2C_CCR_CCR(x) (x)
  338. #define I2C1_BASE 0x40005400
  339. #define I2C2_BASE 0x40005800
  340. /* USART */
  341. struct usart {
  342. uint32_t sr; /* 00: Status */
  343. uint32_t dr; /* 04: Data */
  344. uint32_t brr; /* 08: Baud rate */
  345. uint32_t cr1; /* 0C: Control 1 */
  346. uint32_t cr2; /* 10: Control 2 */
  347. uint32_t cr3; /* 14: Control 3 */
  348. uint32_t gtpr; /* 18: Guard time and prescaler */
  349. };
  350. #define USART_SR_CTS (1u<<9)
  351. #define USART_SR_LBD (1u<<8)
  352. #define USART_SR_TXE (1u<<7)
  353. #define USART_SR_TC (1u<<6)
  354. #define USART_SR_RXNE (1u<<5)
  355. #define USART_SR_IDLE (1u<<4)
  356. #define USART_SR_ORE (1u<<3)
  357. #define USART_SR_NE (1u<<2)
  358. #define USART_SR_FE (1u<<1)
  359. #define USART_SR_PE (1u<<0)
  360. #define USART_CR1_UE (1u<<13)
  361. #define USART_CR1_M (1u<<12)
  362. #define USART_CR1_WAKE (1u<<11)
  363. #define USART_CR1_PCE (1u<<10)
  364. #define USART_CR1_PS (1u<< 9)
  365. #define USART_CR1_PEIE (1u<< 8)
  366. #define USART_CR1_TXEIE (1u<< 7)
  367. #define USART_CR1_TCIE (1u<< 6)
  368. #define USART_CR1_RXNEIE (1u<< 5)
  369. #define USART_CR1_IDLEIE (1u<< 4)
  370. #define USART_CR1_TE (1u<< 3)
  371. #define USART_CR1_RE (1u<< 2)
  372. #define USART_CR1_RWU (1u<< 1)
  373. #define USART_CR1_SBK (1u<< 0)
  374. #define USART_CR3_CTSIE (1u<<10)
  375. #define USART_CR3_CTSE (1u<< 9)
  376. #define USART_CR3_RTSE (1u<< 8)
  377. #define USART_CR3_DMAT (1u<< 7)
  378. #define USART_CR3_DMAR (1u<< 6)
  379. #define USART_CR3_SCEN (1u<< 5)
  380. #define USART_CR3_NACK (1u<< 4)
  381. #define USART_CR3_HDSEL (1u<< 3)
  382. #define USART_CR3_IRLP (1u<< 2)
  383. #define USART_CR3_IREN (1u<< 1)
  384. #define USART_CR3_EIE (1u<< 0)
  385. #define USART1_BASE 0x40013800
  386. #define USART2_BASE 0x40004400
  387. #define USART3_BASE 0x40004800
  388. #define USB_BASE 0x40005c00
  389. #define USB_BUF_BASE 0x40006000
  390. #define USB_OTG_FS_BASE 0x50000000
  391. /*
  392. * Local variables:
  393. * mode: C
  394. * c-file-style: "Linux"
  395. * c-basic-offset: 4
  396. * tab-width: 4
  397. * indent-tabs-mode: nil
  398. * End:
  399. */