Olfox recently raised a concern with the 400kHz frequency setting for the TWIM peripheral, which I confirm is incorrect in the SDK (17) and nRF52832 datasheet (v1.7); I attempted to write code to determine correct settings but there appears to be a maximum divisor above which the frequency falls rather than increases.
The quoted value for 400kHz is 0x06400000, however this does not work at anything like the correct rate. 0x05F00000 appears to be the largest value that is meaningful (on the v2 part I have to hand) and generates nearly the required 400kHz.
The discussion is here gpio-to-1-7v-after-enabling-twi though as that discussion is now locked I am posting this new thread. My updated calculations are here:
#define MAGIC_SCALE_FACTOR 32 #define MAGIC_SYSTEM_CLOCK 16000000ULL; // Typically 16MHz for TWI/TWIM/SPI/SPIM #define MAGIC_INACTIVE_BIT_MASK 0xFFFF0000UL #define MAGIC_INACTIVE_ROUNDING_BIT 0x8000UL #define MAGIC_MAX_REGISTER_VALUE 0x7FFFF000UL STATIC_ASSERT(((~MAGIC_INACTIVE_BIT_MASK+1)>>1) == MAGIC_INACTIVE_ROUNDING_BIT, "MAGIC_INACTIVE_BIT_MASK incorrect MAGIC_INACTIVE_ROUNDING_BIT"); // --------Calculated------------------- --------Documentation-------- // Required Register Actual Error Register Required Actual // ======== =========== ======== ======= =========== ======== ======== // 100000, 0x019A0000, 100097, +0.097% 0x01980000, 100000, 100000, // 200000, 0x03330000, 199951, -0.024% -, -, -, // 250000, 0x04000000, 250000, +0.000% 0x04000000, 250000, 250000, // 400000, 0x06660000, 399902, -0.024% 0x06400000, 400000, 400000, // 400000, 0x05FF0000, 374755, -6.311% 0x06400000, 400000, 400000, // Note 0x06660000 entry does not work as expected above 0x05F00000 CalculatedRegisterValue = (uint32_t)((((uint64_t)RequiredBaudRate << MagicScaler) / SystemClock) + MAGIC_INACTIVE_ROUNDING_BIT) & MAGIC_INACTIVE_BIT_MASK;
Thanks to Olfox for instigating this investigation; we would appreciate a review by Nordic.