board.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 <AT91SAM7X.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 LED1 (1 << 19) /* PA0 / PGMEN0 & PWM0 TIOA0 48 */
  46. #define LED2 (1 << 20) /* PA1 / PGMEN1 & PWM1 TIOB0 47 */
  47. #define LED3 (1 << 21) /* PA2 & PWM2 SCK0 44 */
  48. #define LED4 (1 << 22) /* PA3 & TWD NPCS3 43 */
  49. #define LED_MASK (LED1|LED2|LED3|LED4)
  50. int leds[] = {LED1, LED2, LED3, LED4};
  51. /**
  52. * This function will init led on the board
  53. */
  54. static void rt_hw_board_led_init()
  55. {
  56. /* enable the clock of the PIO A, PIO B */
  57. AT91C_PMC_PCER = 1 << AT91C_ID_PIOA | 1 << AT91C_ID_PIOB;
  58. /* configure PIO as output for led */
  59. AT91C_PIOB_PER = LED_MASK;
  60. AT91C_PIOB_OER = LED_MASK;
  61. }
  62. /**
  63. * This function will take the led on board on.
  64. *
  65. * @param n the number nth led
  66. */
  67. void rt_hw_board_led_on(int n)
  68. {
  69. if (n >= 0 && n < 4)
  70. {
  71. AT91C_PIOB_CODR = leds[n];
  72. }
  73. }
  74. /**
  75. * This function will take the led on board off.
  76. *
  77. * @param n the number nth led
  78. */
  79. void rt_hw_board_led_off(int n)
  80. {
  81. if (n >= 0 && n < 4)
  82. {
  83. AT91C_PIOB_SODR = leds[n];
  84. }
  85. }
  86. /*
  87. * RT-Thread Console Interface, used by rt_kprintf
  88. */
  89. /**
  90. * This function is used to display a string on console, normally, it's
  91. * invoked by rt_kprintf
  92. *
  93. * @param str the displayed string
  94. */
  95. void rt_hw_console_output(const char* str)
  96. {
  97. while (*str)
  98. {
  99. if (*str == '\n')
  100. {
  101. while (!(AT91C_US0_CSR & AT91C_US_TXRDY));
  102. AT91C_US0_THR = '\r';
  103. }
  104. /* Wait for Empty Tx Buffer */
  105. while (!(AT91C_US0_CSR & AT91C_US_TXRDY));
  106. /* Transmit Character */
  107. AT91C_US0_THR = *str;
  108. str ++;
  109. }
  110. }
  111. static void rt_hw_console_init()
  112. {
  113. /* Enable Clock for USART0 */
  114. AT91C_PMC_PCER = 1 << AT91C_ID_US0;
  115. /* Enable RxD0 and TxDO Pin */
  116. AT91C_PIO_PDR = (1 << 5) | (1 << 6);
  117. AT91C_US0_CR = AT91C_US_RSTRX | /* Reset Receiver */
  118. AT91C_US_RSTTX | /* Reset Transmitter */
  119. AT91C_US_RXDIS | /* Receiver Disable */
  120. AT91C_US_TXDIS; /* Transmitter Disable */
  121. AT91C_US0_MR = AT91C_US_USMODE_NORMAL | /* Normal Mode */
  122. AT91C_US_CLKS_CLOCK | /* Clock = MCK */
  123. AT91C_US_CHRL_8_BITS | /* 8-bit Data */
  124. AT91C_US_PAR_NONE | /* No Parity */
  125. AT91C_US_NBSTOP_1_BIT; /* 1 Stop Bit */
  126. /* set baud rate divisor */
  127. AT91C_US0_BRGR = BRD;
  128. AT91C_US0_CR = AT91C_US_RXEN | /* Receiver Enable */
  129. AT91C_US_TXEN; /* Transmitter Enable */
  130. }
  131. /**
  132. * This function will initial sam7x board.
  133. */
  134. void rt_hw_board_init()
  135. {
  136. extern void rt_serial_init(void);
  137. /* init hardware console */
  138. rt_hw_console_init();
  139. /* init led */
  140. rt_hw_board_led_init();
  141. /* init PITC */
  142. AT91C_PITC_PIMR = (1 << 25) | (1 << 24) | PIV;
  143. /* install timer handler */
  144. rt_hw_interrupt_install(AT91C_ID_SYS, rt_hw_timer_handler, RT_NULL);
  145. AT91C_AIC_SMR(AT91C_ID_SYS) = 0;
  146. rt_hw_interrupt_umask(AT91C_ID_SYS);
  147. }
  148. /*@}*/