Maxim Integrated / CommunicationHandler

Dependencies:   USBDevice

Fork of CommunicationHandler by Murat Aslan

Communication Protocol

ASCII and C# API Commands

Configuration and Status Commands

Add Interface

Add new interface.

ASCIIC#
Commandadd_interface <interface_type>:<interface_no>
add_interface <interface_type>:<interface_no> <pin_map>
int AddInterface(InterfaceType interfaceType, byte interfaceNo);
int AddInterface(InterfaceType interfaceType, byte interfaceNo, params Pin[] pinMap);
Parametersinterface_type: I2C, SPI, GPIO, UART, ONEWIRE, PWM, AIN, AOUT
interface_no : 0, 1, 2, …
pin_map:
SPI: MOSI,MISO,SCK,SSEL
I2C: SDA, SCL
UART: TX, RX, CTS, RTS
GPIO: MSB ... LSB
ONEWIRE: PIN
ADC: PIN
PWM: PIN
Returnsresponse status: (int)ResponseStatus(e.g. 0 for SuccessfulResponse, -1 for NullResponse)
Examplesadd_interface i2c:0 P1_6 P1_7

add_interface uart:0 P0_0 P0_1 P0_2 P0_3

add_interface spi:0 P0_5 P0_6 P0_4 P0_7

add_interface onewire:0 AIN2

add_interface gpio:0 P0_0 P0_1 P0_2 P0_3

add_interface gpio:1 P4_4
add_interface pwm:0 P0_7
Bridge.AddInterface(InterfaceType.I2C, 0, Pin.P1_6, Pin.P1_7);
Bridge.AddInterface(InterfaceType.UART, 0, Pin.P0_0, Pin.P0_1, Pin.P0_2, Pin.P0_3);
Bridge.AddInterface(InterfaceType.SPI, 0, Pin.P0_4, Pin.P0_5, Pin.P0_6, Pin.P0_7);
Bridge.AddInterface(InterfaceType.ONEWIRE, 0, Pin.AIN2);
Bridge.AddInterface(InterfaceType.GPIO, 0, Pin.P0_0, Pin.P0_1, Pin.P0_2, Pin.P0_3);
Bridge.AddInterface(InterfaceType.GPIO, 1, Pin.P4_4);
Bridge.AddInterface(InterfaceType.PWM, 0, Pin.P0_7);

Remove Interface

Remove a specific interface.

ASCIIC#
Commandremove_interface <int_type>:<int_no>int RemoveInterface(InterfaceType interfaceType, byte interfaceNo);
Parametersinterface_type: I2C, SPI, GPIO,
UART, ONEWIRE, PWM, AIN, AOUT
interface_no : 0, 1, 2, …
Returnsresponse status: (int)ResponseStatus(e.g. 0 for SuccessfulResponse, -1 for NullResponse)
Examplesremove_interface I2C:0Bridge.RemoveInterface(InterfaceType.I2C, 0);

Remove All Interfaces

Remove all interfaces.

ASCIIC#
Commandremove_all_interfacesint RemoveAllInterfaces();
Parameters--
Returnsresponse status: (int)ResponseStatus(e.g. 0 for SuccessfulResponse, -1 for NullResponse)
Examplesremove_all_interfacesBridge.RemoveAllInterfaces();

List Interfaces

List all added interfaces.

ASCIIC#
Commandlist_interfacesint ListInterfaces(out Interface[] interfaces);
ParametersInterface[] interfaces as a reference parameter
Returnsresponse status: (int)ResponseStatus(e.g. 0 for SuccessfulResponse, -1 for NullResponse)
Exampleslist_interfacesBridge.ListInterfaces(out interface);

Serial Settings

Configures the parameters for serial communication through selected port and connects to the device.

ASCIIC#
CommandN/Aint SerialSettings(string portName);
ParametersN/Astring portName (eg. "COM9")
ReturnsN/AError code: (int)ResponseStatus
(e.g. 0 for SuccessfulResponse, -1 for NullResponse)
ExamplesN/ABridge.SerialSettings("COM9");

Switch to ASCII Mode

Switch from binary mode to ASCII mode. Only available in binary mode.

ASCIIC#
CommandN/Aint SwitchToAsciiMode();
ParametersN/A-
ReturnsN/AError code: (int)ResponseStatus
(e.g. 0 for SuccessfulResponse, -1 for NullResponse)
ExamplesN/ABridge.SwitchToAsciiMode();

Switch to Binary Mode

Switch from ASCII mode to binary mode. Only available in ASCII mode.

ASCIIC#
Commandswitch_to_binary_modeN/A
Parameters-N/A
ReturnsError codeN/A
Examplesswitch_to_binary_modeN/A

Get Build Version

Gets the build version of Bridge.dll.

ASCIIC#
CommandN/Astring GetBuildVersion();
ParametersN/A-
ReturnsN/ABuild version (e.g. "1.0.10")
ExamplesN/ABridge.GetBuildVersion();

Get Communication Status

Gets current communication status.

ASCIIC#
CommandN/ACommunicationStatus GetCommunicationStatus();
ParametersN/A-
ReturnsN/ACommunication status (e.g. CommunicationStatus.Busy)
ExamplesN/ABridge.GetCommunicationStatus();

Get Available Ports

Gets available ports which connects to PICO via the vendor and product ids.

ASCIIC#
CommandN/Astring[] GetAvailablePorts();
ParametersN/A-
ReturnsN/AAvailable ports (e.g. ["COM9"])
ExamplesN/ABridge.GetAvailablePorts();

Get Platform Info

Gather platform specific information.

ASCIIC#
Commandget_platform_infoint GetPlatformInfo(out PlatformInfo platform_info);
ParametersPlatformInfo platform_info as a reference
parameter for pass by reference
Returnsplatform_name=<platform_name>,
version=<version>
Error code: (int)ResponseStatus
(e.g. 0 for SuccessfulResponse, -1 for NullResponse)
Examplesget_platform_infoBridge.GetPlatformInfo(out platform_info);

Close

Closes the port used for the serial communication.

ASCIIC#
CommandN/Avoid Close();
ParametersN/A-
ReturnsN/A-
ExamplesN/Avoid Close();

SPI Commands

SPI Format

Set SPI format of SPI interface.

ASCIIC#
Commandspi_format <interface_no> <bits> <mode>int SpiFormat(byte interfaceNo, byte bits, byte spiMode);
Parametersinterface_no: 0, 1, 2, …
bits : Number of bits per SPI frame
mode : Clock polarity and phase mode (0, 1, 2, 3)
mode 0: POL=0 & PHA=0
mode 1: POL=0 & PHA=1
mode 2: POL=1 & PHA=0
mode 3: POL=1 & PHA=1
Returnsresponse status: (int)ResponseStatus(e.g. 0 for SuccessfulResponse, -1 for NullResponse)
Examplesspi_format 0 8 0Bridge.SpiFormat(0, 8, 0);

SPI Frequency

Set frequency of SPI interface.

ASCIIC#
Commandspi_frequency <interface_no> <frequency_in_hz>int SpiFrequency(byte interfaceNo, uint frequencyHz);
Parametersinterface_no: 0, 1, 2, …
frequency_in_hz: The bus frequency in hertz
Returnsresponse status: (int)ResponseStatus(e.g. 0 for SuccessfulResponse, -1 for NullResponse)
Examplesspi_frequency 0 24000000Bridge.SpiFrequency(0, 24000000);

SPI Write

Write byte over SPI interface. Read response data byte from SPI slave.

ASCIIC#
Commandspi_write <interface_no> <value>int SpiWrite(byte interfaceNo, uint[] value, out uint[] response);
Parametersinterface_no: 0, 1, 2, …
value : Data to be sent to the SPI slave
response : SPI slave response data with reference
Returnsspi_response=0x<data>response status: (int)ResponseStatus (e.g. 0 for SuccessfulResponse, -1 for NullResponse)
Integer SPI slave response data with reference.
Examplesspi_frequency 0 24000000Bridge.SpiFrequency(0, 24000000);

I2C Commands

I2C Frequency

Set frequency of I2C interface.

ASCIIC#
Commandi2c_frequency <interface_no> <frequency_in_hz>int I2cFrequency(byte interfaceNo, uint frequencyHz);
Parametersinterface_no: 0, 1, 2, …
frequency_in_hz: The bus frequency in hertz
Returnsresponse status: (int)ResponseStatus(e.g. 0 for SuccessfulResponse, -1 for NullResponse)
Examplesi2c_frequency 0 24000000Bridge.I2cFrequency(0, 24000000);

I2C Read

Read data from I2C interface.

ASCIIC#
Commandi2c_read <interface_no> <address> <length>

To specify memory address during i2c read:
i2c_read <interface_no> <address> <length><memory_start_address>
int I2cRead(byte interfaceNo, byte address, byte length, out int[] read_data);
Parametersinterface_no: 0, 1, 2, …
address : 7-bit I2C slave address
length : Number of bytes to read
read_data : Integer I2C slave response data via pass by reference
Returnsresponse status: (int)ResponseStatus(e.g. 0 for SuccessfulResponse, -1 for NullResponse)
and Integer I2C slave response data with reference
Examplesi2c_read 0 0x2A 3Bridge.I2cRead(0, 0x2A, 3, out read_data);

I2C Write

Write data over I2C interface.

ASCIIC#
Commandi2c_write <interface_no><address><data…>int I2cWrite(byte interfaceNo, byte address, byte[] data);
Parametersinterface_no: 0, 1, 2, …
address : 7-bit I2C slave address
data : data to send
Returnsresponse status: (int)ResponseStatus(e.g. 0 for SuccessfulResponse, -1 for NullResponse)
Examplesi2c_write 0 0x2A 0x01 0x02 0x03Bridge.I2cWrite(0, 0x2A, new int[] {0x01, 0x02, 0x03});

I2C Repeated Write

Write data over I2C interface with repeated option.

ASCIIC#
Commandi2c_write_r <interface_no> <address> <data…>int I2cWriteRepeated(byte interfaceNo, byte address, byte[] data);
Parametersinterface_no: 0, 1, 2, …
address : 7-bit I2C slave address
data : data to send
Returnsresponse status: (int)ResponseStatus(e.g. 0 for SuccessfulResponse, -1 for NullResponse)
Examplesi2c_write_r 0 0x2A 0x01 0x02 0x03Bridge.I2cWriteRepeated(0, 0x2A, new int[] {0x01, 0x02, 0x03});

I2C Start

Sends a start flag to I2C bus.

ASCIIC#
Commandi2c_start <interface_no>int I2cStart(byte interfaceNo);
Parametersinterface_no: 0, 1, 2, …
Returnsresponse status: (int)ResponseStatus(e.g. 0 for SuccessfulResponse, -1 for NullResponse)
Examplesi2c_start 0Bridge.I2cStart(0);

I2C Stop

Sends a stop flag to I2C bus.

ASCIIC#
Commandi2c_stop <interface_no>int I2cStop(byte interfaceNo);
Parametersinterface_no: 0, 1, 2, …
Returnsresponse status: (int)ResponseStatus(e.g. 0 for SuccessfulResponse, -1 for NullResponse)
Examplesi2c_stop 0Bridge.I2cStop(0);

UART Commands

UART Format

Set UART interface format.

ASCIIC#
Commanduart_format <interface_no> <bits> <parity> <stop_bits>int UartFormat(byte interfaceNo, byte bits, Parity parity, byte stopBits) ;
Parametersinterface_no: 0, 1, 2, …
bits : The number of bits in a word (5-8)
parity: Use the number as 0:none, 1:odd, 2:even
stop_bits : The number of stop bits (1 or 2)
Returnsresponse status: (int)ResponseStatus(e.g. 0 for SuccessfulResponse, -1 for NullResponse)
Examplesuart_format 0 8 0 1Bridge.UartFormat(0, 9, Parity.None, 1);

UART Set Baudrate

Set UART interface baudrate.

ASCIIC#
Commanduart_baudrate <interface_no> <baudrate>int UartBaud(byte interfaceNo, uint baudrate) ;
Parametersinterface_no: 0, 1, 2, …
baudrate : Integer baudrate
Returnsresponse status: (int)ResponseStatus(e.g. 0 for SuccessfulResponse, -1 for NullResponse)
Examplesuart_baudrate 0 115200Bridge.UartBaud(0, 115200);

UART Read

Read data from UART interface.

ASCIIC#
Commanduart_read <interface_no> <length>int UartRead(byte interfaceNo, byte length, out int[] read_data);
Parametersinterface_no: 0, 1, 2, …
length : Number of bytes to read
read_data : Integer UART data array via pass by reference
Returnsuart_read=0x<data1>,0x<data2>,...response status: (int)ResponseStatus (e.g. 0 for SuccessfulResponse, -1 for NullResponse) and Integer UART data array with reference
Examplesuart_read 0 3Bridge.UartRead(0, 3, out read_data);

UART Write

Write data over UART interface.

ASCIIC#
Commanduart_write <interface_no> <data…>int UartWrite(byte interfaceNo, byte[] data);
Parametersinterface_no: 0, 1, 2, …
data: data array to send
Returnsresponse status: (int)ResponseStatus(e.g. 0 for SuccessfulResponse, -1 for NullResponse)
Examplesuart_write 0 0x01 0x02 0x03Bridge.UartWrite(0, new int[] {0x01, 0x02, 0x03});

GPIO Commands

GPIO Read

Read data from GPIO.

ASCIIC#
Commandgpio_read <interface_no>int GpioRead(byte interfaceNo, out int read_data);
Parametersinterface_no: 0, 1, 2, …
read_data : Integer data with reference
Returnsgpio_read=0x...response status: (int)ResponseStatus
(e.g. 0 for SuccessfulResponse, -1 for NullResponse)
and Integer data with reference
Examplesgpio_read 0Bridge.GpioRead(0, out read_data);

GPIO Write

Write data to GPIO.

ASCIIC#
Commandgpio_write <interface_no> <data>int GpioWrite(byte interfaceNo, byte data);
Parametersinterface_no: 0, 1, 2, …
data: data byte to send
Returnsresponse status: (int)ResponseStatus(e.g. 0 for SuccessfulResponse, -1 for NullResponse)
Examplesgpio_write 0 0xAABridge.GpioWrite(0, 0xAA);

PWM Commands

PWM Write

Write data to PWM.

ASCIIC#
Commandpwm_write <interface_no> <duty_cycle>int PwmWrite(byte interfaceNo, float duty_cycle);
Parametersinterface_no: 0, 1, 2, …
duty_cycle: A floating-point value representing the output duty-cycle.
Duty cycle should lie between 0.0 (representing on 0%) and 1.0 (representing on 100%).
Values outside this range will be saturated to 0.0 or 1.0
Returnsresponse status: (int)ResponseStatus(e.g. 0 for SuccessfulResponse, -1 for NullResponse)
Examplespwm_write 0 0.23Bridge.PwmWrite(0, 0.23);

PWM Read

Read previously set data from PWM.

ASCIIC#
Commandpwm_read <interface_no>int PwmRead(byte interfaceNo, out float read_data);
Parametersinterface_no: 0, 1, 2, …
read_data : float PWM data with reference
Returnspwm_read=0x<data>response status: (int)ResponseStatus
(e.g. 0 for SuccessfulResponse, -1 for NullResponse)
and float PWM data with reference
Examplespwm_read 0Bridge.PwmRead(0, out read_data);

PWM Period

Set PWM period.

ASCIIC#
Commandpwm_period <interface_no> <period_in_us>int PwmPeriod(byte interfaceNo, int periodUs);
ParametersSet the PWM period, specified in micro-seconds,
keeping the duty cycle the same
Returnsresponse status: (int)ResponseStatus(e.g. 0 for SuccessfulResponse, -1 for NullResponse)
Examplespwm_period 0 5000Bridge.PwmPeriod(0, 5000);

PWM Pulsewidth

Set PWM pulsewidth.

ASCIIC#
Commandpwm_pulsewidth <interface_no> <pulse_width>int PwmPulseWidth(byte interfaceNo, int pulseWidth);
ParametersSet the PWM pulsewidth, specified in ms,
keeping the period the same
Returnsresponse status: (int)ResponseStatus(e.g. 0 for SuccessfulResponse, -1 for NullResponse)
Examplespwm_pulsewidth 0 12000Bridge.PwmPulseWidth(0, 12000);

ADC Commands

ADC Read

Read data from ADC.

ASCIIC#
Commandadc_read <interface_no>int AdcRead(byte interfaceNo, out int read_data);
Parametersinterface_no: 0, 1, 2, …read_data : Integer ADC data with reference
Returnsadc_read=0x<data>response status: (int)ResponseStatus
(e.g. 0 for SuccessfulResponse, -1 for NullResponse)
and Integer ADC data with reference
Examplesadc_read 0Bridge.AdcRead(0, out read_data);

C# API Data Structures

Platform Info

struct PlatformInfo{
string name;
int versionNumberMajor;
int versionNumberMinor;
int versionNumberBuild;
};

Interface Type

enum InterfaceType {
I2C, SPI, GPIO, UART, ONEWIRE, PWM, ADC
};

Response Status

enum ResponseStatus {
SuccessfulResponse = 0,
NullResponse = -1,
Timeout = -2,
FunctionIDUnidentified = -3,
FunctionIDUnmatched = -4,
Overflow = -5
};

Communication Status

enum CommunicationStatus
{
Waiting,
Idle,
Busy,
Closed
}

Parity

enum Parity
{
None = 0, Odd, Even, Forced1, Forced0
};

Pin

enum Pin {
P0_0 = (0 << PORT_SHIFT), P0_1, P0_2, P0_3, P0_4, P0_5, P0_6, P0_7,
P1_0 = (1 << PORT_SHIFT), P1_1, P1_2, P1_3, P1_4, P1_5, P1_6, P1_7,
P2_0 = (2 << PORT_SHIFT), P2_1, P2_2, P2_3, P2_4, P2_5, P2_6, P2_7,
P3_0 = (3 << PORT_SHIFT), P3_1, P3_2, P3_3, P3_4, P3_5, P3_6, P3_7,
P4_0 = (4 << PORT_SHIFT), P4_1, P4_2, P4_3, P4_4, P4_5, P4_6, P4_7,
Analog input pins
AIN_0 = (0xA << PORT_SHIFT), AIN_1, AIN_2, AIN_3, AIN_4, AIN_5, AIN_6, AIN_7, AIN_8, AIN_9,
LEDs
LED1 = P2_4,
LED2 = P2_5,
LED3 = P2_6,
LED_RED = LED1,
LED_GREEN = LED2,
LED_BLUE = LED3,
Push button
SW1 = P2_7,
USB bridge connected UART pins
USBTX = P2_1,
USBRX = P2_0,
USBTX = P0_1,
USBRX = P0_0,
STDIO_UART_TX = USBTX,
STDIO_UART_RX = USBRX,
I2C pins
I2C0_SCL = P1_7,
I2C0_SDA = P1_6,
I2C1_SCL = P3_5,
I2C1_SDA = P3_4,
UART pins
UART0_RX = P0_0,
UART0_TX = P0_1,
UART0_CTS = P0_2,
UART0_RTS = P0_3,
UART1_RX = P2_0,
UART1_TX = P2_1,
UART2_RX = P3_0,
UART2_TX = P3_1,
UART2_CTS = P3_2,
UART2_RTS = P3_3,
SPI pins
SPI0_SCK = P0_4,
SPI0_MOSI = P0_5,
SPI0_MISO = P0_6,
SPI0_SS = P0_7,
SPI1_SCK = P1_0,
SPI1_MOSI = P1_1,
SPI1_MISO = P1_2,
SPI1_SS = P1_3,
SPI2_SCK = P2_4,
SPI2_MOSI = P2_5,
SPI2_MISO = P2_6,
SPI2_SS = P2_7,
Not connected
NC = 255
};

Interface

struct Interface {
InterfaceType type;
int no;
Pin pinMap[];
};

Parity

struct Parity {
None = 0, Odd, Even, Forced1, Forced0
};

Bridge()


All wikipages