/stm32/cores/arduino/util/bitband.h

https://codeberg.org/Gruenstreifen-eV/BSFrance-stm32 · C Header · 237 lines · 147 code · 45 blank · 45 comment · 0 complexity · 4ec0b054e19f32e6635f39093a1dec96 MD5 · raw file

  1. //for stm32 sram/peripheral bitband io opration
  2. //2017.5.28 for all stm32 chips
  3. //modify by huawei<huaweiwx@sina.com> 2016.9.18
  4. #ifndef __BITBAND_H__
  5. #define __BITBAND_H__
  6. #define BITMASKPOS(mask) (((mask)==bit(0))?0: \
  7. (((mask)==bit(1))?1: \
  8. (((mask)==bit(2))?2: \
  9. (((mask)==bit(3))?3: \
  10. (((mask)==bit(4))?4: \
  11. (((mask)==bit(5))?5: \
  12. (((mask)==bit(6))?6: \
  13. (((mask)==bit(7))?7: \
  14. (((mask)==bit(8))?8: \
  15. (((mask)==bit(9))?9: \
  16. (((mask)==bit(10))?10: \
  17. (((mask)==bit(11))?11: \
  18. (((mask)==bit(12))?12: \
  19. (((mask)==bit(13))?13: \
  20. (((mask)==bit(14))?14: \
  21. (((mask)==bit(15))?15:16))))))))))))))))
  22. #if !(defined(STM32F0)||defined(STM32L0)||defined(STM32F7))
  23. //bitband io opration,ʵÏÖ51ÀàËÆµÄGPIO¿ØÖƹ¦ÄÜ
  24. #define BITBAND(addr, bitnum) (PERIPH_BB_BASE+((addr &0xFFFFF)<<5)+(bitnum<<2))
  25. #define MEM_ADDR(addr) *((volatile unsigned long *)(addr))
  26. #define BITBAND_ADDR(addr, bitnum) MEM_ADDR(BITBAND(addr, bitnum))
  27. //IO address ¿ÚµØÖ·Ó³Éä
  28. #if defined(STM32F1)
  29. #define GPIOA_ODR_Addr (GPIOA_BASE+12) //0x4001080C
  30. #define GPIOB_ODR_Addr (GPIOB_BASE+12) //0x40010C0C
  31. #define GPIOC_ODR_Addr (GPIOC_BASE+12) //0x4001100C
  32. #define GPIOD_ODR_Addr (GPIOD_BASE+12) //0x4001140C
  33. #define GPIOE_ODR_Addr (GPIOE_BASE+12) //0x4001180C
  34. #define GPIOF_ODR_Addr (GPIOF_BASE+12) //0x40011A0C
  35. #define GPIOG_ODR_Addr (GPIOG_BASE+12) //0x40011E0C
  36. #define GPIOA_IDR_Addr (GPIOA_BASE+8) //0x40010808
  37. #define GPIOB_IDR_Addr (GPIOB_BASE+8) //0x40010C08
  38. #define GPIOC_IDR_Addr (GPIOC_BASE+8) //0x40011008
  39. #define GPIOD_IDR_Addr (GPIOD_BASE+8) //0x40011408
  40. #define GPIOE_IDR_Addr (GPIOE_BASE+8) //0x40011808
  41. #define GPIOF_IDR_Addr (GPIOF_BASE+8) //0x40011A08
  42. #define GPIOG_IDR_Addr (GPIOG_BASE+8) //0x40011E08
  43. #elif defined(STM32F2)||defined(STM32F3)||defined(STM32F4)||\
  44. defined(STM32L1)||defined(STM32L4)
  45. #define GPIOA_ODR_Addr (GPIOA_BASE+20) //0x40020014
  46. #define GPIOB_ODR_Addr (GPIOB_BASE+20) //0x40020414
  47. #define GPIOC_ODR_Addr (GPIOC_BASE+20) //0x40020814
  48. #define GPIOD_ODR_Addr (GPIOD_BASE+20) //0x40020C14
  49. #define GPIOE_ODR_Addr (GPIOE_BASE+20) //0x40021014
  50. #define GPIOF_ODR_Addr (GPIOF_BASE+20) //0x40021414
  51. #define GPIOG_ODR_Addr (GPIOG_BASE+20) //0x40021814
  52. #define GPIOH_ODR_Addr (GPIOH_BASE+20) //0x40021C14
  53. #define GPIOI_ODR_Addr (GPIOI_BASE+20) //0x40022014
  54. #define GPIOA_IDR_Addr (GPIOA_BASE+16) //0x40020010
  55. #define GPIOB_IDR_Addr (GPIOB_BASE+16) //0x40020410
  56. #define GPIOC_IDR_Addr (GPIOC_BASE+16) //0x40020810
  57. #define GPIOD_IDR_Addr (GPIOD_BASE+16) //0x40020C10
  58. #define GPIOE_IDR_Addr (GPIOE_BASE+16) //0x40021010
  59. #define GPIOF_IDR_Addr (GPIOF_BASE+16) //0x40021410
  60. #define GPIOG_IDR_Addr (GPIOG_BASE+16) //0x40021810
  61. #define GPIOH_IDR_Addr (GPIOH_BASE+16) //0x40021C10
  62. #define GPIOI_IDR_Addr (GPIOI_BASE+16) //0x40022010
  63. #endif
  64. //n must be less 16! È·±£nµÄֵСÓÚ16!
  65. //example: in = PAin(0);
  66. // PCout(13) = 1;
  67. #define PAout(n) BITBAND_ADDR(GPIOA_ODR_Addr,n) //Êä³ö
  68. #define PAin(n) BITBAND_ADDR(GPIOA_IDR_Addr,n) //ÊäÈë
  69. #define PORTA GPIOA->ODR //Êä³ö
  70. #define PINA GPIOA->IDR //ÊäÈë
  71. #define PBout(n) BITBAND_ADDR(GPIOB_ODR_Addr,n) //Êä³ö
  72. #define PBin(n) BITBAND_ADDR(GPIOB_IDR_Addr,n) //ÊäÈë
  73. #define PORTB GPIOB->ODR //Êä³ö
  74. #define PINB GPIOB->IDR //ÊäÈë
  75. #define PCout(n) BITBAND_ADDR(GPIOC_ODR_Addr,n) //Êä³ö
  76. #define PCin(n) BITBAND_ADDR(GPIOC_IDR_Addr,n) //ÊäÈë
  77. #define PORTC GPIOC->ODR //Êä³ö
  78. #define PINC GPIOC->IDR //ÊäÈë
  79. #define PDout(n) BITBAND_ADDR(GPIOD_ODR_Addr,n) //Êä³ö
  80. #define PDin(n) BITBAND_ADDR(GPIOD_IDR_Addr,n) //ÊäÈë
  81. #define PORTD GPIOD->ODR //Êä³ö
  82. #define PIND GPIOD->IDR //ÊäÈë
  83. #define PEout(n) BITBAND_ADDR(GPIOE_ODR_Addr,n) //Êä³ö
  84. #define PEin(n) BITBAND_ADDR(GPIOE_IDR_Addr,n) //ÊäÈë
  85. #define PORTE GPIOE->ODR //Êä³ö
  86. #define PINE GPIOE->IDR //ÊäÈë
  87. #define PFout(n) BITBAND_ADDR(GPIOF_ODR_Addr,n) //Êä³ö
  88. #define PFin(n) BITBAND_ADDR(GPIOF_IDR_Addr,n) //ÊäÈë
  89. #define PORTF GPIOF->ODR //Êä³ö
  90. #define PINF GPIOF->IDR //ÊäÈë
  91. #define PGout(n) BITBAND_ADDR(GPIOG_ODR_Addr,n) //Êä³ö
  92. #define PGin(n) BITBAND_ADDR(GPIOG_IDR_Addr,n) //ÊäÈë
  93. #define PORTG GPIOG->ODR //Êä³ö
  94. #define PING GPIOG->IDR //ÊäÈë
  95. #define PHout(n) BITBAND_ADDR(GPIOH_ODR_Addr,n) //Êä³ö
  96. #define PHin(n) BITBAND_ADDR(GPIOH_IDR_Addr,n) //ÊäÈë
  97. #define PORTH GPIOH->ODR //Êä³ö
  98. #define PINH GPIOH->IDR //ÊäÈë
  99. #define PIout(n) BITBAND_ADDR(GPIOI_ODR_Addr,n) //Êä³ö
  100. #define PIin(n) BITBAND_ADDR(GPIOI_IDR_Addr,n) //ÊäÈë
  101. #define PORTI GPIOI->ODR //Êä³ö
  102. #define PINI GPIOI->ODR //ÊäÈë
  103. //for arduino const pin
  104. #define PIN_OUTADDR(n) BITBAND(((uint32_t)&variant_pin_list[n].port->ODR),\
  105. BITMASKPOS(variant_pin_list[n].pinMask))
  106. #define PIN_INADDR(n) BITBAND(((uint32_t)&variant_pin_list[n].port->IDR),\
  107. BITMASKPOS(variant_pin_list[n].pinMask))
  108. #ifdef __cplusplus
  109. extern "C" {
  110. #endif
  111. static inline volatile uint32_t* __BB_addr(volatile void*,
  112. uint32_t,
  113. uint32_t,
  114. uint32_t);
  115. /**
  116. * @brief Obtain a pointer to the bit-band address corresponding to a
  117. * bit in a volatile SRAM address.
  118. * @param address Address in the bit-banded SRAM region
  119. * @param bit Bit in address to bit-band
  120. */
  121. static inline volatile uint32_t* BB_sramp(volatile void *address, uint32_t bit) {
  122. return __BB_addr(address, bit, SRAM_BB_BASE, SRAM_BASE);
  123. }
  124. /**
  125. * @brief Get a bit from an address in the SRAM bit-band region.
  126. * @param address Address in the SRAM bit-band region to read from
  127. * @param bit Bit in address to read
  128. * @return bit's value in address.
  129. */
  130. static inline uint8_t BB_sramGetBit(volatile void *address, uint32_t bit) {
  131. return *BB_sramp(address, bit);
  132. }
  133. /**
  134. * @brief Set a bit in an address in the SRAM bit-band region.
  135. * @param address Address in the SRAM bit-band region to write to
  136. * @param bit Bit in address to write to
  137. * @param val Value to write for bit, either 0 or 1.
  138. */
  139. static inline void BB_sramSetBit(volatile void *address,
  140. uint32_t bit,
  141. uint8_t val) {
  142. *BB_sramp(address, bit) = val;
  143. }
  144. /**
  145. * @brief Obtain a pointer to the bit-band address corresponding to a
  146. * bit in a peripheral address.
  147. * @param address Address in the bit-banded peripheral region
  148. * @param bit Bit in address to bit-band
  149. */
  150. static inline volatile uint32_t* BB_perip(volatile void *address, uint32_t bit) {
  151. return __BB_addr(address, bit, PERIPH_BB_BASE, PERIPH_BASE);
  152. }
  153. /**
  154. * @brief Get a bit from an address in the peripheral bit-band region.
  155. * @param address Address in the peripheral bit-band region to read from
  156. * @param bit Bit in address to read
  157. * @return bit's value in address.
  158. */
  159. static inline uint8_t BB_periGetBit(volatile void *address, uint32_t bit) {
  160. return *BB_perip(address, bit);
  161. }
  162. /**
  163. * @brief Set a bit in an address in the peripheral bit-band region.
  164. * @param address Address in the peripheral bit-band region to write to
  165. * @param bit Bit in address to write to
  166. * @param val Value to write for bit, either 0 or 1.
  167. */
  168. static inline void BB_periSetBit(volatile void *address,
  169. uint32_t bit,
  170. uint8_t val) {
  171. *BB_perip(address, bit) = val;
  172. }
  173. static inline volatile uint32_t* __BB_addr(volatile void *address,
  174. uint32_t bit,
  175. uint32_t bb_base,
  176. uint32_t bb_ref) {
  177. return (volatile uint32_t*)(bb_base + ((uint32_t)address - bb_ref) * 32 +
  178. bit * 4);
  179. }
  180. static inline uint32_t BB_pinAddr(uint32_t pin,uint8_t mode){
  181. uint32_t portaddr =((mode)?((uint32_t) &variant_pin_list[pin].port->ODR)
  182. :((uint32_t) &variant_pin_list[pin].port->IDR));
  183. uint32_t bit = BITMASKPOS(variant_pin_list[pin].pinMask);
  184. return BITBAND(portaddr,bit);
  185. }
  186. #ifdef __cplusplus
  187. }
  188. #endif
  189. #else
  190. # error "BitBand unsuport F0/L0/F7 mcu!"
  191. #endif
  192. #endif