Other Parts Discussed in Thread: SYSBIOS, AMIC110, AMIC120
Hello Experts,
AM335x Beaglebone Black development board, PROCESSOR-SDK-RTOS-AM335X Version: 06.03.00.106, example project main_temperature_sensor.c. modified to suit my slave device.
slave device address 0x48
modify to slave device register address (0x01)
read from slave device with 2 bytes,
enable I2C1 (P9_17 and P9_18)
modify to run @ 400Khz
from Oscilliscope (read 2 bytes from slave device)
| START | 0x48 | write=0 | 0x01 | huge delay | 0x48 | read=1 | byte1 | byte2 | STOP | huge delay |
Please help. thanks in advance
/**
* \file main_temperature_sensor.c
*
* \brief Example application main file. This application will get the
* temperature from the temperature sensor through I2C interface and
* displays on the serial console.
*
*/
/* XDCtools Header files */
#include <xdc/std.h>
#include <xdc/cfg/global.h>
#include <xdc/runtime/System.h>
#include <xdc/runtime/Error.h>
/* BIOS Header files */
#include <ti/sysbios/BIOS.h>
#include <ti/sysbios/knl/Task.h>
#include <stdio.h>
#include <string.h>
#include <ti/drv/i2c/soc/I2C_soc.h>
#include <ti/drv/i2c/test/eeprom_read/src/I2C_log.h>
#include <ti/drv/i2c/test/eeprom_read/src/I2C_board.h>
#include <ti/csl/hw_types.h>
#include <ti/csl/soc.h>
#define CM_PER_I2C1_CLKCTRL (SOC_CM_PER_REGS + 0x48)
static void enableI2C1Module(void)
{
/* Enable I2C1 clock in CM_PER */
HWREG( CM_PER_I2C1_CLKCTRL) = 0x2; /* MODULEMODE = ENABLE */
while ((HWREG( CM_PER_I2C1_CLKCTRL) & 0x30000) != 0) {
; /* Wait for MODULEMODE to become functional */
}
/* Configure I2C1 pins manually (BBB: P9_17=SCL, P9_18=SDA, mode 2) */
HWREG(0x44E10958) = 0x72; /* conf_uart1_rxd -> I2C1_SDA, mode 2, pull-up, receiver enabled */
HWREG(0x44E1095C) = 0x72; /* conf_uart1_txd -> I2C1_SCL, mode 2, pull-up, receiver enabled */
}
/* Temperature sensor slave address */
#define I2C_TEMP_SENSOR_SLAVE_ADDR (0x48U)
/* Instance of I2C controller that is connected to the temperature sensor */
#define I2C_TEMP_SENSOR_INSTANCE (1U)
#define I2C_TRANSACTION_TIMEOUT (2000U)
/**********************************************************************
************************** Global Variables **************************
**********************************************************************/
/*
* ======== Board_initI2C ========
*/
static void Board_initI2C(void)
{
Board_initCfg boardCfg;
boardCfg = BOARD_INIT_PINMUX_CONFIG |
BOARD_INIT_MODULE_CLOCK |
BOARD_INIT_UART_STDIO;
Board_init(boardCfg);
}
/*
* ======== test function ========
*/
void i2c_test(UArg arg0, UArg arg1)
{
I2C_Params i2cParams;
I2C_Handle handle = NULL;
I2C_Transaction i2cTransaction;
uint8_t txBuf[1] = {0x01};
uint8_t rxBuf[2] = {0x00, 0x00};
int16_t status;
int i=0;
I2C_log("\n I2C Temperature Sensor Application \n");
I2C_init();
I2C_Params_init(&i2cParams);
i2cParams.bitRate = I2C_400kHz; /* Use predefined macro */
handle = I2C_open(I2C_TEMP_SENSOR_INSTANCE, &i2cParams);
while(1)
{
I2C_log("\n\r");
I2C_transactionInit(&i2cTransaction);
i2cTransaction.slaveAddress = I2C_TEMP_SENSOR_SLAVE_ADDR;
i2cTransaction.writeBuf = (uint8_t *)&txBuf[0];
i2cTransaction.writeCount = 1;
i2cTransaction.readBuf = (uint8_t *)&rxBuf[0];
i2cTransaction.readCount = 2;
i2cTransaction.timeout = I2C_TRANSACTION_TIMEOUT;
status = I2C_transfer(handle, &i2cTransaction);
if(I2C_STS_SUCCESS == status)
{
for (i = 0; i < 2; i++)
{
I2C_log("\n Byte %d: 0x%02X \n", i, rxBuf[i]);
}
}
else
{
UART_printStatus("\n Some tests have failed. \n");
}
}
I2C_close(handle);
while (1) {
}
}
/*
* ======== main ========
*/
int main(void)
{
Board_initI2C();
enableI2C1Module(); /* enable I2C1 */
#if defined (SOC_AM335X) || defined (SOC_AM437x)
Task_Handle task;
Error_Block eb;
Error_init(&eb);
task = Task_create(i2c_test, NULL, &eb);
if (task == NULL) {
System_printf("Task_create() failed!\n");
BIOS_exit(0);
}
#endif
/* Start BIOS */
BIOS_start();
return (0);
}
