📄 i2c_edma_example.c
字号:
/* ============================================================================
* Copyright (c) Texas Instruments Inc 2002, 2003, 2004, 2005, 2006
*
* Use of this software is controlled by the terms and conditions found
* in the license agreement under which this software has been supplied.
* ============================================================================
*/
/** ============================================================================
* @file i2c_edma_example.c
*
* @path $(CSLPATH)\example\c6486\i2c\i2c_edma_example\src
*
* @desc Example of i2c
*
* ============================================================================
* @n Target Platform: TCI6486 VDB
* ============================================================================
* @n <b> Example Description </b>
* @n The example shows the functionality of I2C in Loop back mode
* - Enables the I2C power saver clock
* - Initalizes and Opens I2C instance.
* - Takes i2c out of reset
* - Configures the EDMA for TX & RX
* - Enables NMIs and Global Interrupts
* - Enables and starts I2C
* - Waits for the transfer to start
* - verifies the data received
* - Stops the transmission and
* - Closes the opened instance
*
*==============================================================================
*
* <b> Procedure to run the example </b>
* @verbatim
* 1. Configure the CCS setup to work with the emulator being used
* 2. Please refer CCS manual for setup configuration and loading
* proper GEL file
* 3. Launch CCS window
* 4. Open project I2c_edma_example.pjt
* 5. Build the project and load the .out file of the project.
*
* @endverbatim
*
* =============================================================================
*/
/** =============================================================================
* Revision History
* ===============*
* 25-Sep-2006 NG File Created
*
* =============================================================================
*/
#include <csl_edma3.h>
#include <csl_i2c.h>
#include <soc.h>
#include <stdio.h>
#include <csl_i2cAux.h>
/* Define data count */
#define DATATX_COUNT 16
#define TEST_ACNT 1
#define TEST_BCNT 16
#define TEST_CCNT 1
#define CSL_I2C_MASTER_ADDR 0x2AA
#define CSL_I2C_SLAVE_ADDR 0x1E
#define CSL_I2C_CLOCK_PRESCALAR 0x13
#define CSL_I2C_CLOCK_CLKL 0x4
#define CSL_I2C_CLOCK_CLKH 0x4
#define CSL_I2C_DISABLE_INTR 0x7F
CSL_Edma3Handle hModule;
CSL_Edma3ParamHandle paramHandle0;
CSL_Edma3ParamHandle paramHandle1;
CSL_Edma3ChannelAttr chParam;
CSL_Edma3ChannelObj ChObj0,ChObj1;
CSL_Edma3ChannelHandle hChannel0,hChannel1;
CSL_Edma3HwDmaChannelSetup chSetup;
CSL_Edma3ParamSetup paramSetup;
CSL_Edma3Obj moduleObj;
CSL_Edma3CmdIntr regionIntr;
CSL_Edma3ChannelErr chErrClear;
CSL_Status EdmaStat;
/* Handle for the I2C instance */
CSL_I2cHandle hI2c;
CSL_I2cObj i2cObj;
Uint8 srcBuff[DATATX_COUNT];
Uint8 dstBuff[DATATX_COUNT];
Uint32 i;
/* clk set up */
CSL_I2cClkSetup clksetup = {
CSL_I2C_CLOCK_PRESCALAR,
CSL_I2C_CLOCK_CLKL,
CSL_I2C_CLOCK_CLKH,
};
void i2c_edma_example(void);
void i2c_edma_setup (void);
/*
* =============================================================================
* @func main
*
* @desc
* This is the main routine, which invokes I2C edma example scripts
* =============================================================================
*/
void main (
void
)
{
i2c_edma_example();
return;
}
/*
* ============================================================================
* @func i2c_edma_example
*
* @desc
* @n This function sets up the config setup parameters
* and start the i2c. Then call i2c_edma_setup () for edma setup for i2c.
* It verifies data tranmited from local buffer to i2c DXR register
* and data tranmited from i2c DRR register to local buffer using edma.
*
* @arg
* None
*
* @return
* None
* ============================================================================
*/
void i2c_edma_example (
void
)
{
CSL_I2cHwSetup hwSetup;
CSL_Status status = CSL_SOK;
Uint32 DataCount = DATATX_COUNT;
Uint8 success = TRUE;
Uint32 index = 0;
hwSetup.mode = CSL_I2C_MODE_MASTER;
hwSetup.dir = CSL_I2C_DIR_TRANSMIT;
hwSetup.addrMode = CSL_I2C_ADDRSZ_SEVEN;
hwSetup.sttbyteen = CSL_I2C_STB_DISABLE;
hwSetup.ownaddr = CSL_I2C_SLAVE_ADDR;
hwSetup.ackMode = CSL_I2C_ACK_ENABLE;
hwSetup.runMode = CSL_I2C_FREE_MODE_DISABLE;
hwSetup.repeatMode = CSL_I2C_REPEAT_MODE_DISABLE;
hwSetup.loopBackMode = CSL_I2C_DLB_ENABLE;
hwSetup.freeDataFormat = CSL_I2C_FDF_DISABLE;
hwSetup.resetMode = CSL_I2C_IRS_ENABLE;
hwSetup.bcm = CSL_I2C_BCM_DISABLE;
hwSetup.inten = 0x00;
hwSetup.clksetup = &clksetup;
/* initializes the transmit buffer */
for (index = 0; index < DATATX_COUNT; index++) {
srcBuff[index] = 0x41 + index;
dstBuff[index] = 0;
}
printf("Example of I2C\n");
/* Initialize I2C module */
status = CSL_i2cInit(NULL);
if (status != CSL_SOK) {
printf("I2C: Initialization error.\n");
printf("\tReason: CSL_i2cInit [status = 0x%x].\n", status);
return;
}
/* open i2c */
hI2c = CSL_i2cOpen(&i2cObj, CSL_I2C, NULL, &status);
if ((status != CSL_SOK) || (hI2c == NULL)) {
printf("I2C: Error opening the instance. \
[status = 0x%x, hI2c = 0x%x]\n", status, hI2c);
return;
}
/* i2c hwsetup */
status = CSL_i2cHwSetup(hI2c, &hwSetup);
if (status != CSL_SOK) {
printf("I2C: Error in I2C Hw Setup. [status = 0x%x]\n",status);
return;
}
/* i2c out of reset */
CSL_i2cHwControl(hI2c, CSL_I2C_CMD_OUTOFRESET, NULL);
/* set data count */
CSL_i2cHwControl(hI2c, CSL_I2C_CMD_SET_DATA_COUNT, &DataCount);
/* Enable I2C */
status = CSL_i2cHwControl(hI2c, CSL_I2C_CMD_ENABLE, NULL);
if (status != CSL_SOK) {
printf("I2C: Error while enabling I2C. [status = 0x%x]\n", status);
return;
}
/* setup edma */
i2c_edma_setup();
for (i = 0; i < DATATX_COUNT; i++) {
if (srcBuff[i] != dstBuff[i]) {
printf("Not mached: RxData = %d\t txData=%d\n", \
dstBuff[i],srcBuff[i]);
success = 0;
break;
}
else {
printf("Matched : RxData = %d\t txData=%d\n", \
dstBuff[i],srcBuff[i]);
}
}
if (success == 1) {
printf("<<EXAMPLE PASSED>> Received data same as Transmitted data\n");
}
else {
printf("<<EXAMPLE FAILED>> Received data not same as Transmitted data\n");
}
/* All done now, close the port. */
/* Close the opened instance */
if (CSL_i2cClose(hI2c) != CSL_SOK)
return;
return;
}
/*
* ============================================================================
* @func rxmyIsr
*
* @desc
* @n This function sets up the interrupt, edma module and invokes routine to
* install interrupt handlers for Mcbsp edma transfer
*
* @arg
* None
*
* @return
* None
* ============================================================================
*/
void i2c_edma_setup (
void
)
{
// Module Initialization
CSL_edma3Init(NULL);
// Module Open
hModule = CSL_edma3Open(&moduleObj,CSL_EDMA3,NULL,&EdmaStat);
// Channel Open
chParam.regionNum = CSL_EDMA3_REGION_GLOBAL;
chSetup.que = CSL_EDMA3_QUE_0;
chParam.chaNum = CSL_EDMA3_CHA_ICXEVT;
hChannel0 = CSL_edma3ChannelOpen(&ChObj0,
CSL_EDMA3,
&chParam,
&EdmaStat);
// Channel Setup
chSetup.paramNum = CSL_EDMA3_CHA_ICXEVT;
CSL_edma3HwChannelSetupQue(hChannel0,chSetup.que);
CSL_edma3HwChannelSetupParam(hChannel0,chSetup.paramNum);
chParam.regionNum = CSL_EDMA3_REGION_GLOBAL;
chSetup.que = CSL_EDMA3_QUE_0;
chParam.chaNum = CSL_EDMA3_CHA_ICREVT;
hChannel1 = CSL_edma3ChannelOpen(&ChObj1,
CSL_EDMA3,
&chParam,
&EdmaStat);
// Channel Setup
chSetup.paramNum = CSL_EDMA3_CHA_ICREVT;
CSL_edma3HwChannelSetupQue(hChannel1,chSetup.que);
CSL_edma3HwChannelSetupParam(hChannel1,chSetup.paramNum);
// Parameter Handle Open
// Open all the handles and keep them ready
paramHandle0 = CSL_edma3GetParamHandle(hChannel0,CSL_EDMA3_CHA_ICXEVT,NULL);
paramHandle1 = CSL_edma3GetParamHandle(hChannel1,CSL_EDMA3_CHA_ICREVT,NULL);
paramSetup.aCntbCnt = CSL_EDMA3_CNT_MAKE(TEST_ACNT,TEST_BCNT);
paramSetup.srcDstBidx = CSL_EDMA3_BIDX_MAKE(TEST_ACNT,0);
paramSetup.srcDstCidx = CSL_EDMA3_CIDX_MAKE(0,0);
paramSetup.cCnt = TEST_CCNT;
paramSetup.option = CSL_EDMA3_OPT_MAKE(FALSE,TRUE,FALSE,TRUE,
CSL_EDMA3_CHA_ICXEVT,
CSL_EDMA3_TCC_NORMAL,
CSL_EDMA3_FIFOWIDTH_NONE,
FALSE, CSL_EDMA3_SYNC_A,
CSL_EDMA3_ADDRMODE_INCR,
CSL_EDMA3_ADDRMODE_INCR);
paramSetup.srcAddr = (Uint32)srcBuff;
paramSetup.dstAddr = (Uint32)&(hI2c->regs->ICDXR);
paramSetup.linkBcntrld = CSL_EDMA3_LINKBCNTRLD_MAKE(CSL_EDMA3_LINK_NULL,0);
CSL_edma3ParamSetup(paramHandle0,¶mSetup);
paramSetup.aCntbCnt = CSL_EDMA3_CNT_MAKE(TEST_ACNT,TEST_BCNT);
paramSetup.srcDstBidx = CSL_EDMA3_BIDX_MAKE(0,TEST_ACNT );
paramSetup.srcDstCidx = CSL_EDMA3_CIDX_MAKE(0,0);
paramSetup.cCnt = TEST_CCNT;
paramSetup.option = CSL_EDMA3_OPT_MAKE(FALSE,TRUE,FALSE,TRUE,
CSL_EDMA3_CHA_ICREVT,
CSL_EDMA3_TCC_NORMAL,
CSL_EDMA3_FIFOWIDTH_NONE,
FALSE, CSL_EDMA3_SYNC_A,
CSL_EDMA3_ADDRMODE_INCR,
CSL_EDMA3_ADDRMODE_INCR);
paramSetup.srcAddr = (Uint32)&(hI2c->regs->ICDRR);
paramSetup.dstAddr = (Uint32)dstBuff;
paramSetup.linkBcntrld = CSL_EDMA3_LINKBCNTRLD_MAKE(CSL_EDMA3_LINK_NULL,0);
CSL_edma3ParamSetup(paramHandle1,¶mSetup);
/* clear the EDMA error registers */
chErrClear.missed = TRUE;
chErrClear.secEvt = TRUE;
CSL_edma3HwChannelControl (hChannel0, CSL_EDMA3_CMD_CHANNEL_DISABLE, NULL);
CSL_edma3HwChannelControl (hChannel1, CSL_EDMA3_CMD_CHANNEL_DISABLE, NULL);
CSL_edma3HwChannelControl (hChannel0, CSL_EDMA3_CMD_CHANNEL_CLEARERR,
&chErrClear);
CSL_edma3HwChannelControl (hChannel1, CSL_EDMA3_CMD_CHANNEL_CLEARERR,
&chErrClear);
CSL_edma3HwChannelControl (hChannel0, CSL_EDMA3_CMD_CHANNEL_CLEAR, NULL);
CSL_edma3HwChannelControl (hChannel1, CSL_EDMA3_CMD_CHANNEL_CLEAR, NULL);
// Trigger channel
CSL_edma3HwChannelControl(hChannel0,CSL_EDMA3_CMD_CHANNEL_ENABLE,NULL);
// Trigger channel
CSL_edma3HwChannelControl(hChannel1,CSL_EDMA3_CMD_CHANNEL_ENABLE,NULL);
/* Start I2C */
if(CSL_SOK != CSL_i2cHwControl(hI2c, CSL_I2C_CMD_START, NULL)){
printf("I2C: Error while starting I2C.\n");
return;
}
/* Because quickturn is slow insert some delay */
for(i=0; i < 10000; i++)
asm("nop");
// Wait for interrupt
regionIntr.region = CSL_EDMA3_REGION_GLOBAL;
regionIntr.intr = 0;
regionIntr.intrh = 0;
do{
CSL_edma3GetHwStatus(hModule,CSL_EDMA3_QUERY_INTRPEND,®ionIntr);
// for(i=0; i < 1000; i++) asm("nop");
}while (!(regionIntr.intr & 0x300000));// channel 20 & 21
/* Stop the transmission */
if (CSL_i2cHwControl(hI2c, CSL_I2C_CMD_STOP, NULL) != CSL_SOK) {
return;
}
/* close instance of EDMA */
CSL_edma3ChannelClose(hChannel0);
CSL_edma3ChannelClose(hChannel1);
CSL_edma3Close(hModule);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -