ZuluSCSI_platform.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. #include <SdFat.h>
  30. extern "C" {
  31. #endif
  32. extern const char *g_platform_name;
  33. #if defined(ZULUSCSI_V1_4)
  34. # define PLATFORM_NAME "ZuluSCSI v1.4"
  35. # define PLATFORM_REVISION "1.4"
  36. # define PLATFORM_MAX_SCSI_SPEED S2S_CFG_SPEED_SYNC_10
  37. # define PLATFORM_OPTIMAL_MIN_SD_WRITE_SIZE 4096
  38. # define PLATFORM_OPTIMAL_MAX_SD_WRITE_SIZE 65536
  39. # define PLATFORM_OPTIMAL_LAST_SD_WRITE_SIZE 8192
  40. # define PLATFORM_FLASH_SECTOR_ERASE
  41. # include "ZuluSCSI_v1_4_gpio.h"
  42. #endif
  43. #ifndef PLATFORM_VDD_WARNING_LIMIT_mV
  44. #define PLATFORM_VDD_WARNING_LIMIT_mV 2800
  45. #endif
  46. #define PLATFORM_DEFAULT_SCSI_SPEED_SETTING 10
  47. // Debug logging functions
  48. void platform_log(const char *s);
  49. // Minimal millis() implementation as GD32F205 does not
  50. // have an Arduino core yet.
  51. unsigned long millis(void);
  52. void delay(unsigned long ms);
  53. // Precise nanosecond delays
  54. // Works in interrupt context also, max delay 500 000 ns, min delay about 500 ns
  55. void delay_ns(unsigned long ns);
  56. static inline void delay_us(unsigned long us)
  57. {
  58. if (us > 0)
  59. {
  60. delay_ns(us * 1000);
  61. }
  62. }
  63. // Approximate fast delay
  64. static inline void delay_100ns()
  65. {
  66. // asm volatile ("nop \n nop \n nop \n nop \n nop");
  67. asm volatile ("nop \n nop \n nop \n nop \n nop");
  68. }
  69. // Initialize SPI and GPIO configuration
  70. void platform_init();
  71. // Initialization for main application, not used for bootloader
  72. void platform_late_init();
  73. // Initialization after the SD Card has been found
  74. void platform_post_sd_card_init();
  75. // Hooks
  76. void platform_end_of_loop_hook(void);
  77. // Disable the status LED
  78. void platform_disable_led(void);
  79. // Setup soft watchdog
  80. void platform_reset_watchdog();
  81. // Poll function that is called every few milliseconds.
  82. // The SD card is free to access during this time, and pauses up to
  83. // few milliseconds shouldn't disturb SCSI communication.
  84. void platform_poll();
  85. // Returns the state of any platform-specific buttons.
  86. // The returned value should be a mask for buttons 1-8 in bits 0-7 respectively,
  87. // where '1' is a button pressed and '0' is a button released.
  88. // Debouncing logic is left up to the specific implementation.
  89. // This function should return without significantly delay.
  90. uint8_t platform_get_buttons();
  91. // Attempt to reclock the MCU - unsupported
  92. typedef enum
  93. {
  94. ZULUSCSI_RECLOCK_SUCCESS,
  95. ZULUSCSI_RECLOCK_NOT_SUPPORTED,
  96. ZULUSCSI_RECLOCK_FAILED
  97. } zuluscsi_reclock_status_t;
  98. zuluscsi_reclock_status_t platform_reclock(uint32_t clk_in_khz){return ZULUSCSI_RECLOCK_NOT_SUPPORTED;}
  99. // Reinitialize SD card connection and save log from interrupt context.
  100. // This can be used in crash handlers.
  101. void platform_emergency_log_save();
  102. // Set callback that will be called during data transfer to/from SD card.
  103. // This can be used to implement simultaneous transfer to SCSI bus.
  104. typedef void (*sd_callback_t)(uint32_t bytes_complete);
  105. void platform_set_sd_callback(sd_callback_t func, const uint8_t *buffer);
  106. // This function is called by scsiPhy.cpp.
  107. // It resets the systick counter to give 1 millisecond of uninterrupted transfer time.
  108. // The total number of skips is kept track of to keep the correct time on average.
  109. void SysTick_Handle_PreEmptively();
  110. // Reprogram firmware in main program area.
  111. #define PLATFORM_BOOTLOADER_SIZE 32768
  112. #define PLATFORM_FLASH_TOTAL_SIZE (512 * 1024)
  113. // must be a factor of each sector map size
  114. #define PLATFORM_FLASH_WRITE_BUFFER_SIZE 2048
  115. void platform_boot_to_main_firmware();
  116. // Configuration customizations based on DIP switch settings
  117. // When DIPSW1 is on, Apple quirks are enabled by default.
  118. void platform_config_hook(S2S_TargetCfg *config);
  119. #define PLATFORM_CONFIG_HOOK(cfg) platform_config_hook(cfg)
  120. // Write a single SCSI pin.
  121. // Example use: SCSI_OUT(ATN, 1) sets SCSI_ATN to low (active) state.
  122. #define SCSI_OUT(pin, state) \
  123. GPIO_BOP(SCSI_OUT_ ## pin ## _PORT) = (SCSI_OUT_ ## pin ## _PIN) << (state ? 16 : 0)
  124. // Read a single SCSI pin.
  125. // Example use: SCSI_IN(ATN), returns 1 for active low state.
  126. #define SCSI_IN(pin) \
  127. ((GPIO_ISTAT(SCSI_ ## pin ## _PORT) & (SCSI_ ## pin ## _PIN)) ? 0 : 1)
  128. // Write SCSI data bus, also sets REQ to inactive.
  129. extern const uint32_t g_scsi_out_byte_to_bop[256];
  130. #define SCSI_OUT_DATA(data) \
  131. GPIO_BOP(SCSI_OUT_PORT) = g_scsi_out_byte_to_bop[(uint8_t)(data)]
  132. // Release SCSI data bus and REQ signal
  133. #define SCSI_RELEASE_DATA_REQ() \
  134. GPIO_BOP(SCSI_OUT_PORT) = SCSI_OUT_DATA_MASK | SCSI_OUT_REQ
  135. // Release all SCSI outputs
  136. #define SCSI_RELEASE_OUTPUTS() \
  137. GPIO_BOP(SCSI_OUT_PORT) = SCSI_OUT_DATA_MASK | SCSI_OUT_REQ, \
  138. GPIO_BOP(SCSI_OUT_IO_PORT) = SCSI_OUT_IO_PIN, \
  139. GPIO_BOP(SCSI_OUT_CD_PORT) = SCSI_OUT_CD_PIN, \
  140. GPIO_BOP(SCSI_OUT_SEL_PORT) = SCSI_OUT_SEL_PIN, \
  141. GPIO_BOP(SCSI_OUT_MSG_PORT) = SCSI_OUT_MSG_PIN, \
  142. GPIO_BOP(SCSI_OUT_RST_PORT) = SCSI_OUT_RST_PIN, \
  143. GPIO_BOP(SCSI_OUT_BSY_PORT) = SCSI_OUT_BSY_PIN
  144. // Read SCSI data bus
  145. #define SCSI_IN_DATA(data) \
  146. (((~GPIO_ISTAT(SCSI_IN_PORT)) & SCSI_IN_MASK) >> SCSI_IN_SHIFT)
  147. #ifdef __cplusplus
  148. }
  149. // From GD32F4xx user manual
  150. const uint32_t platform_flash_sector_map[] =
  151. {
  152. 16 * 1024,
  153. 16 * 1024,
  154. 16 * 1024,
  155. 16 * 1024,
  156. 64 * 1024,
  157. 128 * 1024,
  158. 128 * 1024,
  159. 128 * 1024
  160. };
  161. bool platform_firmware_erase(FsFile &file);
  162. bool platform_firmware_program(FsFile &file);
  163. // SD card driver for SdFat
  164. // SDIO interface, ZuluSCSI v1.4
  165. class SdioConfig;
  166. extern SdioConfig g_sd_sdio_config;
  167. extern SdioConfig g_sd_sdio_config_crash;
  168. #define SD_CONFIG g_sd_sdio_config
  169. #define SD_CONFIG_CRASH g_sd_sdio_config_crash
  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. bool check_sd_read_done();
  173. #endif