lowlevel_init.S学习理解

lowlevel_init.S学习理解
 
 
  在我迷迷糊学看了lowlevel_init.S中的代码之后,有一个感觉就是,要想读懂其中的代码首先得看一下你所用的芯片手册,因为里面有比较详细的存储器控制(MemoryController)描述。我用的是s3c2440手册里写得十分清楚,总共有8个块(Banks)(Bank0~Bank7),每个块对应着不同的控制与地址线,其实这些Bank很大一部分是对应着的控制寄存器的地址,这个要看实际的芯片与板子,看了下面的理解估计会有个大概。
   第二,就是要清楚你的板子上对的存储地址的分配,一般板子手册上都会有的。我的开发板是友善之臂的mini2440(不是很富裕只能买一些平民板子 ),在它的手册里写有SDRAM存储系统与FLASH系统的介绍包括芯片型号,存储大小,这些都是后面移植要注意,毕竟U-boot的移植就是对硬件支持的修改。mini2440的SDRAM使用两片32M bytes总共64M bytes型号为HY57V561620FTP芯片,地址空间为0x30000000~0x34000000
   进入正题:
 
/*
 * Memory Setup stuff - taken from blob memsetup.S
 *
 * Copyright (C) 1999 2000 2001 Erik Mouw ( J.A.K.Mouw@its.tudelft.nl) and
 *                     Jan-Derk Bakker ( J.D.Bakker@its.tudelft.nl)
 *
 * Modified for the Samsung SMDK2410 by
 * (C) Copyright 2002
 * David Mueller, ELSOFT AG, < d.mueller@elsoft.ch>
 *
 * See file CREDITS for list of people who contributed to this
 * project.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 */

#include <config.h>
#include <version.h>

/* some parameters for the board */
/*
 *
 * Taken from linux/arch/arm/boot/compressed/head-s3c2410.S
 *
 * Copyright (C) 2002 Samsung Electronics SW.LEE  < hitchcar@sec.samsung.com>
 *
 */
#define BWSCON 0x48000000  /*总线宽度&等待控制寄存器的宏定义(由s3c2440芯片手册5.4可知BW
                            SCON的地址正好为0x48000000可R/W复位值为0*/
 
 
/* BWSCON */
#define DW8    (0x0)
#define DW16    (0x1)
#define DW32    (0x2)     /*上面三个宏是数据总线宽度,分别是8位、16位、32位在s3c2440芯片手
                          册5.4中可以看到一个对BWSCON中每一位的详解其中DWn就是对每一个Bank
                          要使用的数宽度进行定义*/

#define WAIT    (0x1<<2) /*位WSn的宏定义,具体的作用在手册中有说明*/
#define UBLB    (0x1<<3) /*位STn的宏定义*/
 
#define B1_BWSCON    (DW32)
#define B2_BWSCON    (DW16)
#define B3_BWSCON    (DW16 + WAIT + UBLB)
#define B4_BWSCON    (DW16)
#define B5_BWSCON    (DW16)
#define B6_BWSCON    (DW32)
#define B7_BWSCON    (DW32) /*以上是对BANKn的数据总线宽度与功能的宏定义,对于BANK3因为要接
                             网卡,网卡有wait信号与没有wait信号两种,所以这里有些特别*/
 
 
//以下是对每一个Bank的BANKCON进行配置,这里要根据SRAM手册来配置来对应的值,否则不能正常进行使用,在0~5中大多是对一些时序与Page模式的配置,只对应SRAM的要求就可以了*/
/* BANK0CON */
#define B0_Tacs    0x0 /*  0clk */
#define B0_Tcos    0x0 /*  0clk */
#define B0_Tacc    0x7 /* 14clk */
#define B0_Tcoh    0x0 /*  0clk */
#define B0_Tah    0x0 /*  0clk */
#define B0_Tacp    0x0
#define B0_PMC    0x0 /* normal */
 
/* BANK1CON */
#define B1_Tacs    0x0 /*  0clk */
#define B1_Tcos    0x0 /*  0clk */
#define B1_Tacc    0x7 /* 14clk */
#define B1_Tcoh    0x0 /*  0clk */
#define B1_Tah    0x0 /*  0clk */
#define B1_Tacp    0x0
#define B1_PMC    0x0
 
#define B2_Tacs    0x0
#define B2_Tcos    0x0
#define B2_Tacc    0x7
#define B2_Tcoh    0x0
#define B2_Tah    0x0
#define B2_Tacp    0x0
#define B2_PMC    0x0
 
#define B3_Tacs    0x0 /*  0clk */
#define B3_Tcos    0x3 /*  4clk */
#define B3_Tacc    0x7 /* 14clk */
#define B3_Tcoh    0x1 /*  1clk */
#define B3_Tah    0x0 /*  0clk */
#define B3_Tacp    0x3     /*  6clk */
#define B3_PMC    0x0 /* normal */
 
#define B4_Tacs    0x0 /*  0clk */
#define B4_Tcos    0x0 /*  0clk */
#define B4_Tacc    0x7 /* 14clk */
#define B4_Tcoh    0x0 /*  0clk */
#define B4_Tah    0x0 /*  0clk */
#define B4_Tacp    0x0
#define B4_PMC    0x0 /* normal */
 
#define B5_Tacs    0x0 /*  0clk */
#define B5_Tcos    0x0 /*  0clk */
#define B5_Tacc    0x7 /* 14clk */
#define B5_Tcoh    0x0 /*  0clk */
#define B5_Tah    0x0 /*  0clk */
#define B5_Tacp    0x0
#define B5_PMC    0x0 /* normal */
 
//在B6之后是SDBAM的配置,根据Datasheet里说的后两个Bank是可以作为ROM、SRAM、SDRAM等储存器
//因为这里配置为SDRAM所以其他只对于ROM、SRAM有效的位就不用配置了,其实这些位和上面的Bank是
//一样的。
//Bank的所就相应的选择配置位了MT
#define B6_MT    0x3 /* SDRAM */
#define B6_Trcd     0x1             //RAStoCAS的延时时钟周期
#define B6_SCAN    0x1 /* 9bit */   //列地址位数的宏定义,列扫描数,在SDRAM手册上定有
 
#define B7_MT    0x3 /* SDRAM */
#define B7_Trcd    0x1 /* 3clk */
#define B7_SCAN    0x1 /* 9bit */
 
 
//以下为刷新控制寄存器的配置,得根据SDRAM的具体参数来配置,移植时要特别的注意
/* REFRESH parameter */
#define REFEN    0x1 /* Refresh enable */ //使能刷新
#define TREFMD    0x0 /* CBR(CAS before RAS)/Auto refresh */ //刷新模式在此为自刷新
#define Trp    0x0 /* 2clk */  //RAS预充电时间
#define Trc    0x3 /* 7clk */  //SDRAM半行周期时间,行周期Trc=Tsrc+Trp
#define Tchr    0x2 /* 3clk */ //本人在s3c2440的Datasheet中没有找到这个位
#define REFCNT    1113 /* period=15.6us, HCLK=60Mhz, (2048+1-15.6*60) */
//SDRAM刷新计数值,即刷新频率
 
//以下是重点了,也即是重要的lowlevel_init函数了
/**************************************/
_TEXT_BASE:
 .word TEXT_BASE   //这里是获得代码段的起始地址,我的是0x33F80000(在board/xxx/config.mk中
                  //可到找到“TEXT_BASE=0x33F80000”
 
.globl lowlevel_init  //这里相当于定义一个全局的 lowlevel_init以方便调用
lowlevel_init:
 /* memory control configuration */
 /* make r0 relative the current location so that it */
 /* reads SMRDATA out of FLASH rather than memory ! */
 ldr     r0, =SMRDATA  //SMDATA表示这13个寄存器的值存放的开始地址,值为0x33F8xxxx,处于内
                       //存中,这一句的作用是把其值加载到r0中
 
 ldr r1, _TEXT_BASE    //把代码的起始地址(0x33F80000)加载到r1中
 sub r0, r0, r1        //r0减去r1其结果存入r0,也即SMDATA中的起始地址0x33F8xxxx减去
                       //0x33F80000,其结果就是13个寄存器的值在NOR Flash存放的开始地址

 ldr r1, =BWSCON /* Bus Width Status Controller */ //存储控制器的基地址
 add     r2, r0, #13*4      //在计算出来的存放地址加上#13*4,然后其结果保存在r2中
                           //13个寄存器,每个寄存器占4个字节
0:
 ldr     r3, [r0], #4       //内存中r0的值加载到r3中,然后r0加4,即下一个寄存器的
 str     r3, [r1], #4       //读出寄存器的值保存到r1中,然后r1也偏移4
 cmp     r2, r0             //比较r0与r2的值,如果不等继续返回0:执行,也即13个寄存器的值
                           //是否读完
 bne     0b
 /* everything is fine now */
 mov pc, lr                //程序跳转,返回到cpu_init_crit中
 .ltorg
/* the literal pools origin */
SMRDATA:
    .word (0+(B1_BWSCON<<4)+(B2_BWSCON<<8)+(B3_BWSCON<<12)+(B4_BWSCON<<16)+(B5_BWSCON<<20)+(B6_BWSCON<<24)+(B7_BWSCON<<28))
                                               //设置每个BWCSCON,BANK0由硬件连线定
    .word ((B0_Tacs<<13)+(B0_Tcos<<11)+(B0_Tacc<<8)+(B0_Tcoh<<6)+(B0_Tah<<4)+(B0_Tacp<<2)+(B0_PMC))
    .word ((B1_Tacs<<13)+(B1_Tcos<<11)+(B1_Tacc<<8)+(B1_Tcoh<<6)+(B1_Tah<<4)+(B1_Tacp<<2)+(B1_PMC))
    .word ((B2_Tacs<<13)+(B2_Tcos<<11)+(B2_Tacc<<8)+(B2_Tcoh<<6)+(B2_Tah<<4)+(B2_Tacp<<2)+(B2_PMC))
    .word ((B3_Tacs<<13)+(B3_Tcos<<11)+(B3_Tacc<<8)+(B3_Tcoh<<6)+(B3_Tah<<4)+(B3_Tacp<<2)+(B3_PMC))
    .word ((B4_Tacs<<13)+(B4_Tcos<<11)+(B4_Tacc<<8)+(B4_Tcoh<<6)+(B4_Tah<<4)+(B4_Tacp<<2)+(B4_PMC))
    .word ((B5_Tacs<<13)+(B5_Tcos<<11)+(B5_Tacc<<8)+(B5_Tcoh<<6)+(B5_Tah<<4)+(B5_Tacp<<2)+(B5_PMC))
                                   //以上是对BANKCON0~BANKCON5的设置
    .word ((B6_MT<<15)+(B6_Trcd<<2)+(B6_SCAN))
    .word ((B7_MT<<15)+(B7_Trcd<<2)+(B7_SCAN))  //对BANKCON6、BANKCON7的设置

    .word ((REFEN<<23)+(TREFMD<<22)+(Trp<<20)+(Trc<<18)+(Tchr<<16)+REFCNT)
    .word 0x32     //BANKSIZE寄存器的设置
    .word 0x30      //对MRSRB6设置
    .word 0x30     //对MRSRB7设置

通过以上对代码的分析可以知道,这里主要是对储存寄存器的设置,特别是SMRDATA后面的。这里代码的作用是为加载Bootloader的第二阶段代码准备RAM空间。所谓准备,即是初始化内存芯片,使其可用。这个函数被cpu/xxx/start.S调用。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#include "main.h" #include "stdio.h" #include "string.h" #include "time.h" UART_HandleTypeDef huart1; void SystemClock_Config(void); static void MX_GPIO_Init(void); static void MX_USART1_UART_Init(void); int main(void) { HAL_Init(); SystemClock_Config(); MX_GPIO_Init(); MX_USART1_UART_Init(); while (1) { time_t now = time(NULL); struct tm *timeinfo = localtime(&now); char time_str[9]; sprintf(time_str, "%02d:%02d:%02d", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); HAL_UART_Transmit(&huart1, (uint8_t *)time_str, strlen(time_str), HAL_MAX_DELAY); HAL_Delay(1000); } } void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; /** Initializes the RCC Oscillators according to the specified parameters * in the RCC_OscInitTypeDef structure. */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; RCC_OscInitStruct.HSEState = RCC_HSE_ON; RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { Error_Handler(); } /** Initializes the CPU, AHB and APB buses clocks */ RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2; RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) { Error_Handler(); } } static void MX_USART1_UART_Init(void) { huart1.Instance = USART1; huart1.Init.BaudRate = 115200; huart1.Init.WordLength = UART_WORDLENGTH_8B; huart1.Init.StopBits = UART_STOPBITS_1; huart1.Init.Parity = UART_PARITY_NONE; huart1.Init.Mode = UART_MODE_TX_RX; huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE; huart1.Init.OverSampling = UART_OVERSAMPLING_16; if (HAL_UART_Init(&huart1) != HAL_OK) { Error_Handler(); } } static void MX_GPIO_Init(void) { GPIO_InitTypeDef GPIO_InitStruct = {0}; /* GPIO Ports Clock Enable */ __HAL_RCC_GPIOA_CLK_ENABLE(); /*Configure GPIO pin Output Level */ HAL_GPIO_WritePin(GPIOA, GPIO_PIN_9, GPIO_PIN_RESET); /*Configure GPIO pin : PA9 */ GPIO_InitStruct.Pin = GPIO_PIN_9; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); } 在以上代码的基础上,编写代码以实现计算发送 hh:mm:ss到单片机,修改单片机时间
最新发布
05-30

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值