Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lab3 -- Lab 4 Design and implementation details on the way to a valid SPI-LCD interface driver.

Similar presentations


Presentation on theme: "Lab3 -- Lab 4 Design and implementation details on the way to a valid SPI-LCD interface driver."— Presentation transcript:

1 Lab3 -- Lab 4 Design and implementation details on the way to a valid SPI-LCD interface driver

2 SPI and LCD , Copyright M. Smith, ECE, University of Calgary, Canada
Lab. 4 Use SPI interface, to cause the words “Happy 511 Christmas” to appear on the LCD screen some times and the temperature (Lab. 3) other times 5/18/2019 SPI and LCD , Copyright M. Smith, ECE, University of Calgary, Canada

3 SPI and LCD , Copyright M. Smith, ECE, University of Calgary, Canada
Lab. 4 Blackfin processor Running tasks using TTCOS operating system SPI-interface Optional car control 4 wires 11 wires SPI-bus External parallel I/O 4 wires SPI-interface 5/18/2019 SPI and LCD , Copyright M. Smith, ECE, University of Calgary, Canada

4 To cause the LCD to write the letter ‘A’ on its screen
LCD COMMANDS RS R/W DATA DB7 to DB0 WriteLetter(value) 1 value WriteLetter(‘a’) 0x61 That means we must send the following “voltage” signals to the LCD pins RS R/W E DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB Wait( 40 us) // EnableStrobe Wait( 40 us) 5/18/2019 SPI and LCD , Copyright M. Smith, ECE, University of Calgary, Canada

5 Lots of stuff has to happen in the correct order
Putting data into the Blackfin SPI_TDBR register causes the data to be transmitted over the MOSI line  However the external device will ACCEPT the command as the slave select line PF5 line is AUTOMATICALLY SET LOW by the Blackfin before the transmission is started.  When transmission is ended (16-bits sent) then the slave select line PF5 line is AUTOMATICALLY SET HIGH by the Blackfin  As PF5 line goes high the data sent by the Blackfin over the MOSI line to the special interface is transferred to the LCD  Everything in the garden is wonderful – most stuff happening automatically -- we just have to wire it correctly !!!!!!!!!!!!!! ALL? we have to do is make it happen!  5/18/2019 SPI and LCD , Copyright M. Smith, ECE, University of Calgary, Canada

6 SPI and LCD , Copyright M. Smith, ECE, University of Calgary, Canada
Blackfin must be coded to transmit 16 bits with THIS format over the MOSI line DB7, DB6, ………DB1, DB0 E – Enable / Strobe 1  0 – When this line goes from high to the low, then the command is send to (latched into) LCD To make LCD respond to command 0x4F0 Then Blackfin must transmit 0x5F0 ( E High ) 0x4F0 ( E low ) 0x5F0 ( E high ) RS 1 – LCD data 0 – LCD instruction R/W 1 – Read from LCD 0 – Write to LCD 5/18/2019 SPI and LCD , Copyright M. Smith, ECE, University of Calgary, Canada

7 SPI and LCD , Copyright M. Smith, ECE, University of Calgary, Canada
What we need to know 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)? 5/18/2019 SPI and LCD , Copyright M. Smith, ECE, University of Calgary, Canada

8 Final “temperature” program using co-operative scheduler (Lab. 3)
InitScheduler( ) -- Lab 3 looks something like this AddTask(InitHardware, NO_DELAY, RUN_ONCE) AddTask(MeasureTemperatureTiming, NO_DELAY, 1); AddTask(CalculateTemperature, QUARTER_SECOND, QUARTER_SECOND) AddTask(DisplayTempertureLED, 3/8 seconds, QUARTER_SECOND) AddTask(FlashLED6, NO_DELAY, EIGHTH_SECOND StartScheduler( ) Loop GotoSleepTillInterrupt Dispatch Tasks Very easy to be able to add additional tasks and understand changes of system performance. All tasks written in “C and C++”

9 Lab 4 looks look – three new tasks
InitScheduler( ) AddTask(InitHardware, NO_DELAY, RUN_ONCE) AddTask(Init_SPIandLCD, NO_DELAY, RUN_ONCE) AddTask(MeasureTemperatureTiming, NO_DELAY, 1); AddTask(CalculateTemperature, QUARTER_SECOND, QUARTER_SECOND) AddTask(DisplayTempertureLED, 3/8 seconds, QUARTER_SECOND) AddTask(SPI_Message_HappyXmasLCD, 4.5_SECOND, TWO_SECOND); AddTask(SPI_Message_TemperatureLCD, 5.5_SECOND, TWO_SECOND); StartScheduler( ) Loop GotoSleepTillInterrupt Dispatch Tasks -- DO CODE REVIEW

10 Code reminder – using sprintf( ) Xmas message and temperature
I want to put the temperature (as a number) into a string that gets transmitted over SPI – how would I do that using “C”? volatile bool SPI_In_USE; // ‘SEMAPHORE’ (lock) volatile short int sendMyMessage; // ‘MESSAGE’ (simple) volatile char SPImessage[80]; // ‘MESSAGE’ body volatile float temperature; // Calculated as a Lab. 3 Task // Know terms in red for final #include <stdio.h> DO CODE REVIEW void SPI_Message_TemperatureLCD(void) { if (SPI_In_Use == true) return; // Must wait till SPI interface is not in use by another task // SPI_In_Use must be false – therefore we can change it and use SPI // We want to use the SPI interface – block other tasks from using it SPI_In_Use = true; sprintf(SPImessage, “Temperature %f4.2 C”, temperature); sendMyMessage = DATA; // DEFINE DATA AS 1 SOMEWHERE } 5/18/2019 SPI and LCD , Copyright M. Smith, ECE, University of Calgary, Canada

11 Difference between co-operative and pre-emptive schedulers
Race condition – SPI_In_Use flag might be changed by an interrupt from another device just as we try to change it void SPI_Message_TemperatureLCD(void) { if (SPI_In_Use == true) return; // Must wait till SPI interface is not in use by another task // SPI_In_Use must be false – therefore we can change it and use SPI // BUT Interrupt occurs “right at this moment” -- the interrupting device see SPI_In_USE is false so it changes it // and now the system has a “hardware defect present” – two things trying to use SPI // We want to use the SPI interface – block other tasks from using it NOT VALID NOW SPI_In_Use = true; sprintf(SPImessage, “Temperature %f4.2 C”, temperature); sendMyMessage = DATA; // DEFINE DATA AS 1 SOMEWHERE } // THIS SOLUTION IS NOT NEEDED IN CO-OPERATIVE SCHEDULER AS ONLY 1 TASK RUNS 5/18/2019 SPI and LCD , Copyright M. Smith, ECE, University of Calgary, Canada

12 Fix to race condition needed for interrupt driven schedulers
Race condition – SPI_In_Use might be changed by an interrupt from another device just as we try to change it void SPI_Message_TemperatureLCD(void) { STOP_Interrupts( ); if (SPI_In_Use == false) { // Must wait till SPI interface is not in use by another task // Interrupts still stopped – so can change SPI_In_Use without RACE happening // We want to use the SPI interface – block other tasks from using it SPI_In_Use = true; sprintf(SPImessage, “Temperature %f4.2 C”, temperature); sendMyMessage = DATA; // DEFINE DATA AS 1 SOMEWHERE } START_Interrupts( ); // Restart interrupts // THIS SOLUTION IS NOT NEEDED IN CO-OPERATIVE SCHEDULER AS ONLY 1 TASK RUNS 5/18/2019 SPI and LCD , Copyright M. Smith, ECE, University of Calgary, Canada

13 Quick Test for TemperatureString task
Test( ) { strcpy(SPImessage, “GARBAGE”); CHECK_ARRAY_EQUAL(SPImessage, “GARBAGE”, length(“GARBAGE”)); temperature = 23.5; SPI_IN_USE = 1; SPI_Message_TemperatureLCD( ); // Check can’t change the SPI buffer SPI_IN_USE = 0; SPI_Message_TemperatureLCD( ); // Check can change the SPI buffer CHECK_ARRAY_EQUAL(SPImessage, “Temperature 23.5C”, length(“Temperature 23.5C”)); -- DO CODE REVIEW 5/18/2019 SPI and LCD , Copyright M. Smith, ECE, University of Calgary, Canada

14 All we have left to do is Init_SPIandLCD( ) Task
Use profound procrastination programming AddTask(InitSPI, NO_DELAY, RUN_ONCE); AddTask(SPI_MessageHandler, NO_DELAY, ½ SECOND); AddTask(InitLCDSreen, ONE_SECOND_DELAY, RUN+ONCE) -- DO CODE REVIEW 5/18/2019 SPI and LCD , Copyright M. Smith, ECE, University of Calgary, Canada

15 Turning the commands into an array of commands
Prelab 4 showed what commands need to be sent to LCD screen Express the commands (bits 0 to 7 as “chars” and end with char value 0x00 (end of string) Suppose commands were “0x30 0x30, 0x22” for bits 7 to 0 then #define EOS 0x00 // C++ end of string character char LCDcommandString[ ] = {0x30, 0x30, 0x22, EOS}; 5/18/2019 SPI and LCD , Copyright M. Smith, ECE, University of Calgary, Canada

16 Init LCD Screen ( ) – This is essentially “send a special message”
bool SPI_In_USE; // ‘SEMAPHORE’ (lock) short int sendMyMessage; // ‘MESSAGE’ (simple) char SPImessage[80]; // ‘MESSAGE’ body char LCDcommandString[ ] = {0x30, 0x30, 0x22, 0x00 /* end of string character */}; // LOOK AT LAB. 4 PRELAB REFERENCE SHEET #include <stdio.h> void InitLCDSreen (void) { if (SPI_In_Use == true) return; // Must wait till SPI interface is not in use by another task // SPI_In_Use is false // We want to use the SPI interface – block other tasks from using it SPI_In_Use = true; strcpy(SPImessage, LCDcommandString ); sendMyMessage = INSTRUCTION; // DEFINE INSTRUCTION AS 2 SOMEWHERE } -- DO CODE REVIEW 5/18/2019 SPI and LCD , Copyright M. Smith, ECE, University of Calgary, Canada

17 We now need to do the message handler task -- DO CODE REVIEW
enum {NO_MESSAGE, DATA, INSTRUCTION}; // Be able to explain “enum” for final void SPI_MessageHandler(void) { if ( sendMyMessage == NO_MESSAGE) return; // Message to send – start up the sending task numberCharactersToSend = strlen(SPIMessage); sendMessageTaskNumber = AddTask(SendMessageSPI_LCD, NO_DELAY, AS_FAST_AS_POSSIBLE); // Start with as FAST_AS_POSSIBLE with a slow value -- second / 20; } -- DO CODE REVIEW 5/18/2019 SPI and LCD , Copyright M. Smith, ECE, University of Calgary, Canada

18 SendMessageSPI_LCD( )
{ static int whichState = switch8HIGH; static int messageElement = 0; // Kill this task if all the elements in the message have been sent if (numberCharactersToSend == 0) { DeleteTask(sendMessageTaskNumber); SPIinUse = false; return; } -- DO CODE REVIEW 5/18/2019 SPI and LCD , Copyright M. Smith, ECE, University of Calgary, Canada

19 To cause the LCD to write the letter ‘A’ on its screen
LCD COMMANDS RS R/W DATA DB7 to DB0 WriteLetter(value) 1 value WriteLetter(‘a’) 0x61 That means we must send the following “voltage” signals to the LCD pins RS R/W E DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB STATE 1 STATE 2 EnableStrobe STATE 3 5/18/2019 SPI and LCD , Copyright M. Smith, ECE, University of Calgary, Canada

20 State 1 – send character + SW8 high + SW10 high IF data
#define SW8_high 0x DO CODE REVIEW #define SW8_low 0x DO CODE REVIEW #define SW10_High 0x DO CODE REVIEW #define SW10_Low 0x DO CODE REVIEW switch (whichState) { case STATE1: -- DO CODE REVIEW // get the character to send and convert to short int value (SPI using 16-bit values) unsigned short int SPIvalue = SPImessage(messageElement); // Next char to send // set switch 8 high SPIvalue = SPIvalue | SW8_high; -- DO CODE REVIEW // Do we set switch 10 high if (sendMyMessage == IS_INSTRUCTION) -- DO CODE REVIEW SPIvalue = SPIvalue | SW10_high; -- DO CODE REVIEW if (!SPIReady( ) ) return; // The SPI device might not be ready if Blackfin too fast WriteSPI(SPIvalue); nextState = STATE2 break; 5/18/2019 SPI and LCD , Copyright M. Smith, ECE, University of Calgary, Canada

21 You can finish off the other states present in SendMessageSPI_LCD( )
LCD COMMANDS RS SW10 R/W SW8 DATA DB7 to DB0 WriteLetter(value) 1 value WriteLetter(‘a’) 0x61 That means we must send the following “voltage” signals to the LCD pins RS R/W E DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB STATE 1 STATE 2 EnableStrobe STATE 3 5/18/2019 SPI and LCD , Copyright M. Smith, ECE, University of Calgary, Canada

22 Things we are missing All relate to SPI HARDWARE
bool SPI_Ready(void) void WriteSPI(unsigned short SPIvalue); void InitSPI(void); We are going to have to read the reference sheet 5/18/2019 SPI and LCD , Copyright M. Smith, ECE, University of Calgary, Canada

23 SPI and LCD , Copyright M. Smith, ECE, University of Calgary, Canada
What is SPI 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. DUPLEX MEANS – BOTH DIRECTIONS AT ONCE Master can receive from the slave at the same time as the master sends to the slave Is essentially a 4 wire high speed system, with speeds up to many MHz Blackfin (p10-8) has a register SPI_BAUD where they talk about transfer rates of 25 MHz. Information is précised from SPI information -- LCD information -- 5/18/2019 SPI and LCD , Copyright M. Smith, ECE, University of Calgary, Canada

24 Advantages and drawbacks
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 megaHertz. 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 5/18/2019 SPI and LCD , Copyright M. Smith, ECE, University of Calgary, Canada

25 Concept of Master and Slave
The component that initiates the transfer The component that controls the transfer Slave The component that responds to the transfer 5/18/2019 SPI and LCD , Copyright M. Smith, ECE, University of Calgary, Canada

26 Master / Slave concept Slave Select (Chip Select)
Master sends out active low chip select signal SS1, then slave 1 responds Master sends out active low chip select signal SS2, then slave 2 responds FOR SAFETY – SELECT SIGNAL IS “ACTIVE LOW” NOT “ACTIVE HIGH” IF LINE BREAKS – FLOATS HIGH – TURNS SLAVE DEVICE OFF 5/18/2019 SPI and LCD , Copyright M. Smith, ECE, University of Calgary, Canada

27 Master / Slave concept Master to Slave data movement
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 5/18/2019 SPI and LCD , Copyright M. Smith, ECE, University of Calgary, Canada

28 Master / Slave concept Slave to Master data movement
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 Don’t get master slave mixed up MISO --MASTER IN – SLAVE OUT 5/18/2019 SPI and LCD , Copyright M. Smith, ECE, University of Calgary, Canada

29 Lab. 4 interface SPI from Blackfin master (MOSI, MISO, CLK, PF5
SPI to interface (slave) (MOSI, MISO, CLK, slave select ) LINES TO LOGIC LAB LED OR CAR CONTROL TRANSMITTER OR LAB 4 LCD SCREEN 5/18/2019 SPI and LCD , Copyright M. Smith, ECE, University of Calgary, Canada

30 Transmit 16 bits with THIS format over the MOSI line
DB7, DB6, ………DB1, DB0 These signals come out on the CJ7 and CJ8 pins and go to the logic lab LEDs Leading “high-bit” MSB – Most significant bit comes out of Blackfin master first and is received by slave first 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 5/18/2019 SPI and LCD , Copyright M. Smith, ECE, University of Calgary, Canada

31 Lab. 4 ideas SPI_TDBR Blackfin Processor SPI_RXBR
SLAVE SELECT PF5 used (PF0 to PF7) SPI CLOCK MOSI MISO SLAVE OUTPUT INTERFACE SLAVE INPUT INTERFACE LOAD Slave to LCD DATA CONTROL SWITCHES (LOGIC LAB) CJ7 / CJ8 output lines to LOGIC LAB LEDs 5/18/2019 SPI and LCD , Copyright M. Smith, ECE, University of Calgary, Canada

32 Questions still unanswered
How do we configure the SPI interface inside the Blackfin? How do we activate the chip-select line – PF5? Does activating the PF5 line as SPI output control mean we have to change all the SetupPF8to11ASM( ) 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 5/18/2019 SPI and LCD , Copyright M. Smith, ECE, University of Calgary, Canada

33 SPI and LCD , Copyright M. Smith, ECE, University of Calgary, Canada
What we need to know 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)? 5/18/2019 SPI and LCD , Copyright M. Smith, ECE, University of Calgary, Canada


Download ppt "Lab3 -- Lab 4 Design and implementation details on the way to a valid SPI-LCD interface driver."

Similar presentations


Ads by Google