diff options
| author | tmk <hasu@tmk-kbd.com> | 2015-04-10 01:32:04 +0900 |
|---|---|---|
| committer | tmk <hasu@tmk-kbd.com> | 2015-04-10 01:32:04 +0900 |
| commit | 1a02ebcc612e9a9c0d87e02295c7258de3a70ccc (patch) | |
| tree | e517f3c70bb2d542797e57d13e9023c84af230fb /tmk_core/protocol/ibm4704.h | |
| parent | 6746e37088ce8ba03529c1226bd216705edb2b1f (diff) | |
| parent | a074364c3731d66b56d988c8a6c960a83ea0e0a1 (diff) | |
| download | qmk_firmware-1a02ebcc612e9a9c0d87e02295c7258de3a70ccc.tar.gz qmk_firmware-1a02ebcc612e9a9c0d87e02295c7258de3a70ccc.zip | |
Merge commit 'a074364c3731d66b56d988c8a6c960a83ea0e0a1' as 'tmk_core'
Diffstat (limited to 'tmk_core/protocol/ibm4704.h')
| -rw-r--r-- | tmk_core/protocol/ibm4704.h | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/tmk_core/protocol/ibm4704.h b/tmk_core/protocol/ibm4704.h new file mode 100644 index 000000000..618cce6be --- /dev/null +++ b/tmk_core/protocol/ibm4704.h | |||
| @@ -0,0 +1,110 @@ | |||
| 1 | /* | ||
| 2 | Copyright 2014 Jun WAKO <wakojun@gmail.com> | ||
| 3 | */ | ||
| 4 | #ifndef IBM4704_H | ||
| 5 | #define IBM4704_H | ||
| 6 | |||
| 7 | #define IBM4704_ERR_NONE 0 | ||
| 8 | #define IBM4704_ERR_PARITY 0x70 | ||
| 9 | |||
| 10 | |||
| 11 | void ibm4704_init(void); | ||
| 12 | uint8_t ibm4704_send(uint8_t data); | ||
| 13 | uint8_t ibm4704_recv_response(void); | ||
| 14 | uint8_t ibm4704_recv(void); | ||
| 15 | |||
| 16 | |||
| 17 | /* Check pin configuration */ | ||
| 18 | #if !(defined(IBM4704_CLOCK_PORT) && \ | ||
| 19 | defined(IBM4704_CLOCK_PIN) && \ | ||
| 20 | defined(IBM4704_CLOCK_DDR) && \ | ||
| 21 | defined(IBM4704_CLOCK_BIT)) | ||
| 22 | # error "ibm4704 clock pin configuration is required in config.h" | ||
| 23 | #endif | ||
| 24 | |||
| 25 | #if !(defined(IBM4704_DATA_PORT) && \ | ||
| 26 | defined(IBM4704_DATA_PIN) && \ | ||
| 27 | defined(IBM4704_DATA_DDR) && \ | ||
| 28 | defined(IBM4704_DATA_BIT)) | ||
| 29 | # error "ibm4704 data pin configuration is required in config.h" | ||
| 30 | #endif | ||
| 31 | |||
| 32 | |||
| 33 | /*-------------------------------------------------------------------- | ||
| 34 | * static functions | ||
| 35 | *------------------------------------------------------------------*/ | ||
| 36 | static inline void clock_lo(void) | ||
| 37 | { | ||
| 38 | IBM4704_CLOCK_PORT &= ~(1<<IBM4704_CLOCK_BIT); | ||
| 39 | IBM4704_CLOCK_DDR |= (1<<IBM4704_CLOCK_BIT); | ||
| 40 | } | ||
| 41 | static inline void clock_hi(void) | ||
| 42 | { | ||
| 43 | /* input with pull up */ | ||
| 44 | IBM4704_CLOCK_DDR &= ~(1<<IBM4704_CLOCK_BIT); | ||
| 45 | IBM4704_CLOCK_PORT |= (1<<IBM4704_CLOCK_BIT); | ||
| 46 | } | ||
| 47 | static inline bool clock_in(void) | ||
| 48 | { | ||
| 49 | IBM4704_CLOCK_DDR &= ~(1<<IBM4704_CLOCK_BIT); | ||
| 50 | IBM4704_CLOCK_PORT |= (1<<IBM4704_CLOCK_BIT); | ||
| 51 | _delay_us(1); | ||
| 52 | return IBM4704_CLOCK_PIN&(1<<IBM4704_CLOCK_BIT); | ||
| 53 | } | ||
| 54 | static inline void data_lo(void) | ||
| 55 | { | ||
| 56 | IBM4704_DATA_PORT &= ~(1<<IBM4704_DATA_BIT); | ||
| 57 | IBM4704_DATA_DDR |= (1<<IBM4704_DATA_BIT); | ||
| 58 | } | ||
| 59 | static inline void data_hi(void) | ||
| 60 | { | ||
| 61 | /* input with pull up */ | ||
| 62 | IBM4704_DATA_DDR &= ~(1<<IBM4704_DATA_BIT); | ||
| 63 | IBM4704_DATA_PORT |= (1<<IBM4704_DATA_BIT); | ||
| 64 | } | ||
| 65 | static inline bool data_in(void) | ||
| 66 | { | ||
| 67 | IBM4704_DATA_DDR &= ~(1<<IBM4704_DATA_BIT); | ||
| 68 | IBM4704_DATA_PORT |= (1<<IBM4704_DATA_BIT); | ||
| 69 | _delay_us(1); | ||
| 70 | return IBM4704_DATA_PIN&(1<<IBM4704_DATA_BIT); | ||
| 71 | } | ||
| 72 | |||
| 73 | static inline uint16_t wait_clock_lo(uint16_t us) | ||
| 74 | { | ||
| 75 | while (clock_in() && us) { asm(""); _delay_us(1); us--; } | ||
| 76 | return us; | ||
| 77 | } | ||
| 78 | static inline uint16_t wait_clock_hi(uint16_t us) | ||
| 79 | { | ||
| 80 | while (!clock_in() && us) { asm(""); _delay_us(1); us--; } | ||
| 81 | return us; | ||
| 82 | } | ||
| 83 | static inline uint16_t wait_data_lo(uint16_t us) | ||
| 84 | { | ||
| 85 | while (data_in() && us) { asm(""); _delay_us(1); us--; } | ||
| 86 | return us; | ||
| 87 | } | ||
| 88 | static inline uint16_t wait_data_hi(uint16_t us) | ||
| 89 | { | ||
| 90 | while (!data_in() && us) { asm(""); _delay_us(1); us--; } | ||
| 91 | return us; | ||
| 92 | } | ||
| 93 | |||
| 94 | /* idle state that device can send */ | ||
| 95 | static inline void idle(void) | ||
| 96 | { | ||
| 97 | clock_hi(); | ||
| 98 | data_hi(); | ||
| 99 | } | ||
| 100 | |||
| 101 | /* inhibit device to send | ||
| 102 | * keyboard checks Data line on start bit(Data:hi) and it stops sending if Data line is low. | ||
| 103 | */ | ||
| 104 | static inline void inhibit(void) | ||
| 105 | { | ||
| 106 | clock_hi(); | ||
| 107 | data_lo(); | ||
| 108 | } | ||
| 109 | |||
| 110 | #endif | ||
