Ejemplo n.º 1
0
int davinci_i2c_init(char *bus_name)
{
	struct rt_i2c_bus_device *bus;
	struct davinci_i2c_dev *dev;
	int r;

	bus = rt_malloc(sizeof(struct rt_i2c_bus_device));
	if (bus == RT_NULL)
	{
		rt_kprintf("rt_malloc failed\n");
		return -RT_ENOMEM;
	}
	
	rt_memset((void *)bus, 0, sizeof(struct rt_i2c_bus_device));

	bus->ops = &bus_ops;
	bus->timeout = DAVINCI_I2C_TIMEOUT;

	dev = rt_malloc(sizeof(struct davinci_i2c_dev));
	if (!dev) 
	{
		r = -RT_ENOMEM;
		goto err;
	}

	rt_memset((void *)dev, 0, sizeof(struct davinci_i2c_dev));

	rt_sem_init(&dev->completion, "i2c_ack", 0, RT_IPC_FLAG_FIFO);

	dev->irq = IRQ_I2C;

	dev->clk = clk_get("I2CCLK");
	if (dev->clk == RT_NULL) {
		r = -RT_ERROR;
		goto err1;
	}

	psc_change_state(DAVINCI_DM365_LPSC_I2C, 3);

	dev->base = DAVINCI_I2C_BASE;
	dev->bus_freq = 100;
	dev->bus_delay = 0;

	bus->priv = dev;

	i2c_davinci_init(dev);

	rt_hw_interrupt_install(dev->irq, i2c_davinci_isr, (void *)dev, "I2C");
	rt_hw_interrupt_umask(dev->irq);

	return rt_i2c_bus_device_register(bus, bus_name);

err1:
	rt_free(dev);

err:
	rt_free(bus);

	return r;
}
Ejemplo n.º 2
0
void rt_hw_i2c1_init(void)
{
	struct rt_i2c_bus_device *bus;
	
	RCC_Configuration();
	NVIC_Configuration();
    GPIO_Configuration();
	
	bus=&i2c_bus1;
    rt_memset(bus, 0, sizeof(struct rt_i2c_bus_device));
	bus->ops = &i2c1_ops;
	
	rt_sem_init(&DMA_TX_Sem,"i2c_tx",0,RT_IPC_FLAG_FIFO);
	rt_sem_init(&DMA_RX_Sem,"i2c_rx",0,RT_IPC_FLAG_FIFO);
#ifdef NO_RT_DEVICE
	rt_mutex_init(&I2C1_mutex,"i2c1_m",RT_IPC_FLAG_FIFO);
#endif
	
	rt_i2c_bus_device_register(bus,"i2c1");
}
Ejemplo n.º 3
0
int rt_hw_i2c_init(void)
{
    GPIO_InitTypeDef  GPIO_InitStructure;

#ifdef RT_USING_I2C_BITOPS
    RCC_AHB1PeriphClockCmd(RCC_I2C_SCL | RCC_I2C_SDA, ENABLE);

    /* config SCL PIN */
    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_OUT;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
    GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;

    GPIO_InitStructure.GPIO_Pin = PIN_I2C_SCL;
    GPIO_SetBits(GPIO_PORT_I2C_SCL, PIN_I2C_SCL);
    GPIO_Init(GPIO_PORT_I2C_SCL, &GPIO_InitStructure);

    /* config SDA PIN */
    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_OUT;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
    GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;

    GPIO_InitStructure.GPIO_Pin = PIN_I2C_SDA;
    GPIO_SetBits(GPIO_PORT_I2C_SDA, PIN_I2C_SDA);
    GPIO_Init(GPIO_PORT_I2C_SDA, &GPIO_InitStructure);

    rt_memset((void *)&i2c_device, 0, sizeof(struct rt_i2c_bus_device));
    i2c_device.priv = (void *)&bit_ops;
    rt_i2c_bit_add_bus(&i2c_device, "i2c1");

#else
    I2C_InitTypeDef  I2C_InitStructure;

    /*!< sEE_I2C Periph clock enable */
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);

    /*!< sEE_I2C_SCL_GPIO_CLK and sEE_I2C_SDA_GPIO_CLK Periph clock enable */
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);

    /* Reset sEE_I2C IP */
    RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, ENABLE);

    /* Release reset signal of sEE_I2C IP */
    RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, DISABLE);

    /*!< GPIO configuration */
    /*!< Configure sEE_I2C pins: SCL */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
    GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP;
    GPIO_Init(GPIOB, &GPIO_InitStructure);

    /*!< Configure sEE_I2C pins: SDA */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
    GPIO_Init(GPIOB, &GPIO_InitStructure);

    /* Connect PXx to I2C_SCL*/
    GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_I2C1);

    /* Connect PXx to I2C_SDA*/
    GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_I2C1);


    /*!< I2C configuration */
    /* sEE_I2C configuration */
    I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
    I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
//	I2C_InitStructure.I2C_OwnAddress1 = SLA_ADDRESS;
    I2C_InitStructure.I2C_OwnAddress1 = 0x18;
    I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
    I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
    I2C_InitStructure.I2C_ClockSpeed = 400000;

    /* Apply sEE_I2C configuration after enabling it */
    I2C_Init(I2C1, &I2C_InitStructure);
    /* sEE_I2C Peripheral Enable */
    I2C_Cmd(I2C1, ENABLE);

    rt_memset((void *)&stm32_i2c1, 0, sizeof(struct rt_i2c_bus_device));
    stm32_i2c1.ops = &i2c1_ops;

    rt_i2c_bus_device_register(&stm32_i2c1, "i2c1");
#endif

	return 0;
}