interrupt i2c

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 
已解决

interrupt i2c

跳至解决方案
1,426 次查看
Seitec
Contributor III

Hi I would like learn use MQX.

 

I don't understand one thing. When I have for example interrupt (read from I2C) and I would like do something exactly after receive data. How I do it? Because all project what I saw. There was command read_I2C( . and nothing interrution. Please can somebody explain  how it works?

 

Thank you very much.

 

 

0 项奖励
1 解答
721 次查看
PetrM
Senior Contributor I

Hello,

 

if you have some very specific scenario, you don't have to use I2C driver, just set up pins using _bsp_i2c_io_init(), install your own I2C ISR and enable I2C module and interrupts (see interrupt I2C init function).

If you want to use interrupt I2C driver, you can't get after-interrupt notification, the only way to wait for I2C interrupt is through read or write + fflush (interrrupts are handled within I2C driver):

 

// example for master mode

fd = fopen ("ii2c0:", NULL);

 

// read

result = 0;

do
{
   result += fread (buffer, 1, 1, fd);
} while (result < 1);
your_action();

 

// write

result = 0;

do
{
   result += fwrite (buffer, 1, 1, fd);
} while (result < 1);
result = fflush (fd);

your_action();

 

PetrM

 

在原帖中查看解决方案

0 项奖励
6 回复数
722 次查看
PetrM
Senior Contributor I

Hello,

 

if you have some very specific scenario, you don't have to use I2C driver, just set up pins using _bsp_i2c_io_init(), install your own I2C ISR and enable I2C module and interrupts (see interrupt I2C init function).

If you want to use interrupt I2C driver, you can't get after-interrupt notification, the only way to wait for I2C interrupt is through read or write + fflush (interrrupts are handled within I2C driver):

 

// example for master mode

fd = fopen ("ii2c0:", NULL);

 

// read

result = 0;

do
{
   result += fread (buffer, 1, 1, fd);
} while (result < 1);
your_action();

 

// write

result = 0;

do
{
   result += fwrite (buffer, 1, 1, fd);
} while (result < 1);
result = fflush (fd);

your_action();

 

PetrM

 

0 项奖励
721 次查看
Mohsin455
Contributor IV

Hi PetrM,

 

                I also have a doubt with regards to interrupt  mode.

 

If I try to a read in task code, will the task be blocked till any new data is read or will it be active ?

 

my_read_task() {

// read

result = 0;

do
{
   result += fread (buffer, 1, 1, fd);     // Will this put the task in blocked state if there is no data ?
} while (result < 1);

}

 

Also the same thing while writing. Will the task be blocked till the whole data is transmitted or will it be in active state ?

 

my_write_task() {

// write

result = 0;

do
{
   result += fwrite (buffer, 1, 1, fd);
} while (result < 1);
result = fflush (fd);     // Will this put the task in blocked state while the data is being written ? 

}

 

Thanks,

Mohsin

0 项奖励
721 次查看
PetrM
Senior Contributor I

Hello,

 

the interrupt I2C was designed non-blocking. The intention was to transfer the control upon the task to the user so he can decide what to do while I2C transmission is in progress (to block with delay or do some other computation).

So the task remains active all the time.

Regarding the fflush(), it is also done actively using polling because of poor HW design (lack of interrupt requests to important bus events).

 

Regards,

PetrM

 

0 项奖励
721 次查看
Mohsin455
Contributor IV

Hi PetrM,

 

                      Thanks for your reply. The wory I have is that the interrupt mode in the present state will consume more CPU time as mentioned in the following post.

 

https://community.freescale.com/thread/101676

 

Can you let me know your thoughts on enhancing the I2C driver similar to the above post for kinetis TWRK60N512 ?

 

Does the same  applies to Serial (UART) drivers ?

 

Regards,

Mohsin

0 项奖励
721 次查看
trailman
Contributor V

Hi,

 

I had exactly the same problem on a MCF52259. I wanted to have a full interrupt driven I2C driver making my read or write or ioctl calls sleep while tranfer is in progress.

I also wanted to have timeouts to prevent I2C deadlocks.

I did it but only for master mode (did not have the time to do more).

I posted the modified code in this post.

 

For an interrupt driven serial line, the read() call sleeps, that's why we do not face the problem of a shell task at higher priority waiting a command while consuming all CPU.

 

0 项奖励
721 次查看
Seitec
Contributor III

thank you very much for explanation

0 项奖励