Library for Mini-DK board

Dependencies:   SPI_TFT_ILI9320

Dependents:   LPC1768_Mini-DK_EasyWeb_DM9161 LPC1768_Mini-DK LPC1768_Mini-DK

Fork of Mini-DK by Frank Vannieuwkerke

Mini-DK board overview (Micro SD connector is at the bottom side)

One serial interface , uses CP2102 (USB to RS232 interface, support ISP download )

RJ45-10/100M Ethernet network interface (Ethernet PHY: DM9161)

2.8 inch TFT color LCD interface (SPI interface or 16Bit parallel interface)

Touch panel controller XPT2046 (ADS7843 compatible)

USB 2.0 interface, USB host and USB Device interface.

TF SD / MMC card (SPI) interface.

Two user buttons, One Reset button and ISP button , One INT0 button, two user-programmable LED lights

Serial ISP download, Standard 20-pin JTAG download simulation debugging interface.

Selection between external 5V power supply or USB 5V supply.

Board size: 95mm * 78mm

All IO available on extension connectors

/media/uploads/frankvnk/mini-dk_top.jpg

04/01/13

Erik Olieman (http://mbed.org/users/Sissors/) joined the code development for the Mini-DK board.

Thanks to his input, we were able to obtain a tremendous speed gain, remove warnings, ...

An overview of all modifications is stored in modifs.h

The old page (http://mbed.org/users/frankvnk/code/LPC1768_Mini-DK/) contains the demo code.

IMPORTANT : Due to a change in the mbed libraries (Stream()), we cannot use the printf instruction - we need to use <SPI_TFT>.printf (example - see main.cpp in http://mbed.org/users/frankvnk/code/LPC1768_Mini-DK/)

WARNING: filetoflash (SD to CPU flash)

The SPI_TFT library contains an option to copy an image from the SD card to the CPU flash memory. This allows you to use an image as background without speed loss when writing other text and graphics.

By default, this option is enabled.

It can be disabled by uncommenting the #define mentioned below in Mini_DK.h:

#define NO_FLASH_BUFFER

Since the flash memory has limited write endurance, DO NOT use this feature when you intend to read multiple images from the SD card (eg: when used as a photo frame).

14/01/13

A newer version of the Mini-DK has been released by the manufacturer: Mini-DK2. They replaced the DM9161 PHY with a LAN8720A PHY and better buttons are fitted on the board. All other hardware remains the same. Code for this PHY is available from the NXP MCU SW application team. This allows us to use the mbed 'EthernetInterface' library with little modifications. Further info - see http://mbed.org/forum/mbed/topic/3684/?page=1#comment-18473.

Notes:

The code in 'lpc_phy_lan8720.c' uses 'msDelay' - needs to be replaced with 'osDelay'.

A custom MAC address can be defined using following code:

extern "C" void mbed_mac_address(char * mac) {
 
// define your own MAC Address
  mac[0] = 0x00;  
  mac[1] = 0x01;  
  mac[2] = 0x02;  
  mac[3] = 0x03;  
  mac[4] = 0x04;  
  mac[5] = 0x05;           
  
};

Mini-DK/TouchADS7843/Touch.cpp

Committer:
Sissors
Date:
2013-01-03
Revision:
5:781a72d380a1
Parent:
3:fb4d62b5ffb3
Child:
7:ffdd4e75b366

File content as of revision 5:781a72d380a1:

// Code based on Carlos E. Vidales tutorial : How To Calibrate Touch Screens
// http://www.embedded.com/design/configurable-systems/4023968/How-To-Calibrate-Touch-Screens

#include "Touch.h"
#include "mbed.h"
#include "Arial12x12.h"

/*Coordinate ScreenSample[3] =   {
        { 45, 45 },
        { 45, 270},
        { 190,190}
} ;*/
/*Coordinate DisplaySample[3] =   {
        { 45, 45 },
        { 45, 270},
        { 190,190}
} ;*/

#define THRESHOLD 2

TouchScreenADS7843::TouchScreenADS7843(PinName tp_mosi, PinName tp_miso, PinName tp_sclk, PinName tp_cs, PinName tp_irq, SPI_TFT *_LCD)
        : LCD(_LCD), _tp_spi(tp_mosi, tp_miso, tp_sclk), _tp_cs(tp_cs), _tp_irq(tp_irq)
        {
            DisplaySample[0].x=45;
            DisplaySample[0].y=45;
            DisplaySample[1].x=45;
            DisplaySample[1].y=270;
            DisplaySample[2].x=190;
            DisplaySample[2].y=190;
            ScreenSample[0].x=45;
            ScreenSample[0].y=45;
            ScreenSample[1].x=45;
            ScreenSample[1].y=270;
            ScreenSample[2].x=190;
            ScreenSample[2].y=190;
            _tp_cs=1;
            _tp_spi.frequency(500000);
            _tp_spi.format(8,0);                    // 8 bit spi mode 0
        }

int TouchScreenADS7843::Read_XY(unsigned char XY)
{
    unsigned char msb, lsb;
    unsigned int Temp;

    Temp=0;
    _tp_cs=0;
    wait_us(SPI_RD_DELAY);
    _tp_spi.write(XY);
    wait_us(SPI_RD_DELAY);
    msb = _tp_spi.write(0x00); // msb
    wait_us(SPI_RD_DELAY);
    lsb = _tp_spi.write(0x00); // lsb
    _tp_cs=1;
    Temp = ((msb << 8 ) | lsb);
    Temp >>= 3;
    Temp &= 0xfff;
    Temp /= 4;        // Scaling : return value range must be between 0 and 1024
//    Temp = (((msb & 0x7f) <<8) | lsb) >> 3;        // 12 bit
    return(Temp);
}


void TouchScreenADS7843::TP_GetAdXY(int *x,int *y)
{
    int adx,ady;
    adx = Read_XY(CHX);
    wait_us(1);
    ady = Read_XY(CHY);
    *x = adx;
    *y = ady;
}

void TouchScreenADS7843::TP_DrawPoint(unsigned int Xpos,unsigned int Ypos, unsigned int color)
{
    LCD->wr_reg(0x03, 0x1030);
    LCD->WindowMax();
    LCD->pixel(Xpos,Ypos,color);
    LCD->pixel(Xpos+1,Ypos,color);
    LCD->pixel(Xpos,Ypos+1,color);
    LCD->pixel(Xpos+1,Ypos+1,color);
}

void TouchScreenADS7843::DrawCross(unsigned int Xpos,unsigned int Ypos)
{
    LCD->line(Xpos-15,Ypos,Xpos-2,Ypos,White);
    LCD->line(Xpos+2,Ypos,Xpos+15,Ypos,White);
    LCD->line(Xpos,Ypos-15,Xpos,Ypos-2,White);
    LCD->line(Xpos,Ypos+2,Xpos,Ypos+15,White);

    LCD->line(Xpos-15,Ypos+15,Xpos-7,Ypos+15,DarkGrey);
    LCD->line(Xpos-15,Ypos+7,Xpos-15,Ypos+15,DarkGrey);

    LCD->line(Xpos-15,Ypos-15,Xpos-7,Ypos-15,DarkGrey);
    LCD->line(Xpos-15,Ypos-7,Xpos-15,Ypos-15,DarkGrey);

    LCD->line(Xpos+7,Ypos+15,Xpos+15,Ypos+15,DarkGrey);
    LCD->line(Xpos+15,Ypos+7,Xpos+15,Ypos+15,DarkGrey);

    LCD->line(Xpos+7,Ypos-15,Xpos+15,Ypos-15,DarkGrey);
    LCD->line(Xpos+15,Ypos-15,Xpos+15,Ypos-7,DarkGrey);
}

unsigned char TouchScreenADS7843::Read_Ads7846(Coordinate * screenPtr)
{
    int m0,m1,m2,TP_X[1],TP_Y[1],temp[3];
    uint8_t count=0;
    int buffer[2][9]={{0},{0}};
    if (screenPtr == NULL) screenPtr = &screen;
    do
    {
        TP_GetAdXY(TP_X,TP_Y);
        buffer[0][count]=TP_X[0];
        buffer[1][count]=TP_Y[0];
        count++;
    }
    while(!_tp_irq && (count < 9));
    if(count==9)
    {
        temp[0]=(buffer[0][0]+buffer[0][1]+buffer[0][2])/3;
        temp[1]=(buffer[0][3]+buffer[0][4]+buffer[0][5])/3;
        temp[2]=(buffer[0][6]+buffer[0][7]+buffer[0][8])/3;
        m0=temp[0]-temp[1];
        m1=temp[1]-temp[2];
        m2=temp[2]-temp[0];
        m0=m0>0?m0:(-m0);
        m1=m1>0?m1:(-m1);
        m2=m2>0?m2:(-m2);
        if( (m0>THRESHOLD)  &&  (m1>THRESHOLD)  &&  (m2>THRESHOLD) ) return 0;
        if(m0<m1)
        {
            if(m2<m0)
                screenPtr->x=(temp[0]+temp[2])/2;
            else
                screenPtr->x=(temp[0]+temp[1])/2;
        }
        else if(m2<m1)
            screenPtr->x=(temp[0]+temp[2])/2;
        else
            screenPtr->x=(temp[1]+temp[2])/2;

        temp[0]=(buffer[1][0]+buffer[1][1]+buffer[1][2])/3;
        temp[1]=(buffer[1][3]+buffer[1][4]+buffer[1][5])/3;
        temp[2]=(buffer[1][6]+buffer[1][7]+buffer[1][8])/3;
        m0=temp[0]-temp[1];
        m1=temp[1]-temp[2];
        m2=temp[2]-temp[0];
        m0=m0>0?m0:(-m0);
        m1=m1>0?m1:(-m1);
        m2=m2>0?m2:(-m2);
        if( (m0>THRESHOLD)  &&  (m1>THRESHOLD)  &&  (m2>THRESHOLD) ) return 0;

        if(m0<m1)
        {
            if(m2<m0)
                screenPtr->y=(temp[0]+temp[2])/2;
            else
                screenPtr->y=(temp[0]+temp[1])/2;
        }
        else if(m2<m1)
            screenPtr->y=(temp[0]+temp[2])/2;
        else
            screenPtr->y=(temp[1]+temp[2])/2;
        return 1;
    }
    return 0;
}

uint8_t TouchScreenADS7843::setCalibrationMatrix( Coordinate * displayPtr,
        Coordinate * screenPtr,
        Matrix * matrixPtr)
{
    uint8_t retTHRESHOLD = 0 ;
    // K = (Xs0 - Xs2)*(Ys1 - Ys2) - (Xs1 - Xs2)*(Ys0 - Ys2)
    matrixPtr->Divider = ((screenPtr[0].x - screenPtr[2].x) * (screenPtr[1].y - screenPtr[2].y)) -
                         ((screenPtr[1].x - screenPtr[2].x) * (screenPtr[0].y - screenPtr[2].y)) ;
    if( matrixPtr->Divider == 0 )
    {
        retTHRESHOLD = 1;
    }
    else
    {
        //                 (Xd0 - Xd2)*(Ys1 - Ys2) - (Xd1 - Xd2)*(Ys0 - Ys2)
        //            A = ---------------------------------------------------
        //                                   K
        matrixPtr->An = ((displayPtr[0].x - displayPtr[2].x) * (screenPtr[1].y - screenPtr[2].y)) -
                          ((displayPtr[1].x - displayPtr[2].x) * (screenPtr[0].y - screenPtr[2].y)) ;
        //                 (Xs0 - Xs2)*(Xd1 - Xd2) - (Xd0 - Xd2)*(Xs1 - Xs2)
        //            B = ---------------------------------------------------
        //                                   K
        matrixPtr->Bn = ((screenPtr[0].x - screenPtr[2].x) * (displayPtr[1].x - displayPtr[2].x)) -
                        ((displayPtr[0].x - displayPtr[2].x) * (screenPtr[1].x - screenPtr[2].x)) ;
        //                 Ys0*(Xs2*Xd1 - Xs1*Xd2) + Ys1*(Xs0*Xd2 - Xs2*Xd0) + Ys2*(Xs1*Xd0 - Xs0*Xd1)
        //            C = ----------------------------------------------------------------------------
        //                                   K
        matrixPtr->Cn = (screenPtr[2].x * displayPtr[1].x - screenPtr[1].x * displayPtr[2].x) * screenPtr[0].y +
                        (screenPtr[0].x * displayPtr[2].x - screenPtr[2].x * displayPtr[0].x) * screenPtr[1].y +
                        (screenPtr[1].x * displayPtr[0].x - screenPtr[0].x * displayPtr[1].x) * screenPtr[2].y ;
        //                 (Yd0 - Yd2)*(Ys1 - Ys2) - (Yd1 - Yd2)*(Ys0 - Ys2)
        //            D = ---------------------------------------------------
        //                                   K
        matrixPtr->Dn = ((displayPtr[0].y - displayPtr[2].y) * (screenPtr[1].y - screenPtr[2].y)) -
                        ((displayPtr[1].y - displayPtr[2].y) * (screenPtr[0].y - screenPtr[2].y)) ;
        //                 (Xs0 - Xs2)*(Yd1 - Yd2) - (Yd0 - Yd2)*(Xs1 - Xs2)
        //            E = ---------------------------------------------------
        //                                   K
        matrixPtr->En = ((screenPtr[0].x - screenPtr[2].x) * (displayPtr[1].y - displayPtr[2].y)) -
                        ((displayPtr[0].y - displayPtr[2].y) * (screenPtr[1].x - screenPtr[2].x)) ;
        //                 Ys0*(Xs2*Yd1 - Xs1*Yd2) + Ys1*(Xs0*Yd2 - Xs2*Yd0) + Ys2*(Xs1*Yd0 - Xs0*Yd1)
        //            F = ----------------------------------------------------------------------------
        //                                   K
        matrixPtr->Fn = (screenPtr[2].x * displayPtr[1].y - screenPtr[1].x * displayPtr[2].y) * screenPtr[0].y +
                        (screenPtr[0].x * displayPtr[2].y - screenPtr[2].x * displayPtr[0].y) * screenPtr[1].y +
                        (screenPtr[1].x * displayPtr[0].y - screenPtr[0].x * displayPtr[1].y) * screenPtr[2].y ;
    }
    return( retTHRESHOLD ) ;
}

uint8_t TouchScreenADS7843::getDisplayPoint(void)
{
    uint8_t retTHRESHOLD = 0 ;

    if( matrix.Divider != 0 )
    {
        // XD = AX+BY+C
        display.x = ( (matrix.An * screen.x) +
                          (matrix.Bn * screen.y) +
                           matrix.Cn
                         ) / matrix.Divider ;
        // YD = DX+EY+F
        display.y = ( (matrix.Dn * screen.x) +
                          (matrix.En * screen.y) +
                           matrix.Fn
                         ) / matrix.Divider ;
    }
    else
    {
        retTHRESHOLD = 1;
    }
    return(retTHRESHOLD);
}

void TouchScreenADS7843::TouchPanel_Calibrate(void)
{
    uint8_t i;
    Coordinate screen_cal;
    setCalibrationMatrix( &DisplaySample[0],&ScreenSample[0],&matrix) ;
    LCD->set_font((unsigned char*) Arial12x12);
    for(i=0;i<3;i++)
    {
        LCD->cls();
        LCD->locate(10,10);
        LCD->printf("Touch crosshair to calibrate");
        wait_ms(500);
        DrawCross(DisplaySample[i].x,DisplaySample[i].y);
        do {} while (!Read_Ads7846(&screen_cal));
        ScreenSample[i].x= screen_cal.x;ScreenSample[i].y= screen_cal.y;
    }
    setCalibrationMatrix( &DisplaySample[0],&ScreenSample[0],&matrix) ;
    LCD->cls();
}