rp2040_flash_do_cmd.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // As of 2022-11-16, the raspberrypi platformio package ships with old version
  2. // of pico-sdk. This version lacks the flash_do_cmd() function in flash.h header.
  3. // This file backports the function from new versions.
  4. #pragma once
  5. #ifndef RP2040_HAS_FLASH_DO_CMD
  6. #include <hardware/flash.h>
  7. #include "pico/bootrom.h"
  8. #include "hardware/structs/ssi.h"
  9. #include "hardware/structs/ioqspi.h"
  10. #define BOOT2_SIZE_WORDS 64
  11. static uint32_t boot2_copyout[BOOT2_SIZE_WORDS];
  12. static bool boot2_copyout_valid = false;
  13. static void __no_inline_not_in_flash_func(flash_init_boot2_copyout)(void) {
  14. if (boot2_copyout_valid)
  15. return;
  16. for (int i = 0; i < BOOT2_SIZE_WORDS; ++i)
  17. boot2_copyout[i] = ((uint32_t *)XIP_BASE)[i];
  18. __asm__ volatile ("" : : : "memory");
  19. boot2_copyout_valid = true;
  20. }
  21. static void __no_inline_not_in_flash_func(flash_enable_xip_via_boot2)(void) {
  22. ((void (*)(void))((char*)boot2_copyout+1))();
  23. }
  24. // Bitbanging the chip select using IO overrides, in case RAM-resident IRQs
  25. // are still running, and the FIFO bottoms out. (the bootrom does the same)
  26. static void __no_inline_not_in_flash_func(flash_cs_force)(bool high) {
  27. uint32_t field_val = high ?
  28. IO_QSPI_GPIO_QSPI_SS_CTRL_OUTOVER_VALUE_HIGH :
  29. IO_QSPI_GPIO_QSPI_SS_CTRL_OUTOVER_VALUE_LOW;
  30. hw_write_masked(&ioqspi_hw->io[1].ctrl,
  31. field_val << IO_QSPI_GPIO_QSPI_SS_CTRL_OUTOVER_LSB,
  32. IO_QSPI_GPIO_QSPI_SS_CTRL_OUTOVER_BITS
  33. );
  34. }
  35. static void __no_inline_not_in_flash_func(flash_do_cmd)(const uint8_t *txbuf, uint8_t *rxbuf, size_t count) {
  36. void (*connect_internal_flash)(void) = (void(*)(void))rom_func_lookup(rom_table_code('I', 'F'));
  37. void (*flash_exit_xip)(void) = (void(*)(void))rom_func_lookup(rom_table_code('E', 'X'));
  38. void (*flash_flush_cache)(void) = (void(*)(void))rom_func_lookup(rom_table_code('F', 'C'));
  39. assert(connect_internal_flash && flash_exit_xip && flash_flush_cache);
  40. flash_init_boot2_copyout();
  41. __asm__ volatile ("" : : : "memory");
  42. connect_internal_flash();
  43. flash_exit_xip();
  44. flash_cs_force(0);
  45. size_t tx_remaining = count;
  46. size_t rx_remaining = count;
  47. // We may be interrupted -- don't want FIFO to overflow if we're distracted.
  48. const size_t max_in_flight = 16 - 2;
  49. while (tx_remaining || rx_remaining) {
  50. uint32_t flags = ssi_hw->sr;
  51. bool can_put = !!(flags & SSI_SR_TFNF_BITS);
  52. bool can_get = !!(flags & SSI_SR_RFNE_BITS);
  53. if (can_put && tx_remaining && rx_remaining - tx_remaining < max_in_flight) {
  54. ssi_hw->dr0 = *txbuf++;
  55. --tx_remaining;
  56. }
  57. if (can_get && rx_remaining) {
  58. *rxbuf++ = (uint8_t)ssi_hw->dr0;
  59. --rx_remaining;
  60. }
  61. }
  62. flash_cs_force(1);
  63. flash_flush_cache();
  64. flash_enable_xip_via_boot2();
  65. }
  66. #endif