Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 8 of 8

Thread: Program correctly works only after upload or reboot

  1. #1
    Junior Member
    Join Date
    Mar 2020
    Location
    Italy
    Posts
    17

    Program correctly works only after upload or reboot

    Hi, i'm working on Teensy3.2. I'm newbie.

    Problem: it doesnt correctly work when I connect, for first time, my teensy to USB.
    But it correctly works only if I upload the program.


    In particular I've 2 encoder, 2 MCP23S17 via SPI, 1 OLED via I2C. Some Led connected to MCP.

    Steps test are:
    - I connect my teensy via USB to my Laptop just to power ON.
    - I can see that OLED works fine, 2 encoder works fine (I see it via Serial monitor)
    - BUT seems that MCP are not working.

    - I open IDE
    - upload same code or use Teensy loader to trig a reboot.
    - EVERYTHING Works FINE! :-(

    What's the difference?

    Moreover: I saw that if i connect the teensy to my laptop, without IDE open or teensy loader, the push button on the board doenst reset program.

    Is this correct?


    thank you

    Andrea

  2. #2
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    22,478
    First, the easy question. The button on Teensy is NOT for resetting your program. Pressing the button puts Teensy into programming mode.

    If you have Teensy Loader running on your PC, and if it's set in Auto mode (to automatically reprogram and reboot your Teensy) which is the default if you have compiled code with Arduino, then pressing the button has the effect of rebooting your Teensy. But it not just rebooting. If you keep the Teensy Loader window visible on your screen, you'll see it is actually reprogramming the code onto your Teensy every time you press the button.

    When you press the button without your PC, or even with your PC but without Teensy Loader in Auto mode, it sits there and waits for USB to connect and send new code.

  3. #3
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    22,478
    Now, the harder question... why your program isn't working. I hope you can understand you're asking everyone to blindly guess, since you haven't shown the program, nor how you've connected these I2C devices.

    I can tell you about 2 common issues, which may or may not be related to your issue.

    #1: Teensy boots up quickly, usually before your PC has completely detecting the USB. So if you do Serial.print() before the PC is listening, that data is lost.

    #2: The solution for #1 is usually to add something like "while (!Serial) ; // wait" in the beginning of setup(). This waits for the Arduino serial monitor to be opened. That way, you can be sure everything you print will show up. But that causes another issue, where your program waits forever if the PC isn't connected.

    Something else to consider (again, blind guessing) is that your I2C devices don't reset when Teensy reboots, unless you've done something specific to cause them to reset. So those chips may be left in some state from a previous run. We used to see this sort of problem with I2S motion sensors, which take about 1/10th of a second to setup up. In modern times we have a 300 ms delay in Teensy's startup code, which allows time for that sort of hardware to initialize. Before we had that, many people reported Teensy didn't retain their program. Of course it did, but their programs weren't checking whether those I2C chips had started up. All other Arduino boards have lengthy startup delays, so libraries for those chips never check. Programs would be running, but appear to not run before they were written to not do anything if the sensor wasn't responding. Because the sensor would work after uploading, but not after power cycling the board and their programs would play dead, people would falsely conclude Teensy didn't keep their program in flash memory!

    The point is to remember those other chips retain configuration & settings while powered. These sorts of problems are often a sign of subtle bugs in your program where your code works or doesn't work depending on the settings established inside those chips from a previous or lack of previous run of your code.

    But of course this is all just blind guesswork and speculation, which is the best I can do with so little info.

  4. #4
    Junior Member
    Join Date
    Mar 2020
    Location
    Italy
    Posts
    17
    Sorry Paul, you right!!

    I think the problem is about MCP23S17 that is connected via SPI.
    I'm using this lib:
    https://github.com/sumotoy/gpio_expander
    in particular: mcp23.17.h

    OLED via i2c works fine and Serial port seems to work fine.

    My code is 6 class, I try to resume here the interesting point

    //Initialization
    mcp23s17 mcp_selector(10,0x20);


    //In Setup
    mcp_selector.begin();//x.begin(1) will override automatic SPI initialization
    mcp_selector.gpioPinMode(OUTPUT);

    // the loop function runs over and over again forever
    void loop() {


    delay(2000);

    for (int i=0;i<3;i++){
    if (i==pinON){
    mcp_selector.gpioDigitalWrite(i, HIGH);
    }
    else{
    mcp_selector.gpioDigitalWrite(i, LOW);
    }
    }

    if (pinON==2)
    pinON=0;
    else
    pinON++;

    }

    I tried to disconnect i2c OLED but nothing change.


    Everything works when I upload code or reboot BUT doesnt work when I connect the teensy for the first time.


    Sorry for lack of info in my posts. :-(

  5. #5
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    22,478
    I believe that MCP23S17 chip has a reset pin. If you can connect a wire to that pin, maybe try using digitalWrite() to pulse it low before you use it, so you know it's always starting from a consistent state.

  6. #6
    Junior Member
    Join Date
    Mar 2020
    Location
    Italy
    Posts
    17
    You make my day!!

    Yeah the problem was about startup speed i suppose.
    In fact i have used the reset pin (LOW and HIGH) in the setup method before the MCP init.

    setup(){
    delay(3000);
    digitalWrite(8,LOW);
    delay(50);
    digitalWrite(8,HIGH);
    delay(3000);
    mcp_selector.begin();
    mcp_selector.gpioPinMode(OUTPUT);
    }

    NOTE: with a short delay the problem persist!!

    Thank you very much !!!!!

  7. #7
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    22,478
    It's probably just the delays. For digitalWrite to work, you first need pinMode to make it an output.

  8. #8
    Junior Member
    Join Date
    Mar 2020
    Location
    Italy
    Posts
    17
    Yeah correct.

    Updated code:

    //Initialization of MCP
    mcp23s17 mcp_selector(10,0x21);


    void setup() {


    Serial.begin(9600);
    Serial.setTimeout(100);

    pinMode(8,OUTPUT);
    digitalWrite(8,HIGH);

    delay(3000);
    //Serial.println("RESET");
    digitalWrite(8,LOW);
    delay(50);
    digitalWrite(8,HIGH);
    //Serial.println("END RESET");

    delay(3000);

    mcp_selector.begin();//x.begin(1) will override automatic SPI initialization
    mcp_selector.gpioPinMode(OUTPUT);

    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •