00001
00038 #ifndef DRIVERS_GFX_HX8347A_HX8347A_XMEGA_H_INCLUDED
00039 #define DRIVERS_GFX_HX8347A_HX8347A_XMEGA_H_INCLUDED
00040
00042 #if defined(__GNUC__)
00043 # include <avr/io.h>
00044 #elif defined(__ICCAVR__)
00045 # include <ioavr.h>
00046 #endif
00047
00048 #include <board/hx8347a.h>
00049
00050 #define gfx_select_chip() (GFX_CS_PORT.OUTCLR = GFX_CS_PINMASK)
00051 #define gfx_deselect_chip() (GFX_CS_PORT.OUTSET = GFX_CS_PINMASK)
00052
00053 #define gfx_wait_comms() \
00054 do {} while ((GFX_USART_MODULE.STATUS & USART_TXCIF_bm) == 0x00); \
00055 GFX_USART_MODULE.STATUS = USART_TXCIF_bm;
00056
00057 #define gfx_send_byte(value) \
00058 GFX_USART_MODULE.DATA = (value); \
00059 gfx_wait_comms();
00060
00061 #define gfx_send_dummy_byte() \
00062 gfx_send_byte(0xff);
00063
00064 #define gfx_read_byte() \
00065 (GFX_USART_MODULE.DATA)
00066
00067 #define gfx_enable_receive() \
00068 GFX_USART_MODULE.CTRLB |= USART_RXEN_bm;
00069
00070 #define gfx_disable_receive() \
00071 GFX_USART_MODULE.CTRLB &= ~USART_RXEN_bm;
00072
00074 static void gfx_select_register(uint8_t address)
00075 {
00076 gfx_select_chip();
00077 gfx_send_byte(HX8347A_START_WRITEIDX);
00078 gfx_send_byte(address);
00079 gfx_deselect_chip();
00080 }
00081
00082
00084 static void gfx_write_register(uint8_t address, uint8_t value)
00085 {
00086 gfx_select_register(address);
00087 gfx_select_chip();
00088 gfx_send_byte(HX8347A_START_WRITEREG);
00089 gfx_send_byte(value);
00090 gfx_deselect_chip();
00091 }
00092
00093
00095 static uint8_t gfx_read_register(uint8_t address)
00096 {
00097 uint8_t value;
00098
00099 gfx_select_register(address);
00100 gfx_select_chip();
00101 gfx_send_byte(HX8347A_START_READREG);
00102 gfx_enable_receive();
00103 gfx_send_dummy_byte();
00104 value = gfx_read_byte();
00105 gfx_disable_receive();
00106 gfx_deselect_chip();
00107
00108 return value;
00109 }
00110
00111 static gfx_color_t gfx_read_gram(void)
00112 {
00113 uint8_t red;
00114 uint8_t green;
00115 uint8_t blue;
00116
00117 gfx_select_register(HX8347A_SRAMWRITE);
00118 gfx_select_chip();
00119 gfx_send_byte(HX8347A_START_READREG);
00120 gfx_send_dummy_byte();
00121 gfx_enable_receive();
00122
00123 gfx_send_dummy_byte();
00124 red = gfx_read_byte();
00125 gfx_send_dummy_byte();
00126 green = gfx_read_byte();
00127 gfx_send_dummy_byte();
00128 blue = gfx_read_byte();
00129
00130 gfx_disable_receive();
00131 gfx_deselect_chip();
00132
00133
00134 return gfx_color(red, green, blue);
00135 }
00136
00137 static void gfx_write_gram(gfx_color_t color)
00138 {
00139 gfx_select_register(HX8347A_SRAMWRITE);
00140 gfx_select_chip();
00141 gfx_send_byte(HX8347A_START_WRITEREG);
00142 gfx_send_byte(color & 0xff);
00143 gfx_send_byte(color >> 8);
00144 gfx_deselect_chip();
00145 }
00146
00148 static void gfx_init_comms(void)
00149 {
00150
00151 sysclk_enable_module(SYSCLK_PORT_GEN, SYSCLK_DMA);
00152 DMA.CTRL = DMA_ENABLE_bm;
00153
00154 DMA.CH0.DESTADDR0 = ((uintptr_t)&(GFX_USART_MODULE.DATA)) & 0xff;
00155 DMA.CH0.DESTADDR1 = (((uintptr_t)&(GFX_USART_MODULE.DATA)) >> 8) & 0xff;
00156 DMA.CH0.DESTADDR2 =
00157 (((uint32_t)(uintptr_t)&(GFX_USART_MODULE.DATA)) >> 16) & 0xff;
00158
00159 sysclk_enable_module(SYSCLK_PORT_D, SYSCLK_USART1);
00160
00161 gfx_deselect_chip();
00162 GFX_CS_PORT.DIRSET = GFX_CS_PINMASK;
00163
00164
00165 GFX_USART_PORT.DIRSET = GFX_TXD_PINMASK;
00166
00167 GFX_USART_PORT.DIRCLR = GFX_RXD_PINMASK;
00168
00169 GFX_USART_PORT.DIRSET = GFX_XCK_PINMASK;
00170
00171 GFX_USART_MODULE.CTRLC = USART_CMODE_MSPI_gc;
00172 GFX_USART_MODULE.BAUDCTRLA = 1;
00173 GFX_USART_MODULE.CTRLB = USART_RXEN_bm | USART_TXEN_bm;
00174 }
00175
00177 static void gfx_setup_interface(void)
00178 {
00179
00180 port_select_gpio_pin(GFX_TE_PIN, PORT_DIR_INPUT);
00181
00182
00183
00184 TCF0.CTRLB |= TC0_CCAEN_bm | TC_WGMODE_DS_T_gc;
00185 TCF0.CCA = 75;
00186 TCF0.PER = 100;
00187 TCF0.CTRLA = TC_CLKSEL_DIV1024_gc;
00188
00189
00190 port_select_gpio_pin(GFX_BACKLIGHT_PIN,
00191 PORT_DIR_OUTPUT | PORT_INIT_HIGH);
00192
00193
00194 port_select_gpio_pin(GFX_RESET_PIN,
00195 PORT_DIR_OUTPUT | PORT_INIT_HIGH);
00196 }
00197
00198 void gfx_duplicate_pixel(gfx_color_t color, uint32_t count)
00199 {
00200
00201 assert((count >> 24) == 0);
00202 assert(count > 0);
00203
00204
00205 gfx_select_register(HX8347A_SRAMWRITE);
00206 gfx_select_chip();
00207 gfx_send_byte(HX8347A_START_WRITEREG);
00208
00209
00210 DMA.CH0.SRCADDR0 = ((uintptr_t)&color) & 0xff;
00211 DMA.CH0.SRCADDR1 = (((uintptr_t)&color) >> 8) & 0xff;
00212 DMA.CH0.SRCADDR2 = (((uint32_t)(uintptr_t)&color) >> 16) & 0xff;
00213
00214
00215 DMA.CH0.TRFCNT = sizeof(gfx_color_t);
00216
00217
00218 DMA.CH0.ADDRCTRL =
00219 (uint8_t)DMA_CH_SRCRELOAD_BLOCK_gc |
00220 (uint8_t)DMA_CH_SRCDIR_INC_gc |
00221 (uint8_t)DMA_CH_DESTRELOAD_NONE_gc |
00222 (uint8_t)DMA_CH_DESTDIR_FIXED_gc;
00223 DMA.CH0.TRIGSRC = GFX_USART_TRIGGER;
00224
00225
00226 while (count >= 255) {
00227 DMA.CH0.REPCNT = 255;
00228 DMA.CH0.CTRLA =
00229 DMA_CH_ENABLE_bm |
00230 DMA_CH_REPEAT_bm |
00231 DMA_CH_SINGLE_bm |
00232 DMA_CH_TRFREQ_bm |
00233 DMA_CH_BURSTLEN_1BYTE_gc;
00234
00235
00236 do {} while ((DMA.CH0.CTRLB & DMA_CH_CHBUSY_bm) != 0x00);
00237 gfx_wait_comms();
00238 count -= 255;
00239 }
00240
00241
00242 if ((count & 0xff) > 0) {
00243 DMA.CH0.REPCNT = count & 0xff;
00244 DMA.CH0.CTRLA =
00245 DMA_CH_ENABLE_bm |
00246 DMA_CH_REPEAT_bm |
00247 DMA_CH_SINGLE_bm |
00248 DMA_CH_TRFREQ_bm |
00249 DMA_CH_BURSTLEN_1BYTE_gc;
00250 do {} while ((DMA.CH0.CTRLB & DMA_CH_CHBUSY_bm) != 0x00);
00251 gfx_wait_comms();
00252 }
00253
00254 gfx_deselect_chip();
00255 }
00256
00257 void gfx_copy_pixels_to_screen(const gfx_color_t *pixels, uint32_t count)
00258 {
00259 uint32_t byte_count;
00260 uint16_t remainder_count;
00261 uint8_t block_count;
00262
00263
00264 assert(pixels != NULL);
00265 assert(count > 0);
00266
00267
00268 gfx_select_register(HX8347A_SRAMWRITE);
00269 gfx_select_chip();
00270 gfx_send_byte(HX8347A_START_WRITEREG);
00271
00272
00273 DMA.CH0.SRCADDR0 = ((uintptr_t)pixels) & 0xff;
00274 DMA.CH0.SRCADDR1 = (((uintptr_t)pixels) >> 8) & 0xff;
00275 DMA.CH0.SRCADDR2 = (((uint32_t)(uintptr_t)pixels) >> 16) & 0xff;
00276
00277
00278 DMA.CH0.ADDRCTRL =
00279 (uint8_t)DMA_CH_SRCRELOAD_NONE_gc |
00280 (uint8_t)DMA_CH_SRCDIR_INC_gc |
00281 (uint8_t)DMA_CH_DESTRELOAD_NONE_gc |
00282 (uint8_t)DMA_CH_DESTDIR_FIXED_gc;
00283 DMA.CH0.TRIGSRC = GFX_USART_TRIGGER;
00284
00285
00286 byte_count = count * sizeof(gfx_color_t);
00287 assert((byte_count >> 24) == 0x00);
00288 block_count = byte_count >> 16;
00289 remainder_count = byte_count & 0xffff;
00290
00291
00292 if (block_count > 0) {
00293 DMA.CH0.TRFCNT = 0;
00294 DMA.CH0.REPCNT = block_count;
00295 DMA.CH0.CTRLA =
00296 DMA_CH_ENABLE_bm |
00297 DMA_CH_REPEAT_bm |
00298 DMA_CH_SINGLE_bm |
00299 DMA_CH_BURSTLEN_1BYTE_gc;
00300 do {} while ((DMA.CH0.CTRLB & DMA_CH_CHBUSY_bm) != 0x00);
00301 gfx_wait_comms();
00302 }
00303
00304
00305 if (remainder_count > 0) {
00306 DMA.CH0.TRFCNT = remainder_count;
00307 DMA.CH0.CTRLA =
00308 DMA_CH_ENABLE_bm |
00309 DMA_CH_SINGLE_bm |
00310 DMA_CH_BURSTLEN_1BYTE_gc;
00311 do {} while ((DMA.CH0.CTRLB & DMA_CH_CHBUSY_bm) != 0x00);
00312 gfx_wait_comms();
00313 }
00314
00315 gfx_deselect_chip();
00316 }
00317
00318 void gfx_copy_progmem_pixels_to_screen(const gfx_color_t __progmem_arg *pixels,
00319 uint32_t count)
00320 {
00321 uint32_t byte_count;
00322 uint8_t __progmem_arg *byte_ptr;
00323
00324
00325 assert(pixels != NULL);
00326 assert(count > 0);
00327
00328
00329 gfx_select_register(HX8347A_SRAMWRITE);
00330 gfx_select_chip();
00331 gfx_send_byte(HX8347A_START_WRITEREG);
00332
00333
00334 byte_count = count * sizeof(gfx_color_t);
00335 byte_ptr = (uint8_t __progmem_arg *)pixels;
00336
00337 while (byte_count > 0) {
00338 gfx_send_byte(progmem_read8(byte_ptr));
00339 ++byte_ptr;
00340 --byte_count;
00341 }
00342
00343 gfx_deselect_chip();
00344 }
00345
00346 void gfx_copy_hugemem_pixels_to_screen(const hugemem_ptr_t pixels,
00347 uint32_t count)
00348 {
00349 uint32_t byte_count;
00350 hugemem_ptr_t byte_ptr;
00351
00352
00353 assert(pixels);
00354 assert(count > 0);
00355
00356
00357 gfx_select_register(HX8347A_SRAMWRITE);
00358 gfx_select_chip();
00359 gfx_send_byte(HX8347A_START_WRITEREG);
00360
00361
00362 byte_count = count * sizeof(gfx_color_t);
00363 byte_ptr = pixels;
00364
00365 while (byte_count > 0) {
00366 gfx_send_byte(hugemem_read8(byte_ptr));
00367 byte_ptr = (hugemem_ptr_t)((uint32_t)byte_ptr + 1);
00368 --byte_count;
00369 }
00370
00371 gfx_deselect_chip();
00372 }
00373
00374 void gfx_copy_pixels_from_screen(gfx_color_t *pixels, uint32_t count)
00375 {
00376
00377 assert(pixels != NULL);
00378 assert(count > 0);
00379
00380
00381 gfx_select_register(HX8347A_SRAMWRITE);
00382 gfx_select_chip();
00383 gfx_send_byte(HX8347A_START_READREG);
00384 gfx_send_dummy_byte();
00385 gfx_enable_receive();
00386
00387
00388 while (count > 0) {
00389 uint8_t red;
00390 uint8_t green;
00391 uint8_t blue;
00392
00393 gfx_send_dummy_byte();
00394 red = gfx_read_byte();
00395 gfx_send_dummy_byte();
00396 green = gfx_read_byte();
00397 gfx_send_dummy_byte();
00398 blue = gfx_read_byte();
00399
00400 *pixels = gfx_color(red, green, blue);
00401
00402 ++pixels;
00403 --count;
00404 }
00405
00406 gfx_disable_receive();
00407 gfx_deselect_chip();
00408 }
00409
00410 #endif