Вы здесь

Simple SPI Master Example

The following example shows how to transmit data over SPI bus. For brevity, this example uses default SPI configuration and does not include error detection. You can find the complete example in the “..\Program Files\Diolan\DLN\examples\c_cpp\examples\simple” folder after DLN setup package installation.

C/C++
#include "..\..\..\common\dln_generic.h"
#include "..\..\..\common\dln_spi_master.h"
#pragma comment(lib, "..\\..\\..\\bin\\dln.lib")

int _tmain(int argc, _TCHAR* argv[])
{
	// Open device
	HDLN device;
	DlnOpenUsbDevice(&device);

	// Set SPI frequency
	uint32_t frequency;
	DlnSpiMasterSetFrequency(device, 0, 100000, &frequency);

	// Enable SPI master
	uint16_t conflict;
	DlnSpiMasterEnable(device, 0, &conflict);

	// Prepare output buffer
	uint8_t input[10], output[10];
	for (int i = 0; i < 10; i++) output[i] = i;

	// Perform SPI transaction
	DlnSpiMasterReadWrite(device, 0, 10, output, input);

	// Print received data
	for (int i = 0; i < 10; i++) printf("%02x ", input[i]);

	// Disable SPI and close device
	DlnSpiMasterDisable(device, 0, 0);
	DlnCloseHandle(device);

	return 0;
}

  • Line 1:#include "..\..\..\common\dln_generic.h"

    The dln_generic..h header file declares functions and data structures for the generic interface.

  • Line 2: #include "..\..\..\common\dln_spi_master.h"

    The dln_spi_master.h header file declares functions and data structures for the SPI master interface.

  • Line 3:#pragma comment(lib, "..\\..\\..\\bin\\dln.lib")

    Use specified dln.lib library while project linking.

  • Line 9:DlnOpenUsbDevice(&device);

    The application establishes the connection with the DLN adapter. This application uses the USB connectivity of the adapter. For additional options, refer to the Device Opening & Identification section.

  • Line 13:DlnSpiMasterSetFrequency(device, 0, 100000, frequency);

    The application configures the frequency of the SPI master port 0. You can also configure SPI transmission mode, frame size, etc. See Configuring the SPI Master Interface for details.

  • Line 17:DlnSpiMasterEnable(device, 0, conflict);

    The application enables the SPI master port 0. The DlnSpiMasterEnable() function assigns the corresponding pins to the SPI master module and configures them. If some other module uses a pin required for the SPI bus interface, the DlnSpiMasterEnable() function returns the DLN_RES_PIN_IN_USE error code. The conflict parameter receives the pin’s number.

  • Lines 21:for (int i = 0; i < 10; i++) output[i] = i;

    The application allocates buffers for output and input data. The output buffer is filled with the sequential numbers from 0 to 9.

  • Line 24:DlnSpiMasterReadWrite(device, 0, 10, output, input);

    The DlnSpiMasterReadWrite() function transmits the data to and from the SPI slave device. See the SPI Master Transmission Functions section for additional data transmission functions.

  • Line 27:for (int i = 0; i < 10; i++) printf("%02x ", input[i]);

    The application prints the buffer received from the SPI slave device.

  • Line 30:DlnSpiMasterDisable(device, 0);

    The application releases the SPI master port.

  • Line 31:DlnCloseHandle(device);

    The application closes the handle to the DLN adapter.

Rating: 
Голосов пока нет

Языки

Вход на сайт