Ejemplo n.º 1
0
/********************************************************************//**
 * @brief 		UART transmit function (ring buffer used)
 * @param[in]	None
 * @return 		None
 *********************************************************************/
void UART1_IntTransmit(void)
{
    // Disable THRE interrupt
    UART_IntConfig((LPC_UART_TypeDef *)LPC_UART1, UART_INTCFG_THRE, DISABLE);

	/* Wait for FIFO buffer empty, transfer UART_TX_FIFO_SIZE bytes
	 * of data or break whenever ring buffers are empty */
	/* Wait until THR empty */
    while (UART_CheckBusy((LPC_UART_TypeDef *)LPC_UART1) == SET);

	while (!__BUF_IS_EMPTY(rb.tx_head,rb.tx_tail))
    {
        /* Move a piece of data into the transmit FIFO */
    	if (UART_Send((LPC_UART_TypeDef *)LPC_UART1, (uint8_t *)&rb.tx[rb.tx_tail], \
			1, NONE_BLOCKING)){
        /* Update transmit ring FIFO tail pointer */
        __BUF_INCR(rb.tx_tail);
    	} else {
    		break;
    	}
    }

    /* If there is no more data to send, disable the transmit
       interrupt - else enable it or keep it enabled */
	if (__BUF_IS_EMPTY(rb.tx_head, rb.tx_tail)) {
    	UART_IntConfig((LPC_UART_TypeDef *)LPC_UART1, UART_INTCFG_THRE, DISABLE);
    	// Reset Tx Interrupt state
    	TxIntStat = RESET;
    }
    else{
      	// Set Tx Interrupt state
		TxIntStat = SET;
    	UART_IntConfig((LPC_UART_TypeDef *)LPC_UART1, UART_INTCFG_THRE, ENABLE);
    }
}
Ejemplo n.º 2
0
void GPS_Stop(void)
{
	//Disable GPS interrupt
	UART_IntConfig(UART_3, UART_INTCFG_RBR, DISABLE);
	UART_IntConfig(UART_3, UART_INTCFG_RLS, DISABLE);
	
	//Disable GPS wake-from-sleep
	NVIC_DisableIRQ(UART3_IRQn);
	
	//Turn off GPS via GPIO
	//GPS enable pin is p5[4]
	//GPIO_SetDir(5, 1<<4, GPIO_DIRECTION_OUTPUT);
	//LPC_GPIO5->CLR = 1<<4;
	GPS_Sleep();
	
	//Set state to GPS off (redundant, but do it anyway)
	gpsActive = 0;
	gpsData.valid = 0;
	
	//Disable GPS UART TX
	//UART_TxCmd(UART_3, DISABLE);
	
	//Turn off GPS UART
	//CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCUART3, DISABLE);
		
	return;
}
Ejemplo n.º 3
0
/*********************************************************************//**
 * @brief		UART read function for interrupt mode (using ring buffers)
 * @param[in]	UARTPort	Selected UART peripheral used to send data,
 * 				should be UART0
 * @param[out]	rxbuf Pointer to Received buffer
 * @param[in]	buflen Length of Received buffer
 * @return 		Number of bytes actually read from the ring buffer
 **********************************************************************/
uint32_t UARTReceive(LPC_UART_TypeDef *UARTPort, uint8_t *rxbuf, uint8_t buflen)
{
    uint8_t *data = (uint8_t *) rxbuf;
    uint32_t bytes = 0;

	/* Temporarily lock out UART receive interrupts during this
	   read so the UART receive interrupt won't cause problems
	   with the index values */
	UART_IntConfig(UARTPort, UART_INTCFG_RBR, DISABLE);

	/* Loop until receive buffer ring is empty or
		until max_bytes expires */
	while ((buflen > 0) && (!(__BUF_IS_EMPTY(rb.rx_head, rb.rx_tail))))
	{
		/* Read data from ring buffer into user buffer */
		*data = rb.rx[rb.rx_tail];
		data++;

		/* Update tail pointer */
		__BUF_INCR(rb.rx_tail);

		/* Increment data count and decrement buffer size count */
		bytes++;
		buflen--;
	}

	/* Re-enable UART interrupts */
	UART_IntConfig(UARTPort, UART_INTCFG_RBR, ENABLE);

    return bytes;
}
Ejemplo n.º 4
0
void USART_Init(void)
{
    UART_CFG_Type UARTConfigStruct;
    UART_FIFO_CFG_Type UARTFIFOConfigStruct;

    PINSEL_ConfigPin(0,2,1);
    PINSEL_ConfigPin(0,3,1);

    UART_ConfigStructInit(&UARTConfigStruct);

    UART_Init(USART_ID, &UARTConfigStruct);

    UART_FIFOConfigStructInit(&UARTFIFOConfigStruct);
#if     USART_FIFO_LEVEL == 1
    UARTFIFOConfigStruct.FIFO_Level = UART_FIFO_TRGLEV0;
#elif   USART_FIFO_LEVEL == 4
    UARTFIFOConfigStruct.FIFO_Level = UART_FIFO_TRGLEV1;
#elif   USART_FIFO_LEVEL == 8
    UARTFIFOConfigStruct.FIFO_Level = UART_FIFO_TRGLEV2;
#elif   USART_FIFO_LEVEL == 14
    UARTFIFOConfigStruct.FIFO_Level = UART_FIFO_TRGLEV3;
#endif

    UART_FIFOConfig(USART_ID, &UARTFIFOConfigStruct);


    UART_TxCmd(USART_ID, ENABLE);
    UART_IntConfig(USART_ID, UART_INTCFG_RBR, ENABLE);
    UART_IntConfig(USART_ID, UART_INTCFG_RLS, ENABLE);
    UART_IntConfig(USART_ID, UART_INTCFG_THRE, ENABLE);

    NVIC_SetPriority(USART_IRQ, ((0x01<<3)|0x01));

    NVIC_EnableIRQ(USART_IRQ);
}
Ejemplo n.º 5
0
/*
*@描述:初始化串口
*@参数:void
*@返回:无
*/
void InitUart(void)
{
	// UART Configuration structure variable
	UART_CFG_Type UARTConfigStruct;
	// UART FIFO configuration Struct variable
	UART_FIFO_CFG_Type UARTFIFOConfigStruct;
	// Pin configuration for UART0
	PINSEL_CFG_Type PinCfg;
	/*
	 * Initialize UART pin connect
	 */
	PinCfg.OpenDrain = UARTOpendrain;
	PinCfg.Pinmode = UARTPinMode;
	PinCfg.Portnum = UARTPortTX;
	PinCfg.Pinnum = UARTPinTX;
	PinCfg.Funcnum = UARTFuncTX;
	PINSEL_ConfigPin(&PinCfg);
	PinCfg.Portnum = UARTPortRX;
	PinCfg.Pinnum = UARTPinRX;
	PinCfg.Funcnum = UARTFuncRX;
	PINSEL_ConfigPin(&PinCfg);
	/* Initialize UART Configuration parameter structure to default state:
	 * Baudrate = 9600bps
	 * 8 data bit
	 * 1 Stop bit
	 * None parity
	 */
	UART_ConfigStructInit(&UARTConfigStruct);
	// Re-configure baudrate to 57600bps
	UARTConfigStruct.Baud_rate = 57600;
	// Initialize UART peripheral with given to corresponding parameter
	UART_Init((LPC_UART_TypeDef *)_LPC_UART, &UARTConfigStruct);
	/* Initialize FIFOConfigStruct to default state:
	 * 				- FIFO_DMAMode = DISABLE
	 * 				- FIFO_Level = UART_FIFO_TRGLEV0
	 * 				- FIFO_ResetRxBuf = ENABLE
	 * 				- FIFO_ResetTxBuf = ENABLE
	 * 				- FIFO_State = ENABLE
	 */
	UART_FIFOConfigStructInit(&UARTFIFOConfigStruct);
	UARTFIFOConfigStruct.FIFO_Level = UART_FIFO_TRGLEV3;//8 character 
	// Initialize FIFO for UART0 peripheral
	UART_FIFOConfig((LPC_UART_TypeDef *)_LPC_UART, &UARTFIFOConfigStruct);


	// Enable UART Transmit
	UART_TxCmd((LPC_UART_TypeDef *)_LPC_UART, ENABLE);

    /* Enable UART Rx interrupt */
	UART_IntConfig((LPC_UART_TypeDef *)_LPC_UART, UART_INTCFG_RBR, ENABLE);
	/* Enable UART line status interrupt */
	UART_IntConfig((LPC_UART_TypeDef *)_LPC_UART, UART_INTCFG_RLS, DISABLE);
	/* preemption = 1, sub-priority = 1 */
    NVIC_SetPriority(_UART_IRQ, ((0x01<<3)|0x01));
	/* Enable Interrupt for UART channel */
    NVIC_EnableIRQ(_UART_IRQ);
	BufInit(&bufSerialRec);
	
}
Ejemplo n.º 6
0
void configureInterrupts() {
    UART_IntConfig(UART1_DEVICE, UART_INTCFG_RBR, ENABLE);
    UART_IntConfig(UART1_DEVICE, UART_INTCFG_RLS, ENABLE);
    enableTransmitInterrupt();
    /* preemption = 1, sub-priority = 1 */
    NVIC_SetPriority(UART1_IRQn, ((0x01<<3)|0x01));
    NVIC_EnableIRQ(UART1_IRQn);
}
Ejemplo n.º 7
0
/*********************************************************************//**
 * @brief 		Client Hardware Initialize
 * @param[in] 	None
 * @return 		None
 ***********************************************************************/
void ClientHardwareInit(void)
{
	// UART Configuration structure variable
	UART_CFG_Type uart_config;
	// UART FIFO Configuration Struct variable
	UART_FIFO_CFG_Type uart_fifo;
	// Pin configuration for UART0
	PINSEL_CFG_Type pin;
    
    // Initialize UART0 pin connect
	pin.Funcnum = PINSEL_FUNC_1;
	pin.OpenDrain = PINSEL_PINMODE_OPENDRAIN;
	pin.Pinmode = PINSEL_PINMODE_PULLUP;
	pin.Pinnum = PINSEL_PIN_2;
	pin.Portnum = PINSEL_PORT_0;
	PINSEL_ConfigPin(&pin);
	pin.Pinnum = PINSEL_PIN_3;
	PINSEL_ConfigPin(&pin);

	// Initialize UART Configration parameter structure to default state:
	// Baudrate = 9600bps
	// 8 data bit
	// 1 stop bit
	// None parity
	UART_ConfigStructInit(&uart_config);

	// Set baudrate to 115200 */
	uart_config.Baud_rate = 115200;
	// Initialize UART0 peripheral with give to corresponding parameter */
	UART_Init((LPC_UART_TypeDef *)LPC_UART0, &uart_config);

	// Initialize FIFOConfigStruct to default state:
	// 				- FIFO_DMAMode = DISABLE
	// 				- FIFO_Level = UART_FIFO_TRGLEV0
	// 				- FIFO_ResetRxBuf = ENABLE
	// 				- FIFO_ResetTxBuf = ENABLE
	// 				- FIFO_State = ENABLE
	UART_FIFOConfigStructInit(&uart_fifo);
    
	// Initialize FIFO for UART0 peripheral
	UART_FIFOConfig((LPC_UART_TypeDef *)LPC_UART0, &uart_fifo);

	// Enable UART Transmit
	UART_TxCmd((LPC_UART_TypeDef *)LPC_UART0, ENABLE);

	// Enable UART Rx interrupt
	UART_IntConfig((LPC_UART_TypeDef *)LPC_UART0, UART_INTCFG_RBR, ENABLE);

	// Enable UART line status interrupt
	UART_IntConfig((LPC_UART_TypeDef *)LPC_UART0, UART_INTCFG_RLS, ENABLE);
    
	// preemption = 1, sub-priority = 1
	NVIC_SetPriority(UART0_IRQn, ((0x01 << 3) | 0x01));
    
	// Enable Interrupt for UART0 channel
	NVIC_EnableIRQ(UART0_IRQn);
}
Ejemplo n.º 8
0
void xl_UART_Config(unsigned char port)
{
   UART_CFG_Type      UARTConfigStruct;
   UART_FIFO_CFG_Type UARTFIFOConfigStruct;
   
   UART_ConfigStructInit(&UARTConfigStruct);
   UART_FIFOConfigStructInit(&UARTFIFOConfigStruct);
   
   switch(port)
   {
      case 0:
           PINSEL_ConfigPin(0,2,1);
           PINSEL_ConfigPin(0,3,1);
           UARTConfigStruct.Baud_rate  = 115200;
           
           UART_Init( (UART_ID_Type)port, &UARTConfigStruct );
           UART_FIFOConfig( (UART_ID_Type)port, &UARTFIFOConfigStruct );
           UART_TxCmd( (UART_ID_Type)port, ENABLE );      
           
           UART_IntConfig(UART_0, UART_INTCFG_RBR, ENABLE);
           UART_IntConfig(UART_0, UART_INTCFG_RLS, ENABLE);           
           
           NVIC_SetPriority(UART0_IRQn, ((0x02<<3)|0x02));
	          NVIC_EnableIRQ(UART0_IRQn);          
           break;
           
      case 2:      
           PINSEL_ConfigPin(0,10,1);
           PINSEL_ConfigPin(0,11,1);
           
           /**  */
           PINSEL_ConfigPin(1, 19, 6); 
           UARTConfigStruct.Baud_rate = 9600;
           
           UART_Init( (UART_ID_Type)port, &UARTConfigStruct );
           
           LPC_UART2->RS485CTRL  = 0x31;
           LPC_UART2->RS485DLY   = 0x10;
           
           UART_FIFOConfig( (UART_ID_Type)port, &UARTFIFOConfigStruct );
           UART_TxCmd( (UART_ID_Type)port, ENABLE );
                     
           UART_IntConfig(UART_2, UART_INTCFG_RBR, ENABLE);
           UART_IntConfig(UART_2, UART_INTCFG_RLS, ENABLE); 
           
//           NVIC_SetPriority(UART2_IRQn, ((0x02<<3)|0x03));
	         	NVIC_DisableIRQ(UART2_IRQn);      
           break;
           
     default:
           break;
   }
   

}
Ejemplo n.º 9
0
/* Auto flow control does work, but it turns the serial write functions into
 * blocking functions, which drags USB down. Instead we handle it manually so we
 * can make them asynchronous and let USB run at full speed.
 */
void configureFlowControl() {
    if (UART_FullModemGetStatus(LPC_UART1) & UART1_MODEM_STAT_CTS) {
        // Enable UART Transmit
        UART_TxCmd(UART1_DEVICE, ENABLE);
    }

    // Enable Modem status interrupt
    UART_IntConfig(UART1_DEVICE, UART1_INTCFG_MS, ENABLE);
    // Enable CTS1 signal transition interrupt
    UART_IntConfig(UART1_DEVICE, UART1_INTCFG_CTS, ENABLE);
    resumeReceive();
}
Ejemplo n.º 10
0
void serial_init(uint32_t baud_rate)
{
	UART_CFG_Type UART_config;
	UART_FIFO_CFG_Type UART_fifo_config;
	PINSEL_CFG_Type pin_config;

	// Configure TXD and RXD Pins
	pin_config.OpenDrain = PINSEL_PINMODE_NORMAL;
	pin_config.Pinmode = PINSEL_PINMODE_PULLUP;

	pin_config.Funcnum = UART_TXD_FUNC;
	pin_config.Portnum = UART_TXD_PORT;
	pin_config.Pinnum  = UART_TXD_PIN;
	PINSEL_ConfigPin(&pin_config);

	pin_config.Funcnum = UART_RXD_FUNC;
	pin_config.Portnum = UART_RXD_PORT;
	pin_config.Pinnum  = UART_RXD_PIN;
	PINSEL_ConfigPin(&pin_config);

#pragma message "BT TXD on Pin : P" STRING(UART_TXD_PORT) "_" STRING(UART_TXD_PIN)
#pragma message "BT RXD on Pin : P" STRING(UART_RXD_PORT) "_" STRING(UART_RXD_PIN)

	// Configure UART
	UART_config.Baud_rate = baud_rate;
	UART_config.Databits  = UART_DATABIT_8;
	UART_config.Parity    = UART_PARITY_NONE;
	UART_config.Stopbits  = UART_STOPBIT_1;
	UART_Init((LPC_UART_TypeDef *)UART_PORT, &UART_config);

	// Configure FIFO
	UART_fifo_config.FIFO_DMAMode = DISABLE;
	UART_fifo_config.FIFO_Level = UART_FIFO_TRGLEV0;
	UART_fifo_config.FIFO_ResetRxBuf = ENABLE;
	UART_fifo_config.FIFO_ResetTxBuf = ENABLE;
	UART_FIFOConfig((LPC_UART_TypeDef *)UART_PORT, &UART_fifo_config);

	// Enable UART Transmit
	UART_TxCmd((LPC_UART_TypeDef *)UART_PORT, ENABLE);
	// Enable UART interrupts
	UART_IntConfig((LPC_UART_TypeDef *)UART_PORT, UART_INTCFG_RLS, ENABLE);
	UART_IntConfig((LPC_UART_TypeDef *)UART_PORT, UART_INTCFG_RBR, ENABLE);
	UART_IntConfig((LPC_UART_TypeDef *)UART_PORT, UART_INTCFG_THRE, ENABLE);
	// preemption = 1, sub-priority = 1
	NVIC_SetPriority(UARTx_IRQn, ((0x01 << 3) | 0x01));
	// Enable Interrupt for UART
	NVIC_EnableIRQ(UARTx_IRQn);
}
Ejemplo n.º 11
0
Archivo: uart.c Proyecto: nedos/ddk-arm
void uart3_init(const uint32_t BaudRate, const bool DoubleSpeed)
{
    UART_CFG_Type UARTConfigStruct;
    PINSEL_CFG_Type PinCfg;

    PinCfg.Portnum = UART3_PORTNUM;
    PinCfg.Pinnum  = UART3_RX_PINNUM;
    PinCfg.Funcnum = UART3_FUNCNUM;
    PinCfg.OpenDrain = 0;
    PinCfg.Pinmode = 0;
    PINSEL_ConfigPin(&PinCfg);

    // leave TX3 untouched, we are currently using TX3 as i/o!
    //PinCfg.Pinnum = UART3_TX_PINNUM;
    //PINSEL_ConfigPin(&PinCfg);

    /* Initialize UART Configuration parameter structure to default state:
     * Baudrate = 9600bps
     * 8 data bit
     * 1 Stop bit
     * None parity
     */
    UART_ConfigStructInit(&UARTConfigStruct);
    // Re-configure baudrate
    UARTConfigStruct.Baud_rate = BaudRate;

    // Initialize DEBUG_UART_PORT peripheral with given to corresponding parameter
    UART_Init((LPC_UART_TypeDef *)FPGA_UART3_PORT, &UARTConfigStruct);

    // Enable UART Transmit
    //UART_TxCmd((LPC_UART_TypeDef *)FPGA_UART3_PORT, ENABLE);

    UART_IntConfig((LPC_UART_TypeDef *)FPGA_UART3_PORT, UART_INTCFG_RBR, ENABLE);
    NVIC_EnableIRQ(UART3_IRQn);
}
Ejemplo n.º 12
0
void initSystemBasics(void)
{
    uint8_t success[]="Smart Outlet Project\r\n";
    uint8_t error[]="Smart Outlet Project\r\n";

    UART_CFG_Type UART_ConfigStruct;
    UART_ConfigStruct.Baud_rate = 115200;
    UART_ConfigStruct.Parity = UART_PARITY_NONE;
    UART_ConfigStruct.Databits = UART_DATABIT_8;
    UART_ConfigStruct.Stopbits = UART_STOPBIT_1;

    UART_Init(LPC_UART0, &UART_ConfigStruct);
    UART_IntConfig(LPC_UART0, UART_INTCFG_RBR, ENABLE);

    xQueueInput = xQueueCreate(SIZE_QUEUE, sizeof(uint8_t));
    xQueueOutput = xQueueCreate(SIZE_QUEUE, sizeof(uint8_t));

#ifdef DEBUG_MODE
    if(xQueueInput == 0 && xQueueOutput == 0) {
        UART_Send(LPC_UART0, error, strlen(error), NONE_BLOCKING);
    } else {
        UART_Send(LPC_UART0, success, strlen(success), NONE_BLOCKING);
    }
#endif

    GPIO_SetDir(RELAY_PORT_NUM, RELAY_BIT_VALUE, 1);
    GPIO_SetValue(RELAY_PORT_NUM, RELAY_BIT_VALUE);

    ADC_Init(LPC_ADC, 1000); // TODO - Check if 1000 means 1kHz. Should it be only "1"
}
Ejemplo n.º 13
0
/*********************************************************************************************************
** Function name:     	uartInit
** Descriptions:	    串口初始化,设置为8位数据位,1位停止位,无奇偶校验,波特率为115200
** input parameters:    无
** output parameters:   无
** Returned value:      无
*********************************************************************************************************/
void UART0Init (void)
{
	UART_CFG_Type		UART_ConfigStruct_Test;
	UART_FIFO_CFG_Type 	FIFOCfg_Test;
	UART_INT_Type 		UARTIntCfg_Test;

	GPIO_PinselConfig(RXD0,1);
	GPIO_PinselConfig(TXD0,1);	
	GPIO_PinselConfig(EN_485_0,0);
	WriteEN_485_0(0);

	UART_ConfigStruct_Test.Baud_rate = UART0_BPS;
	UART_ConfigStruct_Test.Databits = UART_DATABIT_8;
	UART_ConfigStruct_Test.Parity = UART_PARITY_NONE;
	UART_ConfigStruct_Test.Stopbits = UART_STOPBIT_1;
	
	UART_Init((LPC_UART_TypeDef *)LPC_UART0,(UART_CFG_Type *)&UART_ConfigStruct_Test);
	UART_TxCmd((LPC_UART_TypeDef *)LPC_UART0,ENABLE);

	FIFOCfg_Test.FIFO_DMAMode = DISABLE;
	FIFOCfg_Test.FIFO_Level = UART_FIFO_TRGLEV2;
	FIFOCfg_Test.FIFO_ResetRxBuf = ENABLE;
	FIFOCfg_Test.FIFO_ResetTxBuf = ENABLE;	
	UART_FIFOConfig((LPC_UART_TypeDef *)LPC_UART0, (UART_FIFO_CFG_Type *)&FIFOCfg_Test);

	UARTIntCfg_Test = UART_INTCFG_RBR;	
	UART_IntConfig((LPC_UART_TypeDef *)LPC_UART0, UARTIntCfg_Test,ENABLE);
  	
	InitFIFO((stcFIFO *)&sUART0RecFIFO,UART0RecBuf,sizeof(UART0RecBuf));

	NVIC_EnableIRQ(UART0_IRQn);
	NVIC_SetPriority(UART0_IRQn, UART0_IRQn);									
}
Ejemplo n.º 14
0
Archivo: uart.c Proyecto: nedos/ddk-arm
void uart0_shutdown(void)
{

    puts("");

    UART_CFG_Type UARTConfigStruct;
    PINSEL_CFG_Type PinCfg;

    PinCfg.Portnum = UART0_PORTNUM;
    PinCfg.Pinnum = UART0_TX_PINNUM;
    PinCfg.Funcnum = 0;
    PinCfg.OpenDrain = 0;
    PinCfg.Pinmode = 0;
    PINSEL_ConfigPin(&PinCfg);
    PinCfg.Pinnum = UART0_RX_PINNUM;
    PINSEL_ConfigPin(&PinCfg);

    GPIO_SetDir(UART0_PORTNUM, (1<<UART0_TX_PINNUM), 1);
    GPIO_SetValue(UART0_PORTNUM, (1<<UART0_TX_PINNUM));

    UART_TxCmd((LPC_UART_TypeDef *)CONSOLE_UART_PORT, DISABLE);

    UART_IntConfig((LPC_UART_TypeDef *)CONSOLE_UART_PORT, UART_INTCFG_RBR, DISABLE);
    NVIC_DisableIRQ(UART0_IRQn);
}
Ejemplo n.º 15
0
/*********************************************************************//**
 * @brief		UART read function for interrupt mode (using ring buffers)
 * @param[in]	UARTPort	Selected UART peripheral used to send data,
 * 				should be UART1.
 * @param[out]	rxbuf Pointer to Received buffer
 * @param[in]	buflen Length of Received buffer
 * @return 		Number of bytes actually read from the ring buffer
 **********************************************************************/
uint32_t UARTReceive(LPC_UART_TypeDef *UARTPort, uint8_t *rxbuf, uint8_t buflen)
{
    uint8_t *data = (uint8_t *) rxbuf;
    uint32_t bytes = 0;

	/* Temporarily lock out UART receive interrupts during this
	   read so the UART receive interrupt won't cause problems
	   with the index values */
	UART_IntConfig(UARTPort, UART_INTCFG_RBR, DISABLE);

	/* Loop until receive buffer ring is empty or
		until max_bytes expires */
	while ((buflen > 0) && (!(__BUF_IS_EMPTY(rb.rx_head, rb.rx_tail))))
	{
		/* Read data from ring buffer into user buffer */
		*data = rb.rx[rb.rx_tail];
		data++;

		/* Update tail pointer */
		__BUF_INCR(rb.rx_tail);

		/* Increment data count and decrement buffer size count */
		bytes++;
		buflen--;

#if (AUTO_RTS_CTS_USE == 0)
		/* In case of driving RTS manually, this pin should be
		 * release into ACTIVE state if buffer is free
		 */
		if (RTS_State == INACTIVE)
		{
			if (!__BUF_WILL_FULL(rb.rx_head, rb.rx_tail))
			{
				// Disable request to send through RTS line
				UART_FullModemForcePinState(LPC_UART1, UART1_MODEM_PIN_RTS, \
						ACTIVE);
				RTS_State = ACTIVE;
			}
		}
#endif
	}

	/* Re-enable UART interrupts */
	UART_IntConfig(UARTPort, UART_INTCFG_RBR, ENABLE);

    return bytes;
}
Ejemplo n.º 16
0
void sio_init(void)
{
	//LPC_USART_3
	PINSEL_CFG_Type PinSelCfg;
	UART_CFG_Type UartCFG_Struct;
	UART_FIFO_CFG_Type UART_FIFO_CFG_Struct;


	// P4_28
	PinSelCfg.Portnum = PINSEL_PORT_4;
	PinSelCfg.Pinnum = PINSEL_PIN_28;
	PinSelCfg.Funcnum = PINSEL_FUNC_3;
	PinSelCfg.OpenDrain = PINSEL_PINMODE_NORMAL;
	PinSelCfg.Pinmode = PINSEL_PINMODE_PULLUP;
	PINSEL_ConfigPin(&PinSelCfg);
	// P4_29
	PinSelCfg.Pinnum = PINSEL_PIN_29;
	PINSEL_ConfigPin(&PinSelCfg);

	uartDataToSend.currPtr = 0;
	uartDataToSend.lastPtr = 0;
	/* Initialize UART Configuration parameter structure to default state:
	 * Baudrate = 9600bps
	 * 8 data bit
	 * 1 Stop bit
	 * None parity
	*/
	UART_ConfigStructInit(&UartCFG_Struct);

	/* Set Baudrate to 115200 */
	UartCFG_Struct.Baud_rate = 115200;

	/* Initialize UART3 peripheral with given to corresponding parameter */
	UART_Init(SERIAL_USART, &UartCFG_Struct);

	/* Initialize FIFOConfigStruct to default state:
	 * 				- FIFO_DMAMode = DISABLE
	 * 				- FIFO_Level = UART_FIFO_TRGLEV0
	 * 				- FIFO_ResetRxBuf = ENABLE
	 * 				- FIFO_ResetTxBuf = ENABLE
	 * 				- FIFO_State = ENABLE
	 */
	UART_FIFOConfigStructInit(&UART_FIFO_CFG_Struct);

	/* Initialize FIFO for UART3 peripheral */
	UART_FIFOConfig(SERIAL_USART, &UART_FIFO_CFG_Struct);

	/*  Enable UART Transmit */
	UART_TxCmd(SERIAL_USART, ENABLE);

	UART_IntConfig(SERIAL_USART, UART_INTCFG_THRE, ENABLE);

	NVIC_SetPriorityGrouping(UART3_PriorGrup);
	NVIC_SetPriority(UART3_IRQn, UART3_Prior);

	NVIC_EnableIRQ(UART3_IRQn);


}
Ejemplo n.º 17
0
/*********************************************************************//**
 * @brief		UART transmit function for interrupt mode (using ring buffers)
 * @param[in]	UARTPort	Selected UART peripheral used to send data,
 * 				should be UART0
 * @param[out]	txbuf Pointer to Transmit buffer
 * @param[in]	buflen Length of Transmit buffer
 * @return 		Number of bytes actually sent to the ring buffer
 **********************************************************************/
static uint32_t UARTSend(LPC_UART_TypeDef *UARTPort, uint8_t txbuf[], uint8_t buflen)
{
	uint8_t* data = &txbuf[0];
	uint32_t bytes = 0;

	/* Temporarily lock out UART transmit interrupts during this
	 read so the UART transmit interrupt won't cause problems
	 with the index values */
	UART_IntConfig(UARTPort, UART_INTCFG_THRE, DISABLE);

	/* Loop until transmit run buffer is full or until n_bytes
	 expires */
	while ((buflen > 0) && (!__BUF_IS_FULL(rb.tx_head, rb.tx_tail)))
	{
		/* Write data from buffer into ring buffer */
		rb.tx[rb.tx_head] = *data;
		data++;

		/* Increment head pointer */
		__BUF_INCR(rb.tx_head);

		/* Increment data count and decrement buffer size count */
		bytes++;
		buflen--;
	}

	/*
	 * Check if current Tx interrupt enable is reset,
	 * that means the Tx interrupt must be re-enabled
	 * due to call UART_IntTransmit() function to trigger
	 * this interrupt type
	 */
	if (TxIntStat == RESET)
	{
		UART_IntTransmit();
	}
	/*
	 * Otherwise, re-enables Tx Interrupt
	 */
	else
	{
		UART_IntConfig(UARTPort, UART_INTCFG_THRE, ENABLE);
	}

	return bytes;
}
Ejemplo n.º 18
0
/*********************************************************************//**
 * @brief	UART0 interrupt handler sub-routine
 * @param	None
 * @return	None
 **********************************************************************/
void UART0_IRQHandler(void)
{
	// Call Standard UART 0 interrupt handler
	uint32_t intsrc, tmp, tmp1;

	/* Determine the interrupt source */
	intsrc = UART_GetIntId(LPC_UART0);
	tmp = intsrc & UART_IIR_INTID_MASK;

	// Receive Line Status
	if (tmp == UART_IIR_INTID_RLS){
		// Check line status
		tmp1 = UART_GetLineStatus(LPC_UART0);
		// Mask out the Receive Ready and Transmit Holding empty status
		tmp1 &= (UART_LSR_OE | UART_LSR_PE | UART_LSR_FE \
				| UART_LSR_BI | UART_LSR_RXFE);
		// If any error exist
		if (tmp1) {

			while(tmp1){
				; //implement error handling here
			}
		}
	}


	intsrc &= (UART_IIR_ABEO_INT | UART_IIR_ABTO_INT);
	// Check if End of auto-baudrate interrupt or Auto baudrate time out
	if (intsrc){
		// Clear interrupt pending
		if(intsrc & UART_IIR_ABEO_INT)
			UART_ABClearIntPending(LPC_UART0, UART_AUTOBAUD_INTSTAT_ABEO);
		if (intsrc & UART_IIR_ABTO_INT)
			UART_ABClearIntPending(LPC_UART0, UART_AUTOBAUD_INTSTAT_ABTO);
			if (Synchronous == RESET)
			{
				/* Interrupt caused by End of auto-baud */
				if (intsrc & UART_AUTOBAUD_INTSTAT_ABEO){
					// Disable AB interrupt
					UART_IntConfig(LPC_UART0, UART_INTCFG_ABEO, DISABLE);
					// Set Sync flag
					Synchronous = SET;
				}

				/* Auto-Baudrate Time-Out interrupt (not implemented) */
				if (intsrc & UART_AUTOBAUD_INTSTAT_ABTO) {
					/* Just clear this bit - Add your code here */
					UART_ABClearIntPending(LPC_UART0, UART_AUTOBAUD_INTSTAT_ABTO);
				}
			}
	}
}
Ejemplo n.º 19
0
/*-----------------------------------------------------------------------------
 * Transmit Call-back dispatch for UART driver
 *-----------------------------------------------------------------------------*/
void UartTxCallBack(void)
{
	uint8_t tmpc;

	if (!__BUF_EMPTY(BufTx)) {
		tmpc = __BUF_RD(BufTx);
		UART_Send(UARTInfo.pUart, &tmpc, 1, BLOCKING);
		TxRestart = 0;
	} else {
		/* disable TX interrupt if nothing to send */
		UART_IntConfig(UARTInfo.pUart, UART_INTCFG_THRE, DISABLE);
		TxRestart = 1;
	}
}
Ejemplo n.º 20
0
static void uart3_init(rt_uint32_t baudrate)
{
	PINSEL_CFG_Type PinSelCfgType;

	UART_CFG_Type UartCfgType;

	UART_FIFO_CFG_Type UartFIFOCfgType;

	//pin config
	//TXD
	PinSelCfgType.Portnum = PINSEL_PORT_0;
	PinSelCfgType.Pinnum  = PINSEL_PIN_0;
	PinSelCfgType.Funcnum = PINSEL_FUNC_2;
	PinSelCfgType.Pinmode = PINSEL_PINMODE_PULLUP;
	PinSelCfgType.OpenDrain = PINSEL_PINMODE_NORMAL;
	PINSEL_ConfigPin(&PinSelCfgType);

	//RXD
	PinSelCfgType.Portnum = PINSEL_PORT_0;
	PinSelCfgType.Pinnum  = PINSEL_PIN_1;
	PinSelCfgType.Funcnum = PINSEL_FUNC_2;
	PinSelCfgType.Pinmode = PINSEL_PINMODE_TRISTATE;
	PinSelCfgType.OpenDrain = PINSEL_PINMODE_NORMAL;
	PINSEL_ConfigPin(&PinSelCfgType);

	//init
	UartCfgType.Baud_rate = baudrate;
	UartCfgType.Parity    = UART_PARITY_NONE;
	UartCfgType.Databits  = UART_DATABIT_8;
	UartCfgType.Stopbits  = UART_STOPBIT_1;
	UART_Init(LPC_DEV_UART, &UartCfgType);

	//FIFO
	UartFIFOCfgType.FIFO_ResetRxBuf = ENABLE;
	UartFIFOCfgType.FIFO_ResetTxBuf = ENABLE;
	UartFIFOCfgType.FIFO_DMAMode    = DISABLE;
	UartFIFOCfgType.FIFO_Level      = UART_FIFO_TRGLEV2;
	UART_FIFOConfig(LPC_DEV_UART, &UartFIFOCfgType);

	//int
	UART_IntConfig(LPC_DEV_UART, UART_INTCFG_RBR, ENABLE);

	//enable
	UART_TxCmd(LPC_DEV_UART, ENABLE);
}
Ejemplo n.º 21
0
void SER_Init(uint32_t baudrate)
{
	UART_CFG_Type UART_ConfigStruct;
	UART_FIFO_CFG_Type UART_FIFOInitStruct;
	
	UART_ConfigStruct.Baud_rate = baudrate;
	UART_ConfigStruct.Parity = UART_PARITY_NONE;
	UART_ConfigStruct.Stopbits = UART_STOPBIT_1;
	UART_ConfigStruct.Databits = UART_DATABIT_8;
	
	UART_Init(LPC_UART0, &UART_ConfigStruct);							//Init UART0 with parameters, defined above

	UART_TxCmd(LPC_UART0, ENABLE);												//Enable transmission on UART TxD pin

	UART_FIFOConfigStructInit(&UART_FIFOInitStruct);			//Fills each UART_FIFOInitStruct member with its default value
	UART_FIFOConfig(LPC_UART0, &UART_FIFOInitStruct);			//Configure FIFO function on UART0 peripheral
	
	UART_IntConfig(LPC_UART0,UART_INTCFG_RBR,ENABLE);			//Enable UART0 RBR Interrupt
	NVIC_EnableIRQ(UART0_IRQn);														//Enable UART0 Interrupts
	
}
Ejemplo n.º 22
0
void vMBMasterPortClose(void)
{
	
	/******************DISABLE the uart NVIC	*******************/
	NVIC_DisableIRQ(UART0_IRQn);
	NVIC_DisableIRQ(UART2_IRQn);
	NVIC_DisableIRQ(UART3_IRQn);
	NVIC_DisableIRQ(UART4_IRQn);
	/*****************DISABLE the uartrx exit********************/

	UART_IntConfig(UART_0, UART_INTCFG_RBR, DISABLE);
	UART_IntConfig(UART_2, UART_INTCFG_RBR, DISABLE);
	UART_IntConfig(UART_3, UART_INTCFG_RBR, DISABLE);
	UART_IntConfig(UART_4, UART_INTCFG_RBR, DISABLE);
	
	/*******************Enable the uart line exit***************/

	UART_IntConfig(UART_0, UART_INTCFG_RLS, DISABLE);
	UART_IntConfig(UART_2, UART_INTCFG_RLS, DISABLE);
	UART_IntConfig(UART_3, UART_INTCFG_RLS, DISABLE);
	UART_IntConfig(UART_4, UART_INTCFG_RLS, DISABLE);
	
	UART_RS485ReceiverCmd(UART_0,DISABLE);
	UART_RS485ReceiverCmd(UART_2,DISABLE);
	UART_RS485ReceiverCmd(UART_3,DISABLE);
	UART_RS485ReceiverCmd(UART_4,DISABLE);
	
	/*******************Uart DISABLE****************************/

	UART_TxCmd(LPC_UART0,DISABLE);
	UART_TxCmd(LPC_UART2,DISABLE);
	UART_TxCmd(LPC_UART3,DISABLE);
	UART_TxCmd((LPC_UART_TypeDef *)LPC_UART4,DISABLE);
//	USART_ITConfig(USART2, USART_IT_TXE | USART_IT_RXNE, DISABLE);
//	USART_Cmd(USART2, DISABLE);
}
Ejemplo n.º 23
0
/*----------------------------------------------------------------------------
  Serial Device "LPC17xx"
  initialize
 *----------------------------------------------------------------------------*/
static int UartDev_Init (void) {
  int i;

  if (UARTInfo.State != _STATE_UNKNOWN_) {
    return (-1);                                      /* Device initialized */
  }

  ConfigureUart (&UART_DEV.Cfg);

  ConfigurePins ();                                   /* Re-configure the UART pins */

  __BUF_RESET(BufTx);                                 /* reset transmit buffer */
  __BUF_RESET(BufRx);                                 /* reset receive buffer */
  TxRestart = 1;

  UARTInfo.State = _STATE_INITIALIZED_;

  UART_TxCmd (UARTInfo.pUart, ENABLE); 					/* TX, RX enable */

  for (i = 0; i < 800; i++);                            /* eleminate schmierzeichen */

  /* Setting up callback function for UART driver */
	// Receive callback
	UART_SetupCbs(UARTInfo.pUart, 0, (void *)UartRxCallBack);
	// Transmit callback
	UART_SetupCbs(UARTInfo.pUart, 1, (void *)UartTxCallBack);

  /* Enable Rx Interrrupt in UART peripheral */
  UART_IntConfig(UARTInfo.pUart, UART_INTCFG_RBR, ENABLE);

  /* preemption = 1, sub-priority = 1 */
  NVIC_SetPriority(UARTInfo.UARTIRQn, ((0x01<<3)|0x01));

  NVIC_EnableIRQ(UARTInfo.UARTIRQn);               /* enable Interrupt */

  return(0);                                       /* success */
}
Ejemplo n.º 24
0
static void UART_FullModemConfig(void)
{
	PINSEL_CFG_Type PinSelectConfigStruct;
	UART_CFG_Type UARTConfigStruct;
	UART_FIFO_CFG_Type UARTFIFOConfigStruct;

	//GSM_RXD_PIN
	PinSelectConfigStruct.Funcnum = PINSEL_FUNC_2;
	PinSelectConfigStruct.OpenDrain = PINSEL_PINMODE_NORMAL;
	PinSelectConfigStruct.Pinmode = PINSEL_PINMODE_PULLUP;
	PinSelectConfigStruct.Pinnum = gsmRxdPin;
	PinSelectConfigStruct.Portnum = GPIO2;

	PINSEL_ConfigPin(&PinSelectConfigStruct);

	//GSM_TXD_PIN
	PinSelectConfigStruct.Pinnum = gsmTxdPin;
	PINSEL_ConfigPin(&PinSelectConfigStruct);

	//GSM_CTS_PIN
	PinSelectConfigStruct.Pinnum = gsmCtsPin;
	PINSEL_ConfigPin(&PinSelectConfigStruct);

	//GSM_DCD_PIN
	PinSelectConfigStruct.Pinnum = gsmDcdPin;
	PINSEL_ConfigPin(&PinSelectConfigStruct);

	//GSM_DTR_PIN
	PinSelectConfigStruct.Pinnum = gsmDtrPin;
	PINSEL_ConfigPin(&PinSelectConfigStruct);

	//GSM_RI_PIN
	PinSelectConfigStruct.Pinnum = gsmRiPin;
	PINSEL_ConfigPin(&PinSelectConfigStruct);

	//GSM_RTS_PIN
	PinSelectConfigStruct.Pinnum = gsmRtsPin;
	PINSEL_ConfigPin(&PinSelectConfigStruct);


	/* Initialize UART Configuration parameter structure to default state:
	 * Baudrate = 9600bps
	 * 8 data bit
	 * 1 Stop bit
	 * None parity
	 */
	UART_ConfigStructInit(&UARTConfigStruct);

	/* Set Baudrate */
	UARTConfigStruct.Baud_rate = 9600;

	/* Initialize UART0 peripheral with given to corresponding parameter */
	UART_Init(UARTx, &UARTConfigStruct);

	/* Initialize FIFOConfigStruct to default state:
	 * 				- FIFO_DMAMode = DISABLE
	 * 				- FIFO_Level = UART_FIFO_TRGLEV0
	 * 				- FIFO_ResetRxBuf = ENABLE
	 * 				- FIFO_ResetTxBuf = ENABLE
	 * 				- FIFO_State = ENABLE
	 */
	UART_FIFOConfigStructInit(&UARTFIFOConfigStruct);

	/* Initialize FIFO for UART1 peripheral */
	UART_FIFOConfig(UARTx, &UARTFIFOConfigStruct);

	UART_FullModemConfigMode(LPC_UART1, UART1_MODEM_MODE_AUTO_CTS, ENABLE);
	UART_FullModemConfigMode(LPC_UART1, UART1_MODEM_MODE_AUTO_RTS, ENABLE);
	//UART_FullModemForcePinState(LPC_UART1, UART1_MODEM_PIN_RTS, ACTIVE);

	/*  Enable UART Transmit */
	UART_TxCmd( UARTx, ENABLE);

	UART_IntConfig(UARTx, UART_INTCFG_RBR , ENABLE);
	//UART_IntConfig(UARTx, UART1_INTCFG_MS, ENABLE);
	UART_FullModemForcePinState(LPC_UART1, UART1_MODEM_PIN_DTR, ACTIVE);

}
Ejemplo n.º 25
0
int comm_init(int which_port,int baudrate,FUNC_CB_COMM_RCV cb_rcv)
{
	// UART Configuration structure variable
	UART_CFG_Type UARTConfigStruct;
	// UART FIFO configuration Struct variable
	UART_FIFO_CFG_Type UARTFIFOConfigStruct;
#if 0
	uint32_t idx, len;
	__IO FlagStatus exitflag;
	uint8_t buffer[10];
#endif
#if 0
#if (UART_TEST_NUM == 0)
	/*
	 * Initialize UART0 pin connect
	 * P0.2: U0_TXD
	 * P0.3: U0_RXD
	 */
	PINSEL_ConfigPin(0,2,1);
	PINSEL_ConfigPin(0,3,1);
#elif (UART_TEST_NUM == 1)
	/*
	 * Initialize UART1 pin connect
	 * P3.16: U1_TXD
	 * P3.17: U1_RXD
	 */
	PINSEL_ConfigPin(3,16,3);
	PINSEL_ConfigPin(3,17,3);
#elif (UART_TEST_NUM == 2)
	/*
	 * Initialize UART2 pin connect
	 * P0.10: U2_TXD
	 * P0.11: U2_RXD
	 */
	PINSEL_ConfigPin(0,10,1);
	PINSEL_ConfigPin(0,11,1);

#endif
#else
	if (which_port == 0){
		/*
		 * Initialize UART0 pin connect
		 * P0.2: U0_TXD
		 * P0.3: U0_RXD
		 */
		PINSEL_ConfigPin(0,2,1);
		PINSEL_ConfigPin(0,3,1);
	}else if(which_port == 1){
		/*
		 * Initialize UART1 pin connect
		 * P3.16: U1_TXD
		 * P3.17: U1_RXD
		 */
		PINSEL_ConfigPin(0, 15, 1);
		PINSEL_ConfigPin(0, 16, 1);
	}else if(which_port == 2){
		/*
		 * Initialize UART2 pin connect
		 * P0.10: U2_TXD
		 * P0.11: U2_RXD
		 */
		PINSEL_ConfigPin(0,10,1);
		PINSEL_ConfigPin(0,11,1);
	}
#endif

	/* Initialize UART Configuration parameter structure to default state:
	 * Baudrate = 9600bps
	 * 8 data bit
	 * 1 Stop bit
	 * None parity
	 */
	UART_ConfigStructInit(&UARTConfigStruct,115200);

	// Initialize UART0 peripheral with given to corresponding parameter
	UART_Init((LPC_UART_TypeDef *)uartDrvDataArray[which_port].reg_base, &UARTConfigStruct);


	/* Initialize FIFOConfigStruct to default state:
	 * 				- FIFO_DMAMode = DISABLE
	 * 				- FIFO_Level = UART_FIFO_TRGLEV0
	 * 				- FIFO_ResetRxBuf = ENABLE
	 * 				- FIFO_ResetTxBuf = ENABLE
	 * 				- FIFO_State = ENABLE
	 */
	UART_FIFOConfigStructInit(&UARTFIFOConfigStruct);

	// Initialize FIFO for UART0 peripheral
	UART_FIFOConfig((LPC_UART_TypeDef *)uartDrvDataArray[which_port].reg_base, &UARTFIFOConfigStruct);


	// Enable UART Transmit
	UART_TxCmd((LPC_UART_TypeDef *)uartDrvDataArray[which_port].reg_base, ENABLE);

    /* Enable UART Rx interrupt */
	UART_IntConfig((LPC_UART_TypeDef *)uartDrvDataArray[which_port].reg_base, UART_INTCFG_RBR, ENABLE);
	/* Enable UART line status interrupt */
	UART_IntConfig((LPC_UART_TypeDef *)uartDrvDataArray[which_port].reg_base, UART_INTCFG_RLS, ENABLE);
	/*
	 * Do not enable transmit interrupt here, since it is handled by
	 * UART_Send() function, just to reset Tx Interrupt state for the
	 * first time
	 */
//	TxIntStat = RESET;
#if 0
	// Reset ring buf head and tail idx
	__BUF_RESET(rb.rx_head);
	__BUF_RESET(rb.rx_tail);
	__BUF_RESET(rb.tx_head);
	__BUF_RESET(rb.tx_tail);
#endif
    /* preemption = 1, sub-priority = 1 */
    NVIC_SetPriority(uartDrvDataArray[which_port].irqn, ((0x01<<3)|0x01));
	/* Enable Interrupt for UART0 channel */
    NVIC_EnableIRQ(uartDrvDataArray[which_port].irqn);
	if(cb_rcv){
		uartDrvDataArray[which_port].cb_rcv=cb_rcv;
	}
	return 0;
}
Ejemplo n.º 26
0
/*********************************************************************//**
 * @brief		c_entry: Main UART-RS485 program body
 * @param[in]	None
 * @return 		int
 **********************************************************************/
int c_entry(void)
{
	// UART Configuration structure variable
	UART_CFG_Type UARTConfigStruct;
	// UART FIFO configuration Struct variable
	UART_FIFO_CFG_Type UARTFIFOConfigStruct;
	// Pin configuration
	PINSEL_CFG_Type PinCfg;
	// RS485 configuration
	UART1_RS485_CTRLCFG_Type rs485cfg;
	uint32_t idx, len;
	uint8_t buffer[10];
	uint32_t tmp;

	// UART0 section ----------------------------------------------------
	/*
	 * Initialize UART0 pin connect
	 */
	PinCfg.Funcnum = 1;
	PinCfg.OpenDrain = 0;
	PinCfg.Pinmode = 0;
	PinCfg.Pinnum = 2;
	PinCfg.Portnum = 0;
	PINSEL_ConfigPin(&PinCfg);
	PinCfg.Pinnum = 3;
	PINSEL_ConfigPin(&PinCfg);

	/* Initialize UART Configuration parameter structure to default state:
	 * Baudrate = 115200 bps
	 * 8 data bit
	 * 1 Stop bit
	 * None parity
	 */
	UART_ConfigStructInit(&UARTConfigStruct);
	UARTConfigStruct.Baud_rate = 115200;

	// Initialize UART0 peripheral with given to corresponding parameter
	UART_Init(LPC_UART0, &UARTConfigStruct);

	/* Initialize FIFOConfigStruct to default state:
	 * 				- FIFO_DMAMode = DISABLE
	 * 				- FIFO_Level = UART_FIFO_TRGLEV0
	 * 				- FIFO_ResetRxBuf = ENABLE
	 * 				- FIFO_ResetTxBuf = ENABLE
	 * 				- FIFO_State = ENABLE
	 */
	UART_FIFOConfigStructInit(&UARTFIFOConfigStruct);

	// Initialize FIFO for UART0 peripheral
	UART_FIFOConfig(LPC_UART0, &UARTFIFOConfigStruct);

	// Enable UART Transmit
	UART_TxCmd(LPC_UART0, ENABLE);

	// print welcome screen
	print_menu();


	// UART1 - RS485 section -------------------------------------------------
	/*
	 * Initialize UART1 pin connect
	 */
	PinCfg.Funcnum = 2;
	PinCfg.OpenDrain = 0;
	PinCfg.Pinmode = 0;
	// TXD1 - P2.0
	PinCfg.Pinnum = 0;
	PinCfg.Portnum = 2;
	PINSEL_ConfigPin(&PinCfg);
	// RXD1 - P2.1
	PinCfg.Pinnum = 1;
	PINSEL_ConfigPin(&PinCfg);
	// DTR1 - P2.5
	PinCfg.Pinnum = 5;
	PINSEL_ConfigPin(&PinCfg);


	/* Initialize UART Configuration parameter structure to default state:
	 * Baudrate = 9600 bps
	 * 8 data bit
	 * 1 Stop bit
	 * Parity: None
	 * Note: Parity will be enabled later in UART_RS485Config() function.
	 */
	UART_ConfigStructInit(&UARTConfigStruct);

	// Initialize UART0 peripheral with given to corresponding parameter
	UART_Init((LPC_UART_TypeDef *)LPC_UART1, &UARTConfigStruct);

	/* Initialize FIFOConfigStruct to default state:
	 * 				- FIFO_DMAMode = DISABLE
	 * 				- FIFO_Level = UART_FIFO_TRGLEV0
	 * 				- FIFO_ResetRxBuf = ENABLE
	 * 				- FIFO_ResetTxBuf = ENABLE
	 * 				- FIFO_State = ENABLE
	 */
	UART_FIFOConfigStructInit(&UARTFIFOConfigStruct);

	// Initialize FIFO for UART0 peripheral
	UART_FIFOConfig((LPC_UART_TypeDef *)LPC_UART1, &UARTFIFOConfigStruct);

	// Configure RS485
	/*
	 * - Auto Direction in Tx/Rx driving is enabled
	 * - Direction control pin is set to DTR1
	 * - Direction control pole is set to "1" that means direction pin
	 * will drive to high state before transmit data.
	 * - Multidrop mode is enable
	 * - Auto detect address is disabled
	 * - Receive state is enable
	 */
	rs485cfg.AutoDirCtrl_State = ENABLE;
	rs485cfg.DirCtrlPin = UART1_RS485_DIRCTRL_DTR;
	rs485cfg.DirCtrlPol_Level = SET;
	rs485cfg.DelayValue = 50;
	rs485cfg.NormalMultiDropMode_State = ENABLE;
#if AUTO_SLVADDR_DETECT
	rs485cfg.AutoAddrDetect_State = ENABLE;
	rs485cfg.MatchAddrValue = SLAVE_ADDR;
#else
	rs485cfg.AutoAddrDetect_State = DISABLE;
#endif
#if RECEIVER_ALWAYS_EN
	rs485cfg.Rx_State = ENABLE;
#else
	rs485cfg.Rx_State = DISABLE;
#endif
	UART_RS485Config(LPC_UART1, &rs485cfg);

    /* Enable UART Rx interrupt */
	UART_IntConfig((LPC_UART_TypeDef *)LPC_UART1, UART_INTCFG_RBR, ENABLE);
	/* Enable UART line status interrupt */
	UART_IntConfig((LPC_UART_TypeDef *)LPC_UART1, UART_INTCFG_RLS, ENABLE);

    /* preemption = 1, sub-priority = 1 */
    NVIC_SetPriority(UART1_IRQn, ((0x01<<3)|0x01));
	/* Enable Interrupt for UART0 channel */
    NVIC_EnableIRQ(UART1_IRQn);

	// Enable UART Transmit
	UART_TxCmd((LPC_UART_TypeDef *)LPC_UART1, ENABLE);

	// for testing...
	while (1){
		len = 0;
		while (len == 0)
		{
			len = UARTReceive((LPC_UART_TypeDef *)LPC_UART1, buffer, sizeof(buffer));
		}

		/* Got some data */
		idx = 0;
		while (idx < len)
		{
			if (buffer[idx] == 13){
				for (tmp = 0; tmp < 1000000; tmp++);
				UART_RS485SendData(LPC_UART1, ack_msg, sizeof(ack_msg));
				UART_Send(LPC_UART0, nextline, sizeof(nextline), BLOCKING);
				UART_RS485SendData(LPC_UART1, &terminator, 1);
			} else {
				/* Echo it back */
				UART_Send(LPC_UART0, &buffer[idx], 1, BLOCKING);
			}
			idx++;
		}
	}

    return 1;
}
//
//
//lecture - UART - uart specific ioctl commands are processed by this method - for instance,
//                 for using hw interrupts or changing speed of operation
//
//
//
portBASE_TYPE FreeRTOS_UART_ioctl( Peripheral_Descriptor_t pxPeripheral, uint32_t ulRequest, void *pvValue )
{
Peripheral_Control_t * const pxPeripheralControl = ( Peripheral_Control_t * const ) pxPeripheral;
UART_CFG_Type xUARTConfig;
uint32_t ulValue = ( uint32_t ) pvValue;
const int8_t cPeripheralNumber = diGET_PERIPHERAL_NUMBER( ( ( Peripheral_Control_t * const ) pxPeripheral ) );
LPC_UART_TypeDef * pxUART = ( LPC_UART_TypeDef * ) diGET_PERIPHERAL_BASE_ADDRESS( ( ( Peripheral_Control_t * const ) pxPeripheral ) );
portBASE_TYPE xReturn = pdPASS;

	/* Sanity check the array index. */
	configASSERT( cPeripheralNumber < ( int8_t ) ( sizeof( xIRQ ) / sizeof( IRQn_Type ) ) );

	taskENTER_CRITICAL();
	{
		switch( ulRequest )
		{
		    //
		    //
		    //
		    //lecture - UART - for hw specific details, refer to chapter 14
		    //                 lpc17xx user-manual for UART3 specific settings
		    //          UART - Tx interrupts and Rx interrupts are enabled here
		    //          UART - we may introduce another set of IOCTL commands that will enable Tx and Rx
		    //                 interrupts separately ??
		    //          UART - some parts of ioctl commands' implementations are incomplete - ??
		    //
		    //
			case ioctlUSE_INTERRUPTS :

				if( ulValue == pdFALSE )
				{
					NVIC_DisableIRQ( xIRQ[ cPeripheralNumber ] );
				}
				else
				{
					/* Enable the Rx and Tx interrupt. */
					UART_IntConfig( pxUART, UART_INTCFG_RBR, ENABLE );
					UART_IntConfig( pxUART, UART_INTCFG_THRE, ENABLE );

					/* Enable the interrupt and set its priority to the minimum
					interrupt priority.  A separate command can be issued to raise
					the priority if desired. */
					NVIC_SetPriority( xIRQ[ cPeripheralNumber ], configMIN_LIBRARY_INTERRUPT_PRIORITY );
					NVIC_EnableIRQ( xIRQ[ cPeripheralNumber ] );

					/* If the Rx is configured to use interrupts, remember the
					transfer control structure that should be used.  A reference
					to the Tx transfer control structure is taken when a write()
					operation is actually performed. */
					pxRxTransferControlStructs[ cPeripheralNumber ] = pxPeripheralControl->pxRxControl;
				}
				break;


			case ioctlSET_SPEED :

				/* Set up the default UART configuration. */
				xUARTConfig.Baud_rate = ulValue;
				xUARTConfig.Databits = UART_DATABIT_8;
				xUARTConfig.Parity = UART_PARITY_NONE;
				xUARTConfig.Stopbits = UART_STOPBIT_1;
				UART_Init( pxUART, &xUARTConfig );
				break;
			//
            //
            //lecture - UART - need to follow the rules of interrupt management and interrupt safe
		    //                 methods / apis
			//
			case ioctlSET_INTERRUPT_PRIORITY :

				/* The ISR uses ISR safe FreeRTOS API functions, so the priority
				being set must be lower than (ie numerically larger than)
				configMAX_LIBRARY_INTERRUPT_PRIORITY. */
				configASSERT( ulValue >= configMAX_LIBRARY_INTERRUPT_PRIORITY );
				NVIC_SetPriority( xIRQ[ cPeripheralNumber ], ulValue );
				break;


			default :

				xReturn = pdFAIL;
				break;
		}
	}
	taskEXIT_CRITICAL();

	return xReturn;
}
size_t FreeRTOS_UART_read( Peripheral_Descriptor_t const pxPeripheral, void * const pvBuffer, const size_t xBytes )
{
Peripheral_Control_t * const pxPeripheralControl = ( Peripheral_Control_t * const ) pxPeripheral;
size_t xReturn = 0U;
LPC_UART_TypeDef * const pxUART = ( LPC_UART_TypeDef * const ) diGET_PERIPHERAL_BASE_ADDRESS( ( ( Peripheral_Control_t * const ) pxPeripheral ) );
//
//lecture - UART - rx transfer structure is NULL for rx polling mode
//                 in the case of i2c, this is different - refer to i2c related code
//
	if( diGET_RX_TRANSFER_STRUCT( pxPeripheralControl ) == NULL )
	{
		#if ioconfigUSE_UART_POLLED_RX == 1
		{
			/* No FreeRTOS objects exist to allow reception without blocking
			the task, so just receive by polling.  No semaphore or queue is
			used here, so the application must ensure only one task attempts
			to make a polling read at a time. */
			xReturn = UART_Receive( pxUART, pvBuffer, xBytes, NONE_BLOCKING );
		}
		#endif /* ioconfigUSE_UART_POLLED_RX */
	}
	else
	{
		/* Sanity check the array index. */
		configASSERT( diGET_PERIPHERAL_NUMBER( pxPeripheralControl ) < ( int8_t ) ( sizeof( xIRQ ) / sizeof( IRQn_Type ) ) );

		switch( diGET_RX_TRANSFER_TYPE( pxPeripheralControl ) )
		{
			case ioctlUSE_CIRCULAR_BUFFER_RX :

				#if ioconfigUSE_UART_CIRCULAR_BUFFER_RX == 1
				{
					/* There is nothing to prevent multiple tasks attempting to
					read the circular buffer at any one time.  The implementation
					of the circular buffer uses a semaphore to indicate when new
					data is available, and the semaphore will ensure that only the
					highest priority task that is attempting a read will actually
					receive bytes. */
					ioutilsRECEIVE_CHARS_FROM_CIRCULAR_BUFFER
						(
							pxPeripheralControl,
							UART_IntConfig( pxUART, UART_INTCFG_RBR, DISABLE ),	/* Disable peripheral. */
							UART_IntConfig( pxUART, UART_INTCFG_RBR, ENABLE ), 	/* Enable peripheral. */
							( ( uint8_t * ) pvBuffer ),							/* Data destination. */
							xBytes,												/* Bytes to read. */
							xReturn												/* Number of bytes read. */
						);
				}
				#endif /* ioconfigUSE_UART_CIRCULAR_BUFFER_RX */
				break;


			case ioctlUSE_CHARACTER_QUEUE_RX :

				#if ioconfigUSE_UART_RX_CHAR_QUEUE == 1
				{
					/* The queue allows multiple tasks to attempt to read
					bytes, but ensures only the highest priority of these
					tasks will actually receive bytes.  If two tasks of equal
					priority attempt to read simultaneously, then the
					application must ensure mutual exclusion, as time slicing
					could result in the string being received being partially
					received by each task. */
					xReturn = xIOUtilsReceiveCharsFromRxQueue( pxPeripheralControl, ( uint8_t * ) pvBuffer, xBytes );
				}
				#endif /* ioconfigUSE_UART_RX_CHAR_QUEUE */
				break;


			default :

				/* Other methods can be implemented here. */
				configASSERT( xReturn );

				/* Prevent compiler warnings when the configuration is set such
				that the following parameters are not used. */
				( void ) pvBuffer;
				( void ) xBytes;
				( void ) pxUART;
				break;
		}
	}

	return xReturn;
}
Ejemplo n.º 29
0
/*********************************************************************//**
 * @brief	Main UART testing example sub-routine
 * 			Print welcome screen first, then press any key to have it
 * 			read in from the terminal and returned back to the terminal.
 * 			- Press ESC to exit
 * 			- Press 'r' to print welcome screen menu again
 **********************************************************************/
int c_entry(void)
{
	// UART Configuration structure variable
	UART_CFG_Type UARTConfigStruct;
	// UART FIFO configuration Struct variable
	UART_FIFO_CFG_Type UARTFIFOConfigStruct;
	// Pin configuration for UART0
	PINSEL_CFG_Type PinCfg;
	uint32_t idx, len;
	__IO FlagStatus exitflag;
	uint8_t buffer[10];

	// DeInit NVIC and SCBNVIC
	NVIC_DeInit();
	NVIC_SCBDeInit();

	/* Configure the NVIC Preemption Priority Bits:
	 * two (2) bits of preemption priority, six (6) bits of sub-priority.
	 * Since the Number of Bits used for Priority Levels is five (5), so the
	 * actual bit number of sub-priority is three (3)
	 */
	NVIC_SetPriorityGrouping(0x05);

	//  Set Vector table offset value
#if (__RAM_MODE__==1)
	NVIC_SetVTOR(0x10000000);
#else
	NVIC_SetVTOR(0x00000000);
#endif

	/*
	 * Initialize UART1 pin connect
	 */
	PinCfg.Funcnum = 1;
	PinCfg.OpenDrain = 0;
	PinCfg.Pinmode = 0;
	PinCfg.Portnum = 0;
	for (idx = 15; idx <= 22; idx++){
		PinCfg.Pinnum = idx;
		PINSEL_ConfigPin(&PinCfg);
	}

	/* Initialize UART Configuration parameter structure to default state:
	 * Baudrate = 9600bps
	 * 8 data bit
	 * 1 Stop bit
	 * None parity
	 */
	UART_ConfigStructInit(&UARTConfigStruct);

	// Initialize UART1 peripheral with given to corresponding parameter
	UART_Init((LPC_UART_TypeDef *)LPC_UART1, &UARTConfigStruct);

	/* Initialize FIFOConfigStruct to default state:
	 * 				- FIFO_DMAMode = DISABLE
	 * 				- FIFO_Level = UART_FIFO_TRGLEV0
	 * 				- FIFO_ResetRxBuf = ENABLE
	 * 				- FIFO_ResetTxBuf = ENABLE
	 * 				- FIFO_State = ENABLE
	 */
	UART_FIFOConfigStructInit(&UARTFIFOConfigStruct);

	// Initialize FIFO for UART1 peripheral
	UART_FIFOConfig((LPC_UART_TypeDef *)LPC_UART1, &UARTFIFOConfigStruct);

#if (AUTO_RTS_CTS_USE==0)
	/*
	 * Determine current state of CTS pin to enable Tx
	 * activity
	 */
	if (UART_FullModemGetStatus(LPC_UART1) & UART1_MODEM_STAT_CTS) {
		// Enable UART Transmit
		UART_TxCmd((LPC_UART_TypeDef *)LPC_UART1, ENABLE);
	}
#else
	// Enable UART Transmit
	UART_TxCmd((UART_TypeDef *)UART1, ENABLE);
#endif

	// Reset ring buf head and tail idx
	__BUF_RESET(rb.rx_head);
	__BUF_RESET(rb.rx_tail);
	__BUF_RESET(rb.tx_head);
	__BUF_RESET(rb.tx_tail);

#if AUTO_RTS_CTS_USE
	UART_FullModemConfigMode(UART1, UART1_MODEM_MODE_AUTO_RTS, ENABLE);
	UART_FullModemConfigMode(UART1, UART1_MODEM_MODE_AUTO_CTS, ENABLE);
#else
	// Enable Modem status interrupt
	UART_IntConfig((LPC_UART_TypeDef *)LPC_UART1, UART1_INTCFG_MS, ENABLE);
	// Enable CTS1 signal transition interrupt
	UART_IntConfig((LPC_UART_TypeDef *)LPC_UART1, UART1_INTCFG_CTS, ENABLE);
	// Modem Status interrupt call back
	UART_SetupCbs((LPC_UART_TypeDef *)LPC_UART1, 4, (void *)UART1_ModemCallBack);
	// Force RTS pin state to ACTIVE
	UART_FullModemForcePinState(LPC_UART1, UART1_MODEM_PIN_RTS, ACTIVE);
	//RESET RTS State flag
	RTS_State = ACTIVE;
#endif

	// Setup callback ---------------
	// Receive callback
	UART_SetupCbs((LPC_UART_TypeDef *)LPC_UART1, 0, (void *)UART1_IntReceive);
	// Transmit callback
	UART_SetupCbs((LPC_UART_TypeDef *)LPC_UART1, 1, (void *)UART1_IntTransmit);
	// Line Status Error callback
	UART_SetupCbs((LPC_UART_TypeDef *)LPC_UART1, 3, (void *)UART1_IntErr);

    /* Enable UART Rx interrupt */
	UART_IntConfig((LPC_UART_TypeDef *)LPC_UART1, UART_INTCFG_RBR, ENABLE);
	/* Enable UART line status interrupt */
	UART_IntConfig((LPC_UART_TypeDef *)LPC_UART1, UART_INTCFG_RLS, ENABLE);

	/*
	 * Do not enable transmit interrupt here, since it is handled by
	 * UART_Send() function, just to reset Tx Interrupt state for the
	 * first time
	 */
	TxIntStat = RESET;

    /* preemption = 1, sub-priority = 1 */
    NVIC_SetPriority(UART1_IRQn, ((0x01<<3)|0x01));
	/* Enable Interrupt for UART1 channel */
    NVIC_EnableIRQ(UART1_IRQn);

	// print welcome screen
	print_menu();

	// reset exit flag
	exitflag = RESET;

    /* Read some data from the buffer */
    while (exitflag == RESET)
    {
       len = 0;
        while (len == 0)
        {
            len = UARTReceive((LPC_UART_TypeDef *)LPC_UART1, buffer, sizeof(buffer));
        }

        /* Got some data */
        idx = 0;
        while (idx < len)
        {
            if (buffer[idx] == 27)
            {
                /* ESC key, set exit flag */
            	UARTSend((LPC_UART_TypeDef *)LPC_UART1, menu3, sizeof(menu3));
                exitflag = SET;
            }
            else if (buffer[idx] == 'r')
            {
                print_menu();
            }
            else
            {
                /* Echo it back */
            	UARTSend((LPC_UART_TypeDef *)LPC_UART1, &buffer[idx], 1);
            }
            idx++;
        }
    }

    // wait for current transmission complete - THR must be empty
    while (UART_CheckBusy((LPC_UART_TypeDef *)LPC_UART1) == SET);

    // DeInitialize UART1 peripheral
    UART_DeInit((LPC_UART_TypeDef *)LPC_UART1);

    /* Loop forever */
    while(1);
    return 1;
}
Ejemplo n.º 30
0
/*************************************************************
Function: void UartInit(uint8_t num,uint32_t baudrate,uint8_t parity)
  Description: 串口初始化函数用于初始化RS232及RS485 将UART配置为中断接收,DMA发送,RS485自动切换方向
  Calls:
  Called By:   main()
  Input:       num 串口号0、1、2
               baudrate 波特率
               parity 校验方式
  Output:      无
  Return:      无
  Others:      无
*************************************************************/
void UartInit ( uint8_t num, uint32_t baudrate, uint8_t parity )
{
    //	uint32_t idx;
    // RS485 configuration
    UART1_RS485_CTRLCFG_Type rs485cfg;
    // UART Configuration structure variable
    UART_CFG_Type UARTConfigStruct;
    // UART FIFO configuration Struct variable
    UART_FIFO_CFG_Type UARTFIFOConfigStruct;
    GPDMA_Channel_CFG_Type GPDMACfg;

    UART_ConfigStructInit ( &UARTConfigStruct );

    UARTConfigStruct.Baud_rate = baudrate;
    UARTConfigStruct.Parity = parity;

    UART_FIFOConfigStructInit ( &UARTFIFOConfigStruct );

    // Enable DMA mode in UART
    UARTFIFOConfigStruct.FIFO_DMAMode = ENABLE;
    // Destination memory - don't care
    GPDMACfgTx.DstMemAddr = 0;
    // Transfer width - don't care
    GPDMACfgTx.TransferWidth = 0;
    // Transfer type
    GPDMACfgTx.TransferType = GPDMA_TRANSFERTYPE_M2P;
    // Source connection - don't care
    GPDMACfgTx.SrcConn = 0;
    // Linker List Item - unused
    GPDMACfgTx.DMALLI = 0;

    rs485cfg.AutoDirCtrl_State = ENABLE;
    rs485cfg.DirCtrlPol_Level = SET;
    rs485cfg.DelayValue = 50;
    rs485cfg.NormalMultiDropMode_State = ENABLE;
    rs485cfg.AutoAddrDetect_State = DISABLE;

    rs485cfg.Rx_State = ENABLE;

    if ( num == 0 )
    {
        //PINSEL_ConfigPin(0,2,1); //UART0
        //PINSEL_ConfigPin(0,3,1); //UART0

        PINSEL_ConfigPin ( 0, 25, 0x03 );
        PINSEL_ConfigPin ( 0, 26, 0xb3 );
        // Initalize UART0 peripheral with given to corresponding parameter
        UART_Init ( RS232_UART, &UARTConfigStruct );
        // Initialize FIFO for UART0 peripheral
        UART_FIFOConfig ( RS232_UART, &UARTFIFOConfigStruct );

        // Enable UART Transmit
        UART_TxCmd ( RS232_UART, ENABLE );
        // channel 0
        GPDMACfgTx.ChannelNum = 0;
        // Source memory
        GPDMACfgTx.SrcMemAddr = ( uint32_t ) &RS232Tx.Buff;
        // Transfer size
        GPDMACfgTx.TransferSize = sizeof ( RS232Tx.Buff );
        // Destination connection
        GPDMACfgTx.DstConn = RS232_TX_PIN;

        RS232Tx.Flag = 0;

        RS232_Err = 0;

        UART_IntConfig ( RS232_UART, UART_INTCFG_RBR, ENABLE );

        UART_IntConfig ( RS232_UART, UART_INTCFG_RLS, ENABLE );

        NVIC_SetPriority ( RS232_IRQN, ( ( 0x01 << 3 ) | 0x01 ) );

        NVIC_EnableIRQ ( RS232_IRQN );
        RS232Rx.Flag = 0 ;
        RS232Rx.Len = 0 ;
        RS232Rx.Idx = 0 ;
    }

    if ( num == 1 )
    {
        PINSEL_ConfigPin ( 2, 0, 2 );
        PINSEL_ConfigPin ( 2, 1, 2 );
        PINSEL_ConfigPin ( 2, 5, 2 ); //U1_DTR
        rs485cfg.DirCtrlPin = UART1_RS485_DIRCTRL_DTR;
        UART_Init ( ( LPC_UART_TypeDef * ) LPC_UART1, &UARTConfigStruct );
        UART_FIFOConfig ( ( LPC_UART_TypeDef * ) LPC_UART1, &UARTFIFOConfigStruct );
        UART_RS485Config ( ( LPC_UART_TypeDef * ) LPC_UART1, &rs485cfg );
        // Enable UART Transmit
        UART_TxCmd ( ( LPC_UART_TypeDef * ) LPC_UART1, ENABLE );
        GPDMACfgTx.ChannelNum = 1;
        // Source memory
        GPDMACfgTx.SrcMemAddr = ( uint32_t ) &RS485Tx1.Buff;
        // Transfer size
        GPDMACfgTx.TransferSize = sizeof ( RS485Tx1.Buff );
        // Destination connection
        GPDMACfgTx.DstConn = GPDMA_CONN_UART1_Tx;
        /* Reset terminal counter */
        RS485Tx1.Flag = 0;
        /* Reset Error counter */
        RS4851_Err = 0;
        /* Enable UART Rx interrupt */
        UART_IntConfig ( ( LPC_UART_TypeDef * ) LPC_UART1, UART_INTCFG_RBR, ENABLE );
        /* Enable UART line status interrupt */
        UART_IntConfig ( ( LPC_UART_TypeDef * ) LPC_UART1, UART_INTCFG_RLS, ENABLE );
        /* preemption = 1, sub-priority = 1 */
        NVIC_SetPriority ( UART1_IRQn, ( ( 0x01 << 3 ) | 0x01 ) );

        /* Enable Interrupt for UART0 channel */
        NVIC_EnableIRQ ( UART1_IRQn );
    }

    if ( num == 2 )
    {
        PINSEL_ConfigPin ( 2, 8, 2 );
        PINSEL_ConfigPin ( 2, 9, 2 );
        PINSEL_ConfigPin ( 2, 6, 4 ); //U2_OE

        UART_Init ( LPC_UART2, &UARTConfigStruct );
        UART_FIFOConfig ( LPC_UART2, &UARTFIFOConfigStruct );
        UART_RS485Config ( ( LPC_UART_TypeDef * ) LPC_UART2, &rs485cfg );
        // Enable UART Transmit
        UART_TxCmd ( LPC_UART2, ENABLE );
        GPDMACfgTx.ChannelNum = 2;
        // Source memory
        GPDMACfgTx.SrcMemAddr = ( uint32_t ) &RS485Tx2.Buff;
        // Transfer size
        GPDMACfgTx.TransferSize = sizeof ( RS485Tx2.Buff );
        // Destination connection
        GPDMACfgTx.DstConn = GPDMA_CONN_UART2_Tx;
        /* Reset terminal counter */
        RS485Tx2.Flag = 0;
        /* Reset Error counter */
        RS4852_Err = 0;
        /* Enable UART Rx interrupt */
        UART_IntConfig ( LPC_UART2, UART_INTCFG_RBR, ENABLE );
        /* Enable UART line status interrupt */
        UART_IntConfig ( LPC_UART2, UART_INTCFG_RLS, ENABLE );
        /* preemption = 1, sub-priority = 1 */
        NVIC_SetPriority ( UART2_IRQn, ( ( 0x01 << 3 ) | 0x01 ) );

        /* Enable Interrupt for UART0 channel */
        NVIC_EnableIRQ ( UART2_IRQn );
    }

    /* Initialize GPDMA controller */
    GPDMA_Init();

    /* Setting GPDMA interrupt */
    // Disable interrupt for DMA
    NVIC_DisableIRQ ( DMA_IRQn );
    /* preemption = 1, sub-priority = 1 */
    NVIC_SetPriority ( DMA_IRQn, ( ( 0x01 << 3 ) | 0x01 ) );

    // Setup channel with given parameter
    GPDMA_Setup ( &GPDMACfgTx );

    // Enable interrupt for DMA
    NVIC_EnableIRQ ( DMA_IRQn );

    // Enable GPDMA channel 0
    //GPDMA_ChannelCmd(0, ENABLE);
    CRC_Init ( CRC_POLY_CRC16 );

}