Skip to content
Snippets Groups Projects
Select Git revision
  • 404b5cac3b71c91c520f63e5d41a9d45b16d2712
  • master default protected
  • ext-memory
  • feature-develop
4 results

spi.s

Blame
    • MJ's avatar
      d8009948
      Reorganized folders · d8009948
      MJ authored
      NOTE: if simulations are still needed, the protocol-controllers branch should suffice. Otherwise, we just need to re-add sources to Vivado.
      d8009948
      History
      Reorganized folders
      MJ authored
      NOTE: if simulations are still needed, the protocol-controllers branch should suffice. Otherwise, we just need to re-add sources to Vivado.
    spi.s 6.07 KiB
    # This test code is to demonstrate SPI functionality of the
    # Pipelined RV32IMC + Protocol controllers
    # NOTE: The SPI Controller for this project can only send
    # 2 bytes per transaction. Interrupts are also triggered at the end of every transaction.
    # =========================================================
    # Demo setup:
    # ESP8266 <---> CORE <---> Arduino Uno
    # ESP8266: Slave 2
    # Arduino: Slave 3
    # Program flow:
    # 1. CORE sends 2 byte packages to both slaves in ASCII (ES (ESP), AU (Arduino)).
    # 2. If both slaves receive the correct package, they send a reply: (0xAA). 
    #  	 If reply matches expected data, send command to both slaves to turn on their built-in LEDs (EL, AL)
    # 	 Else, send "XX"
    # =========================================================
    # Register usage:
    #		s0 -> SPI BUSY
    #		s1 -> SPI DONE
    #		s2 -> SPI Data Out
    #		sp -> Stack pointer; points to word address 0x3FF
    #		gp -> points to PROTOCOLMEM address (0x400)
    #		a0-a1 -> return values
    # 		a2-a7 -> subroutine arguments
    
    .text
    init:
    	addi sp, x0, 0x3FF			# stack pointer addr
    	c.slli sp, 2
    	addi gp, x0, 0x400			# PROTOCOLMEM addr
    	c.slli gp, 2
    
    spi_setup:
    	# Settings: 100kbps, cpha = 0, cpol = 0 (shift@negedge, sample@posedge), ord = 0 (send MSB first)
    	addi t0, x0, 124			# 100kbps prescale
    	c.slli t0, 8				# shift to prescale field
    	# c.addi t0, 0xc				# ord = 0, cpha=1, cpol=1
    	sw t0, 0x8(x0)				# store to SPI Input Control
    
    main:
    	# First part: CORE sends 2 byte packages to ESP8266 & Arduino
    	# Send to ESP first
    	lui a2, 0x04553				# send "ES"
    	c.srli a2, 12				# shift s.t. LSB is at bit0
    	c.li a3, 2					# select slave 2
    	c.jal spi_write				# call spi_write(a2, a3)
    
    	# Send to Arduino
    	lui a2, 0x04155				# send "AU"
    	c.srli a2, 12				# shift s.t. LSB is at bit0
    	c.li a3, 3					# select slave 3
    	c.jal spi_write				# call spi_write(a2, a3)
    
    	# Second part: read reply from ESP8266 & Arduino & send command to turn on builtin LED
    	# Get ESP reply
    	c.li a2, 2					# select slave 2
    	c.jal spi_read				# call spi_read
    	addi t0, x0, 0xAA			# "0xAA"
    
    	lui a2, 0x0454C				# "EL"
    	beq a0, t0, main1			# check if data matches expected. If yes, skip "XX"
    	lui a2, 0x05858				# "XX"
    	main1:
    	c.srli a2, 12				# shift s.t. LSB at bit0
    	c.li a3, 2					# select slave 2
    	c.jal spi_write				# call spi_write(a2, a3)
    
    	# Get Arduino reply
    	c.li a2, 3					# select slave 3
    	c.jal spi_read				# call spi_read(a2)
    	addi t0, x0, 0xAA			# 0xAA