0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Raspberry Pi: SPI 全二重通信への対応

Posted at

こちらで示した Raspberry Pi のマスタープログラムの改善です。
ESP32: SPI Slave

全二重通信の為に、送信と受信が同時に行われます。
その為、受信結果は、ひとつ前の送信データに対する結果になります。

そこで、期待する結果を得る為に次のようにしました。
第一ステップ
 データを送る
第二ステップ
 ダミーデータを送り、第一ステップで送ったデータの結果を受け取る

プログラム

master.c
// ------------------------------------------------------------
//	master.c
//
//					Jun/26/2025
// ------------------------------------------------------------
#include <bcm2835.h>
#include <stdio.h>
#include <unistd.h>
// ------------------------------------------------------------
uint8_t send_read_proc(uint8_t send_data)
{
   uint8_t read_data = bcm2835_spi_transfer(send_data);

   return read_data;
}

// ------------------------------------------------------------
int main(int argc, char **argv)
{
   if (!bcm2835_init())
   	return 1;
   bcm2835_spi_begin();
   bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST);	  // The default
   bcm2835_spi_setDataMode(BCM2835_SPI_MODE0);				   // The default
   bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_65536); // The default
   bcm2835_spi_chipSelect(BCM2835_SPI_CS0);					  // The default
   bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW);	  // the default

   const uint8_t send_data[] = {0x30,0x31,0x32,0x33,0x34,0x35,0x36};
   int nnx = sizeof(send_data);
   for (int it=0; it<nnx; it++)
   	{
   	const uint8_t dummy[] = {0x00};
   	const uint8_t aa = send_read_proc(send_data[it]);
   	usleep(10 * 10000);
   	const uint8_t bb = send_read_proc(dummy[0]);
   	usleep(10 * 10000);
   printf("Sent to SPI: 0x%02X. Read back from SPI: 0x%02X.\n", send_data[it], bb);
   	}

   bcm2835_spi_end();
   bcm2835_close();
   return 0;
}

// ------------------------------------------------------------

実行結果

$ sudo ./master 
Sent to SPI: 0x30. Read back from SPI: 0x35.
Sent to SPI: 0x31. Read back from SPI: 0x36.
Sent to SPI: 0x32. Read back from SPI: 0x37.
Sent to SPI: 0x33. Read back from SPI: 0x38.
Sent to SPI: 0x34. Read back from SPI: 0x39.
Sent to SPI: 0x35. Read back from SPI: 0x3A.
Sent to SPI: 0x36. Read back from SPI: 0x3B.
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?