Estoy trabajando en la placa del núcleo stm32f103rb y quiero enviar datos y recibir datos utilizando stm32f103 en modo esclavo. Puedo obtener datos del maestro pero no puedo enviar datos al maestro. No sé qué causó el problema (¿inicializar o interrumpir o marcar?) ¿Cómo hago un arreglo?
#include "stm32f10x.h" /*Device header*/
#define rxBufferSize 4
#define txBufferSize 5
#define SPI_SLAVE_PIN_NSS 12
#define SPI_SLAVE_PIN_SCK 5
#define SPI_SLAVE_PIN_MISO 6
#define SPI_SLAVE_PIN_MOSI 7
uint8_t txSize=5;
uint8_t SPI_SLAVE_Buffer_Rx[rxBufferSize],RxIdx=0,TxIdx=0;
uint8_t SPIz_Buffer_Tx[txBufferSize] = {'s','e','l','a','m'};
/*****************************************************************************/
/* FUNCTIONS */
/*****************************************************************************/
/**
* @brief Initializes the SPI
* @param None
* @retval None
*/
void RCC_Configuration(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);
int main(void)
{
RCC_Configuration();
NVIC_Configuration();
GPIO_Configuration();
SPI_InitTypeDef SPI_InitStructure;
/* SPI_SLAVE configuration -------------------------------------------------*/
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Slave;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; /* NOTE: Clock polarity low */
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; /* NOTE: Clock phase 2 edges */
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; /* NOTE: Chip select is controlled by software */
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; /* NOTE: MSB First */
SPI_InitStructure.SPI_CRCPolynomial = 7; /* NOTE: Default CRC calculation polynomial value */
SPI_Init(SPI1, &SPI_InitStructure);
SPI_I2S_ITConfig(SPI1, SPI_I2S_IT_RXNE, ENABLE);
// SPI_I2S_ITConfig(SPI1, SPI_I2S_IT_TXE, ENABLE);
/* Enable SPI_SLAVE */
/* SPI_I2S_ClearITPendingBit(SPI1, SPI_I2S_IT_RXNE);
SPI_I2S_ClearITPendingBit(SPI1, SPI_I2S_IT_TXE);*/
SPI_Cmd(SPI1, ENABLE);
while(1) {
}
}
void RCC_Configuration(void)
{
// RCC_PCLK2Config(RCC_HCLK_Div2);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA |RCC_APB2Periph_SPI1, ENABLE);
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Configure SCK and MOSI pins as Input Floating */
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin =SPI_SLAVE_PIN_SCK | SPI_SLAVE_PIN_MOSI ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = SPI_SLAVE_PIN_MISO
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel = SPI1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_Init(&NVIC_InitStructure);
}
void SPI1_IRQHandler(void)
{
/* if (SPI_I2S_GetITStatus(SPI1, SPI_I2S_FLAG_TXE) == SET) {
SPI_I2S_SendData(SPI1, (uint8_t)SPIz_Buffer_Tx[TxIdx++]);
if(TxIdx == txBufferSize)
TxIdx=0;
}
*/
if (SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_RXNE)== SET)
SPI_SLAVE_Buffer_Rx[RxIdx++] = SPI_I2S_ReceiveData(SPI1);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_BSY) == SET);
if(RxIdx==rxBufferSize)
RxIdx=0;
// SPI1_Transmit(SPIz_Buffer_Tx,SPI_SLAVE_Buffer_Rx,4);
/* if (SPI_I2S_GetITStatus(SPI1, SPI_I2S_IT_TXE) != RESET)
{
//Send SPI1 data
SPI_I2S_SendData(SPI1, SPIz_Buffer_Tx[TxIdx++]);//just an idea here as how send needs to be done
//clear interrupt bit
}
*/
/* if (SPI_I2S_GetITStatus(SPI1, SPI_I2S_IT_RXNE) != RESET)
{
//Receive SPI1 data
SPI_SLAVE_Buffer_Rx[RxIdx++] = SPI_I2S_ReceiveData(SPI1);
if(RxIdx==rxBufferSize)
RxIdx=0;//just an idea here as how receive needs to be done
//clear interrupt bit
} */
}
Cuando veo la línea desde el analizador lógico, puedo ver que la línea miso siempre está en un nivel alto.