acid-drop- Hacking the planet from a LilyGo T-Deck using custom firmware |
git clone git://git.acid.vegas/acid-drop.git |
Log | Files | Refs | Archive | README | LICENSE |
Arduino_RTLPAR8.cpp (7492B)
1 #ifdef RTL8722DM 2 3 #include "Arduino_DataBus.h" 4 #include "Arduino_RTLPAR8.h" 5 6 Arduino_RTLPAR8::Arduino_RTLPAR8( 7 int8_t dc, int8_t cs, int8_t wr, int8_t rd, 8 int8_t d0, int8_t d1, int8_t d2, int8_t d3, int8_t d4, int8_t d5, int8_t d6, int8_t d7) 9 : _dc(dc), _cs(cs), _wr(wr), _rd(rd), 10 _d0(d0), _d1(d1), _d2(d2), _d3(d3), _d4(d4), _d5(d5), _d6(d6), _d7(d7) 11 { 12 } 13 14 bool Arduino_RTLPAR8::begin(int32_t speed, int8_t dataMode) 15 { 16 UNUSED(speed); 17 UNUSED(dataMode); 18 19 pinMode(_dc, OUTPUT); 20 digitalWrite(_dc, HIGH); // Data mode 21 _dcPort = (PORTreg_t)portOutputRegister(digitalPinToPort(_dc)); 22 _dcPinMaskSet = digitalPinToBitMask(_dc); 23 _dcPinMaskClr = ~_dcPinMaskSet; 24 25 if (_cs != GFX_NOT_DEFINED) 26 { 27 pinMode(_cs, OUTPUT); 28 digitalWrite(_cs, HIGH); // disable chip select 29 _csPort = (PORTreg_t)portOutputRegister(digitalPinToPort(_cs)); 30 _csPinMaskSet = digitalPinToBitMask(_cs); 31 } 32 _csPinMaskClr = ~_csPinMaskSet; 33 34 pinMode(_wr, OUTPUT); 35 digitalWrite(_wr, HIGH); // Set write strobe high (inactive) 36 _wrPort = (PORTreg_t)portOutputRegister(digitalPinToPort(_wr)); 37 _wrPinMaskSet = digitalPinToBitMask(_wr); 38 _wrPinMaskClr = ~_wrPinMaskSet; 39 40 if (_rd != GFX_NOT_DEFINED) 41 { 42 pinMode(_rd, OUTPUT); 43 digitalWrite(_rd, HIGH); 44 _rdPort = (PORTreg_t)portOutputRegister(digitalPinToPort(_rd)); 45 _rdPinMaskSet = digitalPinToBitMask(_rd); 46 } 47 else 48 { 49 _rdPort = _dcPort; 50 _rdPinMaskSet = 0; 51 } 52 _rdPinMaskClr = ~_rdPinMaskSet; 53 54 // TODO: check pin in same port 55 pinMode(_d0, OUTPUT); 56 digitalWrite(_d0, HIGH); 57 pinMode(_d1, OUTPUT); 58 digitalWrite(_d1, HIGH); 59 pinMode(_d2, OUTPUT); 60 digitalWrite(_d2, HIGH); 61 pinMode(_d3, OUTPUT); 62 digitalWrite(_d3, HIGH); 63 pinMode(_d4, OUTPUT); 64 digitalWrite(_d4, HIGH); 65 pinMode(_d5, OUTPUT); 66 digitalWrite(_d5, HIGH); 67 pinMode(_d6, OUTPUT); 68 digitalWrite(_d6, HIGH); 69 pinMode(_d7, OUTPUT); 70 digitalWrite(_d7, HIGH); 71 72 // INIT 8-bit mask 73 _dataPort = (PORTreg_t)portOutputRegister(digitalPinToPort(_d0)); 74 for (int32_t c = 0; c < 256; c++) 75 { 76 _xset_mask[c] = 0; 77 if (c & 0x01) 78 { 79 _xset_mask[c] |= digitalPinToBitMask(_d0); 80 } 81 if (c & 0x02) 82 { 83 _xset_mask[c] |= digitalPinToBitMask(_d1); 84 } 85 if (c & 0x04) 86 { 87 _xset_mask[c] |= digitalPinToBitMask(_d2); 88 } 89 if (c & 0x08) 90 { 91 _xset_mask[c] |= digitalPinToBitMask(_d3); 92 } 93 if (c & 0x10) 94 { 95 _xset_mask[c] |= digitalPinToBitMask(_d4); 96 } 97 if (c & 0x20) 98 { 99 _xset_mask[c] |= digitalPinToBitMask(_d5); 100 } 101 if (c & 0x40) 102 { 103 _xset_mask[c] |= digitalPinToBitMask(_d6); 104 } 105 if (c & 0x80) 106 { 107 _xset_mask[c] |= digitalPinToBitMask(_d7); 108 } 109 } 110 _dataPinMaskClr = ~_xset_mask[255]; 111 112 return true; 113 } 114 115 void Arduino_RTLPAR8::beginWrite() 116 { 117 DC_HIGH(); 118 CS_LOW(); 119 } 120 121 void Arduino_RTLPAR8::endWrite() 122 { 123 CS_HIGH(); 124 } 125 126 void Arduino_RTLPAR8::writeCommand(uint8_t c) 127 { 128 DC_LOW(); 129 130 WRITE(c); 131 132 DC_HIGH(); 133 } 134 135 void Arduino_RTLPAR8::writeCommand16(uint16_t c) 136 { 137 DC_LOW(); 138 139 _data16.value = c; 140 WRITE(_data16.msb); 141 WRITE(_data16.lsb); 142 143 DC_HIGH(); 144 } 145 146 void Arduino_RTLPAR8::write(uint8_t d) 147 { 148 WRITE(d); 149 } 150 151 void Arduino_RTLPAR8::write16(uint16_t d) 152 { 153 _data16.value = d; 154 WRITE(_data16.msb); 155 WRITE(_data16.lsb); 156 } 157 158 void Arduino_RTLPAR8::writeRepeat(uint16_t p, uint32_t len) 159 { 160 uint32_t dataMaskBase = *_dataPort & _dataPinMaskClr; 161 uint32_t wrMaskBase = *_wrPort & _wrPinMaskClr; 162 uint32_t wrMaskSet = wrMaskBase | _wrPinMaskSet; 163 _data16.value = p; 164 if (_data16.msb == _data16.lsb) 165 { 166 *_dataPort = dataMaskBase | _xset_mask[_data16.msb]; 167 while (len--) 168 { 169 *_wrPort = wrMaskBase; 170 *_wrPort = wrMaskSet; 171 *_wrPort = wrMaskBase; 172 *_wrPort = wrMaskSet; 173 } 174 } 175 else 176 { 177 uint32_t hiMask = _xset_mask[_data16.msb]; 178 uint32_t loMask = _xset_mask[_data16.lsb]; 179 while (len--) 180 { 181 *_dataPort = dataMaskBase | hiMask; 182 *_wrPort = wrMaskBase; 183 *_wrPort = wrMaskSet; 184 185 *_dataPort = dataMaskBase | loMask; 186 *_wrPort = wrMaskBase; 187 *_wrPort = wrMaskSet; 188 } 189 } 190 } 191 192 void Arduino_RTLPAR8::writePixels(uint16_t *data, uint32_t len) 193 { 194 uint32_t dataMaskBase = *_dataPort & _dataPinMaskClr; 195 uint32_t wrMaskBase = *_wrPort & _wrPinMaskClr; 196 uint32_t wrMaskSet = wrMaskBase | _wrPinMaskSet; 197 while (len--) 198 { 199 _data16.value = *(data++); 200 uint32_t hiMask = _xset_mask[_data16.msb]; 201 uint32_t loMask = _xset_mask[_data16.lsb]; 202 *_dataPort = dataMaskBase | hiMask; 203 *_wrPort = wrMaskBase; 204 *_wrPort = wrMaskSet; 205 206 *_dataPort = dataMaskBase | loMask; 207 *_wrPort = wrMaskBase; 208 *_wrPort = wrMaskSet; 209 } 210 } 211 212 void Arduino_RTLPAR8::writeC8D8(uint8_t c, uint8_t d) 213 { 214 DC_LOW(); 215 216 WRITE(c); 217 218 DC_HIGH(); 219 220 WRITE(d); 221 } 222 223 void Arduino_RTLPAR8::writeC8D16(uint8_t c, uint16_t d) 224 { 225 DC_LOW(); 226 227 WRITE(c); 228 229 DC_HIGH(); 230 231 _data16.value = d; 232 WRITE(_data16.msb); 233 WRITE(_data16.lsb); 234 } 235 236 void Arduino_RTLPAR8::writeC8D16D16(uint8_t c, uint16_t d1, uint16_t d2) 237 { 238 DC_LOW(); 239 240 WRITE(c); 241 242 DC_HIGH(); 243 244 _data16.value = d1; 245 WRITE(_data16.msb); 246 WRITE(_data16.lsb); 247 _data16.value = d2; 248 WRITE(_data16.msb); 249 WRITE(_data16.lsb); 250 } 251 252 void Arduino_RTLPAR8::writeBytes(uint8_t *data, uint32_t len) 253 { 254 while (len--) 255 { 256 WRITE(*data++); 257 } 258 } 259 260 void Arduino_RTLPAR8::writePattern(uint8_t *data, uint8_t len, uint32_t repeat) 261 { 262 while (repeat--) 263 { 264 writeBytes(data, len); 265 } 266 } 267 268 void Arduino_RTLPAR8::writeIndexedPixels(uint8_t *data, uint16_t *idx, uint32_t len) 269 { 270 uint32_t dataMaskBase = *_dataPort & _dataPinMaskClr; 271 uint32_t wrMaskBase = *_wrPort & _wrPinMaskClr; 272 uint32_t wrMaskSet = wrMaskBase | _wrPinMaskSet; 273 while (len--) 274 { 275 _data16.value = idx[*data++]; 276 277 *_dataPort = dataMaskBase | _xset_mask[_data16.msb]; 278 *_wrPort = wrMaskBase; 279 *_wrPort = wrMaskSet; 280 281 *_dataPort = dataMaskBase | _xset_mask[_data16.lsb]; 282 *_wrPort = wrMaskBase; 283 *_wrPort = wrMaskSet; 284 } 285 } 286 287 void Arduino_RTLPAR8::writeIndexedPixelsDouble(uint8_t *data, uint16_t *idx, uint32_t len) 288 { 289 uint32_t dataMaskBase = *_dataPort & _dataPinMaskClr; 290 uint32_t wrMaskBase = *_wrPort & _wrPinMaskClr; 291 uint32_t wrMaskSet = wrMaskBase | _wrPinMaskSet; 292 while (len--) 293 { 294 _data16.value = idx[*data++]; 295 uint32_t hiMask = _xset_mask[_data16.msb]; 296 uint32_t loMask = _xset_mask[_data16.lsb]; 297 298 *_dataPort = dataMaskBase | hiMask; 299 *_wrPort = wrMaskBase; 300 *_wrPort = wrMaskSet; 301 302 *_dataPort = dataMaskBase | loMask; 303 *_wrPort = wrMaskBase; 304 *_wrPort = wrMaskSet; 305 306 *_dataPort = dataMaskBase | hiMask; 307 *_wrPort = wrMaskBase; 308 *_wrPort = wrMaskSet; 309 310 *_dataPort = dataMaskBase | loMask; 311 *_wrPort = wrMaskBase; 312 *_wrPort = wrMaskSet; 313 } 314 } 315 316 INLINE void Arduino_RTLPAR8::WRITE(uint8_t d) 317 { 318 uint32_t dataMaskBase = *_dataPort & _dataPinMaskClr; 319 uint32_t wrMaskBase = *_wrPort & _wrPinMaskClr; 320 *_dataPort = dataMaskBase | _xset_mask[d]; 321 322 *_wrPort = wrMaskBase; 323 *_wrPort = wrMaskBase | _wrPinMaskSet; 324 } 325 326 /******** low level bit twiddling **********/ 327 328 INLINE void Arduino_RTLPAR8::DC_HIGH(void) 329 { 330 *_dcPort |= _dcPinMaskSet; 331 } 332 333 INLINE void Arduino_RTLPAR8::DC_LOW(void) 334 { 335 *_dcPort &= _dcPinMaskClr; 336 } 337 338 INLINE void Arduino_RTLPAR8::CS_HIGH(void) 339 { 340 if (_cs != GFX_NOT_DEFINED) 341 { 342 *_csPort |= _csPinMaskSet; 343 } 344 } 345 346 INLINE void Arduino_RTLPAR8::CS_LOW(void) 347 { 348 if (_cs != GFX_NOT_DEFINED) 349 { 350 *_csPort &= _csPinMaskClr; 351 } 352 } 353 354 #endif // #ifdef RTL8722DM