@@ -154,7 +154,6 @@ void i2c_bus_reset(const i2c_dev *dev) {
154
154
void i2c_init (i2c_dev * dev ) {
155
155
rcc_reset_dev (dev -> clk_id );
156
156
rcc_clk_enable (dev -> clk_id );
157
- _i2c_irq_priority_fixup (dev );
158
157
}
159
158
160
159
/* Hack for deprecated bit of STM32F1 functionality */
@@ -199,30 +198,12 @@ void i2c_master_enable(i2c_dev *dev, uint32 flags) {
199
198
nvic_irq_enable (dev -> er_nvic_line );
200
199
i2c_enable_irq (dev , I2C_IRQ_EVENT | I2C_IRQ_BUFFER | I2C_IRQ_ERROR );
201
200
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
-
214
201
/* Make it go! */
215
202
i2c_peripheral_enable (dev );
216
- i2c_enable_ack (dev );
217
203
218
204
dev -> state = I2C_STATE_IDLE ;
219
205
}
220
206
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
-
226
207
/**
227
208
* @brief Process an i2c transaction.
228
209
*
@@ -322,152 +303,6 @@ void _i2c_irq_handler(i2c_dev *dev) {
322
303
*/
323
304
dev -> timestamp = systick_uptime ();
324
305
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
-
471
306
/*
472
307
* EV5: Start condition sent
473
308
*/
@@ -612,7 +447,6 @@ void _i2c_irq_handler(i2c_dev *dev) {
612
447
}
613
448
}
614
449
}
615
-
616
450
}
617
451
618
452
/*
@@ -622,49 +456,10 @@ void _i2c_irq_handler(i2c_dev *dev) {
622
456
void _i2c_irq_error_handler (i2c_dev * dev ) {
623
457
I2C_CRUMB (ERROR_ENTRY , dev -> regs -> SR1 , dev -> regs -> SR2 );
624
458
625
- dev -> error_flags = dev -> regs -> SR1 & (I2C_SR1_BERR |
459
+ dev -> error_flags = dev -> regs -> SR2 & (I2C_SR1_BERR |
626
460
I2C_SR1_ARLO |
627
461
I2C_SR1_AF |
628
462
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
-
668
463
/* Clear flags */
669
464
dev -> regs -> SR1 = 0 ;
670
465
dev -> regs -> SR2 = 0 ;
@@ -712,31 +507,3 @@ static void set_ccr_trise(i2c_dev *dev, uint32 flags) {
712
507
i2c_set_clk_control (dev , ccr );
713
508
i2c_set_trise (dev , trise );
714
509
}
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
- }
0 commit comments