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 #if defined (RETARGET_PERIPHERAL_ENABLE)
00108   /* Enable DK RS232/UART switch */
00109   RETARGET_PERIPHERAL_ENABLE();
00110 #endif
00111 
00112   /* Enable peripheral clocks */
00113   CMU_ClockEnable(cmuClock_HFPER, true);
00114   CMU_ClockEnable(RETARGET_CLK, true);
00115 
00116   /* Configure USART for basic async operation */
00117   init.enable = usartDisable;
00118   USART_InitAsync(usart, &init);
00119 
00120   /* Enable pins at correct UART/USART location. */
00121   #if defined( USART_ROUTEPEN_RXPEN )
00122   usart->ROUTEPEN = USART_ROUTEPEN_RXPEN | USART_ROUTEPEN_TXPEN;
00123   usart->ROUTELOC0 = ( usart->ROUTELOC0 &
00124                        ~( _USART_ROUTELOC0_TXLOC_MASK
00125                           | _USART_ROUTELOC0_RXLOC_MASK ) )
00126                      | ( RETARGET_TX_LOCATION << _USART_ROUTELOC0_TXLOC_SHIFT )
00127                      | ( RETARGET_RX_LOCATION << _USART_ROUTELOC0_RXLOC_SHIFT );
00128   #else
00129   usart->ROUTE = USART_ROUTE_RXPEN | USART_ROUTE_TXPEN | RETARGET_LOCATION;
00130   #endif
00131 
00132   /* Clear previous RX interrupts */
00133   USART_IntClear(RETARGET_UART, USART_IF_RXDATAV);
00134   NVIC_ClearPendingIRQ(RETARGET_IRQn);
00135 
00136   /* Enable RX interrupts */
00137   USART_IntEnable(RETARGET_UART, USART_IF_RXDATAV);
00138   NVIC_EnableIRQ(RETARGET_IRQn);
00139 
00140   /* Finally enable it */
00141   USART_Enable(usart, usartEnable);
00142 
00143 #else
00144   LEUART_TypeDef      *leuart = RETARGET_UART;
00145   LEUART_Init_TypeDef init    = LEUART_INIT_DEFAULT;
00146 
00147   /* Enable DK LEUART/RS232 switch */
00148   RETARGET_PERIPHERAL_ENABLE();
00149 
00150   /* Enable CORE LE clock in order to access LE modules */
00151   CMU_ClockEnable(cmuClock_CORELE, true);
00152 
00153   /* Select LFXO for LEUARTs (and wait for it to stabilize) */
00154   CMU_ClockSelectSet(cmuClock_LFB, cmuSelect_LFXO);
00155 
00156   CMU_ClockEnable(RETARGET_CLK, true);
00157 
00158   /* Do not prescale clock */
00159   CMU_ClockDivSet(RETARGET_CLK, cmuClkDiv_1);
00160 
00161   /* Configure LEUART */
00162   init.enable = leuartDisable;
00163   LEUART_Init(leuart, &init);
00164   /* Enable pins at default location */
00165   leuart->ROUTE = LEUART_ROUTE_RXPEN | LEUART_ROUTE_TXPEN | RETARGET_LOCATION;
00166 
00167   /* Clear previous RX interrupts */
00168   LEUART_IntClear(RETARGET_UART, LEUART_IF_RXDATAV);
00169   NVIC_ClearPendingIRQ(RETARGET_IRQn);
00170 
00171   /* Enable RX interrupts */
00172   LEUART_IntEnable(RETARGET_UART, LEUART_IF_RXDATAV);
00173   NVIC_EnableIRQ(RETARGET_IRQn);
00174 
00175   /* Finally enable it */
00176   LEUART_Enable(leuart, leuartEnable);
00177 #endif
00178 
00179 #if !defined(__CROSSWORKS_ARM) && defined(__GNUC__)
00180   setvbuf(stdout, NULL, _IONBF, 0);   /*Set unbuffered mode for stdout (newlib)*/
00181 #endif
00182 
00183   initialized = true;
00184 }
00185 
00186 
00187 /**************************************************************************/
00191 int RETARGET_ReadChar(void)
00192 {
00193   int c = -1;
00194 
00195   if (initialized == false)
00196   {
00197     RETARGET_SerialInit();
00198   }
00199 
00200   INT_Disable();
00201   if (rxCount > 0)
00202   {
00203     c = rxBuffer[rxReadIndex];
00204     rxReadIndex++;
00205     if (rxReadIndex == RXBUFSIZE)
00206     {
00207       rxReadIndex = 0;
00208     }
00209     rxCount--;
00210   }
00211   INT_Enable();
00212 
00213   return c;
00214 }
00215 
00216 /**************************************************************************/
00221 int RETARGET_WriteChar(char c)
00222 {
00223   if (initialized == false)
00224   {
00225     RETARGET_SerialInit();
00226   }
00227 
00228   /* Add CR or LF to CRLF if enabled */
00229   if (LFtoCRLF && (c == '\n'))
00230   {
00231     RETARGET_TX(RETARGET_UART, '\r');
00232   }
00233   RETARGET_TX(RETARGET_UART, c);
00234 
00235   if (LFtoCRLF && (c == '\r'))
00236   {
00237     RETARGET_TX(RETARGET_UART, '\n');
00238   }
00239 
00240   return c;
00241 }