Using the HT46R46 I/O Ports to Implement Half-Duplex SPI Communication D/N: HA0150E Introduction This application explains how to use two I/O lines on the HT46R46 to implement half-duplex SPI communication. SPI Communication Protocol The Master and Slave use their I/O ports to communicate with each other using a simulated SPI protocol, the following is the method used: DIR: SPI communication direction control bit. Setup using the switch which is connected to PA.7. When the switch is set to zero then the master is reading and the slave is writing. When the switch is set high then the master is writing and the slave is reading. INC: When this bit is high and continual transmission is taking place, the address will automatically increase by one. (must remain no temporary use) Address: Address bit (must remain no temporary use) Data: 8-bit data 1
Fig. 1 Master control clock signal SCK, device select CS (consult the above figure /SS condition) and SPI communication baud rate. In this application, each time the key (connected to PD.) is pressed, the master and slave will conduct communication. The data received will be displayed on the PB port display. 2
Hardware Block Diagram Using the HT46R46 I/O Ports to Implement Half-Duplex SPI Communication Master (HOST) Block Diagram Fig. 2 Slave Hardware Block Diagram Fig. 3 3
Hardware Circuit Diagram Fig. 4 4
Software Flowchart Using the HT46R46 I/O Ports to Implement Half-Duplex SPI Communication Master (HOST) Program Flowchart Start MCU Initialise RAM and I/O Port Master Setup CS, CLK, SDO I/O Port as Output, SDI I/O Port as Inputs KEY is Pressed N Y Set DIR bit to Control SPI to READ or WRITE Master to SPI Communication Routine SPI_HOST_TO_SLAVE N F_SPI_OK = 1 Y Y Master READ Data N Master Received Data Output on PB Port Display WRITE_RAM bit Move and Prepare Next Output Bit Fig. 5 5
Slave Program Flowchart Fig. 6 6
SPI_HOST_TO_SLAVE Subroutine Flowchart 7 Fig. 7
SPI_SLAVE_TO_HOST Subroutine 8 Fig. 8
Program Flow Description After Power-on, the master and the slave will conduct their respective initialization procedures. The SPI communication will be initiated by the master. When the slave responds to the master the I/O port will be used to simulate SPI protocol half-duplex communication. In this application each time the key connected to PD.0 is pressed, the master will conduct SPI communication. This is implemented by the master using the device select signal (CS), and the SPI clock signal (CLK), the read/write control bit (SDIR). When the slave receives CS, CLK and SDIR signals it will respond to the master. Each time an SPI communication has ended, the master and the slave will determine if the data is to be displayed on the PB port display. Then the program will return and continue with the next SPI communication. In the SPI serial communication program, the master and the slave both input data on SDI on the rising edge of SCK. Data is outputted on the SDO line on the falling edge of SCK. Each SCK clock period transmits 1-bit of data. This is repeated 8 times to complete an 8-bit data serial transmission. During this time, according to the condition of SDIR, the direction of the communication is setup. If SDIR = 1 then the data communication is from master to slave or if SDIR is = 0 the data communication is from slave to master. This application program I/O timing is shown as follows in Fig. 9: Fig. 9 SPI Communication Flow 9
Program Variables and Communication Setup Description CS: SPI serial communication device select signal (consult Fig. 9 /SS condition). The master and slave CS are connected. The master controls the CS signal, and the slave checks its condition. The CS line is active when low. SCK: SPI communication clock signal (consult Fig. 9 SCK condition). The master and the slave SCK are connected. The master controls the SCK line and controls the serial transmission speed. The slave checks the condition of the SCK signal and responds to the master with SPI communication. SDO: SPI serial communication output signal (consult Fig. 9 MOSI condition). The master and slave output data on SDO on the falling edge of SCK. SDI:SPI serial communication input signal (consult Fig. 9 MISO condition). The master and the slave SCK receive data on SDI on the high going edge of SCK. SDIR: Setup the SPI serial communication data transfer direction. When SDIR is zero, the master will read from the slave and the communication direction is from slave to master. When SDIR is high, the master writes to the slave and the data communication direction is from master to slave. C_BaudRate: The master will setup the SPI serial communication data transmission rate. In this application fsys is 4MHz and the TIMER clock source is fsys/4=1mhz. If it is required to implement a 4800 transmission rate, then 1/4800 = 208us is required to transmit 1-bit of data. Therefore the SCK clock period is also 208us. Then setup the TIMER to have a period of 104us after which the SCK bit can change giving a SCK clock period of 208us. Therefore when C_BaudRate = 104us the transmit rate is 4800. In the same way when C_BaudRate = 52, the SCK clock period is 104us and the transmit rate is 9600. Other transmit rates can be similarly calculated. The above constants are defined in the user_setting.inc file. In the file, SDIR and C_BaudRate are setup in the master. When implementing SPI serial communication, the master SDO signal and the slave SDI signal are connected. The master SDI signal and the slave SDO line are connected. WRITE_RAM: SPI serial communication output data Buffer READ_RAM: SPI serial communication input data Buffer CMD_RAM: SPI serial communication CMD data Buffer Program Function Description INI_RAM: Initialise RAM subroutine SPI_HOST_TO_ SLAVE: SPI serial communication master subroutine SPI_SLAVE_TO_HOST: SPI serial communication slave subroutine SET_BAUDRATE: SPI serial communication transmit frequency - baud rate - setup subroutine 10