6 C function


6.1 The explain of method interface defined in the dynamic library

6.2 Control function return value limit.

6.3 Precondition

6.4 Call I2C read and write

Note: All parameters are string when they are transformed to C script, so different type parameters need to change type when use the parameters. Otherwise the HiChannel may load timeout or no response.

6.5 Explain of writing C script functions

6.6 Sample

Note:Write a C function format sample only as a reference, and can not directly use

When the need for I2C to read and write, first of all need to customize a header file to define the method call, the following definition header file reference sample:

#define CHIP_PORT atoi(argv[3]) //In Socket Connection, argv[3] defines I2C port

#define CHIP_ADDR atoi(argv[5])//In Socket Connection, argv[5] defines dev address

#define USB_ADDR atoi(argv[2]) //In Usb Connection, argv[2] defines dev address of usb device

#define USB_SPEED atoi(argv[1]) //In Usb Connection, argv[1] defines speed of usb

//The g_connect logo is a Sockect connection or a USB connection, g_connect is equal to 0, as the USB connection, g_connect is not 0, as the Sockect connection

#define INIT_CONNECT if (g_connect) {\

InitSock(argv[1],atoi(argv[2])); \

}else{\

if(USBIO_OpenDevice(0) == INVALID_HANDLE_VALUE ){ \

printf("error=%s,return=%d","err openerror,failed",-1); \

return; \

} \

USBIO_SetStream(0,USB_SPEED);\

}

#define DEINIT_CONNECT if (g_connect) {\

DeinitSock();\

}else{\

USBIO_CloseDevice(0);\

}

#define CHIP_MRD(REG_ADDR, P_RBUF, RLEN) if (g_connect) { ReadregMulti((CHIP_PORT), (CHIP_ADDR), (REG_ADDR), 1, ((char *)(P_RBUF)), (RLEN)); \

} else {\

unsigned char P_WBUF[2];\

P_WBUF[0] = USB_ADDR;\

P_WBUF[1] = REG_ADDR;\

USBIO_StreamI2C(0,2,(PVOID)pSend,RLEN,(PVOID)(P_RBUF));\

}

#define CHIP_MWR(REG_ADDR, P_WBUF, WLEN) if (g_connect) { WriteregMulti((CHIP_PORT), (CHIP_ADDR), (REG_ADDR), 1, ((char *)(P_WBUF)), (WLEN));\

} else {\

unsigned char P_RBUF;\

char *p = (char *)malloc((WLEN)+2);\

char *pw =(char *)(P_WBUF);\

int i = 0;\

p[0]= USB_ADDR;\

p[1]= REG_ADDR;\

for (i=0;i<(WLEN);i++) {\

p[i+2] = pw[i];\

};\

USBIO_StreamI2C(0,2+(WLEN),(PVOID)p,0,(PVOID)(&cRecv));\

}

Defines a header file, in the C script first need to introduce it, the following definition of C sample:

#include "stdio.h"

#include "string.h"

#include Custom header file

#include "USBIOX.H" //Must be introduced

extern int g_connect; //This line of code is a must.

int on_line(int argc, char ** argv)

{

INIT_CONNECT;

unsigned char chip_id[4];

unsigned char on_line;

CHIP_MRD(0x6f, &chip_id, 4);

on_line = (chip_id[2]==0x36) && (chip_id[3]==0x31);

if(on_line) {

printf("error=%s,return=%d","ok",1); }

else {

printf("error=%s,return=%d","ok",0); }

DEINIT_CONNECT;

}

//The internal private method, use 'static' to identify

static int demo_m2( int argc, char ** argv )

{

int i = 0;

printf("demo_m2........");

return 0;

}

//Just to be external calls, and are not shown on the tool list of UI functions, this time using the 'inner_' to identify

int inner_compile( int argc, char ** argv )

{

int i = 0;

printf("I'm inner function");

return 0;

}