SoftwareSerial with ATTiny84 using ATtiny library

hiduino:

[quote author=Coding Badly link=topic=123388.msg1275111#msg1275111 date=1371015653]
I have a version that may work but I have no way to test it. If you agree to provide feedback (that includes success) then I'm willing to publish.

I'll be willing to test it out also.[/quote]

Excellent. Is delivery via Github acceptable?

If you don't mind, I'd like you to review the pin assignments for mistakes / opinions...

LED_PIN is assigned to digital pin 13. TinyISP uses a single LED for status. I chose the on-board LED.

TICK_TOCK_PIN is assigned to digital pin 47. The "Tick Tock" pin is used as a basic chronograph. When the pin toggles a timestamp is output. It allows basic performance monitoring and tuning. I have a version that uses Input Capture so I try to assign TICK_TOCK_PIN to ICP1 (Input Capture for Timer 1).

Knock-Bang is always assigned to MISO which is PB4 / digital pin 50.

The SPI pins are...
MISO is 50.
MOSI is 51.
SCK is 52.
SS is 53.

The SPI pin assignments are only relevant when PROGRAMMER_SPI_CLOCK == SLOW. I believe the Arduino core provides the assignments.

There are two more things that will have to be assigned pins: "Tuning Signal" and "Alternate Knock-Bang".

The Tuning Signal is one-half of a much improved Tiny Tuner. For the m328 processor the Tuning Signal is currently placed on a timer 2 output (OC2B). The only requirement is the pin must be a timer output. A pin close to the target (close to the SPI pins) is helpful.

There are times when MISO cannot be used for Knock-Bang. The Alternate is simply another choice for Knock-Bang. The only requirement is the pin has to support Pin Change Interrupts. I typically choose this pin based on physical location: a pin close to the SPI pins that does not conflict with any other pin assignment.

Finally, the Serial Relay code will have to be updated (this is a note to myself).

I've actually been trying to play with TinyISP on my mega2560 but didn't get too far with it.

The code is very poorly structured for porting so it's certainly understandable you ran into problems. I had to grep for a few things and I wrote the thing.

Yes, Github is fine. I'll try to review it as detailed as I can.

I was able to at least get the TinyISP to build on the mega2560 and program a few ATtiny84/85s. But not get the serial relay to work. I didn't try knock-bang because I figured that required more work. I'll wait for your code.

hiduino:
Yes, Github is fine. I'll try to review it as detailed as I can.

Ready for a test drive...

I was able to at least get the TinyISP to build on the mega2560 and program a few ATtiny84/85s.

Excellent.

But not get the serial relay to work.

I'll take a look at it tonight. I doubt it will work without some changes.

I didn't try knock-bang because I figured that required more work.

It should be working now.

Thanks!

Bonzo:
How ever, a 100nF cap doesn't do much.

A 100nF capacitor connected to what?

Ok, I know you said serial relay was note to yourself. But I tried anyway and was able to get it to work with a Mega2560 and tiny84. I made two changes to the code.

TinyISP_SelectBuildOptions.h (added support for Mega board):

#if (RELAY_SERIAL_ENABLED) && (BOARD_ARDUINO_MEGA)
  #define RELAY_SOFT_SERIAL_ENABLED  1
#endif

TinyISP_RelaySoftSerial.h (added support for different MISO pins):

#if (BOARD_ARDUINO_UNO)
  static SerialRelay RelaySerial( 12, 14 );
#endif

#if (BOARD_ARDUINO_MEGA)
  static SerialRelay RelaySerial( 50, 14 );
#endif

I will continue testing the other items.

Knock-Bang failed to relay from both the Uno and Mega board types, with various tiny84/85/13 and various speeds 1MHz, 8MHz, 16MHz, 9.6 MHz, 4.8 MHz.

--- Monitor starting ---

--- Knock-Bang fault: 15 ---

--- Knock-Bang fault: 15 ---

--- Knock-Bang fault: 15 ---

--- Monitor stopped ---

However, going back to the previous version of TinyISP it worked fine on the Uno and various tinys.

--- Monitor starting ---
Caitlin! 
Caitlin! 
Caitlin! 

--- Monitor stopped ---

Does TinyDebugKnockBang need to be updated also?

I found some commented out lines in KnockBangReceiver.cpp, "Skip the Hello"? What was this for?

When I put back the commented out code I was able to get KnockBang to work on the Mega2560 and Uno to some tinys.

--- Monitor starting ---
KnockBang-Testing...
KnockBang-Testing...
KnockBang-Testing...

--- Monitor stopped ---

hiduino:
Does TinyDebugKnockBang need to be updated also?

Yes. Sorry about that. TinyISP and TinyDebugKnockBang are paired and I made a change to the Knock-Bang protocol...

hiduino:
I found some commented out lines in KnockBangReceiver.cpp, "Skip the Hello"? What was this for?

The protocol goes like this...

MISO is held high by the internal pull-up resistors on both sides. When the transmitter (the ATtiny processor) wants to send data it sinks MISO low and holds it low for a few microseconds. This triggers a pin-change interrupt on the receiver. The receiver checks MISO to ensure there was not a false positive then waits for MISO to float high. The transmitter changes MISO back to an input w/ pull-up allowing it to float high. That sequence is the "knock".

The receiver then pulls MISO low for a few microseconds. Upon noticing MISO go low the transmitter prepares to send data; it waits for MISO to float high. The reciever changes MISO back to an input w/ pull-up allowing it to float high. That sequence is the "hello".

Once MISO floats high, the transmitter proceeds to output data at ~3us per bit followed by a termination string. That sequence is the "bang".

I dropped the "hello". It solves a problem that does not exist.

When I put back the commented out code I was able to get KnockBang to work on the Mega2560 and Uno to some tinys.

Excellent. Updating TinyDebugKnockBang should get the whole thing working. Until you have all the kinks worked out I suggest adding a series resistor (220 to 1000 ohms) on MISO. With the "hello" in the protocol on one side but not on the other there is a slight possibility of a short-circuit through the two processors.

Opps, I hope my avrs are still okay. :astonished:

So with the new protocol on both sides, this short-circuit issue is prevented?

Will you have your updated TinyDebugKnockBang on Git also? I checked but the last update was a year ago.

hiduino:

[quote author=Coding Badly link=topic=123388.msg1278966#msg1278966 date=1371278944]
Excellent. Updating TinyDebugKnockBang should get the whole thing working. Until you have all the kinks worked out I suggest adding a series resistor (220 to 1000 ohms) on MISO.

Opps, I hope my avrs are still okay. :astonished:[/quote]

The resistors are generally a good idea especially when testing software. However, I made a mistake with this...

With the "hello" in the protocol on one side but not on the other there is a slight possibility of a short-circuit through the two processors.

At one point I had a version of TinyDebugKnockBang that skipped the "hello". That version was never published. TinyDebugKnockBang always waits the 6us. I did not drop the "hello"; I made it optional. I apologize for the mistake and the unnecessary concern.

Will you have your updated TinyDebugKnockBang on Git also? I checked but the last update was a year ago.

Something weird is going on with Github. I did a pull and the versions match but browsing they do not match. (The code of interest is here... TinyDebugKnockBang/TinyDebugKnockBang.cpp at master · Coding-Badly/TinyDebugKnockBang · GitHub). bbailout2 was moved...

    // For six microseconds check for Hello
  ".if %[_6us_hst] < 3"                               "\n\t"
    "sbis  %[pin], %[bit]"                            "\n\t"
    "rjmp  L%=bgothello"                              "\n\t"
    "sbis  %[pin], %[bit]"                            "\n\t"
    "rjmp  L%=bgothello"                              "\n\t"
    "sbic  %[pin], %[bit]"                            "\n\t"
    "rjmp  L%=bbailout2"                              "\n\t"
  ".else"                                             "\n\t"
    "ldi   %[d], %[_6us_hst]"                         "\n\t"  //  1  1
  "L%=bwaitforhello:"  "\n\t"
    "sbis  %[pin], %[bit]"                            "\n\t"  //  1  2 2 1
    "rjmp  L%=bgothello"                              "\n\t"  //  2  - - 2
    "dec   %[d]"                                      "\n\t"  //     1 1
    "brne  L%=bwaitforhello"                          "\n\t"  //     2 2
    "rjmp  L%=bbailout2"                              "\n\t"
  ".endif"                                            "\n\t"

  "L%=bgothello:"                                     "\n\t"
    // Putting bbailout2 here instead of with bbailout1 makes the Hello optional
  "L%=bbailout2:"                                     "\n\t"

Got it. Please get a fresh copy of TinyDebugKnockBang and try again.

Okay, with both new copies of TinyISP and TinyDebugKnockBang, Knock-Bang is working on the Mega2560 going to a tiny84 @1MHz. I will continue reviewing and testing with various tinys and speeds.

hiduino:
Ok, I know you said serial relay was note to yourself. But I tried anyway and was able to get it to work with a Mega2560 and tiny84. I made two changes to the code.

That actually uses SoftwareSerial for the relay. Would you prefer one of the other USARTs like Serial1?

hiduino:
Okay, with both new copies of TinyISP and TinyDebugKnockBang, Knock-Bang is working on the Mega2560 going to a tiny84 @1MHz. I will continue reviewing and testing with various tinys and speeds.

Excellent. Thank you.

Yes, I noticed that. But isn't why you are using SoftwareSerial because you can direct it to the MISO pin? If you use the USART then you would have to wire additional connections from the target? SoftwareSerial seems to be working okay, what would be the benefits of using a USART? More speed options? Better accuracy?

I'm having trouble getting KnockBang to work on my Mega2560 (not tried serial yet). What I did:

Download latest TinyDebugKnockBang directory from github, put it in ~/sketchbook/libraries/
Download latest TinyISP directory from github, put it in ~/sketchbook/

Rename TinyISP.pde to TinyISP.ino
Added const to this line:

__attribute__ ((used)) char Copyright[] PROGMEM = "blah"

otherwise I got the error:

TinyISP.ino:101:42: error: variable ‘Copyright’ must be const in order to be put into read-only section by means of ‘__attribute__((progmem))’

Made sure #include <SoftwareSerial.h> was commented out with //

Added these lines to _TinyISP_BuildOptions.h:

#define RELAY_KNOCK_BANG_ENABLED 1
#define RELAY_SERIAL_ENABLED 0

Uploaded TinyISP.ino to Mega as usual (removed cap on reset/GND, uploaded, put it back, tried with and without 220ohm resistor on MISO)

Uploaded demo program from here to ATtiny85 (using arduino-tiny 0017 core, 8MHz internal clock, also tried 1MHz)

Opened serial monitor, changed to 19200 baud, waited.... just got a kind of a square character like [], at 9600 baud i get nothing at all

sej7278:
Opened serial monitor, changed to 19200 baud, waited.... just got a kind of a square character like [], at 9600 baud i get nothing at all

On the Mega2560, you have to wait about 10 seconds for the bootloader to timeout after opening Serial Monitor. You need to watch the "L" LED for the heartbeat pulse before type the "!" character to enter Monitor mode. When you are done with Monitor mode, don't forget to exit by typing "!" again, to put it back in ISP mode. You need to keep Serial Monitor at 19200 baud for this.

hiduino:

sej7278:
Opened serial monitor, changed to 19200 baud, waited.... just got a kind of a square character like [], at 9600 baud i get nothing at all

On the Mega2560, you have to wait about 10 seconds for the bootloader to timeout after opening Serial Monitor. You need to watch the "L" LED for the heartbeat pulse before type the "!" character to enter Monitor mode. When you are done with Monitor mode, don't forget to exit by typing "!" again, to put it back in ISP mode. You need to keep Serial Monitor at 19200 baud for this.

ah, didn't realise you had to enter ! for knockbang mode, now i'm getting:

--- Knock-Bang fault: 11 ---

also 4 and 13.....

hiduino:
On the Mega2560, you have to wait about 10 seconds for the bootloader to timeout after opening Serial Monitor.

You're not using a capacitor to disable auto-reset?

When you are done with Monitor mode, don't forget to exit by typing "!" again, to put it back in ISP mode.

Out of habit, I exit Monitor when I finish but doing that should not actually be necessary. There is code in TinyISP that automatically switches from Monitor to Programmer.