BlueSCSI_platform.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // Platform-specific definitions for BlueSCSI RP2040 hardware.
  2. #pragma once
  3. #include <stdint.h>
  4. #include <Arduino.h>
  5. #include "BlueSCSI_platform_gpio.h"
  6. #include "scsiHostPhy.h"
  7. #ifdef __cplusplus
  8. extern "C" {
  9. #endif
  10. /* These are used in debug output and default SCSI strings */
  11. extern const char *g_bluescsiplatform_name;
  12. #define PLATFORM_NAME "BlueSCSI RP2040"
  13. #define PLATFORM_REVISION "2.0"
  14. #define PLATFORM_MAX_SCSI_SPEED S2S_CFG_SPEED_SYNC_10
  15. #define PLATFORM_OPTIMAL_MIN_SD_WRITE_SIZE 4096
  16. #define PLATFORM_OPTIMAL_MAX_SD_WRITE_SIZE 32768
  17. #define PLATFORM_OPTIMAL_LAST_SD_WRITE_SIZE 8192
  18. #define SD_USE_SDIO 1
  19. #define PLATFORM_HAS_INITIATOR_MODE 1
  20. // NOTE: The driver supports synchronous speeds higher than 10MB/s, but this
  21. // has not been tested due to lack of fast enough SCSI adapter.
  22. // #define PLATFORM_MAX_SCSI_SPEED S2S_CFG_SPEED_TURBO
  23. // Debug logging function, can be used to print to e.g. serial port.
  24. // May get called from interrupt handlers.
  25. void bluescsiplatform_log(const char *s);
  26. void bluescsiplatform_emergency_log_save();
  27. // Timing and delay functions.
  28. // Arduino platform already provides these
  29. unsigned long millis(void);
  30. void delay(unsigned long ms);
  31. // Short delays, can be called from interrupt mode
  32. static inline void delay_ns(unsigned long ns)
  33. {
  34. delayMicroseconds((ns + 999) / 1000);
  35. }
  36. // Approximate fast delay
  37. static inline void delay_100ns()
  38. {
  39. asm volatile ("nop \n nop \n nop \n nop \n nop \n nop \n nop \n nop \n nop \n nop \n nop");
  40. }
  41. // Initialize SD card and GPIO configuration
  42. void bluescsiplatform_init();
  43. // Initialization for main application, not used for bootloader
  44. void bluescsiplatform_late_init();
  45. // Query whether initiator mode is enabled on targets with PLATFORM_HAS_INITIATOR_MODE
  46. bool bluescsiplatform_is_initiator_mode_enabled();
  47. // Setup soft watchdog if supported
  48. void bluescsiplatform_reset_watchdog();
  49. // Set callback that will be called during data transfer to/from SD card.
  50. // This can be used to implement simultaneous transfer to SCSI bus.
  51. typedef void (*sd_callback_t)(uint32_t bytes_complete);
  52. void bluescsiplatform_set_sd_callback(sd_callback_t func, const uint8_t *buffer);
  53. // Reprogram firmware in main program area.
  54. #ifndef RP2040_DISABLE_BOOTLOADER
  55. #define BLUESCSIPLATFORM_BOOTLOADER_SIZE (128 * 1024)
  56. #define BLUESCSIPLATFORM_FLASH_TOTAL_SIZE (1024 * 1024)
  57. #define BLUESCSIPLATFORM_FLASH_PAGE_SIZE 4096
  58. bool bluescsiplatform_rewrite_flash_page(uint32_t offset, uint8_t buffer[BLUESCSIPLATFORM_FLASH_PAGE_SIZE]);
  59. void bluescsiplatform_boot_to_main_firmware();
  60. #endif
  61. // Below are GPIO access definitions that are used from scsiPhy.cpp.
  62. // Write a single SCSI pin.
  63. // Example use: SCSI_OUT(ATN, 1) sets SCSI_ATN to low (active) state.
  64. #define SCSI_OUT(pin, state) \
  65. *(state ? &sio_hw->gpio_clr : &sio_hw->gpio_set) = 1 << (SCSI_OUT_ ## pin)
  66. // Read a single SCSI pin.
  67. // Example use: SCSI_IN(ATN), returns 1 for active low state.
  68. #define SCSI_IN(pin) \
  69. ((sio_hw->gpio_in & (1 << (SCSI_IN_ ## pin))) ? 0 : 1)
  70. // Set pin directions for initiator vs. target mode
  71. #define SCSI_ENABLE_INITIATOR() \
  72. (sio_hw->gpio_oe_set = (1 << SCSI_OUT_ACK) | \
  73. (1 << SCSI_OUT_ATN)), \
  74. (sio_hw->gpio_oe_clr = (1 << SCSI_IN_IO) | \
  75. (1 << SCSI_IN_CD) | \
  76. (1 << SCSI_IN_MSG) | \
  77. (1 << SCSI_IN_REQ))
  78. // Enable driving of shared control pins
  79. #define SCSI_ENABLE_CONTROL_OUT() \
  80. (sio_hw->gpio_oe_set = (1 << SCSI_OUT_CD) | \
  81. (1 << SCSI_OUT_MSG))
  82. // Set SCSI data bus to output
  83. #define SCSI_ENABLE_DATA_OUT() \
  84. (sio_hw->gpio_set = (1 << SCSI_DATA_DIR), \
  85. sio_hw->gpio_oe_set = SCSI_IO_DATA_MASK)
  86. // Write SCSI data bus, also sets REQ to inactive.
  87. extern const uint32_t g_scsi_parity_lookup[256];
  88. #define SCSI_OUT_DATA(data) \
  89. gpio_put_masked(SCSI_IO_DATA_MASK | (1 << SCSI_OUT_REQ), \
  90. g_scsi_parity_lookup[(uint8_t)(data)] | (1 << SCSI_OUT_REQ)), \
  91. SCSI_ENABLE_DATA_OUT()
  92. // Release SCSI data bus and REQ signal
  93. #define SCSI_RELEASE_DATA_REQ() \
  94. (sio_hw->gpio_oe_clr = SCSI_IO_DATA_MASK, \
  95. sio_hw->gpio_clr = (1 << SCSI_DATA_DIR), \
  96. sio_hw->gpio_set = ((1 << SCSI_OUT_REQ)))
  97. // Release all SCSI outputs
  98. #define SCSI_RELEASE_OUTPUTS() \
  99. SCSI_RELEASE_DATA_REQ(), \
  100. sio_hw->gpio_set = (1 << SCSI_OUT_IO) | \
  101. (1 << SCSI_OUT_CD) | \
  102. (1 << SCSI_OUT_MSG) | \
  103. (1 << SCSI_OUT_RST) | \
  104. (1 << SCSI_OUT_BSY) | \
  105. (1 << SCSI_OUT_REQ) | \
  106. (1 << SCSI_OUT_SEL), \
  107. delay(1), \
  108. sio_hw->gpio_oe_clr = (1 << SCSI_OUT_CD) | \
  109. (1 << SCSI_OUT_MSG)
  110. // Read SCSI data bus
  111. #define SCSI_IN_DATA() \
  112. (~sio_hw->gpio_in & SCSI_IO_DATA_MASK) >> SCSI_IO_SHIFT
  113. #ifdef __cplusplus
  114. }
  115. // SD card driver for SdFat
  116. #ifdef SD_USE_SDIO
  117. class SdioConfig;
  118. extern SdioConfig g_sd_sdio_config;
  119. #define SD_CONFIG g_sd_sdio_config
  120. #define SD_CONFIG_CRASH g_sd_sdio_config
  121. #else
  122. class SdSpiConfig;
  123. extern SdSpiConfig g_sd_spi_config;
  124. #define SD_CONFIG g_sd_spi_config
  125. #define SD_CONFIG_CRASH g_sd_spi_config
  126. #endif
  127. #endif