It is currently Sun May 19, 2013 6:18 pm

All times are UTC




Post new topic Reply to topic  [ 17 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: New board with Atmega2560
PostPosted: Sun Dec 12, 2010 3:45 pm 
Offline

Joined: Sun Dec 12, 2010 3:18 pm
Posts: 6
Hi all,
I would like to make a porting of BertOS on my board.
This Board is based on Atmega2560 with 128KB di SRAM.
At the moment this board is the main CPU in an embedded medical system.
In particular It manages an alphanumeric display, the embedded keyboard and all the system control signals.
How can we procede to do this?
Do you have any tutorial or guideline to follow for the introduction of the new board?

Thanks in advance


Top
 Profile  
 
 Post subject: Re: New board with Atmega2560
PostPosted: Mon Dec 13, 2010 10:11 am 
Offline
User avatar

Joined: Tue May 04, 2010 4:15 pm
Posts: 55
Hi jaws,
an in depth guide to porting BeRTOS on new architecture is missing, however have a look at the following thread on the ML which explains the first steps required to start a new port:
http://lists.develer.com/pipermail/bert ... 02073.html

Since the ATmega family is already supported, once you have an implementation of the above steps (ie. serial debug, timer) the kernel should work without further effort.
If you need more help with your patches, please post in the mailing list.

By the way, are you using external memory with your CPU? From what I've seen the ATmega2560 has only 8k of RAM; if you want to use the external memory, probably you will need to change the default linker scripts.

Cheers,
Luca


Top
 Profile  
 
 Post subject: Re: New board with Atmega2560
PostPosted: Wed Dec 15, 2010 8:26 am 
Offline

Joined: Sun Dec 12, 2010 3:18 pm
Posts: 6
Hi Luca,
thanks for your reply.
I setup a simple program including as you suggested only timer and serial driver.
I reported below the example.
I see the led flashing with the correct delay. so the timer seems to work.
I cannot see any output on the serial. What I see with the avrStudio debugging this code, it seems that kdginit is not exapndend in any code and the parameters passed debugging the printf as the fd struct not properly initialized.
I don't know if it is due to some optimization option on the compiler.
How can I change the compiler options?
Any suggestion to understand where is the problem?

thanks in advance
Jaws

Code:
// Empty main.c file generated by the wizard
#include <cpu/irq.h>
#include <avr/io.h>
#include <drv/timer.h>
#include <drv/ser.h>

static Serial out;

static void init(void)
{
   IRQ_ENABLE;
   // insert initialization calls here
   kdbg_init();
   timer_init();
   //Init usart0
   DDRJ |= BV(PJ4) + BV(PJ3)+ BV(PJ7);
   PORTJ |= BV(PJ3) + BV(PJ7);
   ser_init(&out, SER_UART0);
   ser_setbaudrate(&out, 38400);
}

int main(void)
{
   int count = 0;
   bool led = false;
   init();
   kfile_printf(&out.fd, "Starting example...\n\r");
   while (1)
   {
      timer_delay(100);
         kfile_printf(&out.fd, "Counter: %d\n\r", count++);
         if (led)
            PORTJ &= ~BV(PJ7);
         else
            PORTJ |= BV(PJ7);
         led = !led;      
   }

   return 0;
}



Top
 Profile  
 
 Post subject: Re: New board with Atmega2560
PostPosted: Wed Dec 15, 2010 10:24 am 
Offline
User avatar

Joined: Tue May 04, 2010 4:15 pm
Posts: 55
jaws wrote:
Hi Luca,
thanks for your reply.
I setup a simple program including as you suggested only timer and serial driver.
I reported below the example.
I see the led flashing with the correct delay. so the timer seems to work.
I cannot see any output on the serial. What I see with the avrStudio debugging this code, it seems that kdginit is not exapndend in any code and the parameters passed debugging the printf as the fd struct not properly initialized.


Hi jaws,
you can change compilation parameters by modifying your project's makefile, located in [project]/[project]_user.mk. Flags changed in that file will overwrite the defaults.

Bear in mind, though, that BeRTOS has 2 implementations of the serial port: one is used for debugging purposes only, it's slow and safe; the other can be used to transfer all types of data, it's fast and it exposes a kfile interface.

The former is the kdebug serial port, which is initialized with kdbg_init() and can be used with kprintf() or LOG_*() functions (see the logging tutorial for more information). The implementation can be found in bertos/cpu/avr/drv/kdebug_avr.h.

The latter is the proper UART port, which is initialized with ser_init() and accessed with the KFile interface. To implement the serial driver, you need to look at bertos/cpu/avr/drv/ser_avr.c.

Code:
// Empty main.c file generated by the wizard
#include <cpu/irq.h>
#include <avr/io.h>
#include <drv/timer.h>
#include <drv/ser.h>

static Serial out;

static void init(void)
{
   IRQ_ENABLE;
   // insert initialization calls here
   kdbg_init();
   timer_init();
   //Init usart0
   DDRJ |= BV(PJ4) + BV(PJ3)+ BV(PJ7);
   PORTJ |= BV(PJ3) + BV(PJ7);
   ser_init(&out, SER_UART0);
   ser_setbaudrate(&out, 38400);
}


Here you have initialized the same UART two times, one for kdebug and one for serial. By default, the debug port opens on serial port 0 and with 115200 bps, but you can change that using the wizard or by editing cfg/cfg_debug.h.

For now, I suggest you to remove the ser_* and kfile_* calls and try to see output using kprintf() only. kdbg_init() prints the string "*** BeRTOS debug starting ***" (or something similar), if you see that you're done; if not, double check the port number, the baud rate and the code. :)

As a side note, I suggest to encapsulate Led code into macros, so that it's easy to port the project to another board; see the HAL tutorial.

Cheers,
Luca


Top
 Profile  
 
 Post subject: Re: New board with Atmega2560
PostPosted: Mon Jan 03, 2011 11:28 pm 
Offline

Joined: Sun Dec 12, 2010 3:18 pm
Posts: 6
Thanks Luca,
I had the first example with timers and debug port up and running on my board.

The second step I have in mind, is to introduce some threads.
I have followed the example on your tutorial.
I have included debug, timer,kernel, signal, heap in the wizard.
I added the custom linker option to enable the external ram as used to have in my previous project:
Code:
# Flags included by the user.
gba_thread_USER_LDFLAGS = \
   -Wl,-Map,gba_thread.map,--cref -gdwarf-2 \
   -Wl,--section-start,.bss=0x800200 -Wl,--section-start,.data=0x802200 -Wl,--defsym=__bss_end=0x8021ff,--defsym=__heap_start=0x809000,--defsym=__heap_end=0x80ffff


The result is that the program run until the timer_delay instruction and then restart, without enter in the process functions.
Do you have any suggestions?
thanks
J.

Code:
// Empty main.c file generated by the wizard
#include <cfg/macros.h>
#include <kern/irq.h>
#include <kern/proc.h>

#include <hw/hw_led.h>
#include <avr/io.h>
#include <drv/timer.h>

#include <cfg/cfg_debug.h>
#include <cfg/debug.h>

static PROC_DEFINE_STACK(stack1, KERN_MINSTACKSIZE*2);
static PROC_DEFINE_STACK(stack2, KERN_MINSTACKSIZE*2);       

static Process *p1;
static Process *p2;


static void process1(void)
{
    int times = 0;
    bool light_on = false;
    while (true)
    {
        light_on = !light_on;
        // light on or off led1
        if (!light_on) TURN_LED_ON(1);
        else TURN_LED_OFF(1);

        ++times;
        timer_delay(200);
        if (times > 30)
            return;
    }
}

// proc2 is similar to proc1
static void proc2(void)
{
    int times = 0;
    bool light_on = false;
    while (true)
    {
        light_on = !light_on;
        // light on or off led2
        if (!light_on) TURN_LED_ON(2);
        else TURN_LED_OFF(2);

        ++times;
        timer_delay(400);
        if (times > 30)
            return;
    }
}


static void usart_init(void)
{   
    //Init usart0
    DDRJ |= BV(PJ4) + BV(PJ3);
    PORTJ |= BV(PJ3);
}

static void led_init(void)
{   
    LED_INIT();
}

int main(void)
{
    IRQ_ENABLE;
    kdbg_init();
    usart_init();

    kputs("Init...");
    led_init();
    timer_init();
    proc_init();
    kputs("Done.\n");
   
   
     // other initializations...
    // ...
    p1 = proc_new(process1, NULL, sizeof(stack1), stack1);
    proc_setPri(p1, -5);

    p2 = proc_new(proc2, NULL, sizeof(stack2), stack2);
    proc_setPri(p2, 10);

    // now wait for both processes to complete
    // note that "main" is itself a process
    ticks_t start = timer_clock();
    while (timer_clock() - start < ms_to_ticks(3000))
    {
        kputs("main\n");
        timer_delay(500);
    }
    return 0;
}


Top
 Profile  
 
 Post subject: Re: New board with Atmega2560
PostPosted: Tue Jan 04, 2011 1:34 pm 
Offline
User avatar

Joined: Tue May 04, 2010 4:15 pm
Posts: 55
Hi jaws,
I feel like that there are too many things that can go wrong with this complex example. I suggest that you strip down the example to a bare minimum and add features to that.

If the timer is working without any change to the linker script, then I'd try to change the linker script, activate the timer and blink a led only. If that works, activate the kernel and test it with only one process. This way you should be able to isolate the problem and fix it.

jaws wrote:

Code:

static void usart_init(void)
{   
    //Init usart0
    DDRJ |= BV(PJ4) + BV(PJ3);
    PORTJ |= BV(PJ3);
}

If you want to use a serial port, you should use the Serial datatype, which can be initialized with ser_init().

Also, you may want to have a look at the contributed ATmega2560 port which you can find in the automatic weekly snapshot package.

Cheers,
Luca


Top
 Profile  
 
 Post subject: Re: New board with Atmega2560
PostPosted: Tue Jan 04, 2011 1:55 pm 
Offline

Joined: Sun Dec 12, 2010 3:18 pm
Posts: 6
hi Luca, thanks fro your reply.

I did a lot of tests before writing on the forum.

Starting form the example that use the debug messages on serial interface and timer I added one thread. With the same result. I added next the linker options, and no changes on the behavior.

I'll try the snapshot with the atmega2560 integrated on it.

If I well understood, If I use only kputs and kprintf I don't need to add serial datatype but only debug and kdbg_init(). Is it correct?

Thanks
J


Top
 Profile  
 
 Post subject: Re: New board with Atmega2560
PostPosted: Tue Jan 04, 2011 6:46 pm 
Offline
User avatar

Joined: Tue May 04, 2010 4:15 pm
Posts: 55
jaws wrote:
hi Luca, thanks fro your reply.

I did a lot of tests before writing on the forum.

Starting form the example that use the debug messages on serial interface and timer I added one thread. With the same result. I added next the linker options, and no changes on the behavior.


Hi,
IIUC, you have the timer, debug and kernel working without using the external RAM. Am I correct?
Assuming they are working with internal RAM (they should with minimum fuss), I'd try to enable external RAM and see if it works with a bare minimum program:
Code:
void main(void)
{
  kdbg_init();
  while (1)
  {
    kputs("Hello world\n");
    for (int i = 0; i < 65535; ++i)
      ;
  }
}

If this code runs well, you could try to print a variable and then activate the timer.

Also note that in your example, the main() function returns after a while and at that time the micro will reset. You should enclose your code in main() inside a while(1) as the above example.

jaws wrote:
If I well understood, If I use only kputs and kprintf I don't need to add serial datatype but only debug and kdbg_init(). Is it correct?


Yes, you're correct. kprintf() and kputs() only need kdbg_init().

Cheers,
Luca


Top
 Profile  
 
 Post subject: Re: New board with Atmega2560
PostPosted: Tue Jan 11, 2011 10:56 pm 
Offline

Joined: Sun Dec 12, 2010 3:18 pm
Posts: 6
lucaotta wrote:
Also, you may want to have a look at the contributed ATmega2560 port which you can find in the automatic weekly snapshot package.


Hi Luca,
I didn't find anything related to atmega2560 on the last snapshot. Do you confirm that the new contribution is there?
Could you please point me out where I can find the support for this micro?

An update of my board:
I tested, debug and timer also with external memory (undef _PROC_) : everithing works!
Introducing a thread on the code (define _PROC_) I see a continuous restart printing on the serial port: Hello wolrd continuosly.
Any suggestions?
Thanks
J

Code:
#include <cfg/macros.h>
#include <cfg/cfg_debug.h>
#include <cfg/cfg_heap.h>
#include <cfg/cfg_proc.h>
#include <cfg/debug.h>
#include <cpu/irq.h>
#include <cfg/cfg_timer.h>
#include <avr/io.h>
#include <drv/timer.h>
#include <kern/proc.h>

//#define _PROC_

#ifdef _PROC_
static PROC_DEFINE_STACK(stack1, KERN_MINSTACKSIZE);
static Process *p1;

static void process1(void)
{
   int times = 0;
   bool light_on = false;
   while (true)
   {
      light_on = !light_on;
      
      if (!light_on) kputs("TURN_LED_ON(1)");
      else kputs("TURN_LED_OFF(1)");

      ++times;
      timer_delay(200);
      if (times > 30)
         return;
   }
}
#endif
/*******************************************************************
* Enable External Memory
*
* abilita l'accesso alla memoria esterna
*
* WARNING!! se implementata con __init() chiamata da .init0, tenere
* presente che viene chiamata con CALL mentre lo stack non e' ancora
* stato inizializzato (avverra' in .init2).
*
* :init1 e .init3 sono intrambi adatti a questo scopo
*******************************************************************/

void enable_ext_mem (void) __attribute__ ((naked)) \
    __attribute__ ((section (".init3")));

void enable_ext_mem(void) {

    // EXTERNAL MEMORY
    // 0x0000..0x10FF   internal memory
    // 0x1100..0x7FFF   lower sector - 0 wait state - memory mapped I/O
    // 0x8000..0xFFFF   upper sector - 1 wait state 32Kb external RAM
    XMCRA = (1 << SRE)   |  // SRE: External SRAM/XMEM Enable (enabled)
            (0 << SRL2)  |  // SRL2:0:  Wait-state Sector Limit
            (0 << SRL1)  |  //          Lower sector = N/A
            (0 << SRL0)  |  //          Upper sector = 0x2200 - 0xFFFF
            (0 << SRW11) |  // SRW1x:   Wait-state Select Bits for Upper Sector
            (0 << SRW10) |  //          No wait-states
            (0 << SRW01) |  // SRW0x:   Wait-state Select Bits for Lower Sector
            (0 << SRW00);   //          No wait-states

    XMCRB = (0 << XMBK)  |  // XMBK:    External Memory Bus-keeper (no bus-keeper)
            (0 << 6)     |  // Bit 6:3: Reserved Bits
            (0 << 5)     |  //
            (0 << 4)     |  //
            (0 << 3)     |  //
            (0 << XMM2)  |  // XMM2:0:  External Memory High Mask
            (0 << XMM1)  |  //          Full 56KB space
            (0 << XMM0);    //

    // pin di abilitazione della RAM esterna
    // J5 = RAM_A16, J6 = RAM_CS
    DDRJ  |=   _BV(5) | _BV(6);
    PORTJ &=  ~_BV(5);
    PORTJ |=   _BV(6);
}

static void usart_init(void)
{
   //Init usart0
   DDRJ |= BV(PJ4) + BV(PJ3);
   PORTJ |= BV(PJ3);
}

static void init(void)
{
   IRQ_ENABLE;
   // insert initialization calls here

   usart_init();
   kdbg_init();
   timer_init();

}

int main(void)
{
   init();

#ifdef _PROC_
   proc_init();
   p1 = proc_new(process1, NULL, sizeof(stack1), stack1);
   proc_setPri(p1, -5);
#endif

     while (1)
     {
       kputs("Hello world\n");
       timer_delay(500);
     }

   return 0;
}


Top
 Profile  
 
 Post subject: Re: New board with Atmega2560
PostPosted: Wed Jan 12, 2011 12:17 pm 
Offline
User avatar

Joined: Tue May 04, 2010 4:15 pm
Posts: 55
jaws wrote:
lucaotta wrote:
Also, you may want to have a look at the contributed ATmega2560 port which you can find in the automatic weekly snapshot package.


Hi Luca,
I didn't find anything related to atmega2560 on the last snapshot. Do you confirm that the new contribution is there?
Could you please point me out where I can find the support for this micro?


Indeed I forgot to commit wizard support for the CPU...
I've added support only for the CPU right now, example projects for boards will come later on.
If you want to have a look, I guess you will need to download the SVN, since weekly packages are created every week on Tuesday.

Cheers,
Luca


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 17 posts ]  Go to page 1, 2  Next

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group