1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-06-12 01:53:07 +03:00

I2C slave support (originally by bjoham) (#5226)

* I2C slave support; resolving conflicts against current master

* removed unused argument, updateded to hopefully pass Travis

* cleaning up commit as requested by https://github.com/esp8266/Arduino/pull/5162#pullrequestreview-162242359

* cleaning up commit as requested by https://github.com/esp8266/Arduino/pull/5162#pullrequestreview-162242359

* type fix
This commit is contained in:
Matej Sychra
2018-10-26 17:04:16 +02:00
committed by Develo
parent 42c977bd4d
commit cb05b86d49
4 changed files with 894 additions and 90 deletions

View File

@ -19,6 +19,7 @@
Modified 2012 by Todd Krein (todd@krein.org) to implement repeated starts
Modified December 2014 by Ivan Grokhotkov (ivan@esp8266.com) - esp8266 support
Modified April 2015 by Hrsto Gochkov (ficeto@ficeto.com) - alternative esp8266 support
Modified January 2017 by Bjorn Hammarberg (bjoham@esp8266.com) - i2c slave support
*/
extern "C" {
@ -78,10 +79,9 @@ void TwoWire::begin(void){
}
void TwoWire::begin(uint8_t address){
(void)address;
// twi_setAddress(address);
// twi_attachSlaveTxEvent(onRequestService);
// twi_attachSlaveRxEvent(onReceiveService);
twi_setAddress(address);
twi_attachSlaveTxEvent(onRequestService);
twi_attachSlaveRxEvent(onReceiveService);
begin();
}
@ -160,7 +160,7 @@ size_t TwoWire::write(uint8_t data){
++txBufferIndex;
txBufferLength = txBufferIndex;
} else {
// i2c_slave_transmit(&data, 1);
twi_transmit(&data, 1);
}
return 1;
}
@ -171,7 +171,7 @@ size_t TwoWire::write(const uint8_t *data, size_t quantity){
if(!write(data[i])) return i;
}
}else{
// i2c_slave_transmit(data, quantity);
twi_transmit(data, quantity);
}
return quantity;
}
@ -214,51 +214,53 @@ void TwoWire::flush(void){
void TwoWire::onReceiveService(uint8_t* inBytes, int numBytes)
{
(void)inBytes;
(void)numBytes;
// don't bother if user hasn't registered a callback
// if(!user_onReceive){
// return;
// }
if (!user_onReceive) {
return;
}
// // don't bother if rx buffer is in use by a master requestFrom() op
// // i know this drops data, but it allows for slight stupidity
// // meaning, they may not have read all the master requestFrom() data yet
// if(rxBufferIndex < rxBufferLength){
// return;
// }
// // copy twi rx buffer into local read buffer
// // this enables new reads to happen in parallel
// for(uint8_t i = 0; i < numBytes; ++i){
// rxBuffer[i] = inBytes[i];
// }
// // set rx iterator vars
// rxBufferIndex = 0;
// rxBufferLength = numBytes;
// // alert user program
// user_onReceive(numBytes);
// copy twi rx buffer into local read buffer
// this enables new reads to happen in parallel
for (uint8_t i = 0; i < numBytes; ++i) {
rxBuffer[i] = inBytes[i];
}
// set rx iterator vars
rxBufferIndex = 0;
rxBufferLength = numBytes;
// alert user program
user_onReceive(numBytes);
}
void TwoWire::onRequestService(void){
// // don't bother if user hasn't registered a callback
// if(!user_onRequest){
// return;
// }
// // reset tx buffer iterator vars
// // !!! this will kill any pending pre-master sendTo() activity
// txBufferIndex = 0;
// txBufferLength = 0;
// // alert user program
// user_onRequest();
void TwoWire::onRequestService(void)
{
// don't bother if user hasn't registered a callback
if (!user_onRequest) {
return;
}
// reset tx buffer iterator vars
// !!! this will kill any pending pre-master sendTo() activity
txBufferIndex = 0;
txBufferLength = 0;
// alert user program
user_onRequest();
}
void TwoWire::onReceive( void (*function)(int) ){
(void)function;
//user_onReceive = function;
void TwoWire::onReceive( void (*function)(int) ) {
user_onReceive = function;
}
void TwoWire::onRequest( void (*function)(void) ){
(void)function;
//user_onRequest = function;
user_onRequest = function;
}
// Preinstantiate Objects //////////////////////////////////////////////////////