Monday, 28 July 2014

A First program for the LPC810

Previously Flash Magic was used to talk to the ISP of a LPC810. Now we can look at how to program one. NXP provides the free eclipse based IDE LPCXpresso. With this you can compile up to 8k of executable without registering or up to 256k by registering and activating the free version.

You can get LPCXpresso from http://www.lpcware.com/lpcxpresso/download once you have installed it we can proceed and set up a project. Select File->New->Project and set the project up as below.
  • In the First wizard page select "LCPXpresso C Project"
  • In the second LPC8xx -> C Project
  • In the next give it a project Name
  • In the forth LPC8xx -> LPC810
  • In the next wizard page follow the instructions and install CMSIS. This only needs to be done once. Note that you only need the CMSIS_CORE_LPC8xx one. Now at this point there seems to be a bug and you have to exit the wizard after installing CMSIS and start over again until you get to this point in the wizard again in order to continue.
  • In the Sixth wizard page leave DSP Library as none
  • In the next uncheck enable Micro Trace Buffer
  • In the next uncheck enable CRP and check use INC folder

Finally we can click on finish.

Now we switch to the CMSIS project that's been included and build CMSIS for both debug and release.

Switch back to our project and set the active configuration to release.

There is one more this to do and that is to add the highlighted line below in Properties->C/C++ build->Settings->Build Steps->Post Build Steps

arm-none-eabi-size "${BuildArtifactFileName}"
arm-none-eabi-objcopy -O ihex ${BuildArtifactFileName} ${BuildArtifactFileBaseName}.hex
# arm-none-eabi-objcopy -v -O binary "${BuildArtifactFileName}""${BuildArtifactFileBaseName}.bin"
# checksum -p ${TargetChip} -d "${BuildArtifactFileBaseName}.bin"

which will generate a Hex file for Flash Magic when the program is built.

Now you should be able to run build and a Hex file should appear in your release directory. The current main.c does not do much at the moment it's just a infinite loop.

Since we have the UART already connected lets get some output , below is a minimal program to get you going and to connect to a terminal emulator:


#include "LPC8xx.h"

#define BAUDRATE 57600
#define UART_CFG_DATALEN_8      (0x01 << 2)  /* UART 8 bit length mode */
#define UART_CFG_PARITY_NONE    (0x00 << 4)  /* No parity */
#define UART_CFG_STOPLEN_1      (0x00 << 6)  /* UART One Stop Bit Select */
#define UART_STAT_RXRDY         (0x01 << 0)  /* Receiver ready */
#define UART_STAT_TXRDY         (0x01 << 2)  /* Transmitter ready for data */
#define UART_CFG_ENABLE         (0x01 << 0)

int uart0PollChar() {
 if (!(LPC_USART0->STAT & UART_STAT_RXRDY))
  return -1;
 return LPC_USART0->RXDATA;
}

void uart0SendString(char *buffer) {
 while (*buffer != 0) {
  while (!(LPC_USART0->STAT & UART_STAT_TXRDY));
  LPC_USART0->TXDATA = *buffer;
  buffer++;
 }
}

int main(void) {
 /* Calculate and set SystemCoreClock */
 SystemCoreClockUpdate();

 /* Enable AHB clock to the Switch Matrix and UART0 */
 LPC_SYSCON->SYSAHBCLKCTRL |= (1 << 7) | (1 << 14);

 /* Configure Switch Matrix so that Pin2 = U0_TXD and Pin3 = U0_RXD */
 LPC_SWM->PINASSIGN0 = 0xffff0004UL;

 /* Set the UART clock divisor to 1  */
 LPC_SYSCON->UARTCLKDIV = 1; // note on reset the value is 0. i.e clock is disabled

 /* Configure UART0 baud rate */
 LPC_USART0->BRG = (SystemCoreClock >> 4) / BAUDRATE - 1;
 LPC_SYSCON->UARTFRGMULT = (((SystemCoreClock >> 4) * (0x100)) / (BAUDRATE * (LPC_USART0->BRG + 1))) - (0x100);
 LPC_SYSCON->UARTFRGDIV = 0xFF; //note on reset the value is 0

 /* Set UART0 to 8N1 and finally enable */
 LPC_USART0->CFG = UART_CFG_DATALEN_8 | UART_CFG_PARITY_NONE | UART_CFG_STOPLEN_1 | UART_CFG_ENABLE;

    // Enter an infinite loop testing for the '0' and '1' keys
    while(1) {
     switch( uart0PollChar() ){
      case '0':
          uart0SendString("Hello\n\r");
      break;
      case '1':
          uart0SendString("World\n\r");
      break;
     }
    }
    return 0 ;
}


If you cut & paste the above in place of the existing main.c you should be able to get "hello" printed when you press the '0' key and "world" printed when you press the '1' key on a terminal emulator.

For a terminal emulator I use Realterm available from http://realterm.sourceforge.net/ remember to toggle the "open button" to recapture the Uart from flash magic after you have flashed an image else nothing will happen.




No comments:

Post a Comment