18 #include "inc/hw_i2c.h"
19 #include "inc/hw_memmap.h"
20 #include "inc/hw_types.h"
22 #include "driverlib/gpio.h"
23 #include "driverlib/i2c.h"
24 #include "driverlib/pin_map.h"
25 #include "driverlib/sysctl.h"
32 uint32_t peripheral_gpio;
33 uint32_t peripheral_i2c;
36 uint32_t pin_scl_function;
37 uint32_t pin_sda_function;
72 #define NUMBER_OF_I2C_PORTS ( sizeof(I2C_ports) / sizeof(I2C_port_t) )
74 #define check_i2c_num(x, y) if(x >= NUMBER_OF_I2C_PORTS) { return y; }
78 void I2C_init(uint8_t i2c_num)
80 check_i2c_num(i2c_num,);
87 if(!SysCtlPeripheralReady(i2c->peripheral_i2c))
89 SysCtlPeripheralReset(i2c->peripheral_gpio);
90 SysCtlPeripheralEnable(i2c->peripheral_i2c);
91 while(!SysCtlPeripheralReady(i2c->peripheral_i2c)) { };
95 if(!SysCtlPeripheralReady(i2c->peripheral_gpio))
97 SysCtlPeripheralReset(i2c->peripheral_gpio);
98 SysCtlPeripheralEnable(i2c->peripheral_gpio);
99 while(!SysCtlPeripheralReady(i2c->peripheral_gpio)) { };
103 GPIOPinConfigure(i2c->pin_sda_function);
104 GPIOPinConfigure(i2c->pin_scl_function);
107 GPIOPinTypeI2C(i2c->base_gpio, i2c->pin_sda);
108 GPIOPinTypeI2CSCL(i2c->base_gpio, i2c->pin_scl);
111 I2CMasterInitExpClk(i2c->base_i2c, SysCtlClockGet(), i2c->full_speed);
113 I2CMasterEnable(i2c->base_i2c);
116 I2CRxFIFOFlush(i2c->base_i2c);
117 I2CTxFIFOFlush(i2c->base_i2c);
119 i2c->initialised =
true;
127 #define I2CRECVMAXBUFFERLEN 4
129 static uint8_t I2CReceiveBuffer[I2CRECVMAXBUFFERLEN+1];
135 void I2CSendString(uint8_t i2c_num, uint8_t slave_addr, uint8_t array[])
137 check_i2c_num(i2c_num,);
142 I2CMasterSlaveAddrSet(i2c->base_i2c, slave_addr,
false);
143 I2CMasterDataPut(i2c->base_i2c, array[0]);
147 I2CMasterControl(i2c->base_i2c, I2C_MASTER_CMD_SINGLE_SEND);
148 while(I2CMasterBusy(i2c->base_i2c));
153 I2CMasterControl(i2c->base_i2c, I2C_MASTER_CMD_BURST_SEND_START);
154 while(I2CMasterBusy(i2c->base_i2c));
156 while(array[i + 1] != 0)
158 I2CMasterDataPut(i2c->base_i2c, array[i++]);
159 I2CMasterControl(i2c->base_i2c, I2C_MASTER_CMD_BURST_SEND_CONT);
160 while(I2CMasterBusy(i2c->base_i2c));
162 I2CMasterDataPut(i2c->base_i2c, array[i]);
163 I2CMasterControl(i2c->base_i2c, I2C_MASTER_CMD_BURST_SEND_FINISH);
164 while(I2CMasterBusy(i2c->base_i2c));
166 if (I2CMasterErr(i2c->base_i2c))
167 I2CMasterControl(i2c->base_i2c,I2C_MASTER_CMD_BURST_SEND_ERROR_STOP);
173 uint8_t *I2CReceiveGP(uint8_t i2c_num, uint8_t slave_addr, uint8_t reg,uint8_t strlen,
bool reverse)
175 check_i2c_num(i2c_num,NULL);
182 if (strlen>I2CRECVMAXBUFFERLEN || strlen<=0) {
return NULL;}
184 I2CMasterSlaveAddrSet(i2c->base_i2c, slave_addr,
false);
185 I2CMasterDataPut(i2c->base_i2c, reg);
186 I2CMasterControl(i2c->base_i2c, I2C_MASTER_CMD_SINGLE_SEND);
188 while(I2CMasterBusy(i2c->base_i2c));
190 I2CMasterSlaveAddrSet(i2c->base_i2c, slave_addr,
true);
193 I2CMasterControl(i2c->base_i2c, I2C_MASTER_CMD_SINGLE_RECEIVE);
194 while(I2CMasterBusy(i2c->base_i2c));
195 I2CReceiveBuffer[0]=(uint8_t) I2CMasterDataGet(i2c->base_i2c);
196 I2CReceiveBuffer[1]=0;
197 return I2CReceiveBuffer;
201 I2CMasterControl(i2c->base_i2c, I2C_MASTER_CMD_BURST_RECEIVE_START);
202 while(I2CMasterBusy(i2c->base_i2c));
203 I2CReceiveBuffer[0]=(uint8_t) I2CMasterDataGet(i2c->base_i2c);
207 while(I2CMasterBusy(i2c->base_i2c));
208 I2CMasterControl(i2c->base_i2c, I2C_MASTER_CMD_BURST_RECEIVE_CONT);
209 if (I2CMasterErr(i2c->base_i2c))
210 {I2CMasterControl(i2c->base_i2c,I2C_MASTER_CMD_BURST_RECEIVE_ERROR_STOP);
return NULL;}
211 I2CReceiveBuffer[i]=(uint8_t) I2CMasterDataGet(i2c->base_i2c);
215 I2CMasterControl(i2c->base_i2c, I2C_MASTER_CMD_BURST_RECEIVE_FINISH);
216 while(I2CMasterBusy(i2c->base_i2c));
217 I2CReceiveBuffer[strlen-1]=(uint8_t) I2CMasterDataGet(i2c->base_i2c);
218 I2CReceiveBuffer[strlen]=0;
220 if (I2CMasterErr(i2c->base_i2c))
221 {I2CMasterControl(i2c->base_i2c,I2C_MASTER_CMD_BURST_RECEIVE_ERROR_STOP);
return NULL;}
225 for (uint8_t i=0;i<(strlen/2);i++)
226 {uint8_t b=I2CReceiveBuffer[i];I2CReceiveBuffer[i]=I2CReceiveBuffer[strlen-i-1];I2CReceiveBuffer[strlen-i-1]=b;}
227 return I2CReceiveBuffer;
230 uint8_t I2CReceive(uint8_t i2c_num, uint8_t slave_addr, uint8_t reg)
232 check_i2c_num(i2c_num,0);
235 uint8_t *ans=I2CReceiveGP(i2c_num, slave_addr,reg,1,
false);
237 if (ans==NULL)
return 0;
241 uint16_t I2CReceive16(uint8_t i2c_num, uint8_t slave_addr,uint8_t reg)
243 check_i2c_num(i2c_num,0);
246 uint8_t *ans=I2CReceiveGP(i2c_num, slave_addr,reg,2,
false);
247 if (ans==NULL)
return 0;
249 return *(uint16_t *)ans;
252 uint16_t I2CReceive16r(uint8_t i2c_num, uint8_t slave_addr,uint8_t reg)
254 check_i2c_num(i2c_num,0);
257 uint8_t *ans=I2CReceiveGP(i2c_num, slave_addr,reg,2,
true);
258 if (ans==NULL)
return 0;
259 return *(uint16_t *)ans;