Exemplo n.º 1
0
void
ds1307Write(uint8_t addr, uint8_t data)
{
  uint8_t ui3;

  I2C_START_BIT;

  do {
    I2C_SEND_BYTE(TIMER_CTRL_WRITE);
    I2C_GET_ACK(CY);
  } while (NACK == CY);

  do {
    I2C_SEND_BYTE(addr);
    I2C_GET_ACK(CY);
  } while (NACK == CY);

  do {
    I2C_SEND_BYTE(data);
    I2C_GET_ACK(CY);
  } while (NACK == CY);

  CY=0;
  I2C_STOP_BIT;
}
Exemplo n.º 2
0
void
eepromWriteBytes(uint16_t addr, uint8_t *val, uint8_t n_bytes)
{
  uint8_t ui2, ui3;

  DISABLE_ALL_INTERRUPTS;

  for (; n_bytes>0; n_bytes--, val++, addr++) {
    I2C_START_BIT;
    I2C_SEND_BYTE(EEPROM_CTRL_WRITE);   
    I2C_GET_ACK(CY);

#if EP24C > 2
    ui2 = addr>>8;
    I2C_SEND_BYTE(ui2);
    I2C_GET_ACK(CY);
#endif

    ui2 = (uint8_t)addr;
    I2C_SEND_BYTE(ui2);
    I2C_GET_ACK(CY);

    I2C_SEND_BYTE(val[0]);
    I2C_GET_ACK(CY);
    I2C_STOP_BIT;

    delayms(10);
  }

  ENABLE_ALL_INTERRUPTS;
}
Exemplo n.º 3
0
bool I2C_writeRegBuf(I2cDevice devId, u8 regAddr, const u8 *in, u32 size)
{
	const u8 busId = i2cDevTable[devId].busId;
	I2cRegs *const regs = i2cGetBusRegsBase(busId);


	if(!i2cStartTransfer(devId, regAddr, false, regs)) return false;

	while(--size)
	{
		regs->REG_I2C_DATA = *in++;
		regs->REG_I2C_CNT = I2C_ENABLE | I2C_IRQ_ENABLE | I2C_DIRE_WRITE;
		i2cWaitBusy(regs);
		if(!I2C_GET_ACK(regs->REG_I2C_CNT)) // If ack flag is 0 it failed.
		{
			regs->REG_I2C_CNT = I2C_ENABLE | I2C_IRQ_ENABLE | I2C_ERROR | I2C_STOP;
			return false;
		}
	}

	regs->REG_I2C_DATA = *in;
	regs->REG_I2C_CNT = I2C_ENABLE | I2C_IRQ_ENABLE | I2C_DIRE_WRITE | I2C_STOP;
	i2cWaitBusy(regs);
	if(!I2C_GET_ACK(regs->REG_I2C_CNT)) // If ack flag is 0 it failed.
	{
		regs->REG_I2C_CNT = I2C_ENABLE | I2C_IRQ_ENABLE | I2C_ERROR | I2C_STOP;
		return false;
	}

	return true;
}
Exemplo n.º 4
0
uint8_t
ds1307Read(uint8_t addr)
{
  uint8_t ui3, data;

  I2C_START_BIT;

  do {
    I2C_SEND_BYTE(TIMER_CTRL_WRITE);
    I2C_GET_ACK(CY);
  } while (NACK == CY);

  do {
    I2C_SEND_BYTE(addr);
    I2C_GET_ACK(CY);
  } while (NACK == CY);

  I2C_START_BIT;

  do {
    I2C_SEND_BYTE(TIMER_CTRL_READ);
    I2C_GET_ACK(CY);
  } while (NACK == CY);

  I2C_GET_BYTE(data);
  I2C_SEND_ACK(1);
  CY=0;
  I2C_STOP_BIT;

  return(data);
}
Exemplo n.º 5
0
static bool i2cStartTransfer(I2cDevice devId, u8 regAddr, bool read, I2cRegs *const regs)
{
	const u8 devAddr = i2cDevTable[devId].devAddr;


	u32 i = 0;
	for(; i < 8; i++)
	{
		i2cWaitBusy(regs);

		// Select device and start.
		regs->REG_I2C_DATA = devAddr;
		regs->REG_I2C_CNT = I2C_ENABLE | I2C_IRQ_ENABLE | I2C_START;
		i2cWaitBusy(regs);
		if(!I2C_GET_ACK(regs->REG_I2C_CNT)) // If ack flag is 0 it failed.
		{
			regs->REG_I2C_CNT = I2C_ENABLE | I2C_IRQ_ENABLE | I2C_ERROR | I2C_STOP;
			continue;
		}

		// Select register and change direction to write.
		regs->REG_I2C_DATA = regAddr;
		regs->REG_I2C_CNT = I2C_ENABLE | I2C_IRQ_ENABLE | I2C_DIRE_WRITE;
		i2cWaitBusy(regs);
		if(!I2C_GET_ACK(regs->REG_I2C_CNT)) // If ack flag is 0 it failed.
		{
			regs->REG_I2C_CNT = I2C_ENABLE | I2C_IRQ_ENABLE | I2C_ERROR | I2C_STOP;
			continue;
		}

		// Select device in read mode for read transfer.
		if(read)
		{
			regs->REG_I2C_DATA = devAddr | 1u; // Set bit 0 for read.
			regs->REG_I2C_CNT = I2C_ENABLE | I2C_IRQ_ENABLE | I2C_START;
			i2cWaitBusy(regs);
			if(!I2C_GET_ACK(regs->REG_I2C_CNT)) // If ack flag is 0 it failed.
			{
				regs->REG_I2C_CNT = I2C_ENABLE | I2C_IRQ_ENABLE | I2C_ERROR | I2C_STOP;
				continue;
			}
		}

		break;
	}

	if(i < 8) return true;
	else return false;
}