Zoltan Hudak / Mbed OS LCD_McuFriend_FSMC_STM32F407
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers FSMC8.cpp Source File

FSMC8.cpp

00001 #include "FSMC8.h"
00002 #include "stdint.h"
00003 
00004 // Addres bit A18 is used to select LCD's REGISTER or RAM: A18 = 0b00000000000001000000000000000000 = 0x00040000
00005 volatile unsigned char* lcd_reg = (unsigned char*)0x60000000;   // FSMC BANK1, A18 = 0 -> write to LCD's REGISTER
00006 volatile unsigned char* lcd_ram = (unsigned char*)0x60040000;   // FSMC BANK1, A18 = 1 -> read/write to LCD's RAM
00007 
00008 /**
00009  * @brief
00010  * @note
00011  * @param
00012  * @retval
00013  */
00014 FSMC8::FSMC8(PinName reset) :
00015     _reset(reset)
00016 {
00017     _reset = 1;
00018 }
00019 
00020 
00021 /**
00022  * @brief
00023  * @note
00024  * @param
00025  * @retval
00026  */
00027 void FSMC8::wr_cmd8(unsigned char cmd)
00028 {
00029     lcd_reg[0] = cmd;   // Write to LCD's register
00030 }
00031 
00032 /**
00033  * @brief
00034  * @note
00035  * @param
00036  * @retval
00037  */
00038 void FSMC8::wr_data8(unsigned char data)
00039 {
00040     lcd_ram[0] = data;  // Write to LCD's RAM
00041 }
00042 
00043 /**
00044  * @brief
00045  * @note
00046  * @param
00047  * @retval
00048  */
00049 void FSMC8::wr_cmd16(unsigned short cmd)
00050 {
00051     wr_cmd8(cmd >> 8);      // Write MSB to LCD's register
00052     wr_cmd8(cmd & 0xFF);    // Write LSB to LCD's register
00053 }
00054 
00055 /**
00056  * @brief
00057  * @note
00058  * @param
00059  * @retval
00060  */
00061 void FSMC8::wr_data16(unsigned short data)
00062 {
00063     wr_data8(data >> 8);    // Write MSB to LCD's RAM
00064     wr_data8(data & 0xFF);  // Write LSB to LCD's RAM
00065 
00066 }
00067 
00068 /**
00069  * @brief
00070  * @note
00071  * @param
00072  * @retval
00073  */
00074 void FSMC8::wr_gram(unsigned short data)
00075 {
00076     wr_data16(data);
00077 }
00078 
00079 /**
00080  * @brief
00081  * @note
00082  * @param
00083  * @retval
00084  */
00085 void FSMC8::wr_gram(unsigned short data, unsigned int count)
00086 {
00087     if ((data >> 8) == (data & 0xFF)) {
00088         count <<= 1;
00089         while (count) {
00090             wr_data8(data);
00091             count--;
00092         }
00093     }
00094     else {
00095         while (count) {
00096             wr_data16(data);
00097             count--;
00098         }
00099     }
00100 }
00101 
00102 /**
00103  * @brief
00104  * @note
00105  * @param
00106  * @retval
00107  */
00108 void FSMC8::wr_grambuf(unsigned short* data, unsigned int lenght)
00109 {
00110     while (lenght) {
00111         wr_data16(*data++);
00112         lenght--;
00113     }
00114 }
00115 
00116 /**
00117  * @brief
00118  * @note
00119  * @param
00120  * @retval
00121  */
00122 unsigned char FSMC8::rd_data8 (unsigned int i /* = 0 */)
00123 {
00124     return lcd_ram[i];  // Read from LCD's RAM
00125 }
00126 
00127 /**
00128  * @brief
00129  * @note
00130  * @param
00131  * @retval
00132  */
00133 unsigned short FSMC8::rd_gram(bool convert)
00134 {
00135     unsigned int    r = 0;
00136 
00137     rd_data8 (); // dummy read
00138     r |= rd_data8 ();
00139     r <<= 8;
00140     r |= rd_data8 ();
00141 
00142     if (convert) {
00143         r <<= 8;
00144         r |= rd_data8 ();
00145 
00146         // gram is 18bit/pixel, if you set 16bit/pixel (cmd 3A), during writing the 16bits are expanded to 18bit
00147         // during reading, you read the raw 18bit gram
00148         r = RGB24to16((r & 0xFF0000) >> 16, (r & 0xFF00) >> 8, r & 0xFF);   // 18bit pixel padded to 24bits, rrrrrr00_gggggg00_bbbbbb00, converted to 16bit
00149     }
00150 
00151     //    _bus.output();
00152     return (unsigned short)r;
00153 }
00154 
00155 /**
00156  * @brief
00157  * @note
00158  * @param
00159  * @retval
00160  */
00161 unsigned int FSMC8::rd_reg_data32(unsigned char reg)
00162 {
00163     unsigned char  data[4] = { 0 };
00164     unsigned int*  r = (unsigned int*)&data[0];
00165 
00166     wr_cmd8(reg);
00167     data[3] = rd_data8 (0);
00168     data[2] = rd_data8 (0);
00169     data[1] = rd_data8 (0);
00170     data[0] = rd_data8 (0);
00171 
00172     return *r;
00173 }
00174 
00175 // in Par mode EXTC regs (0xB0-0xFF) can be directly read
00176 unsigned int FSMC8::rd_extcreg_data32(unsigned char reg, unsigned char SPIreadenablecmd)
00177 {
00178     return rd_reg_data32(reg);
00179 }
00180 
00181 // ILI932x specific
00182 void FSMC8::dummyread()
00183 {
00184     rd_data8 (); // dummy read
00185 }
00186 
00187 // ILI932x specific
00188 void FSMC8::reg_select(unsigned char reg, bool forread)
00189 {
00190     wr_cmd8(0);     // write MSB
00191     wr_cmd8(reg);   // write LSB
00192 }
00193 
00194 // ILI932x specific
00195 void FSMC8::reg_write(unsigned char reg, unsigned short data)
00196 {
00197     wr_cmd8(0);     // write MSB
00198     wr_cmd8(reg);   // write LSB
00199     wr_data16(data);
00200 }
00201 
00202 // ILI932x specific
00203 unsigned short FSMC8::reg_read(unsigned char reg)
00204 {
00205     unsigned short  r = 0;
00206 
00207     wr_cmd8(0);
00208     wr_cmd8(reg);
00209     r |= rd_data8 () & 0xFF;
00210     r <<= 8;
00211     r |= rd_data8 () & 0xFF;
00212 
00213     return r;
00214 }
00215 
00216 /**
00217  * @brief
00218  * @note
00219  * @param
00220  * @retval
00221  */
00222 void FSMC8::hw_reset()
00223 {
00224     _reset = 1;
00225     wait_ms(15);
00226     _reset = 0; // display reset
00227     wait_ms(2);
00228     _reset = 1; // end reset
00229     wait_ms(100);
00230 }