stm32 灵活静态存储控制器(FSMC)(NORFLASH\PSRAM)

最后更新于:2022-04-01 14:51:23

Flexible static memory controller(FSMC) 今天在处理TFT彩屏的时候突然发现有人用FSMC控制器来处理,然后就认真的研究了下FSMC; ![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-06-21_576915bc9f3db.jpg) 可见他分为4个块,三个类型,我们可以根据自己的需要来选择;这次我就直说FSMC 的Block 1; 首先,基地址BASE_ADDR = 6000 0000;至于片选,datasheet上也说了,我们可以通过控制HADDR(27,26)来选择操作; ![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-06-21_576915bcb5cf3.jpg) 然后还有今天一直困扰我的问题,我要选择A16,我用的是16位数据,他的数据地址为6000 0000 + 2^16*2 = 6002 0000;我一直都在疑惑:明明是A16,为什么是第17位被置1,后来终于在datasheet上发现这个问题的根源! ![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-06-21_576915bccb4b7.jpg) 上面说的很清楚,数据宽度为16位时,HADDR[25:1]与FSMC_A[24:0]相连,那么这时候的FSMC_A16,就与HADDR[17]相连,所以地址就是6000 0000 + 2^17; 下面来看看FSMC如何与TFT联系起来! ![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-06-21_576915bce111e.jpg) A16 --> RS -- 使能 D0~D15 数据线 FSMC_WE --> WE FSMC_OE --> OE 这个连接方式让我想起了微机原理上的8059;呵呵,如果你学过了微机原理,看到这个图应该懂了80%; 代码分析: ~~~ FSMC_NORSRAMInitTypeDef  FSMC_NORSRAMInitStructure; FSMC_NORSRAMTimingInitTypeDef  p; GPIO_InitTypeDef  GPIO_InitStructure;  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC |                        RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE , ENABLE);   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOD, &GPIO_InitStructure);     GPIO_SetBits(GPIOD, GPIO_Pin_13); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOE, &GPIO_InitStructure);   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_14 |                                GPIO_Pin_15; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOD, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = 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);  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;  GPIO_Init(GPIOD, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 ;  GPIO_Init(GPIOD, &GPIO_InitStructure);  p.FSMC_AddressSetupTime = 0x02; p.FSMC_AddressHoldTime = 0x02; p.FSMC_DataSetupTime = 0x05; p.FSMC_AccessMode = FSMC_AccessMode_B; FSMC_NORSRAMInitStructure.FSMC_Bank = FSMC_Bank1_NORSRAM1; 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_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);  FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM1, ENABLE);   ~~~ 时钟初始化是必须的!希望大家不要forget了! 另外gpio的初始化,要看两个地方:1,数据手册上,每种管脚初始化成什么模式;2,哪些管脚需要初始化; ![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-06-21_576915bd02c6b.jpg) 管脚对应自己对着原理图一个一个找! FSMC_NORSRAMTimingInitTypeDef  设置 p.FSMC_AddressSetupTime = 0x02; p.FSMC_AddressHoldTime = 0x02; p.FSMC_DataSetupTime = 0x05; p.FSMC_AccessMode = FSMC_AccessMode_B; 这里我只设置了4项,因为其他几项都是与norflash有关的,所以我们不用设置,因为TFT内部是RAM; 具体时间参考,在datasheet上也给了说明: ![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-06-21_576915bd2138f.jpg) 见网上有人设置FSMC_ADDressHoldTime = 0x0;发现也没啥影响,但是我们还是最好按照这个标准来!因为毕竟是官方给的标准! ~~~ FSMC_NORSRAMInitStructure.FSMC_Bank = FSMC_Bank1_NORSRAM1; ~~~ 选择Bank1_NORSRAM1;因为我们本身就是选的他毋庸置疑; ~~~ FSMC_NORSRAMInitStructure.FSMC_DataAddressMux = FSMC_DataAddressMux_Disable; ~~~ 我再TFT上也没有用到多路复用,因为我就只是跟TFT通信,说到这,我们还可以用它来控制4个彩屏一起显示! ~~~ FSMC_NORSRAMInitStructure.FSMC_MemoryType = FSMC_MemoryType_PSRAM; ~~~ 这个参数是用来选择到底是哪个类型的存储器,他有三个option: ~~~ #define FSMC_MemoryType_SRAM                            ((uint32_t)0x00000000) #define FSMC_MemoryType_PSRAM                           ((uint32_t)0x00000004) #define FSMC_MemoryType_NOR                             ((uint32_t)0x00000008) ~~~ 因为我不知道彩屏的内存到底是哪种类型的,我测试了下,三个选项对彩屏都是一样的效果,因为我对SRAM,NOR比较熟悉,所以就选了一个不熟悉的选项; ~~~ FSMC_NORSRAMInitStructure.FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_16b; ~~~ 16位的数据宽度 ~~~ FSMC_NORSRAMInitStructure.FSMC_BurstAccessMode = FSMC_BurstAccessMode_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; ~~~ 然后我的彩屏控制基地址为 ~~~ #define Bank1_LCD_DATA    ((uint32_t)0x60020000)  #define Bank1_LCD_CTL     ((uint32_t)0x60000000) ~~~ 使用方法: ~~~ *(__IO uint16_t *) (Bank1_LCD_C)= reg; *(__IO uint16_t *) (Bank1_LCD_D)= cmd; ~~~ 可能理解的还不是太好,请大家多多指点;
';