#include "midi_uart.h" #include "hardware/gpio.h" #include "hardware/uart.h" #define PIN_OUT_KIT 0 /* UART0 TX */ #define PIN_IN 1 /* UART0 RX, 6N137 output with a 1 kΩ pull-up on the board */ #define PIN_OUT_SYNTH 4 /* UART1 TX */ #define MIDI_BAUD 31250 static uart_inst_t *const PORTS[2] = { uart0, uart1 }; void midi_init(void){ uart_init(uart0, MIDI_BAUD); uart_init(uart1, MIDI_BAUD); uart_set_format(uart0, 8, 1, UART_PARITY_NONE); uart_set_format(uart1, 8, 1, UART_PARITY_NONE); gpio_set_function(PIN_OUT_KIT, GPIO_FUNC_UART); gpio_set_function(PIN_IN, GPIO_FUNC_UART); gpio_set_function(PIN_OUT_SYNTH, GPIO_FUNC_UART); /* GP5 (UART1 RX) stays a matrix row */ gpio_pull_up(PIN_IN); /* each MIDI current loop draws 6–8 mA, above the 4 mA default */ gpio_set_drive_strength(PIN_OUT_KIT, GPIO_DRIVE_STRENGTH_12MA); gpio_set_drive_strength(PIN_OUT_SYNTH, GPIO_DRIVE_STRENGTH_12MA); } void midi_send(int port, const uint8_t *bytes, int n){ uart_write_blocking(PORTS[port], bytes, (size_t)n); } void midi_note_on(int port, int ch, int note, int vel){ uint8_t m[3] = { (uint8_t)(0x90 | (ch & 15)), (uint8_t)(note & 127), (uint8_t)(vel & 127) }; midi_send(port, m, 3); } void midi_note_off(int port, int ch, int note){ uint8_t m[3] = { (uint8_t)(0x80 | (ch & 15)), (uint8_t)(note & 127), 0 }; midi_send(port, m, 3); } int midi_in_read(void){ return uart_is_readable(uart0) ? uart_getc(uart0) : -1; }