Esempio n. 1
0
void usart3_init(void)
{
	/* RCC initialization */
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE);

	/* GPIO initialization */
	GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_USART3);
	GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_USART3);

	GPIO_InitTypeDef GPIO_InitStruct = {
		.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11,
		.GPIO_Mode = GPIO_Mode_AF,
		.GPIO_Speed = GPIO_Speed_50MHz,
		.GPIO_OType = GPIO_OType_PP,
		.GPIO_PuPd = GPIO_PuPd_UP
	};
	GPIO_Init(GPIOC, &GPIO_InitStruct);

	/* USART initialization */
	USART_InitTypeDef USART_InitStruct = {
		.USART_BaudRate = 9600,
		.USART_Mode = USART_Mode_Rx | USART_Mode_Tx,
		.USART_WordLength = USART_WordLength_8b,
		.USART_StopBits = USART_StopBits_1,
		.USART_Parity = USART_Parity_No
	};
	USART_Init(USART3, &USART_InitStruct);

	USART_Cmd(USART3, ENABLE);

	USART_ClearFlag(USART3, USART_FLAG_TC);

	/* DMA initialization */
	DMA_ClearFlag(DMA1_Stream4, DMA_FLAG_TCIF4);
}

char usart_getc(void)
{
	while(USART_GetFlagStatus(USART3, USART_FLAG_RXNE) != SET);
	return USART_ReceiveData(USART3);
}

void usart_puts(uint8_t *datas, int size)
{
	DMA_ClearFlag(DMA1_Stream4, DMA_FLAG_TCIF4);

	/* Setup the DMA */
	DMA_InitTypeDef DMA_InitStructure = {
		.DMA_BufferSize = (uint32_t)size,
		.DMA_FIFOMode = DMA_FIFOMode_Disable,
		.DMA_FIFOThreshold = DMA_FIFOThreshold_Full,
		.DMA_MemoryBurst = DMA_MemoryBurst_Single,
		.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte,
		.DMA_MemoryInc = DMA_MemoryInc_Enable,
		.DMA_Mode = DMA_Mode_Normal,
		.DMA_PeripheralBaseAddr = (uint32_t)(&USART3->DR),
		.DMA_PeripheralBurst = DMA_PeripheralBurst_Single,
		.DMA_PeripheralInc = DMA_PeripheralInc_Disable,
		.DMA_Priority = DMA_Priority_Medium,
		.DMA_Channel = DMA_Channel_7,
		.DMA_DIR = DMA_DIR_MemoryToPeripheral,
		.DMA_Memory0BaseAddr = (uint32_t)datas
        };
	DMA_Init(DMA1_Stream4, &DMA_InitStructure);

	/* Enable DMA to sent the data */
	DMA_Cmd(DMA1_Stream4, ENABLE);
	USART_DMACmd(USART3, USART_DMAReq_Tx, ENABLE);

	while(DMA_GetFlagStatus(DMA1_Stream4, DMA_FLAG_TCIF4) == RESET);
}

int main()
{
	usart3_init();

	char *string = "STM32: Hello World!\n\r";


	while(1) {
		usart_puts(string, strlen(string) + 1);
	}

	return 0;
}
Esempio n. 2
0
/**
 * @brief  Initializes peripherals used by the I2C EEPROM driver.
 * @param  None
 * @retval None
 */
static void i2c_lowLevel_init(i2c_dev *dev)
    {
    GPIO_InitTypeDef GPIO_I2C1_InitStructure;
    GPIO_InitTypeDef GPIO_I2C2_InitStructure;

    /* Enable the i2c */
    RCC_APB1PeriphClockCmd(dev->clk, ENABLE);

    /* Reset the Peripheral */
    RCC_APB1PeriphResetCmd(dev->clk, ENABLE);
    RCC_APB1PeriphResetCmd(dev->clk, DISABLE);

    /* Enable the GPIOs for the SCL/SDA Pins */
    RCC_AHB1PeriphClockCmd(dev->gpio_port->clk, ENABLE);



    if(dev->I2Cx == I2C1) {
	/* GPIO configuration */
	/* Configure SCL */
	GPIO_I2C1_InitStructure.GPIO_Pin = BIT(dev->scl_pin);
	GPIO_I2C1_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	GPIO_I2C1_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
	GPIO_I2C1_InitStructure.GPIO_OType = GPIO_OType_OD;
	GPIO_I2C1_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
	GPIO_Init(dev->gpio_port->GPIOx, &GPIO_I2C1_InitStructure);

	/* Configure SDA */
	GPIO_I2C1_InitStructure.GPIO_Pin = BIT(dev->sda_pin);
	GPIO_Init(dev->gpio_port->GPIOx, &GPIO_I2C1_InitStructure);

	/* Connect GPIO pins to peripheral */
	GPIO_PinAFConfig(dev->gpio_port->GPIOx, dev->scl_pin, dev->gpio_af);
	GPIO_PinAFConfig(dev->gpio_port->GPIOx, dev->sda_pin, dev->gpio_af);

	NVIC_InitTypeDef NVIC_InitStructure;
	/* Configure and enable I2C DMA TX Channel interrupt */
	NVIC_InitStructure.NVIC_IRQChannel = sEE_I2C1_DMA_TX_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);

	/* Configure and enable I2C DMA RX Channel interrupt */
	NVIC_InitStructure.NVIC_IRQChannel = sEE_I2C1_DMA_RX_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
	NVIC_Init(&NVIC_InitStructure);

	/*!< I2C DMA TX and RX channels configuration */
	/* Enable the DMA clock */
	RCC_AHB1PeriphClockCmd(sEE_I2C1_DMA_CLK, ENABLE);

	/* Clear any pending flag on Rx Stream  */
	DMA_ClearFlag(sEE_I2C1_DMA_STREAM_TX,
		sEE1_TX_DMA_FLAG_FEIF | sEE1_TX_DMA_FLAG_DMEIF | sEE1_TX_DMA_FLAG_TEIF
			| sEE1_TX_DMA_FLAG_HTIF | sEE1_TX_DMA_FLAG_TCIF );
	/* Disable the EE I2C Tx DMA stream */
	DMA_Cmd(sEE_I2C1_DMA_STREAM_TX, DISABLE);
	/* Configure the DMA stream for the EE I2C peripheral TX direction */
	DMA_DeInit(sEE_I2C1_DMA_STREAM_TX );
	I2C1DMA_InitStructure.DMA_Channel = sEE_I2C1_DMA_CHANNEL;
	I2C1DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&I2C1->DR;
	I2C1DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t) 0; /* This parameter will be configured durig communication */
	I2C1DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral; /* This parameter will be configured durig communication */
	I2C1DMA_InitStructure.DMA_BufferSize = 0xFFFF; /* This parameter will be configured durig communication */
	I2C1DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
	I2C1DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
	I2C1DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
	I2C1DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
	I2C1DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
	I2C1DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
	I2C1DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Enable;
	I2C1DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
	I2C1DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
	I2C1DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
	DMA_Init(sEE_I2C1_DMA_STREAM_TX, &I2C1DMA_InitStructure);

	/* Clear any pending flag on Rx Stream */
	DMA_ClearFlag(sEE_I2C1_DMA_STREAM_RX,
		sEE1_RX_DMA_FLAG_FEIF | sEE1_RX_DMA_FLAG_DMEIF | sEE1_RX_DMA_FLAG_TEIF
			| sEE1_RX_DMA_FLAG_HTIF | sEE1_RX_DMA_FLAG_TCIF );
	/* Disable the EE I2C DMA Rx stream */
	DMA_Cmd(sEE_I2C1_DMA_STREAM_RX, DISABLE);
	/* Configure the DMA stream for the EE I2C peripheral RX direction */
	DMA_DeInit(sEE_I2C1_DMA_STREAM_RX );
	DMA_Init(sEE_I2C1_DMA_STREAM_RX, &I2C1DMA_InitStructure);

	/* Enable the DMA Channels Interrupts */
	DMA_ITConfig(sEE_I2C1_DMA_STREAM_TX, DMA_IT_TC, ENABLE);
	DMA_ITConfig(sEE_I2C1_DMA_STREAM_RX, DMA_IT_TC, ENABLE);

    } else if (dev->I2Cx == I2C2) {
	/* GPIO configuration */
	/* Configure SCL */
	GPIO_I2C2_InitStructure.GPIO_Pin = BIT(dev->scl_pin);
	GPIO_I2C2_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	GPIO_I2C2_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
	GPIO_I2C2_InitStructure.GPIO_OType = GPIO_OType_OD;
	GPIO_I2C2_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
	GPIO_Init(dev->gpio_port->GPIOx, &GPIO_I2C2_InitStructure);

	/* Configure SDA */
	GPIO_I2C2_InitStructure.GPIO_Pin = BIT(dev->sda_pin);
	GPIO_Init(dev->gpio_port->GPIOx, &GPIO_I2C2_InitStructure);

	/* Connect GPIO pins to peripheral */
	GPIO_PinAFConfig(dev->gpio_port->GPIOx, dev->scl_pin, dev->gpio_af);
	GPIO_PinAFConfig(dev->gpio_port->GPIOx, dev->sda_pin, dev->gpio_af);

	    NVIC_InitTypeDef NVIC_InitStructure;
	/* Configure and enable I2C DMA TX Channel interrupt */
	NVIC_InitStructure.NVIC_IRQChannel = sEE_I2C2_DMA_TX_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);

	/* Configure and enable I2C DMA RX Channel interrupt */
	NVIC_InitStructure.NVIC_IRQChannel = sEE_I2C2_DMA_RX_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
	NVIC_Init(&NVIC_InitStructure);

	/*!< I2C DMA TX and RX channels configuration */
	/* Enable the DMA clock */
	RCC_AHB1PeriphClockCmd(sEE_I2C2_DMA_CLK, ENABLE);

	/* Clear any pending flag on Rx Stream  */
	DMA_ClearFlag(sEE_I2C2_DMA_STREAM_TX,
		sEE2_TX_DMA_FLAG_FEIF | sEE2_TX_DMA_FLAG_DMEIF | sEE2_TX_DMA_FLAG_TEIF
			| sEE2_TX_DMA_FLAG_HTIF | sEE2_TX_DMA_FLAG_TCIF );
	/* Disable the EE I2C Tx DMA stream */
	DMA_Cmd(sEE_I2C2_DMA_STREAM_TX, DISABLE);
	/* Configure the DMA stream for the EE I2C peripheral TX direction */
	DMA_DeInit(sEE_I2C2_DMA_STREAM_TX );
	I2C2DMA_InitStructure.DMA_Channel = sEE_I2C2_DMA_CHANNEL;
	I2C2DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&I2C2->DR;
	I2C2DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t) 0; /* This parameter will be configured durig communication */
	I2C2DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral; /* This parameter will be configured durig communication */
	I2C2DMA_InitStructure.DMA_BufferSize = 0xFFFF; /* This parameter will be configured durig communication */
	I2C2DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
	I2C2DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
	I2C2DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
	I2C2DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
	I2C2DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
	I2C2DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
	I2C2DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Enable;
	I2C2DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
	I2C2DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
	I2C2DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
	DMA_Init(sEE_I2C2_DMA_STREAM_TX, &I2C2DMA_InitStructure);

	/* Clear any pending flag on Rx Stream */
	DMA_ClearFlag(sEE_I2C2_DMA_STREAM_RX,
		sEE2_RX_DMA_FLAG_FEIF | sEE2_RX_DMA_FLAG_DMEIF | sEE2_RX_DMA_FLAG_TEIF
			| sEE2_RX_DMA_FLAG_HTIF | sEE2_RX_DMA_FLAG_TCIF );
	/* Disable the EE I2C DMA Rx stream */
	DMA_Cmd(sEE_I2C2_DMA_STREAM_RX, DISABLE);
	/* Configure the DMA stream for the EE I2C peripheral RX direction */
	DMA_DeInit(sEE_I2C2_DMA_STREAM_RX );
	DMA_Init(sEE_I2C2_DMA_STREAM_RX, &I2C2DMA_InitStructure);

	/* Enable the DMA Channels Interrupts */
	DMA_ITConfig(sEE_I2C2_DMA_STREAM_TX, DMA_IT_TC, ENABLE);
	DMA_ITConfig(sEE_I2C2_DMA_STREAM_RX, DMA_IT_TC, ENABLE);
    }

}
Esempio n. 3
0
/***********************************
	Hardware Init PWM for Servos
 ***********************************/
void Hardware_PWM_init(void) {

	GPIO_InitTypeDef  GPIO_InitStructure;
	TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
	TIM_OCInitTypeDef  TIM_OCInitStructure;
	uint16_t original_x_pulse = 765;
	uint16_t original_y_pulse = 645;
	uint16_t PrescalerValue = 0;

	/* Enable the GPIO D2/D3 Clock */
	RCC_AHB1PeriphClockCmd(NP2_D1_GPIO_CLK, ENABLE);
	RCC_AHB1PeriphClockCmd(NP2_D2_GPIO_CLK, ENABLE);
	RCC_AHB1PeriphClockCmd(NP2_D3_GPIO_CLK, ENABLE);

	/* TIM2 clock enable */
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

	/* Configure the D2/D3 pin for PWM output */
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
	GPIO_InitStructure.GPIO_Pin = NP2_D2_PIN;
	GPIO_Init(NP2_D2_GPIO_PORT, &GPIO_InitStructure);	//Struct for D2
	GPIO_InitStructure.GPIO_Pin = NP2_D3_PIN;
	GPIO_Init(NP2_D3_GPIO_PORT, &GPIO_InitStructure);	//Struct for D3

	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
	GPIO_InitStructure.GPIO_Pin = NP2_D1_PIN;
	GPIO_Init(NP2_D1_GPIO_PORT, &GPIO_InitStructure);

	/* Connect TIM2 output to D2/D3 pin */
	GPIO_PinAFConfig(NP2_D2_GPIO_PORT, NP2_D2_PINSOURCE, GPIO_AF_TIM2);
	GPIO_PinAFConfig(NP2_D3_GPIO_PORT, NP2_D3_PINSOURCE, GPIO_AF_TIM2);

	/* Compute the prescaler value. SystemCoreClock = 168000000 - set for 500Khz clock */
	PrescalerValue = (uint16_t) ((SystemCoreClock /2) / 500000) - 1;

	/* Time 2 mode and prescaler configuration */
	TIM_TimeBaseStructure.TIM_Period = 10000;
	TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
	TIM_TimeBaseStructure.TIM_ClockDivision = 0;
	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

	/* Configure Timer 2 mode and prescaler */
	TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

	/* PWM Mode configuration for Channel3/4 - set pulse width*/
	TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;				//Set PWM MODE (1 or 2 - NOT CHANNEL)
	TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
	TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
	TIM_OCInitStructure.TIM_Pulse = original_y_pulse;
	TIM_OC3Init(TIM2, &TIM_OCInitStructure);						//Channel 3 - D3
	TIM_OC3PreloadConfig(TIM2, TIM_OCPreload_Enable);
	TIM_OCInitStructure.TIM_Pulse = original_x_pulse;
	TIM_OC4Init(TIM2, &TIM_OCInitStructure);						//Channel 4 - D2
	TIM_OC4PreloadConfig(TIM2, TIM_OCPreload_Enable);

	/* TIM2 enable counter */
	TIM_Cmd(TIM2, ENABLE);
}
Esempio n. 4
0
void bt_usart_conf(u32 br){
	
//  GPIO_InitTypeDef  GPIO_InitStructure;
//  USART_InitTypeDef USART_InitStructure;  
//	if(br==0)br = 115200;
//	/*DMA1 open*/
//	RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);  

//  /* Enable UART GPIO clocks */
//  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
//  /* Enable UART clock */
//  RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
//  //UART
//  USART_InitStructure.USART_BaudRate = br;
//  USART_InitStructure.USART_WordLength = USART_WordLength_8b;
//  USART_InitStructure.USART_StopBits = USART_StopBits_1; 
//  USART_InitStructure.USART_Parity = USART_Parity_No; 
//  USART_InitStructure.USART_HardwareFlowControl = 
//    USART_HardwareFlowControl_None; 
//  USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
//  USART_Init(BT_USART, &USART_InitStructure);
//  USART_Cmd(BT_USART,ENABLE);
//  ///TX
//  RCC_APB2PeriphClockCmd(UART_IO_PERIPH,ENABLE);
//  GPIO_SetBits(UART_IO_PORT,UART_IO_TX);
//  GPIO_InitStructure.GPIO_Pin = UART_IO_TX;
//  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
//  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
//  GPIO_Init(UART_IO_PORT, &GPIO_InitStructure);
//  ///RX
//  RCC_APB2PeriphClockCmd(UART_IO_PERIPH,ENABLE);
//  GPIO_InitStructure.GPIO_Pin=UART_IO_RX;
//  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
//  GPIO_Init(UART_IO_PORT, &GPIO_InitStructure);
	
	USART_InitTypeDef USART_InitStructure;
  GPIO_InitTypeDef GPIO_InitStructure;
	if(br==0)br = 115200;
  /* Peripheral Clock Enable -------------------------------------------------*/
  /* Enable GPIO clock */
  RCC_AHB1PeriphClockCmd(USARTx_TX_GPIO_CLK | USARTx_RX_GPIO_CLK, ENABLE);
  
  /* Enable USART clock */
  USARTx_CLK_INIT(USARTx_CLK, ENABLE);
  
  /* Enable the DMA clock */
  RCC_AHB1PeriphClockCmd(USARTx_DMAx_CLK, ENABLE);
  
  /* USARTx GPIO configuration -----------------------------------------------*/ 
  /* Connect USART pins to AF7 */
  GPIO_PinAFConfig(USARTx_TX_GPIO_PORT, USARTx_TX_SOURCE, USARTx_TX_AF);
  GPIO_PinAFConfig(USARTx_RX_GPIO_PORT, USARTx_RX_SOURCE, USARTx_RX_AF);
  
  /* Configure USART Tx and Rx as alternate function push-pull */
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  
  GPIO_InitStructure.GPIO_Pin = USARTx_TX_PIN;
  GPIO_Init(USARTx_TX_GPIO_PORT, &GPIO_InitStructure);
  
  GPIO_InitStructure.GPIO_Pin = USARTx_RX_PIN;
  GPIO_Init(USARTx_RX_GPIO_PORT, &GPIO_InitStructure);
 
  /* USARTx configuration ----------------------------------------------------*/
  /* Enable the USART OverSampling by 8 */
 // USART_OverSampling8Cmd(USARTx, ENABLE); 
  
  /* USARTx configured as follows:
        - BaudRate = 5250000 baud
		   - Maximum BaudRate that can be achieved when using the Oversampling by 8
		     is: (USART APB Clock / 8) 
			 Example: 
			    - (USART3 APB1 Clock / 8) = (42 MHz / 8) = 5250000 baud
			    - (USART1 APB2 Clock / 8) = (84 MHz / 8) = 10500000 baud
		   - Maximum BaudRate that can be achieved when using the Oversampling by 16
		     is: (USART APB Clock / 16) 
			 Example: (USART3 APB1 Clock / 16) = (42 MHz / 16) = 2625000 baud
			 Example: (USART1 APB2 Clock / 16) = (84 MHz / 16) = 5250000 baud
        - Word Length = 8 Bits
        - one Stop Bit
        - No parity
        - Hardware flow control disabled (RTS and CTS signals)
        - Receive and transmit enabled
  */ 
  USART_InitStructure.USART_BaudRate = br;
  USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  USART_InitStructure.USART_StopBits = USART_StopBits_1;
  /* When using Parity the word length must be configured to 9 bits */
  USART_InitStructure.USART_Parity = USART_Parity_No;
  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  USART_Init(USARTx, &USART_InitStructure);
	USART_Cmd(BT_USART,ENABLE);
}
Esempio n. 5
0
/**
* @brief  Initializes the SPI for the EEPROM.
*         SPI, MISO, MOSI and SCLK are the same used for the SPIRIT1.
*         This function can be replaced by EepromCsPinInitialization if
*         SpiritSpiInit is called.
* @param  None
* @retval None
*/
void EepromSpiInitialization(void)
{ 
  SPI_InitTypeDef SPI_InitStructure;
  GPIO_InitTypeDef GPIO_InitStructure;

  s_EepromSpiPort = s_EepromSpiPortVersion[SdkEvalGetVersion()];
  s_vectnEepromSpiCsPin = (uint16_t *)&s_vectpxEepromSpiCsPinVersion[SdkEvalGetVersion()];
  s_vectpxEepromSpiCsPort = &s_vectpxEepromSpiCsPortVersion[SdkEvalGetVersion()];
    
  if(SdkEvalGetVersion() == SDK_EVAL_VERSION_2_1) {
    /* Enable SPI periph and SCLK, MOSI, MISO and CS GPIO clocks */
    RCC_APB2PeriphClockCmd(EEPROM_V2_SPI_PERIPH_RCC, ENABLE);
    RCC_AHBPeriphClockCmd(EEPROM_V2_SPI_PERIPH_MOSI_RCC | EEPROM_V2_SPI_PERIPH_MISO_RCC | EEPROM_V2_SPI_PERIPH_SCLK_RCC | EEPROM_V2_SPI_PERIPH_CS_RCC, ENABLE);
    
    /* Configure the AF for MOSI, MISO and SCLK GPIO pins*/
    GPIO_PinAFConfig(EEPROM_V2_SPI_PERIPH_MOSI_PORT, EEPROM_V2_SPI_PERIPH_MOSI_RCC_SOURCE, EEPROM_V2_SPI_PERIPH_MOSI_AF);
    GPIO_PinAFConfig(EEPROM_V2_SPI_PERIPH_MISO_PORT, EEPROM_V2_SPI_PERIPH_MISO_RCC_SOURCE, EEPROM_V2_SPI_PERIPH_MISO_AF);
    GPIO_PinAFConfig(EEPROM_V2_SPI_PERIPH_SCLK_PORT, EEPROM_V2_SPI_PERIPH_SCLK_RCC_SOURCE, EEPROM_V2_SPI_PERIPH_SCLK_AF);
    
    /* Configure SPI pins:SCLK, MISO and MOSI */
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_DOWN;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
    
    GPIO_InitStructure.GPIO_Pin = EEPROM_V2_SPI_PERIPH_SCLK_PIN;
    GPIO_Init(EEPROM_V2_SPI_PERIPH_SCLK_PORT, &GPIO_InitStructure);
    
    GPIO_InitStructure.GPIO_Pin = EEPROM_V2_SPI_PERIPH_MISO_PIN;
    GPIO_Init(EEPROM_V2_SPI_PERIPH_MISO_PORT, &GPIO_InitStructure);
    
    GPIO_InitStructure.GPIO_Pin = EEPROM_V2_SPI_PERIPH_MOSI_PIN;
    GPIO_Init(EEPROM_V2_SPI_PERIPH_MOSI_PORT, &GPIO_InitStructure);

  }
  else if(SdkEvalGetVersion() == SDK_EVAL_VERSION_3 || SdkEvalGetVersion() == SDK_EVAL_VERSION_D1) {      
    /* Enable SPI periph and SCLK, MOSI, MISO and CS GPIO clocks */
    RCC_APB1PeriphClockCmd(EEPROM_V3_SPI_PERIPH_RCC, ENABLE);  
    RCC_AHBPeriphClockCmd(EEPROM_V3_SPI_PERIPH_MOSI_RCC | EEPROM_V3_SPI_PERIPH_MISO_RCC | EEPROM_V3_SPI_PERIPH_SCLK_RCC | EEPROM_V3_SPI_PERIPH_CS_RCC, ENABLE);
    
    /* Configure the AF for MOSI, MISO and SCLK GPIO pins*/
    GPIO_PinAFConfig(EEPROM_V3_SPI_PERIPH_MOSI_PORT, EEPROM_V3_SPI_PERIPH_MOSI_RCC_SOURCE, EEPROM_V3_SPI_PERIPH_MOSI_AF);
    GPIO_PinAFConfig(EEPROM_V3_SPI_PERIPH_MISO_PORT, EEPROM_V3_SPI_PERIPH_MISO_RCC_SOURCE, EEPROM_V3_SPI_PERIPH_MISO_AF);
    GPIO_PinAFConfig(EEPROM_V3_SPI_PERIPH_SCLK_PORT, EEPROM_V3_SPI_PERIPH_SCLK_RCC_SOURCE, EEPROM_V3_SPI_PERIPH_SCLK_AF);
    
    /* Configure SPI pins:SCLK, MISO and MOSI */
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_DOWN;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
    
    GPIO_InitStructure.GPIO_Pin = EEPROM_V3_SPI_PERIPH_SCLK_PIN;
    GPIO_Init(EEPROM_V3_SPI_PERIPH_SCLK_PORT, &GPIO_InitStructure);
    
    GPIO_InitStructure.GPIO_Pin = EEPROM_V3_SPI_PERIPH_MISO_PIN;
    GPIO_Init(EEPROM_V3_SPI_PERIPH_MISO_PORT, &GPIO_InitStructure);
    
    GPIO_InitStructure.GPIO_Pin = EEPROM_V3_SPI_PERIPH_MOSI_PIN;
    GPIO_Init(EEPROM_V3_SPI_PERIPH_MOSI_PORT, &GPIO_InitStructure);
    
  }
  
  /* Configure SPI pin: CS */
  GPIO_InitStructure.GPIO_Pin = *s_vectnEepromSpiCsPin;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
  GPIO_Init(*s_vectpxEepromSpiCsPort, &GPIO_InitStructure);
  
  /* Configure SPI peripheral */
  SPI_DeInit(s_EepromSpiPort);
  SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
  SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
  SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
  SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
  SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
  SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
  SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
  SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
  SPI_InitStructure.SPI_CRCPolynomial = 7;
  SPI_Init(s_EepromSpiPort, &SPI_InitStructure);
  
  SPI_Cmd(s_EepromSpiPort, ENABLE);
  
  EepromSPICSHigh();
  
}
Esempio n. 6
0
File: i2c.c Progetto: 9zigen/stm32
// Initialize specified I2C peripheral
// input:
//   I2Cx - I2C port
//   Clock - I2C speed (Hz)
// return:
//   I2C_ERROR if there was a timeout during I2C initialization, I2C_SUCCESS otherwise
// note: minimum APB1 frequency for I2C work is 2MHz
I2C_Status I2Cx_Init(I2C_TypeDef* I2Cx, uint32_t Clock) {
	GPIO_InitTypeDef PORT;
	RCC_ClocksTypeDef RCC_Clocks; // To compute I2C speed depending on current MCU clocking
	uint16_t reg, spd, freq;

	PORT.GPIO_Speed = GPIO_Speed_40MHz;
	PORT.GPIO_OType = GPIO_OType_OD;
	PORT.GPIO_Mode  = GPIO_Mode_AF;
	PORT.GPIO_PuPd  = GPIO_PuPd_UP;

	if (I2Cx == I2C1) {
		// Enable the I2C1 peripheral clock
		RCC->APB1ENR |= RCC_APB1ENR_I2C1EN;
		// Reset the I2C1 peripheral to initial state
		RCC->APB1RSTR |=  RCC_APB1RSTR_I2C1RST;
		RCC->APB1RSTR &= ~RCC_APB1RSTR_I2C1RST;
		// Enable the I2Cx GPIO peripheral clock
		RCC->AHBENR |= I2C1_GPIO_AHB;
		// Initialize the I2C1 GPIO peripheral
		PORT.GPIO_Pin = I2C1_GPIO_SCL | I2C1_GPIO_SDA;
		GPIO_Init(I2C1_GPIO_PORT,&PORT);
		GPIO_PinAFConfig(I2C1_GPIO_PORT,I2C1_GPIO_SCL_SRC,GPIO_AF_I2C1);
		GPIO_PinAFConfig(I2C1_GPIO_PORT,I2C1_GPIO_SDA_SRC,GPIO_AF_I2C1);
	} else {
		// Enable the I2C2 peripheral clock
		RCC->APB1ENR |= RCC_APB1ENR_I2C2EN;
		// Reset the I2C2 peripheral to initial state
		RCC->APB1RSTR |=  RCC_APB1RSTR_I2C2RST;
		RCC->APB1RSTR &= ~RCC_APB1RSTR_I2C2RST;
		// Enable the I2Cx GPIO peripheral clock
		RCC->AHBENR |= I2C2_GPIO_AHB;
		// Initialize the I2C2 GPIO peripheral
		PORT.GPIO_Pin = I2C2_GPIO_SCL | I2C2_GPIO_SDA;
		GPIO_Init(I2C2_GPIO_PORT,&PORT);
		GPIO_PinAFConfig(I2C2_GPIO_PORT,I2C2_GPIO_SCL_SRC,GPIO_AF_I2C2);
		GPIO_PinAFConfig(I2C2_GPIO_PORT,I2C2_GPIO_SDA_SRC,GPIO_AF_I2C2);
	}

	// Configure the I2C peripheral

	// Get CR2 register value and clear FREQ[5:0] bits
	reg = I2Cx->CR2 & ~I2C_CR2_FREQ;
	// Get current RCC clocks
	RCC_GetClocksFreq(&RCC_Clocks);
	// Set FREQ bits depending on PCLK1 value
	freq = (uint16_t)(RCC_Clocks.PCLK1_Frequency / 1000000);
	I2Cx->CR2 |= freq;

	// TRISE can be configured only when I2C peripheral disabled
	I2Cx->CR1 &= ~I2C_CR1_PE;

	// Configure I2C speed
	if (Clock <= 100000) {
		// I2C standard speed (Clock <= 100kHz)
		spd = (uint16_t)(RCC_Clocks.PCLK1_Frequency / (Clock << 1)); // Duty cycle 50%/50%
		// I2C CCR value: Standard mode
		reg = (spd < 0x04) ? 0x04 : spd;
		// Maximum rise time for standard mode
		I2Cx->TRISE = freq + 1;
	} else {
		// I2C fast speed (100kHz > Clock <= 400kHz)
		// PCLK1 frequency must be a multiple of 10MHz
		spd = (uint16_t)(RCC_Clocks.PCLK1_Frequency / (Clock * 3)); // Duty cycle 66%/33% (Tlow/Thigh = 2)
//		spd = (uint16_t)(RCC_Clocks.PCLK1_Frequency / (Clock * 25)); // Duty cycle 64%/33% (Tlow/Thigh = 16/9)
//		reg |= I2C_CCR_DUTY; // I2C fast mode mode duty cycle = 16/9
		// I2C CCR value: Fast mode
		reg = (spd == 0) ? 1 : spd;
		reg |= I2C_CCR_FS;
		// Maximum rise time for fast mode
	    I2Cx->TRISE = (uint16_t)(((freq * 300) / 1000) + 1);
	}
	// Write to I2C CCR register
	I2Cx->CCR = reg;

	// Enable acknowledge, I2C mode, peripheral enabled
	I2Cx->CR1 = I2C_CR1_ACK | I2C_CR1_PE;

	// Set I2C own address: 0x00, 7-bit
	I2Cx->OAR1 = (1 << 14); // Bit 14 should be kept as 1

	// Wait until I2C bus is free
	if (I2Cx_WaitFlagReset(I2Cx,I2C_F_BUSY) == I2C_ERROR) return I2C_ERROR;

	return I2C_SUCCESS;
}
Esempio n. 7
0
/**
 * @brief  USB_OTG_BSP_Init
 *         Initilizes BSP configurations
 * @param  None
 * @retval None
 */
void USB_OTG_BSP_Init(USB_OTG_CORE_HANDLE *pdev)
{
	/* Note: On STM32F4-Discovery board only USB OTG FS core is supported. */

	GPIO_InitTypeDef GPIO_InitStructure;
#ifdef USE_USB_OTG_FS

	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);

	/* Configure SOF VBUS ID DM DP Pins */
	GPIO_InitStructure.GPIO_Pin = 	GPIO_Pin_11 | 	// Data -
									GPIO_Pin_12;	// Data +

	#ifndef USB_MSC_HOST_DISABLE_VBUS
	GPIO_InitStructure.GPIO_Pin |= GPIO_Pin_9;		// VBUS
	#endif
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
	GPIO_Init(GPIOA, &GPIO_InitStructure);

	#ifndef USB_MSC_HOST_DISABLE_VBUS
	GPIO_PinAFConfig(GPIOA,GPIO_PinSource9, GPIO_AF_OTG1_FS);
	#endif
	GPIO_PinAFConfig(GPIOA,GPIO_PinSource11, GPIO_AF_OTG1_FS);
	GPIO_PinAFConfig(GPIOA,GPIO_PinSource12, GPIO_AF_OTG1_FS);

	/* this for ID line debug */
	#ifndef USB_MSC_HOST_DISABLE_ID
	GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_10;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	GPIO_PinAFConfig(GPIOA,GPIO_PinSource10, GPIO_AF_OTG1_FS) ;
	#endif

	RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
	RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_OTG_FS, ENABLE) ;
	
#else //USE_USB_OTG_HS

	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB , ENABLE);

	/* Configure SOF VBUS ID DM DP Pins */  
	GPIO_InitStructure.GPIO_Pin = 	GPIO_Pin_14 |	// Data -
									GPIO_Pin_15;	// Data +

	#ifndef USB_MSC_HOST_DISABLE_ID
	GPIO_InitStructure.GPIO_Pin |= GPIO_Pin_12;
	#endif

	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; 
	GPIO_Init(GPIOB, &GPIO_InitStructure);  

	#ifndef USB_MSC_HOST_DISABLE_ID
	GPIO_PinAFConfig(GPIOB,GPIO_PinSource12, GPIO_AF_OTG2_FS);
	#endif
	
	GPIO_PinAFConfig(GPIOB,GPIO_PinSource14, GPIO_AF_OTG2_FS); 
	GPIO_PinAFConfig(GPIOB,GPIO_PinSource15, GPIO_AF_OTG2_FS);   
	
	/* VBUS */
	#ifndef USB_MSC_HOST_DISABLE_VBUS
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
	GPIO_Init(GPIOB, &GPIO_InitStructure);
	#endif

	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_OTG_HS, ENABLE);  

#endif //USB_OTG_HS
}
Esempio n. 8
0
int sflash_platform_init( int peripheral_id, void** platform_peripheral_out )
{
    GPIO_InitTypeDef GPIO_InitStructure;
    SPI_InitTypeDef  SPI_InitStructure;

    (void) peripheral_id; /* Unused due to single SPI Flash */

    /* Enable clocks */
    SFLASH_SPI_CLK_INIT( SFLASH_SPI_CLK, ENABLE );

    RCC_AHB1PeriphClockCmd( SFLASH_SPI_SCK_GPIO_CLK  | SFLASH_SPI_MISO_GPIO_CLK |
                            SFLASH_SPI_MOSI_GPIO_CLK | SFLASH_CS_CLK, ENABLE      );


    /* Use Alternate Functions for SPI pins */
    GPIO_PinAFConfig( SFLASH_SPI_SCK_GPIO_PORT,  SFLASH_SPI_SCK_SOURCE,  SFLASH_SPI_SCK_AF  );
    GPIO_PinAFConfig( SFLASH_SPI_MISO_GPIO_PORT, SFLASH_SPI_MISO_SOURCE, SFLASH_SPI_MISO_AF );
    GPIO_PinAFConfig( SFLASH_SPI_MOSI_GPIO_PORT, SFLASH_SPI_MOSI_SOURCE, SFLASH_SPI_MOSI_AF );

    /* Setup pin types */
    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_DOWN;

    GPIO_InitStructure.GPIO_Pin   = SFLASH_SPI_SCK_PIN;
    GPIO_Init( SFLASH_SPI_SCK_GPIO_PORT, &GPIO_InitStructure );

    GPIO_InitStructure.GPIO_Pin   =  SFLASH_SPI_MOSI_PIN;
    GPIO_Init( SFLASH_SPI_MOSI_GPIO_PORT, &GPIO_InitStructure );

    GPIO_InitStructure.GPIO_Pin   =  SFLASH_SPI_MISO_PIN;
    GPIO_Init( SFLASH_SPI_MISO_GPIO_PORT, &GPIO_InitStructure );

    /* Chip select is used as a GPIO */
    GPIO_InitStructure.GPIO_Pin   = SFLASH_CS_PIN;
    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_OUT;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;
    GPIO_Init( SFLASH_CS_PORT, &GPIO_InitStructure );

    /* Deselect flash initially */
    GPIO_SetBits( SFLASH_CS_PORT, SFLASH_CS_PIN );

    /*!< SPI configuration */
    SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
    SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
    SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
    SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
    SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
    SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
    SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8;

    SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
    SPI_InitStructure.SPI_CRCPolynomial = 7;
    SPI_Init(SFLASH_SPI, &SPI_InitStructure);

    /* Enable the SPI peripheral */
    SPI_Cmd(SFLASH_SPI, ENABLE);

    *platform_peripheral_out = (void*)SFLASH_SPI;

    return 0;
}
Esempio n. 9
0
void init_Timer()
{

	  GPIO_InitTypeDef GPIO_InitStructure;
	  NVIC_InitTypeDef NVIC_InitStructure;
	  TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
	  uint16_t PrescalerValue = 0;

	  /* TIM3 clock enable */
	  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
	
	  /* Enable the TIM3 gloabal Interrupt */
	  NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
	  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
	  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
	  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	  NVIC_Init(&NVIC_InitStructure);
	
	  /* ---------------------------------------------------------------
	    TIM3 Configuration: Output Compare Timing Mode:
	    TIM3 counter clock at 6 MHz
	    CC1 Update Rate : 128 Hz
	  --------------------------------------------------------------- */
	
	  /* Compute the prescaler value */
	  PrescalerValue = (uint16_t) ((SystemCoreClock / 2) / 6000000) - 1;
	
	  /* Time base configuration */
	  TIM_TimeBaseStructure.TIM_Period = 6000000 -1; //46875;
	  TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
	  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
	  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
	
	  TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
	
	  /* Prescaler configuration */
	  TIM_PrescalerConfig(TIM2, PrescalerValue, TIM_PSCReloadMode_Immediate);
	   
	  /* TIM Interrupts enable */
	  TIM_ITConfig(TIM2, TIM_IT_CC1 , ENABLE);
	
	  /* TIM3 enable counter */
	  TIM_Cmd(TIM2, ENABLE);
		
	return;
  /* TIM3 clock enable */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);

  /* GPIOC clock enable */
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
  
  /* GPIOC Configuration: TIM3 CH1 (PC6), TIM3 CH2 (PC7), TIM3 CH3 (PC8) and TIM3 CH4 (PC9) */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
  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_UP ;
  GPIO_Init(GPIOE, &GPIO_InitStructure); 

  /* Connect TIM3 pins to AF2 */  
  GPIO_PinAFConfig(GPIOE, GPIO_PinSource13, GPIO_AF_TIM1);
	
	PrescalerValue = (uint16_t) ((SystemCoreClock /2) / 20000000) - 1;

  /* Time base configuration */
  TIM_TimeBaseStructure.TIM_Period =1000000;
  TIM_TimeBaseStructure.TIM_Prescaler = 1000;
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

  TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);

  /* PWM1 Mode configuration: Channel1 */
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

  /* PWM1 Mode configuration: Channel3 */
  TIM_OCInitStructure.TIM_Pulse = 45;
  TIM_OC3Init(TIM1, &TIM_OCInitStructure);
  TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable);

  TIM_ARRPreloadConfig(TIM1, ENABLE);

  CCR3_Val = 100;
  /* TIM3 enable counter */
  TIM_Cmd(TIM1, ENABLE);
	
}
Esempio n. 10
0
/**
* Initialise a single USART device
*/
int32_t PIOS_USART_Init(uintptr_t * usart_id, const struct pios_usart_cfg * cfg)
{
	PIOS_DEBUG_Assert(usart_id);
	PIOS_DEBUG_Assert(cfg);

	struct pios_usart_dev * usart_dev;

	usart_dev = (struct pios_usart_dev *) PIOS_USART_alloc();
	if (!usart_dev) goto out_fail;

	/* Bind the configuration to the device instance */
	usart_dev->cfg = cfg;

	/* Map pins to USART function */
	if (usart_dev->cfg->remap) {
		if (usart_dev->cfg->rx.gpio != 0)
			GPIO_PinAFConfig(usart_dev->cfg->rx.gpio,
				usart_dev->cfg->rx.pin_source,
				usart_dev->cfg->remap);
		if (usart_dev->cfg->tx.gpio != 0)
			GPIO_PinAFConfig(usart_dev->cfg->tx.gpio,
				usart_dev->cfg->tx.pin_source,
				usart_dev->cfg->remap);
	}

	/* Initialize the USART Rx and Tx pins */
	if (usart_dev->cfg->rx.gpio != 0)
		GPIO_Init(usart_dev->cfg->rx.gpio, (GPIO_InitTypeDef *)&usart_dev->cfg->rx.init);
	if (usart_dev->cfg->tx.gpio != 0)
		GPIO_Init(usart_dev->cfg->tx.gpio, (GPIO_InitTypeDef *)&usart_dev->cfg->tx.init);

	/* Apply inversion and swap settings */
	if (usart_dev->cfg->rx_invert == true)
		USART_InvPinCmd(usart_dev->cfg->regs, USART_InvPin_Rx, ENABLE);
	else
		USART_InvPinCmd(usart_dev->cfg->regs, USART_InvPin_Rx, DISABLE);

	if (usart_dev->cfg->tx_invert == true)
		USART_InvPinCmd(usart_dev->cfg->regs, USART_InvPin_Tx, ENABLE);
	else
		USART_InvPinCmd(usart_dev->cfg->regs, USART_InvPin_Tx, DISABLE);

	if (usart_dev->cfg->rxtx_swap == true)
		USART_SWAPPinCmd(usart_dev->cfg->regs, ENABLE);
	else
		USART_SWAPPinCmd(usart_dev->cfg->regs, DISABLE);

	/* Configure the USART */
	USART_Init(usart_dev->cfg->regs, (USART_InitTypeDef *)&usart_dev->cfg->init);

	*usart_id = (uintptr_t)usart_dev;

	/* Configure USART Interrupts */
	switch ((uint32_t)usart_dev->cfg->regs) {
	case (uint32_t)USART1:
		PIOS_USART_1_id = (uintptr_t)usart_dev;
		break;
	case (uint32_t)USART2:
		PIOS_USART_2_id = (uintptr_t)usart_dev;
		break;
	case (uint32_t)USART3:
		PIOS_USART_3_id = (uintptr_t)usart_dev;
		break;
	case (uint32_t)UART4:
		PIOS_UART_4_id = (uintptr_t)usart_dev;
		break;
	case (uint32_t)UART5:
		PIOS_UART_5_id = (uintptr_t)usart_dev;
		break;
	}
	NVIC_Init((NVIC_InitTypeDef *)&(usart_dev->cfg->irq.init));
	USART_ITConfig(usart_dev->cfg->regs, USART_IT_RXNE, ENABLE);
	USART_ITConfig(usart_dev->cfg->regs, USART_IT_TXE,  ENABLE);

	// FIXME XXX Clear / reset uart here - sends NUL char else

	/* Enable USART */
	USART_Cmd(usart_dev->cfg->regs, ENABLE);

	return(0);

out_fail:
	return(-1);
}
Esempio n. 11
0
static int f4_sram_pins_init( const wiced_sram_device_t* sram, const stm32f4xx_platform_nor_sram_t* settings )
{
    /* Enable clocks for associated gpios */
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG, ENABLE);
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOI, ENABLE);

    GPIO_PinAFConfig(GPIOD, 7, GPIO_AF_FSMC);   /* one of chip select control signals NE1-NE4 to FSMC  */

    /* Configure gpios for FSMC alternate function */
    /* Connect all gpio lined to the FSMC */

    GPIO_PinAFConfig(GPIOF, 0, GPIO_AF_FSMC);   /* A0 connected to PF0 */
    GPIO_PinAFConfig(GPIOF, 1, GPIO_AF_FSMC);   /* A1 connected to PF1 */
    GPIO_PinAFConfig(GPIOF, 2, GPIO_AF_FSMC);   /* A2 connected to PF2 */
    GPIO_PinAFConfig(GPIOF, 3, GPIO_AF_FSMC);   /* A3 connented to PF3 */
    GPIO_PinAFConfig(GPIOF, 4, GPIO_AF_FSMC);   /* A4 connected to PF4 */
    GPIO_PinAFConfig(GPIOF, 5, GPIO_AF_FSMC);   /* A5 connected to PF5 */
    GPIO_PinAFConfig(GPIOF, 12, GPIO_AF_FSMC);  /* A6 connected to PF12 */
    GPIO_PinAFConfig(GPIOF, 13, GPIO_AF_FSMC);  /* A7 connected to PF13 */
    GPIO_PinAFConfig(GPIOF, 14, GPIO_AF_FSMC);  /* A8 connected to PF14 */
    GPIO_PinAFConfig(GPIOF, 15, GPIO_AF_FSMC);  /* A9 connected to PF15 */
    GPIO_PinAFConfig(GPIOG, 0, GPIO_AF_FSMC);   /* A10 connected to PG0 */
    GPIO_PinAFConfig(GPIOG, 1, GPIO_AF_FSMC);   /* A11 connected to PG1 */
    GPIO_PinAFConfig(GPIOG, 2, GPIO_AF_FSMC);   /* A12 connected to PG2 */
    GPIO_PinAFConfig(GPIOG, 3, GPIO_AF_FSMC);   /* A13 connected to PG3 */
    GPIO_PinAFConfig(GPIOG, 4, GPIO_AF_FSMC);   /* A14 connected to PG4 */
    GPIO_PinAFConfig(GPIOG, 5, GPIO_AF_FSMC);   /* A15 connected to PG5 */

    GPIO_PinAFConfig(GPIOD, 11, GPIO_AF_FSMC);      /* A16 connected to PD11 */
    GPIO_PinAFConfig(GPIOD, 12, GPIO_AF_FSMC);      /* A17 connected to PD12 */
    GPIO_PinAFConfig(GPIOD, 13, GPIO_AF_FSMC);      /* A18 connected to PD13 */
    GPIO_PinAFConfig(GPIOE, 3, GPIO_AF_FSMC);       /* A19 connected to PE3  */
    GPIO_PinAFConfig(GPIOE, 4, GPIO_AF_FSMC);       /* A20 connected to PE4 */
    GPIO_PinAFConfig(GPIOE, 5, GPIO_AF_FSMC);       /* A21 connected to PE5 */

    GPIO_PinAFConfig(GPIOE, 6, GPIO_AF_FSMC);       /* A22 connected to PE6 */

    GPIO_PinAFConfig(GPIOE, 2, GPIO_AF_FSMC);       /* A23 connected to PE2 */
    GPIO_PinAFConfig(GPIOG, 13, GPIO_AF_FSMC);      /* A24 connected to PG13 */
    GPIO_PinAFConfig(GPIOG, 14, GPIO_AF_FSMC);      /* A25 connected to PG14 */

    /* Data lines */
    GPIO_PinAFConfig(GPIOD, 14, GPIO_AF_FSMC);      /* D0 connected to PD14 */
    GPIO_PinAFConfig(GPIOD, 15, GPIO_AF_FSMC);      /* D1 connected to PD15 */
    GPIO_PinAFConfig(GPIOD, 0, GPIO_AF_FSMC);       /* D2 connected to PD0 */
    GPIO_PinAFConfig(GPIOD, 1, GPIO_AF_FSMC);       /* D3 connected to PD1 */
    GPIO_PinAFConfig(GPIOE, 7, GPIO_AF_FSMC);       /* D4 connected to PE7 */
    GPIO_PinAFConfig(GPIOE, 8, GPIO_AF_FSMC);       /* D5 connected to PE8 */
    GPIO_PinAFConfig(GPIOE, 9, GPIO_AF_FSMC);       /* D6 connected to PE9 */
    GPIO_PinAFConfig(GPIOE, 10, GPIO_AF_FSMC);      /* D7 connected to PE10 */

    /* Connect D7-D15 to FSMC is the data bus is 16 bit */
    GPIO_PinAFConfig(GPIOE, 11, GPIO_AF_FSMC);  /* D8 is connected to PE11 */
    GPIO_PinAFConfig(GPIOE, 12, GPIO_AF_FSMC);  /* D9 is connected to PE12 */
    GPIO_PinAFConfig(GPIOE, 13, GPIO_AF_FSMC);  /* D10 is connected to PE13 */
    GPIO_PinAFConfig(GPIOE, 14, GPIO_AF_FSMC);  /* D11 is connected to PE14 */
    GPIO_PinAFConfig(GPIOE, 15, GPIO_AF_FSMC);  /* D12 is connected to PE15 */
    GPIO_PinAFConfig(GPIOD, 8, GPIO_AF_FSMC);   /* D13 is connected to PD8 */
    GPIO_PinAFConfig(GPIOD, 9, GPIO_AF_FSMC);   /* D14 is connected to PD9 */
    GPIO_PinAFConfig(GPIOD, 10, GPIO_AF_FSMC);  /* D15 is connected to PD10 */

    /* control lines */
    /* NOE */
    GPIO_PinAFConfig(GPIOD, 4, GPIO_AF_FSMC); /* NOE is connected to PD4 */

    /* NWE */
    GPIO_PinAFConfig(GPIOD, 5, GPIO_AF_FSMC); /* NWE is connected to PD5 */

    /* NL(NADV) */
    /* NADV stays unconnected in SRAM memories */

    /* On BCM9WCD1AUDIO first spin board NWAIT is connected to the ZZ pin of the PSRAM memory */
    GPIO_InitTypeDef gpio_init_structure;
    gpio_init_structure.GPIO_Speed = GPIO_Speed_50MHz;
    gpio_init_structure.GPIO_Mode  = GPIO_Mode_OUT;
    gpio_init_structure.GPIO_OType = GPIO_OType_PP;
    gpio_init_structure.GPIO_PuPd  =  GPIO_PuPd_UP;
    gpio_init_structure.GPIO_Pin =  ( 1 << 6 );
    GPIO_Init( GPIOD, &gpio_init_structure );
    /* Set to high level, so memory never goes to power-save mode */
    GPIO_SetBits( GPIOD, ( 1 << 6 ) );

     /* NWAIT */

    /* NBL[1] and NBL[0] */
    GPIO_PinAFConfig(GPIOE, 1, GPIO_AF_FSMC); /* NBL[1] is connected to PE1 */
    GPIO_PinAFConfig(GPIOE, 0, GPIO_AF_FSMC); /* NBL[0] is connected to PE0 */

    /* CLK */
    GPIO_PinAFConfig(GPIOD, 3, GPIO_AF_FSMC); /* CLK is connected to PD3 */

    /* Initialise GPIO to operate in high speed Alternate function mode */
    int a;
    for (a = 0; a < 4; ++a)
    {
        sram_gpio_settings[a].port->MODER   |= sram_gpio_settings[a].mode_mask;
        sram_gpio_settings[a].port->OTYPER  |= sram_gpio_settings[a].otype_mask;
        sram_gpio_settings[a].port->OSPEEDR |= sram_gpio_settings[a].speed_mask;
        sram_gpio_settings[a].port->PUPDR   |= sram_gpio_settings[a].pupd_mask;
    }

    return 0;
}
Esempio n. 12
0
OSStatus MicoPwmInitialize( mico_pwm_t pwm_peripheral, uint32_t frequency, float duty_cycle )
{
  TIM_TimeBaseInitTypeDef tim_time_base_structure;
  TIM_OCInitTypeDef       tim_oc_init_structure;
  GPIO_InitTypeDef        gpio_init_structure;
  RCC_ClocksTypeDef       rcc_clock_frequencies;
  const platform_pwm_mapping_t* pwm                 = &pwm_mappings[pwm_peripheral];
  uint16_t                      period              = 0;
  float                         adjusted_duty_cycle = ( ( duty_cycle > 100.0f ) ? 100.0f : duty_cycle );
  
  MicoMcuPowerSaveConfig(false);
  
  RCC_GetClocksFreq( &rcc_clock_frequencies );
  
  if ( pwm->tim == TIM1 || pwm->tim == TIM8 || pwm->tim == TIM9 || pwm->tim == TIM10 || pwm->tim == TIM11 )
  {
    RCC_APB2PeriphClockCmd( pwm->tim_peripheral_clock, ENABLE );
    period = (uint16_t)( rcc_clock_frequencies.PCLK2_Frequency / frequency - 1 ); /* Auto-reload value counts from 0; hence the minus 1 */
  }
  else
  {
    RCC_APB1PeriphClockCmd( pwm->tim_peripheral_clock, ENABLE );
    period = (uint16_t)( 2*rcc_clock_frequencies.PCLK1_Frequency / frequency - 1 ); /* Auto-reload value counts from 0; hence the minus 1 */
  }
  
  RCC_AHB1PeriphClockCmd( pwm->pin->peripheral_clock, ENABLE );
  
  GPIO_PinAFConfig( pwm->pin->bank, pwm->pin->number, pwm->gpio_af );
  gpio_init_structure.GPIO_Pin   = (uint32_t) ( 1 << pwm->pin->number );
  gpio_init_structure.GPIO_Mode  = GPIO_Mode_AF;
  gpio_init_structure.GPIO_Speed = GPIO_Speed_100MHz;
  gpio_init_structure.GPIO_OType = GPIO_OType_PP;
  gpio_init_structure.GPIO_PuPd  = GPIO_PuPd_UP;
  GPIO_Init( pwm->pin->bank, &gpio_init_structure );
  
  
  /* Time base configuration */
  tim_time_base_structure.TIM_Period            = (uint32_t) period;
  tim_time_base_structure.TIM_Prescaler         = (uint16_t) 0;  
  tim_time_base_structure.TIM_ClockDivision     = 0;
  tim_time_base_structure.TIM_CounterMode       = TIM_CounterMode_Up;
  tim_time_base_structure.TIM_RepetitionCounter = 0;
  TIM_TimeBaseInit( pwm->tim, &tim_time_base_structure );
  
  /* PWM1 Mode configuration */
  tim_oc_init_structure.TIM_OCMode       = TIM_OCMode_PWM1;
  tim_oc_init_structure.TIM_OutputState  = TIM_OutputState_Enable;
  tim_oc_init_structure.TIM_OutputNState = TIM_OutputNState_Enable;
  tim_oc_init_structure.TIM_Pulse        = (uint16_t) ( adjusted_duty_cycle * (float) period / 100.0f );
  tim_oc_init_structure.TIM_OCPolarity   = TIM_OCPolarity_High;
  tim_oc_init_structure.TIM_OCNPolarity  = TIM_OCNPolarity_High;
  tim_oc_init_structure.TIM_OCIdleState  = TIM_OCIdleState_Reset;
  tim_oc_init_structure.TIM_OCNIdleState = TIM_OCIdleState_Set;
  
  switch ( pwm->channel )
  {
  case 1:
    {
      TIM_OC1Init( pwm->tim, &tim_oc_init_structure );
      TIM_OC1PreloadConfig( pwm->tim, TIM_OCPreload_Enable );
      break;
    }
  case 2:
    {
      TIM_OC2Init( pwm->tim, &tim_oc_init_structure );
      TIM_OC2PreloadConfig( pwm->tim, TIM_OCPreload_Enable );
      break;
    }
  case 3:
    {
      TIM_OC3Init( pwm->tim, &tim_oc_init_structure );
      TIM_OC3PreloadConfig( pwm->tim, TIM_OCPreload_Enable );
      break;
    }
  case 4:
    {
      TIM_OC4Init( pwm->tim, &tim_oc_init_structure );
      TIM_OC4PreloadConfig( pwm->tim, TIM_OCPreload_Enable );
      break;
    }
  default:
    {
      break;
    }
  }
  
  MicoMcuPowerSaveConfig(true);
  
  return kNoErr;
}
Esempio n. 13
0
/**
  * @brief  USB_OTG_BSP_Init
  *         Initilizes BSP configurations
  * @param  None
  * @retval None
  */
void USB_OTG_BSP_Init(USB_OTG_CORE_HANDLE *pdev)
{
 /* Note: On STM32F4-Discovery board only USB OTG FS core is supported. */

  GPIO_InitTypeDef GPIO_InitStructure;
 #ifdef USE_USB_OTG_FS 

  RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOA , ENABLE);  
  
  /* Configure SOF VBUS ID DM DP Pins */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9  | 
      GPIO_Pin_11 | 
        GPIO_Pin_12;
  
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
  GPIO_Init(GPIOA, &GPIO_InitStructure);  
  
  GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_OTG1_FS) ; 
  GPIO_PinAFConfig(GPIOA,GPIO_PinSource11,GPIO_AF_OTG1_FS) ; 
  GPIO_PinAFConfig(GPIOA,GPIO_PinSource12,GPIO_AF_OTG1_FS) ;
  
  /* this for ID line debug */
  
  
  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_10;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;  
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_Init(GPIOA, &GPIO_InitStructure);  
  GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_OTG1_FS) ;   


  RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
  RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_OTG_FS, ENABLE) ; 
 #else // USE_USB_OTG_HS 

  #ifdef USE_ULPI_PHY // ULPI
  RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB | 
                         RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOH | 
                           RCC_AHB1Periph_GPIOI, ENABLE);    
  
  
  GPIO_PinAFConfig(GPIOA,GPIO_PinSource3, GPIO_AF_OTG2_HS) ; // D0
  GPIO_PinAFConfig(GPIOA,GPIO_PinSource5, GPIO_AF_OTG2_HS) ; // CLK
  GPIO_PinAFConfig(GPIOB,GPIO_PinSource0, GPIO_AF_OTG2_HS) ; // D1
  GPIO_PinAFConfig(GPIOB,GPIO_PinSource1, GPIO_AF_OTG2_HS) ; // D2
  GPIO_PinAFConfig(GPIOB,GPIO_PinSource5, GPIO_AF_OTG2_HS) ; // D7
  GPIO_PinAFConfig(GPIOB,GPIO_PinSource10,GPIO_AF_OTG2_HS) ; // D3
  GPIO_PinAFConfig(GPIOB,GPIO_PinSource11,GPIO_AF_OTG2_HS) ; // D4
  GPIO_PinAFConfig(GPIOB,GPIO_PinSource12,GPIO_AF_OTG2_HS) ; // D5
  GPIO_PinAFConfig(GPIOB,GPIO_PinSource13,GPIO_AF_OTG2_HS) ; // D6
  GPIO_PinAFConfig(GPIOH,GPIO_PinSource4, GPIO_AF_OTG2_HS) ; // NXT
  GPIO_PinAFConfig(GPIOI,GPIO_PinSource11,GPIO_AF_OTG2_HS) ; // DIR
  GPIO_PinAFConfig(GPIOC,GPIO_PinSource0, GPIO_AF_OTG2_HS) ; // STP
  
  // CLK
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 ; 
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_Init(GPIOA, &GPIO_InitStructure);  
  
  // D0
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3  ; 
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
  GPIO_Init(GPIOA, &GPIO_InitStructure);  
  
  
  
  // D1 D2 D3 D4 D5 D6 D7
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1  |
    GPIO_Pin_5 | GPIO_Pin_10 | 
      GPIO_Pin_11| GPIO_Pin_12 | 
        GPIO_Pin_13 ;
  
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
  GPIO_Init(GPIOB, &GPIO_InitStructure);  
  
  
  // STP
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0  ;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_Init(GPIOC, &GPIO_InitStructure);  
  
  //NXT  
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_Init(GPIOH, &GPIO_InitStructure);  
  
  
  //DIR
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 ; 
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_Init(GPIOI, &GPIO_InitStructure);  
  
  
  RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_OTG_HS | 
                         RCC_AHB1Periph_OTG_HS_ULPI, ENABLE) ;    
   
  #else
  
   #ifdef USE_I2C_PHY    
  RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOB , ENABLE);  
  /* Configure RESET INTN SCL SDA (Phy/I2C) Pins */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | 
    GPIO_Pin_1 | 
      GPIO_Pin_10 | 
        GPIO_Pin_11;
  
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_Init(GPIOB, &GPIO_InitStructure);  
  
  GPIO_PinAFConfig(GPIOB,GPIO_PinSource0,GPIO_AF_OTG2_FS) ; 
  GPIO_PinAFConfig(GPIOB,GPIO_PinSource1,GPIO_AF_OTG2_FS) ; 
  GPIO_PinAFConfig(GPIOB,GPIO_PinSource10,GPIO_AF_OTG2_FS) ; 
  GPIO_PinAFConfig(GPIOB,GPIO_PinSource11,GPIO_AF_OTG2_FS);
  RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_OTG_HS, ENABLE) ;  
  
   #else

  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB , ENABLE);
  
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12  | 
    GPIO_Pin_13 |
      GPIO_Pin_14 | 
        GPIO_Pin_15;
  
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_Init(GPIOB, &GPIO_InitStructure);  
  
  GPIO_PinAFConfig(GPIOB,GPIO_PinSource12, GPIO_AF_OTG2_FS) ; 
  GPIO_PinAFConfig(GPIOB,GPIO_PinSource13,GPIO_AF_OTG2_FS) ; 
  GPIO_PinAFConfig(GPIOB,GPIO_PinSource14,GPIO_AF_OTG2_FS) ; 
  GPIO_PinAFConfig(GPIOB,GPIO_PinSource15,GPIO_AF_OTG2_FS) ;
  RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_OTG_HS, ENABLE) ;  
   #endif    
  #endif
 #endif //USB_OTG_HS

  /* Intialize Timer for delay function */
  USB_OTG_BSP_TimeInit();   
}
/**
  * @brief  Configures all SDRAM memory I/Os pins. 
  * @param  None. 
  * @retval None.
  */
void NOR_GPIOConfig(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
  
  /* Enable GPIO Clock */
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD | RCC_AHB1Periph_GPIOG | RCC_AHB1Periph_GPIOE |
                         RCC_AHB1Periph_GPIOF, ENABLE);

/* GPIOs Configuration -------------------------------------------------------*/
/*
 +-------------------+--------------------+--------------------+--------------------+
 +                       NOR flash pins assignment             +                    +
 +-------------------+--------------------+--------------------+--------------------+
 | PD0  <-> FMC_D2   | PE2  <-> FMC_A23   | PF0  <-> FMC_A0    | PG0 <-> FMC_A10    | 
 | PD1  <-> FMC_D3   | PE3  <-> FMC_A19   | PF1  <-> FMC_A1    | PG1 <-> FMC_A11    | 
 | PD3  <-> FMC_CLK  | PE4  <-> FMC_A20   | PF2  <-> FMC_A2    | PG2 <-> FMC_A12    | 
 | PD4  <-> FMC_NOE  | PE5  <-> FMC_A21   | PF3  <-> FMC_A3    | PG3 <-> FMC_A13    | 
 | PD5  <-> FMC_NWE  | PE6  <-> FMC_A22   | PF4  <-> FMC_A4    | PG4 <-> FMC_A14    | 
 | PD6  <-> FMC_NWAIT| PE7  <-> FMC_D4    | PF5  <-> FMC_A5    | PG5 <-> FMC_A15    |
 | PD7  <-> FMC_NE1  | PE8  <-> FMC_D5    | PF12 <-> FMC_A6    |--------------------+   
 | PD8  <-> FMC_D13  | PE9  <-> FMC_D6    | PF13 <-> FMC_A7    | 
 | PD9  <-> FMC_D14  | PE10 <-> FMC_D7    | PF14 <-> FMC_A8    | 
 | PD10 <-> FMC_D15  | PE11 <-> FMC_D8    | PF15 <-> FMC_A9    |                    
 | PD11 <-> FMC_A16  | PE12 <-> FMC_D9    | -------------------+                  
 | PD12 <-> FMC_A17  | PE13 <-> FMC_D10   |                     
 | PD13 <-> FMC_A18  | PE14 <-> FMC_D11   | 
 | PD14 <-> FMC_D0   | PE15 <-> FMC_D12   |
 | PD15 <-> FMC_D1   |--------------------+  
 +-------------------+
 
*/   
  
/* GPIOs Configuration --------------------------------------------------------*/
  /* Common GPIO configuration */
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL; 
  
  /* GPIOD configuration */
  GPIO_PinAFConfig(GPIOD, GPIO_PinSource0, GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOD, GPIO_PinSource1, GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOD, GPIO_PinSource3, GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOD, GPIO_PinSource4, GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOD, GPIO_PinSource5, GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOD, GPIO_PinSource6, GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOD, GPIO_PinSource7, GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOD, GPIO_PinSource8, GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOD, GPIO_PinSource9, GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOD, GPIO_PinSource10, GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOD, GPIO_PinSource11, GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOD, GPIO_PinSource12, GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOD, GPIO_PinSource13, GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOD, GPIO_PinSource14, GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOD, GPIO_PinSource15, GPIO_AF_FMC);
  
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0  | GPIO_Pin_1  |GPIO_Pin_3  | GPIO_Pin_4 | 
                                GPIO_Pin_5  | GPIO_Pin_6  |GPIO_Pin_7  | GPIO_Pin_8 |
                                GPIO_Pin_9  | GPIO_Pin_10 |GPIO_Pin_11 |GPIO_Pin_12 |
                                GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;

  GPIO_Init(GPIOD, &GPIO_InitStructure);

  /* GPIOE configuration */
  GPIO_PinAFConfig(GPIOE, GPIO_PinSource2, GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOE, GPIO_PinSource3, GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOE, GPIO_PinSource4, GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOE, GPIO_PinSource5, GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOE, GPIO_PinSource6, GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOE, GPIO_PinSource7, GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOE, GPIO_PinSource8, GPIO_AF_FMC); 
  GPIO_PinAFConfig(GPIOE, GPIO_PinSource9, GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOE, GPIO_PinSource10, GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOE, GPIO_PinSource11, GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOE, GPIO_PinSource12, GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOE, GPIO_PinSource13, GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOE, GPIO_PinSource14, GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOE, GPIO_PinSource15, GPIO_AF_FMC);
  
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2  |GPIO_Pin_3  | GPIO_Pin_4 |GPIO_Pin_5  | 
                                GPIO_Pin_6  |GPIO_Pin_7  | GPIO_Pin_8 |GPIO_Pin_9  | 
                                GPIO_Pin_10 |GPIO_Pin_11 |GPIO_Pin_12 |GPIO_Pin_13 |
                                GPIO_Pin_14 | GPIO_Pin_15;
                                
  GPIO_Init(GPIOE, &GPIO_InitStructure);

  /* GPIOF configuration */ 
  GPIO_PinAFConfig(GPIOF,GPIO_PinSource0, GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOF,GPIO_PinSource1, GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOF,GPIO_PinSource2, GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOF,GPIO_PinSource3, GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOF,GPIO_PinSource4, GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOF,GPIO_PinSource5, GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOF,GPIO_PinSource12, GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOF,GPIO_PinSource13, GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOF,GPIO_PinSource14, GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOF,GPIO_PinSource15, GPIO_AF_FMC);
  
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0  |GPIO_Pin_1  | GPIO_Pin_2  | GPIO_Pin_3  | 
                                GPIO_Pin_4  |GPIO_Pin_5  | GPIO_Pin_12 | GPIO_Pin_13 | 
                                GPIO_Pin_14 |GPIO_Pin_15;

  GPIO_Init(GPIOF, &GPIO_InitStructure);

  /* GPIOG configuration */
  GPIO_PinAFConfig(GPIOG,GPIO_PinSource0, GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOG,GPIO_PinSource1, GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOG,GPIO_PinSource2, GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOG,GPIO_PinSource3, GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOG,GPIO_PinSource4, GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOG,GPIO_PinSource5, GPIO_AF_FMC);
  
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 |GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | 
                                GPIO_Pin_4 |GPIO_Pin_5;

  GPIO_Init(GPIOG, &GPIO_InitStructure); 

}
Esempio n. 15
0
/**
  * @brief  Configures all SDRAM memory I/Os pins.
  * @param  None.
  * @retval None.
  */
void SDRAM_GPIOConfig(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;

  /* Enable GPIOs clock */
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOD | RCC_AHB1Periph_GPIOE |
                         RCC_AHB1Periph_GPIOF | RCC_AHB1Periph_GPIOG | RCC_AHB1Periph_GPIOH |
                         RCC_AHB1Periph_GPIOI, ENABLE);

/*-- GPIOs Configuration -----------------------------------------------------*/
/*
 +-------------------+--------------------+--------------------+--------------------+
 +                       SDRAM pins assignment                                      +
 +-------------------+--------------------+--------------------+--------------------+
 | PD0  <-> FMC_D2   | PE0  <-> FMC_NBL0  | PF0  <-> FMC_A0    | PG0 <-> FMC_A10    |
 | PD1  <-> FMC_D3   | PE1  <-> FMC_NBL1  | PF1  <-> FMC_A1    | PG1 <-> FMC_A11    |
 | PD8  <-> FMC_D13  | PE7  <-> FMC_D4    | PF2  <-> FMC_A2    | PG4 <-> FMC_A14    |
 | PD9  <-> FMC_D14  | PE8  <-> FMC_D5    | PF3  <-> FMC_A3    | PG5 <-> FMC_A15    |
 | PD10 <-> FMC_D15  | PE9  <-> FMC_D6    | PF4  <-> FMC_A4    | PG8 <-> FC_SDCLK   |
 | PD14 <-> FMC_D0   | PE10 <-> FMC_D7    | PF5  <-> FMC_A5    | PG15 <-> FMC_NCAS  |
 | PD15 <-> FMC_D1   | PE11 <-> FMC_D8    | PF11 <-> FC_NRAS   |--------------------+
 +-------------------| PE12 <-> FMC_D9    | PF12 <-> FMC_A6    |
                     | PE13 <-> FMC_D10   | PF13 <-> FMC_A7    |
                     | PE14 <-> FMC_D11   | PF14 <-> FMC_A8    |
                     | PE15 <-> FMC_D12   | PF15 <-> FMC_A9    |
 +-------------------+--------------------+--------------------+
 | PH2 <-> FMC_SDCKE0| PI4 <-> FMC_NBL2   |
 | PH3 <-> FMC_SDNE0 | PI5 <-> FMC_NBL3   |
 | PH5 <-> FMC_SDNW  |--------------------+
 +-------------------+
 +-------------------+------------------+
 +   32-bits Mode: D31-D16              +
 +-------------------+------------------+
 | PH8 <-> FMC_D16   | PI0 <-> FMC_D24  |
 | PH9 <-> FMC_D17   | PI1 <-> FMC_D25  |
 | PH10 <-> FMC_D18  | PI2 <-> FMC_D26  |
 | PH11 <-> FMC_D19  | PI3 <-> FMC_D27  |
 | PH12 <-> FMC_D20  | PI6 <-> FMC_D28  |
 | PH13 <-> FMC_D21  | PI7 <-> FMC_D29  |
 | PH14 <-> FMC_D22  | PI9 <-> FMC_D30  |
 | PH15 <-> FMC_D23  | PI10 <-> FMC_D31 |
 +------------------+-------------------+

 +-------------------+
 +  Pins remapping   +
 +-------------------+
 | PC0 <-> FMC_SDNWE |
 | PC2 <-> FMC_SDNE0 |
 | PC3 <-> FMC_SDCKE0|
 +-------------------+

*/

  /* Common GPIO configuration */
  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;

  /* GPIOD configuration */
  GPIO_PinAFConfig(GPIOD, GPIO_PinSource0, GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOD, GPIO_PinSource1, GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOD, GPIO_PinSource8, GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOD, GPIO_PinSource9, GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOD, GPIO_PinSource10, GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOD, GPIO_PinSource14, GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOD, GPIO_PinSource15, GPIO_AF_FMC);

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0  |GPIO_Pin_1  |GPIO_Pin_8 |GPIO_Pin_9 |
                                GPIO_Pin_10 |GPIO_Pin_14 |GPIO_Pin_15;

  GPIO_Init(GPIOD, &GPIO_InitStructure);

  /* GPIOE configuration */
  GPIO_PinAFConfig(GPIOE, GPIO_PinSource0 , GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOE, GPIO_PinSource1 , GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOE, GPIO_PinSource7 , GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOE, GPIO_PinSource8 , GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOE, GPIO_PinSource9 , GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOE, GPIO_PinSource10 , GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOE, GPIO_PinSource11 , GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOE, GPIO_PinSource12 , GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOE, GPIO_PinSource13 , GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOE, GPIO_PinSource14 , GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOE, GPIO_PinSource15 , GPIO_AF_FMC);

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0  | GPIO_Pin_1  | GPIO_Pin_7 | GPIO_Pin_8  |
                                GPIO_Pin_9  | GPIO_Pin_10 | GPIO_Pin_11| GPIO_Pin_12 |
                                GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;

  GPIO_Init(GPIOE, &GPIO_InitStructure);

  /* GPIOF configuration */
  GPIO_PinAFConfig(GPIOF, GPIO_PinSource0 , GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOF, GPIO_PinSource1 , GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOF, GPIO_PinSource2 , GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOF, GPIO_PinSource3 , GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOF, GPIO_PinSource4 , GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOF, GPIO_PinSource5 , GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOF, GPIO_PinSource11 , GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOF, GPIO_PinSource12 , GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOF, GPIO_PinSource13 , GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOF, GPIO_PinSource14 , GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOF, GPIO_PinSource15 , GPIO_AF_FMC);

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0  | GPIO_Pin_1  | GPIO_Pin_2  | GPIO_Pin_3  |
                                GPIO_Pin_4  | GPIO_Pin_5  | GPIO_Pin_11 | GPIO_Pin_12 |
                                GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;

  GPIO_Init(GPIOF, &GPIO_InitStructure);

  /* GPIOG configuration */
  GPIO_PinAFConfig(GPIOG, GPIO_PinSource0 , GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOG, GPIO_PinSource1 , GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOG, GPIO_PinSource4 , GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOG, GPIO_PinSource5 , GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOG, GPIO_PinSource8 , GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOG, GPIO_PinSource15 , GPIO_AF_FMC);


  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 |GPIO_Pin_1 |GPIO_Pin_4 |GPIO_Pin_5 |
                                GPIO_Pin_8 | GPIO_Pin_15;

  GPIO_Init(GPIOG, &GPIO_InitStructure);

  /* GPIOH configuration */
  GPIO_PinAFConfig(GPIOH, GPIO_PinSource2 , GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOH, GPIO_PinSource3 , GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOH, GPIO_PinSource5 , GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOH, GPIO_PinSource8 , GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOH, GPIO_PinSource9 , GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOH, GPIO_PinSource10 , GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOH, GPIO_PinSource11 , GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOH, GPIO_PinSource12 , GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOH, GPIO_PinSource13 , GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOH, GPIO_PinSource14 , GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOH, GPIO_PinSource15 , GPIO_AF_FMC);


  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2  | GPIO_Pin_3  | GPIO_Pin_5 | GPIO_Pin_8  |
                                GPIO_Pin_9  | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12 |
                                GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;

  GPIO_Init(GPIOH, &GPIO_InitStructure);

  /* GPIOI configuration */
  GPIO_PinAFConfig(GPIOI, GPIO_PinSource0 , GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOI, GPIO_PinSource1 , GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOI, GPIO_PinSource2 , GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOI, GPIO_PinSource3 , GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOI, GPIO_PinSource4 , GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOI, GPIO_PinSource5 , GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOI, GPIO_PinSource6 , GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOI, GPIO_PinSource7 , GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOI, GPIO_PinSource9 , GPIO_AF_FMC);
  GPIO_PinAFConfig(GPIOI, GPIO_PinSource10 , GPIO_AF_FMC);

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 |
                                GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7 |
                                        GPIO_Pin_9 | GPIO_Pin_10;

  GPIO_Init(GPIOI, &GPIO_InitStructure);

}
Esempio n. 16
0
void uart1Init(void)
{
    GPIO_InitTypeDef  GPIO_InitStructure;
    USART_InitTypeDef USART_InitStructure;
    DMA_InitTypeDef   DMA_InitStructure;
    NVIC_InitTypeDef  NVIC_InitStructure;

    GPIO_PinAFConfig(UART1_GPIO, UART1_TX_PINSOURCE, GPIO_AF_USART1);
    GPIO_PinAFConfig(UART1_GPIO, UART1_RX_PINSOURCE, GPIO_AF_USART1);

    GPIO_InitStructure.GPIO_Pin   = UART1_TX_PIN | UART1_RX_PIN;
    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_Init(UART1_GPIO, &GPIO_InitStructure);

    // DMA TX Interrupt
    NVIC_InitStructure.NVIC_IRQChannel                   = DMA2_Stream7_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority        = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;

    NVIC_Init(&NVIC_InitStructure);

    USART_InitStructure.USART_BaudRate            = 115200;
    USART_InitStructure.USART_WordLength          = USART_WordLength_8b;
    USART_InitStructure.USART_StopBits            = USART_StopBits_1;
    USART_InitStructure.USART_Parity              = USART_Parity_No;
    USART_InitStructure.USART_Mode                = USART_Mode_Rx | USART_Mode_Tx;
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;

    USART_Init(USART1, &USART_InitStructure);

    // Receive DMA into a circular buffer

    DMA_DeInit(DMA2_Stream5);

    DMA_InitStructure.DMA_Channel            = DMA_Channel_4;
    DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&USART1->DR;
    DMA_InitStructure.DMA_Memory0BaseAddr    = (uint32_t)rx1Buffer;
    DMA_InitStructure.DMA_DIR                = DMA_DIR_PeripheralToMemory;
    DMA_InitStructure.DMA_BufferSize         = UART1_BUFFER_SIZE;
    DMA_InitStructure.DMA_PeripheralInc      = DMA_PeripheralInc_Disable;
    DMA_InitStructure.DMA_MemoryInc          = DMA_MemoryInc_Enable;
    DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
    DMA_InitStructure.DMA_MemoryDataSize     = DMA_MemoryDataSize_Byte;
    DMA_InitStructure.DMA_Mode               = DMA_Mode_Circular;
    DMA_InitStructure.DMA_Priority           = DMA_Priority_Medium;
    DMA_InitStructure.DMA_FIFOMode           = DMA_FIFOMode_Disable;
    DMA_InitStructure.DMA_FIFOThreshold      = DMA_FIFOThreshold_1QuarterFull;
    DMA_InitStructure.DMA_MemoryBurst        = DMA_MemoryBurst_Single;
    DMA_InitStructure.DMA_PeripheralBurst    = DMA_PeripheralBurst_Single;

    DMA_Init(DMA2_Stream5, &DMA_InitStructure);

    DMA_Cmd(DMA2_Stream5, ENABLE);

    USART_DMACmd(USART1, USART_DMAReq_Rx, ENABLE);

    rx1DMAPos = DMA_GetCurrDataCounter(DMA2_Stream5);

    // Transmit DMA
    DMA_DeInit(DMA2_Stream7);

  //DMA_InitStructure.DMA_Channel            = DMA_Channel_4;
  //DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&USART1->DR;
    DMA_InitStructure.DMA_Memory0BaseAddr    = (uint32_t)tx1Buffer;
    DMA_InitStructure.DMA_DIR                = DMA_DIR_MemoryToPeripheral;
  //DMA_InitStructure.DMA_BufferSize         = UART_BUFFER_SIZE;
  //DMA_InitStructure.DMA_PeripheralInc      = DMA_PeripheralInc_Disable;
  //DMA_InitStructure.DMA_MemoryInc          = DMA_MemoryInc_Enable;
  //DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
  //DMA_InitStructure.DMA_MemoryDataSize     = DMA_MemoryDataSize_Byte;
    DMA_InitStructure.DMA_Mode               = DMA_Mode_Normal;
  //DMA_InitStructure.DMA_Priority           = DMA_Priority_Medium;
  //DMA_InitStructure.DMA_FIFOMode           = DMA_FIFOMode_Disable;
  //DMA_InitStructure.DMA_FIFOThreshold      = DMA_FIFOThreshold_1QuarterFull;
  //DMA_InitStructure.DMA_MemoryBurst        = DMA_MemoryBurst_Single;
  //DMA_InitStructure.DMA_PeripheralBurst    = DMA_PeripheralBurst_Single;

    DMA_Init(DMA2_Stream7, &DMA_InitStructure);

    DMA_SetCurrDataCounter(DMA2_Stream7, 0);

    DMA_ITConfig(DMA2_Stream7, DMA_IT_TC, ENABLE);

    USART_DMACmd(USART1, USART_DMAReq_Tx, ENABLE);

    USART_Cmd(USART1, ENABLE);

    evrRegisterListener(uart1ListenerCB);
}
void spi_init(void)
	{
	    GPIO_InitTypeDef GPIO_InitStructure;
	    SPI_InitTypeDef  SPI_InitStructure;

	    /* Enable the SPI periph */
	    RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);//RCC_APB2Periph_SPI1

	    /* Enable SCK, MOSI and MISO GPIO clocks */
	    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA | RCC_AHBPeriph_GPIOC, ENABLE);

	    /* Enable CS  GPIO clock */
//	    RCC_AHB1PeriphClockCmd(LIS302DL_SPI_CS_GPIO_CLK, ENABLE);

	    /* Enable INT1 GPIO clock */
	    //RCC_AHB1PeriphClockCmd(LIS302DL_SPI_INT1_GPIO_CLK, ENABLE);

	    /* Enable INT2 GPIO clock */
	    //RCC_AHB1PeriphClockCmd(LIS302DL_SPI_INT2_GPIO_CLK, ENABLE);

	    GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_0);
	    GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_0);
	    GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_0);

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

	    /* SPI SCK pin configuration */
	    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
	    GPIO_Init(GPIOA, &GPIO_InitStructure);

	    /* SPI  MOSI pin configuration */
	    GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_7;
	    GPIO_Init(GPIOA, &GPIO_InitStructure);

	    /* SPI MISO pin configuration */
	    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
	    GPIO_Init(GPIOA, &GPIO_InitStructure);

	    /* SPI configuration -------------------------------------------------------*/
	    SPI_I2S_DeInit(SPI1);
	    SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
	    SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
	    SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
	    SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
	    SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
	    SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
	    SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
	    SPI_InitStructure.SPI_CRCPolynomial = 7;
	    SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
	    SPI_Init(SPI1, &SPI_InitStructure);

	    /* Enable SPI1  */
	    SPI_Cmd(SPI1, ENABLE);

	    /* Configure GPIO PIN for Lis Chip select */
	    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
	    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
	    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	    GPIO_Init(GPIOC, &GPIO_InitStructure);

	    /* Deselect : Chip Select high */
	    GPIO_SetBits(GPIOC,GPIO_Pin_13);
	}
Esempio n. 18
0
void pyb_cc3000_spi_init(void) {
    DEBUG_printf("pyb_cc3000_spi_init\n");

    // enable SPI clock
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);

    // GPIO clocks should already be enabled

    /*!< SPI pins configuration *************************************************/

    /*!< Connect SPI pins to AF5 */
    GPIO_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF_SPI2);
    GPIO_PinAFConfig(GPIOB, GPIO_PinSource14, GPIO_AF_SPI2);
    GPIO_PinAFConfig(GPIOB, GPIO_PinSource15, GPIO_AF_SPI2);

    GPIO_InitTypeDef GPIO_InitStructure;
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
    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_DOWN;
    GPIO_Init(GPIOB, &GPIO_InitStructure);

    /*
  inf.baudRate = 100000; // FIXME - just slow for debug
  inf.spiMode = SPIF_SPI_MODE_1;  // Mode 1   CPOL= 0  CPHA= 1
  */

    /*!< SPI configuration */
    SPI_InitTypeDef SPI_InitStructure;
    SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
    SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
    SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; // should be correct
    SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; // clock is low when idle
    SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; // data latched on second edge, which is falling edge for low-idle
    SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; // software control
    SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8; // clock freq = f_PCLK / this_prescale_value
    SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; // should be correct
    SPI_InitStructure.SPI_CRCPolynomial = 7; // ?
    SPI_Init(SPI2, &SPI_InitStructure);

    /*!< Enable the SPI  */
    SPI_Cmd(SPI2, ENABLE);

  /*
  // WLAN CS, EN and WALN IRQ Configuration
  jshSetPinStateIsManual(WLAN_CS_PIN, false);
  jshPinOutput(WLAN_CS_PIN, 1); // de-assert CS
  jshSetPinStateIsManual(WLAN_EN_PIN, false);
  jshPinOutput(WLAN_EN_PIN, 0); // disable WLAN
  jshSetPinStateIsManual(WLAN_IRQ_PIN, true);
  jshPinSetState(WLAN_IRQ_PIN, JSHPINSTATE_GPIO_IN_PULLUP); // flip into read mode with pullup
  */
    // configure wlan CS and EN pins
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_Init(GPIOC, &GPIO_InitStructure);
    pyb_cc3000_set_cs(1); // de-assert CS
    pyb_cc3000_set_en(0); // disable wlan

    // configure EXTI on A14
    EXTILine14_Config();

    // wait a little (ensure that WLAN takes effect)
    sys_tick_delay_ms(500); // force a 500ms delay! FIXME
}
Esempio n. 19
0
OSStatus host_platform_bus_init( void )
{
    SPI_InitTypeDef  spi_init;
    DMA_InitTypeDef  dma_init_structure;
    GPIO_InitTypeDef gpio_init_structure;
    NVIC_InitTypeDef nvic_init_structure;

    MCU_CLOCKS_NEEDED();

    mico_rtos_init_semaphore(&spi_transfer_finished_semaphore, 1);

    /* Enable SPI_SLAVE DMA clock */
    RCC_AHB1PeriphClockCmd( SPIX_DMA_CLK, ENABLE );

    /* Enable SPI_SLAVE Periph clock */
    SPIX_CLK_FUNCTION( SPIX_CLK, ENABLE );

    /* Enable GPIO Bank B & C */
    RCC_AHB1PeriphClockCmd( SPI_BUS_CLOCK_BANK_CLK | SPI_BUS_MISO_BANK_CLK | SPI_BUS_MOSI_BANK_CLK | SPI_BUS_CS_BANK_CLK | SPI_IRQ_CLK, ENABLE );

    /* Enable SYSCFG. Needed for selecting EXTI interrupt line */
    RCC_APB2PeriphClockCmd( RCC_APB2Periph_SYSCFG, ENABLE );

    /* Setup the interrupt input for WLAN_IRQ */
    gpio_init_structure.GPIO_Mode = GPIO_Mode_IN;
    gpio_init_structure.GPIO_PuPd = GPIO_PuPd_NOPULL;
    gpio_init_structure.GPIO_Speed = GPIO_Speed_100MHz;
    gpio_init_structure.GPIO_Pin = ( 1 << SPI_IRQ_PIN );
    GPIO_Init( SPI_IRQ_BANK, &gpio_init_structure );

    gpio_irq_enable(SPI_IRQ_BANK, SPI_IRQ_PIN, IRQ_TRIGGER_RISING_EDGE, spi_irq_handler, 0);

    /* Setup the SPI lines */
    /* Setup SPI slave select GPIOs */
    gpio_init_structure.GPIO_Mode = GPIO_Mode_AF;
    gpio_init_structure.GPIO_OType = GPIO_OType_PP;
    gpio_init_structure.GPIO_Speed = GPIO_Speed_100MHz;
    gpio_init_structure.GPIO_Pin = ( 1 << SPI_BUS_CLOCK_PIN ) | ( 1 << SPI_BUS_MISO_PIN ) | ( 1 << SPI_BUS_MOSI_PIN );
    GPIO_Init( SPI_BUS_CLOCK_BANK, &gpio_init_structure );
    GPIO_PinAFConfig( SPI_BUS_CLOCK_BANK, SPI_BUS_CLOCK_PIN, SPIX_AF );
    GPIO_PinAFConfig( SPI_BUS_MISO_BANK, SPI_BUS_MISO_PIN, SPIX_AF );
    GPIO_PinAFConfig( SPI_BUS_MOSI_BANK, SPI_BUS_MOSI_PIN, SPIX_AF );

    /* Setup SPI slave select GPIOs */
    gpio_init_structure.GPIO_Mode = GPIO_Mode_OUT;
    gpio_init_structure.GPIO_OType = GPIO_OType_PP;
    gpio_init_structure.GPIO_Speed = GPIO_Speed_100MHz;
    gpio_init_structure.GPIO_Pin = ( 1 << SPI_BUS_CS_PIN );
    GPIO_Init( SPI_BUS_CS_BANK, &gpio_init_structure );
    GPIO_SetBits( SPI_BUS_CS_BANK, ( 1 << SPI_BUS_CS_PIN ) ); /* Set CS high (disabled) */

    /* Set GPIO_B[1:0] to 01 to put WLAN module into gSPI mode */
    MicoGpioInitialize( (mico_gpio_t)WL_GPIO0, OUTPUT_PUSH_PULL );
    MicoGpioOutputHigh( (mico_gpio_t)WL_GPIO0 );
    
    MicoGpioInitialize( (mico_gpio_t)WL_GPIO1, OUTPUT_PUSH_PULL );
    MicoGpioOutputLow( (mico_gpio_t)WL_GPIO1 );

    /* Setup DMA for SPIX RX */
    DMA_DeInit( SPIX_DMA_RX_STREAM );
    dma_init_structure.DMA_Channel = SPIX_DMA_RX_CHANNEL;
    dma_init_structure.DMA_PeripheralBaseAddr = (uint32_t) &SPIX->DR;
    dma_init_structure.DMA_Memory0BaseAddr = 0;
    dma_init_structure.DMA_DIR = DMA_DIR_PeripheralToMemory;
    dma_init_structure.DMA_BufferSize = 0;
    dma_init_structure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
    dma_init_structure.DMA_MemoryInc = DMA_MemoryInc_Enable;
    dma_init_structure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
    dma_init_structure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
    dma_init_structure.DMA_Mode = DMA_Mode_Normal;
    dma_init_structure.DMA_Priority = DMA_Priority_VeryHigh;
    dma_init_structure.DMA_FIFOMode = DMA_FIFOMode_Disable;
    dma_init_structure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
    dma_init_structure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
    dma_init_structure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
    DMA_Init( SPIX_DMA_RX_STREAM, &dma_init_structure );

    /* Setup DMA for SPIX TX */
    DMA_DeInit( SPIX_DMA_TX_STREAM );
    dma_init_structure.DMA_Channel = SPIX_DMA_TX_CHANNEL;
    dma_init_structure.DMA_PeripheralBaseAddr = (uint32_t) &SPIX->DR;
    dma_init_structure.DMA_Memory0BaseAddr = 0;
    dma_init_structure.DMA_DIR = DMA_DIR_MemoryToPeripheral;
    dma_init_structure.DMA_BufferSize = 0;
    dma_init_structure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
    dma_init_structure.DMA_MemoryInc = DMA_MemoryInc_Enable;
    dma_init_structure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
    dma_init_structure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
    dma_init_structure.DMA_Mode = DMA_Mode_Normal;
    dma_init_structure.DMA_Priority = DMA_Priority_VeryHigh;
    dma_init_structure.DMA_FIFOMode = DMA_FIFOMode_Disable;
    dma_init_structure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
    dma_init_structure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
    dma_init_structure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
    DMA_Init( SPIX_DMA_TX_STREAM, &dma_init_structure );

    /* Must be lower priority than the value of configMAX_SYSCALL_INTERRUPT_PRIORITY */
    /* otherwise FreeRTOS will not be able to mask the interrupt */
    /* keep in mind that ARMCM3 interrupt priority logic is inverted, the highest value */
    /* is the lowest priority */
    nvic_init_structure.NVIC_IRQChannel                   = SPIX_DMA_RX_IRQ_CHANNEL;
    nvic_init_structure.NVIC_IRQChannelPreemptionPriority = (uint8_t) 0x3;
    nvic_init_structure.NVIC_IRQChannelSubPriority        = 0x0;
    nvic_init_structure.NVIC_IRQChannelCmd                = ENABLE;
    NVIC_Init( &nvic_init_structure );
    DMA_ITConfig(SPIX_DMA_RX_STREAM, DMA_IT_TC, ENABLE);

    /* Enable DMA for TX */
    SPI_I2S_DMACmd( SPIX, SPI_I2S_DMAReq_Tx | SPI_I2S_DMAReq_Rx, ENABLE );

    /* Setup SPI */
    spi_init.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
    spi_init.SPI_Mode = SPI_Mode_Master;
    spi_init.SPI_DataSize = SPI_DataSize_8b;
    spi_init.SPI_CPOL = SPI_CPOL_High;
    spi_init.SPI_CPHA = SPI_CPHA_2Edge;
    spi_init.SPI_NSS = SPI_NSS_Soft;
    spi_init.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
    spi_init.SPI_FirstBit = SPI_FirstBit_MSB;
    spi_init.SPI_CRCPolynomial = (uint16_t) 7;

    /* Init SPI and enable it */
    SPI_Init( SPIX, &spi_init );
    SPI_Cmd( SPIX, ENABLE );

    MCU_CLOCKS_NOT_NEEDED();

    return kNoErr;
}
Esempio n. 20
0
/**
  * @brief  Initializes peripherals used by the I2C EEPROM driver.
  * @param  None
  * @retval None
  */
void sEE_LowLevel_Init(void)
{
	GPIO_InitTypeDef  GPIO_InitStructure; 
   
  /*!< sEE_I2C Periph clock enable */
  RCC_APB1PeriphClockCmd(sEE_I2C_CLK, ENABLE);
  
  /*!< sEE_I2C_SCL_GPIO_CLK and sEE_I2C_SDA_GPIO_CLK Periph clock enable */
  RCC_AHB1PeriphClockCmd(sEE_I2C_SCL_GPIO_CLK | sEE_I2C_SDA_GPIO_CLK, ENABLE);

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
  
  /* Reset sEE_I2C IP */
  RCC_APB1PeriphResetCmd(sEE_I2C_CLK, ENABLE);
  
  /* Release reset signal of sEE_I2C IP */
  RCC_APB1PeriphResetCmd(sEE_I2C_CLK, DISABLE);
    
  /*!< 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_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;
  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);

  /* 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);  
  
  /* Configure and enable I2C DMA TX Channel interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = sEE_I2C_DMA_TX_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = sEE_I2C_DMA_PREPRIO;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = sEE_I2C_DMA_SUBPRIO;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);

  /* Configure and enable I2C DMA RX Channel interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = sEE_I2C_DMA_RX_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = sEE_I2C_DMA_PREPRIO;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = sEE_I2C_DMA_SUBPRIO;
  NVIC_Init(&NVIC_InitStructure);  
  
  /*!< I2C DMA TX and RX channels configuration */
  /* Enable the DMA clock */
  RCC_AHB1PeriphClockCmd(sEE_I2C_DMA_CLK, ENABLE);
  
  /* Clear any pending flag on Rx Stream  */
  DMA_ClearFlag(sEE_I2C_DMA_STREAM_TX, sEE_TX_DMA_FLAG_FEIF | sEE_TX_DMA_FLAG_DMEIF | sEE_TX_DMA_FLAG_TEIF | \
                                       sEE_TX_DMA_FLAG_HTIF | sEE_TX_DMA_FLAG_TCIF);
  /* Disable the EE I2C Tx DMA stream */
  DMA_Cmd(sEE_I2C_DMA_STREAM_TX, DISABLE);
  /* Configure the DMA stream for the EE I2C peripheral TX direction */
  DMA_DeInit(sEE_I2C_DMA_STREAM_TX);
  sEEDMA_InitStructure.DMA_Channel = sEE_I2C_DMA_CHANNEL;
  sEEDMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)sEE_I2C_DR_Address;
  sEEDMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)0;    /* This parameter will be configured durig communication */;
  sEEDMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral; /* This parameter will be configured durig communication */
  sEEDMA_InitStructure.DMA_BufferSize = 0xFFFF;              /* This parameter will be configured durig communication */
  sEEDMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  sEEDMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
  sEEDMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
  sEEDMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
  sEEDMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
  sEEDMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
  sEEDMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Enable;
  sEEDMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
  sEEDMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
  sEEDMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
  DMA_Init(sEE_I2C_DMA_STREAM_TX, &sEEDMA_InitStructure);

  /* Clear any pending flag on Rx Stream */
//  DMA_ClearFlag(sEE_I2C_DMA_STREAM_RX, sEE_RX_DMA_FLAG_FEIF | sEE_RX_DMA_FLAG_DMEIF | sEE_RX_DMA_FLAG_TEIF | \
//                                       sEE_RX_DMA_FLAG_HTIF | sEE_RX_DMA_FLAG_TCIF);
  /* Disable the EE I2C DMA Rx stream */
  DMA_Cmd(sEE_I2C_DMA_STREAM_RX, DISABLE);
  /* Configure the DMA stream for the EE I2C peripheral RX direction */
  DMA_DeInit(sEE_I2C_DMA_STREAM_RX);
  DMA_Init(sEE_I2C_DMA_STREAM_RX, &sEEDMA_InitStructure);
  
  /* Enable the DMA Channels Interrupts */
  DMA_ITConfig(sEE_I2C_DMA_STREAM_TX, DMA_IT_TC, ENABLE);
  DMA_ITConfig(sEE_I2C_DMA_STREAM_RX, DMA_IT_TC, ENABLE);      
}
Esempio n. 21
0
File: UART6.c Progetto: welbur/zj
/*
*********************************************************************************************************
*	函 数 名: USART6_Init
*	功能说明: 初始化CPU的USART6串口硬件设备。预留
*	形    参:无
*	返 回 值: 无
*********************************************************************************************************
*/
void USART6_Init(_UART_BAUD BaudRate)
{
	USART_InitTypeDef USART_InitStructure;   
	GPIO_InitTypeDef GPIO_InitStructure;
	NVIC_InitTypeDef   NVIC_InitStructure;
	USART_ClockInitTypeDef USART_ClockInitStruct;
	
	RCC_AHB1PeriphClockCmd(USART6_TX_PORT_CLK | USART6_RX_PORT_CLK , ENABLE);
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART6, ENABLE);
	
	/* 配置发送管脚*/
  GPIO_PinAFConfig(USART6_TX_PORT, USART6_TX_SOURCE, GPIO_AF_USART6);

  /*配置接收管脚*/
  GPIO_PinAFConfig(USART6_RX_PORT, USART6_RX_SOURCE, GPIO_AF_USART6);

  /* Configure USART Tx as alternate function  */
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_SPEED;
  GPIO_InitStructure.GPIO_Pin = USART6_TX_PIN;  
  GPIO_Init(USART6_TX_PORT, &GPIO_InitStructure);

  /* Configure USART Rx as alternate function  */
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Pin = USART6_RX_PIN;
  GPIO_Init(USART6_RX_PORT, &GPIO_InitStructure);
	

       
  /* USARTx configured as follow:
        - BaudRate = 115200 baud  
        - Word Length = 8 Bits
        - One Stop Bit
        - No parity
        - Hardware flow control disabled (RTS and CTS signals)
        - Receive and transmit enabled
  */
  USART_InitStructure.USART_BaudRate = BaudRate;
  USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  USART_InitStructure.USART_StopBits = USART_StopBits_1;
  USART_InitStructure.USART_Parity = USART_Parity_No;
  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
	USART_Init(USART6, &USART_InitStructure);
	USART_ClockStructInit(&USART_ClockInitStruct);    //之前没有填入缺省值,是不行的
  USART_ClockInit(USART6, &USART_ClockInitStruct);
	
	NVIC_InitStructure.NVIC_IRQChannel =USART6_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
  NVIC_InitStructure.NVIC_IRQChannelCmd= ENABLE;
  NVIC_Init(&NVIC_InitStructure);
  
	#if UART6_DMA_RX_ENABLE
	/*空闲中断*/
  USART_ITConfig(USART6, USART_IT_IDLE , ENABLE);
  #else
	USART_ITConfig(USART6, USART_IT_RXNE | USART_IT_IDLE , ENABLE);
	#endif
	/* Enable USART */
  USART_Cmd(USART6, ENABLE);
  
  /* 
		CPU的小缺陷:串口配置好,如果直接Send,则第1个字节发送不出去
		如下语句解决第1个字节无法正确发送出去的问题:
	 	清发送完成标志,Transmission Complete flag 
	*/
	USART_ClearFlag(USART6, USART_FLAG_TC); 
	
	memset((u8*)&UART6_Str,0x00,sizeof(UART6_Str));
	#if UART6_DMA_RX_ENABLE
	  USART6_RX_DMA();
	#endif
	#if UART6_DMA_TX_ENABLE
		USART6_TX_DMA();
		UART6_Str.Send_Finish = 1;
	#endif

}	
Esempio n. 22
0
USART2Class::USART2Class(){
	m_queue = xQueueCreate(TX_BUFFERSIZE,sizeof(char));
	vQueueAddToRegistry(m_queue,"u2tx");
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD,ENABLE);

	GPIO_InitTypeDef pd5def;

	GPIO_StructInit(&pd5def);
	pd5def.GPIO_Pin = GPIO_Pin_5;
	pd5def.GPIO_Mode = GPIO_Mode_AF;
	pd5def.GPIO_OType = GPIO_OType_PP;
	pd5def.GPIO_PuPd = GPIO_PuPd_NOPULL;
	pd5def.GPIO_Speed = GPIO_Speed_100MHz;
	GPIO_Init(GPIOD,&pd5def);

	GPIO_InitTypeDef pd6def;
	GPIO_StructInit(&pd6def);
	pd6def.GPIO_Pin = GPIO_Pin_6;
	pd6def.GPIO_Mode = GPIO_Mode_AF;
	pd6def.GPIO_PuPd = GPIO_PuPd_NOPULL;
	pd6def.GPIO_Speed = GPIO_Speed_100MHz;
	GPIO_Init(GPIOD,&pd6def);

	GPIO_PinAFConfig(GPIOD, GPIO_PinSource5, GPIO_AF_USART2);
	GPIO_PinAFConfig(GPIOD, GPIO_PinSource6, GPIO_AF_USART2);


	USART_InitTypeDef usart2;

	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);

	USART_StructInit(&usart2);
	usart2.USART_BaudRate = 115200;
	usart2.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	usart2.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
	usart2.USART_Parity = USART_Parity_No;
	usart2.USART_StopBits = USART_StopBits_1;
	usart2.USART_WordLength = USART_WordLength_8b;

	USART_Init(USART2,&usart2);

	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1,ENABLE);


	DMA_InitTypeDef dma1_6;
	DMA_StructInit(&dma1_6);
	dma1_6.DMA_PeripheralBaseAddr = (uint32_t)&(USART2->DR);
	dma1_6.DMA_Memory0BaseAddr = (uint32_t)m_txBuf;
	dma1_6.DMA_DIR = DMA_DIR_MemoryToPeripheral;
	dma1_6.DMA_BufferSize = 1;
	dma1_6.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
	dma1_6.DMA_MemoryInc = DMA_MemoryInc_Enable;
	dma1_6.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
	dma1_6.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
	dma1_6.DMA_Mode = DMA_Mode_Normal;
	dma1_6.DMA_Priority = DMA_Priority_High;

	dma1_6.DMA_Channel = DMA_Channel_4;
	DMA_Init(DMA1_Stream6,&dma1_6);

	DMA_InitTypeDef dma1_5;
	DMA_StructInit(&dma1_5);
	dma1_5.DMA_PeripheralBaseAddr = (uint32_t)&(USART2->DR);
	dma1_5.DMA_Memory0BaseAddr = (uint32_t)m_rxBuf;
	dma1_5.DMA_DIR = DMA_DIR_PeripheralToMemory;
	dma1_5.DMA_BufferSize = RX_BUFFERSIZE;
	dma1_5.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
	dma1_5.DMA_MemoryInc = DMA_MemoryInc_Enable;
	dma1_5.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
	dma1_5.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
	dma1_5.DMA_Mode = DMA_Mode_Circular;
	dma1_5.DMA_Priority = DMA_Priority_Low;

	dma1_5.DMA_Channel = DMA_Channel_4;
	DMA_Init(DMA1_Stream5,&dma1_5);

	DMA_Cmd(DMA1_Stream5,ENABLE);


	for(int i=0;i<RX_BUFFERSIZE;i++){
		m_rxBuf[i] = 0;
	}
	
	USART_DMACmd(USART2,USART_DMAReq_Tx|USART_DMAReq_Rx, ENABLE);
	USART_Cmd(USART2, ENABLE);
}
Esempio n. 23
0
void USB_OTG_BSP_Init(USB_OTG_CORE_HANDLE *pdev)
{

// EXTI_InitTypeDef EXTI_InitStructure;
 #ifdef USE_STM3210C_EVAL

  RCC_OTGFSCLKConfig(RCC_OTGFSCLKSource_PLLVCO_Div3);
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_OTG_FS, ENABLE) ;

#else // USE_STM322xG_EVAL  
  GPIO_InitTypeDef GPIO_InitStructure;
 #ifdef USE_USB_OTG_FS 
 
  RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOA , ENABLE);  
  
  /* Configure SOF VBUS ID DM DP Pins */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8  | 
    GPIO_Pin_9  | 
      GPIO_Pin_11 | 
        GPIO_Pin_12;
  
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
  GPIO_Init(GPIOA, &GPIO_InitStructure);  
  
  GPIO_PinAFConfig(GPIOA,GPIO_PinSource8,GPIO_AF_OTG1_FS) ;
  GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_OTG1_FS) ; 
  GPIO_PinAFConfig(GPIOA,GPIO_PinSource11,GPIO_AF_OTG1_FS) ; 
  GPIO_PinAFConfig(GPIOA,GPIO_PinSource12,GPIO_AF_OTG1_FS) ;
  
  /* this for ID line debug */
  
  
  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_10;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;  
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_Init(GPIOA, &GPIO_InitStructure);  
  GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_OTG1_FS) ;   


  RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
  RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_OTG_FS, ENABLE) ; 
 #else // USE_USB_OTG_HS 

  #ifdef USE_ULPI_PHY // ULPI
  RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB | 
                         RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOH | 
                           RCC_AHB1Periph_GPIOI, ENABLE);    
  
  
  GPIO_PinAFConfig(GPIOA,GPIO_PinSource3, GPIO_AF_OTG2_HS) ; // D0
  GPIO_PinAFConfig(GPIOA,GPIO_PinSource5, GPIO_AF_OTG2_HS) ; // CLK
  GPIO_PinAFConfig(GPIOB,GPIO_PinSource0, GPIO_AF_OTG2_HS) ; // D1
  GPIO_PinAFConfig(GPIOB,GPIO_PinSource1, GPIO_AF_OTG2_HS) ; // D2
  GPIO_PinAFConfig(GPIOB,GPIO_PinSource5, GPIO_AF_OTG2_HS) ; // D7
  GPIO_PinAFConfig(GPIOB,GPIO_PinSource10,GPIO_AF_OTG2_HS) ; // D3
  GPIO_PinAFConfig(GPIOB,GPIO_PinSource11,GPIO_AF_OTG2_HS) ; // D4
  GPIO_PinAFConfig(GPIOB,GPIO_PinSource12,GPIO_AF_OTG2_HS) ; // D5
  GPIO_PinAFConfig(GPIOB,GPIO_PinSource13,GPIO_AF_OTG2_HS) ; // D6
  GPIO_PinAFConfig(GPIOH,GPIO_PinSource4, GPIO_AF_OTG2_HS) ; // NXT
  GPIO_PinAFConfig(GPIOI,GPIO_PinSource11,GPIO_AF_OTG2_HS) ; // DIR
  GPIO_PinAFConfig(GPIOC,GPIO_PinSource0, GPIO_AF_OTG2_HS) ; // STP
  
  // CLK
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 ; 
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_Init(GPIOA, &GPIO_InitStructure);  
  
  // D0
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3  ; 
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
  GPIO_Init(GPIOA, &GPIO_InitStructure);  
  
  
  
  // D1 D2 D3 D4 D5 D6 D7
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1  |
    GPIO_Pin_5 | GPIO_Pin_10 | 
      GPIO_Pin_11| GPIO_Pin_12 | 
        GPIO_Pin_13 ;
  
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
  GPIO_Init(GPIOB, &GPIO_InitStructure);  
  
  
  // STP
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0  ;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_Init(GPIOC, &GPIO_InitStructure);  
  
  //NXT  
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_Init(GPIOH, &GPIO_InitStructure);  
  
  
  //DIR
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 ; 
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_Init(GPIOI, &GPIO_InitStructure);  
  
  
  RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_OTG_HS | 
                         RCC_AHB1Periph_OTG_HS_ULPI, ENABLE) ;    
   
  #else

  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB , ENABLE);
  
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12  | 
    GPIO_Pin_13 |
      GPIO_Pin_14 | 
        GPIO_Pin_15;
  
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_Init(GPIOB, &GPIO_InitStructure);  
  
  GPIO_PinAFConfig(GPIOB,GPIO_PinSource12, GPIO_AF_OTG2_FS) ; 
  GPIO_PinAFConfig(GPIOB,GPIO_PinSource13,GPIO_AF_OTG2_FS) ; 
  GPIO_PinAFConfig(GPIOB,GPIO_PinSource14,GPIO_AF_OTG2_FS) ; 
  GPIO_PinAFConfig(GPIOB,GPIO_PinSource15,GPIO_AF_OTG2_FS) ;
  RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_OTG_HS, ENABLE) ;   
  #endif
 #endif //USB_OTG_HS
#endif //USE_STM322xG_EVAL

//  
//  /* Configure pin for overcurrent detection */
//  RCC_APB2PeriphClockCmd(HOST_OVRCURR_PORT_RCC, ENABLE);
//  
//  /* Configure SOF Pin */
//  GPIO_InitStructure.GPIO_Pin = HOST_OVRCURR_LINE;
//  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
//  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
//  GPIO_Init(HOST_OVRCURR_PORT, &GPIO_InitStructure);
//  
//  /* Connect Button EXTI Line to Button GPIO Pin */
//  GPIO_EXTILineConfig(HOST_OVRCURR_PORT_SOURCE, HOST_OVRCURR_PIN_SOURCE);  
//  
//  /* Configure Button EXTI line */
//  EXTI_InitStructure.EXTI_Line = HOST_OVRCURR_EXTI_LINE;
//  EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;  
//  EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; 
//  EXTI_InitStructure.EXTI_LineCmd = ENABLE;
//  EXTI_Init(&EXTI_InitStructure);
   USB_OTG_BSP_TimeInit();
}
Esempio n. 24
0
void Cam_DCMI_Config(void)
{
	/*Vision

HREF	PA4
PCLK	PA6
VSYN	PB7

D0		PC6
D1		PC7
D2 		PC8
D3		PC9
D4		PC11c
D5		PB6
D6		PB8
D7		PB9
*/  
/*Vision*/
  DCMI_InitTypeDef DCMI_InitStructure;
  GPIO_InitTypeDef GPIO_InitStructure;
  NVIC_InitTypeDef NVIC_InitStructure;
	
	DCMI_DeInit();
  /* Enable DCMI GPIOs clocks */
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB |  RCC_AHB1Periph_GPIOC |
                         RCC_AHB1Periph_GPIOD | RCC_AHB1Periph_GPIOE, ENABLE);
  /* Enable DCMI clock */
  RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_DCMI, ENABLE);


  GPIO_PinAFConfig(GPIOA, GPIO_PinSource4, GPIO_AF_DCMI); //HSYNC
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_DCMI); //PCLK
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_DCMI); //VSYNC
	
  GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_DCMI); //DCMI0
  GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_DCMI); //DCMI1
	GPIO_PinAFConfig(GPIOC, GPIO_PinSource8, GPIO_AF_DCMI); //DCMI2
  GPIO_PinAFConfig(GPIOC, GPIO_PinSource9, GPIO_AF_DCMI); //DCMI3
  GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_DCMI);//DCMI4
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_DCMI); //DCMI5
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_DCMI); //DCMI6
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_DCMI); //DCMI7

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;  

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_11;  //D0 - D4
  GPIO_Init(GPIOC, &GPIO_InitStructure);
	

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7| GPIO_Pin_8 | GPIO_Pin_9;  //D5 - D7, VSYNC
  GPIO_Init(GPIOB, &GPIO_InitStructure);
	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;  
  GPIO_Init(GPIOB, &GPIO_InitStructure);
	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;  //HSYNC
  GPIO_Init(GPIOA, &GPIO_InitStructure);	

  // PCLK(PA6)
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
		
  /* DCMI configuration *******************************************************/ 
  DCMI_InitStructure.DCMI_CaptureMode = DCMI_CaptureMode_SnapShot;
  DCMI_InitStructure.DCMI_SynchroMode = DCMI_SynchroMode_Hardware;
  DCMI_InitStructure.DCMI_PCKPolarity = DCMI_PCKPolarity_Rising;
  DCMI_InitStructure.DCMI_VSPolarity = DCMI_VSPolarity_Low; //DCMI_VSPolarity_High
  DCMI_InitStructure.DCMI_HSPolarity = DCMI_HSPolarity_Low; //DCMI_HSPolarity_High
  DCMI_InitStructure.DCMI_CaptureRate = DCMI_CaptureRate_All_Frame;//DCMI_CaptureRate_All_Frame
  DCMI_InitStructure.DCMI_ExtendedDataMode = DCMI_ExtendedDataMode_8b; //?
  
  DCMI_Init(&DCMI_InitStructure);

	DCMI_JPEGCmd(ENABLE);
      
  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
  NVIC_InitStructure.NVIC_IRQChannel = DCMI_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;  
	NVIC_Init(&NVIC_InitStructure); 

  DCMI_ITConfig(DCMI_IT_FRAME, ENABLE);

}
Esempio n. 25
0
/*******************************************************************************
*					TIM1的函数
*******************************************************************************/
void TIM1_PWM_Configuration(u16 Prescaler,u16 Period,u8 OC1 ,u8 OC2 ,u8 OC3 ,u8 OC4)							   // PA8    PA9    PA10    PA11
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
	RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);

    if(OC1==1)
    {
	    /* GPIOC Configuration: TIM1 Channel 1 Output */
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;			 
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;	      
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;	 	
        GPIO_Init(GPIOA, &GPIO_InitStructure);
    }
	if(OC2==1)
    {
	    /* GPIOC Configuration: TIM1 Channel 2 Output */
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;			  
        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_UP;		
        GPIO_Init(GPIOA, &GPIO_InitStructure);
		GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_2);
    }
	if(OC3==1)
    {
	    /* GPIOC Configuration: TIM1 Channel 3 Output */
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;			  
        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_UP;		
        GPIO_Init(GPIOA, &GPIO_InitStructure);
		GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_2);
    }
	if(OC4==1)
    {
	    /* GPIOC Configuration: TIM1 Channel 4 Output */
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;			 
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;	     
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;	 
        GPIO_Init(GPIOA, &GPIO_InitStructure);
    }


    TIM_DeInit(TIM1);												 //复位定时器8所有寄存器

    /* Time Base configuration */
    TIM1_TimeBaseStructure.TIM_Prescaler = Prescaler-1;				 //预分频数为0,不分频
    TIM1_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;	 //计娄方式为顺序计数模式,增大型
    TIM1_TimeBaseStructure.TIM_Period = Period-1;					 //设置计数器溢出后的重载初值
    TIM1_TimeBaseStructure.TIM_ClockDivision =  0x00;				 //配置时钟分隔值
    TIM1_TimeBaseStructure.TIM_RepetitionCounter = 0x0;			     //循环计数次数值

    TIM_TimeBaseInit(TIM1,&TIM1_TimeBaseStructure);				     //用以上参数初始化定时器时间基础模块
  
    /* Channel 1 Configuration in PWM mode */
    TIM1_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; 			     //输出方式为PWM模式1
 
    TIM1_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;   //使能输出比较状态
    TIM1_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; //使能定时器互补输出               
    TIM1_OCInitStructure.TIM_Pulse = 20000; 						 //设置脉宽
    TIM1_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;   	 //输出比较极性为高
    TIM1_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;	     //打开空闲状态选择关闭
  
    /* 初始化TM1通道1*/
    if(OC1==1)TIM_OC1Init(TIM1,&TIM1_OCInitStructure); 		         //用以上参数初始化TIM8的通道1
	if(OC2==1)TIM_OC2Init(TIM1,&TIM1_OCInitStructure); 		         //用以上参数初始化TIM8的通道2
	if(OC3==1)TIM_OC3Init(TIM1,&TIM1_OCInitStructure); 		         //用以上参数初始化TIM8的通道3
	if(OC4==1)TIM_OC4Init(TIM1,&TIM1_OCInitStructure); 		         //用以上参数初始化TIM8的通道4
 
    /* TIM1 counter enable */
    TIM_Cmd(TIM1,ENABLE);							   				 //使能定时器8

    /* Main Output Enable */
    TIM_CtrlPWMOutputs(TIM1,ENABLE);				   				 //使能定时器8的PWM输出	 频率

}  
Esempio n. 26
0
/* This funcion initializes the USART1 peripheral
 * 
 * Arguments: baudrate --> the baudrate at which the USART is 
 * 						   supposed to operate
 */
void init_USART1(uint32_t baudrate){
	/* This is a concept that has to do with the libraries provided by ST
	 * to make development easier the have made up something similar to 
	 * classes, called TypeDefs, which actually just define the common
	 * parameters that every peripheral needs to work correctly
	 * 
	 * They make our life easier because we don't have to mess around with 
	 * the low level stuff of setting bits in the correct registers
	 */
	GPIO_InitTypeDef GPIO_InitStruct; // this is for the GPIO pins used as TX and RX
	USART_InitTypeDef USART_InitStruct; // this is for the USART1 initilization
//	NVIC_InitTypeDef NVIC_InitStructure; // this is used to configure the NVIC (nested vector interrupt controller)

	/* enable APB2 peripheral clock for USART1 
	 * note that only USART1 and USART6 are connected to APB2
	 * the other USARTs are connected to APB1
////	 */
////	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
	/* enable the peripheral clock for the pins used by 
	 * USART1, PB6 for TX and PB7 for RX
	 */
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);

    /* The RX and TX pins are now connected to their AF
	 * so that the USART1 can take over control of the 
	 * pins
	 */
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2);
	GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2);
    
    
	/* This sequence sets up the TX and RX pins 
	 * so they work correctly with the USART1 peripheral
	 */
//	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; // Pins 6 (TX) and 7 (RX) are used
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; 			// the pins are configured as alternate function so the USART peripheral has access to them
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;		// this defines the IO speed and has nothing to do with the baudrate!
	GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;			// this defines the output type as push pull mode (as opposed to open drain)
	GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;			// this activates the pullup resistors on the IO pins
	GPIO_Init(GPIOA, &GPIO_InitStruct);					// now all the values are passed to the GPIO_Init() function which sets the GPIO registers

	
	//USART_DeInit(USART2);
    //USART_Cmd(USART2,ENABLE);

	/* Now the USART_InitStruct is used to define the 
	 * properties of USART1 
	 */
	USART_InitStruct.USART_BaudRate = baudrate;				// the baudrate is set to the value we passed into this init function
	USART_InitStruct.USART_WordLength = USART_WordLength_8b;// we want the data frame size to be 8 bits (standard)
	USART_InitStruct.USART_StopBits = USART_StopBits_1;		// we want 1 stop bit (standard)
	USART_InitStruct.USART_Parity = USART_Parity_No;		// we don't want a parity bit (standard)
	USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None; // we don't want flow control (standard)
	USART_InitStruct.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; // we want to enable the transmitter and the receiver
	
    USART_Init(USART2, &USART_InitStruct);					// again all the properties are passed to the USART_Init function which takes care of all the bit setting


	/* Here the USART1 receive interrupt is enabled
	 * and the interrupt controller is configured 
	 * to jump to the USART1_IRQHandler() function
	 * if the USART1 receive interrupt occurs
	 */
//	USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); // enable the USART1 receive interrupt 

//	NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;		 // we want to configure the USART1 interrupts
//	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;// this sets the priority group of the USART1 interrupts
//	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;		 // this sets the subpriority inside the group
//	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;			 // the USART1 interrupts are globally enabled
//	NVIC_Init(&NVIC_InitStructure);							 // the properties are passed to the NVIC_Init function which takes care of the low level stuff	

	// finally this enables the complete USART1 peripheral
	USART_Cmd(USART2, ENABLE);
}
Esempio n. 27
0
void init_I2C1(void){

		/* This is a concept that has to do with the libraries provided by ST
		* to make development easier the have made up something similar to
		* classes, called TypeDefs, which actually just define the common
		* parameters that every peripheral needs to work correctly
		*
		* They make our life easier because we don't have to mess around with
		* the low level stuff of setting bits in the correct registers
		*/
		GPIO_InitTypeDef GPIO_InitStruct; // this is for the GPIO pins used as I2C1SDA and I2C1SCL
		GPIO_InitTypeDef GPIO_Output;
		I2C_InitTypeDef I2C_InitStruct; // this is for the I2C1 initilization

		/* enable APB1 peripheral clock for I2C1 */
		RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C3, ENABLE);//zmianaRCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);

		/* enable the peripheral clock for the pins used by
		* I2C1, PB6 for I2C SCL and PB9 for I2C1_SDL
		*/
		/* GPIOB clock enable */
		RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
		//RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);

		/* This sequence sets up the I2C1SDA and I2C1SCL pins
		* so they work correctly with the I2C1 peripheral
		*/
		GPIO_InitStruct.GPIO_Pin = GPIO_Pin_8; // Pins 6 (I2C1_SCL) and 9 (I2C1_SDA) are used
		GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; 			// the pins are configured as alternate function so the USART peripheral has access to them
		GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;		// this defines the IO speed and has nothing to do with the baudrate!
		GPIO_InitStruct.GPIO_OType = GPIO_OType_OD;			// this defines the output type as open drain mode (as opposed to push pull)
		GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;			// this activates the pullup resistors on the IO pins
		GPIO_Init(GPIOA, &GPIO_InitStruct);					// now all the values are passed to the GPIO_Init() function which sets the GPIO registers


		//nowe
		//RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
		RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);

		/* This sequence sets up the I2C1SDA and I2C1SCL pins
		* so they work correctly with the I2C1 peripheral
		*/
		GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9; // Pins 6 (I2C1_SCL) and 9 (I2C1_SDA) are used
		GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; 			// the pins are configured as alternate function so the USART peripheral has access to them
		GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;		// this defines the IO speed and has nothing to do with the baudrate!
		GPIO_InitStruct.GPIO_OType = GPIO_OType_OD;			// this defines the output type as open drain mode (as opposed to push pull)
		GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;			// this activates the pullup resistors on the IO pins
		GPIO_Init(GPIOC, &GPIO_InitStruct);					// now all the values are passed to the GPIO_Init() function which sets the GPIO registers

		//nowe
		RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);

		/* Configure PD12, PD13, PD14 and PD15 in output pushpull mode */
		GPIO_Output.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13| GPIO_Pin_14| GPIO_Pin_15;
		GPIO_Output.GPIO_Mode = GPIO_Mode_OUT;
		GPIO_Output.GPIO_OType = GPIO_OType_PP;
		GPIO_Output.GPIO_Speed = GPIO_Speed_100MHz;
		GPIO_Output.GPIO_PuPd = GPIO_PuPd_NOPULL;
		GPIO_Init(GPIOD, &GPIO_Output);


		/* The I2C1_SCL and I2C1_SDA pins are now connected to their AF
		* so that the USART1 can take over control of the
		* pins
		*/
		GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_I2C3); //
		GPIO_PinAFConfig(GPIOC, GPIO_PinSource9, GPIO_AF_I2C3);

		/* Configure I2C1 */
		/* I2C DeInit */
		I2C_DeInit(I2C3);

		/* Enable the I2C peripheral */
		I2C_Cmd(I2C3, ENABLE);

		/* Set the I2C structure parameters */
		I2C_InitStruct.I2C_Mode = I2C_Mode_I2C;
		I2C_InitStruct.I2C_DutyCycle = I2C_DutyCycle_2;
		I2C_InitStruct.I2C_OwnAddress1 = 0xEE;
		I2C_InitStruct.I2C_Ack = I2C_Ack_Enable;
		I2C_InitStruct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
		I2C_InitStruct.I2C_ClockSpeed = 30000;

		/* Initialize the I2C peripheral w/ selected parameters */
		I2C_Init(I2C3, &I2C_InitStruct);
}
Esempio n. 28
0
/*
 * Tx:PC10 , Rx:PB11
 */
void init_USART(int buadrate)
{
	//RCC Initialization
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);

	//GPIO Initialization
	GPIO_InitTypeDef GPIO_InitStruct = {
		.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11,
		.GPIO_Mode = GPIO_Mode_AF,
		.GPIO_Speed = GPIO_Speed_50MHz,
		.GPIO_OType = GPIO_OType_PP,
		.GPIO_PuPd = GPIO_PuPd_UP
	};

	GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_USART3);
	GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_USART3);

	GPIO_Init(GPIOC, &GPIO_InitStruct);

	//USART Initialization
	USART_InitTypeDef USART_InitStruct = {
		.USART_BaudRate = buadrate,
		.USART_Mode = USART_Mode_Rx | USART_Mode_Tx,
		.USART_WordLength = USART_WordLength_8b,
		.USART_StopBits = USART_StopBits_1,
		.USART_Parity = USART_Parity_No
	};

	USART_Init(USART3, &USART_InitStruct);

	USART_Cmd(USART3, ENABLE);
}


void send_data(char *string)
{
	while(*string != '\0') {
		USART_SendData(USART3, (uint16_t)(*string));
		string++;
		while(USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET);
	}
}


void init_ADC()
{
	//RCC Initialization
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
	RCC_APB1PeriphClockCmd(RCC_APB2Periph_ADC3, ENABLE);

	DMA_InitTypeDef DMA_InitStruct = {
		.DMA_Channel = DMA_Channel_2,
		.DMA_PeripheralBaseAddr = ADC3_DR_ADDRESS,
		.DMA_Memory0BaseAddr = (unsigned int) &ADC_Value,
		.DMA_DIR = DMA_DIR_PeripheralToMemory,
		.DMA_BufferSize = 1,
		.DMA_PeripheralInc = DMA_PeripheralInc_Disable,
		.DMA_MemoryInc = DMA_MemoryInc_Disable,
		.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord,
		.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord,
		.DMA_Mode = DMA_Mode_Circular,
		.DMA_Priority = DMA_Priority_High,
		.DMA_FIFOMode = DMA_FIFOMode_Disable,
		.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull,
		.DMA_MemoryBurst = DMA_MemoryBurst_Single,
		.DMA_PeripheralBurst = DMA_PeripheralBurst_Single		
	};

	DMA_Init(DMA2_Stream0, &DMA_InitStruct);
	DMA_Cmd(DMA_Stream0, ENABLE);	

	//GPIO Initialization
	GPIO_InitTypeDef GPIO_InitStruct = {
		.GPIO_Pin = GPIO_Pin_3,
		.GPIO_Mode = GPIO_Mode_AN,
		.GPIO_PuPd = GPIO_PuPd_NOPULL
	};

	GPIO_Init(GPIOA, &GPIO_InitStruct);

	//ADC Common Initialization
	ADC_CommonInitTypeDef ADC_CommonInitStruct = {
		.ADC_Mode = ADC_Mode_Independent,
		.ADC_Prescaler = ADC_Prescaler_Div2,
		.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled,
		.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles
	};

	ADC_CommonInit(&ADC_CommonInitStruct);

	//ADC Initialization
	ADC_InitTypeDef ADC_InitStruct = {
		.ADC_Resolution = ADC_Resolution_12b,
		.ADC_ScanConvMode = DISABLE,
		.ADC_ContinuousConvMode = ENABLE,
		.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None,
		.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1,
		.ADC_DataAlign = ADC_DataAlign_Right,
		.ADC_NbrOfConversion = 1
	};

	//Set ADC to channel 3
	ADC_RegularChannelConfig(ADC3, ADC_Channel_3, 1, ADC_SampleTime_3Cycles);

	ADC_DMARequestAfterLastTransferCmd(ADC3, ENABLE);

	ADC_DMACmd(ADC3, ENABLE);

	ADC_SoftwareStartConv(ADC3);
}


int main()
{
	init_USART(9600);
	init_ADC();

	char ADC_Prompt[100] = {'\0'};

	while(1) {
		sprintf(ADC_Prompt, "ADC Value = %d\n\r", ADC_Value);

		send_data(ADC_Prompt);
		delay(1000000L);		
	}

	return 0;
}
Esempio n. 29
0
 void SST25V_Init(void)
 {

        GPIO_InitTypeDef GPIO_InitStructure;
        SPI_InitTypeDef SPI_InitStructure;

        /* Enable DF_SPI Periph clock */
        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE);
	 RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2,ENABLE);
	 GPIO_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF_SPI2  );
        GPIO_PinAFConfig(GPIOB, GPIO_PinSource14, GPIO_AF_SPI2  );
        GPIO_PinAFConfig(GPIOB, GPIO_PinSource15, GPIO_AF_SPI2  );
	 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
	

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


         /*!< SPI SCK pin configuration */
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
	GPIO_Init(GPIOB, &GPIO_InitStructure);

	/*!< SPI MOSI pin configuration */
	GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_14;
	GPIO_Init(GPIOB, &GPIO_InitStructure);

	/*!< SPI MISO pin configuration */
	GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_15;
	GPIO_Init(GPIOB, &GPIO_InitStructure);			



    //-------  CS _pin  	
    GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_14;   
	GPIO_InitStructure.GPIO_Mode  =  GPIO_Mode_OUT;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_Init(GPIOD, &GPIO_InitStructure);


      //-------  WP  _pin  	
    GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_15;   
    GPIO_InitStructure.GPIO_Mode  =  GPIO_Mode_OUT;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_Init(GPIOD, &GPIO_InitStructure);	

        /*------------------------ DF_SPI configuration ------------------------*/
        SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;//SPI_Direction_1Line_Tx;
        SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
        SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
        SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
        SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
        SPI_InitStructure.SPI_NSS  = SPI_NSS_Soft;
        SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8;/* 72M/64=1.125M */  
        SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
        SPI_InitStructure.SPI_CRCPolynomial = 7; 

	  //SPI1->CR2=0x04; 									//NSS ---SSOE
    //   SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
       //SPI_InitStructure.SPI_Mode = SPI_Mode_Master; 	  //MSTR	

        SPI_I2S_DeInit(DF_SPI);
        SPI_Init(DF_SPI, &SPI_InitStructure);

        /* Enable SPI_MASTER */
        SPI_Cmd(DF_SPI, ENABLE);
        SPI_CalculateCRC(DF_SPI, DISABLE); 

         SST25V_CS_HIGH();
	  SST25V_WP_HIGH();
	  //SST25V_HOLD_HIGH();
	//  SST25V_EnableWriteStatusRegister();
	 // SST25V_WriteStatusRegister(0x02); 
	   SST25V_DBSY(); 
}
Esempio n. 30
0
void ADC_config(void) {
	DMA_InitTypeDef DMA_InitStructure_ADC;
	NVIC_InitTypeDef NVIC_InitStructure;
	GPIO_InitTypeDef GPIO_InitSt_C, GPIO_InitSt_D;
	ADC_CommonInitTypeDef  ADC_struct;
	ADC_InitTypeDef ADC_InitStructure;	
	TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
	
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2 | RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOD, ENABLE);
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
	//CONFIGURE DMA FOR ADC
	DMA_InitStructure_ADC.DMA_Channel = DMA_Channel_0;
	DMA_InitStructure_ADC.DMA_PeripheralBaseAddr = ADC_DR_ADDRESS;
	DMA_InitStructure_ADC.DMA_DIR = DMA_DIR_PeripheralToMemory;
	DMA_InitStructure_ADC.DMA_BufferSize = NUMBER_SAMPL_ADC;
	DMA_InitStructure_ADC.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
	DMA_InitStructure_ADC.DMA_MemoryInc = DMA_MemoryInc_Enable;
	DMA_InitStructure_ADC.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
	DMA_InitStructure_ADC.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
	DMA_InitStructure_ADC.DMA_Mode = DMA_Mode_Normal;
	DMA_InitStructure_ADC.DMA_Priority = DMA_Priority_High;
	DMA_InitStructure_ADC.DMA_FIFOMode = DMA_FIFOMode_Enable;
	DMA_InitStructure_ADC.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
	DMA_InitStructure_ADC.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
	DMA_Init(DMA2_Stream0, &DMA_InitStructure_ADC);
	//enable DMA interrupt
	DMA_ITConfig(DMA2_Stream0, DMA_IT_TC, ENABLE);
	NVIC_InitStructure.NVIC_IRQChannel = DMA2_Stream0_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);
  DMA_Cmd(DMA2_Stream0, ENABLE);
	//CONFIGURE GPIO FOR ADC; 
	/*ADC Channel 10 -> PC0*/
  GPIO_InitSt_C.GPIO_Pin = GPIO_Pin_0;
  GPIO_InitSt_C.GPIO_Mode = GPIO_Mode_AN;
  GPIO_InitSt_C.GPIO_PuPd = GPIO_PuPd_NOPULL ;
	GPIO_Init(GPIOC, &GPIO_InitSt_C);
	//ADC COMMON INIT
	ADC_struct.ADC_Mode = ADC_Mode_Independent ;
	ADC_struct.ADC_Prescaler = ADC_Prescaler_Div8; //700kHz
	ADC_struct.ADC_DMAAccessMode = ADC_DMAAccessMode_1;
	ADC_struct.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
	ADC_CommonInit(&ADC_struct);
  //ADC CHANNEL INIT
  ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
  ADC_InitStructure.ADC_ScanConvMode = DISABLE;
  ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
	ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_Rising;
	ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T3_TRGO;
	ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  ADC_InitStructure.ADC_NbrOfConversion = 1;
  ADC_Init(ADC1, &ADC_InitStructure);
	ADC_RegularChannelConfig(ADC1, ADC_Channel_10, 1, ADC_SampleTime_3Cycles);
	ADC_DMARequestAfterLastTransferCmd(ADC1, DISABLE);
	// ENABLE ADC1 DMA
  ADC_DMACmd(ADC1, ENABLE);
	// ENABLE ADC1
  ADC_Cmd(ADC1, ENABLE);
	/*configure TIM3*/
	TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); 
  TIM_TimeBaseStructure.TIM_Period = 0;        
  TIM_TimeBaseStructure.TIM_Prescaler = 0;       
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;    
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  
  TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
  TIM_SelectOutputTrigger(TIM3, TIM_TRGOSource_Update);
	/* Input Trigger selection */
	TIM_ETRConfig(TIM3, TIM_ExtTRGPSC_OFF, TIM_ExtTRGPolarity_NonInverted, 0);
  TIM_SelectInputTrigger(TIM3, TIM_TS_ETRF);
  /* Slave Mode selection: Trigger Mode */
  TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Trigger);
	/*Trigger ADC -> PD2*/
	GPIO_InitSt_D.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitSt_D.GPIO_PuPd = GPIO_PuPd_DOWN;
  GPIO_InitSt_D.GPIO_Pin = GPIO_Pin_2;
  GPIO_Init(GPIOD, &GPIO_InitSt_D);
	GPIO_PinAFConfig(GPIOD, GPIO_PinSource2, GPIO_AF_TIM3);
}