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
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
00060 rxBuffer[rxWriteIndex] = RETARGET_RX(RETARGET_UART);
00061 rxWriteIndex++;
00062 rxCount++;
00063 if (rxWriteIndex == RXBUFSIZE)
00064 {
00065 rxWriteIndex = 0;
00066 }
00067
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
00098 CMU_ClockEnable(cmuClock_GPIO, true);
00099
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
00108 RETARGET_PERIPHERAL_ENABLE();
00109
00110
00111 CMU_ClockEnable(cmuClock_HFPER, true);
00112 CMU_ClockEnable(RETARGET_CLK, true);
00113
00114
00115 init.enable = usartDisable;
00116 USART_InitAsync(usart, &init);
00117
00118
00119 usart->ROUTE = USART_ROUTE_RXPEN | USART_ROUTE_TXPEN | RETARGET_LOCATION;
00120
00121
00122 USART_IntClear(RETARGET_UART, USART_IF_RXDATAV);
00123 NVIC_ClearPendingIRQ(RETARGET_IRQn);
00124
00125
00126 USART_IntEnable(RETARGET_UART, USART_IF_RXDATAV);
00127 NVIC_EnableIRQ(RETARGET_IRQn);
00128
00129
00130 USART_Enable(usart, usartEnable);
00131
00132 #else
00133 LEUART_TypeDef *leuart = RETARGET_UART;
00134 LEUART_Init_TypeDef init = LEUART_INIT_DEFAULT;
00135
00136
00137 RETARGET_PERIPHERAL_ENABLE();
00138
00139
00140 CMU_ClockEnable(cmuClock_CORELE, true);
00141
00142
00143 CMU_ClockSelectSet(cmuClock_LFB, cmuSelect_LFXO);
00144
00145 CMU_ClockEnable(RETARGET_CLK, true);
00146
00147
00148 CMU_ClockDivSet(RETARGET_CLK, cmuClkDiv_1);
00149
00150
00151 init.enable = leuartDisable;
00152 LEUART_Init(leuart, &init);
00153
00154 leuart->ROUTE = LEUART_ROUTE_RXPEN | LEUART_ROUTE_TXPEN | RETARGET_LOCATION;
00155
00156
00157 LEUART_IntClear(RETARGET_UART, LEUART_IF_RXDATAV);
00158 NVIC_ClearPendingIRQ(RETARGET_IRQn);
00159
00160
00161 LEUART_IntEnable(RETARGET_UART, LEUART_IF_RXDATAV);
00162 NVIC_EnableIRQ(RETARGET_IRQn);
00163
00164
00165 LEUART_Enable(leuart, leuartEnable);
00166 #endif
00167
00168 #if !defined(__CROSSWORKS_ARM) && defined(__GNUC__)
00169 setvbuf(stdout, NULL, _IONBF, 0);
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
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 }