ZuluSCSI_platform.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 ZuluSCSI.
  22. // Can be customized for different microcontrollers, this file is for GD32F205VCT6.
  23. #pragma once
  24. #include <gd32f20x.h>
  25. #include <gd32f20x_gpio.h>
  26. #include <scsi2sd.h>
  27. #include <ZuluSCSI_config.h>
  28. #ifdef __cplusplus
  29. extern "C" {
  30. #endif
  31. extern const char *g_platform_name;
  32. #include "platform_hw_config.h"
  33. enum ZuluSCSIVersion_t
  34. {
  35. ZSVersion_unknown,
  36. ZSVersion_v1_1,
  37. ZSVersion_v1_1_ODE,
  38. ZSVersion_v1_2
  39. };
  40. extern enum ZuluSCSIVersion_t g_zuluscsi_version;
  41. extern bool g_moved_select_in;
  42. // Debug logging functions
  43. void platform_log(const char *s);
  44. // Minimal millis() implementation as GD32F205 does not
  45. // have an Arduino core yet.
  46. unsigned long millis(void);
  47. void delay(unsigned long ms);
  48. // Precise nanosecond delays
  49. // Works in interrupt context also, max delay 500 000 ns, min delay about 500 ns
  50. void delay_ns(unsigned long ns);
  51. static inline void delay_us(unsigned long us)
  52. {
  53. if (us > 0)
  54. {
  55. delay_ns(us * 1000);
  56. }
  57. }
  58. // Approximate fast delay
  59. static inline void delay_100ns()
  60. {
  61. asm volatile ("nop \n nop \n nop \n nop \n nop");
  62. }
  63. // Initialize SPI and GPIO configuration
  64. void platform_init();
  65. // Initialization for main application, not used for bootloader
  66. void platform_late_init();
  67. // Initialization after the SD Card has been found
  68. void platform_post_sd_card_init();
  69. // Set the status LED only if it is not in a blinking routine
  70. void platform_write_led(bool state);
  71. #define LED_ON() platform_write_led(true)
  72. #define LED_OFF() platform_write_led(false)
  73. // Used by the blinking routine
  74. void platform_set_blink_status(bool status);
  75. // LED override will set the status LED regardless of the blinking routine
  76. void platform_write_led_override(bool state);
  77. #define LED_ON_OVERRIDE() platform_write_led_override(true)
  78. #define LED_OFF_OVERRIDE() platform_write_led_override(false)
  79. // Disable the status LED
  80. void platform_disable_led(void);
  81. // Specific error code tied to the MCU when the SD card is not detected
  82. uint8_t platform_no_sd_card_on_init_error_code();
  83. // Setup soft watchdog
  84. void platform_reset_watchdog();
  85. // Reset MCU after a certain amount of time
  86. void platform_reset_mcu();
  87. // Poll function that is called every few milliseconds.
  88. // The SD card is free to access during this time, and pauses up to
  89. // few milliseconds shouldn't disturb SCSI communication.
  90. void platform_poll();
  91. // Returns the state of any platform-specific buttons.
  92. // The returned value should be a mask for buttons 1-8 in bits 0-7 respectively,
  93. // where '1' is a button pressed and '0' is a button released.
  94. // Debouncing logic is left up to the specific implementation.
  95. // This function should return without significantly delay.
  96. uint8_t platform_get_buttons();
  97. // Return system clock in Hz
  98. uint32_t platform_sys_clock_in_hz();
  99. // Return whether device supports reclocking the MCU
  100. inline bool platform_reclock_supported(){return false;}
  101. // Returns true if reboot was for mass storage - unsupported
  102. inline bool platform_rebooted_into_mass_storage() {return false;}
  103. // Reinitialize SD card connection and save log from interrupt context.
  104. // This can be used in crash handlers.
  105. void platform_emergency_log_save();
  106. // Set callback that will be called during data transfer to/from SD card.
  107. // This can be used to implement simultaneous transfer to SCSI bus.
  108. typedef void (*sd_callback_t)(uint32_t bytes_complete);
  109. void platform_set_sd_callback(sd_callback_t func, const uint8_t *buffer);
  110. // This function is called by scsiPhy.cpp.
  111. // It resets the systick counter to give 1 millisecond of uninterrupted transfer time.
  112. // The total number of skips is kept track of to keep the correct time on average.
  113. void SysTick_Handle_PreEmptively();
  114. // Reprogram firmware in main program area.
  115. #define PLATFORM_BOOTLOADER_SIZE 32768
  116. #define PLATFORM_FLASH_TOTAL_SIZE (256 * 1024)
  117. #define PLATFORM_FLASH_PAGE_SIZE 2048
  118. bool platform_rewrite_flash_page(uint32_t offset, uint8_t buffer[PLATFORM_FLASH_PAGE_SIZE]);
  119. void platform_boot_to_main_firmware();
  120. // True if the board has a physical eject button
  121. bool platform_has_phy_eject_button();
  122. // Configuration customizations based on DIP switch settings
  123. // When DIPSW1 is on, Apple quirks are enabled by default.
  124. void platform_config_hook(S2S_TargetCfg *config);
  125. #define PLATFORM_CONFIG_HOOK(cfg) platform_config_hook(cfg)
  126. // Write a single SCSI pin.
  127. // Example use: SCSI_OUT(ATN, 1) sets SCSI_ATN to low (active) state.
  128. #define SCSI_OUT(pin, state) \
  129. GPIO_BOP(SCSI_OUT_ ## pin ## _PORT) = (SCSI_OUT_ ## pin ## _PIN) << (state ? 16 : 0)
  130. // Read a single SCSI pin.
  131. // Example use: SCSI_IN(ATN), returns 1 for active low state.
  132. #define SCSI_IN(pin) \
  133. ((GPIO_ISTAT(SCSI_ ## pin ## _PORT) & (SCSI_ ## pin ## _PIN)) ? 0 : 1)
  134. // Write SCSI data bus, also sets REQ to inactive.
  135. extern const uint32_t g_scsi_out_byte_to_bop[256];
  136. #define SCSI_OUT_DATA(data) \
  137. GPIO_BOP(SCSI_OUT_PORT) = g_scsi_out_byte_to_bop[(uint8_t)(data)]
  138. // Release SCSI data bus and REQ signal
  139. #define SCSI_RELEASE_DATA_REQ() \
  140. GPIO_BOP(SCSI_OUT_PORT) = SCSI_OUT_DATA_MASK | SCSI_OUT_REQ
  141. // Release all SCSI outputs
  142. #define SCSI_RELEASE_OUTPUTS() \
  143. GPIO_BOP(SCSI_OUT_PORT) = SCSI_OUT_DATA_MASK | SCSI_OUT_REQ, \
  144. GPIO_BOP(SCSI_OUT_IO_PORT) = SCSI_OUT_IO_PIN, \
  145. GPIO_BOP(SCSI_OUT_CD_PORT) = SCSI_OUT_CD_PIN, \
  146. GPIO_BOP(SCSI_OUT_SEL_PORT) = SCSI_OUT_SEL_PIN, \
  147. GPIO_BOP(SCSI_OUT_MSG_PORT) = SCSI_OUT_MSG_PIN, \
  148. GPIO_BOP(SCSI_OUT_RST_PORT) = SCSI_OUT_RST_PIN, \
  149. GPIO_BOP(SCSI_OUT_BSY_PORT) = SCSI_OUT_BSY_PIN
  150. // Read SCSI data bus
  151. #define SCSI_IN_DATA(data) \
  152. (((~GPIO_ISTAT(SCSI_IN_PORT)) & SCSI_IN_MASK) >> SCSI_IN_SHIFT)
  153. #ifdef __cplusplus
  154. }
  155. // SD card driver for SdFat
  156. #ifndef SD_USE_SDIO
  157. // SPI interface, ZuluSCSI v1.0
  158. class SdSpiConfig;
  159. extern SdSpiConfig g_sd_spi_config;
  160. #define SD_CONFIG g_sd_spi_config
  161. #define SD_CONFIG_CRASH g_sd_spi_config
  162. #else
  163. // SDIO interface, ZuluSCSI v1.1
  164. class SdioConfig;
  165. extern SdioConfig g_sd_sdio_config;
  166. extern SdioConfig g_sd_sdio_config_crash;
  167. #define SD_CONFIG g_sd_sdio_config
  168. #define SD_CONFIG_CRASH g_sd_sdio_config_crash
  169. #endif
  170. // Check if a DMA request for SD card read has completed.
  171. // This is used to optimize the timing of data transfers on SCSI bus.
  172. // When called outside of SD callback processing, always returns false.
  173. bool check_sd_read_done();
  174. #endif