U-BOOT移植
U-boot是一个大的单循环程序。为linux运行做硬件初始化。与STM32产品相同,每个项目用到的硬件是不同的。所以嵌入式LINUX必须学会U-boot 适配不同的硬件。
用STM32 理解U-BOOT移植
u-boot源码中有支持STM32的部分
根目录drivers\gpio 里有一个stm32_gpio.c 来看下这个都写了啥,下面简要拿出来几段
static const unsigned long io_base[] = {
STM32_GPIOA_BASE, STM32_GPIOB_BASE, STM32_GPIOC_BASE,
STM32_GPIOD_BASE, STM32_GPIOE_BASE, STM32_GPIOF_BASE,
STM32_GPIOG_BASE
};
struct stm32_gpio_regs {
u32 crl; /* GPIO port configuration low */
u32 crh; /* GPIO port configuration high */
u32 idr; /* GPIO port input data */
u32 odr; /* GPIO port output data */
u32 bsrr; /* GPIO port bit set/reset */
u32 brr; /* GPIO port bit reset */
u32 lckr; /* GPIO port configuration lock */
};
int stm32_gpin_get(const struct stm32_gpio_dsc *dsc)
{
struct stm32_gpio_regs *gpio_regs;
int rv;
if (CHECK_DSC(dsc)) {
rv = -EINVAL;
goto out;
}
gpio_regs = (struct stm32_gpio_regs *)io_base[dsc->port];
rv = readl(&gpio_regs->idr) & (1 << dsc->pin);
out:
return rv;
}
就这个代码是不是很熟悉。u-boot包装了寄存器级别的操作。相当于完成了STM32标准库的功能。
也就是说恩智浦官方没有STM32类似的标准库,但会维护U-boot也相当于一个官方标准库了。
U-boot 如何让GPIO工作
之前说过 u-boot 的 配置文件 和 CMD文件
drivers\gpio 的 .built-in.o.cmd文件内如如下:
cmd_drivers/gpio/built-in.o := arm-linux-gnueabihf-ld.bfd -r -o drivers/gpio/built-in.o drivers/gpio/mxc_gpio.o
这里看到用imx6GPIO对应源码是mxc_gpio.c 截取有用部分如下:
/* Gpio simple map */
struct gpio_regs {
u32 gpiodata;
u32 gpiodir;
};
#define GPIO_TO_PORT(n) (n / 32)
#define AIPS1_ARB_BASE_ADDR 0x02000000
#define ATZ1_BASE_ADDR AIPS1_ARB_BASE_ADDR
#define AIPS1_OFF_BASE_ADDR (ATZ1_BASE_ADDR + 0x80000)
#define GPIO1_BASE_ADDR (AIPS1_OFF_BASE_ADDR + 0x1C000)
static unsigned long gpio_ports[] = {
[0] = GPIO1_BASE_ADDR,
[1] = GPIO2_BASE_ADDR,
[2] = GPIO3_BASE_ADDR,
}
int gpio_get_value(unsigned gpio)
{
unsigned int port = GPIO_TO_PORT(gpio);
struct gpio_regs *regs;
u32 val;
if (port >= ARRAY_SIZE(gpio_ports))
return -1;
if (RDC_CHECK(port))
return -1;
RDC_SPINLOCK_UP(port);
gpio &= 0x1f;
regs = (struct gpio_regs *)gpio_ports[port];
val = (readl(®s->gpio_psr) >> gpio) & 0x01;
RDC_SPINLOCK_DOWN(port);
return val;
}
还是拿获取IO状态来说,GPIO1_BASE_ADDR =0x0209 C000
查找IMX6U 参考手册如下图:
0x0209 C000 位置是DR寄存器。所以以上就是U-BOOT操作硬件的底层代码