MicrocontrollerKit`
MicrocontrollerKit`

MicrocontrollerEmbedCode

MicrocontrollerEmbedCode[sys,μc,p]

embeds the systems model sys to the microcontroller μc using p.

Details

  • MicrocontrollerEmbedCode generates the source code for sys to work with the microcontroller specification μc, compiles it, and embeds the resulting machine code to the μc.
  • In addition a MicrocontrollerCodeData object is generated that can be used to obtain various properties.
  • The systems model sys can be a discrete-time TransferFunctionModel, StateSpaceModel, AffineStateSpaceModel, NonlinearStateSpaceModel, or SystemsConnectionsModel.
  • The microcontroller μc is given as an association with the following primary keys:
  • "Target"target name
    "Inputs"input channels
    "Outputs"output channels
  • Possible input and output channel specifications:
  • cnametypechannel cname configured as type
    cname<|"Type"type|>channel cname configured as type
    cname<|"Type"type,s1v1,|>additional specifications
    typecname for type is unique
    <|"Type"type,|> or Automatic-><|"Type"type,|>cname for type is unique
  • Other possible keys for μc:
  • "ADC"A/D conversion specifications
    "ClockFrequency"frequency of the microcontroller's system clock
    "I2CAddress"I2C address when operating as a I2C slave device
    "Microcontroller"microcontroller to generate code for
    "OperatingVoltage"operating voltage of the microcontroller
    "Serial"serial communication specifications
    "SerialOverUSB"serial over USB specifications
    "SlaveSelect"slave select pin when operating as a SPI slave device
    "Timer"timer or counter number
  • Keys of μc to directly inject code into the source code:
  • "IncludeFiles"files to include
    "Definitions"definition of macros
    "Declarations"function declarations
    "Utilities"utility functions and variables
    "Initializations"program initialization
    "Loop"execute at each sampling instant
    "PreLoop"execute before "Loop"
    "PostLoop"execute after "Loop"
    "UntimedLoop"execute repeatedly whenever the processor is free
  • The programming mechanism p is specified as an association.
  • Possible keys of p include:
  • "BaudRate"baud rate at which to embed the code
    "ConnectionPort"port where external programmer is connected
    "ExternalProgrammer"external programmer to be used
    "ProgrammerName"programmer to be used
    "ProgrammerConfigurationName"programmer configuration to be used
    "ProgrammerConfigurationInstallation"location of the programmer configuration software
    "ProgrammerInstallation"location of the programmer software
  • If p is a string the external programmer is assumed to be connected to the serial port p.
  • MicrocontrollerEmbedCode[sys,μc,p,specs] can be used to give additional specifications.
  • The specifications specs is as association with the following possible keys:
  • "CleanIntermediate"Truewhether temporary files should be deleted
    "CompileOptions"Automaticoptions to pass through to the compiler
    "CompilerInstallation"Automaticlocation of the compiler software
    "CompilerName"Automaticwhich compile command to use
    "CreateBinary"Truewhether the machine code should be created
    "I2C"{}external I2C device specifications
    "IncludeDirectories"{}directories to add to the include path
    "Language"Automaticsource code language
    "Libraries"{}libraries to use to build the executable
    "SPI"{}external SPI device specifications
    "WorkingDirectory"Automaticdirectory where temporary files are generated

Examples

open all close all

Basic Examples  (1)

Load the package:

In[1]:=
Click for copyable input

Turn the LED on an Adafruit Trinket 5V at half brightness:

In[2]:=
Click for copyable input
Out[2]=

The byte count of the embedded machine code:

In[3]:=
Click for copyable input
Out[3]=

The source code that was compiled to generate the machine code:

In[4]:=
Click for copyable input
Out[4]=

Scope  (36)

Generalizations & Extensions  (1)

Possible Issues  (2)