Appiko
SDK_EVAL_Com_UART.c
1 
24 /* Includes ------------------------------------------------------------------*/
25 #include "SDK_EVAL_Com.h"
26 #include "SDK_EVAL_Config.h"
27 
68 #define RECEIVE_QUEUE_SIZE NUCLEO_UARTx_RX_QUEUE_SIZE
69 
70 
71 static uint8_t rxQ[RECEIVE_QUEUE_SIZE];
72 static uint16_t rxHead = 0;
73 static uint16_t rxTail = 0;
74 volatile uint16_t rxUsed = 0;
75 static UART_HandleTypeDef huart;
76 static DMA_HandleTypeDef dma_handle;
77 
89 void prepareDmaTx(void){}
90 
96 void SdkEvalComInit(void)
97 {
98  GPIO_InitTypeDef GPIO_InitStructure;
99 
100  NUCLEO_UARTx_GPIO_CLK_ENABLE();
101  NUCLEO_UARTx_CLK_ENABLE();
102  NUCLEO_UARTx_DMA_CLK_ENABLE();
103 
104  dma_handle.Instance=NUCLEO_UARTx_RX_DMA_CHANNEL;
105 #ifdef USE_STM32L0XX_NUCLEO
106  dma_handle.Init.Request=NUCLEO_UARTx_RX_DMA_REQUEST;
107 #endif
108  dma_handle.Init.Direction=DMA_PERIPH_TO_MEMORY;
109  dma_handle.Init.PeriphInc=DMA_PINC_DISABLE;
110  dma_handle.Init.MemInc=DMA_MINC_ENABLE;
111  dma_handle.Init.PeriphDataAlignment=DMA_PDATAALIGN_BYTE;
112  dma_handle.Init.MemDataAlignment=DMA_MDATAALIGN_BYTE;
113  dma_handle.Init.Mode=DMA_CIRCULAR;
114  dma_handle.Init.Priority=DMA_PRIORITY_LOW;
115  HAL_DMA_Init(&dma_handle);
116 
117  GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
118  GPIO_InitStructure.Pull = GPIO_PULLUP;
119  GPIO_InitStructure.Speed = GPIO_SPEED_HIGH;
120 
121  GPIO_InitStructure.Pin = NUCLEO_UARTx_RX_PIN;
122  GPIO_InitStructure.Alternate = NUCLEO_UARTx_AF;
123  HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
124 
125  GPIO_InitStructure.Pin = NUCLEO_UARTx_TX_PIN;
126  GPIO_InitStructure.Alternate = NUCLEO_UARTx_AF;
127  HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
128 
129 
130  huart.Init.BaudRate=SDK_EVAL_UART_BAUDRATE;
131  huart.Init.WordLength=UART_WORDLENGTH_8B;
132  huart.Init.StopBits=UART_STOPBITS_1;
133  huart.Init.Parity=UART_PARITY_NONE;
134  huart.Init.HwFlowCtl=UART_HWCONTROL_NONE;
135  huart.Init.Mode=UART_MODE_TX_RX;
136  __HAL_LINKDMA(&huart, hdmarx, dma_handle);
137 
138  huart.Instance=NUCLEO_UARTx;
139 
140 
141  HAL_UART_Init(&huart);
142 
143  __HAL_UART_ENABLE(&huart);
144 
145 
146 
147  HAL_UART_Receive_DMA(&huart,rxQ,RECEIVE_QUEUE_SIZE);
148 
149 }
150 
151 
152 void SdkEvalComBaudrate(uint32_t baudrate)
153 {
154  huart.Init.BaudRate=baudrate;
155  __HAL_UART_DISABLE(&huart);
156  HAL_UART_Init(&huart);
157  __HAL_UART_ENABLE(&huart);
158 }
159 
160 
161 #ifdef __ICCARM__
162 #include <yfuns.h>
163 #include <stdint.h>
164 #define stdout _LLIO_STDOUT
165 
166 // IAR Standard library hook for serial output
167 size_t __write(int handle, const unsigned char * buffer, size_t size)
168 {
169 
170  /* This template only writes to "standard out" and "standard err",
171  * for all other file handles it returns failure. */
172  if (handle != _LLIO_STDOUT && handle != _LLIO_STDERR) {
173  return _LLIO_ERROR;
174  }
175  if (buffer == 0) {
176  uint8_t c=0;
177  HAL_UART_Transmit(&huart, &c, 1, 100);
178  return 0;
179  }
180 
181 
182  HAL_UART_Transmit(&huart, (uint8_t*)buffer, size, 300);
183 
184  return size;
185 }
186 
187 size_t fflush(int handle)
188 {
189  return __write(_LLIO_STDOUT, NULL, 0);
190 }
191 
192 
193 
194 // IAR Standard library hook for serial input
195 size_t __read(int handle, unsigned char * buffer, size_t size)
196 {
197  int nChars = 0;
198 
199  /* This template only reads from "standard in", for all other file
200  * handles it returns failure. */
201  if (handle != _LLIO_STDIN)
202  {
203  return _LLIO_ERROR;
204  }
205 
206  if(huart.hdmarx)
207  {
208  rxHead=(RECEIVE_QUEUE_SIZE-huart.hdmarx->Instance->CNDTR)%RECEIVE_QUEUE_SIZE;
209 
210  if(rxHead>=rxTail)
211  rxUsed=rxHead-rxTail;
212  else
213  rxUsed=RECEIVE_QUEUE_SIZE-rxTail+rxHead;
214 
215  }
216 
217  for(nChars = 0; (rxUsed>0) && (nChars < size); nChars++) {
218  *buffer++ = rxQ[rxTail];
219  rxTail = (rxTail+1) % RECEIVE_QUEUE_SIZE;
220  rxUsed--;
221  }
222 
223  return nChars;
224 }
225 
226 void enqueueRxChars(unsigned char * buffer, uint16_t size)
227 {
228  while (( size > 0 ) && (rxUsed < (RECEIVE_QUEUE_SIZE-1))) {
229  rxQ[rxHead] = *buffer++;
230  rxHead = (rxHead+1) % RECEIVE_QUEUE_SIZE;
231  rxUsed++;
232  size--;
233  }
234 }
235 
236 uint8_t __io_getcharNonBlocking(uint8_t *data)
237 {
238  if (__read(_LLIO_STDIN,data,1))
239  return 1;
240  else
241  return 0;
242 }
243 
244 void __io_putchar( char c )
245 {
246  __write(_LLIO_STDOUT, (unsigned char *)&c, 1);
247 }
248 
249 int __io_getchar()
250 {
251  unsigned char c;
252  __read(_LLIO_STDIN, &c, 1);
253  return (int)(c);
254 }
255 
256 void __io_flush( void )
257 {
258  __write(_LLIO_STDOUT, NULL, 0);
259 }
260 
261 #else
262 #ifdef __CC_ARM
263 
264 #include <stdio.h>
265 /* keil debug port defines */
266 #define ITM_Port8(n) (*((volatile unsigned char *)(0xE0000000+4*n)))
267 #define ITM_Port16(n) (*((volatile unsigned short*)(0xE0000000+4*n)))
268 #define ITM_Port32(n) (*((volatile unsigned long *)(0xE0000000+4*n)))
269 #define DEMCR (*((volatile unsigned long *)(0xE000EDFC)))
270 #define TRCENA 0x01000000
271 
272 struct __FILE { int handle; /* Add whatever needed */ };
273 FILE __stdout;
274 FILE __stdin;
275 
276 /* KEIL fputc implementation template allowing to redirect printf output towards serial port (UART/USB) */
277 int fputc(int c, FILE *f) {
278 
279  HAL_UART_Transmit(&huart, (uint8_t*)&c, 1, 300);
280 
281  return 1;
282 
283 }
284 
285 uint8_t __io_getcharNonBlocking(uint8_t *data)
286 {
287  int c = fgetc(&__stdin);
288 
289  if (c == EOF)
290  return 0;
291  else {
292  *data = (uint8_t)c;
293  return 1;
294  }
295 }/* end serialReadByte() */
296 
297 int fgetc (FILE *f) {
298  int data = -1;
299  int nChars = 0;
300 
301 
302  if(huart.hdmarx)
303  {
304  rxHead=(RECEIVE_QUEUE_SIZE-huart.hdmarx->Instance->CNDTR)%RECEIVE_QUEUE_SIZE;
305 
306  if(rxHead>=rxTail)
307  rxUsed=rxHead-rxTail;
308  else
309  rxUsed=RECEIVE_QUEUE_SIZE-rxTail+rxHead;
310 
311  }
312 
313  for(nChars = 0; (rxUsed>0) && (nChars < 1); nChars++) {
314  data = rxQ[rxTail];
315  rxTail = (rxTail+1) % RECEIVE_QUEUE_SIZE;
316  rxUsed--;
317  }
318 
319  return data;
320 }
321 
322 
323 #endif
324 #endif
325 
326 uint8_t append_to_buf(void)
327 {
328  uint8_t c;
329 
330  HAL_UART_Receive(&huart, &c, 1, 100);
331  rxQ[rxHead] = c;
332  rxHead=(rxHead+1)%RECEIVE_QUEUE_SIZE;
333  rxUsed++;
334 
335  if(c=='\n' || c=='\r')
336  return 1;
337 
338  return 0;
339 
340 }
356 /******************* (C) COPYRIGHT 2016 STMicroelectronics *****END OF FILE****/
This file contains SDK EVAL configuration and useful defines.
void SdkEvalComInit(void)
Configures UART port in RX DMA mode and TX blocking mode.
This file contains definitions for Software Development Kit eval board COM ports.