ZuluSCSI_platform.h 5.7 KB

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