git.y1.nz

gbdk-2020

GameBoy Development Kit
download: https://git.y1.nz/archives/gbdk.tar.gz
README | Files | Log | Refs | LICENSE

commit 04bac46997dc11ec2721c33c25847adf1a3daec9
parent 8849193ffc7e22511743a4c1b130784e716f5988
Author: bbbbbr <bbbbbr@users.noreply.github.com>
Date:   Mon, 11 Aug 2025 16:55:57 -0700

Duck: changes resulting from printer research and example work (#807)

- Rework printer status functions
- Add printer query function
Diffstat:
Mgbdk-lib/include/duck/laptop_io.h37++++++++++++++++++++-----------------
Mgbdk-lib/libc/targets/sm83/duck/megaduck_laptop_io.c32+++++++++++++++++++++++---------
2 files changed, 43 insertions(+), 26 deletions(-)

diff --git a/gbdk-lib/include/duck/laptop_io.h b/gbdk-lib/include/duck/laptop_io.h @@ -35,7 +35,7 @@ #define DUCK_IO_CMD_ABORT_OR_FAIL 0x04u #define DUCK_IO_CMD_PLAY_SPEECH 0x05u #define DUCK_IO_CMD_RUN_CART_IN_SLOT 0x08u -#define DUCK_IO_CMD_PRINT_INIT_MAYBE_EXT_IO 0x09u /**< Command to init the printer and return status + model type */ +#define DUCK_IO_CMD_PRINT_INIT_EXT_IO 0x09u /**< Command to init the printer and return status + model type */ #define DUCK_IO_CMD_SET_RTC 0x0Bu /**< Command to set hardware RTC by sending a multi-byte packet */ #define DUCK_IO_CMD_GET_RTC 0x0Cu /**< Command to get hardware RTC by receiving a a multi-byte packet */ #define DUCK_IO_CMD_PRINT_SEND_BYTES 0x11u /**< Send printer data */ @@ -90,15 +90,10 @@ // Printer init reply related // Init Reply Bit.0 -#define DUCK_IO_PRINTER_INIT_MASK 0x01u // Bit.0 -#define DUCK_IO_PRINTER_INIT_OK 0x01u -#define DUCK_IO_PRINTER_INIT_FAIL 0x00u - -// Init Reply Bit.1 -#define DUCK_IO_PRINTER_TYPE_MASK 0x02u // Bit.1 -#define DUCK_IO_PRINTER_TYPE_2_PASS 0x00u // Bit.1 = 0 // 13 x 12 byte packets + 1 x 5 or 6 byte packet (with CR and/or LF) +#define DUCK_IO_PRINTER_FAIL 0x00u +#define DUCK_IO_PRINTER_TYPE_2_PASS 0x01u // Bit.1 = 0 // 13 x 12 byte packets + 1 x 5 or 6 byte packet (with CR and/or LF) #define DUCK_IO_PRINTER_TYPE_1_PASS 0x02u // Bit.1 = 1 // 3 x 12 byte packets + 118 non-packet bytes - +#define DUCK_IO_PRINTER_MAYBE_BUSY 0x03u // Maybe indicating that Printer Type 1 is busy? extern volatile bool duck_io_rx_byte_done; @@ -168,24 +163,32 @@ void duck_io_enable_read_byte(void); bool duck_io_laptop_init(void); -/** Returns whether the MegaDuck Printer was detected during duck_io_laptop_init() +/** Returns status of MegaDuck Printer as last detected by duck_io_laptop_init() or duck_io_printer_query() - Returns `true` if successful, otherwise `false` + Returned unsigned 8 bit value will have: + \li @ref Bit 0: Printer Status. Mask with @ref DUCK_IO_PRINTER_INIT_OK + \li @ref Bit 1: Printer Type. Mask with @ref DUCK_IO_PRINTER_TYPE_MASK @ref duck_io_laptop_init() must be called first */ -bool duck_io_printer_detected(void); +uint8_t duck_io_printer_last_status(void); + + +/** Performs a 3 x printer query serial command and returns raw system printer reply + Should be called immediately before trying to print -/** Returns which type of MegaDuck Printer was detected during duck_io_laptop_init() + Returned unsigned 8 bit value will have: + \li @ref Bit 0: Printer Status. Mask with @ref DUCK_IO_PRINTER_INIT_OK + \li @ref Bit 1: Printer Type. Mask with @ref DUCK_IO_PRINTER_TYPE_MASK - Return value should be one of the following: - \li @ref DUCK_IO_PRINTER_TYPE_1_PASS (single pass monochrome) - \li @ref DUCK_IO_PRINTER_TYPE_2_PASS (two pass possibly supports shades of grey) + The resulting value will be cached and used for + any subsequent duck_io_printer_detected() and + duck_io_printer_type() calls. @ref duck_io_laptop_init() must be called first */ -uint8_t duck_io_printer_type(void); +uint8_t duck_io_printer_query(void); // ===== Higher level IO functions ===== diff --git a/gbdk-lib/libc/targets/sm83/duck/megaduck_laptop_io.c b/gbdk-lib/libc/targets/sm83/duck/megaduck_laptop_io.c @@ -19,7 +19,7 @@ volatile uint8_t duck_io_rx_byte; uint8_t duck_io_tx_buf[DUCK_IO_LEN_TX_MAX]; uint8_t duck_io_tx_buf_len; - uint8_t duck_io_priniter_init_result = DUCK_IO_PRINTER_INIT_FAIL; // Default printer to not connected + uint8_t duck_io_priniter_init_result = DUCK_IO_PRINTER_FAIL; // Default printer to not connected static void _delay_1_msec(void); @@ -143,8 +143,8 @@ bool duck_io_send_cmd_and_buffer(uint8_t io_cmd) { } // Send buffer length + 2 (for length header and checksum bytes) - _delay_1_msec; // Delay for unknown reasons (present in system rom) - if (!duck_io_send_byte_and_check_ack_msecs_timeout(packet_length, DUCK_IO_TIMEOUT_200_MSEC, DUCK_IO_REPLY_SEND_BUFFER_OK)) { + _delay_1_msec; // Delay, perhaps for byte serial transfer time? (present in system rom) + if (!duck_io_send_byte_and_check_ack_msecs_timeout(packet_length, DUCK_IO_TIMEOUT_200_MSEC, DUCK_IO_REPLY_SEND_BUFFER_OK)) { IE_REG = int_enables_saved; return false; } @@ -303,9 +303,10 @@ bool duck_io_laptop_init(void) { // Initialize Serially attached peripheral duck_io_init_ok = duck_io_controller_init(); if (duck_io_init_ok) { + // Save response from some command // (so far not seen being used in System ROM 32K Bank 0) - duck_io_send_byte(DUCK_IO_CMD_PRINT_INIT_MAYBE_EXT_IO); + duck_io_send_byte(DUCK_IO_CMD_PRINT_INIT_EXT_IO); // TODO: This wait with no timeout is how the System ROM does it, // but it can probably be changed to a long delay and @@ -323,12 +324,25 @@ bool duck_io_laptop_init(void) { } -bool duck_io_printer_detected(void) { - return (duck_io_priniter_init_result & DUCK_IO_PRINTER_INIT_OK); +uint8_t duck_io_printer_last_status(void) { + return (duck_io_priniter_init_result); } -uint8_t duck_io_printer_type(void) { - return (duck_io_priniter_init_result & DUCK_IO_PRINTER_TYPE_MASK); -} +uint8_t duck_io_printer_query(void) { + // Query three times in a row, per System ROM behavior + // Might be doing some kind of initialization/test + for (uint8_t c = 0u; c < 3u; c++) { + // Delay per system rom behavior + delay(50); + duck_io_send_byte(DUCK_IO_CMD_PRINT_INIT_EXT_IO); + duck_io_read_byte_with_msecs_timeout(200); + + // Cache result + duck_io_priniter_init_result = duck_io_rx_byte; + // Fail if the printer is not detected and/or not available + if (duck_io_rx_byte == 0) return duck_io_rx_byte; + } + return duck_io_rx_byte; +}

This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.