These are the examples provided for [[/users/frank26080115/libraries/LPC1700CMSIS_Lib/]] Note, the entire "program" is not compilable!

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers glcd_ll.c Source File

glcd_ll.c

00001 /*************************************************************************
00002  *
00003  *
00004  *    (c) Copyright IAR Systems 2006
00005  *
00006  *    File name   : glcd_ll.c
00007  *    Description : GLCD low level functions
00008  *
00009  *    History :
00010  *    1. Date        : December 2, 2006
00011  *       Author      : Stanimir Bonev
00012  *       Description : Create
00013  *
00014  *    $Revision: 30123 $
00015  **************************************************************************/
00016 #include "glcd_ll.h"
00017 #include "lpc_types.h"
00018 #include "LPC17xx.h"
00019 #include "lpc17xx_clkpwr.h"
00020 
00021 /*************************************************************************
00022  * Function Name: GLCD_SetReset
00023  * Parameters: Boolean State
00024  * Return: none
00025  *
00026  * Description: Set reset pin state
00027  *
00028  *************************************************************************/
00029 void GLCD_SetReset (Bool State)
00030 {
00031     if(State)
00032     {
00033         LPC_GPIO3->FIOSET |= (1UL<<25);
00034     }
00035     else
00036     {
00037         LPC_GPIO3->FIOCLR |= (1<<25);
00038     }
00039 }
00040 
00041 /*************************************************************************
00042  * Function Name: GLCD_Backlight
00043  * Parameters: Int8U Light
00044  * Return: none
00045  *
00046  * Description: Set backlight pin state
00047  *
00048  *************************************************************************/
00049 void GLCD_Backlight (uint8_t Light)
00050 {
00051     LPC_PWM1->MR3 = BACKLIGHT_OFF + Light;
00052     LPC_PWM1->LER |= (1<<3);
00053 }
00054 
00055 /*************************************************************************
00056  * Function Name: GLCD_LLInit
00057  * Parameters: none
00058  * Return: none
00059  *
00060  * Description: Init Reset and Backlight control outputs
00061  *
00062  *************************************************************************/
00063 void GLCD_LLInit (void)
00064 {
00065     // LCD Reset output
00066     LPC_GPIO3->FIODIR |= (1UL<<25);
00067 
00068     GLCD_SetReset(0);
00069 
00070     // LCD backlight PWM 8bit init
00071     LPC_PINCON->PINSEL7 |= (3<<20) ;    // assign P3.26 to PWM1.3
00072     LPC_SC->PCONP |= (1<<6);            // enable clock of PWM1
00073     LPC_PWM1->TCR &= ~(1<<3);
00074     LPC_PWM1->TCR &= ~(1<<0);           // disable counting
00075     LPC_PWM1->TCR |= (1<<1);            // reset
00076     LPC_PWM1->CTCR &= ~(3<<0);          // from prescaler
00077     LPC_PWM1->MCR = 2 ;                 // Reset on PWMMR0
00078     LPC_PWM1->PCR &= ~(1<<3);           // Selects single edge controlled mode for PWM3
00079     LPC_PWM1->PCR |= (1<<11);           // The PWM3 output enabled
00080 
00081     LPC_PWM1->PR = 0;
00082     LPC_PWM1->MR0 = 0xFF;               // 8bit resolution
00083     LPC_PWM1->LER |= (1<<0);
00084     LPC_PWM1->MR3 = 0;
00085     LPC_PWM1->LER |= (1<<3);
00086     LPC_PWM1->TCR |= (1<<3);            // enable PWM function
00087     LPC_PWM1->TCR &= ~(1<<1);           // release reset
00088     LPC_PWM1->TCR |= (1<<0);            // enable counting
00089     GLCD_Backlight(0);
00090 }
00091 
00092 /*************************************************************************
00093  * Function Name: GLCD_SPI_ChipSelect
00094  * Parameters: Boolean Select
00095  * Return: none
00096  *
00097  * Description: SSP0 Chip select control
00098  * Select = true  - Chip is enable
00099  * Select = false - Chip is disable
00100  *
00101  *************************************************************************/
00102 void GLCD_SPI_ChipSelect (Bool Select)
00103 {
00104     if (Select)
00105     {
00106         LPC_GPIO1->FIOCLR |= (1<<21);
00107     }
00108     else
00109     {
00110         LPC_GPIO1->FIOSET |= (1UL<<21);
00111     }
00112 }
00113 
00114 /*************************************************************************
00115  * Function Name: GLCD_SPI_SetWordWidth
00116  * Parameters: Int32U Width
00117  * Return: Boolean
00118  *
00119  * Description: Set SSP 0 word width
00120  *
00121  *************************************************************************/
00122 Bool GLCD_SPI_SetWordWidth (uint32_t Width)
00123 {
00124     if(4 > Width || Width > 16)
00125     {
00126         return(FALSE);
00127     }
00128 
00129     LPC_SSP0->CR0 &= ~(0x0F);
00130     LPC_SSP0->CR0 |= (Width - 1) & 0x0F;
00131     return(TRUE);
00132 }
00133 
00134 /*************************************************************************
00135  * Function Name: GLCD_SPI_SetClockFreq
00136  * Parameters: Int32U Frequency
00137  * Return: Int32U
00138  *
00139  * Description: Set SSP 0 clock
00140  *
00141  *************************************************************************/
00142 uint32_t GLCD_SPI_SetClockFreq (uint32_t Frequency)
00143 {
00144     uint32_t Fspi = CLKPWR_GetPCLK(CLKPWR_PCLKSEL_SSP0);
00145     uint32_t Div = 2;
00146     while((Div * Frequency) < Fspi)
00147     {
00148         if((Div += 2) == 254)
00149         {
00150             break;
00151         }
00152     }
00153     LPC_SSP0->CPSR = Div;
00154     return(Fspi/Div);
00155 }
00156 
00157 /*************************************************************************
00158  * Function Name: GLCD_SPI_Init
00159  * Parameters: uint32_t Clk, uint32_t Width
00160  * Return: none
00161  *
00162  * Description: Init SSP0
00163  *
00164  *************************************************************************/
00165 void GLCD_SPI_Init(uint32_t Clk, uint32_t Width)
00166 {
00167     uint32_t i;
00168     volatile uint32_t Dummy;
00169 //  Assign GPIO to SSP0 - SCK, MOSI, MISO
00170     LPC_PINCON->PINSEL3 |= (3<<8);  // SCK0 - P1.20
00171     LPC_PINCON->PINSEL3 |= (3<<14); // MISO0 - P1.23
00172     LPC_PINCON->PINSEL3 |= (3<<16); // MOSI0 - P1.24
00173 
00174   // Chip select
00175     LPC_GPIO1->FIODIR |= (1UL<<21); //SSEL0 used as GPIOP1.21
00176 
00177     GLCD_SPI_ChipSelect(FALSE);
00178 
00179   // Spi init
00180     LPC_SC->PCONP |= (1<<21);   // SSP0 clock enable
00181     LPC_SSP0->CR1  &= ~(1<<1);  // Disable module
00182     LPC_SSP0->CR1  &= ~(1<<0);  // Disable Loop Back Mode
00183     LPC_SSP0->CR1  &= ~(1<<2);  // Master mode
00184     LPC_SSP0->CR0  &= ~(3<<4);  // SPI
00185     LPC_SSP0->CR0 &= ~(1<<6);   // CPOL = 0
00186     LPC_SSP0->CR0 &= ~(1<<7);   // CPHA = 0
00187     LPC_SSP0->IMSC = 0;         // disable all interrupts
00188     LPC_SSP0->DMACR = 0;        // disable DMA
00189     LPC_SSP0->CR1  |= (1<<1);   // Enable module
00190     for (i = 0; i < 8; i++ )
00191     {
00192         Dummy = LPC_SSP0->DR; // clear the RxFIFO
00193     }
00194     // Set SSP clock frequency
00195     GLCD_SPI_SetClockFreq(Clk);
00196     // Set data width
00197     GLCD_SPI_SetWordWidth(Width);
00198 }
00199 
00200 /*************************************************************************
00201  * Function Name: GLCD_SPI_TranserByte
00202  * Parameters: uint32_t Data
00203  * Return: uint32_t
00204  *
00205  * Description: Transfer byte from SSP0
00206  *
00207  *************************************************************************/
00208 uint32_t GLCD_SPI_TranserByte (uint32_t Data)
00209 {
00210     while(!(LPC_SSP0->SR & (1<<1))); //check bit TNF
00211     LPC_SSP0->DR = Data;
00212     while((LPC_SSP0->SR & (1<<4))); //check bit BSY
00213     return(LPC_SSP0->DR);
00214 }
00215 
00216 /*************************************************************************
00217  * Function Name: GLCD_SPI_SendBlock
00218  * Parameters: pInt8U pData, uint32_t Size
00219  *
00220  * Return: void
00221  *
00222  * Description: Write block of data to SSP
00223  *
00224  *************************************************************************/
00225 void GLCD_SPI_SendBlock (unsigned char *pData, uint32_t Size)
00226 {
00227     volatile uint32_t Dummy;
00228     uint32_t OutCount = Size;
00229     while (OutCount)
00230     {
00231         while((LPC_SSP0->SR & (1<<1)) && OutCount)
00232         {
00233             LPC_SSP0->DR = *pData++ | 0x100;  // Data
00234             --OutCount;
00235         }
00236     }
00237     while(LPC_SSP0->SR & (1<<4)); //check bit BSY
00238   // draining RX Fifo
00239     while (LPC_SSP0->SR &(1<<2)) // check bit RNE
00240     {
00241         Dummy = LPC_SSP0->DR;
00242     }
00243 }
00244 
00245 /*************************************************************************
00246  * Function Name: GLCD_SPI_ReceiveBlock
00247  * Parameters: unsigned char * pData, uint32_t Size
00248  *
00249  * Return: void
00250  *
00251  * Description: Read block of data from SSP
00252  *
00253  *************************************************************************/
00254 void GLCD_SPI_ReceiveBlock (unsigned char *pData, uint32_t Size)
00255 {
00256     uint32_t Delta = 0;
00257     while (Size || Delta)
00258     {
00259         while((LPC_SSP0->SR & (1<<1)) && (Delta < SSP_FIFO_SIZE) && Size)
00260         {
00261             LPC_SSP0->DR = 0xFFFF;
00262             --Size; ++Delta;
00263         }
00264         while (LPC_SSP0->SR & (1<<2)) //check bit RNE
00265         {
00266             *pData++ = LPC_SSP0->DR;
00267             --Delta;
00268         }
00269     }
00270 }