board.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * File : board.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2010-11-13 weety first version
  23. */
  24. #include <rtthread.h>
  25. #include <rthw.h>
  26. #include <mmu.h>
  27. #include "board.h"
  28. /**
  29. * @addtogroup dm365
  30. */
  31. /*@{*/
  32. #if defined(__CC_ARM)
  33. extern int Image$$ER_ZI$$ZI$$Base;
  34. extern int Image$$ER_ZI$$ZI$$Length;
  35. extern int Image$$ER_ZI$$ZI$$Limit;
  36. #elif (defined (__GNUC__))
  37. rt_uint8_t _irq_stack_start[1024];
  38. rt_uint8_t _fiq_stack_start[1024];
  39. rt_uint8_t _undefined_stack_start[512];
  40. rt_uint8_t _abort_stack_start[512];
  41. rt_uint8_t _svc_stack_start[1024] SECTION(".nobss");
  42. extern unsigned char __bss_start;
  43. extern unsigned char __bss_end;
  44. #endif
  45. extern void rt_hw_clock_init(void);
  46. extern void rt_hw_uart_init(void);
  47. static struct mem_desc dm365_mem_desc[] = {
  48. { 0x80000000, 0x88000000-1, 0x80000000, SECT_RW_CB, 0, SECT_MAPPED }, /* 128M cached SDRAM memory */
  49. { 0xA0000000, 0xA8000000-1, 0x80000000, SECT_RW_NCNB, 0, SECT_MAPPED }, /* 128M No cached SDRAM memory */
  50. { 0xFFFF0000, 0xFFFF1000-1, 0x80000000, SECT_TO_PAGE, PAGE_RO_CB, PAGE_MAPPED }, /* isr vector table */
  51. { 0x01C00000, 0x02000000-1, 0x01C00000, SECT_RW_NCNB, 0, SECT_MAPPED }, /* CFG BUS peripherals */
  52. { 0x02000000, 0x0A000000-1, 0x02000000, SECT_RW_NCNB, 0, SECT_MAPPED }, /* AEMIF */
  53. };
  54. /**
  55. * This function will handle rtos timer
  56. */
  57. void rt_timer_handler(int vector, void *param)
  58. {
  59. rt_tick_increase();
  60. }
  61. /**
  62. * This function will init timer0 for system ticks
  63. */
  64. void rt_hw_timer_init()
  65. {
  66. /* timer0, input clocks 24MHz */
  67. volatile timer_regs_t *regs =
  68. (volatile timer_regs_t*)DAVINCI_TIMER1_BASE;//DAVINCI_TIMER0_BASE;
  69. psc_change_state(DAVINCI_DM365_LPSC_TIMER0, 3);
  70. psc_change_state(DAVINCI_DM365_LPSC_TIMER1, 3);
  71. /*disable timer*/
  72. regs->tcr &= ~(0x3UL << 6);
  73. //TIMMODE 32BIT UNCHAINED MODE
  74. regs->tgcr |=(0x1UL << 2);
  75. /*not in reset timer */
  76. regs->tgcr |= (0x1UL << 0);
  77. //regs->tgcr &= ~(0x1UL << 1);
  78. /* set Period Registers */
  79. regs->prd12 = 24000000/RT_TICK_PER_SECOND;
  80. regs->tim12 = 0;
  81. /* Set enable mode */
  82. regs->tcr |= (0x2UL << 6); //period mode
  83. /* install interrupt handler */
  84. rt_hw_interrupt_install(IRQ_DM365_TINT2, rt_timer_handler,
  85. RT_NULL, "timer1_12");//IRQ_DM365_TINT0_TINT12
  86. rt_hw_interrupt_umask(IRQ_DM365_TINT2);//IRQ_DM365_TINT2
  87. }
  88. #define LSR_DR 0x01 /* Data ready */
  89. #define LSR_THRE 0x20 /* Xmit holding register empty */
  90. #define BPS 115200 /* serial baudrate */
  91. typedef struct uartport
  92. {
  93. volatile rt_uint32_t rbr;
  94. volatile rt_uint32_t ier;
  95. volatile rt_uint32_t fcr;
  96. volatile rt_uint32_t lcr;
  97. volatile rt_uint32_t mcr;
  98. volatile rt_uint32_t lsr;
  99. volatile rt_uint32_t msr;
  100. volatile rt_uint32_t scr;
  101. volatile rt_uint32_t dll;
  102. volatile rt_uint32_t dlh;
  103. volatile rt_uint32_t res[2];
  104. volatile rt_uint32_t pwremu_mgmt;
  105. volatile rt_uint32_t mdr;
  106. }uartport;
  107. #define thr rbr
  108. #define iir fcr
  109. #define UART0 ((struct uartport *)DAVINCI_UART0_BASE)
  110. static void davinci_uart_putc(char c)
  111. {
  112. while (!(UART0->lsr & LSR_THRE));
  113. UART0->thr = c;
  114. }
  115. /**
  116. * This function is used to display a string on console, normally, it's
  117. * invoked by rt_kprintf
  118. *
  119. * @param str the displayed string
  120. */
  121. void rt_hw_console_output(const char* str)
  122. {
  123. while (*str)
  124. {
  125. if (*str=='\n')
  126. {
  127. davinci_uart_putc('\r');
  128. }
  129. davinci_uart_putc(*str++);
  130. }
  131. }
  132. static void rt_hw_console_init(void)
  133. {
  134. rt_uint32_t divisor;
  135. divisor = (24000000 + (BPS * (16 / 2))) / (16 * BPS);
  136. UART0->ier = 0;
  137. UART0->lcr = 0x83; //8N1
  138. UART0->dll = 0;
  139. UART0->dlh = 0;
  140. UART0->lcr = 0x03;
  141. UART0->mcr = 0x03; //RTS,CTS
  142. UART0->fcr = 0x07; //FIFO
  143. UART0->lcr = 0x83;
  144. UART0->dll = divisor & 0xff;
  145. UART0->dlh = (divisor >> 8) & 0xff;
  146. UART0->lcr = 0x03;
  147. UART0->mdr = 0; //16x over-sampling
  148. UART0->pwremu_mgmt = 0x6000;
  149. }
  150. /**
  151. * This function will init dm365 board
  152. */
  153. void rt_hw_board_init()
  154. {
  155. /* initialize console */
  156. rt_hw_console_init();
  157. /* initialize mmu */
  158. rt_hw_mmu_init(dm365_mem_desc, sizeof(dm365_mem_desc)/sizeof(dm365_mem_desc[0]));
  159. /* initialize hardware interrupt */
  160. rt_hw_interrupt_init();
  161. /* initialize the system clock */
  162. rt_hw_clock_init();
  163. /* initialize heap memory system */
  164. #ifdef __CC_ARM
  165. rt_system_heap_init((void*)&Image$$ER_ZI$$ZI$$Limit, (void*)0x88000000);
  166. #else
  167. rt_system_heap_init((void*)&__bss_end, (void*)0x88000000);
  168. #endif
  169. /* initialize early device */
  170. #ifdef RT_USING_COMPONENTS_INIT
  171. rt_components_board_init();
  172. #endif
  173. #ifdef RT_USING_CONSOLE
  174. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  175. #endif
  176. /* initialize timer0 */
  177. rt_hw_timer_init();
  178. }
  179. /*@}*/