test
Fork of UniGraphic by
Protocols/SPI8.cpp@1:ff019d22b275, 2015-02-13 (annotated)
- Committer:
- Geremia
- Date:
- Fri Feb 13 15:25:10 2015 +0000
- Revision:
- 1:ff019d22b275
- Parent:
- 0:75ec1b3cde17
- Child:
- 4:12ba0ecc2c1f
Added SPI_16, LCD done
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Geremia | 0:75ec1b3cde17 | 1 | #include "SPI8.h" |
Geremia | 0:75ec1b3cde17 | 2 | |
Geremia | 0:75ec1b3cde17 | 3 | //#define USE_CS |
Geremia | 0:75ec1b3cde17 | 4 | |
Geremia | 1:ff019d22b275 | 5 | SPI8::SPI8(int Hz, PinName mosi, PinName miso, PinName sclk, PinName CS, PinName reset, PinName DC) |
Geremia | 0:75ec1b3cde17 | 6 | : _CS(CS), _spi(mosi, miso, sclk), _reset(reset), _DC(DC) |
Geremia | 0:75ec1b3cde17 | 7 | { |
Geremia | 0:75ec1b3cde17 | 8 | _reset = 1; |
Geremia | 0:75ec1b3cde17 | 9 | _DC=1; |
Geremia | 0:75ec1b3cde17 | 10 | _CS=1; |
Geremia | 0:75ec1b3cde17 | 11 | _spi.format(8,0); // 8 bit spi mode 0 |
Geremia | 0:75ec1b3cde17 | 12 | // _spi.frequency(12000000); // 10 Mhz SPI clock, 12mhz for F411 |
Geremia | 1:ff019d22b275 | 13 | _spi.frequency(Hz); |
Geremia | 0:75ec1b3cde17 | 14 | hw_reset(); |
Geremia | 0:75ec1b3cde17 | 15 | } |
Geremia | 0:75ec1b3cde17 | 16 | |
Geremia | 1:ff019d22b275 | 17 | void SPI8::wr_cmd8(unsigned char cmd) |
Geremia | 0:75ec1b3cde17 | 18 | { |
Geremia | 0:75ec1b3cde17 | 19 | #ifdef USE_CS |
Geremia | 0:75ec1b3cde17 | 20 | _CS = 0; |
Geremia | 0:75ec1b3cde17 | 21 | #endif |
Geremia | 0:75ec1b3cde17 | 22 | _DC.write(0); // 0=cmd |
Geremia | 0:75ec1b3cde17 | 23 | _spi.write(cmd); // write 8bit |
Geremia | 0:75ec1b3cde17 | 24 | #ifdef USE_CS |
Geremia | 0:75ec1b3cde17 | 25 | _CS = 1; |
Geremia | 0:75ec1b3cde17 | 26 | #endif |
Geremia | 0:75ec1b3cde17 | 27 | } |
Geremia | 1:ff019d22b275 | 28 | void SPI8::wr_data8(unsigned char data) |
Geremia | 0:75ec1b3cde17 | 29 | { |
Geremia | 0:75ec1b3cde17 | 30 | #ifdef USE_CS |
Geremia | 0:75ec1b3cde17 | 31 | _CS = 0; |
Geremia | 0:75ec1b3cde17 | 32 | #endif |
Geremia | 0:75ec1b3cde17 | 33 | _DC.write(1); // 1=data |
Geremia | 1:ff019d22b275 | 34 | _spi.write(data); // write 8bit |
Geremia | 0:75ec1b3cde17 | 35 | #ifdef USE_CS |
Geremia | 0:75ec1b3cde17 | 36 | _CS = 1; |
Geremia | 0:75ec1b3cde17 | 37 | #endif |
Geremia | 0:75ec1b3cde17 | 38 | } |
Geremia | 1:ff019d22b275 | 39 | void SPI8::wr_data8(unsigned char data, unsigned int count) |
Geremia | 0:75ec1b3cde17 | 40 | { |
Geremia | 0:75ec1b3cde17 | 41 | #ifdef USE_CS |
Geremia | 0:75ec1b3cde17 | 42 | _CS = 0; |
Geremia | 0:75ec1b3cde17 | 43 | #endif |
Geremia | 0:75ec1b3cde17 | 44 | _DC.write(1); // 1=data |
Geremia | 0:75ec1b3cde17 | 45 | while(count) |
Geremia | 0:75ec1b3cde17 | 46 | { |
Geremia | 1:ff019d22b275 | 47 | _spi.write(data); // write 8bit |
Geremia | 0:75ec1b3cde17 | 48 | count--; |
Geremia | 0:75ec1b3cde17 | 49 | } |
Geremia | 0:75ec1b3cde17 | 50 | #ifdef USE_CS |
Geremia | 0:75ec1b3cde17 | 51 | _CS = 1; |
Geremia | 0:75ec1b3cde17 | 52 | #endif |
Geremia | 0:75ec1b3cde17 | 53 | } |
Geremia | 1:ff019d22b275 | 54 | void SPI8::wr_data8buf(unsigned char* data, unsigned int lenght) |
Geremia | 0:75ec1b3cde17 | 55 | { |
Geremia | 0:75ec1b3cde17 | 56 | #ifdef USE_CS |
Geremia | 0:75ec1b3cde17 | 57 | _CS = 0; |
Geremia | 0:75ec1b3cde17 | 58 | #endif |
Geremia | 0:75ec1b3cde17 | 59 | _DC.write(1); // 1=data |
Geremia | 0:75ec1b3cde17 | 60 | while(lenght) |
Geremia | 0:75ec1b3cde17 | 61 | { |
Geremia | 1:ff019d22b275 | 62 | _spi.write(*data++); // write 8bit |
Geremia | 1:ff019d22b275 | 63 | // data++; |
Geremia | 1:ff019d22b275 | 64 | lenght--; |
Geremia | 1:ff019d22b275 | 65 | } |
Geremia | 1:ff019d22b275 | 66 | #ifdef USE_CS |
Geremia | 1:ff019d22b275 | 67 | _CS = 1; |
Geremia | 1:ff019d22b275 | 68 | #endif |
Geremia | 1:ff019d22b275 | 69 | } |
Geremia | 1:ff019d22b275 | 70 | void SPI8::wr_cmd16(unsigned short cmd) |
Geremia | 1:ff019d22b275 | 71 | { |
Geremia | 1:ff019d22b275 | 72 | #ifdef USE_CS |
Geremia | 1:ff019d22b275 | 73 | _CS = 0; |
Geremia | 1:ff019d22b275 | 74 | #endif |
Geremia | 1:ff019d22b275 | 75 | _DC.write(0); // 0=cmd |
Geremia | 1:ff019d22b275 | 76 | _spi.write(cmd>>8); // write 8bit |
Geremia | 1:ff019d22b275 | 77 | _spi.write(cmd&0xFF); // write 8bit |
Geremia | 1:ff019d22b275 | 78 | #ifdef USE_CS |
Geremia | 1:ff019d22b275 | 79 | _CS = 1; |
Geremia | 1:ff019d22b275 | 80 | #endif |
Geremia | 1:ff019d22b275 | 81 | } |
Geremia | 1:ff019d22b275 | 82 | void SPI8::wr_data16(unsigned short data) |
Geremia | 1:ff019d22b275 | 83 | { |
Geremia | 1:ff019d22b275 | 84 | #ifdef USE_CS |
Geremia | 1:ff019d22b275 | 85 | _CS = 0; |
Geremia | 1:ff019d22b275 | 86 | #endif |
Geremia | 1:ff019d22b275 | 87 | _DC.write(1); // 1=data |
Geremia | 1:ff019d22b275 | 88 | _spi.write(data>>8); // write 8bit |
Geremia | 1:ff019d22b275 | 89 | _spi.write(data&0xFF); // write 8bit |
Geremia | 1:ff019d22b275 | 90 | #ifdef USE_CS |
Geremia | 1:ff019d22b275 | 91 | _CS = 1; |
Geremia | 1:ff019d22b275 | 92 | #endif |
Geremia | 1:ff019d22b275 | 93 | } |
Geremia | 1:ff019d22b275 | 94 | void SPI8::wr_data16(unsigned short data, unsigned int count) |
Geremia | 1:ff019d22b275 | 95 | { |
Geremia | 1:ff019d22b275 | 96 | #ifdef USE_CS |
Geremia | 1:ff019d22b275 | 97 | _CS = 0; |
Geremia | 1:ff019d22b275 | 98 | #endif |
Geremia | 1:ff019d22b275 | 99 | _DC.write(1); // 1=data |
Geremia | 1:ff019d22b275 | 100 | if((data>>8)==(data&0xFF)) |
Geremia | 1:ff019d22b275 | 101 | { |
Geremia | 1:ff019d22b275 | 102 | count<<=1; |
Geremia | 1:ff019d22b275 | 103 | while(count) |
Geremia | 1:ff019d22b275 | 104 | { |
Geremia | 1:ff019d22b275 | 105 | _spi.write(data); // write 8bit |
Geremia | 1:ff019d22b275 | 106 | count--; |
Geremia | 1:ff019d22b275 | 107 | } |
Geremia | 1:ff019d22b275 | 108 | } |
Geremia | 1:ff019d22b275 | 109 | else |
Geremia | 1:ff019d22b275 | 110 | { |
Geremia | 1:ff019d22b275 | 111 | while(count) |
Geremia | 1:ff019d22b275 | 112 | { |
Geremia | 1:ff019d22b275 | 113 | _spi.write(data>>8); // write 8bit |
Geremia | 1:ff019d22b275 | 114 | _spi.write(data&0xFF); // write 8bit |
Geremia | 1:ff019d22b275 | 115 | count--; |
Geremia | 1:ff019d22b275 | 116 | } |
Geremia | 1:ff019d22b275 | 117 | } |
Geremia | 1:ff019d22b275 | 118 | #ifdef USE_CS |
Geremia | 1:ff019d22b275 | 119 | _CS = 1; |
Geremia | 1:ff019d22b275 | 120 | #endif |
Geremia | 1:ff019d22b275 | 121 | } |
Geremia | 1:ff019d22b275 | 122 | void SPI8::wr_data16buf(unsigned short* data, unsigned int lenght) |
Geremia | 1:ff019d22b275 | 123 | { |
Geremia | 1:ff019d22b275 | 124 | #ifdef USE_CS |
Geremia | 1:ff019d22b275 | 125 | _CS = 0; |
Geremia | 1:ff019d22b275 | 126 | #endif |
Geremia | 1:ff019d22b275 | 127 | _DC.write(1); // 1=data |
Geremia | 1:ff019d22b275 | 128 | while(lenght) |
Geremia | 1:ff019d22b275 | 129 | { |
Geremia | 1:ff019d22b275 | 130 | _spi.write((*data)>>8); // write 8bit |
Geremia | 1:ff019d22b275 | 131 | _spi.write((*data)&0xFF); // write 8bit |
Geremia | 1:ff019d22b275 | 132 | data++; |
Geremia | 0:75ec1b3cde17 | 133 | lenght--; |
Geremia | 0:75ec1b3cde17 | 134 | } |
Geremia | 0:75ec1b3cde17 | 135 | #ifdef USE_CS |
Geremia | 0:75ec1b3cde17 | 136 | _CS = 1; |
Geremia | 0:75ec1b3cde17 | 137 | #endif |
Geremia | 0:75ec1b3cde17 | 138 | } |
Geremia | 0:75ec1b3cde17 | 139 | void SPI8::hw_reset() |
Geremia | 0:75ec1b3cde17 | 140 | { |
Geremia | 0:75ec1b3cde17 | 141 | wait_ms(15); |
Geremia | 0:75ec1b3cde17 | 142 | _DC = 1; |
Geremia | 0:75ec1b3cde17 | 143 | // _CS = 1; |
Geremia | 0:75ec1b3cde17 | 144 | _CS = 0; |
Geremia | 0:75ec1b3cde17 | 145 | _reset = 0; // display reset |
Geremia | 0:75ec1b3cde17 | 146 | wait_us(50); |
Geremia | 0:75ec1b3cde17 | 147 | _reset = 1; // end reset |
Geremia | 0:75ec1b3cde17 | 148 | wait_ms(15); |
Geremia | 0:75ec1b3cde17 | 149 | #ifndef USE_CS |
Geremia | 0:75ec1b3cde17 | 150 | _CS=0; // put CS low now and forever |
Geremia | 0:75ec1b3cde17 | 151 | #endif |
Geremia | 0:75ec1b3cde17 | 152 | } |
Geremia | 0:75ec1b3cde17 | 153 | void SPI8::BusEnable(bool enable) |
Geremia | 0:75ec1b3cde17 | 154 | { |
Geremia | 0:75ec1b3cde17 | 155 | _CS = enable ? 0:1; |
Geremia | 0:75ec1b3cde17 | 156 | } |