CASP models can interface with modules on serial, SPI and I2C bus for embedded micro-controller boards. This example project demonstrates a model that reads and writes to SD card module on Arduino Uno.
Pre-Requisites
- Arduino Uno board.
- SD card module.
- Resistors and LEDs as shown in the figure.
- CASP software is installed along with CASP Python Libraries.
- Arduino BSP is installed.
- The project files are located at CASP installed directory ‘CASP/support/examples/hardware_interface/10_sd_card’. These are also available for download at this link.
Description
SD card module is connected to the target board as shown in the circuit diagram. Two separate models are developed to read and write integer values to the SD card. User shall enable only one model at a time. User may copy the sd_file.txt file from the project directory to the SD card for this demonstration.
SD card read model reads the numbers from the file, converts to respective integer value and outputs to pin-3 to vary its brightness based on the integer value. User can see the integer value read from the SD card from the ‘Configure Simulation Hardware’ interface window by clicking on Simulation->Configure Simulation IO menu item from the CASP main tool bar. A Python script is also provided that plots the data from the board.
Similarly, the write model counts the number of pulses generated from the pulse generator and writes the count to the SD card file. Please go through the SD card block documentation for selecting suitable SD card.
Pin-2 will be high if the SD card encounters errors during reading and writing. User may enable CASP serial debug feature and check for detailed errors. Please go through the individual block parameters used in the model along with respective documentation for better understanding.

CASP Model

Procedure
- Run CASP and open the project ‘CASP/support/examples/hardware_interface/10_sd_card/10_sd_card.prj’ from CASP installed directory.
- Open ‘Setup Simulation’ window and change the ‘Target Programmer Port’ to which the board is connected.
- Click Run button to build the model and program the board.
Python Script
The Python script that plots the data from the Arduino board is shown below. The script file is available in the project directory. Within the script, change the serial port in the method ‘obj.InitSerialPort()’ to which the board is connected and run the script from Python terminal.
import casp
import casp_targets
import casp_defs
from models import casp_plot_n_control
#Initialize CASP
casp.Init()
# Create and open default project.
casp.ProjectOpenDefault()
# Instantiate class object
obj = casp_plot_n_control.Model(4,4)
# Initiate serial port. Change serial port with actual port to which the board is connected
obj.InitSerialPort("COM35") #serial_port_name, master
#obj.InitEthernet(False, False, "0.0.0.0", "0.0.0.0", 52001, "127.0.0.1", 42001) #server, tcp, gateway_ip, local_ip, local_port, remote_ip, remote_port
# Add plot
obj.AddPlotter(False, 0, 1, "val0") #data_type_real, data_index, data_size, title
# Finalize plotter
obj.Finalize()
# Set simulation to real time (by setting simulation speed factor to 1).
casp.SimSetupSpeedFactor(1)
# Build and program the board.
casp.SimStart()
# Open project folder. Go through the log files (log_build_wnd.txt and log_msg_wnd.txt) in the project folder for any error/warning messages.
#casp.FolderProject()
# Promptly close CASP model editor program. simulation panel program will still be running.
casp.Close()