BlueSCSI_platform.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /**
  2. * ZuluSCSI™ - Copyright (c) 2022-2025 Rabbit Hole Computing™
  3. *
  4. * ZuluSCSI™ firmware is licensed under the GPL version 3 or any later version.
  5. *
  6. * https://www.gnu.org/licenses/gpl-3.0.html
  7. * ----
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  20. **/
  21. // Platform-specific definitions for BlueSCSI RP2040 hardware.
  22. #pragma once
  23. #include <stdint.h>
  24. #include <Arduino.h>
  25. #include "BlueSCSI_config.h"
  26. #include "BlueSCSI_platform_network.h"
  27. #include <BlueSCSI_settings.h>
  28. #ifdef BLUESCSI_PICO
  29. // BlueSCSI Pico carrier board variant
  30. #include "BlueSCSI_platform_gpio_Pico.h"
  31. #elif defined(BLUESCSI_PICO_2)
  32. // BlueSCSI Pico 2 carrier board variant
  33. #include "BlueSCSI_platform_gpio_Pico_2.h"
  34. #elif defined(BLUESCSI_V2)
  35. // BS2 hardware variant, using Raspberry Pico board on a carrier PCB
  36. #include "BlueSCSI_platform_gpio_v2.h"
  37. #elif defined(BLUESCSI_BLASTER)
  38. // RP2350B variant, using mcu chip directly
  39. #include "BlueSCSI_platform_gpio_Blaster.h"
  40. #else
  41. // Normal RP2040 variant, using RP2040 chip directly
  42. #include "BlueSCSI_platform_gpio_RP2040.h"
  43. #endif
  44. #include "scsiHostPhy.h"
  45. #ifdef __cplusplus
  46. extern "C" {
  47. #endif
  48. /* These are used in debug output and default SCSI strings */
  49. extern const char *g_platform_name;
  50. // NOTE: The driver supports synchronous speeds higher than 10MB/s, but this
  51. // has not been tested due to lack of fast enough SCSI adapter.
  52. // #define PLATFORM_MAX_SCSI_SPEED S2S_CFG_SPEED_SYNC_20
  53. // Debug logging function, can be used to print to e.g. serial port.
  54. // May get called from interrupt handlers.
  55. void platform_log(const char *s);
  56. void platform_emergency_log_save();
  57. // Timing and delay functions.
  58. // Arduino platform already provides these
  59. unsigned long millis(void);
  60. void delay(unsigned long ms);
  61. // Short delays, can be called from interrupt mode
  62. static inline void delay_ns(unsigned long ns)
  63. {
  64. delayMicroseconds((ns + 999) / 1000);
  65. }
  66. // Approximate fast delay
  67. static inline void delay_100ns()
  68. {
  69. asm volatile ("nop \n nop \n nop \n nop \n nop \n nop \n nop \n nop \n nop \n nop \n nop");
  70. }
  71. // Initialize SD card and GPIO configuration
  72. void platform_init();
  73. // Initialization for main application, not used for bootloader
  74. void platform_late_init();
  75. // Initialization after the SD Card has been found
  76. void platform_post_sd_card_init();
  77. // Set the status LED only if it is not in a blinking routine
  78. void platform_write_led(bool state);
  79. #define LED_ON() platform_write_led(true)
  80. #define LED_OFF() platform_write_led(false)
  81. // Used by the blinking routine
  82. void platform_set_blink_status(bool status);
  83. // LED override will set the status LED regardless of the blinking routine
  84. void platform_write_led_override(bool state);
  85. #define LED_ON_OVERRIDE() platform_write_led_override(true)
  86. #define LED_OFF_OVERRIDE() platform_write_led_override(false)
  87. // Disable the status LED
  88. void platform_disable_led(void);
  89. // Specific error code tied to the MCU when the SD card is not detected
  90. uint8_t platform_no_sd_card_on_init_error_code();
  91. // Query whether initiator mode is enabled on targets with PLATFORM_HAS_INITIATOR_MODE
  92. bool platform_is_initiator_mode_enabled();
  93. void platform_initiator_gpio_setup();
  94. bool platform_supports_initiator_mode();
  95. void platform_enable_initiator_mode();
  96. // Setup soft watchdog if supported
  97. void platform_reset_watchdog();
  98. // Reset MCU
  99. void platform_reset_mcu();
  100. // Poll function that is called every few milliseconds.
  101. // The SD card is free to access during this time, and pauses up to
  102. // few milliseconds shouldn't disturb SCSI communication.
  103. void platform_poll();
  104. // Returns the state of any platform-specific buttons.
  105. // The returned value should be a mask for buttons 1-8 in bits 0-7 respectively,
  106. // where '1' is a button pressed and '0' is a button released.
  107. // Debouncing logic is left up to the specific implementation.
  108. // This function should return without significantly delay.
  109. uint8_t platform_get_buttons();
  110. uint32_t platform_sys_clock_in_hz();
  111. // Return whether device supports reclocking the MCU
  112. inline bool platform_reclock_supported(){return true;}
  113. #ifdef RECLOCKING_SUPPORTED
  114. // reclock the MCU
  115. bool platform_reclock(bluescsi_speed_grade_t speed_grade);
  116. #endif
  117. // Returns true if reboot was for mass storage
  118. bool platform_rebooted_into_mass_storage();
  119. // Set callback that will be called during data transfer to/from SD card.
  120. // This can be used to implement simultaneous transfer to SCSI bus.
  121. typedef void (*sd_callback_t)(uint32_t bytes_complete);
  122. void platform_set_sd_callback(sd_callback_t func, const uint8_t *buffer);
  123. // Reprogram firmware in main program area.
  124. #ifndef RP2040_DISABLE_BOOTLOADER
  125. #define PLATFORM_BOOTLOADER_SIZE (128 * 1024)
  126. #define PLATFORM_FLASH_TOTAL_SIZE (1024 * 1024)
  127. #define PLATFORM_FLASH_PAGE_SIZE 4096
  128. bool platform_rewrite_flash_page(uint32_t offset, uint8_t buffer[PLATFORM_FLASH_PAGE_SIZE]);
  129. #endif
  130. void platform_boot_to_main_firmware();
  131. // ROM drive in the unused external flash area
  132. #ifndef RP2040_DISABLE_ROMDRIVE
  133. #define PLATFORM_HAS_ROM_DRIVE 1
  134. // Check maximum available space for ROM drive in bytes
  135. uint32_t platform_get_romdrive_maxsize();
  136. // Read ROM drive area
  137. bool platform_read_romdrive(uint8_t *dest, uint32_t start, uint32_t count);
  138. // Reprogram ROM drive area
  139. #define PLATFORM_ROMDRIVE_PAGE_SIZE 4096
  140. bool platform_write_romdrive(const uint8_t *data, uint32_t start, uint32_t count);
  141. #endif
  142. // Parity lookup tables for write and read from SCSI bus.
  143. // These are used by macros below and the code in scsi_accel_rp2040.cpp
  144. extern const uint16_t g_scsi_parity_lookup[256];
  145. extern const uint16_t g_scsi_parity_check_lookup[512];
  146. // Returns true if the board has a physical eject button
  147. bool platform_has_phy_eject_button();
  148. void platform_disable_i2c();
  149. bool platform_check_picow();
  150. #ifdef __cplusplus
  151. }
  152. // SD card driver for SdFat
  153. #ifdef SD_USE_SDIO
  154. class SdioConfig;
  155. extern SdioConfig g_sd_sdio_config;
  156. #define SD_CONFIG g_sd_sdio_config
  157. #define SD_CONFIG_CRASH g_sd_sdio_config
  158. #else
  159. class SdSpiConfig;
  160. extern SdSpiConfig g_sd_spi_config;
  161. #define SD_CONFIG g_sd_spi_config
  162. #define SD_CONFIG_CRASH g_sd_spi_config
  163. #endif
  164. #endif