1

我是飞思卡尔 MQX 的新手,我正在尝试使用 MQX 功能在输入引脚上设置中断(以防万一我们想更改 MPU)。我找不到任何好的教程...有人可以指点我一个方向吗?谢谢

4

1 回答 1

1

Let's setup an interrupt on rising edge for PTA5, ok?

Define a macro to represent your pin. Not really necessary, but helps.

#define MY_GPIO_INT_PIN     (GPIO_PORT_A|GPIO_PIN_IRQ_RISING|GPIO_PIN5)

Declare some needed variables

PORT_MemMapPtr  pctl; 
GPIO_PIN_STRUCT  pins[2];
MQX_FILE_PTR pin_fd;

Get the base pointer for your pin port, and set the apropriate mux option (found on your chip reference manual).

//note: this code should be in init_gpio.c, from your bsp folder.
pctl = (PORT_MemMapPtr) PORTA_BASE_PTR;
/* PTA5 as GPIO (Alt.1)  */
pctl->PCR[5] = PORT_PCR_MUX(1) ;

Fill an array of pin structs. Note that you can setup more than one pin at once, and the array needs to be terminated with GPIO_LIST_END, so the driver knows where to stop.

pins[0] = MY_GPIO_INT_PIN;
pins[1] = GPIO_LIST_END;

As a semi POSIX compliant os, pretty much anything is treated as a file on MQX. Lets open a file handler for your pin:

pin_fd = fopen("gpio:input", (char*)pins);

Check if all went well

if(NULL == pin_fd){
    //something bad happened, check for error with ferror(fd)
}

Now register the callback for your pin

void pin_int_callback(void* data){
    //interrupt handle code goes here
}

if(IO_OK != ioctl(pin_fd, GPIO_IOCTL_SET_IRQ_FUNCTION, (void*)pin_int_callback)){
   //something bad happened registering your callback
}

Done! Try putting it all together.

于 2014-03-21T16:20:07.880 回答