board.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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-11-13 weety first version
  9. */
  10. #include <rtthread.h>
  11. #include <rthw.h>
  12. #include <mmu.h>
  13. #include "board.h"
  14. /**
  15. * @addtogroup dm365
  16. */
  17. /*@{*/
  18. #if defined(__CC_ARM)
  19. extern int Image$$ER_ZI$$ZI$$Base;
  20. extern int Image$$ER_ZI$$ZI$$Length;
  21. extern int Image$$ER_ZI$$ZI$$Limit;
  22. #elif (defined (__GNUC__))
  23. rt_uint8_t _irq_stack_start[1024];
  24. rt_uint8_t _fiq_stack_start[1024];
  25. rt_uint8_t _undefined_stack_start[512];
  26. rt_uint8_t _abort_stack_start[512];
  27. rt_uint8_t _svc_stack_start[1024] rt_section(".nobss");
  28. extern unsigned char __bss_start;
  29. extern unsigned char __bss_end;
  30. #endif
  31. extern void rt_hw_clock_init(void);
  32. extern void rt_hw_uart_init(void);
  33. static struct mem_desc dm365_mem_desc[] = {
  34. { 0x80000000, 0x88000000-1, 0x80000000, SECT_RW_CB, 0, SECT_MAPPED }, /* 128M cached SDRAM memory */
  35. { 0xA0000000, 0xA8000000-1, 0x80000000, SECT_RW_NCNB, 0, SECT_MAPPED }, /* 128M No cached SDRAM memory */
  36. { 0xFFFF0000, 0xFFFF1000-1, 0x80000000, SECT_TO_PAGE, PAGE_RO_CB, PAGE_MAPPED }, /* isr vector table */
  37. { 0x01C00000, 0x02000000-1, 0x01C00000, SECT_RW_NCNB, 0, SECT_MAPPED }, /* CFG BUS peripherals */
  38. { 0x02000000, 0x0A000000-1, 0x02000000, SECT_RW_NCNB, 0, SECT_MAPPED }, /* AEMIF */
  39. };
  40. /**
  41. * This function will handle rtos timer
  42. */
  43. void rt_timer_handler(int vector, void *param)
  44. {
  45. rt_tick_increase();
  46. }
  47. /**
  48. * This function will init timer0 for system ticks
  49. */
  50. void rt_hw_timer_init()
  51. {
  52. /* timer0, input clocks 24MHz */
  53. volatile timer_regs_t *regs =
  54. (volatile timer_regs_t*)DAVINCI_TIMER1_BASE;//DAVINCI_TIMER0_BASE;
  55. psc_change_state(DAVINCI_DM365_LPSC_TIMER0, 3);
  56. psc_change_state(DAVINCI_DM365_LPSC_TIMER1, 3);
  57. /*disable timer*/
  58. regs->tcr &= ~(0x3UL << 6);
  59. //TIMMODE 32BIT UNCHAINED MODE
  60. regs->tgcr |=(0x1UL << 2);
  61. /*not in reset timer */
  62. regs->tgcr |= (0x1UL << 0);
  63. //regs->tgcr &= ~(0x1UL << 1);
  64. /* set Period Registers */
  65. regs->prd12 = 24000000/RT_TICK_PER_SECOND;
  66. regs->tim12 = 0;
  67. /* Set enable mode */
  68. regs->tcr |= (0x2UL << 6); //period mode
  69. /* install interrupt handler */
  70. rt_hw_interrupt_install(IRQ_DM365_TINT2, rt_timer_handler,
  71. RT_NULL, "timer1_12");//IRQ_DM365_TINT0_TINT12
  72. rt_hw_interrupt_umask(IRQ_DM365_TINT2);//IRQ_DM365_TINT2
  73. }
  74. #define LSR_DR 0x01 /* Data ready */
  75. #define LSR_THRE 0x20 /* Xmit holding register empty */
  76. #define BPS 115200 /* serial baudrate */
  77. typedef struct uartport
  78. {
  79. volatile rt_uint32_t rbr;
  80. volatile rt_uint32_t ier;
  81. volatile rt_uint32_t fcr;
  82. volatile rt_uint32_t lcr;
  83. volatile rt_uint32_t mcr;
  84. volatile rt_uint32_t lsr;
  85. volatile rt_uint32_t msr;
  86. volatile rt_uint32_t scr;
  87. volatile rt_uint32_t dll;
  88. volatile rt_uint32_t dlh;
  89. volatile rt_uint32_t res[2];
  90. volatile rt_uint32_t pwremu_mgmt;
  91. volatile rt_uint32_t mdr;
  92. }uartport;
  93. #define thr rbr
  94. #define iir fcr
  95. #define UART0 ((struct uartport *)DAVINCI_UART0_BASE)
  96. static void davinci_uart_putc(char c)
  97. {
  98. while (!(UART0->lsr & LSR_THRE));
  99. UART0->thr = c;
  100. }
  101. /**
  102. * This function is used to display a string on console, normally, it's
  103. * invoked by rt_kprintf
  104. *
  105. * @param str the displayed string
  106. */
  107. void rt_hw_console_output(const char* str)
  108. {
  109. while (*str)
  110. {
  111. if (*str=='\n')
  112. {
  113. davinci_uart_putc('\r');
  114. }
  115. davinci_uart_putc(*str++);
  116. }
  117. }
  118. static void rt_hw_console_init(void)
  119. {
  120. rt_uint32_t divisor;
  121. divisor = (24000000 + (BPS * (16 / 2))) / (16 * BPS);
  122. UART0->ier = 0;
  123. UART0->lcr = 0x83; //8N1
  124. UART0->dll = 0;
  125. UART0->dlh = 0;
  126. UART0->lcr = 0x03;
  127. UART0->mcr = 0x03; //RTS,CTS
  128. UART0->fcr = 0x07; //FIFO
  129. UART0->lcr = 0x83;
  130. UART0->dll = divisor & 0xff;
  131. UART0->dlh = (divisor >> 8) & 0xff;
  132. UART0->lcr = 0x03;
  133. UART0->mdr = 0; //16x over-sampling
  134. UART0->pwremu_mgmt = 0x6000;
  135. }
  136. /**
  137. * This function will init dm365 board
  138. */
  139. void rt_hw_board_init()
  140. {
  141. /* initialize console */
  142. rt_hw_console_init();
  143. /* initialize mmu */
  144. rt_hw_mmu_init(dm365_mem_desc, sizeof(dm365_mem_desc)/sizeof(dm365_mem_desc[0]));
  145. /* initialize hardware interrupt */
  146. rt_hw_interrupt_init();
  147. /* initialize the system clock */
  148. rt_hw_clock_init();
  149. /* initialize heap memory system */
  150. #ifdef __CC_ARM
  151. rt_system_heap_init((void*)&Image$$ER_ZI$$ZI$$Limit, (void*)0x88000000);
  152. #else
  153. rt_system_heap_init((void*)&__bss_end, (void*)0x88000000);
  154. #endif
  155. /* initialize early device */
  156. #ifdef RT_USING_COMPONENTS_INIT
  157. rt_components_board_init();
  158. #endif
  159. #if defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE)
  160. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  161. #endif
  162. /* initialize timer0 */
  163. rt_hw_timer_init();
  164. }
  165. /*@}*/