Skip to content

Instantly share code, notes, and snippets.

@calebreister
Forked from andresv/sys_bios_i2c_example.c
Last active August 29, 2015 14:19
Show Gist options
  • Save calebreister/5736e9e909d4b7dedd0c to your computer and use it in GitHub Desktop.
Save calebreister/5736e9e909d4b7dedd0c to your computer and use it in GitHub Desktop.
EDMA3_DRV_Handle* hi2cEdma;
GIO_Handle i2c_outHandle;
GIO_Handle i2c_inHandle;
I2c_Params i2cParams;
//----------------------------------------------------------------
// Setup I2C
//----------------------------------------------------------------
void I2C_init(EDMA3_DRV_Handle* hEdma) {
I2c_ChanParams chanParams;
GIO_Params ioParams;
DEV_Params devParams;
Error_Block eb;
Error_init(&eb);
hi2cEdma = hEdma;
// Initialize pinmux and evm related configurations
configureI2c();
// create I2C device
DEV_Params_init(&devParams);
devParams.deviceParams = (Ptr)&i2cParams;
devParams.devid = 0;
devParams.initFxn = &i2c_device_init_fxn;
DEV_create("/i2c0", (Ptr)&I2c_IOMFXNS, &devParams, &eb);
// update the edma Handle
chanParams.hEdma = *hi2cEdma;
chanParams.masterOrSlave = I2c_CommMode_MASTER;
GIO_Params_init(&ioParams);
ioParams.chanParams = &chanParams;
// Create the I2C Channels for the TX and RX communication
i2c_outHandle = GIO_create("/i2c0", GIO_OUTPUT, &ioParams, &eb);
i2c_inHandle = GIO_create("/i2c0", GIO_INPUT, &ioParams, &eb);
if ((NULL == i2c_outHandle) || (NULL == i2c_inHandle)) {
System_printf("\nI2C stream creation failed\n");
BIOS_exit(0);
}
}
//----------------------------------------------------------------
// Write data to I2C
//----------------------------------------------------------------
void I2C_write(uint8_t addr_7_bit, uint8_t* data, uint8_t len) {
int status;
I2c_DataParam data_buffer;
data_buffer.slaveAddr = addr_7_bit;
data_buffer.buffer = data;
data_buffer.bufLen = len;
data_buffer.flags = I2c_WRITE | I2c_MASTER | I2c_START | I2c_STOP;
status = GIO_submit(i2c_outHandle, IOM_WRITE, &data_buffer, &data_buffer.bufLen, NULL);
if (IOM_COMPLETED != status) {
System_printf("\r\nI2C GIO_submit failed. returned : %d", status);
}
}
//----------------------------------------------------------------
// Read data from I2C
//----------------------------------------------------------------
void I2C_read(uint8_t addr_7_bit, uint8_t* buffer, uint8_t len) {
}
// DEV_create(...) uses it to init I2C
static void i2c_device_init_fxn() {
I2c_init();
i2cParams = I2c_PARAMS;
i2cParams.busFreq = 200000;
i2cParams.hwiNumber = 8;
i2cParams.opMode = I2c_OpMode_POLLED;
if(I2c_OpMode_POLLED == i2cParams.opMode) {
System_printf("I2c is configured in polled mode");
}
else if (I2c_OpMode_INTERRUPT == i2cParams.opMode) {
System_printf("I2c is configured in interrupt mode");
}
else if (I2c_OpMode_DMAINTERRUPT == i2cParams.opMode) {
System_printf("I2c is configured in dma mode");
}
else {
System_printf("Error: unknown mode of operation!!!!!!!!!!");
}
}
//-------------------------------------------------------------------------------------------------------------------------
EDMA3_DRV_Handle edma3init(unsigned int edma3Id, EDMA3_DRV_Result*);
extern EDMA3_DRV_Handle hEdma;
static void init_edma(void) {
EDMA3_DRV_Result edmaResult = 0;
hEdma = edma3init(0, &edmaResult);
if (edmaResult != EDMA3_DRV_SOK) {
System_printf("\nEDMA driver initialization FAIL\n");
}
else {
System_printf("\nEDMA driver initialization PASS.\n");
}
}
void main() {
Error_init(&ERROR_task_packet_sender);
init_edma();
TASK_packet_receiver = Task_create(packet_receiver, NULL, &ERROR_task_packet_receiver);
I2C_init(&hEdma);
Clock_Params clkParams;
Clock_Params_init(&clkParams);
clkParams.startFlag = TRUE;
clkParams.period = 1000;
CLOCK_uart_out = Clock_create(timer_fired, 1000, &clkParams, NULL);
BIOS_start(); // enable interrupts and start SYS/BIOS
}
#pragma DATA_ALIGN(config, 128);
uint8_t config[2] = {0x30, 0x00};
static void packet_receiver(UArg a0, UArg a1) {
while (1) {
I2C_write(0x10, config, 2);
Task_sleep(1000);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment