PORT "Chapter 12 Port Control and Interrupts (PORT)" 管脚控制与中断
关于管脚复用(PTC0复用GPIO)官方库:
PORT_SetPinMux(PORTC, 0U, kPORT_MuxAsGpio);
<fsl_port.h>
/*! @brief Pin mux selection */
typedef enum _port_mux
{
kPORT_PinDisabledOrAnalog = 0U, /*!< Corresponding pin is disabled, but is used as an analog pin. */
kPORT_MuxAsGpio = 1U, /*!< Corresponding pin is configured as GPIO. */
kPORT_MuxAlt2 = 2U, /*!< Chip-specific */
kPORT_MuxAlt3 = 3U, /*!< Chip-specific */
kPORT_MuxAlt4 = 4U, /*!< Chip-specific */
kPORT_MuxAlt5 = 5U, /*!< Chip-specific */
kPORT_MuxAlt6 = 6U, /*!< Chip-specific */
kPORT_MuxAlt7 = 7U, /*!< Chip-specific */
kPORT_MuxAlt8 = 8U, /*!< Chip-specific */
kPORT_MuxAlt9 = 9U, /*!< Chip-specific */
kPORT_MuxAlt10 = 10U, /*!< Chip-specific */
kPORT_MuxAlt11 = 11U, /*!< Chip-specific */
kPORT_MuxAlt12 = 12U, /*!< Chip-specific */
kPORT_MuxAlt13 = 13U, /*!< Chip-specific */
kPORT_MuxAlt14 = 14U, /*!< Chip-specific */
kPORT_MuxAlt15 = 15U, /*!< Chip-specific */
} port_mux_t;
/*!
* @brief Configures the pin muxing.
*
* @param base PORT peripheral base pointer.
* @param pin PORT pin number.
* @param mux pin muxing slot selection.
* - #kPORT_PinDisabledOrAnalog: Pin disabled or work in analog function.
* - #kPORT_MuxAsGpio : Set as GPIO.
* - #kPORT_MuxAlt2 : chip-specific.
* - #kPORT_MuxAlt3 : chip-specific.
* - #kPORT_MuxAlt4 : chip-specific.
* - #kPORT_MuxAlt5 : chip-specific.
* - #kPORT_MuxAlt6 : chip-specific.
* - #kPORT_MuxAlt7 : chip-specific.
* @Note : This function is NOT recommended to use together with the PORT_SetPinsConfig, because
* the PORT_SetPinsConfig need to configure the pin mux anyway (Otherwise the pin mux is
* reset to zero : kPORT_PinDisabledOrAnalog).
* This function is recommended to use to reset the pin mux
*
*/
static inline void PORT_SetPinMux(PORT_Type *base, uint32_t pin, port_mux_t mux)
{
base->PCR[pin] = (base->PCR[pin] & ~PORT_PCR_MUX_MASK) | PORT_PCR_MUX(mux);
}
<MKV58F24.h>
#define PORT_PCR_MUX_MASK (0xF00U)
#define PORT_PCR_MUX_SHIFT (8U)
#define PORT_PCR_MUX(x) (((uint32_t)(((uint32_t)(x)) << PORT_PCR_MUX_SHIFT)) & PORT_PCR_MUX_MASK)
/** PORT - Register Layout Typedef */
typedef struct {
__IO uint32_t PCR[32]; /**< Pin Control Register n, array offset: 0x0, array step: 0x4 */
__O uint32_t GPCLR; /**< Global Pin Control Low Register, offset: 0x80 */
__O uint32_t GPCHR; /**< Global Pin Control High Register, offset: 0x84 */
uint8_t RESERVED_0[24];
__IO uint32_t ISFR; /**< Interrupt Status Flag Register, offset: 0xA0 */
uint8_t RESERVED_1[28];
__IO uint32_t DFER; /**< Digital Filter Enable Register, offset: 0xC0 */
__IO uint32_t DFCR; /**< Digital Filter Clock Register, offset: 0xC4 */
__IO uint32_t DFWR; /**< Digital Filter Width Register, offset: 0xC8 */
} PORT_Type;
PORT分多个通道,PORTA~PORTE,
每个通道寄存器按地址顺序为PCR0~PCR31,GPCLR,GPCHR,ISFR,DFER,DFCR,DFWR,
注意ISFR前后均有一段不使用的空间。
数字滤波寄存器DFER、DFCR、DFWR仅PORTD有效。
每个PCR长32bits,第8到第11位为复用控制,0000为管脚禁用,0001为ALT1(GPIO),其后0010为ALT2以此类推只ALT15(基本上也没有一个管脚能有15个功能,具体参考管脚复用表)
PORT_PCR_MUX完成了左移八位的功能(PORT_PCR_MUX_SHIFT)以达到第八位复用控制寄存器。
龙邱GPIO库中:
void GPIO_Init (GPIO_Type* port, int index, GPIO_CFG dir,int data)
PORT_MemMapPtr p;
switch((u32)port)
{
case 0x400FF000u:
p = PORTA;
break;
case 0x400FF040u:
p = PORTB;
break;
case 0x400FF080u:
p = PORTC;
break;
case 0x400FF0C0u:
p = PORTD;
break;
case 0x400FF100u:
p = PORTE;
break;
default:
break;
}
PORT_PCR_REG(p,index)=PORT_PCR_MUX(1);
<MKV58F24.h>
#define PORT_PCR_REG(base,index) ((base)->PCR[index])
利用了宏完成了PORT复用操作,PORT_PCR_MUX(1)直接调用官方库,指定了GPIO(ALT1)复用方式
关于外部中断标志
DMA请求会自动清除标志,此外需手动清除。
清除方式是在ISFR中对应位“写入”逻辑1,而不是将其清0。