Skip to content

Save some space in MuxTO #147

@WestfW

Description

@WestfW

Space in MuxTO is very tight; depending on compiler version, it can't be built to fit into the available 12k of flash.
This piece of code is (should be) a drop-in replacement for the CRC function in crc16.cpp. It does bitwise calculation of the CRC instead of the table-driven byte-at-a-time algorithm that is currently used. This is presumably about 8x slower (which I believe shouldn't matter), but it eliminates the 512byte crc_table, making it about 1/40th the flash size.

(CRC Algorithm tested on a desktop, and code compiles in Arduino. Not yet tested in an actual Nano every...)

/*                                                                                                                     
 * "reflected" (LSB first) CCITT CRC-16                                                                                
 */                                                                                                                    
uint16_t POLY = 0x8408;  // bit reversed 0x1021                                                                        
                                                                                                                       
uint16_t CRC::next(uint8_t newchar, uint16_t crc) {                                                                 
  int j;          
  uint_fast16_t crcshadow = crc ^ newchar;  // move some uxth instructions outside the bit loop.                                                                                                                                                                                                       
  for (j = 0; j < 8; j++) {                                                                                            
    if (crcshadow & 1) { // Check if LSB is set                                                                              
      crcshadow = (crcshadow >> 1) ^ POLY;                                                                                         
    } else {                                                                                                           
      crcshadow >>= 1;                                                                                                       
    }                                                                                                                  
  }                                                                                                                    
  return crcshadow;                                                                                                          
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions