ZuluSCSI_platform.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /**
  2. * ZuluSCSI™ - Copyright (c) 2022 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 <gd32f4xx.h>
  25. #include <gd32f4xx_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. #if defined(ZULUSCSI_V1_4)
  33. # define PLATFORM_NAME "ZuluSCSI v1.4"
  34. # define PLATFORM_REVISION "1.4"
  35. # define PLATFORM_MAX_SCSI_SPEED S2S_CFG_SPEED_SYNC_10
  36. # define PLATFORM_OPTIMAL_MIN_SD_WRITE_SIZE 4096
  37. # define PLATFORM_OPTIMAL_MAX_SD_WRITE_SIZE 65536
  38. # define PLATFORM_OPTIMAL_LAST_SD_WRITE_SIZE 8192
  39. # include "ZuluSCSI_v1_4_gpio.h"
  40. #endif
  41. #ifndef PLATFORM_VDD_WARNING_LIMIT_mV
  42. #define PLATFORM_VDD_WARNING_LIMIT_mV 2800
  43. #endif
  44. // Debug logging functions
  45. void platform_log(const char *s);
  46. // Minimal millis() implementation as GD32F205 does not
  47. // have an Arduino core yet.
  48. unsigned long millis(void);
  49. void delay(unsigned long ms);
  50. // Precise nanosecond delays
  51. // Works in interrupt context also, max delay 500 000 ns, min delay about 500 ns
  52. void delay_ns(unsigned long ns);
  53. static inline void delay_us(unsigned long us)
  54. {
  55. if (us > 0)
  56. {
  57. delay_ns(us * 1000);
  58. }
  59. }
  60. // Approximate fast delay
  61. static inline void delay_100ns()
  62. {
  63. asm volatile ("nop \n nop \n nop \n nop \n nop");
  64. }
  65. // Initialize SPI and GPIO configuration
  66. void platform_init();
  67. // Initialization for main application, not used for bootloader
  68. void platform_late_init();
  69. // Hooks
  70. void platform_end_of_loop_hook(void);
  71. // Disable the status LED
  72. void platform_disable_led(void);
  73. // Setup soft watchdog
  74. void platform_reset_watchdog();
  75. // Poll function that is called every few milliseconds.
  76. // The SD card is free to access during this time, and pauses up to
  77. // few milliseconds shouldn't disturb SCSI communication.
  78. void platform_poll();
  79. // Reinitialize SD card connection and save log from interrupt context.
  80. // This can be used in crash handlers.
  81. void platform_emergency_log_save();
  82. // Set callback that will be called during data transfer to/from SD card.
  83. // This can be used to implement simultaneous transfer to SCSI bus.
  84. typedef void (*sd_callback_t)(uint32_t bytes_complete);
  85. void platform_set_sd_callback(sd_callback_t func, const uint8_t *buffer);
  86. // This function is called by scsiPhy.cpp.
  87. // It resets the systick counter to give 1 millisecond of uninterrupted transfer time.
  88. // The total number of skips is kept track of to keep the correct time on average.
  89. void SysTick_Handle_PreEmptively();
  90. // Reprogram firmware in main program area.
  91. #define PLATFORM_BOOTLOADER_SIZE 32768
  92. #define PLATFORM_FLASH_TOTAL_SIZE (256 * 1024)
  93. #define PLATFORM_FLASH_PAGE_SIZE 2048
  94. bool platform_rewrite_flash_page(uint32_t offset, uint8_t buffer[PLATFORM_FLASH_PAGE_SIZE]);
  95. void platform_boot_to_main_firmware();
  96. // Configuration customizations based on DIP switch settings
  97. // When DIPSW1 is on, Apple quirks are enabled by default.
  98. void platform_config_hook(S2S_TargetCfg *config);
  99. #define PLATFORM_CONFIG_HOOK(cfg) platform_config_hook(cfg)
  100. // Write a single SCSI pin.
  101. // Example use: SCSI_OUT(ATN, 1) sets SCSI_ATN to low (active) state.
  102. #define SCSI_OUT(pin, state) \
  103. GPIO_BOP(SCSI_OUT_ ## pin ## _PORT) = (SCSI_OUT_ ## pin ## _PIN) << (state ? 16 : 0)
  104. // Read a single SCSI pin.
  105. // Example use: SCSI_IN(ATN), returns 1 for active low state.
  106. #define SCSI_IN(pin) \
  107. ((GPIO_ISTAT(SCSI_ ## pin ## _PORT) & (SCSI_ ## pin ## _PIN)) ? 0 : 1)
  108. // Write SCSI data bus, also sets REQ to inactive.
  109. extern const uint32_t g_scsi_out_byte_to_bop[256];
  110. #define SCSI_OUT_DATA(data) \
  111. GPIO_BOP(SCSI_OUT_PORT) = g_scsi_out_byte_to_bop[(uint8_t)(data)]
  112. // Release SCSI data bus and REQ signal
  113. #define SCSI_RELEASE_DATA_REQ() \
  114. GPIO_BOP(SCSI_OUT_PORT) = SCSI_OUT_DATA_MASK | SCSI_OUT_REQ
  115. // Release all SCSI outputs
  116. #define SCSI_RELEASE_OUTPUTS() \
  117. GPIO_BOP(SCSI_OUT_PORT) = SCSI_OUT_DATA_MASK | SCSI_OUT_REQ, \
  118. GPIO_BOP(SCSI_OUT_IO_PORT) = SCSI_OUT_IO_PIN, \
  119. GPIO_BOP(SCSI_OUT_CD_PORT) = SCSI_OUT_CD_PIN, \
  120. GPIO_BOP(SCSI_OUT_SEL_PORT) = SCSI_OUT_SEL_PIN, \
  121. GPIO_BOP(SCSI_OUT_MSG_PORT) = SCSI_OUT_MSG_PIN, \
  122. GPIO_BOP(SCSI_OUT_RST_PORT) = SCSI_OUT_RST_PIN, \
  123. GPIO_BOP(SCSI_OUT_BSY_PORT) = SCSI_OUT_BSY_PIN
  124. // Read SCSI data bus
  125. #define SCSI_IN_DATA(data) \
  126. (((~GPIO_ISTAT(SCSI_IN_PORT)) & SCSI_IN_MASK) >> SCSI_IN_SHIFT)
  127. #ifdef __cplusplus
  128. }
  129. // SD card driver for SdFat
  130. // SDIO interface, ZuluSCSI v1.4
  131. class SdioConfig;
  132. extern SdioConfig g_sd_sdio_config;
  133. extern SdioConfig g_sd_sdio_config_crash;
  134. #define SD_CONFIG g_sd_sdio_config
  135. #define SD_CONFIG_CRASH g_sd_sdio_config_crash
  136. // Check if a DMA request for SD card read has completed.
  137. // This is used to optimize the timing of data transfers on SCSI bus.
  138. bool check_sd_read_done();
  139. #endif