X-Git-Url: https://rtime.felk.cvut.cz/gitweb/fpga/rpi-motor-control.git/blobdiff_plain/a54714b1caefa707bad771a992278d073b1377ee..59b72ac21a7189e402f90a288889174af4fe3015:/pmsm-control/test_sw/rp_spi.c diff --git a/pmsm-control/test_sw/rp_spi.c b/pmsm-control/test_sw/rp_spi.c new file mode 100644 index 0000000..a1de3ce --- /dev/null +++ b/pmsm-control/test_sw/rp_spi.c @@ -0,0 +1,257 @@ +/* + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "rp_spi.h" /*struct rpi_in */ + +//#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) +#define ARRAY_SIZE 16 +static void pabort(const char *s) +{ + perror(s); + abort(); +} + +static const char *device = "/dev/spidev0.1"; +static uint8_t mode = 0; +static uint8_t bits = 8; +static uint32_t speed = 500000; +static uint16_t delay = 0; +static int fd; + +/** + * \brief Testovaci funkce. Odesle same nuly. Delka 64bit. Vysledek vypise. + */ +void transfer() +{ + int ret; + uint8_t tx[]={0,0,0,0,0,0,0,0} ;/*= { + 0x00, 0x00, 0x01, 0x23, 0x45, 0x67, + 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23, + 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, + 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, + 0xCD, 0xEF + };/**/ + printf("ARRAY_SIZE=%d\n",ARRAY_SIZE); + uint8_t rx[ARRAY_SIZE] = {0, }; /* inicializace vsech prvku na nulu? */ + struct spi_ioc_transfer tr = { + .tx_buf = (unsigned long)tx, + .rx_buf = (unsigned long)rx, + .len = ARRAY_SIZE, + .delay_usecs = delay, + .speed_hz = speed, + .bits_per_word = bits, + }; + + ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr); + if (ret < 1) + pabort("can't send spi message"); + + for (ret = 0; ret < ARRAY_SIZE; ret++) { + if (!(ret % 6)) + puts(""); + printf("%.2X ", rx[ret]); + } + puts(""); +} +struct rpi_in spi_read(uint8_t * tx) +{ + uint8_t *uint8_p; + uint16_t tmp; + int sign; + struct rpi_in in; + int ret; + + uint8_t rx[ARRAY_SIZE] = {0, }; /* inicializace vsech prvku na nulu? */ + struct spi_ioc_transfer tr = { + .tx_buf = (unsigned long)tx, + .rx_buf = (unsigned long)rx, + .len = ARRAY_SIZE, + .delay_usecs = delay, + .speed_hz = speed, + .bits_per_word = bits, + }; + + ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr); + /*vypisovani prichozich dat */ + /*/ + if (ret < 1) + pabort("can't send spi message"); + + for (ret = 0; ret < ARRAY_SIZE; ret++) { + if (!(ret % 6)) + puts(""); + printf("%.2X ", rx[ret]); + } + puts(""); + /*/ + + /*prichozi data: + * rx[0] - bity 95 downto 88 - bits, that come first + * rx[1] - bity 87 downto 80 + * rx[2] - bity 79 downto 72 + * rx[3] - bity 71 downto 64 + * rx[4] - bity 63 downto 56 + * rx[5] - bity 55 downto 48 + * rx[6] - bity 47 downto 40 + * rx[7] - bity 39 downto 32 + * rx[8] - bity 31 downto 24 + * rx[9] - bity 23 downto 16 + * rx[10] - bity 15 downto 8 + * rx[11] - bity 7 downto 0 + */ + + /*uprava endianity pozice*/ + uint8_p=(uint8_t*)&in.pozice; + /* x86 je Little-Endian */ + uint8_p[0]=rx[3]; /* LSB */ + uint8_p[1]=rx[2]; + uint8_p[2]=rx[1]; + uint8_p[3]=rx[0]; /*MSB*/ /*with sign bit*/ + uint8_p[4]=uint8_p[5]=uint8_p[6]=uint8_p[7]=0; + + /*halove sondy + * hal1 - bit63 + * hal2 - bit62 + * hal3 - bit61 + */ + in.hal1=!!(0x80 & rx[4]); + in.hal2=!!(0x40 & rx[4]); + in.hal3=!!(0x20 & rx[4]); + + /*pwm enable + * en1 - bit60 + * en2 - bit59 + * en2 - bit58 + */ + in.en1=!!(0x10 & rx[4]); + in.en2=!!(0x08 & rx[4]); + in.en3=!!(0x04 & rx[4]); + + /*shutdown + * shdn1 - bit57 + * shdn2 - bit56 + * shdn3 - bit55 + */ + in.shdn1=!!(0x02 & rx[4]); + in.shdn2=!!(0x01 & rx[4]); + in.shdn3=!!(0x80 & rx[5]); + + /*debug bits + * + */ + in.b54=!!(0x40 & rx[5]); + in.b53=!!(0x20 & rx[5]); + in.b52=!!(0x10 & rx[5]); + in.b51=!!(0x08 & rx[5]); + in.b50=!!(0x04 & rx[5]); + in.b49=!!(0x02 & rx[5]); + in.b48=!!(0x01 & rx[5]); + in.b47=!!(0x80 & rx[6]); + in.b46=!!(0x40 & rx[6]); + in.b45=!!(0x20 & rx[6]); + in.b44=!!(0x10 & rx[6]); + in.b43=!!(0x08 & rx[6]); + in.b42=!!(0x04 & rx[6]); + in.b41=!!(0x02 & rx[6]); + in.b40=!!(0x01 & rx[6]); + in.b39=!!(0x80 & rx[7]); + in.b38=!!(0x40 & rx[7]); + in.b37=!!(0x20 & rx[7]); + in.b36=!!(0x10 & rx[7]); + + /** currents + * ch0 - bits 35 downto 24 + * 35..32 in rx[7] - last 4 + * 31..24 in rx[8] - all bytte + * ch1 - bits 23 downto 12 + * 23..16 in rx[9] - all byte + * 15..12 in rx[10] - first 4 + * ch2 - bits 11 downto 0 + * 11..8 in rx[10] - last 4 + * 7..0 in rx[11] - all byte + */ + + in.ch0=0x0F & rx[7]; + in.ch0<<=8; + in.ch0|=rx[8]; + + in.ch1= rx[9]; + in.ch1<<=8; + in.ch1|=(rx[10] & 0xF0); + in.ch1>>=4; + + in.ch2=(0xF & rx[10]); + in.ch2<<=8; + in.ch2|=rx[11]; + + + return in; + + +} + +int spi_init() +{ + int ret = 0; + + fd = open(device, O_RDWR); + if (fd < 0) + pabort("can't open device"); + printf("device open\n"); + /* + * spi mode + */ + ret = ioctl(fd, SPI_IOC_WR_MODE, &mode); + if (ret == -1) + pabort("can't set spi mode"); + + ret = ioctl(fd, SPI_IOC_RD_MODE, &mode); + if (ret == -1) + pabort("can't get spi mode"); + + /* + * bits per word + */ + ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits); + if (ret == -1) + pabort("can't set bits per word"); + + ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits); + if (ret == -1) + pabort("can't get bits per word"); + + /* + * max speed hz + */ + ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed); + if (ret == -1) + pabort("can't set max speed hz"); + + ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed); + if (ret == -1) + pabort("can't get max speed hz"); + + printf("spi mode: %d\n", mode); + printf("bits per word: %d\n", bits); + printf("delay: %d\n", delay); + printf("max speed: %d Hz (%d KHz)\n", speed, speed/1000); + + + return ret; +} + +void spi_disable(){ + close(fd); + +}