Modify UARTInit for polling

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Modify UARTInit for polling

974 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by cwpjr on Thu May 16 08:58:49 MST 2013
I took the UARTInit function from the UART example program (LPC1114/301) and commented out the interrupt disable and enable sections, and it does not work.

As far as I can see none of the initialization code has anything to do with whether interrupts are used or not.

So are modifications needed to the init code or is there and example of polled uart?
0 Kudos
16 Replies

922 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by R2D2 on Sun May 19 02:59:59 MST 2013

Quote: cwpjr
So should I assume if I want to utilize any example code in my projects that my project must be a CMSIS project?



If you are experienced enough to setup your system yourself (PLL, SYSAHBCLKCTRL...) you don't need it. But as a beginner and to use a somehow standard environment to share code it's very useful. Makes it much easier to find problems in this environment than checking own (new) setups.
0 Kudos

922 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by cwpjr on Sat May 18 11:22:19 MST 2013
Thanks again. I managed to get the uart working by dogged effort without full CMSIS, but I need to learn from your I2c and timer examples...

So should I assume if I want to utilize any example code in my projects that my project must be a CMSIS project?
0 Kudos

922 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by R2D2 on Fri May 17 20:50:33 MST 2013
Just use project wizard to create a new LPCXpresso project with CMSIS and copy your files (and not the CMSIS source and header files as now) in new src folder.
That's a save way to setup Includes, Libraries and Symbols.
0 Kudos

922 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by cwpjr on Fri May 17 20:18:59 MST 2013
I saw that IOCON was not turned on so I did it and now it is communicating but not at BAUDRATE.

So you blame this on not including CMSIS. Fine because I suspect there are other system setting that UARTINIT is dependent on. That why the baud setting is still broke. Simply saying in the uart example program that the UARTINIT routine depends on CMSIS would have been proper. Otherwise it is incomplete.

And when I included uart.h it surely meant I wasinterested in driving the uart. I suspected that the failure to link some labels that are defined in uart.h was externally controlled BS of  a #if CONFIG_ENABLE_DRIVER_UART==1                       controlled section. Good grief.

As for my project it was created with your wizards, but obviously with no wizard guidance as to whether my navigation thru the wizard would hamstring me or include or not this that or the other.

To use UARTINIT I need CMSIS, else I'd have to flail further.

Search of help using "add cmsis to existing project" returns "Nothing found.  "

Assuming it is possible, how would I safely add CMSIS to my project?

Or do I have to create a new (CMSIS) project?

Thanks, I am learning...
0 Kudos

922 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by R2D2 on Fri May 17 20:02:30 MST 2013
Your project is including:

Quote:

nm  lpcm0asm.axf > "E:\CodeRedLPCexpresso\FIG_M0 LPCXpresso_5.1.2_2065\workspace\lpcm0asm\src\FigM0_sym.txt";

That's not relative and causing errors.

You are not including CMSIS, so there's no SystemInit() done.
Without CMSIS you are not setting up the system yourself, just hoping that default settings are somehow right for your project.

You are excluding important defines in uart.h with

Quote:

#if CONFIG_ENABLE_DRIVER_UART==1

Result:
Missing SystemInit leads to a 12MHz IRC clock and reset IOCON bit in SYSAHBCLKCTRL.
Therefore your pins a not switched to RXD, TXD. They are still PIO and UART has no pin connection.
All this can easily be seen in your debugger via Peripherals View.

Enabling IOCON with:
  LPC_SYSCON->SYSAHBCLKCTRL |= (1<<16); //enable IOCON
is creating at least a baudrate of 12E6/16/6 = 125kHz.
0 Kudos

922 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by cwpjr on Fri May 17 17:05:17 MST 2013
This means that you are exporting projects with special settings (absolute instead relative to workspace)

CodeRedLPCexpresso is installed here:
E:CodeRedLPCexpresso/LPCXpresso_5.1.2_2065

My project is in:
E:CodeRedLPCexpresso/FIG_M0 LPCXpresso_5.1.2_2065

Can I migrate it to where it should be for support?
0 Kudos

922 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by R2D2 on Fri May 17 16:42:40 MST 2013

Quote: cwpjr
I run a 12 year old xp system with 768mb of ram. Drive c: fits xp and  windows installed programs only and barely has enough room to defrag.
Drive E: holds My Documents, the code red installation and all else.

Does this mean I can't get project help?



This means that you are exporting projects with special settings (absolute instead relative to workspace). Where your workspace is located is irrelevant then.
So whoever is willing to help you is now 'surprised' by your settings.
If you use Standard LPCXpresso projects and relative settings everyone can import this project without problems. That's the main idea behind keeping projects portable.
I personally refuse to work with projects I can't import within a minute. That's a waste of time (and that's my spare time).
0 Kudos

922 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by cwpjr on Fri May 17 15:27:17 MST 2013
I run a 12 year old xp system with 768mb of ram. Drive c: fits xp and  windows installed programs only and barely has enough room to defrag.
Drive E: holds My Documents, the code red installation and all else.

Does this mean I can't get project help?

Anyway I tried to uploaded the modified uart.c from the uart example program (which I modified only UARTInit) and  my project main.c which includes the UARTInit routine only. But those aren't valid attachment file extension.

so my projects main.c with modified UARTInit from the UART Example code, where this same code works in UART/Drivers/Uart.c but not when pasted into my main.c: (There must be some other initialization going on outside of the UARTInit routine in startup of the UART example code.)

#ifdef __USE_CMSIS
#include "LPC11xx.h"
#endif

#include <cr_section_macros.h>
#include <NXP/crp.h>
// Variable to store CRP value in. Will be placed automatically
// by the linker when "Enable Code Read Protect" selected.
// See crp.h header for more information
__CRP const unsigned int CRP_WORD = CRP_NO_CRP ;

// TODO: insert other include files here
#include <Stdint.h>

// TODO: insert other definitions and declarations here
#include "LPC11xx.h"
#include "uart.h"
#include "system_LPC11xx.h"
void UARTInit(uint32_t  baudrate)
{
  uint32_t Fdiv;
  uint32_t regVal;

//  UARTTxEmpty = 1;
//  UARTCount = 0;

//  NVIC_DisableIRQ(UART_IRQn);

  LPC_IOCON->PIO1_6 &= ~0x07;    /*  UART I/O config */
  LPC_IOCON->PIO1_6 |= 0x01;     /* UART RXD */
  LPC_IOCON->PIO1_7 &= ~0x07;
  LPC_IOCON->PIO1_7 |= 0x01;     /* UART TXD */
  /* Enable UART clock */
  LPC_SYSCON->SYSAHBCLKCTRL |= (1<<12);
  LPC_SYSCON->UARTCLKDIV = 0x1;     /* divided by 1 */

  LPC_UART->LCR = 0x83;             /* 8 bits, no Parity, 1 Stop bit */
  regVal = LPC_SYSCON->UARTCLKDIV;

//  Fdiv = (((SystemCoreClock*LPC_SYSCON->SYSAHBCLKDIV)/regVal)/16)/baudrate ;    /*baud rate */
  Fdiv = (((48000000*LPC_SYSCON->SYSAHBCLKDIV)/regVal)/16)/baudrate ;    /*baud rate */

  LPC_UART->DLM = Fdiv / 256;
  LPC_UART->DLL = Fdiv % 256;
  LPC_UART->LCR = 0x03;        /* DLAB = 0 */
  LPC_UART->FCR = 0x07;        /* Enable and reset TX and RX FIFO. */

  /* Read to clear the line status. */
  regVal = LPC_UART->LSR;

  /* Ensure a clean start, no data in either TX or RX FIFO. */
// CodeRed - added parentheses around comparison in operand of &
//>>LITERAL VALUES USED HERE BECAUSE\
//>>LSR_THRE, etc is in uart.h but somehow not seen<<<<<<<<
//  while (( LPC_UART->LSR & (LSR_THRE|LSR_TEMT)) != (LSR_THRE|LSR_TEMT) );
  while (( LPC_UART->LSR & (0x20|0x40)) != (0x20|0x40) );
//  while ( LPC_UART->LSR & LSR_RDR )
      while ( LPC_UART->LSR & 0x01 )
  {
    regVal = LPC_UART->RBR;    /* Dump data from RX FIFO */
  }

  /* Enable the UART Interrupt */
//  NVIC_EnableIRQ(UART_IRQn);

//#if CONFIG_UART_ENABLE_INTERRUPT==1
//#if CONFIG_UART_ENABLE_TX_INTERRUPT==1
//  LPC_UART->IER = IER_RBR | IER_THRE | IER_RLS;    /* Enable UART interrupt */
//#else
//  LPC_UART->IER = IER_RBR | IER_RLS;    /* Enable UART interrupt */
//#endif
//#endif
  LPC_UART->THR = 'A';
  regVal = LPC_UART->RBR;
  return;
}

int c_64by32div    ( int divisor, long long dividend )//int divmsw, int divlsb )
{
    int    rem = dividend % divisor;    // rem = divmsw+divlsb % divisor;
    int quo = dividend / divisor;    // quo = divmsw+divlsb / divisor;
    return (quo);    // quo in t/x (r0/r3), rem in w (r2)
}

long long c_32by32to64mul (int a, int b)
{
    long long res = a * b;
    return (res);    // LSW in x (r3), MSB in n (r1)
}

#define UART_BAUD 115200

int main(void) {

    // TODO: insert code here
    UARTInit(UART_BAUD);
    // Enter an infinite loop, just incrementing a counter
    volatile static int i = 0 ;
    while(1) {
        FIG_COLD();
        i++ ;
    }
    return 0 ;
}
0 Kudos

922 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by R2D2 on Thu May 16 19:48:51 MST 2013
This project isn't building here.


Quote:

Error: Program "make" not found in PATH
PATH=[E:\LPCXpresso_5.1.0_2048\lpcxpresso\bin;E:\LPCXpresso_5.1.0_2048\lpcxpresso\msys\bin;E:\LPCXpresso_5.1.0_2048\lpcxpresso\tools\bin;E:/LPCXpresso_5.1.0_2048/lpcxpresso/jre/bin/client;E:/LPCXpresso_5.1.0_2048/lpcxpresso/jre/bin;E:/LPCXpresso_5.1.0_2048/lpcxpresso/jre/lib/i386;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\QuickTime\QTSystem\;C:\SFU\common\;C:\Program Files\Common Files\GTK\2.0\bin;E:\LPCXpresso_5.1.0_2048\lpcxpresso;E:\LPCXpresso_5.1.0_2048\lpcxpresso\tools\arm-none-eabi\bin]

It's obviously no Standard project. I don't use LPCXpresso_5.1.0_2048 and my drive E: is my DVD :)
0 Kudos

922 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by cwpjr on Thu May 16 17:28:46 MST 2013
The init commented out the interrupt calls and the test at the end of the init routine writes an A and readsthe rx register. This same code worked in the uart example project.

Project exported selecting settings debug and src.
0 Kudos

922 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by cwpjr on Thu May 16 16:51:57 MST 2013
This works when there is a Declaration. Without the corresponding .h there is none"
- Select 'LPC_IOCON' with left double click
- Then use amazing 'Open Declaration' with F3

This is from the example uart program and I couldn't find the .h there.
I did the Open Declaration in the uart project and nothing happened.
How do I find the .h that declares these?

  LPC_IOCON->PIO1_6 &= ~0x07;    /*  UART I/O config */
  LPC_IOCON->PIO1_6 |= 0x01;     /* UART RXD */
  LPC_IOCON->PIO1_7 &= ~0x07;
  LPC_IOCON->PIO1_7 |= 0x01;     /* UART TXD */
  /* Enable UART clock */
  LPC_SYSCON->SYSAHBCLKCTRL |= (1<<12);
  LPC_SYSCON->UARTCLKDIV = 0x1;     /* divided by 1 */

  LPC_UART->LCR = 0x83;             /* 8 bits, no Parity, 1 Stop bit */
  regVal = LPC_SYSCON->UARTCLKDIV;

  Fdiv = (((SystemCoreClock*LPC_SYSCON->SYSAHBCLKDIV)/regVal)/16)/baudrate ;    /*baud rate */

  LPC_UART->DLM = Fdiv / 256;
  LPC_UART->DLL = Fdiv % 256;
  LPC_UART->LCR = 0x03;        /* DLAB = 0 */
  LPC_UART->FCR = 0x07;        /* Enable and reset TX and RX FIFO. */

  /* Read to clear the line status. */
  regVal = LPC_UART->LSR;

  /* Ensure a clean start, no data in either TX or RX FIFO. */
// CodeRed - added parentheses around comparison in operand of &
  while (( LPC_UART->LSR & (LSR_THRE|LSR_TEMT)) != (LSR_THRE|LSR_TEMT) );
  while ( LPC_UART->LSR & LSR_RDR )
  {
    regVal = LPC_UART->RBR;    /* Dump data from RX FIFO */
  }
0 Kudos

922 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by R2D2 on Thu May 16 16:31:56 MST 2013

Quote:
How would I find the .h file that defines LPC_IOCON?

- Select 'LPC_IOCON' with left double click
- Then use amazing 'Open Declaration' with F3


Quote:
I've attached a new zip with same name of the project I put the Uart init code in and it needs a .h file.

There's no valid project in there.
0 Kudos

922 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by cwpjr on Thu May 16 15:42:14 MST 2013
Making progress. How would I find the .h file that defines
LPC_IOCON
?
I've attached a new zip with same name of the project I put the Uart init code in and it needs a .h file.

If you could fix or specify how them I should be good to go because I got the polling to work before I copied it into this project.

Thanks!
0 Kudos

922 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by R2D2 on Thu May 16 14:50:40 MST 2013

Quote: cwpjr
As posted I commented out interrupts in /drivers/uart.c in the uart init function.



And now the problem is what?

Without interrupt no receiving via interrupt is possible. That's now your job.

I don't understand why you are doing that, but anyway. UM10398 Chapter 13.5.9 UART Line Status Register is describing RDR. That's a good point to start.
0 Kudos

922 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by cwpjr on Thu May 16 14:22:29 MST 2013
As posted I commented out interrupts in /drivers/uart.c in the uart init function.
0 Kudos

922 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by R2D2 on Thu May 16 09:20:56 MST 2013

Quote: cwpjr
I took the UARTInit function from the UART example program (LPC1114/301) and commented out the interrupt disable and enable sections, and it does not work.



:confused:

Could be useful if you post your code.
0 Kudos