Initial Release
This commit is contained in:
4
uart/CMakeLists.txt
Normal file
4
uart/CMakeLists.txt
Normal file
@@ -0,0 +1,4 @@
|
||||
if (NOT PICO_NO_HARDWARE)
|
||||
add_subdirectory(hello_uart)
|
||||
add_subdirectory(uart_advanced)
|
||||
endif ()
|
||||
12
uart/hello_uart/CMakeLists.txt
Normal file
12
uart/hello_uart/CMakeLists.txt
Normal file
@@ -0,0 +1,12 @@
|
||||
add_executable(hello_uart
|
||||
hello_uart.c
|
||||
)
|
||||
|
||||
# Pull in our pico_stdlib which pulls in commonly used features
|
||||
target_link_libraries(hello_uart pico_stdlib)
|
||||
|
||||
# create map/bin/hex file etc.
|
||||
pico_add_extra_outputs(hello_uart)
|
||||
|
||||
# add url via pico_set_program_url
|
||||
example_auto_set_url(hello_uart)
|
||||
44
uart/hello_uart/hello_uart.c
Normal file
44
uart/hello_uart/hello_uart.c
Normal file
@@ -0,0 +1,44 @@
|
||||
/**
|
||||
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include "pico/stdlib.h"
|
||||
#include "hardware/uart.h"
|
||||
|
||||
/// \tag::hello_uart[]
|
||||
|
||||
#define UART_ID uart0
|
||||
#define BAUD_RATE 115200
|
||||
|
||||
// We are using pins 0 and 1, but see the GPIO function select table in the
|
||||
// datasheet for information on which other pins can be used.
|
||||
#define UART_TX_PIN 0
|
||||
#define UART_RX_PIN 1
|
||||
|
||||
int main() {
|
||||
// Set up our UART with the required speed.
|
||||
uart_init(UART_ID, BAUD_RATE);
|
||||
|
||||
// Set the TX and RX pins by using the function select on the GPIO
|
||||
// Set datasheet for more information on function select
|
||||
gpio_set_function(UART_TX_PIN, GPIO_FUNC_UART);
|
||||
gpio_set_function(UART_RX_PIN, GPIO_FUNC_UART);
|
||||
|
||||
// Use some the various UART functions to send out data
|
||||
// In a default system, printf will also output via the default UART
|
||||
|
||||
// Send out a character without any conversions
|
||||
uart_putc_raw(UART_ID, 'A');
|
||||
|
||||
// Send out a character but do CR/LF conversions
|
||||
uart_putc(UART_ID, 'B');
|
||||
|
||||
// Send out a string, with CR/LF conversions
|
||||
uart_puts(UART_ID, " Hello, UART!\n");
|
||||
}
|
||||
|
||||
/// \end::hello_uart[]
|
||||
12
uart/uart_advanced/CMakeLists.txt
Normal file
12
uart/uart_advanced/CMakeLists.txt
Normal file
@@ -0,0 +1,12 @@
|
||||
add_executable(uart_advanced
|
||||
uart_advanced.c
|
||||
)
|
||||
|
||||
# Pull in our pico_stdlib which pulls in commonly used features
|
||||
target_link_libraries(uart_advanced pico_stdlib hardware_uart)
|
||||
|
||||
# create map/bin/hex file etc.
|
||||
pico_add_extra_outputs(uart_advanced)
|
||||
|
||||
# add url via pico_set_program_url
|
||||
example_auto_set_url(uart_advanced)
|
||||
86
uart/uart_advanced/uart_advanced.c
Normal file
86
uart/uart_advanced/uart_advanced.c
Normal file
@@ -0,0 +1,86 @@
|
||||
/**
|
||||
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
|
||||
|
||||
#include "pico/stdlib.h"
|
||||
#include "hardware/uart.h"
|
||||
#include "hardware/irq.h"
|
||||
|
||||
|
||||
/// \tag::uart_advanced[]
|
||||
|
||||
#define UART_ID uart0
|
||||
#define BAUD_RATE 115200
|
||||
#define DATA_BITS 8
|
||||
#define STOP_BITS 1
|
||||
#define PARITY UART_PARITY_NONE
|
||||
|
||||
// We are using pins 0 and 1, but see the GPIO function select table in the
|
||||
// datasheet for information on which other pins can be used.
|
||||
#define UART_TX_PIN 0
|
||||
#define UART_RX_PIN 1
|
||||
|
||||
static int chars_rxed = 0;
|
||||
|
||||
// RX interrupt handler
|
||||
void on_uart_rx() {
|
||||
while (uart_is_readable(UART_ID)) {
|
||||
uint8_t ch = uart_getc(UART_ID);
|
||||
// Can we send it back?
|
||||
if (uart_is_writable(UART_ID)) {
|
||||
// Change it slightly first!
|
||||
ch++;
|
||||
uart_putc(UART_ID, ch);
|
||||
}
|
||||
chars_rxed++;
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
// Set up our UART with a basic baud rate.
|
||||
uart_init(UART_ID, 2400);
|
||||
|
||||
// Set the TX and RX pins by using the function select on the GPIO
|
||||
// Set datasheet for more information on function select
|
||||
gpio_set_function(UART_TX_PIN, GPIO_FUNC_UART);
|
||||
gpio_set_function(UART_RX_PIN, GPIO_FUNC_UART);
|
||||
|
||||
// Actually, we want a different speed
|
||||
// The call will return the actual baud rate selected, which will be as close as
|
||||
// possible to that requested
|
||||
int actual = uart_set_baudrate(UART_ID, BAUD_RATE);
|
||||
|
||||
// Set UART flow control CTS/RTS, we don't want these, so turn them off
|
||||
uart_set_hw_flow(UART_ID, false, false);
|
||||
|
||||
// Set our data format
|
||||
uart_set_format(UART_ID, DATA_BITS, STOP_BITS, PARITY);
|
||||
|
||||
// Turn off FIFO's - we want to do this character by character
|
||||
uart_set_fifo_enabled(UART_ID, false);
|
||||
|
||||
// Set up a RX interrupt
|
||||
// We need to set up the handler first
|
||||
// Select correct interrupt for the UART we are using
|
||||
int UART_IRQ = UART_ID == uart0 ? UART0_IRQ : UART1_IRQ;
|
||||
|
||||
// And set up and enable the interrupt handlers
|
||||
irq_set_exclusive_handler(UART_IRQ, on_uart_rx);
|
||||
irq_set_enabled(UART_IRQ, true);
|
||||
|
||||
// Now enable the UART to send interrupts - RX only
|
||||
uart_set_irq_enables(UART_ID, true, false);
|
||||
|
||||
// OK, all set up.
|
||||
// Lets send a basic string out, and then run a loop and wait for RX interrupts
|
||||
// The handler will count them, but also reflect the incoming data back with a slight change!
|
||||
uart_puts(UART_ID, "\nHello, uart interrupts\n");
|
||||
|
||||
while (1)
|
||||
tight_loop_contents();
|
||||
}
|
||||
|
||||
/// \end:uart_advanced[]
|
||||
Reference in New Issue
Block a user