1
0

board.c 2.4 KB

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