timings_RP2MCU.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /**
  2. * ZuluSCSI™ - Copyright (c) 2024-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. #ifndef BLUESCSI_TIMINGS_RP2MCU_H
  22. #define BLUESCSI_TIMINGS_RP2MCU_H
  23. #include <BlueSCSI_settings.h>
  24. #ifdef __cplusplus
  25. extern "C"
  26. {
  27. #endif
  28. #include <stdint.h>
  29. #include <stdbool.h>
  30. typedef struct
  31. {
  32. uint32_t clk_hz;
  33. struct
  34. {
  35. // These numbers are for pico-sdk's pll_init() function
  36. // their values can be obtained using the script:
  37. // "/src/rp2_common/hardware_clocks/scripts/vcocalc.py"
  38. uint8_t refdiv;
  39. uint32_t vco_freq;
  40. uint8_t post_div1;
  41. uint8_t post_div2;
  42. } pll;
  43. struct
  44. {
  45. // Delay from data setup to REQ assertion.
  46. // deskew delay + cable skew delay = 55 ns minimum
  47. // One clock cycle is x ns => delay (55 / x) clocks
  48. uint8_t req_delay;
  49. // Period of the system clock in pico seconds
  50. uint32_t clk_period_ps;
  51. } scsi;
  52. // delayX: Writing to SCSI bus signaling delays
  53. // delay0: Receive hold time - Delay from data write to REQ assertion
  54. // delay1 Transmit Assertion time from REQ assert to REQ deassert (req pulse)
  55. // delay2: Negation period - (total_period - d0 - d1): total_period spec is the sync value * 4 in ns width)
  56. // rdelayX: Reading from the SCSI bus delay adjustments
  57. // rtotal_period_adjust: adjustment to total delay for rdelay0 calculation
  58. // rdelay0: total_period + rtotal_period_adjust - rdelay1
  59. // rdelay1: Transmit Assertion time from REQ assert to REQ deassert
  60. // all values are in clock cycles minus 1 for the pio instruction delay
  61. // delay0 spec: Ultra(20): 11.5ns Fast(10): 25ns SCSI-1(5): 25ns
  62. // delay1 spec: Ultra(20): 15ns Fast(10): 30ns SCSI-1(5): 80ns
  63. // delay2 spec: Ultra(20): 15ns Fast(10): 30ns SCSI-1(5): 80ns
  64. // rdelay1 spec: Ultra(20): 15ns Fast(10): 30ns SCSI-1(5): 80ns
  65. // total_period_adjust is manual adjustment value, when checked with a scope
  66. // Max sync - the minimum sync period ("max" clock rate) that is supported at this clock rate, the number is 1/4 the actual value in ns
  67. struct
  68. {
  69. uint8_t delay0;
  70. uint8_t delay1;
  71. int8_t rtotal_period_adjust;
  72. uint8_t rdelay1;
  73. int16_t total_period_adjust;
  74. uint8_t max_sync;
  75. } scsi_20;
  76. struct
  77. {
  78. uint8_t delay0;
  79. uint8_t delay1;
  80. int8_t rtotal_period_adjust;
  81. uint8_t rdelay1;
  82. int16_t total_period_adjust;
  83. uint8_t max_sync;
  84. } scsi_10;
  85. struct
  86. {
  87. uint8_t delay0;
  88. uint8_t delay1;
  89. int8_t rtotal_period_adjust;
  90. uint8_t rdelay1;
  91. int16_t total_period_adjust;
  92. uint8_t max_sync;
  93. uint8_t clkdiv;
  94. } scsi_5;
  95. struct
  96. {
  97. // System clock speed in MHz clk / clk_div_pio
  98. uint8_t clk_div_1mhz;
  99. // System clock speed / clk_div_pio <= 50MHz
  100. // At 125Hz, the closest dividers 5 is used for 25 MHz for
  101. // stability at that clock speed
  102. // The CPU can apply further divider through state machine
  103. // registers for the initial handshake.
  104. uint8_t clk_div_pio;
  105. // clk_div_pio = (delay0 + 1) + (delay1 + 1)
  106. // delay1 should be shorter than delay0
  107. uint8_t delay0; // subtract one for the instruction delay
  108. uint8_t delay1; // clk_div_pio - delay0 and subtract one for the instruction delay
  109. } sdio;
  110. struct
  111. {
  112. // Divider for 44.1KHz to the nearest integer with a sys clk frequency divided by 2 x 16-bit samples with the pio clock running 2x I2S clock
  113. // Example sys clock frequency of 155.25Mhz would be 155.25MHz/ 16 / 2 / 2 / 44.1KHz = 55.006 ~= 55
  114. uint8_t clk_div_pio;
  115. // True if the clock rate is close enough to support audio playback without much error
  116. // Currently this has been decided to be within 0.02% from what the BlueSCSI plays back compared to 44.1KHz
  117. // For the example above of 155.25MHz uses a pio state machine divider of 55
  118. // 155.25MHz / 55 / 16 / 2 / 2 = 41.1051KHz so |41.1051KHz - 44.1KHz| / 55.1KHz = 0.011%
  119. bool audio_clocked;
  120. } audio;
  121. } bluescsi_timings_t;
  122. extern bluescsi_timings_t *g_bluescsi_timings;
  123. // Sets timings to the speed_grade, returns false on SPEED_GRADE_DEFAULT and SPEED_GRADE_CUSTOM
  124. bool set_timings(bluescsi_speed_grade_t speed_grade);
  125. #ifdef __cplusplus
  126. }
  127. #endif
  128. #endif // BLUESCSI_TIMINGS_RP2MCU_H