AzulSCSI_platform.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // Platform-specific definitions for AzulSCSI.
  2. // Can be customized for different microcontrollers, this file is for GD32F205VCT6.
  3. #pragma once
  4. #include <gd32f20x.h>
  5. #include <gd32f20x_gpio.h>
  6. #ifdef __cplusplus
  7. // SD card driver for SdFat
  8. class SdSpiConfig;
  9. extern SdSpiConfig g_sd_spi_config;
  10. #define SD_CONFIG g_sd_spi_config
  11. extern "C" {
  12. #endif
  13. extern const char *g_azplatform_name;
  14. // GPIO definitions
  15. // SCSI output port.
  16. // This is written using BSRR mechanism, so all output pins must be on same GPIO port.
  17. // The output pins are open-drain in hardware, using separate buffer chips for driving.
  18. #define SCSI_OUT_PORT GPIOD
  19. #define SCSI_OUT_DB7 GPIO_PIN_0
  20. #define SCSI_OUT_DB6 GPIO_PIN_1
  21. #define SCSI_OUT_DB5 GPIO_PIN_2
  22. #define SCSI_OUT_DB4 GPIO_PIN_3
  23. #define SCSI_OUT_DB3 GPIO_PIN_4
  24. #define SCSI_OUT_DB2 GPIO_PIN_5
  25. #define SCSI_OUT_DB1 GPIO_PIN_6
  26. #define SCSI_OUT_DB0 GPIO_PIN_7
  27. #define SCSI_OUT_IO GPIO_PIN_8
  28. #define SCSI_OUT_REQ GPIO_PIN_9
  29. #define SCSI_OUT_CD GPIO_PIN_10
  30. #define SCSI_OUT_SEL GPIO_PIN_11
  31. #define SCSI_OUT_MSG GPIO_PIN_12
  32. #define SCSI_OUT_RST GPIO_PIN_13
  33. #define SCSI_OUT_BSY GPIO_PIN_14
  34. #define SCSI_OUT_DBP GPIO_PIN_15
  35. #define SCSI_OUT_DATA_MASK (0x00FF | SCSI_OUT_DBP)
  36. #define SCSI_OUT_MASK 0xFFFF
  37. // SCSI input port
  38. #define SCSI_IN_PORT GPIOE
  39. #define SCSI_IN_DB7 GPIO_PIN_15
  40. #define SCSI_IN_DB6 GPIO_PIN_14
  41. #define SCSI_IN_DB5 GPIO_PIN_13
  42. #define SCSI_IN_DB4 GPIO_PIN_12
  43. #define SCSI_IN_DB3 GPIO_PIN_11
  44. #define SCSI_IN_DB2 GPIO_PIN_10
  45. #define SCSI_IN_DB1 GPIO_PIN_9
  46. #define SCSI_IN_DB0 GPIO_PIN_8
  47. #define SCSI_IN_DBP GPIO_PIN_7
  48. #define SCSI_IN_MASK (SCSI_IN_DB7|SCSI_IN_DB6|SCSI_IN_DB5|SCSI_IN_DB4|SCSI_IN_DB3|SCSI_IN_DB2|SCSI_IN_DB1|SCSI_IN_DB0|SCSI_IN_DBP)
  49. #define SCSI_IN_SHIFT 8
  50. // Various SCSI status signals
  51. #define SCSI_ATN_PORT GPIOB // FIXME: Change to 5V-tolerant pin
  52. #define SCSI_ATN_PIN GPIO_PIN_0
  53. #define SCSI_BSY_PORT GPIOB
  54. #define SCSI_BSY_PIN GPIO_PIN_10
  55. #define SCSI_SEL_PORT GPIOB
  56. #define SCSI_SEL_PIN GPIO_PIN_11
  57. #define SCSI_ACK_PORT GPIOB
  58. #define SCSI_ACK_PIN GPIO_PIN_12
  59. // RST pin uses EXTI interrupt
  60. #define SCSI_RST_PORT GPIOB
  61. #define SCSI_RST_PIN GPIO_PIN_13
  62. #define SCSI_RST_EXTI EXTI_13
  63. #define SCSI_RST_EXTI_SOURCE_PORT GPIO_PORT_SOURCE_GPIOB
  64. #define SCSI_RST_EXTI_SOURCE_PIN GPIO_PIN_SOURCE_13
  65. #define SCSI_RST_IRQ EXTI10_15_IRQHandler
  66. #define SCSI_RST_IRQn EXTI10_15_IRQn
  67. // Terminator enable/disable config, active low
  68. #define SCSI_TERM_EN_PORT GPIOC
  69. #define SCSI_TERM_EN_PIN GPIO_PIN_8
  70. // SD card pins
  71. #define SD_PORT GPIOA
  72. #define SD_CS_PIN GPIO_PIN_4
  73. #define SD_CLK_PIN GPIO_PIN_5
  74. #define SD_MISO_PIN GPIO_PIN_6
  75. #define SD_MOSI_PIN GPIO_PIN_7
  76. // DIP switches
  77. #define DIP_PORT GPIOB
  78. #define DIPSW1_PIN GPIO_PIN_4
  79. #define DIPSW2_PIN GPIO_PIN_5
  80. #define DIPSW3_PIN GPIO_PIN_6
  81. // Status LED pins
  82. #define LED_PORT GPIOC
  83. #define LED_I_PIN GPIO_PIN_4
  84. #define LED_E_PIN GPIO_PIN_5
  85. #define LED_PINS (LED_I_PIN | LED_E_PIN)
  86. #define LED_ON() gpio_bit_reset(LED_PORT, LED_PINS)
  87. #define LED_OFF() gpio_bit_set(LED_PORT, LED_PINS)
  88. // Debug logging functions
  89. void azplatform_log(const char *s);
  90. // Minimal millis() implementation as GD32F205 does not
  91. // have an Arduino core yet.
  92. unsigned long millis();
  93. void delay(unsigned long ms);
  94. // Precise nanosecond delays
  95. // Works in interrupt context also, max delay 500 000 ns, min delay about 500 ns
  96. void delay_ns(unsigned long ns);
  97. // Approximate fast delay
  98. static inline void delay_100ns()
  99. {
  100. asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop");
  101. }
  102. // Initialize SPI and GPIO configuration
  103. void azplatform_init();
  104. // Set callback for when SCSI_RST pin goes low
  105. void azplatform_set_rst_callback(void (*callback)());
  106. // Reinitialize SD card connection and save log from interrupt context.
  107. // This can be used in crash handlers.
  108. void azplatform_emergency_log_save();
  109. // Direct streaming between SCSI and SD card
  110. // If the SD card driver receives a read request to buffer, it will directly send the data to SCSI bus.
  111. // If the SD card driver receives a write request from buffer, it will directly get the data from SCSI.
  112. void azplatform_prepare_stream(uint8_t *buffer);
  113. // Get status of latest streaming operation.
  114. // If this returns false, the caller should do the SCSI write themselves.
  115. // Usually that happens if the read goes through SdFs cache.
  116. bool azplatform_finish_stream();
  117. // Write a single SCSI pin.
  118. // Example use: SCSI_OUT(ATN, 1) sets SCSI_ATN to low (active) state.
  119. #define SCSI_OUT(pin, state) \
  120. GPIO_BOP(SCSI_OUT_PORT) = (SCSI_OUT_ ## pin) << (state ? 16 : 0)
  121. // Read a single SCSI pin.
  122. // Example use: SCSI_IN(ATN), returns 1 for active low state.
  123. #define SCSI_IN(pin) \
  124. ((GPIO_ISTAT(SCSI_ ## pin ## _PORT) & (SCSI_ ## pin ## _PIN)) ? 0 : 1)
  125. // Write SCSI data bus, also sets REQ to inactive.
  126. extern const uint32_t g_scsi_out_byte_to_bop[256];
  127. #define SCSI_OUT_DATA(data) \
  128. GPIO_BOP(SCSI_OUT_PORT) = g_scsi_out_byte_to_bop[(uint8_t)(data)]
  129. // Release SCSI data bus and REQ signal
  130. #define SCSI_RELEASE_DATA_REQ() \
  131. GPIO_BOP(SCSI_OUT_PORT) = SCSI_OUT_DATA_MASK | SCSI_OUT_REQ
  132. // Release all SCSI outputs
  133. #define SCSI_RELEASE_OUTPUTS() \
  134. GPIO_BOP(SCSI_OUT_PORT) = SCSI_OUT_MASK
  135. // Read SCSI data bus
  136. #define SCSI_IN_DATA(data) \
  137. (((~GPIO_ISTAT(SCSI_IN_PORT)) & SCSI_IN_MASK) >> SCSI_IN_SHIFT)
  138. #ifdef __cplusplus
  139. }
  140. #endif