2
0

cortexm4.pp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. {
  2. System register definitions and utility code for Cortex-M4
  3. Created by Jeppe Johansen 2012 - [email protected]
  4. }
  5. {$IFNDEF FPC_DOTTEDUNITS}
  6. unit cortexm4;
  7. {$ENDIF FPC_DOTTEDUNITS}
  8. interface
  9. {$PACKRECORDS 2}
  10. const
  11. SCS_BASE = $E000E000;
  12. DWT_BASE = $E0001000;
  13. FP_BASE = $E0002000;
  14. ITM_BASE = $E0000000;
  15. TPIU_BASE = $E0040000;
  16. ETM_BASE = $E0041000;
  17. type
  18. TNVICRegisters = record
  19. ISER: array[0..7] of longword;
  20. reserved0: array[0..23] of longword;
  21. ICER: array[0..7] of longword;
  22. reserved1: array[0..23] of longword;
  23. ISPR: array[0..7] of longword;
  24. reserved2: array[0..23] of longword;
  25. ICPR: array[0..7] of longword;
  26. reserved3: array[0..23] of longword;
  27. IABR: array[0..7] of longword;
  28. reserved4: array[0..55] of longword;
  29. IP: array[0..239] of byte;
  30. reserved5: array[0..643] of longword;
  31. STIR: longword;
  32. end;
  33. TSCBRegisters = record
  34. CPUID, {!< CPU ID Base Register }
  35. ICSR, {!< Interrupt Control State Register }
  36. VTOR, {!< Vector Table Offset Register }
  37. AIRCR, {!< Application Interrupt / Reset Control Register }
  38. SCR, {!< System Control Register }
  39. CCR: longword; {!< Configuration Control Register }
  40. SHP: array[0..11] of byte; {!< System Handlers Priority Registers (4-7, 8-11, 12-15) }
  41. SHCSR, {!< System Handler Control and State Register }
  42. CFSR, {!< Configurable Fault Status Register }
  43. HFSR, {!< Hard Fault Status Register }
  44. DFSR, {!< Debug Fault Status Register }
  45. MMFAR, {!< Mem Manage Address Register }
  46. BFAR, {!< Bus Fault Address Register }
  47. AFSR: longword; {!< Auxiliary Fault Status Register }
  48. // CPUID registers
  49. PFR: array[0..1] of longword; {!< Processor Feature Register }
  50. DFR, {!< Debug Feature Register }
  51. ADR: longword; {!< Auxiliary Feature Register }
  52. MMFR: array[0..3] of longword; {!< Memory Model Feature Register }
  53. ISAR: array[0..5] of longword; {!< ISA Feature Register }
  54. CLIDR,
  55. CTR,
  56. CCSIDR,
  57. CSSELR: longword;
  58. CPACR: longword;
  59. end;
  60. TSysTickRegisters = record
  61. Ctrl,
  62. Load,
  63. Val,
  64. Calib: longword;
  65. end;
  66. TIDRegisters = record
  67. PID4_7: array[0..3] of longword;
  68. PID0_3: array[0..3] of longword;
  69. CID: array[0..3] of longword;
  70. end;
  71. TCoreDebugRegisters = record
  72. DHCSR,
  73. DCRSR,
  74. DCRDR,
  75. DEMCR: longword;
  76. end;
  77. TFPRegisters = record
  78. Ctrl,
  79. Remap: longword;
  80. Comp: array[0..7] of longword;
  81. res: array[0..987] of longword;
  82. ID: TIDRegisters;
  83. end;
  84. TDWTEntry = record
  85. Comp,
  86. Mask,
  87. Func,
  88. res: longword;
  89. end;
  90. TDWTRegisters = record
  91. Ctrl,
  92. CycCnt,
  93. CPICnt,
  94. ExcCnt,
  95. SleepCnt,
  96. LSUCnt,
  97. FoldCnt,
  98. PCSR: longword;
  99. Entries: array[0..3] of TDWTEntry;
  100. end;
  101. TITMRegisters = record
  102. Stimulus: array[0..31] of longword;
  103. res0: array[0..($E00-$7C-4)-1] of byte;
  104. TraceEnable: longword;
  105. res1: array[0..($E40-$E00-4)-1] of byte;
  106. TracePrivilege: longword;
  107. res2: array[0..($E80-$E40-4)-1] of byte;
  108. TraceControl: longword;
  109. res3: array[0..($EF8-$E80-4)-1] of byte;
  110. IntegrationWrite,
  111. IntegrationRead,
  112. IntegrationModeCtrl: longword;
  113. res4: array[0..($FB0-$F00-4)-1] of byte;
  114. LockAccess,
  115. LockStatus: longword;
  116. res5: array[0..($FD0-$FB4-4)-1] of byte;
  117. ID: TIDRegisters;
  118. end;
  119. TTPIURegisters = record
  120. SupportedSyncPortSizes,
  121. CurrentSyncPortSize: longword;
  122. res0: array[0..($10-$04-4)-1] of byte;
  123. AsyncColckPrescaler: longword;
  124. res1: array[0..($F0-$10-4)-1] of byte;
  125. SelectedPinProtocol: longword;
  126. res2: array[0..($100-$F0-4)-1] of byte;
  127. TriggerControl: array[0..2] of longword;
  128. res3: array[0..($200-$108-4)-1] of byte;
  129. TestPattern: array[0..2] of longword;
  130. res4: array[0..($300-$208-4)-1] of byte;
  131. FormatFlushStatus,
  132. FormatControl,
  133. FormatSyncCounter: longword;
  134. res5: array[0..($EF0-$308-4)-1] of byte;
  135. ITATBCTR2: longword;
  136. res6: longword;
  137. ITATBCTR0: longword;
  138. end;
  139. TFPExtRegisters = record
  140. FPCCR,
  141. FPCAR,
  142. FPDSCR: longword;
  143. MVFR: array[0..2] of longword;
  144. end;
  145. var
  146. // System Control
  147. InterruptControlType: longword absolute (SCS_BASE+$0004);
  148. SCB: TSCBRegisters absolute (SCS_BASE+$0D00);
  149. SysTick: TSysTickRegisters absolute (SCS_BASE+$0010);
  150. NVIC: TNVICRegisters absolute (SCS_BASE+$0100);
  151. SoftwareTriggerInterrupt: longword absolute (SCS_BASE+$0000);
  152. FPExt: TFPExtRegisters absolute (SCS_BASE+$0F34);
  153. // Core Debug
  154. CoreDebug: TCoreDebugRegisters absolute (SCS_BASE+$0DF0);
  155. // Flash Patch
  156. FP: TFPRegisters absolute FP_BASE;
  157. DWT: TDWTRegisters absolute DWT_BASE;
  158. ITM: TITMRegisters absolute ITM_BASE;
  159. TPIU: TTPIURegisters absolute TPIU_BASE;
  160. type
  161. TITM_Port = 0..31;
  162. procedure ITM_SendData(Port: TITM_Port; Data: longword); inline;
  163. implementation
  164. const
  165. CoreDebug_DEMCR_TRCENA = $01000000;
  166. ITM_TCR_ITMENA = $00000001;
  167. procedure ITM_SendData(Port: TITM_Port; Data: longword);
  168. begin
  169. if ((CoreDebug.DEMCR and CoreDebug_DEMCR_TRCENA) <> 0) and
  170. ((itm.TraceControl and ITM_TCR_ITMENA) <> 0) and
  171. ((ITM.TraceEnable and (1 shl Port)) <> 0) then
  172. begin
  173. while ITM.Stimulus[Port] = 0 do;
  174. ITM.Stimulus[Port] := Data;
  175. end;
  176. end;
  177. end.