1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-04-19 23:22:16 +03:00

Use i2c instead of serial for wifio virtual io

This commit is contained in:
Ivan Grokhotkov 2015-01-21 04:17:03 +03:00
parent ee35484577
commit 045c446213
2 changed files with 30 additions and 44 deletions

View File

@ -24,11 +24,12 @@
#include <stdint.h> #include <stdint.h>
#include "Arduino.h" #include "Arduino.h"
#include "i2c.h"
inline void protocolError() inline void protocolError()
{ {
delay(5); delay(5);
Serial.flush(); i2c_stop();
} }
namespace wifio { namespace wifio {
@ -112,48 +113,39 @@ struct AnalogReadResultCommand {
uint8_t parity : 1; uint8_t parity : 1;
}; };
template<typename T> template<typename T>
void sendCommand(Stream& stream, T& t) void sendCommand(uint8_t addr, T& t)
{ {
// TODO: calculate parity // TODO: calculate parity
t.parity = 1; t.parity = 1;
stream.write(reinterpret_cast<const uint8_t*>(&t), sizeof(T)); if (i2c_master_write_to(addr, reinterpret_cast<const uint8_t*>(&t), sizeof(T), true) != sizeof(T))
{
protocolError();
}
} }
template<typename T> template<typename TC, typename TR>
bool expectCommand(Stream& stream, enum Command cmd, T& t) bool sendCommandWaitForReply(uint8_t addr, TC& c, Command rt, TR& r, int32_t d_us)
{ {
size_t nIt = 0; c.parity = 1;
t = { 0 }; if (i2c_master_write_to(addr, reinterpret_cast<const uint8_t*>(&c), sizeof(TC), true) != sizeof(TC))
while (stream.available() < sizeof(T))
{ {
if (++nIt == 100) { protocolError();
return false; return false;
}
delayMicroseconds(10);
} }
uint8_t* p = reinterpret_cast<uint8_t*>(&t); if (d_us)
for (size_t i = 0; i < sizeof(T); ++i) {
p[i] = stream.read(); delayMicroseconds(d_us);
// TODO : check parity }
if (static_cast<uint8_t>(cmd) != t.header.cmd) { if (i2c_master_read_from(addr, reinterpret_cast<uint8_t*>(&r), sizeof(TR), true) != sizeof(TR) || r.header.cmd != rt)
{
protocolError(); protocolError();
return false; return false;
} }
return true; return true;
} }
inline Command peekCommand(Stream& stream)
{
if (!stream.available())
return CMD_NONE;
uint8_t b = stream.peek();
CommandHeader* h = reinterpret_cast<CommandHeader*>(&b);
if (h->magic != MAGIC)
return CMD_INVALID;
return static_cast<Command>(h->cmd);
}
}// namespace wifio }// namespace wifio

View File

@ -24,41 +24,34 @@
namespace wifio { namespace wifio {
const int WIFIO_AVR_SLAVE_ADDR=0x23;
void pinMode(int avrPin, int mode) { void pinMode(int avrPin, int mode) {
PinModeCommand cmd { MAGIC, CMD_PINMODE, static_cast<uint8_t>(avrPin), static_cast<uint8_t>(mode) }; PinModeCommand cmd { MAGIC, CMD_PINMODE, static_cast<uint8_t>(avrPin), static_cast<uint8_t>(mode) };
sendCommand(Serial, cmd); sendCommand(WIFIO_AVR_SLAVE_ADDR, cmd);
} }
void digitalWrite(int avrPin, int value) { void digitalWrite(int avrPin, int value) {
DigitalWriteCommand cmd { MAGIC, CMD_DIGITALWRITE, static_cast<uint8_t>(avrPin), static_cast<uint8_t>(value) }; DigitalWriteCommand cmd { MAGIC, CMD_DIGITALWRITE, static_cast<uint8_t>(avrPin), static_cast<uint8_t>(value) };
sendCommand(Serial, cmd); sendCommand(WIFIO_AVR_SLAVE_ADDR, cmd);
} }
int digitalRead(int avrPin) { int digitalRead(int avrPin) {
DigitalReadCommand cmd { MAGIC, CMD_DIGITALREAD, static_cast<uint8_t>(avrPin) }; DigitalReadCommand cmd { MAGIC, CMD_DIGITALREAD, static_cast<uint8_t>(avrPin) };
sendCommand(Serial, cmd); DigitalReadResultCommand reply = { 0 };
DigitalReadResultCommand reply; sendCommandWaitForReply(WIFIO_AVR_SLAVE_ADDR, cmd, CMD_DIGITALREADRESULT, reply, 0);
if (!expectCommand(Serial, CMD_DIGITALREADRESULT, reply) || reply.pin != avrPin) {
protocolError();
return 0;
}
return reply.value; return reply.value;
} }
void analogWrite(int avrPin, int value) { void analogWrite(int avrPin, int value) {
AnalogWriteCommand cmd { MAGIC, CMD_ANALOGWRITE, static_cast<uint8_t>(value), static_cast<uint8_t>(avrPin) }; AnalogWriteCommand cmd { MAGIC, CMD_ANALOGWRITE, static_cast<uint8_t>(value), static_cast<uint8_t>(avrPin) };
sendCommand(Serial, cmd); sendCommand(WIFIO_AVR_SLAVE_ADDR, cmd);
} }
int analogRead(int avrPin) { int analogRead(int avrPin) {
AnalogReadCommand cmd { MAGIC, CMD_ANALOGREAD, static_cast<uint8_t>(avrPin) }; AnalogReadCommand cmd { MAGIC, CMD_ANALOGREAD, static_cast<uint8_t>(avrPin) };
sendCommand(Serial, cmd); AnalogReadResultCommand reply = { 0 };
AnalogReadResultCommand reply; sendCommandWaitForReply(WIFIO_AVR_SLAVE_ADDR, cmd, CMD_ANALOGREADRESULT, reply, 120);
if (!expectCommand(Serial, CMD_ANALOGREADRESULT, reply) || reply.pin != avrPin) {
protocolError();
return 0;
}
return ((reply.val_h << 7) | reply.val_l); return ((reply.val_h << 7) | reply.val_l);
} }
@ -119,7 +112,8 @@ extern "C" void analogWrite(uint8_t pin, int value) {
} }
} }
void initVariant() void initVariant()
{ {
i2c_init(SDA, SCL);
} }