i2c slave class to RaspberryPi with interrupt
Fork of i2cslave by
Diff: i2cslave.cpp
- Revision:
- 2:0dff052266ca
- Parent:
- 1:cd92200b8a50
- Child:
- 3:5d19d3a231b3
diff -r cd92200b8a50 -r 0dff052266ca i2cslave.cpp --- a/i2cslave.cpp Thu Jul 12 08:09:43 2018 +0000 +++ b/i2cslave.cpp Thu Jul 12 10:17:19 2018 +0000 @@ -1,12 +1,36 @@ #include "i2cslave.h" +/*example************************************************* + +#include "mbed.h" +#include "i2cslave.h" + +int32_t Registar[0x20]; +i2cslave i2c(p28, p27, Registar); + +int main() +{ + i2c.address(0xa0); + Registar[10] = 0; + while(1) + { + printf("%d\r\n", Registar[0]); + Registar[1] = 1234; + wait_ms(500); + Registar[1] = 5678; + wait_ms(500); + } +} + +**********************************************************/ + #if DEVICE_I2CSLAVE namespace mbed { i2cslave* instance; -i2cslave::i2cslave(PinName sda, PinName scl, char *_Registar) : I2CSlave(sda, scl) +i2cslave::i2cslave(PinName sda, PinName scl, int32_t *_Registar) : I2CSlave(sda, scl) { frequency(400000); i2cslave::attach(this, &i2cslave::getdata); @@ -22,40 +46,31 @@ char reg=DATA[0]; char num =DATA[1]; char X[num]; - char f=0; + for(int i = 0; i < num; i++)X[i] = 0; + int32_t tmp = 0; wait_us(50); switch(I2CSlave::receive()) { case 0 :break; case I2CSlave::ReadAddressed: { - char *po = Registar+reg; - for(int i=0;i<num;i++) - X[i]=*po+i; + int32_t tmp = *(Registar+reg); + for(int i = 0; i < num; i++) + { + X[i] = (tmp >> i*8) & 0xFF; + } I2CSlave::write(X,num); - f=1; - do - { - - //f = I2CSlave::write(Registar[reg]); - f = I2CSlave::write(*(Registar+reg)); - reg++; - }while(f==1); break; } - case I2CSlave::WriteGeneral:{break;} + case I2CSlave::WriteGeneral:break; case I2CSlave::WriteAddressed: { - char num = DATA[1]; - for(int i=1; i<num; i++,reg++) - char X[num]; - I2CSlave::read(X,num); - for (int i=0;i<num;i++) - { - //Registar[reg]=X[i]; - *(Registar+reg) = X[i]; - reg++; - } + I2CSlave::read(X, num); + for (int i=0; i < num; i++) + { + tmp |= X[i] << (i*8); + } + *(Registar+reg) = tmp; break; } }