No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

SerialFlash_directwrite.h 8.2KB

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