stm32f103c8t6 spi communication with Arduino
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2017-04-15 2:42 AM
Hello
I devolved a code for stm32 as a master and for arduino as a slave , to send a byte through SPI.
But it did not work. I want to know what is the wrong in my code.
STM32 Master Code :
static void SPI_Config(void);
int main()
{ SPI_Config(); for (int z=0 ; z<50 ; z++){ SPI_I2S_SendData(SPI1,'A');} return 0;}static void SPI_Config(void)
{GPIO_InitTypeDef GPIO_InitStructure;SPI_InitTypeDef SPI_InitStructure;// Configure SPI1_CLKa and SPI1_MOSI GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_7; GPIO_Init(GPIOA, &GPIO_InitStructure); // SPI1 no remap (NSS/PA4, SCK/PA5, MISO/PA6, MOSI/PA7) GPIO_PinRemapConfig(GPIO_Remap_SPI1, DISABLE); // Spi init/* SPI configuration *****************************************/SPI_InitStructure.SPI_Direction =SPI_Direction_2Lines_FullDuplex;SPI_InitStructure.SPI_Mode = SPI_Mode_Master;SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;SPI_InitStructure.SPI_BaudRatePrescaler =SPI_BaudRatePrescaler_64;SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;SPI_InitStructure.SPI_CRCPolynomial = 0;SPI_Init(SPI1, &SPI_InitStructure);SPI_Cmd(SPI1, ENABLE);}Arduino Slave code:
#include <SPI.h>
char buff [50];volatile byte indx;volatile boolean process;void setup (void) {
Serial.begin (115200); pinMode(MISO, OUTPUT); // have to send on master in so it set as output SPCR |= _BV(SPE); // turn on SPI in slave mode indx = 0; // buffer empty process = false; SPI.attachInterrupt(); // turn on interrupt}ISR (SPI_STC_vect) // SPI interrupt routine { byte c = SPDR; // read byte from SPI Data Register if (indx < sizeof buff) { buff [indx++] = c; // save data in the next index in the array buff if (c == '\r') //check for the end of the word process = true; }}void loop (void) {
if (process) { process = false; //reset the process Serial.println (buff); //print the array on serial monitor indx= 0; //reset button to zero }}- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2017-04-15 8:38 AM
You would need to make sure the TXE (Transmit Empty) flag is asserted before stuffing additional data.
Up vote any posts that you find helpful, it shows what's working..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2017-04-15 10:53 AM
How can I do This ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2017-04-15 11:41 AM
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
you don't show your code that actually attempts the tx/rx. how are you handling slave select?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2017-04-15 4:53 PM
I made the arduino as a slave
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2017-04-15 6:13 PM
you dont show your code that actually attempts the transmit and/or receive. you are showing the initialization code, which is very helpful so thank you for that. its a good place to start.
among other questions, how are you handling the slave select? in the code you posted above, it shows you chose to handle it in software. that would mean you selected a pin [on the master] as gpio and manipulated it before and after your attempted read/write command. its completely understandable to do so since apparently leaving it as hardware select, raises slave select with every byte clocked in/out which can work slowly or not at all sometimes. I don't know. I use that pin as a gpio and manipulate it manually before and after a given spi read.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2017-04-16 4:14 AM
int main(void)
{/* SPI1 configuration ---------------------------------------*/SPI_Config();while (1){/* Wait till Transmit buffer is empty */while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET){}/* Send dummy data */SPI_I2S_SendData(SPI1, 'b');}}- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2017-04-16 4:16 AM
This is the Main code
actually I want you to tell me what is specifically wrong in my code because actually I am not a professional programmer
