.

Committer:
Geremia
Date:
Fri Feb 13 15:25:10 2015 +0000
Revision:
1:ff019d22b275
Child:
4:12ba0ecc2c1f
Added SPI_16, LCD done

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Geremia 1:ff019d22b275 1 #include "SPI16.h"
Geremia 1:ff019d22b275 2 //#define USE_CS
Geremia 1:ff019d22b275 3
Geremia 1:ff019d22b275 4 SPI16::SPI16(int Hz, PinName mosi, PinName miso, PinName sclk, PinName CS, PinName reset, PinName DC)
Geremia 1:ff019d22b275 5 : _CS(CS), _spi(mosi, miso, sclk), _reset(reset), _DC(DC)
Geremia 1:ff019d22b275 6 {
Geremia 1:ff019d22b275 7 _reset = 1;
Geremia 1:ff019d22b275 8 _DC=1;
Geremia 1:ff019d22b275 9 _CS=1;
Geremia 1:ff019d22b275 10 _spi.format(16,0); // 8 bit spi mode 0
Geremia 1:ff019d22b275 11 // _spi.frequency(12000000); // 10 Mhz SPI clock, 12mhz for F411
Geremia 1:ff019d22b275 12 _spi.frequency(Hz);
Geremia 1:ff019d22b275 13 hw_reset();
Geremia 1:ff019d22b275 14 }
Geremia 1:ff019d22b275 15
Geremia 1:ff019d22b275 16 void SPI16::wr_cmd8(unsigned char cmd)
Geremia 1:ff019d22b275 17 {
Geremia 1:ff019d22b275 18 #ifdef USE_CS
Geremia 1:ff019d22b275 19 _CS = 0;
Geremia 1:ff019d22b275 20 #endif
Geremia 1:ff019d22b275 21 _spi.format(8,0); // it takes time, better use wr_cmd16 with NOP cmd
Geremia 1:ff019d22b275 22 _DC.write(0); // 0=cmd
Geremia 1:ff019d22b275 23 _spi.write(cmd); // write 8bit
Geremia 1:ff019d22b275 24 _spi.format(16,0);
Geremia 1:ff019d22b275 25 #ifdef USE_CS
Geremia 1:ff019d22b275 26 _CS = 1;
Geremia 1:ff019d22b275 27 #endif
Geremia 1:ff019d22b275 28 }
Geremia 1:ff019d22b275 29 void SPI16::wr_data8(unsigned char data)
Geremia 1:ff019d22b275 30 {
Geremia 1:ff019d22b275 31 #ifdef USE_CS
Geremia 1:ff019d22b275 32 _CS = 0;
Geremia 1:ff019d22b275 33 #endif
Geremia 1:ff019d22b275 34 _spi.format(8,0); // it takes time, check prev cmd parameter, in case use wr_data16 with repeated byte
Geremia 1:ff019d22b275 35 _DC.write(1); // 1=data
Geremia 1:ff019d22b275 36 _spi.write(data); // write 8bit
Geremia 1:ff019d22b275 37 _spi.format(16,0);
Geremia 1:ff019d22b275 38 #ifdef USE_CS
Geremia 1:ff019d22b275 39 _CS = 1;
Geremia 1:ff019d22b275 40 #endif
Geremia 1:ff019d22b275 41 }
Geremia 1:ff019d22b275 42 void SPI16::wr_data8(unsigned char data, unsigned int count)
Geremia 1:ff019d22b275 43 {
Geremia 1:ff019d22b275 44 #ifdef USE_CS
Geremia 1:ff019d22b275 45 _CS = 0;
Geremia 1:ff019d22b275 46 #endif
Geremia 1:ff019d22b275 47 _spi.format(8,0);
Geremia 1:ff019d22b275 48 _DC.write(1); // 1=data
Geremia 1:ff019d22b275 49 while(count)
Geremia 1:ff019d22b275 50 {
Geremia 1:ff019d22b275 51 _spi.write(data); // write 8bit
Geremia 1:ff019d22b275 52 count--;
Geremia 1:ff019d22b275 53 }
Geremia 1:ff019d22b275 54 _spi.format(16,0);
Geremia 1:ff019d22b275 55 #ifdef USE_CS
Geremia 1:ff019d22b275 56 _CS = 1;
Geremia 1:ff019d22b275 57 #endif
Geremia 1:ff019d22b275 58 }
Geremia 1:ff019d22b275 59 void SPI16::wr_data8buf(unsigned char* data, unsigned int lenght)
Geremia 1:ff019d22b275 60 {
Geremia 1:ff019d22b275 61 #ifdef USE_CS
Geremia 1:ff019d22b275 62 _CS = 0;
Geremia 1:ff019d22b275 63 #endif
Geremia 1:ff019d22b275 64 _spi.format(8,0);
Geremia 1:ff019d22b275 65 _DC.write(1); // 1=data
Geremia 1:ff019d22b275 66 while(lenght)
Geremia 1:ff019d22b275 67 {
Geremia 1:ff019d22b275 68 _spi.write(*data++); // write 8bit
Geremia 1:ff019d22b275 69 // data++;
Geremia 1:ff019d22b275 70 lenght--;
Geremia 1:ff019d22b275 71 }
Geremia 1:ff019d22b275 72 _spi.format(16,0);
Geremia 1:ff019d22b275 73 #ifdef USE_CS
Geremia 1:ff019d22b275 74 _CS = 1;
Geremia 1:ff019d22b275 75 #endif
Geremia 1:ff019d22b275 76 }
Geremia 1:ff019d22b275 77 void SPI16::wr_cmd16(unsigned short cmd)
Geremia 1:ff019d22b275 78 {
Geremia 1:ff019d22b275 79 #ifdef USE_CS
Geremia 1:ff019d22b275 80 _CS = 0;
Geremia 1:ff019d22b275 81 #endif
Geremia 1:ff019d22b275 82 _DC.write(0); // 0=cmd
Geremia 1:ff019d22b275 83 _spi.write(cmd); // write 16bit
Geremia 1:ff019d22b275 84 #ifdef USE_CS
Geremia 1:ff019d22b275 85 _CS = 1;
Geremia 1:ff019d22b275 86 #endif
Geremia 1:ff019d22b275 87 }
Geremia 1:ff019d22b275 88 void SPI16::wr_data16(unsigned short data)
Geremia 1:ff019d22b275 89 {
Geremia 1:ff019d22b275 90 #ifdef USE_CS
Geremia 1:ff019d22b275 91 _CS = 0;
Geremia 1:ff019d22b275 92 #endif
Geremia 1:ff019d22b275 93 _DC.write(1); // 1=data
Geremia 1:ff019d22b275 94 _spi.write(data); // write 16bit
Geremia 1:ff019d22b275 95 #ifdef USE_CS
Geremia 1:ff019d22b275 96 _CS = 1;
Geremia 1:ff019d22b275 97 #endif
Geremia 1:ff019d22b275 98 }
Geremia 1:ff019d22b275 99 void SPI16::wr_data16(unsigned short data, unsigned int count)
Geremia 1:ff019d22b275 100 {
Geremia 1:ff019d22b275 101 #ifdef USE_CS
Geremia 1:ff019d22b275 102 _CS = 0;
Geremia 1:ff019d22b275 103 #endif
Geremia 1:ff019d22b275 104 _DC.write(1); // 1=data
Geremia 1:ff019d22b275 105 while(count)
Geremia 1:ff019d22b275 106 {
Geremia 1:ff019d22b275 107 _spi.write(data);
Geremia 1:ff019d22b275 108 count--;
Geremia 1:ff019d22b275 109 }
Geremia 1:ff019d22b275 110 #ifdef USE_CS
Geremia 1:ff019d22b275 111 _CS = 1;
Geremia 1:ff019d22b275 112 #endif
Geremia 1:ff019d22b275 113 }
Geremia 1:ff019d22b275 114 void SPI16::wr_data16buf(unsigned short* data, unsigned int lenght)
Geremia 1:ff019d22b275 115 {
Geremia 1:ff019d22b275 116 #ifdef USE_CS
Geremia 1:ff019d22b275 117 _CS = 0;
Geremia 1:ff019d22b275 118 #endif
Geremia 1:ff019d22b275 119 _DC.write(1); // 1=data
Geremia 1:ff019d22b275 120 while(lenght)
Geremia 1:ff019d22b275 121 {
Geremia 1:ff019d22b275 122 _spi.write(*data);
Geremia 1:ff019d22b275 123 data++;
Geremia 1:ff019d22b275 124 lenght--;
Geremia 1:ff019d22b275 125 }
Geremia 1:ff019d22b275 126 #ifdef USE_CS
Geremia 1:ff019d22b275 127 _CS = 1;
Geremia 1:ff019d22b275 128 #endif
Geremia 1:ff019d22b275 129 }
Geremia 1:ff019d22b275 130 void SPI16::hw_reset()
Geremia 1:ff019d22b275 131 {
Geremia 1:ff019d22b275 132 wait_ms(15);
Geremia 1:ff019d22b275 133 _DC = 1;
Geremia 1:ff019d22b275 134 // _CS = 1;
Geremia 1:ff019d22b275 135 _CS = 0;
Geremia 1:ff019d22b275 136 _reset = 0; // display reset
Geremia 1:ff019d22b275 137 wait_us(50);
Geremia 1:ff019d22b275 138 _reset = 1; // end reset
Geremia 1:ff019d22b275 139 wait_ms(15);
Geremia 1:ff019d22b275 140 #ifndef USE_CS
Geremia 1:ff019d22b275 141 _CS=0; // put CS low now and forever
Geremia 1:ff019d22b275 142 #endif
Geremia 1:ff019d22b275 143 }
Geremia 1:ff019d22b275 144 void SPI16::BusEnable(bool enable)
Geremia 1:ff019d22b275 145 {
Geremia 1:ff019d22b275 146 _CS = enable ? 0:1;
Geremia 1:ff019d22b275 147 }