User Guide Topics

The MCHP Blockset automatically generates optimized C code from Simulink models for deployment on Microchip microcontrollers.

Code Generation Workflow

  • Model Design - Create algorithm in Simulink
  • Configuration - Set up target hardware and compiler
  • Build - Generate and compile code
  • Deploy - Program device with generated firmware

Model Configuration Parameters

System Target File

Select the appropriate target for your device family:

  • MCHP_dsPIC_PIC24.tlc - dsPIC and PIC24 devices
  • MCHP_PIC32.tlc - PIC32 devices
  • MCHP_SAM.tlc - SAM (ARM) devices

Code Generation Settings

ParameterRecommended ValueDescription
Target selectionert.tlcEmbedded Coder target
LanguageCGenerate C code (default)
Code interface packagingReusable functionFor standalone deployment
Generate code onlyUncheckedEnable compilation

MCHP Target Settings

Configure in Code Generation → MCHP Target section:

  • Device - Select target microcontroller
  • Compiler Version - Select installed compiler
  • Optimization Level - 0 (debug) to 3 (max speed)
  • Build Action - Build, Build and Download, etc.

Optimization Settings

Compiler Optimization Levels

LevelFlagDescriptionUse Case
0-O0No optimizationDebug, code analysis
1-O1Basic optimizationDevelopment testing
2-O2Moderate optimizationBalanced speed/size
3-O3Aggressive optimizationMaximum performance
s-OsSize optimizationMemory-constrained devices

Model Optimization

  • Solver - Fixed-step, Discrete (no continuous time)
  • Eliminate dead branches - Enable for code size reduction
  • Inline parameters - Enable for performance
  • Reusable functions - Enable for modular code

Custom Code Integration

Adding Custom C Code

Use Code Generation → Custom Code section:

  • Include directories - Add header file paths
  • Source files - Add .c files to compilation
  • Additional include files - Specify headers to include
  • Custom code - Add code snippets (initialization, etc.)

Legacy Code Integration

Use Legacy Code Tool to wrap existing C functions:

  • Create legacy code specification structure
  • Register function interface with Simulink
  • Generate S-Function wrapper
  • Use S-Function block in model

Code Generation Process

Build Steps

  • TLC Processing - Model converted to Target Language Compiler format
  • Code Generation - C code generated from TLC
  • Compilation - XC compiler builds object files
  • Linking - Object files linked to create executable
  • Post-processing - Generate .hex, .elf, .lst files

Generated Files

File TypeExtensionDescription
Source Code.c, .hGenerated C source and headers
Executable.elfELF format with debug symbols
Programming File.hexIntel HEX for device programming
Listing.lstAssembly listing with addresses
Map File.mapMemory map and symbol table

Fixed-Point Code Generation

Fixed-Point Toolbox Integration

For DSP and motor control applications:

  • Use fi() objects for fixed-point signals
  • Configure word lengths: 8, 16, 32 bits
  • Set fraction lengths for precision
  • Enable fixed-point code generation in model settings

dsPIC Assembly Optimization

Blockset includes assembly code replacements for:

  • Math Functions - sin, cos, atan2, sqrt
  • Matrix Operations - multiply, transpose
  • DSP Blocks - FIR, IIR filters, FFT
  • Saturation - Optimized saturation arithmetic

Note: Assembly replacements are automatically used when available for the target device family.

Memory Management

RAM Allocation

  • Stack - Configured in linker script
  • Heap - Avoid dynamic allocation if possible
  • Global Variables - Minimize for better performance
  • DMA Buffers - Use aligned memory sections

Flash Optimization

  • Use const for constant data
  • Enable compiler size optimization (-Os)
  • Minimize duplicate code with reusable functions
  • Use lookup tables for complex math

Build Diagnostics

Common Build Errors

ErrorCauseSolution
Compiler not foundPath not configuredSet compiler path in model settings
Symbol not definedMissing library/headerAdd to custom code includes
Memory overflowCode/data too largeEnable optimization, reduce model size
Unsupported blockBlock not compatibleReplace with supported alternative

Build Warnings

Address these warnings for robust code:

  • Unused variables - Remove or comment out
  • Type mismatches - Use explicit casts
  • Uninitialized variables - Add initialization
  • Deprecated functions - Update to current API

Code Review and Analysis

Generated Code Inspection

Review generated code for:

  • Efficiency - Check loop structure, function calls
  • Safety - Verify array bounds, pointer usage
  • Readability - Enable code generation comments
  • Compliance - MISRA-C, coding standards

Code Metrics

Use Code Generation Report to analyze:

  • Lines of code generated
  • ROM/RAM usage
  • Function complexity
  • Stack depth estimates

Best Practices

  • Use discrete solver with fixed time step
  • Avoid dynamic memory allocation
  • Minimize global variable usage
  • Use appropriate data types (int16, uint32, etc.)
  • Enable code generation report for documentation
  • Test with PIL before final deployment
  • Use version control for model files

Home | User Guide | External Mode | PIL Testing | Compiler Setup | Video Demos | Block Reference