Inputs:
Outputs:
+ - { name: "ErrFlag", type: "bool" }
Parameters:
- { name: "Thermal1 SPI data (MCP6S93):", type: "uint16 vector of length 2" }
The format of block parameters depends on the SPI chip. The MCP6S93
needs two commands to be properly initialized – channel selection
command and gain selection command. Block parameters for those chips
- are vector of two 16 bit numbers, for example `[0x4000; 0x4100]`.
- Each element of the vector stands for one command.
+ are vector of two 16 bit numbers, for example `[hex2dec('4000');
+ hex2dex('4100')]`. Each element of the vector stands for one
+ command.
For ADC Conditioning, six MCP6S93 are connected in daisy chain. One
SPI transfer for these devices is 96 bits long and we need two
transfers to fully configure the chips. Block parameter for this is
a 2-by-6 matrix of 16 bit numbers. For example `[4000 4000 4000 4000
- 4000 4000; 4100 4100 4100 4100 4100 4100]`. Each row represents one
- SPI transfer.
+ 4000 4000; 4100 4100 4100 4100 4100 4100]` (note: call to `hex2dec`
+ omitted for brevity). Each row represents one SPI transfer.
The TPS65381 chip can be configured by a set of 16 bit commands. The
- parameter is a arbitrary length vector of 16 bit numbers, for
- example `[1234; 5678; 01010; 02020; 03030; 04040]`.
+ parameter is an arbitrary length vector of 16 bit numbers, for
+ example `[hex2dec('0600') hex2dec('0c00') hex2dec('af00')
+ hex2dec('4800')]`.
+
+ If the initialization is successful, ErrFlag output is false. When
+ an error occurred, it is set to true.
Help: *desc
PARAM_COUNT
};
+enum outputs {
+ OUT_ERRFLAG_ID,
+ OUT_COUNT
+};
+
#define THERMISTOR1_VEC_PARAM(S) ssGetSFcnParam(S, PARAM_THERMISTOR1_ID)
#define THERMISTOR2_VEC_PARAM(S) ssGetSFcnParam(S, PARAM_THERMISTOR2_ID)
#define ADCS_VEC_PARAM(S) ssGetSFcnParam(S, PARAM_ADC_SCALE_ID)
}
/*
- * Configure output ports: 0
+ * Configure output ports: 1
*/
- if (!ssSetNumOutputPorts(S, 0)) {
+ if (!ssSetNumOutputPorts(S, OUT_COUNT)) {
return;
}
+ rppAddOutputPort(S, OUT_ERRFLAG_ID, SS_BOOLEAN);
+
/* Set standard options for this block */
rppSetStandardOptions(S);
}
%function BlockInstanceSetup(block, system) void
%if EXISTS("rpp_hydctrinit_in_model") == 0
%assign ::rpp_hydctrinit_in_model = 1
- %<LibAddToCommonIncludes("drv/drv.h")>
+ %<LibAddToCommonIncludes("rpp/spi.h")>
%else
%<LibBlockReportError(block, "Only one Board Init block is allowed in the model.")>
%endif
%if !SLibCodeGenForSim()
- #define SPI_TX_ERR() do { \
- rpp_sci_printk("ERROR: SPI transmission failed.\r\n"); \
- while (1); \
- } while(0)
-
-
- /% Single-value devices %/
- %assign dev_id = ["SPIDEV_MCP6S93_TH1", "SPIDEV_MCP6S93_TH2", "SPIDEV_TPS65381_PWR", "SPIDEV_MCP6S93_SENSUP", "SPIDEV_MCP6S93_DAC"]
- %assign par_name = ["thermistor1", "thermistor2", "powersupply", "sensorsupply", "dacloopback"]
-
- %foreach dev = SIZE(dev_id, 1)
- %assign count = LibBlockParameterWidth(%<par_name[dev]>)
- %foreach idx = count
- %assign val = LibBlockParameterValue(%<par_name[dev]>, idx)
- if (rpp_spi_transfer16(%<dev_id[dev]>, %<val>, NULL) != SUCCESS)
- SPI_TX_ERR();
- %endforeach
- %endforeach
-
- /% Daisy chained device %/
- %assign size = LibBlockParameterDimensions(adcs)
- %foreach command = size[0]
- {
- uint16_t data[%<size[1]>] = {
- %foreach data = size[1]
- cpu_to_be16(%<LibBlockParameterValue(adcs, size[0]*data + command)>),
- %endforeach
- };
- if (rpp_spi_transfer(SPIDEV_MCP6S93_6ADC, sizeof(data), &data, NULL) != SUCCESS)
- SPI_TX_ERR();
- }
+ %assign err_flag = LibBlockOutputSignal(0, "", "", 0)
+
+ rpp_spi_init();
+ {
+ boolean_t err = FALSE;
+ uint16_t rx;
+
+ /% Single-value devices %/
+ %assign dev_id = ["SPIDEV_MCP6S93_TH1", "SPIDEV_MCP6S93_TH2", "SPIDEV_TPS65381_PWR", "SPIDEV_MCP6S93_SENSUP", "SPIDEV_MCP6S93_DAC"]
+ %assign par_name = ["thermistor1", "thermistor2", "powersupply", "sensorsupply", "dacloopback"]
+
+ %foreach dev = SIZE(dev_id, 1)
+ %assign count = LibBlockParameterWidth(%<par_name[dev]>)
+ %foreach idx = count
+ %assign val = LibBlockParameterValue(%<par_name[dev]>, idx)
+ err |= (rpp_spi_transfer16(%<dev_id[dev]>, %<val>, &rx) != SUCCESS);
+ %%rpp_sci_printf("%<dev_id[dev]> %<val> -> 0x%04x\n", rx);
+ %endforeach
+ %endforeach
+
+ /% Daisy chained device %/
+ %assign size = LibBlockParameterDimensions(adcs)
+ %foreach command = size[0]
+ {
+ uint16_t data[%<size[1]>] = {
+ %foreach data = size[1]
+ cpu_to_be16(%<LibBlockParameterValue(adcs, size[0]*data + command)>),
+ %endforeach
+ };
+ err |= (rpp_spi_transfer(SPIDEV_MCP6S93_6ADC, sizeof(data), &data, NULL) != SUCCESS);
+ }
%endforeach
+ %<err_flag> = err;
+ }
%endif
%endfunction