Exemplo n.º 1
1
uint32_t I2CWriteSingle(uint32_t i2c_base, uint8_t address, uint8_t reg, uint8_t data) {

	// Set slave register to be written
	while(I2CMasterBusy(i2c_base));
	I2CMasterSlaveAddrSet(i2c_base, address, 0);
	I2CMasterDataPut(i2c_base, reg);
	I2CMasterControl(i2c_base, I2C_MASTER_CMD_BURST_SEND_START);
	while(I2CMasterBusy(i2c_base));

	// Check for errors
	if (I2CMasterErr(i2c_base) != I2C_MASTER_ERR_NONE) return 0;

	// Write data
	I2CMasterDataPut(i2c_base, data);
	I2CMasterControl(i2c_base, I2C_MASTER_CMD_BURST_SEND_CONT);
	while(I2CMasterBusy(i2c_base));

	// Check for errors
	if (I2CMasterErr(i2c_base) != I2C_MASTER_ERR_NONE) return 0;

	// End transmission
	I2CMasterControl(i2c_base, I2C_MASTER_CMD_BURST_SEND_FINISH);
	while(I2CMasterBusy(i2c_base));

	// Check for errors
	if (I2CMasterErr(i2c_base) != I2C_MASTER_ERR_NONE) return 0;

	return 1;
}
Exemplo n.º 2
0
uint32_t I2CReadSingle(uint32_t i2c_base, uint8_t address, uint8_t reg) {
	uint32_t data = 0;

	// Set slave register to be read
	while(I2CMasterBusy(i2c_base));
	I2CMasterSlaveAddrSet(i2c_base, address, 0);
	I2CMasterDataPut(i2c_base, reg);
	I2CMasterControl(i2c_base, I2C_MASTER_CMD_SINGLE_SEND);
	while(I2CMasterBusy(i2c_base));

	// Check for errors
	if (I2CMasterErr(i2c_base) != I2C_MASTER_ERR_NONE) return 0;

	// Request for register data
	I2CMasterSlaveAddrSet(i2c_base, address, 1);
	I2CMasterControl(i2c_base, I2C_MASTER_CMD_SINGLE_RECEIVE);
	while(I2CMasterBusy(i2c_base));

	// Check for errors
	if (I2CMasterErr(i2c_base) != I2C_MASTER_ERR_NONE) return 0;

	// Read received data
	data = I2CMasterDataGet(i2c_base);

	return data;
}
Exemplo n.º 3
0
void I2C_Write0(uint16_t device_address, uint16_t device_register, uint8_t device_data)
{
   //specify that we want to communicate to device address with an intended write to bus
   I2CMasterSlaveAddrSet(I2C0_BASE, device_address, false);

   //register to be read
   I2CMasterDataPut(I2C0_BASE, device_register);

   //send control byte and register address byte to slave device
   I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_START);

   //wait for MCU to finish transaction
   while(I2CMasterBusy(I2C0_BASE));

   I2CMasterSlaveAddrSet(I2C0_BASE, device_address, true);

   //specify data to be written to the above mentioned device_register
   I2CMasterDataPut(I2C0_BASE, device_data);

   //wait while checking for MCU to complete the transaction
   I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_RECEIVE_FINISH);

   //wait for MCU & device to complete transaction
   while(I2CMasterBusy(I2C0_BASE));
}
Exemplo n.º 4
0
uint32_t I2C_Read0(uint16_t device_address, uint16_t device_register)
{
   //specify that we want to communicate to device address with an intended write to bus
   I2CMasterSlaveAddrSet(I2C0_BASE, device_address, false);

   //the register to be read
   I2CMasterDataPut(I2C0_BASE, device_register);

   //send control byte and register address byte to slave device
   I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_SEND);

   //wait for MCU to complete send transaction
   while(I2CMasterBusy(I2C0_BASE));

   //read from the specified slave device
   I2CMasterSlaveAddrSet(I2C0_BASE, device_address, true);

   //send control byte and read from the register from the MCU
   I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE);

   //wait while checking for MCU to complete the transaction
   while(I2CMasterBusy(I2C0_BASE));

   //Get the data from the MCU register and return to caller
   return( (uint32_t)I2CMasterDataGet(I2C0_BASE));
 }
Exemplo n.º 5
0
void i2c_rx_multi(unsigned char SlaveAddr, unsigned char dest, unsigned char num_bytes, unsigned long *data)
{
    unsigned int i=0;
    
    // Set the address
    I2CMasterSlaveAddrSet( I2C1_MASTER_BASE, SlaveAddr, I2C_SEND );
    I2CMasterDataPut( I2C1_MASTER_BASE, dest );
    I2CMasterControl( I2C1_MASTER_BASE, I2C_MASTER_CMD_SINGLE_SEND );
    while (I2CMasterBusy( I2C1_MASTER_BASE ));
    
    // Set the address again to tell the device to start sending data
    I2CMasterSlaveAddrSet( I2C1_MASTER_BASE, SlaveAddr, I2C_RECEIVE );
    
    I2CMasterControl( I2C1_MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_START );
    while(I2CMasterBusy( I2C1_MASTER_BASE ));
    *data++ = I2CMasterDataGet(I2C1_MASTER_BASE);
        
    while(i++ < (num_bytes-2))
    {
        I2CMasterControl( I2C1_MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_CONT );
        while(I2CMasterBusy( I2C1_MASTER_BASE ));
        *data++ = I2CMasterDataGet(I2C1_MASTER_BASE);
    }
    
    I2CMasterControl( I2C1_MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_FINISH );
    while(I2CMasterBusy( I2C1_MASTER_BASE ));
    *data++ = I2CMasterDataGet(I2C1_MASTER_BASE);
}
/*
*********************************************************************************************************
*                     BSP_DACReadRegister (CPU_INT08U ucRegister, CPU_INT08U *pucData)
*
* Description : Read a register in the TLV320AIC3107 DAC.
*
* Argument(s) : ucRegister is the offset to the register to write.
*               pucData is a pointer to the returned data.
*
* Return(s)   : True on success or false on error.
*
* Caller(s)   : Sound driver.
*
* Note(s)     : 
*
*********************************************************************************************************
*/
static  CPU_BOOLEAN  BSP_DACReadRegister (CPU_INT08U ucRegister, CPU_INT08U *pucData)
{
    // Set the slave address and "WRITE"/false.
    I2CMasterSlaveAddrSet(DAC_I2C_MASTER_BASE, TI_TLV320AIC3107_ADDR, false);

    // Write the first byte to the controller (register)
    I2CMasterDataPut(DAC_I2C_MASTER_BASE, ucRegister);

    // Continue the transfer.
    I2CMasterControl(DAC_I2C_MASTER_BASE, I2C_MASTER_CMD_BURST_SEND_START);

    // Wait until the current byte has been transferred.
    while(I2CMasterIntStatus(DAC_I2C_MASTER_BASE, false) == 0)
    {
    }

    if(I2CMasterErr(DAC_I2C_MASTER_BASE) != I2C_MASTER_ERR_NONE)
    {
        I2CMasterIntClear(DAC_I2C_MASTER_BASE);
        return(false);
    }

    // Wait until the current byte has been transferred.
    while(I2CMasterIntStatus(DAC_I2C_MASTER_BASE, false))
    {
        I2CMasterIntClear(DAC_I2C_MASTER_BASE);
    }


    // Set the slave address and "READ"/true.
    I2CMasterSlaveAddrSet(DAC_I2C_MASTER_BASE, TI_TLV320AIC3107_ADDR, true);

    // Read Data Byte.
    I2CMasterControl(DAC_I2C_MASTER_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE);

    // Wait until the current byte has been transferred.
    while(I2CMasterIntStatus(DAC_I2C_MASTER_BASE, false) == 0)
    {
    }

    if(I2CMasterErr(DAC_I2C_MASTER_BASE) != I2C_MASTER_ERR_NONE)
    {
        I2CMasterIntClear(DAC_I2C_MASTER_BASE);
        return(false);
    }

    // Wait until the current byte has been transferred.
    while(I2CMasterIntStatus(DAC_I2C_MASTER_BASE, false))
    {
        I2CMasterIntClear(DAC_I2C_MASTER_BASE);
    }

	*pucData  = I2CMasterDataGet(DAC_I2C_MASTER_BASE);

    return(true);
}
Exemplo n.º 7
0
unsigned long i2c_rx_single(unsigned char SlaveAddr, unsigned char dest)
{
    I2CMasterSlaveAddrSet( I2C1_MASTER_BASE, SlaveAddr, I2C_SEND );
    I2CMasterDataPut( I2C1_MASTER_BASE, dest );
    I2CMasterControl( I2C1_MASTER_BASE, I2C_MASTER_CMD_SINGLE_SEND );
    while (I2CMasterBusy( I2C1_MASTER_BASE ));
    
    I2CMasterSlaveAddrSet( I2C1_MASTER_BASE, SlaveAddr, I2C_RECEIVE );
    I2CMasterControl( I2C1_MASTER_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE );
    while(I2CMasterBusy( I2C1_MASTER_BASE ));
    
    return I2CMasterDataGet(I2C1_MASTER_BASE);
}
Exemplo n.º 8
0
//*****************************************************************************
//
// Reads a single gyro register.  This routine is blocking.
//
// \param ui8RegisterAddress is the register address
// \return read data
//
//*****************************************************************************
uint8_t
Accel_RegRead(uint8_t ui8RegisterAddress)
{
    uint8_t ui8Data = 0;

    if (VERBOSE) UARTprintf("GryroRegRead(0x%x)\n", ui8RegisterAddress);

    // Set the slave device address for WRITE
    //   false = this I2C Master is initiating a writes to the slave.
    //   true  = this I2C Master is initiating reads from the slave.
    I2CMasterSlaveAddrSet(I2C1_BASE, ACCEL_SLAVE_ADDRESS, false);

    //
    // Transaction #1: Send the register address
    //

    // Set the Gyro Register address to write
    I2CMasterDataPut(I2C1_BASE, ui8RegisterAddress);

    // Start, send device address, write one byte (register address), and end the transaction
    I2CMasterControl(I2C1_BASE, I2C_MASTER_CMD_SINGLE_SEND);

    // Wait for completion
    while(I2CMasterBusy(I2C1_BASE))
    {
    	//spin wait
    }
    //TODO: Check I2CMasterErr(I2C1_BASE)

    //
    // Transaction #2: Read the register data
    //

    // Set the slave device address for READ
    //   false = this I2C Master is initiating a writes to the slave.
    //   true  = this I2C Master is initiating reads from the slave.
    I2CMasterSlaveAddrSet(I2C1_BASE, ACCEL_SLAVE_ADDRESS, true);

    // Start, send device address, read one byte (register data), and end the transaction
    I2CMasterControl(I2C1_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE);

    // Wait for completion
    while(I2CMasterBusy(I2C1_BASE))
    {
        //spin wait
    }
    //TODO: Check I2CMasterErr(I2C1_BASE)

    ui8Data = I2CMasterDataGet(I2C1_BASE);
    return ui8Data;
}
Exemplo n.º 9
0
unsigned long byteAccelRead(int reg)
{
		short temp;
		I2CMasterSlaveAddrSet(I2C_MASTER_BASE, 0x1D, false);
		I2CMasterDataPut(I2C_MASTER_BASE, reg);
		I2CMasterControl(I2C_MASTER_BASE, I2C_MASTER_CMD_BURST_SEND_START);
		while(I2CMasterBusy(I2C_MASTER_BASE));
		I2CMasterSlaveAddrSet(I2C_MASTER_BASE, 0x1D, true);
		I2CMasterControl(I2C_MASTER_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE);
		while(I2CMasterBusy(I2C_MASTER_BASE));
		return I2CMasterDataGet(I2C_MASTER_BASE);
	

	
}
Exemplo n.º 10
0
uint32_t i2c_read_bytes(uint8_t address, uint8_t* buffer, uint32_t length) {
    uint32_t future = I2C_MAX_DELAY_US;
    
    // Receive operation
    I2CMasterSlaveAddrSet(address, true);

    // Multiple receive operation
    I2CMasterControl(I2C_MASTER_CMD_BURST_RECEIVE_START);

    // Calculate timeout
    future += board_timer_get();

    // Iterate overall all bytes
    while (length) {
        // Wait until complete or timeout
        while (I2CMasterBusy()) {
            // Update timeout status and return if expired
            if (board_timer_expired(future)) return length;
        }
        
        // Read data from I2C
        *buffer++ = I2CMasterDataGet();
        length--;

        // Check if it's the last byte
        if (length == 1) I2CMasterControl(I2C_MASTER_CMD_BURST_RECEIVE_FINISH);
        else             I2CMasterControl(I2C_MASTER_CMD_BURST_RECEIVE_CONT);
    }
    
    // Return bytes read
    return length;
}
Exemplo n.º 11
0
/**
 * \brief   Initializes the I2C interface for a codec
 *
 * \param   baseAddr      Base Address of the I2C Module Registers which
 *                        is used for the codec
 *          intCh         Channel Number where the I2C ISR to be registered
 *          slaveAddr     Slave Address of the codec
 *
 * Note: This API enables the system interrupt for the given I2C module only.
 *       It does not do any pin multiplexing or global interrupt enabling. 
 *       This shall be called only after AINTC initialization.
 *
 * \return  None.
 *
 **/
void I2CCodecIfInit(unsigned int baseAddr, unsigned int intCh, 
                    unsigned int slaveAddr)
{
    unsigned int sysIntNum = 0;   

    /* Put i2c in reset/disabled state */
    I2CMasterDisable(baseAddr);

    /* Configure i2c bus speed to 100khz */
    I2CMasterInitExpClk(baseAddr, 24000000, 8000000, 100000);

    /* Set i2c slave address */
    I2CMasterSlaveAddrSet(baseAddr, slaveAddr);

    I2CMasterEnable(baseAddr);
   
    /*
    ** Setup the interrupt in AINTC for the i2c module.
    ** If another instance is to be added, this shall include
    ** checking for the other instance base address also. 
    */ 
    if(SOC_I2C_0_REGS == baseAddr)
    {
#ifdef _TMS320C6X
    	sysIntNum = SYS_INT_I2C0_INT;
#else
        sysIntNum = SYS_INT_I2CINT0;
#endif
    }

    I2CCodecIntSetup(sysIntNum, intCh);
}
Exemplo n.º 12
0
unsigned long I2CController::nolock_write8(uint8_t addr, uint8_t data, bool sendStartCondition, bool sendStopCondition)
{
	unsigned long ret;

	I2CMasterSlaveAddrSet(_base, addr, 0);
	I2CMasterDataPut(_base, data);
	if (sendStartCondition) {
		I2CMasterControl(_base, sendStopCondition ? I2C_MASTER_CMD_SINGLE_SEND : I2C_MASTER_CMD_BURST_SEND_START);
		ret = I2CMasterErr(_base);
		if (ret != I2C_MASTER_ERR_NONE) {
			return ret;
		}

	} else {
		I2CMasterControl(_base, sendStopCondition ? I2C_MASTER_CMD_BURST_SEND_FINISH : I2C_MASTER_CMD_BURST_SEND_CONT);
		ret = I2CMasterErr(_base);
		if (ret != I2C_MASTER_ERR_NONE) {
			return ret;
		}

	}
	ret = waitFinish();
	if (ret != I2C_MASTER_ERR_NONE) {
		return ret;
	}

	return 0;
}
Exemplo n.º 13
0
/**
 * \brief   Sets up the EEPROM I2C interface
 *
 * \param   slaveAddr   Slave Address of the EEPROM
 *
 * \return  None.
 */
void EEPROMI2CSetUp(unsigned int slaveAddr)
{
    /* Configuring system clocks for I2C0 instance. */
    I2C0ModuleClkConfig();

    /* Performing Pin Multiplexing for I2C0. */
    I2C0PinMux();

    /* Put i2c in reset/disabled state */
    I2CMasterDisable(I2C_BASE_ADDR);

    I2CSoftReset(I2C_BASE_ADDR);

    /* Configure i2c bus speed to 100khz */
    I2CMasterInitExpClk(I2C_BASE_ADDR, 48000000, 24000000, 100000);

    /* Set i2c slave address */
    I2CMasterSlaveAddrSet(I2C_BASE_ADDR, slaveAddr);

    /* Disable all I2C interrupts */
    I2CMasterIntDisableEx(I2C_BASE_ADDR, 0xFFFFFFFF);

    /* Bring I2C module out of reset */
    I2CMasterEnable(I2C_BASE_ADDR);

    while(!I2CSystemStatusGet(I2C_BASE_ADDR));
}
Exemplo n.º 14
0
unsigned long I2CController::read8(uint8_t addr, uint8_t *data, bool sendStartCondition, bool sendStopCondition)
{
	RecursiveMutexGuard guard(&_lock);
	unsigned long ret;

	I2CMasterSlaveAddrSet(_base, addr, 1);
	if (sendStartCondition) {
		I2CMasterControl(_base, sendStopCondition ? I2C_MASTER_CMD_SINGLE_SEND : I2C_MASTER_CMD_BURST_RECEIVE_START);
		ret = I2CMasterErr(_base);
		if (ret != I2C_MASTER_ERR_NONE) {
			return ret;
		}
	} else {
		I2CMasterControl(_base, sendStopCondition ? I2C_MASTER_CMD_BURST_RECEIVE_FINISH : I2C_MASTER_CMD_BURST_RECEIVE_CONT);
		ret = I2CMasterErr(_base);
		if (ret != I2C_MASTER_ERR_NONE) {
			return ret;
		}
	}
	ret = waitFinish(_defaultTimeout);
	if (ret != I2C_MASTER_ERR_NONE) {
		return ret;
	}

	*data = I2CMasterDataGet(_base);
	ret = I2CMasterErr(_base);
	if (ret != I2C_MASTER_ERR_NONE) {
		return ret;
	}
	return 0;
}
Exemplo n.º 15
0
Arquivo: main.c Projeto: arduic/GitHub
//Read from device using address and put the read values into buff. num=num of bytes to read
void I2CRead(unsigned char device, unsigned char regiester, unsigned char num, unsigned long *buff){
	//All commented code could likely be delted currently saving for record / emergency
	/*I2CMasterSlaveAddrSet(I2C1_MASTER_BASE, device, false);	//Set Device to transmit to

	I2CMasterDataPut(I2C1_MASTER_BASE,regiester);	//Put on regiester to prep writting
	I2CMasterControl(I2C1_MASTER_BASE,I2C_MASTER_CMD_SINGLE_SEND);
	//while(I2CMasterBusBusy(I2C1_MASTER_BASE));	//Wait till data sent
	while(I2CMasterBusy(I2C1_MASTER_BASE));		//this is good

	short i=0;
	for (i=0; i<num; i++){
		I2CMasterSlaveAddrSet(I2C1_MASTER_BASE, device, true);	//Set device to RECIEVE FROM
		/*I2CMasterDataPut(I2C1_MASTER_BASE,regiester+i);	//Put on regiester to prep writting
		I2CMasterControl(I2C1_MASTER_BASE,I2C_MASTER_CMD_SINGLE_SEND);
		//while(I2CMasterBusBusy(I2C1_MASTER_BASE));	//Wait till data sent
		while(I2CMasterBusy(I2C1_MASTER_BASE));
	 */
	/*if(i==0){
			I2CMasterControl(I2C1_MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_START);
		}else if(i==(num-1)){
			I2CMasterControl(I2C1_MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_FINISH);
		}else{
			I2CMasterControl(I2C1_MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_CONT);
		}
		//while(I2CMasterIntStatus(I2C1_MASTER_BASE, false) == 0);
		while(I2CMasterBusy(I2C1_MASTER_BASE));
		buff[i] = I2CMasterDataGet(I2C1_MASTER_BASE);
		//I2CMasterIntClear(I2C1_MASTER_BASE);
	}
	//buff[0]=0x02;
	 */
	short i=0;	//Initalize i because CCS doesn't like initalizing inside for loop :P
	for(i=0; i<num; i++){
		I2CMasterSlaveAddrSet(I2C1_MASTER_BASE, device, false);	//Set device to write to
		I2CMasterDataPut(I2C1_MASTER_BASE,regiester+i);	//Put on regiester to prep writting
		I2CMasterControl(I2C1_MASTER_BASE,I2C_MASTER_CMD_SINGLE_SEND);	//Make a single send there is no bursting through
		while(I2CMasterBusy(I2C1_MASTER_BASE));		//wait for the transmission to end via checking the master's state NOT THE BUS

		I2CMasterSlaveAddrSet(I2C1_MASTER_BASE, device, true);	//Set device to RECIEVE FROM
		I2CMasterControl(I2C1_MASTER_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE);	//Set a single read because it will only return this address
		while(I2CMasterBusy(I2C1_MASTER_BASE));		//Wait for the master to stop recieving data

		buff[i] = I2CMasterDataGet(I2C1_MASTER_BASE);	//Place the data recieved (that is in the FIFO) into the buffer to return
	}

}
Exemplo n.º 16
0
uint32_t I2CReadMultiple(uint32_t i2c_base, uint8_t address, uint8_t reg, uint8_t *data, uint32_t size) {
	uint32_t byte_count;
	uint32_t master_command;

	// Set slave register to be read
	while(I2CMasterBusy(i2c_base));
	I2CMasterSlaveAddrSet(i2c_base, address, 0);
	I2CMasterDataPut(i2c_base, reg);
	I2CMasterControl(i2c_base, I2C_MASTER_CMD_SINGLE_SEND);
	while(I2CMasterBusy(i2c_base));

	// Check for errors
	if (I2CMasterErr(i2c_base) != I2C_MASTER_ERR_NONE) return 0;

	// Start burst read
	I2CMasterSlaveAddrSet(i2c_base, address, 1);
	master_command = I2C_MASTER_CMD_BURST_RECEIVE_START;

	for (byte_count = 0; byte_count < size; byte_count++) {

		// The second byte has to be read with CONT
		if (byte_count == 1) master_command = I2C_MASTER_CMD_BURST_RECEIVE_CONT;

		// The last byte has to be read with FINISH
		if (byte_count == size - 1) master_command = I2C_MASTER_CMD_BURST_RECEIVE_FINISH;

		// If only one byte, reconfigure to SINGLE
		if (size == 1) master_command = I2C_MASTER_CMD_SINGLE_RECEIVE;

		// Initiate read
		I2CMasterControl(i2c_base, master_command);

		// Wait for master operation
		while(I2CMasterBusy(i2c_base));

		// Check for errors
		if (I2CMasterErr(i2c_base) != I2C_MASTER_ERR_NONE) return 0;

		// Put data into array
		data[byte_count] = I2CMasterDataGet(i2c_base);
	}

	return byte_count;
}
Exemplo n.º 17
0
void i2c_tx_single(unsigned char SlaveAddr, unsigned char dest, unsigned char data)
{
    I2CMasterSlaveAddrSet( I2C1_MASTER_BASE, SlaveAddr, I2C_SEND );
    
    I2CMasterDataPut( I2C1_MASTER_BASE, dest );
    I2CMasterControl( I2C1_MASTER_BASE, I2C_MASTER_CMD_BURST_SEND_START );
    while(I2CMasterBusy(I2C1_MASTER_BASE));
    
    I2CMasterDataPut( I2C1_MASTER_BASE, data );
    I2CMasterControl( I2C1_MASTER_BASE, I2C_MASTER_CMD_BURST_SEND_FINISH );
    while(I2CMasterBusy(I2C1_MASTER_BASE));
}
Exemplo n.º 18
0
//used to control the leds
void LED_CONTROL(int LED_NUMBER, int LED_COMMAND)
{

	data_handler(LED_NUMBER,LED_COMMAND);

	// Slave address of TCA6507 is 0x45 (binary 1001 101)
	// false=write / true=read
	I2CMasterSlaveAddrSet(I2C0_MASTER_BASE, 0x45, false);

// the for loop is there to make the code more compact and removes some redundant commands.
	int COUNTER;
		for (COUNTER=0;COUNTER!=5;COUNTER++)
		{

			if(COUNTER == 2)
			{
				//puts the command-byte in the dataput getting ready to sending it.
				I2CMasterDataPut(I2C0_MASTER_BASE, COMMAND_BYTE_INCREMENT);
				//starts sending the data.
				I2CMasterControl(I2C0_MASTER_BASE,I2C_MASTER_CMD_BURST_SEND_START);
			}
			else if(COUNTER == 3)
			{
				//gets the first led_current_setting containing the byte for select0 ready to transmitting.
				I2CMasterDataPut(I2C0_MASTER_BASE,led_current_setting[0] );
				//keeps sending data.
				I2CMasterControl(I2C0_MASTER_BASE,I2C_MASTER_CMD_BURST_SEND_CONT);
			}
			else if(COUNTER == 4)
			{
				//gets the second led_current_setting containing the byte for select1 ready to transmitting.
				I2CMasterDataPut(I2C0_MASTER_BASE,led_current_setting[1] );
				//keeps sending data.
				I2CMasterControl(I2C0_MASTER_BASE,I2C_MASTER_CMD_BURST_SEND_CONT);
			}
			else if(COUNTER == 5)
			{
				//gets the third led_current_setting containing the byte for select2 ready to transmitting.
				I2CMasterDataPut(I2C0_MASTER_BASE,led_current_setting[2] );
				//transmitting the final byte and a stop command.
				I2CMasterControl(I2C0_MASTER_BASE, I2C_MASTER_CMD_BURST_SEND_FINISH );
			}


			// Wait for I2C to finish.
			while(I2CMasterBusy(I2C0_MASTER_BASE));

			//a short delay.
			SysCtlDelay(80000);
		}

}
Exemplo n.º 19
0
Arquivo: main.c Projeto: arduic/GitHub
//This will setup the com for a device, declare the regiester to write to and then write to that regiester
//Note I2C waits for a response bit before continuing. If there is no device on the other end the masterbusy loop will NEVER exit
void I2CTransmit(unsigned char device, unsigned char regiester, unsigned char value){
	I2CMasterSlaveAddrSet(I2C1_MASTER_BASE, device, false);	//Set Device to transmit to

	I2CMasterDataPut(I2C1_MASTER_BASE,regiester);	//Put on regiester to prep writting
	I2CMasterControl(I2C1_MASTER_BASE,I2C_MASTER_CMD_BURST_SEND_START);	//Send start bit and the first thing
	while(I2CMasterBusy(I2C1_MASTER_BASE));	//Wait till data sent

	I2CMasterDataPut(I2C1_MASTER_BASE,value);	//Put more data on
	I2CMasterControl(I2C1_MASTER_BASE, I2C_MASTER_CMD_BURST_SEND_FINISH);	//Send data and finish bit
	while(I2CMasterBusy(I2C1_MASTER_BASE));	//Wait till done


}
Exemplo n.º 20
0
/*
** Initializes the I2C interface for a slave
*/
void I2C0IfConfig(unsigned int slaveAddr, unsigned int speed)
{
    /* Put i2c in reset/disabled state */
    I2CMasterDisable(SOC_I2C_0_REGS);

    /* Configure i2c bus speed to 100khz */
    I2CMasterInitExpClk(SOC_I2C_0_REGS, 24000000, 8000000, speed);

    /* Set i2c slave address */
    I2CMasterSlaveAddrSet(SOC_I2C_0_REGS, slaveAddr);

    I2CMasterEnable(SOC_I2C_0_REGS);
}
/*
*********************************************************************************************************
*                        BSP_DACWriteRegister (CPU_INT08U ucRegister, CPU_INT32U ulData)
*
* Description : Write a register in the TLV320AIC3107 DAC.
*
* Argument(s) : ucRegister is the offset to the register to write.
*               ulData is the data to be written to the DAC register.
*
* Return(s)   : True on success or false on error.
*
* Caller(s)   : Sound driver.
*
* Note(s)     : This function will write the register passed in ucAddr with the value
*               passed in to ulData.  The data in ulData is actually 9 bits and the
*               value in ucAddr is interpreted as 7 bits.
*********************************************************************************************************
*/
static  CPU_BOOLEAN  BSP_DACWriteRegister (CPU_INT08U ucRegister, CPU_INT32U ulData)
{
    // Set the slave address.
    I2CMasterSlaveAddrSet(DAC_I2C_MASTER_BASE, TI_TLV320AIC3107_ADDR, false);

    // Write the first byte to the controller (register)
    I2CMasterDataPut(DAC_I2C_MASTER_BASE, ucRegister);

    // Continue the transfer.
    I2CMasterControl(DAC_I2C_MASTER_BASE, I2C_MASTER_CMD_BURST_SEND_START);

    // Wait until the current byte has been transferred.
    while(I2CMasterIntStatus(DAC_I2C_MASTER_BASE, false) == 0)
    {
    }

    if(I2CMasterErr(DAC_I2C_MASTER_BASE) != I2C_MASTER_ERR_NONE)
    {
        I2CMasterIntClear(DAC_I2C_MASTER_BASE);
        return(false);
    }

    // Wait until the current byte has been transferred.
    while(I2CMasterIntStatus(DAC_I2C_MASTER_BASE, false))
    {
        I2CMasterIntClear(DAC_I2C_MASTER_BASE);
    }

    // Write the data byte to the controller.
    I2CMasterDataPut(DAC_I2C_MASTER_BASE, ulData);

    // End the transfer.
    I2CMasterControl(DAC_I2C_MASTER_BASE, I2C_MASTER_CMD_BURST_SEND_FINISH);

    // Wait until the current byte has been transferred.
    while(I2CMasterIntStatus(DAC_I2C_MASTER_BASE, false) == 0)
    {
    }

    if(I2CMasterErr(DAC_I2C_MASTER_BASE) != I2C_MASTER_ERR_NONE)
    {
        return(false);
    }

    while(I2CMasterIntStatus(DAC_I2C_MASTER_BASE, false))
    {
        I2CMasterIntClear(DAC_I2C_MASTER_BASE);
    }

    return(true);
}
Exemplo n.º 22
0
void I2CSetup(void)
{
    I2CPinMuxSetup(0);

    /* Put i2c in reset/disabled state */
    I2CMasterDisable(SOC_I2C_0_REGS);

    /* Configure i2c bus speed to 100khz */
    I2CMasterInitExpClk(SOC_I2C_0_REGS, 24000000, 8000000, 100000);

    /* Set i2c slave address */
    I2CMasterSlaveAddrSet(SOC_I2C_0_REGS, 0x48);
    ConfigureAINTCIntI2C();
}
Exemplo n.º 23
0
uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity, uint8_t sendStop)
{
  uint8_t error = 0;
  uint8_t oldWriteIndex = rxWriteIndex;
  uint8_t spaceAvailable = (rxWriteIndex >= rxReadIndex) ?
         BUFFER_LENGTH - (rxWriteIndex - rxReadIndex) : (rxReadIndex - rxWriteIndex);

  if (quantity > spaceAvailable)
      quantity = spaceAvailable;
  if (!quantity) return 0;

  //Select which slave we are requesting data from
  //true indicates we are reading from the slave
  I2CMasterSlaveAddrSet(MASTER_BASE, address, true);

  unsigned long cmd = 0;

  if((quantity > 1) || !sendStop)
      cmd = RUN_BIT | START_BIT | ACK_BIT;
  else cmd = RUN_BIT | START_BIT | (sendStop << 2);

  error = getRxData(cmd);
  if(error) return 0;

  currentState = MASTER_RX;

  for (int i = 1; i < quantity; i++) {
      //since NACK is being sent on last byte, a consecutive burst read will
      //need to send a start condition
      if(i == (quantity - 1))
        cmd = RUN_BIT;
      else
        cmd = RUN_BIT | ACK_BIT;

      error = getRxData(cmd);
      if(error) return i;
  }

  if(sendStop) {
      HWREG(MASTER_BASE + I2C_O_MCS) = STOP_BIT;
      while(I2CMasterBusy(MASTER_BASE));
      currentState = IDLE;
  }

  uint8_t bytesWritten = (rxWriteIndex >= oldWriteIndex) ?
         BUFFER_LENGTH - (rxWriteIndex - oldWriteIndex) : (oldWriteIndex - rxWriteIndex);

  return(bytesWritten);

}
Exemplo n.º 24
0
//used to control the brightness value.
void CHANGE_BRIGHTNESS_VALUES(int value0,int value1)
{
	// Slave address of TCA6507 is 0x45 (binary 1001 101)
		// false=write / true=read
		I2CMasterSlaveAddrSet(I2C0_MASTER_BASE, 0x45, false);

		int COUNTER;
		for (COUNTER=0;COUNTER!=4;COUNTER++)
		{

			//ensure that the user input value is a legal value if not it skips out of the function.
			if(value0||value1 > 15 )
			{
				COUNTER=4;
			}
			else if(value0||value1 <  1 )
			{
				COUNTER=4;
			}



			else if(COUNTER == 2)
			{
				//puts the command-byte in the dataput getting ready to sending it.
				I2CMasterDataPut(I2C0_MASTER_BASE, COMMAND_BYTE_MAXIMUM_INTENSITY);

				//starts sending the data.
				I2CMasterControl(I2C0_MASTER_BASE,I2C_MASTER_CMD_BURST_SEND_START);
			}
			else if(COUNTER == 3)
				{
					//value0 and value1 is send through the same byte where value0 is the first 4bit and value1 is the last 4bit.
					I2CMasterDataPut(I2C0_MASTER_BASE,NUMBER_HEX[value0]+(10*NUMBER_HEX[value1]));

					//transmitting the final byte and a stop command.
					I2CMasterControl(I2C0_MASTER_BASE,I2C_MASTER_CMD_BURST_SEND_FINISH );
				}


			// Wait for I2C to finish.
			while(I2CMasterBusy(I2C0_MASTER_BASE));

			//a short delay.
			SysCtlDelay(80000);

		}


}
Exemplo n.º 25
0
void main(void) {

	unsigned char temp_data[10] = "00.0 C \n\n\r";		// Temp format to be edited by read
	unsigned short int i = 0; 							// general counter

	// Setup the I2C see lab 7 ***************************************************************************************
	SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ);  //setup clock

	SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0);		// Enable I2C hardware
	SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);	// Enable Pin hardware

	GPIOPinConfigure(GPIO_PB3_I2C0SDA);				// Configure GPIO pin for I2C Data line
	GPIOPinConfigure(GPIO_PB2_I2C0SCL);				// Configure GPIO Pin for I2C clock line

	GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_2 | GPIO_PIN_3);  // Set Pin Type

	GPIOPadConfigSet(GPIO_PORTB_BASE, GPIO_PIN_2, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD);// SDA MUST BE STD
	GPIOPadConfigSet(GPIO_PORTB_BASE, GPIO_PIN_3, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_OD);	// SCL MUST BE OPEN DRAIN
	I2CMasterInitExpClk(I2C0_BASE, SysCtlClockGet(), false); // The False sets the controller to 100kHz communication
	I2CMasterSlaveAddrSet(I2C0_BASE, TEMP_ADDR, true);  				// false means transmit
	//****************************************************************************************************************

	// Setup the UART see lab 6 **************************************************************************************

	SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);		// Enable UART hardware
	SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);		// Enable Pin hardware

	GPIOPinConfigure(GPIO_PA0_U0RX);		// Configure GPIO pin for UART RX line
	GPIOPinConfigure(GPIO_PA1_U0TX);		// Configure GPIO Pin for UART TX line
	GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);     // Set Pins for UART

	UARTConfigSetExpClk(UART0_BASE, SysCtlClockGet(), 115200,		// Configure UART to 8N1 at 115200bps
			(UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE | UART_CONFIG_PAR_NONE));
	//****************************************************************************************************************

	Print_header();			// Print Header

	while(1){

		Read_temp(temp_data);						// Read Data from Temp Sensor
		SysCtlDelay(6000000);						// Delay
		for(i=0;i<10;i++){							// Loop to print out data string
			UARTCharPut(UART0_BASE, temp_data[i]);
		}

	}

}
Exemplo n.º 26
0
void accelWrite (int reg,char data)
{
		// Sets the slave address
		I2CMasterSlaveAddrSet(I2C_MASTER_BASE, 0x1D, false);
		// Sends the register we want
		I2CMasterDataPut(I2C_MASTER_BASE, reg);
		I2CMasterControl(I2C_MASTER_BASE, I2C_MASTER_CMD_BURST_SEND_START);
		while(I2CMasterBusy(I2C_MASTER_BASE));
		


		// Set the register value
		I2CMasterDataPut(I2C_MASTER_BASE, data);
		I2CMasterControl(I2C_MASTER_BASE, I2C_MASTER_CMD_BURST_SEND_FINISH);
		while(I2CMasterBusy(I2C_MASTER_BASE));
}
Exemplo n.º 27
0
/*
** Function to initialize the audio codec
*/
static void AudioCodecInit(void)
{
    volatile unsigned int delay = 0xFFF;

    /* Initialize the I2C interface for the codec AIC31 */
    I2CMasterSlaveAddrSet(I2C_INST_BASE, I2C_SLAVE_CODEC_AIC31);

    AIC31Reset(I2C_INST_BASE);
    while(delay--);

    /* Configure the data format and sampling rate */
    AIC31DataConfig(I2C_INST_BASE, AIC31_DATATYPE_I2S, TONE_SLOT_SIZE, 0);
    AIC31SampleRateConfig(I2C_INST_BASE, AIC31_MODE_BOTH, TONE_SAMPLE_RATE);

    /* Initialize both ADC and DAC */
    AIC31ADCInit(I2C_INST_BASE);
    AIC31DACInit(I2C_INST_BASE);
}
Exemplo n.º 28
0
//*****************************************************************************
//
//! \internal
//!
//! Start a transfer to the SSD0303 controller.
//!
//! \param ucChar is the first byte to be written to the controller.
//!
//! This function will start a transfer to the SSD0303 controller via the I2C
//! bus.
//!
//! The data is written in a polled fashion; this function will not return
//! until the byte has been written to the controller.
//!
//! \return None.
//
//*****************************************************************************
static void
OSRAMWriteFirst(unsigned char ucChar)
{
    //
    // Set the slave address.
    //
    I2CMasterSlaveAddrSet(I2C_MASTER_BASE, SSD0303_ADDR, false);

    //
    // Write the first byte to the controller.
    //
    I2CMasterDataPut(I2C_MASTER_BASE, ucChar);

    //
    // Start the transfer.
    //
    I2CMasterControl(I2C_MASTER_BASE, I2C_MASTER_CMD_BURST_SEND_START);
}
Exemplo n.º 29
0
uint32_t i2c_write_bytes(uint8_t address, uint8_t* buffer, uint32_t length) {
    uint32_t future = I2C_MAX_DELAY_US;
    
    // Transmit operation
    I2CMasterSlaveAddrSet(address, false);

    // Write byte to I2C buffer
    I2CMasterDataPut(*buffer++);
    length--;

    // Multiple transmit operation
    I2CMasterControl(I2C_MASTER_CMD_BURST_SEND_START);

    // Calculate timeout
    future += board_timer_get();

    // Wait until complete or timeout
    while (I2CMasterBusy()) {
        // Check timeout status and return if expired
        if (board_timer_expired(future)) return length;
    }

    // Iterate overall all bytes
    while (length) {
        // Write byte to I2C buffer
        I2CMasterDataPut(*buffer++);

        // Check if it's the last byte
        if (length == 1) I2CMasterControl(I2C_MASTER_CMD_BURST_SEND_FINISH);
        else             I2CMasterControl(I2C_MASTER_CMD_BURST_SEND_CONT);

        // Wait until complete or timeout
        while (I2CMasterBusy()) {
            // Check timeout status and return if expired
            if (board_timer_expired(future)) return length;
        }

        // Update the length
        length--;
    }
    
    // Return bytes written
    return length;
}
Exemplo n.º 30
0
//*****************************************************************************
//
// Writes a single gyro register.  This routine is blocking.
//
// \param ui8RegisterAddress is the register address
// \param ui8Data is the data to write
//
//*****************************************************************************
void
Accel_RegWrite(uint8_t ui8RegisterAddress, uint8_t ui8Data)
{
    if (VERBOSE) UARTprintf("GryroWriteReg(0x%x, 0x%x)\n", ui8RegisterAddress, ui8Data);

    // Set the slave device address for WRITE
    //   false = this I2C Master is initiating a writes to the slave.
    //   true  = this I2C Master is initiating reads from the slave.
    I2CMasterSlaveAddrSet(I2C1_BASE, ACCEL_SLAVE_ADDRESS, false);

    //
    // Transaction #1: Send the register address
    //

    // Set the Gyro Register address to write
    I2CMasterDataPut(I2C1_BASE, ui8RegisterAddress);

    // Start, send device address, write one byte (register address)...
    I2CMasterControl(I2C1_BASE, I2C_MASTER_CMD_BURST_SEND_START);

    // Wait for completion
    while(I2CMasterBusy(I2C1_BASE))
    {
    	//spin wait
    }
    //TODO: Check I2CMasterErr(I2C1_BASE)

    //
    // Transaction #1 continued: Send the register data
    //

    //Set the Gyro Register address to write
    I2CMasterDataPut(I2C1_BASE, ui8Data);

    // ... write another byte (register data) and end the transaction
    I2CMasterControl(I2C1_BASE, I2C_MASTER_CMD_BURST_SEND_FINISH);

    // Wait for completion
    while(I2CMasterBusy(I2C1_BASE))
    {
        //spin wait
    }
    //TODO: Check I2CMasterErr(I2C1_BASE)
}