OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 9019|回复: 1

两块STM32F4之间spi握手通信移位问题

[复制链接]

1

主题

1

帖子

0

精华

新手入门

积分
25
金钱
25
注册时间
2013-4-17
在线时间
0 小时
发表于 2013-4-17 21:56:18 | 显示全部楼层 |阅读模式
我用两块STM32F4的SPI1双向通信,想实现主SPI发送1024个数据,从SPI收到的时候回复一个ack,主SPI收到这个ack的时候再发送1024个字节。有好几个问题想请教大侠。

首先主SPI的配置如下:

        GPIO_InitTypeDef GPIO_InitStruct;
SPI_InitTypeDef SPI_InitStruct;

// enable clock for used IO pins
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_4 | GPIO_Pin_3;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOB, &GPIO_InitStruct);
// connect SPI1 pins to SPI alternate function
GPIO_PinAFConfig(GPIOB, GPIO_PinSource5, GPIO_AF_SPI1);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource4, GPIO_AF_SPI1);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource3, GPIO_AF_SPI1);

// enable peripheral clock
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
/* configure SPI1 in Mode 0 
* CPOL = 0 --> clock is low when idle
* CPHA = 0 --> data is sampled at the first edge
*/
SPI_InitStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex; // set to full duplex mode, seperate MOSI and MISO lines
SPI_InitStruct.SPI_Mode = SPI_Mode_Master;     // transmit in master mode, NSS pin has to be always high
SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b; // one packet of data is 8 bits wide
SPI_InitStruct.SPI_CPOL = SPI_CPOL_Low;        // clock is low when idle
SPI_InitStruct.SPI_CPHA = SPI_CPHA_1Edge;      // data sampled at first edge
SPI_InitStruct.SPI_NSS = SPI_NSS_Soft | SPI_NSSInternalSoft_Set; // set the NSS management to internal and pull internal NSS high
SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16; // SPI frequency is APB2 frequency / 4
SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;// data is transmitted MSB first
SPI_InitStruct.SPI_CRCPolynomial = 7;
SPI1->CR1 |=0x1000;
SPI1->CR2 |=0x20;
SPI_Init(SPI1, &SPI_InitStruct); 

SPI_Cmd(SPI1, ENABLE); // enable SPI1[/code]主机的发送接收函数:
  1. uint8_t SPI1_sendrecv(uint8_t data){</div>
  2. <div><br />
  3. </div>
  4. <div><span class="Apple-tab-span" style="white-space:pre;"> </span>SPI1-&gt;DR = data; // write data to be transmitted to the SPI data register</div>
  5. <div><span class="Apple-tab-span" style="white-space:pre;"> </span>while( !(SPI1-&gt;SR &amp; SPI_I2S_FLAG_TXE) ); // wait until transmit complete</div>
  6. <div><span class="Apple-tab-span" style="white-space:pre;"> </span>while( !(SPI1-&gt;SR &amp; SPI_I2S_FLAG_RXNE) ); // wait until receive complete</div>
  7. <div><span class="Apple-tab-span" style="white-space:pre;"> </span>while( SPI1-&gt;SR &amp; SPI_I2S_FLAG_BSY ); // wait until SPI is not busy anymore</div>
  8. <div><span class="Apple-tab-span" style="white-space:pre;"> </span>return SPI1-&gt;DR; // return received data from SPI data register</div>
  9. <div>}<br />
  10. <br />
  11. 主机主函数发送:<br />
  12. <br />
  13. for(index_data=0;index_data&lt;BufferSize;index_data++){</div>
  14. <div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;receive_data[index_data]=SPI1_sendrecv(send_data[index_data]);</div>
  15. <div>}</div>
  16. <div>printf("%s\n\r",receive_data);
复制代码
从机的配置:[code]void init_SPI1(void){
GPIO_InitTypeDef GPIO_InitStruct;
SPI_InitTypeDef SPI_InitStruct;

// enable clock for used IO pins
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_4 | GPIO_Pin_3;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOB, &GPIO_InitStruct);
// connect SPI1 pins to SPI alternate function
GPIO_PinAFConfig(GPIOB, GPIO_PinSource3, GPIO_AF_SPI1);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource4, GPIO_AF_SPI1);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource5, GPIO_AF_SPI1);
// enable peripheral clock
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
/* configure SPI1 in Mode 0 
* CPOL = 0 --> clock is low when idle
* CPHA = 0 --> data is sampled at the first edge
*/
SPI_InitStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex; // set to full duplex mode, seperate MOSI and MISO lines
SPI_InitStruct.SPI_Mode = SPI_Mode_Slave;     // transmit in master mode, NSS pin has to be always high
SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b; // one packet of data is 8 bits wide
SPI_InitStruct.SPI_CPOL = SPI_CPOL_Low;        // clock is low when idle
SPI_InitStruct.SPI_CPHA = SPI_CPHA_1Edge;      // data sampled at first edge
SPI_InitStruct.SPI_NSS = SPI_NSS_Soft;  // set the NSS management to software
SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;// data is transmitted MSB first
SPI_InitStruct.SPI_CRCPolynomial = 7;
SPI1->CR1 |=0x1000;
SPI1->CR2 |=0x20;
SPI_Init(SPI1, &SPI_InitStruct); 

SPI_Cmd(SPI1, ENABLE); // enable SPI1
}

从机发送和接收函数:

void SPI1_Send(uint8_t data){
SPI1->DR = data; // write data to be transmitted to the SPI data register
while( !(SPI1->SR & SPI_I2S_FLAG_TXE) ); // wait until transmit complete
}

uint8_t SPI1_Receive(void){
while( !(SPI1->SR & SPI_I2S_FLAG_RXNE) ); // wait until receive complete
return SPI1->DR; // return received data from SPI data register
}

从机主函数接收:

while(1){
if(index_spi==BufferSize){
index_spi=0;
       printf("%s\n\r",receive_data);
}
else{
receive_data[index_spi]=SPI1_Receive(); 
SPI1_Send(receive_data[index_spi]);
index_spi++;
}
1. 首先测试两个之间就只是双向发送和接收,接收到的结果会有一位的偏移,比如我发一段字节:Google Inc. is an American multinational 
从机收到:lGoogle Inc. is an American multinationa,从机主机都是同时收发。从机收到一个字节就同时发回这个字节。然后主机收到的也是lGoogle Inc. is an American multinationa,不知道这个是不是正常的,有没有大侠能解释下这一位的偏移是怎么回事?

2. 然后加上握手部分,希望主机发完字符串然后发送无用字节并等待回复,从机收完字符串之后收无用字符并同时发出确认字节。
主机主函数发送:    

for(index_data=0;index_data<BufferSize;index_data++){
                   receive_data[index_data]=SPI1_sendrecv(send_data[index_data]);
}
               for(j=0;j<4;j++){
ack_string[j]=SPI1_sendrecv(0x00);
}
printf("%s\n\r",receive_data);
printf("ack string: %s\n\r",ack_string);

从机主函数接收:

char ack_string[4]="GOOD";
while(1){
if(index_spi==BufferSize){
index_spi=0;
       printf("%s\n\r",receive_data);
                for(i=0;i<4;i++){
SPI1_Receive();
SPI1_Send(ack_string);
       }
}
else{
receive_data[index_spi]=SPI1_Receive(); 
SPI1_Send(receive_data[index_spi]);
index_spi++;
}
}

主机发送Google Inc. is an American multinational,
从机收到gle Inc. is an American multinational
主机收到的字符串GOODgle Inc. is an American multinationa
收到的ack string是ack string: l  

所以结果好像是从机回复的ack主机都在下一次收数据的时候才收到了。这个也很难理解,而且我试过了不管ack设多长,主机都是后面一步才在一串数据的开头收到这段ack。
正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

530

主题

11万

帖子

34

精华

管理员

Rank: 12Rank: 12Rank: 12

积分
165309
金钱
165309
注册时间
2010-12-1
在线时间
2108 小时
发表于 2013-4-18 00:45:21 | 显示全部楼层
移位问题,可能和你数据处理有关.重点检查下.
我是开源电子网www.openedv.com站长,有关站务问题请与我联系。
正点原子STM32开发板购买店铺http://openedv.taobao.com
正点原子官方微信公众平台,点击这里关注“正点原子”
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2024-11-22 16:39

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表