access IO-Board in C

Post Reply
Splicer
Posts: 4
Joined: Tue Nov 12, 2013 3:27 am
languages_spoken: english, german
ODROIDs: Odroid-XU, IO-Board
Contact:

access IO-Board in C

Unread post by Splicer » Tue Nov 12, 2013 3:55 am

Hi, I'm new to the Forum and quite new to the Ordroid
sorry if this topic is redundant or at the wrong place :)

Ive installed the Odroid-USBIO from https://github.com/hardkernel/Odroid-USBIO and the given C program worked fine for toggeling the LED and such
I tried to run the python programs as well but the still give me timeout errors (yes i installed pyusb1.0)
so i thought i could write a C lib that could do the same as the hk_usb_io.py
I also took a look the main.c
for toggling the LED it works just as it should, but on reading if the button is pressed I i am stuck

So my question is the following:
How exactly do I access the IO-Board using C?

Here is the code i have been beginning to write:

Code: Select all

//main.c
#include <stdio.h>
#include <sys/types.h>
#include "hidapi.h"
#include "usb_io.c"

#define TRUE 1
#define FALSE 0

int main() {
	hid_device *dev;
	dev = dev_open();
	if(!dev)	return -1;
	
	printf("%s\n", module_version());
	printf("%s\n", rom_version(dev));//<- gives me crap
	
	printf("Toggeling LED\n");
	toggle_led(dev);
	
	printf("Push button!\n");
	while(!read_switch(dev)){} //<-stuck
	printf("Button pressed\n");
	
	return 0;
	}

Code: Select all

//usb_io.c
/*
Ported version of hk_usb_io.py as c file
*/

#ifndef usb_io
#define usb_io

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include "hidapi.h"

#define TRUE 1
#define FALSE 0

//instructions
#define _mod_ver  "0.51"	// python HKUSBIO module version
#define _mod_date "2/25/2013"	// module date
#define u_ad0 0x37	// read ADC value from RA0
#define u_ad1 0x38	// read ADC value from RA1
#define u_i2c_init 0x40	// i2c_init(void)
#define u_i2c_idle 0x41	// i2c_idle(void)
#define u_i2c_strt 0x42	// i2c_start(uchar)
#define u_i2c_stop 0x43	// i2c_stop(void)
#define u_i2c_slak 0x44	// uchar i2c_slave_ack(void)
#define u_i2c_writ 0x45	// void i2c_write(uchar)
#define u_i2c_mack 0x46	// void i2c_master_ack(uchar)
#define u_i2c_read 0x47	// uchar i2c_read(void)
#define u_i2c_dtrd 0x48	// uchar i2c_isdatardy(void)
#define u_spi_init 0x50	// void spi_init(mode, baud, sample)
#define u_spi_tran 0x51	// uchar spi_transfer(regAddr)
#define u_spi_cs 0x52	// void spi_cs(enable|disable)
#define u_rom 0x85	// get PIC rom version
#define u_led 0x80	// toggle LED 
#define u_swc 0x81	// get switch pressed or not
#define u_gpd 0x84	// configure GPIO direction on a pin
#define u_gpi 0x82	// read value on GPIO pin
#define u_gpo 0x83	// write value to GPIO pin
#define u_uss 0x86	// send a string to the UART
#define u_tst 0x87	// test if UART has a char available
#define u_urc 0x88	// read a single char from UART
#define u_usc 0x89	// send a single char to the UART
#define h_getr 0x98	// SFR register to read
#define h_setr 0x99	// SFR register to set
#define h_getb 0x9a	// SFR read register bit
#define h_setb 0x9b	// SFR set register bit

//registers
#define INTCON 0xf2   
#define INTCON2 0xf1  
#define INTCON3 0xf0 
#define SSP1CON3 0xcb 
#define SSP1MSK 0xca  
#define SSP1BUF 0xc9  
#define SSP1ADD 0xc8 
#define SSP1STAT 0xc7
#define SSP1CON1 0xc6 
#define SSP1CON2 0xc5 
#define TMR2 0xbc    
#define T2CON 0xba    
#define PSTR1CON 0xb9 
#define BAUDCON1 0xb8 
#define PWM1CON 0xb7 
#define SPBRGH1 0xb0
#define TRISE 0x96    
#define TRISD 0x95    
#define TRISC 0x94   
#define TRISB 0x93    
#define TRISA 0x92    
#define LATE 0x8d     
#define LATD 0x8c    
#define LATC 0x8b 
#define LATB 0x8a     
#define LATA 0x89     
#define PORTE 0x84   
#define PORTD 0x83    
#define PORTC 0x82   
#define PORTB 0x81    
#define PORTA 0x80   
#define ANSELE 0x5f
#define ANSELD 0x5e   
#define ANSELC 0x5d   
#define ANSELB 0x5c  
#define ANSELA 0x5b   
#define GP_RAM1 0x52  
#define GP_RAM2 0x51  
#define GP_RAM3 0x50 
#define GP_RAM4 0x4f
#define SPBRG1 0xaf   
#define CCPTMRS 0x59  
#define CCP1CON 0xbd 
#define CCPR1H 0xbf   
#define CCPR1L 0xbe   
#define T2CON 0xba    
#define PR2 0xbb 
#define VREFCON1 0x7c
#define VREFCON2 0x7b 
#define VREFCON0 0x7d 
#define ADCON0  0xc2

//bits
#define SSP1STAT_SMP 7 
#define SSP1STAT_CKE 6 
#define SSP1STAT_BF 0 
#define SSP1CON1_WCOL 7 
#define SSP1CON1_SSPOV 6 
#define SSP1CON1_SSPEN 5 
#define SSP1CON1_CKP 4
#define VREFCON1_DACEN 7 
#define VREFCON1_DACLPS 6 
#define VREFCON1_DACOE 5 
#define VREFCON1_DACNSS 0 
#define VREFCON1_DACPSS1 3 
#define VREFCON1_DACPSS0 2

//constants
#define rd4 1		// GPIO pin rd4	def=input
#define rd5 2		// GPIO pin rd5	def=input
#define rd6 3		// GPIO pin rd6	def=output
#define rd7 4		// GPIO pin rd7	def=output
#define dir_output 0	// control GPIO pin direction
#define dir_input 1
#define SPI_LOW_BAUD 0// 750khz
#define SPI_MED_BAUD 1// 3mhz
#define SPI_HI_BAUD  2// 12mhz
#define SPI_SAMP_MID 0// sample input in middle data input time
#define SPI_SAMP_END 1// sample input at end of data input
#define SPI_MODE0 0
#define SPI_MODE1 1
#define SPI_MODE2 2
#define SPI_MODE3 3
#define SPI_CS_ENABLE 0
#define SPI_CS_DISABLE 1
#define I2C_DATA_ACK 0	// i2c constants
#define I2C_DATA_NOACK 1
#define I2C_WRITE_CMD 0
#define I2C_READ_CMD 1
#define I2C_START_CMD 0
#define I2C_REP_START_CMD 1
#define I2C_REQ_ACK 0
#define I2C_REQ_NOACK 0

//buffer
#define MAX_STR 65

hid_device* dev_open() {

	hid_device *device;

	device = hid_open(0x04D8, 0x003F, NULL);

	if(device) {
		printf("HID Device opened\n");
		hid_set_nonblocking(device, TRUE);
	} else
		printf("HID Device open failed\n");

	return device;
}

void dev_close(hid_device *device) {
	hid_close(device);
}

void checkmSec(int msec) {
	struct timeval ts, te;
	long tdiff, sec, ms;

	gettimeofday(&ts, NULL);

	while(1) {
		gettimeofday(&te, NULL);

		tdiff = (1000000*(te.tv_sec - ts.tv_sec)) + (te.tv_usec - ts.tv_usec);

		ms = tdiff / 1000;

		if(ms > msec)
			break;
	}
}

//init() see beginning of main() in main.c 

//o_buf : send to PIC
//i_buf : recived from PIC
//i_buf[0] ist der Befehl auf den geantwortet wird

char * module_version() {
	static char  a[80];
	strcpy(a, "Module Version: ");
	strcat(a, _mod_ver);
	strcat(a, " Date: ");
	strcat(a, _mod_date);
	return &a[0];
} 

const char * rom_version(hid_device *device) {	// get PIC ROM version
	unsigned char o_buf[MAX_STR];
	unsigned char i_buf[MAX_STR];
	// read ROM version
	o_buf[1] = u_rom; //0 oder 1 ?
	if (hid_write(device, o_buf, sizeof(o_buf)) == -1) {dev_close(device);}
	checkmSec(50);
	if (hid_write(device, i_buf, sizeof(i_buf)) == -1) {dev_close(device);}
	static char a[80];
	strcpy(a, "ROM v");
	strncat(a, &i_buf[1], 1);
	strcat(a,".");
	strncat(a, &i_buf[2], 2); 
	//return &i_buf[0];
	return &a[0];
}

void toggle_led(hid_device *device) {	// toggle LED
	unsigned char o_buf[MAX_STR];
	unsigned char i_buf[MAX_STR];
	o_buf[0] = u_led;
	if (hid_write(device, o_buf, sizeof(o_buf)) == -1) {dev_close(device);}
}

char read_switch(hid_device *device) {	// read switch press
	unsigned char o_buf[MAX_STR];
	unsigned char i_buf[MAX_STR];
	char ret;
	o_buf[0] = u_swc; //0 oder 1 ?
	if (hid_write(device, o_buf, sizeof(o_buf)) == -1) {dev_close(device);}
	checkmSec(50);
	if (hid_write(device, i_buf, sizeof(i_buf)) == -1) {dev_close(device);}
	if (i_buf[1]==0) {ret=TRUE;}
	else {ret=FALSE;}
	return ret;
}

void gpio_init(hid_device *device,char pin, char pdir) {	// set GPIO direction on pin
	unsigned char o_buf[MAX_STR];
	unsigned char i_buf[MAX_STR];
	
}

void gpio_out(hid_device *device,char pin) {	// otuput a value on GPIO pin
	unsigned char o_buf[MAX_STR];
	unsigned char i_buf[MAX_STR];
	
}

unsigned char gpio_in(hid_device *device,char pin) {	// read value on GPIO pin
	unsigned char o_buf[MAX_STR];
	unsigned char i_buf[MAX_STR];
	unsigned char a;
	return a;	
}

unsigned int adc_ra0(hid_device *device) { // do ADC conversion on RA0
	unsigned char o_buf[MAX_STR];
	unsigned char i_buf[MAX_STR];
	unsigned int a;
	return a;
}

unsigned int adc_ra1(hid_device *device) {	// do ADC conversion on RA1
	unsigned char o_buf[MAX_STR];
	unsigned char i_buf[MAX_STR];
	unsigned int a;
}

unsigned char ser_test(hid_device *device) {	// check if a char available on serial port
	unsigned char o_buf[MAX_STR];
	unsigned char i_buf[MAX_STR];
	unsigned char a;
	return a;
}

void ser_putc(hid_device *device,char schar) {	// send a char to the serial port
	unsigned char o_buf[MAX_STR];
	unsigned char i_buf[MAX_STR];
	
}

void ser_puts(hid_device *device, const char * strval) {	// send a string to the serial port
	unsigned char o_buf[MAX_STR];
	unsigned char i_buf[MAX_STR];
	
}

unsigned char ser_getc(hid_device *device) { // get a single char from the serial port
	unsigned char o_buf[MAX_STR];
	unsigned char i_buf[MAX_STR];
	unsigned char a;
	return a;
}

unsigned char sfr_get_reg(hid_device *device, char reg) {	// get a SFR register
	unsigned char o_buf[MAX_STR];
	unsigned char i_buf[MAX_STR];
	unsigned char a;
	return a;
}

unsigned char sfr_set_reg(hid_device *device, char reg, char rval) {	// set a SFR register
	unsigned char o_buf[MAX_STR];
	unsigned char i_buf[MAX_STR];
	unsigned char a;
	return a;
}

unsigned char sfr_get_regbit(hid_device *device, char reg, char bval) {	// get a SFR register bit
	unsigned char o_buf[MAX_STR];
	unsigned char i_buf[MAX_STR];
	unsigned char a;
	return a;
}

unsigned char sfr_set_regbit(hid_device *device, char reg, char rbit, char bval) {	// set a SFR register bit
	unsigned char o_buf[MAX_STR];
	unsigned char i_buf[MAX_STR];
	unsigned char a;
	return a;
}

void i2c_init(hid_device *device) {	// init i2c
	unsigned char o_buf[MAX_STR];
	unsigned char i_buf[MAX_STR];
	
}

void i2c_idle(hid_device *device) {	// i2c idle
	unsigned char o_buf[MAX_STR];
	unsigned char i_buf[MAX_STR];
	
}

void i2c_start(hid_device *device, char cval) {	// i2c start	
	unsigned char o_buf[MAX_STR];
	unsigned char i_buf[MAX_STR];
	
}

void i2c_stop(hid_device *device) {	// i2c stop
	unsigned char o_buf[MAX_STR];
	unsigned char i_buf[MAX_STR];
	
}

unsigned char i2c_slave_ack(hid_device *device) {	// i2c slave ack
	unsigned char o_buf[MAX_STR];
	unsigned char i_buf[MAX_STR];
	unsigned char a;
	return a;
}

void i2c_write(hid_device *device, char cval) {	// i2c write
	unsigned char o_buf[MAX_STR];
	unsigned char i_buf[MAX_STR];
	
}

void i2c_master_ack(hid_device *device, char cval) {	// 1=nack, 0=ack
	unsigned char o_buf[MAX_STR];
	unsigned char i_buf[MAX_STR];
	
}

unsigned char i2c_read(hid_device *device) {	// i2c read
	unsigned char o_buf[MAX_STR];
	unsigned char i_buf[MAX_STR];
	unsigned char a;
	return a;
}

unsigned char i2c_isdatardy(hid_device *device) {	// check if i2c char avail
	unsigned char o_buf[MAX_STR];
	unsigned char i_buf[MAX_STR];
	unsigned char a;
	return a;
}
/*
void spi_init(hid_device *device, mode, baud, sample) {	// SPI init
	unsigned char o_buf[MAX_STR];
	unsigned char i_buf[MAX_STR];
	
}

unsigned char spi_transfer(hid_device *device, value) {	// SPI transfer 
	unsigned char o_buf[MAX_STR];
	unsigned char i_buf[MAX_STR];
	
}

void spi_cs(hid_device *device, select) {	// enable or disable SPI CS
	unsigned char o_buf[MAX_STR];
	unsigned char i_buf[MAX_STR];
	
}
*/
#endif
Thank you for any answers :)

mlinuxguy
Posts: 807
Joined: Thu Feb 28, 2013 10:28 am
languages_spoken: english
ODROIDs: X, X2, XU, XU3, XU4, C1, C1+, C2, N1, USB-IO
Contact:

Re: access IO-Board in C

Unread post by mlinuxguy » Tue Nov 12, 2013 5:23 am

Please note that the reason printing a ROM version returns nothing is because the default ROM on the USB-IO board doesn't have that routine. To get the python code to work you need to flash it with my ROM.

Refer to this earlier post where I replied to a similar problem:
http://forum.odroid.com/viewtopic.php?f ... 142#p16477

Splicer
Posts: 4
Joined: Tue Nov 12, 2013 3:27 am
languages_spoken: english, german
ODROIDs: Odroid-XU, IO-Board
Contact:

Re: access IO-Board in C

Unread post by Splicer » Wed Nov 13, 2013 7:09 pm

Thank you for your quick answer. Im gonna try that.
But still i dont completly understand how this HID-Protocol thing with the IO-Board works.

Splicer
Posts: 4
Joined: Tue Nov 12, 2013 3:27 am
languages_spoken: english, german
ODROIDs: Odroid-XU, IO-Board
Contact:

Re: access IO-Board in C

Unread post by Splicer » Fri Nov 15, 2013 12:27 am

oK with the new hex file installed it works perfectly with python
i also checked the given and my c files an i think i do now understand how the protocol works
at least i got toe ROM version, the toggling function and the button function working
i might post the c-lib when or if its ready

Splicer
Posts: 4
Joined: Tue Nov 12, 2013 3:27 am
languages_spoken: english, german
ODROIDs: Odroid-XU, IO-Board
Contact:

Re: access IO-Board in C

Unread post by Splicer » Sun Nov 24, 2013 2:23 am

Ok I finished the C-Lib for the IO-Board.
feel free to use it.
it may not be perfect and maybe needs to be improved, but it should work basically.
to make it work you might have to install the latest firmware that was liked above.
Attachments
usb_io.h
Ported version of hk_usb_io.py as c file
(13.77 KiB) Downloaded 556 times

User avatar
odroid
Site Admin
Posts: 29711
Joined: Fri Feb 22, 2013 11:14 pm
languages_spoken: English
ODROIDs: ODROID
Contact:

Re: access IO-Board in C

Unread post by odroid » Sun Nov 24, 2013 12:23 pm

Great !
Thank you for the library.

pmousoul
Posts: 18
Joined: Wed Jun 04, 2014 9:34 pm
languages_spoken: english
ODROIDs: U3
Contact:

Re: access IO-Board in C

Unread post by pmousoul » Thu Jun 05, 2014 8:50 pm

Hello,

I'm new in this forum too.

I would like to ask how can I write C code for controlling from the odroid u3 the leds/switches and the pwm of the usbio board?

What are the functions I can use in C to accomplish that?

What C code I must use to have access to the registers of the usbio board in order to make it work as I would like?

For example I would like to use 4 leds, 4 switches and the pwm of the usbio board.

Do I have to change the usbio board firmware? Or it is possible to accomplish what I want just using the firmware provided?

It would be nice if there was a tutorial that would describe with examples how to get working in C the leds, switches, pwm, adc.. e.t.c. of the usbio board.

Is there such a tutorial?


Thank you very much for your time! :)

User avatar
odroid
Site Admin
Posts: 29711
Joined: Fri Feb 22, 2013 11:14 pm
languages_spoken: English
ODROIDs: ODROID
Contact:

Re: access IO-Board in C

Unread post by odroid » Thu Jun 05, 2014 9:17 pm

Refer this link first.
http://forum.odroid.com/viewtopic.php?f=87&t=4595

If you bought the USB IO board recently, it should have the latest firmware.

Post your questions on that forum I linked.

pmousoul
Posts: 18
Joined: Wed Jun 04, 2014 9:34 pm
languages_spoken: english
ODROIDs: U3
Contact:

Re: access IO-Board in C

Unread post by pmousoul » Thu Jun 05, 2014 9:21 pm

I bought the USB IO a month ago.

Thank you very much for the fast reply.

terrar
Posts: 19
Joined: Fri Dec 12, 2014 7:47 pm
languages_spoken: english, german
ODROIDs: C1
Contact:

Re: access IO-Board in C

Unread post by terrar » Tue Mar 03, 2015 12:19 am

sorry wrong thread

catan
Posts: 18
Joined: Thu Mar 05, 2015 1:16 am
languages_spoken: Italian
ODROIDs: Odroid C1
Contact:

Re: access IO-Board in C

Unread post by catan » Mon Mar 21, 2016 9:07 am

Hi guys,

someone played with the io-board ADC? I need a fast ADC (much more faster than the s805/s905 default adc). From the datasheet i see that the io-board adc is capable of work at 48mhz (means 1 sample every 41ns), did some one try it? There is some C code for use the dac and write the values on a file? Any help will be realy appreciate ;)

User avatar
odroid
Site Admin
Posts: 29711
Joined: Fri Feb 22, 2013 11:14 pm
languages_spoken: English
ODROIDs: ODROID
Contact:

Re: access IO-Board in C

Unread post by odroid » Mon Mar 21, 2016 12:07 pm

No. The ADC sampling rate is not related to the CPU clock.
Refer the datasheet on the PIC MCU.
http://www.microchip.com/wwwproducts/en/PIC18F45K50

Post Reply

Return to “Hardware and peripherals”

Who is online

Users browsing this forum: No registered users and 1 guest