aurix_hal.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #include <machine/intrinsics.h>
  2. #include <machine/wdtcon.h>
  3. #include <stdlib.h>
  4. #include <stdint.h>
  5. #include "IfxScu_reg.h"
  6. #include "IfxScu_bf.h"
  7. #include "IfxCpu_reg.h"
  8. #include "IfxCpu_bf.h"
  9. #include "IfxStm_reg.h"
  10. #include "IfxStm_bf.h"
  11. #include "IfxQspi_reg.h"
  12. #include "IfxStm_reg.h"
  13. #include "IfxStm_bf.h"
  14. # define BOARD_NAME "TriBoard-TC233A"
  15. # define BOARD_TITLE "TC233A TriBoard"
  16. # define MCU_NAME "TC233A"
  17. #include "IfxPort_reg.h"
  18. #include "IfxPort_bf.h"
  19. #include "IfxAsclin_reg.h"
  20. #include "IfxAsclin_bf.h"
  21. #include "aurix_hal_sys.h"
  22. static Ifx_P * const PORT15 = (Ifx_P *)&MODULE_P15;
  23. static Ifx_P * const PORT14 = (Ifx_P *)&MODULE_P14;
  24. static Ifx_P * const PORT11 = (Ifx_P *)&MODULE_P11;
  25. static Ifx_ASCLIN * const UART = (Ifx_ASCLIN *)&MODULE_ASCLIN0;
  26. /* UART primitives */
  27. #define RX_CLEAR(u) ((u)->FLAGSCLEAR.U = (IFX_ASCLIN_FLAGSCLEAR_RFLC_MSK << IFX_ASCLIN_FLAGSCLEAR_RFLC_OFF))
  28. #define TX_CLEAR(u) ((u)->FLAGSCLEAR.U = (IFX_ASCLIN_FLAGSCLEAR_TFLC_MSK << IFX_ASCLIN_FLAGSCLEAR_TFLC_OFF))
  29. #define PUT_CHAR(u, c) ((u)->TXDATA.U = (c))
  30. #define GET_CHAR(u) ((u)->RXDATA.U)
  31. #define GET_ERROR_STATUS(u) (((u)->FLAGS.U) & ASC_ERROR_MASK)
  32. #define RESET_ERROR(u) ((u)->FLAGSCLEAR.U = ASC_CLRERR_MASK)
  33. #define TX_START(u) ((u)->FLAGSSET.U = (IFX_ASCLIN_FLAGSSET_TFLS_MSK << IFX_ASCLIN_FLAGSSET_TFLS_OFF))
  34. #define TX_READY(u) ((u)->FLAGS.B.TFL != 0) /* Transmit FIFO Level */
  35. #define RX_READY(u) ((u)->FLAGS.B.RFL != 0) /* Receive FIFO Level */
  36. /* baud rate values at 100 MHz */
  37. #define BAUD_9600 (48 * 1)
  38. #define BAUD_19200 (48 * 2)
  39. #define BAUD_38400 (48 * 4)
  40. #define BAUD_57600 (48 * 6)
  41. #define BAUD_115200 (48 * 12)
  42. /* Port Modes */
  43. #define IN_NOPULL0 0x00 /* Port Input No Pull Device */
  44. #define IN_PULLDOWN 0x01 /* Port Input Pull Down Device */
  45. #define IN_PULLUP 0x02 /* Port Input Pull Up Device */
  46. #define IN_NOPULL3 0x03 /* Port Input No Pull Device */
  47. #define OUT_PPGPIO 0x10 /* Port Output General Purpose Push/Pull */
  48. #define OUT_PPALT1 0x11 /* Port Output Alternate 1 Function Push/Pull */
  49. #define OUT_PPALT2 0x12 /* Port Output Alternate 2 Function Push/Pull */
  50. #define OUT_PPALT3 0x13 /* Port Output Alternate 3 Function Push/Pull */
  51. #define OUT_PPALT4 0x14 /* Port Output Alternate 4 Function Push/Pull */
  52. #define OUT_PPALT5 0x15 /* Port Output Alternate 5 Function Push/Pull */
  53. #define OUT_PPALT6 0x16 /* Port Output Alternate 6 Function Push/Pull */
  54. #define OUT_PPALT7 0x17 /* Port Output Alternate 7 Function Push/Pull */
  55. #define OUT_ODGPIO 0x18 /* Port Output General Purpose Open Drain */
  56. #define OUT_ODALT1 0x19 /* Port Output Alternate 1 Function Open Drain */
  57. #define OUT_ODALT2 0x1A /* Port Output Alternate 2 Function Open Drain */
  58. #define OUT_ODALT3 0x1B /* Port Output Alternate 3 Function Open Drain */
  59. #define OUT_ODALT4 0x1C /* Port Output Alternate 4 Function Open Drain */
  60. #define OUT_ODALT5 0x1D /* Port Output Alternate 5 Function Open Drain */
  61. #define OUT_ODALT6 0x1E /* Port Output Alternate 6 Function Open Drain */
  62. #define OUT_ODALT7 0x1F /* Port Output Alternate 7 Function Open Drain */
  63. /* definitions for RX error conditions */
  64. #define ASC_ERROR_MASK ((IFX_ASCLIN_FLAGS_PE_MSK << IFX_ASCLIN_FLAGS_PE_OFF) | \
  65. (IFX_ASCLIN_FLAGS_FE_MSK << IFX_ASCLIN_FLAGS_FE_OFF) | \
  66. (IFX_ASCLIN_FLAGS_RFO_MSK << IFX_ASCLIN_FLAGS_RFO_OFF))
  67. #define ASC_CLRERR_MASK ((IFX_ASCLIN_FLAGSCLEAR_PEC_MSK << IFX_ASCLIN_FLAGSCLEAR_PEC_OFF) | \
  68. (IFX_ASCLIN_FLAGSCLEAR_FEC_MSK << IFX_ASCLIN_FLAGSCLEAR_FEC_OFF) | \
  69. (IFX_ASCLIN_FLAGSCLEAR_RFOC_MSK << IFX_ASCLIN_FLAGSCLEAR_RFOC_OFF))
  70. void platform_init(void)
  71. {
  72. unlock_wdtcon();
  73. unlock_safety_wdtcon(); //EVR13CON is safety_endinit protected ("SE" in user manual)
  74. SCU_EVR13CON.U |= 0b11 << 28; //shut off internal regulator
  75. lock_wdtcon();
  76. lock_safety_wdtcon();
  77. SYSTEM_Init();
  78. PORT11->IOCR8.U = (0b10000 << 19) | (0b10000 << 11); //P11.10 as GPO Push pull
  79. PORT11->OMR.U = (1 << 10) | (1 << 26); //P11.10 High
  80. }
  81. //trigger later
  82. void trigger_setup(void)
  83. {
  84. PORT14->IOCR4.U = (0b10000 << 3); //P14.4 -> output
  85. PORT14->OMR.B.PCL4 = 1; //trigger low
  86. }
  87. void trigger_high(void)
  88. {
  89. PORT14->OMR.B.PS4 = 1; //trigger high
  90. }
  91. void trigger_low(void)
  92. {
  93. PORT14->OMR.B.PCL4 = 1; //trigger low
  94. }
  95. void init_uart(void)
  96. {
  97. PORT15->IOCR0.U = (0b10010 << 19); //P15.3 -> input, P15.2 -> UART TX PP
  98. PORT15->OMR.B.PS2 = 1;
  99. unlock_wdtcon();
  100. UART->CLC.U = 0;
  101. PORT15->PDR0.B.PD2 = 0;
  102. PORT15->PDR0.B.PD3 = 0;
  103. lock_wdtcon();
  104. (void)UART->CLC.U; //?
  105. UART->IOCR.B.ALTI = 1; //P15.3 for as RX
  106. UART->CSR.U = 0; //turn off UART clock to allow registers to be modified
  107. //1 byte into fifo per write, enable tx fifo, flush tx fifo
  108. UART->TXFIFOCON.U = (1 << 6) | (1 << 1) | (1 << 0);
  109. //same as tx fifo stuff
  110. UART->RXFIFOCON.U = (1 << 6) | (1 << 1) | (1 << 0);
  111. //HighTec UART assumes 100MHz clock and uses prescale of 9+1, so we have prescale of 1+1
  112. //to keep things the same
  113. //prescale 9+1, oversample 16, sample position 7,8,9, 3 samples per bit
  114. UART->BITCON.U = (1) | (15 << 16) | (9 << 24) | (1 << 31);
  115. //8n1 UART
  116. UART->FRAMECON.U = (1 << 9) | (0 << 16) | (0 << 30);
  117. UART->DATCON.U = 7; //8bit data length
  118. /* #define BAUD_NUM (48 * 40) */
  119. /* #define BAUD_DEN (3125) */
  120. #define BAUD_NUM (1001)
  121. #define BAUD_DEN (3002)
  122. /* #define BAUD_NUM (200) */
  123. /* #define BAUD_DEN (120) */
  124. //NOTE: DEN>NUM
  125. /*
  126. * fosc * num / ((prescale + 1) * den * (oversample + 1))
  127. * = 100MHz * 48 * 4 / (10 * 3125 * 16)
  128. * = 38400
  129. */
  130. UART->BRG.U = (BAUD_DEN << 0) | (BAUD_NUM << 16);
  131. UART->FRAMECON.B.MODE = 1; //asc mode
  132. UART->CSR.U = 1; //CLC as clock source
  133. TX_START(UART); //macro from bspconfig, expand later
  134. }
  135. int poll_uart(char *c)
  136. {
  137. unsigned char ret;
  138. int res = 0;
  139. if (RX_READY(UART))
  140. {
  141. ret = (unsigned char)GET_CHAR(UART);
  142. /* acknowledge receive */
  143. RX_CLEAR(UART);
  144. /* check for error condition */
  145. if (GET_ERROR_STATUS(UART))
  146. {
  147. /* reset error flags */
  148. RESET_ERROR(UART);
  149. /* ignore this character */
  150. }
  151. else
  152. {
  153. /* this is a valid character */
  154. *c = ret;
  155. res = 1;
  156. }
  157. }
  158. return res;
  159. }
  160. void putch(char c)
  161. {
  162. while (!TX_READY(UART));
  163. TX_CLEAR(UART);
  164. PUT_CHAR(UART, c);
  165. }
  166. char getch(void)
  167. {
  168. char ch;
  169. while (!poll_uart(&ch));
  170. return ch;
  171. }