PIC32MZ-serial.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. void _mon_putc(char c);
  2. #define BAUD_GEN(sysclk, baud) ((sysclk / (16 * baud)) - 1)
  3. #ifdef MICROCHIP_PIC32
  4. #if defined (__32MZ2048ECH144__) || defined(__32MZ2048ECM144__) || defined(__32MZ2048EFM144__)
  5. /* Code generated from Harmony example then exported using Window -> PIC32 Memory View -> Configuration Bits into system_config.h */
  6. #define SYS_CLK_FREQ 200000000ul
  7. #define SYS_CLK_BUS_PERIPHERAL_2 100000000ul
  8. // DEVCFG3
  9. #pragma config FMIIEN = ON // Ethernet RMII/MII Enable (MII Enabled)
  10. #pragma config FETHIO = ON // Ethernet I/O Pin Select (Default Ethernet I/O)
  11. #pragma config PGL1WAY = ON // Permission Group Lock One Way Configuration (Allow only one reconfiguration)
  12. #pragma config PMDL1WAY = ON // Peripheral Module Disable Configuration (Allow only one reconfiguration)
  13. #pragma config IOL1WAY = ON // Peripheral Pin Select Configuration (Allow only one reconfiguration)
  14. #pragma config FUSBIDIO = ON // USB USBID Selection (Controlled by the USB Module)
  15. // DEVCFG2
  16. #pragma config FPLLIDIV = DIV_1 // System PLL Input Divider (1x Divider)
  17. #pragma config FPLLRNG = RANGE_5_10_MHZ // System PLL Input Range (5-10 MHz Input)
  18. #pragma config FPLLICLK = PLL_FRC // System PLL Input Clock Selection (FRC is input to the System PLL)
  19. #pragma config FPLLMULT = MUL_50 // System PLL Multiplier (PLL Multiply by 50)
  20. #pragma config FPLLODIV = DIV_2 // System PLL Output Clock Divider (2x Divider)
  21. #pragma config UPLLFSEL = FREQ_24MHZ // USB PLL Input Frequency Selection (USB PLL input is 24 MHz)
  22. // DEVCFG1
  23. #pragma config FNOSC = SPLL // Oscillator Selection Bits (System PLL)
  24. #pragma config DMTINTV = WIN_127_128 // DMT Count Window Interval (Window/Interval value is 127/128 counter value)
  25. #pragma config FSOSCEN = OFF // Secondary Oscillator Enable (Disable SOSC)
  26. #pragma config IESO = OFF // Internal/External Switch Over (Disabled)
  27. #pragma config POSCMOD = OFF // Primary Oscillator Configuration (Primary osc disabled)
  28. #pragma config OSCIOFNC = OFF // CLKO Output Signal Active on the OSCO Pin (Disabled)
  29. #pragma config FCKSM = CSECME // Clock Switching and Monitor Selection (Clock Switch Enabled, FSCM Enabled)
  30. #pragma config WDTPS = PS1048576 // Watchdog Timer Postscaler (1:1048576)
  31. #pragma config WDTSPGM = STOP // Watchdog Timer Stop During Flash Programming (WDT stops during Flash programming)
  32. #pragma config WINDIS = NORMAL // Watchdog Timer Window Mode (Watchdog Timer is in non-Window mode)
  33. #pragma config FWDTEN = OFF // Watchdog Timer Enable (WDT Disabled)
  34. #pragma config FWDTWINSZ = WINSZ_25 // Watchdog Timer Window Size (Window size is 25%)
  35. #pragma config DMTCNT = DMT31 // Deadman Timer Count Selection (2^31 (2147483648))
  36. #pragma config FDMTEN = OFF // Deadman Timer Enable (Deadman Timer is disabled)
  37. // DEVCFG0
  38. #pragma config ICESEL = ICS_PGx2 // ICE/ICD Comm Channel Select (Communicate on PGEC2/PGED2)
  39. // DEVCP0
  40. #pragma config CP = OFF // Code Protect (Protection Disabled)
  41. #include <xc.h>
  42. #endif
  43. #endif
  44. static void init_serial(unsigned int sysClk) {
  45. #ifdef MICROCHIP_PIC32
  46. #if defined (__32MZ2048ECH144__) || defined(__32MZ2048ECM144__) || defined(__32MZ2048EFM144__)
  47. /* This is for pin B14 which is connected to the USB to UART connector J11 located under Ethernet connector */
  48. /* Setup UART2 */
  49. #ifdef SYS_CLK_BUS_PERIPHERAL_2
  50. U2BRG = BAUD_GEN(SYS_CLK_BUS_PERIPHERAL_2, 115200);
  51. #else
  52. if (sysClk > 100000000)
  53. sysClk /= 2;
  54. U2BRG = BAUD_GEN(sysClk, 115200);
  55. #endif
  56. ANSELBCLR = 0x4000;
  57. ANSELGCLR = 0x0040;
  58. RPB14R = 0x02;
  59. U2RXR = 0x01;
  60. U2MODE = 0x8000;
  61. U2STA = 0x400;
  62. #endif
  63. #endif
  64. (void)sysClk;
  65. }