praline without the much fixed ram (#3123)

This commit is contained in:
Totoo
2026-04-03 22:17:21 +02:00
committed by GitHub
parent 03131c8a0c
commit 888634ce35
3 changed files with 652 additions and 648 deletions
+1 -1
View File
@@ -636,7 +636,7 @@ init_status_t init() {
// This function returns LD_SUCCESS (0) if the FPGA confirms the bitstream // This function returns LD_SUCCESS (0) if the FPGA confirms the bitstream
// Call fpga_bridge_init and continue boot regardless of result // Call fpga_bridge_init and continue boot regardless of result
// (Watchdog was resetting device when we halted with while(1)) // (Watchdog was resetting device when we halted with while(1))
int load_result = fpga_bridge_init(); int load_result = fpga_bridge_init(&shared_memory.bb_data.data[0]);
(void)load_result; // Ignore result for now, just let boot continue (void)load_result; // Ignore result for now, just let boot continue
/* RELEASE FPGA RESET */ /* RELEASE FPGA RESET */
@@ -135,7 +135,6 @@
#define FPGA_TX_DEFAULT_INTERP 0x00 /* No interpolation */ #define FPGA_TX_DEFAULT_INTERP 0x00 /* No interpolation */
#define FPGA_TX_DEFAULT_PHASE_STEP 0x00 /* Zero phase step */ #define FPGA_TX_DEFAULT_PHASE_STEP 0x00 /* Zero phase step */
// ============================================================================ // ============================================================================
// Static Variables - Declare BEFORE use // Static Variables - Declare BEFORE use
// ============================================================================ // ============================================================================
@@ -148,7 +147,7 @@
const uint8_t* mem_ptr; // Current read position in SPIFI memory const uint8_t* mem_ptr; // Current read position in SPIFI memory
size_t next_block_sz; size_t next_block_sz;
uint8_t init_flag; uint8_t init_flag;
uint8_t buffer[4096 + 2]; // Compressed block + next size uint8_t buffer[4128 + 2]; // Compressed block + next size
}; };
// ============================================================================ // ============================================================================
@@ -171,12 +170,15 @@
// SSP1 transfer one byte // SSP1 transfer one byte
static uint8_t ssp1_transfer_byte(uint8_t data) { static uint8_t ssp1_transfer_byte(uint8_t data) {
// Wait for TX FIFO not full // Wait for TX FIFO not full
while ((SSP1_SR_LOCAL & SSP_SR_TNF_LOCAL) == 0) {} while ((SSP1_SR_LOCAL & SSP_SR_TNF_LOCAL) == 0) {
}
SSP1_DR_LOCAL = data; SSP1_DR_LOCAL = data;
// Wait for not busy // Wait for not busy
while (SSP1_SR_LOCAL & SSP_SR_BSY_LOCAL) {} while (SSP1_SR_LOCAL & SSP_SR_BSY_LOCAL) {
}
// Wait for RX FIFO not empty // Wait for RX FIFO not empty
while ((SSP1_SR_LOCAL & SSP_SR_RNE_LOCAL) == 0) {} while ((SSP1_SR_LOCAL & SSP_SR_RNE_LOCAL) == 0) {
}
return SSP1_DR_LOCAL; return SSP1_DR_LOCAL;
} }
@@ -258,7 +260,6 @@
// Read: Send [reg & 0x7F, 0x00, 0x00] -> value in byte 3 // Read: Send [reg & 0x7F, 0x00, 0x00] -> value in byte 3
// Write: Send [(reg | 0x80), value, 0x00] // Write: Send [(reg | 0x80), value, 0x00]
// ============================================================================ // ============================================================================
// SPI Mode Switching // SPI Mode Switching
// ============================================================================ // ============================================================================
@@ -283,7 +284,6 @@
SSP1_CR1_LOCAL = SSP_CR1_SSE; // Enable SSP1 SSP1_CR1_LOCAL = SSP_CR1_SSE; // Enable SSP1
} }
// ============================================================================ // ============================================================================
// Low-Level SPI Register Access (internal, no mode switch) // Low-Level SPI Register Access (internal, no mode switch)
// ============================================================================ // ============================================================================
@@ -448,7 +448,6 @@
// This is equivalent to fpga_init() in the reference HackRF firmware // This is equivalent to fpga_init() in the reference HackRF firmware
/* fpga_register_init with consistent values */ /* fpga_register_init with consistent values */
static void fpga_register_init(void) { static void fpga_register_init(void) {
/* Boot into RX mode */ /* Boot into RX mode */
current_mode = FPGA_MODE_RX; current_mode = FPGA_MODE_RX;
@@ -485,6 +484,10 @@
// Finish at end marker (block_sz == 0) // Finish at end marker (block_sz == 0)
if (block_sz == 0) return 0; if (block_sz == 0) return 0;
if (block_sz > 4128) {
// Flash corruption detected! Return 0 to abort programming safely.
return 0;
}
// Read compressed block from SPIFI memory // Read compressed block from SPIFI memory
memcpy(ctx->buffer, ctx->mem_ptr, block_sz + 2); memcpy(ctx->buffer, ctx->mem_ptr, block_sz + 2);
@@ -499,7 +502,7 @@
// Program iCE40 FPGA using SPIFI memory-mapped data // Program iCE40 FPGA using SPIFI memory-mapped data
// Based on ice40_spi_syscfg_program() from ice40_spi.c // Based on ice40_spi_syscfg_program() from ice40_spi.c
static bool program_fpga_from_spifi(const uint8_t* bitstream_start) { static bool program_fpga_from_spifi(const uint8_t* bitstream_start, uint8_t* mem_base) {
// Drive CRESET_B = 0, SPI_SS = 0 // Drive CRESET_B = 0, SPI_SS = 0
fpga_creset_low(); fpga_creset_low();
fpga_cs_low(); fpga_cs_low();
@@ -518,17 +521,19 @@
ssp1_transfer_byte(0); ssp1_transfer_byte(0);
// Send configuration image // Send configuration image
// Use static buffers to avoid stack overflow (~8KB would be needed) // Use buffers to avoid stack overflow (~8KB would be needed)
static uint8_t out_buffer[4096]; uint8_t* out_buffer = mem_base;
static struct spifi_fpga_read_ctx ctx; struct spifi_fpga_read_ctx* ctx = (struct spifi_fpga_read_ctx*)(out_buffer + 4096);
ctx.mem_ptr = bitstream_start; memset(ctx, 0, sizeof(struct spifi_fpga_read_ctx));
ctx.next_block_sz = 0; memset(out_buffer, 0, 4096);
ctx.init_flag = 0; ctx->mem_ptr = bitstream_start;
ctx->next_block_sz = 0;
ctx->init_flag = 0;
fpga_cs_low(); fpga_cs_low();
// Full LZ4 decompress and send all bytes // Full LZ4 decompress and send all bytes
for (;;) { for (;;) {
size_t read_sz = spifi_fpga_read_block_cb(&ctx, out_buffer); size_t read_sz = spifi_fpga_read_block_cb(ctx, out_buffer);
if (read_sz == 0) break; if (read_sz == 0) break;
for (size_t j = 0; j < read_sz; j++) { for (size_t j = 0; j < read_sz; j++) {
ssp1_transfer_byte(out_buffer[j]); ssp1_transfer_byte(out_buffer[j]);
@@ -567,7 +572,7 @@
// Main Initialization Entry Point // Main Initialization Entry Point
// ============================================================================ // ============================================================================
int fpga_bridge_init(void) { int fpga_bridge_init(uint8_t* mem_base) {
// Enable SSP1 clock for FPGA programming // Enable SSP1 clock for FPGA programming
// Use PLL1 (204MHz) to match original HackRF - IRC (12MHz) is 17x too slow // Use PLL1 (204MHz) to match original HackRF - IRC (12MHz) is 17x too slow
CGU_BASE_SSP1_CLK = CGU_BASE_SSP1_CLK_AUTOBLOCK(1) | CGU_BASE_SSP1_CLK = CGU_BASE_SSP1_CLK_AUTOBLOCK(1) |
@@ -601,7 +606,7 @@
const uint8_t* bitstream_start = (const uint8_t*)(FPGA_BITSTREAM_MEM_ADDR + bitstream_offset); const uint8_t* bitstream_start = (const uint8_t*)(FPGA_BITSTREAM_MEM_ADDR + bitstream_offset);
// Full FPGA programming // Full FPGA programming
bool success = program_fpga_from_spifi(bitstream_start); bool success = program_fpga_from_spifi(bitstream_start, mem_base);
// Initialize FPGA registers immediately after programming // Initialize FPGA registers immediately after programming
if (success) { if (success) {
@@ -614,7 +619,6 @@
// Now switch to MAX2831 mode // Now switch to MAX2831 mode
ssp1_set_mode_max2831(); ssp1_set_mode_max2831();
} }
return success ? 0 : 2; return success ? 0 : 2;
} }
#else #else
@@ -102,7 +102,7 @@ typedef enum {
/* Initialize the FPGA - loads bitstream from SPIFI flash /* Initialize the FPGA - loads bitstream from SPIFI flash
* Returns: 0 on success, non-zero on failure */ * Returns: 0 on success, non-zero on failure */
int fpga_bridge_init(void); int fpga_bridge_init(uint8_t* mem_base);
/* Set operating mode - MUST be called before using mode-specific functions /* Set operating mode - MUST be called before using mode-specific functions
* This ensures registers 3-5 are interpreted correctly */ * This ensures registers 3-5 are interpreted correctly */