Problems with a Nano clone

I'm working on a project that is essentially a Nano clone, and I'm having a lot of trouble. First off, I couldn't get 1.6.3 IDE to work with the Leonardo I'm using as an ISCP programmer, so I had to uninstall and revert back to 1.05. Problem 2 is that I'm using the ATmega328 since the P version was either more expensive or out of stock at the time, so I had to modify the avrdude config files to accept the signature of the 328. That's all fine, I can load sketches with the programmer, but there's still two problems I can't figure out.

First off, setting pin 2 to INPUT_PULLUP doesn't work. In fact, even with an external pullup resistor, it always reads low. I can do digital writes to this pin just fine. This is a problem because this is the only input on this board, and I don't have any extra pins available. I might be able to swap out pin 2 for another one, but it needs to be an interrupt.

Second, programming with the onboard USB to Serial doesn't seem to work. The chip never responds, and I think it might be a reset issue. I used the Cypress CY7C65213 chip instead of the FTDI used on the official Nano, but I did connect DTR to reset as per the official schematics. I don't know if there is some sort of difference with how the FTDI chip treats DTR, or what. It looks like DTR pulses low several times in the first few seconds after it's connected, then doesn't do anything else until it's unplugged again. Programming does work once-- right after I burn the bootloader the first time. Serial communication works as expected, so the chip is working fine.

Any advice at all would be greatly appreciated.

First off, I couldn't get 1.6.3 IDE to work with the Leonardo....

First off, setting pin 2 to INPUT_PULLUP doesn't work....

Second, programming with the onboard USB to Serial doesn't seem to work....

I want to say something clever.... my brain is melting from a current transformer... sorry. someone else will need to post some snappy quip.

rexxar:
I couldn't get 1.6.3 IDE to work with the Leonardo I'm using as an ISCP programmer[/quote
That's ICSP, not ISCP. I suspect you wired it wrong or tried to use it incorrectly but there is not enough information to make any further guesses. I recommend a USBtinyISP or USBasp from eBay for $4 or $5. They are known working

rexxar:
Problem 2 is that I'm using the ATmega328 since the P version was either more expensive or out of stock at the time, so I had to modify the avrdude config files to accept the signature of the 328. That's all fine, I can load sketches with the programmer

So "Problem 2" is not a problem. That's good.

rexxar:
setting pin 2 to INPUT_PULLUP doesn't work. In fact, even with an external pullup resistor, it always reads low. I can do digital writes to this pin just fine.

That is very strange. Was there anything connected to the pin when you used it as an input? How did you determine that you could write to the pin? Did you use a meter or an LED?

rexxar:
I might be able to swap out pin 2 for another one, but it needs to be an interrupt.

Pin 3 is an interrupt pin on the ATmega328.

rexxar:
programming with the onboard USB to Serial doesn't seem to work. The chip never responds, and I think it might be a reset issue. I used the Cypress CY7C65213 chip instead of the FTDI used on the official Nano, but I did connect DTR to reset as per the official schematics. I don't know if there is some sort of difference with how the FTDI chip treats DTR, or what. It looks like DTR pulses low several times in the first few seconds after it's connected, then doesn't do anything else until it's unplugged again. Programming does work once-- right after I burn the bootloader the first time.

That is a classic symptom of an auto-reset circuit that is not working. Is your Reset pull-up 10K? Is your DTR to Reset capacitor 100 nF?

dave-in-nj:
I want to say something clever.... my brain is melting from a current transformer... sorry. someone else will need to post some snappy quip.

I don't have a problem with introductory clauses and I can stop any time I like!

johnwasser:
That is very strange. Was there anything connected to the pin when you used it as an input? How did you determine that you could write to the pin? Did you use a meter or an LED?

The only thing connected to that pin is a 1uF cap for debouncing and the switch. I verified the write with my oscilloscope. I don't know what's happened between now and when I wrote this post, but the pullups seem to be working now. Maybe I had a solder bridge or something.

That is a classic symptom of an auto-reset circuit that is not working. Is your Reset pull-up 10K? Is your DTR to Reset capacitor 100 nF?

Yes, I have a 10k pullup and 100nF cap. After a long break I noticed that DTR seems to fire whenever a program tries to open the serial port. Is this normal behavior, or something weird with the Cypress drivers?

rexxar:
Yes, I have a 10k pullup and 100nF cap. After a long break I noticed that DTR seems to fire whenever a program tries to open the serial port. Is this normal behavior, or something weird with the Cypress drivers?

That is normal. The Arduino normally gets reset every time the USB serial connection is established.

Okay, I think I've got it figured out. I'm using the internal 8MHz oscillator, and the serial library seems to still be expecting a 16MHz clock, so the baudrate isn't matching up. I'm using CodeBlocks for my IDE, so I just tweaked some settings and that's all working again.

johnwasser:
That is normal. The Arduino normally gets reset every time the USB serial connection is established.

Okay, apparently my code is messed up. It will only accept serial input if it's already sending something. If I have it print a debug line once, it will accept one command and print its result, but do nothing with any further commands. If I have it constantly print debug messages it will behave normally (except for the massive amount of debug text).

Here's the relevant section of code. It was originally written for a Leonardo clone, does anything look off?

void checkSerial() {

    // Get the number of bytes to read and abort if there is no data
    int bufsize = Serial.available();
    if ( !bufsize ) return;

    // Skip lone characters.. this happens sometimes on PuTTY,
    // when a lone \r lags behind the rest of the data being sent
    // and shows up in an iteration of this function all on its lonesome.
    if ( bufsize == 1 ) {
        (void) Serial.read();
        return;
    }

    // Create a buffer and read serial data into it
    char buffer[SERIAL_BUFFER_SIZE+1];
    Serial.readBytes ( buffer, bufsize );
    // Make sure the buffer terminates,
    // and get rid of any carriage returns
    buffer[bufsize] = '\0';
    if ( buffer[bufsize-1] == '\r' ) {
        buffer[bufsize-1] = '\0';
    }

    // Parse arguments and return if none were given
    param_t args[CMD_MAX_ARGS]; // arg_t
    int n_args = parse_args( buffer, args );
    if ( !n_args ) return;

    // Get command from args array
    char* cmd = args[0][0];

    bool routed = 0;

#define ROUTE_CMD( x, y )                    \
if ( 0 == strcmp_P( cmd, (char*)F( x ) ) ) { \
    y ( args, n_args );                      \
    routed = 1;                              \
}
    ROUTE_CMD( "handshake-iodriver",    cmd_handshake );
    ROUTE_CMD( "version",               cmd_version );
    ROUTE_CMD( "params",                cmd_params );
    ROUTE_CMD( "read",                  cmd_read );
    ROUTE_CMD( "write",                 cmd_write );
    ROUTE_CMD( "prof-print",            cmd_profprint );
    ROUTE_CMD( "prof-w",                cmd_profw );
    ROUTE_CMD( "prof-wc",               cmd_profwc );
    ROUTE_CMD( "prof-mv",               cmd_profmv );
    ROUTE_CMD( "prof-del",              cmd_profdel );
    ROUTE_CMD( "voltage",               cmd_voltage );
    ROUTE_CMD( "memory",                cmd_memory );
    ROUTE_CMD( "preview",               cmd_preview );
    ROUTE_CMD( "reload",                cmd_reload );
    ROUTE_CMD( "erase",                 cmd_erase );
    ROUTE_CMD( "register",              cmd_register );
#undef ROUTE_CMD
    if ( !routed ) {
        Serial.println( F("Unknown command.") );
    }
}

It looks like it only responds if I send it a command before it hits the debug line, and it won't recognize any command, it just pints "Unknown command" and the debug text at the same time. After that it's completely non-responsive.

Okay, so I added this bit of code to have it print back out what it recieved:

for (int i = 0; i<= bufsize; ++i)
    {
        Serial.print(buffer[i]);
    }

I sent the command "memory" and got this back: µ½Éåÿ
The binary value for those characters isn't even close to those for 'memory' so I'm not sure what's going on. I'm guessing that for whatever reason it's receiving garbled data and it's breaking my code. It's not the serial chip, because if I short TX and RX it will repeat exactly what I send it.

Sounds like a baud rate mismatch.

dave-in-nj:
I want to say something clever.... my brain is melting from a current transformer... sorry. someone else will need to post some snappy quip.

Sounds like a baud rate mismatch.

EXACTLY !!!!

johnwasser:
Sounds like a baud rate mismatch.

You're right.

I've figured out why it's not responding. The serial buffer isn't working like we expected. This code was originally written for a Leonoardo clone, and the serial buffer filled up immediately, perhaps because the USB/serial interface was built in? Right now the buffer only has time to store one byte before it gets read, so I suppose the solution is to increase baudrate and add some delays to wait for it to fill.