BlueSCSI_platform.h 7.8 KB

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