在实际开发中有可能用到AT32的SPI从机模式,初步进行一下测试。
硬件环境:使用一颗STM32作为主机,使用AT32F403AVGT7开发板作为从机 使用其spi4作为从机接收。同时不使用默认的spi4对应io,而是使用重映射功能.
关键初始化代码如下
void RCC_Configuration(void)
{
/* Enable peripheral clocks --------------------------------------------------*/
/* Enable SPIy clock and GPIO clock for SPIy and SPIz */
RCC_APB1PeriphClockCmd(RCC_APB1PERIPH_SPI4, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2PERIPH_GPIOA | RCC_APB2PERIPH_GPIOB | RCC_APB2PERIPH_GPIOC | RCC_APB2PERIPH_AFIO |RCC_APB2PERIPH_GPIOD, ENABLE);
/* Enable SPIz Periph clock */
}
void SPI4_initial()
{
GPIO_InitType GPIO_InitStructure;
SPI_InitType SPI_InitStructure;
NVIC_InitType NVIC_InitStructure;
GPIO_PinsRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);
GPIO_StructInit(&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pins = GPIO_Pins_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = SPI4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
GPIO_InitStructure.GPIO_Pins = MOSI_4 | SCK_4;
GPIO_InitStructure.GPIO_MaxSpeed = GPIO_MaxSpeed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pins = SPI_RF_CS;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
// GPIO_SetBits(GPIOB,SPI_RF_CS);
/* Configure SPI4 pins: MISO ---------------------------------*/
GPIO_InitStructure.GPIO_Pins = MISO_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
AFIO->MAP5 = 0x20000000;
/* SPI4 Config -------------------------------------------------------------*/
SPI_DefaultInitParaConfig(&SPI_InitStructure);
SPI_InitStructure.SPI_TransMode = SPI_TRANSMODE_FULLDUPLEX;
SPI_InitStructure.SPI_Mode = SPI_MODE_SLAVE;
SPI_InitStructure.SPI_FrameSize = SPI_FRAMESIZE_8BIT;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_LOW;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1EDGE;
SPI_InitStructure.SPI_NSSSEL = SPI_NSSSEL_SOFT;
SPI_InitStructure.SPI_FirstBit = SPI_FIRSTBIT_MSB;
SPI_InitStructure.SPI_CPOLY = 7;
SPI_Init(SPI4, &SPI_InitStructure);
SPI4->CTRL1 |= (uint16_t)0x0040;
}
void testspi(){
uint8_t RxIdx = 0;
while (RxIdx < 20)
{
while (SPI_I2S_GetFlagStatus(SPI4, SPI_I2S_FLAG_RNE) == RESET);
spi_rev_temp[RxIdx++] = SPI_I2S_RxData(SPI4);
}
}
Talk is cheap show me the code。
在附件中
实验现象就是buf里可以收到主机发送的内容。
个人经验在于两点:
1.手册里的iomux模块在代码里其实就是afio部分,如果你想使用重映射功能,别忘记开启他的时钟。
2.使用spi4 时候 我使用的v1.3的库文件,enable函数出现一些问题,所以直接操纵寄存器就好。
|