Skip to content

Instantly share code, notes, and snippets.

@andresv
Created February 4, 2013 14:30
Show Gist options
  • Save andresv/4707015 to your computer and use it in GitHub Desktop.
Save andresv/4707015 to your computer and use it in GitHub Desktop.
Demonstrates how to use I2C with SYS/BIOS. It is actually little bit buggy.
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);
}
}
@andresv
Copy link
Author

andresv commented Feb 4, 2013

var Defaults = xdc.useModule('xdc.runtime.Defaults');
var Diags = xdc.useModule('xdc.runtime.Diags');
var Error = xdc.useModule('xdc.runtime.Error');
var Log = xdc.useModule('xdc.runtime.Log');
var LoggerBuf = xdc.useModule('xdc.runtime.LoggerBuf');
var Main = xdc.useModule('xdc.runtime.Main');
var Memory = xdc.useModule('xdc.runtime.Memory')
var SysMin = xdc.useModule('xdc.runtime.SysMin');
var System = xdc.useModule('xdc.runtime.System');
var Text = xdc.useModule('xdc.runtime.Text');

var BIOS = xdc.useModule('ti.sysbios.BIOS');
var Clock = xdc.useModule('ti.sysbios.knl.Clock');
var Swi = xdc.useModule('ti.sysbios.knl.Swi');
var Task = xdc.useModule('ti.sysbios.knl.Task');
var Semaphore = xdc.useModule('ti.sysbios.knl.Semaphore');
var Hwi = xdc.useModule('ti.sysbios.hal.Hwi');
var Idle = xdc.useModule('ti.sysbios.knl.Idle');
var Event = xdc.useModule('ti.sysbios.knl.Event');
var Mailbox = xdc.useModule('ti.sysbios.knl.Mailbox');
var Timer = xdc.useModule('ti.sysbios.timers.timer64.Timer');
var ECM = xdc.useModule('ti.sysbios.family.c64p.EventCombiner');
var ti_sysbios_hal_Cache = xdc.useModule('ti.sysbios.hal.Cache');
var xdc_runtime_Timestamp = xdc.useModule('xdc.runtime.Timestamp');
var DEV = xdc.useModule('ti.sysbios.io.DEV');
var GIO = xdc.useModule('ti.sysbios.io.GIO');
var Agent = xdc.useModule('ti.sysbios.rta.Agent');
var Load = xdc.useModule('ti.sysbios.utils.Load');
var SysStd = xdc.useModule('xdc.runtime.SysStd');

/* 
 * Program.argSize sets the size of the .args section. 
 * The examples don't use command line args so argSize is set to 0.
 */
Program.argSize = 0x0;

/*
 * Uncomment this line to globally disable Asserts.
 * All modules inherit the default from the 'Defaults' module.  You
 * can override these defaults on a per-module basis using Module.common$. 
 * Disabling Asserts will save code space and improve runtime performance.
Defaults.common$.diags_ASSERT = Diags.ALWAYS_OFF;
 */

/*
 * Uncomment this line to keep module names from being loaded on the target.
 * The module name strings are placed in the .const section. Setting this
 * parameter to false will save space in the .const section.  Error and
 * Assert messages will contain an "unknown module" prefix instead
 * of the actual module name.
Defaults.common$.namedModule = false;
 */

/*
 * Minimize exit handler array in System.  The System module includes
 * an array of functions that are registered with System_atexit() to be
 * called by System_exit().
 */
System.maxAtexitHandlers = 4;       

/* 
 * Uncomment this line to disable the Error print function.  
 * We lose error information when this is disabled since the errors are
 * not printed.  Disabling the raiseHook will save some code space if
 * your app is not using System_printf() since the Error_print() function
 * calls System_printf().
Error.raiseHook = null;
 */

/* 
 * Uncomment this line to keep Error, Assert, and Log strings from being
 * loaded on the target.  These strings are placed in the .const section.
 * Setting this parameter to false will save space in the .const section.
 * Error, Assert and Log message will print raw ids and args instead of
 * a formatted message.
Text.isLoaded = false;
 */

/*
 * Uncomment this line to disable the output of characters by SysMin
 * when the program exits.  SysMin writes characters to a circular buffer.
 * This buffer can be viewed using the SysMin Output view in ROV.
SysMin.flushAtExit = false;
 */

/*
 * The BIOS module will create the default heap for the system.
 * Specify the size of this default heap.
 */
//BIOS.heapSize = 9437184;

/* System stack size (used by ISRs and Swis) */
Program.stack = 65536;

/* Circular buffer size for System_printf() */
SysMin.bufSize = 0x200;

/* 
 * Create and install logger for the whole system
 */
var loggerBufParams = new LoggerBuf.Params();
loggerBufParams.numEntries = 16;
var logger0 = LoggerBuf.create(loggerBufParams);
Defaults.common$.logger = logger0;
Main.common$.diags_INFO = Diags.ALWAYS_ON;

System.SupportProxy = SysMin;


ECM.eventGroupHwiNum[0] = 7;
ECM.eventGroupHwiNum[1] = 8;
ECM.eventGroupHwiNum[2] = 9;
ECM.eventGroupHwiNum[3] = 10;
Agent.sysbiosHwiLogging = false;
Agent.sysbiosSwiLogging = true;
Agent.sysbiosLoggerSize = 32768;
Memory.defaultHeapSize = 8192;

Program.sectMap[".text"] = "DDR";

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment