Вы находитесь на странице: 1из 6

Micromouse Online Forum

Search

The Micromouse Community

Search
Advanced search

B o ard ind e x N o n Micro mo use Ele ct ro nics


FAQ

Register

Login

SPI on the STM32 USART


Post a reply

Search this to pic

9 posts Page 1 of 1

Search

SPI on the STM32 USART


b y sgome s Sun Fe b 0 7, 20 10 4:19 p m

I've been playing with the STM32 lately and I have to say I'm quite impressed. However I found the first tiny little issue
that is frustrating. The SPI peripheral. I'm using the Nokia 6100/6610 display and it has this odd 9- bit data frame.
Unfortunately the STM32 will only allow 8 or 16 bit frames. So it looks like I will have to bit- bang the SPI. No big deal I
know but I'm one for throwing a byte at a register and letting my code get on with things. Having to sit around and babysit
the SPI is frustrating.

sgome s
Po s ts : 9
J o ine d : Fri J an 0 8 , 20 10 11:19 p m
Lo c atio n: Camp b e ll, CA USA

Anyone have a clever idea how to use the 16- bit mode to produce a 9- bit frame? Hmmm I just had a thought....maybe
use a USART in synchronous mode somehow....
Shannon
Las t e d ite d b y s g o me s o n We d Fe b 10 , 20 10 11:57 p m, e d ite d 1 time in to tal.
T

Re: T he f irst chink in the armor f or ST M32


b y pe t e h Sun Fe b 0 7, 20 10 5:27 p m

The USARTS on the STM32 can be run in synchronous mode where they behave exactly like a SPI peripheral and they
support 8 or 9 data bits.

pe t e h
Site Ad min
Po s ts : 49 4
J o ine d : We d J an 25, 20 0 6 4:11 p m
Lo c atio n: Lic hfie ld , Staffo rd s hire

I have not tried this - just seen it in the reference book.


You can have it cheap, good and soon - pick any two.
T

PDFmyURL.com

Re: T he f irst chink in the armor f or ST M32


b y sgome s Sun Fe b 0 7, 20 10 11:32 p m

yep exactly right. What a relief it was to read the reference manual and find that they have all the bits for doing 8 or 9 bit
SPI via the USARTs. Man this is a fantastic chip!

sgome s
Po s ts : 9
J o ine d : Fri J an 0 8 , 20 10 11:19 p m
Lo c atio n: Camp b e ll, CA USA

For the record:


//turn on the clock
RCC_APB1ENR |= RCC_APB1ENR_USART2EN;
//PA2 (MOSI) to alternate function push- pull
mode = (uint32_t)0x09 << GPIOA_CRL_CNF2_MODE2_BIT;
temp = GPIOA_CRL & ~ GPIOA_CRL_CNF2_MODE2_MASK;
GPIOA_CRL = temp | mode;
//PA3 (MISO) to alternate function input floating
mode = (uint32_t)0x04 << GPIOA_CRL_CNF3_MODE3_BIT;
temp = GPIOA_CRL & ~ GPIOA_CRL_CNF3_MODE3_MASK;
GPIOA_CRL = temp | mode;
//PA4 (SCK) to alternate function push- pull
mode - (uint32_t)0x09 << GPIOA_CRL_CNF4_MODE4_BIT;
temp = GPIOA_CRL & ~ GPIOA_CRL_CNF4_MODE4_MASK;
GPIOA_CRL = temp | mode;
//set clock rate to 6MHz ;
USART2_BRR = 36000000L/6000000L; //APB1 bus max speed is 36MHz
USART2_CR1 |= (USART2_CR1_RE | USART2_CR1_TE | USART2_CR1_M); // RX, TX enable, 9- bit
USART2_CR2 |= USART2_CR2_CLKEN; //enable SCK pin CPOL and CPHA are also in this register
USART2_CR1 |= USART2_CR1_UE; // USART enable
//interrupts can also be enabled in the USART2_CR1 register
Shannon
T

Re: T he f irst chink in the armor f or ST M32


b y sgome s Mo n Fe b 0 8 , 20 10 3:12 p m

sgome s
Po s ts : 9

PDFmyURL.com

Just discovered another caveat. The SPI bus is a MSB first bus and the USART is a LSB first bus. So to use the USART
in SPI mode you'll have to reverse the order of the data bits before stuffing it into the transmit register.

J o ine d : Fri J an 0 8 , 20 10 11:19 p m


Lo c atio n: Camp b e ll, CA USA

Something like this for 8- bits:


c = (c & 0x0F) << 4 | (c & 0xF0) >> 4;
c = (c & 0x33) << 2 | (c & 0xCC) >> 2;
c = (c & 0x55) << 1 | (c & 0xAA) >> 1;
probably converting to assembly and optimiz ing it might make it faster.
T

Re: T he f irst chink in the armor f or ST M32


b y sgome s We d Fe b 10 , 20 10 10 :36 p m

I asked ST about this issue of MSB first vs. LSB first. Apparently it is true that if using the USART for SPI communication
you DO have to reverse the order of the bits. However there is an easy way to do it. I've copied his response below:

sgome s
Po s ts : 9
J o ine d : Fri J an 0 8 , 20 10 11:19 p m
Lo c atio n: Camp b e ll, CA USA

Dear User,
Please find here belo w latest info rmatio n co ncerning the request R10 0 6 0 10 0
Yo u are co rrect. There is no reverse bit in the USART registers.
There is, ho wever, a 1 clo ck cycle Thumb2 instructio n that will do it fo r yo u. RBIT. There's also a macro in the STM32
Standard library That allo ws yo u to call the assy lang. instructio n fro m C.
A co py o f it belo w:
CO DE: SELECT ALL

/**
* @brief Reverse bit order of value
*
* @param uint32_t value to reverse
* @return uint32_t reversed value
*
* Reverse bit order of value
*/
uint32_t __RBIT(uint32_t value)
{
PDFmyURL.com

__ASM("rbit r0, r0");


__ASM("bx lr");
}
T

Re: T he f irst chink in the armor f or ST M32


b y pe t e h We d Fe b 10 , 20 10 10 :49 p m

That is very handy. thanks for passing that on.

pe t e h
Site Ad min
Po s ts : 49 4
J o ine d : We d J an 25, 20 0 6 4:11 p m
Lo c atio n: Lic hfie ld , Staffo rd s hire

You can have it cheap, good and soon - pick any two.
T

Re: SPI on the ST M32 USART


b y re va Mo n Ap r 30 , 20 12 6 :47 am

Im trying to interface Nokia LCD 6610 to STM32L through sync USART. Though there is clock and data on MOSI and
SCK pins LCD isnt displaying anything. Below is the code i've used for transfer of data through USART
USART_DeInit(USART2);
USART_InitStructure.USART_BaudRate =0x02255100;//0x0001C200; // baud rate=115200
USART_InitStructure.USART_WordLength =USART_WordLength_9b;
USART_InitStructure.USART_StopBits =USART_StopBits_1;
USART_InitStructure.USART_Parity=USART_Parity_No;
USART_InitStructure.USART_Mode=USART_Mode_Tx;
USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
USART_Init(USART2, &USART_InitStructure);

re va
Po s ts : 3
J o ine d : Sat Ap r 28 , 20 12 6 :47 p m

USART_ClkInitStructure.USART_Clock= USART_Clock_Enable;
USART_ClkInitStructure.USART_CPOL=USART_CPOL_Low;
USART_ClkInitStructure.USART_CPHA=USART_CPHA_1Edge;
USART_ClkInitStructure.USART_LastBit=USART_LastBit_Enable;
USART_ClockInit(USART2, &USART_ClkInitStructure);
USART_Cmd(USART2,ENABLE);
T

Re: SPI on the ST M32 USART


b y re va Mo n Ap r 30 , 20 12 6 :51 am

Below is the code implemented for data transfer through USART

re va
Po s ts : 3
J o ine d : Sat Ap r 28 , 20 12 6 :47 p m

PDFmyURL.com

void WriteSpiData(unsigned int data)


{

data = (data & 0x0F) << 4 | (data & 0xF0) >> 4;


data = (data & 0x33) << 2 | (data & 0xCC) >> 2;
data = (data & 0x55) << 1 | (data & 0xAA) >> 1;
data = ((data << 1 ) | 0x0001);
//data=(data | 0X0100);
USART_SendData(USART2, data);
while(USART_GetFlagStatus(USART2, USART_FLAG_TXE )==RESET);

}
void WriteSpiCommand(unsigned int data)
{

data = (data & 0x0F) << 4 | (data & 0xF0) >> 4;


data = (data & 0x33) << 2 | (data & 0xCC) >> 2;
data = (data & 0x55) << 1 | (data & 0xAA) >> 1;
data = ((data << 1 ) & ~ 0x0001);
//data=(data & ~ 0X0100);
USART_SendData(USART2, data);
while(USART_GetFlagStatus(USART2, USART_FLAG_TXE )==RESET);
}

Please help me with this


T

Re: SPI on the ST M32 USART


b y re va Tue May 0 1, 20 12 12:41 p m

hey!! i changed the settings to USART1 .. a better USART in terms of speed ... and..... behold its workin! ... jus done wid

re va
Po s ts : 3
J o ine d : Sat Ap r 28 , 20 12 6 :47 p m

PDFmyURL.com

my demo
) ...
Thanks guys \m/...........
really thank sgomes ... the data reversal was quite helpful
CHANGED USART settings
USART_DeInit(USART1);
USART_InitStructure.USART_BaudRate =0x00011114;//0x0001C200; // baud rate=115200
USART_InitStructure.USART_WordLength =USART_WordLength_9b;
USART_InitStructure.USART_StopBits =USART_StopBits_1;
USART_InitStructure.USART_Parity=USART_Parity_No;
USART_InitStructure.USART_Mode=USART_Mode_Tx;
USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
USART_Init(USART1, &USART_InitStructure);
USART_ClkInitStructure.USART_Clock= USART_Clock_Enable;
USART_ClkInitStructure.USART_CPOL=USART_CPOL_Low;
USART_ClkInitStructure.USART_CPHA=USART_CPHA_1Edge;
USART_ClkInitStructure.USART_LastBit=USART_LastBit_Enable;
USART_ClockInit(USART1, &USART_ClkInitStructure);
USART_Cmd(USART1,ENABLE);
T

Display posts from previous:

All posts

Sort by

Post time

Ascending

Go

Post a reply

9 posts Page 1 of 1

Return to Electronics

Jump to:

Electronics

Go

WHO IS O N LIN E
Users browsing this forum: No registered users and 1 guest

The team Delete all board cookies All times are UTC

Board index
Po we re d b y p hp BB Fo rum So ftware p hp BB G ro up

PDFmyURL.com

Вам также может понравиться