s5pv210与stm32 spi通信
spi通信不支持从设备主动给主设备发送数据,所以我把spi的用户空间驱动改了一下,实现过程是这样的,用一个中断来响应从设备的要求,即当STM32要主动给主设备发送数据的时候,将中断脚拉低,用户空间检测到中断后,主动给STM32发送一个空数据,这样spi就能读到STM32所要发送的数据了。SPI通信发数据与接数据是同时进行的,这个大家可以看下SPI协议。
以下我我修改的用户空间的驱动:
#include "stm32f10x.h"#include "uart.h"#include "spi.h"void SPItest(void);void UARTtest(USART_TypeDef *port);extern void Uart_SendStringn(USART_TypeDef *port,char *pt,int n);extern void Delay_ARMJISHU(__IO uint32_t nCount);void SPIsend_Init(void){GPIO_InitTypeDef GPIO_InitStructure;//spi send interrupt controlRCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOC, ENABLE);//使能PC,AFIOGPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //推挽输出 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //GPIO_Init(GPIOC, &GPIO_InitStructure); //end}void send_onoff(int onoff){if(onoff)GPIO_WriteBit(GPIOC, GPIO_Pin_7, Bit_RESET);elseGPIO_WriteBit(GPIOC, GPIO_Pin_7, Bit_SET);}int main(void){ //int i;NVIC_Configuration();//配置 NVIC 和 Vector Table Uart1_COMInit(115200); //串口1连接v210或PC机 SPI1_Init();SPIsend_Init(); send_onoff(Bit_RESET);Uart_SendString(USART1,"串口1开始:\r\n") ;//串口1接PC机while (1){ SPItest();} }void send_data(int mode){int i;send_onoff(Bit_SET);switch(mode){case 0x01:for(i=0;i<38;i++){SPI_I2S_SendData(SPI1,i + 1);}break;case 0x02:for(i=0;i<38;i++){SPI_I2S_SendData(SPI1,i + 2);}break;case 0x03:for(i=0;i<38;i++){SPI_I2S_SendData(SPI1,i + 3);}break;default:break;}send_onoff(Bit_RESET);cmd = 0;return;}void SPItest(void){int i;//static intx=0;if(RxIdx==SPI_BufferSize) //中断方式接收{cmd = SPI1_Buffer_Rx[0];for(i=0;i< RxIdx;i++){Uart_SendByte(USART1,SPI1_Buffer_Rx[i]);//向PC机发送// SPI_I2S_SendData(SPI1,0xff); SPI1_Buffer_Rx[i]=0;}//while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);//SPI_I2S_SendData(SPI1,x++);//send_onoff(1);//SPI_I2S_ITConfig(SPI1, SPI_I2S_IT_RXNE, ENABLE);//Delay_ARMJISHU(10000000);RxIdx=0;SPI_I2S_ITConfig(SPI1, SPI_I2S_IT_RXNE, ENABLE);}if(cmd)send_data(cmd); //查询方式接收:/*while (SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_RXNE) == RESET); SPI3_Buffer_Rx[RxIdx++] = SPI_I2S_ReceiveData(SPI3);if(RxIdx==SPI_BufferSize){for(i=0;i< RxIdx;i++){ Uart_SendByte(USART1,SPI3_Buffer_Rx[i]);//向PC机发送}RxIdx=0;}*/}void UARTtest(USART_TypeDef *port){int i;for(i=0;i<10;i++)Uart_SendByte(port,'0'+i);Delay_ARMJISHU(10000000);}具体代码我就不作说明了