#include #include #include #include #include #include #include #include #include #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) static uint8_t mode; static uint8_t bits = 8; static uint32_t speed = 5000000; // 5MHz static uint16_t delay; void transfert(int fd, uint8_t tx[], int i) { int ret; FILE* save = NULL; char buffer[3]; char buffer1[3]="01"; uint8_t rx[ARRAY_SIZE(tx)] = {0, }; struct spi_ioc_transfer tr = { .tx_buf = (unsigned long)tx, .rx_buf = (unsigned long)rx, .len = ARRAY_SIZE(tx), .delay_usecs = delay, .speed_hz = speed, .bits_per_word = bits, }; ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr); if (i!=0) { printf("\n#--SPI--#\nDATA HEX: %02X\n", rx[3]); sprintf(buffer,"%i", rx[3]); //converti la donnée de la mémoire en char printf("DATA DEC: %s\n",buffer); } i=0; } void WREN (int fd) // write read enable { int i=0; uint8_t tx[1]; tx[0] = 0x06; // envoi wren transfert(fd,tx,i); } void ecrireframserie (int fd, uint8_t trame[]) { uint8_t tx[4]; int i=0; tx[0] = 0x02; // mode ecriture tx[1] = trame[0]; // 5 premiers bits non comptés 3 derniers adresse tx[2] = trame[1]; // adresse 8 bits tx[3] = trame[2]; // data 8 bits transfert(fd,tx,i); } void lireframserie (int fd, uint8_t trame[]) { uint8_t tx[4]; int i=1; tx[0] = 0x03; // mode lecture tx[1] = trame[0]; // 5 premiers bits nuls 3 derniers adresse tx[2] = trame[1]; // adresse 8 bits tx[3] = 0x00; // data 8 bits transfert(fd,tx,i); } int main (int argc, char *argv[]) //spi { int i=0; uint8_t trame[5]; char B[10]; int fd = open("/dev/spidev1.0", O_RDWR); if (argc==4) // provient d'un execl { for (i=0; i < argc; i++) { strcpy(B,argv[i]); trame[i] = atoi(B); // trame 0=add 1, trame 1=add 2, trame2=data, trame3=mode } } if (argc==5) // provient de la console { for (i=1; i < argc; i++) { strcpy(B,argv[i]); trame[i-1] = atoi(B); // trame 0=add 1, trame 1=add 2, trame2=data, trame3=mode } } if (trame[3]==2) { WREN(fd); ecrireframserie(fd,trame); printf("\n#--SPI--#\nECRITURE REUSSIE\n"); } if (trame[3]==1) { lireframserie(fd,trame); } return 0; }