14 #include <hal_devices/i2c_sc18s602.h>
18 namespace log = satcat5::log;
38 if (spi_mode > 3)
return false;
44 u8 flags = (u8)(spi_mode << 2);
45 return m_parent->
write(m_devaddr, 1, 0xF0, 1, &flags);
54 u8 devidx,
const u8* wrdata, u8 rwbytes,
58 if (m_busy > 0)
return false;
59 if (devidx > 3)
return false;
62 if (execute(devidx, wrdata, rwbytes, 0)) {
63 m_callback = callback;
71 u8 devidx,
const u8* wrdata, u8 wrbytes, u8 rdbytes,
77 if (m_busy > 0)
return false;
78 if (devidx > 3)
return false;
79 if (wrbytes >
sizeof(temp))
return false;
80 if (rdbytes >
sizeof(temp))
return false;
81 if (wrbytes + rdbytes >
sizeof(temp))
return false;
84 const u8* rwdata = wrdata;
86 if (wrbytes > 0) memcpy(temp, wrdata, wrbytes);
87 memset(temp + wrbytes, 0, rdbytes);
92 if (execute(devidx, rwdata, wrbytes + rdbytes, wrbytes)) {
93 m_callback = callback;
100 bool Sc18is602::execute(u8 devidx,
const u8* wrdata, u8 rwbytes, u8 skip)
107 u32 devmask = (1u << devidx);
108 if (!m_parent->
write(m_devaddr, 1, devmask, rwbytes, wrdata,
this))
113 if (skip >= rwbytes)
return true;
116 if (!m_parent->
read(m_devaddr, 0, 0, rwbytes,
this))
123 void Sc18is602::i2c_done(
124 bool noack,
const I2cAddr& devaddr,
125 u32 regaddr,
unsigned nread,
const u8* rdata)
129 log::Log(log::WARNING,
"SC18IS602",
"Unexpected callback.");
132 log::Log(log::WARNING,
"SC18IS602",
"Missing ACK from I2C address")
137 if (--m_busy > 0)
return;
138 if (m_callback && nread <= m_skip)
139 m_callback->spi_done(0, 0);
141 m_callback->spi_done(nread - m_skip, rdata + m_skip);
Callback interface for cfg::I2cGeneric events.
Polymorphic API for a generic I2C interface.
virtual bool read(const satcat5::util::I2cAddr &devaddr, u8 regbytes, u32 regaddr, u8 nread, satcat5::cfg::I2cEventListener *callback=0)=0
Add a read operation to the queue.
virtual bool write(const satcat5::util::I2cAddr &devaddr, u8 regbytes, u32 regaddr, u8 nwrite, const u8 *data, satcat5::cfg::I2cEventListener *callback=0)=0
Add a write operation to the queue.
SPI Event Handler callback for use with SpiGeneric.
Polymorphic API for a generic SPI interface.
Device driver for the NXP SC18IS602B I2C-to-SPI bridge.
bool exchange(u8 devidx, const u8 *wrdata, u8 rwbytes, satcat5::cfg::SpiEventListener *callback=0) override
Queue a read-and-write SPI bus transaction.
bool configure(unsigned spi_mode)
Configure the SPI mode (0/1/2/3 sets CPOL, CPHA)
bool query(u8 devidx, const u8 *wrdata, u8 wrbytes, u8 rdbytes, satcat5::cfg::SpiEventListener *callback=0) override
Queue a write-then-read SPI bus transaction.
bool busy() override
Is the SPI controller currently busy?
The Log class creates and formats one log message.
Log & write(const char *str)
Formatting methods for various data types.
Diagnostic logging to UART and/or Ethernet ports.
Conversion function for I2C device addresses.
const u16 m_addr
Native internal address representation.