diff --git a/sw/airborne/arch/omap/mcu_periph/spi_arch.c b/sw/airborne/arch/omap/mcu_periph/spi_arch.c new file mode 100644 index 0000000000..eb5783482e --- /dev/null +++ b/sw/airborne/arch/omap/mcu_periph/spi_arch.c @@ -0,0 +1,180 @@ +/* + * Copyright (C) 2014 Felix Ruess + * + * This file is part of paparazzi. + * + * paparazzi is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * paparazzi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with paparazzi; see the file COPYING. If not, see + * . + */ + +/** + * @file arch/omap/mcu_periph/spi_arch.c + * + */ + +#include +#include +#include +#include +#include + +#include +#include + +#include "mcu_periph/spi.h" + + +void spi_init_slaves(void) +{ + /* for now we assume that each SPI device has it's SLAVE CS already set up + * e.g. in pin muxing of BBB + */ +} + +bool_t spi_submit(struct spi_periph* p, struct spi_transaction* t) +{ + int fd = (int)p->reg_addr; + + struct spi_ioc_transfer xfer; + memset(&xfer, 0, sizeof xfer); + + /* length in bytes of transaction */ + uint8_t buf_len = Max(t->input_length, t->output_length); + + /* handle transactions with different input/output length */ + if (buf_len > t->output_length) { + uint8_t tx_buf[buf_len]; + memset(tx_buf, 0, sizeof tx_buf); + /* copy bytes to transmit to larger buffer, rest filled with zero */ + memcpy(tx_buf, (void*)t->output_buf, t->output_length); + xfer.tx_buf = (unsigned long)tx_buf; + } + else { + xfer.tx_buf = (unsigned long)t->output_buf; + } + + if (buf_len > t->input_length) { + uint8_t rx_buf[buf_len]; + memset(rx_buf, 0, sizeof rx_buf); + xfer.rx_buf = (unsigned long)rx_buf; + } + else { + xfer.rx_buf = (unsigned long)t->input_buf; + } + + xfer.len = buf_len; + /* fixed speed of 1Mhz for now, use SPIClockDiv?? */ + xfer.speed_hz = 1000000; + xfer.delay_usecs = 0; + if (t->dss == SPIDss16bit) { + xfer.bits_per_word = 16; + } else { + xfer.bits_per_word = 8; + } + if (t->select == SPISelectUnselect || t->select == SPIUnselect) { + xfer.cs_change = 1; + } + + if (ioctl(fd, SPI_IOC_MESSAGE(1), xfer) < 0) { + t->status = SPITransFailed; + return FALSE; + } + + /* copy recieved data if we had to use an extra rx_buffer */ + if (buf_len > t->input_length) { + memcpy((void*)t->input_buf, (void*)xfer.rx_buf, t->input_length); + } + + t->status = SPITransSuccess; + return TRUE; +} + +bool_t spi_lock(struct spi_periph* p, uint8_t slave) { + // not implemented + return FALSE; +} + +bool_t spi_resume(struct spi_periph* p, uint8_t slave) { + // not implemented + return FALSE; +} + + +#if USE_SPI0 +void spi0_arch_init(void) +{ + int fd = open("/dev/spidev1.0", O_RDWR); + + if (fd < 0) { + perror("Could not open SPI device /dev/spidev1.0"); + spi0.reg_addr = NULL; + return; + } + spi0.reg_addr = (void*)fd; + + /* spi mode */ + if (ioctl(fd, SPI_IOC_WR_MODE, (SPI_CPOL|SPI_CPHA)) < 0) { + perror("SPI0: can't set spi mode"); + } + + /* set to MSB first */ + if (ioctl(fd, SPI_IOC_WR_LSB_FIRST, 0) < 0) { + perror("SPI0: can't set spi bit justification"); + } + + /* bits per word default to 8 */ + if (ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, 8) < 0) { + perror("SPI0: can't set bits per word"); + } + + /* max speed in hz, 1MHz for now */ + if (ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, 1000000) < 0) { + perror("SPI0: can't set max speed hz"); + } +} +#endif /* USE_SPI0 */ + +#if USE_SPI1 +void spi1_arch_init(void) +{ + int fd = open("/dev/spidev1.1", O_RDWR); + + if (fd < 0) { + perror("Could not open SPI device /dev/spidev1.1"); + spi1.reg_addr = NULL; + return; + } + spi1.reg_addr = (void*)fd; + + /* spi mode */ + if (ioctl(fd, SPI_IOC_WR_MODE, (SPI_CPOL|SPI_CPHA)) < 0) { + perror("SPI1: can't set spi mode"); + } + + /* set to MSB first */ + if (ioctl(fd, SPI_IOC_WR_LSB_FIRST, 0) < 0) { + perror("SPI1: can't set spi bit justification"); + } + + /* bits per word default to 8 */ + if (ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, 8) < 0) { + perror("SPI1: can't set bits per word"); + } + + /* max speed in hz, 1MHz for now */ + if (ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, 1000000) < 0) { + perror("SPI1: can't set max speed hz"); + } +} +#endif /* USE_SPI1 */ diff --git a/sw/airborne/arch/omap/mcu_periph/spi_arch.h b/sw/airborne/arch/omap/mcu_periph/spi_arch.h new file mode 100644 index 0000000000..f5d8d53eb6 --- /dev/null +++ b/sw/airborne/arch/omap/mcu_periph/spi_arch.h @@ -0,0 +1,30 @@ +/* + * Copyright (C) 2014 Felix Ruess + * + * This file is part of paparazzi. + * + * paparazzi is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * paparazzi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with paparazzi; see the file COPYING. If not, see + * . + */ + +/** + * @file arch/omap/mcu_periph/spi_arch.h + * Handling of SPI hardware for Linux/OMAP. + */ + +#ifndef SPI_ARCH_H +#define SPI_ARCH_H + + +#endif // SPI_ARCH_H