YAHAL
Yet Another Hardware Abstraction Library
Loading...
Searching...
No Matches
bootrom_rp2040.cpp
1//
2// Created by andreas on 01.01.26.
3//
4
5#include "bootrom_rp2040.h"
6#include "RP2040.h"
7
8using namespace _IO_QSPI_;
9using namespace _XIP_SSI_;
10
11// Static members
12bootrom_rp2040::rom_header_t * bootrom_rp2040::_rom_header {0x00000000};
13uint8_t bootrom_rp2040::_rx_buffer[13] {0};
14uint8_t bootrom_rp2040::_tx_buffer[13] {0};
15uint32_t bootrom_rp2040::_boot2_copy[64];
16
17std::array<uint8_t, 8> COPY_TO_RAM bootrom_rp2040::read_unique_id() {
18
19 // Copy the boot2 loader from flash to ram
20 for(size_t i=0; i < 64; ++i) {
21 _boot2_copy[i] = *(((uint32_t *)0x10000000) + i);
22 }
23
24 // Memory barrier
25 asm volatile("" : : : "memory");
26
27 bootrom_rp2040::connect_internal_flash();
28 bootrom_rp2040::flash_exit_xip();
29
30 // Activate CS line
31 IO_QSPI.GPIO_QSPI_SS_CTRL.OUTOVER = GPIO_QSPI_SS_CTRL_OUTOVER__LOW;
32
33 for (int i=0; i < 13; ++i) _rx_buffer[i] = _tx_buffer[i] = 0;
34 _tx_buffer[0] = 0x4b;
35 size_t tx_idx = 0;
36 size_t rx_idx = 0;
37 while (tx_idx < 13 || rx_idx < 13) {
38 if (XIP_SSI.SR.TFNF) {
39 XIP_SSI.DR0 = _tx_buffer[tx_idx++];
40 }
41 if (XIP_SSI.SR.RFNE) {
42 _rx_buffer[rx_idx++] = XIP_SSI.DR0;
43 }
44 }
45 // Deactivate CS line
46 IO_QSPI.GPIO_QSPI_SS_CTRL.OUTOVER = GPIO_QSPI_SS_CTRL_OUTOVER__HIGH;
47
48 bootrom_rp2040::flash_flush_cache();
49 //bootrom_rp2040::flash_enter_cmd_xip();
50 ((void (*)(void))((intptr_t)_boot2_copy+1))();
51
52 // Copy the last 8 bytes from the receive-buffer
53 // 1 byte cmd + 4 bytes dummy + 8 bytes serial ID
54 std::array<uint8_t, 8> arr {0};
55 for(int i=5; i < 13; ++i) arr[i-5] = _rx_buffer[i];
56 return arr;
57}
58
59std::array<char, 17> bootrom_rp2040::read_unique_id_string() {
60 auto id = read_unique_id();
61 std::array<char, 17> res {0};
62 size_t res_index = 0;
63 for(uint8_t & c : id) {
64 uint8_t n = (c >> 4);
65 res[res_index++] = n > 9 ? n+'A'-10 : n+'0';
66 n = c & 0xf;
67 res[res_index++] = n > 9 ? n+'A'-10 : n+'0';
68 }
69 return res;
70}
CMSIS-Core(M) Device Peripheral Access Layer Header File for Device RP2040.