<commit_before>/**
 * @file i2c.c
 * @author <NAME>
 * @date 3 Jan 2016
 * @brief File containing driver functions for i2c peripheral on stm32f4xx.
 *
 * This file contains private and public functions for using the I2C peripheral
 * on an stm32f4xx microcontroller. Comes as part of the 
 * stm32f4xx-amperture-periphlib package. At current does not support 
 * interrupts or DMA.
 *
 * This driver package at current is not meant to be simply included without
 * review into a project. It is fully expected that the programmer will 
 * read the functions they are calling, and edit them accordingly to their 
 * needs.
 *
 * @see http://www.st.com/web/en/resource/technical/document/programming_manual/DM00046982.pdf
 */

#include <stm32f4xx.h>


void i2cInit(I2C_TypeDef I2Cx, GPIO_TypeDef GPIOx, uint8_t sclPin,
        uint8_t sdaPin, uint8_t afMode){

    // Enable the I2C and GPIO Clocks in the RCC
    RCC -> APB1ENR |= RCC_APB1ENR_I2C1EN;
    RCC -> AHB1ENR |= RCC_AHB1ENR_GPIOBEN;

    // Disable the I2C Peripheral
    I2Cx -> CR1     &=      ~(I2C_CR1_PE);

    // Set up the First Control Register
    // NOTE: Users, please comment out unnecessary bits.
    I2Cx -> CR1     =   (0
                        // + I2C_CR1_SMBUS         // SMBus Mode
                        // + I2C_CR1_SMBTYPE       // SMBus Type
                        + I2C_CR1_ENARP         // ARP Enable
                        + I2C_CR1_ENPEC         // PEC Enable
                        + I2C_CR1_ENGC          // General Call Enable
                        + I2C_CR1_NOSTRETCH     // (SLV)ClockStretchDisable
                        + I2C_CR1_START         // Start Gen Enable
                        + I2C_CR1_STOP          // Stop Gen Enable
                        + I2C_CR1_ACK           // Enable Acknowledge
                        + I2C_CR1_POS           // ACK/PEC postition (rec)
                        + I2C_CR1_PEC           // Packet Error Check
                        // + I2C_CR1_ALERT         // SMB Alert
                        // + I2C_CR1_SWRST         // Soft Reset
                        );

    // Set up the Second Control Register
    // NOTE: Users, please comment out unnecessary bits.
    I2Cx -> CR2     =   (0
                        + (I2C_CR2_FREQ & 2)    // Frequency
                        // + I2C_CR2_ITERREN       // Error Int Enable
                        // + I2C_CR2_ITEVTEN       // Event Int Enable
                        // + I2C_CR2_ITBUFEN       // Buffer Int Enable
                        // + I2C_CR2_DMAEN         // DMA Request Enable
                        // + I2C_CR2_LAST          // DMA Last Transfer
                        );

    // Re-Enable the I2C Peripheral
    I2Cx -> CR1     |=  I2C_CR1_PE;

    // Set GPIO Pins to Alternate Function Mode, High Speed, NoPull, 
    GPIOx -> MODER      |=  (0x02 << (2 * sclPin));
    GPIOx -> OSPEEDR    |=  (0x03 << (2 * sclPin));
    GPIOx -> PUPDR      &=  ~(0x03 << (2 * sclPin));
    GPIOx -> MODER      |=  (0x02 << (2 * sdaPin));
    GPIOx -> OSPEEDR    |=  (0x03 << (2 * sdaPin));
    GPIOx -> PUPDR      &=  ~(0x03 << (2 * sdaPin));

    // Set the GPIO Pins to the appropriate Alternate Function Setting
    // Please refer to the datasheet for which numbers to use.
    // http://www.st.com/st-web-ui/static/active/en/resource/technical/document/datasheet/DM00102166.pdf
    // Page 45
    if(sclPin > 7) GPIOx -> AFR[1] |= (afMode << (4 * (sclPin - 7)));
    else GPIOx -> AFR[0] |= (afMode << (4 * sclPin));

    if(sdaPin > 7) GPIOx -> AFR[1] |= (afMode << (4 * (sdaPin - 7)));
    else GPIOx -> AFR[0] |= (afMode << (4 * sdaPin));

}<commit_msg>Changed ADD bitwise operations to OR, should be faster.<commit_after>/**
 * @file i2c.c
 * @author <NAME>
 * @date 3 Jan 2016
 * @brief File containing driver functions for i2c peripheral on stm32f4xx.
 *
 * This file contains private and public functions for using the I2C peripheral
 * on an stm32f4xx microcontroller. Comes as part of the 
 * stm32f4xx-amperture-periphlib package. At current does not support 
 * interrupts or DMA.
 *
 * This driver package at current is not meant to be simply included without
 * review into a project. It is fully expected that the programmer will 
 * read the functions they are calling, and edit them accordingly to their 
 * needs.
 *
 * @see http://www.st.com/web/en/resource/technical/document/programming_manual/DM00046982.pdf
 */

#include <stm32f4xx.h>


void i2cInit(I2C_TypeDef I2Cx, GPIO_TypeDef GPIOx, uint8_t sclPin,
        uint8_t sdaPin, uint8_t afMode){

    // Enable the I2C and GPIO Clocks in the RCC
    RCC -> APB1ENR |= RCC_APB1ENR_I2C1EN;
    RCC -> AHB1ENR |= RCC_AHB1ENR_GPIOBEN;

    // Disable the I2C Peripheral
    I2Cx -> CR1     &=      ~(I2C_CR1_PE);

    // Set up the First Control Register
    // NOTE: Users, please comment out unnecessary bits.
    I2Cx -> CR1     =   (0
                        // | I2C_CR1_SMBUS         // SMBus Mode
                        // | I2C_CR1_SMBTYPE       // SMBus Type
                        | I2C_CR1_ENARP         // ARP Enable
                        | I2C_CR1_ENPEC         // PEC Enable
                        | I2C_CR1_ENGC          // General Call Enable
                        | I2C_CR1_NOSTRETCH     // (SLV)ClockStretchDisable
                        | I2C_CR1_START         // Start Gen Enable
                        | I2C_CR1_STOP          // Stop Gen Enable
                        | I2C_CR1_ACK           // Enable Acknowledge
                        | I2C_CR1_POS           // ACK/PEC postition (rec)
                        | I2C_CR1_PEC           // Packet Error Check
                        // | I2C_CR1_ALERT         // SMB Alert
                        // | I2C_CR1_SWRST         // Soft Reset
                        );

    // Set up the Second Control Register
    // NOTE: Users, please comment out unnecessary bits.
    I2Cx -> CR2     =   (0
                        | (I2C_CR2_FREQ & 2)    // Frequency
                        // | I2C_CR2_ITERREN       // Error Int Enable
                        // | I2C_CR2_ITEVTEN       // Event Int Enable
                        // | I2C_CR2_ITBUFEN       // Buffer Int Enable
                        // | I2C_CR2_DMAEN         // DMA Request Enable
                        // | I2C_CR2_LAST          // DMA Last Transfer
                        );

    // Re-Enable the I2C Peripheral
    I2Cx -> CR1     |=  I2C_CR1_PE;

    // Set GPIO Pins to Alternate Function Mode, High Speed, NoPull, 
    GPIOx -> MODER      |=  (0x02 << (2 * sclPin));
    GPIOx -> OSPEEDR    |=  (0x03 << (2 * sclPin));
    GPIOx -> PUPDR      &=  ~(0x03 << (2 * sclPin));
    GPIOx -> MODER      |=  (0x02 << (2 * sdaPin));
    GPIOx -> OSPEEDR    |=  (0x03 << (2 * sdaPin));
    GPIOx -> PUPDR      &=  ~(0x03 << (2 * sdaPin));

    // Set the GPIO Pins to the appropriate Alternate Function Setting
    // Please refer to the datasheet for which numbers to use.
    // http://www.st.com/st-web-ui/static/active/en/resource/technical/document/datasheet/DM00102166.pdf
    // Page 45
    if(sclPin > 7) GPIOx -> AFR[1] |= (afMode << (4 * (sclPin - 7)));
    else GPIOx -> AFR[0] |= (afMode << (4 * sclPin));

    if(sdaPin > 7) GPIOx -> AFR[1] |= (afMode << (4 * (sdaPin - 7)));
    else GPIOx -> AFR[0] |= (afMode << (4 * sdaPin));

}
{'fraction_non_alphanumeric': 0.10315732172019597, 'fraction_numerical': 0.038649972781709306, 'mean_word_length': 1.5099043715846994, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '3451257', 'n_tokens_mistral': 2547, 'n_tokens_neox': 2331, 'n_words': 650}