aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--converter/pc98_usb/config.h9
-rw-r--r--converter/pc98_usb/matrix.c6
-rw-r--r--protocol/serial_soft.c42
3 files changed, 46 insertions, 11 deletions
diff --git a/converter/pc98_usb/config.h b/converter/pc98_usb/config.h
index 8bcaa26db..6f9f8fc3c 100644
--- a/converter/pc98_usb/config.h
+++ b/converter/pc98_usb/config.h
@@ -55,7 +55,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
55 */ 55 */
56#define SERIAL_BAUD 19200 56#define SERIAL_BAUD 19200
57#define SERIAL_PARITY_ODD 57#define SERIAL_PARITY_ODD
58#define SERIAL_BIT_ORDER_MSB 58#define SERIAL_BIT_ORDER_LSB
59 59
60#define SERIAL_RXD_DDR DDRD 60#define SERIAL_RXD_DDR DDRD
61#define SERIAL_RXD_PORT PORTD 61#define SERIAL_RXD_PORT PORTD
@@ -67,7 +67,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
67 SERIAL_RXD_DDR &= ~(1<<SERIAL_RXD_BIT); \ 67 SERIAL_RXD_DDR &= ~(1<<SERIAL_RXD_BIT); \
68 SERIAL_RXD_PORT |= (1<<SERIAL_RXD_BIT); \ 68 SERIAL_RXD_PORT |= (1<<SERIAL_RXD_BIT); \
69 /* enable interrupt: INT2(rising edge) */ \ 69 /* enable interrupt: INT2(rising edge) */ \
70 EICRA |= ((1<<ISC21)|(1<<ISC20)); \ 70 /*EICRA |= ((1<<ISC21)|(1<<ISC20));*/ \
71 /* enable interrupt: INT2(falling edge) */ \
72 EICRA |= ((1<<ISC21)|(0<<ISC20)); \
71 EIMSK |= (1<<INT2); \ 73 EIMSK |= (1<<INT2); \
72} while (0) 74} while (0)
73#define SERIAL_RXD_INT_ENTER() 75#define SERIAL_RXD_INT_ENTER()
@@ -75,7 +77,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
75 /* clear interrupt flag */ \ 77 /* clear interrupt flag */ \
76 EIFR = (1<<INTF2); \ 78 EIFR = (1<<INTF2); \
77} while (0) 79} while (0)
78#define SERIAL_RXD_READ() (~SERIAL_RXD_PIN&(1<<SERIAL_RXD_BIT)) 80//#define SERIAL_RXD_READ() (~SERIAL_RXD_PIN&(1<<SERIAL_RXD_BIT))
81#define SERIAL_RXD_READ() ((SERIAL_RXD_PIN&(1<<SERIAL_RXD_BIT)))
79 82
80#define SERIAL_TXD_DDR DDRD 83#define SERIAL_TXD_DDR DDRD
81#define SERIAL_TXD_PORT PORTD 84#define SERIAL_TXD_PORT PORTD
diff --git a/converter/pc98_usb/matrix.c b/converter/pc98_usb/matrix.c
index b3c11dc21..ef2bc3818 100644
--- a/converter/pc98_usb/matrix.c
+++ b/converter/pc98_usb/matrix.c
@@ -73,6 +73,8 @@ void matrix_init(void)
73 PC98_RDY_PORT |= (1<<PC98_RDY_BIT); 73 PC98_RDY_PORT |= (1<<PC98_RDY_BIT);
74 PC98_RTY_PORT |= (1<<PC98_RTY_BIT); 74 PC98_RTY_PORT |= (1<<PC98_RTY_BIT);
75 75
76 DDRD |= 1<<7;
77
76 78
77 serial_init(); 79 serial_init();
78 80
@@ -83,6 +85,9 @@ void matrix_init(void)
83 _delay_us(13); 85 _delay_us(13);
84 PC98_RDY_PORT |= (1<<PC98_RDY_BIT); 86 PC98_RDY_PORT |= (1<<PC98_RDY_BIT);
85 87
88 // PC98 ready
89 PC98_RDY_PORT &= ~(1<<PC98_RDY_BIT);
90
86 // initialize matrix state: all keys off 91 // initialize matrix state: all keys off
87 for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00; 92 for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
88 93
@@ -96,6 +101,7 @@ uint8_t matrix_scan(void)
96 101
97 uint8_t code; 102 uint8_t code;
98 PC98_RDY_PORT |= (1<<PC98_RDY_BIT); 103 PC98_RDY_PORT |= (1<<PC98_RDY_BIT);
104 _delay_us(30);
99 code = serial_recv(); 105 code = serial_recv();
100 PC98_RDY_PORT &= ~(1<<PC98_RDY_BIT); 106 PC98_RDY_PORT &= ~(1<<PC98_RDY_BIT);
101 if (!code) return 0; 107 if (!code) return 0;
diff --git a/protocol/serial_soft.c b/protocol/serial_soft.c
index e0661c3aa..47b1e4571 100644
--- a/protocol/serial_soft.c
+++ b/protocol/serial_soft.c
@@ -48,6 +48,23 @@ POSSIBILITY OF SUCH DAMAGE.
48 48
49#define WAIT_US (1000000/SERIAL_BAUD) 49#define WAIT_US (1000000/SERIAL_BAUD)
50 50
51#if 1
52#define WAIT_TICK (1000000/SERIAL_BAUD)
53#define WAIT4(tick) _delay_us(tick)
54#else
55#define WAIT_TICK ((16000000/SERIAL_BAUD)/4 - 5)
56static inline void WAIT4(uint8_t tick)
57{
58 __asm__ __volatile__ (
59 "1: dec %0" "\n\t"
60 "nop" "\n\t"
61 "brne 1b"
62 :
63 : "r" (tick)
64 );
65}
66#endif
67
51void serial_init(void) 68void serial_init(void)
52{ 69{
53 SERIAL_RXD_INIT(); 70 SERIAL_RXD_INIT();
@@ -60,6 +77,7 @@ static uint8_t rbuf[RBUF_SIZE];
60static uint8_t rbuf_head = 0; 77static uint8_t rbuf_head = 0;
61static uint8_t rbuf_tail = 0; 78static uint8_t rbuf_tail = 0;
62 79
80
63uint8_t serial_recv(void) 81uint8_t serial_recv(void)
64{ 82{
65 uint8_t data = 0; 83 uint8_t data = 0;
@@ -103,6 +121,7 @@ void serial_send(uint8_t data)
103/* detect edge of start bit */ 121/* detect edge of start bit */
104ISR(SERIAL_RXD_VECT) 122ISR(SERIAL_RXD_VECT)
105{ 123{
124PORTD ^= 1<<7;
106 SERIAL_RXD_INT_ENTER() 125 SERIAL_RXD_INT_ENTER()
107 126
108 uint8_t data = 0; 127 uint8_t data = 0;
@@ -120,11 +139,15 @@ ISR(SERIAL_RXD_VECT)
120#endif 139#endif
121 140
122 /* to center of start bit */ 141 /* to center of start bit */
123 _delay_us(WAIT_US/2); 142 //_delay_us(WAIT_US/2);
143 WAIT4(WAIT_TICK/2);
144PORTD ^= 1<<7;
124 do { 145 do {
125 /* to center of next bit */ 146 /* to center of next bit */
126 _delay_us(WAIT_US); 147 //_delay_us(WAIT_US);
148 WAIT4(WAIT_TICK);
127 149
150PORTD ^= 1<<7;
128 if (SERIAL_RXD_READ()) { 151 if (SERIAL_RXD_READ()) {
129 data |= mask; 152 data |= mask;
130 parity ^= 1; 153 parity ^= 1;
@@ -137,19 +160,22 @@ ISR(SERIAL_RXD_VECT)
137 } while (mask); 160 } while (mask);
138 161
139 /* to center of parity bit */ 162 /* to center of parity bit */
140 _delay_us(WAIT_US); 163 //_delay_us(WAIT_US);
141 parity ^= SERIAL_RXD_READ(); 164 WAIT4(WAIT_TICK);
165 if (SERIAL_RXD_READ()) { parity ^= 1; }
166PORTD ^= 1<<7;
142 167
143 /* to center of stop bit */ 168 /* to center of stop bit */
144 _delay_us(WAIT_US); 169 //_delay_us(WAIT_US);
145 _delay_us(WAIT_US/2); 170 WAIT4(WAIT_TICK);
146 171
147 parity = 1;
148 uint8_t next = (rbuf_head + 1) % RBUF_SIZE; 172 uint8_t next = (rbuf_head + 1) % RBUF_SIZE;
149 if (parity && next != rbuf_tail) { 173 //if (parity && next != rbuf_tail) {
174 if (next != rbuf_tail) {
150 rbuf[rbuf_head] = data; 175 rbuf[rbuf_head] = data;
151 rbuf_head = next; 176 rbuf_head = next;
152 } 177 }
153 178
154 SERIAL_RXD_INT_EXIT(); 179 SERIAL_RXD_INT_EXIT();
180PORTD ^= 1<<7;
155} 181}