继续:
下面开始配置 GPIO和FSMC void bsp_InitNorFlash(void) { FSMC_NORSRAMInitTypeDef FSMC_NORSRAMInitStructure; FSMC_NORSRAMTimingInitTypeDef p; GPIO_InitTypeDef GPIO_InitStructure; /* 使能GPIO时钟 */ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD | RCC_AHB1Periph_GPIOE | RCC_AHB1Periph_GPIOF | RCC_AHB1Periph_GPIOG, ENABLE); // 找到RCC_AHB1PeriphClockCmd 函数,可以看到这个函数修改的寄存器名字 <--- 寄存器0x40023830 = 0x00100078 /* 使能 FSMC 时钟 */ RCC_AHB3PeriphClockCmd(RCC_AHB3Periph_FSMC, ENABLE); <--- 寄存器0x40023838 = 0x00000001 /* GPIOD configuration */ GPIO_PinAFConfig(GPIOD, GPIO_PinSource0, GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOD, GPIO_PinSource1, GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOD, GPIO_PinSource4, GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOD, GPIO_PinSource5, GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOD, GPIO_PinSource8, GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOD, GPIO_PinSource9, GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOD, GPIO_PinSource10, GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOD, GPIO_PinSource11, GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOD, GPIO_PinSource12, GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOD, GPIO_PinSource13, GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOD, GPIO_PinSource14, GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOD, GPIO_PinSource15, GPIO_AF_FSMC); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOD, &GPIO_InitStructure); /* GPIOE configuration */ GPIO_PinAFConfig(GPIOE, GPIO_PinSource3 , GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOE, GPIO_PinSource4 , GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOE, GPIO_PinSource5 , GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOE, GPIO_PinSource6 , GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOE, GPIO_PinSource7 , GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOE, GPIO_PinSource8 , GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOE, GPIO_PinSource9 , GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOE, GPIO_PinSource10 , GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOE, GPIO_PinSource11 , GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOE, GPIO_PinSource12 , GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOE, GPIO_PinSource13 , GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOE, GPIO_PinSource14 , GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOE, GPIO_PinSource15 , GPIO_AF_FSMC); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11| GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15; GPIO_Init(GPIOE, &GPIO_InitStructure); /* GPIOF configuration */ GPIO_PinAFConfig(GPIOF, GPIO_PinSource0 , GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOF, GPIO_PinSource1 , GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOF, GPIO_PinSource2 , GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOF, GPIO_PinSource3 , GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOF, GPIO_PinSource4 , GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOF, GPIO_PinSource5 , GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOF, GPIO_PinSource12 , GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOF, GPIO_PinSource13 , GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOF, GPIO_PinSource14 , GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOF, GPIO_PinSource15 , GPIO_AF_FSMC); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15; GPIO_Init(GPIOF, &GPIO_InitStructure); /* GPIOG configuration */ GPIO_PinAFConfig(GPIOG, GPIO_PinSource0 , GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOG, GPIO_PinSource1 , GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOG, GPIO_PinSource2 , GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOG, GPIO_PinSource3 , GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOG, GPIO_PinSource4 , GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOG, GPIO_PinSource5 , GPIO_AF_FSMC); GPIO_PinAFConfig(GPIOG, GPIO_PinSource9 , GPIO_AF_FSMC); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_9; GPIO_Init(GPIOG, &GPIO_InitStructure); /* PD6 作为忙信号, 配置为GPIO输入模式,软件查询 */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; /* 输入模式 */ GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOD, &GPIO_InitStructure); #if 0 上面的代码的目的配置 GPIO相关的寄存器 typedef struct { __IO uint32_t MODER; /*!< GPIO port mode register, Address offset: 0x00 */ __IO uint32_t OTYPER; /*!< GPIO port output type register, Address offset: 0x04 */ __IO uint32_t OSPEEDR; /*!< GPIO port output speed register, Address offset: 0x08 */ __IO uint32_t PUPDR; /*!< GPIO port pull-up/pull-down register, Address offset: 0x0C */ __IO uint32_t IDR; /*!< GPIO port input data register, Address offset: 0x10 */ __IO uint32_t ODR; /*!< GPIO port output data register, Address offset: 0x14 */ __IO uint16_t BSRRL; /*!< GPIO port bit set/reset low register, Address offset: 0x18 */ __IO uint16_t BSRRH; /*!< GPIO port bit set/reset high register, Address offset: 0x1A */ __IO uint32_t LCKR; /*!< GPIO port configuration lock register, Address offset: 0x1C */ __IO uint32_t AFR[2]; /*!< GPIO alternate function registers, Address offset: 0x20-0x24 */ } GPIO_TypeDef; 为了查看方便,我们添加一些代码: GPIOD GPIOE GPIOF GPIOG #endif #if 1 /* 添加的测试代码 */ { static uint32_t addr, value; addr = (uint32_t)&GPIOD->MODER; value = *(uint32_t *)addr; <--- 寄存器0x40020C00= 0xAAAA0A0A addr = (uint32_t)&GPIOD->OTYPER; value = *(uint32_t *)addr; <--- 寄存器0x40020C04=0x00000000 addr = (uint32_t)&GPIOD->OSPEEDR; value = *(uint32_t *)addr; <--- 寄存器0x40020C08=0xFFFF0F0F addr = (uint32_t)&GPIOD->UPDR; value = *(uint32_t *)addr; <--- 寄存器0x40020C0C=0x00000000 //addr = (uint32_t)&GPIOD->IDR; value = *(uint32_t *)addr; <--- 寄存器0x40020C10=0x000000FD //addr = (uint32_t)&GPIOD->ODR; value = *(uint32_t *)addr; <--- 寄存器0x40020C14=0x00000000 //addr = (uint32_t)&GPIOD->BSRRL; value = *(uint32_t *)addr; <--- 寄存器0x40020C18=0x00000000 //addr = (uint32_t)&GPIOD->BSRRH; value = *(uint32_t *)addr; <--- 寄存器0x40020C1A=0x00000000 //addr = (uint32_t)&GPIOD->LCKR; value = *(uint32_t *)addr; <--- 寄存器0x40020C1C=0x00000000 addr = (uint32_t)&GPIOD->AFR[0]; value = *(uint32_t *)addr; <--- 寄存器0x40020C20=0x00CC00CC addr = (uint32_t)&GPIOD->AFR[1]; value = *(uint32_t *)addr; <--- 寄存器0x40020C24=0xCCCCCCCC addr = (uint32_t)&GPIOE->MODER; value = *(uint32_t *)addr; <--- 寄存器0x40021000= 0xAAAAAA80 addr = (uint32_t)&GPIOE->OTYPER; value = *(uint32_t *)addr; <--- 寄存器0x40021004=0x00000000 addr = (uint32_t)&GPIOE->OSPEEDR; value = *(uint32_t *)addr; <--- 寄存器0x40021008=0xFFFFFFC0 addr = (uint32_t)&GPIOE->UPDR; value = *(uint32_t *)addr; <--- 寄存器0x4002100C=0x00000000 //addr = (uint32_t)&GPIOE->IDR; value = *(uint32_t *)addr; <--- 寄存器0x40021010=0x00006004 //addr = (uint32_t)&GPIOE->ODR; value = *(uint32_t *)addr; <--- 寄存器0x40021014=0x00000000 //addr = (uint32_t)&GPIOE->BSRRL; value = *(uint32_t *)addr; <--- 寄存器0x40021018=0x00000000 //addr = (uint32_t)&GPIOE->BSRRH; value = *(uint32_t *)addr; <--- 寄存器0x4002101A=0x00000000 //addr = (uint32_t)&GPIOE->LCKR; value = *(uint32_t *)addr; <--- 寄存器0x4002101C=0x00000000 addr = (uint32_t)&GPIOE->AFR[0]; value = *(uint32_t *)addr; <--- 寄存器0x40021020=0xCCCCC000 addr = (uint32_t)&GPIOE->AFR[1]; value = *(uint32_t *)addr; <--- 寄存器0x40021024=0xCCCCCCCC addr = (uint32_t)&GPIOF->MODER; value = *(uint32_t *)addr; <--- 寄存器0x40021400= 0xAA000AAA addr = (uint32_t)&GPIOF->OTYPER; value = *(uint32_t *)addr; <--- 寄存器0x40021404=0x00000000 addr = (uint32_t)&GPIOF->OSPEEDR; value = *(uint32_t *)addr; <--- 寄存器0x40021408=0xFF000FFF addr = (uint32_t)&GPIOF->UPDR; value = *(uint32_t *)addr; <--- 寄存器0x4002140C=0x00000000 //addr = (uint32_t)&GPIOF->IDR; value = *(uint32_t *)addr; <--- 寄存器0x40021410=0x00000980 //addr = (uint32_t)&GPIOF->ODR; value = *(uint32_t *)addr; <--- 寄存器0x40021414=0x00000000 //addr = (uint32_t)&GPIOF->BSRRL; value = *(uint32_t *)addr; <--- 寄存器0x40021418=0x00000000 //addr = (uint32_t)&GPIOF->BSRRH; value = *(uint32_t *)addr; <--- 寄存器0x4002141A=0x00000000 //addr = (uint32_t)&GPIOF->LCKR; value = *(uint32_t *)addr; <--- 寄存器0x4002141C=0x00000000 addr = (uint32_t)&GPIOF->AFR[0]; value = *(uint32_t *)addr; <--- 寄存器0x40021420=0x00CCCCCC addr = (uint32_t)&GPIOF->AFR[1]; value = *(uint32_t *)addr; <--- 寄存器0x40021424=0xCCCC0000 addr = (uint32_t)&GPIOG->MODER; value = *(uint32_t *)addr; <--- 寄存器0x40021800= 0x00080AAA addr = (uint32_t)&GPIOG->OTYPER; value = *(uint32_t *)addr; <--- 寄存器0x40021804=0x00000000 addr = (uint32_t)&GPIOG->OSPEEDR; value = *(uint32_t *)addr; <--- 寄存器0x40021808=0x000C0FFF addr = (uint32_t)&GPIOG->UPDR; value = *(uint32_t *)addr; <--- 寄存器0x4002180C=0x00000000 //addr = (uint32_t)&GPIOG->IDR; value = *(uint32_t *)addr; <--- 寄存器0x40021810=0x000016C0 //addr = (uint32_t)&GPIOG->ODR; value = *(uint32_t *)addr; <--- 寄存器0x40021814=0x00000000 //addr = (uint32_t)&GPIOG->BSRRL; value = *(uint32_t *)addr; <--- 寄存器0x40021818=0x00000000 //addr = (uint32_t)&GPIOG->BSRRH; value = *(uint32_t *)addr; <--- 寄存器0x4002181A=0x00000000 //addr = (uint32_t)&GPIOG->LCKR; value = *(uint32_t *)addr; <--- 寄存器0x4002181C=0x00000000 addr = (uint32_t)&GPIOG->AFR[0]; value = *(uint32_t *)addr; <--- 寄存器0x40021820=0x00CCCCCC addr = (uint32_t)&GPIOG->AFR[1]; value = *(uint32_t *)addr; <--- 寄存器0x40021824=0x000000C0 } #endif /*-- FSMC Configuration ------------------------------------------------------*/ p.FSMC_AddressSetupTime = 0x06; /* 0x05正常, 0x04 出错 */ p.FSMC_AddressHoldTime = 0x01; p.FSMC_DataSetupTime = 0x0C; /* 0x0B正常, 0x0A 出错 */ p.FSMC_BusTurnAroundDuration = 0x00; p.FSMC_CLKDivision = 0x00; p.FSMC_DataLatency = 0x00; p.FSMC_AccessMode = FSMC_AccessMode_B; FSMC_NORSRAMInitStructure.FSMC_Bank = FSMC_Bank1_NORSRAM2; FSMC_NORSRAMInitStructure.FSMC_DataAddressMux = FSMC_DataAddressMux_Disable; FSMC_NORSRAMInitStructure.FSMC_MemoryType = FSMC_MemoryType_NOR; FSMC_NORSRAMInitStructure.FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_16b; FSMC_NORSRAMInitStructure.FSMC_BurstAccessMode = FSMC_BurstAccessMode_Disable; FSMC_NORSRAMInitStructure.FSMC_AsynchronousWait = FSMC_AsynchronousWait_Disable; FSMC_NORSRAMInitStructure.FSMC_WaitSignalPolarity = FSMC_WaitSignalPolarity_Low; FSMC_NORSRAMInitStructure.FSMC_WrapMode = FSMC_WrapMode_Disable; FSMC_NORSRAMInitStructure.FSMC_WaitSignalActive = FSMC_WaitSignalActive_BeforeWaitState; FSMC_NORSRAMInitStructure.FSMC_WriteOperation = FSMC_WriteOperation_Enable; FSMC_NORSRAMInitStructure.FSMC_WaitSignal = FSMC_WaitSignal_Disable; FSMC_NORSRAMInitStructure.FSMC_ExtendedMode = FSMC_ExtendedMode_Disable; FSMC_NORSRAMInitStructure.FSMC_WriteBurst = FSMC_WriteBurst_Disable; FSMC_NORSRAMInitStructure.FSMC_ReadWriteTimingStruct = &p; FSMC_NORSRAMInitStructure.FSMC_WriteTimingStruct = &p; FSMC_NORSRAMInit(&FSMC_NORSRAMInitStructure); #if 1 //添加的测试代码 //FSMC_Bank1->BTCR[FSMC_NORSRAMInitStruct->FSMC_Bank] |= (uint32_t)BCR_FACCEN_SET; //FSMC_Bank1->BTCR[FSMC_NORSRAMInitStruct->FSMC_Bank+1] = ... //FSMC_Bank1E->BWTR[FSMC_NORSRAMInitStruct->FSMC_Bank] = 0x0FFFFFFF; { static uint32_t addr, value; addr = (uint32_t)&FSMC_Bank1->BTCR[FSMC_NORSRAMInitStructure.FSMC_Bank]; value = *(uint32_t *)addr; <--- 寄存器0xA0000008 =0x00001058 addr = (uint32_t)&FSMC_Bank1->BTCR[FSMC_NORSRAMInitStructure.FSMC_Bank+1]; value = *(uint32_t *)addr; <--- 寄存器0xA000000C =0x10000C16 addr = (uint32_t)&FSMC_Bank1E->BWTR[FSMC_NORSRAMInitStructure.FSMC_Bank]; value = *(uint32_t *)addr; <--- 寄存器0xA000010C =0x0FFFFFFF } #endif /*!< Enable FSMC Bank1_SRAM2 Bank */ FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM2, ENABLE); 0xA0000008 = 0X1059 addr = (uint32_t)&FSMC_Bank1->BTCR[FSMC_NORSRAMInitStructure.FSMC_Bank]; value = (*(uint32_t *)addr); <--- 寄存器0xA0000008 =0x00001059 while (1); // 原地踏步 寄存器配置完毕,之后 J-Link 就可以直接访问NOR Flash了。 NOR Flash物理地址:0x6400 0000 |