board.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2011-05-23 aozima first implementation for PIC32.
  9. */
  10. // Adds support for PIC32 Peripheral library functions and macros
  11. #include <plib.h>
  12. #include <rtthread.h>
  13. // Configuration Bits
  14. #pragma config FNOSC = PRIPLL // Oscillator Selection
  15. #pragma config FPLLIDIV = DIV_2 // PLL Input Divider (PIC32 Starter Kit: use divide by 2 only)
  16. #pragma config FPLLMUL = MUL_20 // PLL Multiplier
  17. #pragma config FPLLODIV = DIV_1 // PLL Output Divider
  18. #pragma config FPBDIV = DIV_1 // Peripheral Clock divisor
  19. #pragma config FWDTEN = OFF // Watchdog Timer
  20. #pragma config WDTPS = PS1 // Watchdog Timer Postscale
  21. #pragma config FCKSM = CSDCMD // Clock Switching & Fail Safe Clock Monitor
  22. #pragma config OSCIOFNC = OFF // CLKO Enable
  23. #pragma config POSCMOD = XT // Primary Oscillator
  24. #pragma config IESO = OFF // Internal/External Switch-over
  25. #pragma config FSOSCEN = OFF // Secondary Oscillator Enable
  26. #pragma config CP = OFF // Code Protect
  27. #pragma config BWP = OFF // Boot Flash Write Protect
  28. #pragma config PWP = OFF // Program Flash Write Protect
  29. #pragma config ICESEL = ICS_PGx2 // ICE/ICD Comm Channel Select
  30. #pragma config DEBUG = OFF // Debugger Disabled for Starter Kit
  31. // The following is used by the main application
  32. #define SYS_FREQ (80000000UL)
  33. #define PB_DIV (1 << ((OSCCON&_OSCCON_PBDIV0_MASK)>>_OSCCON_PBDIV0_POSITION) )
  34. #define PRESCALE 256
  35. #define TOGGLES_PER_SEC RT_TICK_PER_SECOND
  36. #define T1_TICK (SYS_FREQ/PB_DIV/PRESCALE/TOGGLES_PER_SEC)
  37. static void rt_hw_show_info(void)
  38. {
  39. rt_kprintf("\r\n\r\n---------- board info ----------\r\n");
  40. rt_kprintf("DEVICE_FAMILY: PIC32\r\n");
  41. rt_kprintf("CPU_ARCHITECTURE: MIPS\r\n");
  42. rt_kprintf("CPU_FREQ: %uMHz\r\n",SYS_FREQ/1000000UL);
  43. }
  44. static void rt_hw_timer_handler(void)
  45. {
  46. /* enter interrupt */
  47. rt_interrupt_enter();
  48. rt_tick_increase();
  49. /* leave interrupt */
  50. rt_interrupt_leave();
  51. }
  52. /**
  53. * This function will initial board.
  54. */
  55. void rt_hw_board_init()
  56. {
  57. // Configure the device for maximum performance, but do not change the PBDIV clock divisor.
  58. // Given the options, this function will change the program Flash wait states,
  59. // RAM wait state and enable prefetch cache, but will not change the PBDIV.
  60. // The PBDIV value is already set via the pragma FPBDIV option above.
  61. SYSTEMConfig(SYS_FREQ, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);
  62. /* use DBPRINTF */
  63. /* rt_hw_console_init(); */
  64. rt_hw_usart_init();
  65. rt_console_set_device("uart1");
  66. rt_hw_show_info();
  67. // enable multi-vector interrupts
  68. INTEnableSystemMultiVectoredInt();
  69. rt_hw_interrupt_disable();
  70. // // STEP 2. configure the core timer
  71. // OpenCoreTimer(CORE_TICK_RATE);
  72. //
  73. // // set up the core timer interrupt with a prioirty of 2 and zero sub-priority
  74. // mConfigIntCoreTimer((CT_INT_ON | CT_INT_PRIOR_2 | CT_INT_SUB_PRIOR_0));
  75. // STEP 2. configure Timer 1 using internal clock, 1:256 prescale
  76. OpenTimer1(T1_ON | T1_SOURCE_INT | T1_PS_1_256, T1_TICK);
  77. // set up the timer interrupt with a priority of 2
  78. ConfigIntTimer1(T1_INT_ON | T1_INT_PRIOR_2);
  79. /* Setup the software interrupt. */
  80. mConfigIntCoreSW0( CSW_INT_ON | CSW_INT_PRIOR_1 | CSW_INT_SUB_PRIOR_0 );
  81. }
  82. void __ISR(_TIMER_1_VECTOR, ipl2) Timer1Handler(void)
  83. {
  84. // clear the interrupt flag
  85. mT1ClearIntFlag();
  86. // .. things to do
  87. rt_hw_timer_handler();
  88. }
  89. //void __ISR(_CORE_TIMER_VECTOR, ipl2) CoreTimerHandler(void)
  90. //{
  91. // // clear the interrupt flag
  92. // mCTClearIntFlag();
  93. //
  94. // // .. things to do
  95. // rt_hw_timer_handler();
  96. //
  97. // // update the period
  98. // UpdateCoreTimer(CORE_TICK_RATE);
  99. //}
  100. void __ISR(_CORE_SOFTWARE_0_VECTOR, ipl1) CoreSW0Handler(void);