.
Protocols/BUS16.cpp@21:ae0a4eedfc90, 2015-03-31 (annotated)
- Committer:
- Geremia
- Date:
- Tue Mar 31 21:14:48 2015 +0000
- Revision:
- 21:ae0a4eedfc90
Add BUS_8 and BUS_16 (slow as expected)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Geremia | 21:ae0a4eedfc90 | 1 | /* mbed UniGraphic library - BUS16 protocol class |
Geremia | 21:ae0a4eedfc90 | 2 | * Copyright (c) 2015 Giuliano Dianda |
Geremia | 21:ae0a4eedfc90 | 3 | * Released under the MIT License: http://mbed.org/license/mit |
Geremia | 21:ae0a4eedfc90 | 4 | * |
Geremia | 21:ae0a4eedfc90 | 5 | * Derived work of: |
Geremia | 21:ae0a4eedfc90 | 6 | * |
Geremia | 21:ae0a4eedfc90 | 7 | * mbed library for 240*320 pixel display TFT based on ILI9341 LCD Controller |
Geremia | 21:ae0a4eedfc90 | 8 | * Copyright (c) 2013 Peter Drescher - DC2PD |
Geremia | 21:ae0a4eedfc90 | 9 | * |
Geremia | 21:ae0a4eedfc90 | 10 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
Geremia | 21:ae0a4eedfc90 | 11 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
Geremia | 21:ae0a4eedfc90 | 12 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
Geremia | 21:ae0a4eedfc90 | 13 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
Geremia | 21:ae0a4eedfc90 | 14 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
Geremia | 21:ae0a4eedfc90 | 15 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
Geremia | 21:ae0a4eedfc90 | 16 | * THE SOFTWARE. |
Geremia | 21:ae0a4eedfc90 | 17 | */ |
Geremia | 21:ae0a4eedfc90 | 18 | #include "BUS16.h" |
Geremia | 21:ae0a4eedfc90 | 19 | |
Geremia | 21:ae0a4eedfc90 | 20 | BUS16::BUS16(PinName* buspins, PinName CS, PinName reset, PinName DC, PinName WR, PinName RD) |
Geremia | 21:ae0a4eedfc90 | 21 | : _bus(buspins), _CS(CS), _reset(reset), _DC(DC), _WR(WR), _RD(RD) |
Geremia | 21:ae0a4eedfc90 | 22 | { |
Geremia | 21:ae0a4eedfc90 | 23 | _reset = 1; |
Geremia | 21:ae0a4eedfc90 | 24 | _DC=1; |
Geremia | 21:ae0a4eedfc90 | 25 | _WR=1; |
Geremia | 21:ae0a4eedfc90 | 26 | _RD=1; |
Geremia | 21:ae0a4eedfc90 | 27 | _CS=1; |
Geremia | 21:ae0a4eedfc90 | 28 | _bus.mode(PullNone); |
Geremia | 21:ae0a4eedfc90 | 29 | _bus.output(); // will re-enable our GPIO port |
Geremia | 21:ae0a4eedfc90 | 30 | hw_reset(); |
Geremia | 21:ae0a4eedfc90 | 31 | } |
Geremia | 21:ae0a4eedfc90 | 32 | |
Geremia | 21:ae0a4eedfc90 | 33 | void BUS16::wr_cmd8(unsigned char cmd) |
Geremia | 21:ae0a4eedfc90 | 34 | { |
Geremia | 21:ae0a4eedfc90 | 35 | _DC = 0; // 0=cmd |
Geremia | 21:ae0a4eedfc90 | 36 | _bus.write(cmd); // write 8bit |
Geremia | 21:ae0a4eedfc90 | 37 | _WR=0; |
Geremia | 21:ae0a4eedfc90 | 38 | _WR=1; |
Geremia | 21:ae0a4eedfc90 | 39 | _DC = 1; // 1=data next |
Geremia | 21:ae0a4eedfc90 | 40 | } |
Geremia | 21:ae0a4eedfc90 | 41 | void BUS16::wr_data8(unsigned char data) |
Geremia | 21:ae0a4eedfc90 | 42 | { |
Geremia | 21:ae0a4eedfc90 | 43 | _bus.write(data); // write 8bit |
Geremia | 21:ae0a4eedfc90 | 44 | _WR=0; |
Geremia | 21:ae0a4eedfc90 | 45 | _WR=1; |
Geremia | 21:ae0a4eedfc90 | 46 | } |
Geremia | 21:ae0a4eedfc90 | 47 | void BUS16::wr_cmd16(unsigned short cmd) |
Geremia | 21:ae0a4eedfc90 | 48 | { |
Geremia | 21:ae0a4eedfc90 | 49 | _DC = 0; // 0=cmd |
Geremia | 21:ae0a4eedfc90 | 50 | _bus.write(cmd>>8); // write 8bit |
Geremia | 21:ae0a4eedfc90 | 51 | _WR=0; |
Geremia | 21:ae0a4eedfc90 | 52 | _WR=1; |
Geremia | 21:ae0a4eedfc90 | 53 | _bus.write(cmd&0xFF); // write 8bit |
Geremia | 21:ae0a4eedfc90 | 54 | _WR=0; |
Geremia | 21:ae0a4eedfc90 | 55 | _WR=1; |
Geremia | 21:ae0a4eedfc90 | 56 | _DC = 1; // 1=data next |
Geremia | 21:ae0a4eedfc90 | 57 | } |
Geremia | 21:ae0a4eedfc90 | 58 | void BUS16::wr_data16(unsigned short data) |
Geremia | 21:ae0a4eedfc90 | 59 | { |
Geremia | 21:ae0a4eedfc90 | 60 | _bus.write(data>>8); // write 8bit |
Geremia | 21:ae0a4eedfc90 | 61 | _WR=0; |
Geremia | 21:ae0a4eedfc90 | 62 | _WR=1; |
Geremia | 21:ae0a4eedfc90 | 63 | _bus.write(data&0xFF); // write 8bit |
Geremia | 21:ae0a4eedfc90 | 64 | _WR=0; |
Geremia | 21:ae0a4eedfc90 | 65 | _WR=1; |
Geremia | 21:ae0a4eedfc90 | 66 | } |
Geremia | 21:ae0a4eedfc90 | 67 | void BUS16::wr_gram(unsigned short data) |
Geremia | 21:ae0a4eedfc90 | 68 | { |
Geremia | 21:ae0a4eedfc90 | 69 | _bus.write(data); // write 16bit |
Geremia | 21:ae0a4eedfc90 | 70 | _WR=0; |
Geremia | 21:ae0a4eedfc90 | 71 | _WR=1; |
Geremia | 21:ae0a4eedfc90 | 72 | } |
Geremia | 21:ae0a4eedfc90 | 73 | void BUS16::wr_gram(unsigned short data, unsigned int count) |
Geremia | 21:ae0a4eedfc90 | 74 | { |
Geremia | 21:ae0a4eedfc90 | 75 | while(count) |
Geremia | 21:ae0a4eedfc90 | 76 | { |
Geremia | 21:ae0a4eedfc90 | 77 | _bus.write(data); // rewrite even if same data, otherwise too much fast |
Geremia | 21:ae0a4eedfc90 | 78 | _WR=0; |
Geremia | 21:ae0a4eedfc90 | 79 | _WR=1; |
Geremia | 21:ae0a4eedfc90 | 80 | count--; |
Geremia | 21:ae0a4eedfc90 | 81 | } |
Geremia | 21:ae0a4eedfc90 | 82 | } |
Geremia | 21:ae0a4eedfc90 | 83 | void BUS16::wr_grambuf(unsigned short* data, unsigned int lenght) |
Geremia | 21:ae0a4eedfc90 | 84 | { |
Geremia | 21:ae0a4eedfc90 | 85 | while(lenght) |
Geremia | 21:ae0a4eedfc90 | 86 | { |
Geremia | 21:ae0a4eedfc90 | 87 | _bus.write(*data); // write 16bit |
Geremia | 21:ae0a4eedfc90 | 88 | _WR=0; |
Geremia | 21:ae0a4eedfc90 | 89 | _WR=1; |
Geremia | 21:ae0a4eedfc90 | 90 | data++; |
Geremia | 21:ae0a4eedfc90 | 91 | lenght--; |
Geremia | 21:ae0a4eedfc90 | 92 | } |
Geremia | 21:ae0a4eedfc90 | 93 | } |
Geremia | 21:ae0a4eedfc90 | 94 | unsigned short BUS16::rd_gram(bool convert) |
Geremia | 21:ae0a4eedfc90 | 95 | { |
Geremia | 21:ae0a4eedfc90 | 96 | unsigned int r=0; |
Geremia | 21:ae0a4eedfc90 | 97 | _bus.input(); |
Geremia | 21:ae0a4eedfc90 | 98 | |
Geremia | 21:ae0a4eedfc90 | 99 | _RD = 0; |
Geremia | 21:ae0a4eedfc90 | 100 | _bus.read(); //dummy read |
Geremia | 21:ae0a4eedfc90 | 101 | _RD = 1; |
Geremia | 21:ae0a4eedfc90 | 102 | |
Geremia | 21:ae0a4eedfc90 | 103 | _RD = 0; |
Geremia | 21:ae0a4eedfc90 | 104 | // _RD = 0; // add wait |
Geremia | 21:ae0a4eedfc90 | 105 | r |= _bus.read(); |
Geremia | 21:ae0a4eedfc90 | 106 | _RD = 1; |
Geremia | 21:ae0a4eedfc90 | 107 | if(convert) |
Geremia | 21:ae0a4eedfc90 | 108 | { |
Geremia | 21:ae0a4eedfc90 | 109 | r <<= 8; |
Geremia | 21:ae0a4eedfc90 | 110 | _RD = 0; |
Geremia | 21:ae0a4eedfc90 | 111 | // _RD = 0; // add wait |
Geremia | 21:ae0a4eedfc90 | 112 | r |= _bus.read()>>8; //MSB of port read is blue, LSB is red of next pixel |
Geremia | 21:ae0a4eedfc90 | 113 | _RD = 1; |
Geremia | 21:ae0a4eedfc90 | 114 | // gram is 18bit/pixel, if you set 16bit/pixel (cmd 3A), during writing the 16bits are expanded to 18bit |
Geremia | 21:ae0a4eedfc90 | 115 | // during reading, you read the raw 18bit gram |
Geremia | 21:ae0a4eedfc90 | 116 | r = RGB24to16((r&0xFF0000)>>16, (r&0xFF00)>>8, r&0xFF);// 18bit pixel padded to 24bits, rrrrrr00_gggggg00_bbbbbb00, converted to 16bit |
Geremia | 21:ae0a4eedfc90 | 117 | } |
Geremia | 21:ae0a4eedfc90 | 118 | _bus.output(); |
Geremia | 21:ae0a4eedfc90 | 119 | return (unsigned short)r; |
Geremia | 21:ae0a4eedfc90 | 120 | } |
Geremia | 21:ae0a4eedfc90 | 121 | unsigned int BUS16::rd_reg_data32(unsigned char reg) |
Geremia | 21:ae0a4eedfc90 | 122 | { |
Geremia | 21:ae0a4eedfc90 | 123 | wr_cmd8(reg); |
Geremia | 21:ae0a4eedfc90 | 124 | unsigned int r=0; |
Geremia | 21:ae0a4eedfc90 | 125 | // _DC = 1; // 1=data |
Geremia | 21:ae0a4eedfc90 | 126 | _bus.input(); |
Geremia | 21:ae0a4eedfc90 | 127 | |
Geremia | 21:ae0a4eedfc90 | 128 | _RD = 0; |
Geremia | 21:ae0a4eedfc90 | 129 | _bus.read(); //dummy read |
Geremia | 21:ae0a4eedfc90 | 130 | _RD = 1; |
Geremia | 21:ae0a4eedfc90 | 131 | |
Geremia | 21:ae0a4eedfc90 | 132 | _RD = 0; |
Geremia | 21:ae0a4eedfc90 | 133 | // _RD = 0; // add wait |
Geremia | 21:ae0a4eedfc90 | 134 | r |= (_bus.read()&0xFF); |
Geremia | 21:ae0a4eedfc90 | 135 | r <<= 8; |
Geremia | 21:ae0a4eedfc90 | 136 | _RD = 1; |
Geremia | 21:ae0a4eedfc90 | 137 | |
Geremia | 21:ae0a4eedfc90 | 138 | _RD = 0; |
Geremia | 21:ae0a4eedfc90 | 139 | // _RD = 0; // add wait |
Geremia | 21:ae0a4eedfc90 | 140 | r |= (_bus.read()&0xFF); |
Geremia | 21:ae0a4eedfc90 | 141 | r <<= 8; |
Geremia | 21:ae0a4eedfc90 | 142 | _RD = 1; |
Geremia | 21:ae0a4eedfc90 | 143 | |
Geremia | 21:ae0a4eedfc90 | 144 | _RD = 0; |
Geremia | 21:ae0a4eedfc90 | 145 | // _RD = 0; // add wait |
Geremia | 21:ae0a4eedfc90 | 146 | r |= (_bus.read()&0xFF); |
Geremia | 21:ae0a4eedfc90 | 147 | r <<= 8; |
Geremia | 21:ae0a4eedfc90 | 148 | _RD = 1; |
Geremia | 21:ae0a4eedfc90 | 149 | |
Geremia | 21:ae0a4eedfc90 | 150 | _RD = 0; |
Geremia | 21:ae0a4eedfc90 | 151 | // _RD = 0; // add wait |
Geremia | 21:ae0a4eedfc90 | 152 | r |= (_bus.read()&0xFF); |
Geremia | 21:ae0a4eedfc90 | 153 | _RD = 1; |
Geremia | 21:ae0a4eedfc90 | 154 | |
Geremia | 21:ae0a4eedfc90 | 155 | _CS = 1; // toggle CS to interupt the cmd in case was not supported |
Geremia | 21:ae0a4eedfc90 | 156 | _CS = 0; |
Geremia | 21:ae0a4eedfc90 | 157 | |
Geremia | 21:ae0a4eedfc90 | 158 | _bus.output(); |
Geremia | 21:ae0a4eedfc90 | 159 | return r; |
Geremia | 21:ae0a4eedfc90 | 160 | } |
Geremia | 21:ae0a4eedfc90 | 161 | // in Par mode EXTC regs (0xB0-0xFF) can be directly read |
Geremia | 21:ae0a4eedfc90 | 162 | unsigned int BUS16::rd_extcreg_data32(unsigned char reg, unsigned char SPIreadenablecmd) |
Geremia | 21:ae0a4eedfc90 | 163 | { |
Geremia | 21:ae0a4eedfc90 | 164 | return rd_reg_data32(reg); |
Geremia | 21:ae0a4eedfc90 | 165 | } |
Geremia | 21:ae0a4eedfc90 | 166 | // ILI932x specific |
Geremia | 21:ae0a4eedfc90 | 167 | void BUS16::dummyread() |
Geremia | 21:ae0a4eedfc90 | 168 | { |
Geremia | 21:ae0a4eedfc90 | 169 | _bus.input(); |
Geremia | 21:ae0a4eedfc90 | 170 | _RD = 0; |
Geremia | 21:ae0a4eedfc90 | 171 | _bus.read(); // dummy read |
Geremia | 21:ae0a4eedfc90 | 172 | _RD=1; |
Geremia | 21:ae0a4eedfc90 | 173 | // _bus.output(); |
Geremia | 21:ae0a4eedfc90 | 174 | } |
Geremia | 21:ae0a4eedfc90 | 175 | // ILI932x specific |
Geremia | 21:ae0a4eedfc90 | 176 | void BUS16::reg_select(unsigned char reg, bool forread) |
Geremia | 21:ae0a4eedfc90 | 177 | { |
Geremia | 21:ae0a4eedfc90 | 178 | _DC = 0; |
Geremia | 21:ae0a4eedfc90 | 179 | _bus.write(reg); // write 16bit |
Geremia | 21:ae0a4eedfc90 | 180 | _WR=0; |
Geremia | 21:ae0a4eedfc90 | 181 | _WR=1; |
Geremia | 21:ae0a4eedfc90 | 182 | _DC = 1; // 1=data next |
Geremia | 21:ae0a4eedfc90 | 183 | } |
Geremia | 21:ae0a4eedfc90 | 184 | // ILI932x specific |
Geremia | 21:ae0a4eedfc90 | 185 | void BUS16::reg_write(unsigned char reg, unsigned short data) |
Geremia | 21:ae0a4eedfc90 | 186 | { |
Geremia | 21:ae0a4eedfc90 | 187 | _DC = 0; |
Geremia | 21:ae0a4eedfc90 | 188 | _bus.write(reg); // write 16bit |
Geremia | 21:ae0a4eedfc90 | 189 | _WR=0; |
Geremia | 21:ae0a4eedfc90 | 190 | _WR=1; |
Geremia | 21:ae0a4eedfc90 | 191 | _DC = 1; |
Geremia | 21:ae0a4eedfc90 | 192 | _bus.write(data); // write 16bit |
Geremia | 21:ae0a4eedfc90 | 193 | _WR=0; |
Geremia | 21:ae0a4eedfc90 | 194 | _WR=1; |
Geremia | 21:ae0a4eedfc90 | 195 | } |
Geremia | 21:ae0a4eedfc90 | 196 | // ILI932x specific |
Geremia | 21:ae0a4eedfc90 | 197 | unsigned short BUS16::reg_read(unsigned char reg) |
Geremia | 21:ae0a4eedfc90 | 198 | { |
Geremia | 21:ae0a4eedfc90 | 199 | unsigned short r=0; |
Geremia | 21:ae0a4eedfc90 | 200 | _DC = 0; |
Geremia | 21:ae0a4eedfc90 | 201 | _bus.write(reg); // write 16bit |
Geremia | 21:ae0a4eedfc90 | 202 | _WR=0; |
Geremia | 21:ae0a4eedfc90 | 203 | _WR=1; |
Geremia | 21:ae0a4eedfc90 | 204 | _DC = 1; |
Geremia | 21:ae0a4eedfc90 | 205 | _bus.input(); |
Geremia | 21:ae0a4eedfc90 | 206 | _RD=0; |
Geremia | 21:ae0a4eedfc90 | 207 | r |= _bus.read(); // read 16bit |
Geremia | 21:ae0a4eedfc90 | 208 | _RD=1; |
Geremia | 21:ae0a4eedfc90 | 209 | _bus.output(); |
Geremia | 21:ae0a4eedfc90 | 210 | return r; |
Geremia | 21:ae0a4eedfc90 | 211 | } |
Geremia | 21:ae0a4eedfc90 | 212 | void BUS16::hw_reset() |
Geremia | 21:ae0a4eedfc90 | 213 | { |
Geremia | 21:ae0a4eedfc90 | 214 | wait_ms(15); |
Geremia | 21:ae0a4eedfc90 | 215 | _DC = 1; |
Geremia | 21:ae0a4eedfc90 | 216 | _CS = 1; |
Geremia | 21:ae0a4eedfc90 | 217 | _WR = 1; |
Geremia | 21:ae0a4eedfc90 | 218 | _RD = 1; |
Geremia | 21:ae0a4eedfc90 | 219 | _reset = 0; // display reset |
Geremia | 21:ae0a4eedfc90 | 220 | wait_ms(2); |
Geremia | 21:ae0a4eedfc90 | 221 | _reset = 1; // end reset |
Geremia | 21:ae0a4eedfc90 | 222 | wait_ms(100); |
Geremia | 21:ae0a4eedfc90 | 223 | } |
Geremia | 21:ae0a4eedfc90 | 224 | void BUS16::BusEnable(bool enable) |
Geremia | 21:ae0a4eedfc90 | 225 | { |
Geremia | 21:ae0a4eedfc90 | 226 | _CS = enable ? 0:1; |
Geremia | 21:ae0a4eedfc90 | 227 | } |