Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
P
Programmable RF Attenuator
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
1
Issues
1
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
image/svg+xml
Discourse
Discourse
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Projects
Programmable RF Attenuator
Commits
d7d1966a
Commit
d7d1966a
authored
Dec 17, 2021
by
Juan David González Cobas
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
do usb reset directly via libusb
parent
5c5b406a
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
2 additions
and
215 deletions
+2
-215
pp.c
software/lib/pp.c
+2
-215
No files found.
software/lib/pp.c
View file @
d7d1966a
...
...
@@ -23,190 +23,6 @@
#define OAU_NUM_CHANNELS 4
const
char
*
const
liboau_att_version_s
=
"liboasis-usb-att version: "
;
// GIT_VERSION;
struct
oau_context
{
struct
ftdi_context
*
ftdi
;
uint8_t
relay_state
;
};
static
int
oau_current_handle
=
0
;
static
struct
oau_context
oau_contexts
[
OAU_MAX_HANDLES
];
static
struct
{
int
r1
,
r2
;
}
oau_relay_mapping
[
OAU_NUM_CHANNELS
]
=
{
{
0
,
1
},
{
2
,
3
},
{
4
,
5
},
{
6
,
7
}
};
int
oau_att_open
(
int
lun
)
{
int
ret
;
if
(
oau_current_handle
>=
OAU_MAX_HANDLES
)
return
-
ENOMEM
;
struct
oau_context
*
ctx
=
&
oau_contexts
[
oau_current_handle
];
int
fd
=
oau_current_handle
;
oau_current_handle
++
;
if
((
ctx
->
ftdi
=
ftdi_new
())
==
0
)
{
return
-
ENOMEM
;
}
printf
(
"Open device: %x:%x
\n
"
,
OAU_VENDOR_ID
,
OAU_DEVICE_ID
);
if
((
ret
=
ftdi_usb_open
(
ctx
->
ftdi
,
OAU_VENDOR_ID
,
OAU_DEVICE_ID
))
<
0
)
{
printf
(
"Open failed
\n
"
);
ftdi_free
(
ctx
->
ftdi
);
return
ret
;
}
if
((
ret
=
ftdi_set_interface
(
ctx
->
ftdi
,
INTERFACE_A
))
<
0
)
{
printf
(
"Set iface failed
\n
"
);
ftdi_usb_close
(
ctx
->
ftdi
);
ftdi_free
(
ctx
->
ftdi
);
return
ret
;
}
if
((
ret
=
ftdi_set_bitmode
(
ctx
->
ftdi
,
0xff
,
BITMODE_BITBANG
))
<
0
)
{
printf
(
"Set bitmode failed
\n
"
);
ftdi_usb_close
(
ctx
->
ftdi
);
ftdi_free
(
ctx
->
ftdi
);
return
ret
;
}
uint8_t
pins
;
if
((
ret
=
ftdi_read_pins
(
ctx
->
ftdi
,
&
pins
))
<
0
)
{
printf
(
"Read pins failed
\n
"
);
ftdi_usb_close
(
ctx
->
ftdi
);
ftdi_free
(
ctx
->
ftdi
);
return
ret
;
}
ctx
->
relay_state
=
pins
;
return
fd
;
}
int
oau_att_close
(
int
fd
)
{
struct
oau_context
*
ctx
=
&
oau_contexts
[
fd
];
int
ret
;
if
((
ret
=
ftdi_usb_close
(
ctx
->
ftdi
))
<
0
)
{
return
ret
;
}
ftdi_free
(
ctx
->
ftdi
);
return
0
;
}
enum
oau_att_value_enum
oau_att_get_relay
(
int
fd
,
int
channel
)
{
struct
oau_context
*
ctx
=
&
oau_contexts
[
fd
];
if
(
channel
<
1
||
channel
>
OAU_NUM_CHANNELS
)
return
-
EINVAL
;
uint8_t
r1_mask
=
1
<<
oau_relay_mapping
[
channel
-
1
].
r1
;
uint8_t
r2_mask
=
1
<<
oau_relay_mapping
[
channel
-
1
].
r2
;
int
r1
=
(
ctx
->
relay_state
&
r1_mask
);
int
r2
=
(
ctx
->
relay_state
&
r2_mask
);
if
(
r1
&&
!
r2
)
return
ATT_1
;
else
if
(
r1
&&
r2
)
return
ATT_NONE
;
else
if
(
!
r1
&&
!
r2
)
return
ATT_2
;
return
ATT_ERR
;
}
int
oau_att_set_relay
(
int
fd
,
int
channel
,
enum
oau_att_value_enum
val
)
{
struct
oau_context
*
ctx
=
&
oau_contexts
[
fd
];
fprintf
(
stderr
,
"att_set_delay fd %d chan %d val %d
\n
"
,
fd
,
channel
,
val
);
if
(
channel
<
1
||
channel
>
OAU_NUM_CHANNELS
)
return
-
EINVAL
;
uint8_t
r1_mask
=
1
<<
oau_relay_mapping
[
channel
-
1
].
r1
;
uint8_t
r2_mask
=
1
<<
oau_relay_mapping
[
channel
-
1
].
r2
;
switch
(
val
)
{
case
ATT_2
:
ctx
->
relay_state
&=
~
(
r1_mask
|
r2_mask
);
break
;
case
ATT_1
:
ctx
->
relay_state
&=
~
r2_mask
;
ctx
->
relay_state
|=
r1_mask
;
break
;
case
ATT_NONE
:
ctx
->
relay_state
|=
r1_mask
;
ctx
->
relay_state
|=
r2_mask
;
break
;
default:
return
-
EINVAL
;
}
printf
(
"RState %x
\n
"
,
ctx
->
relay_state
);
int
ret
=
ftdi_write_data
(
ctx
->
ftdi
,
&
ctx
->
relay_state
,
1
);
if
(
ret
<
0
)
return
-
EIO
;
uint8_t
pins
;
ftdi_read_pins
(
ctx
->
ftdi
,
&
pins
);
printf
(
"RState2 %x
\n
"
,
pins
);
return
0
;
}
int
oau_att_get_nchannels
(
int
fd
)
{
return
OAU_NUM_CHANNELS
;
}
int
fix_eeprom_crc
(
unsigned
char
*
output
,
int
size
)
{
unsigned
short
checksum
,
value
;
int
i
;
/* calculate checksum */
checksum
=
0xAAAA
;
for
(
i
=
0
;
i
<
size
/
2
-
1
;
i
++
)
{
value
=
output
[
i
*
2
];
value
+=
output
[(
i
*
2
)
+
1
]
<<
8
;
checksum
=
value
^
checksum
;
checksum
=
(
checksum
<<
1
)
|
(
checksum
>>
15
);
}
output
[
size
-
2
]
=
checksum
;
output
[
size
-
1
]
=
checksum
>>
8
;
}
static
usbdev_reset
(
unsigned
int
bus
,
unsigned
int
dev
);
static
void
eeprom_backup
(
unsigned
char
*
eeprom_raw
,
int
sz
)
{
static
char
fname
[
64
];
...
...
@@ -278,7 +94,8 @@ int oau_factory_program_eeprom(unsigned int bus, unsigned int dev, char *serial)
if
(
1
)
{
ret
=
ftdi_write_eeprom
(
ctx
);
printf
(
"ftdi_write_eeprom returned %d
\n
"
,
ret
);
ret
=
usbdev_reset
(
bus
,
dev
);
//ret = usbdev_reset(bus, dev);
ret
=
libusb_reset_device
(
ctx
->
usb_dev
);
}
ftdi_free
(
ctx
);
...
...
@@ -298,33 +115,3 @@ int main(int argc, char *argv[])
ret
=
oau_factory_program_eeprom
(
bus
,
dev
,
serial
);
return
ret
;
}
#include <unistd.h>
#include <fcntl.h>
#include <linux/usbdevice_fs.h>
#include <sys/ioctl.h>
static
usbdev_reset
(
unsigned
int
bus
,
unsigned
int
dev
)
{
char
devtemplate
[]
=
"/dev/bus/usb/%03d/%03d"
;
char
filename
[
sizeof
(
devtemplate
)];
int
fd
;
int
rc
;
snprintf
(
filename
,
sizeof
(
filename
),
devtemplate
,
bus
,
dev
);
fd
=
open
(
filename
,
O_WRONLY
);
if
(
fd
<
0
)
{
perror
(
"Error opening output file"
);
return
1
;
}
printf
(
"Resetting USB device %s
\n
"
,
filename
);
rc
=
ioctl
(
fd
,
USBDEVFS_RESET
,
0
);
if
(
rc
<
0
)
{
perror
(
"Error in ioctl"
);
return
1
;
}
printf
(
"Reset successful
\n
"
);
close
(
fd
);
return
0
;
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment