You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

79 lines
1.4 KiB

#include "stm32f4xx.h"
#include "eeprom.h"
#include "iic.h"
static struct iic_descr *_i2c;
static uint8_t _address;
static int _is_16bits = 0;
#define MEM_DEVICE_WRITE_ADDR (_address << 1)
#define MEM_DEVICE_READ_ADDR ((_address << 1) | 0x1)
int at24_iic_init(struct iic_descr *i2c, uint8_t addr, int bits)
{
_i2c = i2c;
_address = addr;
_is_16bits = bits;
iic_ack(_i2c, 0);
return 0;
}
void at24_iic_close(void)
{
if (_i2c) {
_i2c = 0;
_address = 0;
}
}
int at24_iic_write(uint16_t addr, uint8_t data)
{
if (_i2c == 0)
return -1;
iic_start(_i2c);
iic_send_slave_address(_i2c, MEM_DEVICE_WRITE_ADDR);
if (_is_16bits) {
/* two bytes address */
iic_send_byte(_i2c, (addr >> 8) & 0xff);
}
iic_send_byte(_i2c, addr & 0xff);
/* one byte data */
iic_send_byte(_i2c, data);
iic_stop(_i2c);
return 0;
}
uint8_t at24_iic_read(uint16_t addr)
{
uint8_t data = 0xff;
if (_i2c == 0) {
return data;
}
iic_start(_i2c);
iic_send_slave_address(_i2c, MEM_DEVICE_WRITE_ADDR);
if (_is_16bits) {
/* two bytes address */
iic_send_byte(_i2c, (addr >> 8) & 0xff);
}
iic_send_byte(_i2c, (addr & 0xff));
iic_start(_i2c);
iic_send_slave_address(_i2c, MEM_DEVICE_READ_ADDR);
data = iic_recv_byte(_i2c);
iic_stop(_i2c);
return data;
}