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

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

Embed Size (px)

Citation preview

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

Lab3 -- Lab 4

Design and implementation details on the way to a valid

SPI-LCD interface driver

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

04/18/23 SPI and LCD , Copyright M. Smith, ECE, University of Calgary, Canada

2 / 26

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)?

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

Final “temperature” program using co-operative scheduler

InitScheduler( ) with adjusted timer interrupt settings AddTask(InitHardware, NO_DELAY, RUN_ONCE) AddTask(MeasureTiming, 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++”

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

Lab 4 looks look InitScheduler( ) AddTask(InitHardware, NO_DELAY, RUN_ONCE) – including SPI and LCD AddTask(MeasureTiming, NO_DELAY, 1); AddTask(CalculateTemperature, QUARTER_SECOND, QUARTER_SECOND) AddTask(DisplayTempertureLED, 3/8 seconds, QUARTER_SECOND) AddTask(SPI_Message_HappyXmasLCD, ONE_SECOND, TWO_SECOND); AddTask(SPI_Message_TemperatureLCD, TWO_SECOND, TWO_SECOND); StartScheduler( ) Loop

• GotoSleepTillInterrupt

• Dispatch Tasks

The SPI_Message handling is all done during Lab. 3

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

Lab 3 tests the SPI interface – must be much simpler

InitScheduler( ) with adjusted timer interrupt settings AddTask(InitHardware_SPI, NO_DELAY, RUN_ONCE) AddTask(SPI_Message_Simple1, 1/10 SECOND, 1 /5 SECOND); (or faster) AddTask(SPI_Message_Simple2, 1/4 SECOND, 1 /5 SECOND); StartScheduler( ) Loop

• GotoSleepTillInterrupt

• Dispatch Tasks

The Messages are very simple – they simply flash the LED lights on the logic lab station is a “christmas tree light” fashion”

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

SPI_Message_Simple1( )SPI_Message_Simple2( )

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_MessageCanBeSent_G = 0;

volatile int pleaseSend_SPI_Message_G = 0;

volatile short int SPI_Message_G[200];

volatile short int SPI_MessageSize_G = 0;

04/18/23 SPI and LCD , Copyright M. Smith, ECE, University of Calgary, Canada

6 / 26

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

SPI_Message_Simple1( ) Racing LEDS 0 to 7 on Logic Lab.

void Task_SPI_Simple1(void) {static short int MyMessage[ ] = {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 interfaceif (SPI_MessageCanBeSent_G == 0) return; // Somebody else using SPI

// This step only works with a “co-operative scheduler” where only 1 task can access memory

SPI_MessageCanBeSent_G = 0; // Stop anybody else sending a message

SPI_MessageSize_G = 8;for (int count = 0; count < SPI_MessageSize_G; count++)

SPI_Message[count] = MyMessage[count]; // Copy message into shared buffer

// Tell SPI controller that there is a message to sendpleaseSendSPIMessage = 1;

}

04/18/23 SPI and LCD , Copyright M. Smith, ECE, University of Calgary, Canada

7 / 26

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

SPI Controller is another two task

Controller can’t send “all the message” along the SPI interface using wait loop• LCD messages can take 400 ms or more

Controller sends one character using a WriteOneSPIChar_Task( ) and goes back to sleep

04/18/23 SPI and LCD , Copyright M. Smith, ECE, University of Calgary, Canada

8 / 26

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

void Task_SPI_controller(void)volatile short int next_SPIinfo = 0;

volatile short int nextValueReady = 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 (pleaseSendSPIMessage == 0) return;

if (SPI_MessageSize_G == counter) { pleaseSendSPIMessage = 0; SPI_MessageCanBeSent_G = 1; counter = 0; return);

if (nextValueReady != 0) return // SPI interface not ready – still transmitting last value

next_SPIinfo = SPI_Message_G[counter++];

nextValueReady = 1;

}

04/18/23 SPI and LCD , Copyright M. Smith, ECE, University of Calgary, Canada

9 / 26

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

Lab 3 Now looks like this InitScheduler( ) AddTask(InitHardware_SPI, NO_DELAY, RUN_ONCE) AddTask(SPI_Controller, SHORT_DELAY, RUN_OFTEN) AddTask(SPI_Message_Simple1, 1/10 SECOND, 1 /5 SECOND); (or faster) AddTask(SPI_Message_Simple2, 1/4 SECOND, 1 /5 SECOND); AddTask(WriteSPIInfo, SHORT_DELAY, RUN_OFTEN); StartScheduler( )

And we have NO idea of how to write WriteSPIInfo( ) or InitHardware_SPI( ) as these interface to the SPI hardware directly.

Need to learn about the SPI interface

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

04/18/23 SPI and LCD , Copyright M. Smith, ECE, University of Calgary, Canada

11 / 26

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 -- http://www.totalphase.com/docs/articles/article03/• LCD information -- http://home.iae.nl/users/pouweha/lcd/lcd.shtml

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

04/18/23 SPI and LCD , Copyright M. Smith, ECE, University of Calgary, Canada

12 / 26

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

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

04/18/23 SPI and LCD , Copyright M. Smith, ECE, University of Calgary, Canada

13 / 26

Concept of Master and Slave

Master• The component

that initiates the transfer

• The component that controls the transfer

Slave• The component

that responds to the transfer

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

04/18/23 SPI and LCD , Copyright M. Smith, ECE, University of Calgary, Canada

14 / 26

Master / Slave conceptSlave 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

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

04/18/23 SPI and LCD , Copyright M. Smith, ECE, University of Calgary, Canada

15 / 26

Master / Slave conceptMaster 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 FinalMOSI --MASTER OUT – SLAVE IN

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

04/18/23 SPI and LCD , Copyright M. Smith, ECE, University of Calgary, Canada

16 / 26

Master / Slave conceptSlave 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 upMISO --MASTER IN – SLAVE OUT

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

04/18/23 SPI and LCD , Copyright M. Smith, ECE, University of Calgary, Canada

17 / 26

Lab. 3 interfaceSPI from Blackfin master (MOSI, MISO, CLK, PF5

LINES TO LOGIC LAB LEDOR CAR CONTROL

TRANSMITTEROR LAB 4 LCD SCREEN

SPI to interface (slave)(MOSI, MISO, CLK, slave select )

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

04/18/23 SPI and LCD , Copyright M. Smith, ECE, University of Calgary, Canada

18 / 26

Transmit 16 bits with THIS format over the MOSI line

DB7, DB6, ………DB1, DB0

These signals come out on theCJ7 and CJ8 pins and goto the logic lab LEDs

Leading “high-bit”MSB – Most significant bitcomes out of Blackfinmaster first and isreceived 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

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

04/18/23 SPI and LCD , Copyright M. Smith, ECE, University of Calgary, Canada

19 / 26

Lab. 3 ideas

SPI_TDBR Blackfin Processor SPI_RXBR

CJ7 / CJ8 output lines to LOGIC LAB LEDs

SWITCHES (LOGIC LAB)

SLAVE INPUT INTERFACE SLAVE OUTPUT INTERFACE

MOSI MISO

SLAVE SELECTPF5 used (PF0 to PF7)

DATACONTROL

SPICLOCK

LOADSlave toLCD

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

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 nextValueReady;

void WriteSPIInfo (void) {

if (nextValueReady == 0) return; Nothing to send

*pSPI_TDBR = next_SPIinfo;

WaitAwhileForSPIToTransmit( ) // how long is this ? // Is there a hardware flag we can read?

nextValueReady = 0; // Tell SPI controller to send again?

}

04/18/23 SPI and LCD , Copyright M. Smith, ECE, University of Calgary, Canada

20 / 26

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

04/18/23 SPI and LCD , Copyright M. Smith, ECE, University of Calgary, Canada

21 / 26

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

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

04/18/23 SPI and LCD , Copyright M. Smith, ECE, University of Calgary, Canada

22 / 26

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)?