Added SPI burst mode to spi 8 bit.
Dependents: Bicycl_Computer_NUCLEO-F411RE Bicycl_Computer_NUCLEO-L476RG
Fork of UniGraphic by
Added SPI burst mode to this graphics driver. If whoever wants this rolled in to repository let me know. I replaced _spi.write(); with fastWrite(); and clearRX();
SPI8.cpp
// need to re-create SPI firmware to access SPI handle static SPI_HandleTypeDef SpiHandle; void SPI8::fastWrite(int data) { SpiHandle.Instance = SPI1; // Check if data is transmitted while ((SpiHandle.Instance->SR & SPI_SR_TXE) == 0); SpiHandle.Instance->DR = data; } void SPI8::clearRX( void ) { SpiHandle.Instance = SPI1; //Check if the RX buffer is busy //While busy, keep checking while (SpiHandle.Instance->SR & SPI_SR_BSY){ // Check RX buffer readable while ((SpiHandle.Instance->SR & SPI_SR_RXNE) == 0); int dummy = SpiHandle.Instance->DR; } }
Protocols/PAR16.cpp@36:0ced7cf0ec8c, 2017-08-04 (annotated)
- Committer:
- trevieze
- Date:
- Fri Aug 04 19:47:21 2017 +0000
- Revision:
- 36:0ced7cf0ec8c
- Parent:
- 11:b842b8e332cb
- Child:
- 20:14daa48ffd4c
Added Gimp Graphic Display Routine for Compass
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Geremia | 4:12ba0ecc2c1f | 1 | /* mbed UniGraphic library - PAR16 protocol class |
Geremia | 4:12ba0ecc2c1f | 2 | * Copyright (c) 2015 Giuliano Dianda |
Geremia | 4:12ba0ecc2c1f | 3 | * Released under the MIT License: http://mbed.org/license/mit |
Geremia | 4:12ba0ecc2c1f | 4 | * |
Geremia | 4:12ba0ecc2c1f | 5 | * Derived work of: |
Geremia | 4:12ba0ecc2c1f | 6 | * |
Geremia | 4:12ba0ecc2c1f | 7 | * mbed library for 240*320 pixel display TFT based on ILI9341 LCD Controller |
Geremia | 4:12ba0ecc2c1f | 8 | * Copyright (c) 2013 Peter Drescher - DC2PD |
Geremia | 4:12ba0ecc2c1f | 9 | * |
Geremia | 4:12ba0ecc2c1f | 10 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
Geremia | 4:12ba0ecc2c1f | 11 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
Geremia | 4:12ba0ecc2c1f | 12 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
Geremia | 4:12ba0ecc2c1f | 13 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
Geremia | 4:12ba0ecc2c1f | 14 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
Geremia | 4:12ba0ecc2c1f | 15 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
Geremia | 4:12ba0ecc2c1f | 16 | * THE SOFTWARE. |
Geremia | 4:12ba0ecc2c1f | 17 | */ |
Geremia | 4:12ba0ecc2c1f | 18 | #include "PAR16.h" |
Geremia | 4:12ba0ecc2c1f | 19 | |
Geremia | 4:12ba0ecc2c1f | 20 | PAR16::PAR16(PortName port, PinName CS, PinName reset, PinName DC, PinName WR, PinName RD) |
Geremia | 4:12ba0ecc2c1f | 21 | : _port(port,0xFFFF), _CS(CS), _reset(reset), _DC(DC), _WR(WR), _RD(RD) |
Geremia | 4:12ba0ecc2c1f | 22 | { |
Geremia | 4:12ba0ecc2c1f | 23 | _reset = 1; |
Geremia | 4:12ba0ecc2c1f | 24 | _DC=1; |
Geremia | 4:12ba0ecc2c1f | 25 | _WR=1; |
Geremia | 4:12ba0ecc2c1f | 26 | _RD=1; |
Geremia | 4:12ba0ecc2c1f | 27 | _CS=1; |
Geremia | 4:12ba0ecc2c1f | 28 | #ifdef STMPORTDEBUG |
Geremia | 4:12ba0ecc2c1f | 29 | findport(port); //on return, GPIO get disabled |
Geremia | 4:12ba0ecc2c1f | 30 | #endif |
Geremia | 4:12ba0ecc2c1f | 31 | _port.mode(PullNone); |
Geremia | 4:12ba0ecc2c1f | 32 | _port.output(); // will re-enable our GPIO port |
Geremia | 4:12ba0ecc2c1f | 33 | hw_reset(); |
Geremia | 4:12ba0ecc2c1f | 34 | } |
Geremia | 4:12ba0ecc2c1f | 35 | |
Geremia | 4:12ba0ecc2c1f | 36 | #ifdef STMPORTDEBUG |
Geremia | 4:12ba0ecc2c1f | 37 | // create a port obj with STM HAL drivers, just to collect memorymapped regs |
Geremia | 4:12ba0ecc2c1f | 38 | void PAR16::findport(PortName port) |
Geremia | 4:12ba0ecc2c1f | 39 | { |
Geremia | 4:12ba0ecc2c1f | 40 | port_t tempport; |
Geremia | 4:12ba0ecc2c1f | 41 | port_init(&tempport, port, 0xFF, PIN_INPUT); |
Geremia | 4:12ba0ecc2c1f | 42 | outreg = tempport.reg_out; |
Geremia | 4:12ba0ecc2c1f | 43 | inreg = tempport.reg_in; |
Geremia | 4:12ba0ecc2c1f | 44 | // debug("out 0x%.8X in 0x%.8X\r\n", outreg, inreg); |
Geremia | 4:12ba0ecc2c1f | 45 | } |
Geremia | 4:12ba0ecc2c1f | 46 | #endif |
Geremia | 4:12ba0ecc2c1f | 47 | void PAR16::wr_cmd8(unsigned char cmd) |
Geremia | 4:12ba0ecc2c1f | 48 | { |
Geremia | 4:12ba0ecc2c1f | 49 | #ifdef USE_CS |
Geremia | 4:12ba0ecc2c1f | 50 | _CS = 0; |
Geremia | 4:12ba0ecc2c1f | 51 | #endif |
Geremia | 4:12ba0ecc2c1f | 52 | _DC = 0; // 0=cmd |
Geremia | 4:12ba0ecc2c1f | 53 | _port.write(cmd); // write 8bit |
Geremia | 4:12ba0ecc2c1f | 54 | _WR=0; |
Geremia | 4:12ba0ecc2c1f | 55 | _WR=1; |
Geremia | 4:12ba0ecc2c1f | 56 | #ifdef USE_CS |
Geremia | 4:12ba0ecc2c1f | 57 | _CS = 1; |
Geremia | 4:12ba0ecc2c1f | 58 | #endif |
Geremia | 4:12ba0ecc2c1f | 59 | } |
Geremia | 4:12ba0ecc2c1f | 60 | void PAR16::wr_data8(unsigned char data) |
Geremia | 4:12ba0ecc2c1f | 61 | { |
Geremia | 4:12ba0ecc2c1f | 62 | #ifdef USE_CS |
Geremia | 4:12ba0ecc2c1f | 63 | _CS = 0; |
Geremia | 4:12ba0ecc2c1f | 64 | #endif |
Geremia | 4:12ba0ecc2c1f | 65 | _DC = 1; // 1=data |
Geremia | 4:12ba0ecc2c1f | 66 | _port.write(data); // write 8bit |
Geremia | 4:12ba0ecc2c1f | 67 | _WR=0; |
Geremia | 4:12ba0ecc2c1f | 68 | _WR=1; |
Geremia | 4:12ba0ecc2c1f | 69 | #ifdef USE_CS |
Geremia | 4:12ba0ecc2c1f | 70 | _CS = 1; |
Geremia | 4:12ba0ecc2c1f | 71 | #endif |
Geremia | 4:12ba0ecc2c1f | 72 | } |
Geremia | 4:12ba0ecc2c1f | 73 | void PAR16::wr_cmd16(unsigned short cmd) |
Geremia | 4:12ba0ecc2c1f | 74 | { |
Geremia | 4:12ba0ecc2c1f | 75 | #ifdef USE_CS |
Geremia | 4:12ba0ecc2c1f | 76 | _CS = 0; |
Geremia | 4:12ba0ecc2c1f | 77 | #endif |
Geremia | 4:12ba0ecc2c1f | 78 | _DC = 0; // 0=cmd |
Geremia | 4:12ba0ecc2c1f | 79 | _port.write(cmd>>8); // write 8bit |
Geremia | 4:12ba0ecc2c1f | 80 | _WR=0; |
Geremia | 4:12ba0ecc2c1f | 81 | _WR=1; |
Geremia | 4:12ba0ecc2c1f | 82 | _port.write(cmd&0xFF); // write 8bit |
Geremia | 4:12ba0ecc2c1f | 83 | _WR=0; |
Geremia | 4:12ba0ecc2c1f | 84 | _WR=1; |
Geremia | 4:12ba0ecc2c1f | 85 | #ifdef USE_CS |
Geremia | 4:12ba0ecc2c1f | 86 | _CS = 1; |
Geremia | 4:12ba0ecc2c1f | 87 | #endif |
Geremia | 4:12ba0ecc2c1f | 88 | } |
Geremia | 4:12ba0ecc2c1f | 89 | void PAR16::wr_data16(unsigned short data) |
Geremia | 4:12ba0ecc2c1f | 90 | { |
Geremia | 4:12ba0ecc2c1f | 91 | #ifdef USE_CS |
Geremia | 4:12ba0ecc2c1f | 92 | _CS = 0; |
Geremia | 4:12ba0ecc2c1f | 93 | #endif |
Geremia | 4:12ba0ecc2c1f | 94 | _DC = 1; // 1=data |
Geremia | 4:12ba0ecc2c1f | 95 | _port.write(data>>8); // write 8bit |
Geremia | 4:12ba0ecc2c1f | 96 | _WR=0; |
Geremia | 4:12ba0ecc2c1f | 97 | _WR=1; |
Geremia | 4:12ba0ecc2c1f | 98 | _port.write(data&0xFF); // write 8bit |
Geremia | 4:12ba0ecc2c1f | 99 | _WR=0; |
Geremia | 4:12ba0ecc2c1f | 100 | _WR=1; |
Geremia | 4:12ba0ecc2c1f | 101 | #ifdef USE_CS |
Geremia | 4:12ba0ecc2c1f | 102 | _CS = 1; |
Geremia | 4:12ba0ecc2c1f | 103 | #endif |
Geremia | 4:12ba0ecc2c1f | 104 | } |
Geremia | 4:12ba0ecc2c1f | 105 | void PAR16::wr_gram(unsigned short data) |
Geremia | 4:12ba0ecc2c1f | 106 | { |
Geremia | 4:12ba0ecc2c1f | 107 | #ifdef USE_CS |
Geremia | 4:12ba0ecc2c1f | 108 | _CS = 0; |
Geremia | 4:12ba0ecc2c1f | 109 | #endif |
Geremia | 4:12ba0ecc2c1f | 110 | _DC = 1; // 1=data |
Geremia | 4:12ba0ecc2c1f | 111 | _port.write(data); // write 16bit |
Geremia | 4:12ba0ecc2c1f | 112 | _WR=0; |
Geremia | 4:12ba0ecc2c1f | 113 | _WR=1; |
Geremia | 4:12ba0ecc2c1f | 114 | #ifdef USE_CS |
Geremia | 4:12ba0ecc2c1f | 115 | _CS = 1; |
Geremia | 4:12ba0ecc2c1f | 116 | #endif |
Geremia | 4:12ba0ecc2c1f | 117 | } |
Geremia | 4:12ba0ecc2c1f | 118 | void PAR16::wr_gram(unsigned short data, unsigned int count) |
Geremia | 4:12ba0ecc2c1f | 119 | { |
Geremia | 4:12ba0ecc2c1f | 120 | #ifdef USE_CS |
Geremia | 4:12ba0ecc2c1f | 121 | _CS = 0; |
Geremia | 4:12ba0ecc2c1f | 122 | #endif |
Geremia | 4:12ba0ecc2c1f | 123 | _DC = 1; // 1=data |
Geremia | 4:12ba0ecc2c1f | 124 | while(count) |
Geremia | 4:12ba0ecc2c1f | 125 | { |
Geremia | 4:12ba0ecc2c1f | 126 | _port.write(data); // write 16bit |
Geremia | 4:12ba0ecc2c1f | 127 | _WR=0; |
Geremia | 4:12ba0ecc2c1f | 128 | _WR=1; |
Geremia | 4:12ba0ecc2c1f | 129 | count--; |
Geremia | 4:12ba0ecc2c1f | 130 | } |
Geremia | 4:12ba0ecc2c1f | 131 | #ifdef USE_CS |
Geremia | 4:12ba0ecc2c1f | 132 | _CS = 1; |
Geremia | 4:12ba0ecc2c1f | 133 | #endif |
Geremia | 4:12ba0ecc2c1f | 134 | } |
Geremia | 4:12ba0ecc2c1f | 135 | void PAR16::wr_grambuf(unsigned short* data, unsigned int lenght) |
Geremia | 4:12ba0ecc2c1f | 136 | { |
Geremia | 4:12ba0ecc2c1f | 137 | #ifdef USE_CS |
Geremia | 4:12ba0ecc2c1f | 138 | _CS = 0; |
Geremia | 4:12ba0ecc2c1f | 139 | #endif |
Geremia | 4:12ba0ecc2c1f | 140 | _DC = 1; // 1=data |
Geremia | 4:12ba0ecc2c1f | 141 | while(lenght) |
Geremia | 4:12ba0ecc2c1f | 142 | { |
Geremia | 4:12ba0ecc2c1f | 143 | _port.write(*data); // write 16bit |
Geremia | 4:12ba0ecc2c1f | 144 | _WR=0; |
Geremia | 4:12ba0ecc2c1f | 145 | _WR=1; |
Geremia | 4:12ba0ecc2c1f | 146 | data++; |
Geremia | 4:12ba0ecc2c1f | 147 | lenght--; |
Geremia | 4:12ba0ecc2c1f | 148 | } |
Geremia | 4:12ba0ecc2c1f | 149 | #ifdef USE_CS |
Geremia | 4:12ba0ecc2c1f | 150 | _CS = 1; |
Geremia | 4:12ba0ecc2c1f | 151 | #endif |
Geremia | 4:12ba0ecc2c1f | 152 | } |
Geremia | 11:b842b8e332cb | 153 | unsigned short PAR16::rd_gram(bool convert) |
Geremia | 5:b222a9461d6b | 154 | { |
Geremia | 5:b222a9461d6b | 155 | #ifdef USE_CS |
Geremia | 5:b222a9461d6b | 156 | _CS = 0; |
Geremia | 5:b222a9461d6b | 157 | #endif |
Geremia | 11:b842b8e332cb | 158 | unsigned int r=0; |
Geremia | 7:bb0383b91104 | 159 | _DC = 1; // 1=data |
Geremia | 7:bb0383b91104 | 160 | _port.input(); |
Geremia | 7:bb0383b91104 | 161 | |
Geremia | 7:bb0383b91104 | 162 | _RD = 0; |
Geremia | 7:bb0383b91104 | 163 | _port.read(); //dummy read |
Geremia | 7:bb0383b91104 | 164 | _RD = 1; |
Geremia | 7:bb0383b91104 | 165 | |
Geremia | 7:bb0383b91104 | 166 | _RD = 0; |
Geremia | 7:bb0383b91104 | 167 | // _RD = 0; // add wait |
Geremia | 7:bb0383b91104 | 168 | r |= _port.read(); |
Geremia | 7:bb0383b91104 | 169 | _RD = 1; |
Geremia | 11:b842b8e332cb | 170 | if(convert) |
Geremia | 11:b842b8e332cb | 171 | { |
Geremia | 11:b842b8e332cb | 172 | r <<= 8; |
Geremia | 11:b842b8e332cb | 173 | _RD = 0; |
Geremia | 11:b842b8e332cb | 174 | // _RD = 0; // add wait |
Geremia | 11:b842b8e332cb | 175 | r |= _port.read()>>8; //MSB of port read is blue, LSB is red of next pixel |
Geremia | 11:b842b8e332cb | 176 | _RD = 1; |
Geremia | 11:b842b8e332cb | 177 | // gram is 18bit/pixel, if you set 16bit/pixel (cmd 3A), during writing the 16bits are expanded to 18bit |
Geremia | 11:b842b8e332cb | 178 | // during reading, you read the raw 18bit gram |
Geremia | 11:b842b8e332cb | 179 | r = RGB24to16((r&0xFF0000)>>16, (r&0xFF00)>>8, r&0xFF);// 18bit pixel padded to 24bits, rrrrrr00_gggggg00_bbbbbb00, converted to 16bit |
Geremia | 11:b842b8e332cb | 180 | } |
Geremia | 7:bb0383b91104 | 181 | #ifdef USE_CS |
Geremia | 7:bb0383b91104 | 182 | _CS = 1; |
Geremia | 7:bb0383b91104 | 183 | #endif |
Geremia | 7:bb0383b91104 | 184 | _port.output(); |
Geremia | 11:b842b8e332cb | 185 | return (unsigned short)r; |
Geremia | 7:bb0383b91104 | 186 | } |
Geremia | 7:bb0383b91104 | 187 | unsigned int PAR16::rd_reg_data32(unsigned char reg) |
Geremia | 7:bb0383b91104 | 188 | { |
Geremia | 7:bb0383b91104 | 189 | #ifdef USE_CS |
Geremia | 7:bb0383b91104 | 190 | _CS = 0; |
Geremia | 7:bb0383b91104 | 191 | #endif |
Geremia | 7:bb0383b91104 | 192 | wr_cmd8(reg); |
Geremia | 5:b222a9461d6b | 193 | unsigned int r=0; |
Geremia | 5:b222a9461d6b | 194 | _DC = 1; // 1=data |
Geremia | 5:b222a9461d6b | 195 | _port.input(); |
Geremia | 5:b222a9461d6b | 196 | |
Geremia | 5:b222a9461d6b | 197 | _RD = 0; |
Geremia | 5:b222a9461d6b | 198 | _port.read(); //dummy read |
Geremia | 5:b222a9461d6b | 199 | _RD = 1; |
Geremia | 5:b222a9461d6b | 200 | |
Geremia | 5:b222a9461d6b | 201 | _RD = 0; |
Geremia | 5:b222a9461d6b | 202 | // _RD = 0; // add wait |
Geremia | 5:b222a9461d6b | 203 | r |= (_port.read()&0xFF); |
Geremia | 5:b222a9461d6b | 204 | r <<= 8; |
Geremia | 5:b222a9461d6b | 205 | _RD = 1; |
Geremia | 5:b222a9461d6b | 206 | |
Geremia | 5:b222a9461d6b | 207 | _RD = 0; |
Geremia | 5:b222a9461d6b | 208 | // _RD = 0; // add wait |
Geremia | 5:b222a9461d6b | 209 | r |= (_port.read()&0xFF); |
Geremia | 5:b222a9461d6b | 210 | r <<= 8; |
Geremia | 5:b222a9461d6b | 211 | _RD = 1; |
Geremia | 5:b222a9461d6b | 212 | |
Geremia | 5:b222a9461d6b | 213 | _RD = 0; |
Geremia | 5:b222a9461d6b | 214 | // _RD = 0; // add wait |
Geremia | 5:b222a9461d6b | 215 | r |= (_port.read()&0xFF); |
Geremia | 5:b222a9461d6b | 216 | r <<= 8; |
Geremia | 5:b222a9461d6b | 217 | _RD = 1; |
Geremia | 5:b222a9461d6b | 218 | |
Geremia | 5:b222a9461d6b | 219 | _RD = 0; |
Geremia | 5:b222a9461d6b | 220 | // _RD = 0; // add wait |
Geremia | 5:b222a9461d6b | 221 | r |= (_port.read()&0xFF); |
Geremia | 5:b222a9461d6b | 222 | _RD = 1; |
Geremia | 5:b222a9461d6b | 223 | |
Geremia | 5:b222a9461d6b | 224 | _CS = 1; // force CS HIG to interupt the cmd in case was not supported |
Geremia | 5:b222a9461d6b | 225 | #ifndef USE_CS //if CS is not used, force fixed LOW again |
Geremia | 5:b222a9461d6b | 226 | _CS = 0; |
Geremia | 5:b222a9461d6b | 227 | #endif |
Geremia | 5:b222a9461d6b | 228 | _port.output(); |
Geremia | 5:b222a9461d6b | 229 | return r; |
Geremia | 5:b222a9461d6b | 230 | } |
Geremia | 7:bb0383b91104 | 231 | // in Par mode EXTC regs (0xB0-0xFF) can be directly read |
Geremia | 7:bb0383b91104 | 232 | unsigned int PAR16::rd_extcreg_data32(unsigned char reg, unsigned char SPIreadenablecmd) |
Geremia | 5:b222a9461d6b | 233 | { |
Geremia | 7:bb0383b91104 | 234 | return rd_reg_data32(reg); |
Geremia | 5:b222a9461d6b | 235 | } |
Geremia | 4:12ba0ecc2c1f | 236 | void PAR16::hw_reset() |
Geremia | 4:12ba0ecc2c1f | 237 | { |
Geremia | 4:12ba0ecc2c1f | 238 | wait_ms(15); |
Geremia | 4:12ba0ecc2c1f | 239 | _DC = 1; |
Geremia | 4:12ba0ecc2c1f | 240 | _CS = 1; |
Geremia | 4:12ba0ecc2c1f | 241 | _WR = 1; |
Geremia | 4:12ba0ecc2c1f | 242 | _RD = 1; |
Geremia | 4:12ba0ecc2c1f | 243 | _reset = 0; // display reset |
Geremia | 4:12ba0ecc2c1f | 244 | wait_us(50); |
Geremia | 4:12ba0ecc2c1f | 245 | _reset = 1; // end reset |
Geremia | 4:12ba0ecc2c1f | 246 | wait_ms(15); |
Geremia | 4:12ba0ecc2c1f | 247 | #ifndef USE_CS |
Geremia | 4:12ba0ecc2c1f | 248 | _CS=0; // put CS low now and forever |
Geremia | 4:12ba0ecc2c1f | 249 | #endif |
Geremia | 4:12ba0ecc2c1f | 250 | } |
Geremia | 4:12ba0ecc2c1f | 251 | void PAR16::BusEnable(bool enable) |
Geremia | 4:12ba0ecc2c1f | 252 | { |
Geremia | 4:12ba0ecc2c1f | 253 | _CS = enable ? 0:1; |
Geremia | 4:12ba0ecc2c1f | 254 | } |