tftspi.c

Go to the documentation of this file.
00001 /**************************************************************************/
00036 #include "em_device.h"
00037 #include "em_usart.h"
00038 #include "em_gpio.h"
00039 #include "em_cmu.h"
00040 #include "tftspi.h"
00041 
00043 static const USART_InitSync_TypeDef inittft =
00044 { usartEnable,     /* Enable RX/TX when init completed. */
00045   48000000,        /* Use 48MHz reference clock */
00046   1000000,         /* 7 Mbits/s. */
00047   usartDatabits9,  /* 9 databits. */
00048   true,            /* Master mode. */
00049   true,            /* Send most significant bit first. */
00050   usartClockMode3, /* Clock idle low, sample on rising edge. */
00051   false,
00052   usartPrsRxCh0,
00053   false };
00054 
00055 
00056 /**************************************************************************/
00063 void SPI_TFT_Init(void)
00064 {
00065   /* Enabling clock to USART */
00066   CMU_ClockEnable(cmuClock_HFPER, true);
00067   CMU_ClockEnable(cmuClock_USART1, true);
00068   CMU_ClockEnable(cmuClock_GPIO, true);
00069 
00070   /* IO configuration (USART 1, Location #1) */
00071   GPIO_PinModeSet(gpioPortD, 0, gpioModePushPull, 0); /* TX - MOSI */
00072   GPIO_PinModeSet(gpioPortD, 1, gpioModeInput, 0);    /* RX - MISO */
00073   GPIO_PinModeSet(gpioPortD, 2, gpioModePushPull, 0); /* CLK */
00074   GPIO_PinModeSet(gpioPortD, 3, gpioModePushPull, 1); /* CS  */
00075 
00076   /* Ensure out of reset configuration */
00077   USART_Reset(USART1);
00078 
00079   /* Initialize USART1, in SPI master mode. */
00080   USART_InitSync(USART1, &inittft);
00081 
00082   USART1->ROUTE =
00083     USART_ROUTE_TXPEN |
00084     USART_ROUTE_RXPEN |
00085     USART_ROUTE_CLKPEN |
00086     USART_ROUTE_LOCATION_LOC1;
00087 }
00088 
00089 
00090 /**************************************************************************/
00100 void SPI_TFT_WriteRegister(uint8_t reg, uint16_t data)
00101 {
00102   /* Enable chip select */
00103   GPIO_PinOutClear(gpioPortD, 3);
00104 
00105   /* Select register first */
00106   USART1->CTRL = USART1->CTRL & ~USART_CTRL_BIT8DV;
00107 
00108   USART_Tx(USART1, reg & 0xFF);
00109   USART_Rx(USART1);
00110 
00111   /* Write data */
00112   USART1->CTRL = USART1->CTRL | USART_CTRL_BIT8DV;
00113   USART_Tx(USART1, (data & 0xff00) >> 8);
00114   USART_Rx(USART1);
00115   USART_Tx(USART1, (data & 0x00ff));
00116   USART_Rx(USART1);
00117 
00118   /* Clear chip select */
00119   GPIO_PinOutSet(gpioPortD, 3);
00120 }