element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • 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 Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • 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
  • Settings
Raspberry Pi
  • Products
  • More
Raspberry Pi
Blog Add external SPI Flash Memory to Raspberry Pico - 5: pass STL containers as read-only parameters
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Raspberry Pi to participate - click to join for free!
Featured Articles
Announcing Pi
Technical Specifications
Raspberry Pi FAQs
Win a Pi
GPIO Pinout
Raspberry Pi Wishlist
Comparison Chart
Quiz
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: Jan Cumps
  • Date Created: 9 Apr 2023 11:21 AM Date Created
  • Views 1285 views
  • Likes 8 likes
  • Comments 7 comments
Related
Recommended
  • pico
  • pico_m25
  • pico_eurocard
  • c++

Add external SPI Flash Memory to Raspberry Pico - 5: pass STL containers as read-only parameters

Jan Cumps
Jan Cumps
9 Apr 2023
Add external SPI Flash Memory to Raspberry Pico - 5: pass STL containers as read-only parameters

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. take care that an STL container is read-only when passing it to a member.

image

Pass an Array as read-only input parameter

C++ can be used as a strict language. One of the strict options is to flag that an object, passed as reference to a method, will / can not be modified inside the method. The object will have the same state after the execution as before the execution.

class m25 {
public:
  // ...
  void write(uint32_t address, const std::vector<uint8_t>& data);

In the write() method, the first parameter is passed by value. That is non-mutable by design.
The second parameter is a reference to a vector object. I pass it by reference, to avoid that a copy has to be made of this object. This is a common c++ construct. But that gives the write() the powers to alter the vector or its content.

When you add const in front of the parameter, it's a sign for the compiler that you can only perform calls on that object that don't alter it. When you try to call a modifier (add or change date, empty the container, ...), the compiler will throw an error.

That's it - a small construct that can help avoid that an object is altered. There's more to const. See from page 66 on in Industrial Strength c++.

image

link to all posts in this series

  • Sign in to reply
  • andrethomas
    andrethomas over 1 year ago in reply to Jan Cumps

    Thanks, appreciated... time to learn some more Slight smile

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Jan Cumps
    Jan Cumps over 1 year ago in reply to andrethomas

    I haven't touched the code since April last year. But this seems to have the write in it:

    image

    /cfs-file/__key/commentfiles/f7d226abd59f475c9d224a79e3f0ec07-dc3ac6aa-86c4-43e7-81af-2d7ea5b9babf/spi_5F00_flash_5F00_20240125.zip

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Jan Cumps
    Jan Cumps over 1 year ago in reply to andrethomas

    Let me check what my own latest version is... It's been a while.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • andrethomas
    andrethomas over 1 year ago

    Hi, do you still have a download link for the read() and write() portions of the library? The last one I could find was 20221203 but that predates the read() thanks!

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Jan Cumps
    Jan Cumps over 2 years ago in reply to Jan Cumps

    This is what happens if I try to mess with the data buffer of the const-qualified vector:

    image

    CMakeFiles/m25.dir/m25.cpp.obj -MF m25\CMakeFiles\m25.dir\m25.cpp.obj.d -o m25/CMakeFiles/m25.dir/m25.cpp.obj -c C:/Users/jancu/Documents/Pico/PicoSDKv1.5.0/workspace_1.5/spi_flash/m25/m25.cpp
    [build] C:/Users/jancu/Documents/Pico/PicoSDKv1.5.0/workspace_1.5/spi_flash/m25/m25.cpp: In member function 'void m25::write(uint32_t, const std::vector<unsigned char>&)':
    [build] C:/Users/jancu/Documents/Pico/PicoSDKv1.5.0/workspace_1.5/spi_flash/m25/m25.cpp:60:11: error: assignment of read-only location '(& data)->std::vector<unsigned char>::operator[](0)'
    [build]    60 |   data[0] = 8;

    • 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 © 2025 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

  • X
  • Facebook
  • linkedin
  • YouTube