microbit-dal/source/MicroBitI2C.cpp

77 lines
2.1 KiB
C++
Raw Normal View History

#include "mbed.h"
microbit: Added configurable concurrency modes for MicroBitMessageBus handlers. MessageBus handlers can now have one of four concurrency modes for the eventuality of an event being raised whilst a previous event is still being processed. An additional (optional) parameter is provided to the listen() functions to allow this to be selected on a per event handler basis. The permissable options are: MESSAGE_BUS_LISTENER_REENTRANT: The event handler is fired with the new event, regardless of whether or not a previous event is still be processed by that handler. MESSAGE_BUS_LISTENER_QUEUE_IF_BUSY: The new event is queued until such a time as the previous event has completed execution. The new event is then processed. This option does not preclude the processing of the new event by other event handlers. MESSAGE_BUS_LISTENER_DROP_IF_BUSY: The new event is dropped, and will never be processed the the event handler. This option does not preclude the processing of the new event by other event handlers. MESSAGE_BUS_LISTENER_NONBLOCKING: The event handler is self-declaring that it never blocks. This flag is used purely for optimisation, as it permits direct execution of the event hadnelr without inducing any overhead from the scheduler. In addition, the following minor revisions were made in this release: * Cleanup of the #include dependencies contained in the microbit-dal .h files * Bugfix to the scheduler block on event matching code. * Introduced a MICROBIT_ID_ALERT MessageBus channel, for general purpose eventing using nonces.
2015-09-11 15:39:38 +00:00
#include "MicroBit.h"
#include "twi_master.h"
#include "nrf_delay.h"
/**
* Constructor.
* Create an instance of i2c
* @param sda the Pin to be used for SDA
* @param scl the Pin to be used for SCL
* Example:
* @code
* MicroBitI2C i2c(MICROBIT_PIN_SDA, MICROBIT_PIN_SCL);
* @endcode
* @note this should prevent i2c lockups as well.
*/
MicroBitI2C::MicroBitI2C(PinName sda, PinName scl) : I2C(sda,scl)
{
this->retries = 0;
}
int MicroBitI2C::read(int address, char *data, int length, bool repeated)
{
int result = I2C::read(address,data,length,repeated);
//0 indicates a success, presume failure
while(result != 0 && retries < MICROBIT_I2C_MAX_RETRIES)
{
_i2c.i2c->EVENTS_ERROR = 0;
_i2c.i2c->ENABLE = TWI_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos;
_i2c.i2c->POWER = 0;
nrf_delay_us(5);
_i2c.i2c->POWER = 1;
_i2c.i2c->ENABLE = TWI_ENABLE_ENABLE_Enabled << TWI_ENABLE_ENABLE_Pos;
twi_master_init_and_clear();
result = I2C::read(address,data,length,repeated);
retries++;
}
if(retries == MICROBIT_I2C_MAX_RETRIES - 1)
uBit.panic(MICROBIT_I2C_LOCKUP);
retries = 0;
return result;
}
int MicroBitI2C::write(int address, const char *data, int length, bool repeated)
{
int result = I2C::write(address,data,length,repeated);
//0 indicates a success, presume failure
while(result != 0 && retries < MICROBIT_I2C_MAX_RETRIES)
{
_i2c.i2c->EVENTS_ERROR = 0;
_i2c.i2c->ENABLE = TWI_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos;
_i2c.i2c->POWER = 0;
nrf_delay_us(5);
_i2c.i2c->POWER = 1;
_i2c.i2c->ENABLE = TWI_ENABLE_ENABLE_Enabled << TWI_ENABLE_ENABLE_Pos;
twi_master_init_and_clear();
result = I2C::write(address,data,length,repeated);
retries++;
}
if(retries == MICROBIT_I2C_MAX_RETRIES - 1)
uBit.panic(MICROBIT_I2C_LOCKUP);
retries = 0;
return result;
}