GBDK 2020 Docs  4.4.0
API Documentation for GBDK 2020
laptop_io.h
Go to the documentation of this file.
1 #include <gbdk/platform.h>
2 #include <stdint.h>
3 #include <stdbool.h>
4 
26 #ifndef _MEGADUCK_LAPTOP_IO_H
27 #define _MEGADUCK_LAPTOP_IO_H
28 
29 
30 // Commands sent via serial IO to the Duck laptop peripheral hardware
31 #define DUCK_IO_CMD_INIT_START 0x00u
32 #define DUCK_IO_CMD_GET_KEYS 0x00u
33 #define DUCK_IO_CMD_DONE_OR_OK 0x01u
34 // #define DUCK_IO_CMD_DONE_OR_OK_AND_SOMETHING 0x81u
35 #define DUCK_IO_CMD_ABORT_OR_FAIL 0x04u
36 #define DUCK_IO_CMD_PLAY_SPEECH 0x05u
37 #define DUCK_IO_CMD_RUN_CART_IN_SLOT 0x08u
38 #define DUCK_IO_CMD_PRINT_INIT_MAYBE_EXT_IO 0x09u
39 #define DUCK_IO_CMD_SET_RTC 0x0Bu
40 #define DUCK_IO_CMD_GET_RTC 0x0Cu
41 #define DUCK_IO_CMD_PRINT_SEND_BYTES 0x11u
44 // #define FF60_REG_BEFORE_XFER 0x00u
45 #define DUCK_IO_REPLY_BOOT_UNSET 0x00u
46 #define DUCK_IO_REPLY_BOOT_FAIL 0x01u
47 #define DUCK_IO_REPLY_BUFFER_XFER_OK 0x01u
48 #define DUCK_IO_REPLY_SEND_BUFFER_OK 0x03u
49 // #define DUCK_IO_REPLY_READ_FAIL_MAYBE 0x00u
50 #define DUCK_IO_REPLY_BOOT_OK 0x01u
51 
52 #define DUCK_IO_LEN_KBD_GET 2u
53 #define DUCK_IO_LEN_RTC_GET 8u
54 #define DUCK_IO_LEN_RTC_SET 8u
55 #define DUCK_IO_LEN_PLAY_SPEECH 1u
57 #define DUCK_IO_REPLY_NO_CART_IN_SLOT 06u
58 
59 // #define MEGADUCK_KBD_BYTE_1_EXPECT 0x0Eu
60 // #define MEGADUCK_SIO_BOOT_OK 0x01u
61 
62 #define DUCK_IO_LEN_RX_MAX 14u // 13 data bytes + 1 checksum byte max reply length?
63 #define DUCK_IO_LEN_TX_MAX 14u // 13 data bytes + 1 checksum byte max reply length?
64 
65 #define DUCK_IO_TIMEOUT_2_MSEC 2u // Used for hardware init counter sequence
66 #define DUCK_IO_TIMEOUT_100_MSEC 100u
67 #define DUCK_IO_TIMEOUT_200_MSEC 200u
68 
69 
70 // Pre-recorded Speech Samples for playback
71 #define DUCK_IO_SPEECH_CMD_MIN 1
72 #define DUCK_IO_SPEECH_CMD_MAX 6
73 
74 
75 // RTC packet byte ordering (all in BCD format)
76 #define DUCK_IO_RTC_YEAR 0u
77 #define DUCK_IO_RTC_MON 1u
78 #define DUCK_IO_RTC_DAY 2u
79 #define DUCK_IO_RTC_WEEKDAY 3u
80 #define DUCK_IO_RTC_AMPM 4u
81 #define DUCK_IO_RTC_HOUR 5u
82 #define DUCK_IO_RTC_MIN 6u
83 #define DUCK_IO_RTC_SEC 7u
84 
85 
86 // Keyboard packet byte ordering (all in BCD format)
87 #define DUCK_IO_KBD_FLAGS 0u
88 #define DUCK_IO_KBD_KEYCODE 1u
89 
90 
91 // Printer init reply related
92 // Init Reply Bit.0
93 #define DUCK_IO_PRINTER_INIT_MASK 0x01u // Bit.0
94 #define DUCK_IO_PRINTER_INIT_OK 0x01u
95 #define DUCK_IO_PRINTER_INIT_FAIL 0x00u
96 
97 // Init Reply Bit.1
98 #define DUCK_IO_PRINTER_TYPE_MASK 0x02u // Bit.1
99 #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)
100 #define DUCK_IO_PRINTER_TYPE_1_PASS 0x02u // Bit.1 = 1 // 3 x 12 byte packets + 118 non-packet bytes
101 
102 
103 
104 extern volatile bool duck_io_rx_byte_done;
105 extern volatile uint8_t duck_io_rx_byte;
106 
107 
108 // TODO: change these to user supplied buffers?
111 
114 
115 
116 // ===== Low level helper IO functions =====
117 
118 
119 // TODO: No longer in use(?)
120 //
121 // Waits for a serial transfer to complete with a timeout
122 //
123 // @param timeout_len_ms Unit size is in msec (100 is about ~ 103 msec or 6.14 frames)
124 //
125 // Serial ISR populates status var if anything was received
126 //
127 // void duck_io_wait_done_with_timeout(uint8_t timeout_len_ms);
128 
129 
136 
137 
145 
146 
156 
157 
169 
170 
178 
179 
189 
190 
191 // ===== Higher level IO functions =====
192 
193 
203 
204 
215 bool duck_io_send_byte_and_check_ack_msecs_timeout(uint8_t tx_byte, uint8_t timeout_len_ms, uint8_t expected_reply);
216 
217 
231 
232 
246 
247 #endif // _MEGADUCK_LAPTOP_IO_H
bool duck_io_send_byte_and_check_ack_msecs_timeout(uint8_t tx_byte, uint8_t timeout_len_ms, uint8_t expected_reply)
void duck_io_send_byte(uint8_t tx_byte)
bool duck_io_send_cmd_and_receive_buffer(uint8_t io_cmd)
#define DUCK_IO_LEN_TX_MAX
Definition: laptop_io.h:63
uint8_t duck_io_rx_buf[DUCK_IO_LEN_RX_MAX]
void duck_io_enable_read_byte(void)
bool duck_io_send_cmd_and_buffer(uint8_t io_cmd)
bool duck_io_printer_detected(void)
uint8_t duck_io_printer_type(void)
uint8_t duck_io_read_byte_no_timeout(void)
uint8_t duck_io_tx_buf[DUCK_IO_LEN_TX_MAX]
#define DUCK_IO_LEN_RX_MAX
Definition: laptop_io.h:62
uint8_t duck_io_rx_buf_len
volatile bool duck_io_rx_byte_done
uint8_t duck_io_tx_buf_len
bool duck_io_laptop_init(void)
bool duck_io_read_byte_with_msecs_timeout(uint8_t timeout_len_ms)
volatile uint8_t duck_io_rx_byte
unsigned char uint8_t
Definition: stdint.h:51