-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add Wi-Fi firmware partition support for Pico 2 W #1969
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
800cb52
462148f
2bb0f11
75d119e
2cb5d14
d690e3e
78f1b16
4394654
c1bdf1b
caf5492
918408a
0fa63dd
37ee6fa
f20b302
643a925
dff70a9
1802cb9
d68e3ed
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,18 @@ | |
#define CYW43_SLEEP_CHECK_MS 50 | ||
#endif | ||
|
||
static async_context_t *cyw43_async_context; | ||
static async_context_t *cyw43_async_context = NULL; | ||
|
||
#if CYW43_USE_FIRMWARE_PARTITION | ||
#include "pico/bootrom.h" | ||
#include "hardware/flash.h" | ||
#include "boot/picobin.h" | ||
#include <stdlib.h> | ||
|
||
int32_t cyw43_wifi_fw_len; | ||
int32_t cyw43_clm_len; | ||
uintptr_t fw_data; | ||
#endif | ||
|
||
static void cyw43_sleep_timeout_reached(async_context_t *context, async_at_time_worker_t *worker); | ||
static void cyw43_do_poll(async_context_t *context, async_when_pending_worker_t *worker); | ||
|
@@ -104,6 +115,87 @@ static void cyw43_sleep_timeout_reached(async_context_t *context, __unused async | |
} | ||
|
||
bool cyw43_driver_init(async_context_t *context) { | ||
#if CYW43_USE_FIRMWARE_PARTITION | ||
uint32_t buffer[(16 * 4) + 1] = {}; // maximum of 16 partitions, each with maximum of 4 words returned, plus 1 | ||
int ret = rom_get_partition_table_info(buffer, count_of(buffer), PT_INFO_PARTITION_LOCATION_AND_FLAGS | PT_INFO_PARTITION_ID); | ||
|
||
assert(buffer[0] == (PT_INFO_PARTITION_LOCATION_AND_FLAGS | PT_INFO_PARTITION_ID)); | ||
|
||
if (ret > 0) { | ||
int i = 1; | ||
int p = 0; | ||
int picked_p = -1; | ||
while (i < ret) { | ||
i++; | ||
uint32_t flags_and_permissions = buffer[i++]; | ||
bool has_id = (flags_and_permissions & PICOBIN_PARTITION_FLAGS_HAS_ID_BITS); | ||
if (has_id) { | ||
uint64_t id = 0; | ||
id |= buffer[i++]; | ||
id |= ((uint64_t)(buffer[i++]) << 32ull); | ||
if (id == CYW43_FIRMWARE_PARTITION_ID) { | ||
picked_p = p; | ||
kilograham marked this conversation as resolved.
Show resolved
Hide resolved
|
||
break; | ||
} | ||
} | ||
|
||
p++; | ||
} | ||
|
||
if (picked_p >= 0) { | ||
#ifdef __riscv | ||
// Increased bootrom stack is required for this function | ||
bootrom_stack_t stack = { | ||
.base = malloc(0x400), | ||
.size = 0x400 | ||
}; | ||
rom_set_bootrom_stack(&stack); | ||
#endif | ||
uint32_t* workarea = malloc(0x1000); | ||
peterharperuk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
picked_p = rom_pick_ab_update_partition(workarea, 0x1000, picked_p); | ||
free(workarea); | ||
#ifdef __riscv | ||
// Reset bootrom stack | ||
rom_set_bootrom_stack(&stack); | ||
free(stack.base); | ||
#endif | ||
|
||
if (picked_p < 0) { | ||
if (picked_p == BOOTROM_ERROR_NOT_FOUND) { | ||
CYW43_DEBUG("Chosen CYW43 firmware partition was not verified\n"); | ||
} else if (picked_p == BOOTROM_ERROR_NOT_PERMITTED) { | ||
CYW43_DEBUG("Too many update boots going on at once\n"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not sure what this error message means! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It means there are multiple partitions being updated at once, so if you update the boot partition with a TBYB image and the firmware partition in the same boot you get this error. I can't actually think of a scenario when this might happen, because you can only have one |
||
} | ||
return false; | ||
} | ||
|
||
CYW43_DEBUG("Chosen CYW43 firmware in partition %d\n", picked_p); | ||
int ret = rom_get_partition_table_info(buffer, count_of(buffer), PT_INFO_PARTITION_LOCATION_AND_FLAGS | PT_INFO_SINGLE_PARTITION | (picked_p << 24)); | ||
assert(buffer[0] == (PT_INFO_PARTITION_LOCATION_AND_FLAGS | PT_INFO_SINGLE_PARTITION)); | ||
assert(ret == 3); | ||
|
||
uint32_t location_and_permissions = buffer[1]; | ||
uint32_t saddr = ((location_and_permissions >> PICOBIN_PARTITION_LOCATION_FIRST_SECTOR_LSB) & 0x1fffu) * FLASH_SECTOR_SIZE; | ||
uint32_t eaddr = (((location_and_permissions >> PICOBIN_PARTITION_LOCATION_LAST_SECTOR_LSB) & 0x1fffu) + 1) * FLASH_SECTOR_SIZE; | ||
// Starts with metadata block | ||
while(saddr < eaddr && *(uint32_t*)(XIP_NOCACHE_NOALLOC_NOTRANSLATE_BASE + saddr) != PICOBIN_BLOCK_MARKER_END) { | ||
saddr += 4; | ||
} | ||
saddr += 4; | ||
// Now onto data | ||
cyw43_wifi_fw_len = *(uint32_t*)(XIP_NOCACHE_NOALLOC_NOTRANSLATE_BASE + saddr); | ||
cyw43_clm_len = *(uint32_t*)(XIP_NOCACHE_NOALLOC_NOTRANSLATE_BASE + saddr + 4); | ||
fw_data = XIP_NOCACHE_NOALLOC_NOTRANSLATE_BASE + saddr + 8; | ||
} else { | ||
CYW43_DEBUG("No CYW43 firmware partition found, so cannot get firmware from partition\n"); | ||
return false; | ||
} | ||
} else { | ||
CYW43_DEBUG("No partition table, so cannot get firmware from partition - get_partition_table_info returned %d\n", ret); | ||
return false; | ||
} | ||
|
||
#endif | ||
cyw43_init(&cyw43_state); | ||
cyw43_async_context = context; | ||
// we need the IRQ to be on the same core as the context, because we need to be able to enable/disable the IRQ | ||
|
@@ -114,13 +206,15 @@ bool cyw43_driver_init(async_context_t *context) { | |
} | ||
|
||
void cyw43_driver_deinit(async_context_t *context) { | ||
assert(context == cyw43_async_context); | ||
async_context_remove_at_time_worker(context, &sleep_timeout_worker); | ||
async_context_remove_when_pending_worker(context, &cyw43_poll_worker); | ||
// the IRQ IS on the same core as the context, so must be de-initialized there | ||
async_context_execute_sync(context, cyw43_irq_deinit, NULL); | ||
cyw43_deinit(&cyw43_state); | ||
cyw43_async_context = NULL; | ||
if (cyw43_async_context != NULL) { | ||
assert(context == cyw43_async_context); | ||
async_context_remove_at_time_worker(context, &sleep_timeout_worker); | ||
async_context_remove_when_pending_worker(context, &cyw43_poll_worker); | ||
// the IRQ IS on the same core as the context, so must be de-initialized there | ||
async_context_execute_sync(context, cyw43_irq_deinit, NULL); | ||
cyw43_deinit(&cyw43_state); | ||
cyw43_async_context = NULL; | ||
} | ||
} | ||
|
||
// todo maybe add an #ifdef in cyw43_driver | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#!/usr/bin/env python3 | ||
# | ||
# Copyright (c) 2024 Raspberry Pi (Trading) Ltd. | ||
# | ||
# SPDX-License-Identifier: BSD-3-Clause | ||
|
||
import sys | ||
|
||
with open(sys.argv[1], "r") as f: | ||
data = f.read() | ||
lines = data.split(";") | ||
for line in lines[1].split("\n"): | ||
if "#define CYW43_WIFI_FW_LEN" in line: | ||
cyw43_wifi_fw_len = int(line.split(")")[0].split("(")[-1]) | ||
if "#define CYW43_CLM_LEN" in line: | ||
cyw43_clm_len = int(line.split(")")[0].split("(")[-1]) | ||
data = lines[0] | ||
bits = data.split(",") | ||
bits[0] = bits[0].split("{")[-1] | ||
bits[-1] = bits[-1].split("}")[0] | ||
for i in range(len(bits)): | ||
bits[i] = bits[i].strip() | ||
bits[i] = bits[i].strip(',') | ||
bits[i] = int(bits[i], base=0) | ||
print(f"Start {bits[4]}, end {bits[-1]}, num {len(bits)}") | ||
print(bits[:10]) | ||
|
||
print(f"Wifi {cyw43_wifi_fw_len}, clm {cyw43_clm_len}") | ||
|
||
data = ( | ||
cyw43_wifi_fw_len.to_bytes(4, 'little', signed=True) + | ||
cyw43_clm_len.to_bytes(4, 'little', signed=True) + | ||
bytearray(bits) | ||
) | ||
|
||
with open(sys.argv[2], "w") as f: | ||
for b in data: | ||
f.write(f".byte 0x{b:02x}\n") |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/* | ||
* Copyright (c) 2024 Raspberry Pi (Trading) Ltd. | ||
* | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
*/ | ||
|
||
extern int cyw43_wifi_fw_len; | ||
extern int cyw43_clm_len; | ||
|
||
#define CYW43_WIFI_FW_LEN (cyw43_wifi_fw_len) | ||
#define CYW43_CLM_LEN (cyw43_clm_len) | ||
extern uintptr_t fw_data; | ||
|
||
#include "boot/picobin.h" | ||
#include "pico/bootrom.h" |
Uh oh!
There was an error while loading. Please reload this page.