Extension of original I2CSlave library to support multiple slave addresses and interrupts.
I2CSlaveX.cpp@1:b7e586fffbf2, 2014-07-12 (annotated)
- Committer:
- jaerts
- Date:
- Sat Jul 12 15:26:34 2014 +0000
- Revision:
- 1:b7e586fffbf2
- Parent:
- 0:f94c714b3b4d
Update example code
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jaerts | 0:f94c714b3b4d | 1 | #include "I2CSlaveX.h" |
jaerts | 0:f94c714b3b4d | 2 | |
jaerts | 0:f94c714b3b4d | 3 | #if DEVICE_I2CSLAVE |
jaerts | 0:f94c714b3b4d | 4 | |
jaerts | 0:f94c714b3b4d | 5 | namespace mbed { |
jaerts | 0:f94c714b3b4d | 6 | |
jaerts | 0:f94c714b3b4d | 7 | I2CSlaveX* instance; |
jaerts | 0:f94c714b3b4d | 8 | |
jaerts | 0:f94c714b3b4d | 9 | I2CSlaveX::I2CSlaveX(PinName sda, PinName scl) : I2CSlave(sda, scl) { |
jaerts | 0:f94c714b3b4d | 10 | |
jaerts | 0:f94c714b3b4d | 11 | } |
jaerts | 0:f94c714b3b4d | 12 | |
jaerts | 0:f94c714b3b4d | 13 | // Add optional index argument for slaves that accept multiple addresses |
jaerts | 0:f94c714b3b4d | 14 | void I2CSlaveX::address(int address, int idx) { |
jaerts | 0:f94c714b3b4d | 15 | int addr = (address & 0xFF) | 1; |
jaerts | 0:f94c714b3b4d | 16 | i2c_slave_address(&_i2c, idx, addr, 0); |
jaerts | 0:f94c714b3b4d | 17 | } |
jaerts | 0:f94c714b3b4d | 18 | |
jaerts | 0:f94c714b3b4d | 19 | // Add optional argument that will be set with the received data value (usually slave address) |
jaerts | 0:f94c714b3b4d | 20 | int I2CSlaveX::receive(char *data) { |
jaerts | 0:f94c714b3b4d | 21 | if(data) { |
jaerts | 0:f94c714b3b4d | 22 | *data = _i2c.i2c->DAT; |
jaerts | 0:f94c714b3b4d | 23 | } |
jaerts | 0:f94c714b3b4d | 24 | return i2c_slave_receive(&_i2c); |
jaerts | 0:f94c714b3b4d | 25 | } |
jaerts | 0:f94c714b3b4d | 26 | |
jaerts | 0:f94c714b3b4d | 27 | // Return read length rather than match/no match |
jaerts | 0:f94c714b3b4d | 28 | int I2CSlaveX::read(char *data, int length) { |
jaerts | 0:f94c714b3b4d | 29 | return i2c_slave_read(&_i2c, data, length); |
jaerts | 0:f94c714b3b4d | 30 | } |
jaerts | 0:f94c714b3b4d | 31 | |
jaerts | 0:f94c714b3b4d | 32 | // Return write length rather than match/no match |
jaerts | 0:f94c714b3b4d | 33 | int I2CSlaveX::write(const char *data, int length) { |
jaerts | 0:f94c714b3b4d | 34 | return i2c_slave_write(&_i2c, data, length); |
jaerts | 0:f94c714b3b4d | 35 | } |
jaerts | 0:f94c714b3b4d | 36 | |
jaerts | 0:f94c714b3b4d | 37 | void I2CSlaveX::attach(void (*fptr)(void)) { |
jaerts | 0:f94c714b3b4d | 38 | if (fptr) { |
jaerts | 0:f94c714b3b4d | 39 | _receive.attach(fptr); |
jaerts | 0:f94c714b3b4d | 40 | enable_irq(); |
jaerts | 0:f94c714b3b4d | 41 | } else { |
jaerts | 0:f94c714b3b4d | 42 | disable_irq(); |
jaerts | 0:f94c714b3b4d | 43 | } |
jaerts | 0:f94c714b3b4d | 44 | } |
jaerts | 0:f94c714b3b4d | 45 | |
jaerts | 0:f94c714b3b4d | 46 | void I2CSlaveX::_irq_handler(uint32_t id, uint8_t addr, uint8_t state) { |
jaerts | 0:f94c714b3b4d | 47 | instance->_receive.call(); |
jaerts | 0:f94c714b3b4d | 48 | } |
jaerts | 0:f94c714b3b4d | 49 | |
jaerts | 0:f94c714b3b4d | 50 | void I2CSlaveX::enable_irq() { |
jaerts | 0:f94c714b3b4d | 51 | // Hacky way to call this instance |
jaerts | 0:f94c714b3b4d | 52 | instance = this; |
jaerts | 0:f94c714b3b4d | 53 | NVIC_SetVector(I2C_IRQn, (uint32_t)(&I2CSlaveX::_irq_handler)); |
jaerts | 0:f94c714b3b4d | 54 | NVIC_EnableIRQ(I2C_IRQn); |
jaerts | 0:f94c714b3b4d | 55 | } |
jaerts | 0:f94c714b3b4d | 56 | |
jaerts | 0:f94c714b3b4d | 57 | void I2CSlaveX::disable_irq() { |
jaerts | 0:f94c714b3b4d | 58 | NVIC_DisableIRQ(I2C_IRQn); |
jaerts | 0:f94c714b3b4d | 59 | } |
jaerts | 0:f94c714b3b4d | 60 | |
jaerts | 0:f94c714b3b4d | 61 | } |
jaerts | 0:f94c714b3b4d | 62 | |
jaerts | 0:f94c714b3b4d | 63 | #endif |