| BlueNRG-1 SDK drivers APIs

- Main Page
- Modules
- Classes
- Files

- File List
- File Members

SDK_EVAL_Com.c
Go to the documentation of this file.
1
24 /* Includes ------------------------------------------------------------------*/
25 #include "SDK_EVAL_Com.h"
26 #include <stdio.h>
27
28
46 typedef struct _IO_ReceiveCallBack
47 {
51 IO_RECEIVE_DATA_USER_CALLBACK_TYPE IO_user_application;
52 } IO_ReceiveCallBack;
53
62 #define READ_DATA_SIZE 1024
63 #define READ_BUFFER_IS_EMPTY() (Read_ptr_in == Read_ptr_out)
64
83 /* IO User Callback used to handle received data from I/O channel*/
84 static IO_ReceiveCallBack IO_Receive_Data;
85
86 static uint8_t Read_Buffer [READ_DATA_SIZE];
87 static uint32_t Read_ptr_in = 0;
88 static uint32_t Read_ptr_out = 0;
89
90
114 static void SdkEvalUart_Send_Data(uint8_t tx_data)
115 {
116 /* wait for the transmission end */
117 while (UART_GetFlagStatus(UART_FLAG_TXFE) == RESET);
118 /* send the data */
119 UART_SendData(tx_data);
120 }/* end SdkEvalUart_Send_Data() */
121
122
137 void SdkEvalComUartInit(uint32_t baudrate)
138 {
139 UART_InitType UART_InitStructure;
140
141 /* GPIO Periph clock enable */
142 SysCtrl_PeripheralClockCmd(CLOCK_PERIPH_UART | CLOCK_PERIPH_GPIO, ENABLE);
143
144 GPIO_InitType GPIO_InitStructure;
145
146 /* Configure GPIO_Pin_8 and GPIO_Pin_11 as UART_TXD and UART_RXD*/
147 GPIO_InitStructure.GPIO_Pin = SDK_EVAL_UART_TX_PIN;
148 GPIO_InitStructure.GPIO_Mode = SDK_EVAL_UART_TX_MODE;
149 GPIO_InitStructure.GPIO_Pull = DISABLE;
150 GPIO_InitStructure.GPIO_HighPwr = DISABLE;
151 GPIO_Init(&GPIO_InitStructure);
152
153 GPIO_InitStructure.GPIO_Pin = SDK_EVAL_UART_RX_PIN;
154 GPIO_InitStructure.GPIO_Mode = SDK_EVAL_UART_RX_MODE;
155 GPIO_Init(&GPIO_InitStructure);
156
157 /*
158 ------------ USART configuration -------------------
159 - BaudRate = 115200 baud
160 - Word Length = 8 Bits
161 - One Stop Bit
162 - No parity
163 - Hardware flow control disabled (RTS and CTS signals)
164 - Receive and transmit enabled
165 */
166 UART_InitStructure.UART_BaudRate = baudrate;
167 UART_InitStructure.UART_WordLengthTransmit = UART_WordLength_8b;
168 UART_InitStructure.UART_WordLengthReceive = UART_WordLength_8b;
169 UART_InitStructure.UART_StopBits = UART_StopBits_1;
170 UART_InitStructure.UART_Parity = UART_Parity_No;
171 UART_InitStructure.UART_HardwareFlowControl = UART_HardwareFlowControl_None;
172 UART_InitStructure.UART_Mode = UART_Mode_Rx | UART_Mode_Tx;
173 UART_InitStructure.UART_FifoEnable = DISABLE;
174 UART_Init(&UART_InitStructure);
175
176 /* Enable UART */
177 UART_Cmd(ENABLE);
178 }
179
180
187 void SdkEvalComUartIrqConfig(FunctionalState EnableIrq)
188 {
189 /* NVIC configuration */
190 NVIC_InitType NVIC_InitStructure;
191
192 /* Enable the UART Interrupt */
193 NVIC_InitStructure.NVIC_IRQChannel = UART_IRQn;
194 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = LOW_PRIORITY;
195 NVIC_InitStructure.NVIC_IRQChannelCmd = EnableIrq;
196 NVIC_Init(&NVIC_InitStructure);
197
198 UART_ITConfig(UART_IT_RX, EnableIrq);
199
200 }
201
202
203
209 void SdkEvalComIOUartIrqHandler(void)
210 {
211 uint8_t read_data;
212
213 if(UART_GetITStatus(UART_IT_RX) != RESET) {
214
215 while(!UART_GetFlagStatus(UART_FLAG_RXFE)) {
216
217 /* Read byte from the receive FIFO */
218 read_data = (uint8_t) (UART_ReceiveData() & 0xFF);
219
220 /* IO Receive Data: it handles the received data within the User Application Function
221 defined as parameter of SdkEvalComIOConfig() */
222 SdkEvalComIOReceiveData(&read_data,1);
223 }
224 /* Clear the UART1 Receive interrupt */
225 UART_ClearITPendingBit(UART_IT_RX);
226 }
227 }
228
229
235 void SdkEvalComIOConfig(IO_RECEIVE_DATA_USER_CALLBACK_TYPE io_receive_function)
236 {
237 IO_Receive_Data.IO_user_application = io_receive_function;
238 /* Init Serial Uart */
239 SdkEvalComUartInit(UART_BAUDRATE);
240 SdkEvalComUartIrqConfig(ENABLE);
241 }
242
243
249 void SdkEvalComIOSendData(uint8_t tx_data)
250 {
251 SdkEvalUart_Send_Data(tx_data);
252 }
253
260 void SdkEvalComIOReceiveData(uint8_t * rx_data, uint16_t data_size)
261 {
262 if(IO_Receive_Data.IO_user_application)
263 IO_Receive_Data.IO_user_application(rx_data,data_size);
264 }
265
266
272 void Read_Buffer_Push(uint8_t byte)
273 {
274 Read_Buffer[Read_ptr_in] = byte;
275
276 Read_ptr_in++;
277
278 /* To avoid buffer overflow */
279 if(Read_ptr_in == READ_DATA_SIZE)
280 {
281 Read_ptr_in = 0;
282 }
283 }
284
285
291 ErrorStatus Read_Buffer_Pop(uint8_t *byte)
292 {
293 if(READ_BUFFER_IS_EMPTY())
294 return ERROR;
295
296 *byte = Read_Buffer[Read_ptr_out];
297
298 Read_ptr_out++;
299
300 if(Read_ptr_out == READ_DATA_SIZE)
301 Read_ptr_out = 0;
302
303 return SUCCESS;
304 }
305
306
313 void SdkEvalComIOProcessInputData(uint8_t * rx_data, uint16_t data_size)
314 {
315 for(uint16_t i = 0; i < data_size; i++){
316 Read_Buffer_Push(rx_data[i]);
317 }
318 }
319
320
321 #ifdef __ICCARM__
322 #include <yfuns.h>
323 #undef putchar
324
325 int putchar(int c)
326 {
327 SdkEvalComIOSendData((uint8_t) c);
328 return c;
329 }
330
331 void __io_putchar(char c)
332 {
333 putchar(c);
334 }
335
336 size_t _write(int handle, const unsigned char * buffer, size_t size)
337 {
338 size_t nChars = 0;
339
340 if (handle != _LLIO_STDOUT && handle != _LLIO_STDERR) {
341 return _LLIO_ERROR;
342 }
343
344 if (buffer == 0) {
345 // This means that we should flush internal buffers.
346 //spin until TX complete (TX is idle)
347 return 0;
348 }
349
350 while(size--) {
351 __io_putchar(*buffer++);
352 ++nChars;
353 }
354
355 return nChars;
356 }
357
358
359 size_t __read(int handle, unsigned char * buffer, size_t size)
360 {
361 size_t bytes = 0;
362
363 while(bytes < size && !Read_Buffer_Pop(buffer+bytes)){ //TBR
364 bytes++;
365 }
366 return bytes;
367 }
368
369 uint8_t __io_getcharNonBlocking(uint8_t *data)
370 {
371 if (__read(_LLIO_STDIN,data,1))
372 return 1;
373 else
374 return 0;
375 }/* end serialReadByte() */
376 #else
377 #ifdef __CC_ARM
378
379 #include <stdio.h>
380 /* keil debug port defines */
381 #define ITM_Port8(n) (*((volatile unsigned char *)(0xE0000000+4*n)))
382 #define ITM_Port16(n) (*((volatile unsigned short*)(0xE0000000+4*n)))
383 #define ITM_Port32(n) (*((volatile unsigned long *)(0xE0000000+4*n)))
384 #define DEMCR (*((volatile unsigned long *)(0xE000EDFC)))
385 #define TRCENA 0x01000000
386
387 struct __FILE { int handle; /* Add whatever needed */ };
388 FILE __stdout;
389 FILE __stdin;
390
391 /* KEIL fputc implementation template allowing to redirect printf output towards serial port (UART/USB) */
392 int fputc(int c, FILE *f) {
393
394 SdkEvalComIOSendData((uint8_t) c);
395
396 return 1;
397
398 }
399
400 uint8_t __io_getcharNonBlocking(uint8_t *data)
401 {
402 int c = fgetc(&__stdin);
403
404 if (c == EOF)
405 return 0;
406 else {
407 *data = (uint8_t)c;
408 return 1;
409 }
410 }/* end serialReadByte() */
411
412 int fgetc (FILE *f) {
413 int data = -1;
414 Read_Buffer_Pop((uint8_t *)&data);
415 return data;
416 }
417
418 #else
419 #ifdef __GNUC__
420 #include <stdio.h>
421
422 #undef putchar
423
424 int putchar(int c)
425 {
426 SdkEvalComIOSendData((uint8_t) c);
427 return c;
428 }
429
430 int _write(int fd, char *str, int len)
431 {
432 for(int i=0;i<len;i++) {
433 SdkEvalComIOSendData((uint8_t) str[i]);
434 }
435 return len;
436
437 }
438
439 int _read(int handle, unsigned char * buffer, size_t size)
440 {
441 size_t bytes = 0;
442
443 while(bytes < size && !Read_Buffer_Pop(buffer+bytes)){ //TBR
444 bytes++;
445 }
446 return bytes;
447 }
448
449 uint8_t __io_getcharNonBlocking(uint8_t *data)
450 {
451 if (_read(0,data,1))
452 return 1;
453 else
454 return 0;
455 }/* end serialReadByte() */
456
457
458 #include <stdlib.h>
459 #include <string.h>
460
461 void __aeabi_memcpy(void *dest, const void *src, size_t n)
462 {
463 memcpy(dest, src, n);
464 }
465
466 void __aeabi_memcpy4(void *dest, const void *src, size_t n)
467 {
468 memcpy(dest, src, n);
469 }
470
471 void __aeabi_memclr(void *dest, size_t n)
472 {
473 memset(dest, 0, n);
474 }
475
476 void __aeabi_memclr4(void *dest, size_t n)
477 {
478 memset(dest, 0, n);
479 }
480
481 void __aeabi_memset(void *dest, char c, size_t n)
482 {
483 memset(dest, c, n);
484 }
485
486 #endif
487 #endif
488 #endif
489
505 /******************* (C) COPYRIGHT 2015 STMicroelectronics *****END OF FILE****/
SdkEvalComIOProcessInputData
void SdkEvalComIOProcessInputData(uint8_t *rx_data, uint16_t data_size)
Push a buffer in the read buffer.
Definition: SDK_EVAL_Com.c:313
SDK_EVAL_Com.h
This file provides all the low level API to manage UART interface for eval board. ...
SDK_EVAL_UART_TX_PIN
#define SDK_EVAL_UART_TX_PIN
BlueNRG1 Eval Board (STEVAL-IDB007Vx) UART definitions.
Definition: USER_Platform_Configuration.h:46
SdkEvalComIOConfig
void SdkEvalComIOConfig(IO_RECEIVE_DATA_USER_CALLBACK_TYPE io_receive_function)
Configure the selected I/O communciation channel.
Definition: SDK_EVAL_Com.c:235
SdkEvalComUartIrqConfig
void SdkEvalComUartIrqConfig(FunctionalState EnableIrq)
Enable UART IRQ request.
Definition: SDK_EVAL_Com.c:187
READ_DATA_SIZE
#define READ_DATA_SIZE
Definition: SDK_EVAL_Com.c:62
SDK_EVAL_UART_RX_PIN
#define SDK_EVAL_UART_RX_PIN
Definition: USER_Platform_Configuration.h:49
SdkEvalComIOSendData
void SdkEvalComIOSendData(uint8_t tx_data)
Send a byte on selected I/O port.
Definition: SDK_EVAL_Com.c:249
Read_Buffer_Pop
ErrorStatus Read_Buffer_Pop(uint8_t *byte)
Pop out a byte from the read buffer.
Definition: SDK_EVAL_Com.c:291
Read_Buffer_Push
void Read_Buffer_Push(uint8_t byte)
Push a byte in the read buffer.
Definition: SDK_EVAL_Com.c:272
_IO_ReceiveCallBack::IO_user_application
IO_RECEIVE_DATA_USER_CALLBACK_TYPE IO_user_application
Definition: SDK_EVAL_Com.c:51
SdkEvalComIOUartIrqHandler
void SdkEvalComIOUartIrqHandler(void)
Serial IRQ handler body to be put on user UART_IRQHandler()
Definition: SDK_EVAL_Com.c:209
IO_RECEIVE_DATA_USER_CALLBACK_TYPE
void(* IO_RECEIVE_DATA_USER_CALLBACK_TYPE)(uint8_t *rx_data, uint16_t data_size)
IO_Receive_Data User callback type.
Definition: SDK_EVAL_Com.h:57
SDK_EVAL_UART_TX_MODE
#define SDK_EVAL_UART_TX_MODE
Definition: USER_Platform_Configuration.h:47
READ_BUFFER_IS_EMPTY
#define READ_BUFFER_IS_EMPTY()
Definition: SDK_EVAL_Com.c:63
SdkEvalComUartInit
void SdkEvalComUartInit(uint32_t baudrate)
Configures Uart port.
Definition: SDK_EVAL_Com.c:137
SdkEvalComIOReceiveData
void SdkEvalComIOReceiveData(uint8_t *rx_data, uint16_t data_size)
Receive data on selected I/O port.
Definition: SDK_EVAL_Com.c:260
IO_ReceiveCallBack
struct _IO_ReceiveCallBack IO_ReceiveCallBack
IO_Receive_Data User callback.
_IO_ReceiveCallBack
IO_Receive_Data User callback.
Definition: SDK_EVAL_Com.c:46
SDK_EVAL_UART_RX_MODE
#define SDK_EVAL_UART_RX_MODE
Definition: USER_Platform_Configuration.h:50