ssize_t uart_read_r(struct _reent *r, int fd, void *ptr, size_t len) { while (!rx_buf.len); if (len > rx_buf.len) len = rx_buf.len; char *c = (char*)ptr; for (int i = 0; i < len; i++) rb_getc(&rx_buf, c++); return len; }
ssize_t apc220_read_str(char *ptr) { int len = -1; int result = 0; char c; if ( xSemaphoreRx != NULL ) { if( xSemaphoreTake( xSemaphoreRx, 0 ) == pdTRUE ) { result = rb_getc(&rx_buf1, &c); if (result != 0) { buffer_rx[buffer_rx_index++] = c; } xSemaphoreGive( xSemaphoreRx ); // We don't need to protect rx_buf2 because at this point rx_buf1 is available so // the ISR will attempt to write into rx_buf1 instead of rx_buf2 result = rb_getc(&rx_buf2, &c); if (result != 0) { buffer_rx[buffer_rx_index++] = c; } } } if ((buffer_rx_index > 0) && (buffer_rx[buffer_rx_index-1] == '\0')) { len = buffer_rx_index-1; for (int i=0 ;i < len ; i++) { ptr[i] = buffer_rx[i]; } buffer_rx_index = 0; } return len; }
void apc220_send_task() { char c; int result = 0; if( xSemaphoreTake( xSemaphoreTx, ( portTickType ) 10 ) == pdTRUE ) { result = rb_getc(&tx_buf, &c); xSemaphoreGive( xSemaphoreTx ); } if (result) { while ( !(USART1->SR & 0x00000040) ); USART_SendData(USART1, c); uart_stats.tx_bytes++; } }
void USART1_IRQHandler(void) { if (USART1->SR & USART_SR_RXNE) { if (!rb_putc(&rx_buf, USART1->DR)) uart_stats.rx_overrun++; else uart_stats.rx_bytes++; } if (USART1->SR & USART_SR_TXE) { char c; if (rb_getc(&tx_buf, &c)) { // send a queued byte // USART1->DR = c; } else { // nothing to send, disable interrupt // USART1->CR1 &= ~USART_CR1_TXEIE; } uart_stats.tx_bytes++; } }