soft_test.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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] = {0};
  169. void user_init(void)
  170. {
  171. //can pwm uart 初始化
  172. can_start_init();
  173. UART_Init();
  174. pwm_init();
  175. //PVD_Config();
  176. //上电读内存参数
  177. flash_read_funcktion();
  178. //上电亮绿灯
  179. HAL_GPIO_WritePin(GPIOA, LED3_Pin, GPIO_PIN_RESET);
  180. HAL_GPIO_WritePin(GPIOB, GPIO_PIN_14, GPIO_PIN_SET); //使能PWM
  181. //初始化缓存区
  182. rkfifo_init(&recv_rkfifo, recv_vk_protocol, sizeof(recv_vk_protocol), 1);
  183. rkfifo_init(&candebug_rkfifo, candebug_protocol, sizeof(candebug_protocol), 1);
  184. rkfifo_init(&send_uart_rkfifo, send_uart_buffer, sizeof(send_uart_buffer), 1);
  185. //注册PMU
  186. Int2String(serial.num,dev_pmu.sn,9);
  187. Int2String(APP_VERSION,dev_pmu.soft_serial,6);
  188. regist_dev_info(&dev_pmu,DEVICE_PMU,false,dev_pmu.sn,9,dev_pmu.soft_serial,6,NULL,0,"vk",3);
  189. }
  190. uint8_t isLeapYear(uint16_t year)
  191. {
  192. uint8_t res=0;
  193. if(year%4 == 0) // 能够被4整除
  194. {
  195. if(year%100 != 0 || year%400 == 0) //能被4整除不能被100整除或者能够被400整除
  196. {
  197. res = 1;
  198. }
  199. else
  200. {
  201. res =0;
  202. }
  203. }
  204. return res;
  205. }
  206. // 将Unix时间戳转换为北京时间
  207. // unixTime: 需要判断的Unix时间戳
  208. // *tempBeijing:返回的北京时间
  209. // return:none
  210. // note:没对输入参数正确性做判断
  211. rtc_time_t beijing_time;
  212. void covUnixTimeStp2Beijing( uint32_t unixTime, rtc_time_t *tempBeijing )
  213. {
  214. uint32_t totleDayNum = 0, totleSecNum = 0;
  215. uint16_t remainDayofYear = 0, tempYear = 0;
  216. uint8_t *pr = NULL;
  217. static uint8_t month_day[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; //平年
  218. static uint8_t Leap_month_day[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; //闰年
  219. totleDayNum = unixTime / ( 24 * 60 * 60 ); //总天数(注意加括号)
  220. totleSecNum = unixTime % ( 24 * 60 * 60 ); //当天剩余的秒速
  221. memset( tempBeijing, 0x00, sizeof( rtc_time_t ) );
  222. // 1.先计算时间 HH:MM:SS
  223. tempBeijing->ui8Hour = totleSecNum / 3600;
  224. tempBeijing->ui8Minute = ( totleSecNum % 3600 ) / 60; //error:变量搞错
  225. tempBeijing->ui8Second = ( totleSecNum % 3600 ) % 60;
  226. // 2.对时间进行时区调整(注意:这里可能造成日期 +1)
  227. tempBeijing->ui8Hour += TIMEZONE;
  228. if ( tempBeijing->ui8Hour > 23 )
  229. {
  230. //printf("modify day..\n");
  231. tempBeijing->ui8Hour -= 24;
  232. remainDayofYear++; // 日期+1
  233. }
  234. // 3.计算哪一年
  235. tempBeijing->ui8Year = 1970 + ( totleDayNum / FOURYEARDAY ) * 4; // 4年为一个周期
  236. remainDayofYear += totleDayNum % FOURYEARDAY;
  237. //printf("year:%d, day:%d.\n", tempBeijing->ui8Year, remainDayofYear);
  238. tempYear = isLeapYear( tempBeijing->ui8Year ) ? 366 : 365;
  239. while ( remainDayofYear >= tempYear ) // 计算4年整数倍外的年。
  240. {
  241. tempBeijing->ui8Year++;
  242. remainDayofYear -= tempYear;
  243. tempYear = isLeapYear( tempBeijing->ui8Year ) ? 366 : 365;
  244. }
  245. // 4.计算哪一月的哪一天
  246. pr = isLeapYear( tempBeijing->ui8Year ) ? Leap_month_day : month_day;
  247. remainDayofYear++; // 这里开始计算具体日期。remainDayofYear为 0 时其实是 1 号,所以这里要 +1
  248. while ( remainDayofYear > *( pr + tempBeijing->ui8Month ) )
  249. {
  250. remainDayofYear -= *( pr + tempBeijing->ui8Month );
  251. tempBeijing->ui8Month++;
  252. }
  253. //printf("year:%d, day:%d.\n", tempBeijing->ui8Year, remainDayofYear);
  254. tempBeijing->ui8Month++; //month
  255. tempBeijing->ui8DayOfMonth = remainDayofYear; //day
  256. //printf("year:%d, day:%d.\n", tempBeijing->ui8Year, tempBeijing->ui8DayOfMonth);
  257. }
  258. void PVD_IRQHandler(void)
  259. {
  260. HAL_PWR_PVD_IRQHandler();
  261. }
  262. void PVD_Config(void)
  263. {
  264. /*##-1- Enable Power Clock #################################################*/
  265. __HAL_RCC_PWR_CLK_ENABLE();
  266. /*##-2- Configure the NVIC for PVD #########################################*/
  267. HAL_NVIC_SetPriority(PVD_IRQn, 0, 0);
  268. HAL_NVIC_EnableIRQ(PVD_IRQn);
  269. PWR_PVDTypeDef sConfigPVD;
  270. sConfigPVD.PVDLevel = PWR_PVDLEVEL_3;
  271. sConfigPVD.Mode = PWR_PVD_MODE_IT_RISING;
  272. HAL_PWR_ConfigPVD(&sConfigPVD);
  273. /* Enable the PVD Output */
  274. HAL_PWR_EnablePVD();
  275. }
  276. void HAL_PWR_PVDCallback(void)
  277. {
  278. volinf._vol_flag = ( int16_t )0xA48A;
  279. //HAL_GPIO_WritePin(GPIOA, LED1_Pin, GPIO_PIN_RESET);
  280. volinf.cal_vol = 15;
  281. WriteFlashNBtye( FLASH_VOL_IFO, ( uint8_t * )&volinf, sizeof( volinf ) );
  282. }