im doing the project in lpc 1768.. for my project i have to read the serial port value from the cc2431 zigbee node..but i couldnt read the serial port value..while debugging the execution stops in the while loop in the serial read that is while (SER_BUF_EMPTY(ser_in));
board:MCB1700 baud rate:38400 code:
#include <RTL.h> #include <stdio.h> #include <LPC17xx.H> #include "serial.c"
int main() {
int a; int len=10; char b[10]; ser_OpenPort(1); ser_InitPort1 (38400,8,0,1); a=ser_Read(b,&len); printf("%d",a); ser_ClosePort(1);
}
serial.c:
#include "LPC17xx.h"
/*---------------------------------------------------------------------------- Defines for ring buffers *---------------------------------------------------------------------------*/ #define SER_BUF_SIZE (128) // serial buffer in bytes (power 2) #define SER_BUF_MASK (SER_BUF_SIZE-1ul) // buffer size mask #define SystemFrequency 50000000 /* Buffer read / write macros */ #define SER_BUF_RESET(serBuf) (serBuf.rdIdx = serBuf.wrIdx = 0) #define SER_BUF_WR(serBuf, dataIn) (serBuf.data[SER_BUF_MASK & serBuf.wrIdx++] = (dataIn)) #define SER_BUF_RD(serBuf) (serBuf.data[SER_BUF_MASK & serBuf.rdIdx++]) #define SER_BUF_EMPTY(serBuf) (serBuf.rdIdx == serBuf.wrIdx) #define SER_BUF_FULL(serBuf) (serBuf.rdIdx == serBuf.wrIdx+1) #define SER_BUF_COUNT(serBuf) (SER_BUF_MASK & (serBuf.wrIdx - serBuf.rdIdx))
// buffer type typedef struct __SER_BUF_T {
unsigned char data[SER_BUF_SIZE]; unsigned int wrIdx; unsigned int rdIdx;
} SER_BUF_T;
unsigned long ser_txRestart; // NZ if TX restart is required unsigned short ser_lineState; // ((msr << 8) | (lsr)) SER_BUF_T ser_out; // Serial data buffers SER_BUF_T ser_in;
/*---------------------------------------------------------------------------- open the serial port *---------------------------------------------------------------------------*/ void ser_OpenPort (char portNum) {
if ( portNum == 0 )
{ /* Port 0 */ NVIC_DisableIRQ(UART0_IRQn); LPC_PINCON->PINSEL0 &= ~0x000000F0; LPC_PINCON->PINSEL0 |= 0x00000050; /* RxD0 is P0.3 and TxD0 is P0.2 */ }
else
{ /* Port 1 */ NVIC_DisableIRQ(UART1_IRQn); LPC_PINCON->PINSEL4 &= ~0x0000000F; LPC_PINCON->PINSEL4 |= 0x0000000A; /* Enable RxD1 P2.1, TxD1 P2.0 */ }
return;
/*---------------------------------------------------------------------------- close the serial port *---------------------------------------------------------------------------*/
void ser_ClosePort (char portNum ) {
{ /* POrt 0 */ LPC_PINCON->PINSEL0 &= ~0x000000F0; /* Disable the interrupt in the VIC and UART controllers */ LPC_UART0->IER = 0; NVIC_DisableIRQ(UART0_IRQn); }
{ /* Port 1 */ LPC_PINCON->PINSEL4 &= ~0x0000000F; /* Disable the interrupt in the VIC and UART controllers */ LPC_UART1->IER = 0; NVIC_DisableIRQ(UART1_IRQn); }
} /*---------------------------------------------------------------------------- initialize the serial port *---------------------------------------------------------------------------*/
void ser_InitPort1 (unsigned long baudrate, unsigned int databits, unsigned int parity, unsigned int stopbits)
{
unsigned char lcr_p, lcr_s, lcr_d; unsigned int dll; unsigned int pclkdiv, pclk;
switch (databits) { case 5: // 5 Data bits lcr_d = 0x00; break; case 6: // 6 Data bits lcr_d = 0x01; break; case 7: // 7 Data bits lcr_d = 0x02; break; case 8: // 8 Data bits default: lcr_d = 0x03; break;
switch (stopbits)
case 1: // 1,5 Stop bits case 2: // 2 Stop bits lcr_s = 0x04; break; case 0: // 1 Stop bit default: lcr_s = 0x00; break; }
switch (parity)
case 1: // Parity Odd lcr_p = 0x08; break; case 2: // Parity Even lcr_p = 0x18; break; case 3: // Parity Mark lcr_p = 0x28; break; case 4: // Parity Space lcr_p = 0x38; break; case 0: // Parity None default: lcr_p = 0x00; break; }
SER_BUF_RESET(ser_out); // reset out buffer SER_BUF_RESET(ser_in); // reset in buffer
/* Bit 8,9 are for UART1 */ pclkdiv = (LPC_SC->PCLKSEL0 >> 8) & 0x03;
switch ( pclkdiv )
{ case 0x00: default: pclk = SystemFrequency/4; break; case 0x01: pclk = SystemFrequency; break; case 0x02: pclk = SystemFrequency/2; break; case 0x03: pclk = SystemFrequency/8; break; }
dll = (pclk/16)/baudrate ; /*baud rate */ LPC_UART1->FDR = 0; // Fractional divider not used LPC_UART1->LCR = 0x80 | lcr_d | lcr_p | lcr_s; // Data bits, Parity, Stop bit LPC_UART1->DLL = dll; // Baud Rate depending on PCLK LPC_UART1->DLM = (dll >> 8); // High divisor latch LPC_UART1->LCR = 0x00 | lcr_d | lcr_p | lcr_s; // DLAB = 0 LPC_UART1->IER = 0x03; // Enable TX/RX interrupts
LPC_UART1->FCR = 0x07; /* Enable and reset TX and RX FIFO. */ ser_txRestart = 1; // TX fifo is empty
/* Enable the UART Interrupt */ NVIC_EnableIRQ(UART1_IRQn); return; }
/*---------------------------------------------------------------------------- read data from serial port *---------------------------------------------------------------------------*/ int ser_Read (char *buffer, const int *length)
int bytesToRead, bytesRead;
/* Read *length bytes, block if *bytes are not avaialable */
bytesToRead = *length; bytesToRead = (bytesToRead < (*length)) ? bytesToRead : (*length); bytesRead = bytesToRead;
while (bytesToRead--)
{ while (SER_BUF_EMPTY(ser_in)); // Block until data is available if none *buffer++ = SER_BUF_RD(ser_in);
return (bytesRead);
So the main thing you have learned is that code that blocks, waiting for data to read are a bad design idea. Would you stand outside the supermarket the full night if they for some reason happen to close one hour earlier so you didn't get in to buy yuor food at the last minute? If it's bad to stand a night outside a closed supermarket, it's also bad for your software to just hang, wating for data that isn't arriving.
Isn't it now time to try some normal debugging? Maybe the zigbee node requires you to send something to it before it will respond? Maybe it's incorrectly connected? Maybe it isn't using the serial configuration you have specified.
Exactly what have you done to debug this issue, besides posting here?
zigbee node sending data...i can see that using tera term...and also settings for serial port are correct..38400 baud rate,parity none,8 data bits,1 stop bit
So what have you done to check the connection to the LPC, and the receiving software in the LPC?
I don't think anyone will even attempt to read your code, since you did not bother to read the instructions how to post source code. The little infomation available directly above the message input box. Notice what happened to the line breaks, and indentations, when you posted it as just message text?
Why don't you use a normal interrupt-driven ring-buffer solution? If Keil doesn't have some sample code perfect for your processor, they do have for similar NXP processors that you can adapt.
Next thing - a million #define - doesn't directly make your code easier to read. It just hides potential logic errors.
And what does you UART interrupt handler look like?
Come on man.
but i couldnt read the serial port value
Oh yeah.
i stored the serial read value in the iteger "a" and displayed using the printf statement.,..but the serial window doesnt display anything..
"i stored the serial read value in the iteger 'a'"
Why an integer?
"displayed using the printf statement"
If you don't get anything from you printf statement, how do you know if the problem lies in the connection from the LPC to the PC, in the printf itself, in the receiving of the character, in the input connection to the LPC, etc, etc?
You really need to get down to debugging your system to see where exactly it's going wrong!
Here's some tips to get you started:
www.8052.com/.../120313
www.eetimes.com/.../Developing-a-good-bedside-manner