Exemple #1
0
void Serial_Init(const uint32_t BaudRate, const bool DoubleSpeed)
{
    UART_CFG_Type UARTConfigStruct;

    /*
     * Initialize UART pin connect
     */
//	scu_pinmux(0x2 ,0 , MD_PDN, FUNC1); 				// UART0_TXD
//	scu_pinmux(0x2 ,1 , MD_PLN|MD_EZI|MD_ZI, FUNC1); 	// UART0_RXD
    scu_pinmux(0x2 ,3 , MD_PDN, FUNC2); 				// UART3_TXD
    scu_pinmux(0x2 ,4 , MD_PLN|MD_EZI|MD_ZI, FUNC2);	// UART3_RXD

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

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

    // Enable UART Transmit
    UART_TxCmd(DEBUG_UART_PORT, ENABLE);
}
Exemple #2
0
void uart_init() {

	UART_CFG_Type cfg;
	UART_FIFO_CFG_Type fifo_cfg;

	UART_ConfigStructInit(&cfg);
	cfg.Baud_rate = 57600;

	PINSEL_CFG_Type PinCfg;

	PinCfg.Funcnum = 1;
	PinCfg.OpenDrain = 0;
	PinCfg.Pinmode = 0;
	PinCfg.Pinnum = 2;
	PinCfg.Portnum = 0;
	PINSEL_ConfigPin(&PinCfg);
	PinCfg.Pinnum = 3;
	PINSEL_ConfigPin(&PinCfg);

	UART_Init(LPC_UART0, &cfg);

	UART_FIFOConfigStructInit(&fifo_cfg);

	UART_FIFOConfig(LPC_UART0, &fifo_cfg);

	UART_TxCmd(LPC_UART0, ENABLE);

}
Exemple #3
0
void serial_init(void) {
	PINSEL_CFG_Type PinCfg;
	UART_CFG_Type UARTConfigStruct;
	UART_FIFO_CFG_Type UARTFIFOConfigStruct;

	//Init pins
	PinCfg.Funcnum = PINSEL_FUNC_1;
	PinCfg.OpenDrain = PINSEL_PINMODE_NORMAL;
	PinCfg.Pinmode = PINSEL_PINMODE_PULLUP;
	PinCfg.Portnum = PINSEL_PORT_0;
	PinCfg.Pinnum = PINSEL_PIN_2;
	PINSEL_ConfigPin(&PinCfg);
	PinCfg.Pinnum = PINSEL_PIN_3;
	PINSEL_ConfigPin(&PinCfg);
		
	//Init UART Configuration parameter structure to default state: 115200,8,1,N
	UART_ConfigStructInit(&UARTConfigStruct);
	UARTConfigStruct.Baud_rate = 115200;

	//Init FIFOConfigStruct using defaults
	UART_FIFOConfigStructInit(&UARTFIFOConfigStruct);

	//Start the devices USB serial
	UART_Init((LPC_UART_TypeDef *)LPC_UART0, &UARTConfigStruct);
	UART_FIFOConfig((LPC_UART_TypeDef *)LPC_UART0, &UARTFIFOConfigStruct);
	UART_TxCmd((LPC_UART_TypeDef *)LPC_UART0, ENABLE);
}
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);
}
Exemple #5
0
void Serial_Init(const uint32_t BaudRate, const bool DoubleSpeed)
{
	UART_CFG_Type UARTConfigStruct;

	/*
	 * Initialize UART1 pin connect
	 */
#if (BOARD == BOARD_XPLORER)
	scu_pinmux(0x1 ,13 , MD_PDN, FUNC1); 				// UART1_TXD
	scu_pinmux(0x1 ,14 , MD_PLN|MD_EZI|MD_ZI, FUNC1); 	// UART1_RXD
#else
	scu_pinmux(0x4 ,1 , MD_PDN|MD_EZI, FUNC6); 	// UART3_TXD
	scu_pinmux(0x4 ,2 , MD_PDN|MD_EZI, FUNC6); 	// UART3_RXD
#endif

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

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

	// Enable UART Transmit
	UART_TxCmd(DEBUG_UART_PORT, ENABLE);
}
Exemple #6
0
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);
}
Exemple #7
0
void board_init(void)
{
  SystemInit();
  CGU_Init();
	SysTick_Config( CGU_GetPCLKFrequency(CGU_PERIPHERAL_M4CORE)/CFG_TICKS_PER_SECOND );	/* 1 ms Timer */

	//------------- USB Bus power HOST ONLY-------------//
	scu_pinmux(0x1, 7, MD_PUP | MD_EZI, FUNC4);	// P1_7 USB0_PWR_EN, USB0 VBus function Xplorer

	scu_pinmux(0x2, 6, MD_PUP | MD_EZI, FUNC4); // P2_6 is configured as GPIO5[6] for USB1_PWR_EN
	GPIO_SetDir   (5, BIT_(6), 1);              // GPIO5[6] is output
	GPIO_SetValue (5, BIT_(6));                 // GPIO5[6] output high

	// Leds Init
	for (uint8_t i=0; i<BOARD_MAX_LEDS; i++)
	{
	  scu_pinmux(leds[i].port, leds[i].pin, MD_PUP|MD_EZI|MD_ZI, FUNC0);
	  GPIO_SetDir(leds[i].port, BIT_(leds[i].pin), 1); // output
	}

#if CFG_UART_ENABLE
	//------------- UART init -------------//
	UART_CFG_Type UARTConfigStruct;

	scu_pinmux(0x6 ,4, MD_PDN|MD_EZI, FUNC2); 	// UART0_TXD
	scu_pinmux(0x6 ,5, MD_PDN|MD_EZI, FUNC2); 	// UART0_RXD

	UART_ConfigStructInit(&UARTConfigStruct);                   // default: baud = 9600, 8 bit data, 1 stop bit, no parity
	UARTConfigStruct.Baud_rate = CFG_UART_BAUDRATE;             // Re-configure baudrate

	UART_Init((LPC_USARTn_Type*) LPC_USART0, &UARTConfigStruct); // Initialize UART port
	UART_TxCmd((LPC_USARTn_Type*) LPC_USART0, ENABLE);           // Enable UART
#endif

}
Exemple #8
0
/*
Desc:Configures the PINSEL data structure to setup pin P0.2
	 for use with the I2C bus and enables it
*/
void setupUART(){
	//UART works with 4 main steps
	PINSEL_CFG_Type PinCfg; //Configure Pin select register
	UART_CFG_Type UARTConfigStruct;	//Configure the UART Register
	UART_FIFO_CFG_Type FifoCfg; //Configure the UART FIFO Register

	//Init UART
	PinCfg.Funcnum = 1;
	PinCfg.OpenDrain = 0;
	PinCfg.Pinmode = 0;

	PinCfg.Portnum = 0;
	PinCfg.Pinnum = 2;
	PINSEL_ConfigPin(&PinCfg);


	UART_ConfigStructInit(&UARTConfigStruct);
	UART_Init((LPC_UART_TypeDef *)LPC_UART0, &UARTConfigStruct);

	UART_FIFOConfigStructInit(&FifoCfg);
	UART_FIFOConfig((LPC_UART_TypeDef *) LPC_UART0, &FifoCfg);

	//Enable UART Transmit
	UART_TxCmd((LPC_UART_TypeDef *)LPC_UART0, ENABLE);
}
/*
*@描述:初始化串口
*@参数: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);
	
}
Exemple #10
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);


}
Exemple #11
0
void uart_init(void)
{
	// UART Configuration structure variable
	UART_CFG_Type UARTConfigStruct;
	// UART FIFO configuration Struct variable
	UART_FIFO_CFG_Type UARTFIFOConfigStruct;
	// Pin configuration for UART
	PINSEL_CFG_Type PinCfg;

	/*
	* Initialize UART3 pin connect: P4.28 -> TXD3; P4.29 -> RXD3
	* or P0.2 -> TXD0, P0.3 -> RXD0
	*/
	PinCfg.OpenDrain = PINSEL_PINMODE_NORMAL;
	PinCfg.Pinmode = PINSEL_PINMODE_PULLUP;
#if DBG_UART_NUM == 3	
	PinCfg.Funcnum = PINSEL_FUNC_3;
	PinCfg.Portnum = 4;
	PinCfg.Pinnum = 28;
	PINSEL_ConfigPin(&PinCfg);
	PinCfg.Pinnum = 29;
	PINSEL_ConfigPin(&PinCfg);
#else
	PinCfg.Funcnum = PINSEL_FUNC_1;
	PinCfg.Portnum = 0;
	PinCfg.Pinnum = 2;
	PINSEL_ConfigPin(&PinCfg);
	PinCfg.Pinnum = 3;
	PINSEL_ConfigPin(&PinCfg);
#endif	

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

	// Initialize UART peripheral with given to corresponding parameter
	UART_Init(DBG_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);

	// Initialize FIFO for UART peripheral
	UART_FIFOConfig(DBG_UART, &UARTFIFOConfigStruct);

	// Enable UART Transmit
	UART_TxCmd(DBG_UART, ENABLE);
}
Exemple #12
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);
}
Exemple #13
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;
   }
   

}
Exemple #14
0
/*
 * Configure the pins according to their intended default function
 */
int _roboveroConfig(uint8_t * args)
{
    int i;
    UART_CFG_Type UARTConfigStruct;
    PWM_TIMERCFG_Type PWMCfgDat;

    configAllPins();

    /*
     * Enable 7 analog inputs
     */
    ADC_Init(LPC_ADC, 200000);
    for (i = 0; i < 4; i++)
    		ADC_ChannelCmd(LPC_ADC, i, ENABLE); 
    for (i = 5; i < 8; i++)
        ADC_ChannelCmd(LPC_ADC, i, ENABLE);
    /*
     * Configure I2C0 for IMU communications
     */
    I2C_Init(LPC_I2C0, 100000);
    I2C_Cmd(LPC_I2C0, ENABLE);

    /*
     * Initialize CAN bus
     */
    CAN_Init(LPC_CAN1, 100000);

    /*
     * Initialize UART1 
     */
    UART_ConfigStructInit(&UARTConfigStruct);
    UARTConfigStruct.Baud_rate = 115200;
    UART_Init((LPC_UART_TypeDef*)LPC_UART1, &UARTConfigStruct);
    UART_TxCmd((LPC_UART_TypeDef*)LPC_UART1, ENABLE);

    /*
     * Initialize PWM
     *
     * Peripheral clock is 30MHz. Prescale by 30 cycles for 1us resolution.
     */    
    PWMCfgDat.PrescaleOption = PWM_TIMER_PRESCALE_TICKVAL;
    PWMCfgDat.PrescaleValue = 30;
    PWM_Init(LPC_PWM1, PWM_MODE_TIMER, &PWMCfgDat);

    return 0;
}
Exemple #15
0
/******************************************************************************
 *
 * Description:
 *   Initialize the console (UART connected to USB-to-UART bridge)
 *
 *****************************************************************************/
void console_init(void)
{
  // UART Configuration structure variable
	UART_CFG_Type UARTConfigStruct;

  // pinsel for UART
	PINSEL_ConfigPin(0,2,1);
	PINSEL_ConfigPin(0,3,1);

	UART_ConfigStructInit(&UARTConfigStruct);
  UARTConfigStruct.Baud_rate = 115200;

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

	// Enable UART Transmit
	UART_TxCmd(CONSOLE_DEV, ENABLE);
}
Exemple #16
0
void DEBUG_init(void)
// init code for the USB serial line
{
	UART_CFG_Type UARTConfigStruct;			// UART Configuration structure variable
	UART_FIFO_CFG_Type UARTFIFOConfigStruct;	// UART FIFO configuration Struct variable
	PINSEL_CFG_Type PinCfg;				// Pin configuration for UART
	/*
	 * Initialize UART pin connect
	 */
	PinCfg.Funcnum = 1;
	PinCfg.OpenDrain = 0;
	PinCfg.Pinmode = 0;
	// USB serial first
	PinCfg.Portnum = 0;
	PinCfg.Pinnum = 2;
	PINSEL_ConfigPin(&PinCfg);
	PinCfg.Pinnum = 3;
	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 FIFOConfigStruct to default state:
	 * - FIFO_DMAMode = DISABLE
	 * - FIFO_Level = UART_FIFO_TRGLEV0
	 * - FIFO_ResetRxBuf = ENABLE
	 * - FIFO_ResetTxBuf = ENABLE
	 * - FIFO_State = ENABLE
	 */
	UART_FIFOConfigStructInit(&UARTFIFOConfigStruct);
	// Built the basic structures, lets start the devices/
	// USB serial
	UART_Init((LPC_UART_TypeDef *)LPC_UART0, &UARTConfigStruct);		// Initialize UART0 peripheral with given to corresponding parameter
	UART_FIFOConfig((LPC_UART_TypeDef *)LPC_UART0, &UARTFIFOConfigStruct);	// Initialize FIFO for UART0 peripheral
	UART_TxCmd((LPC_UART_TypeDef *)LPC_UART0, ENABLE);			// Enable UART Transmit
	
}
Exemple #17
0
void printsetup(void){
        UART_CFG_Type UARTConfigStruct;			// UART Configuration structure variable
	UART_FIFO_CFG_Type UARTFIFOConfigStruct;	// UART FIFO configuration Struct variable
	PINSEL_CFG_Type PinCfg;				// Pin configuration for UART
        
        PinCfg.OpenDrain = 0;
        PinCfg.Pinmode = 0;
        PinCfg.Funcnum = 1;   
        PinCfg.Portnum = 0;
        PinCfg.Pinnum = 2;
        PINSEL_ConfigPin(&PinCfg);
        PinCfg.Pinnum = 3;
        PINSEL_ConfigPin(&PinCfg);

        UART_ConfigStructInit(&UARTConfigStruct);      

        UART_FIFOConfigStructInit(&UARTFIFOConfigStruct);

        UART_Init((LPC_UART_TypeDef *) LPC_UART0, &UARTConfigStruct);

        UART_TxCmd((LPC_UART_TypeDef *) LPC_UART0, ENABLE);
}
Exemple #18
0
void serialUSBInit() {
    UART_CFG_Type uartConfigStruct;
    UART_FIFO_CFG_Type uartFIFOConfigStruct;
    PINSEL_CFG_Type pinCfg;

    //Transmission setup
    pinCfg.Funcnum = 1;
    pinCfg.OpenDrain = 0;
    pinCfg.Pinmode = 0;
    pinCfg.Portnum = 0;
    pinCfg.Pinnum = 2;
    PINSEL_ConfigPin(&pinCfg);

    //Reciever setup
    pinCfg.Pinnum = 3;
    PINSEL_ConfigPin(&pinCfg);

    UART_ConfigStructInit(&uartConfigStruct);
    UART_FIFOConfigStructInit(&uartFIFOConfigStruct);

    UART_Init((LPC_UART_TypeDef *)LPC_UART0, &uartConfigStruct);
    UART_FIFOConfig((LPC_UART_TypeDef *)LPC_UART0, &uartFIFOConfigStruct);
    UART_TxCmd((LPC_UART_TypeDef *)LPC_UART0, ENABLE);
}
Exemple #19
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);

}
Exemple #20
0
void initSerial() {

  if (DBG_LEVEL >= 2) _DBG_("Starting Serial Init");
  
// UART Configuration structure variable
	UART_CFG_Type uartConfig;
// UART FIFO configuration Struct variable
	UART_FIFO_CFG_Type uartFifoConfig;
// Pin configuration for UART
	PINSEL_CFG_Type PinCfg;

  if (DBG_LEVEL >= 2) _DBG_("Structs assigned");
  
// Initialize UART pin connect
	PinCfg.Funcnum = PINSEL_FUNC_2;
	PinCfg.OpenDrain = PINSEL_PINMODE_NORMAL;
	PinCfg.Pinmode = PINSEL_PINMODE_PULLUP;
	PinCfg.Portnum = PINSEL_PORT_0;
	PinCfg.Pinnum = 0; //Out pin 9
	PINSEL_ConfigPin(&PinCfg);
	PinCfg.Pinnum = 1; //Out pin 10
	PINSEL_ConfigPin(&PinCfg);
	
	if (DBG_LEVEL >= 2) _DBG_("Pins Configured");
	
	/* Initialize UART Configuration parameter structure:
	 * - Baudrate = 115200bps
	 * - 8 data bit
	 * - 1 Stop bit
	 * - No parity
	 */
	UART_ConfigStructInit(&uartConfig);
	  uartConfig.Parity = UART_PARITY_NONE;
    uartConfig.Databits = UART_DATABIT_8;
    uartConfig.Stopbits = UART_STOPBIT_1;
    uartConfig.Baud_rate = 115200;
	
	if (DBG_LEVEL >= 2) _DBG_("UART Configured");
	
	/* Initialize FIFOConfigStruct to default state:
	 * - FIFO_DMAMode = DISABLE
	 * - FIFO_Level = UART_FIFO_TRGLEV0
	 * - FIFO_ResetRxBuf = ENABLE
	 * - FIFO_ResetTxBuf = ENABLE
	 * - FIFO_State = ENABLE
	 */
	UART_FIFOConfigStructInit(&uartFifoConfig);
  
  if (DBG_LEVEL >= 2) _DBG_("FIFO Configured");
  
// Initialize UART3
	UART_Init(LPC_UART3, &uartConfig);
	
	if (DBG_LEVEL >= 2) _DBG_("UART Initialised");
	
// Initialize FIFO for UART3
	UART_FIFOConfig(LPC_UART3, &uartFifoConfig);
	
	if (DBG_LEVEL >= 2) _DBG_("FIFO Initialised");
	
// Enable UART Transmit
	UART_TxCmd(LPC_UART3, ENABLE);
	
	if (DBG_LEVEL >= 2) _DBG_("UART Started");
}
/*********************************************************************//**
 * @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

#if (UART_PORT == 0)
	/*
	 * 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);
#endif

#if (UART_PORT == 1)
	/*
	 * Initialize UART1 pin connect
	 */
	PinCfg.Funcnum = 2;
	PinCfg.OpenDrain = 0;
	PinCfg.Pinmode = 0;
	PinCfg.Pinnum = 0;
	PinCfg.Portnum = 2;
	PINSEL_ConfigPin(&PinCfg);
	PinCfg.Pinnum = 1;
	PINSEL_ConfigPin(&PinCfg);
#endif

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

	// Initialize UART0 peripheral with given to corresponding parameter
	UART_Init(TEST_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);

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


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

	// 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 = UART_Receive(TEST_UART, buffer, sizeof(buffer), NONE_BLOCKING);
        }

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

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

    // DeInitialize UART0 peripheral
    UART_DeInit(TEST_UART);

    /* Loop forever */
    while(1);
    return 1;
}
/*********************************************************************//**
 * @brief		c_entry: Main UART 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;

	uint32_t idx, len;
	__IO FlagStatus exitflag;
	uint8_t buffer[10];

	/*
	 * Initialize UART1 pin connect
	 */
    PINSEL_ConfigPin(0, 15, 1);//UART1 - TXD
	PINSEL_ConfigPin(0, 16, 1);//UART1 - RXD
	PINSEL_ConfigPin(0, 17, 1);//UART1 - CTS
	PINSEL_ConfigPin(0, 22, 1);//UART1 - RTS

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

	// Initialize UART0 peripheral with given to corresponding parameter
	UART_Init(TEST_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);

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

	// Configure UART1 hardware flow control RTS/CTS
	UART_FullModemForcePinState(TEST_UART,UART1_MODEM_PIN_RTS,ACTIVE);

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

	// 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 = UART_Receive(TEST_UART, buffer, sizeof(buffer), NONE_BLOCKING);
        }

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

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

    // DeInitialize UART0 peripheral
    UART_DeInit(TEST_UART);

    /* Loop forever */
    while(1);
}
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;
}
Exemple #24
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;
}
/*********************************************************************//**
 * @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;
}
/*********************************************************************//**
 * @brief		c_entry: Main UART 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 for UART
	PINSEL_CFG_Type PinCfg;
	uint32_t idx,len;
	__IO FlagStatus exitflag;
	uint8_t buffer,temp;

#if (UART_PORT == 0)
	/*
	 * Initialize UART0 pin connect
	 */
	PinCfg.Funcnum = 1;
	PinCfg.OpenDrain = 0;
	PinCfg.Pinmode = 0;
	PinCfg.Pinnum = 2;
	PinCfg.Portnum = 0;
	PINSEL_ConfigPin(&PinCfg);//P0.2 TXD0
	PinCfg.Pinnum = 3;
	PINSEL_ConfigPin(&PinCfg);//P0.3 RXD0
#endif

#if (UART_PORT == 1)
	/*
	 * Initialize UART1 pin connect
	 */
	PinCfg.Funcnum = 2;
	PinCfg.OpenDrain = 0;
	PinCfg.Pinmode = 0;
	PinCfg.Pinnum = 0;
	PinCfg.Portnum = 2;
	PINSEL_ConfigPin(&PinCfg);
	PinCfg.Pinnum = 1;
	PINSEL_ConfigPin(&PinCfg);
#endif
	/*
	 * Initialize UART3 pin connect
	 */
	PinCfg.Funcnum = 3;
	PinCfg.OpenDrain = 0;
	PinCfg.Pinmode = 0;
	PinCfg.Pinnum = 25;
	PinCfg.Portnum = 0;
	PINSEL_ConfigPin(&PinCfg);//P0.25 TXD3

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

	// Initialize UART0 & UART3 peripheral with given to corresponding parameter
	UART_Init(TEST_UART, &UARTConfigStruct);
	UART_Init(TEST_IRDA, &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 & UART3 peripheral
	UART_FIFOConfig(TEST_UART, &UARTFIFOConfigStruct);
	UART_FIFOConfig(TEST_IRDA, &UARTFIFOConfigStruct);

	//Configure and enable IrDA mode on UART
	UART_IrDACmd(TEST_IRDA,ENABLE);
	// Enable UART Transmit
	UART_TxCmd(TEST_UART, ENABLE);
	UART_TxCmd(TEST_IRDA, ENABLE);
	// print welcome screen
	print_menu();

	// Reset exit flag
	exitflag = RESET;
	idx=0;buffer=0;

    /* Read some data from the buffer */
    while (exitflag == RESET)
    {
    	if(idx==0)
    	{
    		UART_Send(TEST_UART, menu5, sizeof(menu5), BLOCKING);
    	}
    	len=0;
    	while(len==0)
    	{
    		len = UART_Receive(TEST_UART, &temp, 1, NONE_BLOCKING);
    	}
    	if(temp==27)
    	{
    		UART_Send(TEST_UART, menu4, sizeof(menu4), BLOCKING);
    		exitflag=SET;
    	}
    	else if(temp=='r')
    	{
    		idx=0;buffer=0;
    		print_menu();
    		UART_Send(TEST_IRDA, &buffer, 1, BLOCKING);
    	}
    	else
    	{
    		idx++;
    		switch(temp)
    		{
    		case '0': buffer=(buffer<<4)|0x00;break;
    		case '1': buffer=(buffer<<4)|0x01;break;
    		case '2': buffer=(buffer<<4)|0x02;break;
    		case '3': buffer=(buffer<<4)|0x03;break;
    		case '4': buffer=(buffer<<4)|0x04;break;
    		case '5': buffer=(buffer<<4)|0x05;break;
    		case '6': buffer=(buffer<<4)|0x06;break;
    		case '7': buffer=(buffer<<4)|0x07;break;
    		case '8': buffer=(buffer<<4)|0x08;break;
    		case '9': buffer=(buffer<<4)|0x09;break;
    		case 'a': buffer=(buffer<<4)|0x0A;break;
    		case 'A': buffer=(buffer<<4)|0x0A;break;
    		case 'b': buffer=(buffer<<4)|0x0B;break;
    		case 'B': buffer=(buffer<<4)|0x0B;break;
    		case 'c': buffer=(buffer<<4)|0x0C;break;
    		case 'C': buffer=(buffer<<4)|0x0C;break;
    		case 'd': buffer=(buffer<<4)|0x0D;break;
    		case 'D': buffer=(buffer<<4)|0x0D;break;
    		case 'e': buffer=(buffer<<4)|0x0E;break;
    		case 'E': buffer=(buffer<<4)|0x0E;break;
    		case 'f': buffer=(buffer<<4)|0x0F;break;
    		case 'F': buffer=(buffer<<4)|0x0F;break;
    		default: idx=0;buffer=0;break;
    		}
    		if(idx==2)
    		{
    			temp=buffer>>4;
    			if(temp <= 9)temp=temp+ 0x30;
    			else temp=temp+0x37;
    			UART_Send(TEST_UART, &temp, 1, BLOCKING);
    			temp=(buffer&0x0F);
    			if(temp <= 9)temp=temp+ 0x30;
    			else temp=temp+0x37;
    			UART_Send(TEST_UART, &temp, 1, BLOCKING);

            	UART_Send(TEST_IRDA, &buffer, 1, BLOCKING);
            	idx=0;buffer=0;
    		}
    	}
    }
/*********************************************************************//**
 * @brief		c_entry: Main UART-FULLMODEM 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 for UART1
	PINSEL_CFG_Type PinCfg;
	uint32_t idx, len;
	__IO FlagStatus exitflag;
	uint8_t buffer[10];

	/*
	 * Initialize UART1 pin connect
	 * If using MCB1700 eval board, assign pin P2.0 - P2.7
	 * If using IAR 1768 KS board, assign pin P0.7 - P0.15
	 */
#ifdef MCB_LPC_1768
	PinCfg.Funcnum = 2;
	PinCfg.OpenDrain = 0;
	PinCfg.Pinmode = 0;
	PinCfg.Portnum = 2;
	for (idx = 0; idx <= 7; idx++){
		PinCfg.Pinnum = idx;
		PINSEL_ConfigPin(&PinCfg);
	}
#elif defined(IAR_LPC_1768)
	PinCfg.Funcnum = 1;
	PinCfg.OpenDrain = 0;
	PinCfg.Pinmode = 0;
	PinCfg.Portnum = 0;
	for (idx = 15; idx <= 22; idx++){
		PinCfg.Pinnum = idx;
		PINSEL_ConfigPin(&PinCfg);
	}
#endif

	/* 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((LPC_UART_TypeDef *)LPC_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(LPC_UART1, UART1_MODEM_MODE_AUTO_RTS, ENABLE);
	UART_FullModemConfigMode(LPC_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);
	// Force RTS pin state to ACTIVE
	UART_FullModemForcePinState(LPC_UART1, UART1_MODEM_PIN_RTS, ACTIVE);
	//RESET RTS State flag
	RTS_State = ACTIVE;
#endif


    /* 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;
}
Exemple #28
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 );

}
/*********************************************************************//**
 * @brief		c_entry: Main UART 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 for UART0
	PINSEL_CFG_Type PinCfg;
	// Auto baudrate configuration structure
	UART_AB_CFG_Type ABConfig;

	uint32_t idx, len;
	__IO FlagStatus exitflag;
	uint8_t buffer[10];

	/*
	 * 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 = 9600bps
	 * 8 data bit
	 * 1 Stop bit
	 * None parity
	 */
	UART_ConfigStructInit(&UARTConfigStruct);

	/* Initialize UART0 peripheral with given to corresponding parameter
	 * in this case, don't care the baudrate value UART initialized
	 * since this will be determine when running auto baudrate
	 */
	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);


    /* Enable UART End of Auto baudrate interrupt */
	UART_IntConfig(LPC_UART0, UART_INTCFG_ABEO, ENABLE);
	/* Enable UART Auto baudrate timeout interrupt */
	UART_IntConfig(LPC_UART0, UART_INTCFG_ABTO, ENABLE);

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


/* ---------------------- Auto baud rate section ----------------------- */
	// Reset Synchronous flag for auto-baudrate mode
	Synchronous = RESET;

	// Configure Auto baud rate mode
    ABConfig.ABMode = UART_AUTOBAUD_MODE0;
    ABConfig.AutoRestart = ENABLE;

    // Start auto baudrate mode
    UART_ABCmd(LPC_UART0, &ABConfig, ENABLE);
    print_menu();

    /* Loop until auto baudrate mode complete */
    while (Synchronous == RESET);


    // Print status of auto baudrate
    UART_Send(LPC_UART0, syncmenu, sizeof(syncmenu), BLOCKING);
/* ---------------------- End of Auto baud rate section ----------------------- */

	// 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 = UART_Receive(LPC_UART0, buffer, sizeof(buffer), NONE_BLOCKING);
        }

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

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

    // DeInitialize UART0 peripheral
    UART_DeInit(LPC_UART0);

    /* Loop forever */
    while(1);
    return 1;
}
Exemple #30
0
/*********************************************************************//**
 * @brief		c_entry: Main UART program body
 * @param[in]	None
 * @return 		int
 **********************************************************************/
int c_entry(void)
{
	uint8_t *rx_char;
	uint32_t idx;
	// UART Configuration structure variable
	UART_CFG_Type UARTConfigStruct;
	// UART FIFO configuration Struct variable
	UART_FIFO_CFG_Type UARTFIFOConfigStruct;
	GPDMA_Channel_CFG_Type GPDMACfg;
	// Pin configuration for UART0
	PINSEL_CFG_Type PinCfg;

	/*
	 * 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 = 9600bps
	 * 8 data bit
	 * 1 Stop bit
	 * None parity
	 */
	UART_ConfigStructInit(&UARTConfigStruct);

	// 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);

	// Enable DMA mode in UART
	UARTFIFOConfigStruct.FIFO_DMAMode = ENABLE;

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

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


	/* GPDMA Interrupt configuration section ------------------------------------------------- */

    /* 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 GPDMA channel --------------------------------
	// channel 0
	GPDMACfg.ChannelNum = 0;
	// Source memory
	GPDMACfg.SrcMemAddr = (uint32_t) &menu1;
	// Destination memory - don't care
	GPDMACfg.DstMemAddr = 0;
	// Transfer size
	GPDMACfg.TransferSize = sizeof(menu1);
	// Transfer width - don't care
	GPDMACfg.TransferWidth = 0;
	// Transfer type
	GPDMACfg.TransferType = GPDMA_TRANSFERTYPE_M2P;
	// Source connection - don't care
	GPDMACfg.SrcConn = 0;
	// Destination connection
	GPDMACfg.DstConn = GPDMA_CONN_UART0_Tx;
	// Linker List Item - unused
	GPDMACfg.DMALLI = 0;
	// Setup channel with given parameter
	GPDMA_Setup(&GPDMACfg);

	// Setup GPDMA channel --------------------------------
	// channel 1
	GPDMACfg.ChannelNum = 1;
	// Source memory - don't care
	GPDMACfg.SrcMemAddr = 0;
	// Destination memory
	GPDMACfg.DstMemAddr = (uint32_t) &rx_buf;
	// Transfer size
	GPDMACfg.TransferSize = sizeof(rx_buf);
	// Transfer width - don't care
	GPDMACfg.TransferWidth = 0;
	// Transfer type
	GPDMACfg.TransferType = GPDMA_TRANSFERTYPE_P2M;
	// Source connection
	GPDMACfg.SrcConn = GPDMA_CONN_UART0_Rx;
	// Destination connection - don't care
	GPDMACfg.DstConn = 0;
	// Linker List Item - unused
	GPDMACfg.DMALLI = 0;
	GPDMA_Setup(&GPDMACfg);

	/* Reset terminal counter */
	Channel0_TC = 0;
	/* Reset Error counter */
	Channel0_Err = 0;

    // Enable interrupt for DMA
    NVIC_EnableIRQ (DMA_IRQn);

	// Enable GPDMA channel 0
	GPDMA_ChannelCmd(0, ENABLE);
	// Make sure GPDMA channel 1 is disabled
	GPDMA_ChannelCmd(1, DISABLE);

	/* Wait for GPDMA on UART0 Tx processing complete */
    while ((Channel0_TC == 0) && (Channel0_Err == 0));

    // Main loop - echos back to the terminal
    while (1)
    {
    	/* Reset terminal counter */
    	Channel1_TC = 0;
    	/* Reset Error counter */
    	Channel1_Err = 0;

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

    	// Enable GPDMA channel 1
    	GPDMA_ChannelCmd(1, ENABLE);

    	// Clear Rx buffer using DMA
    	for (idx = 0; idx < RX_BUF_SIZE; idx++){
    		rx_buf[idx] = 0;
    	}

        // now, start receive character using GPDMA
        rx_char = (uint8_t *) &rx_buf;
        while ((Channel1_TC == 0) && (Channel1_Err == 0)){
			// Check whether if there's any character received, then print it back
			if (*rx_char != 0)
			{
				UART_Send(LPC_UART0, rx_char, 1, BLOCKING);
				rx_char++;
			}
        }
    }

    // DeInitialize UART0 peripheral
    UART_DeInit(LPC_UART0);

    /* Loop forever */
    while(1);
    return 1;
}