board.c 4.1 KB

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