arunkollam
Junior Member level 2
- Joined
- Jun 26, 2008
- Messages
- 20
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1,281
- Activity points
- 1,412
int UART0_getchar (void) {
while (!(LPC_UART0->LSR & 0x01))
{
}
}
return (LPC_UART0->RBR);
int UART1_getchar (void)
{
while (!(LPC_UART1->LSR & 0x01))
{
}
return (LPC_UART1->RBR);
}
Code C - [expand] 1 2 3 4 Time = 0; while ( (!(LPC_UART0->LSR & 0x01)) && (Time<LIMIT) ) Time++;
//****************************************************************************
// LPC122X Hardware setup
//
//****************************************************************************
// taken from NXP example code for LPB12000 to calculate baud rate setup
// This involves solving an equation with 2 or 3 unknowns depending on how you interpret
// the formula in the user manual.
// This is quite complex to solve so instead of writing from scratch, I've
// taken this example code including uart_set_divisors().
// This has been split into 2 routines: 1 for UART0 and another for UART1
// This allows me the option of changing the setup for one without compromising
// the other.
// The UART init routine has a precalculated default included for 115200
// If the baudrate is set to this in the defines, the precalculated version is used
// instead of calculating it.
// This calculating routine simply works so I've copied all the defines etc in a big block
// rather than work out which ones are not needed.
/**************************************************************************//**
* $Id: lpc12xx_uart.c 541 2010-09-15 09:04:20Z nxp21428 $
*
* @file lpc12xx_uart.c
* @brief Contains all functions support for UART firmware library on LPC12xx.
* @version 1.0
* @date 26. Sep. 2010
* @author NXP MCU Team
*
* @note
* Copyright (C) 2010 NXP Semiconductors(NXP). All rights reserved.
*
* @par
* Software that is described herein is for illustrative purposes only
* which provides customers with programming information regarding the
* products. This software is supplied "AS IS" without any warranties.
* NXP Semiconductors assumes no responsibility or liability for the
* use of the software, conveys no license or title under any patent,
* copyright, or mask work right to the product. NXP Semiconductors
* reserves the right to make changes in the software without
* notification. NXP Semiconductors also make no representation or
* warranty that such application will be suitable for the specified
* use without further testing or modification.
******************************************************************************/
/* Macro to define caculate */
#define UART_LOAD_DLL(div) ((div) & 0xFF)
#define UART_LOAD_DLM(div) (((div) >> 8) & 0xFF)
#define UART_FDR_DIVADDVAL(n) ((uint32_t)(n&0x0F)) /* Pre-scaler divisor */
#define UART_FDR_MULVAL(n) ((uint32_t)((n<<4)&0xF0)) /* Multiplier value */
#define UART_FIFOLVL_RXFIFOLVL(n) ((uint32_t)(n&0x0F)) /*receiver FIFO */
#define UART_FIFOLVL_TXFIFOLVL(n) ((uint32_t)((n>>8)&0x0F)) /*transmitter FIFO */
/* Macro to define parameter */
#define UART_ACCEPTED_BAUDRATE_ERROR (1) /* Acceptable baudrate error, 1/2(n) percent */
#define UART_TX_FIFO_SIZE (16)
#define UART_DEVICE_NUMBER (2)
/* Macro to define bit mask */
#define UART_RBR_MASKBIT ((uint8_t)0xFF) /* Received Buffer mask bit */
#define UART_THR_MASKBIT ((uint8_t)0xFF) /* Transmit Holding mask bit */
#define UART_IER_BITMASK ((uint32_t)(0x307)) /* IER normal bit mask */
#define UART_IER_BITMASK2 ((uint32_t)(0x38F)) /* IER include modem bit mask */
#define UART_IIR_BITMASK ((uint32_t)(0x3CF)) /* IIR bit mask */
#define UART_FCR_BITMASK ((uint8_t)(0xCF)) /* FCR bit mask */
#define UART_LCR_BITMASK ((uint8_t)(0xFF)) /* LCR bit mask */
#define UART_MCR_BITMASK ((uint8_t)(0xF3)) /* MCR bit mask */
#define UART_LSR_BITMASK ((uint8_t)(0xFF)) /* LSR bit mask */
#define UART_MSR_BITMASK ((uint8_t)(0xFF)) /* MSR register bit mask */
#define UART_SCR_BIMASK ((uint8_t)(0xFF)) /* UART Scratch Pad bit mask */
#define UART_ACR_BITMASK ((uint32_t)(0x307)) /* ACR bit mask */
#define UART_ICR_BITMASK ((uint32_t)(0x3F)) /* IRDA bit mask */
#define UART_FDR_BITMASK ((uint32_t)(0xFF)) /* FDR bit mask */
#define UART_TER_BITMASK ((uint8_t)(0x80)) /* TER bit mask */
#define UART_RS485CTRL_BITMASK ((uint32_t)(0x3F)) /* 485 CTRL bit mask */
#define UART_RS485ADRMATCH_BITMASK ((uint8_t)(0xFF)) /* 485 ADR bit mask */
#define UART_RS485DLY_BITMASK ((uint8_t)(0xFF)) /* 485 DLY bit mask */
#define UART_FIFOLVL_BITMASK ((uint32_t)(0x0F0F))/* 485 FIFOLVL bit mask */
/* Macro to define control bit */
#define UART_LCR_DLAB_EN ((uint8_t)(1<<7)) /* DivisorLatchesAccess enable*/
#define UART_FCR_FIFO_EN ((uint8_t)(1<<0)) /* FIFO enable */
#define UART_FCR_RX_RS ((uint8_t)(1<<1)) /* FIFO RX reset */
#define UART_FCR_TX_RS ((uint8_t)(1<<2)) /* FIFO TX reset */
#define UART_TER_TXEN ((uint8_t)(1<<7)) /* Transmit enable bit */
#define UART_ACR_START ((uint32_t)(1<<0)) /* Auto baudrate Start */
#define UART_ACR_ABEOINT_CLR ((uint32_t)(1<<8)) /* Auto baudrate end INT clear */
#define UART_ACR_ABTOINT_CLR ((uint32_t)(1<<9)) /* Auto baudrate Ttime-out INT clear */
//****************************************************************************
// RS485 UART setup and initialisation
// Uses UART0
//****************************************************************************
/*
* @brief Set uart baudrate
* @param UARTx Pointer to selected UART peripheral, should be LPC_UART0, LPC_UART1.
* @param baudrate baudrate number
* @return status SUCCESS or ERROR
*/
/**************************************************************************//**
* $Id: lpc12xx_uart.c 541 2010-09-15 09:04:20Z nxp21428 $
*
* @file lpc12xx_uart.c
* @brief Contains all functions support for UART firmware library on LPC12xx.
* @version 1.0
* @date 26. Sep. 2010
* @author NXP MCU Team
*
* @note
* Copyright (C) 2010 NXP Semiconductors(NXP). All rights reserved.
*
* @par
* Software that is described herein is for illustrative purposes only
* which provides customers with programming information regarding the
* products. This software is supplied "AS IS" without any warranties.
* NXP Semiconductors assumes no responsibility or liability for the
* use of the software, conveys no license or title under any patent,
* copyright, or mask work right to the product. NXP Semiconductors
* reserves the right to make changes in the software without
* notification. NXP Semiconductors also make no representation or
* warranty that such application will be suitable for the specified
* use without further testing or modification.
******************************************************************************/
// Routine taken from example code from NXP for LPC1200
int uart0_set_divisors( uint32_t baudrate)
{
uint32_t uClk = 0;
uint32_t calcBaudrate = 0;
uint32_t temp = 0;
uint32_t errorStatus = 0;
uint32_t mulFracDiv, dividerAddFracDiv;
uint32_t diviser = 0 ;
uint32_t mulFracDivOptimal = 1;
uint32_t dividerAddOptimal = 0;
uint32_t diviserOptimal = 0;
uint32_t relativeError = 0;
uint32_t relativeOptimalError = (baudrate >> UART_ACCEPTED_BAUDRATE_ERROR);
/* get UART block clock */
uClk = (MainClock/LPC_SYSCON->UART0CLKDIV)>>4; /* div by 16 */
// uClk = (MainClock/LPC_SYSCON->UART1CLKDIV)>>4; /* div by 16 */
/* In the Uart IP block, baud rate is calculated using FDR and DLL-DLM registers
* The formula is :
* BaudRate= uClk * (mulFracDiv/(mulFracDiv+dividerAddFracDiv) / (16 * (DLL)
* It involves floating point calculations. That's the reason the formulae are adjusted with
* Multiply and divide method.*/
/* The value of mulFracDiv and dividerAddFracDiv should comply to the following expressions:
* 0 < mulFracDiv <= 15, 0 <= dividerAddFracDiv <= 15 */
for (dividerAddFracDiv = 0 ; dividerAddFracDiv < 15 ;dividerAddFracDiv++){
for (mulFracDiv = (dividerAddFracDiv + 1) ; mulFracDiv <= 15 ;mulFracDiv++){
temp = (mulFracDiv * uClk) / ((mulFracDiv + dividerAddFracDiv));
diviser = temp / baudrate;
if ((temp % baudrate) > (baudrate >> 1))
diviser++;
if (diviser > 2 && diviser < 65536){
calcBaudrate = temp / diviser;
if (calcBaudrate <= baudrate)
relativeError = baudrate - calcBaudrate;
else
relativeError = calcBaudrate - baudrate;
if ((relativeError < relativeOptimalError)){
mulFracDivOptimal = mulFracDiv ;
dividerAddOptimal = dividerAddFracDiv;
diviserOptimal = diviser;
relativeOptimalError = relativeError;
errorStatus = SUCCESS;
}
} /* End of if */
} /* end of inner for loop */
} /* end of outer for loop */
// Now set up the UART0 registers
if (errorStatus == SUCCESS){
LPC_UART0->LCR |= UART_LCR_DLAB_EN;
/* Then reset DLAB bit */
while(LPC_UART0->LCR & UART_LCR_DLAB_EN){
LPC_UART0->DLL = UART_LOAD_DLL(diviserOptimal);
LPC_UART0->DLM = UART_LOAD_DLM(diviserOptimal);
LPC_UART0->LCR &= (~UART_LCR_DLAB_EN) & UART_LCR_BITMASK;
LPC_UART0->FDR = (UART_FDR_MULVAL(mulFracDivOptimal) |\
UART_FDR_DIVADDVAL(dividerAddOptimal)) & UART_FDR_BITMASK;
}
}
return errorStatus;
}
int uart1_set_divisors( uint32_t baudrate)
{
uint32_t uClk = 0;
uint32_t calcBaudrate = 0;
uint32_t temp = 0;
uint32_t errorStatus = 0;
uint32_t mulFracDiv, dividerAddFracDiv;
uint32_t diviser = 0 ;
uint32_t mulFracDivOptimal = 1;
uint32_t dividerAddOptimal = 0;
uint32_t diviserOptimal = 0;
uint32_t relativeError = 0;
uint32_t relativeOptimalError = (baudrate >> UART_ACCEPTED_BAUDRATE_ERROR);
/* get UART block clock */
uClk = (MainClock/LPC_SYSCON->UART0CLKDIV)>>4; /* div by 16 */
// uClk = (MainClock/LPC_SYSCON->UART1CLKDIV)>>4; /* div by 16 */
/* In the Uart IP block, baud rate is calculated using FDR and DLL-DLM registers
* The formula is :
* BaudRate= uClk * (mulFracDiv/(mulFracDiv+dividerAddFracDiv) / (16 * (DLL)
* It involves floating point calculations. That's the reason the formulae are adjusted with
* Multiply and divide method.*/
/* The value of mulFracDiv and dividerAddFracDiv should comply to the following expressions:
* 0 < mulFracDiv <= 15, 0 <= dividerAddFracDiv <= 15 */
for (dividerAddFracDiv = 0 ; dividerAddFracDiv < 15 ;dividerAddFracDiv++){
for (mulFracDiv = (dividerAddFracDiv + 1) ; mulFracDiv <= 15 ;mulFracDiv++){
temp = (mulFracDiv * uClk) / ((mulFracDiv + dividerAddFracDiv));
diviser = temp / baudrate;
if ((temp % baudrate) > (baudrate >> 1))
diviser++;
if (diviser > 2 && diviser < 65536){
calcBaudrate = temp / diviser;
if (calcBaudrate <= baudrate)
relativeError = baudrate - calcBaudrate;
else
relativeError = calcBaudrate - baudrate;
if ((relativeError < relativeOptimalError)){
mulFracDivOptimal = mulFracDiv ;
dividerAddOptimal = dividerAddFracDiv;
diviserOptimal = diviser;
relativeOptimalError = relativeError;
errorStatus = SUCCESS;
}
} /* End of if */
} /* end of inner for loop */
} /* end of outer for loop */
// Now set up the UART0 registers
if (errorStatus == SUCCESS){
LPC_UART1->LCR |= UART_LCR_DLAB_EN;
/* Then reset DLAB bit */
while(LPC_UART1->LCR & UART_LCR_DLAB_EN){
LPC_UART1->DLL = UART_LOAD_DLL(diviserOptimal);
LPC_UART1->DLM = UART_LOAD_DLM(diviserOptimal);
LPC_UART1->LCR &= (~UART_LCR_DLAB_EN) & UART_LCR_BITMASK;
LPC_UART1->FDR = (UART_FDR_MULVAL(mulFracDivOptimal) |\
UART_FDR_DIVADDVAL(dividerAddOptimal)) & UART_FDR_BITMASK;
}
}
return errorStatus;
}
/*----------------------------------------------------------------------------
Initialize UART0 pins, Baudrate
*----------------------------------------------------------------------------*/
void UART0_init (void) {
volatile uint32_t regVal;
uint32_t Fdiv;
/* configure PINs GPIO0.1, GPIO0.2 for UART0 */
LPC_SYSCON->SYSAHBCLKCTRL |= ((1UL << 31) | /* enable clock for GPIO0 */
(1UL << 16) ); /* enable clock for IOCON */
LPC_IOCON->PIO0_1 &= ~(7UL << 0); /* clear FUNC bits */
LPC_IOCON->PIO0_1 |= (2UL << 0); /* P0.1 is RxD0 */
LPC_IOCON->PIO0_2 &= ~(7UL << 0); /* clear FUNC bits */
LPC_IOCON->PIO0_2 |= (2UL << 0); /* P0.2 is TxD0 */
/* configure UART0 */
LPC_SYSCON->SYSAHBCLKCTRL |= (1UL << 12); /* Enable clock to UART0 */
LPC_SYSCON->UART0CLKDIV = (2UL << 0); /* UART0 clock = CCLK / 2 */
LPC_UART0->LCR = 0x83; /* 8 bits, no Parity, 1 Stop bit */
if(UART0_BAUDRATE == 115200){
// This is where the UART is configured if it is 115200 DEBUG/TEST ONLY...
LPC_UART0->LCR = 0x83; /* 8 bits, no Parity, 1 Stop bit */
LPC_UART0->DLL = 4; /* 115200 Baud Rate @ 12.0 MHZ PCLK */
LPC_UART0->FDR = 0x85; /* FR 1.627, DIVADDVAL 5, MULVAL 8 */
LPC_UART0->DLM = 0; /* High divisor latch = 0 */
LPC_UART0->LCR = 0x03; /* DLAB = 0 */
}
else
{//This is where we calculate the baudrate settings using NXP sample software
uart0_set_divisors(UART0_BAUDRATE); // let's go do the maths and work it all out
}
regVal = LPC_UART0->LSR; /* clear status register */
}
void UART1_init (void) {
volatile uint32_t regVal;
uint32_t Fdiv;
/* configure PINs GPIO0.9, GPIO0.8 for UART1 */
LPC_SYSCON->SYSAHBCLKCTRL |= ((1UL << 31) | /* enable clock for GPIO0 */
(1UL << 16) ); /* enable clock for IOCON */
LPC_IOCON->PIO0_9 &= ~(7UL << 0); /* clear FUNC bits */
LPC_IOCON->PIO0_9 |= (2UL << 0); /* P0.1 is RxD0 */
LPC_IOCON->PIO0_8 &= ~(7UL << 0); /* clear FUNC bits */
LPC_IOCON->PIO0_8 |= (2UL << 0); /* P0.2 is TxD0 */
/* configure UART0 */
LPC_SYSCON->SYSAHBCLKCTRL |= (1UL << 13); /* Enable clock to UART1 */
LPC_SYSCON->UART1CLKDIV = (2UL << 0); /* UART0 clock = CCLK / 2 */
LPC_UART1->LCR = 0x83; /* 8 bits, no Parity, 1 Stop bit */
if(UART1_BAUDRATE == 115200){
// This is where the UART is configured if it is 115200 DEBUG/TEST ONLY...
LPC_UART1->LCR = 0x83; /* 8 bits, no Parity, 1 Stop bit */
LPC_UART1->DLL = 4; /* 115200 Baud Rate @ 12.0 MHZ PCLK */
LPC_UART1->FDR = 0x85; /* FR 1.627, DIVADDVAL 5, MULVAL 8 */
LPC_UART1->DLM = 0; /* High divisor latch = 0 */
LPC_UART1->LCR = 0x03; /* DLAB = 0 */
}
else
{//This is where we calculate the baudrate settings using NXP sample software
uart1_set_divisors(UART1_BAUDRATE); // let's go do the maths and work it all out
}
}
/*----------------------------------------------------------------------------
Write character to Serial Port
*----------------------------------------------------------------------------*/
uint8_t UART0_sendchar (uint8_t c) {
while (!(LPC_UART0->LSR & 0x20));
LPC_UART0->THR = c;
return (c);
}
uint8_t UART1_sendchar (uint8_t c) {
while (!(LPC_UART1->LSR & 0x20));
LPC_UART1->THR = c;
return (c);
}
/*----------------------------------------------------------------------------
Read character from Serial Port (blocking read)
*----------------------------------------------------------------------------*/
// This is where the alarm check routines are inserted.
// If there is no character to get, go check the alarm status.
// If the alarm is triggered, interrupt the process and go send the new messages
// The CPU has a 15 byte input buffer so incoming messages will get stored while the alarm is sorted out.
// They will then get picked up after sorting out the alarm.
// If this buffer is filled, then messages will get lost/ignored/dumped until the buffer is emptied.uint32_t delay[16
//
int UART0_getchar (void) {
while (!(LPC_UART0->LSR & 0x01))
{
// msTicks = 0; // clear the timer;
}
return (LPC_UART0->RBR);
}
int UART1_getchar (void)
{
{
Time = 0;
while ( (!(LPC_UART0->LSR & 0x01)) && (Time<LIMIT) )
Time++;
}
return (LPC_UART1->RBR);
}
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 int UART1_getchar (void) { unsigned long time; Time = 0; while ( (!(LPC_UART0->LSR & 0x01)) && (Time<LIMIT) ) // waits till a char to receive or time for 2 bytes to receive Time++; if ( time == LIMIT ) // if time == LIMIT that means its exited the while with time delay retun FAIL; return (LPC_UART1->RBR); // if successfully data received return received data }
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 int UART1_getchar (void) { unsigned long time; start timer; // with a delay of more than 2 byte while ( (!(LPC_UART0->LSR & 0x01)) && (T0IF == 0) ); // waits till a char to receive or timer interrupt to set if ( T0IF == 1 ) { T0IF = 0; return FAIL; } return (LPC_UART1->RBR); // if successfully data received return received data }
(LPC_UART0->LSR & 0x01)
#define LIMIT 2
#define END_COMMAND 0x00
main(void)
{
uint32_t *temp_ptr;
int pelcod_payload_status = 0;
int pelcod_message_type = 0;
//int i = 0 ; // counter
// Initialise the LEDs
LED_init();
// Initialise the RS232/RS485 ports
UART0_init();
UART1_init();
while (FOREVER)
{
pelcod_payload_status = A_get_osiris_payload();
switch (pelcod_payload_status)
{
case END_COMMAND:
break; // Our work is done so leave this case statement,
default:
//Unknown error HELP!!!!!!
//If we have got here something has seriously gone wrong as we should always get a valid error message
break;
}
}
}
int UART1_getchar (void)
{
unsigned long time;
Time = 0;
while ( (!(LPC_UART0->LSR & 0x01)) && (Time<LIMIT) ) // waits till a char to receive or time for 2 bytes to receive
Time++;
if ( time == LIMIT ) // if time == LIMIT that means its exited the while with time delay
{
return END_COMMAND; //this will returns to the main loop,
}
return (LPC_UART1->RBR); // if successfully data received return received data
}
int UART0_getchar (void) {
while (!(LPC_UART0->LSR & 0x01))
{
}
return (LPC_UART0->RBR);
}
int UART1_getchar (void)
{
unsigned long time;
Time = 0;
while ( (!(LPC_[B][U]UART0[/U][/B]->LSR & 0x01)) && (Time<LIMIT) ) // waits till a char to receive or time for 2 bytes to receive
Time++;
if ( time == LIMIT ) // if time == LIMIT that means its exited the while with time delay
retun FAIL;
return (LPC_UART1->RBR); // if successfully data received return received data
}
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?