Skip to content

I2C update following new STM32F1 HAL release #79

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from Aug 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
62 changes: 0 additions & 62 deletions cores/arduino/stm32/twi.c
Original file line number Diff line number Diff line change
Expand Up @@ -427,12 +427,7 @@ void i2c_attachSlaveRxEvent(i2c_t *obj, void (*function)(uint8_t*, int) )
return;

obj->i2c_onSlaveReceive = function;
#ifdef STM32F1xx
obj->i2cTxRxBufferSize = 0;
HAL_I2C_Slave_Receive_IT(&(obj->handle), obj->i2cTxRxBuffer, I2C_TXRX_BUFFER_SIZE);
#else
HAL_I2C_EnableListen_IT(&(obj->handle));
#endif
}

/** @brief sets function called before a slave write operation
Expand All @@ -446,60 +441,9 @@ void i2c_attachSlaveTxEvent(i2c_t *obj, void (*function)(void) )
return;

obj->i2c_onSlaveTransmit = function;
#ifdef STM32F1xx
/* Fill i2c buffer with data to transmit otherwize the buffer will be empty
when master will read the data for the first time */
obj->i2cTxRxBufferSize = 0;
obj->i2c_onSlaveTransmit();
HAL_I2C_Slave_Transmit_IT(&(obj->handle), obj->i2cTxRxBuffer, obj->i2cTxRxBufferSize);
#else
HAL_I2C_EnableListen_IT(&(obj->handle));
#endif
}

#ifdef STM32F1xx

/** @brief Slave Tx Transfer completed callback.
* @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
* the configuration information for the specified I2C.
* @retval None
*/
void HAL_I2C_SlaveTxCpltCallback(I2C_HandleTypeDef *hi2c)
{
i2c_t *obj = get_i2c_obj(hi2c);

if(NULL != obj->i2c_onSlaveTransmit) {
// reset buffer size and fill buffer with new data before the next Tx
obj->i2cTxRxBufferSize = 0;
obj->i2c_onSlaveTransmit();
HAL_I2C_Slave_Transmit_IT(hi2c, obj->i2cTxRxBuffer, obj->i2cTxRxBufferSize);
}
}

/**
* @brief Slave Rx Transfer completed callback.
* @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
* the configuration information for the specified I2C.
* @retval None
*/
void HAL_I2C_SlaveRxCpltCallback(I2C_HandleTypeDef *hi2c)
{
uint8_t nbData = 0;
i2c_t *obj = get_i2c_obj(hi2c);

if(NULL != obj->i2c_onSlaveReceive) {
nbData = I2C_TXRX_BUFFER_SIZE - obj->handle.XferCount;

if(nbData != 0) {
obj->i2c_onSlaveReceive(obj->i2cTxRxBuffer, nbData);
}

HAL_I2C_Slave_Receive_IT(hi2c, obj->i2cTxRxBuffer, I2C_TXRX_BUFFER_SIZE);
}
}

#else /* Others */

/**
* @brief Slave Address Match callback.
* @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
Expand Down Expand Up @@ -552,8 +496,6 @@ void HAL_I2C_ListenCpltCallback(I2C_HandleTypeDef *hi2c)
HAL_I2C_EnableListen_IT(hi2c);
}

#endif /* STM32F1xx */

/**
* @brief I2C error callback.
* @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
Expand All @@ -562,11 +504,7 @@ void HAL_I2C_ListenCpltCallback(I2C_HandleTypeDef *hi2c)
*/
void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *hi2c)
{
#ifdef STM32F1xx
UNUSED(hi2c);
#else
HAL_I2C_EnableListen_IT(hi2c);
#endif
}

/**
Expand Down
18 changes: 0 additions & 18 deletions libraries/Wire/Wire.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,13 @@ void TwoWire::begin(uint8_t address)

i2c_custom_init(&_i2c,I2C_100KHz,I2C_ADDRESSINGMODE_7BIT,ownAddress,master);

#ifndef STM32F1xx
if(master == false){
// i2c_attachSlaveTxEvent(&_i2c, reinterpret_cast<void(*)(i2c_t*)>(&TwoWire::onRequestService));
// i2c_attachSlaveRxEvent(&_i2c, reinterpret_cast<void(*)(i2c_t*, uint8_t*, int)>(&TwoWire::onReceiveService));

i2c_attachSlaveTxEvent(&_i2c, onRequestService);
i2c_attachSlaveRxEvent(&_i2c, onReceiveService);
}
#endif
}

void TwoWire::begin(int address)
Expand All @@ -111,8 +109,6 @@ uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity, uint32_t iaddres
{
UNUSED(sendStop);
if (master == true) {
//NOTE: do not work with STM32F1xx boards (latest HAL version: 1.0.4)
#ifndef STM32F1xx
if (isize > 0) {
// send internal address; this mode allows sending a repeated start to access
// some devices' internal registers. This function is executed by the hardware
Expand All @@ -131,10 +127,6 @@ uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity, uint32_t iaddres
}
endTransmission(false);
}
#else
UNUSED(iaddress);
UNUSED(isize);
#endif /* !STM32F1xx */

// clamp to buffer length
if(quantity > BUFFER_LENGTH){
Expand Down Expand Up @@ -377,22 +369,12 @@ void TwoWire::onRequestService(void)
void TwoWire::onReceive( void (*function)(int) )
{
user_onReceive = function;

#ifdef STM32F1xx
//Enable slave receive IT
i2c_attachSlaveRxEvent(&_i2c, onReceiveService);
#endif
}

// sets function called on slave read
void TwoWire::onRequest( void (*function)(void) )
{
user_onRequest = function;

#ifdef STM32F1xx
//Enable slave transmit IT
i2c_attachSlaveTxEvent(&_i2c, onRequestService);
#endif
}

// Preinstantiate Objects //////////////////////////////////////////////////////
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
/* Wire Master Reader Writer
by Wi6Labs

Demonstrates use of the Wire library, particullary with STM32F1xx boards.
Demonstrates use of the Wire library.
Reads/writes data from/to an I2C/TWI slave device.
Refer to the "Wire Slave Sender Receiver" example for use with this.

Created 27 June 2017
Updated 14 August 2017
- this example is now common to all STM32 boards

This example code is in the public domain.
*/
Expand Down Expand Up @@ -40,5 +42,5 @@ void loop()
Wire.endTransmission(); // stop transmitting
x++;

delay(1000);
delay(500);
}
Original file line number Diff line number Diff line change
@@ -1,45 +1,32 @@
/* Wire Slave Receiver
by Wi6Labs

Demonstrates use of the Wire library, particullary with STM32F1xx boards where
we can't use slave Tx & Rx mode in same time.
Demonstrates use of the Wire library.
Receives/sends data as an I2C/TWI slave device.
Refer to the "Wire Master Reader Writer" example for use with this.

Created 27 June 2017
Updated 14 August 2017
- this example is now common to all STM32 boards

This example code is in the public domain.
*/

#include <Wire.h>

#define SLAVE_MODE_RX 0
#define SLAVE_MODE_TX 1

#define I2C_ADDR 2

int w_r_mode = SLAVE_MODE_TX;
bool switch_mode = false;

void setup()
{
Wire.begin(I2C_ADDR); // join i2c bus with address #4
Wire.onRequest(requestEvent); // register event
Wire.onReceive(receiveEvent); // register event
Serial.begin(9600); // start serial for output
}

void loop()
{
if(switch_mode == true) {
switch_mode = false;
Wire.end();
Wire.begin(I2C_ADDR);
if(w_r_mode == SLAVE_MODE_TX) {
Wire.onRequest(requestEvent); // register event
} else {
Wire.onReceive(receiveEvent); // register event
}
}
//empty loop
}

// function that executes whenever data is received from master
Expand All @@ -48,23 +35,17 @@ void receiveEvent(int howMany)
{
while(1 < Wire.available()) // loop through all but the last
{
char c = Wire.read(); // receive byte as a character
Serial.print(c); // print the character
char c = Wire.read(); // receive byte as a character
Serial.print(c); // print the character
}
int x = Wire.read(); // receive byte as an integer
Serial.println(x); // print the integer

switch_mode = true;
w_r_mode = SLAVE_MODE_TX;
int x = Wire.read(); // receive byte as an integer
Serial.println(x); // print the integer
}

// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent()
{
Wire.write("hello "); // respond with message of 6 bytes
// as expected by master

switch_mode = true;
w_r_mode = SLAVE_MODE_RX;
Wire.write("hello\n"); // respond with message of 6 bytes
// as expected by master
}