mirror of https://github.com/ARMmbed/mbed-os.git
[LPC1549] Added I2C master support
parent
95f4057499
commit
a2f2f44258
|
@ -28,7 +28,7 @@
|
|||
#define DEVICE_SERIAL 1
|
||||
#define DEVICE_SERIAL_FC 1
|
||||
|
||||
#define DEVICE_I2C 0
|
||||
#define DEVICE_I2C 1
|
||||
#define DEVICE_I2CSLAVE 0
|
||||
|
||||
#define DEVICE_SPI 1
|
||||
|
|
|
@ -0,0 +1,258 @@
|
|||
/* mbed Microcontroller Library
|
||||
* Copyright (c) 2006-2013 ARM Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "i2c_api.h"
|
||||
#include "cmsis.h"
|
||||
#include "pinmap.h"
|
||||
#include "error.h"
|
||||
|
||||
static uint8_t repeated_start = 0;
|
||||
|
||||
#define I2C_STAT(x) ((LPC_I2C0->STAT >> 1) & (0x07))
|
||||
|
||||
static inline int i2c_status(i2c_t *obj) {
|
||||
return I2C_STAT(obj);
|
||||
}
|
||||
|
||||
// Wait until the Serial Interrupt (SI) is set
|
||||
static int i2c_wait_SI(i2c_t *obj) {
|
||||
int timeout = 0;
|
||||
while (!(LPC_I2C0->STAT & (1 << 0))) {
|
||||
timeout++;
|
||||
if (timeout > 100000) return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static inline void i2c_interface_enable(i2c_t *obj) {
|
||||
LPC_I2C0->CFG |= (1 << 0);
|
||||
}
|
||||
|
||||
static inline void i2c_power_enable(i2c_t *obj) {
|
||||
// Enables clock for I2C0
|
||||
LPC_SYSCON->SYSAHBCLKCTRL1 |= (1<<13);
|
||||
// LPC_SYSCON->PRESETCTRL1 &= ~(0x1<<13);
|
||||
LPC_SYSCON->PRESETCTRL1 |= (0x1<<13);
|
||||
LPC_SYSCON->PRESETCTRL1 &= ~(0x1 << 13);
|
||||
|
||||
}
|
||||
|
||||
void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
|
||||
|
||||
// ピン定義の確認どうしよう…
|
||||
|
||||
|
||||
// enable power
|
||||
i2c_power_enable(obj);
|
||||
// pin enable
|
||||
LPC_SWM->PINENABLE1 &= ~(0x3 << 3);
|
||||
// set default frequency at 100k
|
||||
i2c_frequency(obj, 100000);
|
||||
i2c_interface_enable(obj);
|
||||
}
|
||||
|
||||
inline int i2c_start(i2c_t *obj) {
|
||||
int status = 0;
|
||||
if (repeated_start) {
|
||||
LPC_I2C0->MSTCTL = (1 << 1) | (1 << 0);
|
||||
repeated_start = 0;
|
||||
} else {
|
||||
LPC_I2C0->MSTCTL = (1 << 1);
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
inline int i2c_stop(i2c_t *obj) {
|
||||
int timeout = 0;
|
||||
|
||||
LPC_I2C0->MSTCTL = (1 << 2) | (1 << 0);
|
||||
while ((LPC_I2C0->STAT & ((1 << 0) | (7 << 1))) != ((1 << 0) | (0 << 1))) {
|
||||
timeout ++;
|
||||
if (timeout > 100000) return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static inline int i2c_do_write(i2c_t *obj, int value, uint8_t addr) {
|
||||
// write the data
|
||||
LPC_I2C0->MSTDAT = value;
|
||||
|
||||
if (!addr)
|
||||
LPC_I2C0->MSTCTL = (1 << 0);
|
||||
|
||||
// wait and return status
|
||||
i2c_wait_SI(obj);
|
||||
return i2c_status(obj);
|
||||
}
|
||||
|
||||
static inline int i2c_do_read(i2c_t *obj, int last) {
|
||||
// wait for it to arrive
|
||||
i2c_wait_SI(obj);
|
||||
if (!last)
|
||||
LPC_I2C0->MSTCTL = (1 << 0);
|
||||
|
||||
// return the data
|
||||
//return (I2C_DAT(obj) & 0xFF);
|
||||
return (LPC_I2C0->MSTDAT & 0xFF);
|
||||
}
|
||||
|
||||
void i2c_frequency(i2c_t *obj, int hz) {
|
||||
// No peripheral clock divider on the M0
|
||||
uint32_t PCLK = SystemCoreClock;
|
||||
|
||||
uint32_t clkdiv = PCLK / (hz * 4) - 1;
|
||||
|
||||
LPC_I2C0->DIV = clkdiv;
|
||||
LPC_I2C0->MSTTIME = 0;
|
||||
}
|
||||
|
||||
int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
|
||||
int count, status;
|
||||
int timeout = 0;
|
||||
|
||||
i2c_start(obj);
|
||||
|
||||
//status = i2c_do_write(obj, (address | 0x01), 1);
|
||||
LPC_I2C0->MSTDAT = (address | 0x01);
|
||||
LPC_I2C0->MSTCTL |= 0x20;
|
||||
while (!(LPC_I2C0->STAT & (1 << 0))) {
|
||||
timeout++;
|
||||
if (timeout > 100000) return -1;
|
||||
}
|
||||
status = ((LPC_I2C0->STAT >> 1) & (0x07));
|
||||
|
||||
if (status != 0x01) {
|
||||
i2c_stop(obj);
|
||||
return I2C_ERROR_NO_SLAVE;
|
||||
}
|
||||
|
||||
// Read in all except last byte
|
||||
for (count = 0; count < (length - 1); count++) {
|
||||
//int value = i2c_do_read(obj, 0);
|
||||
while (!(LPC_I2C0->STAT & (1 << 0))) {
|
||||
timeout++;
|
||||
if (timeout > 100000) return -1;
|
||||
}
|
||||
if (!0)
|
||||
LPC_I2C0->MSTCTL = (1 << 0);
|
||||
data[count] = (LPC_I2C0->MSTDAT & 0xFF);
|
||||
//
|
||||
status = ((LPC_I2C0->STAT >> 1) & (0x07));
|
||||
if (status != 0x00) {
|
||||
i2c_stop(obj);
|
||||
return count;
|
||||
}
|
||||
//data[count] = (char) value;
|
||||
}
|
||||
|
||||
// read in last byte
|
||||
//int value = i2c_do_read(obj, 1);
|
||||
while (!(LPC_I2C0->STAT & (1 << 0))) {
|
||||
timeout++;
|
||||
if (timeout > 100000) return -1;
|
||||
}
|
||||
data[count] = (LPC_I2C0->MSTDAT & 0xFF);
|
||||
//
|
||||
status = i2c_status(obj);
|
||||
if (status != 0x01) {
|
||||
i2c_stop(obj);
|
||||
return length - 1;
|
||||
}
|
||||
|
||||
//data[count] = (char) value;
|
||||
|
||||
// If not repeated start, send stop.
|
||||
if (stop) {
|
||||
i2c_stop(obj);
|
||||
} else {
|
||||
repeated_start = 1;
|
||||
}
|
||||
|
||||
return length;
|
||||
}
|
||||
|
||||
int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
|
||||
int i, status;
|
||||
int timeout = 0;
|
||||
|
||||
i2c_start(obj);
|
||||
|
||||
//status = i2c_do_write(obj, (address & 0xFE), 1);
|
||||
LPC_I2C0->MSTDAT = (address & 0xFE);
|
||||
LPC_I2C0->MSTCTL |= 0x20;
|
||||
// wait and return status
|
||||
while (!(LPC_I2C0->STAT & (1 << 0))) {
|
||||
timeout++;
|
||||
if (timeout > 100000) return -1;
|
||||
}
|
||||
status = ((LPC_I2C0->STAT >> 1) & (0x07));
|
||||
|
||||
if (status != 0x02) {
|
||||
i2c_stop(obj);
|
||||
return I2C_ERROR_NO_SLAVE;
|
||||
}
|
||||
|
||||
for (i=0; i<length; i++) {
|
||||
//status = i2c_do_write(obj, data[i], 0);
|
||||
LPC_I2C0->MSTDAT = data[i];
|
||||
LPC_I2C0->MSTCTL = (1 << 0);
|
||||
// wait and return status
|
||||
while (!(LPC_I2C0->STAT & (1 << 0))) {
|
||||
timeout++;
|
||||
if (timeout > 100000) return -1;
|
||||
}
|
||||
status = ((LPC_I2C0->STAT >> 1) & (0x07));
|
||||
if (status != 0x02) {
|
||||
i2c_stop(obj);
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
// If not repeated start, send stop.
|
||||
if (stop) {
|
||||
i2c_stop(obj);
|
||||
} else {
|
||||
repeated_start = 1;
|
||||
}
|
||||
|
||||
return length;
|
||||
}
|
||||
|
||||
void i2c_reset(i2c_t *obj) {
|
||||
i2c_stop(obj);
|
||||
}
|
||||
|
||||
int i2c_byte_read(i2c_t *obj, int last) {
|
||||
return (i2c_do_read(obj, last) & 0xFF);
|
||||
}
|
||||
|
||||
int i2c_byte_write(i2c_t *obj, int data) {
|
||||
int ack;
|
||||
int status = i2c_do_write(obj, (data & 0xFF), 0);
|
||||
|
||||
switch(status) {
|
||||
case 2:
|
||||
ack = 1;
|
||||
break;
|
||||
default:
|
||||
ack = 0;
|
||||
break;
|
||||
}
|
||||
|
||||
return ack;
|
||||
}
|
Loading…
Reference in New Issue