commit c94d52b9054c24ea49f84dff56a647635ac36365 Author: stephan Date: Sat Aug 20 21:27:30 2022 +0200 updated code to raspi 3 and newer versions of gcc diff --git a/HW/Schematic_C-BerryTFTAdapter.pdf b/HW/Schematic_C-BerryTFTAdapter.pdf new file mode 100644 index 0000000..a4805c0 Binary files /dev/null and b/HW/Schematic_C-BerryTFTAdapter.pdf differ diff --git a/SW/bmp/admatec.bmp b/SW/bmp/admatec.bmp new file mode 100644 index 0000000..3492a1c Binary files /dev/null and b/SW/bmp/admatec.bmp differ diff --git a/SW/fb2cberry/RAIO8870.h b/SW/fb2cberry/RAIO8870.h new file mode 100644 index 0000000..822fdb2 --- /dev/null +++ b/SW/fb2cberry/RAIO8870.h @@ -0,0 +1,401 @@ +/*##############################################################*/ +/* */ +/* File : RAIO8870.h */ +/* */ +/* Project : TFT for Raspberry Pi Revision 2 */ +/* */ +/* Date : 2014-08-14 last update: 2014-08-14 */ +/* */ +/* 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 contains the defines of the RAIO register. */ +/* Furthermore declared the file several functions to control */ +/* the graphic controller RAIO8870. */ +/* */ +/* There can two different color depths to be set for the BMP */ +/* output. It can be changed behind the line color modes. */ +/* */ +/* */ +/* 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 . */ +/* */ +/* */ +/* Revision History: */ +/* */ +/* Version 1.0 - Initial release */ +/* */ +/* */ +/* */ +/*##############################################################*/ + +#ifndef RAIO8870_H +#define RAIO8870_H + +#include + + +//touch +#define touch_available + + +//color modes (color depths) { CM_4K=0, CM_65K }; +#define CM_65K + + +// TFT dimensions +#define DISPLAY_WIDTH 320 +#define DISPLAY_HEIGHT 240 +#define PICTURE_PIXELS ( DISPLAY_WIDTH*DISPLAY_HEIGHT ) + + +// RAIO register -> see datasheet RAIO8870 +#define PCOD 0x00 +#define PWRR 0x01 +#define MRWC 0x02 +#define PCLK 0x04 + +#define SYSR 0x10 +#define DRGB 0x11 +#define IOCR 0x12 +#define IODR 0x13 + +#define HDWR 0x14 +#define HNDFTR 0x15 +#define HNDR 0x16 +#define HSTR 0x17 +#define HPWR 0x18 + +#define VDHR0 0x19 +#define VDHR1 0x1a +#define VNDR0 0x1b +#define VNDR1 0x1c +#define VSTR0 0x1d +#define VSTR1 0x1e +#define VPWR 0x1f + +#define DPCR 0x20 +#define FNCR0 0x21 +#define FNCR1 0x22 +#define CGSR 0x23 +#define HOFS0 0x24 +#define HOFS1 0x25 +#define VOFS0 0x26 +#define VOFS1 0x27 +#define ROMS 0x28 + +#define FLDR 0x29 + +#define HSAW0 0x30 +#define HSAW1 0x31 +#define VSAW0 0x32 +#define VSAW1 0x33 +#define HEAW0 0x34 +#define HEAW1 0x35 +#define VEAW0 0x36 +#define VEAW1 0x37 +#define HSSW0 0x38 +#define HSSW1 0x39 +#define VSSW0 0x3a +#define VSSW1 0x3b +#define HESW0 0x3c +#define HESW1 0x3d +#define VESW0 0x3e +#define VESW1 0x3f + +#define MWCR0 0x40 +#define MWCR1 0x41 +#define TFCR 0x42 +#define TBCR 0x43 +#define BTCR 0x44 +#define CURS 0x45 +#define CURH0 0x46 +#define CURH1 0x47 +#define CURV0 0x48 +#define CURV1 0x49 +#define RCURH0 0x4a +#define RCURH01 0x4b +#define RCURV0 0x4c +#define RCURV1 0x4d +#define MRCD 0x4e +#define BECR0 0x50 +#define BECR1 0x51 +#define LTPR0 0x52 +#define LTPR1 0x53 +#define HSBE0 0x54 +#define HSBE1 0x55 +#define VSBE0 0x56 +#define VSBE1 0x57 +#define HDBE0 0x58 +#define HDBE1 0x59 +#define VDBE0 0x5a +#define VDBE1 0x5b +#define BEWR0 0x5c +#define BEWR1 0x5d +#define BEHR0 0x5e +#define BEHR1 0x5f + +#define BGCR0 0x60 +#define BGCR1 0x61 +#define BGCR2 0x62 +#define FGCR0 0x63 +#define FGCR1 0x64 +#define FGCR2 0x65 +#define PTNO 0x66 +#define BGTR 0x67 + +#define TPCR0 0x70 +#define TPCR1 0x71 +#define TPXH 0x72 +#define TPYH 0x73 +#define TPXYL 0x74 + +#define GCHP0 0x80 +#define GCHP1 0x81 +#define GCVP0 0x82 +#define GCVP1 0x83 +#define GCC0 0x84 +#define GCC1 0x85 + +#define PLLC1 0x88 +#define PLLC2 0x89 + +#define P1CR 0x8a +#define P1DCR 0x8b +#define P2CR 0x8c +#define P2DCR 0x8d +#define MCLR 0x8e +#define INTC 0x8f + +#define DCR 0x90 +#define DLHSR0 0x91 +#define DLHSR1 0x92 +#define DLVSR0 0x93 +#define DLVSR1 0x94 +#define DLHER0 0x95 +#define DLHER1 0x96 +#define DLVER0 0x97 +#define DLVER1 0x98 +#define DCHR0 0x99 +#define DCHR1 0x9a +#define DCVR0 0x9b +#define DCVR1 0x9c +#define DCRR 0x9d + +#define TCR1 0xa0 +#define TCR2 0xa1 +#define OEHTCR1 0xa2 +#define OEHTCR2 0xa3 +#define OEHTCR3 0xa4 +#define OEHTCR4 0xa5 +#define OEHTCR5 0xa6 +#define OEHTCR6 0xa7 +#define OEHTCR7 0xa8 +#define OEHTCR8 0xa9 + +#define STHTCR1 0xaa +#define STHTCR2 0xab +#define STHTCR3 0xac +#define STHTCR4 0xad + +#define Q1HCR1 0xae +#define Q1HCR2 0xaf + +#define OEVTCR1 0xb0 +#define OEVTCR2 0xb1 +#define OEVTCR3 0xb2 +#define OEVTCR4 0xb3 +#define CKVTCR1 0xb4 +#define CKVTCR2 0xb5 +#define CKVTCR3 0xb6 +#define CKVTCR4 0xb7 +#define STVTCR1 0xb8 +#define STVTCR2 0xb9 +#define STVTCR3 0xba +#define STVTCR4 0xbb +#define STVTCR5 0xbc +#define STVTCR6 0xbd +#define STVTCR7 0xbe +#define STVTCR8 0xbf + +#define COMTCR1 0xc0 +#define COMTCR2 0xc1 +#define RGBTCR1 0xc2 +#define RGBTCR2 0xc3 + + +// colors "RRRGGGBB" +#define COLOR_RED 0xE0 +#define COLOR_BLUE 0x03 +#define COLOR_GREEN 0x1C +#define COLOR_BLACK 0x00 +#define COLOR_WHITE 0xFF +#define COLOR_CYAN 0x1F +#define COLOR_YELLOW 0xFC +#define COLOR_MAGENTA 0xE3 +#define COLOR_DARK_GREEN 0x0C + + +// ROP functions +#define ROP_SOURCE 0xC + + +// BTE operation functions +#define BTE_MOVE_POSITIVE 0x02 +#define BTE_SOLID_FILL 0x0C + + +// declaration of a union (used in RAIO8870.c and tft.c) +// ---------------------------------------------------------- +union my_union +{ + uint32_t value; + struct + { + unsigned char low; + unsigned char high; + } split; +}; + + +#ifdef touch_available + + // definition of filter size + #define debounce_buffer_size 4 + extern uint32_t low_pass_x[debounce_buffer_size-1], low_pass_y[debounce_buffer_size-1]; + + // declaration of touch variable + extern uint8_t touch_buffer_full; + extern uint8_t low_pass_pointer; + + + // enumeration of touch modes + enum TOUCH_FUNCTIONS { down, pressed, up, no_touch }; + + // declaration of touch structure + struct touch + { + uint8_t state; + uint32_t touch_x, touch_y; + }; + + extern struct touch my_touch; + +#endif + + +// enumeration of drawing modes +// ---------------------------------------------------------- +enum DRAW_MODES { CIRCLE_NONFILL, CIRCLE_FILL, SQUARE_NONFILL, SQUARE_FILL, LINE}; + + +// initialization of RAIO8870 +// ---------------------------------------------------------- +void RAIO_init( void ); + + +// write command to a register +// ---------------------------------------------------------- +void RAIO_SetRegister( uint8_t reg, uint8_t value ); + + +// read data from a register +// ---------------------------------------------------------- +uint8_t RAIO_GetRegister( uint8_t reg ); + + +// set PWM value for backlight -> 0 (0% PWM) - 255 (100% PWM) +// ---------------------------------------------------------- +void RAIO_SetBacklightPWMValue( uint8_t BL_value ); + + +// set coordinates for active window +// ---------------------------------------------------------- +void Active_Window( uint16_t XL,uint16_t XR ,uint16_t YT ,uint16_t YB); + + +// set cursor +// ---------------------------------------------------------- +void RAIO_set_cursor( uint16_t pos_x ,uint16_t pos_y ); + + +// set mode for BET (Block Transfer Engine) +// ---------------------------------------------------------- +void BTE_mode( uint8_t bte_operation, uint8_t rop_function ); + + +// set color -> see color defines +// ---------------------------------------------------------- +void Text_Background_Color( uint8_t color ); +void Text_Foreground_Color( uint8_t color ); + + +// clear memory +// ---------------------------------------------------------- +void RAIO_clear_screen( void ); + + +// set coordinates for drawing +// ---------------------------------------------------------- +void Set_Geometric_Coordinate( uint16_t X1, uint16_t Y1 ,uint16_t X2 ,uint16_t Y2 ); +void Set_Geometric_Coordinate_circle (uint16_t X1, uint16_t Y1 ,uint8_t rad ); + +// show the BMP picture on the TFT screen +// ---------------------------------------------------------- +void RAIO_Write_Picture( uint16_t *data, uint32_t count ); + + +// set draw mode -> see DRAW_MODES +// ---------------------------------------------------------- +void RAIO_StartDrawing( int16_t whattodraw ); + + +// draw some basic geometrical forms +// ---------------------------------------------------------- +void Draw_Line( uint16_t X1, uint16_t Y1 ,uint16_t X2 ,uint16_t Y2 ); +void Draw_Square( uint16_t X1, uint16_t Y1 ,uint16_t X2 ,uint16_t Y2 ); + + +// 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 font size +// ---------------------------------------------------------- +void RAIO_SetFontSizeFactor( uint8_t size ); + + + +// get touch coordinates +// ---------------------------------------------------------- +#ifdef touch_available + int32_t RAIO_gettouch(); +#endif + + +#endif diff --git a/SW/fb2cberry/fb2cberry.c b/SW/fb2cberry/fb2cberry.c new file mode 100644 index 0000000..592476f --- /dev/null +++ b/SW/fb2cberry/fb2cberry.c @@ -0,0 +1,121 @@ +/* + * my_fbcopy.c + * + */ + +#include +#include +#include + +#include + +#include "tft.h" +#include "RAIO8870.h" + + +int main(int argc, char **argv) +{ + DISPMANX_DISPLAY_HANDLE_T main_display_handle; + DISPMANX_RESOURCE_HANDLE_T screen_resource_handle; + + VC_RECT_T rectangle; + + int ret; + uint32_t image_prt; + uint16_t image[2][ PICTURE_PIXELS ]; + uint32_t current_frame = 0, old_frame=1; + uint32_t i, x, y, start, end, count; + uint16_t *p_current_frame, *p_old_frame, *p_start; + + bcm_host_init(); + + if ( !bcm2835_init() ) + return ( -1 ); + + TFT_init_board(); + + TFT_hard_reset(); + + RAIO_init(); + + //clear old image + p_old_frame = &image[ old_frame ][0]; + for(x=0; x<76800; x++) + { + *p_old_frame++ = 0x1111; + } + + + // open main framebuffer device + main_display_handle = vc_dispmanx_display_open( 0 ); + if ( !main_display_handle ) + { + printf("\n Unable to open primary display"); + return( -1 ); + } + + // now build up the shadow area for RAIO + screen_resource_handle = vc_dispmanx_resource_create( VC_IMAGE_RGB565, DISPLAY_WIDTH, DISPLAY_HEIGHT, &image_prt ); + if ( !screen_resource_handle ) + { + printf("\n Unable to create screen buffer"); + vc_dispmanx_display_close( main_display_handle ); + return ( -1 ); + } + + vc_dispmanx_rect_set( &rectangle, 0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT ); + + while( 1 ) + { + ret = vc_dispmanx_snapshot( main_display_handle, screen_resource_handle, DISPMANX_NO_ROTATE ); + + vc_dispmanx_resource_read_data( screen_resource_handle, &rectangle, &image[ current_frame ][0], DISPLAY_WIDTH*2 ); + + + i=0; + p_current_frame = &image[ current_frame ][ 0 ]; + p_old_frame = &image[ old_frame ][ 0 ]; + + while (i 10 fps + usleep( 100 * 1000 ); + + } + + + return ( 0 ); +} + diff --git a/SW/fb2cberry/makefile b/SW/fb2cberry/makefile new file mode 100644 index 0000000..9a81301 --- /dev/null +++ b/SW/fb2cberry/makefile @@ -0,0 +1,21 @@ + +#include the Broadcom hardware interface library +XINCDIR +=-I$(ROOTDIR)/opt/vc/include +XINCDIR +=-I$(ROOTDIR)/opt/vc/include/interface/vmcs_host/linux +XINCDIR +=-I$(ROOTDIR)/opt/vc/include/interface/vcos/pthreads -DRPI=1 +XLIBS += -L/opt/vc/lib/ -lbcm_host + + +all: fb2cberry + + +fb2cberry: fb2cberry.o + gcc $(XINCDIR) $(LIBS) fb2cberry.o tft.o RAIO8870.o ${XLIBS} -lbcm2835 -lrt -lm -o fb2cberry + + +fb2cberry.o: fb2cberry.c + gcc $(XLIBS) $(XINCDIR) $(LIBS) -Os -c fb2cberry.c + +clean: + rm -rf *o fb2cberry + diff --git a/SW/fb2cberry/tft.h b/SW/fb2cberry/tft.h new file mode 100644 index 0000000..bdb5cfc --- /dev/null +++ b/SW/fb2cberry/tft.h @@ -0,0 +1,120 @@ +/*##############################################################*/ +/* */ +/* File : tft.h */ +/* */ +/* Project : TFT for Raspberry Pi Revision 2 */ +/* */ +/* Date : 2014-08-14 last update: 2014-08-14 */ +/* */ +/* 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 declared functions for the SPI communications */ +/* between the Raspberry Pi and the TFT and for the */ +/* initialization of the GPIO Pins of the Raspberry Pi. */ +/* */ +/* */ +/* 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 . */ +/* */ +/* */ +/* Revision History: */ +/* */ +/* Version 1.0 - Initial release */ +/* */ +/* */ +/* */ +/*##############################################################*/ + +#ifndef TFT_H +#define TFT_H + +#include +#include "RAIO8870.h" + + +// define pins on connector P1 +#define MOSI RPI_V2_GPIO_P1_19 +#define MISO RPI_V2_GPIO_P1_21 +#define SCLK RPI_V2_GPIO_P1_23 +#define OE RPI_V2_GPIO_P1_11 +#define SPI_CE1 RPI_V2_GPIO_P1_26 +#define RAIO_RS RPI_V2_GPIO_P1_12 +#define RAIO_RST RPI_V2_GPIO_P1_22 +#define RAIO_CS RPI_V2_GPIO_P1_24 +#define RAIO_WR RPI_V2_GPIO_P1_18 +#define RAIO_RD RPI_V2_GPIO_P1_16 +#define RAIO_WAIT RPI_V2_GPIO_P1_15 +#define RAIO_INT RPI_V2_GPIO_P1_13 + +#define RAIO_WRpin 24 + + +// declaration of a union (see RAIO8870.h) +// ---------------------------------------------------------- +extern union my_union number; + + +// initialization of GPIO and SPI +// ---------------------------------------------------------- +void TFT_init_board( void ); + + +// hard reset of the graphic controller and the tft +// ---------------------------------------------------------- +void TFT_hard_reset( void ); + + +// wait during raio is busy +// ---------------------------------------------------------- +void TFT_wait_for_raio ( void ); + + +// write byte to register +// ---------------------------------------------------------- +void TFT_RegWrite( uint16_t reg ); + + +// write byte to tft +// ---------------------------------------------------------- +void TFT_DataWrite( uint16_t data ); + + +// read byte from tft +// ---------------------------------------------------------- +uint16_t TFT_DataRead( ); + + +// read RAIO status +uint16_t TFT_StatusRead(); + + +// write 'count'-bytes to tft +// ---------------------------------------------------------- +void TFT_DataMultiWrite( uint16_t *data, uint32_t count ); + +#endif diff --git a/SW/tft_test/RAIO8870.c b/SW/tft_test/RAIO8870.c new file mode 100644 index 0000000..66f014d --- /dev/null +++ b/SW/tft_test/RAIO8870.c @@ -0,0 +1,538 @@ +/*##############################################################*/ +/* */ +/* File : RAIO8870.c */ +/* */ +/* Project : TFT for Raspberry Pi Revision 2 */ +/* */ +/* Date : 2014-08-14 last update: 2014-08-14 */ +/* */ +/* 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 . */ +/* */ +/* */ +/* Revision History: */ +/* */ +/* Version 1.0 - Initial release */ +/* */ +/* */ +/* */ +/*##############################################################*/ + +#include +#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 + +struct touch my_touch; +uint8_t touch_buffer_full; +uint32_t low_pass_x[3], low_pass_y[3]; +uint8_t low_pass_pointer; + + +// write command to a register +// ---------------------------------------------------------- +void RAIO_SetRegister( uint8_t reg, uint8_t value ) +{ + TFT_RegWrite( (uint16_t)reg ); + TFT_DataWrite( (uint16_t)value ); +} + + +// read data from a register +// ---------------------------------------------------------- +uint8_t RAIO_GetRegister( uint8_t reg ) +{ + uint8_t value; + + TFT_RegWrite( (uint16_t)reg ); + + value = TFT_DataRead(); + + return ( 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 ); + // Horizontal Non-Display Period Fine Tuning + RAIO_SetRegister( HNDFTR, 0x02 ); + + // HNDR , Horizontal Non-Display Period Bit[4:0] + // Horizontal Non-Display Period (pixels) = (HNDR + 1)*8 + RAIO_SetRegister( HNDR, 0x03 ); + // HSTR , HSYNC Start Position[4:0], HSYNC Start Position(PCLK) = (HSTR + 1)*8 0x02 + RAIO_SetRegister( HSTR, 0x04 ); + + // 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 ); + + + // ********************* 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); + + // memory clear with background color + Text_Background_Color( COLOR_WHITE ); + RAIO_SetRegister( MCLR, 0x81 ); + TFT_wait_for_raio(); + + RAIO_SetRegister( IODR, 0x07 ); + RAIO_SetRegister( PWRR, 0x80 ); + + +#ifdef touch_available + + // Touch Panel enable + // wait 4096 system clocks period + // ADC clock = system clock / 16 + RAIO_SetRegister( TPCR0, 0xB7 ); + + // 4wire, auto mode, internal vref enabled + // debounce enabled, idle mode + RAIO_SetRegister( TPCR1, 0x84 ); + + // enable touch interrupt + RAIO_SetRegister( INTC, 0x40 ); + + // init touch structure + my_touch.state = no_touch; + + // init touch values + touch_buffer_full = 0; + low_pass_pointer = 0; + +#endif +} + +#ifdef touch_available + +// get touch coords +// ---------------------------------------------------------- +int32_t RAIO_gettouch() +{ + uint8_t mask; + uint8_t dummy_tp; + uint32_t touch_x, touch_y; + + mask= RAIO_GetRegister( INTC ); + + if ( mask & 0x04 ) + { + // read the data for x and y + touch_x = RAIO_GetRegister ( TPXH ); + touch_y = RAIO_GetRegister ( TPYH ); + + // fill low pass filter with the new values + low_pass_x[ low_pass_pointer ] = touch_x; + low_pass_y[ low_pass_pointer ] = touch_y; + low_pass_pointer++; + + + if (low_pass_pointer == debounce_buffer_size) + { + low_pass_pointer = 0; + touch_buffer_full = 1; + } + + // calculate the average + my_touch.touch_x = (low_pass_x[0] + low_pass_x[1] + low_pass_x[2] + low_pass_x[3] ) >> 2; + my_touch.touch_y = (low_pass_y[0] + low_pass_y[1] + low_pass_y[2] + low_pass_y[3] ) >> 2; + + + if (touch_buffer_full == 1) + { + switch (my_touch.state) + { + case down : my_touch.state= pressed; + break; + case no_touch : my_touch.state = down; + break; + default : break; + } + } + + // clear touch irq + mask &= 0xf4 ; + RAIO_SetRegister( INTC, mask ); + + return ( 1 ); + } + else + { + switch (my_touch.state) + { + case up : my_touch.state = no_touch; + break; + case pressed: my_touch.state = up; + break; + default : break; + } + + low_pass_pointer = 0; + touch_buffer_full = 0; + return ( 0 ); + } +} + +#endif + + + +// 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 , 0x81 ); + 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 ); +} + diff --git a/SW/tft_test/RAIO8870.h b/SW/tft_test/RAIO8870.h new file mode 100644 index 0000000..822fdb2 --- /dev/null +++ b/SW/tft_test/RAIO8870.h @@ -0,0 +1,401 @@ +/*##############################################################*/ +/* */ +/* File : RAIO8870.h */ +/* */ +/* Project : TFT for Raspberry Pi Revision 2 */ +/* */ +/* Date : 2014-08-14 last update: 2014-08-14 */ +/* */ +/* 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 contains the defines of the RAIO register. */ +/* Furthermore declared the file several functions to control */ +/* the graphic controller RAIO8870. */ +/* */ +/* There can two different color depths to be set for the BMP */ +/* output. It can be changed behind the line color modes. */ +/* */ +/* */ +/* 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 . */ +/* */ +/* */ +/* Revision History: */ +/* */ +/* Version 1.0 - Initial release */ +/* */ +/* */ +/* */ +/*##############################################################*/ + +#ifndef RAIO8870_H +#define RAIO8870_H + +#include + + +//touch +#define touch_available + + +//color modes (color depths) { CM_4K=0, CM_65K }; +#define CM_65K + + +// TFT dimensions +#define DISPLAY_WIDTH 320 +#define DISPLAY_HEIGHT 240 +#define PICTURE_PIXELS ( DISPLAY_WIDTH*DISPLAY_HEIGHT ) + + +// RAIO register -> see datasheet RAIO8870 +#define PCOD 0x00 +#define PWRR 0x01 +#define MRWC 0x02 +#define PCLK 0x04 + +#define SYSR 0x10 +#define DRGB 0x11 +#define IOCR 0x12 +#define IODR 0x13 + +#define HDWR 0x14 +#define HNDFTR 0x15 +#define HNDR 0x16 +#define HSTR 0x17 +#define HPWR 0x18 + +#define VDHR0 0x19 +#define VDHR1 0x1a +#define VNDR0 0x1b +#define VNDR1 0x1c +#define VSTR0 0x1d +#define VSTR1 0x1e +#define VPWR 0x1f + +#define DPCR 0x20 +#define FNCR0 0x21 +#define FNCR1 0x22 +#define CGSR 0x23 +#define HOFS0 0x24 +#define HOFS1 0x25 +#define VOFS0 0x26 +#define VOFS1 0x27 +#define ROMS 0x28 + +#define FLDR 0x29 + +#define HSAW0 0x30 +#define HSAW1 0x31 +#define VSAW0 0x32 +#define VSAW1 0x33 +#define HEAW0 0x34 +#define HEAW1 0x35 +#define VEAW0 0x36 +#define VEAW1 0x37 +#define HSSW0 0x38 +#define HSSW1 0x39 +#define VSSW0 0x3a +#define VSSW1 0x3b +#define HESW0 0x3c +#define HESW1 0x3d +#define VESW0 0x3e +#define VESW1 0x3f + +#define MWCR0 0x40 +#define MWCR1 0x41 +#define TFCR 0x42 +#define TBCR 0x43 +#define BTCR 0x44 +#define CURS 0x45 +#define CURH0 0x46 +#define CURH1 0x47 +#define CURV0 0x48 +#define CURV1 0x49 +#define RCURH0 0x4a +#define RCURH01 0x4b +#define RCURV0 0x4c +#define RCURV1 0x4d +#define MRCD 0x4e +#define BECR0 0x50 +#define BECR1 0x51 +#define LTPR0 0x52 +#define LTPR1 0x53 +#define HSBE0 0x54 +#define HSBE1 0x55 +#define VSBE0 0x56 +#define VSBE1 0x57 +#define HDBE0 0x58 +#define HDBE1 0x59 +#define VDBE0 0x5a +#define VDBE1 0x5b +#define BEWR0 0x5c +#define BEWR1 0x5d +#define BEHR0 0x5e +#define BEHR1 0x5f + +#define BGCR0 0x60 +#define BGCR1 0x61 +#define BGCR2 0x62 +#define FGCR0 0x63 +#define FGCR1 0x64 +#define FGCR2 0x65 +#define PTNO 0x66 +#define BGTR 0x67 + +#define TPCR0 0x70 +#define TPCR1 0x71 +#define TPXH 0x72 +#define TPYH 0x73 +#define TPXYL 0x74 + +#define GCHP0 0x80 +#define GCHP1 0x81 +#define GCVP0 0x82 +#define GCVP1 0x83 +#define GCC0 0x84 +#define GCC1 0x85 + +#define PLLC1 0x88 +#define PLLC2 0x89 + +#define P1CR 0x8a +#define P1DCR 0x8b +#define P2CR 0x8c +#define P2DCR 0x8d +#define MCLR 0x8e +#define INTC 0x8f + +#define DCR 0x90 +#define DLHSR0 0x91 +#define DLHSR1 0x92 +#define DLVSR0 0x93 +#define DLVSR1 0x94 +#define DLHER0 0x95 +#define DLHER1 0x96 +#define DLVER0 0x97 +#define DLVER1 0x98 +#define DCHR0 0x99 +#define DCHR1 0x9a +#define DCVR0 0x9b +#define DCVR1 0x9c +#define DCRR 0x9d + +#define TCR1 0xa0 +#define TCR2 0xa1 +#define OEHTCR1 0xa2 +#define OEHTCR2 0xa3 +#define OEHTCR3 0xa4 +#define OEHTCR4 0xa5 +#define OEHTCR5 0xa6 +#define OEHTCR6 0xa7 +#define OEHTCR7 0xa8 +#define OEHTCR8 0xa9 + +#define STHTCR1 0xaa +#define STHTCR2 0xab +#define STHTCR3 0xac +#define STHTCR4 0xad + +#define Q1HCR1 0xae +#define Q1HCR2 0xaf + +#define OEVTCR1 0xb0 +#define OEVTCR2 0xb1 +#define OEVTCR3 0xb2 +#define OEVTCR4 0xb3 +#define CKVTCR1 0xb4 +#define CKVTCR2 0xb5 +#define CKVTCR3 0xb6 +#define CKVTCR4 0xb7 +#define STVTCR1 0xb8 +#define STVTCR2 0xb9 +#define STVTCR3 0xba +#define STVTCR4 0xbb +#define STVTCR5 0xbc +#define STVTCR6 0xbd +#define STVTCR7 0xbe +#define STVTCR8 0xbf + +#define COMTCR1 0xc0 +#define COMTCR2 0xc1 +#define RGBTCR1 0xc2 +#define RGBTCR2 0xc3 + + +// colors "RRRGGGBB" +#define COLOR_RED 0xE0 +#define COLOR_BLUE 0x03 +#define COLOR_GREEN 0x1C +#define COLOR_BLACK 0x00 +#define COLOR_WHITE 0xFF +#define COLOR_CYAN 0x1F +#define COLOR_YELLOW 0xFC +#define COLOR_MAGENTA 0xE3 +#define COLOR_DARK_GREEN 0x0C + + +// ROP functions +#define ROP_SOURCE 0xC + + +// BTE operation functions +#define BTE_MOVE_POSITIVE 0x02 +#define BTE_SOLID_FILL 0x0C + + +// declaration of a union (used in RAIO8870.c and tft.c) +// ---------------------------------------------------------- +union my_union +{ + uint32_t value; + struct + { + unsigned char low; + unsigned char high; + } split; +}; + + +#ifdef touch_available + + // definition of filter size + #define debounce_buffer_size 4 + extern uint32_t low_pass_x[debounce_buffer_size-1], low_pass_y[debounce_buffer_size-1]; + + // declaration of touch variable + extern uint8_t touch_buffer_full; + extern uint8_t low_pass_pointer; + + + // enumeration of touch modes + enum TOUCH_FUNCTIONS { down, pressed, up, no_touch }; + + // declaration of touch structure + struct touch + { + uint8_t state; + uint32_t touch_x, touch_y; + }; + + extern struct touch my_touch; + +#endif + + +// enumeration of drawing modes +// ---------------------------------------------------------- +enum DRAW_MODES { CIRCLE_NONFILL, CIRCLE_FILL, SQUARE_NONFILL, SQUARE_FILL, LINE}; + + +// initialization of RAIO8870 +// ---------------------------------------------------------- +void RAIO_init( void ); + + +// write command to a register +// ---------------------------------------------------------- +void RAIO_SetRegister( uint8_t reg, uint8_t value ); + + +// read data from a register +// ---------------------------------------------------------- +uint8_t RAIO_GetRegister( uint8_t reg ); + + +// set PWM value for backlight -> 0 (0% PWM) - 255 (100% PWM) +// ---------------------------------------------------------- +void RAIO_SetBacklightPWMValue( uint8_t BL_value ); + + +// set coordinates for active window +// ---------------------------------------------------------- +void Active_Window( uint16_t XL,uint16_t XR ,uint16_t YT ,uint16_t YB); + + +// set cursor +// ---------------------------------------------------------- +void RAIO_set_cursor( uint16_t pos_x ,uint16_t pos_y ); + + +// set mode for BET (Block Transfer Engine) +// ---------------------------------------------------------- +void BTE_mode( uint8_t bte_operation, uint8_t rop_function ); + + +// set color -> see color defines +// ---------------------------------------------------------- +void Text_Background_Color( uint8_t color ); +void Text_Foreground_Color( uint8_t color ); + + +// clear memory +// ---------------------------------------------------------- +void RAIO_clear_screen( void ); + + +// set coordinates for drawing +// ---------------------------------------------------------- +void Set_Geometric_Coordinate( uint16_t X1, uint16_t Y1 ,uint16_t X2 ,uint16_t Y2 ); +void Set_Geometric_Coordinate_circle (uint16_t X1, uint16_t Y1 ,uint8_t rad ); + +// show the BMP picture on the TFT screen +// ---------------------------------------------------------- +void RAIO_Write_Picture( uint16_t *data, uint32_t count ); + + +// set draw mode -> see DRAW_MODES +// ---------------------------------------------------------- +void RAIO_StartDrawing( int16_t whattodraw ); + + +// draw some basic geometrical forms +// ---------------------------------------------------------- +void Draw_Line( uint16_t X1, uint16_t Y1 ,uint16_t X2 ,uint16_t Y2 ); +void Draw_Square( uint16_t X1, uint16_t Y1 ,uint16_t X2 ,uint16_t Y2 ); + + +// 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 font size +// ---------------------------------------------------------- +void RAIO_SetFontSizeFactor( uint8_t size ); + + + +// get touch coordinates +// ---------------------------------------------------------- +#ifdef touch_available + int32_t RAIO_gettouch(); +#endif + + +#endif diff --git a/SW/tft_test/bmp.c b/SW/tft_test/bmp.c new file mode 100644 index 0000000..8f432b0 --- /dev/null +++ b/SW/tft_test/bmp.c @@ -0,0 +1,201 @@ +/*##############################################################*/ +/* */ +/* File : bmp.c */ +/* */ +/* Project : TFT for Raspberry Pi Revision 2 */ +/* */ +/* Date : 2013-11-22 last update: 2013-11-22 */ +/* */ +/* 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 : */ +/* */ +/* The current software can display BMP files on the C-Berry. */ +/* The BMP file must have a dimension of 320 x 240 pixel and */ +/* a color depth of 24Bit. */ +/* The picture(s) will be stored into a memory. The function */ +/* returns back a pointer with the address of the memory. */ +/* */ +/* */ +/* 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 . */ +/* */ +/* */ +/* Revision History: */ +/* */ +/* Version 1.0 - Initial release */ +/* */ +/* */ +/* */ +/*##############################################################*/ + +#include +#include +#include "bmp.h" +#include "RAIO8870.h" +#include + + +// store BMP files in memory +// ---------------------------------------------------------- +int32_t Read_bmp2memory ( char const *file_name, uint16_t *picture_pointer ) +{ + FILE *my_file; + + // bmp_header_t bmp_header; + uint8_t bmp_header_buffer[54]; + uint8_t bmp_line_buffer[ DISPLAY_WIDTH * 3 ]; + + uint16_t bfType; + uint32_t bfOffBits; + + uint32_t biSize; + + int32_t biWidth; + int32_t biHeight; + uint16_t biBitCount; + + uint32_t y,x; + + uint8_t red, green, blue; + uint16_t color; + + + // check for file + //printf( "Opening input file... " ); + if( ( my_file = fopen( file_name, "rb" ) ) == NULL ) + { + printf( "ERROR: Could not open input file for reading.\n" ); + return( -1 ); + } + //printf( "OK\n" ); + + + // read header + fread( &bmp_header_buffer, 1, 54, my_file ); + + + // check for "BM" + //printf( "Checking magic number... " ); + bfType = bmp_header_buffer[1]; + bfType = (bfType << 8) | bmp_header_buffer[0]; + if( bfType != 0x4D42) + { + printf( "ERROR: Not a bitmap file.\n" ); + fclose( my_file ); + return( -1 ); + } + //printf( "OK\n" ); + + + //printf( "Checking header size... " ); + biSize = bmp_header_buffer[17]; + biSize = (biSize << 8) | bmp_header_buffer[16]; + biSize = (biSize << 8) | bmp_header_buffer[15]; + biSize = (biSize << 8) | bmp_header_buffer[14]; + //printf( "%d ", biSize); + if( biSize != 40 ) + { + printf( "ERROR: Not Windows V3\n" ); + fclose( my_file ); + return( -1 ); + } + //printf( "OK\n" ); + + + //printf( "Checking dimensions... " ); + biWidth = bmp_header_buffer[21]; + biWidth = (biWidth << 8) | bmp_header_buffer[20]; + biWidth = (biWidth << 8) | bmp_header_buffer[19]; + biWidth = (biWidth << 8) | bmp_header_buffer[18]; + + biHeight = bmp_header_buffer[25]; + biHeight = (biHeight << 8) | bmp_header_buffer[24]; + biHeight = (biHeight << 8) | bmp_header_buffer[23]; + biHeight = (biHeight << 8) | bmp_header_buffer[22]; + + biBitCount = bmp_header_buffer[29]; + biBitCount = (biBitCount << 8) | bmp_header_buffer[28]; + + //printf( "%dx%dx%dbbp. ", biWidth, biHeight, biBitCount ); + + if( (biWidth != DISPLAY_WIDTH) || (biHeight != DISPLAY_HEIGHT) || (biBitCount != 24) ) + { + printf( "ERROR. %dx%dx%d required.\n", DISPLAY_WIDTH, DISPLAY_HEIGHT, 24 ); + fclose( my_file ); + return( -1 ); + } + //printf( "OK\n\n" ); + + + bfOffBits = bmp_header_buffer[13]; + bfOffBits = (bfOffBits << 8) | bmp_header_buffer[12]; + bfOffBits = (bfOffBits << 8) | bmp_header_buffer[11]; + bfOffBits = (bfOffBits << 8) | bmp_header_buffer[10]; + + //printf( "biOffBits = %d\n", bfOffBits ); + + + fseek( my_file, bfOffBits, SEEK_SET ); + //printf( "Filling picture buffer... \n" ); + + + for (y=DISPLAY_HEIGHT; y>0; y--) + { + fread( &bmp_line_buffer[0], sizeof(bmp_line_buffer), 1, my_file ); + for (x=DISPLAY_WIDTH; x>0; x--) + { + + blue = bmp_line_buffer[(x-1)*3 +0]; + green = bmp_line_buffer[(x-1)*3 +1]; + red = bmp_line_buffer[(x-1)*3 +2]; + + +#ifdef CM_65K + color = (red >> 3); + color = color << 6; + color = color | (green >> 2); + color = color << 5; + color = color | (blue >> 3); +#elif defined(CM_4K) + color = ( red >> 4 ); + color = color << 4; + color = color | ( green >> 4); + color = color << 4; + color = color | (blue >> 4); +#else + #error "color_mode not defined" +#endif + + *picture_pointer = color; + picture_pointer--; + } + } + + fclose( my_file ); + + return ( 0 ); +} diff --git a/SW/tft_test/bmp.h b/SW/tft_test/bmp.h new file mode 100644 index 0000000..2fd980e --- /dev/null +++ b/SW/tft_test/bmp.h @@ -0,0 +1,65 @@ +/*##############################################################*/ +/* */ +/* File : bmp.h */ +/* */ +/* Project : TFT for Raspberry Pi Revision 2 */ +/* */ +/* Date : 2013-11-22 last update: 2013-11-22 */ +/* */ +/* 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 : */ +/* */ +/* The current software can display BMP files on the C-Berry. */ +/* The BMP file must have a dimension of 320 x 240 pixel and */ +/* a color depth of 24Bit. */ +/* The picture(s) will be stored into a memory. The function */ +/* returns back a pointer with the address of the memory. */ +/* */ +/* */ +/* 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 . */ +/* */ +/* */ +/* Revision History: */ +/* */ +/* Version 1.0 - Initial release */ +/* */ +/* */ +/* */ +/*##############################################################*/ + +#ifndef BMP_H +#define BMP_H + +#include + +// store BMP files in memory +// ---------------------------------------------------------- +int32_t Read_bmp2memory ( char const *file_name, uint16_t *picture_pointer ); + + +#endif diff --git a/SW/tft_test/examples.c b/SW/tft_test/examples.c new file mode 100644 index 0000000..c6f2d93 --- /dev/null +++ b/SW/tft_test/examples.c @@ -0,0 +1,429 @@ +/*##############################################################*/ +/* */ +/* File : examples.c */ +/* */ +/* Project : TFT for Raspberry Pi Revision 2 */ +/* */ +/* Date : 2014-08-14 last update: 2014-08-14 */ +/* */ +/* Author : Hagen Ploog */ +/* Timo Pfander */ +/* */ +/* IDE : Geany 1.22 */ +/* Compiler : gcc (Debian 4.6.3-14+rpi1) 4.6.3 */ +/* */ +/* Copyright (C) 2013 admatec GmbH */ +/* */ +/* */ +/* Description : */ +/* Exmples.c includes the following functions to demonstrate */ +/* some opportunities: */ +/* - depict one BMP file on the TFT */ +/* - depict basic drawings which were created with the */ +/* RAIO graphic controller */ +/* - write some text on the TFT */ +/* - draw functions like sine, cosine, parabola, ... */ +/* */ +/* */ +/* 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 . */ +/* */ +/* */ +/* Revision History: */ +/* */ +/* Version 1.0 - Initial release */ +/* */ +/* */ +/* */ +/*##############################################################*/ + + +#include +#include +#include +#include "RAIO8870.h" +#include "bmp.h" +#include "examples.h" + + +// load and depict a BMP file +// --------------------------------------------- +void example_DepictBMP( char const *file_name ) +{ + uint16_t picture[1][ PICTURE_PIXELS ]; + + RAIO_set_cursor( 0, 0 ); + Read_bmp2memory ( file_name, &picture[0][ PICTURE_PIXELS-1 ] ); + RAIO_Write_Picture ( &picture[0][0], PICTURE_PIXELS ); +} + + +// draw with RAIO +// --------------------------------------------- +void example_DrawWithRAIO( void ) +{ + // clear display with black color + Text_Background_Color ( COLOR_BLACK ); + RAIO_clear_screen(); + + // rectangle + Text_Foreground_Color ( COLOR_BLUE ); + Draw_Square (210, 150, 260, 200); + RAIO_StartDrawing ( SQUARE_FILL ); + + // line + Text_Foreground_Color ( COLOR_GREEN ); + Draw_Line ( 10, 230, 310, 10 ); + RAIO_StartDrawing ( LINE ); + + // circle + Text_Foreground_Color ( COLOR_RED ); + Draw_Circle ( 90, 65, 25 ); + RAIO_StartDrawing ( CIRCLE_FILL ); +} + + +// write text +// --------------------------------------------- +void example_WriteText( unsigned char *text ) +{ + // clear display with black color + Text_Background_Color ( COLOR_BLACK ); + RAIO_clear_screen(); + + // write text in different size and colors + RAIO_SetFontSizeFactor ( 0 ); + RAIO_print_text ( 10, 10, text, COLOR_BLACK , COLOR_WHITE ); + + RAIO_SetFontSizeFactor ( 5 ); + RAIO_print_text( 30, 30, text, COLOR_BLACK, COLOR_RED ); + + RAIO_SetFontSizeFactor ( 10 ); + RAIO_print_text ( 60, 70, text, COLOR_BLACK, COLOR_BLUE ); + + RAIO_SetFontSizeFactor ( 15 ); + RAIO_print_text ( 80, 120, text, COLOR_BLACK, COLOR_GREEN ); +} + + +// draw sinus +// --------------------------------------------- + +// defines the x-intercept and y-intercept on the TFT +#define window_left -M_PI +#define window_right M_PI +#define window_up 2 +#define window_down -2 + + +// windows_up +// +// ↑ +// _ _ _ _ _|_ _ _ _ _ +// | | | +// | | | +// | | | +// windows_left --|---------|---------|--→ windows_right +// | | | +// | | | +// |_ _ _ _ _|_ _ _ _ _| +// | +// +// windows_down + + + +void example_DrawFunction( int16_t function ) +{ + float_t x_real, y_real; + int16_t count; + + // clear display with black color + Text_Background_Color ( COLOR_BLACK ); + RAIO_clear_screen(); + + // draw x-axis + draw_coords_line( window_left, 0, window_right, 0 ); + for( count = (int16_t)window_left; count < (int16_t)window_right; count++ ) + { + Draw_Line ( scale_x( count ), scale_y( window_up*0.01 ), scale_x( count ), scale_y( window_down*0.01 ) ); + Text_Foreground_Color ( COLOR_WHITE ); + RAIO_StartDrawing ( LINE ); + } + + // draw y-axis + draw_coords_line( 0, window_up, 0, window_down ); + for( count = (int16_t)window_down; count < (int16_t)window_up; count++ ) + { + Draw_Line ( scale_x( window_left*0.01 ), scale_y( count ), scale_x( window_right*0.01 ), scale_y( count ) ); + Text_Foreground_Color ( COLOR_WHITE ); + RAIO_StartDrawing ( LINE ); + } + + + // draw function + for( x_real = window_left; x_real < window_right; x_real=x_real+0.02 ) + { + switch (function) // -> see FUNCTIONS + { + case SIN: y_real = sin( x_real ); break; + case COS: y_real = cos( x_real ); break; + case TAN: y_real = tan( x_real ); break; + case PARABOLA: y_real = x_real * x_real; break; + case EXPONENT: y_real = exp( x_real ); break; + case LOGN : y_real = log( x_real ); break; + default: break; + }; + set_point( x_real, y_real); + } +} + + +// transform real world data in TFT data +// --------------------------------------------- + +// transform x-value +uint16_t scale_x( float_t x ) +{ + return ( (uint16_t) (( x - window_left ) * ( DISPLAY_WIDTH / (window_right-window_left))) ); +} + +// transform y-value +uint16_t scale_y( float_t y ) +{ + uint16_t temp_y; + + temp_y = DISPLAY_HEIGHT - (uint16_t) (( y - window_down) * (DISPLAY_HEIGHT / (window_up-window_down))); + if( temp_y > 239 ) { temp_y--; } + + return ( temp_y ); +} + + +// draw coordinate system and function points +// --------------------------------------------- + +// draw coordinate system +void draw_coords_line (float_t x1, float_t y1, float_t x2, float_t y2) +{ + Text_Foreground_Color( COLOR_WHITE ); + Draw_Line( scale_x( x1 ), scale_y( y1 ), scale_x( x2 ), scale_y( y2 ) ); + RAIO_StartDrawing( LINE ); +} + +// draw function point +void set_point ( float_t x_real, float_t y_real ) +{ + if ((x_real> window_left) & (x_real < window_right) & (y_real > window_down) & (y_real 90) && (my_touch.touch_x < 115) && (my_touch.touch_y > 86) && (my_touch.touch_y < 106) ) + { + // draw exit button + Text_Foreground_Color ( COLOR_YELLOW ); + Draw_Square( (DISPLAY_WIDTH/2)-25, (DISPLAY_HEIGHT/2)-15, (DISPLAY_WIDTH/2)+25, (DISPLAY_HEIGHT/2)+15 ); + RAIO_StartDrawing ( SQUARE_FILL ); + RAIO_print_text( (DISPLAY_WIDTH/2)-16, (DISPLAY_HEIGHT/2)-9, "EXIT", COLOR_YELLOW, COLOR_BLACK ); + + // wait for leaving the touch panel + while((my_touch.state == pressed)) + { + bcm2835_delayMicroseconds( 5000 ); + rt = RAIO_gettouch(); + } + + // exit while loop + break; + } + else if ( ((my_touch.state == pressed) || (my_touch.state == down) || (my_touch.state == up)) && (btn_old_state != my_touch.state) ) + { + // safe old touch state + btn_old_state = my_touch.state; + + if( my_touch.touch_x > 25 && my_touch.touch_x < 102 ) + { + // button 1 is active + if( my_touch.touch_y > 21 && my_touch.touch_y < 96 ) + { + Text_Foreground_Color ( COLOR_GREEN ); + Draw_Square( 6, 6, (DISPLAY_WIDTH/2)-3, (DISPLAY_HEIGHT/2)-3 ); + RAIO_StartDrawing ( SQUARE_FILL ); + RAIO_print_text( (DISPLAY_WIDTH/4)-45, (DISPLAY_HEIGHT/4)-20, "Button 1 is", COLOR_GREEN, COLOR_BLACK ); + switch (my_touch.state) + { + case down : RAIO_print_text( (DISPLAY_WIDTH/4)-16, (DISPLAY_HEIGHT/4), "down", COLOR_GREEN, COLOR_BLACK ); + break; + case up : RAIO_print_text( (DISPLAY_WIDTH/4)-9, (DISPLAY_HEIGHT/4), "up", COLOR_GREEN, COLOR_BLACK ); + break; + case pressed: RAIO_print_text( (DISPLAY_WIDTH/4)-30, (DISPLAY_HEIGHT/4), "pressed", COLOR_GREEN, COLOR_BLACK ); + break; + default : break; + } + } + // button 3 is active + if( my_touch.touch_y > 101 && my_touch.touch_y < 178 ) + { + Text_Foreground_Color ( COLOR_GREEN ); + Draw_Square( 6, (DISPLAY_HEIGHT/2)+4, (DISPLAY_WIDTH/2)-3, DISPLAY_HEIGHT-6 ); + RAIO_StartDrawing ( SQUARE_FILL ); + RAIO_print_text( (DISPLAY_WIDTH/4)-45, (DISPLAY_HEIGHT/4)*3-20, "Button 3 is", COLOR_GREEN, COLOR_BLACK ); + switch (my_touch.state) + { + case down : RAIO_print_text( (DISPLAY_WIDTH/4)-16, (DISPLAY_HEIGHT/4)*3, "down", COLOR_GREEN, COLOR_BLACK ); + break; + case up : RAIO_print_text( (DISPLAY_WIDTH/4)-9, (DISPLAY_HEIGHT/4)*3, "up", COLOR_GREEN, COLOR_BLACK ); + break; + case pressed: RAIO_print_text( (DISPLAY_WIDTH/4)-30, (DISPLAY_HEIGHT/4)*3, "pressed", COLOR_GREEN, COLOR_BLACK ); + break; + default : break; + } + } + + } + + if( my_touch.touch_x > 107 && my_touch.touch_x < 182 ) + { + // button 2 is active + if( my_touch.touch_y > 21 && my_touch.touch_y < 96 ) + { + Text_Foreground_Color ( COLOR_GREEN ); + Draw_Square( (DISPLAY_WIDTH/2)+4, 6, DISPLAY_WIDTH-6, (DISPLAY_HEIGHT/2)-3 ); + RAIO_StartDrawing ( SQUARE_FILL ); + RAIO_print_text( (DISPLAY_WIDTH/4)*3-45, (DISPLAY_HEIGHT/4)-20, "Button 2 is", COLOR_GREEN, COLOR_BLACK ); + switch (my_touch.state) + { + case down : RAIO_print_text( (DISPLAY_WIDTH/4)*3-16, (DISPLAY_HEIGHT/4), "down", COLOR_GREEN, COLOR_BLACK ); + break; + case up : RAIO_print_text( (DISPLAY_WIDTH/4)*3-9, (DISPLAY_HEIGHT/4), "up", COLOR_GREEN, COLOR_BLACK ); + break; + case pressed: RAIO_print_text( (DISPLAY_WIDTH/4)*3-30, (DISPLAY_HEIGHT/4), "pressed", COLOR_GREEN, COLOR_BLACK ); + break; + default : break; + } + } + // button 4 is active + if( my_touch.touch_y > 101 && my_touch.touch_y < 178 ) + { + Text_Foreground_Color ( COLOR_GREEN ); + Draw_Square( (DISPLAY_WIDTH/2)+4, (DISPLAY_HEIGHT/2)+4, DISPLAY_WIDTH-6, DISPLAY_HEIGHT-6 ); + RAIO_StartDrawing ( SQUARE_FILL ); + RAIO_print_text( (DISPLAY_WIDTH/4)*3-45, (DISPLAY_HEIGHT/4)*3-20, "Button 4 is", COLOR_GREEN, COLOR_BLACK ); + switch (my_touch.state) + { + case down : RAIO_print_text( (DISPLAY_WIDTH/4)*3-16, (DISPLAY_HEIGHT/4)*3, "down", COLOR_GREEN, COLOR_BLACK ); + break; + case up : RAIO_print_text( (DISPLAY_WIDTH/4)*3-9, (DISPLAY_HEIGHT/4)*3, "up", COLOR_GREEN, COLOR_BLACK ); + break; + case pressed: RAIO_print_text( (DISPLAY_WIDTH/4)*3-30, (DISPLAY_HEIGHT/4)*3, "pressed", COLOR_GREEN, COLOR_BLACK ); + break; + default : break; + } + } + } + } + else if ( (btn_old_state != my_touch.state) ) + { + // safe old touch state + btn_old_state = my_touch.state; + + switch (my_touch.state) + { + case no_touch : btn_set_all_open(); + break; + default : break; + } + } + // time of minimum 15000µs is required + bcm2835_delayMicroseconds( 100000 ); + } +} + + +// set all soft-buttons to open +// --------------------------------------------- +void btn_set_all_open() +{ + // draw button dimensions + Text_Foreground_Color( COLOR_BLACK ); + Draw_Square( 5, 5, (DISPLAY_WIDTH/2)-2, (DISPLAY_HEIGHT/2)-2 ); + Draw_Square( (DISPLAY_WIDTH/2)+3, 5, DISPLAY_WIDTH-5, (DISPLAY_HEIGHT/2)-2 ); + Draw_Square( 5, (DISPLAY_HEIGHT/2)+3, (DISPLAY_WIDTH/2)-2, DISPLAY_HEIGHT-5 ); + Draw_Square( (DISPLAY_WIDTH/2)+3, (DISPLAY_HEIGHT/2)+3, DISPLAY_WIDTH-5, DISPLAY_HEIGHT-5 ); + + // set text and color for each open buttons + Text_Foreground_Color ( COLOR_RED ); + Draw_Square( 6, 6, (DISPLAY_WIDTH/2)-3, (DISPLAY_HEIGHT/2)-3 ); + RAIO_StartDrawing ( SQUARE_FILL ); + RAIO_print_text( (DISPLAY_WIDTH/4)-45, (DISPLAY_HEIGHT/4)-20, "Button 1 is", COLOR_RED, COLOR_BLACK ); + RAIO_print_text( (DISPLAY_WIDTH/4)-16, (DISPLAY_HEIGHT/4) , "open", COLOR_RED, COLOR_BLACK ); + + Text_Foreground_Color ( COLOR_RED ); + Draw_Square( (DISPLAY_WIDTH/2)+4, 6, DISPLAY_WIDTH-6, (DISPLAY_HEIGHT/2)-3 ); + RAIO_StartDrawing ( SQUARE_FILL ); + RAIO_print_text( (DISPLAY_WIDTH/4)*3-45, (DISPLAY_HEIGHT/4)-20, "Button 2 is", COLOR_RED, COLOR_BLACK ); + RAIO_print_text( (DISPLAY_WIDTH/4)*3-16, (DISPLAY_HEIGHT/4) , "open", COLOR_RED, COLOR_BLACK ); + + Text_Foreground_Color ( COLOR_RED ); + Draw_Square( 6, (DISPLAY_HEIGHT/2)+4, (DISPLAY_WIDTH/2)-3, DISPLAY_HEIGHT-6 ); + RAIO_StartDrawing ( SQUARE_FILL ); + RAIO_print_text( (DISPLAY_WIDTH/4)-45, (DISPLAY_HEIGHT/4)*3-20, "Button 3 is", COLOR_RED, COLOR_BLACK ); + RAIO_print_text( (DISPLAY_WIDTH/4)-16, (DISPLAY_HEIGHT/4)*3 , "open", COLOR_RED, COLOR_BLACK ); + + Text_Foreground_Color ( COLOR_RED ); + Draw_Square( (DISPLAY_WIDTH/2)+4, (DISPLAY_HEIGHT/2)+4, DISPLAY_WIDTH-6, DISPLAY_HEIGHT-6 ); + RAIO_StartDrawing ( SQUARE_FILL ); + RAIO_print_text( (DISPLAY_WIDTH/4)*3-45, (DISPLAY_HEIGHT/4)*3-20, "Button 4 is", COLOR_RED, COLOR_BLACK ); + RAIO_print_text( (DISPLAY_WIDTH/4)*3-16, (DISPLAY_HEIGHT/4)*3 , "open", COLOR_RED, COLOR_BLACK ); + + // draw exit button + Text_Foreground_Color ( COLOR_BLUE ); + Draw_Square( (DISPLAY_WIDTH/2)-25, (DISPLAY_HEIGHT/2)-15, (DISPLAY_WIDTH/2)+25, (DISPLAY_HEIGHT/2)+15 ); + RAIO_StartDrawing ( SQUARE_FILL ); + RAIO_print_text( (DISPLAY_WIDTH/2)-16, (DISPLAY_HEIGHT/2)-9, "EXIT", COLOR_BLUE, COLOR_WHITE ); +} diff --git a/SW/tft_test/examples.h b/SW/tft_test/examples.h new file mode 100644 index 0000000..f4c6520 --- /dev/null +++ b/SW/tft_test/examples.h @@ -0,0 +1,111 @@ +/*##############################################################*/ +/* */ +/* File : examples.h */ +/* */ +/* Project : TFT for Raspberry Pi Revision 2 */ +/* */ +/* Date : 2014-08-14 last update: 2014-08-14 */ +/* */ +/* Author : Hagen Ploog */ +/* 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 declared functions for the different examples in */ +/* example.c */ +/* */ +/* */ +/* 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 . */ +/* */ +/* */ +/* Revision History: */ +/* */ +/* Version 1.0 - Initial release */ +/* */ +/* */ +/* */ +/*##############################################################*/ + +#ifndef EXAMPLES_H +#define EXAMPLES_H + +#include +#include + + +// load and depict a BMP file +// --------------------------------------------- +void example_DepictBMP( char const *file_name ); + + +// draw with RAIO +// --------------------------------------------- +void example_DrawWithRAIO( void ); + + +// delete screen and write text +// --------------------------------------------- +void example_WriteText( unsigned char *text ); + + +// draw function // -> see FUNCTIONS +// --------------------------------------------- +void example_DrawFunction( int16_t function ); + +// enumeration of drawing modes +enum FUNCTIONS { SIN, COS, TAN, PARABOLA, EXPONENT, LOGN }; + + +// transform real world data in TFT data +// --------------------------------------------- +uint16_t scale_x( float_t x ); + +uint16_t scale_y( float_t y ); + + +// draw coordinate system and function points +// --------------------------------------------- +void draw_coords_line( float_t x1, float_t y1, float_t x2, float_t y2 ); + +void set_point( float_t x_real, float_t y_real ); + + +// touch the soft-buttons on TFT +// --------------------------------------------- +void example_Touch (); + + +// set all soft-buttons to open +// --------------------------------------------- +void btn_set_all_open(); + + +// draw exit button +// --------------------------------------------- +void btn_draw_exit(); + + + +#endif + diff --git a/SW/tft_test/makefile b/SW/tft_test/makefile new file mode 100644 index 0000000..7375a14 --- /dev/null +++ b/SW/tft_test/makefile @@ -0,0 +1,23 @@ +all: tft_test + +tft_test: tft_test.o tft.o RAIO8870.o bmp.o examples.o + gcc tft_test.o tft.o RAIO8870.o bmp.o examples.o -lbcm2835 -lrt -lm -o tft_test + +tft.o: tft.c tft.h + gcc -Os -c tft.c + +tft_test.o: tft_test.c + gcc -Os -c tft_test.c + +RAIO8870.o: RAIO8870.c RAIO8870.h + gcc -Os -c RAIO8870.c + +bmp.o: bmp.c bmp.h + gcc -Os -c bmp.c + +examples.o: examples.c examples.h bmp.h + gcc -Os -c examples.c + +clean: + rm -rf *o test_tft + diff --git a/SW/tft_test/tft.c b/SW/tft_test/tft.c new file mode 100644 index 0000000..d710c42 --- /dev/null +++ b/SW/tft_test/tft.c @@ -0,0 +1,297 @@ +/*##############################################################*/ +/* */ +/* File : tft.c */ +/* */ +/* Project : TFT for Raspberry Pi Revision 2 */ +/* */ +/* Date : 2014-08-14 last update: 2014-08-14 */ +/* */ +/* 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 controlls the communications between the */ +/* Raspberry Pi and the TFT. The file initialized also the */ +/* GPIO Pins of the Raspberry Pi. */ +/* */ +/* */ +/* 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 . */ +/* */ +/* */ +/* Revision History: */ +/* */ +/* Version 1.0 - Initial release */ +/* */ +/* */ +/* */ +/*##############################################################*/ + + +#include +#include +#include "RAIO8870.h" +#include "tft.h" + + +// initialization of GPIO and SPI +// ---------------------------------------------------------- +void TFT_init_board ( void ) +{ + // *************** set the pins to be an output and turn them on + + bcm2835_gpio_fsel( OE, BCM2835_GPIO_FSEL_OUTP ); + bcm2835_gpio_write( OE, HIGH ); + + bcm2835_gpio_fsel( RAIO_RST, BCM2835_GPIO_FSEL_OUTP ); + bcm2835_gpio_write( RAIO_RST, HIGH ); + + bcm2835_gpio_fsel( RAIO_CS, BCM2835_GPIO_FSEL_OUTP ); + bcm2835_gpio_write( RAIO_CS, HIGH ); + + bcm2835_gpio_fsel( RAIO_RS, BCM2835_GPIO_FSEL_OUTP ); + bcm2835_gpio_write( RAIO_RS, HIGH ); + + bcm2835_gpio_fsel( RAIO_WR, BCM2835_GPIO_FSEL_OUTP ); + bcm2835_gpio_write( RAIO_WR, HIGH ); + + bcm2835_gpio_fsel( RAIO_RD, BCM2835_GPIO_FSEL_OUTP ); + bcm2835_gpio_write( RAIO_RD, HIGH ); + + + // *************** now the inputs + + bcm2835_gpio_fsel( RAIO_WAIT, BCM2835_GPIO_FSEL_INPT ); + bcm2835_gpio_set_pud( RAIO_WAIT, BCM2835_GPIO_PUD_UP); + + bcm2835_gpio_fsel( RAIO_INT, BCM2835_GPIO_FSEL_INPT ); + bcm2835_gpio_set_pud( RAIO_INT, BCM2835_GPIO_PUD_UP); + + bcm2835_gpio_fsel( MISO, BCM2835_GPIO_FSEL_INPT ); + bcm2835_gpio_set_pud( MISO, BCM2835_GPIO_PUD_UP); + + // *************** set pins for SPI + + //bcm2835_gpio_fsel(MISO, BCM2835_GPIO_FSEL_ALT0); + bcm2835_gpio_fsel(MOSI, BCM2835_GPIO_FSEL_ALT0); + bcm2835_gpio_fsel(SCLK, BCM2835_GPIO_FSEL_ALT0); + bcm2835_gpio_fsel(SPI_CE1, BCM2835_GPIO_FSEL_ALT0); + + // set the SPI CS register to the some sensible defaults + volatile uint32_t* paddr = bcm2835_spi0 + BCM2835_SPI0_CS/8; + bcm2835_peri_write( paddr, 0 ); // All 0s + + // clear TX and RX fifos + bcm2835_peri_write_nb( paddr, BCM2835_SPI0_CS_CLEAR ); + + bcm2835_spi_setBitOrder( BCM2835_SPI_BIT_ORDER_MSBFIRST ); + bcm2835_spi_setDataMode( BCM2835_SPI_MODE0 ); + bcm2835_spi_setClockDivider( BCM2835_SPI_CLOCK_DIVIDER_2 ); + bcm2835_spi_chipSelect( BCM2835_SPI_CS1 ); + bcm2835_spi_setChipSelectPolarity( BCM2835_SPI_CS1, LOW ); +} + + +// hard reset of the graphic controller and the tft +// ---------------------------------------------------------- +void TFT_hard_reset( void ) +{ + bcm2835_gpio_write( RAIO_RST, LOW ); + bcm2835_delay( 10 ); + bcm2835_gpio_write( RAIO_RST, HIGH ); + bcm2835_delay( 1 ); +} + + +// wait during raio is busy +// ---------------------------------------------------------- +void TFT_wait_for_raio ( void ) +{ + while ( !bcm2835_gpio_lev( RAIO_WAIT ) ); +} + + +// write data via SPI to tft +// ---------------------------------------------------------- +void TFT_SPI_data_out ( uint16_t data ) +{ + union my_union number; + char buffer[2]; + + number.value = data; + buffer[0] = (char) number.split.high; + buffer[1] = (char) number.split.low; + + bcm2835_spi_writenb( &buffer[0], 2 ); +} + + +uint32_t TFT_shift_data_in() +{ + uint32_t i, data=0; //, data1, data2; + + // Das Umschalten ist der 1te Puls ... ups + bcm2835_gpio_fsel( SCLK, BCM2835_GPIO_FSEL_OUTP ); + + for ( i=15; i>0; i-- ) + { + data <<= 1; + data |= ( bcm2835_gpio_lev( MISO ) ? 0x00000001 : 0x00000000 ); + //bcm2835_delayMicroseconds( 1 ); + bcm2835_gpio_write( SCLK, LOW ); + //bcm2835_delayMicroseconds( 1 ); + bcm2835_gpio_write( SCLK, HIGH ); + } + + //data = bcm2835_spi_transfer( 0 ); + bcm2835_gpio_fsel(SCLK, BCM2835_GPIO_FSEL_ALT0); + + return ( data ); +} + +// write byte to register +// ---------------------------------------------------------- +void TFT_RegWrite( uint16_t reg ) +{ + bcm2835_gpio_write( RAIO_RS, HIGH ); + bcm2835_gpio_write( RAIO_CS, LOW ); + bcm2835_gpio_write( RAIO_WR, LOW ); + bcm2835_gpio_write( OE, LOW ); + + TFT_SPI_data_out ( reg ); + + bcm2835_gpio_write( RAIO_WR, HIGH ); + bcm2835_gpio_write( RAIO_CS, HIGH ); + bcm2835_gpio_write( OE, HIGH ); +} + + +// write byte to tft +// ---------------------------------------------------------- +void TFT_DataWrite( uint16_t data ) +{ + bcm2835_gpio_write( RAIO_RS, LOW ); + bcm2835_gpio_write( RAIO_CS, LOW ); + bcm2835_gpio_write( RAIO_WR, LOW ); + bcm2835_gpio_write( OE, LOW ); + + TFT_SPI_data_out ( data ); + + bcm2835_gpio_write( RAIO_WR, HIGH ); + bcm2835_gpio_write( RAIO_CS, HIGH ); + bcm2835_gpio_write( OE, HIGH ); +}; + + +// read byte from register +// ---------------------------------------------------------- +uint16_t TFT_DataRead( ) +{ + uint16_t data; + + bcm2835_gpio_write( RAIO_RS, LOW ); + bcm2835_gpio_write( RAIO_CS, LOW ); + bcm2835_gpio_write( RAIO_RD, LOW ); + + bcm2835_delayMicroseconds( 1 ); + + bcm2835_gpio_write( RAIO_RD, HIGH ); + bcm2835_gpio_write( RAIO_CS, HIGH ); + + data = TFT_shift_data_in(); + + return ( data ); +} + + +// read byte from register +// ---------------------------------------------------------- +uint16_t TFT_StatusRead( ) +{ + uint16_t data; + + bcm2835_gpio_write( RAIO_RS, HIGH ); + bcm2835_gpio_write( RAIO_CS, LOW ); + bcm2835_gpio_write( RAIO_RD, LOW ); + + bcm2835_delayMicroseconds( 1 ); + + bcm2835_gpio_write( RAIO_RD, HIGH ); + bcm2835_gpio_write( RAIO_CS, HIGH ); + + data = TFT_shift_data_in(); + + return ( data ); +} + + +// write 'count'-bytes to tft +// ---------------------------------------------------------- +void TFT_DataMultiWrite( uint16_t *data, uint32_t count ) +{ + volatile uint32_t* paddr = bcm2835_spi0 + BCM2835_SPI0_CS/4; + volatile uint32_t* fifo = bcm2835_spi0 + BCM2835_SPI0_FIFO/4; + + volatile uint32_t* gpio_set = bcm2835_gpio + BCM2835_GPSET0/4; + volatile uint32_t* gpio_clear = bcm2835_gpio + BCM2835_GPCLR0/4; + + uint32_t i; + + bcm2835_gpio_write( RAIO_RS, LOW ); + bcm2835_gpio_write( RAIO_CS, LOW ); + bcm2835_gpio_write( OE, LOW ); + + for( i=0; i> 8); + *fifo = (uint8_t)(data[i] & 0xFF); + + // write fifo data to SPI TX buffer + while (!(*paddr & BCM2835_SPI0_CS_DONE)) + { + // clear SPI RX buffer + *paddr |=BCM2835_SPI0_CS_CLEAR_RX; + }; + + // deactivate SPI transfer + *paddr &= ~BCM2835_SPI0_CS_TA; + + // WR = 1 + *gpio_set = ( 1 << RAIO_WRpin ); + } + + bcm2835_gpio_write( RAIO_CS, HIGH ); + bcm2835_gpio_write( OE, HIGH ); + +} + diff --git a/SW/tft_test/tft.h b/SW/tft_test/tft.h new file mode 100644 index 0000000..509a3a0 --- /dev/null +++ b/SW/tft_test/tft.h @@ -0,0 +1,120 @@ +/*##############################################################*/ +/* */ +/* File : tft.h */ +/* */ +/* Project : TFT for Raspberry Pi Revision 2 */ +/* */ +/* Date : 2014-08-14 last update: 2014-08-14 */ +/* */ +/* 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 declared functions for the SPI communications */ +/* between the Raspberry Pi and the TFT and for the */ +/* initialization of the GPIO Pins of the Raspberry Pi. */ +/* */ +/* */ +/* 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 . */ +/* */ +/* */ +/* Revision History: */ +/* */ +/* Version 1.0 - Initial release */ +/* */ +/* */ +/* */ +/*##############################################################*/ + +#ifndef TFT_H +#define TFT_H + +#include +//#include "RAIO8870.h" + + +// define pins on connector P1 +#define MOSI RPI_V2_GPIO_P1_19 +#define MISO RPI_V2_GPIO_P1_21 +#define SCLK RPI_V2_GPIO_P1_23 +#define OE RPI_V2_GPIO_P1_11 +#define SPI_CE1 RPI_V2_GPIO_P1_26 +#define RAIO_RS RPI_V2_GPIO_P1_12 +#define RAIO_RST RPI_V2_GPIO_P1_22 +#define RAIO_CS RPI_V2_GPIO_P1_24 +#define RAIO_WR RPI_V2_GPIO_P1_18 +#define RAIO_RD RPI_V2_GPIO_P1_16 +#define RAIO_WAIT RPI_V2_GPIO_P1_15 +#define RAIO_INT RPI_V2_GPIO_P1_13 + +#define RAIO_WRpin 24 + + +// declaration of a union (see RAIO8870.h) +// ---------------------------------------------------------- +extern union my_union number; + + +// initialization of GPIO and SPI +// ---------------------------------------------------------- +void TFT_init_board( void ); + + +// hard reset of the graphic controller and the tft +// ---------------------------------------------------------- +void TFT_hard_reset( void ); + + +// wait during raio is busy +// ---------------------------------------------------------- +void TFT_wait_for_raio ( void ); + + +// write byte to register +// ---------------------------------------------------------- +void TFT_RegWrite( uint16_t reg ); + + +// write byte to tft +// ---------------------------------------------------------- +void TFT_DataWrite( uint16_t data ); + + +// read byte from tft +// ---------------------------------------------------------- +uint16_t TFT_DataRead( ); + + +// read RAIO status +uint16_t TFT_StatusRead(); + + +// write 'count'-bytes to tft +// ---------------------------------------------------------- +void TFT_DataMultiWrite( uint16_t *data, uint32_t count ); + +#endif diff --git a/SW/tft_test/tft_test.c b/SW/tft_test/tft_test.c new file mode 100644 index 0000000..1c44fea --- /dev/null +++ b/SW/tft_test/tft_test.c @@ -0,0 +1,132 @@ +/*##############################################################*/ +/* */ +/* File : tft_test.c */ +/* */ +/* Project : TFT for Raspberry Pi Revision 2 */ +/* */ +/* Date : 2014-08-14 last update: 2014-08-14 */ +/* */ +/* 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 contains the main loop. The main loop uses the */ +/* functions from examples.h to demonstrate different kind */ +/* of opportunities. */ +/* */ +/* */ +/* 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 . */ +/* */ +/* */ +/* Revision History: */ +/* */ +/* Version 1.0 - Initial release */ +/* */ +/* */ +/* */ +/*##############################################################*/ + +#include +#include +#include +#include +#include "tft.h" +//#include "RAIO8870.h" +#include "bmp.h" +#include "examples.h" + + +int main( int argc, char **argv ) +{ + char my_filename[] = "../bmp/admatec.bmp"; + + if (!bcm2835_init()) + return 1; + + + TFT_init_board(); + TFT_hard_reset(); + + RAIO_init(); + RAIO_SetBacklightPWMValue( 255 ); + + + // draw with RAIO + // --------------------------------------------- + example_DrawWithRAIO(); + + delay(5000); + + + // delete screen and write text + // --------------------------------------------- + example_WriteText( "C-Berry" ); + + delay(5000); + + + // draw sinus + // --------------------------------------------- + example_DrawFunction( SIN ); + + delay(5000); + + + // test soft buttons + // --------------------------------------------- + example_Touch(); + + + // depict a BMP file + // -------- ------------------------------------- + example_DepictBMP( &my_filename[0] ); + + delay(5000); + + + + bcm2835_close(); + + return 0; +} + + + + + + + + + + + + + + + + + diff --git a/doc/C-Berry_Touch.pdf b/doc/C-Berry_Touch.pdf new file mode 100644 index 0000000..155cea2 Binary files /dev/null and b/doc/C-Berry_Touch.pdf differ diff --git a/doc/RA8870.pdf b/doc/RA8870.pdf new file mode 100644 index 0000000..50452b0 Binary files /dev/null and b/doc/RA8870.pdf differ