Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pull request for V2 #43

Merged
merged 96 commits into from
Jun 6, 2015
Merged

Pull request for V2 #43

merged 96 commits into from
Jun 6, 2015

Conversation

cpldcpu
Copy link
Member

@cpldcpu cpldcpu commented Jan 15, 2014

This pull request is here to have a place to discuss and document changes leading to v2 Please don't merge yet.

Todo

  • Synchronize branches

Firmware

  • Document interruptless V-USB
  • Document new protocol
  • New build system to support multiple targets
  • New device configuration reply to support 841
  • Discuss: remove saved osccal? -> leave as option
  • Add support for long jump
  • Put CRC checking back in.

Commandline

  • New protocol
  • Host side reset vector patching
  • Add support for long jump
  • Add fast mode
  • Set no-ansi as default in windows
  • Update commandline to support new device reply

Release

  • Build Windows and Macos binaries of commandline tool.
  • Update documentation

This is a completely new architecture.

  • It does not use any interrupts and does therefore not require INT vector patching.
  • Current version is 1572 bytes!
  • It's much faster.

zero sized "In" requests require more handshaking, but do not add more
error checking or more security.
The block transfer is now done in the address and indexfield of a
setup-packet to save a lot of memory in the firmware:
This requires twice the number of transmissions, but is effectively
faster due to less bus congestion and resends.
- removed code for replies from SRAM. The new protocol does not need
this
- only use zero initialized global variables to avoid having a data
section
SETUP/DATA timed out under Linux
1578 bytes, yay..
Conflicts:
	commandline/builds/Windows/micronucleus.exe
@cpldcpu
Copy link
Member Author

cpldcpu commented Feb 4, 2014

Starting to do some work on V2 again.

@Bluebie, one thing I have been considering is to remove the option to save the osccall in the flash. The reason for this is that it gives a false sense of security in my opinion. Since the RC oscillator also has a temperature dependent component, the saved calibration may not be valid if the programming and use temperature are different. (e.g. a device programmed in the summer may fail in the winter). Do you know anything that would break when not saving osccal? It seems to me that the Digispark libraries still have their osccal.asm included.

Edit: Just checked the data sheet. 30°C of temperature difference leads to 1% oscillator frequency deviation.

@Bluebie
Copy link
Member

Bluebie commented Feb 4, 2014

All DigiSpark sketches which do not use a USB library would start at about 3% extra clock inaccuracy when powered by something other than a non-sleeping USB host computer. Also any time a DigiSpark is tested on a USB connection and then run without one later (DigiUSB as print-style debugging is pretty common) would cause unexpected and difficult to debug bugs. 3% doesn't sound like much but it's made a big difference to a lot of timing sensitive stuff. Micronucleus only added osccal saving around 1.06 and it was added for good reasons. I wouldn't personally recommend micronucleus be used on DigiSparks without it.

@cpldcpu
Copy link
Member Author

cpldcpu commented Feb 5, 2014

Ok, then I'll leave the option in. If the configuration systems pans out as well as I hope, it is still possible to offer different versions of the hex files.

Btw, I took the liberty to remove the trailing text from your post. What an odd feature of github to allow editing of other peoples posts?

@cpldcpu
Copy link
Member Author

cpldcpu commented Feb 18, 2014

Implemented changes to the protocol from V1.x to V2.x:

  • Instead of using block data transfers, all data is transferred in SETUP packets, as proposed by @embedded-creations. This reduces the code size of the client.
  • The insertion of the jmp to the user reset vector before the bootloader is done by the commandline tool for a significant complexity reduction of the client.
  • Insertion of the bootloader into the master reset vector is still done in the client firmware. This ensures that the device can not be bricked, even if incorrect data is uploaded from the commandline tool. Worst case scenario is a crash when trying to execute the user program.

@cpldcpu
Copy link
Member Author

cpldcpu commented Feb 18, 2014

Addition of support for 16kb devices poses a bit of a problem. Since the commandline tool is oblivious to the type of device it is programming, it has to be inferred from the available data whether a JMP or RJMP is used or has to be inserted.

It is not sufficient to check for the user space size, since:

  1. The bootloader may be located in the first 8kb of a 16kb device. In that case the vector size is still 4 bytes instead of two on a 8kb device.
  2. On a 8kb device the vector table is always populated with RJMP, on 16kb it may be RJMP or JMP.

It should be noted, that 1. would be more easily solved if all vector handling was done in the client firmware since it is known during compiling of firmware whether JMP is supported or not.. However, 2. requires some complixity and error handling that would bloat the firmware.

@cpldcpu
Copy link
Member Author

cpldcpu commented Feb 18, 2014

Changes to commandline tool

  • The instruction in the reset vector is decoded and the user entry vector is extracted from either RMP or RJMP. If no jmp instruction is found, the commandline tool will abort with an error. This also prevents a problem with micronucleus v1 which would break the user program if no rjmp was used in the reset vector.
 if ( address == 0 ) {
        // save user reset vector (bootloader will patch with its vector)
        uint32_t word0,word1;
        word0 = page_buffer [1] * 0x100 + page_buffer [0];
        word1 = page_buffer [3] * 0x100 + page_buffer [2];

        if (word0==0x940c) {  // long jump
          userReset = word1;          
        } else if ((word0&0xf000)==0xc000) {  // rjmp
          userReset = (word0 & 0x0fff) - 0 + 1;    
        } else {
          fprintf(stderr,
                  "The reset vector of the user program does not contain a branch instruction,\n"
                  "therefore the bootloader can not be inserted. Please rearrage your code.\n"
                  );
          return -1;         
        }        
      }
  • The tinyvectortable entry for the user program entry branch is increase to 4 bytes from 2 bytes. To allow for either rjmp or jmp
  • rjmp is used if the user space is <8kb, jmp of it is above 8kb.
     if ( address >= deviceHandle->flash_size - deviceHandle->page_size ) {
        // move user reset vector to end of last page
        // The reset vector is always the last vector in the tinyvectortable
        unsigned int user_reset_addr = (deviceHandle->pages*deviceHandle->page_size) - 4;

        if (user_reset_addr > 0x2000) {
          //  jmp
          unsigned data = 0x940c;
          page_buffer [user_reset_addr - address + 0] = data >> 0 & 0xff;
          page_buffer [user_reset_addr - address + 1] = data >> 8 & 0xff;
          page_buffer [user_reset_addr - address + 2] = userReset >> 0 & 0xff;
          page_buffer [user_reset_addr - address + 3] = userReset >> 8 & 0xff;        
        } else {
          // rjmp
          unsigned data =  0xc000 | ((userReset - user_reset_addr/2 - 1) & 0x0fff);             
          page_buffer [user_reset_addr - address + 0] = data >> 0 & 0xff;
          page_buffer [user_reset_addr - address + 1] = data >> 8 & 0xff;
        }
      }

Added new global flag to enable unsafe optimizations:

This will disable several safety features in microncleus to save around
40 more bytes

Disabled features:
* Stack pointer and SREG initialization in CRT
* Client side reset vector patching
* USB collision detection. Micronucleus will not work reliability with
hubs if this is disabled.

See t85_aggressive configuration for usage examples.
@cpldcpu
Copy link
Member Author

cpldcpu commented Jan 11, 2015

ENABLE_UNSAFE_OPTIMIZATIONS

Added new global flag to enable unsafe optimizations. This addresses several discussions about potential additional optimization by removing parts of the code that were left in as a fail safe or to support rare special cases. (#58)

Currently the only configuration using this is "t85_aggressive", which should not be used if reliable operation is a requirement, anyways.

Setting this define will disable several safety features in microncleus to save around
40 more bytes

Disabled features:

  • Stack pointer and SREG initialization in CRT
  • Client side reset vector patching
  • USB collision detection. Micronucleus will not work reliability with
    hubs if this is disabled.

See t85_aggressive configuration for usage examples.

@cpldcpu
Copy link
Member Author

cpldcpu commented Jun 6, 2015

I am cleaning up the repository a bit. I removed ruby and update, as they are not yet compatible to V2.0. They can be readded later, once they have been updated.

cpldcpu added a commit that referenced this pull request Jun 6, 2015
First release of V2.0 to master.
@cpldcpu cpldcpu merged commit 1548e70 into master Jun 6, 2015
@cpldcpu
Copy link
Member Author

cpldcpu commented Jun 6, 2015

@Bluebie: I have pushed V2.0 to master. Please check if you are ok with the readme and so on. I removed some parts that were not updated to V2.0 yet (ruby, upgrade)

@sodabrew: Can you provide an OS X binary of the commandline tool again? There were some updates and I had to remove the last binary to prevent it from being out of sync.

@cpldcpu
Copy link
Member Author

cpldcpu commented Jun 6, 2015

@Bluebie: Noticed one more thing: Can you update the tagline of the repository to reflect that not only ATtiny85 is supported?
"AtTiny85 usb bootloader with a strong emphasis on bootloader compactness. "

@sodabrew
Copy link
Contributor

sodabrew commented Jun 6, 2015

Sure thing, I'll take a look as soon as I can.

@cpldcpu cpldcpu deleted the testing-V2-New branch June 7, 2015 23:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants