Lab 3 Lab 4 Design and implementation details
Lab 3 -- Lab 4 Design and implementation details on the way to a valid SPI-LCD interface driver
What we need to know l l What tasks are needed for Lab. 3 and Lab. 4 (Fairly straightforward to answer – about 30 lines of code) What is “SPI hardware” and why to do you want to use it? What is the SPI “master slave” relationship? How do you send commands from the Blackfin to a SPI slave and control Logic Lab LED or Car control or LCD (Lab. 4)? 2/24/2021 SPI and LCD , Copyright M. Smith, ECE, University of Calgary, Canada 2 / 26
Final “temperature” program using co-operative scheduler l l l l l Init. Scheduler( ) with adjusted timer interrupt settings Add. Task(Init. Hardware, NO_DELAY, RUN_ONCE) Add. Task(Measure. Timing, NO_DELAY, 1); Add. Task(Calculate. Temperature, QUARTER_SECOND) Add. Task(Display. Temperture. LED, 3/8 seconds, QUARTER_SECOND) Add. Task(Flash. LED 6, NO_DELAY, EIGHTH_SECOND Start. Scheduler( ) Loop • • Goto. Sleep. Till. Interrupt Dispatch Tasks Very easy to be able to additional tasks and understand changes of system performance. All tasks written in “C and C++”
Lab 4 looks look l l l l l Init. Scheduler( ) Add. Task(Init. Hardware, NO_DELAY, RUN_ONCE) – including SPI and LCD Add. Task(Measure. Timing, NO_DELAY, 1); Add. Task(Calculate. Temperature, QUARTER_SECOND) Add. Task(Display. Temperture. LED, 3/8 seconds, QUARTER_SECOND) Add. Task(SPI_Message_Happy. Xmas. LCD, ONE_SECOND, TWO_SECOND); Add. Task(SPI_Message_Temperature. LCD, TWO_SECOND); Start. Scheduler( ) Loop • • Goto. Sleep. Till. Interrupt Dispatch Tasks The SPI_Message handling is all done during Lab. 3
Lab 3 tests the SPI interface – must be much simpler l l l l Init. Scheduler( ) with adjusted timer interrupt settings Add. Task(Init. Hardware_SPI, NO_DELAY, RUN_ONCE) Add. Task(SPI_Message_Simple 1, 1/10 SECOND, 1 /5 SECOND); (or faster) Add. Task(SPI_Message_Simple 2, 1/4 SECOND, 1 /5 SECOND); Start. Scheduler( ) Loop • • Goto. Sleep. Till. Interrupt Dispatch Tasks The Messages are very simple – they simply flash the LED lights on the logic lab station is a “christmas tree light” fashion”
SPI_Message_Simple 1( ) SPI_Message_Simple 2( ) l l Both these tasks want to use SPI resource to send a message – conflict Fix by having each task “ask” the SPI_Controller if they can send a message – done by “semaphores” (also called “flags”) and “shared buffers” With a co-operative scheduler, the tasks execute one at a time and “only” one task can be changing these “shared flags and buffers” – No race conditions occur (two tasks trying to change the same variable) volatile int SPI_Message. Can. Be. Sent_G = 0; volatile int please. Send_SPI_Message_G = 0; volatile short int SPI_Message_G[200]; volatile short int SPI_Message. Size_G = 0; 2/24/2021 SPI and LCD , Copyright M. Smith, ECE, University of Calgary, Canada 6 / 26
SPI_Message_Simple 1( ) Racing LEDS 0 to 7 on Logic Lab. void Task_SPI_Simple 1(void) { static short int My. Message[ ] = {0, 1, 3, 7, 15, 31, 63, 127}; // no lights, one light, two lights, three lights etc -- your choice // Ask SPI controller if anybody else using SPI interface if (SPI_Message. Can. Be. Sent_G == 0) return; // Somebody else using SPI // This step only works with a “co-operative scheduler” where only 1 task can access memory SPI_Message. Can. Be. Sent_G = 0; // Stop anybody else sending a message SPI_Message. Size_G = 8; for (int count = 0; count < SPI_Message. Size_G; count++) SPI_Message[count] = My. Message[count]; // Copy message into shared buffer // Tell SPI controller that there is a message to send please. Send. SPIMessage = 1; } 2/24/2021 SPI and LCD , Copyright M. Smith, ECE, University of Calgary, Canada 7 / 26
SPI Controller is another two task l Controller can’t send “all the message” along the SPI interface using wait loop • LCD messages can take 400 ms or more l Controller sends one character using a Write. One. SPIChar_Task( ) and goes back to sleep 2/24/2021 SPI and LCD , Copyright M. Smith, ECE, University of Calgary, Canada 8 / 26
void Task_SPI_controller(void) volatile short int next_SPIinfo = 0; volatile short int next. Value. Ready = 0; // If 1, waiting for SPI to transmit void Task_SPI_Controller(void) { static counter = 0; // Count how many parts of message sent // No messages to send or last message already sent if (please. Send. SPIMessage == 0) return; if (SPI_Message. Size_G == counter) { please. Send. SPIMessage = 0; SPI_Message. Can. Be. Sent_G = 1; counter = 0; return); if (next. Value. Ready != 0) return // SPI interface not ready – still transmitting last value next_SPIinfo = SPI_Message_G[counter++]; next. Value. Ready = 1; } 2/24/2021 SPI and LCD , Copyright M. Smith, ECE, University of Calgary, Canada 9 / 26
Lab 3 Now looks like this l l l l Init. Scheduler( ) Add. Task(Init. Hardware_SPI, NO_DELAY, RUN_ONCE) Add. Task(SPI_Controller, SHORT_DELAY, RUN_OFTEN) Add. Task(SPI_Message_Simple 1, 1/10 SECOND, 1 /5 SECOND); (or faster) Add. Task(SPI_Message_Simple 2, 1/4 SECOND, 1 /5 SECOND); Add. Task(Write. SPIInfo, SHORT_DELAY, RUN_OFTEN); Start. Scheduler( ) l And we have NO idea of how to write Write. SPIInfo( ) or Init. Hardware_SPI( ) as these interface to the SPI hardware directly. l Need to learn about the SPI interface
What is SPI l l Serial Peripheral Interface – an industry standard SPI is a serial communication bus developed by Motorola. It is a full-duplex protocol that functions on a master-slave paradigm that is ideally suited to data stream application. • • l Is essentially a 4 wire high speed system, with speeds up to many MHz • l DUPLEX MEANS – BOTH DIRECTIONS AT ONCE Master can receive from the slave at the same time as the master sends to the slave Blackfin (p 10 -8) has a register SPI_BAUD where they talk about transfer rates of 25 MHz. Information is précised from • • SPI information -- http: //www. totalphase. com/docs/article 03/ LCD information -- http: //home. iae. nl/users/pouweha/lcd. shtml 2/24/2021 SPI and LCD , Copyright M. Smith, ECE, University of Calgary, Canada 11 / 26
Advantages and drawbacks l l l SPI is a very simple communication protocol. • It does not have a specific high-level protocol which means that there is almost no overhead. Data can be shifted at very high rates in full duplex mode • This makes it very simple and efficient in a single master single slave scenario. The exchange itself has no pre-defined protocol. This makes it ideal for data-streaming applications. Data can be transferred at high speed, often into the range of the tens of mega. Hertz. The flipside is that there is no acknowledgment, no flow control, and the master may not even be aware of the slave's presence / or absence. • You could do “some” handshaking via software 2/24/2021 SPI and LCD , Copyright M. Smith, ECE, University of Calgary, Canada 12 / 26
Concept of Master and Slave l Master • • l The component that initiates the transfer The component that controls the transfer Slave • The component that responds to the transfer 2/24/2021 SPI and LCD , Copyright M. Smith, ECE, University of Calgary, Canada 13 / 26
Master / Slave concept Slave Select (Chip Select) l l Master sends out active low chip select signal SS 1, then slave 1 responds Master sends out active low chip select signal SS 2, then slave 2 responds FOR SAFETY – SELECT SIGNAL IS “ACTIVE LOW” NOT “ACTIVE HIGH” IF LINE BREAKS – FLOATS HIGH – TURNS SLAVE DEVICE OFF 2/24/2021 SPI and LCD , Copyright M. Smith, ECE, University of Calgary, Canada 14 / 26
Master / Slave concept Master to Slave data movement l l l Master sends out information to slave along MOSI wire Slave receives information from the master along MOSI wire Information (bits) is clocked by SCLK signal. • 1 -bit, 1 clock tick Learn the terms for Quiz 3 and Final MOSI --MASTER OUT – SLAVE IN 2/24/2021 SPI and LCD , Copyright M. Smith, ECE, University of Calgary, Canada 15 / 26
Master / Slave concept Slave to Master data movement l l l Master receives information from slave along MISO wire Slave sends information to the master along MISO wire Information (bits) is clocked by SCLK signal. • 1 -bit, 1 clock tick 2/24/2021 Don’t get master slave mixed up MISO --MASTER IN – SLAVE OUT SPI and LCD , Copyright M. Smith, ECE, University of Calgary, Canada 16 / 26
Lab. 3 interface SPI from Blackfin master (MOSI, MISO, CLK, PF 5 SPI to interface (slave) (MOSI, MISO, CLK, slave select ) LINES TO LOGIC LAB LED OR CAR CONTROL TRANSMITTER OR LAB 4 LCD SCREEN 2/24/2021 SPI and LCD , Copyright M. Smith, ECE, University of Calgary, Canada 17 / 26
Transmit 16 bits with THIS format over the MOSI line DB 7, DB 6, ………DB 1, DB 0 Leading “high-bit” MSB – Most significant bit comes out of Blackfin master first and is received by slave first These signals come out on the CJ 7 and CJ 8 pins and go to the logic lab LEDs Master and slave must agree (before hand – part of initial design) Does the MSB (bit 15) or LSB (bit 0) get transmitted first along the MOSI line 2/24/2021 SPI and LCD , Copyright M. Smith, ECE, University of Calgary, Canada 18 / 26
Lab. 3 ideas SPI_TDBR Blackfin Processor SLAVE SELECT PF 5 used (PF 0 to PF 7) MOSI SPI_RXBR SPI CLOCK MISO SLAVE OUTPUT INTERFACE SLAVE INPUT INTERFACE LOAD CONTROL Slave to LCD DATA CJ 7 / CJ 8 output lines to LOGIC LAB LEDs 2/24/2021 SWITCHES (LOGIC LAB) SPI and LCD , Copyright M. Smith, ECE, University of Calgary, Canada 19 / 26
We know that any value written into SPI transmit data buffer register gets transmitted immediately extern volatile short int next_SPIinfo; extern volatile short int next. Value. Ready; void Write. SPIInfo (void) { if (next. Value. Ready == 0) return; Nothing to send *p. SPI_TDBR = next_SPIinfo; Wait. Awhile. For. SPITo. Transmit( ) // how long is this ? // Is there a hardware flag we can read? next. Value. Ready = 0; // Tell SPI controller to send again? } 2/24/2021 SPI and LCD , Copyright M. Smith, ECE, University of Calgary, Canada 20 / 26
Questions still unanswered l l l How do we configure the SPI interface inside the Blackfin? How do we activate the chip-select line – PF 5? Does activating the PF 5 line as SPI output control mean we have to change all the Setup. PF 8 to 11 ASM( ) and other routines? When do we activate the chip-select line, and how long for? What happens when there is not a new value in the SPI transmit buffer – what does the SPI interface do – it can’t do nothing – does it start transmitting zeros (which would turn out all the LEDs we just turned on 2/24/2021 SPI and LCD , Copyright M. Smith, ECE, University of Calgary, Canada 21 / 26
What we need to know l l What tasks are needed for Lab. 3 and Lab. 4 (Fairly straightforward to answer – about 30 lines of code) What is “SPI hardware” and why to do you want to use it? What is the SPI “master slave” relationship? How do you send commands from the Blackfin to a SPI slave and control Logic Lab LED or Car control or LCD (Lab. 4)? 2/24/2021 SPI and LCD , Copyright M. Smith, ECE, University of Calgary, Canada 22 / 26
- Slides: 22