ZuluSCSI_platform.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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_azplatform_name;
  11. #define PLATFORM_NAME "ZuluSCSI RP2040"
  12. #define PLATFORM_REVISION "2.0"
  13. #define PLATFORM_MAX_SCSI_SPEED S2S_CFG_SPEED_SYNC_10
  14. #define PLATFORM_OPTIMAL_MIN_SD_WRITE_SIZE 4096
  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. // NOTE: The driver supports synchronous speeds higher than 10MB/s, but this
  19. // has not been tested due to lack of fast enough SCSI adapter.
  20. // #define PLATFORM_MAX_SCSI_SPEED S2S_CFG_SPEED_TURBO
  21. // Debug logging function, can be used to print to e.g. serial port.
  22. // May get called from interrupt handlers.
  23. void azplatform_log(const char *s);
  24. void azplatform_emergency_log_save();
  25. // Timing and delay functions.
  26. // Arduino platform already provides these
  27. unsigned long millis(void);
  28. void delay(unsigned long ms);
  29. // Short delays, can be called from interrupt mode
  30. static inline void delay_ns(unsigned long ns)
  31. {
  32. delayMicroseconds((ns + 999) / 1000);
  33. }
  34. // Approximate fast delay
  35. static inline void delay_100ns()
  36. {
  37. asm volatile ("nop \n nop \n nop \n nop \n nop");
  38. }
  39. // Initialize SD card and GPIO configuration
  40. void azplatform_init();
  41. // Initialization for main application, not used for bootloader
  42. void azplatform_late_init();
  43. // Setup soft watchdog if supported
  44. void azplatform_reset_watchdog();
  45. // Set callback that will be called during data transfer to/from SD card.
  46. // This can be used to implement simultaneous transfer to SCSI bus.
  47. typedef void (*sd_callback_t)(uint32_t bytes_complete);
  48. void azplatform_set_sd_callback(sd_callback_t func, const uint8_t *buffer);
  49. // Reprogram firmware in main program area.
  50. #ifndef RP2040_DISABLE_BOOTLOADER
  51. #define AZPLATFORM_BOOTLOADER_SIZE (128 * 1024)
  52. #define AZPLATFORM_FLASH_TOTAL_SIZE (1024 * 1024)
  53. #define AZPLATFORM_FLASH_PAGE_SIZE 4096
  54. bool azplatform_rewrite_flash_page(uint32_t offset, uint8_t buffer[AZPLATFORM_FLASH_PAGE_SIZE]);
  55. void azplatform_boot_to_main_firmware();
  56. #endif
  57. // Below are GPIO access definitions that are used from scsiPhy.cpp.
  58. // Write a single SCSI pin.
  59. // Example use: SCSI_OUT(ATN, 1) sets SCSI_ATN to low (active) state.
  60. #define SCSI_OUT(pin, state) \
  61. *(state ? &sio_hw->gpio_clr : &sio_hw->gpio_set) = 1 << (SCSI_OUT_ ## pin)
  62. // Read a single SCSI pin.
  63. // Example use: SCSI_IN(ATN), returns 1 for active low state.
  64. #define SCSI_IN(pin) \
  65. ((sio_hw->gpio_in & (1 << (SCSI_IN_ ## pin))) ? 0 : 1)
  66. // Enable driving of shared control pins
  67. #define SCSI_ENABLE_CONTROL_OUT() \
  68. (sio_hw->gpio_oe_set = (1 << SCSI_OUT_CD) | \
  69. (1 << SCSI_OUT_MSG))
  70. // Set SCSI data bus to output
  71. #define SCSI_ENABLE_DATA_OUT() \
  72. (sio_hw->gpio_clr = (1 << SCSI_DATA_DIR), \
  73. sio_hw->gpio_oe_set = SCSI_IO_DATA_MASK)
  74. // Write SCSI data bus, also sets REQ to inactive.
  75. extern const uint32_t g_scsi_parity_lookup[256];
  76. #define SCSI_OUT_DATA(data) \
  77. gpio_put_masked(SCSI_IO_DATA_MASK | (1 << SCSI_OUT_REQ), \
  78. g_scsi_parity_lookup[(uint8_t)(data)] | (1 << SCSI_OUT_REQ)), \
  79. SCSI_ENABLE_DATA_OUT()
  80. // Release SCSI data bus and REQ signal
  81. #define SCSI_RELEASE_DATA_REQ() \
  82. (sio_hw->gpio_oe_clr = SCSI_IO_DATA_MASK, \
  83. sio_hw->gpio_set = (1 << SCSI_DATA_DIR) | (1 << SCSI_OUT_REQ))
  84. // Release all SCSI outputs
  85. #define SCSI_RELEASE_OUTPUTS() \
  86. SCSI_RELEASE_DATA_REQ(), \
  87. sio_hw->gpio_oe_clr = (1 << SCSI_OUT_CD) | \
  88. (1 << SCSI_OUT_MSG), \
  89. sio_hw->gpio_set = (1 << SCSI_OUT_IO) | \
  90. (1 << SCSI_OUT_CD) | \
  91. (1 << SCSI_OUT_MSG) | \
  92. (1 << SCSI_OUT_RST) | \
  93. (1 << SCSI_OUT_BSY) | \
  94. (1 << SCSI_OUT_REQ) | \
  95. (1 << SCSI_OUT_SEL)
  96. // Read SCSI data bus
  97. #define SCSI_IN_DATA(data) \
  98. (~sio_hw->gpio_in & SCSI_IO_DATA_MASK) >> SCSI_IO_SHIFT
  99. #ifdef __cplusplus
  100. }
  101. // SD card driver for SdFat
  102. #ifdef SD_USE_SDIO
  103. class SdioConfig;
  104. extern SdioConfig g_sd_sdio_config;
  105. #define SD_CONFIG g_sd_sdio_config
  106. #define SD_CONFIG_CRASH g_sd_sdio_config
  107. #else
  108. class SdSpiConfig;
  109. extern SdSpiConfig g_sd_spi_config;
  110. #define SD_CONFIG g_sd_spi_config
  111. #define SD_CONFIG_CRASH g_sd_spi_config
  112. #endif
  113. #endif