tftspi.c

Go to the documentation of this file.
00001 /**************************************************************************/
00018 #include "em_device.h"
00019 #include "em_usart.h"
00020 #include "em_gpio.h"
00021 #include "em_cmu.h"
00022 #include "tftspi.h"
00023 
00025 static const USART_InitSync_TypeDef inittft =
00026 { usartEnable,     /* Enable RX/TX when init completed. */
00027   48000000,        /* Use 48MHz reference clock */
00028   1000000,         /* 7 Mbits/s. */
00029   usartDatabits9,  /* 9 databits. */
00030   true,            /* Master mode. */
00031   true,            /* Send most significant bit first. */
00032   usartClockMode3, /* Clock idle low, sample on rising edge. */
00033   false,
00034   usartPrsRxCh0,
00035   false };
00036 
00037 
00038 /**************************************************************************/
00045 void SPI_TFT_Init(void)
00046 {
00047   /* Enabling clock to USART */
00048   CMU_ClockEnable(cmuClock_HFPER, true);
00049   CMU_ClockEnable(cmuClock_USART1, true);
00050   CMU_ClockEnable(cmuClock_GPIO, true);
00051 
00052   /* IO configuration (USART 1, Location #1) */
00053   GPIO_PinModeSet(gpioPortD, 0, gpioModePushPull, 0); /* TX - MOSI */
00054   GPIO_PinModeSet(gpioPortD, 1, gpioModeInput, 0);    /* RX - MISO */
00055   GPIO_PinModeSet(gpioPortD, 2, gpioModePushPull, 0); /* CLK */
00056   GPIO_PinModeSet(gpioPortD, 3, gpioModePushPull, 1); /* CS  */
00057 
00058   /* Ensure out of reset configuration */
00059   USART_Reset(USART1);
00060 
00061   /* Initialize USART1, in SPI master mode. */
00062   USART_InitSync(USART1, &inittft);
00063 
00064   USART1->ROUTE =
00065     USART_ROUTE_TXPEN |
00066     USART_ROUTE_RXPEN |
00067     USART_ROUTE_CLKPEN |
00068     USART_ROUTE_LOCATION_LOC1;
00069 }
00070 
00071 
00072 /**************************************************************************/
00082 void SPI_TFT_WriteRegister(uint8_t reg, uint16_t data)
00083 {
00084   /* Enable chip select */
00085   GPIO_PinOutClear(gpioPortD, 3);
00086 
00087   /* Select register first */
00088   USART1->CTRL = USART1->CTRL & ~USART_CTRL_BIT8DV;
00089 
00090   USART_Tx(USART1, reg & 0xFF);
00091   USART_Rx(USART1);
00092 
00093   /* Write data */
00094   USART1->CTRL = USART1->CTRL | USART_CTRL_BIT8DV;
00095   USART_Tx(USART1, (data & 0xff00) >> 8);
00096   USART_Rx(USART1);
00097   USART_Tx(USART1, (data & 0x00ff));
00098   USART_Rx(USART1);
00099 
00100   /* Clear chip select */
00101   GPIO_PinOutSet(gpioPortD, 3);
00102 }