bsp.c 5.1 KB

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