Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
M
micropython
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
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
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
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
card10
micropython
Commits
3b72da67
Commit
3b72da67
authored
10 years ago
by
Damien George
Browse files
Options
Downloads
Patches
Plain Diff
stmhal, STM32F4DISC: Small changes to ST accel driver.
parent
6cf8dd4f
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
stmhal/boards/STM32F4DISC/staccel.py
+27
-17
27 additions, 17 deletions
stmhal/boards/STM32F4DISC/staccel.py
with
27 additions
and
17 deletions
stmhal/boards/STM32F4DISC/staccel.py
100755 → 100644
+
27
−
17
View file @
3b72da67
...
@@ -24,36 +24,38 @@ LIS302DL_WHO_AM_I_ADDR = const(0x0f)
...
@@ -24,36 +24,38 @@ LIS302DL_WHO_AM_I_ADDR = const(0x0f)
LIS302DL_WHO_AM_I_VAL
=
const
(
0x3b
)
LIS302DL_WHO_AM_I_VAL
=
const
(
0x3b
)
LIS302DL_CTRL_REG1_ADDR
=
const
(
0x20
)
LIS302DL_CTRL_REG1_ADDR
=
const
(
0x20
)
LIS302DL_OUT_X
=
const
(
0x29
)
LIS302DL_OUT_X
=
const
(
0x29
)
LIS302DL_OUT_Y
=
const
(
0x2b
)
LIS302DL_OUT_Z
=
const
(
0x2d
)
# Configuration for 100Hz sampling rate, +-2g range
# Configuration for 100Hz sampling rate, +-2g range
LIS302DL_CONF
=
0b01000111
LIS302DL_CONF
=
const
(
0b01000111
)
def
signed8
(
x
):
def
convert_raw_to_g
(
x
):
if
x
&
0x80
:
if
x
&
0x80
:
return
x
-
256
x
=
x
-
256
else
:
return
x
*
18
/
1000
return
x
class
STAccel
:
class
STAccel
:
def
__init__
(
self
):
def
__init__
(
self
):
self
.
cs_pin
=
Pin
(
'
PE3
'
,
Pin
.
OUT_PP
,
Pin
.
PULL_NONE
)
self
.
cs_pin
=
Pin
(
'
PE3
'
,
Pin
.
OUT_PP
,
Pin
.
PULL_NONE
)
self
.
cs_pin
.
high
()
self
.
cs_pin
.
high
()
self
.
spi
=
SPI
(
1
,
SPI
.
MASTER
,
baudrate
=
328125
,
polarity
=
0
,
phase
=
1
,
bits
=
8
)
self
.
spi
=
SPI
(
1
,
SPI
.
MASTER
,
baudrate
=
328125
,
polarity
=
0
,
phase
=
1
,
bits
=
8
)
self
.
wr
(
LIS302DL_CTRL_REG1_ADDR
,
bytearray
([
LIS302DL_CONF
]))
self
.
wr
ite_bytes
(
LIS302DL_CTRL_REG1_ADDR
,
bytearray
([
LIS302DL_CONF
]))
if
self
.
read_id
()
[
0
]
!=
LIS302DL_WHO_AM_I_VAL
:
if
self
.
read_id
()
!=
LIS302DL_WHO_AM_I_VAL
:
raise
Exception
(
'
LIS302DL accelerometer not present
'
)
raise
Exception
(
'
LIS302DL accelerometer not present
'
)
def
r
d
(
self
,
addr
,
nbytes
):
def
r
ead_bytes
(
self
,
addr
,
nbytes
):
if
nbytes
>
1
:
if
nbytes
>
1
:
addr
|=
READWRITE_CMD
|
MULTIPLEBYTE_CMD
addr
|=
READWRITE_CMD
|
MULTIPLEBYTE_CMD
else
:
else
:
addr
|=
READWRITE_CMD
addr
|=
READWRITE_CMD
self
.
cs_pin
.
low
()
self
.
cs_pin
.
low
()
self
.
spi
.
send
(
addr
)
self
.
spi
.
send
(
addr
)
buf
=
self
.
spi
.
send_recv
(
bytearray
(
nbytes
*
[
0
]))
# read data, MSB first
#buf = self.spi.send_recv(bytearray(nbytes * [0])) # read data, MSB first
buf
=
self
.
spi
.
recv
(
nbytes
)
self
.
cs_pin
.
high
()
self
.
cs_pin
.
high
()
return
buf
return
buf
def
wr
(
self
,
addr
,
buf
):
def
wr
ite_bytes
(
self
,
addr
,
buf
):
if
len
(
buf
)
>
1
:
if
len
(
buf
)
>
1
:
addr
|=
MULTIPLEBYTE_CMD
addr
|=
MULTIPLEBYTE_CMD
self
.
cs_pin
.
low
()
self
.
cs_pin
.
low
()
...
@@ -63,11 +65,19 @@ class STAccel:
...
@@ -63,11 +65,19 @@ class STAccel:
self
.
cs_pin
.
high
()
self
.
cs_pin
.
high
()
def
read_id
(
self
):
def
read_id
(
self
):
return
self
.
rd
(
LIS302DL_WHO_AM_I_ADDR
,
1
)
return
self
.
read_bytes
(
LIS302DL_WHO_AM_I_ADDR
,
1
)[
0
]
def
x
(
self
):
return
convert_raw_to_g
(
self
.
read_bytes
(
LIS302DL_OUT_X
,
1
)[
0
])
def
y
(
self
):
return
convert_raw_to_g
(
self
.
read_bytes
(
LIS302DL_OUT_Y
,
1
)[
0
])
def
z
(
self
):
return
convert_raw_to_g
(
self
.
read_bytes
(
LIS302DL_OUT_Z
,
1
)[
0
])
def
get_xyz
(
self
):
def
xyz
(
self
):
val
=
self
.
rd
(
LIS302DL_OUT_X
,
5
)
val
=
self
.
read_bytes
(
LIS302DL_OUT_X
,
5
)
x
=
signed8
(
val
[
0
])
*
18.0
/
1000
return
[
convert_raw_to_g
(
val
[
0
]),
y
=
signed8
(
val
[
2
])
*
18.0
/
1000
convert_raw_to_g
(
val
[
2
]),
z
=
signed8
(
val
[
4
])
*
18.0
/
1000
convert_raw_to_g
(
val
[
4
])]
return
[
x
,
y
,
z
]
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