Menu

AVR Studio 5.1 + Arduino Libraries

Help
2012-05-03
2012-12-09
  • Brandon Bearden

    Brandon Bearden - 2012-05-03

    Things I have accomplished with my Mega 2560 and AVR Studio 5.1

    1) Ability to build and run Femto OS projects in AVR Studio 5.1 after importing them and customizing the toolchain
    2) Ability to build and run Femto OS projects as a C++ project after importing the files and customizing the toolchain
    3) Ability to build and run complex Arduino projects with full 1.0 libraries in a C++ project
    4) Ability to build and run a C++ project that has the Femto OS libraries and main with Arudino init(); and Arduino library code in appBoot() {}

    The problem occurs when I try to run code within the hello world appLoop_Display function. I have been able to blink an LED with Arduino Library code within this function however, anything more complex fails, such as Serial.begin(9600); Serial.println("here");

    What should I do in order to be able to run more complex code within that function?

    By the way, you have an amazing and impressive project here!

    Brandon

     
  • Brandon Bearden

    Brandon Bearden - 2012-05-04

    Oh yeah, even a delay() fails within that function. If I use your delay function it does work though. One thing I was thinking is to redefine delay as your delay function.

     
  • Brandon Bearden

    Brandon Bearden - 2012-05-04

    So, I am not a quick learner it appears. I am able to get serial working now in the appLoop_Display.

    I am now going to try to get the Ethernet working. I will post code here when I am successful, or unsuccessful. (Don't want to jinx myself!)

     
  • Brandon Bearden

    Brandon Bearden - 2012-05-04

    I love it! Again, what a wonderful project. Most of your code is well above me and I am looking forward to digging into this thing.
    Here is my working code. I know many people may frown upon using the Arduino libs with this OS, however, I think there is a large benefit to having that ability.

    #include "Arduino.h"
    #include "SPI.h"
    #include "Ethernet.h"
    #include "EthernetUdp.h"
    extern "C" {
        #include "femtoos_code.h"
    }   
    //ETHERNET CARD/SERVER SETTINGS 
    byte mac[] = { 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0x11 };
    IPAddress server(30,20,10,1);
    int port = 7070;
    unsigned int localPort = 8888;
    EthernetUDP Udp;
    //*/
    void appBoot(void)
    {   
        /*
        // Serial Send Test
        Serial.begin(9600);
        delay(1000); 
        Serial.println("Serial test works!");
        delay(3000); 
        //*/
    
        // UDP Packet Send Test VIA Arduino Ethernet Library
        pinMode(4, OUTPUT);
        digitalWrite(4, HIGH);
        pinMode(53, OUTPUT);
        digitalWrite(53, LOW);
            Ethernet.begin(mac);
        delay(10000);
        Udp.begin(localPort);
        Udp.beginPacket(server, port);
        Udp.write("f|9|7|7|7|7|c|c|c");
        Udp.endPacket(); 
        //*/
    }
    #if (preTaskDefined(Display))
    void appLoop_Display(void)
    { 
      while (true)
      {
        /*
        // Femto OS LED Test
        devLedPORT ^= 0xFF;
        taskDelayFromNow(5000);
        //*/
        /*
        // Arduino Code Blink LED Test
        pinMode(12, OUTPUT);     
            digitalWrite(12, HIGH);
            taskDelayFromNow(1000);
            digitalWrite(12, LOW);
            taskDelayFromNow(1000); 
        //*/
    
        /*
        // Serial Test
        Serial.println("Serial test works!");
        taskDelayFromNow(1000); 
        //*/
    
        // UDP Packet Send Test VIA Arduino Ethernet Library
        Udp.begin(localPort);
            Udp.beginPacket(server, port);
        Udp.write("f|9|7|7|7|7|c|c|c");
        Udp.endPacket();
        taskDelayFromNow(2000);
        //*/
      } 
    }
    #endif
    //Excluded Task
    #if (preTaskDefined(Speed))
    void appLoop_Speed(void)
    {
        while(true){
            // Arduino Code Blink LED Test
            pinMode(12, OUTPUT);     
            digitalWrite(12, HIGH);
            taskDelayFromNow(2000);
            digitalWrite(12, LOW);
            taskDelayFromNow(2000); 
            //*/
        }   
    }
    #endif
    

    There it is… a simple blink and UDP transmission.

     
  • De Vlaam

    De Vlaam - 2012-05-04

    I think this is a very nice achievement. You not a quick learner? Noticing the time between the posts, i think you made a gigantic progress in a very short time!!. Maybe it is also good if you post why i did not work in the first place, i.e. the answer to the question you raised in the first post. Was it the amount of stack memory or something else?

    Further, using a "delay function" in an OS does not make much sense (there are special cases where it can be beneficial, but usually not). Rather use the delay functions of the OS itself.

    It is amazing that you succeeded so fast and thank you for posting working code! That can be the starting point for others.
    Did you also try on other OS for this application?

     
  • Brandon Bearden

    Brandon Bearden - 2012-05-04

    Yeah, I was thinking about that last night in bed, (the fact that I didn't post a solution to my original question), … a true geek.

    So, a couple things:
    First, setting up the AVR Studio CPP project is a bit of chore if you don't know what settings need to be set to what. That is a task best described in a tutorial which I plan to write and will post a link here.

    Second, the main reason I was having issues was, as you suggest, the stack size for the OS and each task. Once I set the correct sizes for the OS and the two tasks, things began to work as expected.

    Third, I did not specify that in order to get the Arduino Libraries working I do call init(); in main within the core Femto OS file. I tried calling it within appBoot, however, I had unpredictable results when doing this. I think it best to call that function in main before anything else is done. Again, I will complete a full tutorial on my steps to accomplish my steps to make the above code work.

    No, I did not try another OS. I looked at many different ones though and felt this was the best solution.

    WRT delay… What code should I be using to replace the delay function in the Arduino Libraries? For example, some temperature sensors I use need delays between certain calls in order to function properly. Or, the LCD is another good example, you have to delay between certain commands so that you don't lock the LCD driver.

    Brandon

     
  • De Vlaam

    De Vlaam - 2012-05-05

    WRT delay... What code should I be using to replace the delay function in the Arduino Libraries?

    Having no experience with Arduino in particular, in general there are three options.

    (1) You only need s short delay, or less of equal to the current tick time (typically 1 ms), simply call
    taskYield()
    The delay of course depends on the number of tasks that run in the same priority. If there are none, the call will quickly return, otherwise other tasks come first in a round robin fashion.

    (2) Explicitly call a delay for a fixed number of ticks.

    taskDelayFromNow(10)
    

    delays at least for 10 ticks, and then returns.

    (3) If you need a very precise delay, you can disable the interrupts and call the 'standard' delay. Make sure however this is very short, for it may hinder other tasks. You typically use this to get delays shorter than one tick.

    taskDisableGlobalInterrupts()
    delay(200);  // in microseconds?
    taskEnableGlobalInterrupts()
    

    btw i do not now exactly how the Arduino delay() works. If it is based on a timer, this may cause havoc on the OS itself, if it is just an (assembler coded) long loop, this will work just fine.

     
  • Brandon Bearden

    Brandon Bearden - 2012-05-05

    Okay. I need to some more research on this topic and I will report back my findings. Thank you for your reply.

     

Log in to post a comment.