Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Communication.c
- *
- * Created on: Jan 14, 2020
- * Author: sigmoid
- */
- #include "main.h"
- #include "Communication.h"
- #include "Timer.h"
- UART_HandleTypeDef huart3;
- UART_HandleTypeDef huart6;
- uint8_t actionCheckGSM[] = "AT\r\n"; // return AT\r\r\nOK\r\n
- uint8_t actionCheckGSMReturn[] = "AT\r\r\nOK\r";
- uint8_t actionEchoDisable[] = "ATE0\r\n"; // Echo Disabled ; return ATE0\r\r\nOK\r\n
- uint8_t actionEchoEnabled[] = "ATE1\r"; // Echo Enabled
- //
- // If the response is "OK" then SMS text mode is supported
- // If the response is "ERROR", then SMS text mode is not supported.
- //
- uint8_t actionIsSmsModeSupported[] = "AT+CMGF=?\r";
- //
- // 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.
- // If the result code "ERROR" is returned, it is likely that the GSM module does not support the SMS text mode
- //
- uint8_t actionSetSmsMode[] = "AT+CMGF=1\r\n";
- uint8_t actionSetPhoneNumber[] = "AT+CMGW=";
- uint8_t actionSendMessage[] = "AT+CMSS=";
- uint8_t actionListAllMessages[] = "AT+CMGL=\"ALL\"\r\n";
- uint8_t actionCheckAllPossibleBaudRates[] = "AT+IPR=?\r";
- uint8_t actionCurrentBaudRateOnGsm[] = "AT+IPR?\r\n";
- uint8_t actionSetGsmBaudRate[] = "AT+IPR=115200\r";
- uint8_t actionIsPinReady[] = "AT+CPIN?\r\n";
- uint8_t actionQSS[] = "AT#QSS?\r\n";
- uint8_t actionFwVersion[] = "AT+CGMR\r\n";
- uint8_t actionReadAllUnreadedMessages[] = "AT+CMGL=\"REC UNREAD\"";
- uint8_t actionReadMessageFromPosition[] = "AT+CMGR=3\r\n";
- uint8_t actionSignalStrength[] = "AT+CSQ\r\n";
- uint8_t actionGenericSimAccess[] = "AT+CSIM=?\r\n";
- uint8_t actionWhatTechnologyUseGsm[] = "AT+COPS?\r\n";
- uint8_t actionWhereIsGsmRegistred[] = "AT+CREG\r\n";
- uint8_t actionSmsSendEnd[] = "26"; // ctrl+z is 26 decimal
- uint8_t gsmRxBuff[RX_BUFFER_SIZE];
- uint8_t checkGSMBuff[RX_BUFFER_SIZE];
- uint8_t echoDisableBuff[RX_BUFFER_SIZE];
- uint8_t setSmsModeBuff[RX_BUFFER_SIZE];
- uint8_t listAllMessagesBuff[RX_BUFFER_SIZE];
- uint8_t setPhoneNumberBuff[RX_BUFFER_SIZE];
- uint8_t checkAllPossibleBaudRatesBuff[RX_BUFFER_SIZE];
- uint8_t currentBaudRateOnGsmBuff[RX_BUFFER_SIZE];
- uint8_t setGsmBaudRateBuff[RX_BUFFER_SIZE];
- uint8_t isPinReadyBuff[RX_BUFFER_SIZE];
- uint8_t QSSBuff[RX_BUFFER_SIZE];
- uint8_t fwVersionBuff[RX_BUFFER_SIZE];
- uint8_t readAllUnreadedMessagesBuff[RX_BUFFER_SIZE];
- uint8_t readMessageFromPositionBuff[RX_BUFFER_SIZE];
- uint8_t signalStrengthBuff[RX_BUFFER_SIZE];
- uint8_t genericSimAccessBuff[RX_BUFFER_SIZE];
- uint8_t whatTechnologyUseGsmBuff[RX_BUFFER_SIZE];
- uint8_t whereIsGsmRegistred[RX_BUFFER_SIZE];
- uint8_t phoneNumber[20];
- uint8_t Sms_Text_Message[100];
- uint8_t SmsSendIndexBuff[RX_BUFFER_SIZE];
- uint32_t gsmCollectMiliseconds = 0;
- bool gsmSendAgain = false;
- bool gsmCheckRxTime = false;
- bool flagRxFromGsmHalfCplt = false;
- enum gsmMode SetGsmMode = Sms_Receive;
- uint8_t Sms_Send_Index = 0;
- uint8_t QSS_SET = 0;
- uint32_t qqs_miliseconds = 0;
- bool qss_first_time = false;
- volatile bool flagTxCmpltUsart3 = false;
- volatile bool flagTxCmpltUsart6 = true;
- //*********************************************************************************************//
- // GSM Functions //
- //*********************************************************************************************//
- /**
- * @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
- * 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
- * the mcu, so in this function we wait for response from gsm
- * @param firstBuffer buffer which contain information what gsm send back to mcu
- * secondBuffer buffer with received data(data from gsm)
- * secondStringLimit counter which indicate buffer limit
- * @retval True if in received string is "OK", otherwise false
- */
- bool Wait_For_Gsm_Response(uint8_t *firstBuffer, uint8_t *secondBuffer, uint8_t secondBufferLimit, uint8_t *atCommand, uint8_t *atCommandWhatDo)
- {
- int i = 0; // position in receive string
- bool flagComplete = false;
- //
- // Collect information about time. Compere gsmCollectMiliseconds + some_time with current_time every passing through a function and
- // if gsmCollectMiliseconds + some_time is less then current time indicate that mcu need to send again AT command
- //
- if(!gsmCheckRxTime)
- {
- gsmCollectMiliseconds = Timer_Miliseconds();
- gsmCheckRxTime = true;
- }
- //
- // 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
- //
- while(i < secondBufferLimit)
- {
- //if(SetGsmMode == Sms_Receive)
- //{
- if(QSS_SET == 1)
- {
- if(!qss_first_time)
- {
- qss_first_time = true;
- qqs_miliseconds = Timer_Miliseconds();
- }
- if(secondBuffer[i] == '2')
- {
- uint8_t IDEMO = 111222;
- }
- if(secondBuffer[i] == '3')
- {
- uint8_t IDEMO1 = 11123322;
- }
- }
- //
- // Does second string contain in a row two characters 'O' and 'K' = "OK"
- //
- else
- {
- if(secondBuffer[i] == 'O')
- {
- i++;
- if(secondBuffer[i] == 'K')
- {
- flagComplete = true;
- }
- }
- }
- //
- // Does second string contain in a row characters 'E', 'R', 'R', 'O', 'R', ':', ' ' = "ERROR: "
- //
- if(secondBuffer[i] == 'E')
- {
- i++;
- if(secondBuffer[i] == 'R')
- {
- i++;
- if(secondBuffer[i] == 'R')
- {
- i++;
- if(secondBuffer[i] == 'O')
- {
- i++;
- if(secondBuffer[i] == 'R')
- {
- i++;
- if(secondBuffer[i] == ':')
- {
- i++;
- if(secondBuffer[i] == ' ')
- {
- }
- }
- }
- }
- }
- }
- }
- if(secondBuffer[i] == '9')
- {
- i++;
- if(secondBuffer[i] == '8')
- {
- i++;
- if(secondBuffer[i] == '7')
- {
- flagComplete = true;
- }
- }
- }
- /*
- if(secondBuffer[i] == 'S')
- {
- i++;
- if(secondBuffer[i] == 'I')
- {
- i++;
- if(secondBuffer[i] == 'M')
- {
- flagComplete = true;
- }
- }
- }
- */
- if(secondBuffer[i] == 'Q')
- {
- i++;
- if(secondBuffer[i] == 'S')
- {
- i++;
- if(secondBuffer[i] == 'S')
- {
- i++;
- if(secondBuffer[i] == ':')
- {
- i++;
- if(secondBuffer[i] == ' ')
- {
- i++;
- if(secondBuffer[i] == '2')
- {
- i++;
- if(secondBuffer[i] == ',')
- {
- i++;
- if(secondBuffer[i] == '3')
- {
- flagComplete = true;
- }
- }
- }
- }
- }
- }
- }
- }
- //}
- /*else if(SetGsmMode == Sms_Send)
- {
- if(secondBuffer[i] == 'O')
- {
- i++;
- if(secondBuffer[i] == 'K')
- {
- flagComplete = true;
- }
- }
- if(secondBuffer[i] == '>')
- {
- flagComplete = true;
- }
- if(secondBuffer[i] == 'C')
- {
- i++;
- if(secondBuffer[i] == 'M')
- {
- i++;
- if(secondBuffer[i] == 'G')
- {
- i++;
- if(secondBuffer[i] == 'W')
- {
- i++;
- if(secondBuffer[i] == ':')
- {
- i++;
- if(secondBuffer[i] == ' ')
- {
- i++;
- Sms_Send_Index = secondBuffer[i];
- flagComplete = true;
- }
- }
- }
- }
- }
- }
- if(secondBuffer[i] == 'C')
- {
- i++;
- if(secondBuffer[i] == 'M')
- {
- i++;
- if(secondBuffer[i] == 'S')
- {
- i++;
- if(secondBuffer[i] == 'S')
- {
- i++;
- if(secondBuffer[i] == ':')
- {
- i++;
- if(secondBuffer[i] == ' ')
- {
- flagComplete = true;
- }
- }
- }
- }
- }
- }
- }*/
- i++;
- }
- //
- // Check is waiting for receive data from gsm too long, if conditions are fulfilled mcu need to send again AT command
- //
- if((gsmCollectMiliseconds + GSM_MAX_RX_TIME_IN_MILISECONDS < Timer_Miliseconds()) && gsmCheckRxTime)
- {
- gsmCheckRxTime = false;
- gsmSendAgain = true;
- }
- //
- // Check flag status and return appropriate logic flag
- //
- //
- // Received string contain some words("OK",..) which indicate that AT command is successful send via uart, gsm accept that AT command
- //
- if(flagComplete)
- {
- // Copy_Data_From_Second_Buffer_To_First(firstBuffer, secondBuffer, secondBufferLimit);
- Reset_Buffer(secondBuffer, '~', RX_BUFFER_SIZE);
- Buffer_Alignment(firstBuffer, '~', RX_BUFFER_SIZE);
- Send_Data_To_Terminal_Via_Uart("\n\r------------------------------------------------------\n\r");
- Send_Data_To_Terminal_Via_Uart(atCommandWhatDo);
- Send_Data_To_Terminal_Via_Uart(" <----> ");
- Send_Data_To_Terminal_Via_Uart("Action: ");
- Send_Data_To_Terminal_Via_Uart(atCommand);
- Send_Data_To_Terminal_Via_Uart(firstBuffer);
- Send_Data_To_Terminal_Via_Uart("\n\r------------------------------------------------------\n");
- gsmCheckRxTime = false;
- gsmSendAgain = false;
- return true;
- }
- //
- // Time for receive data from gsm is too long, send again AT command
- //
- else if(gsmSendAgain)
- {
- return true;
- }
- //
- // Time for waiting for response isn't too long, received data don't contain some words("OK,..), go again though function
- //
- else
- {
- return false;
- }
- }
- /**
- * @brief Make delay if mcu successful received data from gsm, otherwise don't make delay because mcu don't successful receive data from gsm
- * and we want correct answer from gsm so need to send again AT command
- * @param delay choose delay in miliseconds
- * @retval None
- */
- void GSM_Delay(uint32_t delay)
- {
- if(!gsmSendAgain)
- {
- HAL_Delay(delay);
- }
- }
- /**
- * @brief Set elements of buffer to some special character, going from first element to the desired boundary
- * @param buffer buffer whose elements need to be changed
- * choseCharacter choose character in which need to be changed elements of buffer
- * bufferLimit desired limit of buffer
- * @retval None
- */
- void Reset_Buffer(uint8_t *buffer, char choseCharacter, uint8_t bufferLimit)
- {
- uint8_t counter = 0;
- while(counter != bufferLimit)
- {
- buffer[counter] = choseCharacter;
- counter++;
- }
- }
- void Copy_Data_From_Second_Buffer_To_First(uint8_t *firstBuffer, uint8_t *secondBuffer, uint8_t secondBufferLimit)
- {
- int i = secondBufferLimit - 1;
- while(i != -1)
- {
- firstBuffer[i] = secondBuffer[i];
- i--;
- }
- }
- void Buffer_Alignment(uint8_t *buffer, uint8_t chosenCharacter, uint8_t bufferLimit)
- {
- uint8_t counter = 0;
- uint8_t startPositionOfString = 0;
- uint8_t endPositionOfString = 0;
- bool flagStartText = false;
- bool chosenCharacterOnlyFromFrontSide = false;
- //
- // Find start text position and end text position in buffer
- //
- while(counter < bufferLimit)
- {
- if(buffer[counter] != chosenCharacter && !flagStartText)
- {
- /*if(buffer[counter] == '\0')
- {
- counter++;
- while(buffer[counter] == '\0')
- {
- counter++;
- }
- }*/
- startPositionOfString = counter;
- flagStartText = true; // indicate that this is start position of text
- }
- if(buffer[counter] == chosenCharacter && flagStartText)
- {
- endPositionOfString = counter;
- counter = bufferLimit;
- }
- counter++;
- }
- //
- // Buffer have characters chosenCharacter only from the front side | example: uint8_t test[] = "~~~~123123";
- //
- if(endPositionOfString == 0)
- {
- endPositionOfString = bufferLimit - 1;
- chosenCharacterOnlyFromFrontSide = true;
- }
- counter = 0;
- //
- // Buffer have characters (chosenCharacter) only from the front side | example: uint8_t test[] = "~~~~123123";
- //
- if(chosenCharacterOnlyFromFrontSide)
- {
- while(counter != bufferLimit)
- {
- buffer[counter] = buffer[startPositionOfString];
- counter++;
- startPositionOfString++;
- if(startPositionOfString > endPositionOfString)
- {
- while(counter != bufferLimit)
- {
- buffer[counter] = ' ';
- counter++;
- }
- }
- }
- }
- //
- // Buffer have characters (chosenCharacter) : 1. both from the front and back side | example: uint8_t test[] = "~~~~123123~~~~~~~~~";
- // : 2. from the back side | example: uint8_t test[] = "123123~~~~~~~~~~~~~";
- //
- else
- {
- while(counter != bufferLimit)
- {
- buffer[counter] = buffer[startPositionOfString];
- counter++;
- startPositionOfString++;
- if(startPositionOfString == endPositionOfString)
- {
- buffer[counter] = '\0';
- counter++;
- while(counter != bufferLimit)
- {
- buffer[counter] = ' ';
- counter++;
- }
- }
- }
- }
- }
- //*********************************************************************************************//
- // UART Functions //
- //*********************************************************************************************//
- void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
- {
- if(huart->Instance == USART3)
- {
- flagTxCmpltUsart3 = true;
- }
- if(huart->Instance == USART6)
- {
- flagTxCmpltUsart6 = true;
- }
- }
- void Wait_Unit_Uart3_Tx_Is_Complete(void)
- {
- while(!flagTxCmpltUsart3){}
- }
- void Reset_Uart3_Tx_Complete_Flag(void)
- {
- flagTxCmpltUsart3 = false;
- }
- void Wait_Unit_Uart6_Tx_Is_Complete(void)
- {
- while(!flagTxCmpltUsart6){}
- }
- void Reset_Uart6_Tx_Complete_Flag(void)
- {
- flagTxCmpltUsart6 = false;
- }
- void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
- {
- if(huart->Instance == USART3)
- {
- }
- if(huart->Instance == USART6)
- {
- GSM_GL865_Receive_Data(gsmRxBuff);
- }
- }
- /**
- * @brief Send data to GSM GL865
- * @param TxData buffer with data which will be send to GSM GL865
- * @retval None
- */
- void GSM_GL865_Send_Command(uint8_t *TxData)
- {
- if(HAL_UART_Transmit_IT(&huart6, TxData, strlen(TxData)) != HAL_OK)
- {
- HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_SET);
- Error_Handler();
- }
- Wait_Unit_Uart6_Tx_Is_Complete();
- Reset_Uart6_Tx_Complete_Flag();
- }
- /**
- * @brief Receive data from GSM GL865
- * @param RxData buffer for storing incoming data from GSM GL865
- * @retval None
- */
- void GSM_GL865_Receive_Data(uint8_t *RxData)
- {
- if(HAL_UART_Receive_IT(&huart6, RxData, strlen(RxData)) != HAL_OK)
- {
- HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_SET);
- Error_Handler();
- }
- }
- /**
- * @brief Send data to terminal
- * @param TxData buffer with data which will be send to terminal
- * @retval None
- */
- void Send_Data_To_Terminal_Via_Uart(uint8_t *TxData)
- {
- uint8_t n = strlen(TxData);
- if(HAL_UART_Transmit_IT(&huart3, TxData, strlen(TxData)) != HAL_OK)
- {
- while(1){}
- Error_Handler();
- }
- while(!flagTxCmpltUsart3){}
- flagTxCmpltUsart3 = false;
- }
- /**
- * @brief Receive data from terminal
- * @param TxData buffer for storing incoming data from terminal
- * @retval None
- */
- void Receive_Data_From_Terminal_Via_Uart(uint8_t *RxData)
- {
- if(HAL_UART_Receive_IT(&huart3, RxData, strlen(RxData)) != HAL_OK)
- {
- Error_Handler();
- }
- }
- //*********************************************************************************************//
- // UART Initialization //
- //*********************************************************************************************//
- /**
- * @brief USART3 Initialization Function
- * @param None
- * @retval None
- */
- void MX_USART3_UART_Init(void)
- {
- huart3.Instance = USART3;
- huart3.Init.BaudRate = 115200;
- huart3.Init.WordLength = UART_WORDLENGTH_8B;
- huart3.Init.StopBits = UART_STOPBITS_1;
- huart3.Init.Parity = UART_PARITY_NONE;
- huart3.Init.Mode = UART_MODE_TX_RX;
- huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
- huart3.Init.OverSampling = UART_OVERSAMPLING_16;
- if (HAL_UART_Init(&huart3) != HAL_OK)
- {
- Error_Handler();
- }
- }
- /**
- * @brief USART6 Initialization Function
- * @param None
- * @retval None
- */
- void MX_USART6_UART_Init(void)
- {
- huart6.Instance = USART6;
- huart6.Init.BaudRate = 9600;
- huart6.Init.WordLength = UART_WORDLENGTH_8B;
- huart6.Init.StopBits = UART_STOPBITS_1;
- huart6.Init.Parity = UART_PARITY_NONE;
- huart6.Init.Mode = UART_MODE_TX_RX;
- huart6.Init.HwFlowCtl = UART_HWCONTROL_NONE;
- huart6.Init.OverSampling = UART_OVERSAMPLING_16;
- if (HAL_UART_Init(&huart6) != HAL_OK)
- {
- Error_Handler();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement