Added K22F I2C + fixed I2C

First part was pinouts. Second part is that the earlier done new I2C
commands were wrong. The macros for writing for example status register
expect the I2C base address, and were only getting the number of the i2c
peripheral. After figuring that one out technically the changes in
timeout_status_poll aren't necesary, but it won't hurt either.
pull/476/head
Sissors 2014-09-07 21:39:16 +02:00
parent 978732550b
commit 555d43fab4
5 changed files with 37 additions and 19 deletions

View File

@ -39,7 +39,6 @@ typedef enum {
typedef enum {
I2C_0 = 0,
I2C_1 = 1,
I2C_2 = 2,
} I2CName;
#define TPM_SHIFT 8

View File

@ -35,12 +35,24 @@ const PinMap PinMap_DAC[] = {
/************I2C***************/
const PinMap PinMap_I2C_SDA[] = {
{PTB1 , I2C_0 , 2},
{PTB3 , I2C_0 , 2},
{PTC11, I2C_1 , 2},
{PTD3 , I2C_0 , 7},
{PTD9 , I2C_0 , 2},
{PTE0 , I2C_1 , 6},
{PTE25, I2C_0 , 5},
{NC , NC , 0}
};
const PinMap PinMap_I2C_SCL[] = {
{PTB0 , I2C_0 , 2},
{PTB2 , I2C_0 , 2},
{PTC10, I2C_1 , 2},
{PTD2 , I2C_0 , 7},
{PTD8 , I2C_0 , 2},
{PTE1 , I2C_1 , 6},
{PTE24, I2C_0 , 5},
{NC , NC , 0}
};

View File

@ -27,8 +27,8 @@
#define DEVICE_SERIAL 1
#define DEVICE_I2C 0
#define DEVICE_I2CSLAVE 0
#define DEVICE_I2C 1
#define DEVICE_I2CSLAVE 1
#define DEVICE_SPI 0
#define DEVICE_SPISLAVE 0

View File

@ -33,11 +33,10 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
MBED_ASSERT((int)obj->instance != NC);
CLOCK_SYS_EnableI2cClock(obj->instance);
CLOCK_SYS_EnablePortClock(sda >> GPIO_PORT_SHIFT);
CLOCK_SYS_EnablePortClock(scl >> GPIO_PORT_SHIFT);
uint32_t i2c_addrs[] = I2C_BASE_ADDRS;
I2C_HAL_Init(i2c_addrs[obj->instance]);
I2C_HAL_Enable(i2c_addrs[obj->instance]);
I2C_HAL_SetIntCmd(i2c_addrs[obj->instance], true);
i2c_frequency(obj, 100000);
pinmap_pinout(sda, PinMap_I2C_SDA);
@ -57,7 +56,8 @@ int i2c_start(i2c_t *obj) {
int i2c_stop(i2c_t *obj) {
volatile uint32_t n = 0;
uint32_t i2c_addrs[] = I2C_BASE_ADDRS;
I2C_HAL_SendStop(i2c_addrs[obj->instance]);
if (I2C_HAL_IsMaster(i2c_addrs[obj->instance]))
I2C_HAL_SendStop(i2c_addrs[obj->instance]);
// It seems that there are timing problems
// when there is no waiting time after a STOP.
@ -67,11 +67,12 @@ int i2c_stop(i2c_t *obj) {
return 0;
}
static int timeout_status_poll(i2c_t *obj, uint32_t mask) {
static int timeout_status_poll(i2c_t *obj, i2c_status_flag_t flag) {
uint32_t i, timeout = 100000;
uint32_t i2c_addrs[] = I2C_BASE_ADDRS;
for (i = 0; i < timeout; i++) {
if (HW_I2C_S_RD(obj->instance) & mask)
if (I2C_HAL_GetStatusFlag(i2c_addrs[obj->instance], flag))
return 0;
}
return 1;
@ -83,14 +84,15 @@ static int timeout_status_poll(i2c_t *obj, uint32_t mask) {
// 2: failure
static int i2c_wait_end_tx_transfer(i2c_t *obj) {
// wait for the interrupt flag
if (timeout_status_poll(obj, I2C_S_IICIF_MASK)) {
uint32_t i2c_addrs[] = I2C_BASE_ADDRS;
if (timeout_status_poll(obj, kI2CInterruptPending)) {
return 2;
}
uint32_t i2c_addrs[] = I2C_BASE_ADDRS;
I2C_HAL_ClearInt(i2c_addrs[obj->instance]);
// wait transfer complete
if (timeout_status_poll(obj, I2C_S_TCF_MASK)) {
if (timeout_status_poll(obj, kI2CTransferComplete)) {
return 2;
}
@ -103,7 +105,7 @@ static int i2c_wait_end_tx_transfer(i2c_t *obj) {
// 1: failure
static int i2c_wait_end_rx_transfer(i2c_t *obj) {
// wait for the end of the rx transfer
if (timeout_status_poll(obj, I2C_S_IICIF_MASK)) {
if (timeout_status_poll(obj, kI2CInterruptPending)) {
return 1;
}
uint32_t i2c_addrs[] = I2C_BASE_ADDRS;
@ -239,16 +241,17 @@ void i2c_slave_mode(i2c_t *obj, int enable_slave) {
uint32_t i2c_addrs[] = I2C_BASE_ADDRS;
if (enable_slave) {
// set slave mode
BW_I2C_C1_MST(obj->instance, 0);
BW_I2C_C1_MST(i2c_addrs[obj->instance], 0);
I2C_HAL_SetIntCmd(i2c_addrs[obj->instance], true);
} else {
// set master mode
BW_I2C_C1_MST(obj->instance, 1);
BW_I2C_C1_MST(i2c_addrs[obj->instance], 1);
}
}
int i2c_slave_receive(i2c_t *obj) {
switch(HW_I2C_S_RD(obj->instance)) {
uint32_t i2c_addrs[] = I2C_BASE_ADDRS;
switch(HW_I2C_S_RD(i2c_addrs[obj->instance]) {
// read addressed
case 0xE6:
return 1;

View File

@ -2,9 +2,13 @@
#include "test_env.h"
#include "MMA7660.h"
MMA7660 MMA(p28, p27);
//MMA7660 MMA(p28, p27);
I2C i2c(PTB3, PTB2);
int main() {
for (int i = 0; i<128; i++)
printf("Write to %d = %d\r\n", i, i2c.write(i * 2, NULL, 0));
/*
if (!MMA.testConnection())
notify_completion(false);
@ -12,6 +16,6 @@ int main() {
printf("x: %f, y: %f, z: %f\r\n", MMA.x(), MMA.y(), MMA.z());
wait(0.2);
}
*/
notify_completion(true);
}