board.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * File : board.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006, RT-Thread Develop 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://openlab.rt-thread.com/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2006-08-23 Bernard first implementation
  13. */
  14. #include <rtthread.h>
  15. #include <rthw.h>
  16. #include <AT91SAM7S.h>
  17. #include "board.h"
  18. static void rt_hw_board_led_init(void);
  19. /**
  20. * @addtogroup sam7s
  21. */
  22. /*@{*/
  23. /* Periodic Interval Value */
  24. #define PIV (((MCK/16)/1000)*(1000/RT_TICK_PER_SECOND))
  25. /**
  26. * This is the timer interrupt service routine.
  27. * @param vector the irq number for timer
  28. */
  29. void rt_hw_timer_handler(int vector)
  30. {
  31. if (AT91C_PITC_PISR & 0x01)
  32. {
  33. /* increase a tick */
  34. rt_tick_increase();
  35. /* ack interrupt */
  36. AT91C_AIC_EOICR = AT91C_PITC_PIVR;
  37. }
  38. else
  39. {
  40. /* end of interrupt */
  41. AT91C_AIC_EOICR = 0;
  42. }
  43. }
  44. /* PIO Flash PA PB PIN */
  45. #define LED (1 << 8)/* PA8 & TWD NPCS3 43 */
  46. /**
  47. * This function will init led on the board
  48. */
  49. static void rt_hw_board_led_init()
  50. {
  51. /* Enable Clock for PIO */
  52. AT91C_PMC_PCER = 1 << AT91C_ID_PIOA;
  53. /* configure PIO as output for led */
  54. AT91C_PIO_PER = LED;
  55. AT91C_PIO_OER = LED;
  56. }
  57. /**
  58. * This function will take the led on board on.
  59. *
  60. * @param n the number nth led
  61. */
  62. void rt_hw_board_led_on()
  63. {
  64. AT91C_PIO_CODR = LED;
  65. }
  66. /**
  67. * This function will take the led on board off.
  68. *
  69. * @param n the number nth led
  70. */
  71. void rt_hw_board_led_off()
  72. {
  73. AT91C_PIO_SODR = LED;
  74. }
  75. void rt_hw_led_flash()
  76. {
  77. int i;
  78. rt_hw_board_led_off();
  79. for (i = 0; i < 2000000; i ++);
  80. rt_hw_board_led_on();
  81. for (i = 0; i < 2000000; i ++);
  82. }
  83. /*
  84. * RT-Thread Console Interface, used by rt_kprintf
  85. */
  86. /**
  87. * This function is used to display a string on console, normally, it's
  88. * invoked by rt_kprintf
  89. *
  90. * @param str the displayed string
  91. */
  92. void rt_hw_console_output(const char* str)
  93. {
  94. while (*str)
  95. {
  96. if (*str == '\n')
  97. {
  98. while (!(AT91C_US0_CSR & AT91C_US_TXRDY));
  99. AT91C_US0_THR = '\r';
  100. }
  101. /* Wait for Empty Tx Buffer */
  102. while (!(AT91C_US0_CSR & AT91C_US_TXRDY));
  103. /* Transmit Character */
  104. AT91C_US0_THR = *str;
  105. str ++;
  106. }
  107. }
  108. static void rt_hw_console_init()
  109. {
  110. /* Enable Clock for USART0 */
  111. AT91C_PMC_PCER = 1 << AT91C_ID_US0;
  112. /* Enable RxD0 and TxDO Pin */
  113. AT91C_PIO_PDR = (1 << 5) | (1 << 6);
  114. AT91C_US0_CR = AT91C_US_RSTRX | /* Reset Receiver */
  115. AT91C_US_RSTTX | /* Reset Transmitter */
  116. AT91C_US_RXDIS | /* Receiver Disable */
  117. AT91C_US_TXDIS; /* Transmitter Disable */
  118. AT91C_US0_MR = AT91C_US_USMODE_NORMAL | /* Normal Mode */
  119. AT91C_US_CLKS_CLOCK | /* Clock = MCK */
  120. AT91C_US_CHRL_8_BITS | /* 8-bit Data */
  121. AT91C_US_PAR_NONE | /* No Parity */
  122. AT91C_US_NBSTOP_1_BIT; /* 1 Stop Bit */
  123. /* set baud rate divisor */
  124. AT91C_US0_BRGR = BRD;
  125. AT91C_US0_CR = AT91C_US_RXEN | /* Receiver Enable */
  126. AT91C_US_TXEN; /* Transmitter Enable */
  127. }
  128. /**
  129. * This function will initial sam7s64 board.
  130. */
  131. void rt_hw_board_init()
  132. {
  133. extern void rt_serial_init(void);
  134. /* init hardware console */
  135. rt_hw_console_init();
  136. /* init led */
  137. rt_hw_board_led_init();
  138. /* init PITC */
  139. AT91C_PITC_PIMR = (1 << 25) | (1 << 24) | PIV;
  140. /* install timer handler */
  141. rt_hw_interrupt_install(AT91C_ID_SYS, rt_hw_timer_handler, RT_NULL);
  142. AT91C_AIC_SMR(AT91C_ID_SYS) = 0;
  143. rt_hw_interrupt_umask(AT91C_ID_SYS);
  144. }
  145. /*@}*/