bsp.c 6.2 KB

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