<commit_before>/*
 * Copyright (C) 2014 <NAME>ität Berlin
 * Copyright (C) 2014 PHYTEC Messtechnik GmbH
 *
 * This file is subject to the terms and conditions of the GNU Lesser
 * General Public License v2.1. See the file LICENSE in the top level
 * directory for more details.
 */

/**
 * @ingroup     cpu_kinetis_common_i2c
 *
 * @note        This driver only implements the 7-bit addressing master mode.
 *
 * @{
 *
 * @file
 * @brief       Low-level I2C driver implementation
 *
 * @author      <NAME> <<EMAIL>>
 *
 * @}
 */

#include <stdint.h>

#include "cpu.h"
#include "irq.h"
#include "mutex.h"
#include "periph_conf.h"
#include "periph/i2c.h"

#define ENABLE_DEBUG    (0)
#include "debug.h"

/* guard file in case no I2C device is defined */
#if I2C_NUMOF

/**
 * @brief Array holding one pre-initialized mutex for each I2C device
 */
static mutex_t locks[] =  {
#if I2C_0_EN
    [I2C_0] = MUTEX_INIT,
#endif
#if I2C_1_EN
    [I2C_1] = MUTEX_INIT,
#endif
#if I2C_2_EN
    [I2C_2] = MUTEX_INIT
#endif
#if I2C_3_EN
    [I2C_3] = MUTEX_INIT
#endif
};

int i2c_acquire(i2c_t dev)
{
    if (dev >= I2C_NUMOF) {
        return -1;
    }
    mutex_lock(&locks[dev]);
    return 0;
}

int i2c_release(i2c_t dev)
{
    if (dev >= I2C_NUMOF) {
        return -1;
    }
    mutex_unlock(&locks[dev]);
    return 0;
}

int i2c_init_master(i2c_t dev, i2c_speed_t speed)
{
    I2C_Type *i2c;
    PORT_Type *i2c_port;
    int pin_scl = 0;
    int pin_sda = 0;

    /* TODO: read speed configuration */
    switch (speed) {
        case I2C_SPEED_NORMAL:
            break;

        case I2C_SPEED_FAST:
            break;

        default:
            return -2;
    }

    /* read static device configuration */
    switch (dev) {
#if I2C_0_EN

        case I2C_0:
            i2c = I2C_0_DEV;
            i2c_port = I2C_0_PORT;
            pin_scl = I2C_0_SCL_PIN;
            pin_sda = I2C_0_SDA_PIN;
            I2C_0_CLKEN();
            I2C_0_PORT_CLKEN();
            break;
#endif

        default:
            return -1;
    }

    /* configure pins, alternate output */
    i2c_port->PCR[pin_scl] = I2C_0_PORT_CFG;
    i2c_port->PCR[pin_sda] = I2C_0_PORT_CFG;

    /* 
    * TODO: Add baud rate selection function
    * See the Chapter "I2C divider and hold values":
    *     Kinetis K60 Reference Manual, section 192.168.3.11, Table 51-41. 
    *     Kinetis MKW2x  Reference Manual, section 192.168.127.12, Table 52-41. 
    *
    * baud rate = I2C_module_clock / (mul × ICR)
    *
    * The assignment below will set baud rate to I2C_module_clock / (240 x 2).
    */
    i2c->F = I2C_F_MULT(1) | I2C_F_ICR(0x1f);

    /* enable i2c-module and interrupt */
    i2c->C1 = I2C_C1_IICEN_MASK | I2C_C1_IICIE_MASK | I2C_C1_TXAK_MASK;
    i2c->C2 = 0;

    return 0;
}

int i2c_init_slave(i2c_t dev, uint8_t address)
{
    /* TODO: implement slave mode */
    return -1;
}

static inline int _i2c_start(I2C_Type *dev, uint8_t address, uint8_t rw_flag)
{
    /* bus free ? */
    if (dev->S & I2C_S_BUSY_MASK) {
        return -1;
    }

    dev->S = I2C_S_IICIF_MASK;

    dev->C1 = I2C_C1_IICEN_MASK | I2C_C1_MST_MASK | I2C_C1_TX_MASK;
    dev->D = address << 1 | (rw_flag & 1);

    /* wait for bus-busy to be set */
    while (!(dev->S & I2C_S_BUSY_MASK));<commit_msg>cpu/kinetis_common/i2c: fix indent, trailing space<commit_after>/*
 * Copyright (C) 2014 Freie Universität Berlin
 * Copyright (C) 2014 PHYTEC Messtechnik GmbH
 *
 * This file is subject to the terms and conditions of the GNU Lesser
 * General Public License v2.1. See the file LICENSE in the top level
 * directory for more details.
 */

/**
 * @ingroup     cpu_kinetis_common_i2c
 *
 * @note        This driver only implements the 7-bit addressing master mode.
 *
 * @{
 *
 * @file
 * @brief       Low-level I2C driver implementation
 *
 * @author      <NAME> <<EMAIL>>
 *
 * @}
 */

#include <stdint.h>

#include "cpu.h"
#include "irq.h"
#include "mutex.h"
#include "periph_conf.h"
#include "periph/i2c.h"

#define ENABLE_DEBUG    (0)
#include "debug.h"

/* guard file in case no I2C device is defined */
#if I2C_NUMOF

/**
 * @brief Array holding one pre-initialized mutex for each I2C device
 */
static mutex_t locks[] =  {
#if I2C_0_EN
    [I2C_0] = MUTEX_INIT,
#endif
#if I2C_1_EN
    [I2C_1] = MUTEX_INIT,
#endif
#if I2C_2_EN
    [I2C_2] = MUTEX_INIT
#endif
#if I2C_3_EN
    [I2C_3] = MUTEX_INIT
#endif
};

int i2c_acquire(i2c_t dev)
{
    if (dev >= I2C_NUMOF) {
        return -1;
    }
    mutex_lock(&locks[dev]);
    return 0;
}

int i2c_release(i2c_t dev)
{
    if (dev >= I2C_NUMOF) {
        return -1;
    }
    mutex_unlock(&locks[dev]);
    return 0;
}

int i2c_init_master(i2c_t dev, i2c_speed_t speed)
{
    I2C_Type *i2c;
    PORT_Type *i2c_port;
    int pin_scl = 0;
    int pin_sda = 0;

    /* TODO: read speed configuration */
    switch (speed) {
        case I2C_SPEED_NORMAL:
            break;

        case I2C_SPEED_FAST:
            break;

        default:
            return -2;
    }

    /* read static device configuration */
    switch (dev) {
#if I2C_0_EN

        case I2C_0:
            i2c = I2C_0_DEV;
            i2c_port = I2C_0_PORT;
            pin_scl = I2C_0_SCL_PIN;
            pin_sda = I2C_0_SDA_PIN;
            I2C_0_CLKEN();
            I2C_0_PORT_CLKEN();
            break;
#endif

        default:
            return -1;
    }

    /* configure pins, alternate output */
    i2c_port->PCR[pin_scl] = I2C_0_PORT_CFG;
    i2c_port->PCR[pin_sda] = I2C_0_PORT_CFG;

    /*
     * TODO: Add baud rate selection function
     * See the Chapter "I2C divider and hold values":
     *     Kinetis K60 Reference Manual, section 192.168.3.11, Table 51-41.
     *     Kinetis MKW2x  Reference Manual, section 192.168.127.12, Table 52-41.
     *
     * baud rate = I2C_module_clock / (mul × ICR)
     *
     * The assignment below will set baud rate to I2C_module_clock / (240 x 2).
     */
    i2c->F = I2C_F_MULT(1) | I2C_F_ICR(0x1f);

    /* enable i2c-module and interrupt */
    i2c->C1 = I2C_C1_IICEN_MASK | I2C_C1_IICIE_MASK | I2C_C1_TXAK_MASK;
    i2c->C2 = 0;

    return 0;
}

int i2c_init_slave(i2c_t dev, uint8_t address)
{
    /* TODO: implement slave mode */
    return -1;
}

static inline int _i2c_start(I2C_Type *dev, uint8_t address, uint8_t rw_flag)
{
    /* bus free ? */
    if (dev->S & I2C_S_BUSY_MASK) {
        return -1;
    }

    dev->S = I2C_S_IICIF_MASK;

    dev->C1 = I2C_C1_IICEN_MASK | I2C_C1_MST_MASK | I2C_C1_TX_MASK;
    dev->D = address << 1 | (rw_flag & 1);

    /* wait for bus-busy to be set */
    while (!(dev->S & I2C_S_BUSY_MASK));
{'fraction_non_alphanumeric': 0.1134020618556701, 'fraction_numerical': 0.04805942995755003, 'mean_word_length': 2.358961303462322, 'pattern_counts': {'":': 2, '<': 16, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, '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': '4405870', 'n_tokens_mistral': 2865, 'n_tokens_neox': 2728, 'n_words': 640}