Penguino Library - UART
Contents
See the UART Communication documentation for more examples and tips on using UART.
UART
1 #include "penguino/uart/uart.h"
Function |
Functionality |
void uart_init(uint32_t baudrate) |
Initialises UART over USB to a specified baud rate. |
void uart_init_scaled(uint32_t scaledBaud) |
Initialises UART over USB to a pre-scaled baud rate relative to the clock speed. |
void uart_putc(unsigned char data) |
Send a character over UART |
void uart_puts(const char *s) |
Send a string over UART |
bool uart_hasData(void) |
Returns true if (and only if) the UART buffer contains data |
void uart_waitData(void) |
Waits until data is available in the UART buffer |
char uart_getc(void) |
Returns (then clears) the first character received into the UART buffer |
void uart_setBlocking(bool blocking) |
Sets UART to blocking mode |
Example:
1 #include "penguino/uart/uart.h"
2
3 int main( void ) {
4
5 uart_init( 115200 );
6
7 while ( 1 ) {
8 if ( uart_hasData( ) ) {
9 // grab data from UART
10 unsigned char data = uart_getc( );
11
12 // echo it back to UART
13 uart_putc( data );
14 }
15 }
16
17 }
UART as stdio
1 #include "penguino/uart/uart-stdio.h"
These functions require UART to be initialised with the above uart_init function.
Function |
Functionality |
void uart_stdio_init(void) |
Allows the UART buffer to act as standard IO |
void uart_stdio_echo(bool enable) |
Enables or disables echoing of received characters. Useful when accepting user input, but should be disabled for software interaction. By default echo is disabled. |
Example:
1 #include <stdio.h>
2
3 #include "penguino/uart/uart.h"
4 #include "penguino/uart/uart-stdio.h"
5 #include "penguino/time.h"
6
7 #define UART_BAUD_RATE 115200
8
9 int main( void ) {
10 char name[80];
11 int age = 0;
12
13 // initalise UART
14 uart_init( UART_BAUD_RATE );
15
16 // enable UART as stdio
17 uart_stdio_init( );
18
19 // enable echo on the UART stdin so the user can see what they are typing
20 uart_stdio_echo( true );
21
22 while ( 1 ) {
23 printf( "\r\n\r\nHello there, I'm a Penguino AVR!\r\n\r\n" );
24
25 printf( "What's your name? " );
26 scanf( "%s", name ); // N.B. this is a format vulnerability in C! example only
27
28 printf( "\r\n\r\n" );
29
30 printf( "Why hello, %s! How old are you? ", name );
31 scanf( "%d", &age );
32
33 printf( "\r\n\r\n" );
34
35 printf( "Thanks, %s, for telling me you're %d years old!\r\n\r\n", name, age );
36
37 // snooze
38 delay_ms( 300 );
39 }
40
41 return 0;
42 }
![icy [labs]](/moin_static171/common/wikilogo.png)