1
0
mirror of git://projects.qi-hardware.com/f32xbase.git synced 2024-11-30 17:51:52 +02:00

Moved C2 bitbang functions from c2-om.c to (#included) c2-bitbang.c

- f32x/c2-om.c: renamed C2 bitbang functions from om_* to c2_*
- f32x/c2-om.c,  f32x/c2-bitbang.c: moved most of the content of c2-om.c to
  c2-bitbang.c and #include c2-bitbang.c
- f32x/Makefile: c2-om.o depends on c2-bitbang.c now
This commit is contained in:
Werner Almesberger 2010-10-21 19:09:43 -03:00
parent 3c9abdc65a
commit 6a4da0eaef
3 changed files with 136 additions and 115 deletions

View File

@ -55,6 +55,8 @@ ifeq (.depend,$(wildcard .depend))
include .depend include .depend
endif endif
c2-om.o: c2-bitbang.c
clean: clean:
rm -f $(OBJS) $(OBJS_om) .depend rm -f $(OBJS) $(OBJS_om) .depend

127
f32x/c2-bitbang.c Normal file
View File

@ -0,0 +1,127 @@
/*
* f32x/c2-bitbang.c - Basic C2 messages, bitbang
*
* Written 2008, 2010 by Werner Almesberger
* Copyright 2008, 2010 Werner Almesberger
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
/*
* This file is meant to be #included by a .c file that defines C2CK and C2D,
* provides the gpio_* functions, etc.
*/
/* ----- Bit-level operations ---------------------------------------------- */
static void c2_pulse(void)
{
gpio_low(C2CK);
gpio_high(C2CK);
}
static void c2_send(uint32_t value, int bits)
{
int i;
for (i = 0; i != bits; i++) {
gpio_set(C2D, (value >> i) & 1);
c2_pulse();
}
}
static uint32_t c2_recv(int bits)
{
uint32_t v = 0;
int i;
for (i = 0; i != bits; i++) {
v |= gpio_get(C2D) << i;
c2_pulse();
}
return v;
}
/* ----- C2 Register read/write -------------------------------------------- */
static void c2_addr_write(uint8_t addr)
{
c2_pulse();
gpio_output(C2D);
c2_send(C2_ADDR_WRITE, 2);
c2_send(addr, 8);
gpio_input(C2D);
c2_pulse();
}
static uint8_t c2_addr_read(void)
{
c2_pulse();
gpio_output(C2D);
c2_send(C2_ADDR_READ, 2);
gpio_input(C2D);
c2_pulse();
return c2_recv(8);
}
static void c2_data_write(uint32_t data, int bytes)
{
c2_pulse();
gpio_output(C2D);
c2_send(C2_DATA_WRITE, 2);
c2_send(bytes-1, 2);
c2_send(data, 8*bytes);
gpio_input(C2D);
c2_pulse();
while (!c2_recv(1));
}
static uint32_t c2_data_read(int bytes)
{
c2_pulse();
gpio_output(C2D);
c2_send(C2_DATA_READ, 2);
c2_send(bytes-1, 2);
gpio_input(C2D);
c2_pulse();
while (!c2_recv(1));
return c2_recv(8*bytes);
}
/* ----- C2 initialization ------------------------------------------------- */
static void c2_init(void)
{
gpio_init();
gpio_input(C2D);
gpio_output(C2CK);
gpio_low(C2CK);
usleep(20);
gpio_high(C2CK);
usleep(2);
}
static void c2_reset(void)
{
gpio_input(C2D);
gpio_low(C2CK);
usleep(20);
// gpio_input(C2CK);
gpio_output(C2CK);
gpio_high(C2CK);
}

View File

@ -27,125 +27,17 @@
#define C2D 4, 13 /* E12 = SPI_CLK0 */ #define C2D 4, 13 /* E12 = SPI_CLK0 */
/* ----- Bit-level operations ---------------------------------------------- */ #include "c2-bitbang.c"
static void c2_pulse(void)
{
gpio_low(C2CK);
gpio_high(C2CK);
}
static void c2_send(uint32_t value, int bits)
{
int i;
for (i = 0; i != bits; i++) {
gpio_set(C2D, (value >> i) & 1);
c2_pulse();
}
}
static uint32_t c2_recv(int bits)
{
uint32_t v = 0;
int i;
for (i = 0; i != bits; i++) {
v |= gpio_get(C2D) << i;
c2_pulse();
}
return v;
}
/* ----- C2 Register read/write -------------------------------------------- */
static void om_addr_write(uint8_t addr)
{
c2_pulse();
gpio_output(C2D);
c2_send(C2_ADDR_WRITE, 2);
c2_send(addr, 8);
gpio_input(C2D);
c2_pulse();
}
static uint8_t om_addr_read(void)
{
c2_pulse();
gpio_output(C2D);
c2_send(C2_ADDR_READ, 2);
gpio_input(C2D);
c2_pulse();
return c2_recv(8);
}
static void om_data_write(uint32_t data, int bytes)
{
c2_pulse();
gpio_output(C2D);
c2_send(C2_DATA_WRITE, 2);
c2_send(bytes-1, 2);
c2_send(data, 8*bytes);
gpio_input(C2D);
c2_pulse();
while (!c2_recv(1));
}
static uint32_t om_data_read(int bytes)
{
c2_pulse();
gpio_output(C2D);
c2_send(C2_DATA_READ, 2);
c2_send(bytes-1, 2);
gpio_input(C2D);
c2_pulse();
while (!c2_recv(1));
return c2_recv(8*bytes);
}
/* ----- C2 initialization ------------------------------------------------- */
static void om_init(void)
{
gpio_init();
gpio_input(C2D);
gpio_output(C2CK);
gpio_low(C2CK);
usleep(20);
gpio_high(C2CK);
usleep(2);
}
static void om_reset(void)
{
gpio_input(C2D);
gpio_low(C2CK);
usleep(20);
// gpio_input(C2CK);
gpio_output(C2CK);
gpio_high(C2CK);
}
/* ----- Operations -------------------------------------------------------- */ /* ----- Operations -------------------------------------------------------- */
struct c2_ops c2_om = { struct c2_ops c2_om = {
.init = om_init, .init = c2_init,
.reset = om_reset, .reset = c2_reset,
.addr_write = om_addr_write, .addr_write = c2_addr_write,
.addr_read = om_addr_read, .addr_read = c2_addr_read,
.data_write = om_data_write, .data_write = c2_data_write,
.data_read = om_data_read, .data_read = c2_data_read,
}; };