power_switch.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. #include <stdio.h>
  2. #define SCPI_ARGS_N_C 2
  3. #define SCPI_ARGS_N_Q 0
  4. #include "app/scpi/scpi_handler.h"
  5. #include "app/control_table/control_table.h"
  6. #include "drivers/switchboard_control/switchboard_control.h"
  7. // -----
  8. // @argTypesCommand
  9. // Declare argument parser entities for command form
  10. // Supported arguments: 1=NUMERIC
  11. DECLARE_SCPI_ARGS_C( eScpiArg_Numeric, eScpiArg_Numeric );
  12. // -----
  13. // @argTypesQuery
  14. // Declare argument parser entities for query form
  15. // Supported arguments: 0
  16. DECLARE_SCPI_ARGS_Q();
  17. DECLARE_ARGUMENT_NUMERIC_VALUES_I32(AllowedValues_SW_Number, 1, 10);
  18. DECLARE_ARGUMENT_NUMERIC_VALUES_I32(AllowedValues_SW_State, 0, 3);
  19. const uint8_t fsqvbl_CommandHandlerSwCtrl = 1; // CTRL:SW
  20. const uint8_t fsqvbl_CommandHandlerMeasStart = 2; // MEAS:START
  21. const uint8_t fsqvbl_CommandHandlerMeasStop = 3; // MEAS:STOP
  22. const uint8_t fsqvbl_CommandHandlerMeasCont = 4; // MEAS:CONTinue
  23. const uint8_t fsqvbl_CommandHandlerMeasMode = 5; // MEAS:MODE
  24. static uint16_t sw_cmd_reg = 0xc000;
  25. #include "app/scpi/commandHandlers/power_switch.h"
  26. #include "app/nfm/nfm_base.h"
  27. // Refer to:
  28. // [1] SCPI Specification, revision 1999.0
  29. // "Standard Commands for Programmable Instruments (SCPI), VERSION 1999.0, May 1999"
  30. // [2] Gpib Programming Tutorial, (http://g2pc1.bu.edu/~qzpeng/gpib/manual/GpibProgTut.pdf)
  31. // Electronics Group (http://www.few.vu.nl/~elec), 11 January 2000 Electronics Group
  32. // [3] IEEE 488.2 Standard, revision IEEE Std 488.2-1987 (1992)
  33. // "IEEE Standard Codes, Formats, Protocols, and Common Commands for Use With IEEE Std 488.1-1987, IEEE
  34. // =================================================================================
  35. // @fsqvbl_CommandHandlerMEASnSWITCH_group
  36. // State's virtual table
  37. static void fsqe_CommandHandlerCtrl_Switch_Group( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx );
  38. static void fsql_CommandHandlerCtrl_Switch_Group( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx );
  39. static const struct fFSeqEntry_t * fsqf_CommandHandlerCtrl_Switch_Group( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx, const struct fFSeqEntry_t * * pDeferredNext );
  40. const fFSeqVTable_t fsqvbl_CommandHandlerCtrl_group =
  41. {
  42. .f = fsqf_CommandHandlerCtrl_Switch_Group,
  43. .enter = fsqe_CommandHandlerCtrl_Switch_Group,
  44. .leave = fsql_CommandHandlerCtrl_Switch_Group
  45. };
  46. static void fsqe_CommandHandlerCtrl_Switch_Group( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx )
  47. {
  48. sProcessProgramDataCommonContext_t * common_ctx = ctx;
  49. common_ctx->MeasAndSwitch.idx = 0;
  50. SCPI_PARSE_ARGUMENTS( common_ctx ); (void)common_ctx->argsParserStatus; // status is modified
  51. }
  52. static void fsql_CommandHandlerCtrl_Switch_Group( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx )
  53. {
  54. }
  55. // Set bit into possition func
  56. uint16_t set_bit(uint16_t cmd_register, uint16_t SW_Number, uint16_t SW_State) {
  57. unsigned char bit_position=0;
  58. switch (SW_Number)
  59. {
  60. case 1: {bit_position = 1;} break;
  61. case 2: {bit_position = 9;} break;
  62. case 3: {bit_position = 2;} break;
  63. case 4: {bit_position = 10;} break;
  64. case 5: {bit_position = 3;} break;
  65. case 6: {bit_position = 4;} break;
  66. case 7: {bit_position = 12;} break;
  67. case 8: {bit_position = 13;} break;
  68. case 9: {bit_position = 14; SW_State =~SW_State;} break;
  69. case 10: {bit_position = 15; SW_State =~SW_State;} break;
  70. }
  71. if (SW_Number == 4)
  72. { // clear control bits positions
  73. cmd_register &= ~(3 << bit_position);
  74. // set control bits positions
  75. cmd_register |= (SW_State << bit_position);
  76. }
  77. else
  78. { // reset bit in possition
  79. cmd_register &= ~(1 << bit_position);
  80. cmd_register |= ((SW_State & 1) << bit_position);
  81. }
  82. return cmd_register;
  83. }
  84. static const struct fFSeqEntry_t * fsqf_CommandHandlerCtrl_Switch_Group( const struct fFSeqEntry_t * this,
  85. tFSeqCtx_t ctx,
  86. const struct fFSeqEntry_t * * pDeferredNext )
  87. {
  88. const fFSeqEntry_t * nextstate = NULL;
  89. sProcessProgramDataCommonContext_t * common_ctx = ctx;
  90. sScpiParserContext_t * global_ctx = common_ctx->global_ctx;
  91. switch( common_ctx->event )
  92. {
  93. case eProgramData_Event_Write:
  94. {
  95. if( eScpiStatus_success != common_ctx->argsParserStatus ) // check argument parser status
  96. {
  97. common_ctx->status = eProgramDataArgumentSyntax; // parameter syntax error, caller should generate error message
  98. }
  99. else if( ! common_ctx->isQuery )
  100. {
  101. common_ctx->status = eProgramDataIllegalArgument; // forward set, illegal parameter value, caller should generate error message
  102. if( common_ctx->handler_ctx == &fsqvbl_CommandHandlerSwCtrl )
  103. {
  104. // Parse first argument
  105. const sNumericEntry_t * ne = SCPI_PROCESS_ARGUMENT_NUMERIC(common_ctx, &AllowedValues_SW_Number, 0);
  106. if( ScpiNumericSuccess != ne->error ) break;
  107. uint16_t swNumber = ne->Value.demicalInteger;
  108. // Parse second argument
  109. ne = SCPI_PROCESS_ARGUMENT_NUMERIC(common_ctx, &AllowedValues_SW_State, 1);
  110. if( ScpiNumericSuccess != ne->error ) break;
  111. uint16_t swState = ne->Value.demicalInteger;
  112. sw_cmd_reg = set_bit(sw_cmd_reg, swNumber, swState);
  113. HAL_GPIO_WritePin( GPIOA, GPIO_PIN_8, GPIO_PIN_SET); // Turn OFF active Reset signal on external shift REG that was set during powerup
  114. SBHandle.ClrNSS();
  115. SBHandle.Transmit(((uint8_t*)&(sw_cmd_reg)), sizeof(uint8_t));
  116. SBHandle.SetNSS();
  117. common_ctx->status = eProgramDataDone;
  118. }
  119. }
  120. else
  121. {
  122. common_ctx->status = eProgramDataNeedRead; // request processed, wait for reading...
  123. (void)nextstate; // stay in this state
  124. }
  125. }
  126. break;
  127. case eProgramData_Event_Read:
  128. {
  129. // @idx - current position of the source data to be outputed
  130. if( common_ctx->MeasAndSwitch.idx == 0 ) // first reading
  131. {
  132. size_t length = 0;
  133. if( common_ctx->handler_ctx == &fsqvbl_CommandHandlerSwCtrl )
  134. {
  135. length = _snprintf( common_ctx->tempBuffer, sizeof(common_ctx->tempBuffer), "%d""%s", (sw_cmd_reg^49152), "\n" /*invert back bits 15 and 14 to keep valid response despite inversion in 'set_bit' procedure*/);
  136. }
  137. else if( common_ctx->handler_ctx == &fsqvbl_CommandHandlerMeasStart )
  138. {
  139. ControlHandle.Start_Meas();
  140. length = _snprintf( common_ctx->tempBuffer, sizeof(common_ctx->tempBuffer), "%s", "OK\n");
  141. }
  142. else if( common_ctx->handler_ctx == &fsqvbl_CommandHandlerMeasStop )
  143. {
  144. ControlHandle.Stop_Meas();
  145. length = _snprintf( common_ctx->tempBuffer, sizeof(common_ctx->tempBuffer), "%s", "OK\n");
  146. }
  147. else if( common_ctx->handler_ctx == &fsqvbl_CommandHandlerMeasCont )
  148. {
  149. ControlHandle.Continue_Meas();
  150. length = _snprintf( common_ctx->tempBuffer, sizeof(common_ctx->tempBuffer), "%s", "OK\n");
  151. }
  152. else if( common_ctx->handler_ctx == &fsqvbl_CommandHandlerMeasMode )
  153. {
  154. uint8_t res = ControlHandle.Mode_Meas();
  155. if(res != ERROR_CODE)
  156. {
  157. length = _snprintf( common_ctx->tempBuffer, sizeof(common_ctx->tempBuffer), "%s", res ? "TABLE\n" : "MANUAL\n");
  158. }
  159. }
  160. if( length > 0 )
  161. {
  162. // place null-terminator in the end of line
  163. common_ctx->tempBuffer[length] = '\0';
  164. }
  165. else
  166. {
  167. fsq_RaiseError( SCPI_ERROR_INTERNAL_DEVICE,
  168. SCPI_ERROR_INTERNAL_DEVICE_MSG,
  169. global_ctx->sParser.xHandlerToken.shead,
  170. global_ctx->sParser.xHandlerToken.stail );
  171. common_ctx->status = eProgramData_SpecificError; // specific error already generated
  172. break;
  173. }
  174. }
  175. // Since @done flag is set, this dispatcher shall not be called anymore.
  176. // Since this handler is implemented as a single-state automat, there no
  177. // ... other states to go to:
  178. (void)nextstate;
  179. // modify current postion index:
  180. SCPI_RESPONSE_HELPER( common_ctx, common_ctx->MeasAndSwitch.idx );
  181. }
  182. break;
  183. }
  184. return nextstate;
  185. }