-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRAIO8870.cpp
More file actions
425 lines (339 loc) · 12.7 KB
/
RAIO8870.cpp
File metadata and controls
425 lines (339 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/*##############################################################*/
/* */
/* File : RAIO8870.c */
/* */
/* Project : TFT for Raspberry Pi Revision 2 */
/* */
/* Date : 2013-11-22 last update: 2014-08-26 */
/* */
/* Author : Hagen Ploog */
/* Kai Gillmann */
/* Timo Pfander */
/* */
/* IDE : Geany 1.22 */
/* Compiler : gcc (Debian 4.6.3-14+rpi1) 4.6.3 */
/* */
/* Copyright (C) 2013 admatec GmbH */
/* */
/* */
/* Description : */
/* */
/* This file contain several functions to initialize and */
/* control the graphic controller RAIO8870. */
/* */
/* */
/* License: */
/* */
/* This program is free software; you can redistribute it */
/* and/or modify it under the terms of the GNU General */
/* Public License as published by the Free Software */
/* Foundation; either version 3 of the License, or */
/* (at your option) any later version. */
/* */
/* This program is distributed in the hope that it will */
/* be useful, but WITHOUT ANY WARRANTY; without even the */
/* implied warranty of MERCHANTABILITY or */
/* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General */
/* Public License for more details. */
/* */
/* You should have received a copy of the GNU General */
/* Public License along with this program; if not, */
/* see <http://www.gnu.org/licenses/>. */
/* */
/* */
/* Revision History: */
/* */
/* Version 1.0 - Initial release */
/* Version 1.1 - added function RAIO_clear_screen( ... ) */
/* added function RAIO_SetFontSizeFactor( ... ) */
/* added function RAIO_print_text( ... ) */
/* Version 1.2 - added function RAIO_set_cursor( ... ) */
/* changed function RAIO_print_text( ... ) */
/* */
/* */
/* */
/*##############################################################*/
#include <bcm2835.h>
#include "RAIO8870.h"
#include "tft.h"
uint16_t txc = 0x00; // character x position on screen
uint16_t tyc = 0x00; // character y position on screen
uint8_t char_higth = 15; // character hight depends on character set
#ifdef CM_4K
static uint8_t BankNo_WR=0, BankNo_RD=1;
#endif
// write command to a register
// ----------------------------------------------------------
void RAIO_SetRegister( uint8_t reg, uint8_t value )
{
TFT_RegWrite( (uint16_t)reg );
TFT_DataWrite( (uint16_t)value );
}
// set PWM value for backlight
// ----------------------------------------------------------
void RAIO_SetBacklightPWMValue( uint8_t BL_value )
{
RAIO_SetRegister( P1CR, 0x88 ); // Enable PWM1 output devider 256
RAIO_SetRegister( P1DCR, BL_value ); // -> BL_vaue = 0 (0% PWM) - 255 (100% PWM)
}
// initialization of RAIO8870
// ----------------------------------------------------------
void RAIO_init( void )
{
static uint8_t PLL_Initial_Flag = 0;
// *************** PLL settings (System Clock)
if ( !PLL_Initial_Flag ) // wait until PLL is ready
{
PLL_Initial_Flag = 1; // set Flag to avoid repeated PLL init
RAIO_SetRegister( PLLC1, 0x07 ); // set sys_clk
bcm2835_delayMicroseconds( 200 );
RAIO_SetRegister( PLLC2, 0x03 ); // set sys_clk
bcm2835_delayMicroseconds( 200 );
RAIO_SetRegister( PWRR, 0x01 ); // Raio software reset ( bit 0 ) set
RAIO_SetRegister( PWRR, 0x00 ); // Raio software reset ( bit 0 ) set to 0
delay( 100 );
// *************** color modes (color depths)
#ifdef CM_65K
// System Configuration Register
RAIO_SetRegister( SYSR, 0x0A ); // digital TFT
// parallel data out
// no external memory
// 8bit memory data bus
// 16bpp 65K color
// 16bit MCU-interface (data)
RAIO_SetRegister( DPCR, 0x00 ); // one layer
#elif defined(CM_4K)
// System Configuration Register
RAIO_SetRegister( SYSR, 0x06 ); // digital TFT
// parallel data out
// no external memory
// 8bit memory data bus
// 12bpp 4K color
// 16bit MCU-interface (data)
RAIO_SetRegister( DPCR, 0x80 ); // two layers
RAIO_SetRegister( MWCR1, BankNo_WR );
RAIO_SetRegister( LTPR0, BankNo_RD );
#else
#error "color_mode not defined"
#endif
}
// *************** horizontal settings
// 0x27+1 * 8 = 320 pixel
RAIO_SetRegister( HDWR , (DISPLAY_WIDTH / 8) - 1 );
RAIO_SetRegister( HNDFTR, 0x02 ); // Horizontal Non-Display Period Fine Tuning
// HNDR , Horizontal Non-Display Period Bit[4:0]
// Horizontal Non-Display Period (pixels) = (HNDR + 1)*8
RAIO_SetRegister( HNDR, 0x03 ); // 0x06
RAIO_SetRegister( HSTR, 0x04 ); //HSTR , HSYNC Start Position[4:0], HSYNC Start Position(PCLK) = (HSTR + 1)*8 0x02
// HPWR , HSYNC Polarity ,The period width of HSYNC.
// 1xxxxxxx activ high 0xxxxxxx activ low
// HSYNC Width [4:0] HSYNC Pulse width
// (PCLK) = (HPWR + 1)*8
RAIO_SetRegister( HPWR, 0x03 ); // 0x00
// ********************* vertical settings
// 0x0EF +1 = 240 pixel
RAIO_SetRegister( VDHR0 , ( (DISPLAY_HEIGHT-1) & 0xFF ) );
RAIO_SetRegister( VDHR1 , ( (DISPLAY_HEIGHT-1) >> 8) );
// VNDR0 , Vertical Non-Display Period Bit [7:0]
// Vertical Non-Display area = (VNDR + 1)
// VNDR1 , Vertical Non-Display Period Bit [8]
// Vertical Non-Display area = (VNDR + 1)
RAIO_SetRegister( VNDR0, 0x10 );
RAIO_SetRegister( VNDR1, 0x00 );
// VPWR , VSYNC Polarity ,VSYNC Pulse Width[6:0]
// VSYNC , Pulse Width(PCLK) = (VPWR + 1)
RAIO_SetRegister( VPWR, 0x00 );
// *************** miscellaneous settings
// active Window
Active_Window( 0, DISPLAY_WIDTH-1, 0, DISPLAY_HEIGHT-1 );
// PCLK fetch data on rising edge
RAIO_SetRegister( PCLK, 0x00 );
// Backlight dimming
RAIO_SetBacklightPWMValue(50);
Text_Background_Color( COLOR_WHITE );
// memory clear with background color
RAIO_SetRegister( MCLR, 0x81 );
TFT_wait_for_raio();
RAIO_SetRegister( IODR, 0x07 );
RAIO_SetRegister( PWRR, 0x80 );
}
// set coordinates for active window
// ----------------------------------------------------------
void Active_Window( uint16_t XL, uint16_t XR , uint16_t YT, uint16_t YB )
{
union my_union number;
//setting active window X
number.value = XL;
RAIO_SetRegister( HSAW0, number.split.low );
RAIO_SetRegister( HSAW1, number.split.high );
number.value = XR;
RAIO_SetRegister( HEAW0, number.split.low );
RAIO_SetRegister( HEAW1, number.split.high );
//setting active window Y
number.value = YT;
RAIO_SetRegister( VSAW0, number.split.low );
RAIO_SetRegister( VSAW1, number.split.high );
number.value = YB;
RAIO_SetRegister( VEAW0, number.split.low );
RAIO_SetRegister( VEAW1, number.split.high );
}
// set cursor
// ----------------------------------------------------------
void RAIO_set_cursor( uint16_t pos_x ,uint16_t pos_y )
{
union my_union number;
number.value = pos_x;
RAIO_SetRegister( CURH0, number.split.low );
RAIO_SetRegister( CURH1, number.split.high );
number.value = pos_y;
RAIO_SetRegister( CURV0, number.split.low );
RAIO_SetRegister( CURV1, number.split.high );
}
// show the BMP picture on the TFT screen
// ----------------------------------------------------------
void RAIO_Write_Picture( uint16_t *data, uint32_t count )
{
TFT_RegWrite( MRWC );
TFT_DataMultiWrite( data, count);
#ifdef CM_4K
if ( BankNo_WR==0 )
{
BankNo_WR=1;
BankNo_RD=0;
}
else
{
BankNo_WR=0;
BankNo_RD=1;
}
RAIO_SetRegister( MWCR1, BankNo_WR );
RAIO_SetRegister( LTPR0, BankNo_RD );
#endif
}
// set mode for BET (Block Transfer Engine)
// ----------------------------------------------------------
void BTE_mode( uint8_t bte_operation, uint8_t rop_function )
{
RAIO_SetRegister( BECR1, bte_operation | (rop_function<<4) );
}
// set color
// ----------------------------------------------------------
void Text_Background_Color( uint8_t color )
{
RAIO_SetRegister( TBCR, color );
}
void Text_Foreground_Color( uint8_t color)
{
RAIO_SetRegister( TFCR, color);
}
// clear screen
// ----------------------------------------------------------
void RAIO_clear_screen( void )
{
// for more informations see RA8870 specification page 40
//
// | Bit | Function
// |-----|-------------------------------------------------
// | 7 | 0 = stop clear 1 = start clear
// | 6 | 0 = fullwindow 1 = activewindow
// | 5-1 | NA
// | 0 | 0 = Memory clear with BTE background color 1 = Memory clear with font background color
//
// Reg 0x43 define font background color ( RRRGGGBB )
// Reg 0x60, 0x61, 0x62 define BTE background color ( BGCR0=red[4:0], BGCR1=green[5:0], BGCR2=blue[4:0] )
RAIO_SetRegister( MCLR , 0x80 );
TFT_wait_for_raio();
}
// print text
// ----------------------------------------------------------
void RAIO_print_text( uint16_t pos_x, uint16_t pos_y, unsigned char *str, uint8_t BG_color, uint8_t FG_color )
{
// set cursor
RAIO_set_cursor( pos_x, pos_y );
// set color
Text_Background_Color( BG_color );
Text_Foreground_Color( FG_color );
// set text mode
RAIO_SetRegister( MWCR0, 0x80 );
// write text to display
TFT_RegWrite( MRWC );
while ( *str != '\0' )
{
TFT_DataWrite( *str );
++str;
TFT_wait_for_raio();
}
TFT_wait_for_raio();
// set graphic mode
RAIO_SetRegister( MWCR0, 0x00 );
}
// set font size
// ----------------------------------------------------------
void RAIO_SetFontSizeFactor( uint8_t size )
{
size = (size & 0x0f);
RAIO_SetRegister ( FNCR1, size );
}
// set coordinates for drawing line and square
// ----------------------------------------------------------
void Set_Geometric_Coordinate(uint16_t X1, uint16_t Y1 ,uint16_t X2 ,uint16_t Y2 )
{
union my_union number;
number.value = X1;
RAIO_SetRegister( DLHSR0, number.split.low );
RAIO_SetRegister( DLHSR1, number.split.high );
number.value = Y1;
RAIO_SetRegister( DLVSR0, number.split.low );
RAIO_SetRegister( DLVSR1, number.split.high );
number.value = X2;
RAIO_SetRegister( DLHER0, number.split.low );
RAIO_SetRegister( DLHER1, number.split.high );
number.value = Y2;
RAIO_SetRegister( DLVER0, number.split.low );
RAIO_SetRegister( DLVER1, number.split.high );
}
// set coordinates for drawing circle
// ----------------------------------------------------------
void Set_Geometric_Coordinate_circle (uint16_t X1, uint16_t Y1 ,uint8_t rad )
{
union my_union number;
number.value = X1;
RAIO_SetRegister( DCHR0, number.split.low );
RAIO_SetRegister( DCHR1, number.split.high );
number.value = Y1;
RAIO_SetRegister( DCVR0, number.split.low );
RAIO_SetRegister( DCVR1, number.split.high );
RAIO_SetRegister( DCRR, rad );
}
// set draw mode
// ----------------------------------------------------------
void RAIO_StartDrawing( int16_t whattodraw )
{
switch( whattodraw ) // -> see DRAW_MODES
{
case CIRCLE_NONFILL: {RAIO_SetRegister( DCR, 0x40 ); break;}
case CIRCLE_FILL: {RAIO_SetRegister( DCR, 0x60 ); break;}
case SQUARE_NONFILL: {RAIO_SetRegister( DCR, 0x90 ); break;}
case SQUARE_FILL: {RAIO_SetRegister( DCR, 0xB0 ); break;}
case LINE: {RAIO_SetRegister( DCR, 0x80 ); break;}
default: break;
}
TFT_wait_for_raio();
}
// draw some basic geometrical forms
// ----------------------------------------------------------
void Draw_Line( uint16_t X1, uint16_t Y1 ,uint16_t X2 ,uint16_t Y2 )
{
Set_Geometric_Coordinate( X1, Y1, X2, Y2 );
RAIO_StartDrawing( LINE );
}
void Draw_Square( uint16_t X1, uint16_t Y1 ,uint16_t X2 ,uint16_t Y2 )
{
Set_Geometric_Coordinate( X1, Y1, X2, Y2 );
RAIO_StartDrawing( SQUARE_NONFILL );
}
void Draw_Circle( uint16_t X1, uint16_t Y1 ,uint8_t rad )
{
Set_Geometric_Coordinate_circle ( X1, Y1, rad );
RAIO_StartDrawing( CIRCLE_NONFILL );
}