static OsStatus_t KL25Z_I2CConfig(Device_t *dev, uint32_t expectedSpeed, uint32_t *actualSpeed, uint32_t configMask, uint16_t slaveAddr) { OsStatus_t ret = kStatusOk; KL25ZI2CDevData_t *dat = (KL25ZI2CDevData_t *)dev->config->devConfigData; KL25ZCustomI2CData_t *custom = (KL25ZCustomI2CData_t *)dev->deviceData; DeviceConfig_t *dcfg = (DeviceConfig_t *)dev->config; (void)actualSpeed; if(custom->busy) { /* device busy, exit */ ret = kDeviceBusy; goto cleanup; } custom->busy = true; /* select the mode of I2C operation */ if(configMask & I2C_DEVICE_SLAVE) { custom->master = false; I2C_SlaveTransferCreateHandle(dat->I2C, &custom->shnadle,KL25Z_SlaveI2CIsr , dev); custom->enabled = false; i2c_slave_config_t cfg; I2C_SlaveGetDefaultConfig(&cfg); cfg.enableHighDrive = false; /* perform addressing mode setup */ if(configMask & I2C_10BIT_ADDR) { cfg.addressingMode = kI2C_Address7bit; } else { cfg.addressingMode = kI2C_RangeMatch; } cfg.slaveAddress = slaveAddr; /* perfom I2C configuration */ I2C_SlaveInit(dat->I2C,&cfg,OS_CPU_RATE/2); } else { custom->master = true; I2C_MasterTransferCreateHandle(dat->I2C, &custom->mhandle, KL25Z_MasterI2CIsr, dev); custom->enabled = false; i2c_master_config_t cfg; I2C_MasterGetDefaultConfig(&cfg); cfg.baudRate_Bps = expectedSpeed; cfg.enableMaster = false; /* perfom I2C configuration */ I2C_MasterInit(dat->I2C,&cfg,OS_CPU_RATE/2); } /* keeps the I2C disabled */ NVIC_ClearPendingIRQ(dcfg->irqOffset); NVIC_DisableIRQ(dcfg->irqOffset); I2C_SlaveClearStatusFlags(dat->I2C, 0xFFFFFFFF); I2C_DisableInterrupts(dat->I2C, 0xFFFFFFFF); custom->busy = false; cleanup: return(ret); }
int main(void) { i2c_slave_config_t slaveConfig; i2c_master_config_t masterConfig; uint32_t sourceClock; i2c_master_transfer_t masterXfer; BOARD_InitPins(); BOARD_BootClockRUN(); BOARD_InitDebugConsole(); /*Init DMA for example*/ DMAMGR_Init(); /* set master priority lower than slave */ NVIC_EnableIRQ(DMA0_IRQn); NVIC_SetPriority(DMA0_IRQn, 1); PRINTF("\r\nI2C example -- MasterDMA_SlaveInterrupt.\r\n"); /*1.Set up i2c slave first*/ /* * slaveConfig.addressingMode = kI2C_Address7bit; * slaveConfig.enableGeneralCall = false; * slaveConfig.enableWakeUp = false; * slaveConfig.enableHighDrive = false; * slaveConfig.enableBaudRateCtl = false; * slaveConfig.enableSlave = true; */ I2C_SlaveGetDefaultConfig(&slaveConfig); slaveConfig.addressingMode = kI2C_Address7bit; slaveConfig.slaveAddress = I2C_MASTER_SLAVE_ADDR_7BIT; slaveConfig.upperAddress = 0; I2C_SlaveInit(EXAMPLE_I2C_SLAVE_BASEADDR, &slaveConfig); for (uint32_t i = 0U; i < I2C_DATA_LENGTH; i++) { g_slave_buff[i] = 0; } memset(&g_s_handle, 0, sizeof(g_s_handle)); I2C_SlaveTransferCreateHandle(EXAMPLE_I2C_SLAVE_BASEADDR, &g_s_handle, i2c_slave_callback, NULL); I2C_SlaveTransferNonBlocking(EXAMPLE_I2C_SLAVE_BASEADDR, &g_s_handle, kI2C_SlaveCompletionEvent); /*2.Set up i2c master to send data to slave*/ for (uint32_t i = 0U; i < I2C_DATA_LENGTH; i++) { g_master_buff[i] = i; } PRINTF("Master will send data :"); for (uint32_t i = 0U; i < I2C_DATA_LENGTH; i++) { if (i % 8 == 0U) { PRINTF("\r\n"); } PRINTF("0x%2x ", g_master_buff[i]); } PRINTF("\r\n\r\n"); /* * masterConfig.baudRate_Bps = 100000U; * masterConfig.enableHighDrive = false; * masterConfig.enableStopHold = false; * masterConfig.glitchFilterWidth = 0U; * masterConfig.enableMaster = true; */ I2C_MasterGetDefaultConfig(&masterConfig); masterConfig.baudRate_Bps = I2C_BAUDRATE; sourceClock = CLOCK_GetFreq(I2C_MASTER_CLK_SRC); I2C_MasterInit(EXAMPLE_I2C_MASTER_BASEADDR, &masterConfig, sourceClock); memset(&g_m_dma_handle, 0, sizeof(g_m_dma_handle)); memset(&masterXfer, 0, sizeof(masterXfer)); masterXfer.slaveAddress = I2C_MASTER_SLAVE_ADDR_7BIT; masterXfer.direction = kI2C_Write; masterXfer.subaddress = 0; masterXfer.subaddressSize = 0; masterXfer.data = g_master_buff; masterXfer.dataSize = I2C_DATA_LENGTH; masterXfer.flags = kI2C_TransferDefaultFlag; DMAMGR_RequestChannel((dma_request_source_t)DMA_REQUEST_SRC, 0, &dmaHandle); I2C_MasterTransferCreateHandleDMA(EXAMPLE_I2C_MASTER_BASEADDR, &g_m_dma_handle, NULL, NULL, &dmaHandle); I2C_MasterTransferDMA(EXAMPLE_I2C_MASTER_BASEADDR, &g_m_dma_handle, &masterXfer); /* wait for transfer completed. */ while (!completionFlag) { } completionFlag = false; /*3.Transfer completed. Check the data.*/ for (uint32_t i = 0U; i < I2C_DATA_LENGTH; i++) { if (g_slave_buff[i] != g_master_buff[i]) { PRINTF("\r\nError occured in this transfer ! \r\n"); break; } } PRINTF("Slave received data :"); for (uint32_t i = 0U; i < I2C_DATA_LENGTH; i++) { if (i % 8 == 0) { PRINTF("\r\n"); } PRINTF("0x%2x ", g_slave_buff[i]); } PRINTF("\r\n\r\n"); /*4.Set up slave ready to send data to master.*/ for (uint32_t i = 0U; i < I2C_DATA_LENGTH; i++) { g_slave_buff[i] = ~g_slave_buff[i]; } PRINTF("This time , slave will send data: :"); for (uint32_t i = 0U; i < I2C_DATA_LENGTH; i++) { if (i % 8 == 0) { PRINTF("\r\n"); } PRINTF("0x%2x ", g_slave_buff[i]); } PRINTF("\r\n\r\n"); /* Already setup the slave transfer in item 1 */ /*5.Set up master to receive data from slave.*/ for (uint32_t i = 0U; i < I2C_DATA_LENGTH; i++) { g_master_buff[i] = 0; } masterXfer.slaveAddress = I2C_MASTER_SLAVE_ADDR_7BIT; masterXfer.direction = kI2C_Read; masterXfer.subaddress = 0; masterXfer.subaddressSize = 0; masterXfer.data = g_master_buff; masterXfer.dataSize = I2C_DATA_LENGTH; masterXfer.flags = kI2C_TransferDefaultFlag; I2C_MasterTransferDMA(EXAMPLE_I2C_MASTER_BASEADDR, &g_m_dma_handle, &masterXfer); /* wait for transfer completed. */ while (!completionFlag) { } completionFlag = false; /*6.Transfer completed. Check the data.*/ for (uint32_t i = 0U; i < I2C_DATA_LENGTH; i++) { if (g_slave_buff[i] != g_master_buff[i]) { PRINTF("\r\nError occured in the transfer ! \r\n"); break; } } PRINTF("Master received data :"); for (uint32_t i = 0U; i < I2C_DATA_LENGTH; i++) { if (i % 8 == 0) { PRINTF("\r\n"); } PRINTF("0x%2x ", g_master_buff[i]); } PRINTF("\r\n\r\n"); PRINTF("\r\nEnd of I2C example .\r\n"); while (1) { } }