AzulSCSI_platform.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. static inline void delay_100ns()
  96. {
  97. #if HXTAL_VALUE==8000000
  98. asm("nop"); asm("nop"); asm("nop");
  99. #else
  100. asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop");
  101. #endif
  102. }
  103. // Initialize SPI and GPIO configuration
  104. void azplatform_init();
  105. // Set callback for when SCSI_RST pin goes low
  106. void azplatform_set_rst_callback(void (*callback)());
  107. // Reinitialize SD card connection and save log from interrupt context.
  108. // This can be used in crash handlers.
  109. void azplatform_emergency_log_save();
  110. // Write a single SCSI pin.
  111. // Example use: SCSI_OUT(ATN, 1) sets SCSI_ATN to low (active) state.
  112. #define SCSI_OUT(pin, state) \
  113. GPIO_BOP(SCSI_OUT_PORT) = (SCSI_OUT_ ## pin) << (state ? 16 : 0)
  114. // Read a single SCSI pin.
  115. // Example use: SCSI_IN(ATN), returns 1 for active low state.
  116. #define SCSI_IN(pin) \
  117. ((GPIO_ISTAT(SCSI_ ## pin ## _PORT) & (SCSI_ ## pin ## _PIN)) ? 0 : 1)
  118. // Write SCSI data bus
  119. extern const uint32_t g_scsi_out_byte_to_bop[256];
  120. #define SCSI_OUT_DATA(data) \
  121. GPIO_BOP(SCSI_OUT_PORT) = g_scsi_out_byte_to_bop[(uint8_t)(data)]
  122. // Release SCSI data bus
  123. #define SCSI_RELEASE_DATA() \
  124. GPIO_BOP(SCSI_OUT_PORT) = SCSI_OUT_DATA_MASK
  125. // Release all SCSI outputs
  126. #define SCSI_RELEASE_OUTPUTS() \
  127. GPIO_BOP(SCSI_OUT_PORT) = SCSI_OUT_MASK
  128. // Read SCSI data bus
  129. #define SCSI_IN_DATA(data) \
  130. (((~GPIO_ISTAT(SCSI_IN_PORT)) & SCSI_IN_MASK) >> SCSI_IN_SHIFT)
  131. #ifdef __cplusplus
  132. }
  133. #endif