Advertisement
Guest User

Untitled

a guest
Jan 21st, 2020
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.29 KB | None | 0 0
  1. /*
  2. * Communication.c
  3. *
  4. * Created on: Jan 14, 2020
  5. * Author: sigmoid
  6. */
  7. #include "main.h"
  8. #include "Communication.h"
  9. #include "Timer.h"
  10.  
  11. UART_HandleTypeDef huart3;
  12. UART_HandleTypeDef huart6;
  13.  
  14. uint8_t actionCheckGSM[] = "AT\r\n"; // return AT\r\r\nOK\r\n
  15. uint8_t actionCheckGSMReturn[] = "AT\r\r\nOK\r";
  16. uint8_t actionEchoDisable[] = "ATE0\r\n"; // Echo Disabled ; return ATE0\r\r\nOK\r\n
  17. uint8_t actionEchoEnabled[] = "ATE1\r"; // Echo Enabled
  18. //
  19. // If the response is "OK" then SMS text mode is supported
  20. // If the response is "ERROR", then SMS text mode is not supported.
  21. //
  22. uint8_t actionIsSmsModeSupported[] = "AT+CMGF=?\r";
  23. //
  24. // Used to instruct the GSM modul to operate in SMS text mode. The result code "OK" is returned , which indicates the command line "AT+CMGF=1" has been executed successfully.
  25. // If the result code "ERROR" is returned, it is likely that the GSM module does not support the SMS text mode
  26. //
  27. uint8_t actionSetSmsMode[] = "AT+CMGF=1\r\n";
  28. uint8_t actionSetPhoneNumber[] = "AT+CMGW=";
  29. uint8_t actionSendMessage[] = "AT+CMSS=";
  30. uint8_t actionListAllMessages[] = "AT+CMGL=\"ALL\"\r\n";
  31. uint8_t actionCheckAllPossibleBaudRates[] = "AT+IPR=?\r";
  32. uint8_t actionCurrentBaudRateOnGsm[] = "AT+IPR?\r\n";
  33. uint8_t actionSetGsmBaudRate[] = "AT+IPR=115200\r";
  34. uint8_t actionIsPinReady[] = "AT+CPIN?\r\n";
  35. uint8_t actionQSS[] = "AT#QSS?\r\n";
  36. uint8_t actionFwVersion[] = "AT+CGMR\r\n";
  37. uint8_t actionReadAllUnreadedMessages[] = "AT+CMGL=\"REC UNREAD\"";
  38. uint8_t actionReadMessageFromPosition[] = "AT+CMGR=3\r\n";
  39. uint8_t actionSignalStrength[] = "AT+CSQ\r\n";
  40. uint8_t actionGenericSimAccess[] = "AT+CSIM=?\r\n";
  41. uint8_t actionWhatTechnologyUseGsm[] = "AT+COPS?\r\n";
  42. uint8_t actionWhereIsGsmRegistred[] = "AT+CREG\r\n";
  43. uint8_t actionSmsSendEnd[] = "26"; // ctrl+z is 26 decimal
  44.  
  45. uint8_t gsmRxBuff[RX_BUFFER_SIZE];
  46. uint8_t checkGSMBuff[RX_BUFFER_SIZE];
  47. uint8_t echoDisableBuff[RX_BUFFER_SIZE];
  48. uint8_t setSmsModeBuff[RX_BUFFER_SIZE];
  49. uint8_t listAllMessagesBuff[RX_BUFFER_SIZE];
  50. uint8_t setPhoneNumberBuff[RX_BUFFER_SIZE];
  51. uint8_t checkAllPossibleBaudRatesBuff[RX_BUFFER_SIZE];
  52. uint8_t currentBaudRateOnGsmBuff[RX_BUFFER_SIZE];
  53. uint8_t setGsmBaudRateBuff[RX_BUFFER_SIZE];
  54. uint8_t isPinReadyBuff[RX_BUFFER_SIZE];
  55. uint8_t QSSBuff[RX_BUFFER_SIZE];
  56. uint8_t fwVersionBuff[RX_BUFFER_SIZE];
  57. uint8_t readAllUnreadedMessagesBuff[RX_BUFFER_SIZE];
  58. uint8_t readMessageFromPositionBuff[RX_BUFFER_SIZE];
  59. uint8_t signalStrengthBuff[RX_BUFFER_SIZE];
  60. uint8_t genericSimAccessBuff[RX_BUFFER_SIZE];
  61. uint8_t whatTechnologyUseGsmBuff[RX_BUFFER_SIZE];
  62. uint8_t whereIsGsmRegistred[RX_BUFFER_SIZE];
  63. uint8_t phoneNumber[20];
  64. uint8_t Sms_Text_Message[100];
  65. uint8_t SmsSendIndexBuff[RX_BUFFER_SIZE];
  66.  
  67. uint32_t gsmCollectMiliseconds = 0;
  68. bool gsmSendAgain = false;
  69. bool gsmCheckRxTime = false;
  70. bool flagRxFromGsmHalfCplt = false;
  71.  
  72. enum gsmMode SetGsmMode = Sms_Receive;
  73.  
  74. uint8_t Sms_Send_Index = 0;
  75.  
  76. uint8_t QSS_SET = 0;
  77.  
  78. uint32_t qqs_miliseconds = 0;
  79. bool qss_first_time = false;
  80.  
  81. volatile bool flagTxCmpltUsart3 = false;
  82. volatile bool flagTxCmpltUsart6 = true;
  83. //*********************************************************************************************//
  84. // GSM Functions //
  85. //*********************************************************************************************//
  86.  
  87. /**
  88. * @brief Check does received data(data which are received from GSM module) contain string "OK". If received string contain "OK" copy received string into buffer whose intention
  89. * is it to store information about what data(information) gsm send back to mcu after we send AT command. Use this function because gsm don't immediately send back information to
  90. * the mcu, so in this function we wait for response from gsm
  91. * @param firstBuffer buffer which contain information what gsm send back to mcu
  92. * secondBuffer buffer with received data(data from gsm)
  93. * secondStringLimit counter which indicate buffer limit
  94. * @retval True if in received string is "OK", otherwise false
  95. */
  96. bool Wait_For_Gsm_Response(uint8_t *firstBuffer, uint8_t *secondBuffer, uint8_t secondBufferLimit, uint8_t *atCommand, uint8_t *atCommandWhatDo)
  97. {
  98. int i = 0; // position in receive string
  99. bool flagComplete = false;
  100.  
  101. //
  102. // Collect information about time. Compere gsmCollectMiliseconds + some_time with current_time every passing through a function and
  103. // if gsmCollectMiliseconds + some_time is less then current time indicate that mcu need to send again AT command
  104. //
  105. if(!gsmCheckRxTime)
  106. {
  107. gsmCollectMiliseconds = Timer_Miliseconds();
  108. gsmCheckRxTime = true;
  109. }
  110. //
  111. // Go through the string(from first character to last one) and check is there some known word which is represent in the row character format
  112. //
  113. while(i < secondBufferLimit)
  114. {
  115. //if(SetGsmMode == Sms_Receive)
  116. //{
  117. if(QSS_SET == 1)
  118. {
  119. if(!qss_first_time)
  120. {
  121. qss_first_time = true;
  122. qqs_miliseconds = Timer_Miliseconds();
  123. }
  124. if(secondBuffer[i] == '2')
  125. {
  126. uint8_t IDEMO = 111222;
  127. }
  128. if(secondBuffer[i] == '3')
  129. {
  130. uint8_t IDEMO1 = 11123322;
  131. }
  132. }
  133. //
  134. // Does second string contain in a row two characters 'O' and 'K' = "OK"
  135. //
  136. else
  137. {
  138. if(secondBuffer[i] == 'O')
  139. {
  140. i++;
  141. if(secondBuffer[i] == 'K')
  142. {
  143. flagComplete = true;
  144. }
  145. }
  146. }
  147. //
  148. // Does second string contain in a row characters 'E', 'R', 'R', 'O', 'R', ':', ' ' = "ERROR: "
  149. //
  150. if(secondBuffer[i] == 'E')
  151. {
  152. i++;
  153. if(secondBuffer[i] == 'R')
  154. {
  155. i++;
  156. if(secondBuffer[i] == 'R')
  157. {
  158. i++;
  159. if(secondBuffer[i] == 'O')
  160. {
  161. i++;
  162. if(secondBuffer[i] == 'R')
  163. {
  164. i++;
  165. if(secondBuffer[i] == ':')
  166. {
  167. i++;
  168. if(secondBuffer[i] == ' ')
  169. {
  170.  
  171. }
  172. }
  173. }
  174. }
  175. }
  176. }
  177. }
  178. if(secondBuffer[i] == '9')
  179. {
  180. i++;
  181. if(secondBuffer[i] == '8')
  182. {
  183. i++;
  184. if(secondBuffer[i] == '7')
  185. {
  186. flagComplete = true;
  187. }
  188. }
  189. }
  190. /*
  191. if(secondBuffer[i] == 'S')
  192. {
  193. i++;
  194. if(secondBuffer[i] == 'I')
  195. {
  196. i++;
  197. if(secondBuffer[i] == 'M')
  198. {
  199. flagComplete = true;
  200. }
  201.  
  202. }
  203. }
  204. */
  205. if(secondBuffer[i] == 'Q')
  206. {
  207. i++;
  208. if(secondBuffer[i] == 'S')
  209. {
  210. i++;
  211. if(secondBuffer[i] == 'S')
  212. {
  213. i++;
  214. if(secondBuffer[i] == ':')
  215. {
  216. i++;
  217. if(secondBuffer[i] == ' ')
  218. {
  219. i++;
  220. if(secondBuffer[i] == '2')
  221. {
  222. i++;
  223. if(secondBuffer[i] == ',')
  224. {
  225. i++;
  226. if(secondBuffer[i] == '3')
  227. {
  228. flagComplete = true;
  229. }
  230. }
  231. }
  232. }
  233. }
  234. }
  235. }
  236. }
  237. //}
  238. /*else if(SetGsmMode == Sms_Send)
  239. {
  240.  
  241. if(secondBuffer[i] == 'O')
  242. {
  243. i++;
  244. if(secondBuffer[i] == 'K')
  245. {
  246. flagComplete = true;
  247. }
  248. }
  249. if(secondBuffer[i] == '>')
  250. {
  251. flagComplete = true;
  252. }
  253. if(secondBuffer[i] == 'C')
  254. {
  255. i++;
  256. if(secondBuffer[i] == 'M')
  257. {
  258. i++;
  259. if(secondBuffer[i] == 'G')
  260. {
  261.  
  262. i++;
  263. if(secondBuffer[i] == 'W')
  264. {
  265. i++;
  266. if(secondBuffer[i] == ':')
  267. {
  268. i++;
  269. if(secondBuffer[i] == ' ')
  270. {
  271. i++;
  272. Sms_Send_Index = secondBuffer[i];
  273. flagComplete = true;
  274. }
  275. }
  276. }
  277. }
  278. }
  279. }
  280. if(secondBuffer[i] == 'C')
  281. {
  282. i++;
  283. if(secondBuffer[i] == 'M')
  284. {
  285. i++;
  286. if(secondBuffer[i] == 'S')
  287. {
  288. i++;
  289. if(secondBuffer[i] == 'S')
  290. {
  291. i++;
  292. if(secondBuffer[i] == ':')
  293. {
  294. i++;
  295. if(secondBuffer[i] == ' ')
  296. {
  297. flagComplete = true;
  298. }
  299. }
  300. }
  301. }
  302. }
  303. }
  304. }*/
  305. i++;
  306. }
  307. //
  308. // Check is waiting for receive data from gsm too long, if conditions are fulfilled mcu need to send again AT command
  309. //
  310. if((gsmCollectMiliseconds + GSM_MAX_RX_TIME_IN_MILISECONDS < Timer_Miliseconds()) && gsmCheckRxTime)
  311. {
  312. gsmCheckRxTime = false;
  313. gsmSendAgain = true;
  314. }
  315. //
  316. // Check flag status and return appropriate logic flag
  317. //
  318.  
  319. //
  320. // Received string contain some words("OK",..) which indicate that AT command is successful send via uart, gsm accept that AT command
  321. //
  322. if(flagComplete)
  323. {
  324. // Copy_Data_From_Second_Buffer_To_First(firstBuffer, secondBuffer, secondBufferLimit);
  325. Reset_Buffer(secondBuffer, '~', RX_BUFFER_SIZE);
  326. Buffer_Alignment(firstBuffer, '~', RX_BUFFER_SIZE);
  327. Send_Data_To_Terminal_Via_Uart("\n\r------------------------------------------------------\n\r");
  328. Send_Data_To_Terminal_Via_Uart(atCommandWhatDo);
  329. Send_Data_To_Terminal_Via_Uart(" <----> ");
  330. Send_Data_To_Terminal_Via_Uart("Action: ");
  331. Send_Data_To_Terminal_Via_Uart(atCommand);
  332. Send_Data_To_Terminal_Via_Uart(firstBuffer);
  333. Send_Data_To_Terminal_Via_Uart("\n\r------------------------------------------------------\n");
  334. gsmCheckRxTime = false;
  335. gsmSendAgain = false;
  336. return true;
  337. }
  338. //
  339. // Time for receive data from gsm is too long, send again AT command
  340. //
  341. else if(gsmSendAgain)
  342. {
  343. return true;
  344. }
  345. //
  346. // Time for waiting for response isn't too long, received data don't contain some words("OK,..), go again though function
  347. //
  348. else
  349. {
  350. return false;
  351. }
  352. }
  353. /**
  354. * @brief Make delay if mcu successful received data from gsm, otherwise don't make delay because mcu don't successful receive data from gsm
  355. * and we want correct answer from gsm so need to send again AT command
  356. * @param delay choose delay in miliseconds
  357. * @retval None
  358. */
  359. void GSM_Delay(uint32_t delay)
  360. {
  361. if(!gsmSendAgain)
  362. {
  363. HAL_Delay(delay);
  364. }
  365. }
  366. /**
  367. * @brief Set elements of buffer to some special character, going from first element to the desired boundary
  368. * @param buffer buffer whose elements need to be changed
  369. * choseCharacter choose character in which need to be changed elements of buffer
  370. * bufferLimit desired limit of buffer
  371. * @retval None
  372. */
  373. void Reset_Buffer(uint8_t *buffer, char choseCharacter, uint8_t bufferLimit)
  374. {
  375. uint8_t counter = 0;
  376.  
  377. while(counter != bufferLimit)
  378. {
  379. buffer[counter] = choseCharacter;
  380. counter++;
  381. }
  382. }
  383.  
  384. void Copy_Data_From_Second_Buffer_To_First(uint8_t *firstBuffer, uint8_t *secondBuffer, uint8_t secondBufferLimit)
  385. {
  386. int i = secondBufferLimit - 1;
  387.  
  388. while(i != -1)
  389. {
  390. firstBuffer[i] = secondBuffer[i];
  391. i--;
  392. }
  393. }
  394.  
  395. void Buffer_Alignment(uint8_t *buffer, uint8_t chosenCharacter, uint8_t bufferLimit)
  396. {
  397. uint8_t counter = 0;
  398. uint8_t startPositionOfString = 0;
  399. uint8_t endPositionOfString = 0;
  400. bool flagStartText = false;
  401. bool chosenCharacterOnlyFromFrontSide = false;
  402. //
  403. // Find start text position and end text position in buffer
  404. //
  405. while(counter < bufferLimit)
  406. {
  407. if(buffer[counter] != chosenCharacter && !flagStartText)
  408. {
  409. /*if(buffer[counter] == '\0')
  410. {
  411. counter++;
  412. while(buffer[counter] == '\0')
  413. {
  414. counter++;
  415. }
  416. }*/
  417. startPositionOfString = counter;
  418. flagStartText = true; // indicate that this is start position of text
  419. }
  420.  
  421. if(buffer[counter] == chosenCharacter && flagStartText)
  422. {
  423. endPositionOfString = counter;
  424. counter = bufferLimit;
  425. }
  426.  
  427. counter++;
  428. }
  429. //
  430. // Buffer have characters chosenCharacter only from the front side | example: uint8_t test[] = "~~~~123123";
  431. //
  432. if(endPositionOfString == 0)
  433. {
  434. endPositionOfString = bufferLimit - 1;
  435. chosenCharacterOnlyFromFrontSide = true;
  436. }
  437.  
  438. counter = 0;
  439.  
  440. //
  441. // Buffer have characters (chosenCharacter) only from the front side | example: uint8_t test[] = "~~~~123123";
  442. //
  443. if(chosenCharacterOnlyFromFrontSide)
  444. {
  445. while(counter != bufferLimit)
  446. {
  447. buffer[counter] = buffer[startPositionOfString];
  448.  
  449. counter++;
  450. startPositionOfString++;
  451.  
  452. if(startPositionOfString > endPositionOfString)
  453. {
  454. while(counter != bufferLimit)
  455. {
  456. buffer[counter] = ' ';
  457. counter++;
  458. }
  459. }
  460. }
  461. }
  462. //
  463. // Buffer have characters (chosenCharacter) : 1. both from the front and back side | example: uint8_t test[] = "~~~~123123~~~~~~~~~";
  464. // : 2. from the back side | example: uint8_t test[] = "123123~~~~~~~~~~~~~";
  465. //
  466. else
  467. {
  468. while(counter != bufferLimit)
  469. {
  470. buffer[counter] = buffer[startPositionOfString];
  471.  
  472. counter++;
  473. startPositionOfString++;
  474.  
  475. if(startPositionOfString == endPositionOfString)
  476. {
  477. buffer[counter] = '\0';
  478. counter++;
  479. while(counter != bufferLimit)
  480. {
  481. buffer[counter] = ' ';
  482. counter++;
  483. }
  484. }
  485. }
  486. }
  487. }
  488.  
  489. //*********************************************************************************************//
  490. // UART Functions //
  491. //*********************************************************************************************//
  492.  
  493. void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
  494. {
  495. if(huart->Instance == USART3)
  496. {
  497. flagTxCmpltUsart3 = true;
  498. }
  499. if(huart->Instance == USART6)
  500. {
  501. flagTxCmpltUsart6 = true;
  502. }
  503. }
  504.  
  505. void Wait_Unit_Uart3_Tx_Is_Complete(void)
  506. {
  507. while(!flagTxCmpltUsart3){}
  508. }
  509.  
  510. void Reset_Uart3_Tx_Complete_Flag(void)
  511. {
  512. flagTxCmpltUsart3 = false;
  513. }
  514.  
  515. void Wait_Unit_Uart6_Tx_Is_Complete(void)
  516. {
  517. while(!flagTxCmpltUsart6){}
  518. }
  519.  
  520. void Reset_Uart6_Tx_Complete_Flag(void)
  521. {
  522. flagTxCmpltUsart6 = false;
  523. }
  524.  
  525. void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
  526. {
  527. if(huart->Instance == USART3)
  528. {
  529.  
  530. }
  531. if(huart->Instance == USART6)
  532. {
  533. GSM_GL865_Receive_Data(gsmRxBuff);
  534. }
  535. }
  536.  
  537. /**
  538. * @brief Send data to GSM GL865
  539. * @param TxData buffer with data which will be send to GSM GL865
  540. * @retval None
  541. */
  542.  
  543. void GSM_GL865_Send_Command(uint8_t *TxData)
  544. {
  545.  
  546.  
  547. if(HAL_UART_Transmit_IT(&huart6, TxData, strlen(TxData)) != HAL_OK)
  548. {
  549. HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_SET);
  550. Error_Handler();
  551. }
  552. Wait_Unit_Uart6_Tx_Is_Complete();
  553. Reset_Uart6_Tx_Complete_Flag();
  554. }
  555.  
  556. /**
  557. * @brief Receive data from GSM GL865
  558. * @param RxData buffer for storing incoming data from GSM GL865
  559. * @retval None
  560. */
  561. void GSM_GL865_Receive_Data(uint8_t *RxData)
  562. {
  563. if(HAL_UART_Receive_IT(&huart6, RxData, strlen(RxData)) != HAL_OK)
  564. {
  565. HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_SET);
  566. Error_Handler();
  567. }
  568. }
  569.  
  570. /**
  571. * @brief Send data to terminal
  572. * @param TxData buffer with data which will be send to terminal
  573. * @retval None
  574. */
  575. void Send_Data_To_Terminal_Via_Uart(uint8_t *TxData)
  576. {
  577. uint8_t n = strlen(TxData);
  578. if(HAL_UART_Transmit_IT(&huart3, TxData, strlen(TxData)) != HAL_OK)
  579. {
  580. while(1){}
  581. Error_Handler();
  582. }
  583. while(!flagTxCmpltUsart3){}
  584. flagTxCmpltUsart3 = false;
  585. }
  586. /**
  587. * @brief Receive data from terminal
  588. * @param TxData buffer for storing incoming data from terminal
  589. * @retval None
  590. */
  591. void Receive_Data_From_Terminal_Via_Uart(uint8_t *RxData)
  592. {
  593. if(HAL_UART_Receive_IT(&huart3, RxData, strlen(RxData)) != HAL_OK)
  594. {
  595. Error_Handler();
  596. }
  597. }
  598.  
  599. //*********************************************************************************************//
  600. // UART Initialization //
  601. //*********************************************************************************************//
  602.  
  603. /**
  604. * @brief USART3 Initialization Function
  605. * @param None
  606. * @retval None
  607. */
  608. void MX_USART3_UART_Init(void)
  609. {
  610. huart3.Instance = USART3;
  611. huart3.Init.BaudRate = 115200;
  612. huart3.Init.WordLength = UART_WORDLENGTH_8B;
  613. huart3.Init.StopBits = UART_STOPBITS_1;
  614. huart3.Init.Parity = UART_PARITY_NONE;
  615. huart3.Init.Mode = UART_MODE_TX_RX;
  616. huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  617. huart3.Init.OverSampling = UART_OVERSAMPLING_16;
  618. if (HAL_UART_Init(&huart3) != HAL_OK)
  619. {
  620. Error_Handler();
  621. }
  622. }
  623.  
  624. /**
  625. * @brief USART6 Initialization Function
  626. * @param None
  627. * @retval None
  628. */
  629. void MX_USART6_UART_Init(void)
  630. {
  631. huart6.Instance = USART6;
  632. huart6.Init.BaudRate = 9600;
  633. huart6.Init.WordLength = UART_WORDLENGTH_8B;
  634. huart6.Init.StopBits = UART_STOPBITS_1;
  635. huart6.Init.Parity = UART_PARITY_NONE;
  636. huart6.Init.Mode = UART_MODE_TX_RX;
  637. huart6.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  638. huart6.Init.OverSampling = UART_OVERSAMPLING_16;
  639. if (HAL_UART_Init(&huart6) != HAL_OK)
  640. {
  641. Error_Handler();
  642. }
  643. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement