From: asterix Date: Mon, 29 Oct 2007 14:48:14 +0000 (+0000) Subject: Add emulator spi drive module. X-Git-Tag: 1.0.0~283 X-Git-Url: https://codewiz.org/gitweb?a=commitdiff_plain;h=950d7eddec58ba6c9cd82dc129e206c89be16e81;p=bertos.git Add emulator spi drive module. git-svn-id: https://src.develer.com/svnoss/bertos/trunk@959 38d2e660-2303-0410-9eaa-f027e97ec537 --- diff --git a/drv/spi_emu.c b/drv/spi_emu.c new file mode 100644 index 00000000..9d15ca19 --- /dev/null +++ b/drv/spi_emu.c @@ -0,0 +1,118 @@ +/** + * \file + * + * + * \brief Emulated SPI Master for DSP firmware download (impl.) + * + * \version $Id: spi.c 15328 2007-03-21 15:07:04Z batt $ + * + * \author Francesco Sacchi + * \author Daniele Basile + */ + + +#include +#include +#include +#include "hw_spi.h" + +void spi_assertSS(void) +{ + ATOMIC(SS_ACTIVE()); +} + +void spi_deassertSS(void) +{ + ATOMIC(SS_INACTIVE()); +} + +/** + * Send byte \c c over MOSI line, CONFIG_SPI_DATAORDER first. + * SS pin state is left unchanged. + */ +uint8_t spi_sendRecv(uint8_t c) +{ + uint8_t data = 0; + uint8_t shift = SPI_DATAORDER_START; + + + ATOMIC( + for (int i = 0; i < 8; i++) + { + /* Shift the i-th bit to MOSI */ + if (c & shift) + MOSI_HIGH(); + else + MOSI_LOW(); + /* Assert clock */ + SCK_ACTIVE(); + data |= IS_MISO_HIGH() ? shift : 0; + /* De-assert clock */ + SCK_INACTIVE(); + SPI_DATAORDER_SHIFT(shift); + } + ); + return data; +} + +MOD_DEFINE(spi); +void spi_init(void) +{ + ATOMIC(SPI_HW_INIT()); + MOD_INIT(spi); +} + +/** + * Read \param len from spi, and put it in \param buff. + */ +void spi_read(void *_buff, size_t len) +{ + uint8_t *buff = (uint8_t *)_buff; + + while (len--) + /* Read byte from spi and put it in buffer. */ + *buff++ = spi_sendRecv(0); + +} + +/** + * Write \param len to spi, and take it from \param buff. + */ +void spi_write(const void *_buff, size_t len) +{ + const uint8_t *buff = (const uint8_t *)_buff; + + while (len--) + /* Write byte pointed at by *buff to spi */ + spi_sendRecv(*buff++); + +} diff --git a/drv/spi_emu.h b/drv/spi_emu.h new file mode 100644 index 00000000..ff716906 --- /dev/null +++ b/drv/spi_emu.h @@ -0,0 +1,72 @@ +/** + * \file + * + * + * \brief Emulated SPI Master for DSP firmware download (interface) + * + * \version $Id: spi.h 15321 2007-03-21 14:45:12Z asterix $ + * + * \author Francesco Sacchi + * \author Daniele Basile + */ + + +#ifndef DRV_SPI_EMU_H +#define DRV_SPI_EMU_H + +#include +#include + +/** + * Define send and receive order bit. + * \{ + */ +#define SPI_LSB_FIRST 1 +#define SPI_MSB_FIRST 2 +/* \} */ + +#if CONFIG_SPI_DATAORDER == SPI_LSB_FIRST + #define SPI_DATAORDER_START 1 + #define SPI_DATAORDER_SHIFT(i) ((i) <<= 1) +#elif CONFIG_SPI_DATAORDER == SPI_MSB_FIRST + #define SPI_DATAORDER_START 0x80 + #define SPI_DATAORDER_SHIFT(i) ((i) >>= 1) +#endif + +void spi_write(const void *buf, size_t len); +void spi_read(void *buf, size_t len); +uint8_t spi_sendRecv(uint8_t c); +void spi_init(void); +void spi_assertSS(void); +void spi_deassertSS(void); + +#endif /* DRV_SPI_EMU_H */