bsp.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * bsp.c
  3. *
  4. * Created on: Feb 9, 2012
  5. * Author: TongXiangbiao
  6. */
  7. #include "bsp.h"
  8. #include "rt.h"
  9. #ifdef __LIGHT_DIFF_ACT_PRO__ // sunxi 20190904 光差保护
  10. #include "fec.h" // sunxi 20190904 added
  11. #endif // __LIGHT_DIFF_ACT_PRO__
  12. int g_con_uart_index;
  13. /*------------------------------ 类型结构 -------------------------------------
  14. */
  15. /*------------------------------ 全局变量 -------------------------------------
  16. */
  17. const char * szTool = "JHW GH-F308 1.00.01";
  18. struct init_t g_bsp_init_func[]=
  19. {
  20. {ccu_init,ERR_CODE_INIT_SOFTWARE}, //初始化系统时钟CCU
  21. {bsp_ustimer_init,ERR_CODE_INIT_SOFTWARE}, //初始化内核USTIMER
  22. {ustimer_init,ERR_CODE_INIT_SOFTWARE}, //初始化USTIMER
  23. {shm_init,ERR_CODE_INIT_SOFTWARE}, //初始化shm
  24. {mb_init,ERR_CODE_INIT_SOFTWARE}, //初始化mb
  25. {watchdog_init,ERR_CODE_INIT_SOFTWARE}, //初始化watchdog
  26. {gpio_init,ERR_CODE_INIT_SOFTWARE}, //初始化GPIO
  27. {i2c_init,ERR_CODE_INIT_SOFTWARE}, //初始化I2C模块
  28. {rtc_init,ERR_CODE_INIT_SOFTWARE}, //初始化RTC模块
  29. {dspi_init,ERR_CODE_INIT_SOFTWARE}, //初始化DSPI模块
  30. {log_init,ERR_CODE_LOG}, //日志初始化需要上电时间,放在RTC初始化后
  31. // {adc_init,ERR_CODE_INIT_SOFTWARE}, //初始化ADC模块,需要根据配置设置ADC输入范围,移至通道配置后
  32. // {freq_init,ERR_CODE_INIT_SOFTWARE}, //初始化频率测量模块 -------- noted by sunxi 移到裸核上
  33. // {mac_init,ERR_CODE_INIT_SOFTWARE}, //初始化MAC模块
  34. {e2prom_init,ERR_CODE_INIT_E2PROM}, //初始化E2PROM模块
  35. #ifdef BSP_CAN_ENABLE
  36. {can_init,ERR_CODE_INIT_SOFTWARE}, //初始化CAN模块
  37. {can_app_init,ERR_CODE_INIT_SOFTWARE}, //初始化CAN_APP模块
  38. #endif
  39. // {uart_init,ERR_CODE_INIT_SOFTWARE}, //初始化UART模块,无需集中初始化,使用时再初始化,防止和通讯子站软件冲突
  40. // {pit_init,ERR_CODE_UNKOWN} //初始化PIT(可编程中断定时器)
  41. #ifdef __LIGHT_DIFF_ACT_PRO__ // sunxi 20190904 光差保护
  42. {fec2_reinit,ERR_CODE_NUM-1}, // sunxi 20190904 added
  43. #endif // __LIGHT_DIFF_ACT_PRO__
  44. };
  45. #define BSP_INIT_NUM ((int)(sizeof(g_bsp_init_func)/sizeof(g_bsp_init_func[0])))
  46. INIT_FUNC g_bsp_exit_func[]=
  47. {
  48. // 关闭打开的文件
  49. #ifdef __LIGHT_DIFF_ACT_PRO__ // sunxi 20190908 光差保护
  50. fec2_exit, // sunxi 20190904 added
  51. #endif // __LIGHT_DIFF_ACT_PRO__
  52. // flash_exit,
  53. e2prom_exit,
  54. //关闭bsp中打开的中断
  55. // pit_exit,
  56. #ifdef BSP_CAN_ENABLE
  57. can_app_exit,
  58. can_exit,
  59. #endif
  60. // freq_exit,
  61. // adc_exit,
  62. gpio_exit,
  63. watchdog_exit,
  64. shm_exit,
  65. mb_exit,
  66. ustimer_exit,
  67. bsp_ustimer_exit,
  68. log_exit,
  69. ccu_exit,
  70. };
  71. #define BSP_EXIT_NUM (sizeof(g_bsp_exit_func)/sizeof(g_bsp_exit_func[0]))
  72. unsigned long g_cpu_clk;
  73. int get_clocks(void);
  74. int bsp_init(void)
  75. {
  76. int i,ret;
  77. //得到CPU主时钟频率
  78. get_clocks();
  79. for(i=0;i<BSP_INIT_NUM;i++)
  80. {
  81. ret = g_bsp_init_func[i].func();
  82. if(ret != 0)
  83. {
  84. rt_err_set(g_bsp_init_func[i].err_code,0);
  85. rt_printf("bsp_init err(i=%d,ret=%d)\r\n",i,ret);
  86. }
  87. }
  88. return 0;
  89. }
  90. int bsp_exit(void)
  91. {
  92. int i,ret;
  93. for(i=0;i<(int)(BSP_EXIT_NUM);i++)
  94. {
  95. ret = g_bsp_exit_func[i]();
  96. if(ret != 0)
  97. {
  98. rt_printf("bsp_exit err(i=%d,ret=%d)\r\n",i,ret);
  99. }
  100. }
  101. return 0;
  102. }
  103. #define MCF_CCM_CCR_PLLMULT_MASK 0X03
  104. #define MCF_CLOCK_PCR_OUTDIV_MASK 0x1F
  105. #define CCM_MISCCR2_FBHALF 0x02
  106. #define PLL_CR_FBKDIV_BITS 0x3f
  107. #define MCF_CLOCK_PCR (*(unsigned int*)(0xFC0C0000))
  108. #define MCF_CLOCK_PDR (*(unsigned int*)(0xFC0C0004))
  109. int get_clocks(void)
  110. {
  111. #if 0
  112. //int pllmult_nopci[] = {10, 15, 16, 20};
  113. unsigned long inp_clk,vco_clk,cpu_clk,bus_clk,flb_clk,ddr_clk,temp;
  114. inp_clk = CFG_EXT_OSC_CLK; // Input clock
  115. //vco_clk = pllmult_nopci[(MCF_CCM_CCR >> 6) & MCF_CCM_CCR_PLLMULT_MASK] * inp_clk;
  116. vco_clk = ((MCF_CLOCK_PCR & PLL_CR_FBKDIV_BITS) + 1) * inp_clk;
  117. temp = ((MCF_CLOCK_PDR >> 0) & MCF_CLOCK_PCR_OUTDIV_MASK) + 1;
  118. cpu_clk = vco_clk / temp; //cpu clock
  119. temp = ((MCF_CLOCK_PDR >> 5) & MCF_CLOCK_PCR_OUTDIV_MASK) + 1;
  120. flb_clk = bus_clk = vco_clk / temp; // flb & bus clock
  121. if(MCF_CCM_MISCCR2 & CCM_MISCCR2_FBHALF)
  122. {
  123. flb_clk >>= 1; // FlexBus clock half
  124. }
  125. ddr_clk = cpu_clk;
  126. if(!(MCF_CCM_MISCCR2 & MCF_CCM_MISCCR2_DDR2CLK))
  127. {
  128. ddr_clk >>= 1;
  129. }
  130. //初始化全局CPU主时钟频率
  131. g_cpu_clk = cpu_clk;
  132. #endif
  133. return 0;
  134. }
  135. int get_clocks_printf(void)
  136. {
  137. #if 0
  138. //int pllmult_nopci[] = {10, 15, 16, 20};
  139. unsigned long inp_clk,vco_clk,cpu_clk,bus_clk,flb_clk,ddr_clk,temp;
  140. inp_clk = CFG_EXT_OSC_CLK; // Input clock
  141. //vco_clk = pllmult_nopci[(MCF_CCM_CCR >> 6) & MCF_CCM_CCR_PLLMULT_MASK] * inp_clk;
  142. vco_clk = ((MCF_CLOCK_PCR & PLL_CR_FBKDIV_BITS) + 1) * inp_clk;
  143. temp = ((MCF_CLOCK_PDR >> 0) & MCF_CLOCK_PCR_OUTDIV_MASK) + 1;
  144. cpu_clk = vco_clk / temp; //cpu clock
  145. temp = ((MCF_CLOCK_PDR >> 5) & MCF_CLOCK_PCR_OUTDIV_MASK) + 1;
  146. flb_clk = bus_clk = vco_clk / temp; // flb & bus clock
  147. if(MCF_CCM_MISCCR2 & CCM_MISCCR2_FBHALF)
  148. {
  149. flb_clk >>= 1; // FlexBus clock half
  150. }
  151. ddr_clk = cpu_clk;
  152. if(!(MCF_CCM_MISCCR2 & MCF_CCM_MISCCR2_DDR2CLK))
  153. {
  154. ddr_clk >>= 1;
  155. }
  156. rt_printf("inp_clk=%ldMHZ,vco_clk=%ldMHZ,cpu_clk=%ldMHZ\r\n",inp_clk/1000000,vco_clk/1000000,cpu_clk/1000000);
  157. rt_printf("bus_clk=%ldMHZ,flb_clk=%ldMHZ,ddr_clk=%ldMHZ\r\n",bus_clk/1000000,flb_clk/1000000,ddr_clk/1000000);
  158. #endif
  159. return 0;
  160. }
  161. char * q16_to_str(long q16)
  162. {
  163. static char str[32];
  164. #if 1
  165. sprintf(str,"%f",(float)q16/Q16_BASE);
  166. #else
  167. str[0] = 0;
  168. if(q16<0)
  169. {
  170. q16 = -q16;
  171. sprintf(str,"-");
  172. }
  173. sprintf(str+strlen(str),"%d.%04d",q16>>16,(q16&0xffff)*10000/0x10000);
  174. #endif
  175. return str;
  176. }