soft_test.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. #include "soft_test.h"
  2. #include "main.h"
  3. #include "tim.h"
  4. #include "soft_can.h"
  5. #include "soft_uart.h"
  6. #include "can.h"
  7. #include "usart.h"
  8. #include "soft_terrain.h"
  9. #include "soft_obstacle.h"
  10. #include "soft_p_2_c.h"
  11. #include "soft_flash.h"
  12. #include "soft_obstacle.h"
  13. #include "string.h"
  14. #include "can_debug.h"
  15. #include "soft_version.h"
  16. #include "config.h"
  17. /******************************************************************
  18. * ****************测试程序*****************************************
  19. * ****************************************************************/
  20. uint8_t UART3_send_buf[8] = {0, 1, 2, 3, 4, 5, 6, 7}; //DMA发送 不能用局部变量 可以使用static局部变量
  21. void uart_send_msg(void)
  22. {
  23. static uint32_t uart_time = 0;
  24. if (HAL_GetTick() - uart_time < 100)
  25. {
  26. return;
  27. }
  28. else
  29. {
  30. uart_time = HAL_GetTick();
  31. }
  32. uint32_t cur_time = HAL_GetTick();
  33. memcpy(&UART3_send_buf[0],&cur_time,4);
  34. //HAL_UART_Transmit(&huart3,&UART3_send_buf[i],1,0xFFFF);
  35. //HAL_UART_Transmit_DMA(&huart2, UART2_send_buf, 8); //DMA发送数据
  36. //HAL_UART_Transmit(&huart2,&UART2_send_buf[i],1,0xFFFF);
  37. /*
  38. while (huart3.gState == HAL_UART_STATE_READY)
  39. {
  40. assd++;
  41. }
  42. */
  43. HAL_UART_Transmit_DMA(&huart3, UART3_send_buf, 4);
  44. }
  45. void ttest_func(void)
  46. {
  47. /*
  48. if (HAL_GetTick() - led_time > 1000 && HAL_GetTick() > 10000)
  49. {
  50. HAL_GPIO_TogglePin(GPIOA,LED2_Pin);
  51. led_time = HAL_GetTick();
  52. }
  53. if ((HAL_GetTick() > 5000) && (a == 1))
  54. {
  55. __HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_1, 1300);
  56. __HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_2, 1700);
  57. __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_1, 1700);
  58. __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_2, 1700);
  59. a = 0;
  60. }
  61. */
  62. //5HZ 发送CAN
  63. //uart_send_msg();
  64. //HAL_GPIO_WritePin(GPIOA, LED1_Pin, SET); //蓝色
  65. //HAL_GPIO_WritePin(GPIOA, LED2_Pin, SET); //红色
  66. //HAL_GPIO_WritePin(GPIOA, LED3_Pin, SET); //绿色
  67. }
  68. uint32_t led_time_green = 0;
  69. uint32_t led_time_purple = 0;
  70. uint32_t led_time_blue = 0;
  71. uint32_t led_time_yellow = 0;
  72. bool led_green = false;
  73. bool led_purple = false;
  74. bool led_blue = false;
  75. bool led_yellow = false;
  76. bool only_star_one_green = false;
  77. bool only_star_one_purple = false;
  78. bool only_star_one_blue = false;
  79. bool only_star_one_yellow = false;
  80. bool no_led_star = false;
  81. /**
  82. * @file pmu_inside_led
  83. * @brief 板载LED灯
  84. * @param None
  85. * @details 绿:正常 --紫色:仿地 --蓝:前避 -- 黄:后避
  86. * @author Zhang Sir
  87. **/
  88. void pmu_inside_led()
  89. {
  90. if (led_green == false && led_purple == false && led_blue == false && led_green == false && led_yellow == false)
  91. {
  92. no_led_star = true;
  93. }
  94. else
  95. {
  96. no_led_star = false;
  97. }
  98. //正常闪绿灯 设备连接后依次闪其他颜色
  99. if ((HAL_GetTick() - led_time_green > 2500) && (no_led_star == true) &&
  100. (only_star_one_green == false))
  101. {
  102. HAL_GPIO_WritePin(GPIOA, LED3_Pin, GPIO_PIN_RESET);
  103. led_time_green = HAL_GetTick();
  104. led_green = true;
  105. only_star_one_green = true;
  106. only_star_one_purple = true;
  107. only_star_one_blue = true;
  108. only_star_one_yellow = true;
  109. }
  110. else if (terrain_is_link == true && no_led_star == true && only_star_one_purple == true)
  111. {
  112. HAL_GPIO_WritePin(GPIOA, LED2_Pin, GPIO_PIN_RESET);
  113. HAL_GPIO_WritePin(GPIOA, LED1_Pin, GPIO_PIN_RESET);
  114. led_time_purple = HAL_GetTick();
  115. led_purple = true;
  116. }
  117. else if (obs_f_is_link == true && no_led_star == true && only_star_one_blue == true)
  118. {
  119. HAL_GPIO_WritePin(GPIOA, LED1_Pin, GPIO_PIN_RESET);
  120. led_time_blue = HAL_GetTick();
  121. led_blue = true;
  122. }
  123. else if (obs_b_is_link == true && no_led_star == true && only_star_one_yellow == true)
  124. {
  125. HAL_GPIO_WritePin(GPIOA, LED3_Pin, GPIO_PIN_RESET);
  126. HAL_GPIO_WritePin(GPIOA, LED2_Pin, GPIO_PIN_RESET);
  127. led_time_yellow = HAL_GetTick();
  128. led_yellow = true;
  129. }
  130. //灭灯
  131. if ((HAL_GetTick() - led_time_green >= 500) && led_green == true)
  132. {
  133. HAL_GPIO_WritePin(GPIOA, LED3_Pin, GPIO_PIN_SET);
  134. led_green = false;
  135. only_star_one_green = false;
  136. }
  137. if ((HAL_GetTick() - led_time_purple >= 500) && led_purple == true)
  138. {
  139. HAL_GPIO_WritePin(GPIOA, LED2_Pin, GPIO_PIN_SET);
  140. HAL_GPIO_WritePin(GPIOA, LED1_Pin, GPIO_PIN_SET);
  141. led_purple = false;
  142. only_star_one_purple = false;
  143. }
  144. if ((HAL_GetTick() - led_time_blue >= 500) && led_blue == true)
  145. {
  146. HAL_GPIO_WritePin(GPIOA, LED1_Pin, GPIO_PIN_SET);
  147. led_blue = false;
  148. only_star_one_blue = false;
  149. }
  150. if ((HAL_GetTick() - led_time_yellow >= 500) && led_yellow == true)
  151. {
  152. HAL_GPIO_WritePin(GPIOA, LED2_Pin, GPIO_PIN_SET);
  153. HAL_GPIO_WritePin(GPIOA, LED3_Pin, GPIO_PIN_SET);
  154. led_yellow = false;
  155. only_star_one_yellow = false;
  156. }
  157. //ttest_func();
  158. }
  159. /**
  160. * @file user_init
  161. * @brief 初始化参数
  162. * @param None
  163. * @details 串口 can口 pwm rkfifo flash读取
  164. * @author Zhang Sir
  165. **/
  166. uint8_t recv_vk_protocol[MAX_UART_BUF] = {0};
  167. uint8_t candebug_protocol[MAX_UART_BUF] = {0};
  168. //uint8_t send_uart_buffer[ MAX_UART_BUF * 4 ] = {0};
  169. uint8_t send_uart_buffer[ MAX_UART_BUF * 4] = {0};
  170. void user_init(void)
  171. {
  172. //can pwm uart 初始化
  173. can_start_init();
  174. UART_Init();
  175. pwm_init();
  176. //PVD_Config();
  177. //上电读内存参数
  178. flash_read_funcktion();
  179. //上电亮绿灯
  180. HAL_GPIO_WritePin(GPIOA, LED3_Pin, GPIO_PIN_RESET);
  181. HAL_GPIO_WritePin(GPIOB, GPIO_PIN_14, GPIO_PIN_SET); //使能PWM
  182. //初始化缓存区
  183. rkfifo_init(&recv_rkfifo, recv_vk_protocol, sizeof(recv_vk_protocol), 1);
  184. rkfifo_init(&candebug_rkfifo, candebug_protocol, sizeof(candebug_protocol), 1);
  185. rkfifo_init(&send_uart_rkfifo, send_uart_buffer, sizeof(send_uart_buffer), 1);
  186. //注册PMU
  187. Int2String(serial.num,dev_pmu.sn,9);
  188. Int2String(APP_VERSION,dev_pmu.soft_serial,6);
  189. regist_dev_info(&dev_pmu,DEVICE_PMU,false,dev_pmu.sn,9,dev_pmu.soft_serial,6,NULL,0,"vk",3);
  190. }
  191. uint8_t isLeapYear(uint16_t year)
  192. {
  193. uint8_t res=0;
  194. if(year%4 == 0) // 能够被4整除
  195. {
  196. if(year%100 != 0 || year%400 == 0) //能被4整除不能被100整除或者能够被400整除
  197. {
  198. res = 1;
  199. }
  200. else
  201. {
  202. res =0;
  203. }
  204. }
  205. return res;
  206. }
  207. // 将Unix时间戳转换为北京时间
  208. // unixTime: 需要判断的Unix时间戳
  209. // *tempBeijing:返回的北京时间
  210. // return:none
  211. // note:没对输入参数正确性做判断
  212. rtc_time_t beijing_time;
  213. void covUnixTimeStp2Beijing( uint32_t unixTime, rtc_time_t *tempBeijing )
  214. {
  215. uint32_t totleDayNum = 0, totleSecNum = 0;
  216. uint16_t remainDayofYear = 0, tempYear = 0;
  217. uint8_t *pr = NULL;
  218. static uint8_t month_day[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; //平年
  219. static uint8_t Leap_month_day[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; //闰年
  220. totleDayNum = unixTime / ( 24 * 60 * 60 ); //总天数(注意加括号)
  221. totleSecNum = unixTime % ( 24 * 60 * 60 ); //当天剩余的秒速
  222. memset( tempBeijing, 0x00, sizeof( rtc_time_t ) );
  223. // 1.先计算时间 HH:MM:SS
  224. tempBeijing->ui8Hour = totleSecNum / 3600;
  225. tempBeijing->ui8Minute = ( totleSecNum % 3600 ) / 60; //error:变量搞错
  226. tempBeijing->ui8Second = ( totleSecNum % 3600 ) % 60;
  227. // 2.对时间进行时区调整(注意:这里可能造成日期 +1)
  228. tempBeijing->ui8Hour += TIMEZONE;
  229. if ( tempBeijing->ui8Hour > 23 )
  230. {
  231. //printf("modify day..\n");
  232. tempBeijing->ui8Hour -= 24;
  233. remainDayofYear++; // 日期+1
  234. }
  235. // 3.计算哪一年
  236. tempBeijing->ui8Year = 1970 + ( totleDayNum / FOURYEARDAY ) * 4; // 4年为一个周期
  237. remainDayofYear += totleDayNum % FOURYEARDAY;
  238. //printf("year:%d, day:%d.\n", tempBeijing->ui8Year, remainDayofYear);
  239. tempYear = isLeapYear( tempBeijing->ui8Year ) ? 366 : 365;
  240. while ( remainDayofYear >= tempYear ) // 计算4年整数倍外的年。
  241. {
  242. tempBeijing->ui8Year++;
  243. remainDayofYear -= tempYear;
  244. tempYear = isLeapYear( tempBeijing->ui8Year ) ? 366 : 365;
  245. }
  246. // 4.计算哪一月的哪一天
  247. pr = isLeapYear( tempBeijing->ui8Year ) ? Leap_month_day : month_day;
  248. remainDayofYear++; // 这里开始计算具体日期。remainDayofYear为 0 时其实是 1 号,所以这里要 +1
  249. while ( remainDayofYear > *( pr + tempBeijing->ui8Month ) )
  250. {
  251. remainDayofYear -= *( pr + tempBeijing->ui8Month );
  252. tempBeijing->ui8Month++;
  253. }
  254. //printf("year:%d, day:%d.\n", tempBeijing->ui8Year, remainDayofYear);
  255. tempBeijing->ui8Month++; //month
  256. tempBeijing->ui8DayOfMonth = remainDayofYear; //day
  257. //printf("year:%d, day:%d.\n", tempBeijing->ui8Year, tempBeijing->ui8DayOfMonth);
  258. }
  259. void PVD_IRQHandler(void)
  260. {
  261. HAL_PWR_PVD_IRQHandler();
  262. }
  263. void PVD_Config(void)
  264. {
  265. /*##-1- Enable Power Clock #################################################*/
  266. __HAL_RCC_PWR_CLK_ENABLE();
  267. /*##-2- Configure the NVIC for PVD #########################################*/
  268. HAL_NVIC_SetPriority(PVD_IRQn, 0, 0);
  269. HAL_NVIC_EnableIRQ(PVD_IRQn);
  270. PWR_PVDTypeDef sConfigPVD;
  271. sConfigPVD.PVDLevel = PWR_PVDLEVEL_3;
  272. sConfigPVD.Mode = PWR_PVD_MODE_IT_RISING;
  273. HAL_PWR_ConfigPVD(&sConfigPVD);
  274. /* Enable the PVD Output */
  275. HAL_PWR_EnablePVD();
  276. }
  277. void HAL_PWR_PVDCallback(void)
  278. {
  279. volinf._vol_flag = ( int16_t )0xA48A;
  280. //HAL_GPIO_WritePin(GPIOA, LED1_Pin, GPIO_PIN_RESET);
  281. volinf.cal_vol = 15;
  282. WriteFlashNBtye( FLASH_VOL_IFO, ( uint8_t * )&volinf, sizeof( volinf ) );
  283. }