该SRAM芯片有A18~0共19根地址线,即2^19=524288个地址,有D15~0共16根数据线,因此位宽为16位,也就是2字节。总容量为524288×2=1048576字节=1MB。
连线:
A0~13接PC0~13,由于PC14和PC15接了LSE晶振,所以把A14~15接到PA0~1上,这16位地址手动生成。注意:SRAM不可使用混合模式,A15~0不能和D15~0接到一起。
A16~18接FSMC的PD11~13,这三位地址由FSMC自动生成。
D0~1接PD14~15,D2~3接PD0~1,D4~12接PE7~15,D13~15接PD8~10。
片选信号NE3(E=Enable)接到FSMC上的NE1:PD7。注意微雪IS62WV51216BLL SRAM板上虽然有NE2~3、CS1~3五个片选引脚,但其实只有NE3上面焊接了电阻连接到了芯片上(如下图所示)。
NOE(OE=Output Enable,前缀N表示低电平有效)接PD4。NWE(WE=Write Enable)接PD5。NBL0(BL=Byte Lane)接PE0,NBL1接PE1。
最后把电源引脚VCC和GND接上,电压为3.3V。
【程序】
#include
#include
// 单片机上的片选信号NE1(PD7)必须接到SRAM板上的NE3引脚上, 因为其他引脚都没有焊接电阻!!!
int fputc(int ch, FILE *fp)
{
if (fp == stdout)
{
if (ch == '\n')
{
while ((USART1->SR & USART_SR_TXE) == 0);
USART1->DR = '\r';
}
while ((USART1->SR & USART_SR_TXE) == 0);
USART1->DR = ch;
}
return ch;
}
// addr的范围: 0~524287
uint16_t read(uint32_t addr)
{
GPIOC->ODR = addr & 0x3fff;
GPIOA->ODR = (addr >> 14) & 0x03;
return *(volatile uint16_t *)(0x60000000 | (addr << 1));
}
void write(uint32_t addr, uint16_t value)
{
GPIOC->ODR = addr & 0x3fff;
GPIOA->ODR = (addr >> 14) & 0x03;
*(volatile uint16_t *)(0x60000000 | (addr << 1)) = value;
}
// 8位访存测试
void test8(void)
{
volatile uint8_t *const arr = (volatile uint8_t *)0x60000000;
uint8_t i;
for (i = 0; i < 20; i++)
{
GPIOC->ODR = i >> 1; // 地址引脚
arr[i] = 3 * i;
}
for (i = 0; i < 20; i++)
{
GPIOC->ODR = i >> 1;
printf("%d ", arr[i]);
}
printf("\n");
}
// 16位访存测试
void test16(void)
{
uint32_t i;
for (i = 0; i <= 524287; i++)
write(i, i / 10);
for (i = 0; i <= 100; i++)
{
printf("[%d]%d ", i, read(i));
if (i % 5 == 4)
printf("\n");
}
for (i = 523500; i <= 524287; i++)
{
printf("[%d]%d ", i, read(i));
if (i % 5 == 4)
printf("\n");
}
printf("\n");
}
int main(void)
{
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN | RCC_AHB1ENR_GPIOCEN | RCC_AHB1ENR_GPIODEN | RCC_AHB1ENR_GPIOEEN;
RCC->AHB3ENR = RCC_AHB3ENR_FSMCEN;
RCC->APB2ENR = RCC_APB2ENR_USART1EN;
GPIOA->AFR[1] = 0x00000070; // PA9为USART1的发送端口
GPIOA->MODER |= 0x00080005; // PA0~1设为输出, PA9设为复用
GPIOC->MODER = 0x05555555; // PC0~13设为输出
// PD, PE上所有与FSMC有关的引脚全部设为复用
GPIOD->AFR[0] = 0xc0cc00cc; // FSMC为AF12
GPIOD->AFR[1] = 0xcccccccc;
GPIOD->MODER = 0xaaaa8a0a;
GPIOE->AFR[0] = 0xc00000cc;
GPIOE->AFR[1] = 0xcccccccc;
GPIOE->MODER = 0xaaaa800a;
FSMC_Bank1->BTCR[0] &= ~(FSMC_BCR1_WAITEN | FSMC_BCR1_FACCEN | FSMC_BCR1_MTYP | FSMC_BCR1_MUXEN); // MTYPE=00(SRAM), MUXEN=0
//FSMC_Bank1->BTCR[0] |= FSMC_BCR1_MTYP_0; // MTYPE=01(PSRAM)
FSMC_Bank1->BTCR[1] = 0x313; // DATAST=3, ADDHLD=1, ADDSET=3
USART1->BRR = SystemCoreClock / 115200;
USART1->CR1 = USART_CR1_UE | USART_CR1_TE;
test8();
test16();
while (1);
}
【程序运行结果】
0 3 6 9 12 15 18 21 24 27 30 33 36 39 42 45 48 51 54 57
[0]0 [1]0 [2]0 [3]0 [4]0
[5]0 [6]0 [7]0 [8]0 [9]0
[10]1 [11]1 [12]1 [13]1 [14]1
[15]1 [16]1 [17]1 [18]1 [19]1
[20]2 [21]2 [22]2 [23]2 [24]2
[25]2 [26]2 [27]2 [28]2 [29]2
[30]3 [31]3 [32]3 [33]3 [34]3
[35]3 [36]3 [37]3 [38]3 [39]3
[40]4 [41]4 [42]4 [43]4 [44]4
[45]4 [46]4 [47]4 [48]4 [49]4
[50]5 [51]5 [52]5 [53]5 [54]5
[55]5 [56]5 [57]5 [58]5 [59]5
[60]6 [61]6 [62]6 [63]6 [64]6
[65]6 [66]6 [67]6 [68]6 [69]6
[70]7 [71]7 [72]7 [73]7 [74]7
[75]7 [76]7 [77]7 [78]7 [79]7
[80]8 [81]8 [82]8 [83]8 [84]8
[85]8 [86]8 [87]8 [88]8 [89]8
[90]9 [91]9 [92]9 [93]9 [94]9
[95]9 [96]9 [97]9 [98]9 [99]9
[100]10 [523500]52350 [523501]52350 [523502]52350 [523503]52350 [523504]52350
[523505]52350 [523506]52350 [523507]52350 [523508]52350 [523509]52350
[523510]52351 [523511]52351 [523512]52351 [523513]52351 [523514]52351
[523515]52351 [523516]52351 [523517]52351 [523518]52351 [523519]52351
[523520]52352 [523521]52352 [523522]52352 [523523]52352 [523524]52352
[523525]52352 [523526]52352 [523527]52352 [523528]52352 [523529]52352
[523530]52353 [523531]52353 [523532]52353 [523533]52353 [523534]52353
[523535]52353 [523536]52353 [523537]52353 [523538]52353 [523539]52353
[523540]52354 [523541]52354 [523542]52354 [523543]52354 [523544]52354
[523545]52354 [523546]52354 [523547]52354 [523548]52354 [523549]52354
[523550]52355 [523551]52355 [523552]52355 [523553]52355 [523554]52355
[523555]52355 [523556]52355 [523557]52355 [523558]52355 [523559]52355
[523560]52356 [523561]52356 [523562]52356 [523563]52356 [523564]52356
[523565]52356 [523566]52356 [523567]52356 [523568]52356 [523569]52356
[523570]52357 [523571]52357 [523572]52357 [523573]52357 [523574]52357
[523575]52357 [523576]52357 [523577]52357 [523578]52357 [523579]52357
[523580]52358 [523581]52358 [523582]52358 [523583]52358 [523584]52358
[523585]52358 [523586]52358 [523587]52358 [523588]52358 [523589]52358
[523590]52359 [523591]52359 [523592]52359 [523593]52359 [523594]52359
[523595]52359 [523596]52359 [523597]52359 [523598]52359 [523599]52359
[523600]52360 [523601]52360 [523602]52360 [523603]52360 [523604]52360
[523605]52360 [523606]52360 [523607]52360 [523608]52360 [523609]52360
[523610]52361 [523611]52361 [523612]52361 [523613]52361 [523614]52361
[523615]52361 [523616]52361 [523617]52361 [523618]52361 [523619]52361
[523620]52362 [523621]52362 [523622]52362 [523623]52362 [523624]52362
[523625]52362 [523626]52362 [523627]52362 [523628]52362 [523629]52362
[523630]52363 [523631]52363 [523632]52363 [523633]52363 [523634]52363
[523635]52363 [523636]52363 [523637]52363 [523638]52363 [523639]52363
[523640]52364 [523641]52364 [523642]52364 [523643]52364 [523644]52364
[523645]52364 [523646]52364 [523647]52364 [523648]52364 [523649]52364
[523650]52365 [523651]52365 [523652]52365 [523653]52365 [523654]52365
[523655]52365 [523656]52365 [523657]52365 [523658]52365 [523659]52365
[523660]52366 [523661]52366 [523662]52366 [523663]52366 [523664]52366
[523665]52366 [523666]52366 [523667]52366 [523668]52366 [523669]52366
[523670]52367 [523671]52367 [523672]52367 [523673]52367 [523674]52367
[523675]52367 [523676]52367 [523677]52367 [523678]52367 [523679]52367
[523680]52368 [523681]52368 [523682]52368 [523683]52368 [523684]52368
[523685]52368 [523686]52368 [523687]52368 [523688]52368 [523689]52368
[523690]52369 [523691]52369 [523692]52369 [523693]52369 [523694]52369
[523695]52369 [523696]52369 [523697]52369 [523698]52369 [523699]52369
[523700]52370 [523701]52370 [523702]52370 [523703]52370 [523704]52370
[523705]52370 [523706]52370 [523707]52370 [523708]52370 [523709]52370
[523710]52371 [523711]52371 [523712]52371 [523713]52371 [523714]52371
[523715]52371 [523716]52371 [523717]52371 [523718]52371 [523719]52371
[523720]52372 [523721]52372 [523722]52372 [523723]52372 [523724]52372
[523725]52372 [523726]52372 [523727]52372 [523728]52372 [523729]52372
[523730]52373 [523731]52373 [523732]52373 [523733]52373 [523734]52373
[523735]52373 [523736]52373 [523737]52373 [523738]52373 [523739]52373
[523740]52374 [523741]52374 [523742]52374 [523743]52374 [523744]52374
[523745]52374 [523746]52374 [523747]52374 [523748]52374 [523749]52374
[523750]52375 [523751]52375 [523752]52375 [523753]52375 [523754]52375
[523755]52375 [523756]52375 [523757]52375 [523758]52375 [523759]52375
[523760]52376 [523761]52376 [523762]52376 [523763]52376 [523764]52376
[523765]52376 [523766]52376 [523767]52376 [523768]52376 [523769]52376
[523770]52377 [523771]52377 [523772]52377 [523773]52377 [523774]52377
[523775]52377 [523776]52377 [523777]52377 [523778]52377 [523779]52377
[523780]52378 [523781]52378 [523782]52378 [523783]52378 [523784]52378
[523785]52378 [523786]52378 [523787]52378 [523788]52378 [523789]52378
[523790]52379 [523791]52379 [523792]52379 [523793]52379 [523794]52379
[523795]52379 [523796]52379 [523797]52379 [523798]52379 [523799]52379
[523800]52380 [523801]52380 [523802]52380 [523803]52380 [523804]52380
[523805]52380 [523806]52380 [523807]52380 [523808]52380 [523809]52380
[523810]52381 [523811]52381 [523812]52381 [523813]52381 [523814]52381
[523815]52381 [523816]52381 [523817]52381 [523818]52381 [523819]52381
[523820]52382 [523821]52382 [523822]52382 [523823]52382 [523824]52382
[523825]52382 [523826]52382 [523827]52382 [523828]52382 [523829]52382
[523830]52383 [523831]52383 [523832]52383 [523833]52383 [523834]52383
[523835]52383 [523836]52383 [523837]52383 [523838]52383 [523839]52383
[523840]52384 [523841]52384 [523842]52384 [523843]52384 [523844]52384
[523845]52384 [523846]52384 [523847]52384 [523848]52384 [523849]52384
[523850]52385 [523851]52385 [523852]52385 [523853]52385 [523854]52385
[523855]52385 [523856]52385 [523857]52385 [523858]52385 [523859]52385
[523860]52386 [523861]52386 [523862]52386 [523863]52386 [523864]52386
[523865]52386 [523866]52386 [523867]52386 [523868]52386 [523869]52386
[523870]52387 [523871]52387 [523872]52387 [523873]52387 [523874]52387
[523875]52387 [523876]52387 [523877]52387 [523878]52387 [523879]52387
[523880]52388 [523881]52388 [523882]52388 [523883]52388 [523884]52388
[523885]52388 [523886]52388 [523887]52388 [523888]52388 [523889]52388
[523890]52389 [523891]52389 [523892]52389 [523893]52389 [523894]52389
[523895]52389 [523896]52389 [523897]52389 [523898]52389 [523899]52389
[523900]52390 [523901]52390 [523902]52390 [523903]52390 [523904]52390
[523905]52390 [523906]52390 [523907]52390 [523908]52390 [523909]52390
[523910]52391 [523911]52391 [523912]52391 [523913]52391 [523914]52391
[523915]52391 [523916]52391 [523917]52391 [523918]52391 [523919]52391
[523920]52392 [523921]52392 [523922]52392 [523923]52392 [523924]52392
[523925]52392 [523926]52392 [523927]52392 [523928]52392 [523929]52392
[523930]52393 [523931]52393 [523932]52393 [523933]52393 [523934]52393
[523935]52393 [523936]52393 [523937]52393 [523938]52393 [523939]52393
[523940]52394 [523941]52394 [523942]52394 [523943]52394 [523944]52394
[523945]52394 [523946]52394 [523947]52394 [523948]52394 [523949]52394
[523950]52395 [523951]52395 [523952]52395 [523953]52395 [523954]52395
[523955]52395 [523956]52395 [523957]52395 [523958]52395 [523959]52395
[523960]52396 [523961]52396 [523962]52396 [523963]52396 [523964]52396
[523965]52396 [523966]52396 [523967]52396 [523968]52396 [523969]52396
[523970]52397 [523971]52397 [523972]52397 [523973]52397 [523974]52397
[523975]52397 [523976]52397 [523977]52397 [523978]52397 [523979]52397
[523980]52398 [523981]52398 [523982]52398 [523983]52398 [523984]52398
[523985]52398 [523986]52398 [523987]52398 [523988]52398 [523989]52398
[523990]52399 [523991]52399 [523992]52399 [523993]52399 [523994]52399
[523995]52399 [523996]52399 [523997]52399 [523998]52399 [523999]52399
[524000]52400 [524001]52400 [524002]52400 [524003]52400 [524004]52400
[524005]52400 [524006]52400 [524007]52400 [524008]52400 [524009]52400
[524010]52401 [524011]52401 [524012]52401 [524013]52401 [524014]52401
[524015]52401 [524016]52401 [524017]52401 [524018]52401 [524019]52401
[524020]52402 [524021]52402 [524022]52402 [524023]52402 [524024]52402
[524025]52402 [524026]52402 [524027]52402 [524028]52402 [524029]52402
[524030]52403 [524031]52403 [524032]52403 [524033]52403 [524034]52403
[524035]52403 [524036]52403 [524037]52403 [524038]52403 [524039]52403
[524040]52404 [524041]52404 [524042]52404 [524043]52404 [524044]52404
[524045]52404 [524046]52404 [524047]52404 [524048]52404 [524049]52404
[524050]52405 [524051]52405 [524052]52405 [524053]52405 [524054]52405
[524055]52405 [524056]52405 [524057]52405 [524058]52405 [524059]52405
[524060]52406 [524061]52406 [524062]52406 [524063]52406 [524064]52406
[524065]52406 [524066]52406 [524067]52406 [524068]52406 [524069]52406
[524070]52407 [524071]52407 [524072]52407 [524073]52407 [524074]52407
[524075]52407 [524076]52407 [524077]52407 [524078]52407 [524079]52407
[524080]52408 [524081]52408 [524082]52408 [524083]52408 [524084]52408
[524085]52408 [524086]52408 [524087]52408 [524088]52408 [524089]52408
[524090]52409 [524091]52409 [524092]52409 [524093]52409 [524094]52409
[524095]52409 [524096]52409 [524097]52409 [524098]52409 [524099]52409
[524100]52410 [524101]52410 [524102]52410 [524103]52410 [524104]52410
[524105]52410 [524106]52410 [524107]52410 [524108]52410 [524109]52410
[524110]52411 [524111]52411 [524112]52411 [524113]52411 [524114]52411
[524115]52411 [524116]52411 [524117]52411 [524118]52411 [524119]52411
[524120]52412 [524121]52412 [524122]52412 [524123]52412 [524124]52412
[524125]52412 [524126]52412 [524127]52412 [524128]52412 [524129]52412
[524130]52413 [524131]52413 [524132]52413 [524133]52413 [524134]52413
[524135]52413 [524136]52413 [524137]52413 [524138]52413 [524139]52413
[524140]52414 [524141]52414 [524142]52414 [524143]52414 [524144]52414
[524145]52414 [524146]52414 [524147]52414 [524148]52414 [524149]52414
[524150]52415 [524151]52415 [524152]52415 [524153]52415 [524154]52415
[524155]52415 [524156]52415 [524157]52415 [524158]52415 [524159]52415
[524160]52416 [524161]52416 [524162]52416 [524163]52416 [524164]52416
[524165]52416 [524166]52416 [524167]52416 [524168]52416 [524169]52416
[524170]52417 [524171]52417 [524172]52417 [524173]52417 [524174]52417
[524175]52417 [524176]52417 [524177]52417 [524178]52417 [524179]52417
[524180]52418 [524181]52418 [524182]52418 [524183]52418 [524184]52418
[524185]52418 [524186]52418 [524187]52418 [524188]52418 [524189]52418
[524190]52419 [524191]52419 [524192]52419 [524193]52419 [524194]52419
[524195]52419 [524196]52419 [524197]52419 [524198]52419 [524199]52419
[524200]52420 [524201]52420 [524202]52420 [524203]52420 [524204]52420
[524205]52420 [524206]52420 [524207]52420 [524208]52420 [524209]52420
[524210]52421 [524211]52421 [524212]52421 [524213]52421 [524214]52421
[524215]52421 [524216]52421 [524217]52421 [524218]52421 [524219]52421
[524220]52422 [524221]52422 [524222]52422 [524223]52422 [524224]52422
[524225]52422 [524226]52422 [524227]52422 [524228]52422 [524229]52422
[524230]52423 [524231]52423 [524232]52423 [524233]52423 [524234]52423
[524235]52423 [524236]52423 [524237]52423 [524238]52423 [524239]52423
[524240]52424 [524241]52424 [524242]52424 [524243]52424 [524244]52424
[524245]52424 [524246]52424 [524247]52424 [524248]52424 [524249]52424
[524250]52425 [524251]52425 [524252]52425 [524253]52425 [524254]52425
[524255]52425 [524256]52425 [524257]52425 [524258]52425 [524259]52425
[524260]52426 [524261]52426 [524262]52426 [524263]52426 [524264]52426
[524265]52426 [524266]52426 [524267]52426 [524268]52426 [524269]52426
[524270]52427 [524271]52427 [524272]52427 [524273]52427 [524274]52427
[524275]52427 [524276]52427 [524277]52427 [524278]52427 [524279]52427
[524280]52428 [524281]52428 [524282]52428 [524283]52428 [524284]52428
[524285]52428 [524286]52428 [524287]52428
在FSMC中共有4个Bank,其中第一个Bank专门用于访问NOR Flash和SRAM、PSRAM存储器,寻址空间为256MB。IS62WV51216BLL芯片属于SRAM存储器。地址、数据线混合模式(Multiplexed I/Os)通常只能用于NOR Flash存储器,不能用于SRAM存储器。所以,在程序中我们将MUXEN置0,关闭了混合模式。EXTMOD为0,不开启扩展模式。
Bank 2和Bank 3用于NAND Flash,Bank 4用于PC Card。
Bank 1的地址范围是0x60000000~0x6FFFFFFF,该Bank又分为4个Subbank,记为Bank1 - NOR/PSRAM 1~4,每个Subbank的寻址空间为64MB。但在100脚的芯片中,只有Bank1 - NOR/PSRAM 1可用,其片选端口为NE1(PD7),地址范围为0x60000000~0x63FFFFFF。地址的第27~26位决定Subbank,第25~0位由外部地址线决定。
由于该SRAM芯片每个地址访问的是两个字节的数据,而在STM32单片机中每个地址只允许访问1字节的数据,所以在访存时,STM32单片机先将第25~1位送到SRAM地址线上(对于该芯片来说,则是第20~1位),然后根据最后一位地址位决定NBL1~0引脚的电平。
Bank 1的每个Subbank都有3个寄存器:FSMC_BCRx,FSMC_BTRx,FSMC_BWTRx,其中x为1~4。只有选用了扩展模式(EXTMOD=1时),BWTRx寄存器才有效。BTCR[0]就是FSMC_BCR1寄存器,BTCR[1]为FSMC_BTR1,BTCR[2]为FSMC_BCR2,BTCR[3]为FSMC_BTR2,以此类推。
默认情况下,Subbank1是打开的(MBKEN=1),但存储器类型却是NOR Flash(MTYP=10),因此需要在程序中改成SRAM(MTYP=00),且还要将FACCEN置0(是否开启NOR Flash访问)。位宽默认就是16位(MWID=01),无需改动。由于SRAM上没有NWAIT引脚,因此还需把WAITEN置0。
还要注意的是,BCR寄存器中的第7位为保留位,必须要保持复位时的默认值,也就是1(而不是0)。
由于100脚的单片机里没有A15~0引脚,因此我们把它接到了PC0~13和PA0~1上,然后在每次读写内存前都手动设置。
Bank1 - NOR/PSRAM 1一共有64MB的寻址空间(26位地址线),我们只用了其中1MB的地址空间(19位地址线+1位)。那么有没有办法把这64MB都用完呢?答案是肯定的。我们可以用一个6-64线译码器来译码,接上64个这样的SRAM芯片,总容量为64MB,占用的地址空间刚好就是64MB。当然实际上肯定没有6-64线的译码器,得用很多更小的译码器(如2-4译码器、3-8译码器和4-16译码器等)来拼接。
我们以2-4译码器(如74HC139)为例,如图所示:
输入端接单片机上的地址线A19~20,每个输出端通过一个或门接到每个SRAM芯片的NE3片选引脚上,或门的另一个输入端接单片机上的NE1引脚,即PD7。低地址段A18~0全部接到一起。
当PD7=0(有效),且A20~19=01时,译码器的输出端DCBA为1101,NE3(2)=0,此时刚好选中了第二片SRAM。
当PD7=1(无效)时,或门的输出端全为1,4片SRAM都没有被选中。
用5片2-4译码器可以构成3-8译码器,可以接8个SRAM芯片,用掉4MB的地址空间,容量为8MB。这样接下去,理论上肯定是能把64MB的地址空间都用完的。
如果用上译码器上本身的使能信号E,把它接到PD7上,那么我们就不需要接4个或门了。直接把输出端接到每个SRAM的NE3上就行了。
74HC154就是一个4-16译码器,输出端低电平有效,可以挂接16个SRAM芯片。
假设我们真的用完了这64MB的地址空间,且每个SRAM芯片都是16位的,那么使用的地址线则是A24~0,不是A25~0。A25这根地址线只能用于8位的存储器芯片。
STM32中的地址只能访问一个字节,若我们访问的地址是addr = 0x63E24567,那么实际在地址线A24~0上输出的地址是(addr & 0x3FFFFFF) >> 1 = 0x1F122B3 = 0b1111100010010001010110011,addr的最后一位反映在NBL1~0上。