bsp.c 5.5 KB

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