board.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * File : board.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2010, 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. * 2010-03-30 Kyle First version
  13. */
  14. #include <rtthread.h>
  15. #include "compiler.h"
  16. #include "pm.h"
  17. #include "gpio.h"
  18. #include "usart.h"
  19. #include "intc.h"
  20. #include "serial.h"
  21. #define FOSC0 12000000
  22. #define FCPU 60000000
  23. #define FHSB FCPU
  24. #define FPBA FCPU
  25. #define FPBB FCPU
  26. extern void rt_hw_serial_isr(void);
  27. extern void rt_hw_usart_init(void);
  28. /**
  29. * System tick interrupt handler.
  30. */
  31. static void rt_hw_timer_handler(void)
  32. {
  33. // Clears the interrupt request.
  34. Set_system_register(AVR32_COMPARE, FCPU / RT_TICK_PER_SECOND);
  35. rt_tick_increase();
  36. }
  37. /**
  38. * Initialize system clock and all peripherals.
  39. */
  40. static void peripherals_init(void)
  41. {
  42. /*
  43. * PM initialization: OSC0 = 12MHz XTAL, PLL0 = 60MHz System Clock
  44. */
  45. pm_freq_param_t pm_freq_param =
  46. {
  47. .cpu_f = FCPU,
  48. .pba_f = FPBA,
  49. .osc0_f = FOSC0,
  50. .osc0_startup = AVR32_PM_OSCCTRL0_STARTUP_2048_RCOSC
  51. };
  52. pm_configure_clocks(&pm_freq_param);
  53. /*
  54. * USART1 initialization
  55. */
  56. gpio_enable_module_pin(AVR32_USART1_TXD_0_1_PIN, AVR32_USART1_TXD_0_1_FUNCTION);
  57. gpio_enable_module_pin(AVR32_USART1_RXD_0_1_PIN, AVR32_USART1_RXD_0_1_FUNCTION);
  58. static const usart_options_t usartOptions = {
  59. .baudrate = 115200,
  60. .charlength = 8,
  61. .paritytype = USART_NO_PARITY,
  62. .stopbits = USART_1_STOPBIT,
  63. .channelmode = USART_NORMAL_CHMODE
  64. };
  65. usart_init_rs232(&AVR32_USART1, &usartOptions, FCPU);
  66. INTC_init_interrupts();
  67. INTC_register_interrupt(&rt_hw_serial_isr, AVR32_USART1_IRQ, AVR32_INTC_INT0);
  68. AVR32_USART1.ier = AVR32_USART_IER_RXRDY_MASK;
  69. }
  70. /**
  71. * Initialize CPU cycle counter for system tick.
  72. */
  73. static void cpu_counter_init(void)
  74. {
  75. INTC_register_interrupt(&rt_hw_timer_handler, AVR32_CORE_COMPARE_IRQ, AVR32_INTC_INT3);
  76. Set_system_register(AVR32_COMPARE, FCPU / RT_TICK_PER_SECOND);
  77. Set_system_register(AVR32_COUNT, 0);
  78. }
  79. void rt_hw_board_init(void)
  80. {
  81. extern struct rt_device _rt_usart_device;
  82. extern struct avr32_serial_device uart;
  83. Disable_global_interrupt();
  84. peripherals_init();
  85. cpu_counter_init();
  86. rt_hw_serial_register(&_rt_usart_device, "uart1", RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM, &uart);
  87. rt_console_set_device("uart1");
  88. }