|
1 |
| -# flipperzero-i2ctools |
| 1 | +# flipperzero-i2ctools |
| 2 | + |
| 3 | +```c |
| 4 | +typedef struct |
| 5 | +{ |
| 6 | + unsigned int PIN0:1; |
| 7 | + unsigned int PIN1:1; |
| 8 | + unsigned int PIN2:1; |
| 9 | + unsigned int PIN3:1; |
| 10 | + unsigned int PIN4:1; |
| 11 | + unsigned int PIN5:1; |
| 12 | + unsigned int PIN6:1; |
| 13 | + unsigned int PIN7:1; |
| 14 | +} PORT; |
| 15 | + |
| 16 | +/* TODO: Example address shown, but the proper address */ |
| 17 | +#define PORT0 *(volatile PORT *)0x1234 |
| 18 | + |
| 19 | +/* Define the port used for I2C data and clk as shown above to access them pin wise */ |
| 20 | +#define I2C_DATA PORT0.PIN0 |
| 21 | +#define I2C_CLK PORT0.PIN1 |
| 22 | + |
| 23 | +#define HIGH 1 |
| 24 | +#define LOW 0 |
| 25 | + |
| 26 | +/* I2C Start - bit bang */ |
| 27 | +void I2C_START(void) |
| 28 | +{ |
| 29 | + /* I2C Start condition, data line goes low when clock is high */ |
| 30 | + I2C_DATA = HIGH; |
| 31 | + I2C_CLK = HIGH; |
| 32 | + I2C_DATA = LOW; |
| 33 | + I2C_CLK = LOW; |
| 34 | +} |
| 35 | + |
| 36 | +/* I2C Stop - bit bang */ |
| 37 | +void I2C_STOP (void) |
| 38 | +{ |
| 39 | + /* I2C Stop condition, clock goes high when data is low */ |
| 40 | + I2C_CLK = LOW; |
| 41 | + I2C_DATA = LOW; |
| 42 | + I2C_CLK = HIGH; |
| 43 | + I2C_DATA = HIGH; |
| 44 | +} |
| 45 | + |
| 46 | +/* I2C Write - bit bang */ |
| 47 | +void I2C_WRITE(unsigned char data) |
| 48 | +{ |
| 49 | + unsigned char outBits; |
| 50 | + unsigned char inBit; |
| 51 | + |
| 52 | + /* 8 bits */ |
| 53 | + for(outBits = 0; outBits < 8; outBits++) |
| 54 | + { |
| 55 | + if(data & 0x80) |
| 56 | + I2C_DATA = 1; |
| 57 | + else |
| 58 | + I2C_DATA = 0; |
| 59 | + data <<= 1; |
| 60 | + /* Generate clock for 8 data bits */ |
| 61 | + SCLK = HIGH; |
| 62 | + SCLK = LOW; |
| 63 | + } |
| 64 | + |
| 65 | + /* Generate clock for ACK */ |
| 66 | + I2C_CLK = HIGH; |
| 67 | + /* Wait for clock to go high, clock stretching */ |
| 68 | + while(I2C_CLK); |
| 69 | + /* Clock high, valid ACK */ |
| 70 | + inBit = I2C_DATA; |
| 71 | + I2C_CLK = LOW; |
| 72 | +} |
| 73 | + |
| 74 | +unsigned char I2C_READ (void) |
| 75 | +{ |
| 76 | + unsigned char inData, inBits; |
| 77 | + |
| 78 | + inData = 0x00; |
| 79 | + /* 8 bits */ |
| 80 | + for(inBits = 0; inBits < 8; inBits++) |
| 81 | + { |
| 82 | + inData <<= 1; |
| 83 | + I2C_CLK = HIGH; |
| 84 | + inData |= I2C_DATA; |
| 85 | + I2C_CLK = LOW; |
| 86 | + } |
| 87 | + |
| 88 | + return inData; |
| 89 | +} |
| 90 | + |
| 91 | +/* Examble for writing to I2C Slave */ |
| 92 | +void writeI2CSlave (unsigned char data) |
| 93 | +{ |
| 94 | + /* Start */ |
| 95 | + I2C_START(); |
| 96 | + /* Slave address */ |
| 97 | + I2C_WRITE(0xAA) |
| 98 | + /* Slave control byte */ |
| 99 | + I2C_WRITE(0xBB); |
| 100 | + /* Slave data */ |
| 101 | + I2C_WRITE(data); |
| 102 | + /* Stop */ |
| 103 | + I2C_STOP(); |
| 104 | +} |
| 105 | + |
| 106 | +/* Examble for reading from I2C Slave */ |
| 107 | +unsigned char readI2CSlave(unsigned char data) |
| 108 | +{ |
| 109 | + unsigned char inData; |
| 110 | + |
| 111 | + /* Start */ |
| 112 | + I2C_START(); |
| 113 | + /* Slave address */ |
| 114 | + I2C_WRITE(0xAA); |
| 115 | + /* Slave control byte */ |
| 116 | + I2C_WRITE(data); |
| 117 | + /* Stop */ |
| 118 | + I2C_STOP(); |
| 119 | + |
| 120 | + /* Start */ |
| 121 | + I2C_START(); |
| 122 | + /* Slave address + read */ |
| 123 | + I2C_WRITE(0xAA | 1); |
| 124 | + /* Read */ |
| 125 | + inData = I2C_READ(); |
| 126 | + |
| 127 | + return inData; |
| 128 | +} |
| 129 | +``` |
0 commit comments