-
Notifications
You must be signed in to change notification settings - Fork 903
/
Copy pathuart_tx.c
43 lines (34 loc) · 1.36 KB
/
uart_tx.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/**
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include "pico/stdlib.h"
#include "hardware/pio.h"
#include "uart_tx.pio.h"
// We're going to use PIO to print "Hello, world!" on the same GPIO which we
// normally attach UART0 to.
#define PIO_TX_PIN 0
// Check the pin is compatible with the platform
#if PIO_TX_PIN >= NUM_BANK0_GPIOS
#error Attempting to use a pin>=32 on a platform that does not support it
#endif
int main() {
// This is the same as the default UART baud rate on Pico
const uint SERIAL_BAUD = 115200;
PIO pio;
uint sm;
uint offset;
// This will find a free pio and state machine for our program and load it for us
// We use pio_claim_free_sm_and_add_program_for_gpio_range (for_gpio_range variant)
// so we will get a PIO instance suitable for addressing gpios >= 32 if needed and supported by the hardware
bool success = pio_claim_free_sm_and_add_program_for_gpio_range(&uart_tx_program, &pio, &sm, &offset, PIO_TX_PIN, 1, true);
hard_assert(success);
uart_tx_program_init(pio, sm, offset, PIO_TX_PIN, SERIAL_BAUD);
while (true) {
uart_tx_program_puts(pio, sm, "Hello, world! (from PIO!)\r\n");
sleep_ms(1000);
}
// This will free resources and unload our program
pio_remove_program_and_unclaim_sm(&uart_tx_program, pio, sm, offset);
}