Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
multipass
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
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
derf
multipass
Commits
8b71c58a
Commit
8b71c58a
authored
6 years ago
by
Birte Kristina Friesel
Browse files
Options
Downloads
Patches
Plain Diff
arduino-nano: i2c: Use idle waiting
parent
b2f7f066
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/arch/arduino-nano/driver/i2c.cc
+21
-18
21 additions, 18 deletions
src/arch/arduino-nano/driver/i2c.cc
with
21 additions
and
18 deletions
src/arch/arduino-nano/driver/i2c.cc
+
21
−
18
View file @
8b71c58a
#include
"driver/i2c.h"
#include
"driver/i2c.h"
#include
"arch.h"
#include
<avr/io.h>
#include
<avr/io.h>
#include
<avr/interrupt.h>
inline
void
await_twint
(
unsigned
char
twcr_values
)
{
TWCR
=
twcr_values
|
_BV
(
TWINT
)
|
_BV
(
TWIE
);
while
(
!
(
TWCR
&
_BV
(
TWINT
)))
{
arch
.
idle
();
}
}
/*
/*
* Send an I2C (re)start condition and the EEPROM address in read mode. Returns
* Send an I2C (re)start condition and the EEPROM address in read mode. Returns
...
@@ -7,16 +17,14 @@
...
@@ -7,16 +17,14 @@
*/
*/
static
signed
char
i2c_start_read
(
unsigned
char
addr
)
static
signed
char
i2c_start_read
(
unsigned
char
addr
)
{
{
TWCR
=
_BV
(
TWINT
)
|
_BV
(
TWSTA
)
|
_BV
(
TWEN
);
await_twint
(
_BV
(
TWSTA
)
|
_BV
(
TWEN
));
while
(
!
(
TWCR
&
_BV
(
TWINT
)));
if
(
!
(
TWSR
&
0x18
))
// 0x08 == START ok, 0x10 == RESTART ok
if
(
!
(
TWSR
&
0x18
))
// 0x08 == START ok, 0x10 == RESTART ok
return
-
1
;
return
-
1
;
// Note: The R byte ("... | 1") causes the TWI momodule to switch to
// Note: The R byte ("... | 1") causes the TWI momodule to switch to
// Master Receive mode
// Master Receive mode
TWDR
=
(
addr
<<
1
)
|
1
;
TWDR
=
(
addr
<<
1
)
|
1
;
TWCR
=
_BV
(
TWINT
)
|
_BV
(
TWEN
);
await_twint
(
_BV
(
TWEN
));
while
(
!
(
TWCR
&
_BV
(
TWINT
)));
if
(
TWSR
!=
0x40
)
// 0x40 == SLA+R transmitted, ACK receveid
if
(
TWSR
!=
0x40
)
// 0x40 == SLA+R transmitted, ACK receveid
return
-
2
;
return
-
2
;
...
@@ -29,14 +37,12 @@ static signed char i2c_start_read(unsigned char addr)
...
@@ -29,14 +37,12 @@ static signed char i2c_start_read(unsigned char addr)
*/
*/
static
signed
char
i2c_start_write
(
unsigned
char
addr
)
static
signed
char
i2c_start_write
(
unsigned
char
addr
)
{
{
TWCR
=
_BV
(
TWINT
)
|
_BV
(
TWSTA
)
|
_BV
(
TWEN
);
await_twint
(
_BV
(
TWSTA
)
|
_BV
(
TWEN
));
while
(
!
(
TWCR
&
_BV
(
TWINT
)));
if
(
!
(
TWSR
&
0x18
))
// 0x08 == START ok, 0x10 == RESTART ok
if
(
!
(
TWSR
&
0x18
))
// 0x08 == START ok, 0x10 == RESTART ok
return
-
1
;
return
-
1
;
TWDR
=
(
addr
<<
1
)
|
0
;
TWDR
=
(
addr
<<
1
)
|
0
;
TWCR
=
_BV
(
TWINT
)
|
_BV
(
TWEN
);
await_twint
(
_BV
(
TWEN
));
while
(
!
(
TWCR
&
_BV
(
TWINT
)));
if
(
TWSR
!=
0x18
)
// 0x18 == SLA+W transmitted, ACK received
if
(
TWSR
!=
0x18
)
// 0x18 == SLA+W transmitted, ACK received
return
-
2
;
return
-
2
;
...
@@ -61,8 +67,7 @@ static signed char i2c_send(uint8_t len, uint8_t *data)
...
@@ -61,8 +67,7 @@ static signed char i2c_send(uint8_t len, uint8_t *data)
for
(
pos
=
0
;
pos
<
len
;
pos
++
)
{
for
(
pos
=
0
;
pos
<
len
;
pos
++
)
{
TWDR
=
data
[
pos
];
TWDR
=
data
[
pos
];
TWCR
=
_BV
(
TWINT
)
|
_BV
(
TWEN
);
await_twint
(
_BV
(
TWEN
));
while
(
!
(
TWCR
&
_BV
(
TWINT
)));
if
(
TWSR
!=
0x28
)
// 0x28 == byte transmitted, ACK received
if
(
TWSR
!=
0x28
)
// 0x28 == byte transmitted, ACK received
return
pos
;
return
pos
;
}
}
...
@@ -79,14 +84,7 @@ static signed char i2c_receive(uint8_t len, uint8_t *data)
...
@@ -79,14 +84,7 @@ static signed char i2c_receive(uint8_t len, uint8_t *data)
uint8_t
pos
=
0
;
uint8_t
pos
=
0
;
for
(
pos
=
0
;
pos
<
len
;
pos
++
)
{
for
(
pos
=
0
;
pos
<
len
;
pos
++
)
{
if
(
pos
==
len
-
1
)
{
await_twint
(
_BV
(
TWEN
)
|
(
_BV
(
TWEA
)
*
(
pos
<
len
-
1
)
)
);
// Don't ACK the last byte
TWCR
=
_BV
(
TWINT
)
|
_BV
(
TWEN
);
}
else
{
// Automatically send ACK
TWCR
=
_BV
(
TWINT
)
|
_BV
(
TWEN
)
|
_BV
(
TWEA
);
}
while
(
!
(
TWCR
&
_BV
(
TWINT
)));
data
[
pos
]
=
TWDR
;
data
[
pos
]
=
TWDR
;
/*
/*
* No error handling here -- We send the acks, the EEPROM only
* No error handling here -- We send the acks, the EEPROM only
...
@@ -111,6 +109,7 @@ void I2C::scan(unsigned int *results)
...
@@ -111,6 +109,7 @@ void I2C::scan(unsigned int *results)
for
(
unsigned
char
address
=
0
;
address
<
128
;
address
++
)
{
for
(
unsigned
char
address
=
0
;
address
<
128
;
address
++
)
{
if
(
i2c_start_read
(
address
)
==
0
)
{
if
(
i2c_start_read
(
address
)
==
0
)
{
results
[
address
/
(
8
*
sizeof
(
unsigned
int
))]
|=
1
<<
(
address
%
(
8
*
sizeof
(
unsigned
int
)));
results
[
address
/
(
8
*
sizeof
(
unsigned
int
))]
|=
1
<<
(
address
%
(
8
*
sizeof
(
unsigned
int
)));
i2c_stop
();
}
}
}
}
i2c_stop
();
i2c_stop
();
...
@@ -144,3 +143,7 @@ signed char I2C::xmit(unsigned char address,
...
@@ -144,3 +143,7 @@ signed char I2C::xmit(unsigned char address,
}
}
I2C
i2c
;
I2C
i2c
;
ISR
(
TWI_vect
)
{
}
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