Merge pull request #2285 from balanceTWK/rtt_master

[BSP] Fixed imxrt1050 gpio driver | 修复 imxrt1050 gpio 驱动
This commit is contained in:
Bernard Xiong 2019-01-28 05:54:13 +08:00 committed by GitHub
commit 8bcc2d1265
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 290 additions and 286 deletions

View File

@ -17,7 +17,7 @@
#ifdef RT_USING_PIN #ifdef RT_USING_PIN
#if defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL #if defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL
#error "Please don't define 'FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL'!" #error "Please don't define 'FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL'!"
#endif #endif
struct rt1052_pin struct rt1052_pin
@ -218,13 +218,13 @@ static struct rt1052_irq rt1052_irq_map[] =
{PIN_IRQ_DISABLE, {PIN_IRQ_PIN_NONE, PIN_IRQ_MODE_RISING, RT_NULL, RT_NULL} } {PIN_IRQ_DISABLE, {PIN_IRQ_PIN_NONE, PIN_IRQ_MODE_RISING, RT_NULL, RT_NULL} }
}; };
void gpio_isr(GPIO_Type* base, rt_uint32_t gpio_pin) void gpio_isr(GPIO_Type *base, rt_uint32_t gpio_pin)
{ {
if((GPIO_PortGetInterruptFlags(base) & (1 << gpio_pin)) != 0) if ((GPIO_PortGetInterruptFlags(base) & (1 << gpio_pin)) != 0)
{ {
GPIO_PortClearInterruptFlags(base, gpio_pin); GPIO_PortClearInterruptFlags(base, (1 << gpio_pin));
if(rt1052_irq_map[gpio_pin].irq_info.hdr != RT_NULL) if (rt1052_irq_map[gpio_pin].irq_info.hdr != RT_NULL)
{ {
rt1052_irq_map[gpio_pin].irq_info.hdr(rt1052_irq_map[gpio_pin].irq_info.args); rt1052_irq_map[gpio_pin].irq_info.hdr(rt1052_irq_map[gpio_pin].irq_info.args);
} }
@ -237,7 +237,7 @@ void GPIO1_Combined_0_15_IRQHandler(void)
rt_interrupt_enter(); rt_interrupt_enter();
for(gpio_pin = 0; gpio_pin <= 15; gpio_pin++) for (gpio_pin = 0; gpio_pin <= 15; gpio_pin++)
{ {
gpio_isr(GPIO1, gpio_pin); gpio_isr(GPIO1, gpio_pin);
} }
@ -251,7 +251,7 @@ void GPIO1_Combined_16_31_IRQHandler(void)
rt_interrupt_enter(); rt_interrupt_enter();
for(gpio_pin = 16; gpio_pin <= 31; gpio_pin++) for (gpio_pin = 16; gpio_pin <= 31; gpio_pin++)
{ {
gpio_isr(GPIO1, gpio_pin); gpio_isr(GPIO1, gpio_pin);
} }
@ -265,7 +265,7 @@ void GPIO2_Combined_0_15_IRQHandler(void)
rt_interrupt_enter(); rt_interrupt_enter();
for(gpio_pin = 0; gpio_pin <= 15; gpio_pin++) for (gpio_pin = 0; gpio_pin <= 15; gpio_pin++)
{ {
gpio_isr(GPIO2, gpio_pin); gpio_isr(GPIO2, gpio_pin);
} }
@ -279,7 +279,7 @@ void GPIO2_Combined_16_31_IRQHandler(void)
rt_interrupt_enter(); rt_interrupt_enter();
for(gpio_pin = 16; gpio_pin <= 31; gpio_pin++) for (gpio_pin = 16; gpio_pin <= 31; gpio_pin++)
{ {
gpio_isr(GPIO2, gpio_pin); gpio_isr(GPIO2, gpio_pin);
} }
@ -293,7 +293,7 @@ void GPIO3_Combined_0_15_IRQHandler(void)
rt_interrupt_enter(); rt_interrupt_enter();
for(gpio_pin = 0; gpio_pin <= 15; gpio_pin++) for (gpio_pin = 0; gpio_pin <= 15; gpio_pin++)
{ {
gpio_isr(GPIO3, gpio_pin); gpio_isr(GPIO3, gpio_pin);
} }
@ -307,7 +307,7 @@ void GPIO3_Combined_16_31_IRQHandler(void)
rt_interrupt_enter(); rt_interrupt_enter();
for(gpio_pin = 16; gpio_pin <= 31; gpio_pin++) for (gpio_pin = 16; gpio_pin <= 31; gpio_pin++)
{ {
gpio_isr(GPIO3, gpio_pin); gpio_isr(GPIO3, gpio_pin);
} }
@ -321,7 +321,7 @@ void GPIO4_Combined_0_15_IRQHandler(void)
rt_interrupt_enter(); rt_interrupt_enter();
for(gpio_pin = 0; gpio_pin <= 15; gpio_pin++) for (gpio_pin = 0; gpio_pin <= 15; gpio_pin++)
{ {
gpio_isr(GPIO4, gpio_pin); gpio_isr(GPIO4, gpio_pin);
} }
@ -334,7 +334,7 @@ void GPIO4_Combined_16_31_IRQHandler(void)
rt_interrupt_enter(); rt_interrupt_enter();
for(gpio_pin = 16; gpio_pin <= 31; gpio_pin++) for (gpio_pin = 16; gpio_pin <= 31; gpio_pin++)
{ {
gpio_isr(GPIO4, gpio_pin); gpio_isr(GPIO4, gpio_pin);
} }
@ -348,7 +348,7 @@ void GPIO5_Combined_0_15_IRQHandler(void)
rt_interrupt_enter(); rt_interrupt_enter();
for(gpio_pin = 0; gpio_pin <= 2; gpio_pin++) for (gpio_pin = 0; gpio_pin <= 2; gpio_pin++)
{ {
gpio_isr(GPIO5, gpio_pin); gpio_isr(GPIO5, gpio_pin);
} }
@ -360,9 +360,9 @@ static IRQn_Type rt1052_get_irqnum(GPIO_Type *gpio, rt_uint32_t gpio_pin)
{ {
IRQn_Type irq_num = NotAvail_IRQn; /* Invalid interrupt number */ IRQn_Type irq_num = NotAvail_IRQn; /* Invalid interrupt number */
if(gpio == GPIO1) if (gpio == GPIO1)
{ {
if(gpio_pin <= 15) if (gpio_pin <= 15)
{ {
irq_num = GPIO1_Combined_0_15_IRQn; irq_num = GPIO1_Combined_0_15_IRQn;
} }
@ -371,9 +371,9 @@ static IRQn_Type rt1052_get_irqnum(GPIO_Type *gpio, rt_uint32_t gpio_pin)
irq_num = GPIO1_Combined_16_31_IRQn; irq_num = GPIO1_Combined_16_31_IRQn;
} }
} }
else if(gpio == GPIO2) else if (gpio == GPIO2)
{ {
if(gpio_pin <= 15) if (gpio_pin <= 15)
{ {
irq_num = GPIO2_Combined_0_15_IRQn; irq_num = GPIO2_Combined_0_15_IRQn;
} }
@ -382,9 +382,9 @@ static IRQn_Type rt1052_get_irqnum(GPIO_Type *gpio, rt_uint32_t gpio_pin)
irq_num = GPIO2_Combined_16_31_IRQn; irq_num = GPIO2_Combined_16_31_IRQn;
} }
} }
else if(gpio == GPIO3) else if (gpio == GPIO3)
{ {
if(gpio_pin <= 15) if (gpio_pin <= 15)
{ {
irq_num = GPIO3_Combined_0_15_IRQn; irq_num = GPIO3_Combined_0_15_IRQn;
} }
@ -393,9 +393,9 @@ static IRQn_Type rt1052_get_irqnum(GPIO_Type *gpio, rt_uint32_t gpio_pin)
irq_num = GPIO3_Combined_16_31_IRQn; irq_num = GPIO3_Combined_16_31_IRQn;
} }
} }
else if(gpio == GPIO4) else if (gpio == GPIO4)
{ {
if(gpio_pin <= 15) if (gpio_pin <= 15)
{ {
irq_num = GPIO4_Combined_0_15_IRQn; irq_num = GPIO4_Combined_0_15_IRQn;
} }
@ -404,9 +404,9 @@ static IRQn_Type rt1052_get_irqnum(GPIO_Type *gpio, rt_uint32_t gpio_pin)
irq_num = GPIO4_Combined_16_31_IRQn; irq_num = GPIO4_Combined_16_31_IRQn;
} }
} }
else if(gpio == GPIO5) else if (gpio == GPIO5)
{ {
if(gpio_pin <= 15) if (gpio_pin <= 15)
{ {
irq_num = GPIO5_Combined_0_15_IRQn; irq_num = GPIO5_Combined_0_15_IRQn;
} }
@ -424,70 +424,70 @@ static void rt1052_pin_mode(rt_device_t dev, rt_base_t pin, rt_base_t mode)
gpio_pin_config_t gpio; gpio_pin_config_t gpio;
rt_uint32_t config_value = 0; rt_uint32_t config_value = 0;
if((pin > __ARRAY_LEN(rt1052_pin_map)) || (pin == 0)) if ((pin > __ARRAY_LEN(rt1052_pin_map)) || (pin == 0))
{ {
return; return;
} }
if(rt1052_pin_map[pin].gpio != GPIO5) if (rt1052_pin_map[pin].gpio != GPIO5)
{ {
CLOCK_EnableClock(kCLOCK_Iomuxc); CLOCK_EnableClock(kCLOCK_Iomuxc);
IOMUXC_SetPinMux(0x401F8010U + pin*4, 0x5U, 0, 0, 0, 1); IOMUXC_SetPinMux(0x401F8010U + pin * 4, 0x5U, 0, 0, 0, 1);
} }
else else
{ {
CLOCK_EnableClock(kCLOCK_IomuxcSnvs); CLOCK_EnableClock(kCLOCK_IomuxcSnvs);
IOMUXC_SetPinMux(0x400A8000U + (pin-125)*4, 0x5U, 0, 0, 0, 1); IOMUXC_SetPinMux(0x400A8000U + (pin - 125) * 4, 0x5U, 0, 0, 0, 1);
} }
gpio.outputLogic = 0; gpio.outputLogic = 0;
gpio.interruptMode = kGPIO_NoIntmode; gpio.interruptMode = kGPIO_NoIntmode;
switch(mode) switch (mode)
{ {
case PIN_MODE_OUTPUT: case PIN_MODE_OUTPUT:
{ {
config_value = 0x1030U; config_value = 0x0030U;/* Drive Strength R0/6 */
gpio.direction = kGPIO_DigitalOutput; gpio.direction = kGPIO_DigitalOutput;
} }
break; break;
case PIN_MODE_INPUT: case PIN_MODE_INPUT:
{ {
config_value = 0x1030U; config_value = 0x0830U;/* Open Drain Enable */
gpio.direction = kGPIO_DigitalInput; gpio.direction = kGPIO_DigitalInput;
} }
break; break;
case PIN_MODE_INPUT_PULLDOWN: case PIN_MODE_INPUT_PULLDOWN:
{ {
config_value = 0x1030U; config_value = 0x3030U;/* 100K Ohm Pull Down */
gpio.direction = kGPIO_DigitalInput; gpio.direction = kGPIO_DigitalInput;
} }
break; break;
case PIN_MODE_INPUT_PULLUP: case PIN_MODE_INPUT_PULLUP:
{ {
config_value = 0x5030U; config_value = 0xB030U;/* 100K Ohm Pull Up */
gpio.direction = kGPIO_DigitalInput; gpio.direction = kGPIO_DigitalInput;
} }
break; break;
case PIN_MODE_OUTPUT_OD: case PIN_MODE_OUTPUT_OD:
{ {
config_value = 0x1830U; config_value = 0x0830U;/* Open Drain Enable */
gpio.direction = kGPIO_DigitalOutput; gpio.direction = kGPIO_DigitalOutput;
} }
break; break;
} }
if(rt1052_pin_map[pin].gpio != GPIO5) if (rt1052_pin_map[pin].gpio != GPIO5)
{ {
IOMUXC_SetPinConfig(0, 0, 0, 0, 0x401F8200U + pin*4, config_value); IOMUXC_SetPinConfig(0, 0, 0, 0, 0x401F8200U + pin * 4, config_value);
} }
else else
{ {
IOMUXC_SetPinConfig(0, 0, 0, 0, 0x400A8018U + (pin-125)*4, config_value); IOMUXC_SetPinConfig(0, 0, 0, 0, 0x400A8018U + (pin - 125) * 4, config_value);
} }
GPIO_PinInit(rt1052_pin_map[pin].gpio, rt1052_pin_map[pin].gpio_pin, &gpio); GPIO_PinInit(rt1052_pin_map[pin].gpio, rt1052_pin_map[pin].gpio_pin, &gpio);
@ -506,18 +506,18 @@ static void rt1052_pin_write(rt_device_t dev, rt_base_t pin, rt_base_t value)
static rt_err_t rt1052_pin_attach_irq(struct rt_device *device, rt_int32_t pin, static rt_err_t rt1052_pin_attach_irq(struct rt_device *device, rt_int32_t pin,
rt_uint32_t mode, void (*hdr)(void *args), void *args) rt_uint32_t mode, void (*hdr)(void *args), void *args)
{ {
struct rt1052_pin* pin_map = RT_NULL; struct rt1052_pin *pin_map = RT_NULL;
struct rt1052_irq* irq_map = RT_NULL; struct rt1052_irq *irq_map = RT_NULL;
pin_map = &rt1052_pin_map[pin]; pin_map = &rt1052_pin_map[pin];
irq_map = &rt1052_irq_map[rt1052_pin_map[pin].gpio_pin]; irq_map = &rt1052_irq_map[rt1052_pin_map[pin].gpio_pin];
if(pin_map == RT_NULL || irq_map == RT_NULL) if (pin_map == RT_NULL || irq_map == RT_NULL)
{ {
return RT_ENOSYS; return RT_ENOSYS;
} }
if(irq_map->enable == PIN_IRQ_ENABLE) if (irq_map->enable == PIN_IRQ_ENABLE)
{ {
return RT_EBUSY; return RT_EBUSY;
} }
@ -532,18 +532,18 @@ static rt_err_t rt1052_pin_attach_irq(struct rt_device *device, rt_int32_t pin,
static rt_err_t rt1052_pin_detach_irq(struct rt_device *device, rt_int32_t pin) static rt_err_t rt1052_pin_detach_irq(struct rt_device *device, rt_int32_t pin)
{ {
struct rt1052_pin* pin_map = RT_NULL; struct rt1052_pin *pin_map = RT_NULL;
struct rt1052_irq* irq_map = RT_NULL; struct rt1052_irq *irq_map = RT_NULL;
pin_map = &rt1052_pin_map[pin]; pin_map = &rt1052_pin_map[pin];
irq_map = &rt1052_irq_map[rt1052_pin_map[pin].gpio_pin]; irq_map = &rt1052_irq_map[rt1052_pin_map[pin].gpio_pin];
if(pin_map == RT_NULL || irq_map == RT_NULL) if (pin_map == RT_NULL || irq_map == RT_NULL)
{ {
return RT_ENOSYS; return RT_ENOSYS;
} }
if(irq_map->enable == PIN_IRQ_DISABLE) if (irq_map->enable == PIN_IRQ_DISABLE)
{ {
return RT_EOK; return RT_EOK;
} }
@ -560,87 +560,91 @@ static rt_err_t rt1052_pin_irq_enable(struct rt_device *device, rt_base_t pin, r
{ {
gpio_pin_config_t gpio; gpio_pin_config_t gpio;
IRQn_Type irq_num; IRQn_Type irq_num;
rt_uint32_t config_value = 0x1b0a0; rt_uint32_t config_value = 0;
struct rt1052_pin *pin_map = RT_NULL;
struct rt1052_pin* pin_map = RT_NULL; struct rt1052_irq *irq_map = RT_NULL;
struct rt1052_irq* irq_map = RT_NULL;
pin_map = &rt1052_pin_map[pin]; pin_map = &rt1052_pin_map[pin];
irq_map = &rt1052_irq_map[rt1052_pin_map[pin].gpio_pin]; irq_map = &rt1052_irq_map[rt1052_pin_map[pin].gpio_pin];
if(pin_map == RT_NULL || irq_map == RT_NULL) if (pin_map == RT_NULL || irq_map == RT_NULL)
{ {
return RT_ENOSYS; return RT_ENOSYS;
} }
if(enabled == PIN_IRQ_ENABLE) if (enabled == PIN_IRQ_ENABLE)
{ {
if(irq_map->enable == PIN_IRQ_ENABLE) if (irq_map->enable == PIN_IRQ_ENABLE)
{ {
return RT_EBUSY; return RT_EBUSY;
} }
if(irq_map->irq_info.pin != pin) if (irq_map->irq_info.pin != pin)
{ {
return RT_EIO; return RT_EIO;
} }
irq_map->enable = PIN_IRQ_ENABLE; irq_map->enable = PIN_IRQ_ENABLE;
if(rt1052_pin_map[pin].gpio != GPIO5) if (rt1052_pin_map[pin].gpio != GPIO5)
{ {
CLOCK_EnableClock(kCLOCK_Iomuxc); CLOCK_EnableClock(kCLOCK_Iomuxc);
IOMUXC_SetPinMux(0x401F8010U + pin*4, 0x5U, 0, 0, 0, 0); IOMUXC_SetPinMux(0x401F8010U + pin * 4, 0x5U, 0, 0, 0, 1);
} }
else else
{ {
CLOCK_EnableClock(kCLOCK_IomuxcSnvs); CLOCK_EnableClock(kCLOCK_IomuxcSnvs);
IOMUXC_SetPinMux(0x400A8000U + (pin-125)*4, 0x5U, 0, 0, 0, 0); IOMUXC_SetPinMux(0x400A8000U + (pin - 125) * 4, 0x5U, 0, 0, 0, 1);
} }
gpio.direction = kGPIO_DigitalInput; gpio.direction = kGPIO_DigitalInput;
gpio.outputLogic = 0; gpio.outputLogic = 0;
switch(irq_map->irq_info.mode) switch (irq_map->irq_info.mode)
{ {
case PIN_IRQ_MODE_RISING: case PIN_IRQ_MODE_RISING:
{ {
config_value = 0x3030U;/* 100K Ohm Pull Down */
gpio.interruptMode = kGPIO_IntRisingEdge; gpio.interruptMode = kGPIO_IntRisingEdge;
} }
break; break;
case PIN_IRQ_MODE_FALLING: case PIN_IRQ_MODE_FALLING:
{ {
config_value = 0xB030U;/* 100K Ohm Pull Up */
gpio.interruptMode = kGPIO_IntFallingEdge; gpio.interruptMode = kGPIO_IntFallingEdge;
} }
break; break;
case PIN_IRQ_MODE_RISING_FALLING: case PIN_IRQ_MODE_RISING_FALLING:
{ {
config_value = 0x0830U;/* Open Drain Enable */
gpio.interruptMode = kGPIO_IntRisingOrFallingEdge; gpio.interruptMode = kGPIO_IntRisingOrFallingEdge;
} }
break; break;
case PIN_IRQ_MODE_HIGH_LEVEL: case PIN_IRQ_MODE_HIGH_LEVEL:
{ {
config_value = 0x3030U;/* 100K Ohm Pull Down */
gpio.interruptMode = kGPIO_IntHighLevel; gpio.interruptMode = kGPIO_IntHighLevel;
} }
break; break;
case PIN_IRQ_MODE_LOW_LEVEL: case PIN_IRQ_MODE_LOW_LEVEL:
{ {
config_value = 0xB030U;/* 100K Ohm Pull Up */
gpio.interruptMode = kGPIO_IntLowLevel; gpio.interruptMode = kGPIO_IntLowLevel;
} }
break; break;
} }
if(rt1052_pin_map[pin].gpio != GPIO5) if (rt1052_pin_map[pin].gpio != GPIO5)
{ {
IOMUXC_SetPinConfig(0, 0, 0, 0, 0x401F8200U + pin*4, config_value); IOMUXC_SetPinConfig(0, 0, 0, 0, 0x401F8200U + pin * 4, config_value);
} }
else else
{ {
IOMUXC_SetPinConfig(0, 0, 0, 0, 0x400A8018U + (pin-125)*4, config_value); IOMUXC_SetPinConfig(0, 0, 0, 0, 0x400A8018U + (pin - 125) * 4, config_value);
} }
irq_num = rt1052_get_irqnum(rt1052_pin_map[pin].gpio, rt1052_pin_map[pin].gpio_pin); irq_num = rt1052_get_irqnum(rt1052_pin_map[pin].gpio, rt1052_pin_map[pin].gpio_pin);
@ -651,9 +655,9 @@ static rt_err_t rt1052_pin_irq_enable(struct rt_device *device, rt_base_t pin, r
GPIO_PinInit(rt1052_pin_map[pin].gpio, rt1052_pin_map[pin].gpio_pin, &gpio); GPIO_PinInit(rt1052_pin_map[pin].gpio, rt1052_pin_map[pin].gpio_pin, &gpio);
GPIO_PortEnableInterrupts(rt1052_pin_map[pin].gpio, 1U << rt1052_pin_map[pin].gpio_pin); GPIO_PortEnableInterrupts(rt1052_pin_map[pin].gpio, 1U << rt1052_pin_map[pin].gpio_pin);
} }
else if(enabled == PIN_IRQ_DISABLE) else if (enabled == PIN_IRQ_DISABLE)
{ {
if(irq_map->enable == PIN_IRQ_DISABLE) if (irq_map->enable == PIN_IRQ_DISABLE)
{ {
return RT_EOK; return RT_EOK;
} }