Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
E
e7020e_2021
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Edward Källstedt
e7020e_2021
Commits
ad8bf117
Commit
ad8bf117
authored
4 years ago
by
Per Lindgren
Browse files
Options
Downloads
Patches
Plain Diff
test of i2c->spi wip5, read product + version
parent
29635dab
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
examples/rtt_rtic_i2c.rs
+74
-20
74 additions, 20 deletions
examples/rtt_rtic_i2c.rs
with
74 additions
and
20 deletions
examples/rtt_rtic_i2c.rs
+
74
−
20
View file @
ad8bf117
...
@@ -72,30 +72,62 @@ const APP: () = {
...
@@ -72,30 +72,62 @@ const APP: () = {
(
0b00
<<
0
)
/* 1843 kHz */
;
(
0b00
<<
0
)
/* 1843 kHz */
;
// (0b01 << 0) /* 461 kHz */;
// (0b01 << 0) /* 461 kHz */;
let
x
=
i2c
.write
(
i2c_addr
,
&
[
i2c_command_conf
,
i2c_conf_reg
]);
let
x
=
i2c
.write
(
i2c_addr
,
&
[
i2c_command_conf
,
i2c_conf_reg
])
.unwrap
();
rprintln!
(
"configure {:?}"
,
x
);
rprintln!
(
"configure {:?}"
,
x
);
cortex_m
::
asm
::
delay
(
100_000
);
//
cortex_m::asm::delay(10
_00
0_000);
// write to spi with CS0 (command 01..0f)
// write to spi with CS0 (command 01..0f)
let
i2c_command_cs0
=
0x01
;
// bit 0 set
let
i2c_command_cs0
=
0x01
;
// bit 0 set
let
pmw_command_product_id
=
0x00
;
let
pmw_command_product_id
=
0x00
;
let
pmw_command_product_version
=
0x01
;
let
x
=
i2c
.write
(
i2c_addr
,
&
[
i2c_command_cs0
,
pmw_command_product_id
]);
let
x
=
i2c
.write
(
i2c_addr
,
&
[
i2c_command_cs0
,
pmw_command_product_id
,
0
]);
rprintln!
(
"request product_id {:?}"
,
x
);
rprintln!
(
"request product_id {:?}"
,
x
);
cortex_m
::
asm
::
delay
(
10000
);
// cortex_m::asm::delay(10_000_000);
// read the result
let
mut
buff
=
[
0
,
0
,
0
,
0
];
rprintln!
(
"buff {:?}"
,
buff
);
// send an extra byte, to actually read (data can be 0)
let
x
=
i2c
.read
(
i2c_addr
,
&
mut
buff
);
let
x
=
i2c
.write
(
i2c_addr
,
&
[
i2c_command_cs0
,
0x00u8
]);
// read the buffer
// read the buffer
cortex_m
::
asm
::
delay
(
10000
);
cortex_m
::
asm
::
delay
(
100_000
);
rprintln!
(
"data received {:?}"
,
x
);
rprintln!
(
"data received {:?}"
,
buff
);
let
x
=
i2c
.write
(
i2c_addr
,
&
[
i2c_command_cs0
,
pmw_command_product_version
,
0
]);
rprintln!
(
"request product_version {:?}"
,
x
);
// cortex_m::asm::delay(10_000_000);
// read the result
// read the result
let
mut
buff
=
[
0u8
,
1
];
let
mut
buff
=
[
0
,
0
,
0
,
0
];
rprintln!
(
"buff {:?}"
,
buff
);
let
x
=
i2c
.read
(
i2c_addr
,
&
mut
buff
);
let
x
=
i2c
.read
(
i2c_addr
,
&
mut
buff
);
// read the buffer
// read the buffer
cortex_m
::
asm
::
delay
(
10000
);
cortex_m
::
asm
::
delay
(
100
_0
00
);
rprintln!
(
"data received {:?}"
,
x
);
rprintln!
(
"data received {:?}"
,
x
);
rprintln!
(
"data received {:?}"
,
buff
);
rprintln!
(
"data received {:?}"
,
buff
);
// // test of the abstractions
// use embedded_hal::spi::MODE_3;
// use SC18IS602::{Order, Speed, SH18IS602};
// let mut spi_emu =
// SH18IS602::new(i2c, 0, Order::MsbFirst, MODE_3, Speed::Speed1843kHz, false);
// rprintln!("spi_emu initialized");
// let mut id_request = [0x00];
// spi_emu.transfer(&mut id_request).unwrap();
// rprintln!("id_request {:?}", id_request);
// let mut id_request = [0x00];
// spi_emu.transfer(&mut id_request).unwrap();
// rprintln!("response {:?}", id_request);
}
}
#[idle]
#[idle]
...
@@ -127,10 +159,10 @@ mod SC18IS602 {
...
@@ -127,10 +159,10 @@ mod SC18IS602 {
}
}
pub
enum
Speed
{
pub
enum
Speed
{
Speed
_
1843
_
kHz
=
0b00
,
Speed1843kHz
=
0b00
,
Speed
_
461
_
kHz
=
0b01
,
Speed461kHz
=
0b01
,
Speed
_
115
_
kHz
=
0b10
,
Speed115kHz
=
0b10
,
Speed
_
58
_
kHz
=
0b11
,
Speed58kHz
=
0b11
,
}
}
pub
enum
Order
{
pub
enum
Order
{
...
@@ -157,26 +189,30 @@ mod SC18IS602 {
...
@@ -157,26 +189,30 @@ mod SC18IS602 {
spi
::
Mode
,
spi
::
Mode
,
};
};
enum
Error
{
#[derive(Copy,
Clone,
Debug)]
pub
enum
Error
{
NotConfigured
,
NotConfigured
,
}
}
struct
SH18IS602
<
I2C
>
pub
struct
SH18IS602
<
I2C
>
where
where
I2C
:
i2c
::
Write
+
i2c
::
Read
,
I2C
:
i2c
::
Write
+
i2c
::
Read
,
{
{
addr
:
u8
,
addr
:
u8
,
cs
:
bool
,
cs
:
bool
,
i2c
:
I2C
,
i2c
:
I2C
,
// a backing buffer for shadowing SPI transfers
buff
:
[
u8
;
200
],
buff
:
[
u8
;
200
],
}
}
use
rtt_target
::
rprintln
;
use
Function
::
*
;
use
Function
::
*
;
impl
<
I2C
>
SH18IS602
<
I2C
>
impl
<
I2C
>
SH18IS602
<
I2C
>
where
where
I2C
:
i2c
::
Write
+
i2c
::
Read
,
I2C
:
i2c
::
Write
+
i2c
::
Read
,
{
{
fn
new
(
pub
fn
new
(
mut
i2c
:
I2C
,
mut
i2c
:
I2C
,
addr
:
u8
,
addr
:
u8
,
order
:
Order
,
order
:
Order
,
...
@@ -191,16 +227,22 @@ mod SC18IS602 {
...
@@ -191,16 +227,22 @@ mod SC18IS602 {
|
(
mode
.phase
as
u8
)
<<
2
|
(
mode
.phase
as
u8
)
<<
2
|
speed
as
u8
;
|
speed
as
u8
;
let
x
=
i2c
.write
(
addr
,
&
[
SpiConfigure
.id
(),
cfg
])
.ok
();
i2c
.write
(
addr
,
&
[
SpiConfigure
.id
(),
cfg
])
.map_err
(|
_
|
panic!
())
.ok
();
cortex_m
::
asm
::
delay
(
100_000
);
cortex_m
::
asm
::
delay
(
100_000
);
if
cs
{
if
cs
{
rprintln!
(
"GPIO SS0"
);
// Configure SS0 as GPIO
// Configure SS0 as GPIO
i2c
.write
(
addr
,
&
[
GpioEnable
.id
(),
0x1
])
.ok
();
i2c
.write
(
addr
,
&
[
GpioEnable
.id
(),
0x1
])
.map_err
(|
_
|
panic!
())
.ok
();
// Configure GPIO SS0 as a PushPull Output
// Configure GPIO SS0 as a PushPull Output
i2c
.write
(
addr
,
&
[
GpioConfigure
.id
(),
GpioMode
::
PushPull
.val
()])
i2c
.write
(
addr
,
&
[
GpioConfigure
.id
(),
GpioMode
::
PushPull
.val
()])
.map_err
(|
_
|
panic!
())
.ok
();
.ok
();
}
}
SH18IS602
{
SH18IS602
{
...
@@ -223,13 +265,23 @@ mod SC18IS602 {
...
@@ -223,13 +265,23 @@ mod SC18IS602 {
fn
transfer
<
'w
>
(
&
mut
self
,
words
:
&
'w
mut
[
u8
])
->
Result
<&
'w
[
u8
],
Self
::
Error
>
{
fn
transfer
<
'w
>
(
&
mut
self
,
words
:
&
'w
mut
[
u8
])
->
Result
<&
'w
[
u8
],
Self
::
Error
>
{
// initiate a transfer on SS0
// initiate a transfer on SS0
self
.buff
[
0
]
=
0x01
;
// SSO write
self
.buff
[
0
]
=
0x01
;
// SSO write
self
.buff
[
1
..
]
.clone_from_slice
(
words
);
self
.buff
[
1
..
words
.len
()
+
1
]
.clone_from_slice
(
words
);
// perform the transaction on words.len() + 1 bytes
// perform the transaction on words.len() + 1 bytes
// the actual SPI transfer should be words.len()
// the actual SPI transfer should be words.len()
rprintln!
(
"write {:?}"
,
&
self
.buff
[
0
..
words
.len
()
+
1
]);
self
.i2c
self
.i2c
.write
(
self
.addr
,
&
self
.buff
[
0
..
words
.len
()
+
1
])
.write
(
self
.addr
,
&
self
.buff
[
0
..
words
.len
()
+
1
])
.map_err
(|
_
|
panic!
())
.ok
();
.ok
();
self
.i2c
.read
(
self
.addr
,
words
)
.ok
();
cortex_m
::
asm
::
delay
(
100_000
);
self
.i2c
.read
(
self
.addr
,
words
)
.map_err
(|
_
|
panic!
())
.ok
();
cortex_m
::
asm
::
delay
(
100_000
);
rprintln!
(
"read {:?}"
,
words
);
Ok
(
words
)
Ok
(
words
)
}
}
}
}
...
@@ -246,6 +298,7 @@ mod SC18IS602 {
...
@@ -246,6 +298,7 @@ mod SC18IS602 {
}
else
{
}
else
{
self
.i2c
self
.i2c
.write
(
self
.addr
,
&
[
Function
::
GpioWrite
.id
(),
0x0
])
.write
(
self
.addr
,
&
[
Function
::
GpioWrite
.id
(),
0x0
])
.map_err
(|
_
|
panic!
())
.ok
();
.ok
();
Ok
(())
Ok
(())
}
}
...
@@ -257,6 +310,7 @@ mod SC18IS602 {
...
@@ -257,6 +310,7 @@ mod SC18IS602 {
}
else
{
}
else
{
self
.i2c
self
.i2c
.write
(
self
.addr
,
&
[
Function
::
GpioWrite
.id
(),
0x1
])
.write
(
self
.addr
,
&
[
Function
::
GpioWrite
.id
(),
0x1
])
.map_err
(|
_
|
panic!
())
.ok
();
.ok
();
Ok
(())
Ok
(())
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment