stm32f10x.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * stm32f10x.h
  3. *
  4. * Core and peripheral registers.
  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. /* C pointer types */
  12. #define STK volatile struct stk * const
  13. #define SCB volatile struct scb * const
  14. #define NVIC volatile struct nvic * const
  15. #define FLASH volatile struct flash * const
  16. #define PWR volatile struct pwr * const
  17. #define BKP volatile struct bkp * const
  18. #define RCC volatile struct rcc * const
  19. #define IWDG volatile struct iwdg * const
  20. #define GPIO volatile struct gpio * const
  21. #define AFIO volatile struct afio * const
  22. #define EXTI volatile struct exti * const
  23. #define DMA volatile struct dma * const
  24. #define TIM volatile struct tim * const
  25. #define SPI volatile struct spi * const
  26. #define I2C volatile struct i2c * const
  27. #define USART volatile struct usart * const
  28. #define USB volatile struct usb * const
  29. #define USB_BUFD volatile struct usb_bufd * const
  30. #define USB_BUF volatile uint32_t * const
  31. #define USB_OTG volatile struct usb_otg * const
  32. /* C-accessible registers. */
  33. static STK stk = (struct stk *)STK_BASE;
  34. static SCB scb = (struct scb *)SCB_BASE;
  35. static NVIC nvic = (struct nvic *)NVIC_BASE;
  36. static FLASH flash = (struct flash *)FLASH_BASE;
  37. static PWR pwr = (struct pwr *)PWR_BASE;
  38. static BKP bkp = (struct bkp *)BKP_BASE;
  39. static RCC rcc = (struct rcc *)RCC_BASE;
  40. static IWDG iwdg = (struct iwdg *)IWDG_BASE;
  41. static GPIO gpioa = (struct gpio *)GPIOA_BASE;
  42. static GPIO gpiob = (struct gpio *)GPIOB_BASE;
  43. static GPIO gpioc = (struct gpio *)GPIOC_BASE;
  44. static GPIO gpiod = (struct gpio *)GPIOD_BASE;
  45. static GPIO gpioe = (struct gpio *)GPIOE_BASE;
  46. static GPIO gpiof = (struct gpio *)GPIOF_BASE;
  47. static GPIO gpiog = (struct gpio *)GPIOG_BASE;
  48. static AFIO afio = (struct afio *)AFIO_BASE;
  49. static EXTI exti = (struct exti *)EXTI_BASE;
  50. static DMA dma1 = (struct dma *)DMA1_BASE;
  51. static DMA dma2 = (struct dma *)DMA2_BASE;
  52. static TIM tim1 = (struct tim *)TIM1_BASE;
  53. static TIM tim2 = (struct tim *)TIM2_BASE;
  54. static TIM tim3 = (struct tim *)TIM3_BASE;
  55. static TIM tim4 = (struct tim *)TIM4_BASE;
  56. static TIM tim5 = (struct tim *)TIM5_BASE;
  57. static TIM tim6 = (struct tim *)TIM6_BASE;
  58. static TIM tim7 = (struct tim *)TIM7_BASE;
  59. static SPI spi1 = (struct spi *)SPI1_BASE;
  60. static SPI spi2 = (struct spi *)SPI2_BASE;
  61. static SPI spi3 = (struct spi *)SPI3_BASE;
  62. static I2C i2c1 = (struct i2c *)I2C1_BASE;
  63. static I2C i2c2 = (struct i2c *)I2C2_BASE;
  64. static USART usart1 = (struct usart *)USART1_BASE;
  65. static USART usart2 = (struct usart *)USART2_BASE;
  66. static USART usart3 = (struct usart *)USART3_BASE;
  67. static USB usb = (struct usb *)USB_BASE;
  68. static USB_BUFD usb_bufd = (struct usb_bufd *)USB_BUF_BASE;
  69. static USB_BUF usb_buf = (uint32_t *)USB_BUF_BASE;
  70. static USB_OTG usb_otg = (struct usb_otg *)USB_OTG_BASE;
  71. /* NVIC table */
  72. extern uint32_t vector_table[];
  73. /* System */
  74. void stm32_init(void);
  75. void stm32_bootloader_enter(void);
  76. void system_reset(void);
  77. /* Clocks */
  78. #define SYSCLK_MHZ 72
  79. #define SYSCLK (SYSCLK_MHZ * 1000000)
  80. #define sysclk_ns(x) (((x) * SYSCLK_MHZ) / 1000)
  81. #define sysclk_us(x) ((x) * SYSCLK_MHZ)
  82. #define sysclk_ms(x) ((x) * SYSCLK_MHZ * 1000)
  83. #define sysclk_stk(x) ((x) * (SYSCLK_MHZ / STK_MHZ))
  84. /* SysTick Timer */
  85. #define STK_MHZ (SYSCLK_MHZ / 8)
  86. void delay_ticks(unsigned int ticks);
  87. void delay_ns(unsigned int ns);
  88. void delay_us(unsigned int us);
  89. void delay_ms(unsigned int ms);
  90. typedef uint32_t stk_time_t;
  91. #define stk_now() (stk->val)
  92. #define stk_diff(x,y) (((x)-(y)) & STK_MASK) /* d = y - x */
  93. #define stk_add(x,d) (((x)-(d)) & STK_MASK) /* y = x + d */
  94. #define stk_sub(x,d) (((x)+(d)) & STK_MASK) /* y = x - d */
  95. #define stk_timesince(x) stk_diff(x,stk_now())
  96. #define stk_us(x) ((x) * STK_MHZ)
  97. #define stk_ms(x) stk_us((x) * 1000)
  98. #define stk_sysclk(x) ((x) / (SYSCLK_MHZ / STK_MHZ))
  99. /* NVIC */
  100. #define IRQx_enable(x) do { \
  101. barrier(); \
  102. nvic->iser[(x)>>5] = 1u<<((x)&31); \
  103. } while (0)
  104. #define IRQx_disable(x) do { \
  105. nvic->icer[(x)>>5] = 1u<<((x)&31); \
  106. cpu_sync(); \
  107. } while (0)
  108. #define IRQx_is_enabled(x) ((nvic->iser[(x)>>5]>>((x)&31))&1)
  109. #define IRQx_set_pending(x) (nvic->ispr[(x)>>5] = 1u<<((x)&31))
  110. #define IRQx_clear_pending(x) (nvic->icpr[(x)>>5] = 1u<<((x)&31))
  111. #define IRQx_is_pending(x) ((nvic->ispr[(x)>>5]>>((x)&31))&1)
  112. #define IRQx_set_prio(x,y) (nvic->ipr[x] = (y) << 4)
  113. #define IRQx_get_prio(x) (nvic->ipr[x] >> 4)
  114. /* GPIO */
  115. void gpio_configure_pin(GPIO gpio, unsigned int pin, unsigned int mode);
  116. #define gpio_write_pin(gpio, pin, level) \
  117. ((gpio)->bsrr = ((level) ? 0x1u : 0x10000u) << (pin))
  118. #define gpio_write_pins(gpio, mask, level) \
  119. ((gpio)->bsrr = (uint32_t)(mask) << ((level) ? 0 : 16))
  120. #define gpio_read_pin(gpio, pin) (((gpio)->idr >> (pin)) & 1)
  121. bool_t gpio_pins_connected(GPIO gpio1, unsigned int pin1,
  122. GPIO gpio2, unsigned int pin2);
  123. /* FPEC */
  124. void fpec_init(void);
  125. void fpec_page_erase(uint32_t flash_address);
  126. void fpec_write(const void *data, unsigned int size, uint32_t flash_address);
  127. #define FLASH_PAGE_SIZE 1024
  128. /*
  129. * Local variables:
  130. * mode: C
  131. * c-file-style: "Linux"
  132. * c-basic-offset: 4
  133. * tab-width: 4
  134. * indent-tabs-mode: nil
  135. * End:
  136. */