Advertisement
honey_the_codewitch

tft_io.hpp

Dec 14th, 2021
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 26.16 KB | None | 0 0
  1. #pragma once
  2. #include <Arduino.h>
  3. // This code only works on the ESP32. The generic code for Arduino that is in place is non-functional in my tests. I'm not sure why, but it might be a timing issue.
  4. #if defined(ESP32)
  5.     #define OPTIMIZE_ESP32
  6. #endif
  7. #define FORCE_INLINE __attribute((always_inline))
  8. namespace arduino {
  9.     enum struct tft_io_type {
  10.         spi = 0,
  11.         i2c = 1,
  12.         parallel8 = 2
  13.     };
  14.     template<int8_t PinCS,
  15.         int8_t PinWR,
  16.         int8_t PinRD,
  17.         int8_t PinD0,
  18.         int8_t PinD1,
  19.         int8_t PinD2,
  20.         int8_t PinD3,
  21.         int8_t PinD4,
  22.         int8_t PinD5,
  23.         int8_t PinD6,
  24.         int8_t PinD7>
  25.     struct tft_p8 {
  26.         constexpr static const tft_io_type type = tft_io_type::parallel8;
  27.         constexpr static const bool readable = true;
  28.         constexpr static const size_t dma_size = 0;
  29.         constexpr static const int8_t pin_cs = PinCS;
  30.         constexpr static const int8_t pin_wr = PinWR;
  31.         constexpr static const int8_t pin_rd = PinRD;
  32.         constexpr static const int8_t pin_d0 = PinD0;
  33.         constexpr static const int8_t pin_d1 = PinD1;
  34.         constexpr static const int8_t pin_d2 = PinD2;
  35.         constexpr static const int8_t pin_d3 = PinD3;
  36.         constexpr static const int8_t pin_d4 = PinD4;
  37.         constexpr static const int8_t pin_d5 = PinD5;
  38.         constexpr static const int8_t pin_d6 = PinD6;
  39.         constexpr static const int8_t pin_d7 = PinD7;
  40. #ifdef OPTIMIZE_ESP32
  41.     private:
  42.         constexpr static const bool has_data_low_pins = (pin_d0>=0 && pin_d0<32) ||
  43.                                                         (pin_d1>=0 && pin_d1<32) ||
  44.                                                         (pin_d2>=0 && pin_d2<32) ||
  45.                                                         (pin_d3>=0 && pin_d3<32) ||
  46.                                                         (pin_d4>=0 && pin_d4<32) ||
  47.                                                         (pin_d5>=0 && pin_d5<32) ||
  48.                                                         (pin_d6>=0 && pin_d6<32) ||
  49.                                                         (pin_d7>=0 && pin_d7<32);
  50.         constexpr static const bool has_data_high_pins = (pin_d0>31) ||
  51.                                                         (pin_d1>31) ||
  52.                                                         (pin_d2>31) ||
  53.                                                         (pin_d3>31) ||
  54.                                                         (pin_d4>31) ||
  55.                                                         (pin_d5>31) ||
  56.                                                         (pin_d6>31) ||
  57.                                                         (pin_d7>31);
  58.         constexpr static uint32_t get_dir_mask_low() {
  59.             uint32_t result = 0;
  60.             if(pin_d0>-1 && pin_d0<32) {
  61.                 result |= 1<<pin_d0;
  62.             }
  63.             if(pin_d1>-1 && pin_d1<32) {
  64.                 result |= 1<<pin_d1;
  65.             }
  66.             if(pin_d2>-1 && pin_d2<32) {
  67.                 result |= 1<<pin_d2;
  68.             }
  69.             if(pin_d3>-1 && pin_d3<32) {
  70.                 result |= 1<<pin_d3;
  71.             }
  72.             if(pin_d4>-1 && pin_d4<32) {
  73.                 result |= 1<<pin_d4;
  74.             }
  75.             if(pin_d5>-1 && pin_d5<32) {
  76.                 result |= 1<<pin_d5;
  77.             }
  78.             if(pin_d6>-1 && pin_d6<32) {
  79.                 result |= 1<<pin_d6;
  80.             }
  81.             if(pin_d7>-1 && pin_d7<32) {
  82.                 result |= 1<<pin_d7;
  83.             }
  84.             return result;
  85.         }
  86.         constexpr static uint32_t get_dir_mask_high() {
  87.             uint32_t result = 0;
  88.             if(pin_d0>31) {
  89.                 result |= 1<<((pin_d0-32)&31);
  90.             }
  91.             if(pin_d1>31) {
  92.                 result |= 1<<((pin_d1-32)&31);
  93.             }
  94.             if(pin_d2>31) {
  95.                 result |= 1<<((pin_d2-32)&31);
  96.             }
  97.             if(pin_d3>31) {
  98.                 result |= 1<<((pin_d3-32)&31);
  99.             }
  100.             if(pin_d4>31) {
  101.                 result |= 1<<((pin_d4-32)&31);
  102.             }
  103.             if(pin_d5>31) {
  104.                 result |= 1<<((pin_d5-32)&31);
  105.             }
  106.             if(pin_d6>31) {
  107.                 result |= 1<<((pin_d6-32)&31);
  108.             }
  109.             if(pin_d7>31) {
  110.                 result |= 1<<((pin_d7-32)&31);
  111.             }
  112.             return result;
  113.         }
  114.        
  115.         constexpr static const uint32_t dir_mask_low = get_dir_mask_low();
  116.         constexpr static const uint32_t dir_mask_high = get_dir_mask_high();
  117.        
  118.         inline static uint32_t clr_mask_low() FORCE_INLINE {
  119.             if(pin_wr>31) {
  120.                 uint32_t result = dir_mask_low;
  121.                 wr_low();
  122.                 return result;
  123.             } else if(pin_wr>-1) {
  124.                 return (dir_mask_low | 1<<pin_wr);
  125.             }
  126.             return 0;
  127.         }
  128.         inline static uint32_t clr_mask_high() FORCE_INLINE {
  129.             if(pin_wr>31) {
  130.                 return (dir_mask_high | 1<<((pin_wr-32)&31));
  131.             } else if(pin_wr>-1) {
  132.                 uint32_t result = dir_mask_high;
  133.                 wr_low();
  134.                 return result;
  135.             }
  136.             return 0;
  137.         }
  138.         static uint32_t xset_mask_low[256*has_data_low_pins];
  139.         static uint32_t xset_mask_high[256*has_data_high_pins];
  140. #endif // OPTIMIZE_ESP32
  141.     public:
  142.         static bool initialize() {
  143.             if(pin_cs > -1) {
  144.                 pinMode(pin_cs,OUTPUT);
  145.                 digitalWrite(pin_cs,HIGH);
  146.             }
  147.             if(pin_wr > -1) {
  148.                 pinMode(pin_wr,OUTPUT);
  149.                 digitalWrite(pin_wr,HIGH);
  150.             }
  151.             if(pin_rd > -1) {
  152.                 pinMode(pin_rd,OUTPUT);
  153.                 digitalWrite(pin_rd,HIGH);
  154.             }
  155.             if(pin_d0 > -1) {
  156.                 pinMode(pin_d0,OUTPUT);
  157.                 digitalWrite(pin_d0,HIGH);
  158.             }
  159.             if(pin_d1 > -1) {
  160.                 pinMode(pin_d1,OUTPUT);
  161.                 digitalWrite(pin_d1,HIGH);
  162.             }
  163.             if(pin_d2 > -1) {
  164.                 pinMode(pin_d2,OUTPUT);
  165.                 digitalWrite(pin_d2,HIGH);
  166.             }
  167.             if(pin_d3 > -1) {
  168.                 pinMode(pin_d3,OUTPUT);
  169.                 digitalWrite(pin_d3,HIGH);
  170.             }
  171.             if(pin_d4 > -1) {
  172.                 pinMode(pin_d4,OUTPUT);
  173.                 digitalWrite(pin_d4,HIGH);
  174.             }
  175.             if(pin_d5 > -1) {
  176.                 pinMode(pin_d5,OUTPUT);
  177.                 digitalWrite(pin_d5,HIGH);
  178.             }
  179.             if(pin_d6 > -1) {
  180.                 pinMode(pin_d6,OUTPUT);
  181.                 digitalWrite(pin_d6,HIGH);
  182.             }
  183.             if(pin_d7 > -1) {
  184.                 pinMode(pin_d7,OUTPUT);
  185.                 digitalWrite(pin_d7,HIGH);
  186.             }
  187. #ifdef OPTIMIZE_ESP32
  188.             if(has_data_low_pins) {
  189.                 for (int32_t c = 0; c<256; c++) {
  190.                     xset_mask_low[c] = 0;
  191.                     if ( pin_d0>-1 && pin_d0<32 && (c & 0x01)) xset_mask_low[c] |= (1 << pin_d0);
  192.                     if ( pin_d1>-1 && pin_d1<32 && (c & 0x02) ) xset_mask_low[c] |= (1 << pin_d1);
  193.                     if ( pin_d2>-1 && pin_d2<32 && (c & 0x04) ) xset_mask_low[c] |= (1 << pin_d2);
  194.                     if ( pin_d3>-1 && pin_d3<32 && (c & 0x08) ) xset_mask_low[c] |= (1 << pin_d3);
  195.                     if ( pin_d4>-1 && pin_d4<32 && (c & 0x10) ) xset_mask_low[c] |= (1 << pin_d4);
  196.                     if ( pin_d5>-1 && pin_d5<32 && (c & 0x20) ) xset_mask_low[c] |= (1 << pin_d5);
  197.                     if ( pin_d6>-1 && pin_d6<32 && (c & 0x40) ) xset_mask_low[c] |= (1 << pin_d6);
  198.                     if ( pin_d7>-1 && pin_d7<32 && (c & 0x80) ) xset_mask_low[c] |= (1 << pin_d7);
  199.                 }
  200.             }
  201.             if(has_data_high_pins) {
  202.                 for (int32_t c = 0; c<256; c++) {
  203.                     xset_mask_high[c] = 0;
  204.                     if ( pin_d0>31 && (c & 0x01)) xset_mask_high[c] |= (1 << ((pin_d0-32)&31));
  205.                     if ( pin_d1>31 && (c & 0x02) ) xset_mask_high[c] |= (1 << ((pin_d1-32)&31));
  206.                     if ( pin_d2>31 && (c & 0x04) ) xset_mask_high[c] |= (1 << ((pin_d2-32)&31));
  207.                     if ( pin_d3>31 && (c & 0x08) ) xset_mask_high[c] |= (1 << ((pin_d3-32)&31));
  208.                     if ( pin_d4>31 && (c & 0x10) ) xset_mask_high[c] |= (1 << ((pin_d4-32)&31));
  209.                     if ( pin_d5>31 && (c & 0x20) ) xset_mask_high[c] |= (1 << ((pin_d5-32)&31));
  210.                     if ( pin_d6>31 && (c & 0x40) ) xset_mask_high[c] |= (1 << ((pin_d6-32)&31));
  211.                     if ( pin_d7>31 && (c & 0x80) ) xset_mask_high[c] |= (1 << ((pin_d7-32)&31));
  212.                 }
  213.             }
  214. #endif // OPTIMIZE_ESP32
  215.             // Set to output once again in case ESP8266 D6 (MISO) is used for CS
  216.             if(pin_cs>-1) {
  217.                 pinMode(pin_cs, OUTPUT);
  218.                 digitalWrite(pin_cs, HIGH); // Chip select high (inactive)
  219.             }
  220.             return true;
  221.         }
  222.         static void deinitialize() {
  223.  
  224.         }
  225.         static uint8_t read_raw8() {
  226.             rd_low();
  227.             uint8_t b = 0xAA;
  228. #ifdef OPTIMIZE_ESP32
  229.             if(has_data_low_pins && has_data_high_pins) {
  230.                
  231.                 // 3x for bus access stabilization
  232.                 uint32_t pins_l = gpio_input_get();
  233.                 pins_l = gpio_input_get();
  234.                 pins_l = gpio_input_get();
  235.                 uint32_t pins_h = gpio_input_get_high();
  236.                 if(pin_d0>31) {
  237.                     b = (((pins_h>>((pin_d0-32)&31))&1)<<0);
  238.                 } else if(pin_d0>-1) {
  239.                     b = (((pins_l>>(pin_d0))&1)<<0);
  240.                 } else {
  241.                     b=0;
  242.                 }
  243.                 if(pin_d1>31) {
  244.                     b |= (((pins_h>>((pin_d1-32)&31))&1)<<1);
  245.                 } else if(pin_d1>-1) {
  246.                     b |= (((pins_l>>(pin_d1))&1)<<1);
  247.                 }
  248.                 if(pin_d2>31) {
  249.                     b |= (((pins_h>>((pin_d2-32)&31))&1)<<2);
  250.                 } else if(pin_d2>-1) {
  251.                     b |= (((pins_l>>(pin_d2))&1)<<2);
  252.                 }
  253.                 if(pin_d3>31) {
  254.                     b |= (((pins_h>>((pin_d3-32)&31))&1)<<3);
  255.                 } else if(pin_d3>-1) {
  256.                     b |= (((pins_l>>(pin_d3))&1)<<3);
  257.                 }
  258.                 if(pin_d4>31) {
  259.                     b |= (((pins_h>>((pin_d4-32)&31))&1)<<4);
  260.                 } else if(pin_d4>-1) {
  261.                     b |= (((pins_l>>((pin_d4)&31))&1)<<4);
  262.                 }
  263.                 if(pin_d5>31) {
  264.                     b |= (((pins_h>>((pin_d5-32)&31))&1)<<5);
  265.                 } else if(pin_d5>-1) {
  266.                     b |= (((pins_l>>(pin_d5))&1)<<5);
  267.                 }
  268.                 if(pin_d6>31) {
  269.                     b |= (((pins_h>>((pin_d6-32)&31))&1)<<6);
  270.                 } else if(pin_d6>-1) {
  271.                     b |= (((pins_l>>(pin_d6))&1)<<6);
  272.                 }
  273.                 if(pin_d7>31) {
  274.                     b |= (((pins_h>>((pin_d7-32)&31))&1)<<7);
  275.                 } else if(pin_d7>-1) {
  276.                     b |= (((pins_l>>(pin_d7))&1)<<7);
  277.                 }
  278.             } else if(has_data_low_pins) {
  279.                 uint32_t pins_l = gpio_input_get();
  280.                 pins_l = gpio_input_get();
  281.                 pins_l = gpio_input_get();
  282.                 if(pin_d0>-1) {
  283.                     b = (((pins_l>>(pin_d0))&1)<<0);
  284.                 } else {
  285.                     b=0;
  286.                 }
  287.                 if(pin_d1>-1) {
  288.                     b |= (((pins_l>>(pin_d1))&1)<<1);
  289.                 }
  290.                 if(pin_d2>-1) {
  291.                     b |= (((pins_l>>(pin_d2))&1)<<2);
  292.                 }
  293.                 if(pin_d3>-1) {
  294.                     b |= (((pins_l>>(pin_d3))&1)<<3);
  295.                 }
  296.                 if(pin_d4>-1) {
  297.                     b |= (((pins_l>>(pin_d4))&1)<<4);
  298.                 }
  299.                 if(pin_d5>-1) {
  300.                     b |= (((pins_l>>(pin_d5))&1)<<5);
  301.                 }
  302.                 if(pin_d6>-1) {
  303.                     b |= (((pins_l>>(pin_d6))&1)<<6);
  304.                 }
  305.                 if(pin_d7>-1) {
  306.                     b |= (((pins_l>>(pin_d7))&1)<<7);
  307.                 }
  308.             } else {
  309.                 uint32_t pins_h = gpio_input_get_high();
  310.                 pins_h = gpio_input_get_high();
  311.                 pins_h = gpio_input_get_high();
  312.                 if(pin_d0>-1) {
  313.                     b = (((pins_h>>((pin_d0-32)&31))&1)<<0);
  314.                 } else {
  315.                     b=0;
  316.                 }
  317.                 if(pin_d1>-1) {
  318.                     b |= (((pins_h>>((pin_d1-32)&31))&1)<<1);
  319.                 }
  320.                 if(pin_d2>-1) {
  321.                     b |= (((pins_h>>((pin_d2-32)&31))&1)<<2);
  322.                 }
  323.                 if(pin_d3>-1) {
  324.                     b |= (((pins_h>>((pin_d3-32)&31))&1)<<3);
  325.                 }
  326.                 if(pin_d4>-1) {
  327.                     b |= (((pins_h>>((pin_d4-32)&31))&1)<<4);
  328.                 }
  329.                 if(pin_d5>-1) {
  330.                     b |= (((pins_h>>((pin_d5-32)&31))&1)<<5);
  331.                 }
  332.                 if(pin_d6>-1) {
  333.                     b |= (((pins_h>>((pin_d6-32)&31))&1)<<6);
  334.                 }
  335.                 if(pin_d7>-1) {
  336.                     b |= (((pins_h>>((pin_d7-32)&31))&1)<<7);
  337.                 }
  338.             }
  339. #else // !OPTIMIZE_ESP32
  340.             if(pin_d0>-1) {
  341.                 b=digitalRead(pin_d0);
  342.             } else {
  343.                 b=0;
  344.             }
  345.             if(pin_d1>-1) {
  346.                 b|=digitalRead(pin_d1)<<1;
  347.             }
  348.             if(pin_d2>-1) {
  349.                 b|=digitalRead(pin_d2)<<2;
  350.             }
  351.             if(pin_d3>-1) {
  352.                 b|=digitalRead(pin_d3)<<3;
  353.             }
  354.             if(pin_d4>-1) {
  355.                 b|=digitalRead(pin_d4)<<4;
  356.             }
  357.             if(pin_d5>-1) {
  358.                 b|=digitalRead(pin_d5)<<5;
  359.             }
  360.             if(pin_d6>-1) {
  361.                 b|=digitalRead(pin_d6)<<6;
  362.             }
  363.             if(pin_d7>-1) {
  364.                 b|=digitalRead(pin_d7)<<7;
  365.             }
  366. #endif // !OPTIMIZE_ESP32
  367.             rd_high();
  368.             return b;
  369.         }
  370.         inline static void write_raw8(uint8_t value) FORCE_INLINE {
  371. #ifdef OPTIMIZE_ESP32
  372.             if(has_data_low_pins) {
  373.                 GPIO.out_w1tc = clr_mask_low(); GPIO.out_w1ts = xset_mask_low[value];
  374.             }
  375.             if(has_data_high_pins) {
  376.                 GPIO.out1_w1tc.val = clr_mask_high(); GPIO.out1_w1ts.val = xset_mask_high[value];
  377.             }
  378. #else // !OPTIMIZE_ESP32
  379.             digitalWrite(pin_d0,value&1);
  380.             digitalWrite(pin_d1,(value>>1)&1);
  381.             digitalWrite(pin_d2,(value>>2)&1);
  382.             digitalWrite(pin_d3,(value>>3)&1);
  383.             digitalWrite(pin_d4,(value>>4)&1);
  384.             digitalWrite(pin_d5,(value>>5)&1);
  385.             digitalWrite(pin_d6,(value>>6)&1);
  386.             digitalWrite(pin_d7,(value>>7)&1);
  387. #endif // !OPTIMIZE_ESP32
  388.             wr_high();
  389.         }
  390.         static void write_raw8_repeat(uint8_t value, size_t count) {
  391.             write_raw8(value);
  392.             while(--count) {
  393.                 wr_low(); wr_high();
  394.             }
  395.         }
  396.         inline static void write_raw(const uint8_t* buffer, size_t length) {
  397.             if(length) {
  398. #if 1
  399.                 uint8_t old=*buffer;
  400.                 write_raw8(*buffer++);
  401.                 while(--length) {
  402.                     if(old==*buffer) {
  403.                         wr_low(); wr_high();
  404.                     } else {
  405.                         write_raw8(*buffer);
  406.                     }
  407.                     old = *buffer++;
  408.                 }
  409. #else
  410.                 while(length--) {
  411.                     write_raw8(*buffer++);
  412.                 }
  413. #endif
  414.             }
  415.         }
  416.         inline static void write_raw16(uint16_t value) FORCE_INLINE {
  417.             uint8_t b = uint8_t(value >> 8);
  418. #ifdef OPTIMIZE_ESP32
  419.             if(has_data_low_pins) {
  420.                 GPIO.out_w1tc = clr_mask_low(); GPIO.out_w1ts = xset_mask_low[b];
  421.             }
  422.             if(has_data_high_pins) {
  423.                 GPIO.out1_w1tc.val = clr_mask_high(); GPIO.out1_w1ts.val = xset_mask_high[b];
  424.             }
  425.             wr_high();
  426.             b=value;
  427.             if(has_data_low_pins) {
  428.                 GPIO.out_w1tc = clr_mask_low(); GPIO.out_w1ts = xset_mask_low[b];
  429.             }
  430.             if(has_data_high_pins) {
  431.                 GPIO.out1_w1tc.val = clr_mask_high(); GPIO.out1_w1ts.val = xset_mask_high[b];
  432.             }
  433. #else // !OPTIMIZE_ESP32
  434.             digitalWrite(pin_d0,b&1);
  435.             digitalWrite(pin_d1,(b>>1)&1);
  436.             digitalWrite(pin_d2,(b>>2)&1);
  437.             digitalWrite(pin_d3,(b>>3)&1);
  438.             digitalWrite(pin_d4,(b>>4)&1);
  439.             digitalWrite(pin_d5,(b>>5)&1);
  440.             digitalWrite(pin_d6,(b>>6)&1);
  441.             digitalWrite(pin_d7,(b>>7)&1);
  442.             wr_high();
  443.             b=value;
  444.             digitalWrite(pin_d0,b&1);
  445.             digitalWrite(pin_d1,(b>>1)&1);
  446.             digitalWrite(pin_d2,(b>>2)&1);
  447.             digitalWrite(pin_d3,(b>>3)&1);
  448.             digitalWrite(pin_d4,(b>>4)&1);
  449.             digitalWrite(pin_d5,(b>>5)&1);
  450.             digitalWrite(pin_d6,(b>>6)&1);
  451.             digitalWrite(pin_d7,(b>>7)&1);
  452. #endif // !OPTIMIZE_ESP32
  453.             wr_high();
  454.         }
  455.         static void write_raw16_repeat(uint16_t value, size_t count) {
  456.             write_raw16(value);
  457.             if((value >> 8) == (value & 0x00FF)) {
  458.                 while(--count) {
  459.                     wr_low(); wr_high();
  460.                     wr_low(); wr_high();
  461.                 }
  462.             } else {
  463.                 while(--count) {
  464.                     write_raw16(value);
  465.                 }
  466.             }
  467.         }
  468.         inline static void write_raw32(uint32_t value) FORCE_INLINE {
  469. #ifdef OPTIMIZE_ESP32
  470.             uint8_t b = uint8_t(value >> 24);
  471.             if(has_data_low_pins) {
  472.                 GPIO.out_w1tc = clr_mask_low(); GPIO.out_w1ts = xset_mask_low[b];
  473.             }
  474.             if(has_data_high_pins) {
  475.                 GPIO.out1_w1tc.val = clr_mask_high(); GPIO.out1_w1ts.val = xset_mask_high[b];
  476.             }
  477.             wr_high();
  478.             b=uint8_t(value>>16);
  479.             if(has_data_low_pins) {
  480.                 GPIO.out_w1tc = clr_mask_low(); GPIO.out_w1ts = xset_mask_low[b];
  481.             }
  482.             if(has_data_high_pins) {
  483.                 GPIO.out1_w1tc.val = clr_mask_high(); GPIO.out1_w1ts.val = xset_mask_high[b];
  484.             }
  485.             wr_high();
  486.             b = uint8_t(value >> 8);
  487.             if(has_data_low_pins) {
  488.                 GPIO.out_w1tc = clr_mask_low(); GPIO.out_w1ts = xset_mask_low[b];
  489.             }
  490.             if(has_data_high_pins) {
  491.                 GPIO.out1_w1tc.val = clr_mask_high(); GPIO.out1_w1ts.val = xset_mask_high[b];
  492.             }
  493.             wr_high();
  494.             b=value;
  495.             if(has_data_low_pins) {
  496.                 GPIO.out_w1tc = clr_mask_low(); GPIO.out_w1ts = xset_mask_low[b];
  497.             }
  498.             if(has_data_high_pins) {
  499.                 GPIO.out1_w1tc.val = clr_mask_high(); GPIO.out1_w1ts.val = xset_mask_high[b];
  500.             }
  501.             wr_high();
  502. #else // !OPTIMIZE_ESP32
  503.             write_raw16(value>>16);
  504.             write_raw16(value);
  505. #endif // !OPTIMIZE_ESP32
  506.         }
  507.         static void write_raw32_repeat(uint32_t value, size_t count) {
  508.             while(count--) {
  509.                 write_raw32(value);
  510.             }
  511.         }
  512.         inline static void pin_mode(int8_t pin, uint8_t mode) FORCE_INLINE {
  513. #ifdef OPTIMIZE_ESP32
  514.             if(pin<32) {
  515.                 if(mode==INPUT) {
  516.                     GPIO.enable_w1tc = ((uint32_t)1 << pin);
  517.                 } else {
  518.                     GPIO.enable_w1ts = ((uint32_t)1 << pin);
  519.                 }
  520.             } else {
  521.                 if(mode==INPUT) {
  522.                     GPIO.enable1_w1tc.val = ((uint32_t)1 << ((pin-32)&31));
  523.                 } else {
  524.                     GPIO.enable1_w1ts.val = ((uint32_t)1 << ((pin-32)&31));
  525.                 }
  526.             }
  527.             ESP_REG(DR_REG_IO_MUX_BASE + esp32_gpioMux[pin].reg) // Register lookup
  528.                 = ((uint32_t)2 << FUN_DRV_S)                        // Set drive strength 2
  529.                 | (FUN_IE)                                          // Input enable
  530.                 | ((uint32_t)2 << MCU_SEL_S);                       // Function select 2
  531.             GPIO.pin[pin].val = 1;  
  532. #else // !OPTIMIZE_ESP32
  533.         if(mode==INPUT) {
  534.             digitalWrite(pin,LOW);
  535.             pinMode(pin,INPUT);
  536.         } else {
  537.             digitalWrite(pin,HIGH);
  538.             pinMode(pin,OUTPUT);
  539.         }
  540. #endif // !OPTIMIZE_ESP32
  541.         }
  542.         inline static void begin_write() FORCE_INLINE {
  543.             cs_low();
  544.         }
  545.         inline static void end_write() FORCE_INLINE {
  546.             cs_high();
  547.         }
  548.         inline static void begin_read() FORCE_INLINE {
  549.             cs_low();
  550.         }
  551.         inline static void end_read() FORCE_INLINE {
  552.             cs_high();
  553.         }
  554.         static void direction(uint8_t mode) {
  555.             pin_mode(pin_d0,mode);
  556.             pin_mode(pin_d1,mode);
  557.             pin_mode(pin_d2,mode);
  558.             pin_mode(pin_d3,mode);
  559.             pin_mode(pin_d4,mode);
  560.             pin_mode(pin_d5,mode);
  561.             pin_mode(pin_d6,mode);
  562.             pin_mode(pin_d7,mode);
  563.         }
  564.         inline static void cs_low() FORCE_INLINE {
  565. #ifdef OPTIMIZE_ESP32
  566.             if(pin_cs>31) {
  567.                 GPIO.out1_w1tc.val = (1 << ((pin_cs - 32)&31));
  568.             } else if(pin_cs>-1) {
  569.                 GPIO.out_w1tc = (1 << ((pin_cs - 32)&31));
  570.             }
  571. #else // !OPTIMIZE_ESP32
  572.             digitalWrite(pin_cs,LOW);
  573. #endif // !OPTIMIZE_ESP32
  574.         }
  575.         inline static void cs_high() FORCE_INLINE {
  576. #ifdef OPTIMIZE_ESP32
  577.             if(pin_cs>31) {
  578.                 GPIO.out1_w1ts.val = (1 << ((pin_cs - 32)&31));
  579.             } else if(pin_cs>-1) {
  580.                 GPIO.out_w1ts = (1 << ((pin_cs - 32)&31));
  581.             }
  582. #else // !OPTIMIZE_ESP32
  583.             digitalWrite(pin_cs,HIGH);
  584. #endif // !OPTIMIZE_ESP32
  585.         }
  586.        
  587.         inline static void wr_low() FORCE_INLINE {
  588. #ifdef OPTIMIZE_ESP32
  589.             if(pin_wr>31) {
  590.                 GPIO.out1_w1tc.val = (1 << ((pin_wr - 32)&31));
  591.             } else if(pin_wr>-1) {
  592.                 GPIO.out_w1tc = (1 << ((pin_wr - 32)&31));
  593.             }
  594. #else // !OPTIMIZE_ESP32
  595.             digitalWrite(pin_wr,LOW);
  596. #endif // !OPTIMIZE_ESP32
  597.         }
  598.         inline static void wr_high() FORCE_INLINE {
  599. #ifdef OPTIMIZE_ESP32
  600.             if(pin_wr>31) {
  601.                 GPIO.out1_w1ts.val = (1 << ((pin_wr - 32)&31));
  602.             } else if(pin_cs>-1) {
  603.                 GPIO.out_w1ts = (1 << ((pin_wr - 32)&31));
  604.             }
  605. #else // !OPTIMIZE_ESP32
  606.             digitalWrite(pin_wr,HIGH);
  607. #endif // !OPTIMIZE_ESP32
  608.         }
  609.  
  610.         inline static void rd_low() FORCE_INLINE {
  611. #ifdef OPTIMIZE_ESP32
  612.             if(pin_rd>31) {
  613.                 GPIO.out1_w1tc.val = (1 << ((pin_rd - 32)&31));
  614.             } else if(pin_rd>-1) {
  615.                 GPIO.out_w1tc = (1 << ((pin_rd - 32)&31));
  616.             }
  617. #else // !OPTIMIZE_ESP32
  618.             digitalWrite(pin_rd,LOW);
  619. #endif // !OPTIMIZE_ESP32
  620.         }
  621.         inline static void rd_high() FORCE_INLINE {
  622. #ifdef OPTIMIZE_ESP32
  623.             if(pin_rd>31) {
  624.                 GPIO.out1_w1ts.val = (1 << ((pin_rd - 32)&31));
  625.             } else if(pin_cs>-1) {
  626.                 GPIO.out_w1ts = (1 << ((pin_rd - 32)&31));
  627.             }
  628. #else // !OPTIMIZE_ESP32
  629.             digitalWrite(pin_rd,HIGH);
  630. #endif // !OPTIMIZE_ESP32
  631.         }
  632.  
  633.     };
  634. #ifdef OPTIMIZE_ESP32
  635.     template<int8_t PinCS,
  636.             int8_t PinWR,
  637.             int8_t PinRD,
  638.             int8_t PinD0,
  639.             int8_t PinD1,
  640.             int8_t PinD2,
  641.             int8_t PinD3,
  642.             int8_t PinD4,
  643.             int8_t PinD5,
  644.             int8_t PinD6,
  645.             int8_t PinD7>
  646.             uint32_t tft_p8<PinCS,
  647.                                 PinWR,
  648.                                 PinRD,
  649.                                 PinD0,
  650.                                 PinD1,
  651.                                 PinD2,
  652.                                 PinD3,
  653.                                 PinD4,
  654.                                 PinD5,
  655.                                 PinD6,
  656.                                 PinD7>::xset_mask_low[256*tft_p8<PinCS,
  657.                                 PinWR,
  658.                                 PinRD,
  659.                                 PinD0,
  660.                                 PinD1,
  661.                                 PinD2,
  662.                                 PinD3,
  663.                                 PinD4,
  664.                                 PinD5,
  665.                                 PinD6,
  666.                                 PinD7>::has_data_low_pins];
  667.     template<int8_t PinCS,
  668.             int8_t PinWR,
  669.             int8_t PinRD,
  670.             int8_t PinD0,
  671.             int8_t PinD1,
  672.             int8_t PinD2,
  673.             int8_t PinD3,
  674.             int8_t PinD4,
  675.             int8_t PinD5,
  676.             int8_t PinD6,
  677.             int8_t PinD7>
  678.             uint32_t tft_p8<PinCS,
  679.                                 PinWR,
  680.                                 PinRD,
  681.                                 PinD0,
  682.                                 PinD1,
  683.                                 PinD2,
  684.                                 PinD3,
  685.                                 PinD4,
  686.                                 PinD5,
  687.                                 PinD6,
  688.                                 PinD7>::xset_mask_high[256*tft_p8<PinCS,
  689.                                 PinWR,
  690.                                 PinRD,
  691.                                 PinD0,
  692.                                 PinD1,
  693.                                 PinD2,
  694.                                 PinD3,
  695.                                 PinD4,
  696.                                 PinD5,
  697.                                 PinD6,
  698.                                 PinD7>::has_data_high_pins];
  699. #endif // OPTIMIZE_ESP32
  700. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement