Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
70d329d
docs(hal-i2c): Add initial design document for the I2C HAL API overhaul
Aug 9, 2018
1f70325
docs(hal-i2c): Add modifications for timeout and multimaster support
Aug 20, 2018
6453cbc
docs(hal-i2c): Update the RFC to reflect removing the DMAUsage parameter
Sep 6, 2018
6d2162c
docs(hal-i2c): Add missing stop and last parameters to i2c_read/write
Sep 6, 2018
846658a
docs(hal-i2c): Move address parameter into a seperate i2c_slave function
Sep 6, 2018
52ca16c
docs(hal-i2c): Add additional defined function behaviours
Oct 2, 2018
28e4f05
docs(hal-i2c): Make additional amendments to I2C API specification
Oct 29, 2018
d2ed736
docs(hal-i2c): Add additional notes to synchronous read/write
Oct 29, 2018
b2db175
docs(hal-i2c): Add clock stretching field to target capabilities
Oct 29, 2018
4a88ec4
docs(hal-i2c): Add context pointer to async transfer function
Oct 29, 2018
76e954c
docs(hal-i2c): Fix return type of i2c_frequency function
Nov 5, 2018
0f3cba4
docs(hal-i2c): Move design document to the updated folder
Nov 14, 2018
280ffce
docs(hal-i2c): Update design document to match recent API alterations
Dec 20, 2018
459097e
feat(hal-i2c): Disable I2C on all targets in preparation
Oct 29, 2018
6a51a72
feat(hal-i2c): Update the HAL I2C header file to new API
Oct 29, 2018
c681eeb
feat(hal-i2c): Update I2C driver API to new specification
Oct 29, 2018
2118993
feat(hal-i2c): Add synchronous HAL I2C implementation for K64F
Oct 29, 2018
8322216
feat(hal-i2c): Add synchronous HAL I2C implementation for STM32
Oct 29, 2018
9b54bc0
feat(hal-i2c): Remove unused DMAUsage member from I2C class
Nov 5, 2018
79185d3
feat(hal-i2c): Add asynchronous HAL I2C implementation for STM32
Oct 17, 2018
b8fce70
fix(hal-i2c): Fix issue with master byte read/write failing on K64F
Nov 13, 2018
5722179
fix(hal-i2c): Add missing i2c_free function to API
Nov 13, 2018
0272127
fix(hal-i2c): Fix incorrect minimum frequency in STM I2C implementation
Nov 28, 2018
a7ce674
fix(hal-i2c): Fix STM implementation calling event handler too early
Nov 28, 2018
e837039
refactor(hal-i2c): Rename HAL slave status enum to match Driver API
Dec 3, 2018
2d0d952
fix(hal-i2c): Return the correct number of transferred bytes in STM32
Dec 3, 2018
1510f5a
fix(hal-i2c): Fix incorrect byte counts during aborted R/W transfers
Dec 3, 2018
42cd01c
refactor(hal-i2c): Change STOP function to return success or failure
Dec 5, 2018
e82a475
fix(hal-i2c): Add missing semicolon to STM32 I2C attribute 'tx_count'
Dec 5, 2018
16b41dd
fix(hal-i2c): STM: Return the num of bytes sent for incomplete transfers
Dec 5, 2018
75909df
refactor(hal-i2c): Remove return status from I2C start/stop functions
Dec 11, 2018
837cf18
fix(hal-i2c): STM: Fix frequency function checking the wrong variable
Dec 20, 2018
9c6f7fd
style(hal-i2c): STM: Remove empty comment block from code
Dec 20, 2018
1c26c91
fix(hal-i2c): STM: Add missing return statement when re-initing as slave
Jan 7, 2019
3887f85
refactor(hal-i2c): Add an API function to configure clock stretching
Jan 7, 2019
216b1f3
refactor(hal-i2c): Remove i2c_timeout function from the API
Jan 7, 2019
77e0cba
style(hal-i2c): Fix astyle code formatting failures
Jan 28, 2019
4bda684
fix(hal-i2c): K64F: Ignore calls to set slave address in master mode
Jan 30, 2019
c2390bb
fix(hal-i2c): Disable `I2CSLAVE` feature flag on K64F devices
Feb 25, 2019
19ae834
fix(hal-i2c): Return the num bytes transferred in STM slave read/write
Mar 4, 2019
51fc969
fix(hal-i2c): Disable slave mode and 10-bit from I2C capabilities on K64
Mar 4, 2019
c57fb11
fix(hal-i2c): Disable 10-bit addressing I2C capabilities on STM32
Mar 12, 2019
d150905
fix(hal-i2c): Fix compiler warning from unused variable on STM32
Mar 14, 2019
c1ba83f
fix(hal-i2c): Fix return value for transfers that timeout in STM
Mar 25, 2019
7d9de66
rebase fixes
maciejbocianski Mar 27, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
494 changes: 494 additions & 0 deletions docs/design-documents/hal/0001-i2c-overhaul.md

Large diffs are not rendered by default.

41 changes: 8 additions & 33 deletions drivers/I2C.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ SingletonPtr<PlatformMutex> I2C::_mutex;

I2C::I2C(PinName sda, PinName scl) :
#if DEVICE_I2C_ASYNCH
_irq(this), _usage(DMA_USAGE_NEVER), _deep_sleep_locked(false),
_irq(this), _deep_sleep_locked(false),
#endif
_i2c(), _hz(100000)
{
Expand All @@ -41,7 +41,9 @@ I2C::I2C(PinName sda, PinName scl) :
_sda = sda;
_scl = scl;
recover(sda, scl);
i2c_init(&_i2c, _sda, _scl);
i2c_init(&_i2c, sda, scl, false);
i2c_frequency(&_i2c, _hz);

// Used to avoid unnecessary frequency updates
_owner = this;
unlock();
Expand All @@ -50,7 +52,7 @@ I2C::I2C(PinName sda, PinName scl) :
void I2C::frequency(int hz)
{
lock();
_hz = hz;
_hz = (uint32_t)hz;

// We want to update the frequency even if we are already the bus owners
i2c_frequency(&_i2c, _hz);
Expand All @@ -77,7 +79,7 @@ int I2C::write(int address, const char *data, int length, bool repeated)
aquire();

int stop = (repeated) ? 0 : 1;
int written = i2c_write(&_i2c, address, data, length, stop);
int written = i2c_write(&_i2c, address, (void *)data, length, stop);

unlock();
return length != written;
Expand All @@ -86,7 +88,7 @@ int I2C::write(int address, const char *data, int length, bool repeated)
int I2C::write(int data)
{
lock();
int ret = i2c_byte_write(&_i2c, data);
int ret = i2c_write(&_i2c, 0, (void *)&data, 1, false);
unlock();
return ret;
}
Expand All @@ -108,11 +110,7 @@ int I2C::read(int ack)
{
lock();
int ret;
if (ack) {
ret = i2c_byte_read(&_i2c, 0);
} else {
ret = i2c_byte_read(&_i2c, 1);
}
i2c_read(&_i2c, 0, &ret, 1, (ack == 0));
unlock();
return ret;
}
Expand Down Expand Up @@ -191,40 +189,17 @@ int I2C::recover(PinName sda, PinName scl)

int I2C::transfer(int address, const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length, const event_callback_t &callback, int event, bool repeated)
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that shouldn't be empty!

lock();
if (i2c_active(&_i2c)) {
unlock();
return -1; // transaction ongoing
}
lock_deep_sleep();
aquire();

_callback = callback;
int stop = (repeated) ? 0 : 1;
_irq.callback(&I2C::irq_handler_asynch);
i2c_transfer_asynch(&_i2c, (void *)tx_buffer, tx_length, (void *)rx_buffer, rx_length, address, stop, _irq.entry(), event, _usage);
unlock();
return 0;
}

void I2C::abort_transfer(void)
{
lock();
i2c_abort_asynch(&_i2c);
unlock_deep_sleep();
unlock();
}

void I2C::irq_handler_asynch(void)
{
int event = i2c_irq_handler_asynch(&_i2c);
if (_callback && event) {
_callback.call(event);
}

if (event) {
unlock_deep_sleep();
}
}

void I2C::lock_deep_sleep()
Expand Down
3 changes: 1 addition & 2 deletions drivers/I2C.h
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,6 @@ class I2C : private NonCopyable<I2C> {
void irq_handler_asynch(void);
event_callback_t _callback;
CThunk<I2C> _irq;
DMAUsage _usage;
bool _deep_sleep_locked;
#endif
#endif
Expand All @@ -221,7 +220,7 @@ class I2C : private NonCopyable<I2C> {

i2c_t _i2c;
static I2C *_owner;
int _hz;
uint32_t _hz;
static SingletonPtr<PlatformMutex> _mutex;
PinName _sda;
PinName _scl;
Expand Down
23 changes: 13 additions & 10 deletions drivers/I2CSlave.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ namespace mbed {

I2CSlave::I2CSlave(PinName sda, PinName scl) : _i2c()
{
i2c_init(&_i2c, sda, scl);
i2c_init(&_i2c, sda, scl, true);
i2c_frequency(&_i2c, 100000);
i2c_slave_mode(&_i2c, 1);
}

void I2CSlave::frequency(int hz)
Expand All @@ -35,39 +34,43 @@ void I2CSlave::frequency(int hz)
void I2CSlave::address(int address)
{
int addr = (address & 0xFF) | 1;
i2c_slave_address(&_i2c, 0, addr, 0);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The line:
int addr = (address & 0xFF) | 1;
breaks 10bit addressing

i2c_slave_address(&_i2c, addr);
}

int I2CSlave::receive(void)
{
return i2c_slave_receive(&_i2c);
return i2c_slave_status(&_i2c);
}

int I2CSlave::read(char *data, int length)
{
return i2c_slave_read(&_i2c, data, length) != length;
return i2c_read(&_i2c, 0, data, length, false) != length;
}

int I2CSlave::read(void)
{
return i2c_byte_read(&_i2c, 0);
int ret;
i2c_read(&_i2c, 0, &ret, 1, false);

return ret;
}

int I2CSlave::write(const char *data, int length)
{
return i2c_slave_write(&_i2c, data, length) != length;
return i2c_write(&_i2c, 0, data, length, false) != length;
}

int I2CSlave::write(int data)
{
return i2c_byte_write(&_i2c, data);
return i2c_write(&_i2c, 0, (void *)&data, 1, false);
}

void I2CSlave::stop(void)
{
i2c_stop(&_i2c);
}

}
} // namespace mbed

#endif
#endif // DEVICE_I2CSLAVE
2 changes: 2 additions & 0 deletions drivers/I2CSlave.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ class I2CSlave {
#if !defined(DOXYGEN_ONLY)

protected:
PinName _sda;
PinName _scl;
/* Internal i2c object identifying the resources */
i2c_t _i2c;

Expand Down
Loading