Merge pull request #302 from Zaltora/spi_fixes

Fixes SPI Bug(s)
This commit is contained in:
Ruslan V. Uss 2016-12-05 19:08:17 +06:00 committed by GitHub
commit 61c3d509e5
2 changed files with 25 additions and 1 deletions

View file

@ -221,6 +221,26 @@ uint32_t spi_transfer_32(uint8_t bus, uint32_t data)
return res;
}
static void _rearm_extras_bit(uint8_t bus, bool arm) {
if(!_minimal_pins[bus]) return ;
static uint8_t status[2] ;
if (arm)
{
if (status[bus] & 0x01) SPI(bus).USER0 |= (SPI_USER0_ADDR) ;
if (status[bus] & 0x02) SPI(bus).USER0 |= (SPI_USER0_COMMAND) ;
if (status[bus] & 0x04) SPI(bus).USER0 |= (SPI_USER0_DUMMY | SPI_USER0_MISO);
status[bus] = 0;
}
else
{
if (SPI(bus).USER0 & SPI_USER0_ADDR) { SPI(bus).USER0 &= ~(SPI_USER0_ADDR) ; status[bus] |= 0x01 ; }
if (SPI(bus).USER0 & SPI_USER0_COMMAND) { SPI(bus).USER0 &= ~(SPI_USER0_COMMAND) ; status[bus] |= 0x02 ; }
if (SPI(bus).USER0 & SPI_USER0_DUMMY) { SPI(bus).USER0 &= ~(SPI_USER0_DUMMY | SPI_USER0_MISO); status[bus] |= 0x04 ; }
}
}
size_t spi_transfer(uint8_t bus, const void *out_data, void *in_data, size_t len, spi_word_size_t word_size)
{
if (!out_data || !len) return 0;
@ -234,6 +254,7 @@ size_t spi_transfer(uint8_t bus, const void *out_data, void *in_data, size_t len
size_t offset = i * _SPI_BUF_SIZE;
_spi_buf_transfer(bus, (const uint8_t *)out_data + offset,
in_data ? (uint8_t *)in_data + offset : NULL, buf_size, e, word_size);
if (blocks) _rearm_extras_bit(bus, false) ;
}
uint8_t tail = len % buf_size;
@ -243,6 +264,7 @@ size_t spi_transfer(uint8_t bus, const void *out_data, void *in_data, size_t len
in_data ? (uint8_t *)in_data + blocks * _SPI_BUF_SIZE : NULL, tail, e, word_size);
}
if (blocks) _rearm_extras_bit(bus, true) ;
return len;
}
@ -253,6 +275,7 @@ static void _repeat_send(uint8_t bus, uint32_t* dword,int32_t* repeats,spi_word_
{
uint16_t bytes_to_transfer = __min(*repeats * size , _SPI_BUF_SIZE);
_wait(bus);
if (i) _rearm_extras_bit(bus, false) ;
_set_size(bus,bytes_to_transfer);
for(i = 0; i < (bytes_to_transfer + 3) / 4;i++)
SPI(bus).W[i] = *dword; //need test with memcpy !
@ -260,6 +283,7 @@ static void _repeat_send(uint8_t bus, uint32_t* dword,int32_t* repeats,spi_word_
*repeats -= (bytes_to_transfer / size ) ;
}
_wait(bus);
_rearm_extras_bit(bus, true) ;
}
void spi_repeat_send_8(uint8_t bus, uint8_t data,int32_t repeats)

View file

@ -317,9 +317,9 @@ static inline void spi_set_command(uint8_t bus,uint8_t bits, uint16_t data)
static inline void spi_set_address(uint8_t bus,uint8_t bits, uint32_t data)
{
if(!bits) return ;
SPI(bus).USER1 = SET_FIELD(SPI(bus).USER1, SPI_USER1_ADDR_BITLEN, --bits);
SPI(bus).USER0 |= SPI_USER0_ADDR ; //enable ADDRess function in SPI module
SPI(bus).ADDR = data<<(32-bits) ; //align address data to high bits
SPI(bus).USER1 = SET_FIELD(SPI(bus).USER1, SPI_USER1_ADDR_BITLEN, --bits);
}
/**