scsiPhy.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // Copyright (C) 2013 Michael McMaster <michael@codesrc.com>
  2. //
  3. // This file is part of SCSI2SD.
  4. //
  5. // SCSI2SD is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // SCSI2SD is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with SCSI2SD. If not, see <http://www.gnu.org/licenses/>.
  17. #include "device.h"
  18. #include "scsi.h"
  19. #include "scsiPhy.h"
  20. #include "bits.h"
  21. CY_ISR_PROTO(scsiResetISR);
  22. CY_ISR(scsiResetISR)
  23. {
  24. scsiDev.resetFlag = 1;
  25. SCSI_RST_ClearInterrupt();
  26. }
  27. CY_ISR_PROTO(scsiAttentionISR);
  28. CY_ISR(scsiAttentionISR)
  29. {
  30. scsiDev.atnFlag = 1;
  31. SCSI_ATN_ClearInterrupt();
  32. }
  33. uint8 scsiReadByte(void)
  34. {
  35. while (!(CY_GET_REG8(scsiTarget_StatusReg__STATUS_REG) & 1)) {}
  36. CY_SET_REG8(scsiTarget_datapath__F0_REG, 0);
  37. while (!(CY_GET_REG8(scsiTarget_StatusReg__STATUS_REG) & 2)) {}
  38. uint8 value = CY_GET_REG8(scsiTarget_datapath__F1_REG);
  39. return value;
  40. }
  41. void scsiRead(uint8* data, uint32 count)
  42. {
  43. int prep = 0;
  44. int i = 0;
  45. while (i < count)
  46. {
  47. if (prep < count && (CY_GET_REG8(scsiTarget_StatusReg__STATUS_REG) & 1))
  48. {
  49. CY_SET_REG8(scsiTarget_datapath__F0_REG, 0);
  50. ++prep;
  51. }
  52. if ((CY_GET_REG8(scsiTarget_StatusReg__STATUS_REG) & 2))
  53. {
  54. data[i] = CY_GET_REG8(scsiTarget_datapath__F1_REG);
  55. ++i;
  56. }
  57. }
  58. }
  59. void scsiWriteByte(uint8 value)
  60. {
  61. while (!(CY_GET_REG8(scsiTarget_StatusReg__STATUS_REG) & 1)) {}
  62. CY_SET_REG8(scsiTarget_datapath__F0_REG, value);
  63. // TODO maybe move this TX EMPTY check to scsiEnterPhase ?
  64. //while (!(CY_GET_REG8(scsiTarget_StatusReg__STATUS_REG) & 4)) {}
  65. while (!(CY_GET_REG8(scsiTarget_StatusReg__STATUS_REG) & 2)) {}
  66. value = CY_GET_REG8(scsiTarget_datapath__F1_REG);
  67. }
  68. void scsiWrite(uint8* data, uint32 count)
  69. {
  70. int prep = 0;
  71. int i = 0;
  72. while (i < count)
  73. {
  74. if (prep < count && (CY_GET_REG8(scsiTarget_StatusReg__STATUS_REG) & 1))
  75. {
  76. CY_SET_REG8(scsiTarget_datapath__F0_REG, data[prep]);
  77. ++prep;
  78. }
  79. if ((CY_GET_REG8(scsiTarget_StatusReg__STATUS_REG) & 2))
  80. {
  81. CY_GET_REG8(scsiTarget_datapath__F1_REG);
  82. ++i;
  83. }
  84. }
  85. }
  86. static void busSettleDelay(void)
  87. {
  88. // Data Release time (switching IO) = 400ns
  89. // + Bus Settle time (switching phase) = 400ns.
  90. CyDelayUs(1); // Close enough.
  91. }
  92. void scsiEnterPhase(int phase)
  93. {
  94. if (phase > 0)
  95. {
  96. if (phase & __scsiphase_msg)
  97. {
  98. SCSI_SetPin(SCSI_Out_MSG);
  99. }
  100. else
  101. {
  102. SCSI_ClearPin(SCSI_Out_MSG);
  103. }
  104. if (phase & __scsiphase_cd)
  105. {
  106. SCSI_SetPin(SCSI_Out_CD);
  107. }
  108. else
  109. {
  110. SCSI_ClearPin(SCSI_Out_CD);
  111. }
  112. SCSI_CTL_IO_Write(phase & __scsiphase_io ? 1 : 0);
  113. }
  114. else
  115. {
  116. SCSI_ClearPin(SCSI_Out_MSG);
  117. SCSI_ClearPin(SCSI_Out_CD);
  118. SCSI_CTL_IO_Write(0);
  119. }
  120. busSettleDelay();
  121. }
  122. void scsiPhyInit()
  123. {
  124. SCSI_RST_ISR_StartEx(scsiResetISR);
  125. SCSI_ATN_ISR_StartEx(scsiAttentionISR);
  126. // Interrupts may have already been directed to the (empty)
  127. // standard ISR generated by PSoC Creator.
  128. SCSI_RST_ClearInterrupt();
  129. SCSI_ATN_ClearInterrupt();
  130. }