|
1 | 1 | /* |
2 | | - * Copyright 2020 Micro:bit Educational Foundation and contributors |
| 2 | + * Copyright 2020-2022 Micro:bit Educational Foundation and contributors |
3 | 3 | * SPDX-License-Identifier: Apache-2.0 |
4 | 4 | * |
5 | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
|
16 | 16 | */ |
17 | 17 | #include "mbed.h" |
18 | 18 |
|
19 | | -static UnbufferedSerial pc_serial(USBTX, USBRX, 115200); |
| 19 | +UnbufferedSerial pc_serial(USBTX, USBRX, 115200); |
20 | 20 |
|
21 | | -void rx_irq() { |
| 21 | +// Wire up printf to serial |
| 22 | +FileHandle *mbed::mbed_override_console(int fd) { |
| 23 | + return &pc_serial; |
| 24 | +} |
| 25 | + |
| 26 | +void rx_irq_echo() { |
22 | 27 | char single_byte; |
23 | | - // Echo each byte received |
24 | 28 | if (pc_serial.read(&single_byte, 1)) { |
25 | 29 | pc_serial.write(&single_byte, 1); |
26 | 30 | } |
27 | 31 | } |
28 | 32 |
|
29 | 33 | int main(void) { |
| 34 | + printf("printf test! (MbedOS %d.%d.%d)\n", |
| 35 | + MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION); |
| 36 | + |
| 37 | + // Send a string directly (size calculation removes the null terminator) |
30 | 38 | char hello[] = "hello world!\n"; |
31 | | - size_t hello_size = sizeof(hello) / sizeof(hello[0]); |
| 39 | + size_t hello_size = (sizeof(hello) / sizeof(hello[0])) - 1; |
32 | 40 | pc_serial.write(hello, hello_size); |
33 | | - pc_serial.attach(&rx_irq, SerialBase::RxIrq); |
| 41 | + |
| 42 | + // Configure RX IRQ to a function to echos back the data |
| 43 | + pc_serial.attach(&rx_irq_echo, SerialBase::RxIrq); |
34 | 44 | } |
0 commit comments