PageRenderTime 180ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/MakefileBasedBuild/Atmel/sam3x/sam3x-ek/libraries/libboard_sam3x-ek/source/hx8347.c

https://bitbucket.org/hakanserce/adk2012_demo
C | 794 lines | 403 code | 105 blank | 286 comment | 36 complexity | c256a4bba60a9148ed7dd3cb2fb785ff MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause
  1. /* ----------------------------------------------------------------------------
  2. * ATMEL Microcontroller Software Support
  3. * ----------------------------------------------------------------------------
  4. * Copyright (c) 2010, Atmel Corporation
  5. *
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are met:
  10. *
  11. * - Redistributions of source code must retain the above copyright notice,
  12. * this list of conditions and the disclaimer below.
  13. *
  14. * Atmel's name may not be used to endorse or promote products derived from
  15. * this software without specific prior written permission.
  16. *
  17. * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
  18. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  20. * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
  21. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  22. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  23. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  24. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  25. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  26. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. * ----------------------------------------------------------------------------
  28. */
  29. /**
  30. * \file
  31. *
  32. * Implementation of HX8347 driver.
  33. *
  34. */
  35. /*----------------------------------------------------------------------------
  36. * Headers
  37. *----------------------------------------------------------------------------*/
  38. #include "board.h"
  39. #include <stdint.h>
  40. #include <string.h>
  41. #include <stdio.h>
  42. /*----------------------------------------------------------------------------
  43. * Local definitions
  44. *----------------------------------------------------------------------------*/
  45. /** Pixel cache size */
  46. #define LCD_DATA_CACHE_SIZE BOARD_LCD_WIDTH
  47. /// HX8347 ID code
  48. #define HX8347_HIMAXID_CODE 0x47
  49. /*----------------------------------------------------------------------------
  50. * Local variables
  51. *----------------------------------------------------------------------------*/
  52. /* Pixel cache used to speed up SPI communication */
  53. static LcdColor_t gLcdPixelCache[LCD_DATA_CACHE_SIZE];
  54. /*----------------------------------------------------------------------------
  55. * Local functions
  56. *----------------------------------------------------------------------------*/
  57. /**
  58. * ----------------------------------------------------------------------------
  59. * \brief Send command to LCD controller.
  60. *
  61. * \param cmd command.
  62. * ----------------------------------------------------------------------------
  63. */
  64. static inline void WriteCmd(uint8_t cmd)
  65. {
  66. LCD_IR = cmd;
  67. }
  68. /**
  69. * ----------------------------------------------------------------------------
  70. * \brief Send one data to LCD controller.
  71. *
  72. * \param data data.
  73. * ----------------------------------------------------------------------------
  74. */
  75. static inline void WriteData(uint16_t data)
  76. {
  77. LCD_D = data;
  78. }
  79. /**
  80. * ----------------------------------------------------------------------------
  81. * \brief Read one data from LCD controller.
  82. * ----------------------------------------------------------------------------
  83. */
  84. static inline uint16_t ReadData( void )
  85. {
  86. return LCD_D ;
  87. }
  88. /**
  89. * \brief Write mutiple data in buffer to LCD controller.
  90. *
  91. * \param pBuf data buffer.
  92. * \param size size in pixels.
  93. */
  94. static void WriteBuffer( const LcdColor_t *pBuf, uint32_t size )
  95. {
  96. uint32_t i ;
  97. for ( i = 0 ; i < size ; ++i )
  98. {
  99. WriteData( pBuf[i] ) ;
  100. }
  101. }
  102. /**
  103. * ----------------------------------------------------------------------------
  104. * \brief Write data to LCD Register.
  105. *
  106. * \param reg Register address.
  107. * \param data Data to be written.
  108. * ----------------------------------------------------------------------------
  109. */
  110. static void WriteReg( uint8_t reg, uint16_t data )
  111. {
  112. WriteCmd( reg ) ;
  113. WriteData( data ) ;
  114. }
  115. /**
  116. * \brief Write data to LCD Register.
  117. *
  118. * \param reg Register address.
  119. * \return Data read.
  120. */
  121. static uint16_t ReadReg( uint8_t reg )
  122. {
  123. WriteCmd( reg ) ;
  124. return ReadData() ;
  125. }
  126. /**
  127. * ----------------------------------------------------------------------------
  128. * \brief Simple delay, implemented as a loop.
  129. * Shall be used only during the initialization phase.
  130. *
  131. * \param ms Delay specified in ms.
  132. * ----------------------------------------------------------------------------
  133. */
  134. static void Delay( uint32_t ms )
  135. {
  136. volatile uint32_t i ;
  137. i = ms * (BOARD_MCK / (1000 * 6)) ;
  138. while(i--) ;
  139. }
  140. /*----------------------------------------------------------------------------
  141. * Basic HX8347 primitives
  142. *----------------------------------------------------------------------------*/
  143. /**
  144. * ----------------------------------------------------------------------------
  145. * \brief Check Box coordinates. Return upper left and bottom right coordinates.
  146. *
  147. * \param pX1 X-coordinate of upper-left corner on LCD.
  148. * \param pY1 Y-coordinate of upper-left corner on LCD.
  149. * \param pX2 X-coordinate of lower-right corner on LCD.
  150. * \param pY2 Y-coordinate of lower-right corner on LCD.
  151. * ----------------------------------------------------------------------------
  152. */
  153. static void CheckBoundaries( uint32_t *pX1, uint32_t *pY1, uint32_t *pX2, uint32_t *pY2 )
  154. {
  155. uint32_t dw;
  156. if ( *pX1 >= BOARD_LCD_WIDTH )
  157. {
  158. *pX1=BOARD_LCD_WIDTH-1 ;
  159. }
  160. if ( *pX2 >= BOARD_LCD_WIDTH )
  161. {
  162. *pX2=BOARD_LCD_WIDTH-1 ;
  163. }
  164. if ( *pY1 >= BOARD_LCD_HEIGHT )
  165. {
  166. *pY1=BOARD_LCD_HEIGHT-1 ;
  167. }
  168. if ( *pY2 >= BOARD_LCD_HEIGHT )
  169. {
  170. *pY2=BOARD_LCD_HEIGHT-1 ;
  171. }
  172. if ( *pX1 > *pX2 )
  173. {
  174. dw = *pX1;
  175. *pX1 = *pX2;
  176. *pX2 = dw;
  177. }
  178. if ( *pY1 > *pY2 )
  179. {
  180. dw = *pY1;
  181. *pY1 = *pY2;
  182. *pY2 = dw;
  183. }
  184. }
  185. /**
  186. * ----------------------------------------------------------------------------
  187. * \brief Set the window size user can access.
  188. * \param x_start X-coordinate of upper-left corner on LCD.
  189. * \param y_start Y-coordinate of upper-left corner on LCD.
  190. * \param x_end X-coordinate of bottom-right corner on LCD.
  191. * \param y_end Y-coordinate of bottom-right corner on LCD.
  192. * ----------------------------------------------------------------------------
  193. */
  194. extern void LCD_SetWindow( uint32_t dwX, uint32_t dwY, uint32_t dwWidth, uint32_t dwHeight )
  195. {
  196. WriteReg( 0x02, (dwX & 0xff00) >> 8 ) ; // column high
  197. WriteReg( 0x03, dwX & 0xff ) ; // column low
  198. WriteReg( 0x06, (dwY & 0xff00) >>8 ) ; // row high
  199. WriteReg( 0x07, dwY & 0xff ) ; // row low
  200. WriteReg( 0x04, (dwWidth & 0xff00) >>8 ) ; // column high
  201. WriteReg( 0x05, dwWidth & 0xff ) ; // column low
  202. WriteReg( 0x08, (dwHeight & 0xff00) >>8 ) ; // row high
  203. WriteReg( 0x09, dwHeight & 0xff ) ; // row low
  204. }
  205. /**
  206. * ----------------------------------------------------------------------------
  207. * \brief Set cursor of LCD srceen.
  208. * \param x X-coordinate of upper-left corner on LCD.
  209. * \param y Y-coordinate of upper-left corner on LCD.
  210. * ----------------------------------------------------------------------------
  211. */
  212. extern void LCD_SetCursor( uint32_t dwX, uint32_t dwY )
  213. {
  214. WriteReg( HX8347_R02H, (dwX & 0xff00) >>8); // column high
  215. WriteReg( HX8347_R03H, dwX & 0xff); // column low
  216. WriteReg( HX8347_R06H, (dwY & 0xff00) >>8); // row high
  217. WriteReg( HX8347_R07H, dwY & 0xff); // row low
  218. }
  219. /**
  220. * ----------------------------------------------------------------------------
  221. * \brief Initialize the LCD controller.
  222. * ----------------------------------------------------------------------------
  223. */
  224. extern uint32_t LCD_Initialize( void )
  225. {
  226. uint16_t chipid ;
  227. const Pin pPins[] = {BOARD_LCD_PINS};
  228. SmcCs_number *pSmcCs = &(SMC->SMC_CS_NUMBER[BOARD_LCD_NCS]);
  229. // Enable pins
  230. PIO_PinConfigure( pPins, PIO_LISTSIZE( pPins ) ) ;
  231. // Enable peripheral clock
  232. PMC_EnablePeripheral( ID_SMC ) ;
  233. // EBI SMC Configuration
  234. pSmcCs->SMC_SETUP = BOARD_LCD_SETUP ;
  235. pSmcCs->SMC_PULSE = BOARD_LCD_PULSE ;
  236. pSmcCs->SMC_CYCLE = BOARD_LCD_CYCLE ;
  237. pSmcCs->SMC_MODE = BOARD_LCD_MODE ;
  238. /* Turn off LCD */
  239. LCD_Off() ;
  240. /*======== LCD module initial code ========*/
  241. // Check HX8347 chipid
  242. chipid = ReadReg( HX8347_R67H ) ;
  243. if ( chipid != HX8347_HIMAXID_CODE )
  244. {
  245. printf( "Read HX8347 chip ID error, skip initialization.\r\n" ) ;
  246. return 1 ;
  247. }
  248. // Start internal OSC
  249. WriteReg(HX8347_R19H, 0x49); // OSCADJ=10 0000, OSD_EN=1 //60Hz
  250. WriteReg(HX8347_R93H, 0x0C); // RADJ=1100
  251. // Power on flow
  252. WriteReg(HX8347_R44H, 0x4D); // VCM=100 1101
  253. WriteReg(HX8347_R45H, 0x11); // VDV=1 0001
  254. WriteReg(HX8347_R20H, 0x40); // BT=0100
  255. WriteReg(HX8347_R1DH, 0x07); // VC1=111
  256. WriteReg(HX8347_R1EH, 0x00); // VC3=000
  257. WriteReg(HX8347_R1FH, 0x04); // VRH=0100
  258. WriteReg(HX8347_R1CH, 0x04); // AP=100
  259. WriteReg(HX8347_R1BH, 0x10); // GASENB=0, PON=1, DK=0, XDK=0, DDVDH_TRI=0, STB=0
  260. Delay(50);
  261. WriteReg(HX8347_R43H, 0x80); // Set VCOMG=1
  262. Delay(50);
  263. /*
  264. // Gamma for CMO 2.8
  265. WriteReg(HX8347_R46H, 0x95);
  266. WriteReg(HX8347_R47H, 0x51);
  267. WriteReg(HX8347_R48H, 0x00);
  268. WriteReg(HX8347_R49H, 0x36);
  269. WriteReg(HX8347_R4AH, 0x11);
  270. WriteReg(HX8347_R4BH, 0x66);
  271. WriteReg(HX8347_R4CH, 0x14);
  272. WriteReg(HX8347_R4DH, 0x77);
  273. WriteReg(HX8347_R4EH, 0x13);
  274. WriteReg(HX8347_R4FH, 0x4C);
  275. WriteReg(HX8347_R50H, 0x46);
  276. WriteReg(HX8347_R51H, 0x46);
  277. */
  278. //240x320 window setting
  279. WriteReg(HX8347_R02H, 0x00); // Column address start2
  280. WriteReg(HX8347_R03H, 0x00); // Column address start1
  281. WriteReg(HX8347_R04H, 0x00); // Column address end2
  282. WriteReg(HX8347_R05H, 0xEF); // Column address end1
  283. WriteReg(HX8347_R06H, 0x00); // Row address start2
  284. WriteReg(HX8347_R07H, 0x00); // Row address start1
  285. WriteReg(HX8347_R08H, 0x01); // Row address end2
  286. WriteReg(HX8347_R09H, 0x3F); // Row address end1
  287. // Display Setting
  288. WriteReg(HX8347_R01H, 0x06); // IDMON=0, INVON=1, NORON=1, PTLON=0
  289. //WriteReg(HX8347_R16H, 0xC8); // MY=1, MX=1, MV=0, BGR=1
  290. WriteReg(HX8347_R16H, 0x68); // MY=0, MX=1, MV=1, RGB XY exchange X mirror
  291. WriteReg(HX8347_R23H, 0x95); // N_DC=1001 0101
  292. WriteReg(HX8347_R24H, 0x95); // P_DC=1001 0101
  293. WriteReg(HX8347_R25H, 0xFF); // I_DC=1111 1111
  294. WriteReg(HX8347_R27H, 0x06); // N_BP=0000 0110
  295. WriteReg(HX8347_R28H, 0x06); // N_FP=0000 0110
  296. WriteReg(HX8347_R29H, 0x06); // P_BP=0000 0110
  297. WriteReg(HX8347_R2AH, 0x06); // P_FP=0000 0110
  298. WriteReg(HX8347_R2CH, 0x06); // I_BP=0000 0110
  299. WriteReg(HX8347_R2DH, 0x06); // I_FP=0000 0110
  300. WriteReg(HX8347_R3AH, 0x01); // N_RTN=0000, N_NW=001
  301. WriteReg(HX8347_R3BH, 0x01); // P_RTN=0000, P_NW=001
  302. WriteReg(HX8347_R3CH, 0xF0); // I_RTN=1111, I_NW=000
  303. WriteReg(HX8347_R3DH, 0x00); // DIV=00
  304. WriteReg(HX8347_R3EH, 0x38); // SON=38h
  305. WriteReg(HX8347_R40H, 0x0F); // GDON=0Fh
  306. WriteReg(HX8347_R41H, 0xF0); // GDOF=F0h
  307. return 0;
  308. }
  309. /**
  310. * ----------------------------------------------------------------------------
  311. * \brief Turn on the LCD.
  312. * ----------------------------------------------------------------------------
  313. */
  314. extern void LCD_On( void )
  315. {
  316. // Display ON Setting
  317. WriteReg( HX8347_R90H, 0x7F ) ; // SAP=0111 1111
  318. WriteReg( HX8347_R26H, 0x04 ) ; // GON=0, DTE=0, D=01
  319. Delay( 100 ) ;
  320. WriteReg( HX8347_R26H, 0x24 ) ; // GON=1, DTE=0, D=01
  321. WriteReg( HX8347_R26H, 0x2C ) ; // GON=1, DTE=0, D=11
  322. Delay( 100 ) ;
  323. WriteReg( HX8347_R26H, 0x3C ) ; // GON=1, DTE=1, D=11
  324. }
  325. /**
  326. * ----------------------------------------------------------------------------
  327. * \brief Turn off the LCD.
  328. * ----------------------------------------------------------------------------
  329. */
  330. extern void LCD_Off( void )
  331. {
  332. WriteReg( HX8347_R90H, 0x00 ) ; // SAP=0000 0000
  333. WriteReg( HX8347_R26H, 0x00 ) ; // GON=0, DTE=0, D=00
  334. }
  335. /**
  336. * ----------------------------------------------------------------------------
  337. * \brief Convert 24 bit RGB color into 5-6-5 rgb color space.
  338. *
  339. * Initialize the LcdColor_t cache with the color pattern.
  340. * \param x 24-bits RGB color.
  341. * \return 0 for successfull operation.
  342. * ----------------------------------------------------------------------------
  343. */
  344. extern uint32_t LCD_SetColor( uint32_t dwRgb24Bits )
  345. {
  346. uint16_t i ;
  347. LcdColor_t wColor ;
  348. wColor = (dwRgb24Bits & 0xF80000) >> 8 |
  349. (dwRgb24Bits & 0x00FC00) >> 5 |
  350. (dwRgb24Bits & 0x0000F8) >> 3;
  351. /* Fill the cache with selected color */
  352. for ( i = 0 ; i < LCD_DATA_CACHE_SIZE ; ++i )
  353. {
  354. gLcdPixelCache[i] = wColor ;
  355. }
  356. return 0 ;
  357. }
  358. /**
  359. * ----------------------------------------------------------------------------
  360. * \brief Draw a LcdColor_t on LCD of given color.
  361. *
  362. * \param x X-coordinate of pixel.
  363. * \param y Y-coordinate of pixel.
  364. * ----------------------------------------------------------------------------
  365. */
  366. extern uint32_t LCD_DrawPixel( uint32_t x, uint32_t y )
  367. {
  368. if ( (x >= BOARD_LCD_WIDTH) || (y >= BOARD_LCD_HEIGHT) )
  369. {
  370. return 1 ;
  371. }
  372. /* Set cursor */
  373. LCD_SetCursor( x, y ) ;
  374. /* Prepare to write in GRAM */
  375. WriteCmd( HX8347_R22H ) ;
  376. WriteData( *gLcdPixelCache ) ;
  377. return 0 ;
  378. }
  379. /**
  380. * ----------------------------------------------------------------------------
  381. * \brief Write several pixels with the same color to LCD GRAM.
  382. *
  383. * LcdColor_t color is set by the LCD_SetColor() function.
  384. * This function is optimized using an sram buffer to transfer block instead of
  385. * individual pixels in order to limit the number of SPI interrupts.
  386. * \param dwX1 X-coordinate of upper-left corner on LCD.
  387. * \param dwY1 Y-coordinate of upper-left corner on LCD.
  388. * \param dwX2 X-coordinate of lower-right corner on LCD.
  389. * \param dwY2 Y-coordinate of lower-right corner on LCD.
  390. * ----------------------------------------------------------------------------
  391. */
  392. extern uint32_t LCD_DrawFilledRectangle( uint32_t dwX1, uint32_t dwY1, uint32_t dwX2, uint32_t dwY2 )
  393. {
  394. uint32_t size, blocks ;
  395. /* Swap coordinates if necessary */
  396. CheckBoundaries( &dwX1, &dwY1, &dwX2, &dwY2 ) ;
  397. /* Determine the refresh window area */
  398. LCD_SetWindow( dwX1, dwY1, dwX2, dwY2 ) ;
  399. /* Set cursor */
  400. LCD_SetCursor( dwX1, dwY1 ) ;
  401. /* Prepare to write in GRAM */
  402. WriteCmd( HX8347_R22H ) ;
  403. size = (dwX2 - dwX1 + 1) * (dwY2 - dwY1 + 1) ;
  404. /* Send pixels blocks => one SPI IT / block */
  405. blocks = size / LCD_DATA_CACHE_SIZE ;
  406. while ( blocks-- )
  407. {
  408. WriteBuffer( gLcdPixelCache, LCD_DATA_CACHE_SIZE ) ;
  409. }
  410. /* Send remaining pixels */
  411. WriteBuffer( gLcdPixelCache, size % LCD_DATA_CACHE_SIZE ) ;
  412. /* Reset the refresh window area */
  413. LCD_SetWindow( 0, 0, BOARD_LCD_WIDTH - 1, BOARD_LCD_HEIGHT - 1 ) ;
  414. return 0 ;
  415. }
  416. /**
  417. * \brief Write several pixels pre-formatted in a bufer to LCD GRAM.
  418. *
  419. * \param dwX1 X-coordinate of upper-left corner on LCD.
  420. * \param dwY1 Y-coordinate of upper-left corner on LCD.
  421. * \param dwX2 X-coordinate of lower-right corner on LCD.
  422. * \param dwY2 Y-coordinate of lower-right corner on LCD.
  423. * \param pBuffer LcdColor_t buffer area.
  424. */
  425. extern uint32_t LCD_DrawPicture( uint32_t dwX1, uint32_t dwY1, uint32_t dwX2, uint32_t dwY2, const LcdColor_t *pBuffer )
  426. {
  427. uint32_t size, blocks ;
  428. LcdColor_t currentColor ;
  429. /* Swap coordinates if necessary */
  430. CheckBoundaries( &dwX1, &dwY1, &dwX2, &dwY2 ) ;
  431. /* Determine the refresh window area */
  432. LCD_SetWindow( dwX1, dwY1, dwX2, dwY2 ) ;
  433. /* Set cursor */
  434. LCD_SetCursor( dwX1, dwY1 ) ;
  435. /* Prepare to write in GRAM */
  436. WriteCmd( HX8347_R22H ) ;
  437. size = (dwX2 - dwX1 + 1) * (dwY2 - dwY1 + 1) ;
  438. /* Check if the buffer is within the SRAM */
  439. if ( (IRAM0_ADDR <= (uint32_t)pBuffer) && ((uint32_t)pBuffer < NFC_RAM_ADDR) )
  440. {
  441. WriteBuffer( pBuffer, size ) ;
  442. }
  443. /* If the buffer is not in SRAM, transfer it in SRAM first before transfer */
  444. else
  445. {
  446. /* Use color buffer as a cache */
  447. currentColor = gLcdPixelCache[0];
  448. /* Send pixels blocks => one SPI IT / block */
  449. blocks = size / LCD_DATA_CACHE_SIZE;
  450. while ( blocks-- )
  451. {
  452. memcpy( gLcdPixelCache, pBuffer, LCD_DATA_CACHE_SIZE * sizeof( LcdColor_t ) ) ;
  453. WriteBuffer( gLcdPixelCache, LCD_DATA_CACHE_SIZE ) ;
  454. pBuffer += LCD_DATA_CACHE_SIZE ;
  455. }
  456. /* Send remaining pixels */
  457. memcpy( gLcdPixelCache, pBuffer, (size % LCD_DATA_CACHE_SIZE) * sizeof( LcdColor_t ) ) ;
  458. WriteBuffer( gLcdPixelCache, size % LCD_DATA_CACHE_SIZE ) ;
  459. /* Restore the color cache */
  460. LCD_SetColor( currentColor ) ;
  461. }
  462. /* Reset the refresh window area */
  463. LCD_SetWindow( 0, 0, BOARD_LCD_WIDTH - 1, BOARD_LCD_HEIGHT - 1 ) ;
  464. return 0 ;
  465. }
  466. /*
  467. * \brief Draw a line on LCD, which is not horizontal or vertical.
  468. *
  469. * \param x X-coordinate of line start.
  470. * \param y Y-coordinate of line start.
  471. * \param length line length.
  472. * \param direction line direction: 0 - horizontal, 1 - vertical.
  473. * \param color LcdColor_t color.
  474. */
  475. static uint32_t DrawLineBresenham( uint32_t dwX1, uint32_t dwY1, uint32_t dwX2, uint32_t dwY2 )
  476. {
  477. int dx, dy ;
  478. int i ;
  479. int xinc, yinc, cumul ;
  480. int x, y ;
  481. x = dwX1 ;
  482. y = dwY1 ;
  483. dx = dwX2 - dwX1 ;
  484. dy = dwY2 - dwY1 ;
  485. xinc = ( dx > 0 ) ? 1 : -1 ;
  486. yinc = ( dy > 0 ) ? 1 : -1 ;
  487. dx = ( dx > 0 ) ? dx : -dx ;
  488. dy = ( dy > 0 ) ? dy : -dy ;
  489. LCD_DrawPixel( x, y ) ;
  490. if ( dx > dy )
  491. {
  492. cumul = dx / 2 ;
  493. for ( i = 1 ; i <= dx ; i++ )
  494. {
  495. x += xinc ;
  496. cumul += dy ;
  497. if ( cumul >= dx )
  498. {
  499. cumul -= dx ;
  500. y += yinc ;
  501. }
  502. LCD_DrawPixel( x, y ) ;
  503. }
  504. }
  505. else
  506. {
  507. cumul = dy / 2 ;
  508. for ( i = 1 ; i <= dy ; i++ )
  509. {
  510. y += yinc ;
  511. cumul += dx ;
  512. if ( cumul >= dy )
  513. {
  514. cumul -= dy ;
  515. x += xinc ;
  516. }
  517. LCD_DrawPixel( x, y ) ;
  518. }
  519. }
  520. return 0 ;
  521. }
  522. /*
  523. * \brief Draw a line on LCD, horizontal and vertical line are supported.
  524. *
  525. * \param dwX1 X-coordinate of line start.
  526. * \param dwY1 Y-coordinate of line start.
  527. * \param dwX2 X-coordinate of line end.
  528. * \param dwY2 Y-coordinate of line end.
  529. */
  530. extern uint32_t LCD_DrawLine ( uint32_t dwX1, uint32_t dwY1, uint32_t dwX2, uint32_t dwY2 )
  531. {
  532. /* Optimize horizontal or vertical line drawing */
  533. if ( ( dwY1 == dwY2 ) || (dwX1 == dwX2) )
  534. {
  535. LCD_DrawFilledRectangle( dwX1, dwY1, dwX2, dwY2 ) ;
  536. }
  537. else
  538. {
  539. DrawLineBresenham( dwX1, dwY1, dwX2, dwY2 ) ;
  540. }
  541. return 0 ;
  542. }
  543. /**
  544. * \brief Draws a circle on LCD, at the given coordinates.
  545. *
  546. * \param dwX X-coordinate of circle center.
  547. * \param dwY Y-coordinate of circle center.
  548. * \param dwR circle radius.
  549. */
  550. extern uint32_t LCD_DrawCircle( uint32_t dwX, uint32_t dwY, uint32_t dwR )
  551. {
  552. int32_t d ; /* Decision Variable */
  553. uint32_t curX ; /* Current X Value */
  554. uint32_t curY ; /* Current Y Value */
  555. if ( dwR == 0 )
  556. {
  557. return 0 ;
  558. }
  559. d = 3 - (dwR << 1) ;
  560. curX = 0 ;
  561. curY = dwR ;
  562. while ( curX <= curY )
  563. {
  564. LCD_DrawPixel(dwX + curX, dwY + curY);
  565. LCD_DrawPixel(dwX + curX, dwY - curY);
  566. LCD_DrawPixel(dwX - curX, dwY + curY);
  567. LCD_DrawPixel(dwX - curX, dwY - curY);
  568. LCD_DrawPixel(dwX + curY, dwY + curX);
  569. LCD_DrawPixel(dwX + curY, dwY - curX);
  570. LCD_DrawPixel(dwX - curY, dwY + curX);
  571. LCD_DrawPixel(dwX - curY, dwY - curX);
  572. if (d < 0) {
  573. d += (curX << 2) + 6;
  574. }
  575. else {
  576. d += ((curX - curY) << 2) + 10;
  577. curY--;
  578. }
  579. curX++;
  580. }
  581. return 0;
  582. }
  583. /**
  584. * \brief Draws a filled circle on LCD, at the given coordinates.
  585. *
  586. * \param dwX X-coordinate of circle center.
  587. * \param dwY Y-coordinate of circle center.
  588. * \param dwRadius circle radius.
  589. */
  590. extern uint32_t LCD_DrawFilledCircle( uint32_t dwX, uint32_t dwY, uint32_t dwRadius )
  591. {
  592. signed int d ; // Decision Variable
  593. uint32_t dwCurX ; // Current X Value
  594. uint32_t dwCurY ; // Current Y Value
  595. uint32_t dwXmin, dwYmin;
  596. if ( dwRadius == 0 )
  597. {
  598. return 0 ;
  599. }
  600. d = 3 - (dwRadius << 1) ;
  601. dwCurX = 0 ;
  602. dwCurY = dwRadius ;
  603. while ( dwCurX <= dwCurY )
  604. {
  605. dwXmin = (dwCurX > dwX) ? 0 : dwX-dwCurX;
  606. dwYmin = (dwCurY > dwY) ? 0 : dwY-dwCurY;
  607. LCD_DrawFilledRectangle( dwXmin, dwYmin, dwX+dwCurX, dwYmin ) ;
  608. LCD_DrawFilledRectangle( dwXmin, dwY+dwCurY, dwX+dwCurX, dwY+dwCurY ) ;
  609. dwXmin = (dwCurY > dwX) ? 0 : dwX-dwCurY;
  610. dwYmin = (dwCurX > dwY) ? 0 : dwY-dwCurX;
  611. LCD_DrawFilledRectangle( dwXmin, dwYmin, dwX+dwCurY, dwYmin ) ;
  612. LCD_DrawFilledRectangle( dwXmin, dwY+dwCurX, dwX+dwCurY, dwY+dwCurX ) ;
  613. if ( d < 0 )
  614. {
  615. d += (dwCurX << 2) + 6 ;
  616. }
  617. else
  618. {
  619. d += ((dwCurX - dwCurY) << 2) + 10;
  620. dwCurY-- ;
  621. }
  622. dwCurX++ ;
  623. }
  624. return 0 ;
  625. }
  626. /**
  627. * \brief Draws a rectangle on LCD, at the given coordinates.
  628. *
  629. * \param dwX 1 X-coordinate of one angle.
  630. * \param dwY1 Y-coordinate of one angle.
  631. * \param dwX2 X-coordinate of another angle.
  632. * \param dwY2 Y-coordinate of another angle.
  633. */
  634. extern uint32_t LCD_DrawRectangle( uint32_t dwX1, uint32_t dwY1, uint32_t dwX2, uint32_t dwY2 )
  635. {
  636. CheckBoundaries( &dwX1, &dwY1, &dwX2, &dwY2 ) ;
  637. LCD_DrawFilledRectangle( dwX1, dwY1, dwX2, dwY1 ) ;
  638. LCD_DrawFilledRectangle( dwX1, dwY2, dwX2, dwY2 ) ;
  639. LCD_DrawFilledRectangle( dwX1, dwY1, dwX1, dwY2 ) ;
  640. LCD_DrawFilledRectangle( dwX2, dwY1, dwX2, dwY2 ) ;
  641. return 0 ;
  642. }
  643. /**
  644. * \brief Set the backlight of the LCD (AAT3193).
  645. *
  646. * \param level Backlight brightness level [1..16], 1 means maximum brightness.
  647. */
  648. extern void LCD_SetBacklight( uint32_t level )
  649. {
  650. uint32_t i ;
  651. const Pin pPins[] = { BOARD_BACKLIGHT_PIN } ;
  652. /* Ensure valid level */
  653. level = (level < 1) ? 1 : level ;
  654. level = (level > 32) ? 32 : level ;
  655. /* Enable pins */
  656. PIO_PinConfigure( pPins, PIO_LISTSIZE( pPins ) ) ;
  657. /* Switch off backlight */
  658. PIO_PinClear( pPins ) ;
  659. i = 600 * (BOARD_MCK / 1000000) ; /* wait for at least 500us */
  660. while ( i-- ) ;
  661. /* Set new backlight level */
  662. for ( i = 0 ; i < level ; i++ )
  663. {
  664. PIO_PinClear( pPins ) ;
  665. PIO_PinClear( pPins ) ;
  666. PIO_PinClear( pPins ) ;
  667. PIO_PinSet( pPins ) ;
  668. PIO_PinSet( pPins ) ;
  669. PIO_PinSet( pPins ) ;
  670. }
  671. }
  672. /**
  673. * \brief Set the display fomat of the LCD to landscape(AAT3193).
  674. *
  675. * \param dwRGB The color value .
  676. */
  677. extern void LCD_SetDisplayLandscape( uint32_t dwRGB )
  678. {
  679. dwRGB = dwRGB;
  680. }
  681. /**
  682. * \brief Set the display fomat of the LCD to portrait(AAT3193).
  683. *
  684. * \param level The color value.
  685. */
  686. extern void LCD_SetDisplayPortrait( uint32_t dwRGB )
  687. {
  688. dwRGB = dwRGB;
  689. }