Skip to content

Commit c92c78e

Browse files
Fix I2C slave not working on STM if you set the frequency after initializing
1 parent c035953 commit c92c78e

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

targets/TARGET_STM/i2c_api.c

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,17 @@ void i2c_init_internal(i2c_t *obj, const i2c_pinmap_t *pinmap)
548548
obj_s->hz = 100000; // 100 kHz per default
549549
}
550550

551+
// Set remaining init parameters to defaults
552+
obj_s->handle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
553+
obj_s->handle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
554+
obj_s->handle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
555+
obj_s->handle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
556+
obj_s->handle.Init.OwnAddress1 = 0;
557+
obj_s->handle.Init.OwnAddress2 = 0;
558+
#ifdef I2C_IP_VERSION_V2
559+
obj_s->handle.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
560+
#endif
561+
551562
// Reset to clear pending flags if any
552563
i2c_hw_reset(obj);
553564
i2c_frequency(obj, obj_s->hz);
@@ -784,19 +795,15 @@ void i2c_frequency(i2c_t *obj, int hz)
784795
/* hz value is stored for computing timing value next time */
785796
obj_s->current_hz = hz;
786797
#endif // I2C_IP_VERSION_V2
787-
788-
// I2C configuration
789-
handle->Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
790-
handle->Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
791-
handle->Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
792-
handle->Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
793-
handle->Init.OwnAddress1 = 0;
794-
handle->Init.OwnAddress2 = 0;
795-
#ifdef I2C_IP_VERSION_V2
796-
handle->Init.OwnAddress2Masks = I2C_OA2_NOMASK;
797-
#endif
798+
798799
HAL_I2C_Init(handle);
799800

801+
// In slave mode, reenable slave interrupts after calling init
802+
if(obj_s->slave != 0)
803+
{
804+
HAL_I2C_EnableListen_IT(&obj_s->handle);
805+
}
806+
800807
/* store frequency for timeout computation */
801808
obj_s->hz = hz;
802809
}

tools/cmake/upload_methods/UploadMethodOPENOCD.cmake

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ function(gen_upload_target TARGET_NAME BINARY_FILE)
4848
${OPENOCD_ADAPTER_SERIAL_COMMAND}
4949
-c "gdb_port disabled" # Don't start a GDB server when just programming
5050
-c "program ${BINARY_FILE} ${MBED_UPLOAD_BASE_ADDR} reset exit"
51-
VERBATIM)
51+
VERBATIM
52+
USES_TERMINAL)
5253

5354
endfunction(gen_upload_target)
5455

0 commit comments

Comments
 (0)