1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
|
/*
* Copyright (C) 2016 FAUmachine Team <info@faumachine.org>.
* This program is free software. You can redistribute it and/or modify it
* under the terms of the GNU General Public License, either version 2 of
* the License, or (at your option) any later version. See COPYING.
*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#define INCLUDE
#include "arch_i2c_slave.c"
#undef INCLUDE
#include "glue.h"
#include "chip_st_lis302dl.h"
#define CHIP_(x) chip_st_lis302dl_ ## x
struct cpssp {
unsigned int state_power;
unsigned int device_addr;
struct sig_std_logic *port_sda;
int is_address_phase;
uint8_t counter;
#define STATE
#define NAME i2c_slave
#define NAME_(x) i2c_slave_ ## x
#include "arch_i2c_slave.c"
#undef NAME_
#undef NAME
#undef STATE
};
static uint8_t
CHIP_(reg_read)(struct cpssp *cpssp, uint8_t addr)
{
return 0; /* FIXME */
}
static void
CHIP_(reg_write)(struct cpssp *cpssp, uint8_t addr, uint8_t val)
{
/* FIXME */
}
static int
CHIP_(ack_addr)(struct cpssp *cpssp, unsigned char addr)
{
if ((addr & 0xfe) == (0x38 | (cpssp->device_addr << 1))) {
/* Selected */
return 1;
}
return 0;
}
static void
CHIP_(read_byte)(struct cpssp *cpssp, unsigned char *valp)
{
if (cpssp->is_address_phase) {
*valp = cpssp->counter;
cpssp->is_address_phase = 0;
} else {
assert(cpssp->counter < 256);
*valp = CHIP_(reg_read)(cpssp, cpssp->counter);
cpssp->counter = (cpssp->counter + 1) & (256 - 1);
assert(cpssp->counter < 256);
}
}
static int
CHIP_(write_byte)(struct cpssp *cpssp, unsigned char val)
{
if (cpssp->is_address_phase) {
cpssp->counter = val;
assert(cpssp->counter < 256);
cpssp->is_address_phase = 0;
} else {
assert(cpssp->counter < 256);
CHIP_(reg_write)(cpssp, cpssp->counter, val);
cpssp->counter = (cpssp->counter + 1) & (256 - 1);
assert(cpssp->counter < 256);
}
return 0;
}
static void
CHIP_(stop_transaction)(struct cpssp *cpssp)
{
cpssp->is_address_phase = 1;
}
static int
i2c_slave_ack_addr(struct cpssp *cpssp, uint8_t val)
{
return CHIP_(ack_addr)(cpssp, val);
}
static void
i2c_slave_read_byte(struct cpssp *cpssp, uint8_t *valp)
{
CHIP_(read_byte)(cpssp, valp);
}
static void
i2c_slave_write_byte(struct cpssp *cpssp, uint8_t val)
{
CHIP_(write_byte)(cpssp, val);
}
static void
i2c_slave_stop_transaction(struct cpssp *cpssp)
{
CHIP_(stop_transaction)(cpssp);
}
static void
i2c_slave_sda_out(struct cpssp *cpssp, unsigned int val)
{
switch (val) {
case SIG_STD_LOGIC_Z: val = 1; break;
case SIG_STD_LOGIC_1: val = 1; break;
case SIG_STD_LOGIC_0: val = 0; break;
default: assert(0);
}
sig_std_logic_set(cpssp->port_sda, cpssp, val);
}
#define BEHAVIOR
#define NAME i2c_slave
#define NAME_(x) i2c_slave_ ## x
#include "arch_i2c_slave.c"
#undef NAME_
#undef NAME
#undef BEHAVIOR
static void
CHIP_(scl_in_set)(void *_cpssp, unsigned int val)
{
struct cpssp *cpssp = _cpssp;
i2c_slave_scl_in(cpssp, val);
}
static void
CHIP_(sda_in_set)(void *_cpssp, unsigned int val)
{
struct cpssp *cpssp = _cpssp;
i2c_slave_sda_in(cpssp, val);
}
static void
CHIP_(sdo_in_set)(void *_cpssp, unsigned int val)
{
struct cpssp *cpssp = _cpssp;
cpssp->device_addr = val;
}
static void
CHIP_(vdd_in_set)(void *_cpssp, unsigned int val)
{
struct cpssp *cpssp = _cpssp;
cpssp->state_power = val;
if (val) {
/* Power-on reset. */
cpssp->is_address_phase = 1;
}
}
void *
CHIP_(create)(
const char *name,
struct sig_manage *manage,
struct sig_std_logic *port_gnd0,
struct sig_std_logic *port_gnd1,
struct sig_std_logic *port_gnd2,
struct sig_std_logic *port_gnd3,
struct sig_std_logic *port_R1,
struct sig_std_logic *port_vdd_io,
struct sig_std_logic *port_vdd,
struct sig_std_logic *port_R0,
struct sig_std_logic *port_sda,
struct sig_std_logic *port_scl,
struct sig_std_logic *port_int2,
struct sig_std_logic *port_int1,
struct sig_std_logic *port_cs,
struct sig_std_logic *port_sdo
)
{
static const struct sig_std_logic_funcs vdd_funcs = {
.boolean_or_set = CHIP_(vdd_in_set),
};
static const struct sig_std_logic_funcs scl_funcs = {
.std_logic_set = CHIP_(scl_in_set),
};
static const struct sig_std_logic_funcs sda_funcs = {
.std_logic_set = CHIP_(sda_in_set),
};
static const struct sig_std_logic_funcs sdo_funcs = {
.boolean_or_set = CHIP_(sdo_in_set),
};
struct cpssp *cpssp;
cpssp = malloc(sizeof(*cpssp));
assert(cpssp);
cpssp->device_addr = 0;
/* FIXME */
i2c_slave_create(cpssp);
cpssp->state_power = 0;
sig_std_logic_connect_in(port_vdd, cpssp, &vdd_funcs);
sig_std_logic_connect_in(port_scl, cpssp, &scl_funcs);
sig_std_logic_connect_in(port_sda, cpssp, &sda_funcs);
sig_std_logic_connect_in(port_sdo, cpssp, &sdo_funcs);
return cpssp;
}
void
CHIP_(destroy)(void *_cpssp)
{
struct cpssp *cpssp = _cpssp;
/* FIXME */
i2c_slave_destroy(cpssp);
free(cpssp);
}
void
CHIP_(suspend)(void *_cpssp, FILE *fp)
{
struct cpssp *cpssp = _cpssp;
generic_suspend(cpssp, sizeof(*cpssp), fp);
}
void
CHIP_(resume)(void *_cpssp, FILE *fp)
{
struct cpssp *cpssp = _cpssp;
generic_resume(cpssp, sizeof(*cpssp), fp);
}
|