retargetserial.c

Go to the documentation of this file.
00001 /***************************************************************************/
00017 #include <stdio.h>
00018 #include "em_device.h"
00019 #include "em_cmu.h"
00020 #include "em_int.h"
00021 #include "em_gpio.h"
00022 #include "retargetserial.h"
00023 
00024 /***************************************************************************/
00029 #if defined(RETARGET_USART)
00030 #include "em_usart.h"
00031 #endif
00032 
00033 #if defined(RETARGET_LEUART)
00034 #include "em_leuart.h"
00035 #endif
00036 
00037 /* Receive buffer */
00038 #define RXBUFSIZE    8                          
00039 static volatile int     rxReadIndex  = 0;       
00040 static volatile int     rxWriteIndex = 0;       
00041 static volatile int     rxCount      = 0;       
00042 static volatile uint8_t rxBuffer[RXBUFSIZE];    
00043 static uint8_t          LFtoCRLF    = 0;        
00044 static bool             initialized = false;    
00046 /**************************************************************************/
00049 void RETARGET_IRQ_NAME(void)
00050 {
00051 #if defined(RETARGET_USART)
00052   if (RETARGET_UART->STATUS & USART_STATUS_RXDATAV)
00053   {
00054 #else
00055   if (RETARGET_UART->IF & LEUART_IF_RXDATAV)
00056   {
00057 #endif
00058 
00059     /* Store Data */
00060     rxBuffer[rxWriteIndex] = RETARGET_RX(RETARGET_UART);
00061     rxWriteIndex++;
00062     rxCount++;
00063     if (rxWriteIndex == RXBUFSIZE)
00064     {
00065       rxWriteIndex = 0;
00066     }
00067     /* Check for overflow - flush buffer */
00068     if (rxCount > RXBUFSIZE)
00069     {
00070       rxWriteIndex = 0;
00071       rxCount      = 0;
00072       rxReadIndex  = 0;
00073     }
00074   }
00075 }
00076 
00079 /**************************************************************************/
00083 void RETARGET_SerialCrLf(int on)
00084 {
00085   if (on)
00086     LFtoCRLF = 1;
00087   else
00088     LFtoCRLF = 0;
00089 }
00090 
00091 
00092 /**************************************************************************/
00095 void RETARGET_SerialInit(void)
00096 {
00097   /* Configure GPIO pins */
00098   CMU_ClockEnable(cmuClock_GPIO, true);
00099   /* To avoid false start, configure output as high */
00100   GPIO_PinModeSet(RETARGET_TXPORT, RETARGET_TXPIN, gpioModePushPull, 1);
00101   GPIO_PinModeSet(RETARGET_RXPORT, RETARGET_RXPIN, gpioModeInput, 0);
00102 
00103 #if defined(RETARGET_USART)
00104   USART_TypeDef           *usart = RETARGET_UART;
00105   USART_InitAsync_TypeDef init   = USART_INITASYNC_DEFAULT;
00106 
00107   /* Enable DK RS232/UART switch */
00108   RETARGET_PERIPHERAL_ENABLE();
00109 
00110   /* Enable peripheral clocks */
00111   CMU_ClockEnable(cmuClock_HFPER, true);
00112   CMU_ClockEnable(RETARGET_CLK, true);
00113 
00114   /* Configure USART for basic async operation */
00115   init.enable = usartDisable;
00116   USART_InitAsync(usart, &init);
00117 
00118   /* Enable pins at UART1 location #2 */
00119   usart->ROUTE = USART_ROUTE_RXPEN | USART_ROUTE_TXPEN | RETARGET_LOCATION;
00120 
00121   /* Clear previous RX interrupts */
00122   USART_IntClear(RETARGET_UART, USART_IF_RXDATAV);
00123   NVIC_ClearPendingIRQ(RETARGET_IRQn);
00124 
00125   /* Enable RX interrupts */
00126   USART_IntEnable(RETARGET_UART, USART_IF_RXDATAV);
00127   NVIC_EnableIRQ(RETARGET_IRQn);
00128 
00129   /* Finally enable it */
00130   USART_Enable(usart, usartEnable);
00131 
00132 #else
00133   LEUART_TypeDef      *leuart = RETARGET_UART;
00134   LEUART_Init_TypeDef init    = LEUART_INIT_DEFAULT;
00135 
00136   /* Enable DK LEUART/RS232 switch */
00137   RETARGET_PERIPHERAL_ENABLE();
00138 
00139   /* Enable CORE LE clock in order to access LE modules */
00140   CMU_ClockEnable(cmuClock_CORELE, true);
00141 
00142   /* Select LFXO for LEUARTs (and wait for it to stabilize) */
00143   CMU_ClockSelectSet(cmuClock_LFB, cmuSelect_LFXO);
00144 
00145   CMU_ClockEnable(RETARGET_CLK, true);
00146 
00147   /* Do not prescale clock */
00148   CMU_ClockDivSet(RETARGET_CLK, cmuClkDiv_1);
00149 
00150   /* Configure LEUART */
00151   init.enable = leuartDisable;
00152   LEUART_Init(leuart, &init);
00153   /* Enable pins at default location */
00154   leuart->ROUTE = LEUART_ROUTE_RXPEN | LEUART_ROUTE_TXPEN | RETARGET_LOCATION;
00155 
00156   /* Clear previous RX interrupts */
00157   LEUART_IntClear(RETARGET_UART, LEUART_IF_RXDATAV);
00158   NVIC_ClearPendingIRQ(RETARGET_IRQn);
00159 
00160   /* Enable RX interrupts */
00161   LEUART_IntEnable(RETARGET_UART, LEUART_IF_RXDATAV);
00162   NVIC_EnableIRQ(RETARGET_IRQn);
00163 
00164   /* Finally enable it */
00165   LEUART_Enable(leuart, leuartEnable);
00166 #endif
00167 
00168 #if !defined(__CROSSWORKS_ARM) && defined(__GNUC__)
00169   setvbuf(stdout, NULL, _IONBF, 0);   /*Set unbuffered mode for stdout (newlib)*/
00170 #endif
00171 
00172   initialized = true;
00173 }
00174 
00175 
00176 /**************************************************************************/
00180 int RETARGET_ReadChar(void)
00181 {
00182   int c = -1;
00183 
00184   if (initialized == false)
00185   {
00186     RETARGET_SerialInit();
00187   }
00188 
00189   INT_Disable();
00190   if (rxCount > 0)
00191   {
00192     c = rxBuffer[rxReadIndex];
00193     rxReadIndex++;
00194     if (rxReadIndex == RXBUFSIZE)
00195     {
00196       rxReadIndex = 0;
00197     }
00198     rxCount--;
00199   }
00200   INT_Enable();
00201 
00202   return c;
00203 }
00204 
00205 /**************************************************************************/
00210 int RETARGET_WriteChar(char c)
00211 {
00212   if (initialized == false)
00213   {
00214     RETARGET_SerialInit();
00215   }
00216 
00217   /* Add CR or LF to CRLF if enabled */
00218   if (LFtoCRLF && (c == '\n'))
00219   {
00220     RETARGET_TX(RETARGET_UART, '\r');
00221   }
00222   RETARGET_TX(RETARGET_UART, c);
00223 
00224   if (LFtoCRLF && (c == '\r'))
00225   {
00226     RETARGET_TX(RETARGET_UART, '\n');
00227   }
00228 
00229   return c;
00230 }