0
votes

I'm currently developing a contiki programm that runs on a Z1 device for debugging purposes. I would like to send UART messages to that device and found out that Cooja appears to supports that with a function called "Show serial port on Z1". That tool in fact helped me to read UART messages sent from the mote, but when I try to send somethin back, the Int handler just doesn't get called. Here's how I initialize UART in code:

uart0_init(0);
uart0_set_input(uart_handler);
uart0_writeb('U');

In the handler itself, I just toggle all LEDs (which definitely works) - however, they never get toggled. I even tried to send a byte in the ISR handler within the uart0-library but even that byte never gets sent. This means that the whole UART communication between Cooja and the Z1 mote does not seem to work correctly. Has anyone ever had the same problem and is able to provide me with a solution for that? It would be a great help!

1

1 Answers

1
votes

I did manage to figure it out in the mean time. Here's the solution for anybody who is facing the same problem. The following code works. Depending on whether you define USE_SERIAL_LINE you can decide if you would like to use the serial-line-module or your own int handler.

#include <stdio.h>
#include <string.h>
#include "contiki.h"
#include "contiki-net.h"
#include "dev/uart0.h"


#define USE_SERIAL_LINE

#ifdef USE_SERIAL_LINE
#include "dev/serial-line.h"
#endif


PROCESS(init_system_proc, "Init system process");
AUTOSTART_PROCESSES(&init_system_proc);

int uart_handler(unsigned char c){
    printf("Single Byte input.\n");
    return 1;
}

PROCESS_THREAD(init_system_proc, ev, data){
        PROCESS_BEGIN();
        uart0_init(0);
#ifdef USE_SERIAL_LINE
        serial_line_init();
        uart0_set_input(serial_line_input_byte);
#else
        uart0_set_input(uart_handler);
#endif

        while (1) {
            PROCESS_YIELD();
#ifdef USE_SERIAL_LINE
        if(ev == serial_line_event_message)
            printf("Serial input.\n");
#endif
    }
    PROCESS_END();
}

There's one thing you need to keep in mind when using printf for UART output. A UART message will only be send when "\n" appears - this means that every printf-statement should end with a new line character. Otherwise your string only gets buffered and send out once a new line character appears.