esr.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #error Deprecated. 'esr_stb' is used instead
  2. #include <stdio.h>
  3. #define SCPI_ARGS_N 0
  4. #include "app/scpi/scpi_handler.h"
  5. #include "app/scpi/commandHandlers/esr.h"
  6. #include "app/acm/acm_base.h"
  7. // Refer to:
  8. // [1] SCPI Specification, revision 1999.0
  9. // "Standard Commands for Programmable Instruments (SCPI), VERSION 1999.0, May 1999"
  10. // [2] Gpib Programming Tutorial, (http://g2pc1.bu.edu/~qzpeng/gpib/manual/GpibProgTut.pdf)
  11. // Electronics Group (http://www.few.vu.nl/~elec), 11 January 2000 Electronics Group
  12. // =================================================================================
  13. // @fsqvbl_CommandHandlerESR
  14. // State's virtual table
  15. static void fsqe_CommandHandlerESR( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx );
  16. static void fsql_CommandHandlerESR( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx );
  17. static const struct fFSeqEntry_t * fsqf_CommandHandlerESR( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx, const struct fFSeqEntry_t * * pDeferredNext );
  18. const fFSeqVTable_t fsqvbl_CommandHandlerESR =
  19. {
  20. .f = fsqf_CommandHandlerESR,
  21. .enter = fsqe_CommandHandlerESR,
  22. .leave = fsql_CommandHandlerESR
  23. };
  24. static void fsqe_CommandHandlerESR( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx )
  25. {
  26. sProcessProgramDataCommonContext_t * common_ctx = ctx;
  27. sScpiParserContext_t * global_ctx = common_ctx->global_ctx;
  28. common_ctx->ESR.idx = 0; // reset position
  29. SCPI_PARSE_ARGUMENTS( common_ctx ); (void)common_ctx->argsParserStatus; // status is modified
  30. (void)global_ctx;
  31. }
  32. static void fsql_CommandHandlerESR( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx )
  33. {
  34. sProcessProgramDataCommonContext_t * common_ctx = ctx;
  35. sScpiParserContext_t * global_ctx = common_ctx->global_ctx;
  36. (void)common_ctx;
  37. (void)global_ctx;
  38. }
  39. static const struct fFSeqEntry_t * fsqf_CommandHandlerESR( const struct fFSeqEntry_t * this,
  40. tFSeqCtx_t ctx,
  41. const struct fFSeqEntry_t * * pDeferredNext )
  42. {
  43. const fFSeqEntry_t * nextstate = NULL;
  44. sProcessProgramDataCommonContext_t * common_ctx = ctx;
  45. sScpiParserContext_t * global_ctx = common_ctx->global_ctx;
  46. (void)common_ctx;
  47. (void)global_ctx;
  48. switch( common_ctx->event )
  49. {
  50. case eProgramData_Event_Write:
  51. {
  52. if( ! common_ctx->isQuery )
  53. {
  54. common_ctx->status = eProgramDataSyntaxError; // invalid command header type: COMMAND not supported (only query)
  55. }
  56. else if( eScpiStatus_success != common_ctx->argsParserStatus ) // check argument parser status
  57. {
  58. common_ctx->status = eProgramDataArgumentSyntax; // parameter syntax error, caller should generate error message
  59. }
  60. else
  61. {
  62. common_ctx->status = eProgramDataNeedRead; // request processed, wait for reading...
  63. (void)nextstate; // stay in this state
  64. }
  65. }
  66. break;
  67. case eProgramData_Event_Read:
  68. {
  69. // Take the input buffer by @pBuffer and @nBufferLength
  70. // @dst = @pBuffer, output response buffer
  71. // @bsize = @nBufferLength, output buffer size
  72. uint8_t * dst = (uint8_t*)global_ctx->sRead.pBufferIdx;
  73. size_t bsize = global_ctx->sRead.nBufferSize;
  74. // @idx - current position of the source data to be outputed
  75. size_t idx = common_ctx->ESR.idx;
  76. if( 0 == idx )
  77. {
  78. memset( common_ctx->tempBuffer, 0, sizeof(common_ctx->tempBuffer) );
  79. uint8_t esr = 0;
  80. // Read ESB:
  81. // "11.5.1.2 Standard Event Status Register Operation", [3]
  82. // "<...> The Standard Event Status Register is destructively read (that is, read and cleared) <...>", [3]
  83. // retrieve ESB and clear it
  84. GPIBMachine.fGPIB_get_event_status_register( &global_ctx->sGPIB.registers, &esr );
  85. int iesr = (int)esr;
  86. // first call: prepare buffer
  87. _snprintf( common_ctx->tempBuffer, sizeof(common_ctx->tempBuffer), "%d", iesr );
  88. }
  89. // @esr_str - response string
  90. const char * esr_str = common_ctx->tempBuffer;
  91. // Copy response string char by char to the output buffer
  92. // ... until the null-character is reached or the output
  93. // ... buffer free space is ran out:
  94. while( ('\0' != esr_str[idx]) && ( 0<bsize ) )
  95. {
  96. *dst++ = esr_str[idx++]; bsize--;
  97. }
  98. // Set the @nDataLength to indicate the copied part size:
  99. // @bsize after copying represent the free space left in bytes;
  100. // @nBufferLength after copying represent the free space in bytes before copying;
  101. // Thus (@nBufferSize-@bsize) is the number of bytes has been copyied:
  102. global_ctx->sRead.nDataLength += global_ctx->sRead.nBufferSize - bsize;
  103. // Check for end-condition:
  104. if( '\0' == esr_str[idx] )
  105. {
  106. common_ctx->status = eProgramDataDone;
  107. }
  108. // Since @done flag is set, this dispatcher shall not be called anymore.
  109. // Since this handler is implemented as a single-state automat, there no
  110. // ... other states to go to:
  111. (void)nextstate;
  112. // modify current postion index:
  113. common_ctx->ESR.idx = idx;
  114. }
  115. break;
  116. }
  117. return nextstate;
  118. }