AzulSCSI_platform.h 5.4 KB

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