spi_prog.rmh

来自「和picoblaze完全兼容的mcu ip core」· RMH 代码 · 共 1,531 行 · 第 1/5 页

RMH
1,531 行
字号
// #645: ;CONSTANT SPI_adc_conv, 10           ;          A/D convert - bit4// #646: ;CONSTANT SPI_dac_cs, 20             ;           D/A select - bit5// #647: ;CONSTANT SPI_amp_shdn, 40           ;       amplifier SHDN - bit6// #648: ;CONSTANT SPI_dac_clr, 80            ;            D/A clear - bit7// #649: ;// #650: ;CONSTANT SPI_output_port, pp        ;SPI data output                *// #651: ;CONSTANT SPI_sdo, 80                ;   SDO - bit7                  *// #652: ;// #653: ;CONSTANT SPI_input_port, pp         ;SPI data input                 *// #654: ;CONSTANT SPI_sdi, 80                ;             SDI - bit7        *// #655: ;CONSTANT SPI_amp_sdi, 40            ;   amplifier SDI - bit6// #656: ;// #657: ;// #658: ;A single scratch pad memory location is also employed to remember the status of// #659: ;the SPI_control_port. This memory location must be defined as follows.// #660: ;   (replace 'ss' with appropriate memory location)// #661: ;// #662: ;CONSTANT SPI_control_status, ss       ;SPI status signals// #663: ;// #664: ;Not all the SPI routines will use this memory location because although they// #665: ;will change the bits on the control port, they will leave them in the same state// #666: ;as they were in when they started.// #667: ;// #668: ;// #669: ;// #670: ;// #671: ;Initialise SPI bus// #672: ;// #673: ;This routine should be used to initialise the SPI bus.// #674: ;The SCK clock is made low.// #675: ;Device selections are made inactive as follows// #676: ;   SPI_sck      = 0      Clock is Low (required)// #677: ;   SPI_rom_cs   = 1      Deselect ROM// #678: ;   spare        = 1      spare control bit// #679: ;   SPI_amp_cs   = 1      Deselect amplifier// #680: ;   SPI_adc_conv = 0      A/D convert ready to apply positive pulse// #681: ;   SPI_dac_cs   = 1      Deselect D/A// #682: ;   SPI_amp_shdn = 0      Amplifier active and available// #683: ;   SPI_dac_clr  = 1      D/A clear off// #684: ;// @10e #685: [SPI_init]000ae // @10e #685: LOAD(s0,AE) ;normally AE2c008 // @10f #686: OUTPUT(s0,SPI_control_port)2e004 // @110 #687: STORE(s0,SPI_control_status) ;preserve status2a000 // @111 #688: RETURN// #689: ;// #690: ;// #691: ;Send and receive one byte to or from the SPI FLASH memory.// #692: ;// #693: ;The data supplied in register 's2' is transmitted to the SPI bus and// #694: ;at the same time any received byte is used to replace the value in 's2'.// #695: ;The SCK clock is generated by software and results in a communication rate of// #696: ;2.5Mbit/s with a 50MHz clock.// #697: ;// #698: ;Note that you must have previously selected the required device on the bus// #699: ;before attempting communication and you must subsequently deselect the device// #700: ;when appropriate.// #701: ;// #702: ;Entry to this routine assumes that SCK is already Low and the clock will be Low// #703: ;at the end of execution (provided in scratch pad memory location SPI_control_status).// #704: ;// #705: ;As a 'master' the signal sequence is as follows..// #706: ;   Receive data bit from SDI line (Flash transmits on previous falling edge)// #707: ;   Transmit data bit on SDO line// #708: ;   Drive SCK transition from low to high// #709: ;   Drive SCK transition from high to low.// #710: ;// @112 #711: [SPI_FLASH_tx_rx]00108 // @112 #711: LOAD(s1,8) ;8-bits to transmit and receive06004 // @113 #712: FETCH(s0,SPI_control_status) ;read control status bits// @114 #713: [next_SPI_FLASH_bit]2c204 // @114 #713: OUTPUT(s2,SPI_output_port) ;output data bit ready to be used on rising edge04302 // @115 #714: INPUT(s3,SPI_input_port) ;read input bit12380 // @116 #715: TEST(s3,SPI_sdi) ;detect state of received bit20200 // @117 #716: SLA(s2) ;shift new data into result and move to next transmit bit0e001 // @118 #717: XOR(s0,SPI_sck) ;clock High (bit0)2c008 // @119 #718: OUTPUT(s0,SPI_control_port) ;drive clock High0e001 // @11a #719: XOR(s0,SPI_sck) ;clock Low (bit0)2c008 // @11b #720: OUTPUT(s0,SPI_control_port) ;drive clock Low1c101 // @11c #721: SUB(s1,1) ;count bits35514 // @11d #722: JUMP(NZ,next_SPI_FLASH_bit) ;repeat until finished2a000 // @11e #723: RETURN// #724: ;// #725: ;// #726: ;Read status register from SPI FLASH memory (ST type M25P16)// #727: ;// #728: ;Transmits instruction 05hex and then receives one byte in response// #729: ;which is returned in register s2.// #730: ;// #731: ; bit    meaning// #732: ;  7    SRWD Status Register Write Protect// #733: ;  6    '0'// #734: ;  5    '0'// #735: ;  4    BP2 Block protect bit// #736: ;  3    BP1 Block protect bit// #737: ;  2    BP0 Block protect bit// #738: ;  1    WEL Write Enable Latch Bit// #739: ;  0    WIP Write In Progress// #740: ;// #741: ;// @11f #742: [read_spi_flash_status]3010e // @11f #742: CALL(SPI_init) ;ensure known state of bus and s0 register0e002 // @120 #743: XOR(s0,SPI_rom_cs) ;select (Low) FLASH2c008 // @121 #744: OUTPUT(s0,SPI_control_port)2e004 // @122 #745: STORE(s0,SPI_control_status) ;preserve status00205 // @123 #746: LOAD(s2,5) ;Read Status register instruction30112 // @124 #747: CALL(SPI_FLASH_tx_rx) ;transmit instruction30112 // @125 #748: CALL(SPI_FLASH_tx_rx) ;Receive status register information0e002 // @126 #749: XOR(s0,SPI_rom_cs) ;deselect (High) FLASH2c008 // @127 #750: OUTPUT(s0,SPI_control_port)2e004 // @128 #751: STORE(s0,SPI_control_status) ;preserve status2a000 // @129 #752: RETURN// #753: ;// #754: ;Set write enable mode in SPI FLASH memory (ST type M25P16)// #755: ;// #756: ;Transmits instruction 06hex.// #757: ;// @12a #758: [set_spi_flash_WREN]3010e // @12a #758: CALL(SPI_init) ;ensure known state of bus and s0 register0e002 // @12b #759: XOR(s0,SPI_rom_cs) ;select (Low) FLASH2c008 // @12c #760: OUTPUT(s0,SPI_control_port)2e004 // @12d #761: STORE(s0,SPI_control_status) ;preserve status00206 // @12e #762: LOAD(s2,6) ;Set write enable mode instruction30112 // @12f #763: CALL(SPI_FLASH_tx_rx) ;transmit instruction0e002 // @130 #764: XOR(s0,SPI_rom_cs) ;deselect (High) FLASH2c008 // @131 #765: OUTPUT(s0,SPI_control_port)2e004 // @132 #766: STORE(s0,SPI_control_status) ;preserve status2a000 // @133 #767: RETURN// #768: ;// #769: ;Reset the write enable mode in SPI FLASH memory (ST type M25P16)// #770: ;// #771: ;Transmits instruction 04hex.// #772: ;// @134 #773: [reset_spi_flash_WREN]3010e // @134 #773: CALL(SPI_init) ;ensure known state of bus and s0 register0e002 // @135 #774: XOR(s0,SPI_rom_cs) ;select (Low) FLASH2c008 // @136 #775: OUTPUT(s0,SPI_control_port)2e004 // @137 #776: STORE(s0,SPI_control_status) ;preserve status00204 // @138 #777: LOAD(s2,4) ;Reset write enable mode instruction30112 // @139 #778: CALL(SPI_FLASH_tx_rx) ;transmit instruction0e002 // @13a #779: XOR(s0,SPI_rom_cs) ;deselect (High) FLASH2c008 // @13b #780: OUTPUT(s0,SPI_control_port)2e004 // @13c #781: STORE(s0,SPI_control_status) ;preserve status2a000 // @13d #782: RETURN// #783: ;// #784: ;Read the identification ID from SPI FLASH memory (ST type M25P16)// #785: ;// #786: ;Transmits instruction 9Fhex and then reads the 3 byte response into [s9,s8,s7]// #787: ;// #788: ;response should be// #789: ;   s9 = Manufacturer Identification = 20 hex// #790: ;   s8 = Memory Type = 20 hex// #791: ;   s7 = Memory Capacity = 15 hex// #792: ;// @13e #793: [read_spi_flash_ID]3010e // @13e #793: CALL(SPI_init) ;ensure known state of bus and s0 register0e002 // @13f #794: XOR(s0,SPI_rom_cs) ;select (Low) FLASH2c008 // @140 #795: OUTPUT(s0,SPI_control_port)2e004 // @141 #796: STORE(s0,SPI_control_status) ;preserve status0029f // @142 #797: LOAD(s2,159) ;Read ID instruction30112 // @143 #798: CALL(SPI_FLASH_tx_rx) ;transmit instruction30112 // @144 #799: CALL(SPI_FLASH_tx_rx) ;receive Manufacturer ID01920 // @145 #800: LOAD(s9,s2)30112 // @146 #801: CALL(SPI_FLASH_tx_rx) ;receive Memory Type01820 // @147 #802: LOAD(s8,s2)30112 // @148 #803: CALL(SPI_FLASH_tx_rx) ;receive Memory Capacity01720 // @149 #804: LOAD(s7,s2)0e002 // @14a #805: XOR(s0,SPI_rom_cs) ;deselect (High) FLASH2c008 // @14b #806: OUTPUT(s0,SPI_control_port)2e004 // @14c #807: STORE(s0,SPI_control_status) ;preserve status2a000 // @14d #808: RETURN// #809: ;// #810: ;Read a single byte from the SPI FLASH memory (ST type M25P16)// #811: ;// #812: ;Transmits instruction 03hex followed by a 24-bit address which must be supplied in the// #813: ;register set [s9,s8,s7]. It then transmits a dummy byte to retrieve the memory data// #814: ;which is returned in register s2.// #815: ;// @14e #816: [read_spi_byte]3010e // @14e #816: CALL(SPI_init) ;ensure known state of bus and s0 register0e002 // @14f #817: XOR(s0,SPI_rom_cs) ;select (Low) FLASH2c008 // @150 #818: OUTPUT(s0,SPI_control_port)2e004 // @151 #819: STORE(s0,SPI_control_status) ;preserve status00203 // @152 #820: LOAD(s2,3) ;Read Data Bytes instruction30112 // @153 #821: CALL(SPI_FLASH_tx_rx) ;transmit instruction01290 // @154 #822: LOAD(s2,s9) ;Transmit 24-bit address30112 // @155 #823: CALL(SPI_FLASH_tx_rx)01280 // @156 #824: LOAD(s2,s8)30112 // @157 #825: CALL(SPI_FLASH_tx_rx)01270 // @158 #826: LOAD(s2,s7)30112 // @159 #827: CALL(SPI_FLASH_tx_rx)30112 // @15a #828: CALL(SPI_FLASH_tx_rx) ;read data byte0e002 // @15b #829: XOR(s0,SPI_rom_cs) ;deselect (High) FLASH2c008 // @15c #830: OUTPUT(s0,SPI_control_port)2e004 // @15d #831: STORE(s0,SPI_control_status) ;preserve status2a000 // @15e #832: RETURN// #833: ;// #834: ;// #835: ;Erase a single sector from the SPI FLASH memory (ST type M25P16)// #836: ;// #837: ;Sets the WREN instruction and then transmits instruction D8 hex followed by a 24-bit// #838: ;address which must be supplied in the register set [s9,s8,s7]. The address must be// #839: ;at some location within the sector to be erased. A sector erase can take up to// #840: ;3 seconds to complete. The routine therefore reads the FLASH status and tests// #841: ;the write in progress (WIP) bit to test for completion// #842: ;// @15f #843: [erase_spi_sector]3012a // @15f #843: CALL(set_spi_flash_WREN) ;set write enable mode3010e // @160 #844: CALL(SPI_init) ;ensure known state of bus and s0 register0e002 // @161 #845: XOR(s0,SPI_rom_cs) ;select (Low) FLASH2c008 // @162 #846: OUTPUT(s0,SPI_control_port)2e004 // @163 #847: STORE(s0,SPI_control_status) ;preserve status002d8 // @164 #848: LOAD(s2,D8) ;Sector erase mode30112 // @165 #849: CALL(SPI_FLASH_tx_rx) ;transmit instruction01290 // @166 #850: LOAD(s2,s9) ;Transmit 24-bit address [s9,s8,s7].30112 // @167 #851: CALL(SPI_FLASH_tx_rx)01280 // @168 #852: LOAD(s2,s8)30112 // @169 #853: CALL(SPI_FLASH_tx_rx)01270 // @16a #854: LOAD(s2,s7)30112 // @16b #855: CALL(SPI_FLASH_tx_rx)0e002 // @16c #856: XOR(s0,SPI_rom_cs) ;deselect (High) FLASH2c008 // @16d #857: OUTPUT(s0,SPI_control_port)2e004 // @16e #858: STORE(s0,SPI_control_status) ;preserve status// @16f #859: [sector_erase_wait]3011f // @16f #859: CALL(read_spi_flash_status) ;test WIP bit until finished12201 // @170 #860: TEST(s2,1)3556f // @171 #861: JUMP(NZ,sector_erase_wait)2a000 // @172 #862: RETURN// #863: ;// #864: ;// #865: ;// #866: ;Bulk erase the whole SPI FLASH memory (ST type M25P16)// #867: ;// #868: ;Sets the WREN instruction and then transmits instruction C7 hex.// #869: ;A bulk erase can take up to 40 seconds to complete. The routine therefore reads the// #870: ;FLASH status and tests the write in progress (WIP) bit to test for completion// #871: ;// @173 #872: [bulk_erase_spi]3012a // @173 #872: CALL(set_spi_flash_WREN) ;set write enable mode3010e // @174 #873: CALL(SPI_init) ;ensure known state of bus and s0 register0e002 // @175 #874: XOR(s0,SPI_rom_cs) ;select (Low) FLASH2c008 // @176 #875: OUTPUT(s0,SPI_control_port)2e004 // @177 #876: STORE(s0,SPI_control_status) ;preserve status002c7 // @178 #877: LOAD(s2,C7) ;Sector erase mode30112 // @179 #878: CALL(SPI_FLASH_tx_rx) ;transmit instruction0e002 // @17a #879: XOR(s0,SPI_rom_cs) ;deselect (High) FLASH2c008 // @17b #880: OUTPUT(s0,SPI_control_port)2e004 // @17c #881: STORE(s0,SPI_control_status) ;preserve status// @17d #882: [bulk_erase_wait]3011f // @17d #882: CALL(read_spi_flash_status) ;test WIP bit until finished12201 // @17e #883: TEST(s2,1)3557d // @17f #884: JUMP(NZ,bulk_erase_wait)2a000 // @180 #885: RETURN// #886: ;// #887: ;// #888: ;// #889: ;Open a page for programming.// #890: ;The 24-bit start address to be supplied in the register set [s9,s8,s7].// #891: ;Note that s7=00 hex for normal page boundaries but you could start at any address.// #892: ;Caution : Exceeding s7=FF hex will result in the roll over to 00 hex but without// #893: ;incrementing to the next page.// #894: ;// #895: ;Transmits instruction 02hex followed by the 24-bit start address.// #896: ;It is then ready to transmit data bytes using the s2 register and the SPI_FLASH_tx_rx// #897: ;subroutine. After transmitting bytes, close the page with the close_prog_page_spi// #898: ;routine.// #899: ;// @181 #900: [open_prog_page_spi]3012a // @181 #900: CALL(set_spi_flash_WREN) ;set write enable mode3010e // @182 #901: CALL(SPI_init) ;ensure known state of bus and s0 register0e002 // @183 #902: XOR(s0,SPI_rom_cs) ;select (Low) FLASH2c008 // @184 #903: OUTPUT(s0,SPI_control_port)2e004 // @185 #904: STORE(s0,SPI_control_status) ;preserve status00202 // @186 #905: LOAD(s2,2) ;Page program mode30112 // @187 #906: CALL(SPI_FLASH_tx_rx) ;transmit instruction01290 // @188 #907: LOAD(s2,s9) ;Transmit 24-bit address [s9,s8,s7].30112 // @189 #908: CALL(SPI_FLASH_tx_rx)01280 // @18a #909: LOAD(s2,s8)30112 // @18b #910: CALL(SPI_FLASH_tx_rx)01270 // @18c #911: LOAD(s2,s7)30112 // @18d #912: CALL(SPI_FLASH_tx_rx)2a000 // @18e #913: RETURN// #914: ;// #915: ;// #916: ;This routine completes a page program operation started with// #917: ;open_prog_page_spi and data bytes sent with SPI_FLASH_tx_rx.// #918: ;// #919: ;A page program can take up to 5ms to complete. The routine therefore reads the// #920: ;FLASH status and tests the write in progress (WIP) bit to test for completion// #921: ;// #922: ;// @18f #923: [close_prog_page_spi]06004 // @18f #923: FETCH(s0,SPI_control_status) ;read control status bits0e002 // @190 #924: XOR(s0,SPI_rom_cs) ;deselect (High) FLASH2c008 // @191 #925: OUTPUT(s0,SPI_control_port)2e004 // @192 #926: STORE(s0,SPI_control_status) ;preserve status// @193 #927: [page_prog_wait]3011f // @193 #927: CALL(read_spi_flash_status) ;test WIP bit until finished12201 // @194 #928: TEST(s2,1)35593 // @195 #929: JUMP(NZ,page_prog_wait)2a000 // @196 #930: RETURN// #931: ;// #932: ;**************************************************************************************// #933: ;Software delay routines// #934: ;**************************************************************************************// #935: ;// #936: ;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?