/*******************************************************************************
* Function Name  : USB_CDC_SetLineCoding
* Description    : SetLineCoding CDC request handler.
*                : Stores received line coding settings and adjusts UART.
* Output         : None
* Return         : USB_Result
*******************************************************************************/
USB_Result USB_CDC_SetLineCoding(uint16_t wINDEX, const USB_CDC_LineCoding_TypeDef* DATA)
{
  assert_param(DATA);
  if (wINDEX != 0)
  {
    /* Invalid interface */
    return USB_ERR_INV_REQ;
  }

  /* Adjust UART settings */

  /* Baud rate */
  UARTInitStructure.UART_BaudRate = DATA->dwDTERate;

  /* Stop bits */
  switch (DATA->bCharFormat)
  {
    case USB_CDC_STOP_BITS1:
      UARTInitStructure.UART_StopBits = UART_StopBits1;
      break;
    case USB_CDC_STOP_BITS2:
      UARTInitStructure.UART_StopBits = UART_StopBits2;
      break;
    default :
      return USB_ERR_INV_REQ;
  }

  /* Parity */
  switch (DATA->bParityType)
  {
    case USB_CDC_PARITY_NONE:
      UARTInitStructure.UART_Parity = UART_Parity_No;
      break;
    case USB_CDC_PARITY_ODD:
      UARTInitStructure.UART_Parity = UART_Parity_Odd;
      break;
    case USB_CDC_PARITY_EVEN:
      UARTInitStructure.UART_Parity = UART_Parity_Even;
      break;
    case USB_CDC_PARITY_MARK:
      UARTInitStructure.UART_Parity = UART_Parity_0;
      break;
    case USB_CDC_PARITY_SPACE:
      UARTInitStructure.UART_Parity = UART_Parity_1;
      break;
    default :
      return USB_ERR_INV_REQ;
  }

  /* Data bits */
  switch (DATA->bDataBits)
  {
    case USB_CDC_DATA_BITS5:
      UARTInitStructure.UART_WordLength = UART_WordLength5b;
      break;
    case USB_CDC_DATA_BITS6:
      UARTInitStructure.UART_WordLength = UART_WordLength6b;
      break;
    case USB_CDC_DATA_BITS7:
      UARTInitStructure.UART_WordLength = UART_WordLength7b;
      break;
    case USB_CDC_DATA_BITS8:
      UARTInitStructure.UART_WordLength = UART_WordLength8b;
      break;
    default :
      return USB_ERR_INV_REQ;
  }

  /* Re-initialize UART */
  UART_Cmd(UART, DISABLE);
  UART_DeInit(UART);
  UART_Init(UART, &UARTInitStructure);
  /* Enable sender, receiver and line state interrupts */
  UART_ITConfig (UART, UART_IT_TX | UART_IT_RX
#ifdef USB_CDC_STATE_REPORTING_SUPPORTED
              | UART_LINE_STATE_EVENTS
#endif /* USB_CDC_STATE_REPORTING_SUPPORTED */
              , ENABLE);
  UART_Cmd(UART, ENABLE);

  /* On success, store new values for GetLineCoding request fastening */
  LineCoding = *DATA;

  return USB_SUCCESS;
}
/*******************************************************************************
* Function Name  : VCOMFunc
* Description    : Demonstration of USB-to-UART VCOM bridge.
*                : Displays prompt on LCD, enables the bridge and waits for
*                : joystick SEL key pressed to stop bridging and return
*                : to main menu.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void VCOMFunc(void) {
  uint32_t key;

  /* Display prompt */
  VCOMHelp();

  /* Save current CPU CLOCK configuration */
  temp_CPU_CLOCK   = MDR_RST_CLK->CPU_CLOCK;
  temp_PLL_CONTROL = MDR_RST_CLK->PLL_CONTROL;

  Setup_CPU_Clock();

  UARTConfiguration();

  /* CDC layer initialization */
  USB_CDC_Init(SendBuffer, 1, SET);

  /* UART configuration */
  UARTInitStructure.UART_BaudRate                = 14400;
  UARTInitStructure.UART_WordLength              = UART_WordLength8b;
  UARTInitStructure.UART_StopBits                = UART_StopBits1;
  UARTInitStructure.UART_Parity                  = UART_Parity_No;
  UARTInitStructure.UART_FIFOMode                = UART_FIFO_OFF;
  UARTInitStructure.UART_HardwareFlowControl     = UART_HardwareFlowControl_RXE | \
                                                   UART_HardwareFlowControl_TXE;
  /* With LBE bit set, output line of UARTTXD transmitter becomes to be bound to
   * UARTRXD receiver input. In order to run this example with UART physically
   * connected to PC, just comment this line */
/*  UARTInitStructure.UART_HardwareFlowControl     |= UART_HardwareFlowControl_LBE; */

  UART_Init (UART,&UARTInitStructure);

#ifdef USB_CDC_LINE_CODING_SUPPORTED
  /* Set line coding initial settings accordingly to UART ones above */
  LineCoding.dwDTERate = UARTInitStructure.UART_BaudRate;
  LineCoding.bCharFormat = USB_CDC_STOP_BITS1;
  LineCoding.bParityType = USB_CDC_PARITY_NONE;
  LineCoding.bDataBits = USB_CDC_DATA_BITS8;
#endif /* USB_CDC_LINE_CODING_SUPPORTED */

  /* Set interrupt handlers */
  pfUARTReceiverFunc = ReceiverFunc;
  pfUARTSenderFunc = SenderFunc;
#ifdef USB_CDC_STATE_REPORTING_SUPPORTED
  pfUARTLineStateFunc = LineStateFunc;
#endif /* USB_CDC_STATE_REPORTING_SUPPORTED */

  /* Enable sender, receiver and line state interrupts */
  UART_ITConfig (UART, UART_IT_TX | UART_IT_RX
#ifdef USB_CDC_STATE_REPORTING_SUPPORTED
              | UART_LINE_STATE_EVENTS
#endif /* USB_CDC_STATE_REPORTING_SUPPORTED */
              , ENABLE);

  /* Enable bridge */
  UART_Cmd(UART, ENABLE);
  Setup_USB();

  /* Wait for SEL to quit */
  key = GetKey();
  for (; key != SEL; key = GetKey())
  {
  }
  WAIT_UNTIL_KEY_RELEASED(SEL);

  /* Disable bridge */
  UART_Cmd(UART, DISABLE);
#if defined (USE_MDR32F9Q2_Rev0) || defined (USE_MDR32F9Q2_Rev1)
  PORTBRestoreConfig();
#endif /* defined (USE_MDR32F9Q2_Rev0) || defined (USE_MDR32F9Q2_Rev1) */
  USB_CDC_ReceiveStop();
  USB_DevicePowerOff();

  /* Restore original CPU CLOCK configuration */
  MDR_RST_CLK->CPU_CLOCK   = temp_CPU_CLOCK;
  MDR_RST_CLK->PLL_CONTROL = temp_PLL_CONTROL;

  DisplayMenu();
}
Beispiel #3
0
/*
 * See the serial2.h header file.
 */
xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
{
xComPortHandle xReturn;
UART_InitTypeDef UART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
EIC_IRQInitTypeDef  EIC_IRQInitStructure;	

	/* Create the queues used to hold Rx and Tx characters. */
	xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
	xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed char ) );

	/* If the queues were created correctly then setup the serial port
	hardware. */
	if( ( xRxedChars != serINVALID_QUEUE ) && ( xCharsForTx != serINVALID_QUEUE ) )
	{
	
		vConfigureQueues( xRxedChars, xCharsForTx, &xQueueEmpty );
	
		portENTER_CRITICAL();
		{
			/* Enable the UART0 Clock. */
			MRCC_PeripheralClockConfig( MRCC_Peripheral_UART0, ENABLE );
			
			/* Configure the UART0_Tx as alternate function */
			GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
			GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_11;
			GPIO_Init(GPIO0, &GPIO_InitStructure);
			
			/* Configure the UART0_Rx as input floating */
			GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
			GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
			GPIO_Init(GPIO0, &GPIO_InitStructure);
			
			/* Configure UART0. */
			UART_InitStructure.UART_WordLength = UART_WordLength_8D;
			UART_InitStructure.UART_StopBits = UART_StopBits_1;
			UART_InitStructure.UART_Parity = UART_Parity_No;
			UART_InitStructure.UART_BaudRate = ulWantedBaud;
			UART_InitStructure.UART_HardwareFlowControl = UART_HardwareFlowControl_None;
			UART_InitStructure.UART_Mode = UART_Mode_Tx_Rx;
			UART_InitStructure.UART_TxFIFOLevel = UART_FIFOLevel_1_2; /* FIFO size 16 bytes, FIFO level 8 bytes */
			UART_InitStructure.UART_RxFIFOLevel = UART_FIFOLevel_1_2; /* FIFO size 16 bytes, FIFO level 8 bytes */
			UART_Init(UART0, &UART_InitStructure);

			/* Enable the UART0 */
			UART_Cmd(UART0, ENABLE);

			/* Configure the IEC for the UART interrupts. */			
			EIC_IRQInitStructure.EIC_IRQChannelCmd = ENABLE;
			EIC_IRQInitStructure.EIC_IRQChannel = UART0_IRQChannel;
			EIC_IRQInitStructure.EIC_IRQChannelPriority = 1;
			EIC_IRQInit(&EIC_IRQInitStructure);
			
			xQueueEmpty = pdTRUE;
			UART_ITConfig( UART0, UART_IT_Transmit | UART_IT_Receive, ENABLE );
		}
		portEXIT_CRITICAL();
	}
	else
	{
		xReturn = ( xComPortHandle ) 0;
	}

	/* This demo file only supports a single port but we have to return
	something to comply with the standard demo header file. */
	return xReturn;
}
void main(void)
#endif
{
  RST_CLK_DeInit();
  RST_CLK_CPU_PLLconfig (RST_CLK_CPU_PLLsrcHSIdiv2,0);
  /* Enable peripheral clocks --------------------------------------------------*/
  RST_CLK_PCLKcmd((RST_CLK_PCLK_RST_CLK | RST_CLK_PCLK_UART1 | RST_CLK_PCLK_UART2 | RST_CLK_PCLK_DMA),ENABLE);
  RST_CLK_PCLKcmd((RST_CLK_PCLK_PORTF | RST_CLK_PCLK_PORTB), ENABLE);

  /* Init NVIC */
  SCB->AIRCR = 0x05FA0000 | ((uint32_t)0x500);
  SCB->VTOR = 0x08000000;
  /* Disable all interrupt */
  NVIC->ICPR[0] = 0xFFFFFFFF;
  NVIC->ICER[0] = 0xFFFFFFFF;

  /* Disable all DMA request */
  MDR_DMA->CHNL_REQ_MASK_CLR = 0xFFFFFFFF;
  MDR_DMA->CHNL_USEBURST_CLR = 0xFFFFFFFF;

  /* Reset PORTD settings */
  PORT_DeInit(MDR_PORTB);
  /* Reset PORTF settings */
  PORT_DeInit(MDR_PORTF);

  /* Configure UART1 pins: RXD, TXD */

  /* Configure PORTB pins 5, 6 */
  PORT_InitStructure.PORT_Pin   = PORT_Pin_6;
  PORT_InitStructure.PORT_OE    = PORT_OE_IN;
  PORT_InitStructure.PORT_FUNC  = PORT_FUNC_ALTER;
  PORT_InitStructure.PORT_MODE  = PORT_MODE_DIGITAL;
  PORT_InitStructure.PORT_SPEED = PORT_SPEED_FAST;
  PORT_Init(MDR_PORTB, &PORT_InitStructure);
  PORT_InitStructure.PORT_Pin   = PORT_Pin_5;
  PORT_InitStructure.PORT_OE    = PORT_OE_OUT;
  PORT_Init(MDR_PORTB, &PORT_InitStructure);

  /* Configure UART2 pins: RXD, TXD */

  /* Configure PORTF pins 0, 1 */
  PORT_InitStructure.PORT_FUNC  = PORT_FUNC_OVERRID;
  PORT_InitStructure.PORT_Pin   = (PORT_Pin_0);
  PORT_InitStructure.PORT_OE    = PORT_OE_IN;
  PORT_Init(MDR_PORTF, &PORT_InitStructure);
  PORT_InitStructure.PORT_Pin   = (PORT_Pin_1);
  PORT_InitStructure.PORT_OE    = PORT_OE_OUT;
  PORT_Init(MDR_PORTF, &PORT_InitStructure);

  /* Init RAM */
  Init_RAM (DstBuf1, BufferSize);
  Init_RAM (SrcBuf1, BufferSize);
  Init_RAM (DstBuf2, BufferSize);
  Init_RAM (SrcBuf2, BufferSize);

  /* Reset all UART settings */
  UART_DeInit(MDR_UART1);
  UART_DeInit(MDR_UART2);

  UART_BRGInit(MDR_UART1,UART_HCLKdiv1);
  UART_BRGInit(MDR_UART2,UART_HCLKdiv1);

  /* UART1 configuration ------------------------------------------------*/
  UART_StructInit (&sUART);

  sUART.UART_BaudRate                           = 1200;
  sUART.UART_WordLength                         = UART_WordLength8b;
  sUART.UART_StopBits                           = UART_StopBits1;
  sUART.UART_Parity                             = UART_Parity_No;
  sUART.UART_FIFOMode                           = UART_FIFO_ON;
  sUART.UART_HardwareFlowControl                = (UART_HardwareFlowControl_RXE | \
                                                   UART_HardwareFlowControl_TXE );
  UART_Init (MDR_UART1,&sUART);
  UART_DMAConfig(MDR_UART1,UART_IT_FIFO_LVL_8words,UART_IT_FIFO_LVL_8words);

  /* UART2 configuration ------------------------------------------------*/
  UART_Init (MDR_UART2,&sUART);
  UART_DMAConfig(MDR_UART2,UART_IT_FIFO_LVL_8words,UART_IT_FIFO_LVL_8words);

  /* Enable UART1 DMA Rx and Tx request */
  UART_DMACmd(MDR_UART1,(UART_DMA_RXE | UART_DMA_TXE), ENABLE);
  /* Enable UART2 DMA Rx and Tx request */
  UART_DMACmd(MDR_UART2,(UART_DMA_RXE | UART_DMA_TXE), ENABLE);

  /* Reset all DMA settings */
  DMA_DeInit();
  DMA_StructInit(&DMA_InitStr);

  /* DMA_Channel_UART1_RX configuration ---------------------------------*/
  /* Set Primary Control Data */
  DMA_PriCtrlStr.DMA_SourceBaseAddr = (uint32_t)(&(MDR_UART1->DR));
  DMA_PriCtrlStr.DMA_DestBaseAddr = (uint32_t)DstBuf1;
  DMA_PriCtrlStr.DMA_SourceIncSize = DMA_SourceIncNo;
  DMA_PriCtrlStr.DMA_DestIncSize = DMA_DestIncByte;
  DMA_PriCtrlStr.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
  DMA_PriCtrlStr.DMA_Mode = DMA_Mode_Basic;
  DMA_PriCtrlStr.DMA_CycleSize = BufferSize;
  DMA_PriCtrlStr.DMA_NumContinuous = DMA_Transfers_8;
  DMA_PriCtrlStr.DMA_SourceProtCtrl = DMA_SourcePrivileged;
  DMA_PriCtrlStr.DMA_DestProtCtrl = DMA_DestPrivileged;
  /* Set Channel Structure */
  DMA_InitStr.DMA_PriCtrlData = &DMA_PriCtrlStr;
  DMA_InitStr.DMA_Priority = DMA_Priority_High;
  DMA_InitStr.DMA_UseBurst = DMA_BurstClear;
  DMA_InitStr.DMA_SelectDataStructure = DMA_CTRL_DATA_PRIMARY;
  /* Init DMA channel */
  DMA_Init(DMA_Channel_UART1_RX, &DMA_InitStr);

  /* DMA_Channel_UART2_RX configuration ---------------------------------*/
  /* Set Primary Control Data */
  DMA_PriCtrlStr.DMA_SourceBaseAddr = (uint32_t)(&(MDR_UART2->DR));
  DMA_PriCtrlStr.DMA_DestBaseAddr = (uint32_t)DstBuf2;
  /* Init DMA channel */
  DMA_Init(DMA_Channel_UART2_RX, &DMA_InitStr);

  /* DMA_Channel_UART1_TX configuration ---------------------------------*/
  /* Set Primary Control Data */
  DMA_PriCtrlStr.DMA_SourceBaseAddr = (uint32_t)SrcBuf1;
  DMA_PriCtrlStr.DMA_DestBaseAddr = (uint32_t)(&(MDR_UART1->DR));
  DMA_PriCtrlStr.DMA_SourceIncSize = DMA_SourceIncByte;
  DMA_PriCtrlStr.DMA_DestIncSize = DMA_DestIncNo;
  DMA_InitStr.DMA_Priority = DMA_Priority_Default;
  /* Init DMA channel */
  DMA_Init(DMA_Channel_UART1_TX, &DMA_InitStr);

  /* DMA_Channel_UART2_TX configuration ---------------------------------*/
  /* Set Primary Control Data */
  DMA_PriCtrlStr.DMA_SourceBaseAddr = (uint32_t)SrcBuf2;
  DMA_PriCtrlStr.DMA_DestBaseAddr = (uint32_t)(&(MDR_UART2->DR));
  /* Init DMA channel */
  DMA_Init(DMA_Channel_UART2_TX, &DMA_InitStr);

  /* Enable UART1 */
  UART_Cmd(MDR_UART1,ENABLE);
  /* Enable UART2 */
  UART_Cmd(MDR_UART2,ENABLE);

  /* Transfer complete */
  while((DMA_GetFlagStatus(DMA_Channel_UART1_TX, DMA_FLAG_CHNL_ENA)))
  {
  }
  while((DMA_GetFlagStatus(DMA_Channel_UART1_RX, DMA_FLAG_CHNL_ENA)))
  {
  }
  while((DMA_GetFlagStatus(DMA_Channel_UART2_TX, DMA_FLAG_CHNL_ENA)))
  {
  }
  while((DMA_GetFlagStatus(DMA_Channel_UART2_RX, DMA_FLAG_CHNL_ENA)))
  {
  }

  /* Check the corectness of written dada */
  TransferStatus1 = Verif_mem ((BufferSize/2), SrcBuf1, DstBuf2);
  TransferStatus2 = Verif_mem ((BufferSize/2), SrcBuf2, DstBuf1);
  /* TransferStatus1, TransferStatus2 = PASSED, if the data transmitted and received
     are correct */
  /* TransferStatus1, TransferStatus2 = FAILED, if the data transmitted and received
     are different */

  while(1)
  {
  }
}
Beispiel #5
0
/*
 * See the serial2.h header file.
 */
xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
{
xComPortHandle xReturn;
UART_InitTypeDef xUART1_Init;
GPIO_InitTypeDef GPIO_InitStructure;
	
	/* Create the queues used to hold Rx characters. */
	xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
	
	/* Create the semaphore used to wake a task waiting for space to become
	available in the FIFO. */
	vSemaphoreCreateBinary( xTxFIFOSemaphore );

	/* If the queue/semaphore was created correctly then setup the serial port
	hardware. */
	if( ( xRxedChars != serINVALID_QUEUE ) && ( xTxFIFOSemaphore != serINVALID_QUEUE ) )
	{
		/* Pre take the semaphore so a task will block if it tries to access
		it. */
		xSemaphoreTake( xTxFIFOSemaphore, 0 );
		
		/* Configure the UART. */
		xUART1_Init.UART_WordLength = UART_WordLength_8D;
		xUART1_Init.UART_StopBits = UART_StopBits_1;
		xUART1_Init.UART_Parity = UART_Parity_No;
		xUART1_Init.UART_BaudRate = ulWantedBaud;
		xUART1_Init.UART_HardwareFlowControl = UART_HardwareFlowControl_None;
		xUART1_Init.UART_Mode = UART_Mode_Tx_Rx;
		xUART1_Init.UART_FIFO = UART_FIFO_Enable;

		/* Enable the UART1 Clock */
		SCU_APBPeriphClockConfig( __UART1, ENABLE );
		
		/* Enable the GPIO3 Clock */
		SCU_APBPeriphClockConfig( __GPIO3, ENABLE );
		
		/* Configure UART1_Rx pin GPIO3.2 */
		GPIO_InitStructure.GPIO_Direction = GPIO_PinInput;
		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
		GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ;
		GPIO_InitStructure.GPIO_IPConnected = GPIO_IPConnected_Enable;
		GPIO_InitStructure.GPIO_Alternate = GPIO_InputAlt1 ;
		GPIO_Init( GPIO3, &GPIO_InitStructure );
		
		/* Configure UART1_Tx pin GPIO3.3 */
		GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput;
		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
		GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ;
		GPIO_InitStructure.GPIO_IPConnected = GPIO_IPConnected_Enable;
		GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt2 ;
		GPIO_Init( GPIO3, &GPIO_InitStructure );
		
		
		portENTER_CRITICAL();
		{		
			/* Configure the UART itself. */
			UART_DeInit( UART1 );		
			UART_Init( UART1, &xUART1_Init );
			UART_ITConfig( UART1, UART_IT_Receive | UART_IT_Transmit, ENABLE );
			UART1->ICR = serCLEAR_ALL_INTERRUPTS;
			UART_LoopBackConfig( UART1, DISABLE );
			UART_IrDACmd( IrDA1, DISABLE );

			/* Configure the VIC for the UART interrupts. */			
			VIC_Config( UART1_ITLine, VIC_IRQ, 9 );
			VIC_ITCmd( UART1_ITLine, ENABLE );

			UART_Cmd( UART1, ENABLE );			
			lTaskWaiting = pdFALSE;
		}
		portEXIT_CRITICAL();
	}
	else
	{
		xReturn = ( xComPortHandle ) 0;
	}

	/* This demo file only supports a single port but we have to return
	something to comply with the standard demo header file. */
	return xReturn;
}
/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
int main (void)
{
uint8_t DataByte=0x01;
static uint8_t ReciveByte[16];
uint32_t i;

  /* Enables the HSI clock on PORTB,PORTD */
  RST_CLK_PCLKcmd(RST_CLK_PCLK_PORTB,ENABLE);
  RST_CLK_PCLKcmd(RST_CLK_PCLK_PORTD,ENABLE);

  /* Fill PortInit structure*/
  PortInit.PORT_PULL_UP = PORT_PULL_UP_OFF;
  PortInit.PORT_PULL_DOWN = PORT_PULL_DOWN_OFF;
  PortInit.PORT_PD_SHM = PORT_PD_SHM_OFF;
  PortInit.PORT_PD = PORT_PD_DRIVER;
  PortInit.PORT_GFEN = PORT_GFEN_OFF;
  PortInit.PORT_FUNC = PORT_FUNC_ALTER;
  PortInit.PORT_SPEED = PORT_SPEED_MAXFAST;
  PortInit.PORT_MODE = PORT_MODE_DIGITAL;

  /* Configure PORTB pins 5 (UART1_TX) as output */
  PortInit.PORT_OE = PORT_OE_OUT;
  PortInit.PORT_Pin = PORT_Pin_5;
  PORT_Init(MDR_PORTB, &PortInit);

  /* Configure PORTB pins 6 (UART1_RX) as input */
  PortInit.PORT_OE = PORT_OE_IN;
  PortInit.PORT_Pin = PORT_Pin_6;
  PORT_Init(MDR_PORTB, &PortInit);

  /* Configure PORTD pins 1 (UART2_TX) as output */
  PortInit.PORT_OE = PORT_OE_OUT;
  PortInit.PORT_Pin = PORT_Pin_1;
  PORT_Init(MDR_PORTD, &PortInit);
  /* Configure PORTD pins 0 (UART1_RX) as input */
  PortInit.PORT_OE = PORT_OE_IN;
  PortInit.PORT_Pin = PORT_Pin_0;
  PORT_Init(MDR_PORTD, &PortInit);

  /* Select HSI/2 as CPU_CLK source*/
  RST_CLK_CPU_PLLconfig (RST_CLK_CPU_PLLsrcHSIdiv2,0);

  /* Enables the CPU_CLK clock on UART1,UART2 */
  RST_CLK_PCLKcmd(RST_CLK_PCLK_UART1, ENABLE);
  RST_CLK_PCLKcmd(RST_CLK_PCLK_UART2, ENABLE);

  /* Set the HCLK division factor = 1 for UART1,UART2*/
  UART_BRGInit(MDR_UART1, UART_HCLKdiv1);
  UART_BRGInit(MDR_UART2, UART_HCLKdiv1);

  /* Initialize UART_InitStructure */
  UART_InitStructure.UART_BaudRate                = 115000;
  UART_InitStructure.UART_WordLength              = UART_WordLength8b;
  UART_InitStructure.UART_StopBits                = UART_StopBits1;
  UART_InitStructure.UART_Parity                  = UART_Parity_No;
  UART_InitStructure.UART_FIFOMode                = UART_FIFO_ON;
  UART_InitStructure.UART_HardwareFlowControl     = UART_HardwareFlowControl_RXE | UART_HardwareFlowControl_TXE;

  /* Configure UART1 parameters*/
  UART_Init (MDR_UART1,&UART_InitStructure);

  /* Configure DMA for UART1*/
  UART_DMAConfig (MDR_UART1, UART_IT_FIFO_LVL_12words, UART_IT_FIFO_LVL_12words);
  UART_DMACmd(MDR_UART1, UART_DMA_TXE | UART_DMA_RXE | UART_DMA_ONERR, ENABLE);

  /* Enables UART1 peripheral */
  UART_Cmd(MDR_UART1,ENABLE);

  /* Configure UART2 parameters*/
  UART_Init (MDR_UART2,&UART_InitStructure);

  /* Configure DMA for UART2*/
  UART_DMAConfig (MDR_UART2, UART_IT_FIFO_LVL_12words, UART_IT_FIFO_LVL_12words);
  UART_DMACmd(MDR_UART2, UART_DMA_TXE | UART_DMA_RXE | UART_DMA_ONERR, ENABLE);

  /* Enables UART2 peripheral */
  UART_Cmd(MDR_UART2,ENABLE);

  while (1)
  {
    /* Check TXFE flag */
    while (UART_GetFlagStatus (MDR_UART1, UART_FLAG_TXFE)!= SET)
    {
    }

    /* Send Data from UART1 */
    for (i=0;i<16;i++)
    {
      UART_SendData (MDR_UART1, (uint16_t)(i+16*DataByte));
    }

    /* Check RXFF flag */
    while (UART_GetFlagStatus (MDR_UART2, UART_FLAG_RXFF)!= SET)
    {
    }

    /* Recive data */
    for (i=0;i<16;i++)
    {
      ReciveByte[i] = UART_ReceiveData (MDR_UART2);
    }

    /* Increment Data */
    DataByte++;
  }
}
Beispiel #7
0
/*******************************************************************************
* Function Name  :  UART0_Config.  
* Description    :  Configure the UART 0 according to the linecoding structure.
* Input          :  None.
* Return         :  Configuration status 
                    TRUE : configuration done with success  
                    FALSE : configuration aborted.
*******************************************************************************/
bool UART0_Config(void)
{
  /* set the bit rate*/
  if (linecoding.bitrate > 115200)
  {
    UART0_Config_Default();
    return (FALSE);
  }
  /* set the Stop bit*/
  switch (linecoding.format)
  {
  case 0: UART_InitStructure.UART_StopBits = UART_StopBits_1;
           break;
  case 2: UART_InitStructure.UART_StopBits = UART_StopBits_2;
           break;
  default :
    {
      UART0_Config_Default();
      return (FALSE);
    }
  }
  /* set the parity bit*/
  switch (linecoding.paritytype)
  {
  case 0: UART_InitStructure.UART_Parity = UART_Parity_No;
           break;
  case 1: UART_InitStructure.UART_Parity = UART_Parity_Even;
           break;
  case 2: UART_InitStructure.UART_Parity = UART_Parity_Odd;
           break;
  case 3: UART_InitStructure.UART_Parity = UART_Parity_OddStick;
           break;
  case 4: UART_InitStructure.UART_Parity = UART_Parity_EvenStick;
           break;
  default :
    {
      UART0_Config_Default();
      return (FALSE);
    }
  }
  /*set the data type*/
  switch (linecoding.datatype)
  {
  case 0x08: UART_InitStructure.UART_WordLength = UART_WordLength_8D;
           break;
  case 0x07: UART_InitStructure.UART_WordLength = UART_WordLength_7D;
           break;
  case 0x06: UART_InitStructure.UART_WordLength = UART_WordLength_6D;
           break;
  case 0x05: UART_InitStructure.UART_WordLength = UART_WordLength_5D;
           break;
  default : 
    {
      UART0_Config_Default();
      return (FALSE);
    }
  }

  UART_InitStructure.UART_BaudRate = linecoding.bitrate;
  UART_InitStructure.UART_HardwareFlowControl = UART_HardwareFlowControl_None;
  UART_InitStructure.UART_Mode = UART_Mode_Tx_Rx;
  UART_InitStructure.UART_FIFO = UART_FIFO_Disable;
  UART_Init(UART0, &UART_InitStructure);
  UART_Cmd(UART0, ENABLE);
  return (TRUE);
}