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通信 (複数バイトの送信)

Last updated at Posted at 2025-06-24

こちらを参考にしました。
bcm2835_spi_transfern()

1バイトの送信例はこちら
Raspberry Pi: SPI通信

プログラム

spin.c
// ------------------------------------------------------------
/*
	spin.c
*/
// ------------------------------------------------------------
#include <bcm2835.h>
#include <stdio.h>
#include <string.h>

// ------------------------------------------------------------
void send_read_proc(char buffer[])
{
	uint32_t len = 0;
	len = strlen(buffer);
	printf("len = %d\n", len);	

	printf("%s\n",buffer);

bcm2835_spi_transfern(buffer,len);
 
	printf("%s\n",buffer);

	printf("*** end ***\n");
}

// ------------------------------------------------------------
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


	char buffer[] = "0123456789";

	strcpy(buffer,"abc");
	uint8_t send_data = 0x23;
	send_read_proc(buffer);


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

// ------------------------------------------------------------
Makefile
spin: spin.c
	gcc -o spin spin.c -l bcm2835
clean:
	rm -f spin

コンパイル

make

実行

sudo ./spin

GPIO9 と GPIO10 を接続している時の実行結果

$ sudo ./spin 
len = 3
abc
abc
*** end ***

GPIO9 と GPIO10 を接続していない時の実行結果

buffer がクリアされています。

$ sudo ./spin 
len = 3
abc

*** end ***
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?