#include <stdlib.h>
#include "board.h"
#include "ioutil.h"
#include "i2c.h"
#include "hif.h"
#include "xmpl.h"
#define PROMPT() PRINT("\n\ri2c>>> ")
#define LINE_SIZE (80)
#define MAX_ARGS (16)
#define EOL "\n"
char *argv[MAX_ARGS];
int argc;
bool process_input(int c);
void process_commands(char **argv, int argc);
int main(void)
{
const uint32_t br = HIF_DEFAULT_BAUDRATE;
uint8_t addr, ret;
int chr;
#if HIF_TYPE == HIF_AT90USB
do
{
}
while (EOF == inchar);
#endif
PRINTF(
"\n\rI2C Example : %s : %ld bit/s\n\r", BOARD_NAME, br);
PRINT(EOL
"i2c bus scan:"EOL);
for (addr=0; addr<128; addr++)
{
if (ret)
{
PRINTF(
" a=0x%02x, rv=0x%02x, OK"EOL, addr<<1, ret);
}
WAIT_MS(1);
}
PROMPT();
do
{
if (process_input(chr))
{
if (argc)
{
process_commands(argv, argc);
}
PROMPT();
}
}
while (1);
}
bool process_input(int c)
{
static char line[LINE_SIZE];
static int idx;
bool rv = false;
if (c != EOF)
{
if (c == '\n' || c == '\r')
{
line[idx] = 0;
idx = 0;
rv = true;
}
else if (idx < sizeof(line))
{
line[idx++] = c;
}
else
{
idx = 0;
}
}
return rv;
}
void process_commands(char **argv, int argc)
{
uint8_t wbuf[32], rbuf[32], i;
for (i = 1; i < argc; i++)
{
wbuf[i-1] = (uint8_t) strtol(argv[i], NULL, 16);
}
switch (argv[0][0])
{
case 'r':
PRINTF(
"READ rlen=%d"EOL, wbuf[1]);
break;
case 'w':
break;
case 'x':
PRINTF(
"WRITE/READ wlen=%d, rlen=%d"EOL, argc-2, wbuf[argc-2]);
break;
default:
PRINTF(
"Invalid command %s"EOL, argv[0]);
break;
}
}