101 lines
1.8 KiB
C
101 lines
1.8 KiB
C
#include <io/usart.h>
|
|
#include <sys/atomic.h>
|
|
|
|
#if defined(__USART_BUFFER__)
|
|
|
|
int usart_tx (uint8_t _usart,void *data,int size){
|
|
usart_t *usart = usart_device_get(_usart);
|
|
|
|
if (usart){
|
|
ATOMIC
|
|
|
|
buffer_set( &(usart->tx), data, size );
|
|
if (data && size){
|
|
usart->mode_set(usart, USART_MODE_TX);
|
|
} else {
|
|
usart->mode_clear(usart, USART_MODE_TX);
|
|
};
|
|
return ESUCCESS;
|
|
};
|
|
return -ENOFILE;
|
|
};
|
|
|
|
int usart_tx_done (uint8_t _usart){
|
|
usart_t *usart = usart_device_get(_usart);
|
|
if (usart){
|
|
return buffer_free( &(usart->tx) );
|
|
};
|
|
return -ENOFILE;
|
|
};
|
|
|
|
int usart_rx (uint8_t _usart,void *data,int size){
|
|
usart_t *usart = usart_device_get(_usart);
|
|
|
|
if (usart){
|
|
ATOMIC
|
|
|
|
buffer_set( &(usart->rx), data, size );
|
|
if (data && size){
|
|
usart->mode_set(usart, USART_MODE_RX);
|
|
} else {
|
|
usart->mode_clear(usart, USART_MODE_RX);
|
|
};
|
|
return ESUCCESS;
|
|
};
|
|
return -ENOFILE;
|
|
};
|
|
|
|
int usart_rx_done (uint8_t _usart){
|
|
usart_t *usart = usart_device_get(_usart);
|
|
if (usart){
|
|
return buffer_free( &(usart->rx) );
|
|
};
|
|
return -ENOFILE;
|
|
};
|
|
|
|
#else
|
|
|
|
int usart_read (uint8_t _usart){
|
|
ATOMIC
|
|
usart_t *usart = usart_device_get(_usart);
|
|
if (usart){
|
|
int ch = usart_fifo_read( &(usart->rx) );
|
|
if (ch >= 0){
|
|
usart->mode_set( usart, USART_MODE_RX );
|
|
};
|
|
return ch;
|
|
};
|
|
return -ENOFILE;
|
|
};
|
|
|
|
int usart_write (uint8_t _usart,int ch){
|
|
ATOMIC
|
|
usart_t *usart = usart_device_get(_usart);
|
|
if (usart){
|
|
usart->mode_set( usart, USART_MODE_TX );
|
|
return usart_fifo_write( &(usart->tx), ch );
|
|
};
|
|
return -ENOFILE;
|
|
};
|
|
|
|
int usart_tx (uint8_t usart,char *b,int size){
|
|
int n,r, tries;
|
|
tries = 10;
|
|
for (n=0;n<size;n++){
|
|
for (r=0;r<tries;r++){
|
|
if (usart_write( usart, *b) >= 0){
|
|
b++;
|
|
break;
|
|
};
|
|
if(r == tries - 1) {
|
|
return -EFAIL;
|
|
}
|
|
wait_ms(1);
|
|
};
|
|
};
|
|
return ESUCCESS;
|
|
};
|
|
|
|
|
|
#endif
|