Difference between revisions of "SD Loader"

From Land Boards Wiki
Jump to navigation Jump to search
Line 504: Line 504:
 
* [https://learn.adafruit.com/adafruit-microsd-spi-sdio/using-sdcardio Using sdcardio]
 
* [https://learn.adafruit.com/adafruit-microsd-spi-sdio/using-sdcardio Using sdcardio]
 
* [https://learn.adafruit.com/adafruit-microsd-spi-sdio/example-listing-files-on-sd-card Listing files on SD card]
 
* [https://learn.adafruit.com/adafruit-microsd-spi-sdio/example-listing-files-on-sd-card Listing files on SD card]
* Constructor
+
* sdcardio class constructor
 
<pre>
 
<pre>
class sdcardio.SDCard(bus: busio.SPI, cs: microcontroller.Pin, baudrate: int = 8000000)
+
sdcardio.SDCard(bus: busio.SPI, cs: microcontroller.Pin, baudrate: int = 8000000)
 
</pre>
 
</pre>
  

Revision as of 16:03, 13 June 2022

Introduction

Legacy computer systems typically load and save programs over serial ports. This is often done by attaching a PC and running a terminal program to send/receive programs to the legacy systems. Integrating modern storage, like SD cards, can be a serious challenge into legacy designs.

Replacing the external PC offers the opportunity to more easily transport the system. A compact solution that emulates the PC for transfers would be useful.

This design transfers files to/from an SD card via a serial port. It runs a menu driven program on small OLED to control transfers and supports SD cards up to 32GB (FAT32 formatted). The serial interface has RTS/CTS hardware handshake support. It can run with 5V or 3.3V target systems or at RS-232 levels. Custom cards could easily be added to emulate cassette interfaces or run.

Features

  • Supports SD cards up to 32GB
    • FAT32 formatted
  • Serial interface
    • Full hardware Handshake support

Cards

QTPy49 Card

  • Easier to cable external cards
  • Flexible power (7-12V)

QTPy49 FRONT REV1.png

QT Py Breakout Card

  • Do not install 5mm Terminal Block

QT Py Breakout P18212-720px.jpg

  • Power via USB C cable

SD CARD X49

  • Build 3.3V version
  • No regulator
  • Install NO5V jumper

SDCARDX49 P935 cropped-512px.jpg

MyMenu

  • I2C interface to OLED, I2C Port Expander with 3 LEDs, 5 switches

MyMenu-CCA-X3 5793-800px.jpg

RS-232 Level Shifter (Optional)

DTE P562-720px.jpg

Level Shifter (Optional)

LVL SH P18228-720px.jpg

QT Py Breakout Card (on GRID49 card) Cabling

  • GRID49 card
  • QT Py uses (2) 1x7 male headers for signals
    • Cut off 1 set of holes of (2) female housings to make 1x7 female headers
  • SD Card uses (1) 1x8 header
  • MyMenu uses (1) 1x5 header
  • QT Py uses 2x8 header for 3.3V and Ground
    • Use 2x3 female housings
  • 40mm cables to Host Target

SD Loader App Schematic.PNG

QT Py Breakout Card Cabling

QT Py SIGNAL CARD PIN COLOR
01 (AD0) INT(AD0) MyMenu P2-5 GRY
02 (AD1) RTS* TARGET TBD GRY
03 (AD2) CTS* TARGET TBD ORA
04 (AD3) SS* SD_CARD J1-5 GRY
05 (SDA) SDA MyMenu P2-3 WHT
06 (SCL) SCL MyMenu P2-4 BRN
07 (TX) TX TARGET TBD WHT
08 (RX) RX TARGET TBD BRN
09 (SCK) SCK SD_CARD J1-3 BLU
10 (MI) MISO SD_CARD J1-2 BRN
11 (MO) MOSI SD_CARD J1-4 WHT
12 (3V) +3.3V GRID49 Q7 WIRE
13 (GND) GND GRID49 R7 WIRE
14 (5V) 5V N/C
PWR-01 +3.3V SD_CARD J1-8 RED
PWR-02 +3.3V TARGET TBD RED
PWR-03 +3.3V MyMenu P2-2 RED
PWR-14 GND MyMenu P2-1 BLK
PWR-15 GND SD_CARD J1-1 BLK
PWR-16 GND TARGET TBD BLK

QTPy49 Card Cabling

QTPy49 CAD.PNG

  • QT Py Card has function specific headers

QTPy49 SD Loader.PNG

QTPy49 Card to MyMenu Cabling

QTPy49 SIGNAL MyMenu COLOR
J5-1 GND P2-1 BLK
J5-2 +3.3V P2-2 RED
J5-3 SDA P2-3 WHT
J5-4 SCL P2-4 BRN
J9-3 INT(AD0) P2-5 GRY

QTPy49 Card to SD_CARD_X49 Cabling

QT Py SIGNAL SD_CARD_X49 COLOR
J3-1 GND J1-1 BLK
J3-2 3.3V J1-8 RED
J3-4 (MO) MOSI J1-4 WHT
J3-5 (MI) MISO J1-2 BRN
J3-6 (SCK) SCK J1-3 BLU
J6-3 (A3) SS* J1-5 GRY

QTPy49 Card to Host Serial

QTPy49 SIGNAL COLOR
J8-3 (AD1) RTS* GRY
J7-1 (AD2) CTS* ORA
J4-4 (RX) RX BRN
J4-3 (TX) TX WHT
J4-2 (VCC) +3.3V Xlator (only)
J4-1 (GND) GND BLK

Arduino Software

  • i2cscan of attached cards shows OLED at 0x3c and MCP23008 at 0x20
I2C addresses found: ['0x20', '0x3c']

Arduino Software Development Environment

  • Developed using Arduino IDE V1.8.xx
  • Arduino IDE Setup for QT Py
  • Requires Arduino IDE version 1.8 or higher
  • Add to Preferences Additional Boards Manager URLs
https://adafruit.github.io/arduino-board-index/package_adafruit_index.json
 
pinMode(pin, INPUT_PULLUP)

#if defined(ARDUINO_SAMD_ZERO) && defined(SERIAL_PORT_USBVIRTUAL)
  // Required for Serial on Zero based boards
  #define Serial SERIAL_PORT_USBVIRTUAL
#endif

const char str[] = "My very long string";

Arduino Human Interface Design (HID)

Arduino SD Card

  • Library
  • Example files

Arduino SD Card Library

  • SD Card library
    • The SD library allows for reading from and writing to SD cards, e.g. on the Arduino Ethernet Shield
    • Based on sdfatlib
    • The library supports FAT16 and FAT32 file systems on standard SD cards and SDHC cards
    • It uses short 8.3 names for files
    • The file names passed to the SD library functions can include paths separated by forward-slashes, /, e.g. “directory/filename.txt”
    • Because the working directory is always the root of the SD card, a name refers to the same file whether or not it includes a leading slash (e.g. “/file.txt” is equivalent to “file.txt”)
    • As of version 1.0, the library supports opening multiple files.
    • The communication between the microcontroller and the SD card uses SPI, which takes place on digital pins 11, 12, and 13 (on most Arduino boards) or 50, 51, and 52 (Arduino Mega)
    • Additionally, another pin must be used to select the SD card
    • This can be the hardware SS pin - pin 10 (on most Arduino boards) or pin 53 (on the Mega) - or another pin specified in the call to SD.begin()

Arduino SD Card Example Code

Arduino SD Card Connections

  • Uses default board SPI pins
  • Uses D3 as chip select

Serial

  • Serial Reference
  • Serial.begin(speed)
  • Serial.available()
    • Get the number of bytes (characters) available for reading from the serial port.
    • This is data that’s already arrived and stored in the serial receive buffer (which holds 64 bytes).
    • Turn off/on receive handshake based on Serial.available() count and storage
  • Serial.write()
    • Writes binary data to the serial port
    • This data is sent as a byte or series of bytes
    • To send the characters representing the digits of a number use the print() function instead.
    • Syntax
Serial.write(val)
Serial.write(str)
Serial.write(buf, len)

CircuitPython Software

  • CircuitPython has several advantages over Arduino IDE
    • Build-test-built cycle time much faster
    • Much easier to do list processing for directory structure
    • Speed not critical in this application
  • Not enough SRAM with SAMD version of the card
    • RP2040 version of the card has plenty of SRAM
    • XAIO RP2040 is $5.40 and ships quickly from US Warehouse

CircuitPython Software Development Environment

import board
dir(board)
['__class__', '__name__', 'A0', 'A1', 'A2', 'A3', 'D0', 'D1', 'D10', 'D2', 'D3', 'D4', 'D5', 'D6', 'D7', 'D8', 'D9', 'I2C', 'LED', 'LED_BLUE', 'LED_GREEN', 'LED_RED', 'MISO', 'MOSI', 'NEOPIXEL', 'NEOPIXEL_POWER', 'RX', 'SCK', 'SCL', 'SDA', 'SPI', 'TX', 'UART', 'board_id']

>>> print(board.board_id)
seeeduino_xiao_rp2040

Install CircuitPython on Seeed XIAO RP2040

  • Long press the "BOOT" button. (The "B" is written on the board shown to the right)
  • Connect the XIAO PR2040 to your computer while you still pressing the button.
  • The computer then will appear a disk driver(RP1-RP2)
  • Download the XIAO-RP2040-CircuitPython.uf2 file for XIAO RP2040
  • Drag the .uf2 file to the disk driver("RP1-RP2")
  • Check the disk drive if the name has changed to "CIRCUITPY" (takes a few seconds)
  • Free memory
>>> import gc
>>> gc.mem_free()
208128
>>> import time
>>> gc.mem_free()
207952

CircuitPython Human Interface Design (HID)

  • MyMenu card
    • 0.91" OLED
    • 3 LEDs
    • 5 pushbuttons
  • i2cscan of attached cards shows OLED at 0x3c and MCP23008 at 0x21
  • Added i2cscan code to find the MCP23008 on the MyMenu card
  • MyMenu demo/test code
    • Read pushbuttons
    • Write OLED
    • Write LEDs
    • Exit by pressing Select button

CircuitPython SD Card

sdcardio.SDCard(bus: busio.SPI, cs: microcontroller.Pin, baudrate: int = 8000000)

Example code

import os
import busio
import board
import sdcardio
import storage

SD_CS = board.D3

sd = sdcardio.SDCard(board.SPI(), board.SD_CS)
vfs = storage.VfsFat(sd)
storage.mount(vfs, '/sd')
os.listdir('/sd')

CircuitPython SD Card Connections

 ** SCLK - CLK - pin 13
 ** MISO - SDI - pin 12
 ** MOSI - SDO - pin 11
 ** SS - CS - depends on your SD card shield or module.

CircuitPython Serial

classbusio.UART(tx: microcontroller.Pin, rx: microcontroller.Pin, *, baudrate: int = 9600, 
  bits: int = 8, parity: Optional[Parity] = None, 
  stop: int = 1, timeout: float = 1, receiver_buffer_size: int = 64)
  • RTS, CTS pins defines on QT Py RP2040
    • RTS = pin D2
    • CTS = pin D3

`

Serial Protocol References