示例#1
0
void i2c_slave_mode(i2c_t *obj, int enable_slave) {
    i2c_slave_config_t slave_config;
    I2C_SlaveGetDefaultConfig(&slave_config);
    slave_config.slaveAddress = 0;
    slave_config.enableSlave = (bool)enable_slave;
    I2C_SlaveInit(i2c_addrs[obj->instance], &slave_config);
}
/*!
 * @brief Main function
 */
int main(void)
{
    i2c_slave_config_t slaveConfig;

    i2c_master_config_t masterConfig;
    uint32_t sourceClock;

    BOARD_InitPins();
    BOARD_BootClockRUN();
    BOARD_InitDebugConsole();

    PRINTF("\r\nI2C example -- MasterFunctionalInterrupt_SlaveFunctionalInterrupt.\r\n");

    /*  Enable master and slave NVIC interrupt. */
    EnableIRQ(I2C_MASTER_IRQ);
    EnableIRQ(I2C_SLAVE_IRQ);

    /* Set i2c slave interrupt priority higher. */
    NVIC_SetPriority(I2C_SLAVE_IRQ, 0);
    NVIC_SetPriority(I2C_MASTER_IRQ, 1);

    /*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;

    I2C_SlaveInit(EXAMPLE_I2C_SLAVE_BASEADDR, &slaveConfig);

    for (uint32_t i = 0U; i < I2C_DATA_LENGTH; i++)
    {
        g_slave_buff[i] = 0;
    }

    /*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 == 0)
        {
            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);

    /* Master send address to slave. */
    I2C_MasterStart(EXAMPLE_I2C_MASTER_BASEADDR, I2C_MASTER_SLAVE_ADDR_7BIT, kI2C_Write);

    /* Enable module interrupt. */
    I2C_EnableInterrupts(EXAMPLE_I2C_MASTER_BASEADDR, kI2C_GlobalInterruptEnable);
    I2C_EnableInterrupts(EXAMPLE_I2C_SLAVE_BASEADDR, kI2C_GlobalInterruptEnable);

    /* Wait slave receive finished. */
    while (g_slaveRxIndex < I2C_DATA_LENGTH)
    {
    }

    /* Disable module interrupt. */
    I2C_DisableInterrupts(EXAMPLE_I2C_MASTER_BASEADDR, kI2C_GlobalInterruptEnable);
    I2C_DisableInterrupts(EXAMPLE_I2C_SLAVE_BASEADDR, kI2C_GlobalInterruptEnable);

    /* Master send stop command. */
    I2C_MasterStop(EXAMPLE_I2C_MASTER_BASEADDR);

    /*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 ready 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;
    }

    /* Master send address to slave. */
    I2C_MasterStart(EXAMPLE_I2C_MASTER_BASEADDR, I2C_MASTER_SLAVE_ADDR_7BIT, kI2C_Read);

    /* Enable module interrupt. */
    I2C_EnableInterrupts(EXAMPLE_I2C_MASTER_BASEADDR, kI2C_GlobalInterruptEnable);
    I2C_EnableInterrupts(EXAMPLE_I2C_SLAVE_BASEADDR, kI2C_GlobalInterruptEnable);

    g_masterReadBegin = true;

    /* Master put receive data in receive buffer. */
    g_masterRxIndex = 0;

    /* Wait master receive finished. */
    while (g_masterRxIndex < I2C_DATA_LENGTH)
    {
    }

    /* Disable module interrupt. */
    I2C_DisableInterrupts(EXAMPLE_I2C_MASTER_BASEADDR, kI2C_GlobalInterruptEnable);
    I2C_DisableInterrupts(EXAMPLE_I2C_SLAVE_BASEADDR, kI2C_GlobalInterruptEnable);

    /*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)
    {
    }
}
示例#3
0
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);
}
示例#4
0
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)
    {
    }
}
示例#5
0
void BootLoader( int mode )
{
    uint8_t beats;
    uint8_t bootDelay;
    uint8_t beatRate;

#if BOOT_LOG_ENABLED
    FILE   *logFs;
#endif

    uint8_t beatCounter = 0;

#if defined( CFG_FORCE_MASK )

    // We configure the pullup as early as we can, to give the pin a chance
    // to respond.

    CFG_FORCE_DDR  &= ~CFG_FORCE_MASK;  // Configure pin for input
    CFG_FORCE_PORT |= CFG_FORCE_MASK;   // Enable Pullup
#endif

    cli();

#if ( !STANDALONE )

    // For the BootLoader, we want the interrupts to goto the BootLoader
    // interrupt vector. Do the special dance.

#if defined( GICR )
    GICR = ( 1 << IVCE );
    GICR = ( 1 << IVSEL );
#else
    MCUCR = ( 1 << IVCE );
    MCUCR = ( 1 << IVSEL );
#endif
#endif // STANDALONE

#if CFG_USE_UART && 0
    InitHardware();
#else
    InitTimer();
#endif

#if BOOT_LOG_ENABLED
    logFs = fdevopen( UART0_PutCharStdio, NULL );

    LogInit( logFs );
#endif

#if defined( CFG_I2C_LED_MASK )
    CFG_I2C_LED_DDR         |= CFG_I2C_LED_MASK;
#endif
#if defined( CFG_BOOTLOADER_BEAT_MASK )
    CFG_BOOTLOADER_BEAT_DDR |= CFG_BOOTLOADER_BEAT_MASK;
#endif

    // Read our i2c address from the EEPROM

    eeprom_read_block( &gEeprom, (void *)( E2END + 1 - sizeof( gEeprom )), sizeof( gEeprom ));

    // Check to see if the FORCE strap has been installed

    if (( gEeprom.structSize == 0xff ) || ( gEeprom.i2cAddr > 0x7f ))
    {
        // EEPROM is uninitialized. Use default data

        gEeprom.i2cAddr = BL_DEFAULT_I2C_ADDR;
    }
    if (( gEeprom.structSize == 0xff ) || ( gEeprom.bootDelay == 0xff ))
    {
        gEeprom.bootDelay = BL_DEFAULT_BOOT_DELAY;
    }

    I2C_SlaveInit( &gI2cGlobals, gEeprom.i2cAddr, ProcessCommand );

    memset( &gBootLoaderGlobals, 0, sizeof( gBootLoaderGlobals ));
    gBootLoaderGlobals.m_pageAddress = ~0uL;

    // Set gStayInBootLoader to zero before we enable interripts. We do this
    // since the interrupt handler will set it to 1 if an Info command is received.

    gStayInBootloader = 0;
    if (( pgm_read_byte_near( 0x0000 ) == 0xFF ) || ( mode == BOOT_MODE_APP ))
    {
        // No user mode program has been downloaded. We really have no choice but to stay in
        // the bootloader.

        gStayInBootloader = 1;
    }

    sei();

    BOOT_LOG0( "\n" );
    BOOT_LOG0( "*****\n" );
    BOOT_LOG2( "***** I2C BootLoader i2cAddr: 0x%02x bootDelay:%d\n", gEeprom.i2cAddr, gEeprom.bootDelay );
    BOOT_LOG0( "*****\n" );

    bootDelay = gEeprom.bootDelay;
    beats = 0;

    // gStayInBootloader is set if the BL_CMD_GET_INFO packet is received or 
    // no user program is found in flash.

    beatRate = 100; // Toggle every 100 msec

// By default, we pick Port E1 (TXD) as the override pin. For the normal bootloader
// we don't use the UART, so this is fine.

#if ( defined( CFG_FORCE_MASK ) && !CFG_USE_UART )

    // We split the test away from the configuration of the pin to give
    // things a chance to settle.   

    gStayInBootloader |= (( CFG_FORCE_PIN & CFG_FORCE_MASK ) == 0 );

#endif

    while ( gStayInBootloader || ( bootDelay > 0 ))
    {
        tick_t prevCount;

        if ( gStayInBootloader  )
        {
            beatRate = 250;
        }

        if ( ++beatCounter >= beatRate )
        {
            beatCounter = 0;

            CFG_BOOTLOADER_BEAT_PORT ^= CFG_BOOTLOADER_BEAT_MASK;

            if ( ++beats >= 10 )
            {
                // This branch taken once per second

                beats = 0;
                if ( bootDelay > 0 )
                {
                    bootDelay--;
                }
            }
        }

        prevCount = gTickCount;
        while ( gTickCount == prevCount )
        {
#if BOOT_LOG_ENABLED
            LogBufDump();
#endif
        }
    }

    RunUserProgram();

} // BootLoader