#include #include "defines.h" #include "kspi.h" #include "kfile.h" #include "ktiva.h" #include "ksync.h" ///////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////// int kspi_write_mode(struct file *pf, unsigned char mode) { int ret; ret = kf_ioctl(pf, SPI_IOC_WR_MODE, (unsigned long)&mode); if(ret < 0) { KALERT("%s: kf_ioctl failed: %d\n", __FUNCTION__, ret); } return ret; } ///////////////////////////////////////////////////////////////////////////// int kspi_read_mode(struct file *pf, unsigned char *mode) { int ret; ret = kf_ioctl(pf, SPI_IOC_RD_MODE, (unsigned long)mode); if(ret < 0) { KALERT("%s: kf_ioctl failed: %d\n", __FUNCTION__, ret); } return ret; } ///////////////////////////////////////////////////////////////////////////// int kspi_write_bits_per_word(struct file *pf, unsigned char bits) { int ret; ret = kf_ioctl(pf, SPI_IOC_WR_BITS_PER_WORD, (unsigned long)&bits); if(ret < 0) { KALERT("%s: kf_ioctl failed: %d\n", __FUNCTION__, ret); } return ret; } ///////////////////////////////////////////////////////////////////////////// int kspi_read_bits_per_word(struct file *pf, unsigned char *bits) { int ret; ret = kf_ioctl(pf, SPI_IOC_RD_BITS_PER_WORD, (unsigned long)bits); if(ret < 0) { KALERT("%s: kf_ioctl failed: %d\n", __FUNCTION__, ret); } return ret; } ///////////////////////////////////////////////////////////////////////////// int kspi_write_max_speed_hz(struct file *pf, unsigned int speed) { int ret; ret = kf_ioctl(pf, SPI_IOC_WR_MAX_SPEED_HZ, (unsigned long)&speed); if(ret < 0) { KALERT("%s: kf_ioctl failed: %d\n", __FUNCTION__, ret); } return ret; } ///////////////////////////////////////////////////////////////////////////// int kspi_read_max_speed_hz(struct file *pf, unsigned int *speed) { int ret; ret = kf_ioctl(pf, SPI_IOC_RD_MAX_SPEED_HZ, (unsigned long)speed); if(ret < 0) { KALERT("%s: kf_ioctl failed: %d\n", __FUNCTION__, ret); } return ret; } ///////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////// int kspi_tx(struct file *pf, const void *pData, size_t nCbData) { int ret; struct spi_ioc_transfer tr = { .tx_buf = (unsigned long)pData, .rx_buf = 0, .len = nCbData, // .delay_usecs = 0, // .speed_hz = _SPI_SPEED_HZ, // .bits_per_word = _SPI_BITS_PER_WORD, }; ret = kf_ioctl(pf, SPI_IOC_MESSAGE(1), (unsigned long)&tr); if(ret < 0) KALERT("%s failed: %d\n", __FUNCTION__, ret); return ret; } ///////////////////////////////////////////////////////////////////////////// int kspi_rx(struct file *pf, void *pData, size_t nCbData) { int ret; struct spi_ioc_transfer tr = { .tx_buf = 0, .rx_buf = (unsigned long)pData, .len = nCbData, // .delay_usecs = 0, // .speed_hz = _SPI_SPEED_HZ, // .bits_per_word = _SPI_BITS_PER_WORD, }; ret = kf_ioctl(pf, SPI_IOC_MESSAGE(1), (unsigned long)&tr); if(ret < 0) KALERT("%s failed: %d\n", __FUNCTION__, ret); return ret; } ///////////////////////////////////////////////////////////////////////////// int kspi_tx_rx(struct file *pf, const void *pTx, void *pRx, size_t nCb) { int ret; struct spi_ioc_transfer tr = { .tx_buf = (unsigned long)pTx, .rx_buf = (unsigned long)pRx, .len = nCb, // .delay_usecs = 0, // .speed_hz = _SPI_SPEED_HZ, // .bits_per_word = _SPI_BITS_PER_WORD, }; ret = kf_ioctl(pf, SPI_IOC_MESSAGE(1), (unsigned long)&tr); if(ret < 0) KALERT("%s failed: %d\n", __FUNCTION__, ret); return ret; } ///////////////////////////////////////////////////////////////////////////// int kspi_rx_byte(struct file *pf, unsigned char *rx) { return kspi_rx(pf, rx, 1); } ///////////////////////////////////////////////////////////////////////////// int kspi_tx_byte(struct file *pf, unsigned char tx) { return kspi_tx(pf, &tx, 1); } ///////////////////////////////////////////////////////////////////////////// int kspi_tx_rx_byte(struct file *pf, unsigned char tx, unsigned char *rx) { return kspi_tx_rx(pf, &tx, rx, 1); } ///////////////////////////////////////////////////////////////////////////// bool KSpiInit(struct file *pf) { bool bRet = false; uint8_t mode = 0; uint8_t bits = 0; uint32_t speed = 0; do { if(kspi_write_mode(pf, SPI_MODE_3) < 0) { KALERT("%s: kspi_write_mode failed!\n", __FUNCTION__); break; } if(kspi_read_mode(pf, &mode) < 0) { KALERT("%s: kspi_read_mode failed!\n", __FUNCTION__); break; } if(mode != SPI_MODE_3) { KALERT("%s: Invalid mode!\n", __FUNCTION__); break; } ///////////////////////////////////////////////////////////////// if(kspi_write_bits_per_word(pf, _SPI_BITS_PER_WORD) < 0) { KALERT("%s: kspi_write_bits_per_word failed!\n", __FUNCTION__); break; } if(kspi_read_bits_per_word(pf, &bits) < 0) { KALERT("%s: kspi_read_bits_per_word failed!\n", __FUNCTION__); break; } if(bits != _SPI_BITS_PER_WORD) { KALERT("%s: Invalid bits per word!\n", __FUNCTION__); break; } ///////////////////////////////////////////////////////////////// if(kspi_write_max_speed_hz(pf, _SPI_SPEED_HZ) < 0) { KALERT("%s: kspi_write_max_speed_hz failed!\n", __FUNCTION__); break; } if(kspi_read_max_speed_hz(pf, &speed) < 0) { KALERT("%s: kspi_read_max_speed_hz failed!\n", __FUNCTION__); break; } if(speed != _SPI_SPEED_HZ) { KALERT("%s: Invalid max. speed!\n", __FUNCTION__); break; } bRet = true; } while(false); return bRet; } void KSpiExit(void) { }