From 4150bec5c95ce96bfb24de2496fd0f2c150960ca Mon Sep 17 00:00:00 2001 From: tmk Date: Sun, 9 Feb 2014 02:42:17 +0900 Subject: [PATCH] Add Initial files of 4704_usb --- protocol/ibm4704.c | 150 +++++++++++++++++++++++++++++++++++++++++++++ protocol/ibm4704.h | 111 +++++++++++++++++++++++++++++++++ 2 files changed, 261 insertions(+) create mode 100644 protocol/ibm4704.c create mode 100644 protocol/ibm4704.h diff --git a/protocol/ibm4704.c b/protocol/ibm4704.c new file mode 100644 index 00000000..26441ed5 --- /dev/null +++ b/protocol/ibm4704.c @@ -0,0 +1,150 @@ +/* +Copyright 2010,2011,2012,2013 Jun WAKO +*/ +#include +#include +#include "debug.h" +#include "ibm4704.h" + + +#define WAIT(stat, us, err) do { \ + if (!wait_##stat(us)) { \ + ibm4704_error = err; \ + goto ERROR; \ + } \ +} while (0) + + +uint8_t ibm4704_error = 0; + + +void ibm4704_init(void) +{ + // POR + //_delay_ms(2500); + //while ( 0xA3 != ibm4704_recv() ) ; + + inhibit(); + DDRD |= 1<<3; + PORTD &= ~(1<<3); +} + +uint8_t ibm4704_send(uint8_t data) +{ + bool parity = true; // odd parity + ibm4704_error = 0; + + /* Request to send */ + idle(); + clock_lo(); +PIND |= 1<<3; + + /* wait for Start bit(Clock:lo/Data:hi) */ + WAIT(data_hi, 300, 0x30); + + /* Data bit */ + for (uint8_t i = 0; i < 8; i++) { + WAIT(clock_hi, 100, 0x31); + //_delay_us(5); +PIND |= 1<<3; + if (data&(1< 2) { + xprintf("x%02X ", ibm4704_error); + } + inhibit(); + _delay_us(200); // wait to recover clock to hi + return -1; +} diff --git a/protocol/ibm4704.h b/protocol/ibm4704.h new file mode 100644 index 00000000..ff8a5b77 --- /dev/null +++ b/protocol/ibm4704.h @@ -0,0 +1,111 @@ +/* +Copyright 2014 Jun WAKO +*/ +#ifndef IBM4704_H +#define IBM4704_H + +#define IBM4704_ERR_NONE 0 +#define IBM4704_ERR_STARTBIT 1 +#define IBM4704_ERR_PARITY 0x70 + + +void ibm4704_init(void); +uint8_t ibm4704_send(uint8_t data); +uint8_t ibm4704_recv_response(void); +uint8_t ibm4704_recv(void); + + +/* Check pin configuration */ +#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" +#endif + +#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" +#endif + + +/*-------------------------------------------------------------------- + * static functions + *------------------------------------------------------------------*/ +static inline void clock_lo(void) +{ + IBM4704_CLOCK_PORT &= ~(1<