board.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * File : board.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006-2012, 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://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2010-06-25 Bernard first version
  13. * 2011-08-08 lgnq modified for Loongson LS1B
  14. * 2015-07-06 chinesebear modified for Loongson LS1C
  15. */
  16. #include <rtthread.h>
  17. #include <drivers/spi.h>
  18. #include <rthw.h>
  19. #include "board.h"
  20. #include "uart.h"
  21. #include "ls1c.h"
  22. #include "ls1c_pin.h"
  23. #include "ls1c_spi.h"
  24. #include "ls1c_spi.h"
  25. #include "drv_spi.h"
  26. /**
  27. * @addtogroup Loongson LS1B
  28. */
  29. /*@{*/
  30. /**
  31. * This is the timer interrupt service routine.
  32. */
  33. void rt_hw_timer_handler(void)
  34. {
  35. unsigned int count;
  36. count = read_c0_compare();
  37. write_c0_compare(count);
  38. write_c0_count(0);
  39. /* increase a OS tick */
  40. rt_tick_increase();
  41. }
  42. /**
  43. * This function will initial OS timer
  44. */
  45. void rt_hw_timer_init(void)
  46. {
  47. write_c0_compare(CPU_HZ/2/RT_TICK_PER_SECOND);
  48. write_c0_count(0);
  49. }
  50. /**
  51. * init hardware FPU
  52. */
  53. void rt_hw_fpu_init(void)
  54. {
  55. rt_uint32_t c0_status = 0;
  56. rt_uint32_t c1_status = 0;
  57. // 使能协处理器1--FPU
  58. c0_status = read_c0_status();
  59. c0_status |= (ST0_CU1 | ST0_FR);
  60. write_c0_status(c0_status);
  61. // 配置FPU
  62. c1_status = read_c1_status();
  63. c1_status |= (FPU_CSR_FS | FPU_CSR_FO | FPU_CSR_FN); // set FS, FO, FN
  64. c1_status &= ~(FPU_CSR_ALL_E); // disable exception
  65. c1_status = (c1_status & (~FPU_CSR_RM)) | FPU_CSR_RN; // set RN
  66. write_c1_status(c1_status);
  67. return ;
  68. }
  69. /**
  70. * This function will initial sam7s64 board.
  71. */
  72. void rt_hw_board_init(void)
  73. {
  74. #ifdef RT_USING_UART
  75. /* init hardware UART device */
  76. rt_hw_uart_init();
  77. #endif
  78. #ifdef RT_USING_CONSOLE
  79. /* set console device */
  80. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  81. #endif
  82. #ifdef RT_USING_CAN
  83. /* init hardware CAN device */
  84. ls1c_bxcan_init();
  85. #endif
  86. #ifdef RT_USING_I2C
  87. #ifdef RT_USING_I2C0
  88. /*
  89. pin_set_purpose(2, PIN_PURPOSE_OTHER);
  90. pin_set_purpose(3, PIN_PURPOSE_OTHER);
  91. pin_set_remap(2, PIN_REMAP_SECOND);
  92. pin_set_remap(3, PIN_REMAP_SECOND);
  93. */
  94. #endif
  95. #ifdef RT_USING_I2C1
  96. pin_set_purpose(2, PIN_PURPOSE_OTHER);
  97. pin_set_purpose(3, PIN_PURPOSE_OTHER);
  98. pin_set_remap(2, PIN_REMAP_SECOND);
  99. pin_set_remap(3, PIN_REMAP_SECOND);
  100. #endif
  101. #ifdef RT_USING_I2C2
  102. pin_set_purpose(51, PIN_PURPOSE_OTHER);
  103. pin_set_purpose(50, PIN_PURPOSE_OTHER);
  104. pin_set_remap(51, PIN_REMAP_FOURTH);
  105. pin_set_remap(50, PIN_REMAP_FOURTH);
  106. #endif
  107. rt_i2c_init();
  108. #endif
  109. #ifdef RT_USING_SPI
  110. #ifdef RT_USING_SPI0
  111. pin_set_purpose(78, PIN_PURPOSE_OTHER);
  112. pin_set_purpose(79, PIN_PURPOSE_OTHER);
  113. pin_set_purpose(80, PIN_PURPOSE_OTHER);
  114. pin_set_purpose(83, PIN_PURPOSE_OTHER);//cs2 - SD card
  115. pin_set_purpose(82, PIN_PURPOSE_OTHER);//cs1
  116. pin_set_remap(78, PIN_REMAP_FOURTH);
  117. pin_set_remap(79, PIN_REMAP_FOURTH);
  118. pin_set_remap(80, PIN_REMAP_FOURTH);
  119. pin_set_remap(83, PIN_REMAP_FOURTH);//cs2 - SD card
  120. pin_set_remap(82, PIN_REMAP_FOURTH);//cs1
  121. ls1c_spi_bus_register(LS1C_SPI_0,"spi0");
  122. #endif
  123. #ifdef RT_USING_SPI1
  124. pin_set_purpose(46, PIN_PURPOSE_OTHER);
  125. pin_set_purpose(47, PIN_PURPOSE_OTHER);
  126. pin_set_purpose(48, PIN_PURPOSE_OTHER);
  127. pin_set_purpose(49, PIN_PURPOSE_OTHER);//CS0 - touch screen
  128. pin_set_remap(46, PIN_REMAP_THIRD);
  129. pin_set_remap(47, PIN_REMAP_THIRD);
  130. pin_set_remap(48, PIN_REMAP_THIRD);
  131. pin_set_remap(49, PIN_REMAP_THIRD);//CS0 - touch screen
  132. ls1c_spi_bus_register(LS1C_SPI_1,"spi1");
  133. #endif
  134. #ifdef RT_USING_SPI0
  135. /* attach cs */
  136. {
  137. static struct rt_spi_device spi_device1;
  138. static struct rt_spi_device spi_device2;
  139. static struct ls1c_spi_cs spi_cs1;
  140. static struct ls1c_spi_cs spi_cs2;
  141. /* spi02: CS2 SD Card*/
  142. spi_cs2.cs = LS1C_SPI_CS_2;
  143. rt_spi_bus_attach_device(&spi_device2, "spi02", "spi0", (void*)&spi_cs2);
  144. spi_cs1.cs = LS1C_SPI_CS_1;
  145. rt_spi_bus_attach_device(&spi_device1, "spi01", "spi0", (void*)&spi_cs1);
  146. msd_init("sd0", "spi02");
  147. }
  148. #endif
  149. #ifdef RT_USING_SPI1
  150. {
  151. static struct rt_spi_device spi_device;
  152. static struct ls1c_spi_cs spi_cs;
  153. /* spi10: CS0 Touch*/
  154. spi_cs.cs = LS1C_SPI_CS_0;
  155. rt_spi_bus_attach_device(&spi_device, "spi10", "spi1", (void*)&spi_cs);
  156. }
  157. #endif
  158. #endif
  159. /* init operating system timer */
  160. rt_hw_timer_init();
  161. #ifdef RT_USING_FPU
  162. /* init hardware fpu */
  163. rt_hw_fpu_init();
  164. #endif
  165. rt_kprintf("current sr: 0x%08x\n", read_c0_status());
  166. }
  167. #ifdef RT_USING_RTGUI
  168. #include <rtgui/driver.h>
  169. #include "display_controller.h"
  170. /* initialize for gui driver */
  171. int rtgui_lcd_init(void)
  172. {
  173. rt_device_t dc;
  174. rt_kprintf("DC initied\n");
  175. pin_set_purpose(76, PIN_PURPOSE_OTHER);
  176. pin_set_remap(76, PIN_REMAP_DEFAULT);
  177. /* init Display Controller */
  178. rt_hw_dc_init();
  179. /* find Display Controller device */
  180. dc = rt_device_find("dc");
  181. /* set Display Controller device as rtgui graphic driver */
  182. rtgui_graphic_set_device(dc);
  183. return 0;
  184. }
  185. INIT_DEVICE_EXPORT(rtgui_lcd_init);
  186. #endif
  187. #define __raw_out_put(unr) \
  188. while (*ptr) \
  189. { \
  190. if (*ptr == '\n') \
  191. { \
  192. /* FIFO status, contain valid data */ \
  193. while (!(UART_LSR(UART##unr##_BASE) & (UARTLSR_TE | UARTLSR_TFE))); \
  194. /* write data */ \
  195. UART_DAT(UART##unr##_BASE) = '\r'; \
  196. } \
  197. /* FIFO status, contain valid data */ \
  198. while (!(UART_LSR(UART##unr##_BASE) & (UARTLSR_TE | UARTLSR_TFE))); \
  199. /* write data */ \
  200. UART_DAT(UART##unr##_BASE) = *ptr; \
  201. ptr ++; \
  202. }
  203. /* UART line status register value */
  204. #define UARTLSR_ERROR (1 << 7)
  205. #define UARTLSR_TE (1 << 6)
  206. #define UARTLSR_TFE (1 << 5)
  207. #define UARTLSR_BI (1 << 4)
  208. #define UARTLSR_FE (1 << 3)
  209. #define UARTLSR_PE (1 << 2)
  210. #define UARTLSR_OE (1 << 1)
  211. #define UARTLSR_DR (1 << 0)
  212. void rt_hw_console_output(const char *ptr)
  213. {
  214. #if defined(RT_USING_UART0)
  215. __raw_out_put(0);
  216. #elif defined(RT_USING_UART2)
  217. __raw_out_put(2);
  218. #elif defined(RT_USING_UART3)
  219. __raw_out_put(3);
  220. #endif
  221. }
  222. /*@}*/