7.10Component main
Source file components/main.c defines the main of the component version of the demo application. This section walks through the source to explain its use of the Stream programming model; error handling code is elided here. main loads a DSP MIPS image, creates three component instances, creates two connections between them, and then sends three commands, as shown below.
main first starts the Stream programming model runtime:
spi_spm_start("spm_demo", &argc, argv, SPI_SPM_FLAG_NONE);
main processes its argc/argv after this call, not before, since spi_spm_start may adjust argc/argv, removing special SPM runtime options from the program’s argument list. Then main loads a DSP MIPS image, passing it an argument to set the debug log enable mask to 0xf.
char *dsp_argv[3] = { "spm_demo", "--spi_log_mask=debug,0xf", NULL };
if (spi_load_image(SPI_PEL_DSP_MIPS, image,
dsp_argv, NULL, SPI_IMAGE_FLAG_NONE)!= 0) {
...
It then finds each of the three components required by the application (file input, green screen removal, and file output) and creates an instance of each component.
/* Find the file-in, gsr, and file-out components. */
file_in = spi_component_find("spi_example_filein",
SPI_PROVIDER_SPI, NULL, NULL);
file_out = spi_component_find("spi_example_fileout",
SPI_PROVIDER_SPI, NULL, NULL);
gsr = spi_component_find("spi_example_gsr",
SPI_PROVIDER_SPI, NULL, NULL);
...
/* Create one instance of each component. */
i0 = spi_instance_new("in0", file_in);
o0 = spi_instance_new("out0", file_out);
g0 = spi_instance_new("gsr0", gsr);
...
Next, it defines the plumbing to connect the components: the file input component output port connects to the green screen reduction component input port, and the green screen reduction component output port connects to the file output component input.
/* Connect file in instance to gsr inst. and gsr inst. to file out inst. */
spi_connection_t in_to_gsr = spi_connect("file_in_to_gsr",
i0, FILE_IN_PORT_OUT,
g0, SPI_GSR_PORT_IN,
1 /* depth */);
spi_connection_t gsr_to_out = spi_connect("gsr_to_file_out",
g0, SPI_GSR_PORT_OUT,
o0, FILE_OUT_PORT_IN,
1 /* depth */);
...
To start the ball rolling, the application issues a FILE_IN_CMD_FILENAME command to the file input component, passing the input filename in the command payload. If the response to the command is not SPI_RESPONSE_ERRNO_OK, the application fails.
/*
* Send the input filename to the file in instance.
* The FILE_IN_CMD_PAYLOAD is the filename string including the
* '\0' terminator.
*/
response = spi_cmd_send(i0, FILE_IN_CMD_FILENAME,
(void *)infile, strlen(infile) + 1);
if (spi_response_get_errno(response) != SPI_RESPONSE_ERRNO_OK) {
fatal("setting input filename: %s",
spi_response_strerror(spi_response_get_errno(response)));
}
spi_response_free(response);
Similarly, the application issues a FILE_OUT_CMD_FILENAME command to the file output component, passing output filename in the command payload. If the response to the command is not SPI_RESPONSE_ERRNO_OK, the application fails.
/* Send the output filename to the file out instance. */
response = spi_cmd_send(o0, FILE_OUT_CMD_FILENAME,
(void *)outfile, strlen(outfile) + 1);
if (spi_response_get_errno(response) != SPI_RESPONSE_ERRNO_OK) {
fatal("setting output filename: %s",
spi_response_strerror(spi_response_get_errno(response)));
}
spi_response_free(response);
The application will be done once the file output component has written the output file. The application sends the FILE_OUT_CMD_REPORT_WRITTEN command to wait for completion.
/* Wait for the file out instance to write its buffer to the output file. */
response = spi_cmd_send(o0, FILE_OUT_CMD_REPORT_WRITTEN, NULL, 0);
if (spi_response_get_errno(response) != SPI_RESPONSE_ERRNO_OK) {
fatal("waiting for file write: %s",
spi_response_strerror(spi_response_get_errno(response)));
}
spi_response_free(response);
Finally, the application stops the Stream programming model; all the work is done.
spi_spm_stop();
The following subsection demonstrates the use of an initialization file to greatly simplify the coding of components/main.c.
7.10.1Initialization file
Instead of writing explicit initialization code as described in the previous section, including tedious error checking, the programmer can provide a simple initialization file as described in Initialization files above. The initialization code in components/main.c is conditionalized #if !defined(INIT_FILE). If it is compiled with spc -D INIT_FILE, the source does not use the explicit initialization code, but rather assumes that the user will invoke the System MIPS application with a command line initialization file option:
$ ./spm_demo.init.sys.out --spi_init_file=init.xml
Initialization file init.xml contains statements that load the target image on DSP MIPS, create instances, create connections between instances, and issue initialization commands to instances; see the diagram in the preceding Component main section.
from="i0:FILE_IN_PORT_OUT" to="g0:GSR_PORT_IN" />
from="g0:GSR_PORT_OUT" to="o0:FILE_OUT_PORT_IN" />
The remaining source in main in components/main.c is extremely simple: it just calls spi_spm_start and spi_spm_stop, letting the initialization file processing by spi_spm_start do all the work.
The next chapter explains how to build and run the complete spm_demo application from the command line.
Share with your friends: |