board.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. * 2010-03-09 ljt8015 Fix a bug in rt_hw_console_init()
  14. */
  15. #include <rtthread.h>
  16. #include <rthw.h>
  17. #include <AT91SAM7X256.h>
  18. #include "board.h"
  19. static void rt_hw_board_led_init(void);
  20. /**
  21. * @addtogroup sam7s
  22. */
  23. /*@{*/
  24. /* Periodic Interval Value */
  25. #define PIV (((MCK/16)/1000)*(1000/RT_TICK_PER_SECOND))
  26. /**
  27. * This is the timer interrupt service routine.
  28. * @param vector the irq number for timer
  29. */
  30. void rt_hw_timer_handler(int vector, void* param)
  31. {
  32. if (AT91C_BASE_PITC->PITC_PISR & 0x01)
  33. {
  34. /* increase a tick */
  35. rt_tick_increase();
  36. /* ack interrupt */
  37. AT91C_BASE_AIC->AIC_EOICR = AT91C_BASE_PITC->PITC_PIVR;
  38. }
  39. else
  40. {
  41. /* end of interrupt */
  42. AT91C_BASE_AIC->AIC_EOICR = 0;
  43. }
  44. }
  45. /* PIO Flash PA PB PIN */
  46. #define LED1 (1 << 19) /* PA0 / PGMEN0 & PWM0 TIOA0 48 */
  47. #define LED2 (1 << 20) /* PA1 / PGMEN1 & PWM1 TIOB0 47 */
  48. #define LED3 (1 << 21) /* PA2 & PWM2 SCK0 44 */
  49. #define LED4 (1 << 22) /* PA3 & TWD NPCS3 43 */
  50. #define LED_MASK (LED1|LED2|LED3|LED4)
  51. int leds[] = {LED1, LED2, LED3, LED4};
  52. /**
  53. * This function will init led on the board
  54. */
  55. static void rt_hw_board_led_init()
  56. {
  57. /* enable the clock of the PIO A, PIO B */
  58. AT91C_BASE_PMC->PMC_PCER = 1 << AT91C_ID_PIOA | 1 << AT91C_ID_PIOB;
  59. /* configure PIO as output for led */
  60. AT91C_BASE_PIOB->PIO_PER = LED_MASK;
  61. AT91C_BASE_PIOB->PIO_OER = LED_MASK;
  62. }
  63. /**
  64. * This function will take the led on board on.
  65. *
  66. * @param n the number nth led
  67. */
  68. void rt_hw_board_led_on(int n)
  69. {
  70. if (n >= 0 && n < 4)
  71. {
  72. AT91C_BASE_PIOB->PIO_CODR = leds[n];
  73. }
  74. }
  75. /**
  76. * This function will take the led on board off.
  77. *
  78. * @param n the number nth led
  79. */
  80. void rt_hw_board_led_off(int n)
  81. {
  82. if (n >= 0 && n < 4)
  83. {
  84. AT91C_BASE_PIOB->PIO_SODR = leds[n];
  85. }
  86. }
  87. /*
  88. * RT-Thread Console Interface, used by rt_kprintf
  89. */
  90. /**
  91. * This function is used to display a string on console, normally, it's
  92. * invoked by rt_kprintf
  93. *
  94. * @param str the displayed string
  95. */
  96. void rt_hw_console_output(const char* str)
  97. {
  98. while (*str)
  99. {
  100. if (*str == '\n')
  101. {
  102. while (!(AT91C_BASE_US0->US_CSR & AT91C_US_TXRDY));
  103. AT91C_BASE_US0->US_THR = '\r';
  104. }
  105. /* Wait for Empty Tx Buffer */
  106. while (!(AT91C_BASE_US0->US_CSR & AT91C_US_TXRDY));
  107. /* Transmit Character */
  108. AT91C_BASE_US0->US_THR = *str;
  109. str ++;
  110. }
  111. }
  112. static void rt_hw_console_init()
  113. {
  114. /* Enable Clock for USART0 */
  115. AT91C_BASE_PMC->PMC_PCER = 1 << AT91C_ID_US0;
  116. /* Enable RxD0 and TxD0 Pin */
  117. //AT91C_BASE_PIOA->PIO_PDR = (1 << 5) | (1 << 6);
  118. AT91C_BASE_PIOA->PIO_PDR = 1 | (1 << 1);//fix bug 2010-3-9
  119. AT91C_BASE_US0->US_CR = AT91C_US_RSTRX | /* Reset Receiver */
  120. AT91C_US_RSTTX | /* Reset Transmitter */
  121. AT91C_US_RXDIS | /* Receiver Disable */
  122. AT91C_US_TXDIS; /* Transmitter Disable */
  123. AT91C_BASE_US0->US_MR = AT91C_US_USMODE_NORMAL | /* Normal Mode */
  124. AT91C_US_CLKS_CLOCK | /* Clock = MCK */
  125. AT91C_US_CHRL_8_BITS | /* 8-bit Data */
  126. AT91C_US_PAR_NONE | /* No Parity */
  127. AT91C_US_NBSTOP_1_BIT; /* 1 Stop Bit */
  128. /* set baud rate divisor */
  129. AT91C_BASE_US0->US_BRGR = BRD;
  130. AT91C_BASE_US0->US_CR = AT91C_US_RXEN | /* Receiver Enable */
  131. AT91C_US_TXEN; /* Transmitter Enable */
  132. }
  133. /**
  134. * This function will initial sam7x board.
  135. */
  136. void rt_hw_board_init(void)
  137. {
  138. extern void rt_serial_init(void);
  139. /* init hardware console */
  140. rt_hw_console_init();
  141. /* init led */
  142. rt_hw_board_led_init();
  143. /* init PITC */
  144. AT91C_BASE_PITC->PITC_PIMR = (1 << 25) | (1 << 24) | PIV;
  145. /* install timer handler */
  146. rt_hw_interrupt_install(AT91C_ID_SYS, rt_hw_timer_handler, RT_NULL, "tick");
  147. AT91C_BASE_AIC->AIC_SMR[AT91C_ID_SYS] = 0;
  148. rt_hw_interrupt_umask(AT91C_ID_SYS);
  149. }
  150. /*@}*/