xt_usb: Replace functions with macros

This commit is contained in:
tmk 2018-03-02 13:56:46 +09:00
parent 42199c90f8
commit acbea7fb15
5 changed files with 32 additions and 70 deletions

View file

@ -12,7 +12,6 @@ TARGET_DIR = .
# project specific files
SRC = protocol/xt_interrupt.c \
protocol/xt_io_avr.c \
matrix.c \
led.c

View file

@ -39,22 +39,7 @@ POSSIBILITY OF SUCH DAMAGE.
#ifndef XT_H
#define XT_H
#include <stdbool.h>
#include "wait.h"
#include "xt_io.h"
#include "print.h"
void xt_host_init(void);
uint8_t xt_host_recv(void);
/*--------------------------------------------------------------------
* static functions
*------------------------------------------------------------------*/
static inline uint16_t wait_clock_lo(uint16_t us)
{
while (clock_in() && us) { asm(""); wait_us(1); us--; }
return us;
}
#endif

View file

@ -51,6 +51,7 @@ POSSIBILITY OF SUCH DAMAGE.
void xt_host_init(void)
{
XT_INT_INIT();
XT_INT_OFF();
/* hard reset */
#ifdef XT_RESET
@ -58,10 +59,14 @@ void xt_host_init(void)
#endif
/* soft reset: pull clock line down for 20ms */
XT_INT_OFF();
data_lo(); clock_lo();
XT_DATA_LO();
XT_CLOCK_LO();
_delay_ms(20);
data_in(); clock_in();
/* input mode with pullup */
XT_CLOCK_IN();
XT_DATA_IN();
XT_INT_ON();
}
@ -93,7 +98,7 @@ ISR(XT_INT_VECT)
} state = START;
static uint8_t data = 0;
uint8_t dbit = data_in();
uint8_t dbit = XT_DATA_READ();
// This is needed if using PCINT which can be called on both falling and rising edge
//if (clock_in()) return;

View file

@ -1,10 +1,29 @@
#ifndef XT_IO_H
#define XT_IO_H
bool clock_in(void);
bool data_in(void);
#define XT_DATA_IN() do { \
XT_DATA_DDR &= ~(1<<XT_DATA_BIT); \
XT_DATA_PORT |= (1<<XT_DATA_BIT); \
} while (0)
void clock_lo(void);
void data_lo(void);
#define XT_DATA_READ() (XT_DATA_PIN&(1<<XT_DATA_BIT))
#define XT_DATA_LO() do { \
XT_DATA_PORT &= ~(1<<XT_DATA_BIT); \
XT_DATA_DDR |= (1<<XT_DATA_BIT); \
} while (0)
#define XT_CLOCK_IN() do { \
XT_CLOCK_DDR &= ~(1<<XT_CLOCK_BIT); \
XT_CLOCK_PORT |= (1<<XT_CLOCK_BIT); \
} while (0)
#define XT_CLOCK_READ() (XT_CLOCK_PIN&(1<<XT_CLOCK_BIT))
#define XT_CLOCK_LO() do { \
XT_CLOCK_PORT &= ~(1<<XT_CLOCK_BIT); \
XT_CLOCK_DDR |= (1<<XT_CLOCK_BIT); \
} while (0)
#endif

View file

@ -1,46 +0,0 @@
#include <stdbool.h>
#include <avr/io.h>
#include <util/delay.h>
/* Check port settings for clock and data line */
#if !(defined(XT_CLOCK_PORT) && \
defined(XT_CLOCK_PIN) && \
defined(XT_CLOCK_DDR) && \
defined(XT_CLOCK_BIT))
# error "XT clock port setting is required in config.h"
#endif
#if !(defined(XT_DATA_PORT) && \
defined(XT_DATA_PIN) && \
defined(XT_DATA_DDR) && \
defined(XT_DATA_BIT))
# error "XT data port setting is required in config.h"
#endif
bool clock_in(void)
{
XT_CLOCK_DDR &= ~(1<<XT_CLOCK_BIT);
XT_CLOCK_PORT |= (1<<XT_CLOCK_BIT);
_delay_us(1);
return XT_CLOCK_PIN&(1<<XT_CLOCK_BIT);
}
bool data_in(void)
{
XT_DATA_DDR &= ~(1<<XT_DATA_BIT);
XT_DATA_PORT |= (1<<XT_DATA_BIT);
_delay_us(1);
return XT_DATA_PIN&(1<<XT_DATA_BIT);
}
void clock_lo(void)
{
XT_CLOCK_PORT &= ~(1<<XT_CLOCK_BIT);
XT_CLOCK_DDR |= (1<<XT_CLOCK_BIT);
}
void data_lo(void)
{
XT_DATA_PORT &= ~(1<<XT_DATA_BIT);
XT_DATA_DDR |= (1<<XT_DATA_BIT);
}