zephyr: drivers: spi: Apollo3 Blue MCU is not able to communicate with SX1262

Apollo3 Blue MCU is not able to communicate with SX1262 transceiver over spi. For test purposes, I used sample/subsys/lorawan/class_a example project.

The code is running on RAK11720 module from RAKwireless. It is based on Apollo3 Blue SoC with a Semtech SX1262 transceiver. Currently, I am trying to add rak11720 board support to zephyr project.

This overlay file can be used to compile class_a example project: apollo3_evb-overlay.txt

There are two problems. Firstly, I need a custom CS support for Apollo3. Current board uses pin 11 as CS pin for SPI 1 peripheral. Secondly, current spi_ambiq_xfer function is not able to set up SX1262 chip. I prepared a sample function to set up chip over spi correctly. Via this function, I am able to join a LoRaWAN network.

static int spi_ambiq_xfer(const struct device *dev, const struct spi_config *config)
{
    struct spi_ambiq_data *data = dev->data;
    struct spi_context *ctx = &data->ctx;
	am_hal_iom_transfer_t trans = {0};
    int ret = 0;
	uint8_t tx_buf[256] = {0};
	uint8_t rx_buf[256] = {0};

	while(1)
	{
		uint8_t offset = 0;
		memset(&trans, 0x00, sizeof(am_hal_iom_transfer_t));
		memset(&tx_buf, 0x00, sizeof(am_hal_iom_transfer_t));
		memset(&rx_buf, 0x00, sizeof(am_hal_iom_transfer_t));

		if ((ctx->tx_len != 0) && (ctx->rx_len != 0))
		{
			if(ctx->rx_buf == NULL)
			{
				offset = ctx->rx_len;
				spi_context_update_rx(ctx, 1, ctx->rx_len);
 			}

			/* Check Length */
			if(ctx->tx_len > sizeof(tx_buf))
			{
				return -1;
 			}

			if(ctx->rx_len > sizeof(rx_buf))
			{
				return -1;
 			}

			uint32_t len = offset + ctx->rx_len;
			memcpy(tx_buf, ctx->tx_buf, ctx->tx_len);

			trans.eDirection      = AM_HAL_IOM_FULLDUPLEX;
			trans.ui32NumBytes    = len;
			trans.pui32TxBuffer   = (uint32_t *) tx_buf;
			trans.pui32RxBuffer   = (uint32_t *) rx_buf;
			
			ret = am_hal_iom_spi_blocking_fullduplex(data->iom_handler, &trans);
			if(ret != 0)
			{
				LOG_ERR("Spi full duplex comm error: %d", ret);
				return ret;
			}

			/* Copy response to rx_buf */
			memcpy(ctx->rx_buf, &rx_buf[offset], ctx->rx_len);

			/* Move buffers */
			spi_context_update_tx(ctx, 1, ctx->tx_len);
			spi_context_update_rx(ctx, 1, ctx->rx_len);
		}
		else if (ctx->tx_len != 0)
		{
			if(ctx->tx_buf == NULL)
			{
				spi_context_update_tx(ctx, 1, ctx->tx_len);
			}

			trans.eDirection      = AM_HAL_IOM_TX;
			trans.ui32NumBytes    = ctx->tx_len;
			trans.pui32TxBuffer   = (uint32_t *) ctx->tx_buf;

			ret = am_hal_iom_blocking_transfer(data->iom_handler, &trans);
			if(ret != 0)
			{
				LOG_ERR("Spi tx comm error: %d", ret);
				return ret;
 			}

			spi_context_update_tx(ctx, 1, ctx->tx_len);
		}
		else if (ctx->rx_len != 0)
		{
			if(ctx->rx_buf == NULL)
			{
				spi_context_update_rx(ctx, 1, ctx->rx_len);
			}

			trans.eDirection      = AM_HAL_IOM_RX;
			trans.ui32NumBytes    = ctx->rx_len;
			trans.pui32RxBuffer   = (uint32_t *) ctx->rx_buf;

			ret = am_hal_iom_blocking_transfer(data->iom_handler, &trans);
			if(ret != 0)
			{
				LOG_ERR("Spi rx comm error: %d", ret);
				return ret;
 			}

			spi_context_update_rx(ctx, 1, ctx->rx_len);
		}
		else
		{
			break;
 		}
	}

	spi_context_complete(ctx, dev, 0);

    return ret;
}

Please check following file to see how I add custom CS support and spi_ambiq_xfer function. spi_ambiq-c.txt

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 20 (20 by maintainers)

Most upvoted comments

Hello @AlessandroLuo,

Rak11720 is working as expected with following patched.

Thanks for the support.

@srcnert yes, please wait for the PR, I did some additional modifications to simplify the transfer functions, need more tests with other devices.