2015-04-09 18:32:04 +02:00
|
|
|
/*
|
|
|
|
Copyright 2014 Jun WAKO <wakojun@gmail.com>
|
|
|
|
*/
|
2020-12-26 05:56:11 +01:00
|
|
|
|
|
|
|
#pragma once
|
2015-04-09 18:32:04 +02:00
|
|
|
|
2019-08-30 20:19:03 +02:00
|
|
|
#define IBM4704_ERR_NONE 0
|
|
|
|
#define IBM4704_ERR_PARITY 0x70
|
2015-04-09 18:32:04 +02:00
|
|
|
|
2019-08-30 20:19:03 +02:00
|
|
|
void ibm4704_init(void);
|
2015-04-09 18:32:04 +02:00
|
|
|
uint8_t ibm4704_send(uint8_t data);
|
|
|
|
uint8_t ibm4704_recv_response(void);
|
|
|
|
uint8_t ibm4704_recv(void);
|
|
|
|
|
|
|
|
/* Check pin configuration */
|
2019-08-30 20:19:03 +02:00
|
|
|
#if !(defined(IBM4704_CLOCK_PORT) && defined(IBM4704_CLOCK_PIN) && defined(IBM4704_CLOCK_DDR) && defined(IBM4704_CLOCK_BIT))
|
|
|
|
# error "ibm4704 clock pin configuration is required in config.h"
|
2015-04-09 18:32:04 +02:00
|
|
|
#endif
|
|
|
|
|
2019-08-30 20:19:03 +02:00
|
|
|
#if !(defined(IBM4704_DATA_PORT) && defined(IBM4704_DATA_PIN) && defined(IBM4704_DATA_DDR) && defined(IBM4704_DATA_BIT))
|
|
|
|
# error "ibm4704 data pin configuration is required in config.h"
|
2015-04-09 18:32:04 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/*--------------------------------------------------------------------
|
|
|
|
* static functions
|
|
|
|
*------------------------------------------------------------------*/
|
2019-08-30 20:19:03 +02:00
|
|
|
static inline void clock_lo(void) {
|
|
|
|
IBM4704_CLOCK_PORT &= ~(1 << IBM4704_CLOCK_BIT);
|
|
|
|
IBM4704_CLOCK_DDR |= (1 << IBM4704_CLOCK_BIT);
|
2015-04-09 18:32:04 +02:00
|
|
|
}
|
2019-08-30 20:19:03 +02:00
|
|
|
static inline void clock_hi(void) {
|
2015-04-09 18:32:04 +02:00
|
|
|
/* input with pull up */
|
2019-08-30 20:19:03 +02:00
|
|
|
IBM4704_CLOCK_DDR &= ~(1 << IBM4704_CLOCK_BIT);
|
|
|
|
IBM4704_CLOCK_PORT |= (1 << IBM4704_CLOCK_BIT);
|
2015-04-09 18:32:04 +02:00
|
|
|
}
|
2019-08-30 20:19:03 +02:00
|
|
|
static inline bool clock_in(void) {
|
|
|
|
IBM4704_CLOCK_DDR &= ~(1 << IBM4704_CLOCK_BIT);
|
|
|
|
IBM4704_CLOCK_PORT |= (1 << IBM4704_CLOCK_BIT);
|
2015-04-09 18:32:04 +02:00
|
|
|
_delay_us(1);
|
2019-08-30 20:19:03 +02:00
|
|
|
return IBM4704_CLOCK_PIN & (1 << IBM4704_CLOCK_BIT);
|
2015-04-09 18:32:04 +02:00
|
|
|
}
|
2019-08-30 20:19:03 +02:00
|
|
|
static inline void data_lo(void) {
|
|
|
|
IBM4704_DATA_PORT &= ~(1 << IBM4704_DATA_BIT);
|
|
|
|
IBM4704_DATA_DDR |= (1 << IBM4704_DATA_BIT);
|
2015-04-09 18:32:04 +02:00
|
|
|
}
|
2019-08-30 20:19:03 +02:00
|
|
|
static inline void data_hi(void) {
|
2015-04-09 18:32:04 +02:00
|
|
|
/* input with pull up */
|
2019-08-30 20:19:03 +02:00
|
|
|
IBM4704_DATA_DDR &= ~(1 << IBM4704_DATA_BIT);
|
|
|
|
IBM4704_DATA_PORT |= (1 << IBM4704_DATA_BIT);
|
2015-04-09 18:32:04 +02:00
|
|
|
}
|
2019-08-30 20:19:03 +02:00
|
|
|
static inline bool data_in(void) {
|
|
|
|
IBM4704_DATA_DDR &= ~(1 << IBM4704_DATA_BIT);
|
|
|
|
IBM4704_DATA_PORT |= (1 << IBM4704_DATA_BIT);
|
2015-04-09 18:32:04 +02:00
|
|
|
_delay_us(1);
|
2019-08-30 20:19:03 +02:00
|
|
|
return IBM4704_DATA_PIN & (1 << IBM4704_DATA_BIT);
|
2015-04-09 18:32:04 +02:00
|
|
|
}
|
|
|
|
|
2019-08-30 20:19:03 +02:00
|
|
|
static inline uint16_t wait_clock_lo(uint16_t us) {
|
|
|
|
while (clock_in() && us) {
|
|
|
|
asm("");
|
|
|
|
_delay_us(1);
|
|
|
|
us--;
|
|
|
|
}
|
2015-04-09 18:32:04 +02:00
|
|
|
return us;
|
|
|
|
}
|
2019-08-30 20:19:03 +02:00
|
|
|
static inline uint16_t wait_clock_hi(uint16_t us) {
|
|
|
|
while (!clock_in() && us) {
|
|
|
|
asm("");
|
|
|
|
_delay_us(1);
|
|
|
|
us--;
|
|
|
|
}
|
2015-04-09 18:32:04 +02:00
|
|
|
return us;
|
|
|
|
}
|
2019-08-30 20:19:03 +02:00
|
|
|
static inline uint16_t wait_data_lo(uint16_t us) {
|
|
|
|
while (data_in() && us) {
|
|
|
|
asm("");
|
|
|
|
_delay_us(1);
|
|
|
|
us--;
|
|
|
|
}
|
2015-04-09 18:32:04 +02:00
|
|
|
return us;
|
|
|
|
}
|
2019-08-30 20:19:03 +02:00
|
|
|
static inline uint16_t wait_data_hi(uint16_t us) {
|
|
|
|
while (!data_in() && us) {
|
|
|
|
asm("");
|
|
|
|
_delay_us(1);
|
|
|
|
us--;
|
|
|
|
}
|
2015-04-09 18:32:04 +02:00
|
|
|
return us;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* idle state that device can send */
|
2019-08-30 20:19:03 +02:00
|
|
|
static inline void idle(void) {
|
2015-04-09 18:32:04 +02:00
|
|
|
clock_hi();
|
|
|
|
data_hi();
|
|
|
|
}
|
|
|
|
|
2019-08-30 20:19:03 +02:00
|
|
|
/* inhibit device to send
|
2015-04-09 18:32:04 +02:00
|
|
|
* keyboard checks Data line on start bit(Data:hi) and it stops sending if Data line is low.
|
|
|
|
*/
|
2019-08-30 20:19:03 +02:00
|
|
|
static inline void inhibit(void) {
|
2015-04-09 18:32:04 +02:00
|
|
|
clock_hi();
|
|
|
|
data_lo();
|
|
|
|
}
|