mirror of https://github.com/ARMmbed/mbed-os.git
[STM32] Make most of the I2C code into a common file
Since most of the code in i2c_api.c is now relying on STM32 HAL, there is now a possibility to make a common usage of this code accross families. The IP version definition is introduced per family, to allow a switch of functionnalities, especially the frequency management which differs. BTw, we fix the F0 frequency settings at the same time. F1 is managed for now as an exception as the HAL API for sequential transmit /receive is not yet available (coming soon)pull/3324/head
parent
23926a2418
commit
3fad50287c
|
@ -66,10 +66,6 @@ struct dac_s {
|
|||
uint32_t channel;
|
||||
};
|
||||
|
||||
struct i2c_s {
|
||||
I2CName i2c;
|
||||
};
|
||||
|
||||
#include "common_objects.h"
|
||||
#include "gpio_object.h"
|
||||
|
||||
|
|
|
@ -60,10 +60,6 @@ struct analogin_s {
|
|||
uint32_t channel;
|
||||
};
|
||||
|
||||
struct i2c_s {
|
||||
I2CName i2c;
|
||||
};
|
||||
|
||||
#include "common_objects.h"
|
||||
#include "gpio_object.h"
|
||||
|
||||
|
|
|
@ -60,10 +60,6 @@ struct analogin_s {
|
|||
uint32_t channel;
|
||||
};
|
||||
|
||||
struct i2c_s {
|
||||
I2CName i2c;
|
||||
};
|
||||
|
||||
#include "common_objects.h"
|
||||
#include "gpio_object.h"
|
||||
|
||||
|
|
|
@ -60,10 +60,6 @@ struct analogin_s {
|
|||
uint32_t channel;
|
||||
};
|
||||
|
||||
struct i2c_s {
|
||||
I2CName i2c;
|
||||
};
|
||||
|
||||
struct can_s {
|
||||
CANName can;
|
||||
int index;
|
||||
|
|
|
@ -60,10 +60,6 @@ struct analogin_s {
|
|||
uint32_t channel;
|
||||
};
|
||||
|
||||
struct i2c_s {
|
||||
I2CName i2c;
|
||||
};
|
||||
|
||||
#include "common_objects.h"
|
||||
#include "gpio_object.h"
|
||||
|
||||
|
|
|
@ -66,10 +66,6 @@ struct dac_s {
|
|||
uint32_t channel;
|
||||
};
|
||||
|
||||
struct i2c_s {
|
||||
I2CName i2c;
|
||||
};
|
||||
|
||||
struct can_s {
|
||||
CANName can;
|
||||
int index;
|
||||
|
|
|
@ -66,10 +66,6 @@ struct dac_s {
|
|||
uint32_t channel;
|
||||
};
|
||||
|
||||
struct i2c_s {
|
||||
I2CName i2c;
|
||||
};
|
||||
|
||||
struct can_s {
|
||||
CANName can;
|
||||
int index;
|
||||
|
|
|
@ -82,6 +82,34 @@ struct serial_s {
|
|||
#endif
|
||||
};
|
||||
|
||||
struct i2c_s {
|
||||
/* The 1st 2 members I2CName i2c
|
||||
* and I2C_HandleTypeDef handle should
|
||||
* be kept as the first members of this struct
|
||||
* to ensure i2c_get_obj to work as expected
|
||||
*/
|
||||
I2CName i2c;
|
||||
I2C_HandleTypeDef handle;
|
||||
uint8_t index;
|
||||
int hz;
|
||||
PinName sda;
|
||||
PinName scl;
|
||||
IRQn_Type event_i2cIRQ;
|
||||
IRQn_Type error_i2cIRQ;
|
||||
uint32_t XferOperation;
|
||||
volatile uint8_t event;
|
||||
#if DEVICE_I2CSLAVE
|
||||
uint8_t slave;
|
||||
volatile uint8_t pending_slave_tx_master_rx;
|
||||
volatile uint8_t pending_slave_rx_maxter_tx;
|
||||
#endif
|
||||
#if DEVICE_I2C_ASYNCH
|
||||
uint32_t address;
|
||||
uint8_t stop;
|
||||
uint8_t available_events;
|
||||
#endif
|
||||
};
|
||||
|
||||
#include "gpio_object.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
|
|
@ -1,390 +0,0 @@
|
|||
/* mbed Microcontroller Library
|
||||
*******************************************************************************
|
||||
* Copyright (c) 2014, STMicroelectronics
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of STMicroelectronics nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*******************************************************************************
|
||||
*/
|
||||
#include "mbed_assert.h"
|
||||
#include "i2c_api.h"
|
||||
|
||||
#if DEVICE_I2C
|
||||
|
||||
#include "cmsis.h"
|
||||
#include "pinmap.h"
|
||||
#include "PeripheralPins.h"
|
||||
|
||||
/* Timeout values for flags and events waiting loops. These timeouts are
|
||||
not based on accurate values, they just guarantee that the application will
|
||||
not remain stuck if the I2C communication is corrupted. */
|
||||
#define FLAG_TIMEOUT ((int)0x1000)
|
||||
#define LONG_TIMEOUT ((int)0x8000)
|
||||
|
||||
I2C_HandleTypeDef I2cHandle;
|
||||
|
||||
int i2c1_inited = 0;
|
||||
#if defined(I2C2_BASE)
|
||||
int i2c2_inited = 0;
|
||||
#endif
|
||||
|
||||
void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
|
||||
// Determine the I2C to use
|
||||
I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA);
|
||||
I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL);
|
||||
|
||||
obj->i2c = (I2CName)pinmap_merge(i2c_sda, i2c_scl);
|
||||
MBED_ASSERT(obj->i2c != (I2CName)NC);
|
||||
|
||||
// Enable I2C1 clock and pinout if not done
|
||||
if ((obj->i2c == I2C_1) && !i2c1_inited) {
|
||||
i2c1_inited = 1;
|
||||
__HAL_RCC_I2C1_CONFIG(RCC_I2C1CLKSOURCE_SYSCLK);
|
||||
__I2C1_CLK_ENABLE();
|
||||
// Configure I2C pins
|
||||
pinmap_pinout(sda, PinMap_I2C_SDA);
|
||||
pinmap_pinout(scl, PinMap_I2C_SCL);
|
||||
pin_mode(sda, OpenDrain);
|
||||
pin_mode(scl, OpenDrain);
|
||||
}
|
||||
|
||||
#if defined(I2C2_BASE)
|
||||
// Enable I2C2 clock and pinout if not done
|
||||
if ((obj->i2c == I2C_2) && !i2c2_inited) {
|
||||
i2c2_inited = 1;
|
||||
__I2C2_CLK_ENABLE();
|
||||
// Configure I2C pins
|
||||
pinmap_pinout(sda, PinMap_I2C_SDA);
|
||||
pinmap_pinout(scl, PinMap_I2C_SCL);
|
||||
pin_mode(sda, OpenDrain);
|
||||
pin_mode(scl, OpenDrain);
|
||||
}
|
||||
#endif
|
||||
|
||||
// Reset to clear pending flags if any
|
||||
i2c_reset(obj);
|
||||
|
||||
// I2C configuration
|
||||
i2c_frequency(obj, 100000); // 100 kHz per default
|
||||
}
|
||||
|
||||
void i2c_frequency(i2c_t *obj, int hz) {
|
||||
MBED_ASSERT((hz == 100000) || (hz == 400000) || (hz == 1000000));
|
||||
I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
|
||||
int timeout;
|
||||
|
||||
// wait before init
|
||||
timeout = LONG_TIMEOUT;
|
||||
while ((__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY)) && (timeout-- != 0));
|
||||
|
||||
// Common settings: I2C clock = 48 MHz, Analog filter = ON, Digital filter coefficient = 0
|
||||
switch (hz) {
|
||||
case 100000:
|
||||
I2cHandle.Init.Timing = 0x10805E89; // Standard mode with Rise Time = 400ns and Fall Time = 100ns
|
||||
break;
|
||||
case 400000:
|
||||
I2cHandle.Init.Timing = 0x00901850; // Fast mode with Rise Time = 250ns and Fall Time = 100ns
|
||||
break;
|
||||
case 1000000:
|
||||
I2cHandle.Init.Timing = 0x00700818; // Fast mode Plus with Rise Time = 60ns and Fall Time = 100ns
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
// I2C configuration
|
||||
I2cHandle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
|
||||
I2cHandle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED;
|
||||
I2cHandle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED;
|
||||
I2cHandle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED;
|
||||
I2cHandle.Init.OwnAddress1 = 0;
|
||||
I2cHandle.Init.OwnAddress2 = 0;
|
||||
I2cHandle.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
|
||||
HAL_I2C_Init(&I2cHandle);
|
||||
}
|
||||
|
||||
inline int i2c_start(i2c_t *obj) {
|
||||
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
|
||||
int timeout;
|
||||
|
||||
I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
|
||||
|
||||
// Clear Acknowledge failure flag
|
||||
__HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_AF);
|
||||
|
||||
// Wait the STOP condition has been previously correctly sent
|
||||
timeout = FLAG_TIMEOUT;
|
||||
while ((i2c->CR2 & I2C_CR2_STOP) == I2C_CR2_STOP){
|
||||
if ((timeout--) == 0) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
// Generate the START condition
|
||||
i2c->CR2 |= I2C_CR2_START;
|
||||
|
||||
// Wait the START condition has been correctly sent
|
||||
timeout = FLAG_TIMEOUT;
|
||||
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY) == RESET) {
|
||||
if ((timeout--) == 0) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
inline int i2c_stop(i2c_t *obj) {
|
||||
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
|
||||
|
||||
// Generate the STOP condition
|
||||
i2c->CR2 |= I2C_CR2_STOP;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
|
||||
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
|
||||
I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
|
||||
int timeout;
|
||||
int count;
|
||||
int value;
|
||||
|
||||
// Update CR2 register
|
||||
i2c->CR2 = (i2c->CR2 & (uint32_t)~((uint32_t)(I2C_CR2_SADD | I2C_CR2_NBYTES | I2C_CR2_RELOAD | I2C_CR2_AUTOEND | I2C_CR2_RD_WRN | I2C_CR2_START | I2C_CR2_STOP)))
|
||||
| (uint32_t)(((uint32_t)address & I2C_CR2_SADD) | (((uint32_t)length << 16) & I2C_CR2_NBYTES) | (uint32_t)I2C_SOFTEND_MODE | (uint32_t)I2C_GENERATE_START_READ);
|
||||
|
||||
// Read all bytes
|
||||
for (count = 0; count < length; count++) {
|
||||
value = i2c_byte_read(obj, 0);
|
||||
data[count] = (char)value;
|
||||
}
|
||||
|
||||
// Wait transfer complete
|
||||
timeout = FLAG_TIMEOUT;
|
||||
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TC) == RESET) {
|
||||
timeout--;
|
||||
if (timeout == 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
__HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_TC);
|
||||
|
||||
// If not repeated start, send stop.
|
||||
if (stop) {
|
||||
i2c_stop(obj);
|
||||
// Wait until STOPF flag is set
|
||||
timeout = FLAG_TIMEOUT;
|
||||
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_STOPF) == RESET) {
|
||||
timeout--;
|
||||
if (timeout == 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
// Clear STOP Flag
|
||||
__HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_STOPF);
|
||||
}
|
||||
|
||||
return length;
|
||||
}
|
||||
|
||||
int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
|
||||
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
|
||||
I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
|
||||
int timeout;
|
||||
int count;
|
||||
|
||||
// Update CR2 register
|
||||
i2c->CR2 = (i2c->CR2 & (uint32_t)~((uint32_t)(I2C_CR2_SADD | I2C_CR2_NBYTES | I2C_CR2_RELOAD | I2C_CR2_AUTOEND | I2C_CR2_RD_WRN | I2C_CR2_START | I2C_CR2_STOP)))
|
||||
| (uint32_t)(((uint32_t)address & I2C_CR2_SADD) | (((uint32_t)length << 16) & I2C_CR2_NBYTES) | (uint32_t)I2C_SOFTEND_MODE | (uint32_t)I2C_GENERATE_START_WRITE);
|
||||
|
||||
for (count = 0; count < length; count++) {
|
||||
i2c_byte_write(obj, data[count]);
|
||||
}
|
||||
|
||||
// Wait transfer complete
|
||||
timeout = FLAG_TIMEOUT;
|
||||
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TC) == RESET) {
|
||||
timeout--;
|
||||
if (timeout == 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
__HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_TC);
|
||||
|
||||
// If not repeated start, send stop
|
||||
if (stop) {
|
||||
i2c_stop(obj);
|
||||
// Wait until STOPF flag is set
|
||||
timeout = FLAG_TIMEOUT;
|
||||
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_STOPF) == RESET) {
|
||||
timeout--;
|
||||
if (timeout == 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
// Clear STOP Flag
|
||||
__HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_STOPF);
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
int i2c_byte_read(i2c_t *obj, int last) {
|
||||
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
|
||||
int timeout;
|
||||
|
||||
// Wait until the byte is received
|
||||
timeout = FLAG_TIMEOUT;
|
||||
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_RXNE) == RESET) {
|
||||
if ((timeout--) == 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
return (int)i2c->RXDR;
|
||||
}
|
||||
|
||||
int i2c_byte_write(i2c_t *obj, int data) {
|
||||
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
|
||||
int timeout;
|
||||
|
||||
// Wait until the previous byte is transmitted
|
||||
timeout = FLAG_TIMEOUT;
|
||||
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TXIS) == RESET) {
|
||||
if ((timeout--) == 0) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
i2c->TXDR = (uint8_t)data;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void i2c_reset(i2c_t *obj) {
|
||||
int timeout;
|
||||
|
||||
// Wait before reset
|
||||
timeout = LONG_TIMEOUT;
|
||||
while ((__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY)) && (timeout-- != 0));
|
||||
|
||||
if (obj->i2c == I2C_1) {
|
||||
__I2C1_FORCE_RESET();
|
||||
__I2C1_RELEASE_RESET();
|
||||
}
|
||||
#if defined(I2C2_BASE)
|
||||
if (obj->i2c == I2C_2) {
|
||||
__I2C2_FORCE_RESET();
|
||||
__I2C2_RELEASE_RESET();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
#if DEVICE_I2CSLAVE
|
||||
|
||||
void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask) {
|
||||
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
|
||||
uint16_t tmpreg = 0;
|
||||
|
||||
// disable
|
||||
i2c->OAR1 &= (uint32_t)(~I2C_OAR1_OA1EN);
|
||||
// Get the old register value
|
||||
tmpreg = i2c->OAR1;
|
||||
// Reset address bits
|
||||
tmpreg &= 0xFC00;
|
||||
// Set new address
|
||||
tmpreg |= (uint16_t)((uint16_t)address & (uint16_t)0x00FE); // 7-bits
|
||||
// Store the new register value
|
||||
i2c->OAR1 = tmpreg;
|
||||
// enable
|
||||
i2c->OAR1 |= I2C_OAR1_OA1EN;
|
||||
}
|
||||
|
||||
void i2c_slave_mode(i2c_t *obj, int enable_slave) {
|
||||
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
|
||||
uint16_t tmpreg;
|
||||
|
||||
// Get the old register value
|
||||
tmpreg = i2c->OAR1;
|
||||
|
||||
// Enable / disable slave
|
||||
if (enable_slave == 1) {
|
||||
tmpreg |= I2C_OAR1_OA1EN;
|
||||
} else {
|
||||
tmpreg &= (uint32_t)(~I2C_OAR1_OA1EN);
|
||||
}
|
||||
|
||||
// Set new mode
|
||||
i2c->OAR1 = tmpreg;
|
||||
}
|
||||
|
||||
// See I2CSlave.h
|
||||
#define NoData 0 // the slave has not been addressed
|
||||
#define ReadAddressed 1 // the master has requested a read from this slave (slave = transmitter)
|
||||
#define WriteGeneral 2 // the master is writing to all slave
|
||||
#define WriteAddressed 3 // the master is writing to this slave (slave = receiver)
|
||||
|
||||
int i2c_slave_receive(i2c_t *obj) {
|
||||
I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
|
||||
int retValue = NoData;
|
||||
|
||||
if (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY) == 1) {
|
||||
if (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_ADDR) == 1) {
|
||||
if (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_DIR) == 1)
|
||||
retValue = ReadAddressed;
|
||||
else
|
||||
retValue = WriteAddressed;
|
||||
|
||||
__HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_ADDR);
|
||||
}
|
||||
}
|
||||
|
||||
return (retValue);
|
||||
}
|
||||
|
||||
int i2c_slave_read(i2c_t *obj, char *data, int length) {
|
||||
char size = 0;
|
||||
|
||||
while (size < length) data[size++] = (char)i2c_byte_read(obj, 0);
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
int i2c_slave_write(i2c_t *obj, const char *data, int length) {
|
||||
char size = 0;
|
||||
I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
|
||||
|
||||
do {
|
||||
i2c_byte_write(obj, data[size]);
|
||||
size++;
|
||||
} while (size < length);
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
|
||||
#endif // DEVICE_I2CSLAVE
|
||||
|
||||
#endif // DEVICE_I2C
|
|
@ -0,0 +1,86 @@
|
|||
/* mbed Microcontroller Library
|
||||
*******************************************************************************
|
||||
* Copyright (c) 2015, STMicroelectronics
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of STMicroelectronics nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*******************************************************************************
|
||||
*/
|
||||
#ifndef MBED_I2C_DEVICE_H
|
||||
#define MBED_I2C_DEVICE_H
|
||||
|
||||
#include "cmsis.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#ifdef DEVICE_I2C
|
||||
|
||||
#if defined I2C1_BASE
|
||||
#define I2C1_EV_IRQn I2C1_IRQn
|
||||
#define I2C1_ER_IRQn I2C1_IRQn
|
||||
#endif
|
||||
#if defined I2C2_BASE
|
||||
#define I2C2_EV_IRQn I2C2_IRQn
|
||||
#define I2C2_ER_IRQn I2C2_IRQn
|
||||
#endif
|
||||
#if defined I2C3_BASE
|
||||
#define I2C3_EV_IRQn I2C3_IRQn
|
||||
#define I2C3_ER_IRQn I2C3_IRQn
|
||||
#endif
|
||||
|
||||
#define I2C_IT_ALL (I2C_IT_ERRI|I2C_IT_TCI|I2C_IT_STOPI|I2C_IT_NACKI|I2C_IT_ADDRI|I2C_IT_RXI|I2C_IT_TXI)
|
||||
|
||||
|
||||
/* Define IP version */
|
||||
#define I2C_IP_VERSION_V2
|
||||
|
||||
/* Family specifc settings for clock source */
|
||||
#define I2CAPI_I2C1_CLKSRC RCC_I2C1CLKSOURCE_SYSCLK
|
||||
|
||||
/* Provide the suitable timing depending on requested frequencie */
|
||||
inline uint32_t get_i2c_timing(int hz)
|
||||
{
|
||||
uint32_t tim = 0;
|
||||
|
||||
switch (hz) {
|
||||
case 100000:
|
||||
tim = 0x10805E89; // Standard mode with Rise Time = 400ns and Fall Time = 100ns
|
||||
break;
|
||||
case 400000:
|
||||
tim = 0x00901850; // Fast mode with Rise Time = 250ns and Fall Time = 100ns
|
||||
break;
|
||||
case 1000000:
|
||||
tim = 0x00700818; // Fast mode Plus with Rise Time = 60ns and Fall Time = 100ns
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return tim;
|
||||
}
|
||||
|
||||
#endif // DEVICE_I2C
|
||||
|
||||
#endif
|
|
@ -1,529 +0,0 @@
|
|||
/* mbed Microcontroller Library
|
||||
*******************************************************************************
|
||||
* Copyright (c) 2016, STMicroelectronics
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of STMicroelectronics nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*******************************************************************************
|
||||
*/
|
||||
#include "mbed_assert.h"
|
||||
#include "i2c_api.h"
|
||||
|
||||
#if DEVICE_I2C
|
||||
|
||||
#include "cmsis.h"
|
||||
#include "pinmap.h"
|
||||
#include "PeripheralPins.h"
|
||||
|
||||
/* Timeout values for flags and events waiting loops. These timeouts are
|
||||
not based on accurate values, they just guarantee that the application will
|
||||
not remain stuck if the I2C communication is corrupted. */
|
||||
#define FLAG_TIMEOUT ((int)0x1000)
|
||||
#define LONG_TIMEOUT ((int)0x8000)
|
||||
|
||||
I2C_HandleTypeDef I2cHandle;
|
||||
|
||||
int i2c1_inited = 0;
|
||||
int i2c2_inited = 0;
|
||||
int i2c3_inited = 0;
|
||||
int fmpi2c1_inited = 0;
|
||||
|
||||
void i2c_init(i2c_t *obj, PinName sda, PinName scl)
|
||||
{
|
||||
// Determine the I2C to use
|
||||
I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA);
|
||||
I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL);
|
||||
|
||||
obj->i2c = (I2CName)pinmap_merge(i2c_sda, i2c_scl);
|
||||
MBED_ASSERT(obj->i2c != (I2CName)NC);
|
||||
|
||||
// Enable I2C1 clock and pinout if not done
|
||||
if ((obj->i2c == I2C_1) && !i2c1_inited) {
|
||||
i2c1_inited = 1;
|
||||
__I2C1_CLK_ENABLE();
|
||||
// Configure I2C pins
|
||||
pinmap_pinout(sda, PinMap_I2C_SDA);
|
||||
pinmap_pinout(scl, PinMap_I2C_SCL);
|
||||
pin_mode(sda, OpenDrain);
|
||||
pin_mode(scl, OpenDrain);
|
||||
}
|
||||
// Enable I2C2 clock and pinout if not done
|
||||
if ((obj->i2c == I2C_2) && !i2c2_inited) {
|
||||
i2c2_inited = 1;
|
||||
__I2C2_CLK_ENABLE();
|
||||
// Configure I2C pins
|
||||
pinmap_pinout(sda, PinMap_I2C_SDA);
|
||||
pinmap_pinout(scl, PinMap_I2C_SCL);
|
||||
pin_mode(sda, OpenDrain);
|
||||
pin_mode(scl, OpenDrain);
|
||||
}
|
||||
#if defined I2C3_BASE
|
||||
// Enable I2C3 clock and pinout if not done
|
||||
if ((obj->i2c == I2C_3) && !i2c3_inited) {
|
||||
i2c3_inited = 1;
|
||||
__I2C3_CLK_ENABLE();
|
||||
// Configure I2C pins
|
||||
pinmap_pinout(sda, PinMap_I2C_SDA);
|
||||
pinmap_pinout(scl, PinMap_I2C_SCL);
|
||||
pin_mode(sda, OpenDrain);
|
||||
pin_mode(scl, OpenDrain);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined FMPI2C1_BASE
|
||||
// Enable I2C3 clock and pinout if not done
|
||||
if ((obj->i2c == FMPI2C_1) && !fmpi2c1_inited) {
|
||||
fmpi2c1_inited = 1;
|
||||
__HAL_RCC_FMPI2C1_CLK_ENABLE();
|
||||
// Configure I2C pins
|
||||
pinmap_pinout(sda, PinMap_I2C_SDA);
|
||||
pinmap_pinout(scl, PinMap_I2C_SCL);
|
||||
pin_mode(sda, OpenDrain);
|
||||
pin_mode(scl, OpenDrain);
|
||||
}
|
||||
#endif
|
||||
|
||||
// Reset to clear pending flags if any
|
||||
i2c_reset(obj);
|
||||
|
||||
// I2C configuration
|
||||
i2c_frequency(obj, 100000); // 100 kHz per default
|
||||
|
||||
// I2C master by default
|
||||
obj->slave = 0;
|
||||
}
|
||||
|
||||
void i2c_frequency(i2c_t *obj, int hz)
|
||||
{
|
||||
MBED_ASSERT((hz > 0) && (hz <= 400000));
|
||||
I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
|
||||
int timeout;
|
||||
|
||||
// wait before init
|
||||
timeout = LONG_TIMEOUT;
|
||||
while ((__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY)) && (timeout-- != 0));
|
||||
|
||||
// I2C configuration
|
||||
I2cHandle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
|
||||
I2cHandle.Init.ClockSpeed = hz;
|
||||
I2cHandle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED;
|
||||
I2cHandle.Init.DutyCycle = I2C_DUTYCYCLE_2;
|
||||
I2cHandle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED;
|
||||
I2cHandle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED;
|
||||
I2cHandle.Init.OwnAddress1 = 0;
|
||||
I2cHandle.Init.OwnAddress2 = 0;
|
||||
HAL_I2C_Init(&I2cHandle);
|
||||
if (obj->slave) {
|
||||
/* Enable Address Acknowledge */
|
||||
I2cHandle.Instance->CR1 |= I2C_CR1_ACK;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
inline int i2c_start(i2c_t *obj)
|
||||
{
|
||||
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
|
||||
int timeout;
|
||||
|
||||
I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
|
||||
|
||||
// Clear Acknowledge failure flag
|
||||
__HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_AF);
|
||||
|
||||
// Wait the STOP condition has been previously correctly sent
|
||||
// This timeout can be avoid in some specific cases by simply clearing the STOP bit
|
||||
timeout = FLAG_TIMEOUT;
|
||||
while ((i2c->CR1 & I2C_CR1_STOP) == I2C_CR1_STOP) {
|
||||
if ((timeout--) == 0) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
// Generate the START condition
|
||||
i2c->CR1 |= I2C_CR1_START;
|
||||
|
||||
// Wait the START condition has been correctly sent
|
||||
timeout = FLAG_TIMEOUT;
|
||||
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_SB) == RESET) {
|
||||
if ((timeout--) == 0) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
inline int i2c_stop(i2c_t *obj)
|
||||
{
|
||||
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
|
||||
|
||||
// Generate the STOP condition
|
||||
i2c->CR1 |= I2C_CR1_STOP;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int i2c_read(i2c_t *obj, int address, char *data, int length, int stop)
|
||||
{
|
||||
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
|
||||
I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
|
||||
int timeout;
|
||||
int count;
|
||||
int value;
|
||||
|
||||
i2c_start(obj);
|
||||
|
||||
// Wait until SB flag is set
|
||||
timeout = FLAG_TIMEOUT;
|
||||
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_SB) == RESET) {
|
||||
timeout--;
|
||||
if (timeout == 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
i2c->DR = __HAL_I2C_7BIT_ADD_READ(address);
|
||||
|
||||
|
||||
// Wait address is acknowledged
|
||||
timeout = FLAG_TIMEOUT;
|
||||
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_ADDR) == RESET) {
|
||||
timeout--;
|
||||
if (timeout == 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
__HAL_I2C_CLEAR_ADDRFLAG(&I2cHandle);
|
||||
|
||||
// Read all bytes except last one
|
||||
for (count = 0; count < (length - 1); count++) {
|
||||
value = i2c_byte_read(obj, 0);
|
||||
data[count] = (char)value;
|
||||
}
|
||||
|
||||
// If not repeated start, send stop.
|
||||
// Warning: must be done BEFORE the data is read.
|
||||
if (stop) {
|
||||
i2c_stop(obj);
|
||||
}
|
||||
|
||||
// Read the last byte
|
||||
value = i2c_byte_read(obj, 1);
|
||||
data[count] = (char)value;
|
||||
|
||||
return length;
|
||||
}
|
||||
|
||||
int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop)
|
||||
{
|
||||
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
|
||||
I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
|
||||
int timeout;
|
||||
int count;
|
||||
|
||||
i2c_start(obj);
|
||||
|
||||
// Wait until SB flag is set
|
||||
timeout = FLAG_TIMEOUT;
|
||||
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_SB) == RESET) {
|
||||
timeout--;
|
||||
if (timeout == 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
i2c->DR = __HAL_I2C_7BIT_ADD_WRITE(address);
|
||||
|
||||
|
||||
// Wait address is acknowledged
|
||||
timeout = FLAG_TIMEOUT;
|
||||
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_ADDR) == RESET) {
|
||||
timeout--;
|
||||
if (timeout == 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
__HAL_I2C_CLEAR_ADDRFLAG(&I2cHandle);
|
||||
|
||||
for (count = 0; count < length; count++) {
|
||||
if (i2c_byte_write(obj, data[count]) != 1) {
|
||||
i2c_stop(obj);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
// If not repeated start, send stop.
|
||||
if (stop) {
|
||||
i2c_stop(obj);
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
int i2c_byte_read(i2c_t *obj, int last)
|
||||
{
|
||||
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
|
||||
int timeout;
|
||||
|
||||
if (last) {
|
||||
// Don't acknowledge the last byte
|
||||
i2c->CR1 &= ~I2C_CR1_ACK;
|
||||
} else {
|
||||
// Acknowledge the byte
|
||||
i2c->CR1 |= I2C_CR1_ACK;
|
||||
}
|
||||
|
||||
// Wait until the byte is received
|
||||
timeout = FLAG_TIMEOUT;
|
||||
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_RXNE) == RESET) {
|
||||
if ((timeout--) == 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
return (int)i2c->DR;
|
||||
}
|
||||
|
||||
int i2c_byte_write(i2c_t *obj, int data)
|
||||
{
|
||||
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
|
||||
int timeout;
|
||||
|
||||
i2c->DR = (uint8_t)data;
|
||||
|
||||
// Wait until the byte is transmitted
|
||||
timeout = FLAG_TIMEOUT;
|
||||
while ((__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TXE) == RESET) &&
|
||||
(__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BTF) == RESET)) {
|
||||
if ((timeout--) == 0) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void i2c_reset(i2c_t *obj)
|
||||
{
|
||||
int timeout;
|
||||
|
||||
// wait before reset
|
||||
timeout = LONG_TIMEOUT;
|
||||
while ((__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY)) && (timeout-- != 0));
|
||||
|
||||
if (obj->i2c == I2C_1) {
|
||||
__I2C1_FORCE_RESET();
|
||||
__I2C1_RELEASE_RESET();
|
||||
}
|
||||
if (obj->i2c == I2C_2) {
|
||||
__I2C2_FORCE_RESET();
|
||||
__I2C2_RELEASE_RESET();
|
||||
}
|
||||
#if defined I2C3_BASE
|
||||
if (obj->i2c == I2C_3) {
|
||||
__I2C3_FORCE_RESET();
|
||||
__I2C3_RELEASE_RESET();
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined FMPI2C1_BASE
|
||||
if (obj->i2c == FMPI2C_1) {
|
||||
__HAL_RCC_FMPI2C1_FORCE_RESET();
|
||||
__HAL_RCC_FMPI2C1_RELEASE_RESET();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
#if DEVICE_I2CSLAVE
|
||||
|
||||
void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask)
|
||||
{
|
||||
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
|
||||
uint16_t tmpreg = 0;
|
||||
|
||||
// Get the old register value
|
||||
tmpreg = i2c->OAR1;
|
||||
// Reset address bits
|
||||
tmpreg &= 0xFC00;
|
||||
// Set new address
|
||||
tmpreg |= (uint16_t)((uint16_t)address & (uint16_t)0x00FE); // 7-bits
|
||||
// Store the new register value
|
||||
i2c->OAR1 = tmpreg;
|
||||
}
|
||||
|
||||
void i2c_slave_mode(i2c_t *obj, int enable_slave)
|
||||
{
|
||||
I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
|
||||
if (enable_slave) {
|
||||
obj->slave = 1;
|
||||
/* Enable Address Acknowledge */
|
||||
I2cHandle.Instance->CR1 |= I2C_CR1_ACK;
|
||||
}
|
||||
}
|
||||
|
||||
// See I2CSlave.h
|
||||
#define NoData 0 // the slave has not been addressed
|
||||
#define ReadAddressed 1 // the master has requested a read from this slave (slave = transmitter)
|
||||
#define WriteGeneral 2 // the master is writing to all slave
|
||||
#define WriteAddressed 3 // the master is writing to this slave (slave = receiver)
|
||||
|
||||
int i2c_slave_receive(i2c_t *obj)
|
||||
{
|
||||
int retValue = NoData;
|
||||
|
||||
if (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY) == 1) {
|
||||
if (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_ADDR) == 1) {
|
||||
if (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TRA) == 1)
|
||||
retValue = ReadAddressed;
|
||||
else
|
||||
retValue = WriteAddressed;
|
||||
|
||||
__HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_ADDR);
|
||||
}
|
||||
}
|
||||
|
||||
return (retValue);
|
||||
}
|
||||
|
||||
int i2c_slave_read(i2c_t *obj, char *data, int length)
|
||||
{
|
||||
uint32_t Timeout;
|
||||
int size = 0;
|
||||
|
||||
I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
|
||||
|
||||
while (length > 0) {
|
||||
/* Wait until RXNE flag is set */
|
||||
// Wait until the byte is received
|
||||
Timeout = FLAG_TIMEOUT;
|
||||
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_RXNE) == RESET) {
|
||||
Timeout--;
|
||||
if (Timeout == 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/* Read data from DR */
|
||||
(*data++) = I2cHandle.Instance->DR;
|
||||
length--;
|
||||
size++;
|
||||
|
||||
if ((__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BTF) == SET) && (length != 0)) {
|
||||
/* Read data from DR */
|
||||
(*data++) = I2cHandle.Instance->DR;
|
||||
length--;
|
||||
size++;
|
||||
}
|
||||
}
|
||||
|
||||
/* Wait until STOP flag is set */
|
||||
Timeout = FLAG_TIMEOUT;
|
||||
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_STOPF) == RESET) {
|
||||
Timeout--;
|
||||
if (Timeout == 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/* Clear STOP flag */
|
||||
__HAL_I2C_CLEAR_STOPFLAG(&I2cHandle);
|
||||
|
||||
/* Wait until BUSY flag is reset */
|
||||
Timeout = FLAG_TIMEOUT;
|
||||
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY) == SET) {
|
||||
Timeout--;
|
||||
if (Timeout == 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
int i2c_slave_write(i2c_t *obj, const char *data, int length)
|
||||
{
|
||||
uint32_t Timeout;
|
||||
int size = 0;
|
||||
|
||||
I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
|
||||
|
||||
while (length > 0) {
|
||||
/* Wait until TXE flag is set */
|
||||
Timeout = FLAG_TIMEOUT;
|
||||
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TXE) == RESET) {
|
||||
Timeout--;
|
||||
if (Timeout == 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* Write data to DR */
|
||||
I2cHandle.Instance->DR = (*data++);
|
||||
length--;
|
||||
size++;
|
||||
|
||||
if ((__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BTF) == SET) && (length != 0)) {
|
||||
/* Write data to DR */
|
||||
I2cHandle.Instance->DR = (*data++);
|
||||
length--;
|
||||
size++;
|
||||
}
|
||||
}
|
||||
|
||||
/* Wait until AF flag is set */
|
||||
Timeout = FLAG_TIMEOUT;
|
||||
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_AF) == RESET) {
|
||||
Timeout--;
|
||||
if (Timeout == 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* Clear AF flag */
|
||||
__HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_AF);
|
||||
|
||||
|
||||
/* Wait until BUSY flag is reset */
|
||||
Timeout = FLAG_TIMEOUT;
|
||||
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY) == SET) {
|
||||
Timeout--;
|
||||
if (Timeout == 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
I2cHandle.State = HAL_I2C_STATE_READY;
|
||||
|
||||
/* Process Unlocked */
|
||||
__HAL_UNLOCK(&I2cHandle);
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
|
||||
#endif // DEVICE_I2CSLAVE
|
||||
|
||||
#endif // DEVICE_I2C
|
|
@ -0,0 +1,47 @@
|
|||
/* mbed Microcontroller Library
|
||||
*******************************************************************************
|
||||
* Copyright (c) 2015, STMicroelectronics
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of STMicroelectronics nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*******************************************************************************
|
||||
*/
|
||||
#ifndef MBED_I2C_DEVICE_H
|
||||
#define MBED_I2C_DEVICE_H
|
||||
|
||||
#include "cmsis.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#ifdef DEVICE_I2C
|
||||
|
||||
#define I2C_IP_VERSION_V1
|
||||
|
||||
#define I2C_IT_ALL (I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR)
|
||||
|
||||
#endif // DEVICE_I2C
|
||||
|
||||
#endif
|
|
@ -99,8 +99,30 @@ struct spi_s {
|
|||
};
|
||||
|
||||
struct i2c_s {
|
||||
/* The 1st 2 members I2CName i2c
|
||||
* and I2C_HandleTypeDef handle should
|
||||
* be kept as the first members of this struct
|
||||
*/
|
||||
I2CName i2c;
|
||||
uint32_t slave;
|
||||
I2C_HandleTypeDef handle;
|
||||
uint8_t index;
|
||||
int hz;
|
||||
PinName sda;
|
||||
PinName scl;
|
||||
IRQn_Type event_i2cIRQ;
|
||||
IRQn_Type error_i2cIRQ;
|
||||
uint8_t XferOperation;
|
||||
volatile uint8_t event;
|
||||
#if DEVICE_I2CSLAVE
|
||||
uint8_t slave;
|
||||
volatile uint8_t pending_slave_tx_master_rx;
|
||||
volatile uint8_t pending_slave_rx_maxter_tx;
|
||||
#endif
|
||||
#if DEVICE_I2C_ASYNCH
|
||||
uint32_t address;
|
||||
uint8_t stop;
|
||||
uint8_t available_events;
|
||||
#endif
|
||||
};
|
||||
|
||||
struct pwmout_s {
|
||||
|
|
|
@ -66,11 +66,6 @@ struct dac_s {
|
|||
uint32_t channel;
|
||||
};
|
||||
|
||||
struct i2c_s {
|
||||
I2CName i2c;
|
||||
uint32_t slave;
|
||||
};
|
||||
|
||||
#include "common_objects.h"
|
||||
#include "gpio_object.h"
|
||||
|
||||
|
|
|
@ -66,11 +66,6 @@ struct dac_s {
|
|||
uint32_t channel;
|
||||
};
|
||||
|
||||
struct i2c_s {
|
||||
I2CName i2c;
|
||||
uint32_t slave;
|
||||
};
|
||||
|
||||
#include "common_objects.h"
|
||||
#include "gpio_object.h"
|
||||
|
||||
|
|
|
@ -66,11 +66,6 @@ struct dac_s {
|
|||
uint32_t channel;
|
||||
};
|
||||
|
||||
struct i2c_s {
|
||||
I2CName i2c;
|
||||
uint32_t slave;
|
||||
};
|
||||
|
||||
struct can_s {
|
||||
CANName can;
|
||||
int index;
|
||||
|
|
|
@ -66,11 +66,6 @@ struct dac_s {
|
|||
uint32_t channel;
|
||||
};
|
||||
|
||||
struct i2c_s {
|
||||
I2CName i2c;
|
||||
uint32_t slave;
|
||||
};
|
||||
|
||||
struct can_s {
|
||||
CANName can;
|
||||
int index;
|
||||
|
|
|
@ -66,11 +66,6 @@ struct dac_s {
|
|||
uint32_t channel;
|
||||
};
|
||||
|
||||
struct i2c_s {
|
||||
I2CName i2c;
|
||||
uint32_t slave;
|
||||
};
|
||||
|
||||
struct can_s {
|
||||
CANName can;
|
||||
int index;
|
||||
|
|
|
@ -66,11 +66,6 @@ struct dac_s {
|
|||
uint32_t channel;
|
||||
};
|
||||
|
||||
struct i2c_s {
|
||||
I2CName i2c;
|
||||
uint32_t slave;
|
||||
};
|
||||
|
||||
struct can_s {
|
||||
CANName can;
|
||||
int index;
|
||||
|
|
|
@ -66,11 +66,6 @@ struct dac_s {
|
|||
uint32_t channel;
|
||||
};
|
||||
|
||||
struct i2c_s {
|
||||
I2CName i2c;
|
||||
uint32_t slave;
|
||||
};
|
||||
|
||||
struct can_s {
|
||||
CANName can;
|
||||
int index;
|
||||
|
|
|
@ -82,6 +82,34 @@ struct serial_s {
|
|||
#endif
|
||||
};
|
||||
|
||||
struct i2c_s {
|
||||
/* The 1st 2 members I2CName i2c
|
||||
* and I2C_HandleTypeDef handle should
|
||||
* be kept as the first members of this struct
|
||||
* to ensure i2c_get_obj to work as expected
|
||||
*/
|
||||
I2CName i2c;
|
||||
I2C_HandleTypeDef handle;
|
||||
uint8_t index;
|
||||
int hz;
|
||||
PinName sda;
|
||||
PinName scl;
|
||||
IRQn_Type event_i2cIRQ;
|
||||
IRQn_Type error_i2cIRQ;
|
||||
uint32_t XferOperation;
|
||||
volatile uint8_t event;
|
||||
#if DEVICE_I2CSLAVE
|
||||
uint8_t slave;
|
||||
volatile uint8_t pending_slave_tx_master_rx;
|
||||
volatile uint8_t pending_slave_rx_maxter_tx;
|
||||
#endif
|
||||
#if DEVICE_I2C_ASYNCH
|
||||
uint32_t address;
|
||||
uint8_t stop;
|
||||
uint8_t available_events;
|
||||
#endif
|
||||
};
|
||||
|
||||
#include "gpio_object.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
|
|
@ -1,455 +0,0 @@
|
|||
/* mbed Microcontroller Library
|
||||
*******************************************************************************
|
||||
* Copyright (c) 2014, STMicroelectronics
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of STMicroelectronics nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*******************************************************************************
|
||||
*/
|
||||
#include "mbed_assert.h"
|
||||
#include "i2c_api.h"
|
||||
|
||||
#if DEVICE_I2C
|
||||
|
||||
#include "cmsis.h"
|
||||
#include "pinmap.h"
|
||||
#include "PeripheralPins.h"
|
||||
|
||||
/* Timeout values for flags and events waiting loops. These timeouts are
|
||||
not based on accurate values, they just guarantee that the application will
|
||||
not remain stuck if the I2C communication is corrupted. */
|
||||
#define FLAG_TIMEOUT ((int)0x4000)
|
||||
#define LONG_TIMEOUT ((int)0x8000)
|
||||
|
||||
I2C_HandleTypeDef I2cHandle;
|
||||
|
||||
int i2c1_inited = 0;
|
||||
int i2c2_inited = 0;
|
||||
int i2c3_inited = 0;
|
||||
|
||||
void i2c_init(i2c_t *obj, PinName sda, PinName scl)
|
||||
{
|
||||
// Determine the I2C to use
|
||||
I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA);
|
||||
I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL);
|
||||
|
||||
obj->i2c = (I2CName)pinmap_merge(i2c_sda, i2c_scl);
|
||||
MBED_ASSERT(obj->i2c != (I2CName)NC);
|
||||
|
||||
// Enable I2C clock and pinout if not done
|
||||
if ((obj->i2c == I2C_1) && !i2c1_inited) {
|
||||
i2c1_inited = 1;
|
||||
__HAL_RCC_I2C1_CONFIG(RCC_I2C1CLKSOURCE_SYSCLK);
|
||||
__I2C1_CLK_ENABLE();
|
||||
// Configure I2C1 pins
|
||||
pinmap_pinout(sda, PinMap_I2C_SDA);
|
||||
pinmap_pinout(scl, PinMap_I2C_SCL);
|
||||
pin_mode(sda, OpenDrain);
|
||||
pin_mode(scl, OpenDrain);
|
||||
}
|
||||
|
||||
#if defined(I2C2_BASE)
|
||||
if ((obj->i2c == I2C_2) && !i2c2_inited) {
|
||||
i2c2_inited = 1;
|
||||
__I2C2_CLK_ENABLE();
|
||||
// Configure I2C2 pins
|
||||
pinmap_pinout(sda, PinMap_I2C_SDA);
|
||||
pinmap_pinout(scl, PinMap_I2C_SCL);
|
||||
pin_mode(sda, OpenDrain);
|
||||
pin_mode(scl, OpenDrain);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(I2C3_BASE)
|
||||
if ((obj->i2c == I2C_3) && !i2c3_inited) {
|
||||
i2c3_inited = 1;
|
||||
__I2C3_CLK_ENABLE();
|
||||
// Configure I2C3 pins
|
||||
pinmap_pinout(sda, PinMap_I2C_SDA);
|
||||
pinmap_pinout(scl, PinMap_I2C_SCL);
|
||||
pin_mode(sda, OpenDrain);
|
||||
pin_mode(scl, OpenDrain);
|
||||
}
|
||||
#endif
|
||||
|
||||
// Reset to clear pending flags if any
|
||||
i2c_reset(obj);
|
||||
|
||||
// I2C configuration
|
||||
i2c_frequency(obj, 100000); // 100 kHz per default
|
||||
}
|
||||
|
||||
void i2c_frequency(i2c_t *obj, int hz)
|
||||
{
|
||||
uint32_t tim = 0;
|
||||
|
||||
MBED_ASSERT((hz == 100000) || (hz == 400000) || (hz == 1000000));
|
||||
|
||||
I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
|
||||
int timeout;
|
||||
|
||||
// wait before init
|
||||
timeout = LONG_TIMEOUT;
|
||||
while ((__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY)) && (timeout-- != 0));
|
||||
|
||||
/*
|
||||
Values calculated with I2C_Timing_Configuration_V1.0.1.xls file (see AN4235)
|
||||
* Standard mode (up to 100 kHz)
|
||||
* Fast Mode (up to 400 kHz)
|
||||
* Fast Mode Plus (up to 1 MHz)
|
||||
Below values obtained with:
|
||||
- I2C clock source = 64 MHz (System Clock w/ HSI) or 72 (System Clock w/ HSE)
|
||||
- Analog filter delay = ON
|
||||
- Digital filter coefficient = 0
|
||||
*/
|
||||
if (SystemCoreClock == 64000000) {
|
||||
switch (hz) {
|
||||
case 100000:
|
||||
tim = 0x10B17DB4; // Standard mode with Rise time = 120ns, Fall time = 120ns
|
||||
break;
|
||||
case 400000:
|
||||
tim = 0x00E22163; // Fast Mode with Rise time = 120ns, Fall time = 120ns
|
||||
break;
|
||||
case 1000000:
|
||||
tim = 0x00A00D1E; // Fast Mode Plus with Rise time = 120ns, Fall time = 10ns
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
} else if (SystemCoreClock == 72000000) {
|
||||
switch (hz) {
|
||||
case 100000:
|
||||
tim = 0x10D28DCB; // Standard mode with Rise time = 120ns, Fall time = 120ns
|
||||
break;
|
||||
case 400000:
|
||||
tim = 0x00F32571; // Fast Mode with Rise time = 120ns, Fall time = 120ns
|
||||
break;
|
||||
case 1000000:
|
||||
tim = 0x00C00D24; // Fast Mode Plus with Rise time = 120ns, Fall time = 10ns
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Enable the Fast Mode Plus capability
|
||||
if (hz == 1000000) {
|
||||
if (obj->i2c == I2C_1) {
|
||||
__HAL_SYSCFG_FASTMODEPLUS_ENABLE(HAL_SYSCFG_FASTMODEPLUS_I2C1);
|
||||
}
|
||||
#if defined(I2C2_BASE)
|
||||
if (obj->i2c == I2C_2) {
|
||||
__HAL_SYSCFG_FASTMODEPLUS_ENABLE(HAL_SYSCFG_FASTMODEPLUS_I2C2);
|
||||
}
|
||||
#endif
|
||||
#if defined(I2C3_BASE)
|
||||
if (obj->i2c == I2C_3) {
|
||||
__HAL_SYSCFG_FASTMODEPLUS_ENABLE(HAL_SYSCFG_FASTMODEPLUS_I2C3);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
// I2C configuration
|
||||
I2cHandle.Init.Timing = tim;
|
||||
I2cHandle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
|
||||
I2cHandle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED;
|
||||
I2cHandle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED;
|
||||
I2cHandle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED;
|
||||
I2cHandle.Init.OwnAddress1 = 0;
|
||||
I2cHandle.Init.OwnAddress2 = 0;
|
||||
I2cHandle.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
|
||||
HAL_I2C_Init(&I2cHandle);
|
||||
}
|
||||
|
||||
inline int i2c_start(i2c_t *obj)
|
||||
{
|
||||
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
|
||||
int timeout;
|
||||
|
||||
I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
|
||||
|
||||
// Clear Acknowledge failure flag
|
||||
__HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_AF);
|
||||
|
||||
// Wait the STOP condition has been previously correctly sent
|
||||
timeout = FLAG_TIMEOUT;
|
||||
while ((i2c->CR2 & I2C_CR2_STOP) == I2C_CR2_STOP){
|
||||
if ((timeout--) == 0) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
// Generate the START condition
|
||||
i2c->CR2 |= I2C_CR2_START;
|
||||
|
||||
// Wait the START condition has been correctly sent
|
||||
timeout = FLAG_TIMEOUT;
|
||||
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY) == RESET) {
|
||||
if ((timeout--) == 0) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
inline int i2c_stop(i2c_t *obj)
|
||||
{
|
||||
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
|
||||
|
||||
// Generate the STOP condition
|
||||
i2c->CR2 |= I2C_CR2_STOP;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int i2c_read(i2c_t *obj, int address, char *data, int length, int stop)
|
||||
{
|
||||
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
|
||||
I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
|
||||
int timeout;
|
||||
int count;
|
||||
int value;
|
||||
|
||||
/* update CR2 register */
|
||||
i2c->CR2 = (i2c->CR2 & (uint32_t)~((uint32_t)(I2C_CR2_SADD | I2C_CR2_NBYTES | I2C_CR2_RELOAD | I2C_CR2_AUTOEND | I2C_CR2_RD_WRN | I2C_CR2_START | I2C_CR2_STOP)))
|
||||
| (uint32_t)(((uint32_t)address & I2C_CR2_SADD) | (((uint32_t)length << 16) & I2C_CR2_NBYTES) | (uint32_t)I2C_SOFTEND_MODE | (uint32_t)I2C_GENERATE_START_READ);
|
||||
|
||||
// Read all bytes
|
||||
for (count = 0; count < length; count++) {
|
||||
value = i2c_byte_read(obj, 0);
|
||||
data[count] = (char)value;
|
||||
}
|
||||
|
||||
// Wait transfer complete
|
||||
timeout = LONG_TIMEOUT;
|
||||
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TC) == RESET) {
|
||||
timeout--;
|
||||
if (timeout == 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
__HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_TC);
|
||||
|
||||
// If not repeated start, send stop.
|
||||
if (stop) {
|
||||
i2c_stop(obj);
|
||||
/* Wait until STOPF flag is set */
|
||||
timeout = FLAG_TIMEOUT;
|
||||
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_STOPF) == RESET) {
|
||||
timeout--;
|
||||
if (timeout == 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
/* Clear STOP Flag */
|
||||
__HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_STOPF);
|
||||
}
|
||||
|
||||
return length;
|
||||
}
|
||||
|
||||
int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop)
|
||||
{
|
||||
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
|
||||
I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
|
||||
int timeout;
|
||||
int count;
|
||||
|
||||
/* update CR2 register */
|
||||
i2c->CR2 = (i2c->CR2 & (uint32_t)~((uint32_t)(I2C_CR2_SADD | I2C_CR2_NBYTES | I2C_CR2_RELOAD | I2C_CR2_AUTOEND | I2C_CR2_RD_WRN | I2C_CR2_START | I2C_CR2_STOP)))
|
||||
| (uint32_t)(((uint32_t)address & I2C_CR2_SADD) | (((uint32_t)length << 16) & I2C_CR2_NBYTES) | (uint32_t)I2C_SOFTEND_MODE | (uint32_t)I2C_GENERATE_START_WRITE);
|
||||
|
||||
for (count = 0; count < length; count++) {
|
||||
i2c_byte_write(obj, data[count]);
|
||||
}
|
||||
|
||||
// Wait transfer complete
|
||||
timeout = FLAG_TIMEOUT;
|
||||
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TC) == RESET) {
|
||||
timeout--;
|
||||
if (timeout == 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
__HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_TC);
|
||||
|
||||
// If not repeated start, send stop.
|
||||
if (stop) {
|
||||
i2c_stop(obj);
|
||||
/* Wait until STOPF flag is set */
|
||||
timeout = FLAG_TIMEOUT;
|
||||
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_STOPF) == RESET) {
|
||||
timeout--;
|
||||
if (timeout == 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
/* Clear STOP Flag */
|
||||
__HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_STOPF);
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
int i2c_byte_read(i2c_t *obj, int last)
|
||||
{
|
||||
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
|
||||
int timeout;
|
||||
|
||||
// Wait until the byte is received
|
||||
timeout = FLAG_TIMEOUT;
|
||||
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_RXNE) == RESET) {
|
||||
if ((timeout--) == 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
return (int)i2c->RXDR;
|
||||
}
|
||||
|
||||
int i2c_byte_write(i2c_t *obj, int data)
|
||||
{
|
||||
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
|
||||
int timeout;
|
||||
|
||||
// Wait until the previous byte is transmitted
|
||||
timeout = FLAG_TIMEOUT;
|
||||
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TXIS) == RESET) {
|
||||
if ((timeout--) == 0) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
i2c->TXDR = (uint8_t)data;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void i2c_reset(i2c_t *obj)
|
||||
{
|
||||
int timeout;
|
||||
|
||||
// wait before reset
|
||||
timeout = LONG_TIMEOUT;
|
||||
while ((__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY)) && (timeout-- != 0));
|
||||
|
||||
__I2C1_FORCE_RESET();
|
||||
__I2C1_RELEASE_RESET();
|
||||
}
|
||||
|
||||
#if DEVICE_I2CSLAVE
|
||||
|
||||
void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask)
|
||||
{
|
||||
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
|
||||
uint16_t tmpreg;
|
||||
|
||||
// disable
|
||||
i2c->OAR1 &= (uint32_t)(~I2C_OAR1_OA1EN);
|
||||
// Get the old register value
|
||||
tmpreg = i2c->OAR1;
|
||||
// Reset address bits
|
||||
tmpreg &= 0xFC00;
|
||||
// Set new address
|
||||
tmpreg |= (uint16_t)((uint16_t)address & (uint16_t)0x00FE); // 7-bits
|
||||
// Store the new register value
|
||||
i2c->OAR1 = tmpreg;
|
||||
// enable
|
||||
i2c->OAR1 |= I2C_OAR1_OA1EN;
|
||||
}
|
||||
|
||||
void i2c_slave_mode(i2c_t *obj, int enable_slave)
|
||||
{
|
||||
|
||||
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
|
||||
uint16_t tmpreg;
|
||||
|
||||
// Get the old register value
|
||||
tmpreg = i2c->OAR1;
|
||||
|
||||
// Enable / disable slave
|
||||
if (enable_slave == 1) {
|
||||
tmpreg |= I2C_OAR1_OA1EN;
|
||||
} else {
|
||||
tmpreg &= (uint32_t)(~I2C_OAR1_OA1EN);
|
||||
}
|
||||
|
||||
// Set new mode
|
||||
i2c->OAR1 = tmpreg;
|
||||
|
||||
}
|
||||
|
||||
// See I2CSlave.h
|
||||
#define NoData 0 // the slave has not been addressed
|
||||
#define ReadAddressed 1 // the master has requested a read from this slave (slave = transmitter)
|
||||
#define WriteGeneral 2 // the master is writing to all slave
|
||||
#define WriteAddressed 3 // the master is writing to this slave (slave = receiver)
|
||||
|
||||
int i2c_slave_receive(i2c_t *obj)
|
||||
{
|
||||
I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
|
||||
int retValue = NoData;
|
||||
|
||||
if (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY) == 1) {
|
||||
if (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_ADDR) == 1) {
|
||||
if (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_DIR) == 1)
|
||||
retValue = ReadAddressed;
|
||||
else
|
||||
retValue = WriteAddressed;
|
||||
__HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_ADDR);
|
||||
}
|
||||
}
|
||||
|
||||
return (retValue);
|
||||
}
|
||||
|
||||
int i2c_slave_read(i2c_t *obj, char *data, int length)
|
||||
{
|
||||
char size = 0;
|
||||
|
||||
while (size < length) data[size++] = (char)i2c_byte_read(obj, 0);
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
int i2c_slave_write(i2c_t *obj, const char *data, int length)
|
||||
{
|
||||
char size = 0;
|
||||
I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
|
||||
|
||||
do {
|
||||
i2c_byte_write(obj, data[size]);
|
||||
size++;
|
||||
} while (size < length);
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
|
||||
#endif // DEVICE_I2CSLAVE
|
||||
|
||||
#endif // DEVICE_I2C
|
|
@ -0,0 +1,102 @@
|
|||
/* mbed Microcontroller Library
|
||||
*******************************************************************************
|
||||
* Copyright (c) 2015, STMicroelectronics
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of STMicroelectronics nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*******************************************************************************
|
||||
*/
|
||||
#ifndef MBED_I2C_DEVICE_H
|
||||
#define MBED_I2C_DEVICE_H
|
||||
|
||||
#include "cmsis.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#ifdef DEVICE_I2C
|
||||
|
||||
#define I2C_IP_VERSION_V2
|
||||
|
||||
#define I2C_IT_ALL (I2C_IT_ERRI|I2C_IT_TCI|I2C_IT_STOPI|I2C_IT_NACKI|I2C_IT_ADDRI|I2C_IT_RXI|I2C_IT_TXI)
|
||||
|
||||
/* Family specifc settings for clock source */
|
||||
#define I2CAPI_I2C1_CLKSRC RCC_I2C1CLKSOURCE_SYSCLK
|
||||
#define I2CAPI_I2C2_CLKSRC RCC_I2C2CLKSOURCE_SYSCLK
|
||||
#define I2CAPI_I2C3_CLKSRC RCC_I2C3CLKSOURCE_SYSCLK
|
||||
|
||||
/* Provide the suitable timing depending on requested frequencie */
|
||||
inline uint32_t get_i2c_timing(int hz)
|
||||
{
|
||||
uint32_t tim = 0;
|
||||
/*
|
||||
Values calculated with I2C_Timing_Configuration_V1.0.1.xls file (see AN4235)
|
||||
* Standard mode (up to 100 kHz)
|
||||
* Fast Mode (up to 400 kHz)
|
||||
* Fast Mode Plus (up to 1 MHz)
|
||||
Below values obtained with:
|
||||
- I2C clock source = 64 MHz (System Clock w/ HSI) or 72 (System Clock w/ HSE)
|
||||
- Analog filter delay = ON
|
||||
- Digital filter coefficient = 0
|
||||
*/
|
||||
if (SystemCoreClock == 64000000) {
|
||||
switch (hz) {
|
||||
case 100000:
|
||||
tim = 0x10B17DB4; // Standard mode with Rise time = 120ns, Fall time = 120ns
|
||||
break;
|
||||
case 400000:
|
||||
tim = 0x00E22163; // Fast Mode with Rise time = 120ns, Fall time = 120ns
|
||||
break;
|
||||
case 1000000:
|
||||
tim = 0x00A00D1E; // Fast Mode Plus with Rise time = 120ns, Fall time = 10ns
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
} else if (SystemCoreClock == 72000000) {
|
||||
switch (hz) {
|
||||
case 100000:
|
||||
tim = 0x10D28DCB; // Standard mode with Rise time = 120ns, Fall time = 120ns
|
||||
break;
|
||||
case 400000:
|
||||
tim = 0x00F32571; // Fast Mode with Rise time = 120ns, Fall time = 120ns
|
||||
break;
|
||||
case 1000000:
|
||||
tim = 0x00C00D24; // Fast Mode Plus with Rise time = 120ns, Fall time = 10ns
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
return tim;
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // DEVICE_I2C
|
||||
|
||||
#endif
|
|
@ -91,6 +91,7 @@ struct i2c_s {
|
|||
I2CName i2c;
|
||||
I2C_HandleTypeDef handle;
|
||||
uint8_t index;
|
||||
int hz;
|
||||
PinName sda;
|
||||
PinName scl;
|
||||
IRQn_Type event_i2cIRQ;
|
||||
|
|
|
@ -0,0 +1,48 @@
|
|||
/* mbed Microcontroller Library
|
||||
*******************************************************************************
|
||||
* Copyright (c) 2015, STMicroelectronics
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of STMicroelectronics nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*******************************************************************************
|
||||
*/
|
||||
#ifndef MBED_I2C_DEVICE_H
|
||||
#define MBED_I2C_DEVICE_H
|
||||
|
||||
#include "cmsis.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#ifdef DEVICE_I2C
|
||||
|
||||
/* Define IP version */
|
||||
#define I2C_IP_VERSION_V1
|
||||
|
||||
#define I2C_IT_ALL (I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR)
|
||||
|
||||
#endif // DEVICE_I2C
|
||||
|
||||
#endif
|
|
@ -66,11 +66,6 @@ struct dac_s {
|
|||
uint32_t channel;
|
||||
};
|
||||
|
||||
struct i2c_s {
|
||||
I2CName i2c;
|
||||
uint32_t slave;
|
||||
};
|
||||
|
||||
struct can_s {
|
||||
CANName can;
|
||||
int index;
|
||||
|
|
|
@ -66,11 +66,6 @@ struct dac_s {
|
|||
uint32_t channel;
|
||||
};
|
||||
|
||||
struct i2c_s {
|
||||
I2CName i2c;
|
||||
uint32_t slave;
|
||||
};
|
||||
|
||||
struct can_s {
|
||||
CANName can;
|
||||
int index;
|
||||
|
|
|
@ -66,11 +66,6 @@ struct dac_s {
|
|||
uint32_t channel;
|
||||
};
|
||||
|
||||
struct i2c_s {
|
||||
I2CName i2c;
|
||||
uint32_t slave;
|
||||
};
|
||||
|
||||
struct can_s {
|
||||
CANName can;
|
||||
int index;
|
||||
|
|
|
@ -66,11 +66,6 @@ struct dac_s {
|
|||
uint32_t channel;
|
||||
};
|
||||
|
||||
struct i2c_s {
|
||||
I2CName i2c;
|
||||
uint32_t slave;
|
||||
};
|
||||
|
||||
struct can_s {
|
||||
CANName can;
|
||||
int index;
|
||||
|
|
|
@ -82,6 +82,34 @@ struct serial_s {
|
|||
#endif
|
||||
};
|
||||
|
||||
struct i2c_s {
|
||||
/* The 1st 2 members I2CName i2c
|
||||
* and I2C_HandleTypeDef handle should
|
||||
* be kept as the first members of this struct
|
||||
* to ensure i2c_get_obj to work as expected
|
||||
*/
|
||||
I2CName i2c;
|
||||
I2C_HandleTypeDef handle;
|
||||
uint8_t index;
|
||||
int hz;
|
||||
PinName sda;
|
||||
PinName scl;
|
||||
IRQn_Type event_i2cIRQ;
|
||||
IRQn_Type error_i2cIRQ;
|
||||
uint32_t XferOperation;
|
||||
volatile uint8_t event;
|
||||
#if DEVICE_I2CSLAVE
|
||||
uint8_t slave;
|
||||
volatile uint8_t pending_slave_tx_master_rx;
|
||||
volatile uint8_t pending_slave_rx_maxter_tx;
|
||||
#endif
|
||||
#if DEVICE_I2C_ASYNCH
|
||||
uint32_t address;
|
||||
uint8_t stop;
|
||||
uint8_t available_events;
|
||||
#endif
|
||||
};
|
||||
|
||||
#include "gpio_object.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
|
|
@ -1,443 +0,0 @@
|
|||
/* mbed Microcontroller Library
|
||||
*******************************************************************************
|
||||
* Copyright (c) 2015, STMicroelectronics
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of STMicroelectronics nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*******************************************************************************
|
||||
*/
|
||||
#include "mbed_assert.h"
|
||||
#include "i2c_api.h"
|
||||
|
||||
#if DEVICE_I2C
|
||||
|
||||
#include "cmsis.h"
|
||||
#include "pinmap.h"
|
||||
#include "PeripheralPins.h"
|
||||
#include "mbed_error.h"
|
||||
|
||||
/* Timeout values for flags and events waiting loops. These timeouts are
|
||||
not based on accurate values, they just guarantee that the application will
|
||||
not remain stuck if the I2C communication is corrupted. */
|
||||
#define FLAG_TIMEOUT ((int)0x4000)
|
||||
#define LONG_TIMEOUT ((int)0x8000)
|
||||
|
||||
I2C_HandleTypeDef I2cHandle;
|
||||
|
||||
int i2c1_inited = 0;
|
||||
int i2c2_inited = 0;
|
||||
int i2c3_inited = 0;
|
||||
int i2c4_inited = 0;
|
||||
|
||||
|
||||
void i2c_init(i2c_t *obj, PinName sda, PinName scl)
|
||||
{
|
||||
// Determine the I2C to use
|
||||
I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA);
|
||||
I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL);
|
||||
|
||||
obj->i2c = (I2CName)pinmap_merge(i2c_sda, i2c_scl);
|
||||
MBED_ASSERT(obj->i2c != (I2CName)NC);
|
||||
|
||||
// Enable I2C clock and pinout if not done
|
||||
if ((obj->i2c == I2C_1) && !i2c1_inited) {
|
||||
i2c1_inited = 1;
|
||||
__HAL_RCC_I2C1_CONFIG(RCC_I2C1CLKSOURCE_PCLK1);
|
||||
__HAL_RCC_I2C1_CLK_ENABLE();
|
||||
// Configure I2C1 pins
|
||||
pinmap_pinout(sda, PinMap_I2C_SDA);
|
||||
pinmap_pinout(scl, PinMap_I2C_SCL);
|
||||
pin_mode(sda, OpenDrain);
|
||||
pin_mode(scl, OpenDrain);
|
||||
}
|
||||
|
||||
#if defined(I2C2_BASE)
|
||||
if ((obj->i2c == I2C_2) && !i2c2_inited) {
|
||||
i2c2_inited = 1;
|
||||
__HAL_RCC_I2C2_CONFIG(RCC_I2C2CLKSOURCE_PCLK1);
|
||||
__HAL_RCC_I2C2_CLK_ENABLE();
|
||||
// Configure I2C2 pins
|
||||
pinmap_pinout(sda, PinMap_I2C_SDA);
|
||||
pinmap_pinout(scl, PinMap_I2C_SCL);
|
||||
pin_mode(sda, OpenDrain);
|
||||
pin_mode(scl, OpenDrain);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(I2C3_BASE)
|
||||
if ((obj->i2c == I2C_3) && !i2c3_inited) {
|
||||
i2c3_inited = 1;
|
||||
__HAL_RCC_I2C3_CONFIG(RCC_I2C3CLKSOURCE_PCLK1);
|
||||
__HAL_RCC_I2C3_CLK_ENABLE();
|
||||
// Configure I2C3 pins
|
||||
pinmap_pinout(sda, PinMap_I2C_SDA);
|
||||
pinmap_pinout(scl, PinMap_I2C_SCL);
|
||||
pin_mode(sda, OpenDrain);
|
||||
pin_mode(scl, OpenDrain);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(I2C4_BASE)
|
||||
if ((obj->i2c == I2C_4) && !i2c4_inited) {
|
||||
i2c4_inited = 1;
|
||||
__HAL_RCC_I2C4_CONFIG(RCC_I2C4CLKSOURCE_PCLK1);
|
||||
__HAL_RCC_I2C4_CLK_ENABLE();
|
||||
// Configure I2C4 pins
|
||||
pinmap_pinout(sda, PinMap_I2C_SDA);
|
||||
pinmap_pinout(scl, PinMap_I2C_SCL);
|
||||
pin_mode(sda, OpenDrain);
|
||||
pin_mode(scl, OpenDrain);
|
||||
}
|
||||
#endif
|
||||
|
||||
// Reset to clear pending flags if any
|
||||
i2c_reset(obj);
|
||||
|
||||
// I2C configuration
|
||||
i2c_frequency(obj, 100000); // 100 kHz per default
|
||||
}
|
||||
|
||||
void i2c_frequency(i2c_t *obj, int hz)
|
||||
{
|
||||
uint32_t tim = 0;
|
||||
|
||||
MBED_ASSERT((hz == 100000) || (hz == 400000) || (hz == 1000000));
|
||||
|
||||
I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
|
||||
int timeout;
|
||||
|
||||
// wait before init
|
||||
timeout = LONG_TIMEOUT;
|
||||
while ((__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY)) && (timeout-- != 0)) {}
|
||||
|
||||
/*
|
||||
Values calculated with I2C_Timing_Configuration tool (excel file)
|
||||
* Standard mode (up to 100 kHz)
|
||||
* Fast Mode (up to 400 kHz)
|
||||
* Fast Mode Plus (up to 1 MHz)
|
||||
Below values obtained with:
|
||||
- I2Cx clock source = APB1CLK = 54 MHz
|
||||
- Analog filter delay = ON
|
||||
- Digital filter coefficient = 0
|
||||
*/
|
||||
switch (hz) {
|
||||
case 100000:
|
||||
tim = 0x10916998; // Standard mode with Rise time = 120ns, Fall time = 120ns
|
||||
break;
|
||||
case 400000:
|
||||
tim = 0x00B11B54; // Fast Mode with Rise time = 120ns, Fall time = 120ns
|
||||
break;
|
||||
case 1000000:
|
||||
tim = 0x0090091B; // Fast Mode Plus with Rise time = 120ns, Fall time = 10ns
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
// I2C configuration
|
||||
I2cHandle.Init.Timing = tim;
|
||||
I2cHandle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
|
||||
I2cHandle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
|
||||
I2cHandle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
|
||||
I2cHandle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
|
||||
I2cHandle.Init.OwnAddress1 = 0;
|
||||
I2cHandle.Init.OwnAddress2 = 0;
|
||||
I2cHandle.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
|
||||
|
||||
if (HAL_I2C_Init(&I2cHandle) != HAL_OK) {
|
||||
error("Cannot initialize I2C");
|
||||
}
|
||||
}
|
||||
|
||||
inline int i2c_start(i2c_t *obj)
|
||||
{
|
||||
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
|
||||
int timeout;
|
||||
|
||||
I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
|
||||
|
||||
// Clear Acknowledge failure flag
|
||||
__HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_AF);
|
||||
|
||||
// Wait the STOP condition has been previously correctly sent
|
||||
timeout = FLAG_TIMEOUT;
|
||||
while ((i2c->CR2 & I2C_CR2_STOP) == I2C_CR2_STOP){
|
||||
if ((timeout--) == 0) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
// Generate the START condition
|
||||
i2c->CR2 |= I2C_CR2_START;
|
||||
|
||||
// Wait the START condition has been correctly sent
|
||||
timeout = FLAG_TIMEOUT;
|
||||
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY) == RESET) {
|
||||
if ((timeout--) == 0) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
inline int i2c_stop(i2c_t *obj)
|
||||
{
|
||||
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
|
||||
|
||||
// Generate the STOP condition
|
||||
i2c->CR2 |= I2C_CR2_STOP;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int i2c_read(i2c_t *obj, int address, char *data, int length, int stop)
|
||||
{
|
||||
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
|
||||
I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
|
||||
int timeout;
|
||||
int count;
|
||||
int value;
|
||||
|
||||
/* update CR2 register */
|
||||
i2c->CR2 = (i2c->CR2 & (uint32_t)~((uint32_t)(I2C_CR2_SADD | I2C_CR2_NBYTES | I2C_CR2_RELOAD | I2C_CR2_AUTOEND | I2C_CR2_RD_WRN | I2C_CR2_START | I2C_CR2_STOP)))
|
||||
| (uint32_t)(((uint32_t)address & I2C_CR2_SADD) | (((uint32_t)length << 16) & I2C_CR2_NBYTES) | (uint32_t)I2C_SOFTEND_MODE | (uint32_t)I2C_GENERATE_START_READ);
|
||||
|
||||
// Read all bytes
|
||||
for (count = 0; count < length; count++) {
|
||||
value = i2c_byte_read(obj, 0);
|
||||
data[count] = (char)value;
|
||||
}
|
||||
|
||||
// Wait transfer complete
|
||||
timeout = LONG_TIMEOUT;
|
||||
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TC) == RESET) {
|
||||
timeout--;
|
||||
if (timeout == 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
__HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_TC);
|
||||
|
||||
// If not repeated start, send stop.
|
||||
if (stop) {
|
||||
i2c_stop(obj);
|
||||
/* Wait until STOPF flag is set */
|
||||
timeout = FLAG_TIMEOUT;
|
||||
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_STOPF) == RESET) {
|
||||
timeout--;
|
||||
if (timeout == 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
/* Clear STOP Flag */
|
||||
__HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_STOPF);
|
||||
}
|
||||
|
||||
return length;
|
||||
}
|
||||
|
||||
int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop)
|
||||
{
|
||||
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
|
||||
I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
|
||||
int timeout;
|
||||
int count;
|
||||
|
||||
/* update CR2 register */
|
||||
i2c->CR2 = (i2c->CR2 & (uint32_t)~((uint32_t)(I2C_CR2_SADD | I2C_CR2_NBYTES | I2C_CR2_RELOAD | I2C_CR2_AUTOEND | I2C_CR2_RD_WRN | I2C_CR2_START | I2C_CR2_STOP)))
|
||||
| (uint32_t)(((uint32_t)address & I2C_CR2_SADD) | (((uint32_t)length << 16) & I2C_CR2_NBYTES) | (uint32_t)I2C_SOFTEND_MODE | (uint32_t)I2C_GENERATE_START_WRITE);
|
||||
|
||||
for (count = 0; count < length; count++) {
|
||||
i2c_byte_write(obj, data[count]);
|
||||
}
|
||||
|
||||
// Wait transfer complete
|
||||
timeout = FLAG_TIMEOUT;
|
||||
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TC) == RESET) {
|
||||
timeout--;
|
||||
if (timeout == 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
__HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_TC);
|
||||
|
||||
// If not repeated start, send stop.
|
||||
if (stop) {
|
||||
i2c_stop(obj);
|
||||
/* Wait until STOPF flag is set */
|
||||
timeout = FLAG_TIMEOUT;
|
||||
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_STOPF) == RESET) {
|
||||
timeout--;
|
||||
if (timeout == 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
/* Clear STOP Flag */
|
||||
__HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_STOPF);
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
int i2c_byte_read(i2c_t *obj, int last)
|
||||
{
|
||||
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
|
||||
int timeout;
|
||||
|
||||
// Wait until the byte is received
|
||||
timeout = FLAG_TIMEOUT;
|
||||
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_RXNE) == RESET) {
|
||||
if ((timeout--) == 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
return (int)i2c->RXDR;
|
||||
}
|
||||
|
||||
int i2c_byte_write(i2c_t *obj, int data)
|
||||
{
|
||||
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
|
||||
int timeout;
|
||||
|
||||
// Wait until the previous byte is transmitted
|
||||
timeout = FLAG_TIMEOUT;
|
||||
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TXIS) == RESET) {
|
||||
if ((timeout--) == 0) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
i2c->TXDR = (uint8_t)data;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void i2c_reset(i2c_t *obj)
|
||||
{
|
||||
int timeout;
|
||||
|
||||
// wait before reset
|
||||
timeout = LONG_TIMEOUT;
|
||||
while ((__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY)) && (timeout-- != 0));
|
||||
|
||||
__I2C1_FORCE_RESET();
|
||||
__I2C1_RELEASE_RESET();
|
||||
}
|
||||
|
||||
#if DEVICE_I2CSLAVE
|
||||
|
||||
void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask)
|
||||
{
|
||||
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
|
||||
uint16_t tmpreg;
|
||||
|
||||
// disable
|
||||
i2c->OAR1 &= (uint32_t)(~I2C_OAR1_OA1EN);
|
||||
// Get the old register value
|
||||
tmpreg = i2c->OAR1;
|
||||
// Reset address bits
|
||||
tmpreg &= 0xFC00;
|
||||
// Set new address
|
||||
tmpreg |= (uint16_t)((uint16_t)address & (uint16_t)0x00FE); // 7-bits
|
||||
// Store the new register value
|
||||
i2c->OAR1 = tmpreg;
|
||||
// enable
|
||||
i2c->OAR1 |= I2C_OAR1_OA1EN;
|
||||
}
|
||||
|
||||
void i2c_slave_mode(i2c_t *obj, int enable_slave)
|
||||
{
|
||||
|
||||
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
|
||||
uint16_t tmpreg;
|
||||
|
||||
// Get the old register value
|
||||
tmpreg = i2c->OAR1;
|
||||
|
||||
// Enable / disable slave
|
||||
if (enable_slave == 1) {
|
||||
tmpreg |= I2C_OAR1_OA1EN;
|
||||
} else {
|
||||
tmpreg &= (uint32_t)(~I2C_OAR1_OA1EN);
|
||||
}
|
||||
|
||||
// Set new mode
|
||||
i2c->OAR1 = tmpreg;
|
||||
|
||||
}
|
||||
|
||||
// See I2CSlave.h
|
||||
#define NoData 0 // the slave has not been addressed
|
||||
#define ReadAddressed 1 // the master has requested a read from this slave (slave = transmitter)
|
||||
#define WriteGeneral 2 // the master is writing to all slave
|
||||
#define WriteAddressed 3 // the master is writing to this slave (slave = receiver)
|
||||
|
||||
int i2c_slave_receive(i2c_t *obj)
|
||||
{
|
||||
I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
|
||||
int retValue = NoData;
|
||||
|
||||
if (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY) == 1) {
|
||||
if (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_ADDR) == 1) {
|
||||
if (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_DIR) == 1)
|
||||
retValue = ReadAddressed;
|
||||
else
|
||||
retValue = WriteAddressed;
|
||||
__HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_ADDR);
|
||||
}
|
||||
}
|
||||
|
||||
return (retValue);
|
||||
}
|
||||
|
||||
int i2c_slave_read(i2c_t *obj, char *data, int length)
|
||||
{
|
||||
char size = 0;
|
||||
|
||||
while (size < length) data[size++] = (char)i2c_byte_read(obj, 0);
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
int i2c_slave_write(i2c_t *obj, const char *data, int length)
|
||||
{
|
||||
char size = 0;
|
||||
I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
|
||||
|
||||
do {
|
||||
i2c_byte_write(obj, data[size]);
|
||||
size++;
|
||||
} while (size < length);
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
|
||||
#endif // DEVICE_I2CSLAVE
|
||||
|
||||
#endif // DEVICE_I2C
|
|
@ -0,0 +1,87 @@
|
|||
/* mbed Microcontroller Library
|
||||
*******************************************************************************
|
||||
* Copyright (c) 2015, STMicroelectronics
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of STMicroelectronics nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*******************************************************************************
|
||||
*/
|
||||
#ifndef MBED_I2C_DEVICE_H
|
||||
#define MBED_I2C_DEVICE_H
|
||||
|
||||
#include "cmsis.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#ifdef DEVICE_I2C
|
||||
|
||||
#define I2C_IP_VERSION_V2
|
||||
|
||||
#define I2C_IT_ALL (I2C_IT_ERRI|I2C_IT_TCI|I2C_IT_STOPI|I2C_IT_NACKI|I2C_IT_ADDRI|I2C_IT_RXI|I2C_IT_TXI)
|
||||
|
||||
/* Family specifc settings for clock source */
|
||||
#define I2CAPI_I2C1_CLKSRC RCC_I2C1CLKSOURCE_PCLK1
|
||||
#define I2CAPI_I2C2_CLKSRC RCC_I2C2CLKSOURCE_PCLK1
|
||||
#define I2CAPI_I2C3_CLKSRC RCC_I2C3CLKSOURCE_PCLK1
|
||||
#define I2CAPI_I2C4_CLKSRC RCC_I2C4CLKSOURCE_PCLK1
|
||||
|
||||
/* Provide the suitable timing depending on requested frequencie */
|
||||
inline uint32_t get_i2c_timing(int hz)
|
||||
{
|
||||
uint32_t tim = 0;
|
||||
/*
|
||||
Values calculated with I2C_Timing_Configuration tool (excel file)
|
||||
* Standard mode (up to 100 kHz)
|
||||
* Fast Mode (up to 400 kHz)
|
||||
* Fast Mode Plus (up to 1 MHz)
|
||||
Below values obtained with:
|
||||
- I2Cx clock source = APB1CLK = 54 MHz
|
||||
- Analog filter delay = ON
|
||||
- Digital filter coefficient = 0
|
||||
*/
|
||||
switch (hz) {
|
||||
case 100000:
|
||||
tim = 0x10916998; // Standard mode with Rise time = 120ns, Fall time = 120ns
|
||||
break;
|
||||
case 400000:
|
||||
tim = 0x00B11B54; // Fast Mode with Rise time = 120ns, Fall time = 120ns
|
||||
break;
|
||||
case 1000000:
|
||||
tim = 0x0090091B; // Fast Mode Plus with Rise time = 120ns, Fall time = 10ns
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return tim;
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // DEVICE_I2C
|
||||
|
||||
#endif
|
|
@ -66,10 +66,6 @@ struct dac_s {
|
|||
uint32_t channel;
|
||||
};
|
||||
|
||||
struct i2c_s {
|
||||
I2CName i2c;
|
||||
};
|
||||
|
||||
struct trng_s {
|
||||
RNG_HandleTypeDef handle;
|
||||
};
|
||||
|
|
|
@ -60,10 +60,6 @@ struct analogin_s {
|
|||
uint32_t channel;
|
||||
};
|
||||
|
||||
struct i2c_s {
|
||||
I2CName i2c;
|
||||
};
|
||||
|
||||
#include "common_objects.h"
|
||||
#include "gpio_object.h"
|
||||
|
||||
|
|
|
@ -60,10 +60,6 @@ struct analogin_s {
|
|||
uint32_t channel;
|
||||
};
|
||||
|
||||
struct i2c_s {
|
||||
I2CName i2c;
|
||||
};
|
||||
|
||||
#include "common_objects.h"
|
||||
#include "gpio_object.h"
|
||||
|
||||
|
|
|
@ -66,10 +66,6 @@ struct dac_s {
|
|||
uint32_t channel;
|
||||
};
|
||||
|
||||
struct i2c_s {
|
||||
I2CName i2c;
|
||||
};
|
||||
|
||||
#include "common_objects.h"
|
||||
#include "gpio_object.h"
|
||||
|
||||
|
|
|
@ -66,10 +66,6 @@ struct dac_s {
|
|||
uint32_t channel;
|
||||
};
|
||||
|
||||
struct i2c_s {
|
||||
I2CName i2c;
|
||||
};
|
||||
|
||||
struct trng_s {
|
||||
RNG_HandleTypeDef handle;
|
||||
};
|
||||
|
|
|
@ -82,6 +82,34 @@ struct serial_s {
|
|||
#endif
|
||||
};
|
||||
|
||||
struct i2c_s {
|
||||
/* The 1st 2 members I2CName i2c
|
||||
* and I2C_HandleTypeDef handle should
|
||||
* be kept as the first members of this struct
|
||||
* to ensure i2c_get_obj to work as expected
|
||||
*/
|
||||
I2CName i2c;
|
||||
I2C_HandleTypeDef handle;
|
||||
uint8_t index;
|
||||
int hz;
|
||||
PinName sda;
|
||||
PinName scl;
|
||||
IRQn_Type event_i2cIRQ;
|
||||
IRQn_Type error_i2cIRQ;
|
||||
uint32_t XferOperation;
|
||||
volatile uint8_t event;
|
||||
#if DEVICE_I2CSLAVE
|
||||
uint8_t slave;
|
||||
volatile uint8_t pending_slave_tx_master_rx;
|
||||
volatile uint8_t pending_slave_rx_maxter_tx;
|
||||
#endif
|
||||
#if DEVICE_I2C_ASYNCH
|
||||
uint32_t address;
|
||||
uint8_t stop;
|
||||
uint8_t available_events;
|
||||
#endif
|
||||
};
|
||||
|
||||
#include "gpio_object.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
|
|
@ -1,427 +0,0 @@
|
|||
/* mbed Microcontroller Library
|
||||
*******************************************************************************
|
||||
* Copyright (c) 2015, STMicroelectronics
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of STMicroelectronics nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*******************************************************************************
|
||||
*/
|
||||
#include "mbed_assert.h"
|
||||
#include "i2c_api.h"
|
||||
|
||||
#if DEVICE_I2C
|
||||
|
||||
#include "cmsis.h"
|
||||
#include "pinmap.h"
|
||||
#include "mbed_error.h"
|
||||
#include "PeripheralPins.h"
|
||||
|
||||
/* Timeout values for flags and events waiting loops. These timeouts are
|
||||
not based on accurate values, they just guarantee that the application will
|
||||
not remain stuck if the I2C communication is corrupted. */
|
||||
#define FLAG_TIMEOUT ((int)0x1000)
|
||||
#define LONG_TIMEOUT ((int)0x8000)
|
||||
|
||||
I2C_HandleTypeDef I2cHandle;
|
||||
|
||||
void i2c_init(i2c_t *obj, PinName sda, PinName scl)
|
||||
{
|
||||
static int i2c1_inited = 0;
|
||||
#if defined(I2C2_BASE)
|
||||
static int i2c2_inited = 0;
|
||||
#endif
|
||||
#if defined(I2C3_BASE)
|
||||
static int i2c3_inited = 0;
|
||||
#endif
|
||||
|
||||
// Determine the I2C to use
|
||||
I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA);
|
||||
I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL);
|
||||
|
||||
obj->i2c = (I2CName)pinmap_merge(i2c_sda, i2c_scl);
|
||||
MBED_ASSERT(obj->i2c != (I2CName)NC);
|
||||
|
||||
// Enable I2C1 clock and pinout if not done
|
||||
if ((obj->i2c == I2C_1) && !i2c1_inited) {
|
||||
i2c1_inited = 1;
|
||||
__HAL_RCC_I2C1_CONFIG(RCC_I2C1CLKSOURCE_SYSCLK);
|
||||
__I2C1_CLK_ENABLE();
|
||||
// Configure I2C pins
|
||||
pinmap_pinout(sda, PinMap_I2C_SDA);
|
||||
pinmap_pinout(scl, PinMap_I2C_SCL);
|
||||
pin_mode(sda, OpenDrain);
|
||||
pin_mode(scl, OpenDrain);
|
||||
}
|
||||
|
||||
#if defined(I2C2_BASE)
|
||||
// Enable I2C2 clock and pinout if not done
|
||||
if ((obj->i2c == I2C_2) && !i2c2_inited) {
|
||||
i2c2_inited = 1;
|
||||
__I2C2_CLK_ENABLE();
|
||||
// Configure I2C pins
|
||||
pinmap_pinout(sda, PinMap_I2C_SDA);
|
||||
pinmap_pinout(scl, PinMap_I2C_SCL);
|
||||
pin_mode(sda, OpenDrain);
|
||||
pin_mode(scl, OpenDrain);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(I2C3_BASE)
|
||||
// Enable I2C3 clock and pinout if not done
|
||||
if ((obj->i2c == I2C_3) && !i2c3_inited) {
|
||||
i2c3_inited = 1;
|
||||
__I2C3_CLK_ENABLE();
|
||||
// Configure I2C pins
|
||||
pinmap_pinout(sda, PinMap_I2C_SDA);
|
||||
pinmap_pinout(scl, PinMap_I2C_SCL);
|
||||
pin_mode(sda, OpenDrain);
|
||||
pin_mode(scl, OpenDrain);
|
||||
}
|
||||
#endif
|
||||
|
||||
// Reset to clear pending flags if any
|
||||
i2c_reset(obj);
|
||||
|
||||
// I2C configuration
|
||||
i2c_frequency(obj, 100000); // 100 kHz per default
|
||||
}
|
||||
|
||||
void i2c_frequency(i2c_t *obj, int hz)
|
||||
{
|
||||
MBED_ASSERT((hz == 100000) || (hz == 400000) || (hz == 1000000));
|
||||
I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
|
||||
int timeout;
|
||||
|
||||
// wait before init
|
||||
timeout = LONG_TIMEOUT;
|
||||
while ((__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY)) && (timeout-- != 0));
|
||||
|
||||
// Common settings: I2C clock = 32 MHz, Analog filter = ON, Digital filter coefficient = 0
|
||||
switch (hz) {
|
||||
case 100000:
|
||||
I2cHandle.Init.Timing = 0x20602938; // Standard mode with Rise Time = 400ns and Fall Time = 100ns
|
||||
break;
|
||||
case 400000:
|
||||
I2cHandle.Init.Timing = 0x00B0122A; // Fast mode with Rise Time = 250ns and Fall Time = 100ns
|
||||
break;
|
||||
case 1000000:
|
||||
I2cHandle.Init.Timing = 0x0030040E; // Fast mode Plus with Rise Time = 60ns and Fall Time = 100ns
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
// I2C configuration
|
||||
I2cHandle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
|
||||
I2cHandle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED;
|
||||
I2cHandle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED;
|
||||
I2cHandle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED;
|
||||
I2cHandle.Init.OwnAddress1 = 0;
|
||||
I2cHandle.Init.OwnAddress2 = 0;
|
||||
I2cHandle.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
|
||||
|
||||
if (HAL_I2C_Init(&I2cHandle) != HAL_OK) {
|
||||
error("Cannot initialize I2C");
|
||||
}
|
||||
}
|
||||
|
||||
inline int i2c_start(i2c_t *obj)
|
||||
{
|
||||
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
|
||||
int timeout;
|
||||
|
||||
I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
|
||||
|
||||
// Clear Acknowledge failure flag
|
||||
__HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_AF);
|
||||
|
||||
// Wait the STOP condition has been previously correctly sent
|
||||
timeout = FLAG_TIMEOUT;
|
||||
while ((i2c->CR2 & I2C_CR2_STOP) == I2C_CR2_STOP){
|
||||
if ((timeout--) == 0) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
// Generate the START condition
|
||||
i2c->CR2 |= I2C_CR2_START;
|
||||
|
||||
// Wait the START condition has been correctly sent
|
||||
timeout = FLAG_TIMEOUT;
|
||||
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY) == RESET) {
|
||||
if ((timeout--) == 0) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
inline int i2c_stop(i2c_t *obj)
|
||||
{
|
||||
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
|
||||
|
||||
// Generate the STOP condition
|
||||
i2c->CR2 |= I2C_CR2_STOP;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int i2c_read(i2c_t *obj, int address, char *data, int length, int stop)
|
||||
{
|
||||
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
|
||||
I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
|
||||
int timeout;
|
||||
int count;
|
||||
int value;
|
||||
|
||||
/* update CR2 register */
|
||||
i2c->CR2 = (i2c->CR2 & (uint32_t)~((uint32_t)(I2C_CR2_SADD | I2C_CR2_NBYTES | I2C_CR2_RELOAD | I2C_CR2_AUTOEND | I2C_CR2_RD_WRN | I2C_CR2_START | I2C_CR2_STOP)))
|
||||
| (uint32_t)(((uint32_t)address & I2C_CR2_SADD) | (((uint32_t)length << 16) & I2C_CR2_NBYTES) | (uint32_t)I2C_SOFTEND_MODE | (uint32_t)I2C_GENERATE_START_READ);
|
||||
|
||||
// Read all bytes
|
||||
for (count = 0; count < length; count++) {
|
||||
value = i2c_byte_read(obj, 0);
|
||||
data[count] = (char)value;
|
||||
}
|
||||
|
||||
// Wait transfer complete
|
||||
timeout = FLAG_TIMEOUT;
|
||||
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TC) == RESET) {
|
||||
timeout--;
|
||||
if (timeout == 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
__HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_TC);
|
||||
|
||||
// If not repeated start, send stop.
|
||||
if (stop) {
|
||||
i2c_stop(obj);
|
||||
/* Wait until STOPF flag is set */
|
||||
timeout = FLAG_TIMEOUT;
|
||||
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_STOPF) == RESET) {
|
||||
timeout--;
|
||||
if (timeout == 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
/* Clear STOP Flag */
|
||||
__HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_STOPF);
|
||||
}
|
||||
|
||||
return length;
|
||||
}
|
||||
|
||||
int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop)
|
||||
{
|
||||
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
|
||||
I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
|
||||
int timeout;
|
||||
int count;
|
||||
|
||||
/* update CR2 register */
|
||||
i2c->CR2 = (i2c->CR2 & (uint32_t)~((uint32_t)(I2C_CR2_SADD | I2C_CR2_NBYTES | I2C_CR2_RELOAD | I2C_CR2_AUTOEND | I2C_CR2_RD_WRN | I2C_CR2_START | I2C_CR2_STOP)))
|
||||
| (uint32_t)(((uint32_t)address & I2C_CR2_SADD) | (((uint32_t)length << 16) & I2C_CR2_NBYTES) | (uint32_t)I2C_SOFTEND_MODE | (uint32_t)I2C_GENERATE_START_WRITE);
|
||||
|
||||
for (count = 0; count < length; count++) {
|
||||
i2c_byte_write(obj, data[count]);
|
||||
}
|
||||
|
||||
// Wait transfer complete
|
||||
timeout = FLAG_TIMEOUT;
|
||||
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TC) == RESET) {
|
||||
timeout--;
|
||||
if (timeout == 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
__HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_TC);
|
||||
|
||||
// If not repeated start, send stop.
|
||||
if (stop) {
|
||||
i2c_stop(obj);
|
||||
/* Wait until STOPF flag is set */
|
||||
timeout = FLAG_TIMEOUT;
|
||||
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_STOPF) == RESET) {
|
||||
timeout--;
|
||||
if (timeout == 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
/* Clear STOP Flag */
|
||||
__HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_STOPF);
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
int i2c_byte_read(i2c_t *obj, int last)
|
||||
{
|
||||
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
|
||||
int timeout;
|
||||
|
||||
// Wait until the byte is received
|
||||
timeout = FLAG_TIMEOUT;
|
||||
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_RXNE) == RESET) {
|
||||
if ((timeout--) == 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
return (int)i2c->RXDR;
|
||||
}
|
||||
|
||||
int i2c_byte_write(i2c_t *obj, int data)
|
||||
{
|
||||
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
|
||||
int timeout;
|
||||
|
||||
// Wait until the previous byte is transmitted
|
||||
timeout = FLAG_TIMEOUT;
|
||||
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TXIS) == RESET) {
|
||||
if ((timeout--) == 0) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
i2c->TXDR = (uint8_t)data;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void i2c_reset(i2c_t *obj)
|
||||
{
|
||||
int timeout;
|
||||
|
||||
// wait before reset
|
||||
timeout = LONG_TIMEOUT;
|
||||
while ((__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY)) && (timeout-- != 0));
|
||||
|
||||
if (obj->i2c == I2C_1) {
|
||||
__I2C1_FORCE_RESET();
|
||||
__I2C1_RELEASE_RESET();
|
||||
}
|
||||
#if defined(I2C2_BASE)
|
||||
if (obj->i2c == I2C_2) {
|
||||
__I2C2_FORCE_RESET();
|
||||
__I2C2_RELEASE_RESET();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
#if DEVICE_I2CSLAVE
|
||||
|
||||
void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask)
|
||||
{
|
||||
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
|
||||
uint16_t tmpreg;
|
||||
|
||||
// disable
|
||||
i2c->OAR1 &= (uint32_t)(~I2C_OAR1_OA1EN);
|
||||
// Get the old register value
|
||||
tmpreg = i2c->OAR1;
|
||||
// Reset address bits
|
||||
tmpreg &= 0xFC00;
|
||||
// Set new address
|
||||
tmpreg |= (uint16_t)((uint16_t)address & (uint16_t)0x00FE); // 7-bits
|
||||
// Store the new register value
|
||||
i2c->OAR1 = tmpreg;
|
||||
// enable
|
||||
i2c->OAR1 |= I2C_OAR1_OA1EN;
|
||||
}
|
||||
|
||||
void i2c_slave_mode(i2c_t *obj, int enable_slave)
|
||||
{
|
||||
|
||||
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
|
||||
uint16_t tmpreg;
|
||||
|
||||
// Get the old register value
|
||||
tmpreg = i2c->OAR1;
|
||||
|
||||
// Enable / disable slave
|
||||
if (enable_slave == 1) {
|
||||
tmpreg |= I2C_OAR1_OA1EN;
|
||||
} else {
|
||||
tmpreg &= (uint32_t)(~I2C_OAR1_OA1EN);
|
||||
}
|
||||
|
||||
// Set new mode
|
||||
i2c->OAR1 = tmpreg;
|
||||
|
||||
}
|
||||
|
||||
// See I2CSlave.h
|
||||
#define NoData 0 // the slave has not been addressed
|
||||
#define ReadAddressed 1 // the master has requested a read from this slave (slave = transmitter)
|
||||
#define WriteGeneral 2 // the master is writing to all slave
|
||||
#define WriteAddressed 3 // the master is writing to this slave (slave = receiver)
|
||||
|
||||
int i2c_slave_receive(i2c_t *obj)
|
||||
{
|
||||
I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
|
||||
int retValue = NoData;
|
||||
|
||||
if (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY) == 1) {
|
||||
if (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_ADDR) == 1) {
|
||||
if (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_DIR) == 1)
|
||||
retValue = ReadAddressed;
|
||||
else
|
||||
retValue = WriteAddressed;
|
||||
__HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_ADDR);
|
||||
}
|
||||
}
|
||||
|
||||
return (retValue);
|
||||
}
|
||||
|
||||
int i2c_slave_read(i2c_t *obj, char *data, int length)
|
||||
{
|
||||
char size = 0;
|
||||
|
||||
while (size < length) data[size++] = (char)i2c_byte_read(obj, 0);
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
int i2c_slave_write(i2c_t *obj, const char *data, int length)
|
||||
{
|
||||
char size = 0;
|
||||
I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
|
||||
|
||||
do {
|
||||
i2c_byte_write(obj, data[size]);
|
||||
size++;
|
||||
} while (size < length);
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
|
||||
#endif // DEVICE_I2CSLAVE
|
||||
|
||||
#endif // DEVICE_I2C
|
|
@ -0,0 +1,90 @@
|
|||
/* mbed Microcontroller Library
|
||||
*******************************************************************************
|
||||
* Copyright (c) 2015, STMicroelectronics
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of STMicroelectronics nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*******************************************************************************
|
||||
*/
|
||||
#ifndef MBED_I2C_DEVICE_H
|
||||
#define MBED_I2C_DEVICE_H
|
||||
|
||||
#include "cmsis.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#ifdef DEVICE_I2C
|
||||
|
||||
#define I2C_IP_VERSION_V2
|
||||
|
||||
#if defined I2C1_BASE
|
||||
#define I2C1_EV_IRQn I2C1_IRQn
|
||||
#define I2C1_ER_IRQn I2C1_IRQn
|
||||
#endif
|
||||
#if defined I2C2_BASE
|
||||
#define I2C2_EV_IRQn I2C2_IRQn
|
||||
#define I2C2_ER_IRQn I2C2_IRQn
|
||||
#endif
|
||||
#if defined I2C3_BASE
|
||||
#define I2C3_EV_IRQn I2C3_IRQn
|
||||
#define I2C3_ER_IRQn I2C3_IRQn
|
||||
#endif
|
||||
|
||||
#define I2C_IT_ALL (I2C_IT_ERRI|I2C_IT_TCI|I2C_IT_STOPI|I2C_IT_NACKI|I2C_IT_ADDRI|I2C_IT_RXI|I2C_IT_TXI)
|
||||
|
||||
/* Family specifc settings for clock source */
|
||||
#define I2CAPI_I2C1_CLKSRC RCC_I2C1CLKSOURCE_SYSCLK
|
||||
#define I2CAPI_I2C3_CLKSRC RCC_I2C3CLKSOURCE_SYSCLK
|
||||
#define I2CAPI_I2C4_CLKSRC RCC_I2C4CLKSOURCE_SYSCLK
|
||||
|
||||
/* Provide the suitable timing depending on requested frequencie */
|
||||
inline uint32_t get_i2c_timing(int hz)
|
||||
{
|
||||
uint32_t tim = 0;
|
||||
|
||||
switch (hz) {
|
||||
case 100000:
|
||||
tim = 0x20602938; // Standard mode with Rise Time = 400ns and Fall Time = 100ns
|
||||
break;
|
||||
case 400000:
|
||||
tim = 0x00B0122A; // Fast mode with Rise Time = 250ns and Fall Time = 100ns
|
||||
break;
|
||||
case 1000000:
|
||||
tim = 0x0030040E; // Fast mode Plus with Rise Time = 60ns and Fall Time = 100ns
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return tim;
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // DEVICE_I2C
|
||||
|
||||
#endif
|
|
@ -65,11 +65,6 @@ struct dac_s {
|
|||
PinName pin;
|
||||
};
|
||||
|
||||
struct i2c_s {
|
||||
I2CName i2c;
|
||||
uint32_t slave;
|
||||
};
|
||||
|
||||
#include "common_objects.h"
|
||||
#include "gpio_object.h"
|
||||
|
||||
|
|
|
@ -65,11 +65,6 @@ struct dac_s {
|
|||
PinName pin;
|
||||
};
|
||||
|
||||
struct i2c_s {
|
||||
I2CName i2c;
|
||||
uint32_t slave;
|
||||
};
|
||||
|
||||
#include "common_objects.h"
|
||||
#include "gpio_object.h"
|
||||
|
||||
|
|
|
@ -65,11 +65,6 @@ struct dac_s {
|
|||
PinName pin;
|
||||
};
|
||||
|
||||
struct i2c_s {
|
||||
I2CName i2c;
|
||||
uint32_t slave;
|
||||
};
|
||||
|
||||
#include "common_objects.h"
|
||||
#include "gpio_object.h"
|
||||
|
||||
|
|
|
@ -65,11 +65,6 @@ struct dac_s {
|
|||
PinName pin;
|
||||
};
|
||||
|
||||
struct i2c_s {
|
||||
I2CName i2c;
|
||||
uint32_t slave;
|
||||
};
|
||||
|
||||
#include "common_objects.h"
|
||||
#include "gpio_object.h"
|
||||
|
||||
|
|
|
@ -80,6 +80,34 @@ struct spi_s {
|
|||
#endif
|
||||
};
|
||||
|
||||
struct i2c_s {
|
||||
/* The 1st 2 members I2CName i2c
|
||||
* and I2C_HandleTypeDef handle should
|
||||
* be kept as the first members of this struct
|
||||
* to ensure i2c_get_obj to work as expected
|
||||
*/
|
||||
I2CName i2c;
|
||||
I2C_HandleTypeDef handle;
|
||||
uint8_t index;
|
||||
int hz;
|
||||
PinName sda;
|
||||
PinName scl;
|
||||
IRQn_Type event_i2cIRQ;
|
||||
IRQn_Type error_i2cIRQ;
|
||||
uint32_t XferOperation;
|
||||
volatile uint8_t event;
|
||||
#if DEVICE_I2CSLAVE
|
||||
uint8_t slave;
|
||||
volatile uint8_t pending_slave_tx_master_rx;
|
||||
volatile uint8_t pending_slave_rx_maxter_tx;
|
||||
#endif
|
||||
#if DEVICE_I2C_ASYNCH
|
||||
uint32_t address;
|
||||
uint8_t stop;
|
||||
uint8_t available_events;
|
||||
#endif
|
||||
};
|
||||
|
||||
#include "gpio_object.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
|
|
@ -1,489 +0,0 @@
|
|||
/* mbed Microcontroller Library
|
||||
*******************************************************************************
|
||||
* Copyright (c) 2014, STMicroelectronics
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of STMicroelectronics nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*******************************************************************************
|
||||
*/
|
||||
#include "mbed_assert.h"
|
||||
#include "i2c_api.h"
|
||||
|
||||
#if DEVICE_I2C
|
||||
|
||||
#include "cmsis.h"
|
||||
#include "pinmap.h"
|
||||
#include "PeripheralPins.h"
|
||||
|
||||
/* Timeout values for flags and events waiting loops. These timeouts are
|
||||
not based on accurate values, they just guarantee that the application will
|
||||
not remain stuck if the I2C communication is corrupted. */
|
||||
#define FLAG_TIMEOUT ((int)0x1000)
|
||||
#define LONG_TIMEOUT ((int)0x8000)
|
||||
|
||||
I2C_HandleTypeDef I2cHandle;
|
||||
|
||||
int i2c1_inited = 0;
|
||||
int i2c2_inited = 0;
|
||||
|
||||
void i2c_init(i2c_t *obj, PinName sda, PinName scl)
|
||||
{
|
||||
// Determine the I2C to use
|
||||
I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA);
|
||||
I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL);
|
||||
|
||||
obj->i2c = (I2CName)pinmap_merge(i2c_sda, i2c_scl);
|
||||
MBED_ASSERT(obj->i2c != (I2CName)NC);
|
||||
|
||||
// Enable I2C1 clock and pinout if not done
|
||||
if ((obj->i2c == I2C_1) && !i2c1_inited) {
|
||||
i2c1_inited = 1;
|
||||
__I2C1_CLK_ENABLE();
|
||||
// Configure I2C pins
|
||||
pinmap_pinout(sda, PinMap_I2C_SDA);
|
||||
pinmap_pinout(scl, PinMap_I2C_SCL);
|
||||
pin_mode(sda, OpenDrain);
|
||||
pin_mode(scl, OpenDrain);
|
||||
}
|
||||
// Enable I2C2 clock and pinout if not done
|
||||
if ((obj->i2c == I2C_2) && !i2c2_inited) {
|
||||
i2c2_inited = 1;
|
||||
__I2C2_CLK_ENABLE();
|
||||
// Configure I2C pins
|
||||
pinmap_pinout(sda, PinMap_I2C_SDA);
|
||||
pinmap_pinout(scl, PinMap_I2C_SCL);
|
||||
pin_mode(sda, OpenDrain);
|
||||
pin_mode(scl, OpenDrain);
|
||||
}
|
||||
|
||||
// Reset to clear pending flags if any
|
||||
i2c_reset(obj);
|
||||
|
||||
// I2C configuration
|
||||
i2c_frequency(obj, 100000); // 100 kHz per default
|
||||
|
||||
// I2C master by default
|
||||
obj->slave = 0;
|
||||
}
|
||||
|
||||
void i2c_frequency(i2c_t *obj, int hz)
|
||||
{
|
||||
MBED_ASSERT((hz != 0) && (hz <= 400000));
|
||||
I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
|
||||
int timeout;
|
||||
|
||||
// wait before init
|
||||
timeout = LONG_TIMEOUT;
|
||||
while ((__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY)) && (timeout-- != 0));
|
||||
|
||||
// I2C configuration
|
||||
I2cHandle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
|
||||
I2cHandle.Init.ClockSpeed = hz;
|
||||
I2cHandle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED;
|
||||
I2cHandle.Init.DutyCycle = I2C_DUTYCYCLE_2;
|
||||
I2cHandle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED;
|
||||
I2cHandle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED;
|
||||
I2cHandle.Init.OwnAddress1 = 0;
|
||||
I2cHandle.Init.OwnAddress2 = 0;
|
||||
HAL_I2C_Init(&I2cHandle);
|
||||
if (obj->slave) {
|
||||
/* Enable Address Acknowledge */
|
||||
I2cHandle.Instance->CR1 |= I2C_CR1_ACK;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
inline int i2c_start(i2c_t *obj)
|
||||
{
|
||||
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
|
||||
int timeout;
|
||||
|
||||
I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
|
||||
|
||||
// Clear Acknowledge failure flag
|
||||
__HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_AF);
|
||||
|
||||
// Wait the STOP condition has been previously correctly sent
|
||||
// This timeout can be avoid in some specific cases by simply clearing the STOP bit
|
||||
timeout = FLAG_TIMEOUT;
|
||||
while ((i2c->CR1 & I2C_CR1_STOP) == I2C_CR1_STOP) {
|
||||
if ((timeout--) == 0) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
// Generate the START condition
|
||||
i2c->CR1 |= I2C_CR1_START;
|
||||
|
||||
// Wait the START condition has been correctly sent
|
||||
timeout = FLAG_TIMEOUT;
|
||||
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_SB) == RESET) {
|
||||
if ((timeout--) == 0) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
inline int i2c_stop(i2c_t *obj)
|
||||
{
|
||||
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
|
||||
|
||||
// Generate the STOP condition
|
||||
i2c->CR1 |= I2C_CR1_STOP;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int i2c_read(i2c_t *obj, int address, char *data, int length, int stop)
|
||||
{
|
||||
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
|
||||
I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
|
||||
int timeout;
|
||||
int count;
|
||||
int value;
|
||||
|
||||
i2c_start(obj);
|
||||
|
||||
// Wait until SB flag is set
|
||||
timeout = FLAG_TIMEOUT;
|
||||
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_SB) == RESET) {
|
||||
timeout--;
|
||||
if (timeout == 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
i2c->DR = I2C_7BIT_ADD_READ(address);
|
||||
|
||||
|
||||
// Wait address is acknowledged
|
||||
timeout = FLAG_TIMEOUT;
|
||||
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_ADDR) == RESET) {
|
||||
timeout--;
|
||||
if (timeout == 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
__HAL_I2C_CLEAR_ADDRFLAG(&I2cHandle);
|
||||
|
||||
// Read all bytes except last one
|
||||
for (count = 0; count < (length - 1); count++) {
|
||||
value = i2c_byte_read(obj, 0);
|
||||
data[count] = (char)value;
|
||||
}
|
||||
|
||||
// If not repeated start, send stop.
|
||||
// Warning: must be done BEFORE the data is read.
|
||||
if (stop) {
|
||||
i2c_stop(obj);
|
||||
}
|
||||
|
||||
// Read the last byte
|
||||
value = i2c_byte_read(obj, 1);
|
||||
data[count] = (char)value;
|
||||
|
||||
return length;
|
||||
}
|
||||
|
||||
int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop)
|
||||
{
|
||||
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
|
||||
I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
|
||||
int timeout;
|
||||
int count;
|
||||
|
||||
i2c_start(obj);
|
||||
|
||||
// Wait until SB flag is set
|
||||
timeout = FLAG_TIMEOUT;
|
||||
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_SB) == RESET) {
|
||||
timeout--;
|
||||
if (timeout == 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
i2c->DR = I2C_7BIT_ADD_WRITE(address);
|
||||
|
||||
|
||||
// Wait address is acknowledged
|
||||
timeout = FLAG_TIMEOUT;
|
||||
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_ADDR) == RESET) {
|
||||
timeout--;
|
||||
if (timeout == 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
__HAL_I2C_CLEAR_ADDRFLAG(&I2cHandle);
|
||||
|
||||
for (count = 0; count < length; count++) {
|
||||
if (i2c_byte_write(obj, data[count]) != 1) {
|
||||
i2c_stop(obj);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
// If not repeated start, send stop.
|
||||
if (stop) {
|
||||
i2c_stop(obj);
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
int i2c_byte_read(i2c_t *obj, int last)
|
||||
{
|
||||
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
|
||||
int timeout;
|
||||
|
||||
if (last) {
|
||||
// Don't acknowledge the last byte
|
||||
i2c->CR1 &= ~I2C_CR1_ACK;
|
||||
} else {
|
||||
// Acknowledge the byte
|
||||
i2c->CR1 |= I2C_CR1_ACK;
|
||||
}
|
||||
|
||||
// Wait until the byte is received
|
||||
timeout = FLAG_TIMEOUT;
|
||||
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_RXNE) == RESET) {
|
||||
if ((timeout--) == 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
return (int)i2c->DR;
|
||||
}
|
||||
|
||||
int i2c_byte_write(i2c_t *obj, int data)
|
||||
{
|
||||
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
|
||||
int timeout;
|
||||
|
||||
i2c->DR = (uint8_t)data;
|
||||
|
||||
// Wait until the byte is transmitted
|
||||
timeout = FLAG_TIMEOUT;
|
||||
while ((__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TXE) == RESET) &&
|
||||
(__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BTF) == RESET)) {
|
||||
if ((timeout--) == 0) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void i2c_reset(i2c_t *obj)
|
||||
{
|
||||
int timeout;
|
||||
|
||||
// wait before reset
|
||||
timeout = LONG_TIMEOUT;
|
||||
while ((__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY)) && (timeout-- != 0));
|
||||
|
||||
if (obj->i2c == I2C_1) {
|
||||
__I2C1_FORCE_RESET();
|
||||
__I2C1_RELEASE_RESET();
|
||||
}
|
||||
if (obj->i2c == I2C_2) {
|
||||
__I2C2_FORCE_RESET();
|
||||
__I2C2_RELEASE_RESET();
|
||||
}
|
||||
}
|
||||
|
||||
#if DEVICE_I2CSLAVE
|
||||
|
||||
void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask)
|
||||
{
|
||||
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
|
||||
uint16_t tmpreg = 0;
|
||||
|
||||
// Get the old register value
|
||||
tmpreg = i2c->OAR1;
|
||||
// Reset address bits
|
||||
tmpreg &= 0xFC00;
|
||||
// Set new address
|
||||
tmpreg |= (uint16_t)((uint16_t)address & (uint16_t)0x00FE); // 7-bits
|
||||
// Store the new register value
|
||||
i2c->OAR1 = tmpreg;
|
||||
}
|
||||
|
||||
void i2c_slave_mode(i2c_t *obj, int enable_slave)
|
||||
{
|
||||
I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
|
||||
if (enable_slave) {
|
||||
obj->slave = 1;
|
||||
/* Enable Address Acknowledge */
|
||||
I2cHandle.Instance->CR1 |= I2C_CR1_ACK;
|
||||
}
|
||||
}
|
||||
|
||||
// See I2CSlave.h
|
||||
#define NoData 0 // the slave has not been addressed
|
||||
#define ReadAddressed 1 // the master has requested a read from this slave (slave = transmitter)
|
||||
#define WriteGeneral 2 // the master is writing to all slave
|
||||
#define WriteAddressed 3 // the master is writing to this slave (slave = receiver)
|
||||
|
||||
int i2c_slave_receive(i2c_t *obj)
|
||||
{
|
||||
int retValue = NoData;
|
||||
|
||||
if (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY) == 1) {
|
||||
if (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_ADDR) == 1) {
|
||||
if (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TRA) == 1)
|
||||
retValue = ReadAddressed;
|
||||
else
|
||||
retValue = WriteAddressed;
|
||||
|
||||
__HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_ADDR);
|
||||
}
|
||||
}
|
||||
|
||||
return (retValue);
|
||||
}
|
||||
|
||||
int i2c_slave_read(i2c_t *obj, char *data, int length)
|
||||
{
|
||||
uint32_t Timeout;
|
||||
int size = 0;
|
||||
|
||||
I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
|
||||
|
||||
while (length > 0) {
|
||||
/* Wait until RXNE flag is set */
|
||||
// Wait until the byte is received
|
||||
Timeout = FLAG_TIMEOUT;
|
||||
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_RXNE) == RESET) {
|
||||
Timeout--;
|
||||
if (Timeout == 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/* Read data from DR */
|
||||
(*data++) = I2cHandle.Instance->DR;
|
||||
length--;
|
||||
size++;
|
||||
|
||||
if ((__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BTF) == SET) && (length != 0)) {
|
||||
/* Read data from DR */
|
||||
(*data++) = I2cHandle.Instance->DR;
|
||||
length--;
|
||||
size++;
|
||||
}
|
||||
}
|
||||
|
||||
/* Wait until STOP flag is set */
|
||||
Timeout = FLAG_TIMEOUT;
|
||||
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_STOPF) == RESET) {
|
||||
Timeout--;
|
||||
if (Timeout == 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/* Clear STOP flag */
|
||||
__HAL_I2C_CLEAR_STOPFLAG(&I2cHandle);
|
||||
|
||||
/* Wait until BUSY flag is reset */
|
||||
Timeout = FLAG_TIMEOUT;
|
||||
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY) == SET) {
|
||||
Timeout--;
|
||||
if (Timeout == 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
int i2c_slave_write(i2c_t *obj, const char *data, int length)
|
||||
{
|
||||
uint32_t Timeout;
|
||||
int size = 0;
|
||||
|
||||
I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
|
||||
|
||||
while (length > 0) {
|
||||
/* Wait until TXE flag is set */
|
||||
Timeout = FLAG_TIMEOUT;
|
||||
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TXE) == RESET) {
|
||||
Timeout--;
|
||||
if (Timeout == 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* Write data to DR */
|
||||
I2cHandle.Instance->DR = (*data++);
|
||||
length--;
|
||||
size++;
|
||||
|
||||
if ((__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BTF) == SET) && (length != 0)) {
|
||||
/* Write data to DR */
|
||||
I2cHandle.Instance->DR = (*data++);
|
||||
length--;
|
||||
size++;
|
||||
}
|
||||
}
|
||||
|
||||
/* Wait until AF flag is set */
|
||||
Timeout = FLAG_TIMEOUT;
|
||||
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_AF) == RESET) {
|
||||
Timeout--;
|
||||
if (Timeout == 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* Clear AF flag */
|
||||
__HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_AF);
|
||||
|
||||
|
||||
/* Wait until BUSY flag is reset */
|
||||
Timeout = FLAG_TIMEOUT;
|
||||
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY) == SET) {
|
||||
Timeout--;
|
||||
if (Timeout == 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
I2cHandle.State = HAL_I2C_STATE_READY;
|
||||
|
||||
/* Process Unlocked */
|
||||
__HAL_UNLOCK(&I2cHandle);
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
|
||||
#endif // DEVICE_I2CSLAVE
|
||||
|
||||
#endif // DEVICE_I2C
|
|
@ -0,0 +1,48 @@
|
|||
/* mbed Microcontroller Library
|
||||
*******************************************************************************
|
||||
* Copyright (c) 2015, STMicroelectronics
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of STMicroelectronics nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*******************************************************************************
|
||||
*/
|
||||
#ifndef MBED_I2C_DEVICE_H
|
||||
#define MBED_I2C_DEVICE_H
|
||||
|
||||
#include "cmsis.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#ifdef DEVICE_I2C
|
||||
|
||||
/* Define IP version */
|
||||
#define I2C_IP_VERSION_V1
|
||||
|
||||
#define I2C_IT_ALL (I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR)
|
||||
|
||||
#endif // DEVICE_I2C
|
||||
|
||||
#endif
|
|
@ -66,11 +66,6 @@ struct dac_s {
|
|||
uint32_t channel;
|
||||
};
|
||||
|
||||
struct i2c_s {
|
||||
I2CName i2c;
|
||||
uint32_t slave;
|
||||
};
|
||||
|
||||
struct can_s {
|
||||
CANName can;
|
||||
int index;
|
||||
|
|
|
@ -66,11 +66,6 @@ struct dac_s {
|
|||
uint32_t channel;
|
||||
};
|
||||
|
||||
struct i2c_s {
|
||||
I2CName i2c;
|
||||
uint32_t slave;
|
||||
};
|
||||
|
||||
struct can_s {
|
||||
CANName can;
|
||||
int index;
|
||||
|
|
|
@ -66,11 +66,6 @@ struct dac_s {
|
|||
uint32_t channel;
|
||||
};
|
||||
|
||||
struct i2c_s {
|
||||
I2CName i2c;
|
||||
uint32_t slave;
|
||||
};
|
||||
|
||||
struct can_s {
|
||||
CANName can;
|
||||
int index;
|
||||
|
|
|
@ -82,6 +82,34 @@ struct serial_s {
|
|||
#endif
|
||||
};
|
||||
|
||||
struct i2c_s {
|
||||
/* The 1st 2 members I2CName i2c
|
||||
* and I2C_HandleTypeDef handle should
|
||||
* be kept as the first members of this struct
|
||||
* to ensure i2c_get_obj to work as expected
|
||||
*/
|
||||
I2CName i2c;
|
||||
I2C_HandleTypeDef handle;
|
||||
uint8_t index;
|
||||
int hz;
|
||||
PinName sda;
|
||||
PinName scl;
|
||||
IRQn_Type event_i2cIRQ;
|
||||
IRQn_Type error_i2cIRQ;
|
||||
uint32_t XferOperation;
|
||||
volatile uint8_t event;
|
||||
#if DEVICE_I2CSLAVE
|
||||
uint8_t slave;
|
||||
volatile uint8_t pending_slave_tx_master_rx;
|
||||
volatile uint8_t pending_slave_rx_maxter_tx;
|
||||
#endif
|
||||
#if DEVICE_I2C_ASYNCH
|
||||
uint32_t address;
|
||||
uint8_t stop;
|
||||
uint8_t available_events;
|
||||
#endif
|
||||
};
|
||||
|
||||
#include "gpio_object.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
|
|
@ -1,465 +0,0 @@
|
|||
/* mbed Microcontroller Library
|
||||
*******************************************************************************
|
||||
* Copyright (c) 2015, STMicroelectronics
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of STMicroelectronics nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*******************************************************************************
|
||||
*/
|
||||
#include "mbed_assert.h"
|
||||
#include "i2c_api.h"
|
||||
|
||||
#if DEVICE_I2C
|
||||
|
||||
#include "cmsis.h"
|
||||
#include "pinmap.h"
|
||||
#include "mbed_error.h"
|
||||
#include "PeripheralPins.h"
|
||||
|
||||
/* Timeout values for flags and events waiting loops. These timeouts are
|
||||
not based on accurate values, they just guarantee that the application will
|
||||
not remain stuck if the I2C communication is corrupted. */
|
||||
#define FLAG_TIMEOUT ((int)0x1000)
|
||||
#define LONG_TIMEOUT ((int)0x8000)
|
||||
|
||||
I2C_HandleTypeDef I2cHandle;
|
||||
|
||||
void i2c_init(i2c_t *obj, PinName sda, PinName scl)
|
||||
{
|
||||
static int i2c1_inited = 0;
|
||||
#if defined(I2C2_BASE)
|
||||
static int i2c2_inited = 0;
|
||||
#endif
|
||||
#if defined(I2C3_BASE)
|
||||
static int i2c3_inited = 0;
|
||||
#endif
|
||||
|
||||
// Determine the I2C to use
|
||||
I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA);
|
||||
I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL);
|
||||
|
||||
obj->i2c = (I2CName)pinmap_merge(i2c_sda, i2c_scl);
|
||||
MBED_ASSERT(obj->i2c != (I2CName)NC);
|
||||
|
||||
// Enable I2C1 clock and pinout if not done
|
||||
if ((obj->i2c == I2C_1) && !i2c1_inited) {
|
||||
i2c1_inited = 1;
|
||||
__HAL_RCC_I2C1_CONFIG(RCC_I2C1CLKSOURCE_SYSCLK);
|
||||
__HAL_RCC_I2C1_CLK_ENABLE();
|
||||
// Configure I2C pins
|
||||
pinmap_pinout(sda, PinMap_I2C_SDA);
|
||||
pinmap_pinout(scl, PinMap_I2C_SCL);
|
||||
pin_mode(sda, OpenDrain);
|
||||
pin_mode(scl, OpenDrain);
|
||||
}
|
||||
|
||||
#if defined(I2C2_BASE)
|
||||
// Enable I2C2 clock and pinout if not done
|
||||
if ((obj->i2c == I2C_2) && !i2c2_inited) {
|
||||
i2c2_inited = 1;
|
||||
__HAL_RCC_I2C2_CLK_ENABLE();
|
||||
// Configure I2C pins
|
||||
pinmap_pinout(sda, PinMap_I2C_SDA);
|
||||
pinmap_pinout(scl, PinMap_I2C_SCL);
|
||||
pin_mode(sda, OpenDrain);
|
||||
pin_mode(scl, OpenDrain);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(I2C3_BASE)
|
||||
// Enable I2C3 clock and pinout if not done
|
||||
if ((obj->i2c == I2C_3) && !i2c3_inited) {
|
||||
i2c3_inited = 1;
|
||||
__HAL_RCC_I2C3_CLK_ENABLE();
|
||||
// Configure I2C pins
|
||||
pinmap_pinout(sda, PinMap_I2C_SDA);
|
||||
pinmap_pinout(scl, PinMap_I2C_SCL);
|
||||
pin_mode(sda, OpenDrain);
|
||||
pin_mode(scl, OpenDrain);
|
||||
}
|
||||
#endif
|
||||
|
||||
// Reset to clear pending flags if any
|
||||
i2c_reset(obj);
|
||||
|
||||
// I2C configuration
|
||||
i2c_frequency(obj, 100000); // 100 kHz per default
|
||||
}
|
||||
|
||||
void i2c_frequency(i2c_t *obj, int hz)
|
||||
{
|
||||
MBED_ASSERT((hz == 100000) || (hz == 400000) || (hz == 1000000));
|
||||
I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
|
||||
int timeout;
|
||||
|
||||
// wait before init
|
||||
timeout = LONG_TIMEOUT;
|
||||
while ((__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY)) && (timeout-- != 0));
|
||||
|
||||
// Update the SystemCoreClock variable.
|
||||
SystemCoreClockUpdate();
|
||||
|
||||
if (SystemCoreClock == 80000000) {
|
||||
// Common settings: I2C clock = 80 MHz, Analog filter = ON, Digital filter coefficient = 0
|
||||
switch (hz) {
|
||||
case 100000:
|
||||
I2cHandle.Init.Timing = 0x30C14E6B; // Standard mode with Rise Time = 400ns and Fall Time = 100ns
|
||||
break;
|
||||
case 400000:
|
||||
I2cHandle.Init.Timing = 0x10D1143A; // Fast mode with Rise Time = 250ns and Fall Time = 100ns
|
||||
break;
|
||||
case 1000000:
|
||||
I2cHandle.Init.Timing = 0x00810E27; // Fast mode Plus with Rise Time = 60ns and Fall Time = 100ns
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
} else if (SystemCoreClock == 48000000) {
|
||||
// Common settings: I2C clock = 48 MHz, Analog filter = ON, Digital filter coefficient = 0
|
||||
switch (hz) {
|
||||
case 100000:
|
||||
I2cHandle.Init.Timing = 0x20A03E55; // Standard mode with Rise Time = 400ns and Fall Time = 100ns
|
||||
break;
|
||||
case 400000:
|
||||
I2cHandle.Init.Timing = 0x10800C21; // Fast mode with Rise Time = 250ns and Fall Time = 100ns
|
||||
break;
|
||||
case 1000000:
|
||||
I2cHandle.Init.Timing = 0x00500816; // Fast mode Plus with Rise Time = 60ns and Fall Time = 100ns
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Enable the Fast Mode Plus capability
|
||||
if (hz == 1000000) {
|
||||
if (obj->i2c == I2C_1) {
|
||||
__HAL_SYSCFG_FASTMODEPLUS_ENABLE(HAL_SYSCFG_FASTMODEPLUS_I2C1);
|
||||
}
|
||||
#if defined(I2C2_BASE)
|
||||
if (obj->i2c == I2C_2) {
|
||||
__HAL_SYSCFG_FASTMODEPLUS_ENABLE(HAL_SYSCFG_FASTMODEPLUS_I2C2);
|
||||
}
|
||||
#endif
|
||||
#if defined(I2C3_BASE)
|
||||
if (obj->i2c == I2C_3) {
|
||||
__HAL_SYSCFG_FASTMODEPLUS_ENABLE(HAL_SYSCFG_FASTMODEPLUS_I2C3);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
// I2C configuration
|
||||
I2cHandle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
|
||||
I2cHandle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
|
||||
I2cHandle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
|
||||
I2cHandle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
|
||||
I2cHandle.Init.OwnAddress1 = 0;
|
||||
I2cHandle.Init.OwnAddress2 = 0;
|
||||
I2cHandle.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
|
||||
|
||||
if (HAL_I2C_Init(&I2cHandle) != HAL_OK) {
|
||||
error("Cannot initialize I2C\n");
|
||||
}
|
||||
}
|
||||
|
||||
inline int i2c_start(i2c_t *obj)
|
||||
{
|
||||
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
|
||||
int timeout;
|
||||
|
||||
I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
|
||||
|
||||
// Clear Acknowledge failure flag
|
||||
__HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_AF);
|
||||
|
||||
// Wait the STOP condition has been previously correctly sent
|
||||
timeout = FLAG_TIMEOUT;
|
||||
while ((i2c->CR2 & I2C_CR2_STOP) == I2C_CR2_STOP){
|
||||
if ((timeout--) == 0) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
// Generate the START condition
|
||||
i2c->CR2 |= I2C_CR2_START;
|
||||
|
||||
// Wait the START condition has been correctly sent
|
||||
timeout = FLAG_TIMEOUT;
|
||||
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY) == RESET) {
|
||||
if ((timeout--) == 0) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
inline int i2c_stop(i2c_t *obj)
|
||||
{
|
||||
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
|
||||
|
||||
// Generate the STOP condition
|
||||
i2c->CR2 |= I2C_CR2_STOP;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int i2c_read(i2c_t *obj, int address, char *data, int length, int stop)
|
||||
{
|
||||
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
|
||||
I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
|
||||
int timeout;
|
||||
int count;
|
||||
int value;
|
||||
|
||||
/* update CR2 register */
|
||||
i2c->CR2 = (i2c->CR2 & (uint32_t)~((uint32_t)(I2C_CR2_SADD | I2C_CR2_NBYTES | I2C_CR2_RELOAD | I2C_CR2_AUTOEND | I2C_CR2_RD_WRN | I2C_CR2_START | I2C_CR2_STOP)))
|
||||
| (uint32_t)(((uint32_t)address & I2C_CR2_SADD) | (((uint32_t)length << 16) & I2C_CR2_NBYTES) | (uint32_t)I2C_SOFTEND_MODE | (uint32_t)I2C_GENERATE_START_READ);
|
||||
|
||||
// Read all bytes
|
||||
for (count = 0; count < length; count++) {
|
||||
value = i2c_byte_read(obj, 0);
|
||||
data[count] = (char)value;
|
||||
}
|
||||
|
||||
// Wait transfer complete
|
||||
timeout = FLAG_TIMEOUT;
|
||||
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TC) == RESET) {
|
||||
timeout--;
|
||||
if (timeout == 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
__HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_TC);
|
||||
|
||||
// If not repeated start, send stop.
|
||||
if (stop) {
|
||||
i2c_stop(obj);
|
||||
/* Wait until STOPF flag is set */
|
||||
timeout = FLAG_TIMEOUT;
|
||||
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_STOPF) == RESET) {
|
||||
timeout--;
|
||||
if (timeout == 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
/* Clear STOP Flag */
|
||||
__HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_STOPF);
|
||||
}
|
||||
|
||||
return length;
|
||||
}
|
||||
|
||||
int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop)
|
||||
{
|
||||
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
|
||||
I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
|
||||
int timeout;
|
||||
int count;
|
||||
|
||||
/* update CR2 register */
|
||||
i2c->CR2 = (i2c->CR2 & (uint32_t)~((uint32_t)(I2C_CR2_SADD | I2C_CR2_NBYTES | I2C_CR2_RELOAD | I2C_CR2_AUTOEND | I2C_CR2_RD_WRN | I2C_CR2_START | I2C_CR2_STOP)))
|
||||
| (uint32_t)(((uint32_t)address & I2C_CR2_SADD) | (((uint32_t)length << 16) & I2C_CR2_NBYTES) | (uint32_t)I2C_SOFTEND_MODE | (uint32_t)I2C_GENERATE_START_WRITE);
|
||||
|
||||
for (count = 0; count < length; count++) {
|
||||
i2c_byte_write(obj, data[count]);
|
||||
}
|
||||
|
||||
// Wait transfer complete
|
||||
timeout = FLAG_TIMEOUT;
|
||||
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TC) == RESET) {
|
||||
timeout--;
|
||||
if (timeout == 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
__HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_TC);
|
||||
|
||||
// If not repeated start, send stop.
|
||||
if (stop) {
|
||||
i2c_stop(obj);
|
||||
/* Wait until STOPF flag is set */
|
||||
timeout = FLAG_TIMEOUT;
|
||||
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_STOPF) == RESET) {
|
||||
timeout--;
|
||||
if (timeout == 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
/* Clear STOP Flag */
|
||||
__HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_STOPF);
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
int i2c_byte_read(i2c_t *obj, int last)
|
||||
{
|
||||
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
|
||||
int timeout;
|
||||
|
||||
// Wait until the byte is received
|
||||
timeout = FLAG_TIMEOUT;
|
||||
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_RXNE) == RESET) {
|
||||
if ((timeout--) == 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
return (int)i2c->RXDR;
|
||||
}
|
||||
|
||||
int i2c_byte_write(i2c_t *obj, int data)
|
||||
{
|
||||
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
|
||||
int timeout;
|
||||
|
||||
// Wait until the previous byte is transmitted
|
||||
timeout = FLAG_TIMEOUT;
|
||||
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TXIS) == RESET) {
|
||||
if ((timeout--) == 0) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
i2c->TXDR = (uint8_t)data;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void i2c_reset(i2c_t *obj)
|
||||
{
|
||||
int timeout;
|
||||
|
||||
// wait before reset
|
||||
timeout = LONG_TIMEOUT;
|
||||
while ((__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY)) && (timeout-- != 0));
|
||||
|
||||
if (obj->i2c == I2C_1) {
|
||||
__HAL_RCC_I2C1_FORCE_RESET();
|
||||
__HAL_RCC_I2C1_RELEASE_RESET();
|
||||
}
|
||||
#if defined(I2C2_BASE)
|
||||
if (obj->i2c == I2C_2) {
|
||||
__HAL_RCC_I2C2_FORCE_RESET();
|
||||
__HAL_RCC_I2C2_RELEASE_RESET();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
#if DEVICE_I2CSLAVE
|
||||
|
||||
void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask)
|
||||
{
|
||||
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
|
||||
uint16_t tmpreg;
|
||||
|
||||
// disable
|
||||
i2c->OAR1 &= (uint32_t)(~I2C_OAR1_OA1EN);
|
||||
// Get the old register value
|
||||
tmpreg = i2c->OAR1;
|
||||
// Reset address bits
|
||||
tmpreg &= 0xFC00;
|
||||
// Set new address
|
||||
tmpreg |= (uint16_t)((uint16_t)address & (uint16_t)0x00FE); // 7-bits
|
||||
// Store the new register value
|
||||
i2c->OAR1 = tmpreg;
|
||||
// enable
|
||||
i2c->OAR1 |= I2C_OAR1_OA1EN;
|
||||
}
|
||||
|
||||
void i2c_slave_mode(i2c_t *obj, int enable_slave)
|
||||
{
|
||||
|
||||
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
|
||||
uint16_t tmpreg;
|
||||
|
||||
// Get the old register value
|
||||
tmpreg = i2c->OAR1;
|
||||
|
||||
// Enable / disable slave
|
||||
if (enable_slave == 1) {
|
||||
tmpreg |= I2C_OAR1_OA1EN;
|
||||
} else {
|
||||
tmpreg &= (uint32_t)(~I2C_OAR1_OA1EN);
|
||||
}
|
||||
|
||||
// Set new mode
|
||||
i2c->OAR1 = tmpreg;
|
||||
|
||||
}
|
||||
|
||||
// See I2CSlave.h
|
||||
#define NoData 0 // the slave has not been addressed
|
||||
#define ReadAddressed 1 // the master has requested a read from this slave (slave = transmitter)
|
||||
#define WriteGeneral 2 // the master is writing to all slave
|
||||
#define WriteAddressed 3 // the master is writing to this slave (slave = receiver)
|
||||
|
||||
int i2c_slave_receive(i2c_t *obj)
|
||||
{
|
||||
I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
|
||||
int retValue = NoData;
|
||||
|
||||
if (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY) == 1) {
|
||||
if (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_ADDR) == 1) {
|
||||
if (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_DIR) == 1)
|
||||
retValue = ReadAddressed;
|
||||
else
|
||||
retValue = WriteAddressed;
|
||||
__HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_ADDR);
|
||||
}
|
||||
}
|
||||
|
||||
return (retValue);
|
||||
}
|
||||
|
||||
int i2c_slave_read(i2c_t *obj, char *data, int length)
|
||||
{
|
||||
char size = 0;
|
||||
|
||||
while (size < length) data[size++] = (char)i2c_byte_read(obj, 0);
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
int i2c_slave_write(i2c_t *obj, const char *data, int length)
|
||||
{
|
||||
char size = 0;
|
||||
I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
|
||||
|
||||
do {
|
||||
i2c_byte_write(obj, data[size]);
|
||||
size++;
|
||||
} while (size < length);
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
|
||||
#endif // DEVICE_I2CSLAVE
|
||||
|
||||
#endif // DEVICE_I2C
|
|
@ -0,0 +1,95 @@
|
|||
/* mbed Microcontroller Library
|
||||
*******************************************************************************
|
||||
* Copyright (c) 2015, STMicroelectronics
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of STMicroelectronics nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*******************************************************************************
|
||||
*/
|
||||
#ifndef MBED_I2C_DEVICE_H
|
||||
#define MBED_I2C_DEVICE_H
|
||||
|
||||
#include "cmsis.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#ifdef DEVICE_I2C
|
||||
|
||||
#define I2C_IP_VERSION_V2
|
||||
|
||||
#define I2C_IT_ALL (I2C_IT_ERRI|I2C_IT_TCI|I2C_IT_STOPI|I2C_IT_NACKI|I2C_IT_ADDRI|I2C_IT_RXI|I2C_IT_TXI)
|
||||
|
||||
/* Family specifc settings for clock source */
|
||||
#define I2CAPI_I2C1_CLKSRC RCC_I2C1CLKSOURCE_SYSCLK
|
||||
#define I2CAPI_I2C2_CLKSRC RCC_I2C2CLKSOURCE_SYSCLK
|
||||
#define I2CAPI_I2C3_CLKSRC RCC_I2C3CLKSOURCE_SYSCLK
|
||||
#define I2CAPI_I2C4_CLKSRC RCC_I2C4CLKSOURCE_SYSCLK
|
||||
|
||||
/* Provide the suitable timing depending on requested frequencie */
|
||||
inline uint32_t get_i2c_timing(int hz)
|
||||
{
|
||||
uint32_t tim = 0;
|
||||
if (SystemCoreClock == 80000000) {
|
||||
// Common settings: I2C clock = 80 MHz, Analog filter = ON, Digital filter coefficient = 0
|
||||
switch (hz) {
|
||||
case 100000:
|
||||
tim = 0x30C14E6B; // Standard mode with Rise Time = 400ns and Fall Time = 100ns
|
||||
break;
|
||||
case 400000:
|
||||
tim = 0x10D1143A; // Fast mode with Rise Time = 250ns and Fall Time = 100ns
|
||||
break;
|
||||
case 1000000:
|
||||
tim = 0x00810E27; // Fast mode Plus with Rise Time = 60ns and Fall Time = 100ns
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
} else if (SystemCoreClock == 48000000) {
|
||||
// Common settings: I2C clock = 48 MHz, Analog filter = ON, Digital filter coefficient = 0
|
||||
switch (hz) {
|
||||
case 100000:
|
||||
tim = 0x20A03E55; // Standard mode with Rise Time = 400ns and Fall Time = 100ns
|
||||
break;
|
||||
case 400000:
|
||||
tim = 0x10800C21; // Fast mode with Rise Time = 250ns and Fall Time = 100ns
|
||||
break;
|
||||
case 1000000:
|
||||
tim = 0x00500816; // Fast mode Plus with Rise Time = 60ns and Fall Time = 100ns
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
return tim;
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // DEVICE_I2C
|
||||
|
||||
#endif
|
|
@ -27,6 +27,8 @@
|
|||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*******************************************************************************
|
||||
*/
|
||||
|
||||
|
||||
#include "mbed_assert.h"
|
||||
#include "i2c_api.h"
|
||||
#include "platform/wait_api.h"
|
||||
|
@ -36,6 +38,9 @@
|
|||
#include "cmsis.h"
|
||||
#include "pinmap.h"
|
||||
#include "PeripheralPins.h"
|
||||
/* F1 HAL not ready to move to I2C common code - this is ongoing */
|
||||
#if !defined(__STM32F1xx_HAL_H)
|
||||
#include "i2c_device.h" // family specific defines
|
||||
|
||||
#ifndef DEBUG_STDIO
|
||||
# define DEBUG_STDIO 0
|
||||
|
@ -48,43 +53,52 @@
|
|||
# define DEBUG_PRINTF(...) {}
|
||||
#endif
|
||||
|
||||
/* Timeout values are based on core clock and I2C clock.
|
||||
The BYTE_TIMEOUT is computed as twice the number of cycles it would
|
||||
take to send 10 bits over I2C. Most Flags should take less than that.
|
||||
This is for immediate FLAG or ACK check.
|
||||
*/
|
||||
#define BYTE_TIMEOUT ((SystemCoreClock / handle->Init.ClockSpeed) * 2 * 10)
|
||||
/* Timeout values based on I2C clock.
|
||||
The BYTE_TIMEOUT_US is computed as 3x the time in us it would
|
||||
take to send 10 bits over I2C. Most Flags should take less than that.
|
||||
This is for complete transfers check.
|
||||
*/
|
||||
#define BYTE_TIMEOUT_US ((SystemCoreClock / handle->Init.ClockSpeed) * 3 * 10)
|
||||
|
||||
#if DEVICE_I2C_ASYNCH
|
||||
#define I2C_S(obj) (struct i2c_s *) (&((obj)->i2c))
|
||||
#else
|
||||
#define I2C_S(obj) (struct i2c_s *) (obj)
|
||||
#endif
|
||||
|
||||
/* could be defined at family level */
|
||||
/* Family specific description for I2C */
|
||||
#define I2C_NUM (5)
|
||||
static I2C_HandleTypeDef* i2c_handles[I2C_NUM];
|
||||
|
||||
/* Timeout values are based on core clock and I2C clock.
|
||||
The BYTE_TIMEOUT is computed as twice the number of cycles it would
|
||||
take to send 10 bits over I2C. Most Flags should take less than that.
|
||||
This is for immediate FLAG or ACK check.
|
||||
*/
|
||||
#define BYTE_TIMEOUT ((SystemCoreClock / obj_s->hz) * 2 * 10)
|
||||
/* Timeout values based on I2C clock.
|
||||
The BYTE_TIMEOUT_US is computed as 3x the time in us it would
|
||||
take to send 10 bits over I2C. Most Flags should take less than that.
|
||||
This is for complete transfers check.
|
||||
*/
|
||||
#define BYTE_TIMEOUT_US ((SystemCoreClock / obj_s->hz) * 3 * 10)
|
||||
/* Timeout values for flags and events waiting loops. These timeouts are
|
||||
not based on accurate values, they just guarantee that the application will
|
||||
not remain stuck if the I2C communication is corrupted.
|
||||
*/
|
||||
#define FLAG_TIMEOUT ((int)0x1000)
|
||||
|
||||
/* GENERIC INIT and HELPERS FUNCTIONS */
|
||||
|
||||
#if defined(I2C1_BASE)
|
||||
static void i2c1_irq(void)
|
||||
{
|
||||
I2C_HandleTypeDef * handle = i2c_handles[0];
|
||||
HAL_I2C_EV_IRQHandler(handle);
|
||||
HAL_I2C_ER_IRQHandler(handle);
|
||||
}
|
||||
|
||||
#endif
|
||||
#if defined(I2C2_BASE)
|
||||
static void i2c2_irq(void)
|
||||
{
|
||||
I2C_HandleTypeDef * handle = i2c_handles[1];
|
||||
HAL_I2C_EV_IRQHandler(handle);
|
||||
HAL_I2C_ER_IRQHandler(handle);
|
||||
}
|
||||
|
||||
#endif
|
||||
#if defined(I2C3_BASE)
|
||||
static void i2c3_irq(void)
|
||||
{
|
||||
|
@ -94,12 +108,12 @@ static void i2c3_irq(void)
|
|||
}
|
||||
#endif
|
||||
#if defined(I2C4_BASE)
|
||||
static void i2c4_irq(void)
|
||||
{
|
||||
I2C_HandleTypeDef * handle = i2c_handles[3];
|
||||
HAL_I2C_EV_IRQHandler(handle);
|
||||
HAL_I2C_ER_IRQHandler(handle);
|
||||
}
|
||||
static void i2c4_irq(void)
|
||||
{
|
||||
I2C_HandleTypeDef * handle = i2c_handles[3];
|
||||
HAL_I2C_EV_IRQHandler(handle);
|
||||
HAL_I2C_ER_IRQHandler(handle);
|
||||
}
|
||||
#endif
|
||||
#if defined(FMPI2C1_BASE)
|
||||
static void i2c5_irq(void)
|
||||
|
@ -134,19 +148,23 @@ void i2c_ev_err_disable(i2c_t *obj) {
|
|||
HAL_NVIC_DisableIRQ(irq_error_n);
|
||||
}
|
||||
|
||||
void i2c_irq_set(i2c_t *obj, uint32_t enable)
|
||||
uint32_t i2c_get_irq_handler(i2c_t *obj)
|
||||
{
|
||||
struct i2c_s *obj_s = I2C_S(obj);
|
||||
I2C_HandleTypeDef *handle = &(obj_s->handle);
|
||||
uint32_t handler = 0;
|
||||
|
||||
switch (obj_s->index) {
|
||||
#if defined(I2C1_BASE)
|
||||
case 0:
|
||||
handler = (uint32_t)&i2c1_irq;
|
||||
break;
|
||||
#endif
|
||||
#if defined(I2C2_BASE)
|
||||
case 1:
|
||||
handler = (uint32_t)&i2c2_irq;
|
||||
break;
|
||||
#endif
|
||||
#if defined(I2C3_BASE)
|
||||
case 2:
|
||||
handler = (uint32_t)&i2c3_irq;
|
||||
|
@ -164,13 +182,8 @@ void i2c_irq_set(i2c_t *obj, uint32_t enable)
|
|||
#endif
|
||||
}
|
||||
|
||||
if (enable) {
|
||||
i2c_handles[obj_s->index] = handle;
|
||||
i2c_ev_err_enable(obj, handler);
|
||||
} else { // disable
|
||||
i2c_ev_err_disable(obj);
|
||||
i2c_handles[obj_s->index] = 0;
|
||||
}
|
||||
i2c_handles[obj_s->index] = handle;
|
||||
return handler;
|
||||
}
|
||||
|
||||
void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
|
||||
|
@ -186,6 +199,7 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
|
|||
obj_s->i2c = (I2CName)pinmap_merge(i2c_sda, i2c_scl);
|
||||
MBED_ASSERT(obj_s->i2c != (I2CName)NC);
|
||||
|
||||
#if defined I2C1_BASE
|
||||
// Enable I2C1 clock and pinout if not done
|
||||
if (obj_s->i2c == I2C_1) {
|
||||
obj_s->index = 0;
|
||||
|
@ -196,8 +210,10 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
|
|||
pin_mode(scl, PullUp);
|
||||
obj_s->event_i2cIRQ = I2C1_EV_IRQn;
|
||||
obj_s->error_i2cIRQ = I2C1_ER_IRQn;
|
||||
__I2C1_CLK_ENABLE();
|
||||
__HAL_RCC_I2C1_CLK_ENABLE();
|
||||
}
|
||||
#endif
|
||||
#if defined I2C2_BASE
|
||||
// Enable I2C2 clock and pinout if not done
|
||||
if (obj_s->i2c == I2C_2) {
|
||||
obj_s->index = 1;
|
||||
|
@ -208,8 +224,9 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
|
|||
pin_mode(scl, PullUp);
|
||||
obj_s->event_i2cIRQ = I2C2_EV_IRQn;
|
||||
obj_s->error_i2cIRQ = I2C2_ER_IRQn;
|
||||
__I2C2_CLK_ENABLE();
|
||||
__HAL_RCC_I2C2_CLK_ENABLE();
|
||||
}
|
||||
#endif
|
||||
#if defined I2C3_BASE
|
||||
// Enable I2C3 clock and pinout if not done
|
||||
if (obj_s->i2c == I2C_3) {
|
||||
|
@ -221,11 +238,11 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
|
|||
pin_mode(scl, PullUp);
|
||||
obj_s->event_i2cIRQ = I2C3_EV_IRQn;
|
||||
obj_s->error_i2cIRQ = I2C3_ER_IRQn;
|
||||
__I2C3_CLK_ENABLE();
|
||||
__HAL_RCC_I2C3_CLK_ENABLE();
|
||||
}
|
||||
#endif
|
||||
#if defined I2C4_BASE
|
||||
// Enable clock and pinout if not done
|
||||
// Enable I2C3 clock and pinout if not done
|
||||
if (obj_s->i2c == I2C_4) {
|
||||
obj_s->index = 3;
|
||||
// Configure I2C pins
|
||||
|
@ -235,13 +252,13 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
|
|||
pin_mode(scl, PullUp);
|
||||
obj_s->event_i2cIRQ = I2C4_EV_IRQn;
|
||||
obj_s->error_i2cIRQ = I2C4_ER_IRQn;
|
||||
__I2C4_CLK_ENABLE();
|
||||
__HAL_RCC_I2C4_CLK_ENABLE();
|
||||
}
|
||||
#endif
|
||||
#if defined FMPI2C1_BASE
|
||||
// Enable clock and pinout if not done
|
||||
// Enable I2C3 clock and pinout if not done
|
||||
if (obj_s->i2c == FMPI2C_1) {
|
||||
obj_s->index = 3;
|
||||
obj_s->index = 4;
|
||||
// Configure I2C pins
|
||||
pinmap_pinout(sda, PinMap_I2C_SDA);
|
||||
pinmap_pinout(scl, PinMap_I2C_SCL);
|
||||
|
@ -257,7 +274,9 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
|
|||
i2c_reset(obj);
|
||||
|
||||
// I2C configuration
|
||||
i2c_frequency(obj, 100000); // 100 kHz per default
|
||||
if(!obj_s->hz)
|
||||
obj_s->hz = 100000; // 100 kHz per default
|
||||
i2c_frequency(obj, obj_s->hz );
|
||||
|
||||
#if DEVICE_I2CSLAVE
|
||||
// I2C master by default
|
||||
|
@ -269,16 +288,10 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
|
|||
// I2C Xfer operation init
|
||||
obj_s->event = 0;
|
||||
obj_s->XferOperation = I2C_FIRST_AND_LAST_FRAME;
|
||||
|
||||
/* Activate default IRQ handlers for sync mode
|
||||
* which would be overwritten in async mode
|
||||
*/
|
||||
i2c_irq_set(obj, 1);
|
||||
}
|
||||
|
||||
void i2c_frequency(i2c_t *obj, int hz)
|
||||
{
|
||||
|
||||
int timeout;
|
||||
struct i2c_s *obj_s = I2C_S(obj);
|
||||
I2C_HandleTypeDef *handle = &(obj_s->handle);
|
||||
|
@ -289,21 +302,79 @@ void i2c_frequency(i2c_t *obj, int hz)
|
|||
timeout = BYTE_TIMEOUT;
|
||||
while ((__HAL_I2C_GET_FLAG(handle, I2C_FLAG_BUSY)) && (--timeout != 0));
|
||||
|
||||
#ifdef I2C_IP_VERSION_V1
|
||||
handle->Init.ClockSpeed = hz;
|
||||
handle->Init.DutyCycle = I2C_DUTYCYCLE_2;
|
||||
#endif
|
||||
#ifdef I2C_IP_VERSION_V2
|
||||
handle->Init.Timing = get_i2c_timing(hz);
|
||||
|
||||
// Enable the Fast Mode Plus capability
|
||||
if (hz == 1000000) {
|
||||
#if defined(I2C1_BASE) && defined(__HAL_SYSCFG_FASTMODEPLUS_ENABLE) && defined (I2C_FASTMODEPLUS_I2C1)
|
||||
if (obj_s->i2c == I2C_1) {
|
||||
__HAL_SYSCFG_FASTMODEPLUS_ENABLE(I2C_FASTMODEPLUS_I2C1);
|
||||
}
|
||||
#endif
|
||||
#if defined(I2C2_BASE) && defined(__HAL_SYSCFG_FASTMODEPLUS_ENABLE) && defined (I2C_FASTMODEPLUS_I2C2)
|
||||
if (obj_s->i2c == I2C_2) {
|
||||
__HAL_SYSCFG_FASTMODEPLUS_ENABLE(I2C_FASTMODEPLUS_I2C2);
|
||||
}
|
||||
#endif
|
||||
#if defined(I2C3_BASE) && defined(__HAL_SYSCFG_FASTMODEPLUS_ENABLE) && defined (I2C_FASTMODEPLUS_I2C3)
|
||||
if (obj_s->i2c == I2C_3) {
|
||||
__HAL_SYSCFG_FASTMODEPLUS_ENABLE(I2C_FASTMODEPLUS_I2C3);
|
||||
}
|
||||
#endif
|
||||
#if defined(I2C4_BASE) && defined(__HAL_SYSCFG_FASTMODEPLUS_ENABLE) && defined (I2C_FASTMODEPLUS_I2C4)
|
||||
if (obj_s->i2c == I2C_4) {
|
||||
__HAL_SYSCFG_FASTMODEPLUS_ENABLE(I2C_FASTMODEPLUS_I2C4);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
#endif //I2C_IP_VERSION_V2
|
||||
|
||||
/*##-1- Configure the I2C clock source. The clock is derived from the SYSCLK #*/
|
||||
#if defined(I2C1_BASE) && defined (__HAL_RCC_I2C1_CONFIG)
|
||||
if (obj_s->i2c == I2C_1) {
|
||||
__HAL_RCC_I2C1_CONFIG(I2CAPI_I2C1_CLKSRC);
|
||||
}
|
||||
#endif
|
||||
#if defined(I2C2_BASE) && defined(__HAL_RCC_I2C2_CONFIG)
|
||||
if (obj_s->i2c == I2C_2) {
|
||||
__HAL_RCC_I2C2_CONFIG(I2CAPI_I2C2_CLKSRC);
|
||||
}
|
||||
#endif
|
||||
#if defined(I2C3_BASE) && defined(__HAL_RCC_I2C3_CONFIG)
|
||||
if (obj_s->i2c == I2C_3) {
|
||||
__HAL_RCC_I2C3_CONFIG(I2CAPI_I2C3_CLKSRC);
|
||||
}
|
||||
#endif
|
||||
#if defined(I2C4_BASE) && defined(__HAL_RCC_I2C4_CONFIG)
|
||||
if (obj_s->i2c == I2C_4) {
|
||||
__HAL_RCC_I2C4_CONFIG(I2CAPI_I2C4_CLKSRC);
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef I2C_ANALOGFILTER_ENABLE
|
||||
/* Enable the Analog I2C Filter */
|
||||
HAL_I2CEx_AnalogFilter_Config(handle,I2C_ANALOGFILTER_ENABLE);
|
||||
#endif
|
||||
|
||||
// I2C configuration
|
||||
handle->Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
|
||||
handle->Init.ClockSpeed = hz;
|
||||
handle->Init.DualAddressMode = I2C_DUALADDRESS_DISABLED;
|
||||
handle->Init.DutyCycle = I2C_DUTYCYCLE_2;
|
||||
handle->Init.GeneralCallMode = I2C_GENERALCALL_DISABLED;
|
||||
handle->Init.NoStretchMode = I2C_NOSTRETCH_DISABLED;
|
||||
handle->Init.OwnAddress1 = 0;
|
||||
handle->Init.OwnAddress2 = 0;
|
||||
HAL_I2C_Init(handle);
|
||||
|
||||
/* store frequency for timeout computation */
|
||||
obj_s->hz = hz;
|
||||
}
|
||||
|
||||
i2c_t *get_i2c_obj(I2C_HandleTypeDef *hi2c){
|
||||
|
||||
/* Aim of the function is to get i2c_s pointer using hi2c pointer */
|
||||
/* Highly inspired from magical linux kernel's "container_of" */
|
||||
/* (which was not directly used since not compatible with IAR toolchain) */
|
||||
|
@ -316,47 +387,50 @@ i2c_t *get_i2c_obj(I2C_HandleTypeDef *hi2c){
|
|||
return (obj);
|
||||
}
|
||||
|
||||
inline int i2c_start(i2c_t *obj) {
|
||||
void i2c_reset(i2c_t *obj) {
|
||||
|
||||
int timeout;
|
||||
struct i2c_s *obj_s = I2C_S(obj);
|
||||
I2C_HandleTypeDef *handle = &(obj_s->handle);
|
||||
|
||||
// Clear Acknowledge failure flag
|
||||
__HAL_I2C_CLEAR_FLAG(handle, I2C_FLAG_AF);
|
||||
handle->Instance = (I2C_TypeDef *)(obj_s->i2c);
|
||||
|
||||
// Wait the STOP condition has been previously correctly sent
|
||||
// This timeout can be avoid in some specific cases by simply clearing the STOP bit
|
||||
// wait before reset
|
||||
timeout = BYTE_TIMEOUT;
|
||||
while ((handle->Instance->CR1 & I2C_CR1_STOP) == I2C_CR1_STOP) {
|
||||
if ((timeout--) == 0) {
|
||||
return 1;
|
||||
}
|
||||
while ((__HAL_I2C_GET_FLAG(handle, I2C_FLAG_BUSY)) && (--timeout != 0));
|
||||
#if defined I2C1_BASE
|
||||
if (obj_s->i2c == I2C_1) {
|
||||
__HAL_RCC_I2C1_FORCE_RESET();
|
||||
__HAL_RCC_I2C1_RELEASE_RESET();
|
||||
}
|
||||
|
||||
// Generate the START condition
|
||||
handle->Instance->CR1 |= I2C_CR1_START;
|
||||
|
||||
// Wait the START condition has been correctly sent
|
||||
timeout = BYTE_TIMEOUT;
|
||||
while (__HAL_I2C_GET_FLAG(handle, I2C_FLAG_SB) == RESET) {
|
||||
if ((timeout--) == 0) {
|
||||
return 1;
|
||||
}
|
||||
#endif
|
||||
#if defined I2C2_BASE
|
||||
if (obj_s->i2c == I2C_2) {
|
||||
__HAL_RCC_I2C2_FORCE_RESET();
|
||||
__HAL_RCC_I2C2_RELEASE_RESET();
|
||||
}
|
||||
|
||||
return 0;
|
||||
#endif
|
||||
#if defined I2C3_BASE
|
||||
if (obj_s->i2c == I2C_3) {
|
||||
__HAL_RCC_I2C3_FORCE_RESET();
|
||||
__HAL_RCC_I2C3_RELEASE_RESET();
|
||||
}
|
||||
#endif
|
||||
#if defined I2C4_BASE
|
||||
if (obj_s->i2c == I2C_4) {
|
||||
__HAL_RCC_I2C4_FORCE_RESET();
|
||||
__HAL_RCC_I2C4_RELEASE_RESET();
|
||||
}
|
||||
#endif
|
||||
#if defined FMPI2C1_BASE
|
||||
if (obj_s->i2c == FMPI2C_1) {
|
||||
__HAL_RCC_FMPI2C1_FORCE_RESET();
|
||||
__HAL_RCC_FMPI2C1_RELEASE_RESET();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
inline int i2c_stop(i2c_t *obj) {
|
||||
struct i2c_s *obj_s = I2C_S(obj);
|
||||
I2C_TypeDef *i2c = (I2C_TypeDef *)obj_s->i2c;
|
||||
|
||||
// Generate the STOP condition
|
||||
i2c->CR1 |= I2C_CR1_STOP;
|
||||
|
||||
return 0;
|
||||
}
|
||||
/* SYNCHRONOUS API FUNCTIONS */
|
||||
|
||||
int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
|
||||
struct i2c_s *obj_s = I2C_S(obj);
|
||||
|
@ -379,6 +453,12 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
|
|||
}
|
||||
|
||||
obj_s->event = 0;
|
||||
|
||||
/* Activate default IRQ handlers for sync mode
|
||||
* which would be overwritten in async mode
|
||||
*/
|
||||
i2c_ev_err_enable(obj, i2c_get_irq_handler(obj));
|
||||
|
||||
ret = HAL_I2C_Master_Sequential_Receive_IT(handle, address, (uint8_t *) data, length, obj_s->XferOperation);
|
||||
|
||||
if(ret == HAL_OK) {
|
||||
|
@ -388,6 +468,8 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
|
|||
wait_us(1);
|
||||
}
|
||||
|
||||
i2c_ev_err_disable(obj);
|
||||
|
||||
if((timeout == 0) || (obj_s->event != I2C_EVENT_TRANSFER_COMPLETE)) {
|
||||
DEBUG_PRINTF(" TIMEOUT or error in i2c_read\r\n");
|
||||
/* re-init IP to try and get back in a working state */
|
||||
|
@ -402,6 +484,187 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
|
|||
return count;
|
||||
}
|
||||
|
||||
/*
|
||||
* UNITARY APIS.
|
||||
* For very basic operations, direct registers access is needed
|
||||
* There are 2 different IPs version that need to be supported
|
||||
*/
|
||||
#ifdef I2C_IP_VERSION_V1
|
||||
int i2c_start(i2c_t *obj) {
|
||||
|
||||
int timeout;
|
||||
struct i2c_s *obj_s = I2C_S(obj);
|
||||
I2C_HandleTypeDef *handle = &(obj_s->handle);
|
||||
|
||||
// Clear Acknowledge failure flag
|
||||
__HAL_I2C_CLEAR_FLAG(handle, I2C_FLAG_AF);
|
||||
|
||||
// Wait the STOP condition has been previously correctly sent
|
||||
// This timeout can be avoid in some specific cases by simply clearing the STOP bit
|
||||
timeout = FLAG_TIMEOUT;
|
||||
while ((handle->Instance->CR1 & I2C_CR1_STOP) == I2C_CR1_STOP) {
|
||||
if ((timeout--) == 0) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
// Generate the START condition
|
||||
handle->Instance->CR1 |= I2C_CR1_START;
|
||||
|
||||
// Wait the START condition has been correctly sent
|
||||
timeout = FLAG_TIMEOUT;
|
||||
while (__HAL_I2C_GET_FLAG(handle, I2C_FLAG_SB) == RESET) {
|
||||
if ((timeout--) == 0) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int i2c_stop(i2c_t *obj) {
|
||||
struct i2c_s *obj_s = I2C_S(obj);
|
||||
I2C_TypeDef *i2c = (I2C_TypeDef *)obj_s->i2c;
|
||||
|
||||
// Generate the STOP condition
|
||||
i2c->CR1 |= I2C_CR1_STOP;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int i2c_byte_read(i2c_t *obj, int last) {
|
||||
|
||||
int timeout;
|
||||
struct i2c_s *obj_s = I2C_S(obj);
|
||||
I2C_HandleTypeDef *handle = &(obj_s->handle);
|
||||
|
||||
if (last) {
|
||||
// Don't acknowledge the last byte
|
||||
handle->Instance->CR1 &= ~I2C_CR1_ACK;
|
||||
} else {
|
||||
// Acknowledge the byte
|
||||
handle->Instance->CR1 |= I2C_CR1_ACK;
|
||||
}
|
||||
|
||||
// Wait until the byte is received
|
||||
timeout = FLAG_TIMEOUT;
|
||||
while (__HAL_I2C_GET_FLAG(handle, I2C_FLAG_RXNE) == RESET) {
|
||||
if ((timeout--) == 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
return (int)handle->Instance->DR;
|
||||
}
|
||||
|
||||
int i2c_byte_write(i2c_t *obj, int data) {
|
||||
|
||||
int timeout;
|
||||
struct i2c_s *obj_s = I2C_S(obj);
|
||||
I2C_HandleTypeDef *handle = &(obj_s->handle);
|
||||
|
||||
handle->Instance->DR = (uint8_t)data;
|
||||
|
||||
// Wait until the byte (might be the address) is transmitted
|
||||
timeout = FLAG_TIMEOUT;
|
||||
while ((__HAL_I2C_GET_FLAG(handle, I2C_FLAG_TXE) == RESET) &&
|
||||
(__HAL_I2C_GET_FLAG(handle, I2C_FLAG_BTF) == RESET) &&
|
||||
(__HAL_I2C_GET_FLAG(handle, I2C_FLAG_ADDR) == RESET)) {
|
||||
if ((timeout--) == 0) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (__HAL_I2C_GET_FLAG(handle, I2C_FLAG_ADDR) != RESET)
|
||||
{
|
||||
__HAL_I2C_CLEAR_ADDRFLAG(handle);
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
#endif //I2C_IP_VERSION_V1
|
||||
#ifdef I2C_IP_VERSION_V2
|
||||
int i2c_start(i2c_t *obj) {
|
||||
struct i2c_s *obj_s = I2C_S(obj);
|
||||
I2C_HandleTypeDef *handle = &(obj_s->handle);
|
||||
I2C_TypeDef *i2c = (I2C_TypeDef *)obj_s->i2c;
|
||||
int timeout;
|
||||
|
||||
// Clear Acknowledge failure flag
|
||||
__HAL_I2C_CLEAR_FLAG(handle, I2C_FLAG_AF);
|
||||
|
||||
// Wait the STOP condition has been previously correctly sent
|
||||
timeout = FLAG_TIMEOUT;
|
||||
while ((i2c->CR2 & I2C_CR2_STOP) == I2C_CR2_STOP){
|
||||
if ((timeout--) == 0) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
// Generate the START condition
|
||||
i2c->CR2 |= I2C_CR2_START;
|
||||
|
||||
// Wait the START condition has been correctly sent
|
||||
timeout = FLAG_TIMEOUT;
|
||||
while (__HAL_I2C_GET_FLAG(handle, I2C_FLAG_BUSY) == RESET) {
|
||||
if ((timeout--) == 0) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int i2c_stop(i2c_t *obj) {
|
||||
struct i2c_s *obj_s = I2C_S(obj);
|
||||
I2C_TypeDef *i2c = (I2C_TypeDef *)obj_s->i2c;
|
||||
|
||||
// Generate the STOP condition
|
||||
i2c->CR2 |= I2C_CR2_STOP;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int i2c_byte_read(i2c_t *obj, int last) {
|
||||
struct i2c_s *obj_s = I2C_S(obj);
|
||||
I2C_TypeDef *i2c = (I2C_TypeDef *)obj_s->i2c;
|
||||
I2C_HandleTypeDef *handle = &(obj_s->handle);
|
||||
int timeout;
|
||||
|
||||
// Wait until the byte is received
|
||||
timeout = FLAG_TIMEOUT;
|
||||
while (__HAL_I2C_GET_FLAG(handle, I2C_FLAG_RXNE) == RESET) {
|
||||
if ((timeout--) == 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
return (int)i2c->RXDR;
|
||||
}
|
||||
|
||||
int i2c_byte_write(i2c_t *obj, int data) {
|
||||
struct i2c_s *obj_s = I2C_S(obj);
|
||||
I2C_TypeDef *i2c = (I2C_TypeDef *)obj_s->i2c;
|
||||
I2C_HandleTypeDef *handle = &(obj_s->handle);
|
||||
int timeout;
|
||||
|
||||
// Wait until the previous byte is transmitted
|
||||
timeout = FLAG_TIMEOUT;
|
||||
while (__HAL_I2C_GET_FLAG(handle, I2C_FLAG_TXIS) == RESET) {
|
||||
if ((timeout--) == 0) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
i2c->TXDR = (uint8_t)data;
|
||||
|
||||
return 1;
|
||||
}
|
||||
#endif //I2C_IP_VERSION_V2
|
||||
|
||||
/*
|
||||
* SYNC APIS
|
||||
*/
|
||||
int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
|
||||
struct i2c_s *obj_s = I2C_S(obj);
|
||||
I2C_HandleTypeDef *handle = &(obj_s->handle);
|
||||
|
@ -424,6 +687,8 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
|
|||
|
||||
obj_s->event = 0;
|
||||
|
||||
i2c_ev_err_enable(obj, i2c_get_irq_handler(obj));
|
||||
|
||||
ret = HAL_I2C_Master_Sequential_Transmit_IT(handle, address, (uint8_t *) data, length, obj_s->XferOperation);
|
||||
|
||||
if(ret == HAL_OK) {
|
||||
|
@ -433,6 +698,8 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
|
|||
wait_us(1);
|
||||
}
|
||||
|
||||
i2c_ev_err_disable(obj);
|
||||
|
||||
if((timeout == 0) || (obj_s->event != I2C_EVENT_TRANSFER_COMPLETE)) {
|
||||
DEBUG_PRINTF(" TIMEOUT or error in i2c_write\r\n");
|
||||
/* re-init IP to try and get back in a working state */
|
||||
|
@ -447,100 +714,55 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
|
|||
return count;
|
||||
}
|
||||
|
||||
int i2c_byte_read(i2c_t *obj, int last) {
|
||||
|
||||
int timeout;
|
||||
void HAL_I2C_MasterTxCpltCallback(I2C_HandleTypeDef *hi2c){
|
||||
/* Get object ptr based on handler ptr */
|
||||
i2c_t *obj = get_i2c_obj(hi2c);
|
||||
struct i2c_s *obj_s = I2C_S(obj);
|
||||
I2C_HandleTypeDef *handle = &(obj_s->handle);
|
||||
|
||||
if (last) {
|
||||
// Don't acknowledge the last byte
|
||||
handle->Instance->CR1 &= ~I2C_CR1_ACK;
|
||||
} else {
|
||||
// Acknowledge the byte
|
||||
handle->Instance->CR1 |= I2C_CR1_ACK;
|
||||
}
|
||||
|
||||
// Wait until the byte is received
|
||||
timeout = BYTE_TIMEOUT;
|
||||
while (__HAL_I2C_GET_FLAG(handle, I2C_FLAG_RXNE) == RESET) {
|
||||
if ((timeout--) == 0) {
|
||||
return -1;
|
||||
#if DEVICE_I2C_ASYNCH
|
||||
/* Handle potential Tx/Rx use case */
|
||||
if ((obj->tx_buff.length) && (obj->rx_buff.length)) {
|
||||
if (obj_s->stop) {
|
||||
obj_s->XferOperation = I2C_LAST_FRAME;
|
||||
} else {
|
||||
obj_s->XferOperation = I2C_NEXT_FRAME;
|
||||
}
|
||||
}
|
||||
|
||||
return (int)handle->Instance->DR;
|
||||
HAL_I2C_Master_Sequential_Receive_IT(hi2c, obj_s->address, (uint8_t*)obj->rx_buff.buffer , obj->rx_buff.length, obj_s->XferOperation);
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
/* Set event flag */
|
||||
obj_s->event = I2C_EVENT_TRANSFER_COMPLETE;
|
||||
}
|
||||
}
|
||||
|
||||
int i2c_byte_write(i2c_t *obj, int data) {
|
||||
|
||||
int timeout;
|
||||
void HAL_I2C_MasterRxCpltCallback(I2C_HandleTypeDef *hi2c){
|
||||
/* Get object ptr based on handler ptr */
|
||||
i2c_t *obj = get_i2c_obj(hi2c);
|
||||
struct i2c_s *obj_s = I2C_S(obj);
|
||||
I2C_HandleTypeDef *handle = &(obj_s->handle);
|
||||
|
||||
handle->Instance->DR = (uint8_t)data;
|
||||
|
||||
// Wait until the byte (might be the address) is transmitted
|
||||
timeout = BYTE_TIMEOUT;
|
||||
while ((__HAL_I2C_GET_FLAG(handle, I2C_FLAG_TXE) == RESET) &&
|
||||
(__HAL_I2C_GET_FLAG(handle, I2C_FLAG_BTF) == RESET) &&
|
||||
(__HAL_I2C_GET_FLAG(handle, I2C_FLAG_ADDR) == RESET)) {
|
||||
if ((timeout--) == 0) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (__HAL_I2C_GET_FLAG(handle, I2C_FLAG_ADDR) != RESET)
|
||||
{
|
||||
__HAL_I2C_CLEAR_ADDRFLAG(handle);
|
||||
}
|
||||
|
||||
return 1;
|
||||
/* Set event flag */
|
||||
obj_s->event = I2C_EVENT_TRANSFER_COMPLETE;
|
||||
}
|
||||
|
||||
void i2c_reset(i2c_t *obj) {
|
||||
|
||||
int timeout;
|
||||
void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *hi2c){
|
||||
/* Get object ptr based on handler ptr */
|
||||
i2c_t *obj = get_i2c_obj(hi2c);
|
||||
struct i2c_s *obj_s = I2C_S(obj);
|
||||
I2C_HandleTypeDef *handle = &(obj_s->handle);
|
||||
|
||||
handle->Instance = (I2C_TypeDef *)(obj_s->i2c);
|
||||
DEBUG_PRINTF("HAL_I2C_ErrorCallback:%d, index=%d\r\n", (int) hi2c->ErrorCode, obj_s->index);
|
||||
|
||||
// wait before reset
|
||||
timeout = BYTE_TIMEOUT;
|
||||
while ((__HAL_I2C_GET_FLAG(handle, I2C_FLAG_BUSY)) && (--timeout != 0));
|
||||
/* re-init IP to try and get back in a working state */
|
||||
i2c_init(obj, obj_s->sda, obj_s->scl);
|
||||
|
||||
if (obj_s->i2c == I2C_1) {
|
||||
__I2C1_FORCE_RESET();
|
||||
__I2C1_RELEASE_RESET();
|
||||
}
|
||||
|
||||
if (obj_s->i2c == I2C_2) {
|
||||
__I2C2_FORCE_RESET();
|
||||
__I2C2_RELEASE_RESET();
|
||||
}
|
||||
#if defined I2C3_BASE
|
||||
if (obj_s->i2c == I2C_3) {
|
||||
__I2C3_FORCE_RESET();
|
||||
__I2C3_RELEASE_RESET();
|
||||
}
|
||||
#endif
|
||||
#if defined I2C4_BASE
|
||||
if (obj_s->i2c == I2C_4) {
|
||||
__I2C4_FORCE_RESET();
|
||||
__I2C4_RELEASE_RESET();
|
||||
}
|
||||
#endif
|
||||
#if defined FMPI2C1_BASE
|
||||
if (obj_s->i2c == FMPI2C_1) {
|
||||
__HAL_RCC_FMPI2C1_FORCE_RESET();
|
||||
__HAL_RCC_FMPI2C1_RELEASE_RESET();
|
||||
}
|
||||
#endif
|
||||
/* Keep Set event flag */
|
||||
obj_s->event = I2C_EVENT_ERROR;
|
||||
}
|
||||
|
||||
#if DEVICE_I2CSLAVE
|
||||
|
||||
/* SLAVE API FUNCTIONS */
|
||||
void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask) {
|
||||
struct i2c_s *obj_s = I2C_S(obj);
|
||||
I2C_HandleTypeDef *handle = &(obj_s->handle);
|
||||
|
@ -549,6 +771,8 @@ void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask) {
|
|||
handle->Init.OwnAddress1 = address;
|
||||
HAL_I2C_Init(handle);
|
||||
|
||||
i2c_ev_err_enable(obj, i2c_get_irq_handler(obj));
|
||||
|
||||
HAL_I2C_EnableListen_IT(handle);
|
||||
}
|
||||
|
||||
|
@ -646,7 +870,6 @@ int i2c_slave_read(i2c_t *obj, char *data, int length) {
|
|||
DEBUG_PRINTF("TIMEOUT or error in i2c_slave_read\r\n");
|
||||
}
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
|
@ -675,58 +898,10 @@ int i2c_slave_write(i2c_t *obj, const char *data, int length) {
|
|||
|
||||
return count;
|
||||
}
|
||||
|
||||
#endif // DEVICE_I2CSLAVE
|
||||
|
||||
void HAL_I2C_MasterTxCpltCallback(I2C_HandleTypeDef *hi2c){
|
||||
/* Get object ptr based on handler ptr */
|
||||
i2c_t *obj = get_i2c_obj(hi2c);
|
||||
struct i2c_s *obj_s = I2C_S(obj);
|
||||
|
||||
#if DEVICE_I2C_ASYNCH
|
||||
/* Handle potential Tx/Rx use case */
|
||||
if ((obj->tx_buff.length) && (obj->rx_buff.length)) {
|
||||
if (obj_s->stop) {
|
||||
obj_s->XferOperation = I2C_LAST_FRAME;
|
||||
} else {
|
||||
obj_s->XferOperation = I2C_NEXT_FRAME;
|
||||
}
|
||||
|
||||
HAL_I2C_Master_Sequential_Receive_IT(hi2c, obj_s->address, (uint8_t*)obj->rx_buff.buffer , obj->rx_buff.length, obj_s->XferOperation);
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
/* Set event flag */
|
||||
obj_s->event = I2C_EVENT_TRANSFER_COMPLETE;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void HAL_I2C_MasterRxCpltCallback(I2C_HandleTypeDef *hi2c){
|
||||
/* Get object ptr based on handler ptr */
|
||||
i2c_t *obj = get_i2c_obj(hi2c);
|
||||
struct i2c_s *obj_s = I2C_S(obj);
|
||||
|
||||
/* Set event flag */
|
||||
obj_s->event = I2C_EVENT_TRANSFER_COMPLETE;
|
||||
}
|
||||
|
||||
void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *hi2c){
|
||||
/* Get object ptr based on handler ptr */
|
||||
i2c_t *obj = get_i2c_obj(hi2c);
|
||||
struct i2c_s *obj_s = I2C_S(obj);
|
||||
|
||||
DEBUG_PRINTF("HAL_I2C_ErrorCallback:%d, index=%d\r\n", (int) hi2c->ErrorCode, obj_s->index);
|
||||
|
||||
/* re-init IP to try and get back in a working state */
|
||||
i2c_init(obj, obj_s->sda, obj_s->scl);
|
||||
|
||||
/* Keep Set event flag */
|
||||
obj_s->event = I2C_EVENT_ERROR;
|
||||
}
|
||||
|
||||
#if DEVICE_I2C_ASYNCH
|
||||
/* ASYNCH MASTER API FUNCTIONS */
|
||||
void HAL_I2C_AbortCpltCallback(I2C_HandleTypeDef *hi2c){
|
||||
/* Get object ptr based on handler ptr */
|
||||
i2c_t *obj = get_i2c_obj(hi2c);
|
||||
|
@ -734,14 +909,13 @@ void HAL_I2C_AbortCpltCallback(I2C_HandleTypeDef *hi2c){
|
|||
I2C_HandleTypeDef *handle = &(obj_s->handle);
|
||||
|
||||
/* Disable IT. Not always done before calling macro */
|
||||
__HAL_I2C_DISABLE_IT(handle, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
|
||||
__HAL_I2C_DISABLE_IT(handle, I2C_IT_ALL);
|
||||
i2c_ev_err_disable(obj);
|
||||
|
||||
/* Set event flag */
|
||||
obj_s->event = I2C_EVENT_ERROR;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void i2c_transfer_asynch(i2c_t *obj, const void *tx, size_t tx_length, void *rx, size_t rx_length, uint32_t address, uint32_t stop, uint32_t handler, uint32_t event, DMAUsage hint) {
|
||||
|
||||
// TODO: DMA usage is currently ignored by this way
|
||||
|
@ -840,7 +1014,8 @@ void i2c_abort_asynch(i2c_t *obj) {
|
|||
HAL_I2C_Master_Abort_IT(handle, Dummy_DevAddress);
|
||||
}
|
||||
|
||||
|
||||
#endif // DEVICE_I2C_ASYNCH
|
||||
|
||||
#endif // STM32F1
|
||||
|
||||
#endif // DEVICE_I2C
|
Loading…
Reference in New Issue