aboutsummaryrefslogtreecommitdiffstats
path: root/libmaple/i2c.h
diff options
context:
space:
mode:
authorPerry Hung <iperry@gmail.com>2011-03-14 18:37:56 -0400
committerPerry Hung <iperry@gmail.com>2011-03-14 22:52:06 -0400
commit334de50c49317f281d87f44e7b9278e08af19254 (patch)
tree2aa29c4f6aecb60c2f4ce5964d876745b1757d60 /libmaple/i2c.h
parent9872b3975bd40c55652bb9dead5f54e2be9740d0 (diff)
downloadlibrambutan-334de50c49317f281d87f44e7b9278e08af19254.tar.gz
librambutan-334de50c49317f281d87f44e7b9278e08af19254.zip
Add rudimentary error handling for nack condition
Diffstat (limited to 'libmaple/i2c.h')
-rw-r--r--libmaple/i2c.h43
1 files changed, 32 insertions, 11 deletions
diff --git a/libmaple/i2c.h b/libmaple/i2c.h
index be05615..a9e2c7b 100644
--- a/libmaple/i2c.h
+++ b/libmaple/i2c.h
@@ -136,17 +136,13 @@ extern "C" {
void i2c_master_enable(i2c_dev *dev, uint32 flags);
int32 i2c_master_xfer(i2c_dev *dev, i2c_msg *msgs, uint16 num);
-static inline void i2c_write(i2c_dev *dev, uint8 byte) {
- dev->regs->DR = byte;
-}
-
/*
* Low level register twiddling functions
*/
/**
* @brief turn on an i2c peripheral
- * @param map i2c peripheral register base
+ * @param dev i2c device
*/
static inline void i2c_peripheral_enable(i2c_dev *dev) {
dev->regs->CR1 |= I2C_CR1_PE;
@@ -154,15 +150,25 @@ static inline void i2c_peripheral_enable(i2c_dev *dev) {
/**
* @brief turn off an i2c peripheral
- * @param map i2c peripheral register base
+ * @param dev i2c device
*/
static inline void i2c_peripheral_disable(i2c_dev *dev) {
dev->regs->CR1 &= ~I2C_CR1_PE;
}
/**
+ * @brief Fill transmit register
+ * @param dev i2c device
+ * @param byte byte to write
+ */
+static inline void i2c_write(i2c_dev *dev, uint8 byte) {
+ dev->regs->DR = byte;
+}
+
+
+/**
* @brief Set input clock frequency, in mhz
- * @param device to configure
+ * @param dev i2c
* @param freq frequency in megahertz (2-36)
*/
static inline void i2c_set_input_clk(i2c_dev *dev, uint32 freq) {
@@ -172,6 +178,13 @@ static inline void i2c_set_input_clk(i2c_dev *dev, uint32 freq) {
dev->regs->CR2 = freq;
}
+
+/**
+ * @brief Set i2c clock control register. See RM008
+ * @param dev i2c device
+ * @return
+ * @sideeffect
+ */
static inline void i2c_set_clk_control(i2c_dev *dev, uint32 val) {
uint32 ccr = dev->regs->CCR;
ccr &= ~I2C_CCR_CCR;
@@ -195,15 +208,23 @@ static inline void i2c_set_trise(i2c_dev *dev, uint32 trise) {
dev->regs->TRISE = trise;
}
-extern void toggle(void);
static inline void i2c_start_condition(i2c_dev *dev) {
- uint32 cr1 = dev->regs->CR1;
-// if (cr1 & (I2C_CR1_START | I2C_CR1_STOP | I2C_CR1_PEC)) {
-// }
+ uint32 cr1;
+ while ((cr1 = dev->regs->CR1) & (I2C_CR1_START |
+ I2C_CR1_STOP |
+ I2C_CR1_PEC)) {
+ ;
+ }
dev->regs->CR1 |= I2C_CR1_START;
}
static inline void i2c_stop_condition(i2c_dev *dev) {
+ uint32 cr1;
+ while ((cr1 = dev->regs->CR1) & (I2C_CR1_START |
+ I2C_CR1_STOP |
+ I2C_CR1_PEC)) {
+ ;
+ }
dev->regs->CR1 |= I2C_CR1_STOP;
}