BlueSCSI_platform.h 7.9 KB

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