common.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * stm32/common.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 DBG volatile struct dbg * const
  16. #define FLASH volatile struct flash * const
  17. #define PWR volatile struct pwr * const
  18. #define RCC volatile struct rcc * const
  19. #define IWDG volatile struct iwdg * const
  20. #define GPIO volatile struct gpio * const
  21. #define EXTI volatile struct exti * const
  22. #define DMA volatile struct dma * const
  23. #define TIM volatile struct tim * const
  24. #define SPI volatile struct spi * const
  25. #define I2C volatile struct i2c * const
  26. #define USART volatile struct usart * const
  27. #define SER_ID volatile uint32_t * const
  28. /* NVIC table */
  29. extern uint32_t vector_table[];
  30. /* System */
  31. void stm32_init(void);
  32. void stm32_bootloader_enter(void);
  33. void system_reset(void);
  34. /* Clocks */
  35. #define SYSCLK (SYSCLK_MHZ * 1000000)
  36. #define sysclk_ns(x) (((x) * SYSCLK_MHZ) / 1000)
  37. #define sysclk_us(x) ((x) * SYSCLK_MHZ)
  38. #define sysclk_ms(x) ((x) * SYSCLK_MHZ * 1000)
  39. #define sysclk_stk(x) ((x) * (SYSCLK_MHZ / STK_MHZ))
  40. /* SysTick Timer */
  41. #define STK_MHZ (SYSCLK_MHZ / 8)
  42. void delay_ticks(unsigned int ticks);
  43. void delay_ns(unsigned int ns);
  44. void delay_us(unsigned int us);
  45. void delay_ms(unsigned int ms);
  46. typedef uint32_t stk_time_t;
  47. #define stk_now() (stk->val)
  48. #define stk_diff(x,y) (((x)-(y)) & STK_MASK) /* d = y - x */
  49. #define stk_add(x,d) (((x)-(d)) & STK_MASK) /* y = x + d */
  50. #define stk_sub(x,d) (((x)+(d)) & STK_MASK) /* y = x - d */
  51. #define stk_timesince(x) stk_diff(x,stk_now())
  52. #define stk_us(x) ((x) * STK_MHZ)
  53. #define stk_ms(x) stk_us((x) * 1000)
  54. #define stk_sysclk(x) ((x) / (SYSCLK_MHZ / STK_MHZ))
  55. /* NVIC */
  56. #define IRQx_enable(x) do { \
  57. barrier(); \
  58. nvic->iser[(x)>>5] = 1u<<((x)&31); \
  59. } while (0)
  60. #define IRQx_disable(x) do { \
  61. nvic->icer[(x)>>5] = 1u<<((x)&31); \
  62. cpu_sync(); \
  63. } while (0)
  64. #define IRQx_is_enabled(x) ((nvic->iser[(x)>>5]>>((x)&31))&1)
  65. #define IRQx_set_pending(x) (nvic->ispr[(x)>>5] = 1u<<((x)&31))
  66. #define IRQx_clear_pending(x) (nvic->icpr[(x)>>5] = 1u<<((x)&31))
  67. #define IRQx_is_pending(x) ((nvic->ispr[(x)>>5]>>((x)&31))&1)
  68. #define IRQx_set_prio(x,y) (nvic->ipr[x] = (y) << 4)
  69. #define IRQx_get_prio(x) (nvic->ipr[x] >> 4)
  70. /* GPIO */
  71. struct gpio;
  72. void gpio_configure_pin(GPIO gpio, unsigned int pin, unsigned int mode);
  73. #define gpio_write_pin(gpio, pin, level) \
  74. ((gpio)->bsrr = ((level) ? 0x1u : 0x10000u) << (pin))
  75. #define gpio_write_pins(gpio, mask, level) \
  76. ((gpio)->bsrr = (uint32_t)(mask) << ((level) ? 0 : 16))
  77. #define gpio_read_pin(gpio, pin) (((gpio)->idr >> (pin)) & 1)
  78. bool_t gpio_pins_connected(GPIO gpio1, unsigned int pin1,
  79. GPIO gpio2, unsigned int pin2);
  80. /* FPEC */
  81. void fpec_init(void);
  82. void fpec_page_erase(uint32_t flash_address);
  83. void fpec_write(const void *data, unsigned int size, uint32_t flash_address);
  84. extern unsigned int flash_kb;
  85. /* Pin mappings */
  86. enum { _A = 0, _B, _C, _D, _E, _F, _G, _H, _I };
  87. enum { _OD = 0, _PP };
  88. struct pin_mapping {
  89. uint8_t pin_id;
  90. uint8_t gpio_bank;
  91. uint8_t gpio_pin;
  92. bool_t push_pull;
  93. };
  94. GPIO gpio_from_id(uint8_t id);
  95. uint8_t write_mapped_pin(
  96. const struct pin_mapping *map, int pin_id, bool_t level);
  97. uint8_t read_mapped_pin(
  98. const struct pin_mapping *map, int pin_id, bool_t *p_level);
  99. extern const struct board_config *board_config;
  100. /*
  101. * Local variables:
  102. * mode: C
  103. * c-file-style: "Linux"
  104. * c-basic-offset: 4
  105. * tab-width: 4
  106. * indent-tabs-mode: nil
  107. * End:
  108. */