Changing SPI Master Bit-Order on the fly

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

Hi,

I'm using CY8CKIT-044, and my current project requires to change the bit-order of SPI on the fly,

as one connected module is LSB-First and the other is MSB-First.

(1) I could not find an API to change bit order of a SPI(Master), am I missing something?

(2) In case we need to hand craft one by ourselves, is following function adequate?

(3) Needless to say, I'm planning to switch the value while no SPI transaction is taking place,

     but is/are there things I must take care of to make things work correctly?

<<code>>

void mcu_spi_msb_first(bool msb_first)

{

    uint32_t *SCB3_RX_CTRL = (uint32_t*)0x40270300 ;

    uint32_t *SCB3_TX_CTRL = (uint32_t*)0x40270200 ;

    uint32_t RX_CTRL, TX_CTRL ;

    RX_CTRL = *SCB3_RX_CTRL ;

    TX_CTRL = *SCB3_TX_CTRL ;

    if (msb_first) {

        RX_CTRL |= 0x00000100 ;

        TX_CTRL |= 0x00000100 ;

    } else {

        RX_CTRL &= 0xFFFFFEFF ;

        TX_CTRL &= 0xFFFFFEFF ;

    }

    *SCB3_RX_CTRL = RX_CTRL ;

    *SCB3_TX_CTRL = TX_CTRL ;

}

<</code>>

moto

1 Solution
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

Finally my shield board for both ILI9341 (SPI MSB First) and ASR1 (SPI LSB First) arrived.

And I could confirm that the function mcu_spi_msb_first() working.

Although I can survive with this for the time being,

it would be nice if PSoC Creator provides API for this

while "generate application".

moto

View solution in original post

7 Replies
Anonymous
Not applicable

SCB0_RX_CTRL, (SCB1_RX_CTRL)
Address: 0x40060300, Bit 8, SCB0_MSB_FIRST

Least significant bit first ('0') or most significant bit first ('1').
(For I2C, this field should be '1'.)
Default Value: 1

SCB0_TX_CTRL, (SCB1_TX_CTRL)
Address: 0x40060200, Bit 8, SCB0_MSB_FIRST
Least significant bit first ('0') or most significant bit first ('1').
(For I2C, this field should be '1'.)
Default Value: 1

Hope this helps or Points you to the wanted direction.

Best regards!

Jurgen

0 Likes
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

Dear Jurgen-san,

Thank you for your response!

By any chance have you looked at my code?

As I'm using an Arduino type shield with CY8CKIT-044,

the default SPI pins are

D13 P6_2 scb[3].spi_clk:0

D12 P6_1 scb[3].spi_miso:0

D11 P6_0 scb[3].spi_mosi:0

So, I assumed that the SCB is SCB[3] instead of SCB[0] and their addresses are

    uint32_t *SCB3_RX_CTRL = (uint32_t*)0x40270300 ;

    uint32_t *SCB3_TX_CTRL = (uint32_t*)0x40270200 ;

Although I'm hoping that I should be able to manipulate the bit,

I was not sure if the change will be reflected immediately

or some other operation(s) are required.

Or even better, if there is already an API to handle this.

Best Regards,

11-Apr-2018

Motoo Tanaka

0 Likes
Anonymous
Not applicable

I imagine you could manipulate the bits as you want, but if the module is communicating when you change the bits, then things might get tricky...

I would suggest trying to change the bit order only when it is not transmitting or is turned off all together.

0 Likes
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

Dear e.pratt-san,

Thank you for your suggestion!

As I also wrote in my original question

> (3) Needless to say, I'm planning to switch the value while no SPI transaction is taking place,

>     but is/are there things I must take care of to make things work correctly?

I'm planning to change the bit while no transaction is taking place,

but I was not sure if only changing this bit will take care of switching the data bit order or

I need to re-initialize the SPI(SCB?) module to reflect the change.

Of course testing with the real hardware may provide me the answer, but currently I'm in the

process of porting my project from a NXP MCU board to PSoC Board (CY8CKIT-044) and

peripherals are yet to be prepared.

Best Regards,

12-Apr-2018

Motoo Tanaka

0 Likes
Anonymous
Not applicable

Adresses for SCB[2] and SCB[3] are as you mentioned.

Sorry, I can not test your code.

SPI TX- or RX-Buffer shall be proven to be empty prior to switching the SCBx_MSB_FIRST bit. You can make this a small function with Interrupt.

Some behaviours you need to test on your own, and please let us know then here in the community, for future reference.

BR

Juergen

Anonymous
Not applicable

Ah; I should have seen that in the original post

0 Likes
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

Finally my shield board for both ILI9341 (SPI MSB First) and ASR1 (SPI LSB First) arrived.

And I could confirm that the function mcu_spi_msb_first() working.

Although I can survive with this for the time being,

it would be nice if PSoC Creator provides API for this

while "generate application".

moto