element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Members
    Members
    • Benefits of Membership
    • Achievement Levels
    • Members Area
    • Personal Blogs
    • Feedback and Support
    • What's New on element14
  • Learn
    Learn
    • Learning Center
    • eBooks
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Dev Tools
    • Manufacturers
    • Raspberry Pi
    • RoadTests & Reviews
    • Avnet Boards Community
    • Product Groups
  • Store
    Store
    • Visit Your Store
    • Choose Another Store
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
Raspberry Pi
  • Products
  • More
Raspberry Pi
Blog Add external SPI Flash Memory to Raspberry Pico - 4: STL containers to return byte arrays
  • Blog
  • Forum
  • Documents
  • Events
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Raspberry Pi requires membership for participation - click to join
Blog Post Actions
  • Subscribe by email
  • More
  • Cancel
  • Share
  • Subscribe by email
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: Jan Cumps
  • Date Created: 3 Dec 2022 12:47 PM Date Created
  • Views 148 views
  • Likes 7 likes
  • Comments 10 comments
Related
Recommended
  • pico
  • pico_m25
  • pico_eurocard

Add external SPI Flash Memory to Raspberry Pico - 4: STL containers to return byte arrays

Jan Cumps
Jan Cumps
3 Dec 2022
Add external SPI Flash Memory to Raspberry Pico - 4: STL containers to return byte arrays

A projects to learn the SPI API of the RP2040 C SDK, and to use it to control an external Flash IC. In this post, I refine the m25 c++ class again. Return byte arrays as Standard Template Library (stl) containersimage

Arrays as return values instead of output parameters

Usually, when a function returns multiple values, we pass it a buffer as one of the parameters, and the function can fill it. There are other options, but this is often how we see it done.

In c++ you can return a container as a return value. You can construct it in the function - right size and value, then hand it over to the calling function. Here's a function that reads 3 bytes from the m25 IC over SPI, then hands over the results an stl vector:

std::vector<uint8_t> m25::rdid() {
    uint8_t buf[3];
    // command
    uint8_t cmdbuf[] = {
            RDID,
    };
    setDataBits(8);
    select();
    spi_write_blocking(spi, cmdbuf, 1);
    spi_read_blocking(spi, 0, buf, 1); // Manufacturer Identification
    spi_read_blocking(spi, 0, buf + sizeof buf[0], 2); // Device Identification (Memory Type || Memory Capacity)
    deselect();
    return std::vector<uint8_t> (buf, buf + (sizeof buf/sizeof buf[0]));
}

The calling program can use it like this:

   // get and print read identifier
    bytes = eeprom->rdid();
    std::cout << "read identifier: ";
    for ( auto bte: bytes ) std::cout << std::hex << static_cast<int>(bte) << " ";
    std::cout << std::endl;

Result:

read identifier: 20 20 15 

VSCode project attached: spi_flash_20221203.zip

link to all posts in this series

  • Sign in to reply

Top Comments

  • Jan Cumps
    Jan Cumps 1 month ago +1
    For the latest language features, tell the compiler that you want to use standard c++ 20: project(spi_flash C CXX ASM) set(CMAKE_C_STANDARD 11) set(CMAKE_CXX_STANDARD 20)
  • Jan Cumps
    Jan Cumps 27 days ago

    I found some time to continue this project. I'll use it to create a flash write and read method.
    The goal is that, for a developer, the code will look like this:

        // test a read and write cycle
        uint32_t address = 0x12345678;         // random address
        bytes = {0x01, 0x02, 0x03, 0x04};      // random data vector
        size_t length = bytes.size();          // data size
        
        eeprom->write(address, bytes);         // write the data
        bytes = eeprom->read(address, length); // read the data back
        
        for ( auto bte: bytes ) std::cout << std::hex << static_cast<int>(bte) << " ";
        std::cout << std::endl;

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Jan Cumps
    Jan Cumps 1 month ago

    There's a good Arduino library for this IC: https://github.com/Marzogh/SPIMemory

    It's not restricted to the Flash memory that I'm using here. It supports several SPI Flash & FRAM Memory manufacturers.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Jan Cumps
    Jan Cumps 1 month ago

    The price for using STL vectors on the Pico is an almost 600 KB increase in program size, in release build mode. in debug build, it's 600KB. This is significant in a 2MB flash device. 
    I removed any other code line that used a  c++ dependency before measuring, like streaming to cout.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Jan Cumps
    Jan Cumps 1 month ago in reply to Jan Cumps

    (typed Arrow double up on a tablet and it shows :) )

    static cast does not cause a performance hit. It's evaluated at compile time and does not generate any code or increase/change variable sizes. 

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Jan Cumps
    Jan Cumps 1 month ago in reply to shabaz

    The conversion ti int is only used for a byte variable. Becuase an output stream , by default, treats is as an ascii character. The cast is to tell thecompiler that we are dealing with a number, not a character.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
>
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2023 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • Facebook
  • Twitter
  • linkedin
  • YouTube