static mal_error_e mal_hspec_stm32f0_i2c_master_clock_init(mal_i2c_init_s *init, uint64_t *i2c_clock) {
	// Set I2C clock source
	RCC_ClocksTypeDef clocks;
	RCC_GetClocksFreq(&clocks);
	// See STM32F0 user manual for I2C clock requirements.
	uint64_t min_clk = 8 * init->bitrate;
	if (MAL_I2C_1 == init->interface) {
		if (SystemCoreClock > min_clk) {
			RCC_I2CCLKConfig(RCC_I2C1CLK_SYSCLK);
			if (NULL != i2c_clock) {
				*i2c_clock = SystemCoreClock;
			}
		} else if (HSI_VALUE > min_clk) {
			RCC_I2CCLKConfig(RCC_I2C1CLK_HSI);
			if (NULL != i2c_clock) {
				*i2c_clock = HSI_VALUE;
			}
		} else {
			return MAL_ERROR_CLOCK_ERROR;
		}
	} else if (MAL_I2C_2 == init->interface) {
		if (NULL != i2c_clock) {
			*i2c_clock = clocks.PCLK_Frequency;
		}
		if (clocks.PCLK_Frequency < min_clk) {
			return MAL_ERROR_CLOCK_ERROR;
		}
	} else {
		return MAL_ERROR_HARDWARE_INVALID;
	}
	if (init->bitrate >= ((4 * clocks.PCLK_Frequency) / 3)) {
		return MAL_ERROR_CLOCK_ERROR;
	}
	return MAL_ERROR_OK;
}
Example #2
0
void mBusInit(uint8_t i2cNum)
{
  #if defined(USING_MBUS1) || defined(USING_MBUS2)
  mBusTypeDef* mBusCurrent = getmBusPtr(i2cNum);
  if(i2cNum == 1)
  {
    #if defined(USING_MBUS1)
    RCC_I2CCLKConfig(RCC_I2C1CLK_SYSCLK);
    mBusCurrent->CPAL_InitStruct = &I2C1_DevStructure;
    CPAL_I2C_StructInit(mBusCurrent->CPAL_InitStruct);
    mBusCurrent->CPAL_InitStruct->CPAL_Dev = CPAL_I2C1; // what's this for?
    #endif
  }
  else //if(i2cNum == 2)
  {
    #if defined(USING_MBUS2)
    RCC_I2CCLKConfig(RCC_I2C2CLK_SYSCLK);
    mBusCurrent->CPAL_InitStruct = &I2C2_DevStructure;
    CPAL_I2C_StructInit(mBusCurrent->CPAL_InitStruct);
    mBusCurrent->CPAL_InitStruct->CPAL_Dev = CPAL_I2C2; // what's this for?
    #endif
  }

  // RX Transfer Structure
  mBusCurrent->mBusRx.pbBuffer = pNULL; //buffer pointer data
  mBusCurrent->mBusRx.wNumData = 0;     //length of data
  mBusCurrent->mBusRx.wAddr1   = 0;     //slaveAddr
  mBusCurrent->mBusRx.wAddr2   = 0;     //regAddr

  // TX Transfer Structure
  mBusCurrent->mBusTx.pbBuffer = pNULL; //buffer pointer data
  mBusCurrent->mBusTx.wNumData =  0;    //length of data
  mBusCurrent->mBusTx.wAddr1   = 0;     //slaveAddr
  mBusCurrent->mBusTx.wAddr2   = 0;     //regAddr
      
  mBusCurrent->CPAL_InitStruct->CPAL_Direction = CPAL_DIRECTION_TXRX;
  mBusCurrent->CPAL_InitStruct->CPAL_Mode = CPAL_MODE_MASTER;
  mBusCurrent->CPAL_InitStruct->CPAL_ProgModel = CPAL_PROGMODEL_DMA;
  mBusCurrent->CPAL_InitStruct->pCPAL_TransferTx = &(mBusCurrent->mBusTx);
  mBusCurrent->CPAL_InitStruct->pCPAL_TransferRx = &(mBusCurrent->mBusRx);
  mBusCurrent->CPAL_InitStruct->wCPAL_Options = CPAL_OPT_I2C_AUTOMATIC_END;

  mBusCurrent->CPAL_InitStruct->pCPAL_I2C_Struct->I2C_Timing = I2C_Timing_FastMode;
  mBusCurrent->CPAL_InitStruct->pCPAL_I2C_Struct->I2C_AnalogFilter = I2C_AnalogFilter_Enable;
  mBusCurrent->CPAL_InitStruct->pCPAL_I2C_Struct->I2C_DigitalFilter = 0x00;
  mBusCurrent->CPAL_InitStruct->pCPAL_I2C_Struct->I2C_Mode = I2C_Mode_I2C;
  mBusCurrent->CPAL_InitStruct->pCPAL_I2C_Struct->I2C_OwnAddress1 = 0x00;
  mBusCurrent->CPAL_InitStruct->pCPAL_I2C_Struct->I2C_Ack = I2C_Ack_Enable;
  mBusCurrent->CPAL_InitStruct->pCPAL_I2C_Struct->I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;

  CPAL_I2C_Init(mBusCurrent->CPAL_InitStruct);
  #endif
}
Example #3
0
/**
  * @brief  Initializes peripherals used by the I2C EEPROM driver.
  * @param  None
  * @retval None
  */
void sEE_I2C_LowLevel_Init(void)
{
  GPIO_InitTypeDef  GPIO_InitStructure;
  
  /* Configure the I2C clock source. The clock is derived from the SYSCLK */
  RCC_I2CCLKConfig(RCC_I2C2CLK_SYSCLK);
    
  /* sEE_I2C_SCL_GPIO_CLK and sEE_I2C_SDA_GPIO_CLK Periph clock enable */
  RCC_AHBPeriphClockCmd(sEE_I2C_SCL_GPIO_CLK | sEE_I2C_SDA_GPIO_CLK, ENABLE);
  
  /* sEE_I2C Periph clock enable */
  RCC_APB1PeriphClockCmd(sEE_I2C_CLK, ENABLE);
  
  /* Connect PXx to I2C_SCL*/
  GPIO_PinAFConfig(sEE_I2C_SCL_GPIO_PORT, sEE_I2C_SCL_SOURCE, sEE_I2C_SCL_AF);
  
  /* Connect PXx to I2C_SDA*/
  GPIO_PinAFConfig(sEE_I2C_SDA_GPIO_PORT, sEE_I2C_SDA_SOURCE, sEE_I2C_SDA_AF);
  
  /* GPIO configuration */  
  /* Configure sEE_I2C pins: SCL */
  GPIO_InitStructure.GPIO_Pin = sEE_I2C_SCL_PIN;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
  GPIO_Init(sEE_I2C_SCL_GPIO_PORT, &GPIO_InitStructure);
  
  /* Configure sEE_I2C pins: SDA */
  GPIO_InitStructure.GPIO_Pin = sEE_I2C_SDA_PIN;
  GPIO_Init(sEE_I2C_SDA_GPIO_PORT, &GPIO_InitStructure);     
}
Example #4
0
void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
    // Determine the I2C to use
    I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA);
    I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL);

    obj->i2c = (I2CName)pinmap_merge(i2c_sda, i2c_scl);
    MBED_ASSERT(obj->i2c != (I2CName)NC);

    // Enable I2C clock
    if (obj->i2c == I2C_1) {
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
        RCC_I2CCLKConfig(RCC_I2C1CLK_SYSCLK);
    }
    if (obj->i2c == I2C_2) {
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2, ENABLE);
    }

    // Configure I2C pins
    pinmap_pinout(scl, PinMap_I2C_SCL);
    pin_mode(scl, OpenDrain);
    pinmap_pinout(sda, PinMap_I2C_SDA);
    pin_mode(sda, OpenDrain);

    // Reset to clear pending flags if any
    i2c_reset(obj);

    // I2C configuration
    i2c_frequency(obj, 100000); // 100 kHz per default
}
Example #5
0
void TwoWire::begin(uint8_t address) {
	if (onBeginCallback)
		onBeginCallback();
	status = SLAVE_IDLE;
	if (twi==I2C1)
	{
		RCC_I2CCLKConfig(RCC_I2C1CLK_HSI);
		RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);

		pinMode(SDA, ALTERNATE);
		pinMode(SCL, ALTERNATE);
	}


	I2C_DeInit(twi);

	I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
	I2C_InitStructure.I2C_AnalogFilter = I2C_AnalogFilter_Enable;
	I2C_InitStructure.I2C_DigitalFilter = 0x00;
		I2C_InitStructure.I2C_OwnAddress1 = address;
	I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
	I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
	I2C_InitStructure.I2C_Timing = 0x00E0D3FF;
	I2C_Init(twi, &I2C_InitStructure);

	NVIC_SetPriority(I2C1_IRQn, 0);
	NVIC_EnableIRQ(I2C1_IRQn);   
	I2C_Cmd(twi, ENABLE);
	I2C_ITConfig(twi, I2C_IT_ADDRI, ENABLE);
}
Example #6
0
/**
  * @brief  Initializes the CEC_I2C..
  * @param  None
  * @retval None
  */
void CEC_I2C_LowLevel_Init(void)
{
  GPIO_InitTypeDef  GPIO_InitStructure;

  /*!< CEC_I2C Periph clock enable */
  RCC_APB1PeriphClockCmd(CEC_I2C_CLK, ENABLE);

  /* Configure the I2C clock source. The clock is derived from the HSI */
  RCC_I2CCLKConfig(RCC_I2C1CLK_HSI);

  /*!< CEC_I2C_SCL_GPIO_CLK, CEC_I2C_SDA_GPIO_CLK Periph clock enable */
  RCC_AHBPeriphClockCmd(CEC_I2C_SCL_GPIO_CLK | CEC_I2C_SDA_GPIO_CLK, ENABLE);

  /* Connect PXx to I2C_SCL */
  GPIO_PinAFConfig(CEC_I2C_SCL_GPIO_PORT, CEC_I2C_SCL_SOURCE, CEC_I2C_SCL_AF);

  /* Connect PXx to I2C_SDA */
  GPIO_PinAFConfig(CEC_I2C_SDA_GPIO_PORT, CEC_I2C_SDA_SOURCE, CEC_I2C_SDA_AF);

  /*!< Configure CEC_I2C pins: SCL */
  GPIO_InitStructure.GPIO_Pin = CEC_I2C_SCL_PIN;
  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_NOPULL;
  GPIO_Init(CEC_I2C_SCL_GPIO_PORT, &GPIO_InitStructure);

  /*!< Configure CEC_I2C pins: SDA */
  GPIO_InitStructure.GPIO_Pin = CEC_I2C_SDA_PIN;
  GPIO_Init(CEC_I2C_SDA_GPIO_PORT, &GPIO_InitStructure);
}
Example #7
0
/**
  * @brief  Configure the I2C Clock source and Power clock
  * @param  None
  * @retval None
  */
static void RCC_Config(void)
{
  /* Configure the I2C1 Clock Source */
  RCC_I2CCLKConfig(RCC_I2C1CLK_HSI);
  
  /* Enable PWR APB clock */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); 
}
Example #8
0
void I2C_Configuration(void)
{
		GPIO_InitTypeDef GPIO_InitStruct; 
		I2C_InitTypeDef I2C_InitStruct;
		NVIC_InitTypeDef NVIC_InitStructure;
	
		// Set SYSCLK as I2C clock source
		RCC_I2CCLKConfig(RCC_I2C1CLK_SYSCLK);
		
		/* Enable GPIOA clock */
		//RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); 
	
		/*!< sEE_I2C Periph clock enable */
		RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1 , ENABLE);
	
		/* Connect PXx to I2C_SCL*/
		GPIO_PinAFConfig(GPIOA , GPIO_PinSource9, GPIO_AF_4);
		/* Connect PXx to I2C_SDA*/
		GPIO_PinAFConfig(GPIOA ,GPIO_PinSource10, GPIO_AF_4);
	  /*!< GPIO configuration */ 
		GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;//GPIO_Mode_IN
		GPIO_InitStruct.GPIO_Speed = GPIO_Speed_Level_3;
		GPIO_InitStruct.GPIO_OType = GPIO_OType_OD;//open-drain
		GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
	
		/*!< Configure sEE_I2C pins: SCL-PA9 */
		GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9;
		GPIO_Init(GPIOA , &GPIO_InitStruct);
		/*!< Configure sEE_I2C pins: SDA-PA10 */
		GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10;
		GPIO_Init(GPIOA , &GPIO_InitStruct);

		/* I2C configuration */
		I2C_InitStruct.I2C_Mode = I2C_Mode_I2C;
		I2C_InitStruct.I2C_AnalogFilter = I2C_AnalogFilter_Enable;
		I2C_InitStruct.I2C_DigitalFilter = 0x00;
		I2C_InitStruct.I2C_OwnAddress1 = (DEVICE_ADR << 1);
		I2C_InitStruct.I2C_Ack = I2C_Ack_Enable;
		I2C_InitStruct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; 
		I2C_InitStruct.I2C_Timing = 0xb0420f13;//100Kbits
	
		/* I2C Peripheral Enable */
		I2C_Cmd(I2C1, ENABLE);
		/* I2C Interrupt Enable */
		I2C_ITConfig(I2C1, I2C_IT_TXI | I2C_IT_RXI | I2C_IT_ADDRI | I2C_IT_STOPI ,ENABLE);
		//I2C_ITConfig(I2C1, I2C_IT_ADDRI | I2C_IT_STOPI ,ENABLE);
		/* Apply I2C configuration after enabling it */
		I2C_Init(I2C1, &I2C_InitStruct);
		
		/* Reconfigure and enable I2C1 error interrupt to have the higher priority */
		NVIC_InitStructure.NVIC_IRQChannel = I2C1_IRQn;
		NVIC_InitStructure.NVIC_IRQChannelPriority = 2;
		NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
		NVIC_Init(&NVIC_InitStructure);
} 
Example #9
0
void mBusInit(void)
{
	RCC_I2CCLKConfig(RCC_I2C1CLK_SYSCLK);
	RCC_I2CCLKConfig(RCC_I2C2CLK_SYSCLK);
	CPAL_I2C_StructInit(&mBusStruct);
	mBusStruct.CPAL_Dev = CPAL_I2C1;
	mBusStruct.CPAL_Direction = CPAL_DIRECTION_TXRX;
	mBusStruct.CPAL_Mode = CPAL_MODE_MASTER;
	mBusStruct.CPAL_ProgModel = CPAL_PROGMODEL_DMA;
	mBusStruct.pCPAL_TransferTx = &mBusTx;
	mBusStruct.pCPAL_TransferRx = &mBusRx;
	mBusStruct.wCPAL_Options = CPAL_OPT_I2C_AUTOMATIC_END;
	mBusStruct.pCPAL_I2C_Struct->I2C_Timing = I2C_Timing_FastMode;
	mBusStruct.pCPAL_I2C_Struct->I2C_AnalogFilter = I2C_AnalogFilter_Enable;
	mBusStruct.pCPAL_I2C_Struct->I2C_DigitalFilter = 0x00;
	mBusStruct.pCPAL_I2C_Struct->I2C_Mode = I2C_Mode_I2C;
	mBusStruct.pCPAL_I2C_Struct->I2C_OwnAddress1 = 0x00;
	mBusStruct.pCPAL_I2C_Struct->I2C_Ack = I2C_Ack_Enable;
	mBusStruct.pCPAL_I2C_Struct->I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
	CPAL_I2C_Init(&mBusStruct);
}
Example #10
0
void i2cInitPort(I2CDevice bus)
{
    I2C_TypeDef *I2Cx = i2cHardwareMap[bus].dev;
    GPIO_InitTypeDef GPIO_InitStructure;
    I2C_InitTypeDef I2C_InitStructure;

    RCC_AHBPeriphClockCmd(i2cHardwareMap[bus].scl_periph | i2cHardwareMap[bus].sda_periph, ENABLE);
    RCC_APB1PeriphClockCmd(i2cHardwareMap[bus].peripheral, ENABLE);
    RCC_I2CCLKConfig(i2cHardwareMap[bus].clock);

    //i2cUnstick(I2Cx);                                         // Clock out stuff to make sure slaves arent stuck

    GPIO_PinAFConfig(i2cHardwareMap[bus].gpioscl, i2cHardwareMap[bus].scl_source, i2cHardwareMap[bus].scl_af);
    GPIO_PinAFConfig(i2cHardwareMap[bus].gpiosda, i2cHardwareMap[bus].sda_source, i2cHardwareMap[bus].sda_af);

    GPIO_StructInit(&GPIO_InitStructure);
    I2C_StructInit(&I2C_InitStructure);

    // Init pins

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

    GPIO_InitStructure.GPIO_Pin = i2cHardwareMap[bus].scl;
    GPIO_Init(i2cHardwareMap[bus].gpioscl, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin = i2cHardwareMap[bus].sda;
    GPIO_Init(i2cHardwareMap[bus].gpiosda, &GPIO_InitStructure);

    I2C_StructInit(&I2C_InitStructure);

    I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
    I2C_InitStructure.I2C_AnalogFilter = I2C_AnalogFilter_Enable;
    I2C_InitStructure.I2C_DigitalFilter = 0x00;
    I2C_InitStructure.I2C_OwnAddress1 = 0x00;
    I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
    I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
    I2C_InitStructure.I2C_Timing = 0x00E0257A; // 400 Khz, 72Mhz Clock, Analog Filter Delay ON, Rise 100, Fall 10.
    //I2C_InitStructure.I2C_Timing              = 0x8000050B;

    I2C_Init(I2Cx, &I2C_InitStructure);

    I2C_Cmd(I2Cx, ENABLE);
}
Example #11
0
void TwoWire::begin(void) {
	if (onBeginCallback)
		onBeginCallback();

	if (twi==I2C1)
	{
		RCC_I2CCLKConfig(RCC_I2C1CLK_HSI);
		RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);

		pinMode(SDA, ALTERNATE);
		pinMode(SCL, ALTERNATE);
	} 
	else if (twi == I2C2) 
	{
		RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2, ENABLE);

		pinMode(SDA1, ALTERNATE);
		pinMode(SCL1, ALTERNATE);
	}


	I2C_DeInit(twi);

	I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
	I2C_InitStructure.I2C_AnalogFilter = I2C_AnalogFilter_Enable;
	I2C_InitStructure.I2C_DigitalFilter = 0x00;
	if (status == UNINITIALIZED)
	{
		I2C_InitStructure.I2C_OwnAddress1 = 0x00;
		status = MASTER_IDLE;
	}
	I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
	I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
	I2C_InitStructure.I2C_Timing = 0x00E0D3FF;
	I2C_Init(twi, &I2C_InitStructure);

	I2C_Cmd(twi, ENABLE);

	
}
Example #12
0
/**
  * @brief  Initializes the TS751_I2C..
  * @param  None
  * @retval None
  */
void TS751_LowLevel_Init(void)
{
  GPIO_InitTypeDef  GPIO_InitStructure;

  /* TS751_I2C Periph clock enable */
  RCC_APB1PeriphClockCmd(TS751_I2C_CLK, ENABLE);
    
  /* Configure the I2C clock source. The clock is derived from the SYSCLK */
  RCC_I2CCLKConfig(RCC_I2C2CLK_SYSCLK);
  
  /* TS751_I2C_SCL_GPIO_CLK, TS751_I2C_SDA_GPIO_CLK 
       and TS751_I2C_SMBUSALERT_GPIO_CLK Periph clock enable */
  RCC_AHBPeriphClockCmd(TS751_I2C_SCL_GPIO_CLK | TS751_I2C_SDA_GPIO_CLK |
                        TS751_I2C_SMBUSALERT_GPIO_CLK, ENABLE);
  
  /* Connect PXx to I2C_SCL */
  GPIO_PinAFConfig(TS751_I2C_SCL_GPIO_PORT, TS751_I2C_SCL_SOURCE, TS751_I2C_SCL_AF);

  /* Connect PXx to I2C_SDA */
  GPIO_PinAFConfig(TS751_I2C_SDA_GPIO_PORT, TS751_I2C_SDA_SOURCE, TS751_I2C_SDA_AF); 

  /* Connect PXx to I2C_SMBUSALER */
  GPIO_PinAFConfig(TS751_I2C_SMBUSALERT_GPIO_PORT, TS751_I2C_SMBUSALERT_SOURCE, TS751_I2C_SMBUSALERT_AF);
    
  /* Configure TS751_I2C pins: SCL */
  GPIO_InitStructure.GPIO_Pin = TS751_I2C_SCL_PIN;
  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_NOPULL;
  GPIO_Init(TS751_I2C_SCL_GPIO_PORT, &GPIO_InitStructure);

  /* Configure TS751_I2C pins: SDA */
  GPIO_InitStructure.GPIO_Pin = TS751_I2C_SDA_PIN;
  GPIO_Init(TS751_I2C_SDA_GPIO_PORT, &GPIO_InitStructure);

  /* Configure TS751_I2C pin: SMBUS ALERT */
  GPIO_InitStructure.GPIO_Pin = TS751_I2C_SMBUSALERT_PIN;
  GPIO_Init(TS751_I2C_SMBUSALERT_GPIO_PORT, &GPIO_InitStructure);
}
Example #13
0
void RCC_Config(void)
{
  
  RCC_DeInit(); 
  RCC_HSEConfig(RCC_HSE_ON); //Включим внешний генератор
  while (SUCCESS != RCC_WaitForHSEStartUp()); //Подождем запуска генератора
  RCC_PLLCmd(DISABLE); 
  RCC_PLLConfig(RCC_PLLSource_PREDIV1,RCC_PLLMul_12); //Вход ПЛЛ внешний генератор умножим на 12
  RCC_PLLCmd(ENABLE);  
  
  RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);   //Системное тактирование от ПЛЛ
  RCC_HCLKConfig(RCC_SYSCLK_Div1);      //Тактирование AHB с делением 1
  
  RCC_PCLKConfig(RCC_HCLK_Div8);        //Тактирование переферии с делением 8
  RCC_ADCCLKConfig(RCC_ADCCLK_PCLK_Div2);       //Тактирование АЦП
  RCC_I2CCLKConfig(RCC_I2C1CLK_SYSCLK);         //Тактируем шину I2C от системного тактирования
  RCC_USARTCLKConfig(RCC_USART1CLK_PCLK);       //Тактируем UART от шини  переферии
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA + RCC_AHBPeriph_GPIOB,ENABLE);
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1 + RCC_APB2Periph_USART1 + RCC_APB2Periph_TIM1 + RCC_APB2Periph_TIM15, ENABLE);
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM14, ENABLE);
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
#ifdef MCO_ENABLE
  GPIOA_Struct_init.GPIO_Pin = GPIO_Pin_8;
  GPIOA_Struct_init.GPIO_Mode = GPIO_Mode_AF;
  GPIOA_Struct_init.GPIO_Speed = GPIO_Speed_Level_2;
  GPIOA_Struct_init.GPIO_OType = GPIO_OType_PP;
  GPIO_Init(GPIOA,&GPIOA_Struct_init);
  RCC_MCOConfig(RCC_MCOSource_SYSCLK);
  while (1);
#else
  RCC_MCOConfig(RCC_MCOSource_NoClock);
#endif /*MCO_ENABLE  */
};
Example #14
0
//initialize the i2c periperal
void init_i2c(void){
    //RCC_APBPeriphClockCmd(RCC_APBPeriph_SYSCFG, ENABLE); //enable for i2c fast mode
    //SYSCFG_I2CFastModePlusConfig(SYSCFG_CFGR1_I2C_FMP_PB6|SYSCFG_CFGR1_I2C_FMP_PB7, ENABLE);
        
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
    RCC_I2CCLKConfig(RCC_I2C1CLK_SYSCLK);
    GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_1);
    GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_1);

    GPIO_InitTypeDef GPIOB_InitStruct = {
        .GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7,
        .GPIO_Speed = GPIO_Speed_50MHz,
        .GPIO_Mode = GPIO_Mode_AF,
        .GPIO_OType = GPIO_OType_OD,
        .GPIO_PuPd = GPIO_PuPd_UP
    };
    GPIO_Init(GPIOB, &GPIOB_InitStruct);
    GPIO_PinLockConfig(GPIOB, GPIO_PinSource6);
    GPIO_PinLockConfig(GPIOB, GPIO_PinSource7);
    
    I2C_InitTypeDef I2C_InitStructure = {
        //.I2C_Timing = 0x20310A0D,
        .I2C_Timing = 0x0010020A,
        .I2C_AnalogFilter = I2C_AnalogFilter_Enable,
        .I2C_DigitalFilter = 0x00,
        .I2C_Mode = I2C_Mode_I2C,
        .I2C_OwnAddress1 = 0x00,
        .I2C_Ack = I2C_Ack_Enable,
        .I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit
    };
    I2C_Init(I2C1, &I2C_InitStructure);
    //I2C_ITConfig(USART1, I2C_IT_NACKI, ENABLE);
    //NVIC_EnableIRQ(I2C1_IRQn);
    I2C_Cmd(I2C1, ENABLE);
}

void I2C_WrReg(uint8_t Reg, uint8_t Val){
    //Wait until I2C isn't busy
	while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY) == SET);

	//"Handle" a transfer - The STM32F0 series has a shocking I2C interface...
	//...Regardless! Send the address of the HMC sensor down the I2C Bus and generate
	//a start saying we're going to write one byte. I'll be completely honest,
	//the I2C peripheral doesn't make too much sense to me and a lot of the code is
	//from the Std peripheral library
	I2C_TransferHandling(I2C1, 0x78, 1, I2C_Reload_Mode, I2C_Generate_Start_Write);

	//Ensure the transmit interrupted flag is set
	while(I2C_GetFlagStatus(I2C1, I2C_FLAG_TXIS) == RESET);

	//Send the address of the register we wish to write to
	I2C_SendData(I2C1, Reg);

	//Ensure that the transfer complete reload flag is Set, essentially a standard
	//TC flag
	while(I2C_GetFlagStatus(I2C1, I2C_FLAG_TCR) == RESET);

	//Now that the HMC5883L knows which register we want to write to, send the address
	//again and ensure the I2C peripheral doesn't add any start or stop conditions
	I2C_TransferHandling(I2C1, 0x78, 1, I2C_AutoEnd_Mode, I2C_No_StartStop);

	//Again, wait until the transmit interrupted flag is set
	while(I2C_GetFlagStatus(I2C1, I2C_FLAG_TXIS) == RESET);

	//Send the value you wish you write to the register
	I2C_SendData(I2C1, Val);

	//Wait for the stop flag to be set indicating a stop condition has been sent
	while(I2C_GetFlagStatus(I2C1, I2C_FLAG_STOPF) == RESET);

	//Clear the stop flag for the next potential transfer
	I2C_ClearFlag(I2C1, I2C_FLAG_STOPF);
}

void i2c_out(uint8_t val){
    //Wait until I2C isn't busy
	while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY) == SET);

	//"Handle" a transfer - The STM32F0 series has a shocking I2C interface...
	//...Regardless! Send the address of the HMC sensor down the I2C Bus and generate
	//a start saying we're going to write one byte. I'll be completely honest,
	//the I2C peripheral doesn't make too much sense to me and a lot of the code is
	//from the Std peripheral library
	I2C_TransferHandling(I2C1, 0x78, 1, I2C_Reload_Mode, I2C_Generate_Start_Write);

	//Ensure the transmit interrupted flag is set
	while(I2C_GetFlagStatus(I2C1, I2C_FLAG_TXIS) == RESET);

	//Send the address of the register we wish to write to
	I2C_SendData(I2C1, val);

	//Ensure that the transfer complete reload flag is Set, essentially a standard
	//TC flag
	while(I2C_GetFlagStatus(I2C1, I2C_FLAG_TCR) == RESET);

	//Clear the stop flag for the next potential transfer
	I2C_ClearFlag(I2C1, I2C_FLAG_STOPF);
}

void I2C_start(uint8_t i2caddress, uint8_t i2cdirection){
    I2C_SlaveAddressConfig(I2C1, i2caddress); 
    
    I2C_MasterRequestConfig(I2C1, i2cdirection);
    while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY) == SET);
    
	//"Handle" a transfer - The STM32F0 series has a shocking I2C interface...
	//...Regardless! Send the address of the HMC sensor down the I2C Bus and generate
	//a start saying we're going to write one byte. I'll be completely honest,
	//the I2C peripheral doesn't make too much sense to me and a lot of the code is
	//from the Std peripheral library
	I2C_TransferHandling(I2C1, i2caddress, 1, I2C_Reload_Mode, I2C_Generate_Start_Write);
	while(I2C_GetFlagStatus(I2C1, I2C_FLAG_TXIS) == RESET);
}
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;
}
Example #16
0
int main(void)
{
	GPIO_InitTypeDef        GPIO_InitStructure;								//structure for GPIO setup
	//TouchInitStr touch_init;
	
	I2C_InitTypeDef					I2C_InitStructure; 		 	
	

  /* SysTick end of count event each 1ms */
  RCC_GetClocksFreq(&RCC_Clocks);
  SysTick_Config(RCC_Clocks.HCLK_Frequency / 1000);	
	
 
	touch_coords.processed=1;
	
	

  /* Initialize LEDs, Key Button, LCD and COM port(USART) available on
     STM32303C-EVAL board *****************************************************/
  STM_EVAL_LEDInit(LED1);
  STM_EVAL_LEDInit(LED2);
  STM_EVAL_LEDInit(LED3);
  STM_EVAL_LEDInit(LED4);
	
	STM_EVAL_LEDInit(LED9);
	

	//init now the TW88 chip on I2C 1
	
	//I2C init ...on PB6 And PB7
	
	/* Configure the I2C clock source. The clock is derived from the SYSCLK */
  RCC_I2CCLKConfig(RCC_I2C1CLK_HSI);													

	RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);

  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB , ENABLE);
	

	
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_4);  // SCL
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_4);	// SDA


  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;																
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;							
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;	
	GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;


  GPIO_InitStructure.GPIO_Pin = (GPIO_Pin_6);	
  GPIO_Init(GPIOB, &GPIO_InitStructure);		

	GPIO_InitStructure.GPIO_Pin = (GPIO_Pin_7);
	GPIO_Init(GPIOB, &GPIO_InitStructure);	
	
	//init our serial port on UART2
	SerialPort_Init(115200,1);
	
	
	printf("\n\r ***** COEN490/ELEC490 Capstone Project Team 14 Welcome! ***** \n\r");
	
	
	I2C_DeInit(I2C1);
	I2C_StructInit(&I2C_InitStructure);
	I2C_InitStructure.I2C_Mode=I2C_Mode_I2C;
	I2C_InitStructure.I2C_AnalogFilter = I2C_AnalogFilter_Enable;
	I2C_InitStructure.I2C_DigitalFilter = 0x00;
	I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
	I2C_InitStructure.I2C_OwnAddress1 = 0x00;
	I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
	//I2C_InitStructure.I2C_Timing = 0x50330309; //0xB0420F13;    ///0x00902025; // 0xc062121f
	I2C_InitStructure.I2C_Timing = 0x00310309; 
	I2C_Init(I2C1,&I2C_InitStructure);
	I2C_Cmd(I2C1,ENABLE);



	
	Delay(1);	
	
  //init TW88
	TW88_Init();
	
	
	//add the osd
	TW88_AddOSD_Win(0,0,0x3F,1);
	
	
	//touch_init.onTouchEvent = onTouchScreenEvent;
	AR1100Init();
	
	
	
  /* Infinite loop */
  while (1)
  {
	
		if (rx_counter>0) {
			printf("Recv: %d",rx_buffer[0]);
			memset(rx_buffer, 0, RX_BUFFER_LENGTH);
			rx_counter=0;
		}
		
		
		if (touch_coords.processed==0) {
			 touch_coords.processed=1;
			 onTouchScreenEvent(touch_coords);
		}
		
		
    /* Toggle LD1 */
    STM_EVAL_LEDToggle(LED1);

    /* Insert 50 ms delay */
    Delay(50);

    /* Toggle LD2 */
    STM_EVAL_LEDToggle(LED2);

    /* Insert 50 ms delay */
    Delay(50);

    /* Toggle LD3 */
    STM_EVAL_LEDToggle(LED3);

    /* Insert 50 ms delay */
    Delay(50);

    /* Toggle LD4 */
    //STM_EVAL_LEDToggle(LED4);

    /* Insert 50 ms delay */
    //Delay(50);
		

		
		
  }
}
Example #17
0
void i2cInitPort(I2C_TypeDef *I2Cx)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    I2C_InitTypeDef I2C_InitStructure;

    if (I2Cx == I2C1) {
        RCC_AHBPeriphClockCmd(I2C1_SCL_CLK_SOURCE | I2C1_SDA_CLK_SOURCE, ENABLE);
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
        RCC_I2CCLKConfig(RCC_I2C1CLK_SYSCLK);

        //i2cUnstick(I2Cx);                                         // Clock out stuff to make sure slaves arent stuck

        GPIO_PinAFConfig(I2C1_SCL_GPIO, I2C1_SCL_PIN_SOURCE, I2C1_SCL_GPIO_AF);
        GPIO_PinAFConfig(I2C1_SDA_GPIO, I2C1_SDA_PIN_SOURCE, I2C1_SDA_GPIO_AF);

        GPIO_StructInit(&GPIO_InitStructure);
        I2C_StructInit(&I2C_InitStructure);

        // Init pins

        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_NOPULL;

        GPIO_InitStructure.GPIO_Pin = I2C1_SCL_PIN;
        GPIO_Init(I2C1_SCL_GPIO, &GPIO_InitStructure);

        GPIO_InitStructure.GPIO_Pin = I2C1_SDA_PIN;
        GPIO_Init(I2C1_SDA_GPIO, &GPIO_InitStructure);

        I2C_StructInit(&I2C_InitStructure);

        I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
        I2C_InitStructure.I2C_AnalogFilter = I2C_AnalogFilter_Enable;
        I2C_InitStructure.I2C_DigitalFilter = 0x00;
        I2C_InitStructure.I2C_OwnAddress1 = 0x00;
        I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
        I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
        if (i2cOverClock) {
            I2C_InitStructure.I2C_Timing = 0x00500E30; // 1000 Khz, 72Mhz Clock, Analog Filter Delay ON, Setup 40, Hold 4.
        } else {
            I2C_InitStructure.I2C_Timing = 0x00E0257A; // 400 Khz, 72Mhz Clock, Analog Filter Delay ON, Rise 100, Fall 10
        }
        //I2C_InitStructure.I2C_Timing              = 0x8000050B;


        I2C_Init(I2C1, &I2C_InitStructure);

        I2C_Cmd(I2C1, ENABLE);
    }

    if (I2Cx == I2C2) {
        RCC_AHBPeriphClockCmd(I2C2_SCL_CLK_SOURCE | I2C2_SDA_CLK_SOURCE, ENABLE);
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2, ENABLE);
        RCC_I2CCLKConfig(RCC_I2C2CLK_SYSCLK);

        //i2cUnstick(I2Cx);                                         // Clock out stuff to make sure slaves arent stuck

        GPIO_PinAFConfig(I2C2_SCL_GPIO, I2C2_SCL_PIN_SOURCE, I2C2_SCL_GPIO_AF);
        GPIO_PinAFConfig(I2C2_SDA_GPIO, I2C2_SDA_PIN_SOURCE, I2C2_SDA_GPIO_AF);

        GPIO_StructInit(&GPIO_InitStructure);
        I2C_StructInit(&I2C_InitStructure);

        // Init pins
        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_NOPULL;

        GPIO_InitStructure.GPIO_Pin = I2C2_SCL_PIN;
        GPIO_Init(I2C2_SCL_GPIO, &GPIO_InitStructure);

        GPIO_InitStructure.GPIO_Pin = I2C2_SDA_PIN;
        GPIO_Init(I2C2_SDA_GPIO, &GPIO_InitStructure);

        I2C_StructInit(&I2C_InitStructure);

        I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
        I2C_InitStructure.I2C_AnalogFilter = I2C_AnalogFilter_Enable;
        I2C_InitStructure.I2C_DigitalFilter = 0x00;
        I2C_InitStructure.I2C_OwnAddress1 = 0x00;
        I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
        I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;

        // FIXME timing is board specific
        //I2C_InitStructure.I2C_Timing = 0x00310309; // //400kHz I2C @ 8MHz input -> PRESC=0x0, SCLDEL=0x3, SDADEL=0x1, SCLH=0x03, SCLL=0x09 - value from TauLabs/Sparky
        // ^ when using this setting and after a few seconds of a scope probe being attached to the I2C bus it was observed that the bus enters
        // a busy state and does not recover.

        if (i2cOverClock) {
            I2C_InitStructure.I2C_Timing = 0x00500E30; // 1000 Khz, 72Mhz Clock, Analog Filter Delay ON, Setup 40, Hold 4.
        } else {
            I2C_InitStructure.I2C_Timing = 0x00E0257A; // 400 Khz, 72Mhz Clock, Analog Filter Delay ON, Rise 100, Fall 10
        }

        //I2C_InitStructure.I2C_Timing              = 0x8000050B;

        I2C_Init(I2C2, &I2C_InitStructure);

        I2C_Cmd(I2C2, ENABLE);
    }
}
Example #18
0
/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
int main(void)
{
  /*!< At this stage the microcontroller clock setting is already configured, 
  this is done through SystemInit() function which is called from startup
  file (startup_stm32f0xx.s) before to branch to application main.
  To reconfigure the default setting of SystemInit() function, refer to
  system_stm32f0xx.c file
  */
  
  /* Initialize LEDs and LCD available on STM320518-EVAL board ****************/
  STM_EVAL_LEDInit(LED1);
  STM_EVAL_LEDInit(LED2);
  STM_EVAL_LEDInit(LED3);
  STM_EVAL_LEDInit(LED4);
    
  /* Initialize TIM6 */
  TIM6_Config();
  
  /* Initialize the LCD */
  STM320518_LCD_Init();
  
  /* Display message on  LCD **************************************************/
  /* Clear the LCD */ 
  LCD_Clear(White);  
  /* Set the LCD Back Color */
  LCD_SetBackColor(Blue);
  /* Set the LCD Text Color */
  LCD_SetTextColor(Yellow);
  LCD_DisplayStringLine(Line0, MESSAGE1);
  LCD_DisplayStringLine(Line1, MESSAGE2);
  LCD_DisplayStringLine(Line3, MESSAGE3);
  
  /* Set the LCD Back Color */
  LCD_SetBackColor(White);
  /* Set the LCD Text Color */
  LCD_SetTextColor(Blue);
  
  /* Configure the Push buttons in interrupt mode *****************************/
  STM_EVAL_PBInit(BUTTON_KEY, Mode_EXTI);
  STM_EVAL_PBInit(BUTTON_TAMPER, Mode_EXTI);
  
  /* Start CPAL communication configuration ***********************************/
  /* Initialize local Reception structures */
  sRxStructure.wNumData = BufferSize;       /* Maximum Number of data to be received */
  sRxStructure.pbBuffer = tRxBuffer;        /* Common Rx buffer for all received data */
  sRxStructure.wAddr1 = 0;                  /* Not needed */
  sRxStructure.wAddr2 = 0;                  /* Not needed */
  
  /* Initialize local Transmission structures */
  sTxStructure.wNumData = BufferSize;       /* Maximum Number of data to be received */
  sTxStructure.pbBuffer = (uint8_t*)tStateSignal;     /* Common Rx buffer for all received data */
  sTxStructure.wAddr1 = OWN_ADDRESS;        /* The own board address */
  sTxStructure.wAddr2 = 0;                  /* Not needed */
  
  /* Set SYSCLK as I2C clock source */
  RCC_I2CCLKConfig(RCC_I2C1CLK_SYSCLK);
  
  /* Configure the device structure */
  CPAL_I2C_StructInit(&I2C_DevStructure);      /* Set all fields to default values */
  I2C_DevStructure.CPAL_Mode = CPAL_MODE_SLAVE;
#ifdef CPAL_I2C_DMA_PROGMODEL
  I2C_DevStructure.wCPAL_Options =  CPAL_OPT_NO_MEM_ADDR | CPAL_OPT_DMATX_TCIT | CPAL_OPT_DMARX_TCIT;
  I2C_DevStructure.CPAL_ProgModel = CPAL_PROGMODEL_DMA;
#elif defined (CPAL_I2C_IT_PROGMODEL)
  I2C_DevStructure.wCPAL_Options =  CPAL_OPT_NO_MEM_ADDR;
  I2C_DevStructure.CPAL_ProgModel = CPAL_PROGMODEL_INTERRUPT;
#else
 #error "Please select one of the programming model (in stm32f0xx_i2c_cpal_conf.h)"
#endif
  I2C_DevStructure.pCPAL_I2C_Struct->I2C_Timing = I2C_TIMING;
  I2C_DevStructure.pCPAL_I2C_Struct->I2C_OwnAddress1 = OWN_ADDRESS;
  I2C_DevStructure.pCPAL_TransferRx = &sRxStructure;
  I2C_DevStructure.pCPAL_TransferTx = &sTxStructure;
  
  /* Initialize CPAL device with the selected parameters */
  CPAL_I2C_Init(&I2C_DevStructure);    
  
  /* Infinite loop */
  while (1)
  {
    /* Write operations ------------------------------------------------------*/
    /* Check if any action has been triggered by push buttons */
    if ((ActionState != ACTION_PENDING) && (ActionState != ACTION_NONE))
    {
      /* Check if the current CPAL device state allows write operation */
      if ((I2C_DevStructure.CPAL_State == CPAL_STATE_READY) || \
        (I2C_DevStructure.CPAL_State == CPAL_STATE_BUSY_RX) ||\
          (I2C_DevStructure.CPAL_State == CPAL_STATE_DISABLED))
      {        
        /* Initialize local Transmission structures */
        sTxStructure.wNumData = BufferSize;   /* Maximum Number of data to be received */
        sTxStructure.wAddr1 = OWN_ADDRESS;    /* The own board address */
        sTxStructure.wAddr2 = 0;              /* Not needed */        
        
        switch (ActionState)
        {
          
        case BUTTON_KEY: 
          sTxStructure.pbBuffer = (uint8_t*)tSignal1; 
          Divider = 1;
          break;
          
        case BUTTON_TAMPER:
          sTxStructure.pbBuffer = (uint8_t*)tSignal2; 
          Divider = 2;
          break;
          
        case ACTION_PERIODIC:
          sTxStructure.pbBuffer = (uint8_t*)tStateSignal; 
          break;    
          
        default:
          sTxStructure.pbBuffer = (uint8_t*)tSignal1; 
          break;            
        } 
        
        /* Configure the device mode to master */
        I2C_DevStructure.CPAL_Mode = CPAL_MODE_MASTER;
        /* Force the CPAL state to ready (in case a read operation has been initiated) */
        I2C_DevStructure.CPAL_State = CPAL_STATE_READY;
                
        /* Prevent other actions to be performed while the current is not finished */
        ActionState = ACTION_PENDING;
        TransmitMode = STATE_ON;
        
        /* Configure a Timer to generate periodic interrupt: used to send state signal */
        TIM17_Config(PeriodicValue/Divider);   
        
        /* Start writing data in master mode */
        if (CPAL_I2C_Write(&I2C_DevStructure) == CPAL_PASS)
        {
         }
      }      
    }
    
    
    /* Read Operations -------------------------------------------------------*/
    if (((I2C_DevStructure.CPAL_State == CPAL_STATE_READY) || \
      (I2C_DevStructure.CPAL_State == CPAL_STATE_DISABLED)) && \
        (TransmitMode == STATE_OFF))
    {                  
      /* Initialize local Reception structures */
      sRxStructure.wNumData = BufferSize;       /* Maximum Number of data to be received */
      sRxStructure.pbBuffer = tRxBuffer;        /* Common Rx buffer for all received data */
      
      /* Reconfigure device for slave receiver mode */
      I2C_DevStructure.CPAL_Mode = CPAL_MODE_SLAVE;
      I2C_DevStructure.CPAL_State = CPAL_STATE_READY;
      
      /* Start waiting for data to be received in slave mode */
      if (CPAL_I2C_Read(&I2C_DevStructure) == CPAL_PASS)
      {
        LCD_DisplayStringLine(Line9, MEASSAGE_EMPTY); 
      }
    }   
  }
}
Example #19
0
uint8_t TW88_Init(void)
{
  //i2c init
  GPIO_InitTypeDef GPIO_InitStructure;
  I2C_InitTypeDef I2C_InitStructure;
  
  //init now the TW88 chip on I2C 1
	
	//I2C init ...on PB6 And PB7
	
	/* Configure the I2C clock source. The clock is derived from the SYSCLK */
  RCC_I2CCLKConfig(RCC_I2C1CLK_HSI);													

	RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);

  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB , ENABLE);
	

	

	
	//init our serial port on UART2
//	SerialPort_Init(115200,1);
	
	
//	printf("\n\r ***** COEN490/ELEC490 Capstone Project Team 14 Welcome! ***** \n\r");
	
	
	I2C_DeInit(I2C1);
	I2C_StructInit(&I2C_InitStructure);
	I2C_InitStructure.I2C_Mode=I2C_Mode_I2C;
	I2C_InitStructure.I2C_AnalogFilter = I2C_AnalogFilter_Enable;
	I2C_InitStructure.I2C_DigitalFilter = 0x00;
	I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
	I2C_InitStructure.I2C_OwnAddress1 = 0x00;
	I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
	//I2C_InitStructure.I2C_Timing = 0x50330309; //0xB0420F13;    ///0x00902025; // 0xc062121f
	I2C_InitStructure.I2C_Timing = 0x00310309; 
	I2C_Init(I2C1,&I2C_InitStructure);
	I2C_Cmd(I2C1,ENABLE);

  GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_4);  // SCL
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_4);	// SDA


  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;


  GPIO_InitStructure.GPIO_Pin = (GPIO_Pin_6);	
  GPIO_Init(GPIOB, &GPIO_InitStructure);		

	GPIO_InitStructure.GPIO_Pin = (GPIO_Pin_7);
	GPIO_Init(GPIOB, &GPIO_InitStructure);	

	
	delay(500);	
  
  
  
  
  
  
  
	//first thing to do would be to make sure that we are on first page
	while (TW88Write(0xff,0x00)==0) {
			//now we detected an error ... we shall resend ?
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
    delay(50);
	}
  
  while (TW88Write(0x06,0x80)==0) { // force reset
      I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
  }
  
	while (TW88Write(0x1C, 0x0F)==0) { // 0F
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	
	while (TW88Write(0x02,0x40)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	
	while (TW88Write(0x04,0x00)==0) { //00
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0x06,0x03)==0) { // 03
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0x07,0x02)==0) { // 02
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0x08,0x00)==0) { // 00
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0x09,0xF0)==0) { // F0
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
			
	}
	while (TW88Write(0x0A,0x19)==0) { // 19
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0x0B,0xD0)==0) { // D0
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0x0C,0xDC)==0) { // DC
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0x0D,0x15)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0x10,0x40)==0) { // brightness 00
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}		
	while (TW88Write(0x11,0x80)==0) { // contrast 50
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}		
	while (TW88Write(0x12,0x71)==0) { //sharpness 71
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0x13,0x9E)==0) { //chroma U 9E
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0x14,0x9E)==0) {  //chroma V 9E
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0x15,0x00)==0) {  //hue 
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}		
	while (TW88Write(0x17,0x33)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}		
	while (TW88Write(0x18,0x44)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0x1C,0x0F)==0) { // Standard Selection 0x08=NTSC 0x0F=all
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0x1D,0x7F)==0) { // 7F
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	
	while (TW88Write(0x1E,0x08)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0x1F,0x00)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0x20,0x50)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	
		while (TW88Write(0x21,0x42)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0x22,0xF0)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	
	while (TW88Write(0x23,0xD8)==0) { // D8
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	
	while (TW88Write(0x24,0xBC)==0) { // BC
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0x25,0xB8)==0) { // B8
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0x26,0x44)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0x27,0x2a)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0x28,0x00)==0) { // 00
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0x29,0x15)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0x2A,0x78)==0) { // 78
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0x2B,0x44)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0x2C,0x30)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0x2D,0x14)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0x2E,0xA5)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
				
		while (TW88Write(0x2F,0x00)==0) { // E0
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0x33,0x85)==0) { // 85
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}		
	while (TW88Write(0x34,0x1A)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0x35,0x40)==0) { // 00 CLAMPING CONTROL ENABLE
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}

	while (TW88Write(0x38,0x80)==0) { // 8E
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0x40,0x00)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0x41,0x20)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0x42,0x04)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0x43,0x22)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0x44,0x08)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}

	while (TW88Write(0x45,0x54)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}

	while (TW88Write(0x46,0x20)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}

	while (TW88Write(0x47,0x00)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}

	while (TW88Write(0x48,0xCF)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}

	while (TW88Write(0x49,0x20)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	
	while (TW88Write(0x4A,0x13)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	
	while (TW88Write(0x4B,0x13)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	
	while (TW88Write(0x4C,0x00)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0x4D,0x30)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0x4E,0x64)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0x4F,0x1E)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0x50,0x00)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0x60,0xE0)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0x61,0x80)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	
	while (TW88Write(0x62,0x7C)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0x63,0x00)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	
	while (TW88Write(0x64,0x00)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0x65,0x80)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0x66,0x00)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0x67,0x00)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0x68,0x00)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0x69,0x00)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0x6A,0x00)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	
	while (TW88Write(0x6B,0x00)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0x6C,0x00)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
		while (TW88Write(0x6D,0x00)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0x6E,0x00)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	
	while (TW88Write(0x70,0x60)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0x71,0x60)==0) { 
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}		
	while (TW88Write(0x72,0x80)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0x73,0x80)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}		
	while (TW88Write(0x74,0x60)==0) { ///brightness
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}		
	while (TW88Write(0x75,0x80)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}		
	while (TW88Write(0x76,0x80)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0x77,0x03)==0) { // 03
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0x78,0x3B)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0x7C,0x1C)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0x7D,0x08)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0x7E,0xF6)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0x7F,0x08)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0x80,0x10)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0x81,0x1D)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0x82,0x03)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0x83,0x00)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0x84,0x67)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0x85,0x94)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0x86,0x00)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}

	while (TW88Write(0x87,0xFF)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0x88,0xCA)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0x89,0x02)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}

	while (TW88Write(0xB0,0x40)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}

	while (TW88Write(0xB1,0x02)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0xB2,0xDB)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0xB3,0x14)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0xB4,0x10)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0xB5,0x20)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0xB6,0x33)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	
	while (TW88Write(0xBB,0x12)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}		
	
	while (TW88Write(0xB7,0x0D)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0xB8,0x04)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0xB9,0x08)==0) {//back porch value
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0xBA,0xE0)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	

	while (TW88Write(0xBC,0x00)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	
	while (TW88Write(0xBD,0x08)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}		
	while (TW88Write(0xBE,0xC2)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}		
	
	
	while (TW88Write(0xBF,0x00)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}		
	
	while (TW88Write(0xC0,0x00)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	
	while (TW88Write(0xC1,0x00)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	

	while (TW88Write(0xC2,0x00)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0xC3,0x00)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0xC4,0x99)==0) { // PWM setup and duty cycle default 0x40 
		// 0xBF - 50% duty cycle - 0x99 - 20% duty cycle
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0xC6,0x00)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0xC7,0x00)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0xC8,0x00)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0xC9,0xED)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0xCA,0xE7)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0xD2,0xFF)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0xD3,0x07)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0xD4,0x00)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0xD5,0x3F)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}

	while (TW88Write(0xD6,0x00)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}

	while (TW88Write(0xD7,0x00)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0xD8,0x00)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}

	while (TW88Write(0xDA,0x3D)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}

	while (TW88Write(0xDB,0xC3)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0xDC,0xFC)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0xDD,0x70)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}

	while (TW88Write(0xDE,0x6F)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	
	while (TW88Write(0xDF,0x6F)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0xE0,0x10)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0xF0,0x00)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0xF4,0x16)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0xF6,0x00)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0xF7,0x00)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0xF8,0xA0)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0xF9,0x01)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0xFA,0x30)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0xFB,0x00)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0xFC,0x48)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	
	while (TW88Write(0xFD,0x30)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0xFE,0x51)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	
	while (TW88Write(0xFF,0x01)==0) { // 2nd page
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0xC0,0x01)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
  
	while (TW88Write(0xCC,0x00)==0) { // 00
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}

	while (TW88Write(0xFF,0x00)==0) { // go back to 1st page
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
  
  while (TW88Write(0x06,0x80)==0) { // force reset
      I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
  }
  delay(10);
  
    while (TW88Write(0x06,0x01)==0) { // rewrite this reg
      I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
  }
  
		
	return 1;
}
Example #20
0
void i2cInitPort(I2C_TypeDef *I2Cx)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    I2C_InitTypeDef I2C_InitStructure;

    if (I2Cx == I2C1) {
        RCC_AHBPeriphClockCmd(I2C1_SCL_CLK_SOURCE | I2C1_SDA_CLK_SOURCE, ENABLE);
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
        RCC_I2CCLKConfig(RCC_I2C1CLK_SYSCLK);

        //i2cUnstick(I2Cx);                                         // Clock out stuff to make sure slaves arent stuck

        GPIO_PinAFConfig(I2C1_SCL_GPIO, I2C1_SCL_PIN_SOURCE, I2C1_SCL_GPIO_AF);
        GPIO_PinAFConfig(I2C1_SDA_GPIO, I2C1_SDA_PIN_SOURCE, I2C1_SDA_GPIO_AF);

        GPIO_StructInit(&GPIO_InitStructure);
        I2C_StructInit(&I2C_InitStructure);

        // Init pins

        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
        GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

        GPIO_InitStructure.GPIO_Pin = I2C1_SCL_PIN;
        GPIO_Init(I2C1_SCL_GPIO, &GPIO_InitStructure);

        GPIO_InitStructure.GPIO_Pin = I2C1_SDA_PIN;
        GPIO_Init(I2C1_SDA_GPIO, &GPIO_InitStructure);

        I2C_StructInit(&I2C_InitStructure);

        I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
        I2C_InitStructure.I2C_AnalogFilter = I2C_AnalogFilter_Enable;
        I2C_InitStructure.I2C_DigitalFilter = 0x00;
        I2C_InitStructure.I2C_OwnAddress1 = 0x00;
        I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
        I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;

        //I2C_InitStructure.I2C_Timing = 0x00E0257A; // 400 Khz, 72Mhz Clock, Analog Filter Delay ON, Setup 100, Hold 10.
        //I2C_InitStructure.I2C_Timing = 0x0070123D; // 800 Khz, 72Mhz Clock, Analog Filter Delay ON, Setup 50, Hold 5.
        I2C_InitStructure.I2C_Timing = 0x00500E30; // 1000 Khz, 72Mhz Clock, Analog Filter Delay ON, Setup 40, Hold 4.

        I2C_Init(I2C1, &I2C_InitStructure);

        I2C_Cmd(I2C1, ENABLE);
    }

    if (I2Cx == I2C2) {
        RCC_AHBPeriphClockCmd(I2C2_SCL_CLK_SOURCE | I2C2_SDA_CLK_SOURCE, ENABLE);
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2, ENABLE);
        RCC_I2CCLKConfig(RCC_I2C2CLK_SYSCLK);

        //i2cUnstick(I2Cx);                                         // Clock out stuff to make sure slaves arent stuck

        GPIO_PinAFConfig(I2C2_SCL_GPIO, I2C2_SCL_PIN_SOURCE, I2C2_SCL_GPIO_AF);
        GPIO_PinAFConfig(I2C2_SDA_GPIO, I2C2_SDA_PIN_SOURCE, I2C2_SDA_GPIO_AF);

        GPIO_StructInit(&GPIO_InitStructure);
        I2C_StructInit(&I2C_InitStructure);

        // Init pins
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
        GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

        GPIO_InitStructure.GPIO_Pin = I2C2_SCL_PIN;
        GPIO_Init(I2C2_SCL_GPIO, &GPIO_InitStructure);

        GPIO_InitStructure.GPIO_Pin = I2C2_SDA_PIN;
        GPIO_Init(I2C2_SDA_GPIO, &GPIO_InitStructure);

        I2C_StructInit(&I2C_InitStructure);

        I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
        I2C_InitStructure.I2C_AnalogFilter = I2C_AnalogFilter_Enable;
        I2C_InitStructure.I2C_DigitalFilter = 0x00;
        I2C_InitStructure.I2C_OwnAddress1 = 0x00;
        I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
        I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;

        //I2C_InitStructure.I2C_Timing = 0x00E0257A; // 400 Khz, 72Mhz Clock, Analog Filter Delay ON, Setup 100, Hold 10.
        //I2C_InitStructure.I2C_Timing = 0x0070123D; // 800 Khz, 72Mhz Clock, Analog Filter Delay ON, Setup 50, Hold 5.
        I2C_InitStructure.I2C_Timing = 0x00500E30; // 1000 Khz, 72Mhz Clock, Analog Filter Delay ON, Setup 40, Hold 4.

        I2C_Init(I2C2, &I2C_InitStructure);

        I2C_Cmd(I2C2, ENABLE);
    }
}