19 #ifndef HARDWARE_SAM_TWI_H 20 #define HARDWARE_SAM_TWI_H 23 #include "include/twi.h" 40 pmc_enable_periph_clk(WIRE_INTERFACE_ID);
41 PIO_Configure(g_APinDescription[PIN_WIRE_SDA].pPort,
42 g_APinDescription[PIN_WIRE_SDA].ulPinType,
43 g_APinDescription[PIN_WIRE_SDA].ulPin,
44 g_APinDescription[PIN_WIRE_SDA].ulPinConfiguration);
45 PIO_Configure(g_APinDescription[PIN_WIRE_SCL].pPort,
46 g_APinDescription[PIN_WIRE_SCL].ulPinType,
47 g_APinDescription[PIN_WIRE_SCL].ulPin,
48 g_APinDescription[PIN_WIRE_SCL].ulPinConfiguration);
49 m_twi = WIRE_INTERFACE;
50 TWI_ConfigureMaster(
m_twi, freq, VARIANT_MCK);
94 virtual int read(uint8_t addr,
void* buf,
size_t count)
97 if (count == 0)
return (0);
107 uint8_t* bp = (uint8_t*) buf;
109 m_twi->TWI_MMR = (addr << 16) | TWI_MMR_MREAD;
110 m_twi->TWI_CR = TWI_CR_START;
112 if (count == 0)
m_twi->TWI_CR |= TWI_CR_STOP;
114 while (((
m_twi->TWI_SR & TWI_SR_RXRDY) == 0) && (--retry));
115 if (retry == 0)
return (-1);
116 *bp++ =
m_twi->TWI_RHR;
120 while (((
m_twi->TWI_SR & TWI_SR_TXCOMP) == 0) && (--retry));
121 if (retry == 0)
return (-1);
134 virtual int write(uint8_t addr, iovec_t* vp)
143 m_twi->TWI_MMR = (addr << 16);
155 for(; vp->buf != NULL; vp++) {
156 const uint8_t* bp = (
const uint8_t*) vp->buf;
157 size_t size = vp->size;
159 m_twi->TWI_THR = *bp++;
162 while ((
m_twi->TWI_SR & TWI_SR_TXRDY) == 0)
163 if (--retry == 0)
return (-1);
191 m_twi->TWI_CR = TWI_CR_STOP;
195 if (sr & TWI_SR_NACK)
return (
false);
196 if (--retry == 0)
return (
false);
197 }
while ((sr & TWI_SR_TXCOMP) == 0);
TWI(uint32_t freq=DEFAULT_FREQ)
virtual int write(uint8_t addr, iovec_t *vp)
static const uint32_t DEFAULT_FREQ
virtual int read(uint8_t addr, void *buf, size_t count)
static const uint32_t RETRY_MAX