2
0

serial.inc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. (*
  2. $Id: serial.inc 25 2007-12-10 21:06:46Z p4p3r0 $
  3. ------------------------------------------------------------------------------
  4. Copyright (C) 2005
  5. Jason Rogers (dovoto)
  6. Dave Murphy (WinterMute)
  7. This software is provided 'as-is', without any express or implied
  8. warranty. In no event will the authors be held liable for any
  9. damages arising from the use of this software.
  10. Permission is granted to anyone to use this software for any
  11. purpose, including commercial applications, and to alter it and
  12. redistribute it freely, subject to the following restrictions:
  13. 1. The origin of this software must not be misrepresented; you
  14. must not claim that you wrote the original software. If you use
  15. this software in a product, an acknowledgment in the product
  16. documentation would be appreciated but is not required.
  17. 2. Altered source versions must be plainly marked as such, and
  18. must not be misrepresented as being the original software.
  19. 3. This notice may not be removed or altered from any source
  20. distribution.
  21. ------------------------------------------------------------------------------
  22. Conversion by Legolas (http://itaprogaming.free.fr) for freepascal compiler
  23. (http://www.freepascal.org)
  24. Copyright (C) 2006 Francesco Lombardi
  25. Check http://sourceforge.net/projects/libndsfpc for updates
  26. ------------------------------------------------------------------------------
  27. $Log$
  28. *)
  29. {$ifndef ARM7}
  30. {$error Serial header is for ARM7 only}
  31. {$endif ARM7}
  32. {$ifdef NDS_INTERFACE}
  33. const
  34. // 'Networking'
  35. REG_RCNT : pcuint16 = pointer($04000134);
  36. REG_KEYXY : pcuint16 = pointer($04000136);
  37. RTC_CR : pcuint16 = pointer($04000138);
  38. RTC_CR8 : pcuint8 = pointer($04000138);
  39. REG_SIOCNT : pcuint16 = pointer($04000128);
  40. SIO_DATA8 : pcuint8 = pointer($0400012A);
  41. SIO_DATA32 : pcuint32 = pointer($04000120);
  42. // Fixme: Does the hardware still support 16 bit comms mode?
  43. // BIOS makes use of 32 bit mode, so some regs still exist
  44. SIO_MULTI_0 : pcuint16 = pointer($04000120);
  45. SIO_MULTI_1 : pcuint16 = pointer($04000122);
  46. SIO_MULTI_2 : pcuint16 = pointer($04000124);
  47. SIO_MULTI_3 : pcuint16 = pointer($04000126);
  48. SIO_MULTI_SEND : pcuint16 = pointer($0400012A);
  49. // SPI chain registers
  50. REG_SPICNT : pcuint16 = pointer($040001C0);
  51. REG_SPIDATA : pcuint16 = pointer($040001C2);
  52. SPI_ENABLE = (1 shl 15);
  53. SPI_IRQ = (1 shl 14);
  54. SPI_BUSY = (1 shl 7);
  55. // Pick the SPI clock speed
  56. SPI_BAUD_4MHZ = 0;
  57. SPI_BAUD_2MHZ = 1;
  58. SPI_BAUD_1MHZ = 2;
  59. SPI_BAUD_512KHZ = 3;
  60. // Pick the SPI transfer length
  61. SPI_BYTE_MODE = (0 shl 10);
  62. SPI_HWORD_MODE = (1 shl 10);
  63. // Pick the SPI device
  64. SPI_DEVICE_POWER = (0 shl 8);
  65. SPI_DEVICE_FIRMWARE = (1 shl 8);
  66. SPI_DEVICE_NVRAM = (1 shl 8);
  67. SPI_DEVICE_TOUCH = (2 shl 8);
  68. SPI_DEVICE_MICROPHONE = (2 shl 8);
  69. // When used, the /CS line will stay low after the transfer ends
  70. // i.e. when we're part of a continuous transfer
  71. SPI_CONTINUOUS = (1 shl 11);
  72. // Fixme: does this stuff really belong in serial.h?
  73. // Power management registers
  74. PM_CONTROL_REG = 0;
  75. PM_BATTERY_REG = 1;
  76. PM_AMPLIFIER_REG = 2;
  77. PM_READ_REGISTER = (1 shl 7);
  78. // PM control register bits - power control
  79. PM_SOUND_AMP = (1 shl 0); // Power the sound hardware (needed to hear stuff in GBA mode too)
  80. PM_SOUND_MUTE = (1 shl 1); // Mute the main speakers, headphone output will still work.
  81. PM_BACKLIGHT_BOTTOM = (1 shl 2); // Enable the top backlight if set
  82. PM_BACKLIGHT_TOP = (1 shl 3); // Enable the bottom backlight if set
  83. PM_SYSTEM_PWR = (1 shl 6); // Turn the power *off* if set
  84. PM_POWER_DOWN = (1 shl 6); // Same thing, I like this name better tho
  85. {$endif NDS_INTERFACE}
  86. {$ifdef NDS_IMPLEMENTATION}
  87. // PM control register bits - LED control
  88. function PM_LED_CONTROL(m: cint): cint; inline;
  89. begin
  90. PM_LED_CONTROL := m shl 4; // ?
  91. end;
  92. {$endif NDS_IMPLEMENTATION}
  93. {$ifdef NDS_INTERFACE}
  94. const
  95. PM_LED_ON = (0 shl 4); // Steady on
  96. PM_LED_SLEEP = (1 shl 4); // Blinking, mostly off
  97. PM_LED_BLINK = (3 shl 4); // Blinking, mostly on
  98. PM_AMP_OFFSET = 2;
  99. PM_AMP_ON = 1;
  100. PM_AMP_OFF = 0;
  101. // Fixme: does this stuff really belong in serial.h?
  102. // Firmware commands
  103. FIRMWARE_WREN = $06;
  104. FIRMWARE_WRDI = $04;
  105. FIRMWARE_RDID = $9F;
  106. FIRMWARE_RDSR = $05;
  107. FIRMWARE_READ = $03;
  108. FIRMWARE_PW = $0A;
  109. FIRMWARE_PP = $02;
  110. FIRMWARE_FAST = $0B;
  111. FIRMWARE_PE = $DB;
  112. FIRMWARE_SE = $D8;
  113. FIRMWARE_DP = $B9;
  114. FIRMWARE_RDP = $AB;
  115. {$endif NDS_INTERFACE}
  116. {$ifdef NDS_IMPLEMENTATION}
  117. procedure SerialWaitBusy(); inline;
  118. begin
  119. while (REG_SPICNT^ and SPI_BUSY) <> 0 do
  120. swiDelay(1);
  121. end;
  122. {$endif NDS_IMPLEMENTATION}
  123. // Warning: These functions use the SPI chain, and are thus 'critical'
  124. // sections, make sure to disable interrupts during the call if you've
  125. // got a VBlank IRQ polling the touch screen, etc...
  126. // Read/write a power management register
  127. {$ifdef NDS_INTERFACE}
  128. function writePowerManagement(reg: cint; command: cint): cint; cdecl; external;
  129. {$endif NDS_INTERFACE}
  130. {$ifdef NDS_IMPLEMENTATION}
  131. function readPowerManagement(reg: cint): cint; inline;
  132. begin
  133. readPowerManagement := writePowerManagement(reg or PM_READ_REGISTER, 0);
  134. end;
  135. {$endif NDS_IMPLEMENTATION}
  136. {$ifdef NDS_INTERFACE}
  137. // Read the firmware
  138. procedure readFirmware(address: cuint32; destination: pointer; size: cuint32); cdecl; external;
  139. {$endif NDS_INTERFACE}
  140. {$ifdef NDS_INTERFACE}
  141. function PM_LED_CONTROL(m: cint): cint; inline;
  142. procedure SerialWaitBusy(); inline;
  143. function readPowerManagement(reg: cint): cint; inline;
  144. {$endif NDS_INTERFACE}