PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

SerialFlash_directwrite.h 8.5KB

3 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #ifndef SerialFlash_directwrite_h
  2. #define SerialFlash_directwrite_h
  3. #include <inttypes.h>
  4. // Adapted from OneWire.h
  5. #if ARDUINO >= 100
  6. #include "Arduino.h" // for delayMicroseconds, digitalPinToBitMask, etc
  7. #else
  8. #include "WProgram.h" // for delayMicroseconds
  9. #include "pins_arduino.h" // for digitalPinToBitMask, etc
  10. #endif
  11. // Platform specific I/O definitions
  12. #if defined(__AVR__)
  13. #define PIN_TO_BASEREG(pin) (portInputRegister(digitalPinToPort(pin)))
  14. #define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin))
  15. #define IO_REG_TYPE uint8_t
  16. #define IO_REG_ASM asm("r30")
  17. #if defined(__AVR_ATmega4809__)
  18. #define DIRECT_READ(base, mask) (((*(base)) & (mask)) ? 1 : 0)
  19. #define DIRECT_MODE_INPUT(base, mask) ((*((base)-8)) &= ~(mask))
  20. #define DIRECT_MODE_OUTPUT(base, mask) ((*((base)-8)) |= (mask))
  21. #define DIRECT_WRITE_LOW(base, mask) ((*((base)-4)) &= ~(mask))
  22. #define DIRECT_WRITE_HIGH(base, mask) ((*((base)-4)) |= (mask))
  23. #else
  24. #define DIRECT_READ(base, mask) (((*(base)) & (mask)) ? 1 : 0)
  25. #define DIRECT_MODE_INPUT(base, mask) ((*((base)+1)) &= ~(mask))
  26. #define DIRECT_MODE_OUTPUT(base, mask) ((*((base)+1)) |= (mask))
  27. #define DIRECT_WRITE_LOW(base, mask) ((*((base)+2)) &= ~(mask))
  28. #define DIRECT_WRITE_HIGH(base, mask) ((*((base)+2)) |= (mask))
  29. #endif
  30. #elif defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MK66FX1M0__) || defined(__MK64FX512__)
  31. #define PIN_TO_BASEREG(pin) (portOutputRegister(pin))
  32. #define PIN_TO_BITMASK(pin) (1)
  33. #define IO_REG_TYPE uint8_t
  34. #define IO_REG_ASM
  35. #define DIRECT_READ(base, mask) (*((base)+512))
  36. #define DIRECT_MODE_INPUT(base, mask) (*((base)+640) = 0)
  37. #define DIRECT_MODE_OUTPUT(base, mask) (*((base)+640) = 1)
  38. #define DIRECT_WRITE_LOW(base, mask) (*((base)+256) = 1)
  39. #define DIRECT_WRITE_HIGH(base, mask) (*((base)+128) = 1)
  40. #elif defined(__MKL26Z64__)
  41. #define PIN_TO_BASEREG(pin) (portOutputRegister(pin))
  42. #define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin))
  43. #define IO_REG_TYPE uint8_t
  44. #define IO_REG_ASM
  45. #define DIRECT_READ(base, mask) ((*((base)+16) & (mask)) ? 1 : 0)
  46. #define DIRECT_MODE_INPUT(base, mask) (*((base)+20) &= ~(mask))
  47. #define DIRECT_MODE_OUTPUT(base, mask) (*((base)+20) |= (mask))
  48. #define DIRECT_WRITE_LOW(base, mask) (*((base)+8) = (mask))
  49. #define DIRECT_WRITE_HIGH(base, mask) (*((base)+4) = (mask))
  50. #elif defined(__SAM3X8E__)
  51. #define PIN_TO_BASEREG(pin) (&(digitalPinToPort(pin)->PIO_PER))
  52. #define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin))
  53. #define IO_REG_TYPE uint32_t
  54. #define IO_REG_ASM
  55. #define DIRECT_READ(base, mask) (((*((base)+15)) & (mask)) ? 1 : 0)
  56. #define DIRECT_MODE_INPUT(base, mask) ((*((base)+5)) = (mask))
  57. #define DIRECT_MODE_OUTPUT(base, mask) ((*((base)+4)) = (mask))
  58. #define DIRECT_WRITE_LOW(base, mask) ((*((base)+13)) = (mask))
  59. #define DIRECT_WRITE_HIGH(base, mask) ((*((base)+12)) = (mask))
  60. #ifndef PROGMEM
  61. #define PROGMEM
  62. #endif
  63. #ifndef pgm_read_byte
  64. #define pgm_read_byte(addr) (*(const uint8_t *)(addr))
  65. #endif
  66. #elif defined(__PIC32MX__)
  67. #define PIN_TO_BASEREG(pin) (portModeRegister(digitalPinToPort(pin)))
  68. #define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin))
  69. #define IO_REG_TYPE uint32_t
  70. #define IO_REG_ASM
  71. #define DIRECT_READ(base, mask) (((*(base+4)) & (mask)) ? 1 : 0) //PORTX + 0x10
  72. #define DIRECT_MODE_INPUT(base, mask) ((*(base+2)) = (mask)) //TRISXSET + 0x08
  73. #define DIRECT_MODE_OUTPUT(base, mask) ((*(base+1)) = (mask)) //TRISXCLR + 0x04
  74. #define DIRECT_WRITE_LOW(base, mask) ((*(base+8+1)) = (mask)) //LATXCLR + 0x24
  75. #define DIRECT_WRITE_HIGH(base, mask) ((*(base+8+2)) = (mask)) //LATXSET + 0x28
  76. #elif defined(ARDUINO_ARCH_ESP8266)
  77. #define PIN_TO_BASEREG(pin) ((volatile uint32_t*) GPO)
  78. #define PIN_TO_BITMASK(pin) (1 << pin)
  79. #define IO_REG_TYPE uint32_t
  80. #define IO_REG_ASM
  81. #define DIRECT_READ(base, mask) ((GPI & (mask)) ? 1 : 0) //GPIO_IN_ADDRESS
  82. #define DIRECT_MODE_INPUT(base, mask) (GPE &= ~(mask)) //GPIO_ENABLE_W1TC_ADDRESS
  83. #define DIRECT_MODE_OUTPUT(base, mask) (GPE |= (mask)) //GPIO_ENABLE_W1TS_ADDRESS
  84. #define DIRECT_WRITE_LOW(base, mask) (GPOC = (mask)) //GPIO_OUT_W1TC_ADDRESS
  85. #define DIRECT_WRITE_HIGH(base, mask) (GPOS = (mask)) //GPIO_OUT_W1TS_ADDRESS
  86. #elif defined(__SAMD21G18A__)
  87. #define PIN_TO_BASEREG(pin) portModeRegister(digitalPinToPort(pin))
  88. #define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin))
  89. #define IO_REG_TYPE uint32_t
  90. #define IO_REG_ASM
  91. #define DIRECT_READ(base, mask) (((*((base)+8)) & (mask)) ? 1 : 0)
  92. #define DIRECT_MODE_INPUT(base, mask) ((*((base)+1)) = (mask))
  93. #define DIRECT_MODE_OUTPUT(base, mask) ((*((base)+2)) = (mask))
  94. #define DIRECT_WRITE_LOW(base, mask) ((*((base)+5)) = (mask))
  95. #define DIRECT_WRITE_HIGH(base, mask) ((*((base)+6)) = (mask))
  96. #elif defined(RBL_NRF51822)
  97. #define PIN_TO_BASEREG(pin) (0)
  98. #define PIN_TO_BITMASK(pin) (pin)
  99. #define IO_REG_TYPE uint32_t
  100. #define IO_REG_ASM
  101. #define DIRECT_READ(base, pin) nrf_gpio_pin_read(pin)
  102. #define DIRECT_WRITE_LOW(base, pin) nrf_gpio_pin_clear(pin)
  103. #define DIRECT_WRITE_HIGH(base, pin) nrf_gpio_pin_set(pin)
  104. #define DIRECT_MODE_INPUT(base, pin) nrf_gpio_cfg_input(pin, NRF_GPIO_PIN_NOPULL)
  105. #define DIRECT_MODE_OUTPUT(base, pin) nrf_gpio_cfg_output(pin)
  106. #elif defined(__arc__) /* Arduino101/Genuino101 specifics */
  107. #include "scss_registers.h"
  108. #include "portable.h"
  109. #include "avr/pgmspace.h"
  110. #define GPIO_ID(pin) (g_APinDescription[pin].ulGPIOId)
  111. #define GPIO_TYPE(pin) (g_APinDescription[pin].ulGPIOType)
  112. #define GPIO_BASE(pin) (g_APinDescription[pin].ulGPIOBase)
  113. #define DIR_OFFSET_SS 0x01
  114. #define DIR_OFFSET_SOC 0x04
  115. #define EXT_PORT_OFFSET_SS 0x0A
  116. #define EXT_PORT_OFFSET_SOC 0x50
  117. /* GPIO registers base address */
  118. #define PIN_TO_BASEREG(pin) ((volatile uint32_t *)g_APinDescription[pin].ulGPIOBase)
  119. #define PIN_TO_BITMASK(pin) pin
  120. #define IO_REG_TYPE uint32_t
  121. #define IO_REG_ASM
  122. static inline __attribute__((always_inline))
  123. IO_REG_TYPE directRead(volatile IO_REG_TYPE *base, IO_REG_TYPE pin)
  124. {
  125. IO_REG_TYPE ret;
  126. if (SS_GPIO == GPIO_TYPE(pin)) {
  127. ret = READ_ARC_REG(((IO_REG_TYPE)base + EXT_PORT_OFFSET_SS));
  128. } else {
  129. ret = MMIO_REG_VAL_FROM_BASE((IO_REG_TYPE)base, EXT_PORT_OFFSET_SOC);
  130. }
  131. return ((ret >> GPIO_ID(pin)) & 0x01);
  132. }
  133. static inline __attribute__((always_inline))
  134. void directModeInput(volatile IO_REG_TYPE *base, IO_REG_TYPE pin)
  135. {
  136. if (SS_GPIO == GPIO_TYPE(pin)) {
  137. WRITE_ARC_REG(READ_ARC_REG((((IO_REG_TYPE)base) + DIR_OFFSET_SS)) & ~(0x01 << GPIO_ID(pin)),
  138. ((IO_REG_TYPE)(base) + DIR_OFFSET_SS));
  139. } else {
  140. MMIO_REG_VAL_FROM_BASE((IO_REG_TYPE)base, DIR_OFFSET_SOC) &= ~(0x01 << GPIO_ID(pin));
  141. }
  142. }
  143. static inline __attribute__((always_inline))
  144. void directModeOutput(volatile IO_REG_TYPE *base, IO_REG_TYPE pin)
  145. {
  146. if (SS_GPIO == GPIO_TYPE(pin)) {
  147. WRITE_ARC_REG(READ_ARC_REG(((IO_REG_TYPE)(base) + DIR_OFFSET_SS)) | (0x01 << GPIO_ID(pin)),
  148. ((IO_REG_TYPE)(base) + DIR_OFFSET_SS));
  149. } else {
  150. MMIO_REG_VAL_FROM_BASE((IO_REG_TYPE)base, DIR_OFFSET_SOC) |= (0x01 << GPIO_ID(pin));
  151. }
  152. }
  153. static inline __attribute__((always_inline))
  154. void directWriteLow(volatile IO_REG_TYPE *base, IO_REG_TYPE pin)
  155. {
  156. if (SS_GPIO == GPIO_TYPE(pin)) {
  157. WRITE_ARC_REG(READ_ARC_REG(base) & ~(0x01 << GPIO_ID(pin)), base);
  158. } else {
  159. MMIO_REG_VAL(base) &= ~(0x01 << GPIO_ID(pin));
  160. }
  161. }
  162. static inline __attribute__((always_inline))
  163. void directWriteHigh(volatile IO_REG_TYPE *base, IO_REG_TYPE pin)
  164. {
  165. if (SS_GPIO == GPIO_TYPE(pin)) {
  166. WRITE_ARC_REG(READ_ARC_REG(base) | (0x01 << GPIO_ID(pin)), base);
  167. } else {
  168. MMIO_REG_VAL(base) |= (0x01 << GPIO_ID(pin));
  169. }
  170. }
  171. #define DIRECT_READ(base, pin) directRead(base, pin)
  172. #define DIRECT_MODE_INPUT(base, pin) directModeInput(base, pin)
  173. #define DIRECT_MODE_OUTPUT(base, pin) directModeOutput(base, pin)
  174. #define DIRECT_WRITE_LOW(base, pin) directWriteLow(base, pin)
  175. #define DIRECT_WRITE_HIGH(base, pin) directWriteHigh(base, pin)
  176. #else
  177. #define PIN_TO_BASEREG(pin) (0)
  178. #define PIN_TO_BITMASK(pin) (pin)
  179. #define IO_REG_TYPE unsigned int
  180. #define IO_REG_ASM
  181. #define DIRECT_READ(base, pin) digitalRead(pin)
  182. #define DIRECT_WRITE_LOW(base, pin) digitalWrite(pin, LOW)
  183. #define DIRECT_WRITE_HIGH(base, pin) digitalWrite(pin, HIGH)
  184. #define DIRECT_MODE_INPUT(base, pin) pinMode(pin,INPUT)
  185. #define DIRECT_MODE_OUTPUT(base, pin) pinMode(pin,OUTPUT)
  186. #endif
  187. #endif