From ff5e239a012abc5d563847417dc3d7a132cac711 Mon Sep 17 00:00:00 2001 From: fpr Date: Mon, 14 Aug 2017 15:02:52 +0200 Subject: [PATCH 1/2] Update I2C STM32F1 core and library to match with new HAL release 1.1.1 Signed-off-by: fpr --- cores/arduino/stm32/twi.c | 62 --------------------------------------- libraries/Wire/Wire.cpp | 18 ------------ 2 files changed, 80 deletions(-) diff --git a/cores/arduino/stm32/twi.c b/cores/arduino/stm32/twi.c index 4db35d0c18..193d7fc7ab 100644 --- a/cores/arduino/stm32/twi.c +++ b/cores/arduino/stm32/twi.c @@ -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 @@ -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 @@ -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 @@ -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 } /** diff --git a/libraries/Wire/Wire.cpp b/libraries/Wire/Wire.cpp index 48f5c9dece..e14f1b6d33 100644 --- a/libraries/Wire/Wire.cpp +++ b/libraries/Wire/Wire.cpp @@ -81,7 +81,6 @@ 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(&TwoWire::onRequestService)); // i2c_attachSlaveRxEvent(&_i2c, reinterpret_cast(&TwoWire::onReceiveService)); @@ -89,7 +88,6 @@ void TwoWire::begin(uint8_t address) i2c_attachSlaveTxEvent(&_i2c, onRequestService); i2c_attachSlaveRxEvent(&_i2c, onReceiveService); } -#endif } void TwoWire::begin(int address) @@ -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 @@ -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){ @@ -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 ////////////////////////////////////////////////////// From a4016989151381081c660f6b039057e87970dfff Mon Sep 17 00:00:00 2001 From: fpr Date: Mon, 14 Aug 2017 15:25:57 +0200 Subject: [PATCH 2/2] Update I2C examples following the new F1 HAL release Signed-off-by: fpr --- .../master_reader_writer.ino | 6 ++- .../slave_sender_receiver.ino | 41 +++++-------------- 2 files changed, 15 insertions(+), 32 deletions(-) diff --git a/libraries/Wire/examples/master_reader_writer/master_reader_writer.ino b/libraries/Wire/examples/master_reader_writer/master_reader_writer.ino index 6f73acf217..0b787de97f 100644 --- a/libraries/Wire/examples/master_reader_writer/master_reader_writer.ino +++ b/libraries/Wire/examples/master_reader_writer/master_reader_writer.ino @@ -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. */ @@ -40,5 +42,5 @@ void loop() Wire.endTransmission(); // stop transmitting x++; - delay(1000); + delay(500); } diff --git a/libraries/Wire/examples/slave_sender_receiver/slave_sender_receiver.ino b/libraries/Wire/examples/slave_sender_receiver/slave_sender_receiver.ino index 92912961f9..fededa6101 100644 --- a/libraries/Wire/examples/slave_sender_receiver/slave_sender_receiver.ino +++ b/libraries/Wire/examples/slave_sender_receiver/slave_sender_receiver.ino @@ -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 -#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 @@ -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 }