BlueSCSI_platform.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // Platform-specific definitions for BlueSCSI Pico 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_platform_name;
  12. #define PLATFORM_NAME "BlueSCSI"
  13. #define PLATFORM_REVISION "2.0"
  14. #define PLATFORM_TOOLBOX_API 0
  15. #define PLATFORM_INQUIRY PLATFORM_NAME "v" FW_VER_NUM
  16. #define PLATFORM_MAX_SCSI_SPEED S2S_CFG_SPEED_SYNC_10
  17. #define PLATFORM_OPTIMAL_MIN_SD_WRITE_SIZE 32768
  18. #define PLATFORM_OPTIMAL_MAX_SD_WRITE_SIZE 65536
  19. #define PLATFORM_OPTIMAL_LAST_SD_WRITE_SIZE 8192
  20. #define SD_USE_SDIO 1
  21. #define PLATFORM_HAS_INITIATOR_MODE 1
  22. #ifndef PLATFORM_VDD_WARNING_LIMIT_mV
  23. #define PLATFORM_VDD_WARNING_LIMIT_mV 2800
  24. #endif
  25. extern SCSI_PINS scsi_pins;
  26. // NOTE: The driver supports synchronous speeds higher than 10MB/s, but this
  27. // has not been tested due to lack of fast enough SCSI adapter.
  28. // #define PLATFORM_MAX_SCSI_SPEED S2S_CFG_SPEED_TURBO
  29. // Debug logging function, can be used to print to e.g. serial port.
  30. // May get called from interrupt handlers.
  31. void platform_log(const char *s);
  32. void platform_emergency_log_save();
  33. // Timing and delay functions.
  34. // Arduino platform already provides these
  35. unsigned long millis(void);
  36. void delay(unsigned long ms);
  37. // Short delays, can be called from interrupt mode
  38. static inline void delay_ns(unsigned long ns)
  39. {
  40. delayMicroseconds((ns + 999) / 1000);
  41. }
  42. // Approximate fast delay
  43. static inline void delay_100ns()
  44. {
  45. asm volatile ("nop \n nop \n nop \n nop \n nop \n nop \n nop \n nop \n nop \n nop \n nop");
  46. }
  47. // Initialize SD card and GPIO configuration
  48. void platform_init();
  49. // Initialization for main application, not used for bootloader
  50. void platform_late_init();
  51. // Disable the status LED
  52. void platform_disable_led(void);
  53. // Enables initiator mode
  54. void platform_enable_initiator_mode();
  55. // Query whether initiator mode is enabled on targets with PLATFORM_HAS_INITIATOR_MODE
  56. bool platform_is_initiator_mode_enabled();
  57. // Setup soft watchdog if supported
  58. void platform_reset_watchdog();
  59. // Poll function that is called every few milliseconds.
  60. // The SD card is free to access during this time, and pauses up to
  61. // few milliseconds shouldn't disturb SCSI communication.
  62. void platform_poll();
  63. // Returns the state of any platform-specific buttons.
  64. // The returned value should be a mask for buttons 1-8 in bits 0-7 respectively,
  65. // where '1' is a button pressed and '0' is a button released.
  66. // Debouncing logic is left up to the specific implementation.
  67. // This function should return without significantly delay.
  68. uint8_t platform_get_buttons();
  69. // Platform method to determine whether this is a certain hardware version
  70. bool is202309a();
  71. // Set callback that will be called during data transfer to/from SD card.
  72. // This can be used to implement simultaneous transfer to SCSI bus.
  73. typedef void (*sd_callback_t)(uint32_t bytes_complete);
  74. void platform_set_sd_callback(sd_callback_t func, const uint8_t *buffer);
  75. void add_extra_sdio_delay(uint16_t additional_delay);
  76. void set_sdio_drive_strength(long ini_setting);
  77. // Reprogram firmware in main program area.
  78. #ifndef RP2040_DISABLE_BOOTLOADER
  79. #define PLATFORM_BOOTLOADER_SIZE (128 * 1024)
  80. #define PLATFORM_FLASH_TOTAL_SIZE (1024 * 1024)
  81. #define PLATFORM_FLASH_PAGE_SIZE 4096
  82. bool platform_rewrite_flash_page(uint32_t offset, uint8_t buffer[PLATFORM_FLASH_PAGE_SIZE]);
  83. void platform_boot_to_main_firmware();
  84. #endif
  85. // ROM drive in the unused external flash area
  86. #ifndef RP2040_DISABLE_ROMDRIVE
  87. #define PLATFORM_HAS_ROM_DRIVE 1
  88. // Check maximum available space for ROM drive in bytes
  89. uint32_t platform_get_romdrive_maxsize();
  90. // Read ROM drive area
  91. bool platform_read_romdrive(uint8_t *dest, uint32_t start, uint32_t count);
  92. // Reprogram ROM drive area
  93. #define PLATFORM_ROMDRIVE_PAGE_SIZE 4096
  94. bool platform_write_romdrive(const uint8_t *data, uint32_t start, uint32_t count);
  95. #endif
  96. // Parity lookup tables for write and read from SCSI bus.
  97. // These are used by macros below and the code in scsi_accel_rp2040.cpp
  98. extern const uint16_t g_scsi_parity_lookup[256];
  99. extern const uint16_t g_scsi_parity_check_lookup[512];
  100. // Network functions
  101. bool platform_network_supported();
  102. void platform_network_poll();
  103. int platform_network_init(char *mac);
  104. void platform_network_add_multicast_address(uint8_t *mac);
  105. bool platform_network_wifi_join(char *ssid, char *password);
  106. int platform_network_wifi_start_scan();
  107. int platform_network_wifi_scan_finished();
  108. void platform_network_wifi_dump_scan_list();
  109. int platform_network_wifi_rssi();
  110. char * platform_network_wifi_ssid();
  111. char * platform_network_wifi_bssid();
  112. int platform_network_wifi_channel();
  113. // Below are GPIO access definitions that are used from scsiPhy.cpp.
  114. // Write a single SCSI pin.
  115. // Example use: SCSI_OUT(ATN, 1) sets SCSI_ATN to low (active) state.
  116. #define SCSI_OUT(pin, state) \
  117. *(state ? &sio_hw->gpio_clr : &sio_hw->gpio_set) = 1 << (scsi_pins.OUT_ ## pin)
  118. // Read a single SCSI pin.
  119. // Example use: SCSI_IN(ATN), returns 1 for active low state.
  120. #define SCSI_IN(pin) \
  121. ((sio_hw->gpio_in & (1 << (scsi_pins.IN_ ## pin))) ? 0 : 1)
  122. // Set pin directions for initiator vs. target mode
  123. #define SCSI_ENABLE_INITIATOR() \
  124. (sio_hw->gpio_oe_set = (1 << SCSI_OUT_ACK) | \
  125. (1 << SCSI_OUT_SEL)), \
  126. (sio_hw->gpio_oe_clr = (1 << SCSI_IN_IO) | \
  127. (1 << SCSI_IN_CD) | \
  128. (1 << SCSI_IN_MSG) | \
  129. (1 << SCSI_OUT_REQ))
  130. #define SCSI_RELEASE_INITIATOR() \
  131. (sio_hw->gpio_oe_clr = (1 << scsi_pins.OUT_ACK) | \
  132. (1 << scsi_pins.OUT_SEL)), \
  133. (sio_hw->gpio_oe_set = (1 << scsi_pins.IN_IO) | \
  134. (1 << scsi_pins.IN_CD) | \
  135. (1 << scsi_pins.IN_MSG) | \
  136. (1 << scsi_pins.IN_REQ))
  137. // Enable driving of shared control pins
  138. #define SCSI_ENABLE_CONTROL_OUT() \
  139. (sio_hw->gpio_oe_set = (1 << scsi_pins.OUT_CD) | \
  140. (1 << scsi_pins.OUT_MSG))
  141. // Set SCSI data bus to output
  142. #define SCSI_ENABLE_DATA_OUT() \
  143. (sio_hw->gpio_set = (1 << SCSI_DATA_DIR), \
  144. sio_hw->gpio_oe_set = SCSI_IO_DATA_MASK)
  145. // Write SCSI data bus, also sets REQ to inactive.
  146. #define SCSI_OUT_DATA(data) \
  147. gpio_put_masked(SCSI_IO_DATA_MASK | (1 << scsi_pins.OUT_REQ), \
  148. g_scsi_parity_lookup[(uint8_t)(data)] | (1 << scsi_pins.OUT_REQ)), \
  149. SCSI_ENABLE_DATA_OUT()
  150. // Release SCSI data bus and REQ signal
  151. #define SCSI_RELEASE_DATA_REQ() \
  152. (sio_hw->gpio_oe_clr = SCSI_IO_DATA_MASK, \
  153. sio_hw->gpio_clr = (1 << SCSI_DATA_DIR), \
  154. sio_hw->gpio_set = ((1 << scsi_pins.OUT_REQ)))
  155. // Release all SCSI outputs
  156. #define SCSI_RELEASE_OUTPUTS() \
  157. SCSI_RELEASE_DATA_REQ(), \
  158. sio_hw->gpio_set = (1 << scsi_pins.OUT_IO) | \
  159. (1 << scsi_pins.OUT_CD) | \
  160. (1 << scsi_pins.OUT_MSG) | \
  161. (1 << scsi_pins.OUT_RST) | \
  162. (1 << scsi_pins.OUT_BSY) | \
  163. (1 << scsi_pins.OUT_REQ) | \
  164. (1 << scsi_pins.OUT_SEL), \
  165. delay(1), \
  166. sio_hw->gpio_oe_clr = (1 << scsi_pins.OUT_CD) | \
  167. (1 << scsi_pins.OUT_MSG)
  168. // Read SCSI data bus
  169. #define SCSI_IN_DATA() \
  170. (~sio_hw->gpio_in & SCSI_IO_DATA_MASK) >> SCSI_IO_SHIFT
  171. #ifdef __cplusplus
  172. }
  173. // SD card driver for SdFat
  174. #ifdef SD_USE_SDIO
  175. class SdioConfig;
  176. extern SdioConfig g_sd_sdio_config;
  177. #define SD_CONFIG g_sd_sdio_config
  178. #define SD_CONFIG_CRASH g_sd_sdio_config
  179. #else
  180. class SdSpiConfig;
  181. extern SdSpiConfig g_sd_spi_config;
  182. #define SD_CONFIG g_sd_spi_config
  183. #define SD_CONFIG_CRASH g_sd_spi_config
  184. #endif
  185. #endif