Surasak Nasuriwong / RMS_UART
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers RMS_UART.cpp Source File

RMS_UART.cpp

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    RMS_UART.cpp
00004   * @author  Surasak N
00005   * @version V2
00006   * @date    20/09/2016
00007   * @brief   Command for Mini-RMS <> RMS via UART
00008   ******************************************************************************/
00009   
00010 #include "mbed.h"
00011 #include "main.h"
00012 
00013 #ifdef EXT_SERIAL
00014     /* Using UART1 as in Debuger (ST-Link) */
00015     Serial serial_device(SERIAL_TX, SERIAL_RX);
00016 #else
00017     /* Using UART2 via external UART to USB */
00018     Serial serial_device(PA_3, PB_3);
00019 #endif
00020 
00021 char Msg_RxBuf[MSG_BUF_SIZE + 1];               // Command Buffer 
00022 int  Msg_index;                                 // Command index
00023 
00024 char Content_RxBuf[CONTENT_BUF_SIZE + 1];       // Contents Buffer 
00025 int  Content_index;                             // Contents index
00026 
00027 char FileName[MSG2FILENAME];                    // Command Buffer 
00028 FileRequest_CMD_Type reqFileCmdType = REQ_NONE; // An Events Request and management
00029 bool MsgContentManagement = false;              // Command and Contents buffer management
00030 bool SystemStandBy = false;                     // Mini-RMS system standby flag
00031 bool OverBufSize   = false;                     // Content buffer size overload
00032 
00033 /**
00034  * @brief   Initials bsp and interrupt of serial communication
00035  * @note    BPS have 9600bps only 
00036  * @retval 
00037  */
00038 void Init_SerialDevice()
00039 {
00040     /* Initial begining bps with 9600bps */
00041     serial_device.baud(BPS_9600);
00042     
00043     /* Initial interrupt callback function */
00044     serial_device.attach(&RxMsgInterruptCallback);
00045 }
00046 
00047 /**
00048  * @brief   Intertupt callback function
00049  * @note    Management fill buffer direction
00050  * @retval 
00051  */
00052 void RxMsgInterruptCallback()
00053 {
00054     /* Received serial Rx interrupt */
00055     
00056     /* Check buffer direction */
00057     if(MsgContentManagement == false)
00058     {
00059         /* Fill buffer to commands */
00060         DoTheMsgCmd();
00061     }
00062     else
00063     {
00064         /* Fill buffer to contents */
00065         DoTheContent();
00066     }
00067 }
00068 
00069 /**
00070  * @brief   Fill serial data as in contents buffer
00071  * @note    
00072  * @retval
00073  */
00074 void DoTheContent()
00075 {   
00076     // Note: you need to actually read from the serial to clear the RX interrupt
00077   
00078     /* Fill data as serial to contents buffer */
00079     Content_RxBuf[Content_index] = serial_device.getc();
00080         
00081     /* Shift contents index */
00082     Content_index++;
00083     
00084     /* Check end of content */
00085     CheckContentMsg();
00086 
00087 }
00088 
00089  /**
00090  * @brief   Fill serial data as in command buffer
00091  * @note    Command type and FileName
00092  * @retval
00093  */
00094 void DoTheMsgCmd()
00095 {
00096         
00097     // Note: you need to actually read from the serial to clear the RX interrupt
00098     /* Fill data as serial to command and file name buffer */
00099     Msg_RxBuf[Msg_index] = serial_device.getc();
00100     
00101     /* Shift command and filename index */
00102     Msg_index++;
00103     
00104     /* Check command type */
00105     CheckCmdType();
00106 }
00107 
00108 /**
00109  * @brief   Checking and processing contents data
00110  * @note    Check command *end* and clear buffer
00111  * @retval
00112  */
00113 void CheckContentMsg()
00114 {
00115     
00116     /* Found the *end* */
00117     if(strstr(Content_RxBuf,CMD_END_OF_CONTENT))
00118     {
00119         /* Call function for check contents */
00120         SendContentToFile();
00121     }
00122     
00123     /* Check content over buffer size */
00124     if(Content_index == CONTENT_BUF_SIZE)
00125     {
00126         /* Clear content buffer and index */
00127         ClearContentIndexAndBuf();
00128         
00129         /* Set content buffer overload flag */
00130         OverBufSize = true;
00131         
00132     }
00133 }
00134 
00135  /**
00136  * @brief   Checking and processing command type
00137  * @note    $ls, $ed, $rd, $rs and $df and etc.
00138  * @retval
00139  */
00140 void CheckCmdType()
00141 {   
00142     // Note : Procese the command type and file name
00143     
00144     /* Command type is list file ($ls) */
00145     if(strstr(Msg_RxBuf,CMD_LISTFILE))
00146     {        
00147         /* Set REQ_LISTDIR  event's reqFileCmdType */
00148         reqFileCmdType = REQ_FLAG_OK;
00149         
00150         /* Set flag evetnt */
00151         /* Waiting! main program call event */
00152         UART_File_event = true;
00153         
00154     }
00155     /* RMS sent "LK" acknowledge */
00156     else if(strstr(Msg_RxBuf,RMS_STATUS_LIST_OK))
00157     {
00158         /* Set REQ_LISTDIR  event's reqFileCmdType */
00159         reqFileCmdType = REQ_LISTDIR;
00160         
00161         /* Set flag evetnt */
00162         /* Waiting! main program call event */
00163         UART_File_event = true;
00164         
00165         /* Clear command index and buffer */
00166         ClearCmdIndexAndBuf();
00167         
00168     }
00169     /* RMS sent "RK" acknowledge */
00170     else if(strstr(Msg_RxBuf,RMS_STATUS_READ_OK))
00171     {
00172         /* Categorization command type */
00173         CheckReadEditCmd();
00174     }
00175     /* Got the *.csv file name */
00176     else if(strstr(Msg_RxBuf,FOUND_CSV_FILE))
00177     {
00178         /* Categorization ommand type */
00179         CheckReadEditCmd();
00180     }
00181     /* Got the *.xml file name */  
00182     else if(strstr(Msg_RxBuf,FOUND_XML_FILE))
00183     {
00184         /* Categorization command type */
00185         CheckReadEditCmd();
00186     }
00187     /* Got the *.log file name */
00188     else if(strstr(Msg_RxBuf,FOUND_LOG_FILE))
00189     {
00190         /* Categorization command type */
00191         CheckReadEditCmd();
00192     }
00193     /* Command type is system restart ($rs) */
00194     else if(strstr(Msg_RxBuf,CMD_SYS_RESTART))
00195     {
00196         /* Set SystemStandBy to not active */
00197         SystemStandBy = false;
00198         
00199         /* Request system restart */
00200         NVIC_SystemReset();
00201     }
00202     /* Command type is help ($-h) */
00203     else if(strstr(Msg_RxBuf,CMD_HELP))
00204     {
00205         /* List all command */
00206         GetHelpCmd();
00207     }
00208     /* Command type is stop ($sp) */
00209     else if(strstr(Msg_RxBuf,CMD_SYS_PAUSE))
00210     {
00211         /* Set SystemStandBy to active */
00212         SystemStandBy = true;
00213     }
00214     /* Command may be mismatched type */
00215     else
00216     {
00217         /* Not receive true type command */
00218         if(Msg_index >= MSG2CMD)
00219         {
00220             if(!((strstr(Msg_RxBuf,RMS_STATUS_LIST_OK))||
00221                  (strstr(Msg_RxBuf,RMS_STATUS_READ_OK))||
00222                  (strstr(Msg_RxBuf,CMD_LISTFILE))      || 
00223                  (strstr(Msg_RxBuf,CMD_READFILE))      || 
00224                  (strstr(Msg_RxBuf,CMD_WRITEFILE)      ||
00225                  (strstr(Msg_RxBuf,CMD_HELP))          || 
00226                  (strstr(Msg_RxBuf,CMD_SYS_RESTART))   ||
00227                  (strstr(Msg_RxBuf,CMD_SYS_PAUSE))     || 
00228                  (strstr(Msg_RxBuf,CMD_DELETEFILE)))))
00229             {
00230                 /* Call command error function */ 
00231                 GetCmdError();
00232             }
00233         }
00234     }
00235 
00236     /* Command and file name buffer are full */
00237     if(Msg_index == MSG_BUF_SIZE)
00238     {
00239        /* Clear command index and buffer */
00240        ClearCmdIndexAndBuf();
00241     }
00242 }
00243 
00244 /**
00245  * @brief   Categorization commands type
00246  * @note    Received file name and type
00247  * @retval
00248  */
00249 void CheckReadEditCmd()
00250 {
00251     /* Command type is read file ($rd) */
00252     if(strstr(Msg_RxBuf,CMD_READFILE))
00253     {   
00254     
00255         /* Check file name */
00256         GetFileName();
00257 
00258         /* Set REQ_READ  event's reqFileCmdType */
00259         reqFileCmdType = REQ_READ_CHECK;
00260         
00261         /* Set flag evetnt */
00262         /* Waiting! main program call event */
00263         UART_File_event = true;
00264     }
00265     else if(strstr(Msg_RxBuf,RMS_STATUS_READ_OK))
00266     {        
00267         /* Set REQ_READ  event's reqFileCmdType */
00268         reqFileCmdType = REQ_READ;
00269         
00270         /* Set flag evetnt */
00271         /* Waiting! main program call event */
00272         UART_File_event = true;
00273     }
00274     
00275     /* Command type is write/create file ($ed) */
00276     else if(strstr(Msg_RxBuf,CMD_WRITEFILE))
00277     {
00278         /* Set REQ_READ  event's reqFileCmdType */
00279         reqFileCmdType = REQ_FLAG_OK;
00280         
00281         /* Set flag evetnt */
00282         /* Waiting! main program call event */
00283         UART_File_event = true;
00284         
00285         /* Check file name */
00286         GetFileName();
00287         
00288         /* Set event flag for fill contents buffer */
00289         MsgContentManagement = true; 
00290 
00291     }
00292     /* Command type is delete ($df) */
00293     else if(strstr(Msg_RxBuf,CMD_DELETEFILE))
00294     {
00295         /* Check file name */
00296         GetFileName();
00297         
00298         /* Set REQ_DELETE  event's reqFileCmdType */
00299         reqFileCmdType = REQ_DELETE;
00300         
00301         /* Set flag evetnt */
00302         /* Waiting! main program call event */
00303         UART_File_event = true;
00304     }
00305     else
00306     {
00307         /* Call command error function */ 
00308         GetCmdError();
00309     }
00310 }
00311 
00312 /**
00313  * @brief   Received command error 
00314  * @note    $??
00315  * @retval
00316  */
00317 void GetCmdError()
00318 {   
00319     /* Send error status "ER" */
00320     serial_device.printf(EC_CMD_ERROR);
00321     
00322     /* Clear command index and buffer */
00323     ClearCmdIndexAndBuf();
00324 }
00325 
00326 /**
00327  * @brief   FileName process
00328  * @note    Fill file name as buffer
00329  * @retval
00330  */
00331 void GetFileName()
00332 {   
00333     /* Copy file name from Msg to FileName */
00334     memcpy(FileName,&Msg_RxBuf[3],Msg_index - 3);
00335     
00336     /* Clear command index and buffer */
00337     ClearCmdIndexAndBuf();
00338 }
00339 
00340 /**
00341  * @brief   Request sent contents to main 
00342  * @note    
00343  * @retval
00344  */
00345 void SendContentToFile()
00346 {   
00347     /* Set fill buffer to command */
00348     MsgContentManagement = true;
00349     
00350     /* Set REQ_WRITE  event's reqFileCmdType */
00351     reqFileCmdType = REQ_WRITE;
00352     
00353     /* Set flag evetnt */
00354     /* Waiting! main program call event */
00355     UART_File_event = true;
00356     
00357 }
00358 
00359 /**
00360  * @brief   Command help
00361  * @note    $-h
00362  * @retval
00363  */
00364 void GetHelpCmd()
00365 {   
00366     /* List all commands*/
00367     serial_device.printf("\r\n$ls : List fils as in directory");
00368     serial_device.printf("\r\n$rd [File Name] : Read fils as file name");
00369     serial_device.printf("\r\n$df [File Name] : Delete fils as file name");
00370     serial_device.printf("\r\n$ed [File Name] [Content] : Write/Create fils as in directory with content");
00371     serial_device.printf("\r\n\tNote : Content must contain EOF signature (*end*) to indicate end of file");
00372     serial_device.printf("\r\n\te.g. ed TestFile.xml test content *end*");
00373     serial_device.printf("\r\n$rs : Mini-RMS system restart");
00374     serial_device.printf("\r\n$sp : Mini-RMS system stop");
00375     serial_device.printf("\r\n\tNote : Need $rs for resumming Mini-RMS system");
00376     
00377     /* Clear buffer */
00378     ClearCmdIndexAndBuf();
00379 }
00380 /**
00381  * @brief   Clear index and buffer of command and file name
00382  * @note    
00383  * @retval
00384  */
00385 void ClearCmdIndexAndBuf()
00386 {   
00387     /* Clear counter */
00388     Msg_index = 0;
00389     
00390     /* Clear buffer */
00391     memset(Msg_RxBuf,_SPACE,MSG_BUF_SIZE);
00392 }
00393 
00394 /**
00395  * @brief   Clear index and buffer of contents 
00396  * @note 
00397  * @retval
00398  */
00399 void ClearContentIndexAndBuf()
00400 {   
00401     /* Clear counter */
00402     Content_index = 0;
00403     
00404     /* Clear buffer */
00405     memset(Content_RxBuf,_SPACE,CONTENT_BUF_SIZE);
00406 }
00407 
00408 /**
00409  * @brief   Clear fileName buffer
00410  * @note 
00411  * @retval
00412  */
00413 void ClearFileNameBuf()
00414 {   
00415     /* Clear buffer */
00416     memset(FileName,_SPACE,MSG2FILENAME);
00417 }
00418 ////////////////////////////////////////////////* END *////////////////////////////////////////////////