LPCxpresso 1769 with SPI and Drivers

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

LPCxpresso 1769 with SPI and Drivers

1,085 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Carsten on Sun Sep 04 22:47:09 MST 2011
Hello,

I am trying to use the SPI on the Lpc1769, therefore the libs lpc17xx_spi.c where used. The Exampels includes a polling project, which I used for the correct implementation of the lpc17xx_spi.c.

The connected Display showed no reaction, so I checked the output and recordniced that the MOSI always seems to be high and the SCK doesn't work. I have no oscilloscope and used an LED instead to see if something happens.

Could it be possible that if destroyed the SPI-Part of the chip with my unprofessioal test. I did even more then only connecting the led.

I will try to poste my code this evening but maybe someone has a essential idea what I made wrong by using the given code red drivers.

Best regads

Carsten
0 Kudos
Reply
6 Replies

992 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Carsten on Tue Sep 06 11:39:50 MST 2011
Hello,

sorry that I only posted the code and not the hole project.

I expected that the SPI status is check when I use SPI_SendData, my mistake.

I used your modified function and everythings works fine. For the future i will use the SPI_ReadWrite() because of the touch function of the display.

Best regards and many thanks for your help and code analysis

Carsten
0 Kudos
Reply

992 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Ex-Zero on Mon Sep 05 12:23:28 MST 2011
#1 Please post a complete project :eek:

#2 CS / RST / MISO / MOSI / SCK are working

#3 LED and 1k resistor are showing #2

#4 Your code isn't working because you use SPI_SendData() without reading SPI status. So you've sucessfully mutilated this sample :mad:
  Either you use SPI_ReadWrite() or you add a simple status check to SPI_SendData()

 void SPI_SendData(LPC_SPI_TypeDef* SPIx, uint16_t Data)
{
 uint32_t stat;
 SPIx->SPDR = Data & SPI_SPDR_BITMASK;
 while (!((stat = SPIx->SPSR) & SPI_SPSR_SPIF));
}
#5 Just tried your code with a Watterott MI0283QT-2:

http://www.youtube.com/watch?v=xW57ltzszwI
0 Kudos
Reply

992 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Carsten on Mon Sep 05 09:04:13 MST 2011
Hello,

here is my code:

/*
===============================================================================
 Name        : main.c
 Author      : 
 Version     :
 Copyright   : Copyright (C) 
 Description : main definition
===============================================================================
*/

#ifdef __USE_CMSIS
#include "LPC17xx.h"
#endif

#include <cr_section_macros.h>
#include <NXP/crp.h>

// Variable to store CRP value in. Will be placed automatically
// by the linker when "Enable Code Read Protect" selected.
// See crp.h header for more information
__CRP const unsigned int CRP_WORD = CRP_NO_CRP ;

#include "lpc17xx_spi.h"
#include "lpc17xx_clkpwr.h"
#include "lpc17xx_pinsel.h"
#include "lpc17xx_gpio.h"

/************************** PRIVATE DEFINITONS **********************/
// PORT number that /CS pin assigned on
#define CS_PORT_NUM        0
// PIN number that  /CS pin assigned on
#define CS_PIN_NUM        16

#define RST_PORT_NUM    0
#define RST_PIN_NUM        2


// SPI Configuration structure variable
SPI_CFG_Type SPI_ConfigStruct;

// Prototypes
int initDisp(uint8_t retries);
void CS_Init(void);

void lcd_clear(unsigned int color);
void lcd_draw(unsigned int color);
void lcd_drawstop(void);
void lcd_drawstart(void);
void lcd_cmd(unsigned int reg, unsigned int param);
void lcd_data(unsigned int c);
void lcd_reset(void);
void delay_ms(int n);

// Defines
#define LCD_WIDTH            (320)
#define LCD_HEIGHT           (240)
#define RGB(r,g,b)           (((r&0xF8)<<8)|((g&0xFC)<<3)|((b&0xF8)>>3)) //5 red | 6 green | 5 blue

#define LCD_ID               (0)
#define LCD_DATA             ((0x72)|(LCD_ID<<2))
#define LCD_REGISTER         ((0x70)|(LCD_ID<<2))

int main(void)
{
    // Init Display
    initDisp(0);
    delay_ms(1000);
    lcd_reset();
    delay_ms(1000);
    
    unsigned int color = 0;

    while(1)
    {
        lcd_clear(color);
        color += 100;

        if (color > 65535)
        {
            color = 0;
        }

        delay_ms(100);
    }

    return 0;
}

/*********************************************************************//**
 * @brief         Initialize CS pin as GPIO function to drive /CS pin
 *                 due to definition of CS_PORT_NUM and CS_PORT_NUM
 * @param        None
 * @return        None
 ***********************************************************************/
void CS_Init(void)
{
    GPIO_SetDir(CS_PORT_NUM, (1<<CS_PIN_NUM), 1);
    GPIO_SetValue(CS_PORT_NUM, (1<<CS_PIN_NUM));
}

void RST_Init(void)
{
    GPIO_SetDir(RST_PORT_NUM, (1<<RST_PIN_NUM), 1);
    GPIO_SetValue(RST_PORT_NUM, (1<<RST_PIN_NUM));
}

/*********************************************************************//**
 * @brief         Drive CS output pin to low/high level to select slave device
 *                 via /CS pin state
 * @param[in]    state State of CS output pin that will be driven:
 *                 - 0: Drive CS pin to low level
 *                 - 1: Drive CS pin to high level
 * @return        None
 ***********************************************************************/
void CS_Force(int32_t state)
{
    if (state){
        GPIO_SetValue(CS_PORT_NUM, (1<<CS_PIN_NUM));
    }else{
        GPIO_ClearValue(CS_PORT_NUM, (1<<CS_PIN_NUM));
    }
}

void RST_Force(int32_t state)
{
    if (state){
        GPIO_SetValue(RST_PORT_NUM, (1<<RST_PIN_NUM));
    }else{
        GPIO_ClearValue(RST_PORT_NUM, (1<<RST_PIN_NUM));
    }
}

int initDisp(uint8_t retries)
{
    PINSEL_CFG_Type PinCfg;
    SPI_DATA_SETUP_Type xferConfig;

    /*
     * Initialize SPI pin connect
     * P0.15 - SCK;
     * P0.16 - SSEL - used as GPIO
     * P0.17 - MISO
     * P0.18 - MOSI
     */
    PinCfg.Funcnum = 3;
    PinCfg.OpenDrain = 0;
    PinCfg.Pinmode = 0;
    PinCfg.Portnum = 0;
    PinCfg.Pinnum = 15;
    PINSEL_ConfigPin(&PinCfg);
    PinCfg.Pinnum = 17;
    PINSEL_ConfigPin(&PinCfg);
    PinCfg.Pinnum = 18;
    PINSEL_ConfigPin(&PinCfg);
    PinCfg.Pinnum = 16;
    PinCfg.Funcnum = 0;
    PINSEL_ConfigPin(&PinCfg);

    // initialize SPI configuration structure to default
    SPI_ConfigStructInit(&SPI_ConfigStruct);

    // Initialize SPI peripheral with parameter given in structure above
    SPI_Init(LPC_SPI, &SPI_ConfigStruct);

    // Initialize /CS pin to GPIO function
    CS_Init();

    // Initialize /RST pin to GPIO function
    RST_Init();

    return 0;

}

void lcd_clear(unsigned int color)
{
  unsigned int i;

  lcd_area(0, 0, (LCD_WIDTH-1), (LCD_HEIGHT-1));

  lcd_drawstart();
  for(i=(LCD_WIDTH*LCD_HEIGHT/8); i!=0; i--)
  {
    lcd_draw(color); //1
    lcd_draw(color); //2
    lcd_draw(color); //3
    lcd_draw(color); //4
    lcd_draw(color); //5
    lcd_draw(color); //6
    lcd_draw(color); //7
    lcd_draw(color); //8
  }
  lcd_drawstop();

  return;
}

void lcd_draw(unsigned int color)
{
    SPI_SendData(LPC_SPI, color>>8);
    SPI_SendData(LPC_SPI, color);

  return;
}

void lcd_drawstop(void)
{
  CS_Force(1);

  return;
}

void lcd_drawstart(void)
{
    CS_Force(0);
    SPI_SendData(LPC_SPI, LCD_REGISTER);
    SPI_SendData(LPC_SPI, 0x22);
  CS_Force(1);

  CS_Force(0);
  SPI_SendData(LPC_SPI, LCD_DATA);

  return;
}

void lcd_cmd(unsigned int reg, unsigned int param)
{
    CS_Force(0);
    SPI_SendData(LPC_SPI, LCD_REGISTER);
    SPI_SendData(LPC_SPI, reg);
    CS_Force(1);

    CS_Force(0);
    SPI_SendData(LPC_SPI, LCD_DATA);
    SPI_SendData(LPC_SPI, param);
    CS_Force(1);
}

void lcd_data(unsigned int c)
{
    CS_Force(0);
    SPI_SendData(LPC_SPI, LCD_DATA);
    SPI_SendData(LPC_SPI, c >> 8);
    SPI_SendData(LPC_SPI, c & 0xFF);
      CS_Force(1);
}

void lcd_area(unsigned int x0, unsigned int y0, unsigned int x1, unsigned int y1)
{
  lcd_cmd(0x03, (x0>>0)); //set x0
  lcd_cmd(0x02, (x0>>8)); //set x0
  lcd_cmd(0x05, (x1>>0)); //set x1
  lcd_cmd(0x04, (x1>>8)); //set x1
  lcd_cmd(0x07, (y0>>0)); //set y0
  lcd_cmd(0x06, (y0>>8)); //set y0
  lcd_cmd(0x09, (y1>>0)); //set y1
  lcd_cmd(0x08, (y1>>8)); //set y1
}

void lcd_reset(void)
{
    //reset
    CS_Force(1);
    RST_Force(0);
    delay_ms(50);
    RST_Force(1);
    delay_ms(50);

    //driving ability
    lcd_cmd(0xEA, 0x0000);
    lcd_cmd(0xEB, 0x0020);
    lcd_cmd(0xEC, 0x000C);
    lcd_cmd(0xED, 0x00C4);
    lcd_cmd(0xE8, 0x0040);
    lcd_cmd(0xE9, 0x0038);
    lcd_cmd(0xF1, 0x0001);
    lcd_cmd(0xF2, 0x0010);
    lcd_cmd(0x27, 0x00A3);

    //power voltage
    lcd_cmd(0x1B, 0x001B);
    lcd_cmd(0x1A, 0x0001);
    lcd_cmd(0x24, 0x002F);
    lcd_cmd(0x25, 0x0057);

    //VCOM offset
    lcd_cmd(0x23, 0x008D); //for flicker adjust

    //power on
    lcd_cmd(0x18, 0x0036);
    lcd_cmd(0x19, 0x0001); //start osc
    lcd_cmd(0x01, 0x0000); //wakeup
    lcd_cmd(0x1F, 0x0088);
    delay_ms(5);
    lcd_cmd(0x1F, 0x0080);
    delay_ms(5);
    lcd_cmd(0x1F, 0x0090);
    delay_ms(5);
    lcd_cmd(0x1F, 0x00D0);
    delay_ms(5);

    //color selection
    lcd_cmd(0x17, 0x0005); //0x0005=65k, 0x0006=262k

    //panel characteristic
    lcd_cmd(0x36, 0x0000);

    //display on
    lcd_cmd(0x28, 0x0038);
    delay_ms(40);
    lcd_cmd(0x28, 0x003C);

    //display options
    #ifdef LCD_MIRROR
    lcd_cmd(0x16, 0x0068); //MY=0 MX=1 MV=1 ML=0 BGR=1
    #else
    lcd_cmd(0x16, 0x00A8); //MY=1 MX=0 MV=1 ML=0 BGR=1
    #endif

    lcd_area(0, 0, (LCD_WIDTH-1), (LCD_HEIGHT-1));

    return;
}

void delay_ms(int n)
{
    volatile int d;
    for (d=0; d<n*3000; d++){}
}




The configuration for the SPI is the standard given by the lpc17xx_spi.c.

Best regards

Carsten
0 Kudos
Reply

992 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Ex-Zero on Mon Sep 05 00:17:10 MST 2011
LED + 1k should work :)

Could be good idea to start with ssp sample (MCB1700), but posting your project will show if it's working :eek:
0 Kudos
Reply

992 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Carsten on Sun Sep 04 23:38:09 MST 2011
Hello,

I know this problem but for a first test I await to see that the led will glow if the clock pulse is available.

Best regards
Carsten
0 Kudos
Reply

992 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by OXO on Sun Sep 04 23:29:55 MST 2011
The data on the mosi/miso/spi clock lines is too fast for you to see with an LED.

You really need to borrow a scope.
0 Kudos
Reply