ZuluSCSI_platform.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  1. /**
  2. * ZuluSCSI™ - Copyright (c) 2022-2025 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. #include "ZuluSCSI_platform.h"
  22. #include "gd32f20x_sdio.h"
  23. #include "gd32f20x_fmc.h"
  24. #include "gd32f20x_fwdgt.h"
  25. #include "gd32_sdio_sdcard.h"
  26. #include "ZuluSCSI_log.h"
  27. #include "ZuluSCSI_config.h"
  28. #include "usbd_conf.h"
  29. #include "usb_serial.h"
  30. #include "greenpak.h"
  31. #include <SdFat.h>
  32. #include <scsi.h>
  33. #include <assert.h>
  34. #include <audio_i2s.h>
  35. #include <ZuluSCSI_audio.h>
  36. #include <ZuluSCSI_settings.h>
  37. extern SdFs SD;
  38. extern bool g_rawdrive_active;
  39. extern "C" {
  40. const char *g_platform_name = PLATFORM_NAME;
  41. static bool g_enable_apple_quirks = false;
  42. bool g_direct_mode = false;
  43. ZuluSCSIVersion_t g_zuluscsi_version = ZSVersion_unknown;
  44. bool g_moved_select_in = false;
  45. static bool g_led_blinking = false;
  46. // hw_config.cpp c functions
  47. #include "platform_hw_config.h"
  48. // usb_log_poll() is called through function pointer to
  49. // avoid including USB in SD card bootloader.
  50. static void (*g_usb_log_poll_func)(void);
  51. static void usb_log_poll();
  52. /*************************/
  53. /* Timing functions */
  54. /*************************/
  55. static volatile uint32_t g_millisecond_counter;
  56. static volatile uint32_t g_watchdog_timeout;
  57. static uint32_t g_ns_to_cycles; // Q0.32 fixed point format
  58. static void watchdog_handler(uint32_t *sp);
  59. unsigned long millis()
  60. {
  61. return g_millisecond_counter;
  62. }
  63. void delay(unsigned long ms)
  64. {
  65. uint32_t start = g_millisecond_counter;
  66. while ((uint32_t)(g_millisecond_counter - start) < ms);
  67. }
  68. void delay_ns(unsigned long ns)
  69. {
  70. uint32_t CNT_start = DWT->CYCCNT;
  71. if (ns <= 100) return; // Approximate call overhead
  72. ns -= 100;
  73. uint32_t cycles = ((uint64_t)ns * g_ns_to_cycles) >> 32;
  74. while ((uint32_t)(DWT->CYCCNT - CNT_start) < cycles);
  75. }
  76. void SysTick_Handler_inner(uint32_t *sp)
  77. {
  78. g_millisecond_counter++;
  79. if (g_watchdog_timeout > 0)
  80. {
  81. g_watchdog_timeout--;
  82. const uint32_t busreset_time = WATCHDOG_CRASH_TIMEOUT - WATCHDOG_BUS_RESET_TIMEOUT;
  83. if (g_watchdog_timeout <= busreset_time)
  84. {
  85. if (!scsiDev.resetFlag)
  86. {
  87. logmsg("WATCHDOG TIMEOUT at PC ", sp[6], " LR ", sp[5], " attempting bus reset");
  88. scsiDev.resetFlag = 1;
  89. }
  90. if (g_watchdog_timeout == 0)
  91. {
  92. watchdog_handler(sp);
  93. }
  94. }
  95. }
  96. }
  97. __attribute__((interrupt, naked))
  98. void SysTick_Handler(void)
  99. {
  100. // Take note of stack pointer so that we can print debug
  101. // info in watchdog handler.
  102. asm("mrs r0, msp\n"
  103. "b SysTick_Handler_inner": : : "r0");
  104. }
  105. // This function is called by scsiPhy.cpp.
  106. // It resets the systick counter to give 1 millisecond of uninterrupted transfer time.
  107. // The total number of skips is kept track of to keep the correct time on average.
  108. void SysTick_Handle_PreEmptively()
  109. {
  110. static int skipped_clocks = 0;
  111. __disable_irq();
  112. uint32_t loadval = SysTick->LOAD;
  113. skipped_clocks += loadval - SysTick->VAL;
  114. SysTick->VAL = 0;
  115. if (skipped_clocks > loadval)
  116. {
  117. // We have skipped enough ticks that it is time to fake a call
  118. // to SysTick interrupt handler.
  119. skipped_clocks -= loadval;
  120. uint32_t stack_frame[8] = {0};
  121. stack_frame[6] = (uint32_t)__builtin_return_address(0);
  122. SysTick_Handler_inner(stack_frame);
  123. }
  124. __enable_irq();
  125. }
  126. uint32_t platform_sys_clock_in_hz()
  127. {
  128. return rcu_clock_freq_get(CK_SYS);
  129. }
  130. /***************/
  131. /* GPIO init */
  132. /***************/
  133. #ifdef PLATFORM_VERSION_1_1_PLUS
  134. static void init_audio_gpio()
  135. {
  136. gpio_pin_remap1_config(GPIO_PCF5, GPIO_PCF5_SPI1_IO_REMAP1, ENABLE);
  137. gpio_pin_remap1_config(GPIO_PCF5, GPIO_PCF5_SPI1_NSCK_REMAP1, ENABLE);
  138. gpio_pin_remap1_config(GPIO_PCF4, GPIO_PCF4_SPI1_SCK_PD3_REMAP, ENABLE);
  139. gpio_init(I2S_CK_PORT, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, I2S_CK_PIN);
  140. gpio_init(I2S_SD_PORT, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, I2S_SD_PIN);
  141. gpio_init(I2S_WS_PORT, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, I2S_WS_PIN);
  142. }
  143. #endif
  144. // Method of determining whichi scsi board is being used
  145. static ZuluSCSIVersion_t get_zuluscsi_version()
  146. {
  147. #ifdef DIGITAL_VERSION_DETECT_PORT
  148. bool pull_down;
  149. bool pull_up;
  150. gpio_init(DIGITAL_VERSION_DETECT_PORT, GPIO_MODE_IPU, 0, DIGITAL_VERSION_DETECT_PIN);
  151. delay_us(10);
  152. pull_up = SET == gpio_input_bit_get(DIGITAL_VERSION_DETECT_PORT, DIGITAL_VERSION_DETECT_PIN);
  153. gpio_init(DIGITAL_VERSION_DETECT_PORT, GPIO_MODE_IPD, 0, DIGITAL_VERSION_DETECT_PIN);
  154. delay_us(10);
  155. pull_down = RESET == gpio_input_bit_get(DIGITAL_VERSION_DETECT_PORT, DIGITAL_VERSION_DETECT_PIN);
  156. if (pull_up && pull_down)
  157. return ZSVersion_v1_1;
  158. if (pull_down && !pull_up)
  159. return ZSVersion_v1_1_ODE;
  160. if (pull_up && !pull_down)
  161. {
  162. return ZSVersion_v1_2;
  163. }
  164. #endif // DIGITAL_DETECT_VERSION
  165. return ZSVersion_unknown;
  166. }
  167. // Initialize SPI and GPIO configuration
  168. // Clock has already been initialized by system_gd32f20x.c
  169. void platform_init()
  170. {
  171. SystemCoreClockUpdate();
  172. // Enable SysTick to drive millis()
  173. g_millisecond_counter = 0;
  174. SysTick_Config(SystemCoreClock / 1000U);
  175. NVIC_SetPriority(SysTick_IRQn, 0x00U);
  176. // Enable DWT counter to drive delay_ns()
  177. g_ns_to_cycles = ((uint64_t)SystemCoreClock << 32) / 1000000000;
  178. CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
  179. DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;
  180. // Enable debug output on SWO pin
  181. DBG_CTL |= DBG_CTL_TRACE_IOEN;
  182. if (TPI->ACPR == 0)
  183. {
  184. CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
  185. TPI->ACPR = SystemCoreClock / 2000000 - 1; // 2 Mbps baudrate for SWO
  186. // TPI->ACPR = SystemCoreClock / 30000000 - 1; // 30 Mbps baudrate for SWO
  187. TPI->SPPR = 2;
  188. TPI->FFCR = 0x100; // TPIU packet framing disabled
  189. // DWT->CTRL |= (1 << DWT_CTRL_EXCTRCENA_Pos);
  190. // DWT->CTRL |= (1 << DWT_CTRL_CYCTAP_Pos)
  191. // | (15 << DWT_CTRL_POSTPRESET_Pos)
  192. // | (1 << DWT_CTRL_PCSAMPLENA_Pos)
  193. // | (3 << DWT_CTRL_SYNCTAP_Pos)
  194. // | (1 << DWT_CTRL_CYCCNTENA_Pos);
  195. ITM->LAR = 0xC5ACCE55;
  196. ITM->TCR = (1 << ITM_TCR_DWTENA_Pos)
  197. | (1 << ITM_TCR_SYNCENA_Pos)
  198. | (1 << ITM_TCR_ITMENA_Pos);
  199. ITM->TER = 0xFFFFFFFF; // Enable all stimulus ports
  200. }
  201. // Enable needed clocks for GPIO
  202. rcu_periph_clock_enable(RCU_AF);
  203. rcu_periph_clock_enable(RCU_GPIOA);
  204. rcu_periph_clock_enable(RCU_GPIOB);
  205. rcu_periph_clock_enable(RCU_GPIOC);
  206. rcu_periph_clock_enable(RCU_GPIOD);
  207. rcu_periph_clock_enable(RCU_GPIOE);
  208. // Switch to SWD debug port (disable JTAG) to release PB4 as GPIO
  209. gpio_pin_remap_config(GPIO_SWJ_SWDPENABLE_REMAP, ENABLE);
  210. // SCSI pins.
  211. // Initialize open drain outputs to high.
  212. SCSI_RELEASE_OUTPUTS();
  213. // determine the ZulusSCSI board version
  214. g_zuluscsi_version = get_zuluscsi_version();
  215. g_moved_select_in = g_zuluscsi_version == ZSVersion_v1_1_ODE || g_zuluscsi_version == ZSVersion_v1_2;
  216. // Init SCSI pins GPIOs
  217. gpio_init(SCSI_OUT_PORT, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, SCSI_OUT_DATA_MASK | SCSI_OUT_REQ);
  218. gpio_init(SCSI_OUT_IO_PORT, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, SCSI_OUT_IO_PIN);
  219. gpio_init(SCSI_OUT_CD_PORT, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, SCSI_OUT_CD_PIN);
  220. gpio_init(SCSI_OUT_SEL_PORT, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, SCSI_OUT_SEL_PIN);
  221. gpio_init(SCSI_OUT_MSG_PORT, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, SCSI_OUT_MSG_PIN);
  222. gpio_init(SCSI_OUT_RST_PORT, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, SCSI_OUT_RST_PIN);
  223. gpio_init(SCSI_OUT_BSY_PORT, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, SCSI_OUT_BSY_PIN);
  224. gpio_init(SCSI_IN_PORT, GPIO_MODE_IN_FLOATING, 0, SCSI_IN_MASK);
  225. gpio_init(SCSI_ATN_PORT, GPIO_MODE_IN_FLOATING, 0, SCSI_ATN_PIN);
  226. gpio_init(SCSI_BSY_PORT, GPIO_MODE_IN_FLOATING, 0, SCSI_BSY_PIN);
  227. gpio_init(SCSI_ACK_PORT, GPIO_MODE_IN_FLOATING, 0, SCSI_ACK_PIN);
  228. gpio_init(SCSI_RST_PORT, GPIO_MODE_IN_FLOATING, 0, SCSI_RST_PIN);
  229. // Terminator enable
  230. gpio_bit_set(SCSI_TERM_EN_PORT, SCSI_TERM_EN_PIN);
  231. gpio_init(SCSI_TERM_EN_PORT, GPIO_MODE_OUT_PP, GPIO_OSPEED_2MHZ, SCSI_TERM_EN_PIN);
  232. #ifndef SD_USE_SDIO
  233. // SD card pins using SPI
  234. gpio_init(SD_PORT, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, SD_CS_PIN);
  235. gpio_init(SD_PORT, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, SD_CLK_PIN);
  236. gpio_init(SD_PORT, GPIO_MODE_IPU, 0, SD_MISO_PIN);
  237. gpio_init(SD_PORT, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, SD_MOSI_PIN);
  238. #else
  239. // SD card pins using SDIO
  240. gpio_init(SD_SDIO_DATA_PORT, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, SD_SDIO_D0 | SD_SDIO_D1 | SD_SDIO_D2 | SD_SDIO_D3);
  241. gpio_init(SD_SDIO_CLK_PORT, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, SD_SDIO_CLK);
  242. gpio_init(SD_SDIO_CMD_PORT, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, SD_SDIO_CMD);
  243. #endif
  244. #ifdef PLATFORM_VERSION_1_1_PLUS
  245. if (g_zuluscsi_version == ZSVersion_v1_1)
  246. {
  247. // SCSI Select
  248. gpio_init(SCSI_SEL_PORT, GPIO_MODE_IN_FLOATING, 0, SCSI_SEL_PIN);
  249. // DIP switches
  250. gpio_init(DIP_PORT, GPIO_MODE_IPD, 0, DIPSW1_PIN | DIPSW2_PIN | DIPSW3_PIN);
  251. gpio_init(EJECT_1_PORT, GPIO_MODE_IPU, 0, EJECT_1_PIN);
  252. gpio_init(EJECT_2_PORT, GPIO_MODE_IPU, 0, EJECT_2_PIN);
  253. }
  254. else if (g_zuluscsi_version == ZSVersion_v1_1_ODE)
  255. {
  256. // SCSI Select
  257. gpio_init(SCSI_ODE_SEL_PORT, GPIO_MODE_IN_FLOATING, 0, SCSI_ODE_SEL_PIN);
  258. // DIP switches
  259. gpio_init(ODE_DIP_PORT, GPIO_MODE_IPD, 0, ODE_DIPSW1_PIN | ODE_DIPSW2_PIN | ODE_DIPSW3_PIN);
  260. // Buttons
  261. gpio_init(EJECT_BTN_PORT, GPIO_MODE_IPU, 0, EJECT_BTN_PIN);
  262. gpio_init(USER_BTN_PORT, GPIO_MODE_IPU, 0, USER_BTN_PIN);
  263. init_audio_gpio();
  264. g_audio_enabled = true;
  265. }
  266. else if (g_zuluscsi_version == ZSVersion_v1_2)
  267. {
  268. // SCSI Select
  269. gpio_init(SCSI_ODE_SEL_PORT, GPIO_MODE_IN_FLOATING, 0, SCSI_ODE_SEL_PIN);
  270. // General settings DIP switch
  271. gpio_init(V1_2_DIPSW_TERM_PORT, GPIO_MODE_IPD, 0, V1_2_DIPSW_TERM_PIN);
  272. gpio_init(V1_2_DIPSW_DBG_PORT, GPIO_MODE_IPD, 0, V1_2_DIPSW_DBG_PIN);
  273. gpio_init(V1_2_DIPSW_QUIRKS_PORT, GPIO_MODE_IPD, 0, V1_2_DIPSW_QUIRKS_PIN);
  274. // Direct/Raw Mode Select
  275. gpio_init(V1_2_DIPSW_DIRECT_MODE_PORT, GPIO_MODE_IPD, 0, V1_2_DIPSW_DIRECT_MODE_PIN);
  276. // SCSI ID dip switch
  277. gpio_init(DIPSW_SCSI_ID_BIT_PORT, GPIO_MODE_IPD, 0, DIPSW_SCSI_ID_BIT_PINS);
  278. // Device select BCD rotary DIP switch
  279. gpio_init(DIPROT_DEVICE_SEL_BIT_PORT, GPIO_MODE_IPD, 0, DIPROT_DEVICE_SEL_BIT_PINS);
  280. // Buttons
  281. gpio_init(EJECT_BTN_PORT, GPIO_MODE_IPU, 0, EJECT_BTN_PIN);
  282. gpio_init(USER_BTN_PORT, GPIO_MODE_IPU, 0, USER_BTN_PIN);
  283. LED_EJECT_OFF();
  284. gpio_init(LED_EJECT_PORT, GPIO_MODE_OUT_PP, GPIO_OSPEED_2MHZ, LED_EJECT_PIN);
  285. }
  286. #else
  287. // SCSI Select
  288. gpio_init(SCSI_SEL_PORT, GPIO_MODE_IN_FLOATING, 0, SCSI_SEL_PIN);
  289. // DIP switches
  290. gpio_init(DIP_PORT, GPIO_MODE_IPD, 0, DIPSW1_PIN | DIPSW2_PIN | DIPSW3_PIN);
  291. // Ejection buttons
  292. gpio_init(EJECT_1_PORT, GPIO_MODE_IPU, 0, EJECT_1_PIN);
  293. gpio_init(EJECT_2_PORT, GPIO_MODE_IPU, 0, EJECT_2_PIN);
  294. #endif // PLATFORM_VERSION_1_1_PLUS
  295. // LED pins
  296. gpio_bit_set(LED_PORT, LED_PINS);
  297. gpio_init(LED_PORT, GPIO_MODE_OUT_PP, GPIO_OSPEED_2MHZ, LED_PINS);
  298. // SWO trace pin on PB3
  299. gpio_init(GPIOB, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_3);
  300. }
  301. static void set_termination(uint32_t port, uint32_t pin, const char *switch_name)
  302. {
  303. if (gpio_input_bit_get(port, pin))
  304. {
  305. logmsg(switch_name, " is ON: Enabling SCSI termination");
  306. gpio_bit_reset(SCSI_TERM_EN_PORT, SCSI_TERM_EN_PIN);
  307. }
  308. else
  309. {
  310. logmsg(switch_name, " is OFF: Disabling SCSI termination");
  311. }
  312. }
  313. static bool get_debug(uint32_t port, uint32_t pin, const char *switch_name)
  314. {
  315. if (gpio_input_bit_get(port, pin))
  316. {
  317. logmsg(switch_name, " is ON: Enabling debug messages");
  318. return true;
  319. }
  320. logmsg(switch_name, " is OFF: Disabling debug messages");
  321. return false;
  322. }
  323. static bool get_quirks(uint32_t port, uint32_t pin, const char *switch_name)
  324. {
  325. if (gpio_input_bit_get(port, pin))
  326. {
  327. logmsg(switch_name, " is ON: Enabling Apple quirks by default");
  328. return true;
  329. }
  330. logmsg(switch_name, " is OFF: Disabling Apple quirks mode by default");
  331. return false;
  332. }
  333. #ifdef PLATFORM_VERSION_1_1_PLUS
  334. static bool get_direct_mode(uint32_t port, uint32_t pin, const char *switch_name)
  335. {
  336. if (!gpio_input_bit_get(port, pin))
  337. {
  338. logmsg(switch_name, " is OFF: Enabling direct/raw mode");
  339. return true;
  340. }
  341. logmsg(switch_name, " is ON: Disabling direct/raw mode");
  342. return false;
  343. }
  344. #endif
  345. void platform_late_init()
  346. {
  347. // Initialize usb for CDC serial output
  348. usb_serial_init();
  349. g_usb_log_poll_func = &usb_log_poll;
  350. logmsg("Platform: ", g_platform_name);
  351. logmsg("FW Version: ", g_log_firmwareversion);
  352. #ifdef PLATFORM_VERSION_1_1_PLUS
  353. if (ZSVersion_v1_1 == g_zuluscsi_version)
  354. {
  355. logmsg("Board Version: ZuluSCSI v1.1 Standard Edition");
  356. set_termination(DIP_PORT, DIPSW3_PIN, "DIPSW3");
  357. g_log_debug = get_debug(DIP_PORT, DIPSW2_PIN, "DIPSW2");
  358. g_enable_apple_quirks = get_quirks(DIP_PORT, DIPSW1_PIN, "DIPSW1");
  359. greenpak_load_firmware();
  360. }
  361. else if (ZSVersion_v1_1_ODE == g_zuluscsi_version)
  362. {
  363. logmsg("Board Version: ZuluSCSI v1.1 ODE");
  364. logmsg("ODE - Optical Drive Emulator");
  365. set_termination(ODE_DIP_PORT, ODE_DIPSW3_PIN, "DIPSW3");
  366. g_log_debug = get_debug(ODE_DIP_PORT, ODE_DIPSW2_PIN, "DIPSW2");
  367. g_enable_apple_quirks = get_quirks(ODE_DIP_PORT, ODE_DIPSW1_PIN, "DIPSW1");
  368. audio_setup();
  369. }
  370. else if (ZSVersion_v1_2 == g_zuluscsi_version)
  371. {
  372. logmsg("Board Version: ZuluSCSI v1.2");
  373. hw_config_init_gpios();
  374. set_termination(V1_2_DIPSW_TERM_PORT, V1_2_DIPSW_TERM_PIN, "DIPSW4");
  375. g_log_debug = get_debug(V1_2_DIPSW_DBG_PORT, V1_2_DIPSW_DBG_PIN, "DIPSW3");
  376. g_direct_mode = get_direct_mode(V1_2_DIPSW_DIRECT_MODE_PORT, V1_2_DIPSW_DIRECT_MODE_PIN, "DIPSW2");
  377. g_enable_apple_quirks = get_quirks(V1_2_DIPSW_QUIRKS_PORT, V1_2_DIPSW_QUIRKS_PIN, "DIPSW1");
  378. hw_config_init_state(g_direct_mode);
  379. }
  380. #else // PLATFORM_VERSION_1_1_PLUS - ZuluSCSI v1.0 and v1.0 minis gpio config
  381. #ifdef ZULUSCSI_V1_0_mini
  382. logmsg("SCSI termination is always on");
  383. #elif defined(ZULUSCSI_V1_0)
  384. set_termination(DIP_PORT, DIPSW3_PIN, "DIPSW3");
  385. g_log_debug = get_debug(DIP_PORT, DIPSW2_PIN, "DIPSW2");
  386. g_enable_apple_quirks = get_quirks(DIP_PORT, DIPSW1_PIN, "DIPSW1");
  387. #endif // ZULUSCSI_V1_0_mini
  388. #endif // PLATFORM_VERSION_1_1_PLUS
  389. }
  390. void platform_post_sd_card_init()
  391. {
  392. #ifdef PLATFORM_VERSION_1_1_PLUS
  393. if (ZSVersion_v1_2 == g_zuluscsi_version && g_scsi_settings.getSystem()->enableCDAudio)
  394. {
  395. logmsg("Audio enabled - an external audio DAC is required on the I2S expansion header");
  396. init_audio_gpio();
  397. g_audio_enabled = true;
  398. audio_setup();
  399. }
  400. #endif
  401. }
  402. void platform_write_led(bool state)
  403. {
  404. if (g_led_blinking) return;
  405. if (g_scsi_settings.getSystem()->invertStatusLed)
  406. state = !state;
  407. if (state)
  408. gpio_bit_reset(LED_PORT, LED_PINS);
  409. else
  410. gpio_bit_set(LED_PORT, LED_PINS);
  411. }
  412. void platform_set_blink_status(bool status)
  413. {
  414. g_led_blinking = status;
  415. }
  416. void platform_write_led_override(bool state)
  417. {
  418. if (g_scsi_settings.getSystem()->invertStatusLed)
  419. state = !state;
  420. if (state)
  421. gpio_bit_reset(LED_PORT, LED_PINS);
  422. else
  423. gpio_bit_set(LED_PORT, LED_PINS);
  424. }
  425. void platform_disable_led(void)
  426. {
  427. gpio_init(LED_PORT, GPIO_MODE_IPU, 0, LED_PINS);
  428. logmsg("Disabling status LED");
  429. }
  430. uint8_t platform_no_sd_card_on_init_error_code()
  431. {
  432. return 0x80 | SD_CMD_RESP_TIMEOUT;
  433. }
  434. /*****************************************/
  435. /* Supply voltage monitor */
  436. /*****************************************/
  437. // Use ADC to implement supply voltage monitoring for the +3.0V rail.
  438. // This works by sampling the Vrefint, which has
  439. // a voltage of 1.2 V, allowing to calculate the VDD voltage.
  440. static void adc_poll()
  441. {
  442. #if PLATFORM_VDD_WARNING_LIMIT_mV > 0
  443. static bool initialized = false;
  444. static int lowest_vdd_seen = PLATFORM_VDD_WARNING_LIMIT_mV;
  445. if (!initialized)
  446. {
  447. rcu_periph_clock_enable(RCU_ADC0);
  448. adc_enable(ADC0);
  449. adc_calibration_enable(ADC0);
  450. adc_tempsensor_vrefint_enable();
  451. adc_inserted_channel_config(ADC0, 0, ADC_CHANNEL_17, ADC_SAMPLETIME_239POINT5);
  452. adc_external_trigger_source_config(ADC0, ADC_INSERTED_CHANNEL, ADC0_1_2_EXTTRIG_INSERTED_NONE);
  453. adc_external_trigger_config(ADC0, ADC_INSERTED_CHANNEL, ENABLE);
  454. adc_software_trigger_enable(ADC0, ADC_INSERTED_CHANNEL);
  455. initialized = true;
  456. }
  457. // Read previous result and start new one
  458. int adc_value = ADC_IDATA0(ADC0);
  459. adc_software_trigger_enable(ADC0, ADC_INSERTED_CHANNEL);
  460. // adc_value = 1200mV * 4096 / Vdd
  461. // => Vdd = 1200mV * 4096 / adc_value
  462. // To avoid wasting time on division, compare against
  463. // limit directly.
  464. const int limit = (1200 * 4096) / PLATFORM_VDD_WARNING_LIMIT_mV;
  465. if (adc_value > limit)
  466. {
  467. // Warn once, and then again if we detect even a lower drop.
  468. int vdd_mV = (1200 * 4096) / adc_value;
  469. if (vdd_mV < lowest_vdd_seen)
  470. {
  471. logmsg("WARNING: Detected supply voltage drop to ", vdd_mV, "mV. Verify power supply is adequate.");
  472. lowest_vdd_seen = vdd_mV - 50; // Small hysteresis to avoid excessive warnings
  473. }
  474. }
  475. #endif
  476. }
  477. /*****************************************/
  478. /* Debug logging and watchdog */
  479. /*****************************************/
  480. // Send log data to USB UART if USB is connected.
  481. // Data is retrieved from the shared log ring buffer and
  482. // this function sends as much as fits in USB CDC buffer.
  483. static void usb_log_poll()
  484. {
  485. static uint32_t logpos = 0;
  486. if (usb_serial_ready())
  487. {
  488. // Retrieve pointer to log start and determine number of bytes available.
  489. uint32_t available = 0;
  490. const char *data = log_get_buffer(&logpos, &available);
  491. // Limit to CDC packet size
  492. uint32_t len = available;
  493. if (len == 0) return;
  494. if (len > USB_CDC_DATA_PACKET_SIZE) len = USB_CDC_DATA_PACKET_SIZE;
  495. // Update log position by the actual number of bytes sent
  496. // If USB CDC buffer is full, this may be 0
  497. usb_serial_send((uint8_t*)data, len);
  498. logpos -= available - len;
  499. }
  500. }
  501. /*****************************************/
  502. /* Crash handlers */
  503. /*****************************************/
  504. // Writes log data to the PB3 SWO pin
  505. void platform_log(const char *s)
  506. {
  507. while (*s)
  508. {
  509. // Write to SWO pin
  510. while (ITM->PORT[0].u32 == 0);
  511. ITM->PORT[0].u8 = *s++;
  512. }
  513. }
  514. void platform_emergency_log_save()
  515. {
  516. if (g_rawdrive_active)
  517. return;
  518. #ifdef ZULUSCSI_HARDWARE_CONFIG
  519. if (g_hw_config.is_active())
  520. return;
  521. #endif
  522. platform_set_sd_callback(NULL, NULL);
  523. SD.begin(SD_CONFIG_CRASH);
  524. FsFile crashfile = SD.open(CRASHFILE, O_WRONLY | O_CREAT | O_TRUNC);
  525. if (!crashfile.isOpen())
  526. {
  527. // Try to reinitialize
  528. int max_retry = 10;
  529. while (max_retry-- > 0 && !SD.begin(SD_CONFIG_CRASH));
  530. crashfile = SD.open(CRASHFILE, O_WRONLY | O_CREAT | O_TRUNC);
  531. }
  532. uint32_t startpos = 0;
  533. crashfile.write(log_get_buffer(&startpos));
  534. crashfile.write(log_get_buffer(&startpos));
  535. crashfile.flush();
  536. crashfile.close();
  537. }
  538. extern uint32_t _estack;
  539. __attribute__((noinline))
  540. void show_hardfault(uint32_t *sp)
  541. {
  542. uint32_t pc = sp[6];
  543. uint32_t lr = sp[5];
  544. uint32_t cfsr = SCB->CFSR;
  545. logmsg("--------------");
  546. logmsg("CRASH!");
  547. logmsg("Platform: ", g_platform_name);
  548. logmsg("FW Version: ", g_log_firmwareversion);
  549. logmsg("scsiDev.cdb: ", bytearray(scsiDev.cdb, 12));
  550. logmsg("scsiDev.phase: ", (int)scsiDev.phase);
  551. logmsg("CFSR: ", cfsr);
  552. logmsg("SP: ", (uint32_t)sp);
  553. logmsg("PC: ", pc);
  554. logmsg("LR: ", lr);
  555. logmsg("R0: ", sp[0]);
  556. logmsg("R1: ", sp[1]);
  557. logmsg("R2: ", sp[2]);
  558. logmsg("R3: ", sp[3]);
  559. uint32_t *p = (uint32_t*)((uint32_t)sp & ~3);
  560. for (int i = 0; i < 8; i++)
  561. {
  562. if (p == &_estack) break; // End of stack
  563. logmsg("STACK ", (uint32_t)p, ": ", p[0], " ", p[1], " ", p[2], " ", p[3]);
  564. p += 4;
  565. }
  566. platform_emergency_log_save();
  567. while (1)
  568. {
  569. if (g_usb_log_poll_func) g_usb_log_poll_func();
  570. // Flash the crash address on the LED
  571. // Short pulse means 0, long pulse means 1
  572. int base_delay = 1000;
  573. for (int i = 31; i >= 0; i--)
  574. {
  575. LED_OFF();
  576. for (int j = 0; j < base_delay; j++) delay_ns(100000);
  577. int delay = (pc & (1 << i)) ? (3 * base_delay) : base_delay;
  578. LED_ON();
  579. for (int j = 0; j < delay; j++) delay_ns(100000);
  580. LED_OFF();
  581. }
  582. for (int j = 0; j < base_delay * 10; j++) delay_ns(100000);
  583. }
  584. }
  585. __attribute__((naked, interrupt))
  586. void HardFault_Handler(void)
  587. {
  588. // Copies stack pointer into first argument
  589. asm("mrs r0, msp\n"
  590. "b show_hardfault": : : "r0");
  591. }
  592. __attribute__((naked, interrupt))
  593. void MemManage_Handler(void)
  594. {
  595. asm("mrs r0, msp\n"
  596. "b show_hardfault": : : "r0");
  597. }
  598. __attribute__((naked, interrupt))
  599. void BusFault_Handler(void)
  600. {
  601. asm("mrs r0, msp\n"
  602. "b show_hardfault": : : "r0");
  603. }
  604. __attribute__((naked, interrupt))
  605. void UsageFault_Handler(void)
  606. {
  607. asm("mrs r0, msp\n"
  608. "b show_hardfault": : : "r0");
  609. }
  610. void __assert_func(const char *file, int line, const char *func, const char *expr)
  611. {
  612. uint32_t dummy = 0;
  613. logmsg("--------------");
  614. logmsg("ASSERT FAILED!");
  615. logmsg("Platform: ", g_platform_name);
  616. logmsg("FW Version: ", g_log_firmwareversion);
  617. logmsg("scsiDev.cdb: ", bytearray(scsiDev.cdb, 12));
  618. logmsg("scsiDev.phase: ", (int)scsiDev.phase);
  619. logmsg("Assert failed: ", file , ":", line, " in ", func, ":", expr);
  620. uint32_t *p = (uint32_t*)((uint32_t)&dummy & ~3);
  621. for (int i = 0; i < 8; i++)
  622. {
  623. if (p == &_estack) break; // End of stack
  624. logmsg("STACK ", (uint32_t)p, ": ", p[0], " ", p[1], " ", p[2], " ", p[3]);
  625. p += 4;
  626. }
  627. platform_emergency_log_save();
  628. while(1)
  629. {
  630. if (g_usb_log_poll_func) g_usb_log_poll_func();
  631. LED_OFF();
  632. for (int j = 0; j < 1000; j++) delay_ns(100000);
  633. LED_ON();
  634. for (int j = 0; j < 1000; j++) delay_ns(100000);
  635. }
  636. }
  637. } /* extern "C" */
  638. static void watchdog_handler(uint32_t *sp)
  639. {
  640. logmsg("-------------- WATCHDOG TIMEOUT");
  641. show_hardfault(sp);
  642. }
  643. void platform_reset_watchdog()
  644. {
  645. // This uses a software watchdog based on systick timer interrupt.
  646. // It gives us opportunity to collect better debug info than the
  647. // full hardware reset that would be caused by hardware watchdog.
  648. g_watchdog_timeout = WATCHDOG_CRASH_TIMEOUT;
  649. // USB log is polled here also to make sure any log messages in fault states
  650. // get passed to USB.
  651. usb_log_poll();
  652. }
  653. void platform_reset_mcu()
  654. {
  655. // reset in 2 sec ( 1 / (40KHz / 32) * 2500 == 2sec)
  656. fwdgt_config(2500, FWDGT_PSC_DIV32);
  657. fwdgt_enable();
  658. }
  659. // Poll function that is called every few milliseconds.
  660. // Can be left empty or used for platform-specific processing.
  661. void platform_poll()
  662. {
  663. #ifdef ENABLE_AUDIO_OUTPUT
  664. audio_poll();
  665. #endif
  666. adc_poll();
  667. usb_log_poll();
  668. }
  669. uint8_t platform_get_buttons()
  670. {
  671. // Buttons are active low: internal pull-up is enabled,
  672. // and when button is pressed the pin goes low.
  673. uint8_t buttons = 0;
  674. #ifdef PLATFORM_VERSION_1_1_PLUS
  675. if (g_zuluscsi_version == ZSVersion_v1_1_ODE || g_zuluscsi_version == ZSVersion_v1_2)
  676. {
  677. if (!gpio_input_bit_get(EJECT_BTN_PORT, EJECT_BTN_PIN)) buttons |= 1;
  678. if (!gpio_input_bit_get(USER_BTN_PORT, USER_BTN_PIN)) buttons |= 4;
  679. }
  680. else
  681. {
  682. if (!gpio_input_bit_get(EJECT_1_PORT, EJECT_1_PIN)) buttons |= 1;
  683. if (!gpio_input_bit_get(EJECT_2_PORT, EJECT_2_PIN)) buttons |= 2;
  684. }
  685. #else
  686. if (!gpio_input_bit_get(EJECT_1_PORT, EJECT_1_PIN)) buttons |= 1;
  687. if (!gpio_input_bit_get(EJECT_2_PORT, EJECT_2_PIN)) buttons |= 2;
  688. #endif
  689. // Simple debouncing logic: handle button releases after 100 ms delay.
  690. static uint32_t debounce;
  691. static uint8_t buttons_debounced = 0;
  692. if (buttons != 0)
  693. {
  694. buttons_debounced = buttons;
  695. debounce = millis();
  696. }
  697. else if ((uint32_t)(millis() - debounce) > 100)
  698. {
  699. buttons_debounced = 0;
  700. }
  701. #ifdef PLATFORM_VERSION_1_1_PLUS
  702. if(g_zuluscsi_version == ZSVersion_v1_1_ODE || g_zuluscsi_version == ZSVersion_v1_2)
  703. {
  704. static uint8_t previous = 0x00;
  705. uint8_t bitmask = buttons_debounced & USER_BTN_MASK;
  706. uint8_t ejectors = (previous ^ bitmask) & previous;
  707. previous = bitmask;
  708. if (ejectors & USER_BTN_MASK)
  709. {
  710. logmsg("User button pressed - feature not yet implemented");
  711. }
  712. }
  713. #endif
  714. return buttons_debounced;
  715. }
  716. /***********************/
  717. /* Flash reprogramming */
  718. /***********************/
  719. bool platform_rewrite_flash_page(uint32_t offset, uint8_t buffer[PLATFORM_FLASH_PAGE_SIZE])
  720. {
  721. if (offset == 0)
  722. {
  723. if (buffer[3] != 0x20 || buffer[7] != 0x08)
  724. {
  725. logmsg("Invalid firmware file, starts with: ", bytearray(buffer, 16));
  726. return false;
  727. }
  728. }
  729. dbgmsg("Writing flash at offset ", offset, " data ", bytearray(buffer, 4));
  730. assert(offset % PLATFORM_FLASH_PAGE_SIZE == 0);
  731. assert(offset >= PLATFORM_BOOTLOADER_SIZE);
  732. fmc_unlock();
  733. fmc_bank0_unlock();
  734. fmc_state_enum status;
  735. status = fmc_page_erase(FLASH_BASE + offset);
  736. if (status != FMC_READY)
  737. {
  738. logmsg("Erase failed: ", (int)status);
  739. return false;
  740. }
  741. uint32_t *buf32 = (uint32_t*)buffer;
  742. uint32_t num_words = PLATFORM_FLASH_PAGE_SIZE / 4;
  743. for (int i = 0; i < num_words; i++)
  744. {
  745. status = fmc_word_program(FLASH_BASE + offset + i * 4, buf32[i]);
  746. if (status != FMC_READY)
  747. {
  748. logmsg("Flash write failed: ", (int)status);
  749. return false;
  750. }
  751. }
  752. fmc_lock();
  753. for (int i = 0; i < num_words; i++)
  754. {
  755. uint32_t expected = buf32[i];
  756. uint32_t actual = *(volatile uint32_t*)(FLASH_BASE + offset + i * 4);
  757. if (actual != expected)
  758. {
  759. logmsg("Flash verify failed at offset ", offset + i * 4, " got ", actual, " expected ", expected);
  760. return false;
  761. }
  762. }
  763. return true;
  764. }
  765. void platform_boot_to_main_firmware()
  766. {
  767. uint32_t *mainprogram_start = (uint32_t*)(0x08000000 + PLATFORM_BOOTLOADER_SIZE);
  768. SCB->VTOR = (uint32_t)mainprogram_start;
  769. __asm__(
  770. "msr msp, %0\n\t"
  771. "bx %1" : : "r" (mainprogram_start[0]),
  772. "r" (mainprogram_start[1]) : "memory");
  773. }
  774. /**************************************/
  775. /* SCSI configuration based on DIPSW1 */
  776. /**************************************/
  777. void platform_config_hook(S2S_TargetCfg *config)
  778. {
  779. // Enable Apple quirks by dip switch
  780. if (g_enable_apple_quirks)
  781. {
  782. if (config->quirks == S2S_CFG_QUIRKS_NONE)
  783. {
  784. config->quirks = S2S_CFG_QUIRKS_APPLE;
  785. }
  786. }
  787. }
  788. /**********************************************/
  789. /* Mapping from data bytes to GPIO BOP values */
  790. /**********************************************/
  791. #define PARITY(n) ((1 ^ (n) ^ ((n)>>1) ^ ((n)>>2) ^ ((n)>>3) ^ ((n)>>4) ^ ((n)>>5) ^ ((n)>>6) ^ ((n)>>7)) & 1)
  792. #define X(n) (\
  793. ((n & 0x01) ? (SCSI_OUT_DB0 << 16) : SCSI_OUT_DB0) | \
  794. ((n & 0x02) ? (SCSI_OUT_DB1 << 16) : SCSI_OUT_DB1) | \
  795. ((n & 0x04) ? (SCSI_OUT_DB2 << 16) : SCSI_OUT_DB2) | \
  796. ((n & 0x08) ? (SCSI_OUT_DB3 << 16) : SCSI_OUT_DB3) | \
  797. ((n & 0x10) ? (SCSI_OUT_DB4 << 16) : SCSI_OUT_DB4) | \
  798. ((n & 0x20) ? (SCSI_OUT_DB5 << 16) : SCSI_OUT_DB5) | \
  799. ((n & 0x40) ? (SCSI_OUT_DB6 << 16) : SCSI_OUT_DB6) | \
  800. ((n & 0x80) ? (SCSI_OUT_DB7 << 16) : SCSI_OUT_DB7) | \
  801. (PARITY(n) ? (SCSI_OUT_DBP << 16) : SCSI_OUT_DBP) | \
  802. (SCSI_OUT_REQ) \
  803. )
  804. const uint32_t g_scsi_out_byte_to_bop[256] =
  805. {
  806. X(0x00), X(0x01), X(0x02), X(0x03), X(0x04), X(0x05), X(0x06), X(0x07), X(0x08), X(0x09), X(0x0a), X(0x0b), X(0x0c), X(0x0d), X(0x0e), X(0x0f),
  807. X(0x10), X(0x11), X(0x12), X(0x13), X(0x14), X(0x15), X(0x16), X(0x17), X(0x18), X(0x19), X(0x1a), X(0x1b), X(0x1c), X(0x1d), X(0x1e), X(0x1f),
  808. X(0x20), X(0x21), X(0x22), X(0x23), X(0x24), X(0x25), X(0x26), X(0x27), X(0x28), X(0x29), X(0x2a), X(0x2b), X(0x2c), X(0x2d), X(0x2e), X(0x2f),
  809. X(0x30), X(0x31), X(0x32), X(0x33), X(0x34), X(0x35), X(0x36), X(0x37), X(0x38), X(0x39), X(0x3a), X(0x3b), X(0x3c), X(0x3d), X(0x3e), X(0x3f),
  810. X(0x40), X(0x41), X(0x42), X(0x43), X(0x44), X(0x45), X(0x46), X(0x47), X(0x48), X(0x49), X(0x4a), X(0x4b), X(0x4c), X(0x4d), X(0x4e), X(0x4f),
  811. X(0x50), X(0x51), X(0x52), X(0x53), X(0x54), X(0x55), X(0x56), X(0x57), X(0x58), X(0x59), X(0x5a), X(0x5b), X(0x5c), X(0x5d), X(0x5e), X(0x5f),
  812. X(0x60), X(0x61), X(0x62), X(0x63), X(0x64), X(0x65), X(0x66), X(0x67), X(0x68), X(0x69), X(0x6a), X(0x6b), X(0x6c), X(0x6d), X(0x6e), X(0x6f),
  813. X(0x70), X(0x71), X(0x72), X(0x73), X(0x74), X(0x75), X(0x76), X(0x77), X(0x78), X(0x79), X(0x7a), X(0x7b), X(0x7c), X(0x7d), X(0x7e), X(0x7f),
  814. X(0x80), X(0x81), X(0x82), X(0x83), X(0x84), X(0x85), X(0x86), X(0x87), X(0x88), X(0x89), X(0x8a), X(0x8b), X(0x8c), X(0x8d), X(0x8e), X(0x8f),
  815. X(0x90), X(0x91), X(0x92), X(0x93), X(0x94), X(0x95), X(0x96), X(0x97), X(0x98), X(0x99), X(0x9a), X(0x9b), X(0x9c), X(0x9d), X(0x9e), X(0x9f),
  816. X(0xa0), X(0xa1), X(0xa2), X(0xa3), X(0xa4), X(0xa5), X(0xa6), X(0xa7), X(0xa8), X(0xa9), X(0xaa), X(0xab), X(0xac), X(0xad), X(0xae), X(0xaf),
  817. X(0xb0), X(0xb1), X(0xb2), X(0xb3), X(0xb4), X(0xb5), X(0xb6), X(0xb7), X(0xb8), X(0xb9), X(0xba), X(0xbb), X(0xbc), X(0xbd), X(0xbe), X(0xbf),
  818. X(0xc0), X(0xc1), X(0xc2), X(0xc3), X(0xc4), X(0xc5), X(0xc6), X(0xc7), X(0xc8), X(0xc9), X(0xca), X(0xcb), X(0xcc), X(0xcd), X(0xce), X(0xcf),
  819. X(0xd0), X(0xd1), X(0xd2), X(0xd3), X(0xd4), X(0xd5), X(0xd6), X(0xd7), X(0xd8), X(0xd9), X(0xda), X(0xdb), X(0xdc), X(0xdd), X(0xde), X(0xdf),
  820. X(0xe0), X(0xe1), X(0xe2), X(0xe3), X(0xe4), X(0xe5), X(0xe6), X(0xe7), X(0xe8), X(0xe9), X(0xea), X(0xeb), X(0xec), X(0xed), X(0xee), X(0xef),
  821. X(0xf0), X(0xf1), X(0xf2), X(0xf3), X(0xf4), X(0xf5), X(0xf6), X(0xf7), X(0xf8), X(0xf9), X(0xfa), X(0xfb), X(0xfc), X(0xfd), X(0xfe), X(0xff)
  822. };
  823. #undef X