bsp.c 5.5 KB

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