Пример #1
0
void HAL_I2C_Stretch_Clock(HAL_I2C_Interface i2c, bool stretch, void* reserved)
{
    if(stretch == true)
    {
        I2C_StretchClockCmd(i2cMap[i2c]->I2C_Peripheral, ENABLE);
    }
    else
    {
        I2C_StretchClockCmd(i2cMap[i2c]->I2C_Peripheral, DISABLE);
    }
}
Пример #2
0
//______________________________________________________________________________________
_i2c 			*Initialize_I2C(int addr, int speed)
{
I2C_InitTypeDef I2C_InitStructure;
GPIO_InitTypeDef	GPIO_InitStructure;
_i2c			*p=calloc(1,sizeof(_i2c));
					if(addr)
						p->addr=addr;
					if(speed)
						p->speed=speed;

					GPIO_StructInit(&GPIO_InitStructure);

					GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
					GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
					GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
					GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
					GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
					GPIO_Init(GPIOB, &GPIO_InitStructure);

					GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_I2C1);
					GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_I2C1);

					RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
					RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, ENABLE);
					RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, DISABLE); 
					
					I2C_DeInit(I2C1);

					I2C_StructInit(&I2C_InitStructure);
					I2C_InitStructure.I2C_Mode = I2C_Mode_SMBusHost;
					I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
					I2C_InitStructure.I2C_OwnAddress1 = 0x00;
					I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
					I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
					I2C_InitStructure.I2C_ClockSpeed = speed;
					I2C_Init(I2C1, &I2C_InitStructure);

					I2C_StretchClockCmd(I2C1, ENABLE);
					I2C_ARPCmd(I2C1, ENABLE);
					I2C_AcknowledgeConfig(I2C1, ENABLE);	
					I2C_Cmd(I2C1, ENABLE);
					I2C_ITConfig(I2C1, I2C_IT_ERR , ENABLE);

					return p;
}
Пример #3
0
int I2C_config(i2c_bus *bus, u32_t clock) {
  I2C_InitTypeDef I2C_InitStruct;
  I2C_StructInit(&I2C_InitStruct);
  I2C_Cmd(I2C_HW(bus), DISABLE);
  I2C_DeInit(I2C_HW(bus));

  I2C_InitStruct.I2C_Mode = I2C_Mode_I2C;
  I2C_InitStruct.I2C_DutyCycle = I2C_DutyCycle_2;
  I2C_InitStruct.I2C_OwnAddress1 = 0x00; // never mind, will be master
  I2C_InitStruct.I2C_Ack = I2C_Ack_Enable;
  I2C_InitStruct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
  I2C_InitStruct.I2C_ClockSpeed = clock;


  I2C_Init(I2C_HW(bus), &I2C_InitStruct);

  I2C_AcknowledgeConfig(I2C_HW(bus), ENABLE);
  I2C_Cmd(I2C_HW(bus), ENABLE);
  I2C_StretchClockCmd(I2C_HW(bus), ENABLE);

  return I2C_OK;
}
Пример #4
0
void i2cInit(I2CDevice device)
{

    i2cDevice_t *i2c;
    i2c = &(i2cHardwareMap[device]);

    I2C_TypeDef *I2Cx;
    I2Cx = i2c->dev;
  
    IO_t scl = IOGetByTag(i2c->scl);
    IO_t sda = IOGetByTag(i2c->sda);

    RCC_ClockCmd(i2c->rcc, ENABLE);
    RCC_I2CCLKConfig(I2Cx == I2C2 ? RCC_I2C2CLK_SYSCLK : RCC_I2C1CLK_SYSCLK);

    IOInit(scl, OWNER_I2C, RESOURCE_I2C_SCL, RESOURCE_INDEX(device));
    IOConfigGPIOAF(scl, IOCFG_I2C, GPIO_AF_4);

    IOInit(sda, OWNER_I2C, RESOURCE_I2C_SDA, RESOURCE_INDEX(device));
    IOConfigGPIOAF(sda, IOCFG_I2C, GPIO_AF_4);

    I2C_InitTypeDef i2cInit = {
        .I2C_Mode = I2C_Mode_I2C,
        .I2C_AnalogFilter = I2C_AnalogFilter_Enable,
        .I2C_DigitalFilter = 0x00,
        .I2C_OwnAddress1 = 0x00,
        .I2C_Ack = I2C_Ack_Enable,
        .I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit,
        .I2C_Timing = (i2c->overClock ? I2C_HIGHSPEED_TIMING : I2C_STANDARD_TIMING)
    };

    I2C_Init(I2Cx, &i2cInit);

    I2C_StretchClockCmd(I2Cx, ENABLE);
 
    I2C_Cmd(I2Cx, ENABLE);
}

uint16_t i2cGetErrorCounter(void)
{
    return i2cErrorCount;
}

bool i2cWrite(I2CDevice device, uint8_t addr_, uint8_t reg, uint8_t data)
{
    addr_ <<= 1;

    I2C_TypeDef *I2Cx;
    I2Cx = i2cHardwareMap[device].dev;

    /* Test on BUSY Flag */
    i2cTimeout = I2C_LONG_TIMEOUT;
    while (I2C_GetFlagStatus(I2Cx, I2C_ISR_BUSY) != RESET) {
        if ((i2cTimeout--) == 0) {
            return i2cTimeoutUserCallback();
        }
    }

        /* Configure slave address, nbytes, reload, end mode and start or stop generation */
    I2C_TransferHandling(I2Cx, addr_, 1, I2C_Reload_Mode, I2C_Generate_Start_Write);

        /* Wait until TXIS flag is set */
    i2cTimeout = I2C_LONG_TIMEOUT;
    while (I2C_GetFlagStatus(I2Cx, I2C_ISR_TXIS) == RESET) {
        if ((i2cTimeout--) == 0) {
            return i2cTimeoutUserCallback();
        }
    }

        /* Send Register address */
    I2C_SendData(I2Cx, (uint8_t) reg);

        /* Wait until TCR flag is set */
    i2cTimeout = I2C_LONG_TIMEOUT;
    while (I2C_GetFlagStatus(I2Cx, I2C_ISR_TCR) == RESET)
    {
        if ((i2cTimeout--) == 0) {
            return i2cTimeoutUserCallback();
        }
    }

        /* Configure slave address, nbytes, reload, end mode and start or stop generation */
    I2C_TransferHandling(I2Cx, addr_, 1, I2C_AutoEnd_Mode, I2C_No_StartStop);

        /* Wait until TXIS flag is set */
    i2cTimeout = I2C_LONG_TIMEOUT;
    while (I2C_GetFlagStatus(I2Cx, I2C_ISR_TXIS) == RESET) {
        if ((i2cTimeout--) == 0) {
            return i2cTimeoutUserCallback();
        }
    }

        /* Write data to TXDR */
    I2C_SendData(I2Cx, data);

        /* Wait until STOPF flag is set */
    i2cTimeout = I2C_LONG_TIMEOUT;
    while (I2C_GetFlagStatus(I2Cx, I2C_ISR_STOPF) == RESET) {
        if ((i2cTimeout--) == 0) {
            return i2cTimeoutUserCallback();
        }
    }

        /* Clear STOPF flag */
    I2C_ClearFlag(I2Cx, I2C_ICR_STOPCF);

    return true;
}
Пример #5
0
void i2cInit(I2CDevice device)
{
    if (device == I2CINVALID)
        return;

    i2cDevice_t *i2c;
    i2c = &(i2cHardwareMap[device]);

    NVIC_InitTypeDef nvic;
    I2C_InitTypeDef i2cInit;

    IO_t scl = IOGetByTag(i2c->scl);
    IO_t sda = IOGetByTag(i2c->sda);

    IOInit(scl, OWNER_I2C_SCL, RESOURCE_INDEX(device));
    IOInit(sda, OWNER_I2C_SDA, RESOURCE_INDEX(device));

    // Enable RCC
    RCC_ClockCmd(i2c->rcc, ENABLE);

    I2C_ITConfig(i2c->dev, I2C_IT_EVT | I2C_IT_ERR, DISABLE);

    i2cUnstick(scl, sda);

    // Init pins
#ifdef STM32F4
    IOConfigGPIOAF(scl, IOCFG_I2C, GPIO_AF_I2C);
    IOConfigGPIOAF(sda, IOCFG_I2C, GPIO_AF_I2C);
#else
    IOConfigGPIO(scl, IOCFG_I2C);
    IOConfigGPIO(sda, IOCFG_I2C);
#endif

    I2C_DeInit(i2c->dev);
    I2C_StructInit(&i2cInit);

    I2C_ITConfig(i2c->dev, I2C_IT_EVT | I2C_IT_ERR, DISABLE);               // Disable EVT and ERR interrupts - they are enabled by the first request
    i2cInit.I2C_Mode = I2C_Mode_I2C;
    i2cInit.I2C_DutyCycle = I2C_DutyCycle_2;
    i2cInit.I2C_OwnAddress1 = 0;
    i2cInit.I2C_Ack = I2C_Ack_Enable;
    i2cInit.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;

    if (i2c->overClock) {
        i2cInit.I2C_ClockSpeed = 800000; // 800khz Maximum speed tested on various boards without issues
    } else {
        i2cInit.I2C_ClockSpeed = 400000; // 400khz Operation according specs
    }

    I2C_Cmd(i2c->dev, ENABLE);
    I2C_Init(i2c->dev, &i2cInit);

    I2C_StretchClockCmd(i2c->dev, ENABLE);


    // I2C ER Interrupt
    nvic.NVIC_IRQChannel = i2c->er_irq;
    nvic.NVIC_IRQChannelPreemptionPriority = NVIC_PRIORITY_BASE(NVIC_PRIO_I2C_ER);
    nvic.NVIC_IRQChannelSubPriority = NVIC_PRIORITY_SUB(NVIC_PRIO_I2C_ER);
    nvic.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&nvic);

    // I2C EV Interrupt
    nvic.NVIC_IRQChannel = i2c->ev_irq;
    nvic.NVIC_IRQChannelPreemptionPriority = NVIC_PRIORITY_BASE(NVIC_PRIO_I2C_EV);
    nvic.NVIC_IRQChannelSubPriority = NVIC_PRIORITY_SUB(NVIC_PRIO_I2C_EV);
    NVIC_Init(&nvic);
}
void sens9DInit(void){
	//PB10/I2C2_SCL, PB11/I2C2_SDA
	// XM Read Address 8bit 0x3B, Gyro 8bit Read 0xD7
	GPIO_InitTypeDef      GPIO_InitStructure;
	I2C_InitTypeDef  I2C_InitStructure;
	u8 statOk=!0;
	int i;



	I2C_DeInit(I2C2);
	I2C_Cmd(I2C2, DISABLE);
	I2C_SoftwareResetCmd(I2C2, ENABLE);
	I2C_SoftwareResetCmd(I2C2, DISABLE);

	RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2,ENABLE);
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE);


	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10|GPIO_Pin_11;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
	GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;
	GPIO_Init(GPIOB, &GPIO_InitStructure);

	GPIOB->BSRRL=GPIO_Pin_10|GPIO_Pin_11;

	i=8;
	while( (i--) && ((GPIOB->IDR&GPIO_Pin_11)==0)){
		delay_us(200);
		GPIOB->BSRRH=GPIO_Pin_10;
		delay_us(200);
		GPIOB->BSRRL=GPIO_Pin_10;
	}

	GPIO_PinAFConfig(GPIOB,GPIO_PinSource10, GPIO_AF_I2C2);
	GPIO_PinAFConfig(GPIOB,GPIO_PinSource11, GPIO_AF_I2C2);
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	GPIO_Init(GPIOB, &GPIO_InitStructure);

	I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
	I2C_InitStructure.I2C_OwnAddress1 = 0x01;
	I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
	I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
	I2C_InitStructure.I2C_ClockSpeed=100e3;
	I2C_InitStructure.I2C_DutyCycle=I2C_DutyCycle_16_9;
	I2C_Init(I2C2, &I2C_InitStructure);
	I2C_StretchClockCmd(I2C2,ENABLE);
	//I2C_GeneralCallCmd(I2C1,ENABLE);
	//I2C_AnalogFilterCmd(I2C2,ENABLE);
	//I2C_DigitalFilterConfig(I2C2,0x02); Not available on 407.. :(
	I2C_Cmd(I2C2, ENABLE);


	delay_us(1e3);

	//CTRL_REG1_G to CTRL_REG5_G 0x20..0x24
	//190Hz, 25Hz cut
	//0.45Hz HPass
	//Full scale 2000dps
	//HPass ΝΟΤ selected
	//					20h			21h				22h			23h			24h
	u8 GyroCTRreg[5]={0b01111111, 0b00100101,	0b00000000,	0b10100000,	0b00000000 };//24h ->0b00000000 disables LPF2 //Enable 0b00000010
	I2C_write(Gy_addr,0x20|0x80,GyroCTRreg,5);
	// HP filter bypassed
	//					1Fh			20h			21h			22h			23h			24h			25h			26h
	u8 XM_CTRreg[8]={0b00000000, 0b01111111, 0b11001000, 0b00000000, 0b00000000, 0b11110100, 0b00000000, 0b00000000 };//200Hz, 50HzBW, 4G, Magn:100Hz,2Gauss
	I2C_write(XM_addr,0x1F|0x80,XM_CTRreg,8);

}
Пример #7
0
/*************************************************************
 * I2C initialization code template
**************************************************************/
void I2C1_Init()
{
    #if (STRCMP($interruptEn$, 0) == 0)
    NVIC_InitTypeDef NVIC_InitStructure;
    #endif
    I2C_InitTypeDef I2C_InitStructure;
	
    //PUT_A_NEW_LINE_HERE
    //
    // Enable I2C clock
    //
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);

    #if (STRCMP($SCL1$, DISABLE) == 0 || STRCMP($SDA1$, DISABLE) == 0)
    //PUT_A_NEW_LINE_HERE
    //
    // Enable AFIO and I2C clock
    //
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);	
    //PUT_A_NEW_LINE_HERE
    //
    // I2C SCL pin remap to PB8, I2C SDA pin remap to PB8
    //
    GPIO_PinRemapConfig(GPIO_Remap_I2C1,ENABLE);
	#endif

    //PUT_A_NEW_LINE_HERE
    //
    // I2C config
    //
    I2C_InitStructure.I2C_Mode = $mode$;
    I2C_InitStructure.I2C_DutyCycle = $dutyCycle$;
    I2C_InitStructure.I2C_OwnAddress1 = $ownAddress$;
    I2C_InitStructure.I2C_Ack = $ack$;
    I2C_InitStructure.I2C_AcknowledgedAddress = $ackAddress$;
    I2C_InitStructure.I2C_ClockSpeed = $speed$;
    I2C_Init($I2C$, &I2C_InitStructure);

    //PUT_A_NEW_LINE_HERE
    //
    // Enable I2C
    //
    I2C_Cmd($I2C$, ENABLE);
    I2C_Init($I2C$, &I2C_InitStructure);
	
    #if (STRCMP($interruptEn$, 0) == 0)	
    //PUT_A_NEW_LINE_HERE
    //
    // Enable I2C DualAddress
    //
    I2C_OwnAddress2Config($I2C$, $ownAddress2$);
    I2C_DualAddressCmd($I2C$, ENABLE);
    #endif
	
    #if($modeSelect$)
    //PUT_A_NEW_LINE_HERE
    //
    // Transmits the address byte to select the slave device
    //
    I2C_Send7bitAddress($I2C$, $slaveAddress$, $transferDirection$);
    #endif
	
    #if(STRCMP($GenCallEn$, DISABLE) == 0)
    //PUT_A_NEW_LINE_HERE
    //
    // Enables I2C1 general call feature
    //
    I2C_GeneralCallCmd($I2C$, ENABLE);
    #endif

    #if(STRCMP($StrchClkEn$, DISABLE) == 0)
    //PUT_A_NEW_LINE_HERE
    //
    // Enables I2C1 Clock stretching
    //
    I2C_StretchClockCmd($I2C$, ENABLE);
    #endif
	
    #if (STRCMP($interruptEn$, 0) == 0)
    //PUT_A_NEW_LINE_HERE
    //
    // Interrupt config
    //
    I2C_ITConfig($I2C$, $interruptEn$, ENABLE);
    NVIC_InitStructure.NVIC_IRQChannel = I2C1_EV_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
    #endif

    #if($pecTransmitEn$)
    //PUT_A_NEW_LINE_HERE
    //
    // PEC Transmit Enable
    //
    #if($pecTransmitEn$)
    I2C_TransmitPEC($I2C$, ENABLE);
    #endif
    #if($pecCalculateEn$)
    I2C_CalculatePEC($I2C$, ENABLE);
    #endif   
    #if(STRCMP($pecPosition$, I2C_PECPosition_Next))
    I2C_PECPositionConfig($I2C$, $pecPosition$);
    #endif  
    #if(STRCMP($pecPosition$, I2C_PECPosition_Current))
    I2C_PECPositionConfig($I2C$, $pecPosition$);
    #endif
    #endif
    
	#if(!STRCMP($mode$, I2C_Mode_I2C))
    //PUT_A_NEW_LINE_HERE
    //
    // PEC Transmit Enable
    //
    I2C_SMBusAlertConfig($I2C$, $SMBusAlertCfg$);
    I2C_ARPCmd($I2C$, $ARPEn$);
    #endif
}