/**
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/structs/watchdog.h"
// This app shows the effect of byte and halfword writes on IO registers. Most
// IO registers sample the entire 32 bit write data bus on any write; the
// transfer size and the 2 LSBs of the address are *ignored*.
//
// This can have unintuitive results, especially given the way busmasters
// replicate narrow write data across the entire 32-bit write data bus.
// However, this behaviour can be quite useful if you are aware of it!
int main() {
stdio_init_all();
// We'll use WATCHDOG_SCRATCH0 as a convenient 32 bit read/write register
// that we can assign arbitrary values to
io_rw_32 *scratch32 = &watchdog_hw->scratch[0];
// Alias the scratch register as two halfwords at offsets +0x0 and +0x2
volatile uint16_t *scratch16 = (volatile uint16_t *) scratch32;
// Alias the scratch register as four bytes at offsets +0x0, +0x1, +0x2, +0x3:
volatile uint8_t *scratch8 = (volatile uint8_t *) scratch32;
// Show that we can read/write the scratch register as normal:
printf("Writing 32 bit value\n");
*scratch32 = 0xdeadbeef;
printf("Should be 0xdeadbeef: 0x%08x\n", *scratch32);
// We can do narrow reads just fine -- IO registers treat this as a 32 bit
// read, and the processor/DMA will pick out the correct byte lanes based
// on transfer size and address LSBs
printf("\nReading back 1 byte at a time\n");
// Little-endian!
printf("Should be ef be ad de: %02x ", scratch8[0]);
printf("%02x ", scratch8[1]);
printf("%02x ", scratch8[2]);
printf("%02x\n", scratch8[3]);
// Byte writes are replicated four times across the 32-bit bus, and IO
// registers usually sample the entire write bus.
printf("\nWriting 8 bit value 0xa5 at offset 0\n");
scratch8[0] = 0xa5;
// Read back the whole scratch register in one go
printf("Should be 0xa5a5a5a5: 0x%08x\n", *scratch32);
// The IO register ignores the address LSBs [1:0] as well as the transfer
// size, so it doesn't matter what byte offset we use
printf("\nWriting 8 bit value at offset 1\n");
scratch8[1] = 0x3c;
printf("Should be 0x3c3c3c3c: 0x%08x\n", *scratch32);
// Halfword writes are also replicated across the write data bus
printf("\nWriting 16 bit value at offset 0\n");
scratch16[0] = 0xf00d;
printf("Should be 0xf00df00d: 0x%08x\n", *scratch32);
}
This firmware image was imported from the pico-examples repository.
Copyright 2020 (c) 2020 Raspberry Pi (Trading) Ltd.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Before flashing, please put your Raspberry Pi Pico™ chip in the bootloader mode by keeping the BOOTSEL button pressed while powering up. For more information, please take a look at your board's datasheet.
If you find yourself unable to perform any of the operations, please navigate to the troubleshooting, and the Q&A pages.
The "π Check installed version" button doesn't send your firmware to the Internet. Instead, its algorithm compares the firmware builds' UF2 block hashes safely in your web browser.
The installed firmware version is compared only with all the different versions (builds) of the same project.
Exercise caution while exploring projects of unknown origin. In principle, anyone can publish their firmware here. Projects endorsed by this website will always come with the "submitted by flashmypico" text written just below their title.
A part of your chip's unique RANDID number may be sent to the server if this project's author has enabled the installed version tracking feature. This number will only be logged if the project's author already has it, and has used it to enable this feature for this particular board.