Skip to content

Commit 69bc70c

Browse files
author
Marti Bolivar
committed
Revert "- tx and rx callbacks for each module"
This reverts commit da01c80. Reverting pull request #54, which breaks examples/i2c-mcp4725-dac.cpp. Signed-off-by: Marti Bolivar <[email protected]>
1 parent 219a980 commit 69bc70c

File tree

4 files changed

+1
-306
lines changed

4 files changed

+1
-306
lines changed

CREDITS

-6
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,3 @@ W: http://sarup.dk/index.html
8181
N: Andy Scott ("xttocs")
8282
8383
D: LiquidCrystal library
84-
85-
N: Barry Carter ("ginge")
86-
87-
D: Added i2C slave support
88-
W: headfuzz.co.uk
89-

libmaple/i2c.c

+1-234
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,6 @@ void i2c_bus_reset(const i2c_dev *dev) {
154154
void i2c_init(i2c_dev *dev) {
155155
rcc_reset_dev(dev->clk_id);
156156
rcc_clk_enable(dev->clk_id);
157-
_i2c_irq_priority_fixup(dev);
158157
}
159158

160159
/* Hack for deprecated bit of STM32F1 functionality */
@@ -199,30 +198,12 @@ void i2c_master_enable(i2c_dev *dev, uint32 flags) {
199198
nvic_irq_enable(dev->er_nvic_line);
200199
i2c_enable_irq(dev, I2C_IRQ_EVENT | I2C_IRQ_BUFFER | I2C_IRQ_ERROR);
201200

202-
/* Configure the slave unit */
203-
if (flags & I2C_SLAVE_DUAL_ADDRESS) {
204-
i2c_slave_dual_address_enable(dev);
205-
}
206-
207-
if (flags & I2C_SLAVE_GENERAL_CALL) {
208-
i2c_slave_general_call_enable(dev);
209-
}
210-
211-
/* store all of the flags */
212-
dev->config_flags = flags;
213-
214201
/* Make it go! */
215202
i2c_peripheral_enable(dev);
216-
i2c_enable_ack(dev);
217203

218204
dev->state = I2C_STATE_IDLE;
219205
}
220206

221-
void i2c_slave_enable(i2c_dev *dev, uint32 flags) {
222-
i2c_disable(dev);
223-
i2c_master_enable(dev, dev->config_flags | flags);
224-
}
225-
226207
/**
227208
* @brief Process an i2c transaction.
228209
*
@@ -322,152 +303,6 @@ void _i2c_irq_handler(i2c_dev *dev) {
322303
*/
323304
dev->timestamp = systick_uptime();
324305

325-
/* Add Slave support
326-
* Barry Carter 2012
327-
328-
*/
329-
330-
/* Check to see if MSL master slave bit is set */
331-
if ((sr2 & I2C_SR2_MSL) != I2C_SR2_MSL) { /* 0 = slave mode 1 = master */
332-
333-
/* Check for address match */
334-
if (sr1 & I2C_SR1_ADDR) {
335-
/* Find out which address was matched */
336-
/* Check the general call address first */
337-
if (sr2 & I2C_SR2_GENCALL) {
338-
dev->i2c_slave_msg->addr = 0;
339-
}
340-
/* We matched the secondary address */
341-
else if (sr2 & I2C_SR2_DUALF) {
342-
dev->i2c_slave_msg->addr = dev->regs->OAR2 & 0xFE;
343-
}
344-
/* We matched the primary address */
345-
else if ((sr2 & I2C_SR2_DUALF) != I2C_SR2_DUALF) {
346-
dev->i2c_slave_msg->addr = dev->regs->OAR1 & 0xFE;
347-
}
348-
/* Shouldn't get here */
349-
else {
350-
dev->i2c_slave_msg->addr = -1; /* uh oh */
351-
}
352-
353-
/* if we have buffered io */
354-
if ((dev->config_flags & I2C_SLAVE_USE_RX_BUFFER) ||
355-
(dev->config_flags & I2C_SLAVE_USE_TX_BUFFER)) {
356-
357-
/* if receiving then this would be a repeated start
358-
*
359-
*if we have some bytes already
360-
*/
361-
if ((dev->state == I2C_STATE_SL_RX) &&
362-
(dev->i2c_slave_msg->xferred > 0) &&
363-
(dev->config_flags & I2C_SLAVE_USE_RX_BUFFER)) {
364-
/* Call the callback with the contents of the data */
365-
if (dev->i2c_slave_recv_callback != NULL) {
366-
(*(dev->i2c_slave_recv_callback))(dev->i2c_slave_msg);
367-
}
368-
}
369-
370-
/* Reset the message back to defaults.
371-
* We are starting a new message
372-
*/
373-
dev->i2c_slave_msg->flags = 0;
374-
dev->i2c_slave_msg->length = 0;
375-
dev->i2c_slave_msg->xferred = 0;
376-
dev->msgs_left = 0;
377-
dev->timestamp = systick_uptime();
378-
379-
/* We have been addressed with SLA+R so
380-
* the master wants us to transmit
381-
*/
382-
if ((sr1 & I2C_SR1_TXE) &&
383-
(dev->config_flags & I2C_SLAVE_USE_TX_BUFFER)) {
384-
/* Call the transmit callback so it can populate the msg
385-
* data with the bytes to go
386-
*/
387-
if (dev->i2c_slave_transmit_callback != NULL) {
388-
(*(dev->i2c_slave_transmit_callback))(dev->i2c_slave_msg);
389-
}
390-
}
391-
dev->state = I2C_STATE_BUSY;
392-
}
393-
394-
sr1 = sr2 = 0;
395-
}
396-
397-
/* EV3: Master requesting data from slave. Transmit a byte*/
398-
if (sr1 & I2C_SR1_TXE) {
399-
if (dev->config_flags & I2C_SLAVE_USE_TX_BUFFER) {
400-
if (dev->i2c_slave_msg->xferred >= dev->i2c_slave_msg->length) {
401-
/* End of the transmit buffer? If so we NACK */
402-
i2c_disable_ack(dev);
403-
/* We have to either issue a STOP or write something here.
404-
* STOP here seems to screw up some masters,
405-
* For now padding with 0
406-
*/
407-
i2c_write(dev, 0);
408-
/*i2c_stop_condition(dev); // This is causing bus lockups way more than it should !? Seems some I2C master devices freak out here*/
409-
}
410-
else
411-
{
412-
/* NACk the last byte */
413-
if (dev->i2c_slave_msg->xferred == dev->i2c_slave_msg->length-1) {
414-
i2c_disable_ack(dev);
415-
}
416-
else {
417-
i2c_enable_ack(dev);
418-
}
419-
i2c_write(dev, dev->i2c_slave_msg->data[dev->i2c_slave_msg->xferred++]);
420-
}
421-
}
422-
else
423-
{
424-
/* Call the callback to get the data we need.
425-
* The callback is expected to write using i2c_write(...)
426-
* If the slave is going to terminate the transfer, this function should
427-
* also do a NACK on the last byte!
428-
*/
429-
if (dev->i2c_slave_transmit_callback != NULL) (*(dev->i2c_slave_transmit_callback))(dev->i2c_slave_msg);
430-
}
431-
432-
dev->state = I2C_STATE_BUSY;
433-
sr1 = sr2 = 0;
434-
}
435-
436-
/* EV2: Slave received data from a master. Get from DR */
437-
if (sr1 & I2C_SR1_RXNE) {
438-
if (dev->config_flags & I2C_SLAVE_USE_RX_BUFFER) {
439-
/* Fill the buffer with the contents of the data register */
440-
dev->i2c_slave_msg->data[dev->i2c_slave_msg->xferred++] = dev->regs->DR;
441-
dev->i2c_slave_msg->length++;
442-
}
443-
else {
444-
/* Call the callback with the contents of the data */
445-
dev->i2c_slave_msg->data[0] = dev->regs->DR;
446-
if (dev->i2c_slave_recv_callback != NULL) (*(dev->i2c_slave_recv_callback))(dev->i2c_slave_msg);
447-
}
448-
dev->state = I2C_STATE_SL_RX;
449-
sr1 = sr2 = 0;
450-
}
451-
452-
/* EV4: Slave has detected a STOP condition on the bus */
453-
if (sr1 & I2C_SR1_STOPF) {
454-
dev->regs->CR1 |= I2C_CR1_PE;
455-
456-
if ((dev->config_flags & I2C_SLAVE_USE_RX_BUFFER) ||
457-
(dev->config_flags & I2C_SLAVE_USE_TX_BUFFER)) {
458-
459-
/* The callback with the data will happen on a NACK of the last data byte.
460-
* This is handled in the error IRQ (AF bit)
461-
*/
462-
}
463-
464-
sr1 = sr2 = 0;
465-
dev->state = I2C_STATE_IDLE;
466-
}
467-
468-
return;
469-
}
470-
471306
/*
472307
* EV5: Start condition sent
473308
*/
@@ -612,7 +447,6 @@ void _i2c_irq_handler(i2c_dev *dev) {
612447
}
613448
}
614449
}
615-
616450
}
617451

618452
/*
@@ -622,49 +456,10 @@ void _i2c_irq_handler(i2c_dev *dev) {
622456
void _i2c_irq_error_handler(i2c_dev *dev) {
623457
I2C_CRUMB(ERROR_ENTRY, dev->regs->SR1, dev->regs->SR2);
624458

625-
dev->error_flags = dev->regs->SR1 & (I2C_SR1_BERR |
459+
dev->error_flags = dev->regs->SR2 & (I2C_SR1_BERR |
626460
I2C_SR1_ARLO |
627461
I2C_SR1_AF |
628462
I2C_SR1_OVR);
629-
630-
/* Are we in slave mode? */
631-
if ((dev->regs->SR2 & I2C_SR2_MSL) != I2C_SR2_MSL) {
632-
/* Check to see if the master device did a NAK on the last bit
633-
* This is perfectly valid for a master to do this on the bus.
634-
* We ignore this. Any further error processing takes us into dead
635-
* loop waiting for the stop condition that will never arrive
636-
*/
637-
if (dev->regs->SR1 & I2C_SR1_AF) {
638-
/* Clear flags */
639-
dev->regs->SR1 = 0;
640-
dev->regs->SR2 = 0;
641-
/* We need to write something to CR1 to clear the flag.
642-
* This isn't really mentioned but seems important */
643-
i2c_enable_ack(dev);
644-
645-
if (dev->state == I2C_STATE_SL_RX &&
646-
dev->config_flags & I2C_SLAVE_USE_RX_BUFFER &&
647-
dev->i2c_slave_msg->xferred > 0) {
648-
/* Call the callback with the contents of the data */
649-
if (dev->i2c_slave_recv_callback != NULL) (*(dev->i2c_slave_recv_callback))(dev->i2c_slave_msg);
650-
}
651-
652-
dev->state = I2C_STATE_IDLE;
653-
return;
654-
}
655-
/* Catch any other strange errors while in slave mode.
656-
* I have seen BERR caused by an over fast master device
657-
* as well as several overflows and arbitration failures.
658-
* We are going to reset SR flags and carry on at this point which
659-
* is not the best thing to do, but stops the bus locking up completely
660-
* If we carry on below and send the stop bit, the code spins forever */
661-
/* Clear flags */
662-
dev->regs->SR1 = 0;
663-
dev->regs->SR2 = 0;
664-
dev->state = I2C_STATE_IDLE;
665-
return;
666-
}
667-
668463
/* Clear flags */
669464
dev->regs->SR1 = 0;
670465
dev->regs->SR2 = 0;
@@ -712,31 +507,3 @@ static void set_ccr_trise(i2c_dev *dev, uint32 flags) {
712507
i2c_set_clk_control(dev, ccr);
713508
i2c_set_trise(dev, trise);
714509
}
715-
716-
717-
/**
718-
* @brief callback for when the device acts as a slave. If using an rx buffer, this is triggered
719-
* after the last byte, otherwise it is called for every incoming packet.
720-
* @param dev I2C device
721-
* @param msg The dev_msg to pass to the slave init code
722-
* @param func The function pointer to call
723-
*/
724-
void i2c_slave_attach_recv_handler(i2c_dev *dev, i2c_msg *msg, i2c_slave_recv_callback_func func) {
725-
dev->i2c_slave_recv_callback = func;
726-
dev->i2c_slave_msg = msg;
727-
msg->xferred = 0;
728-
}
729-
730-
731-
/**
732-
* @brief callback for when the device acts as a slave. If using a tx buffer, this is triggered
733-
* after the device is successsfully addressed with SLA+R.
734-
* @param dev I2C device
735-
* @param msg The dev_msg to pass to the slave init code
736-
* @param func The function pointer to call
737-
*/
738-
void i2c_slave_attach_transmit_handler(i2c_dev *dev, i2c_msg *msg, i2c_slave_transmit_callback_func func) {
739-
dev->i2c_slave_transmit_callback = func;
740-
dev->i2c_slave_msg = msg;
741-
msg->xferred = 0;
742-
}

libmaple/include/libmaple/i2c.h

-52
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ typedef struct i2c_msg {
9393

9494
#define I2C_MSG_READ 0x1
9595
#define I2C_MSG_10BIT_ADDR 0x2
96-
9796
/**
9897
* Bitwise OR of:
9998
* - I2C_MSG_READ (write is default)
@@ -198,10 +197,6 @@ typedef struct i2c_msg {
198197
#define I2C_DUTY_16_9 0x2 // 16/9 duty ratio
199198
/* Flag 0x4 is reserved; DO NOT USE. */
200199
#define I2C_BUS_RESET 0x8 // Perform a bus reset
201-
#define I2C_SLAVE_USE_RX_BUFFER 0x10 // Use a buffered message when doing a slave recv
202-
#define I2C_SLAVE_USE_TX_BUFFER 0x20 // Use a buffered message when doing a slave transmit
203-
#define I2C_SLAVE_DUAL_ADDRESS 0x40 // Enable the dual slave address scheme
204-
#define I2C_SLAVE_GENERAL_CALL 0x80 // Enable the dual slave address scheme
205200
void i2c_master_enable(i2c_dev *dev, uint32 flags);
206201

207202
#define I2C_ERROR_PROTOCOL (-1)
@@ -411,53 +406,6 @@ static inline void i2c_set_trise(i2c_dev *dev, uint32 trise) {
411406
dev->regs->TRISE = trise;
412407
}
413408

414-
/* Barry Carter
415-
* Slave support
416-
*/
417-
418-
/**
419-
* @brief Enable Dual addressing mode to allow peripheral to have 2 addresses
420-
* @param dev I2C device
421-
*/
422-
static inline void i2c_slave_dual_address_enable(i2c_dev *dev) {
423-
dev->regs->OAR2 |= I2C_OAR2_ENDUAL;
424-
}
425-
426-
/**
427-
* @brief Enable General Call to allow the unit to respond on addr 0x00
428-
* @param dev I2C device
429-
*/
430-
static inline void i2c_slave_general_call_enable(i2c_dev *dev) {
431-
dev->regs->CR1 |= I2C_CR1_ENGC;
432-
}
433-
434-
/* callback functions */
435-
/* Callback handler for data received over the bus */
436-
void i2c_slave_attach_recv_handler(i2c_dev *dev, i2c_msg *msg, i2c_slave_recv_callback_func func);
437-
438-
/* Callback handler for data being requested over the bus
439-
* The callback function must call i2c_write to get the data over the bus
440-
*/
441-
void i2c_slave_attach_transmit_handler(i2c_dev *dev, i2c_msg *msg, i2c_slave_transmit_callback_func func);
442-
443-
/**
444-
* @brief Set the primary I2c slave address
445-
* @param dev I2C device
446-
* @param address the 8 or 10 bit i2c address
447-
*/
448-
static inline void i2c_slave_set_own_address(i2c_dev *dev, uint16 address) {
449-
dev->regs->OAR1 = address <<1;
450-
}
451-
452-
/**
453-
* @brief Set the secondary I2c slave address
454-
* @param dev I2C device
455-
* @param address the 8 or 10 bit i2c address
456-
*/
457-
static inline void i2c_slave_set_own_address2(i2c_dev *dev, uint16 address) {
458-
dev->regs->OAR2 = (address <<1 ) | I2C_OAR2_ENDUAL;
459-
}
460-
461409
#ifdef __cplusplus
462410
}
463411
#endif

libmaple/include/libmaple/i2c_common.h

-14
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,9 @@ typedef enum i2c_state {
5252
I2C_STATE_IDLE = 1, /**< Idle */
5353
I2C_STATE_XFER_DONE = 2, /**< Done with transfer */
5454
I2C_STATE_BUSY = 3, /**< Busy */
55-
I2C_STATE_SL_RX = 4, /**< Slave receiving */
5655
I2C_STATE_ERROR = -1 /**< Error occurred */
5756
} i2c_state;
5857

59-
typedef void (*i2c_slave_recv_callback_func)(struct i2c_msg *);
60-
typedef void (*i2c_slave_transmit_callback_func)(struct i2c_msg *);
6158
/**
6259
* @brief I2C device type.
6360
*/
@@ -91,17 +88,6 @@ typedef struct i2c_dev {
9188
nvic_irq_num ev_nvic_line; /**< Event IRQ number */
9289
nvic_irq_num er_nvic_line; /**< Error IRQ number */
9390
volatile i2c_state state; /**< Device state */
94-
uint32 config_flags; /**< Configuration flags */
95-
96-
/* Barry Carter
97-
* Slave implementation. Callback functions in this struct allow
98-
* for a separate callback function for each I2C unit available onboard
99-
*/
100-
i2c_slave_transmit_callback_func i2c_slave_transmit_callback;
101-
i2c_slave_recv_callback_func i2c_slave_recv_callback;
102-
103-
struct i2c_msg *i2c_slave_msg; /* the message that the i2c slave will use */
104-
10591
} i2c_dev;
10692

10793
#endif

0 commit comments

Comments
 (0)