1
0

ssd1289.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. #include "ssd1289.h"
  2. // Compatible list:
  3. // ssd1289
  4. //内联函数定义,用以提高性能
  5. #ifdef __CC_ARM /* ARM Compiler */
  6. #define lcd_inline static __inline
  7. #elif defined (__ICCARM__) /* for IAR Compiler */
  8. #define lcd_inline inline
  9. #elif defined (__GNUC__) /* GNU GCC Compiler */
  10. #define lcd_inline static __inline
  11. #else
  12. #define lcd_inline static
  13. #endif
  14. #define rw_data_prepare() write_cmd(34)
  15. /********* control ***********/
  16. #include "stm32f10x.h"
  17. #include "board.h"
  18. //输出重定向.当不进行重定向时.
  19. #define printf rt_kprintf //使用rt_kprintf来输出
  20. //#define printf(...) //无输出
  21. /* LCD is connected to the FSMC_Bank1_NOR/SRAM2 and NE2 is used as ship select signal */
  22. /* RS <==> A2 */
  23. #define LCD_REG (*((volatile unsigned short *) 0x64000000)) /* RS = 0 */
  24. #define LCD_RAM (*((volatile unsigned short *) 0x64000008)) /* RS = 1 */
  25. static void LCD_FSMCConfig(void)
  26. {
  27. FSMC_NORSRAMInitTypeDef FSMC_NORSRAMInitStructure;
  28. FSMC_NORSRAMTimingInitTypeDef Timing_read,Timing_write;
  29. /* FSMC GPIO configure */
  30. {
  31. GPIO_InitTypeDef GPIO_InitStructure;
  32. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE | RCC_APB2Periph_GPIOF
  33. | RCC_APB2Periph_GPIOG, ENABLE);
  34. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, ENABLE);
  35. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  36. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  37. /*
  38. FSMC_D0 ~ FSMC_D3
  39. PD14 FSMC_D0 PD15 FSMC_D1 PD0 FSMC_D2 PD1 FSMC_D3
  40. */
  41. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_14 | GPIO_Pin_15;
  42. GPIO_Init(GPIOD,&GPIO_InitStructure);
  43. /*
  44. FSMC_D4 ~ FSMC_D12
  45. PE7 ~ PE15 FSMC_D4 ~ FSMC_D12
  46. */
  47. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10
  48. | GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
  49. GPIO_Init(GPIOE,&GPIO_InitStructure);
  50. /* FSMC_D13 ~ FSMC_D15 PD8 ~ PD10 */
  51. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10;
  52. GPIO_Init(GPIOD,&GPIO_InitStructure);
  53. /*
  54. FSMC_A0 ~ FSMC_A5 FSMC_A6 ~ FSMC_A9
  55. PF0 ~ PF5 PF12 ~ PF15
  56. */
  57. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3
  58. | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
  59. GPIO_Init(GPIOF,&GPIO_InitStructure);
  60. /* FSMC_A10 ~ FSMC_A15 PG0 ~ PG5 */
  61. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5;
  62. GPIO_Init(GPIOG,&GPIO_InitStructure);
  63. /* FSMC_A16 ~ FSMC_A18 PD11 ~ PD13 */
  64. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13;
  65. GPIO_Init(GPIOD,&GPIO_InitStructure);
  66. /* RD-PD4 WR-PD5 */
  67. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5;
  68. GPIO_Init(GPIOD,&GPIO_InitStructure);
  69. /* NBL0-PE0 NBL1-PE1 */
  70. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
  71. GPIO_Init(GPIOE,&GPIO_InitStructure);
  72. /* NE1/NCE2 */
  73. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
  74. GPIO_Init(GPIOD,&GPIO_InitStructure);
  75. /* NE2 */
  76. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  77. GPIO_Init(GPIOG,&GPIO_InitStructure);
  78. /* NE3 */
  79. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  80. GPIO_Init(GPIOG,&GPIO_InitStructure);
  81. /* NE4 */
  82. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
  83. GPIO_Init(GPIOG,&GPIO_InitStructure);
  84. }
  85. /* FSMC GPIO configure */
  86. /*-- FSMC Configuration -------------------------------------------------*/
  87. FSMC_NORSRAMInitStructure.FSMC_ReadWriteTimingStruct = &Timing_read;
  88. FSMC_NORSRAMInitStructure.FSMC_WriteTimingStruct = &Timing_write;
  89. FSMC_NORSRAMStructInit(&FSMC_NORSRAMInitStructure);
  90. Timing_read.FSMC_AddressSetupTime = 8; /* 地址建立时间 */
  91. Timing_read.FSMC_AddressHoldTime = 8; /* 地址保持时间 */
  92. Timing_read.FSMC_DataSetupTime = 8; /* 数据建立时间 */
  93. Timing_read.FSMC_AccessMode = FSMC_AccessMode_A; /* FSMC 访问模式 */
  94. Timing_write.FSMC_AddressSetupTime = 8; /* 地址建立时间 */
  95. Timing_write.FSMC_AddressHoldTime = 8; /* 地址保持时间 */
  96. Timing_write.FSMC_DataSetupTime = 8; /* 数据建立时间 */
  97. Timing_write.FSMC_AccessMode = FSMC_AccessMode_A; /* FSMC 访问模式 */
  98. /* Color LCD configuration ------------------------------------
  99. LCD configured as follow:
  100. - Data/Address MUX = Disable
  101. - Memory Type = SRAM
  102. - Data Width = 16bit
  103. - Write Operation = Enable
  104. - Extended Mode = Enable
  105. - Asynchronous Wait = Disable */
  106. FSMC_NORSRAMInitStructure.FSMC_Bank = FSMC_Bank1_NORSRAM2;
  107. FSMC_NORSRAMInitStructure.FSMC_DataAddressMux = FSMC_DataAddressMux_Disable;
  108. FSMC_NORSRAMInitStructure.FSMC_MemoryType = FSMC_MemoryType_SRAM;
  109. FSMC_NORSRAMInitStructure.FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_16b;
  110. FSMC_NORSRAMInitStructure.FSMC_BurstAccessMode = FSMC_BurstAccessMode_Disable;
  111. FSMC_NORSRAMInitStructure.FSMC_AsynchronousWait = FSMC_AsynchronousWait_Disable;
  112. FSMC_NORSRAMInitStructure.FSMC_WaitSignalPolarity = FSMC_WaitSignalPolarity_Low;
  113. FSMC_NORSRAMInitStructure.FSMC_WrapMode = FSMC_WrapMode_Disable;
  114. FSMC_NORSRAMInitStructure.FSMC_WaitSignalActive = FSMC_WaitSignalActive_BeforeWaitState;
  115. FSMC_NORSRAMInitStructure.FSMC_WriteOperation = FSMC_WriteOperation_Enable;
  116. FSMC_NORSRAMInitStructure.FSMC_WaitSignal = FSMC_WaitSignal_Disable;
  117. FSMC_NORSRAMInitStructure.FSMC_ExtendedMode = FSMC_ExtendedMode_Enable;
  118. FSMC_NORSRAMInitStructure.FSMC_WriteBurst = FSMC_WriteBurst_Disable;
  119. FSMC_NORSRAMInit(&FSMC_NORSRAMInitStructure);
  120. FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM2, ENABLE);
  121. }
  122. static void delay(int cnt)
  123. {
  124. volatile unsigned int dl;
  125. while(cnt--)
  126. {
  127. for(dl=0; dl<500; dl++);
  128. }
  129. }
  130. static void lcd_port_init(void)
  131. {
  132. LCD_FSMCConfig();
  133. }
  134. lcd_inline void write_cmd(unsigned short cmd)
  135. {
  136. LCD_REG = cmd;
  137. }
  138. lcd_inline unsigned short read_data(void)
  139. {
  140. return LCD_RAM;
  141. }
  142. lcd_inline void write_data(unsigned short data_code )
  143. {
  144. LCD_RAM = data_code;
  145. }
  146. lcd_inline void write_reg(unsigned char reg_addr,unsigned short reg_val)
  147. {
  148. write_cmd(reg_addr);
  149. write_data(reg_val);
  150. }
  151. lcd_inline unsigned short read_reg(unsigned char reg_addr)
  152. {
  153. unsigned short val=0;
  154. write_cmd(reg_addr);
  155. val = read_data();
  156. return (val);
  157. }
  158. /********* control <只移植以上函数即可> ***********/
  159. static unsigned short deviceid=0;//设置一个静态变量用来保存LCD的ID
  160. //static unsigned short BGR2RGB(unsigned short c)
  161. //{
  162. // u16 r, g, b, rgb;
  163. //
  164. // b = (c>>0) & 0x1f;
  165. // g = (c>>5) & 0x3f;
  166. // r = (c>>11) & 0x1f;
  167. //
  168. // rgb = (b<<11) + (g<<5) + (r<<0);
  169. //
  170. // return( rgb );
  171. //}
  172. static void lcd_SetCursor(unsigned int x,unsigned int y)
  173. {
  174. write_reg(0x004e,x); /* 0-239 */
  175. write_reg(0x004f,y); /* 0-319 */
  176. }
  177. /* 读取指定地址的GRAM */
  178. static unsigned short lcd_read_gram(unsigned int x,unsigned int y)
  179. {
  180. unsigned short temp;
  181. lcd_SetCursor(x,y);
  182. rw_data_prepare();
  183. /* dummy read */
  184. temp = read_data();
  185. temp = read_data();
  186. return temp;
  187. }
  188. static void lcd_clear(unsigned short Color)
  189. {
  190. unsigned int index=0;
  191. lcd_SetCursor(0,0);
  192. rw_data_prepare(); /* Prepare to write GRAM */
  193. for (index=0; index<(LCD_WIDTH*LCD_HEIGHT); index++)
  194. {
  195. write_data(Color);
  196. }
  197. }
  198. static void lcd_data_bus_test(void)
  199. {
  200. unsigned short temp1;
  201. unsigned short temp2;
  202. // /* [5:4]-ID~ID0 [3]-AM-1垂直-0水平 */
  203. // write_reg(0x0003,(1<<12)|(1<<5)|(1<<4) | (0<<3) );
  204. /* wirte */
  205. lcd_SetCursor(0,0);
  206. rw_data_prepare();
  207. write_data(0x5555);
  208. lcd_SetCursor(1,0);
  209. rw_data_prepare();
  210. write_data(0xAAAA);
  211. /* read */
  212. lcd_SetCursor(0,0);
  213. temp1 = lcd_read_gram(0,0);
  214. temp2 = lcd_read_gram(1,0);
  215. if( (temp1 == 0x5555) && (temp2 == 0xAAAA) )
  216. {
  217. printf(" data bus test pass!\r\n");
  218. }
  219. else
  220. {
  221. printf(" data bus test error: %04X %04X\r\n",temp1,temp2);
  222. }
  223. }
  224. void ssd1289_init(void)
  225. {
  226. lcd_port_init();
  227. deviceid = read_reg(0x00);
  228. /* deviceid check */
  229. if( deviceid != 0x8989 )
  230. {
  231. printf("Invalid LCD ID:%08X\r\n",deviceid);
  232. printf("Please check you hardware and configure.\r\n");
  233. }
  234. else
  235. {
  236. printf("\r\nLCD Device ID : %04X ",deviceid);
  237. }
  238. // power supply setting
  239. // set R07h at 0021h (GON=1,DTE=0,D[1:0]=01)
  240. write_reg(0x0007,0x0021);
  241. // set R00h at 0001h (OSCEN=1)
  242. write_reg(0x0000,0x0001);
  243. // set R07h at 0023h (GON=1,DTE=0,D[1:0]=11)
  244. write_reg(0x0007,0x0023);
  245. // set R10h at 0000h (Exit sleep mode)
  246. write_reg(0x0010,0x0000);
  247. // Wait 30ms
  248. delay(3000);
  249. // set R07h at 0033h (GON=1,DTE=1,D[1:0]=11)
  250. write_reg(0x0007,0x0033);
  251. // Entry mode setting (R11h)
  252. // R11H Entry mode
  253. // vsmode DFM1 DFM0 TRANS OEDef WMode DMode1 DMode0 TY1 TY0 ID1 ID0 AM LG2 LG2 LG0
  254. // 0 1 1 0 0 0 0 0 0 1 1 1 * 0 0 0
  255. write_reg(0x0011,0x6070);
  256. // LCD driver AC setting (R02h)
  257. write_reg(0x0002,0x0600);
  258. // power control 1
  259. // DCT3 DCT2 DCT1 DCT0 BT2 BT1 BT0 0 DC3 DC2 DC1 DC0 AP2 AP1 AP0 0
  260. // 1 0 1 0 1 0 0 0 1 0 1 0 0 1 0 0
  261. // DCT[3:0] fosc/4 BT[2:0] DC{3:0] fosc/4
  262. write_reg(0x0003,0x0804);//0xA8A4
  263. write_reg(0x000C,0x0000);//
  264. write_reg(0x000D,0x0808);// 0x080C --> 0x0808
  265. // power control 4
  266. // 0 0 VCOMG VDV4 VDV3 VDV2 VDV1 VDV0 0 0 0 0 0 0 0 0
  267. // 0 0 1 0 1 0 1 1 0 0 0 0 0 0 0 0
  268. write_reg(0x000E,0x2900);
  269. write_reg(0x001E,0x00B8);
  270. write_reg(0x0001,0x2B3F);//驱动输出控制320*240 0x6B3F
  271. write_reg(0x0010,0x0000);
  272. write_reg(0x0005,0x0000);
  273. write_reg(0x0006,0x0000);
  274. write_reg(0x0016,0xEF1C);
  275. write_reg(0x0017,0x0003);
  276. write_reg(0x0007,0x0233);//0x0233
  277. write_reg(0x000B,0x0000|(3<<6));
  278. write_reg(0x000F,0x0000);//扫描开始地址
  279. write_reg(0x0041,0x0000);
  280. write_reg(0x0042,0x0000);
  281. write_reg(0x0048,0x0000);
  282. write_reg(0x0049,0x013F);
  283. write_reg(0x004A,0x0000);
  284. write_reg(0x004B,0x0000);
  285. write_reg(0x0044,0xEF00);
  286. write_reg(0x0045,0x0000);
  287. write_reg(0x0046,0x013F);
  288. write_reg(0x0030,0x0707);
  289. write_reg(0x0031,0x0204);
  290. write_reg(0x0032,0x0204);
  291. write_reg(0x0033,0x0502);
  292. write_reg(0x0034,0x0507);
  293. write_reg(0x0035,0x0204);
  294. write_reg(0x0036,0x0204);
  295. write_reg(0x0037,0x0502);
  296. write_reg(0x003A,0x0302);
  297. write_reg(0x003B,0x0302);
  298. write_reg(0x0023,0x0000);
  299. write_reg(0x0024,0x0000);
  300. write_reg(0x0025,0x8000); // 65hz
  301. write_reg(0x004f,0); // 行首址0
  302. write_reg(0x004e,0); // 列首址0
  303. //数据总线测试,用于测试硬件连接是否正常.
  304. lcd_data_bus_test();
  305. //GRAM测试,此测试可以测试LCD控制器内部GRAM.测试通过保证硬件正常
  306. // lcd_gram_test();
  307. //清屏
  308. lcd_clear( Blue );
  309. }
  310. /* 设置像素点 颜色,X,Y */
  311. void ssd1289_lcd_set_pixel(const char* pixel, int x, int y)
  312. {
  313. lcd_SetCursor(x,y);
  314. rw_data_prepare();
  315. write_data(*(rt_uint16_t*)pixel);
  316. }
  317. /* 获取像素点颜色 */
  318. void ssd1289_lcd_get_pixel(char* pixel, int x, int y)
  319. {
  320. *(rt_uint16_t*)pixel = lcd_read_gram(x, y);
  321. }
  322. /* 画水平线 */
  323. void ssd1289_lcd_draw_hline(const char* pixel, int x1, int x2, int y)
  324. {
  325. /* [5:4]-ID~ID0 [3]-AM-1垂直-0水平 */
  326. write_reg(0x0011,0x6030 | (0<<3)); // AM=0 hline
  327. lcd_SetCursor(x1, y);
  328. rw_data_prepare(); /* Prepare to write GRAM */
  329. while (x1 < x2)
  330. {
  331. write_data(*(rt_uint16_t*)pixel);
  332. x1++;
  333. }
  334. }
  335. /* 垂直线 */
  336. void ssd1289_lcd_draw_vline(const char* pixel, int x, int y1, int y2)
  337. {
  338. /* [5:4]-ID~ID0 [3]-AM-1垂直-0水平 */
  339. write_reg(0x0011,0x6070 | (1<<3)); // AM=0 vline
  340. lcd_SetCursor(x, y1);
  341. rw_data_prepare(); /* Prepare to write GRAM */
  342. while (y1 < y2)
  343. {
  344. write_data(*(rt_uint16_t*)pixel);
  345. y1++;
  346. }
  347. }
  348. /* blit a line */
  349. void ssd1289_lcd_blit_line(const char* pixels, int x, int y, rt_size_t size)
  350. {
  351. rt_uint16_t *ptr;
  352. ptr = (rt_uint16_t*)pixels;
  353. /* [5:4]-ID~ID0 [3]-AM-1垂直-0水平 */
  354. write_reg(0x0011,0x6070 | (0<<3)); // AM=0 hline
  355. lcd_SetCursor(x, y);
  356. rw_data_prepare(); /* Prepare to write GRAM */
  357. while (size)
  358. {
  359. write_data(*ptr ++);
  360. size --;
  361. }
  362. }
  363. struct rt_device_graphic_ops ssd1289_ops =
  364. {
  365. ssd1289_lcd_set_pixel,
  366. ssd1289_lcd_get_pixel,
  367. ssd1289_lcd_draw_hline,
  368. ssd1289_lcd_draw_vline,
  369. ssd1289_lcd_blit_line
  370. };
  371. struct rt_device _lcd_device;
  372. static rt_err_t lcd_init(rt_device_t dev)
  373. {
  374. return RT_EOK;
  375. }
  376. static rt_err_t lcd_open(rt_device_t dev, rt_uint16_t oflag)
  377. {
  378. return RT_EOK;
  379. }
  380. static rt_err_t lcd_close(rt_device_t dev)
  381. {
  382. return RT_EOK;
  383. }
  384. static rt_err_t lcd_control(rt_device_t dev, rt_uint8_t cmd, void *args)
  385. {
  386. switch (cmd)
  387. {
  388. case RTGRAPHIC_CTRL_GET_INFO:
  389. {
  390. struct rt_device_graphic_info *info;
  391. info = (struct rt_device_graphic_info*) args;
  392. RT_ASSERT(info != RT_NULL);
  393. info->bits_per_pixel = 16;
  394. info->pixel_format = RTGRAPHIC_PIXEL_FORMAT_RGB565P;
  395. info->framebuffer = RT_NULL;
  396. info->width = 240;
  397. info->height = 320;
  398. }
  399. break;
  400. case RTGRAPHIC_CTRL_RECT_UPDATE:
  401. /* nothong to be done */
  402. break;
  403. default:
  404. break;
  405. }
  406. return RT_EOK;
  407. }
  408. void rt_hw_lcd_init(void)
  409. {
  410. /* LCD RESET */
  411. /* PF10 : LCD RESET */
  412. {
  413. GPIO_InitTypeDef GPIO_InitStructure;
  414. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOF, ENABLE);
  415. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  416. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  417. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  418. GPIO_Init(GPIOF,&GPIO_InitStructure);
  419. GPIO_ResetBits(GPIOF,GPIO_Pin_10);
  420. GPIO_SetBits(GPIOF,GPIO_Pin_10);
  421. /* wait for lcd reset */
  422. rt_thread_delay(1);
  423. }
  424. /* register lcd device */
  425. _lcd_device.type = RT_Device_Class_Graphic;
  426. _lcd_device.init = lcd_init;
  427. _lcd_device.open = lcd_open;
  428. _lcd_device.close = lcd_close;
  429. _lcd_device.control = lcd_control;
  430. _lcd_device.read = RT_NULL;
  431. _lcd_device.write = RT_NULL;
  432. _lcd_device.user_data = &ssd1289_ops;
  433. ssd1289_init();
  434. /* register graphic device driver */
  435. rt_device_register(&_lcd_device, "lcd",
  436. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_STANDALONE);
  437. }