1. Read TinyOS 2.x TEP 113 Serial Communication Specification

2. Read Nios II UART Core Specification

3. Write UART application on NIOS II IDE to test basci receive, send

#include <stdio.h>
#include <string.h>

int main()
{
  char* msg = "Detected the character ‘t’.\n";
  FILE * fp;
  char prompt = 0;
  printf("Hello from Nios II!\n");
  fp = fopen("/dev/uart_0", "r+");
  if(fp)
  {
    fwrite(msg, strlen(msg), 1, fp);
   while(prompt != ‘v’)
   //while(1)
    {
       prompt = getc(fp);
       if(prompt == ‘t’)
        {
            fwrite(msg, strlen(msg), 1, fp);
        }
        printf("%c",prompt);
    }
    fprintf(fp, "Closing the UART file.\n");
    fclose(fp);
  }
  printf("Hello from Nios II1!\n");

  return 0;
}

Assuming the "UART open." message can be seen so the program should be running correctly, there could be a few causes for the error:
1. Make sure the name "dev/uart" matches the UART name in SOPC Builder, i.e., "uart" is the name in SOPC Builder.
2. Connect to the correct UART port. Usually a computer has COM1 and COM2, so make sure the right one is chosen in HyperTerminal. (If you are using the PC for testing)
3. Make sure the UART setting in SOPC Builder matches the settings in HyperTerminal, such as Parity, Data Bits, Stop Bit, Flow Control, etc.
Please let me know if the above doesn’t help and provide more information, such as the program output, sopc ptf file, etc.

4. Write UART application on NIOS II IDE to test interrupt

Based on Nios II UART research, we need to implement UART driver by ourselves, for we could not define our own interrupt handler by Nios II HAL API.

5 Write another UART driver for Nios II to test

Just drag all source code of original UART driver, and register the driver in our application, and let interrupt handler print something to prove our own driver could be invoked, following is the source code of our application.

#include <stdio.h>
#include <string.h>

#include <fcntl.h>

#include "sys/alt_dev.h"
#include "sys/alt_irq.h"
#include "sys/ioctl.h"
#include "sys/alt_errno.h"

#include "altera_avalon_uart.h"
#include "altera_avalon_uart_regs.h"

altera_avalon_uart_state state =                     \
   {                                                   \
     (void*) UART_0_BASE,                              \
     0,                                                \
     0,                                                \
     0,                                                \
     0,                                                \
     0,                                                \
     ALTERA_AVALON_UART_TERMIOS(UART_0_STOP_BITS,      \
                               (UART_0_PARITY == ‘N’), \
                               (UART_0_PARITY == ‘O’), \
                               UART_0_DATA_BITS,       \
                               UART_0_USE_CTS_RTS,     \
                               UART_0_BAUD)            \
     ALTERA_AVALON_UART_FREQ(name)                     \
     (UART_0_FIXED_BAUD ? ALT_AVALON_UART_FB : 0) |    \
       (UART_0_USE_CTS_RTS ? ALT_AVALON_UART_FC : 0)   \
   };
int main()
{
    altera_avalon_uart_init(&state, UART_0_IRQ);
    while(1)
    {}
}

After input some letters in Hyperterminal, which connect UART0, then interrupt is invoked!

6. Architecure of UART on Mica platform

Component: TestSerialAppC

configuration TestSerialAppC

Wiring

Ported one:

Component: tos.lib.serial.SerialActiveMessageC

configuration SerialActiveMessageC

Author:

Phil Buonadonna

Lewis Girod

Ben Greenstein

Philip Levis

Date:

August 7 2005

Provides

interface AMPacket

interface AMSend[am_id_t]

interface Packet

interface PacketAcknowledgements

interface Receive[am_id_t]

interface SplitControl

Uses

interface Leds

Wiring

Ported one:

 

Component: tos.lib.serial.SerialDispatcherC

configuration SerialDispatcherC

This component provides functionality to send many different kinds of serial packets on top of a general packet sending component. It achieves this by knowing where the different packets in a message_t exist through the SerialPacketInfo interface.

Author:

Philip Levis

Ben Greenstein

Date:

August 7 2005

Provides

interface Init

interface Receive[uart_id_t]

interface Send[uart_id_t]

interface SplitControl

Uses

interface Leds

interface SerialPacketInfo[uart_id_t]

Wiring

Ported one:

 

Component: tos.platforms.mica.PlatformSerialC

configuration PlatformSerialC

Author:

Alec Woo <awoo@archrock.com>

Jonathan Hui <jhui@archrock.com>

Version:

$Revision: 1.1.2.8 $ $Date: 2006/10/30 18:40:45 $

Provides

interface StdControl

interface UartByte

interface UartStream

Wiring

Ported one:

Component: tos.chips.atm128.Atm128Uart0C

configuration Atm128Uart0C

Author:

Alec Woo <awoo@archrock.com>

Jonathan Hui <jhui@archrock.com>

Version:

$Revision: 1.1.2.3 $ $Date: 2006/10/30 18:40:45 $

Provides

interface StdControl

interface UartByte

interface UartStream

Uses

interface Counter<TMicro, uint32_t>

Wiring

Component: tos.chips.atm128.HplAtm128UartC

configuration HplAtm128UartC

HPL for the Atmega 128 serial ports.

Author:

Martin Turon <mturon@xbow.com>

David Gay

Provides

interface HplAtm128Uart as HplUart0

interface HplAtm128Uart as HplUart1

interface StdControl as Uart0RxControl

interface StdControl as Uart0TxControl

interface StdControl as Uart1RxControl

interface StdControl as Uart1TxControl

Wiring

Component: tos.platforms.mica.PlatformC

configuration PlatformC

Initialisation component for mica-family platforms. See TEP107. Each mica-family member must also provide a MotePlatformC with initialisation specific to that member.

Author:

Martin Turon <mturon@xbow.com>

Provides

interface Atm128Calibrate

interface Init

Uses

interface Init as SubInit

Wiring

Component: tos.platforms.mica2.MotePlatformC

configuration MotePlatformC

The portion of a mica-family initialisation that is mote-specific.

Author:

David Gay

Provides

interface Init as PlatformInit

Uses

interface Init as SubInit

Wiring

Component: tos.platforms.mica2.chips.cc1000.HplCC1000InitP

configuration HplCC1000InitP

Hardware initialisation for the CC1000 radio. This component is always included even if the radio is not used.

Author:

David Gay

Provides

interface Init as PlatformInit

Wiring

7. Porting UART for TinyOS on Nios II based on Mica2 Platform

7.1 Define PLATFORM_BAUDRATE in plarform/toni/hardware.h like the followings

enum {
  PLATFORM_BAUDRATE = 115200L
};

7.2 Write UART Driver for TinyOS on Nios II

For the time being, I just fix all names, later on, if time available, I could change the name mechanism to let it better integrate into Nios II.

8. Debug UART

8.1 Write UART Test Application for TinyOS based on Apps/tests/testSerial

8.2 First Error:

In file included from /opt/tinyos-2.x/tos/interfaces/Receive.nc:43:
/opt/tinyos-2.x/tos/types/message.h:4:30: platform_message.h: No such file or directory

Directly copy platform_message.h from null platform to toni platform under tos/platform/toni directory.

Solved!

8.3 Second Error:

In file included from /opt/tinyos-2.x/tos/lib/serial/SerialP.nc:58,
                 from /opt/tinyos-2.x/tos/lib/serial/SerialDispatcherC.nc:49,
                 from /opt/tinyos-2.x/tos/lib/serial/SerialActiveMessageC.nc:45,
                 from TestSerialAppC.nc:55:
/opt/tinyos-2.x/tos/chips/nios2/crc.h:39: syntax error before `PROGMEM’
/opt/tinyos-2.x/tos/chips/nios2/crc.h: In function `crcByte’:
/opt/tinyos-2.x/tos/chips/nios2/crc.h:84: `crcTable’ undeclared (first use in this function)
/opt/tinyos-2.x/tos/chips/nios2/crc.h:84: (Each undeclared identifier is reported only once
/opt/tinyos-2.x/tos/chips/nios2/crc.h:84: for each function it appears in.)

This one would not be useful, delete directly first!  

8.4 Third Error:

In file included from /opt/tinyos-2.x/tos/lib/serial/SerialActiveMessageC.nc:45,
                 from TestSerialAppC.nc:55:
In component `SerialDispatcherC’:
/opt/tinyos-2.x/tos/lib/serial/SerialDispatcherC.nc:51: component PlatformSerialC not found

Forgot to port PlatformSerialC, define it like the followings under platform/toni directory in PlatformSerialC.nc file:

configuration PlatformSerialC {
  provides interface StdControl;
  provides interface UartStream;
  provides interface UartByte;
}
implementation {

  components Nios2Uart0C as Uart0;
  StdControl = Uart0;
  UartStream = Uart0;
  UartByte = Uart0;
  components CounterMicro32C;
  Uart0.Counter -> CounterMicro32C;
}

Solved!

8.5 Fourth Error:

In file included from /opt/tinyos-2.x/tos/chips/nios2/HplNios2UartC.nc:49,
                 from /opt/tinyos-2.x/tos/chips/nios2/Nios2Uart0C.nc:55,
                 from ../../tos/platforms/toni/PlatformSerialC.nc:47,
                 from /opt/tinyos-2.x/tos/lib/serial/SerialDispatcherC.nc:51,
                 from /opt/tinyos-2.x/tos/lib/serial/SerialActiveMessageC.nc:45,
                 from TestSerialAppC.nc:55:
/opt/tinyos-2.x/tos/chips/nios2/HplNios2UartP.nc: In function `Uart0Init.init’:
/opt/tinyos-2.x/tos/chips/nios2/HplNios2UartP.nc:119: `ALTERA_AVALON_UART_CONTROL_TMT_MSK’ undeclare
d (first use in this function)

Include #include "altera_avalon_uart_regs.h" in Nios2Uart.h. Solved!

8.6 Fifth Error:

In file included from /opt/tinyos-2.x/tos/chips/nios2/HplNios2UartC.nc:49,
                 from /opt/tinyos-2.x/tos/chips/nios2/Nios2Uart0C.nc:55,
                 from ../../tos/platforms/toni/PlatformSerialC.nc:47,
                 from /opt/tinyos-2.x/tos/lib/serial/SerialDispatcherC.nc:51,
                 from /opt/tinyos-2.x/tos/lib/serial/SerialActiveMessageC.nc:45,
                 from TestSerialAppC.nc:55:
/opt/tinyos-2.x/tos/chips/nios2/HplNios2UartP.nc: In function `Uart0Init.init’:
/opt/tinyos-2.x/tos/chips/nios2/HplNios2UartP.nc:126: implicit declaration of function `alt_irq_register’

Add

#include "sys/alt_dev.h"
#include "sys/alt_irq.h"
#include "sys/ioctl.h"
#include "sys/alt_errno.h"

#include "altera_avalon_uart_regs.h"

in Nios2Uart.h.  Solved!

8.7 Sixth Error:

In component `HplNios2UartC’:
/opt/tinyos-2.x/tos/chips/nios2/HplNios2UartC.nc:59: no match
../../tos/platforms/toni/CounterMicro32C.nc:20:23: MicaTimer.h: No such file or directory

After checking the source code, it turns the problem is we need to implement another timer to provide counter function to UART.

So implement another timer first!

don’t use another timer, try to use original Milli timer.

Build succesfully already, but can not see anything…continue to debug…

The problem of the last error is the test application problem, now UART could work!

Next Step. Sense node communication?

Advertisements