<commit_before>#include <i2c.h>

/* Note: should convert the while loops to not loop forever */
/* Hmm: what about when a client needs more time and attempts to extend the acknowledgement? */
/* Some useful error statuses:
 *   Start bit sent (Master) SB
 *   Address sent (Master) or Address matched (Slave) ADDR
 *   10-bit header sent (Master) ADD10
 *   Stop received (Slave) STOPF
 *   Data byte transfer finished BTF
 *   Receive buffer not empty RxNE
 *   Transmit buffer empty TxE
 *   Bus error BERR
 *   Arbitration loss (Master) ARLO
 *   Acknowledge failure AF
 *   Overrun/Underrun OVR
 *   PEC error PECERR
 *   Timeout/Tlow error TIMEOUT
 *   SMBus Alert SMBALERT
 */

void InitialiseI2C() {
	i2cHasProblem = false;

	/* enable the clock to i2c1 */
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);

	/* enable the clock to the pins that will be used */
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);

	/* setting the pins to open drain will ensure masters and slaves can only decide
	 * to pull the line low, or leave the line alone. In the case of leaving the line alone the pull up resistors
	 * will drive the input high.
	 *
	 * This allows for us to have multiple masters, or to allow slaves to stretch when they need more time.
	 * See http://www.i2c-bus.org/how-i2c-hardware-works/.
	 */
	GPIO_InitTypeDef GPIO_InitStruct;
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStruct.GPIO_OType = GPIO_OType_OD;
	GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
	GPIO_Init(GPIOB, &GPIO_InitStruct);

	/* PB.08 is SCL: the clock for the i2c protocol
	 * PB.09 is SDA: the data line for the i2c protocol
	 */
	GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_I2C1);
	GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_I2C1);

	I2C_InitTypeDef I2C_InitStruct;
	/* note that the clock speed should be able to go up to 400kHz */
	I2C_InitStruct.I2C_ClockSpeed = 100000; 		// 100kHz

	/* Use I2c (alternative is an SMBus device) */
	I2C_InitStruct.I2C_Mode = I2C_Mode_I2C;

	/* Duty Cycle is 50% as per standard */
	I2C_InitStruct.I2C_DutyCycle = I2C_DutyCycle_2;

	/* Only useful for slave mode (will we do this later? maybe) */
	I2C_InitStruct.I2C_OwnAddress1 = 0x00;

	/* Acknowledgement on read is disabled here as it is explicitly enabled / disabled when retrieving data. */
	I2C_InitStruct.I2C_Ack = I2C_Ack_Disable;

	/* 7 bit acknowledge addresses, hmm strange. */
	I2C_InitStruct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
	I2C_Init(I2C1, &I2C_InitStruct);

	/* Turn it on! */
	I2C_Cmd(I2C1, ENABLE);
};

void WaitForEvent(I2C_TypeDef* I2Cx, uint32_t event) {
	int32_t attempts = 0;
	int32_t maxAttempts = 200000;

	while(!I2C_CheckEvent(I2Cx, event) && attempts < maxAttempts) {
		attempts++;
	}

	if (attempts == maxAttempts) {
		i2cHasProblem = true;
	}
}

void WaitUntilBusIsFree() {
	/* wait until the line is not busy */
	WaitForEvent(I2C1, I2C_FLAG_BUSY);
}

void SendStart() {
	i2cHasProblem = false;

	/* Begin comms! */
	I2C_GenerateSTART(I2C1, ENABLE);

	/* Start condition has been correctly released on the bus (will fail if another device attempts to communicate and bus not free) */
	/* hmmm. I don't like while loops that never stop. */
	WaitForEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT);
}
<commit_msg>reduce the number of attempts to get the i2c result, this drastically increases the loop frequency<commit_after>#include <i2c.h>

/* Note: should convert the while loops to not loop forever */
/* Hmm: what about when a client needs more time and attempts to extend the acknowledgement? */
/* Some useful error statuses:
 *   Start bit sent (Master) SB
 *   Address sent (Master) or Address matched (Slave) ADDR
 *   10-bit header sent (Master) ADD10
 *   Stop received (Slave) STOPF
 *   Data byte transfer finished BTF
 *   Receive buffer not empty RxNE
 *   Transmit buffer empty TxE
 *   Bus error BERR
 *   Arbitration loss (Master) ARLO
 *   Acknowledge failure AF
 *   Overrun/Underrun OVR
 *   PEC error PECERR
 *   Timeout/Tlow error TIMEOUT
 *   SMBus Alert SMBALERT
 */

void InitialiseI2C() {
	i2cHasProblem = false;

	/* enable the clock to i2c1 */
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);

	/* enable the clock to the pins that will be used */
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);

	/* setting the pins to open drain will ensure masters and slaves can only decide
	 * to pull the line low, or leave the line alone. In the case of leaving the line alone the pull up resistors
	 * will drive the input high.
	 *
	 * This allows for us to have multiple masters, or to allow slaves to stretch when they need more time.
	 * See http://www.i2c-bus.org/how-i2c-hardware-works/.
	 */
	GPIO_InitTypeDef GPIO_InitStruct;
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStruct.GPIO_OType = GPIO_OType_OD;
	GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
	GPIO_Init(GPIOB, &GPIO_InitStruct);

	/* PB.08 is SCL: the clock for the i2c protocol
	 * PB.09 is SDA: the data line for the i2c protocol
	 */
	GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_I2C1);
	GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_I2C1);

	I2C_InitTypeDef I2C_InitStruct;
	/* note that the clock speed should be able to go up to 400kHz */
	I2C_InitStruct.I2C_ClockSpeed = 100000; 		// 100kHz

	/* Use I2c (alternative is an SMBus device) */
	I2C_InitStruct.I2C_Mode = I2C_Mode_I2C;

	/* Duty Cycle is 50% as per standard */
	I2C_InitStruct.I2C_DutyCycle = I2C_DutyCycle_2;

	/* Only useful for slave mode (will we do this later? maybe) */
	I2C_InitStruct.I2C_OwnAddress1 = 0x00;

	/* Acknowledgement on read is disabled here as it is explicitly enabled / disabled when retrieving data. */
	I2C_InitStruct.I2C_Ack = I2C_Ack_Disable;

	/* 7 bit acknowledge addresses, hmm strange. */
	I2C_InitStruct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
	I2C_Init(I2C1, &I2C_InitStruct);

	/* Turn it on! */
	I2C_Cmd(I2C1, ENABLE);
};

void WaitForEvent(I2C_TypeDef* I2Cx, uint32_t event) {
	int32_t attempts = 0;
	int32_t maxAttempts = 5000;

	while(!I2C_CheckEvent(I2Cx, event) && attempts < maxAttempts) {
		attempts++;
	}

	if (attempts == maxAttempts) {
		i2cHasProblem = true;
	}
}

void WaitUntilBusIsFree() {
	/* wait until the line is not busy */
	WaitForEvent(I2C1, I2C_FLAG_BUSY);
}

void SendStart() {
	i2cHasProblem = false;

	/* Begin comms! */
	I2C_GenerateSTART(I2C1, ENABLE);

	/* Start condition has been correctly released on the bus (will fail if another device attempts to communicate and bus not free) */
	/* hmmm. I don't like while loops that never stop. */
	WaitForEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT);
}

{'fraction_non_alphanumeric': 0.0845215346170807, 'fraction_numerical': 0.03189769219462002, 'mean_word_length': 4.085201793721973, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5357321', 'n_tokens_mistral': 2640, 'n_tokens_neox': 2433, 'n_words': 747}