---
title: "EFM32GG11 와 W5500 SPI 통신 문제"
url: "https://maker.wiznet.io/forum/14321"
markdown_url: "https://maker.wiznet.io/forum/14321/md"
type: "Forum topic"
category: "Ethernet Chips"
author: "Member 11121"
created: "2021-08-11T09:53:32+09:00"
last_activity: "2021-08-24T22:55:35+09:00"
language: "en"
views: 720
replies: 3
source: "WIZnet Makers forum (https://maker.wiznet.io/forum/)"
---

# EFM32GG11 와 W5500 SPI 통신 문제

## Question

Asked by Member 11121 on 2021-08-11 in Ethernet Chips.

안녕하세요
EFM32GG11 및 EFM32GG12 개발보드와 W5500 모듈 간 SPI 통신이 잘 되지 않아 문의 드립니다.

EFM32 보드와는 register 읽기 쓰기부터 비정상적으로 동작합니다.

다음과 같이 mac어드레스를 쓰고 읽기를 시도하였으나 썼던 값으로 읽히지 않고
0x01 0x01 0x01 0x01 0x01 0x01 과 같이 읽힙니다.
STM32 보드로는 정상동작이 됩니다.

```
  reg_wizchip_cs_cbfunc (spiCSlow, spiCShigh);
  reg_wizchip_spi_cbfunc (spiRecvByte, spiSendByte);
  reg_wizchip_spiburst_cbfunc (spiRecvByteArray, spiSendByteArray);

  uint8_t rx_tx_buff_sizes[] =
{ 2, 2, 2, 2, 2, 2, 2, 2 };
  wizchip_init (rx_tx_buff_sizes, rx_tx_buff_sizes);

  wiz_NetInfo net_info =
{ .mac =
  { 0x00, 0x04, 0x05, 0x06, 0x07, 0x08 }, .dhcp = NETINFO_DHCP };

  setSHAR(net_info.mac);
  uint8_t mac[6] = { 0, };
  getSHAR(mac);
```

등록되는 callback 함수 입니다.

void spiCSlow(void)
{
GPIO_PinOutClear(SPI_CS_PORT, SPI_CS_PIN);
}

void spiCShigh(void)
{
GPIO_PinOutSet(SPI_CS_PORT, SPI_CS_PIN);
}

void spiSendByte(uint8_t sendbyte)
{
// app_log_debug(“tx:0x%.2x\r\n”, sendbyte);
USART_Tx(SPI_USART,sendbyte);
/* Wait for transmition to finished */
while (!(SPI_USART->STATUS & USART_STATUS_TXC)) ;

/* Return the datat read from SPI buffer */
USART_Rx(SPI_USART);
}

uint8_t spiRecvByte(void)
{
// app_log_debug(“tx:0x00…\r\n”);
USART_Tx(SPI_USART,0x00);
/* Wait for transmition to finished */
while (!(SPI_USART->STATUS & USART_STATUS_TXC)) ;

/* Return the datat read from SPI buffer */
uint8_t ret = USART_Rx(SPI_USART);
// app_log_debug(“rx:%.2x\r\n”, ret);
return ret;
}

void spiSendByteArray(uint8_t * buf, size_t size)
{
uint8_t *tx_ptr = buf;
size_t tx_count = size;
while (tx_count > 0)
{
// app_log_debug(“tx:0x%.2x\r\n”, *tx_ptr);
USART_Tx(SPI_USART,*tx_ptr);
tx_count–;
tx_ptr += sizeof(uint8_t);
while (!(SPI_USART->STATUS & USART_STATUS_TXC)) ;
USART_Rx(SPI_USART);
}
}

void spiRecvByteArray(uint8_t * buf, size_t size)
{
uint8_t *rx_ptr = buf;
size_t rx_count = size;
while (rx_count > 0)
{
// app_log_debug(“tx:0x00…\r\n”);
USART_Tx(SPI_USART,0x00);
while (!(SPI_USART->STATUS & USART_STATUS_TXC)) ;
*rx_ptr = USART_Rx(SPI_USART);
// app_log_debug(“rx:%.2x\r\n”, *rx_ptr);
rx_count–;
rx_ptr += sizeof(uint8_t);
}
}

SPI 관련하여 다음과 같이 설정하였습니다.

CMU_ClockEnable(cmuClock_HFPER, true);
/* Enable clock for USART3 */
CMU_ClockEnable(cmuClock_GPIO, true);
CMU_ClockEnable(SPI_CLK, true);

USART_InitSync_TypeDef init = USART_INITSYNC_DEFAULT;
init.autoCsEnable = true;
init.msbf = true;
init.baudrate = 1000000;

/* Configure SPI pins */
GPIO_PinModeSet(SPI_MOSI_PORT, SPI_MOSI_PIN, gpioModePushPull, 1);
GPIO_PinModeSet(SPI_MISO_PORT, SPI_MISO_PIN, gpioModeInput, 0);
GPIO_PinModeSet(SPI_CLK_PORT, SPI_CLK_PIN, gpioModePushPull, 0);
/* Keep CS high to not activate slave */
GPIO_PinModeSet(SPI_CS_PORT, SPI_CS_PIN, gpioModePushPull, 1);

/* Reset USART just in case */
USART_Reset(SPI_USART);

/* Configure to use SPI master with manual CS */
/* For now, configure SPI for worst case 32MHz clock in order to work for all */
/* configurations. */
USART_InitSync(SPI_USART, &init);

/* Module USART3 is configured to location 0 */
SPI_USART->ROUTELOC0 = (SPI_USART->ROUTELOC0 & ~(_USART_ROUTELOC0_TXLOC_MASK
| _USART_ROUTELOC0_RXLOC_MASK
| _USART_ROUTELOC0_CLKLOC_MASK
| _USART_ROUTELOC0_CSLOC_MASK))
| (TX_LOC &lt;&lt; _USART_ROUTELOC0_TXLOC_SHIFT)
| (RX_LOC &lt;&lt; _USART_ROUTELOC0_RXLOC_SHIFT)
| (CLK_LOC &lt;&lt; _USART_ROUTELOC0_CLKLOC_SHIFT)
| (CS_LOC &lt;&lt; _USART_ROUTELOC0_CSLOC_SHIFT);

/* Enable signals TX, RX, CLK, CS */
SPI_USART->ROUTEPEN |= USART_ROUTEPEN_TXPEN | USART_ROUTEPEN_RXPEN |
USART_ROUTEPEN_CLKPEN | USART_ROUTEPEN_CSPEN;

위와 동일한 SPI 설정으로 아두이노를 slave로 하여 SPI 통신 시 정상적으로 data 송수신이 됩니다.

어느 부분에 문제가 있는지 도움 부탁 드리겠습니다.

감사합니다.

## Replies

### Reply 1 by louis, 2021-08-17

안녕하세요

SPI와 UART는 별도의 프로토콜인데 SPI(SEND/RECV)함수 내부에서 UART를 실행하는 것 같습니다.
SPI로 칩과 통신하는 코드를 첨부해드리겠습니다. 참고부탁드리겠습니다.

![](https://github.githubassets.com/favicons/favicon.svg) [github.com](https://github.com/Wiznet/Loopback_FRDM-KL25Z_Eclipse/tree/master/src)

### [Loopback_FRDM-KL25Z_Eclipse/src at master · Wiznet/Loopback_FRDM-KL25Z_Eclipse](https://github.com/Wiznet/Loopback_FRDM-KL25Z_Eclipse/tree/master/src)

[master/src](https://github.com/Wiznet/Loopback_FRDM-KL25Z_Eclipse/tree/master/src)

Create a repository of WIZnet Freescale Example. Contribute to Wiznet/Loopback_FRDM-KL25Z_Eclipse development by creating an account on GitHub.

감사합니다.

### Reply 2 by Member 11121, 2021-08-18 (in reply to reply 1)

EFM32 mcu 에서는 SPIDRV라는 별도의 spi lib 를 이용하거나
다음과 같이 USART tx, rx, clk, scs 를 사용하여 spi를 구현하여 사용하는 것 같습니다.
위 구현 내용도 이걸 참고한 것입니다.

[github.com](https://github.com/hrshygoodness/EFM32-Library/blob/master/v2/an/an0032_efm32_ethernet/spi.c)

#### [hrshygoodness/EFM32-Library/blob/master/v2/an/an0032_efm32_ethernet/spi.c](https://github.com/hrshygoodness/EFM32-Library/blob/master/v2/an/an0032_efm32_ethernet/spi.c)

```
/**************************************************************************//**
 * @file spi.c
 * @brief SPI implementation of Board Control interface
 *        This implementation use the USART1 SPI interface to control board
 *        control registers.
 * @author Silicon Labs
 * @version 1.09
 ******************************************************************************
 * @section License
 * <b>(C) Copyright 2014 Silicon Labs, http://www.silabs.com</b>
 *******************************************************************************
 *
 * Permission is granted to anyone to use this software for any purpose,
 * including commercial applications, and to alter it and redistribute it
 * freely, subject to the following restrictions:
 *
 * 1. The origin of this software must not be misrepresented; you must not
 *    claim that you wrote the original software.
 * 2. Altered source versions must be plainly marked as such, and must not be
 *    misrepresented as being the original software.
```

This file has been truncated. [show original](https://github.com/hrshygoodness/EFM32-Library/blob/master/v2/an/an0032_efm32_ethernet/spi.c)

그러나 두 방법으로도 W5500과 spi 통신이 정상적으로 이루어 지지 않습니다.
(아두이노를 slave로 하여 ioLibrary 사용하여 mac 쓰기, 읽기 관련하여 spi 통신하면 원하는 데이터를 받습니다.)

### Reply 3 by irina, 2021-08-24

1. IoLibrary를 사용하신다면,
   아래와 같이 선택되어있는지 확인을 해주세요.
   [![image](https://maker.wiznet.io/forum_uploads/wiznet/original/2X/a/a6d32317e23aa4f344b21c53be6bb357652c7173.png) image854×185 8.2 KB](https://maker.wiznet.io/forum_uploads/wiznet/original/2X/a/a6d32317e23aa4f344b21c53be6bb357652c7173.png)

   [![image](https://maker.wiznet.io/forum_uploads/wiznet/original/2X/5/5e4d4a949a5e91151572206ac9b3c268f514968d.png) image875×236 11.7 KB](https://maker.wiznet.io/forum_uploads/wiznet/original/2X/5/5e4d4a949a5e91151572206ac9b3c268f514968d.png)

2. getVERSIONR() 를 이용하여, byte단위의 Version 값을 읽어보시길 바랍니다.
   정상적인 SPI 동작이라면, **0x04** 가 읽혀집니다.
   만일 동작하게 설정을 해놨음에도 동작하지 않는다면, SPI Frame을 정상적으로 보내고 있는지 확인하셔야합니다.
   ![image](https://maker.wiznet.io/forum_uploads/wiznet/original/2X/1/1db78f2f826874acd54c966f5f6df71ab318bdd5.png)

---

Source: https://maker.wiznet.io/forum/14321
