[fluksod] implement spi read operation in C

This commit is contained in:
Bart Van Der Meerssche 2011-01-11 09:24:29 +01:00
parent aa412d1dc4
commit d3dd4366da
2 changed files with 65 additions and 15 deletions

View File

@ -110,21 +110,8 @@ function tx(msg, cdev)
end end
function rx(msg, cdev) function rx(msg, cdev)
local sequence = {}
for char in function() return cdev:read(1) end do
if char ~= '\0' then
table.insert(sequence, char)
else
-- one more read to let the AVR send a second 0x00
-- after which the AVR's state machine toggles to read mode
cdev:read(1)
break
end
end
msg.received = {} msg.received = {}
msg.received.raw = table.concat(sequence) msg.received.raw = cdev:spiread()
msg.received.l, msg.received.u = msg.received.raw:match('^l(%w*)%.?u(%w*)%.?$') msg.received.l, msg.received.u = msg.received.raw:match('^l(%w*)%.?u(%w*)%.?$')
if msg.received.l ~= '' and msg.received.l:sub(1, 2) == msg.parsed[1] then if msg.received.l ~= '' and msg.received.l:sub(1, 2) == msg.parsed[1] then

View File

@ -223,6 +223,68 @@ static int nixio_file_read(lua_State *L) {
} }
} }
static int nixio_file_spi_read(lua_State *L) {
int fd = nixio__checkfd(L, 1);
char buffer[NIXIO_BUFFERSIZE];
int readc;
size_t len;
char last = 0;
for (size_t i = 0; i < NIXIO_BUFFERSIZE; i++) {
do {
readc = read(fd, buffer + i, 1);
} while (readc == -1 && errno == EINTR);
if (readc < 0) {
return nixio__perror(L);
}
if (last) {
break;
}
if (buffer[i] == 0x00) {
len = i;
last = 1; /* one last pass through the for loop to sync the state machine */
}
}
/* REVISIT: a read on spidev doens't work for numbytes > 1 */
/* for (size_t i = 0; i < NIXIO_BUFFERSIZE; i += 2) {
do {
readc = read(fd, buffer + i, 2);
} while (readc == -1 && errno == EINTR);
if (readc < 0) {
return nixio__perror(L);
}
if (buffer[i + 1] == 0x00) {
len = i;
if (buffer[i] != 0x00) {
len = i + 1;
do {
readc = read(fd, buffer + i + 2, 1);
} while (readc == -1 && errno == EINTR);
if (readc < 0) {
return nixio__perror(L);
}
}
break;
}
}
*/
lua_pushlstring(L, buffer, len);
return 1;
}
static int nixio_file_seek(lua_State *L) { static int nixio_file_seek(lua_State *L) {
int fd = nixio__checkfd(L, 1); int fd = nixio__checkfd(L, 1);
@ -350,6 +412,7 @@ static int nixio_file__tostring(lua_State *L) {
static const luaL_reg M[] = { static const luaL_reg M[] = {
{"write", nixio_file_write}, {"write", nixio_file_write},
{"read", nixio_file_read}, {"read", nixio_file_read},
{"spiread", nixio_file_spi_read},
{"tell", nixio_file_tell}, {"tell", nixio_file_tell},
{"seek", nixio_file_seek}, {"seek", nixio_file_seek},
{"stat", nixio_file_stat}, {"stat", nixio_file_stat},
@ -365,7 +428,7 @@ static const luaL_reg M[] = {
static const luaL_reg R[] = { static const luaL_reg R[] = {
{"dup", nixio_dup}, {"dup", nixio_dup},
{"open", nixio_open}, {"open", nixio_open},
{"open_flags", nixio_open_flags}, {"open_flags", nixio_open_flags},
{"pipe", nixio_pipe}, {"pipe", nixio_pipe},
{NULL, NULL} {NULL, NULL}
}; };