system.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. #ifndef SYSTEM_H_INCLUDED
  2. #define SYSTEM_H_INCLUDED
  3. #include <stdint.h>
  4. void systick_on(void);
  5. void waitus( uint16_t const us );
  6. void waitms( uint16_t const us );
  7. /* System specific: PLL with 8 MHz external oscillator, CPU at 168MHz */
  8. #define CPU_FREQ (168000000)
  9. #define PLL_FULL_MASK (0x7F037FFF)
  10. /* STM32 specific defines */
  11. #define APB1_CLOCK_ER (*(volatile uint32_t *)(0x40023840))
  12. #define APB1_CLOCK_RST (*(volatile uint32_t *)(0x40023820))
  13. #define TIM2_APB1_CLOCK_ER_VAL (1 << 0)
  14. #define PWR_APB1_CLOCK_ER_VAL (1 << 28)
  15. #define APB2_CLOCK_ER (*(volatile uint32_t *)(0x40023844))
  16. #define APB2_CLOCK_RST (*(volatile uint32_t *)(0x40023824))
  17. #define SYSCFG_APB2_CLOCK_ER (1 << 14)
  18. /* SCB for sleep configuration */
  19. #define SCB_SCR (*(volatile uint32_t *)(0xE000ED10))
  20. #define SCB_SCR_SEVONPEND (1 << 4)
  21. #define SCB_SCR_SLEEPDEEP (1 << 2)
  22. #define SCB_SCR_SLEEPONEXIT (1 << 1)
  23. /* Systick */
  24. #define SYSTICK_BASE (0xE000E010)
  25. #define SYSTICK_CSR (*(volatile uint32_t *)(SYSTICK_BASE + 0x00))
  26. #define SYSTICK_RVR (*(volatile uint32_t *)(SYSTICK_BASE + 0x04))
  27. #define SYSTICK_CVR (*(volatile uint32_t *)(SYSTICK_BASE + 0x08))
  28. #define SYSTICK_CALIB (*(volatile uint32_t *)(SYSTICK_BASE + 0x0C))
  29. #define SYSTICK_MAX (0xFFFFFFu)
  30. #define SYSTICK_TICKS_PER_US (CPU_FREQ / 1000000u)
  31. #define SYSTICK_CSR_ON (0x00000005u)
  32. /* Assembly helpers */
  33. #define DMB() __asm__ volatile ("dmb")
  34. #define WFI() __asm__ volatile ("wfi")
  35. #define WFE() __asm__ volatile ("wfe")
  36. #define SEV() __asm__ volatile ("sev")
  37. /* Master clock setting */
  38. void clock_pll_on(int powersave);
  39. void clock_pll_off(void);
  40. /* NVIC */
  41. /* NVIC ISER Base register (Cortex-M) */
  42. #define NVIC_EXTI0_IRQN (6)
  43. #define NVIC_TIM2_IRQN (28)
  44. #define NVIC_ISER_BASE (0xE000E100)
  45. #define NVIC_ICER_BASE (0xE000E180)
  46. #define NVIC_ICPR_BASE (0xE000E280)
  47. #define NVIC_IPRI_BASE (0xE000E400)
  48. #define CUSTOM_HW
  49. #ifdef CUSTOM_HW
  50. # define BLUE_LED_PIN (0)
  51. # define GREEN_LED_PIN (0)
  52. # define BUTTON_PIN (15)
  53. # define LED_BSRR GPIOE_BSRR
  54. # define BUTTON_IDR GPIOD_IDR
  55. #else
  56. # define BLUE_LED_PIN (15)
  57. # define RED_LED_PIN (14)
  58. # define ORANGE_LED_PIN (13)
  59. # define GREEN_LED_PIN (12)
  60. # define LED_BSRR GPIOD_BSRR
  61. # define BUTTON_IDR GPIOA_IDR
  62. # define BUTTON_PIN (0)
  63. #endif
  64. #define AHB1_CLOCK_ER (*(volatile uint32_t *)(0x40023830))
  65. #define GPIOA_AHB1_CLOCK_ER (1 << 0)
  66. #define GPIOB_AHB1_CLOCK_ER (1 << 1)
  67. #define GPIOC_AHB1_CLOCK_ER (1 << 2)
  68. #define GPIOD_AHB1_CLOCK_ER (1 << 3)
  69. #define GPIOE_AHB1_CLOCK_ER (1 << 4)
  70. #define GPIOF_AHB1_CLOCK_ER (1 << 5)
  71. #define GPIOA_BASE 0x40020000
  72. #define GPIOD_BASE 0x40020c00
  73. #define GPIOE_BASE 0x40021000
  74. #define GPIOA GPIOA_BASE
  75. #define GPIOD GPIOD_BASE
  76. #define GPIOE GPIOE_BASE
  77. #define GPIOA_MODE (*(volatile uint32_t *)(GPIOA_BASE + 0x00))
  78. #define GPIOA_IDR (*(volatile uint32_t *)(GPIOA_BASE + 0x10))
  79. #define GPIOA_OTYPE (*(volatile uint32_t *)(GPIOA_BASE + 0x04))
  80. #define GPIOA_OSPD (*(volatile uint32_t *)(GPIOD_BASE + 0x08))
  81. #define GPIOA_PUPD (*(volatile uint32_t *)(GPIOA_BASE + 0x0c))
  82. #define GPIOA_ODR (*(volatile uint32_t *)(GPIOA_BASE + 0x14))
  83. #define GPIOA_BSRR (*(volatile uint32_t *)(GPIOA_BASE + 0x18))
  84. #define GPIOD_MODE (*(volatile uint32_t *)(GPIOD_BASE + 0x00))
  85. #define GPIOD_OTYPE (*(volatile uint32_t *)(GPIOD_BASE + 0x04))
  86. #define GPIOD_OSPD (*(volatile uint32_t *)(GPIOD_BASE + 0x08))
  87. #define GPIOD_IDR (*(volatile uint32_t *)(GPIOD_BASE + 0x10))
  88. #define GPIOD_PUPD (*(volatile uint32_t *)(GPIOD_BASE + 0x0c))
  89. #define GPIOD_ODR (*(volatile uint32_t *)(GPIOD_BASE + 0x14))
  90. #define GPIOD_BSRR (*(volatile uint32_t *)(GPIOD_BASE + 0x18))
  91. #define GPIOE_MODE (*(volatile uint32_t *)(GPIOE_BASE + 0x00))
  92. #define GPIOE_OTYPE (*(volatile uint32_t *)(GPIOE_BASE + 0x04))
  93. #define GPIOE_OSPD (*(volatile uint32_t *)(GPIOE_BASE + 0x08))
  94. #define GPIOE_PUPD (*(volatile uint32_t *)(GPIOE_BASE + 0x0c))
  95. #define GPIOE_ODR (*(volatile uint32_t *)(GPIOE_BASE + 0x14))
  96. #define GPIOE_BSRR (*(volatile uint32_t *)(GPIOE_BASE + 0x18))
  97. static inline void nvic_irq_enable(uint8_t n)
  98. {
  99. int i = n / 32;
  100. volatile uint32_t *nvic_iser = ((volatile uint32_t *)(NVIC_ISER_BASE + 4 * i));
  101. *nvic_iser |= (1 << (n % 32));
  102. }
  103. static inline void nvic_irq_disable(uint8_t n)
  104. {
  105. int i = n / 32;
  106. volatile uint32_t *nvic_icer = ((volatile uint32_t *)(NVIC_ICER_BASE + 4 * i));
  107. *nvic_icer |= (1 << (n % 32));
  108. }
  109. static inline void nvic_irq_setprio(uint8_t n, uint8_t prio)
  110. {
  111. volatile uint8_t *nvic_ipri = ((volatile uint8_t *)(NVIC_IPRI_BASE + n));
  112. *nvic_ipri = prio;
  113. }
  114. static inline void nvic_irq_clear(uint8_t n)
  115. {
  116. int i = n / 32;
  117. volatile uint8_t *nvic_icpr = ((volatile uint8_t *)(NVIC_ICPR_BASE + 4 * i));
  118. *nvic_icpr = (1 << (n % 32));
  119. }
  120. /*** FLASH ***/
  121. #define FLASH_BASE (0x40023C00)
  122. #define FLASH_ACR (*(volatile uint32_t *)(FLASH_BASE + 0x00))
  123. #define FLASH_ACR_ENABLE_DATA_CACHE (1 << 10)
  124. #define FLASH_ACR_ENABLE_INST_CACHE (1 << 9)
  125. /*** RCC ***/
  126. #define RCC_BASE (0x40023800)
  127. #define RCC_CR (*(volatile uint32_t *)(RCC_BASE + 0x00))
  128. #define RCC_PLLCFGR (*(volatile uint32_t *)(RCC_BASE + 0x04))
  129. #define RCC_CFGR (*(volatile uint32_t *)(RCC_BASE + 0x08))
  130. #define RCC_CR (*(volatile uint32_t *)(RCC_BASE + 0x00))
  131. #define RCC_CR_PLLRDY (1 << 25)
  132. #define RCC_CR_PLLON (1 << 24)
  133. #define RCC_CR_HSERDY (1 << 17)
  134. #define RCC_CR_HSEON (1 << 16)
  135. #define RCC_CR_HSIRDY (1 << 1)
  136. #define RCC_CR_HSION (1 << 0)
  137. #define RCC_CFGR_SW_HSI 0x0
  138. #define RCC_CFGR_SW_HSE 0x1
  139. #define RCC_CFGR_SW_PLL 0x2
  140. #define RCC_PLLCFGR_PLLSRC (1 << 22)
  141. #define RCC_PRESCALER_DIV_NONE 0
  142. #define RCC_PRESCALER_DIV_2 8
  143. #define RCC_PRESCALER_DIV_4 9
  144. /* POWER CONTROL REGISTER */
  145. #define POW_BASE (0x40007000)
  146. #define POW_CR (*(volatile uint32_t *)(POW_BASE + 0x00))
  147. #define POW_SCR (*(volatile uint32_t *)(POW_BASE + 0x04))
  148. #define POW_CR_VOS (1 << 14)
  149. #define POW_CR_FPDS (1 << 9)
  150. #define POW_CR_CSBF (1 << 3)
  151. #define POW_CR_CWUF (1 << 2)
  152. #define POW_CR_PDDS (1 << 1)
  153. #define POW_CR_LPDS (1 << 0)
  154. #define POW_SCR_WUF (1 << 0)
  155. #define POW_SCR_EWUP (1 << 4)
  156. #define POW_SCR_BRE (1 << 9)
  157. #if (CPU_FREQ == 168000000)
  158. # define PLLM 8
  159. # define PLLN 336
  160. # define PLLP 2
  161. # define PLLQ 7
  162. # define PLLR 0
  163. # define POWER_SAVE 1
  164. # define HPRE RCC_PRESCALER_DIV_NONE
  165. # define PPRE1 RCC_PRESCALER_DIV_4
  166. # define PPRE2 RCC_PRESCALER_DIV_2
  167. # define FLASH_WAITSTATES 5
  168. #elif (CPU_FREQ == 120000000)
  169. # define PLLM 8
  170. # define PLLN 240
  171. # define PLLP 2
  172. # define PLLQ 5
  173. # define PLLR 0
  174. # define HPRE RCC_PRESCALER_DIV_NONE
  175. # define PPRE1 RCC_PRESCALER_DIV_4
  176. # define PPRE2 RCC_PRESCALER_DIV_2
  177. # define FLASH_WAITSTATES 3
  178. #elif (CPU_FREQ == 100000000)
  179. # define PLLM 8
  180. # define PLLN 192
  181. # define PLLP 2
  182. # define PLLQ 4
  183. # define PLLR 0
  184. # define POWER_SAVE 1
  185. # define HPRE RCC_PRESCALER_DIV_NONE
  186. # define PPRE1 RCC_PRESCALER_DIV_2
  187. # define PPRE2 RCC_PRESCALER_DIV_NONE
  188. # define FLASH_WAITSTATES 2
  189. #elif (CPU_FREQ == 84000000)
  190. # define PLLM 8
  191. # define PLLN 336
  192. # define PLLP 4
  193. # define PLLQ 7
  194. # define PLLR 0
  195. # define HPRE RCC_PRESCALER_DIV_NONE
  196. # define PPRE1 RCC_PRESCALER_DIV_2
  197. # define PPRE2 RCC_PRESCALER_DIV_NONE
  198. # define FLASH_WAITSTATES 2
  199. #elif (CPU_FREQ == 48000000)
  200. # define PLLM 8
  201. # define PLLN 96
  202. # define PLLP 2
  203. # define PLLQ 2
  204. # define PLLR 0
  205. # define POWER_SAVE 1
  206. # define HPRE RCC_PRESCALER_DIV_NONE
  207. # define PPRE1 RCC_PRESCALER_DIV_4
  208. # define PPRE2 RCC_PRESCALER_DIV_2
  209. # define FLASH_WAITSTATES 3
  210. #else
  211. # error "Please select a valid CPU_FREQ in system.h"
  212. #endif
  213. #endif