Problem using #option with SPI module

Coding and general discussion relating to the compiler

Moderators: David Barker, Jerry Messina

Post Reply
blackcattech
Posts: 113
Joined: Mon Jan 11, 2010 10:39 pm
Location: Chesterfield

Problem using #option with SPI module

Post by blackcattech » Tue Jul 30, 2013 11:02 am

I'm trying to write a driver for the SSD1305 OLED chip which uses the hardware SPI module. I've got most of it working but I am trying to add the ability to set the SPI clock as an option from the main program.

I am using the following (not together, but I don't want to paste my entire program)

Code: Select all

#option OLED_CLOCK = spiOscDiv16 
Const O_CLOCK = OLED_CLOCK

Code: Select all

SetAsMaster(O_CLOCK)
However, this throws an 'Incompatible Parameter Types' error on the SetAsMaster call. I can use SetAsMaster(spiOscDiv16) no problem. i've tried adding as byte to the Const definition, no change.

Any idea what I'm doing wrong?

blackcattech
Posts: 113
Joined: Mon Jan 11, 2010 10:39 pm
Location: Chesterfield

Post by blackcattech » Tue Jul 30, 2013 11:07 am

Oh, and a supplementary question. I include the relevant SPI module in my SSD1305 module based on an #option.

In my module code, can I safely call functions in the SPI library without the SPI. or SPI2. prefix? In other words, if a module is included in another module, is it somehow made private?

I guess what I'm asking is do I need to go through wherever I make calls to the SPI library and add conditional compile options based on which module is included?

User avatar
David Barker
Swordfish Developer
Posts: 1214
Joined: Tue Oct 03, 2006 7:01 pm
Location: Saltburn by the Sea, UK
Contact:

Post by David Barker » Tue Jul 30, 2013 1:27 pm

spiOscDiv16 is a constant declared in the spi module. You cannot pass a value from the compiler to pre-processor! But you can do it the other way around (which makes sense, as pre-processing occurs before compilation)

What you are doing is declaring a new string type called "spiOscDiv16". Try this code to see what I mean:

Code: Select all

Include "spi.bas"
include "usart.bas"

#option OLED_CLOCK = spiOscDiv16
Const O_CLOCK = OLED_CLOCK

usart.SetBaudrate(br19200)
USART.Write(O_CLOCK,13,10)
> I guess what I'm asking is do I need to go through
> wherever I make calls to the SPI library and add
> conditional compile options based on which module
> is included?

Pre-processor values are global, if that is what you mean?

blackcattech
Posts: 113
Joined: Mon Jan 11, 2010 10:39 pm
Location: Chesterfield

Post by blackcattech » Tue Jul 30, 2013 2:49 pm

Ah, I think I follow you. I can't try your example as I don't have a serial connection on my board though.

I'm trying to work out the best way to do this now... All I can think is to add an optional parameter to the oled_init routine where you can set the SPI clock speed and have it defaulting to spiOscDiv16.

To explain my other question...
I've got an #option defined to allow the SPI port to be used by the display to be specified. Based on this my SSD1305 module includes either SPI.bas or SPI2.bas.

In my SSD1305 module, can I just call (for example) WriteByte(data) or do I need to check which module the user has specified as a conditional compile and call either SPI.WriteByte or SPI2.WriteByte?

I'm thinking that if I specify SPI2 for the SSD1305 and then use SPI elsewhere in the program, could things get confused? As I include the SPI module in my SSD1305 module, will Swordfish know to use the routines in this module, or could the inclusion of the other SPI module elsewhere in the code override it?

I'm not using the other SPI port in this particular program but I want to make the library open for reuse.

User avatar
David Barker
Swordfish Developer
Posts: 1214
Joined: Tue Oct 03, 2006 7:01 pm
Location: Saltburn by the Sea, UK
Contact:

Post by David Barker » Tue Jul 30, 2013 3:00 pm

if you are using a conditional compile to link in either SPI or SSPI, then it should be safe just to call WriteByte - if you are worried that WriteByte() is in use by another module, you could use something like:

Code: Select all

#if MY_HARDWARE_SPI
include "spi.bas"
dim spiWriteByte as SPI.WriteByte
#else
include "sspi.bas"
dim spiWriteByte as SSPI.WriteByte
#endif
then in your code just reference spiWriteByte()

blackcattech
Posts: 113
Joined: Mon Jan 11, 2010 10:39 pm
Location: Chesterfield

Post by blackcattech » Tue Jul 30, 2013 3:48 pm

Thanks, David. There are so many useful tricks in Swordfish that aren't immediately obvious when you come from more restrictive embedded development languages.

My concern is that both SPI and SPI2 have the same functions and subs so I don't know how Swordfish determines which to use if both are included. However, your solution is much neater as I can make it obvious in my code that the write is to the SPI port for the OLED display.

User avatar
RangerBob
Posts: 152
Joined: Thu May 31, 2007 8:52 am
Location: Beds, UK

Post by RangerBob » Thu Aug 01, 2013 2:24 pm

I'd have a look at how Steven Wrights SD Filesystem module on the wiki handles SPI selection as it can do either MSSP ports or a software SPI, and it's all selected via options. Sounds like exactly how you want yours to behave.

http://www.sfcompiler.co.uk/wiki/pmwiki ... temVersion

Post Reply