SPI code
--------------------------------------------- header ---------------------------------------------
/* SPI header */
#define SPIy SPI1
#define SPIy_GPIO GPIOA
#define SPIy_SCKPin GPIO_Pin_5
#define SPIy_MISOPin GPIO_Pin_6
#define SPIy_MOSIPin GPIO_Pin_7
#define DYMMY_BYTE 0x00
void GPIO_Configuration();
/* USART header */
#define GPIO_USART GPIOA
#define GPIO_USART_Rx_Pin GPIO_Pin_10
#define GPIO_USART_Tx_Pin GPIO_Pin_9
#define USARTy USART1
#define USARTy_GPIO GPIOA
#define USARTy_TxPin GPIO_Pin_9
#define USARTy_RxPin GPIO_Pin_10
#define USARTy_ClkPin GPIO_Pin_8
void SerialPutChar (uint8_t c);
void Serial_PutString(uint8_t *s);
uint8_t USART_GetCharacter(USART_TypeDef* usart_p);
int fputc(int ch, FILE *f);
void USARTy_Init();
/* RCC header */
void RCC_Configuration();
/* SPI header */
void SPI_Configuration();
--------------------------------------------- RCC.c ---------------------------------------------
void RCC_Configuration()
{
/* USART1, SPI1, USART1 SPI1 PORT clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
}
--------------------------------------------- GPIO.c ---------------------------------------------
void GPIO_Configuration()
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Configure USARTy TX and USARTy CK pins as alterante function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_USART_Tx_Pin | USARTy_ClkPin;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(USARTy_GPIO, &GPIO_InitStructure);
/* Configure SPI1 pins : SCK, MISO and MOSI */
GPIO_InitStructure.GPIO_Pin = SPIy_SCKPin | SPIy_MISOPin | SPIy_MOSIPin;
GPIO_Init(SPIy_GPIO, &GPIO_InitStructure);
/* Configure USARTy RX as input floating */
GPIO_InitStructure.GPIO_Pin = USARTy_RxPin;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(USARTy_GPIO, &GPIO_InitStructure);
}
위 코드는 stm사에서 제공하는 예제 코드를 사용하여 SPI USART 통신을 성공한 code이다. 그런데 이상한 것은 SPI slave 입장에서는 SPIy_SCKPin과 SPIy_MOSIPin이 input mode를 사용해야 하지 않을까?? 나중에 다시 코드를 바꾼 후 실험해봐야겠다.
--------------------------------------------- USART.c ---------------------------------------------
/* Private define ------------------------------------------------------------*/
#define TxBufferSize1 (countof(TxBuffer1) - 1)
#define TxBufferSize2 (countof(TxBuffer2) - 1)
/* Private variables ---------------------------------------------------------*/
uint8_t TxBuffer1[] = "USART Synchronous Example: USARTy -> SPIy using TXE and RXNE Flags";
uint8_t TxBuffer2[] = "USART Synchronous Example: SPIy -> USARTy using TXE and RXNE Flags";
uint8_t RxBuffer1[TxBufferSize2];
uint8_t RxBuffer2[TxBufferSize1];
__IO uint8_t NbrOfDataToRead1 = TxBufferSize2;
__IO uint8_t NbrOfDataToRead2 = TxBufferSize1;
__IO uint8_t TxCounter1 = 0, RxCounter1 = 0;
__IO uint8_t TxCounter2 = 0, RxCounter2 = 0;
USART_InitTypeDef USART_InitStructure;
USART_ClockInitTypeDef USART_ClockInitStructure;
void USARTy_Init()
{
/* USARTy configuration ------------------------------------------------------*/
/* USARTy configured as follow:
- BaudRate = 115200 baud
- Word Length = 8 Bits
- One Stop Bit
- No parity
- Hardware flow control disabled (RTS and CTS signals)
- Receive and transmit enabled
- USART Clock Enabled
- USART CPOL: Clock is active High
- USART CPHA: Data is captured on the second edge
- USART LastBit: The clock pulse of the last data bit is output to
the SCLK pin
*/
USART_ClockInitStructure.USART_Clock = USART_Clock_Enable;
USART_ClockInitStructure.USART_CPOL = USART_CPOL_High;
USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;
USART_ClockInitStructure.USART_LastBit = USART_LastBit_Enable;
USART_ClockInit(USARTy, &USART_ClockInitStructure);
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No ;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USARTy, &USART_InitStructure);
/* Configure the USARTy */
USART_Init(USARTy, &USART_InitStructure);
/* Enable the USARTy */
USART_Cmd(USARTy, ENABLE);
}
--------------------------------------------- SPI.c ---------------------------------------------
void SPI_Configuration()
{
SPI_InitTypeDef SPI_InitStructure;
SPI_StructInit(&SPI_InitStructure);
SPI_I2S_DeInit(SPIy);
/* SPIy Config */
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_High;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_LSB;
/* Configure SPIy */
SPI_Init(SPIy, &SPI_InitStructure);
/* SPIy enable */
SPI_Cmd(SPIy, ENABLE);
}
--------------------------------------------- main.c ---------------------------------------------
void main()
{
RCC_Configuration();
GPIO_Configuration();
SPI_Configuration();
USARTy_Init();
while(NbrOfDataToRead2--)
{
/* Write one byte in the USARTy Transmit Data Register */
USART_SendData(USARTy, TxBuffer1[TxCounter1++]);
/* Wait until end of transmit */
//while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
while(USART_GetFlagStatus(USARTy, USART_FLAG_TC) == RESET)
{
}
/* Wait the byte is entirely received by SPIy */
while(SPI_I2S_GetFlagStatus(SPIy, SPI_I2S_FLAG_RXNE) == RESET)
{
}
/* Store the received byte in the RxBuffer2 */
RxBuffer2[RxCounter2++] = SPI_I2S_ReceiveData(SPIy);
}
/* Clear the USARTy Data Register */
USART_ReceiveData(USARTy);
while(NbrOfDataToRead1--)
{
/* Wait until end of transmit */
while(SPI_I2S_GetFlagStatus(SPIy, SPI_I2S_FLAG_TXE)== RESET)
{
}
/* Write one byte in the SPIy Transmit Data Register */
SPI_I2S_SendData(SPIy, TxBuffer2[TxCounter2++]);
/* Send a Dummy byte to generate clock to slave */
USART_SendData(USARTy, DYMMY_BYTE);
/* Wait until end of transmit */
while(USART_GetFlagStatus(USARTy, USART_FLAG_TC) == RESET)
{
}
/* Wait the byte is entirely received by USARTy */
while(USART_GetFlagStatus(USARTy, USART_FLAG_RXNE) == RESET)
{
}
/* Store the received byte in the RxBuffer1 */
RxBuffer1[RxCounter1++] = USART_ReceiveData(USARTy);
}
printf ("\r\nRxBuffer1 : %s \r\n", RxBuffer1);
printf ("RxBuffer2 : %s \r\n", RxBuffer2);
while(1)
{
}
}
'MCU > ARM' 카테고리의 다른 글
ADC independent DMA (2) (0) | 2016.08.14 |
---|---|
ADC main feature(1) (0) | 2016.08.13 |
SPI 통신 (2) (0) | 2016.08.11 |
SPI 통신 (1) (0) | 2016.08.11 |
(*(volatile unsigned *))0x40021018 의미 (1) | 2013.12.15 |