Fixed error of writing to SPI registers with memcpy (#352)
fixed memcpy() writing registers bug
This commit is contained in:
parent
20674f8470
commit
1457a3ed19
1 changed files with 44 additions and 28 deletions
|
@ -189,7 +189,9 @@ static void _spi_buf_transfer(uint8_t bus, const void *out_data, void *in_data,
|
|||
_wait(bus);
|
||||
size_t bytes = len * (uint8_t)word_size;
|
||||
_set_size(bus, bytes);
|
||||
memcpy((void *)SPI(bus).W, out_data, bytes);
|
||||
// memcpy((void *)SPI(bus).W, out_data, bytes); // <- It's buggy
|
||||
for (uint8_t i = 0; i < bytes; i ++)
|
||||
((uint8_t *)SPI(bus).W)[i] = ((uint8_t *)out_data)[i];
|
||||
_spi_buf_prepare(bus, len, e, word_size);
|
||||
_start(bus);
|
||||
_wait(bus);
|
||||
|
@ -221,23 +223,36 @@ 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] ;
|
||||
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);
|
||||
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 ; }
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -254,7 +269,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) ;
|
||||
if (blocks) _rearm_extras_bit(bus, false);
|
||||
}
|
||||
|
||||
uint8_t tail = len % buf_size;
|
||||
|
@ -264,41 +279,42 @@ 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) ;
|
||||
if (blocks) _rearm_extras_bit(bus, true);
|
||||
return len;
|
||||
}
|
||||
|
||||
static void _repeat_send(uint8_t bus, uint32_t* dword,int32_t* repeats,spi_word_size_t size)
|
||||
static void _repeat_send(uint8_t bus, uint32_t *dword, int32_t *repeats,
|
||||
spi_word_size_t size)
|
||||
{
|
||||
uint8_t i = 0 ;
|
||||
while(*repeats > 0)
|
||||
uint8_t i = 0;
|
||||
while (*repeats > 0)
|
||||
{
|
||||
uint16_t bytes_to_transfer = __min(*repeats * size , _SPI_BUF_SIZE);
|
||||
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++)
|
||||
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 !
|
||||
_start(bus);
|
||||
*repeats -= (bytes_to_transfer / size ) ;
|
||||
*repeats -= (bytes_to_transfer / size);
|
||||
}
|
||||
_wait(bus);
|
||||
_rearm_extras_bit(bus, true) ;
|
||||
_rearm_extras_bit(bus, true);
|
||||
}
|
||||
|
||||
void spi_repeat_send_8(uint8_t bus, uint8_t data,int32_t repeats)
|
||||
void spi_repeat_send_8(uint8_t bus, uint8_t data, int32_t repeats)
|
||||
{
|
||||
uint32_t dword = data << 24 | data << 16 | data << 8 | data;
|
||||
_repeat_send(bus,&dword,&repeats, SPI_8BIT);
|
||||
_repeat_send(bus, &dword, &repeats, SPI_8BIT);
|
||||
}
|
||||
|
||||
void spi_repeat_send_16(uint8_t bus, uint16_t data,int32_t repeats)
|
||||
void spi_repeat_send_16(uint8_t bus, uint16_t data, int32_t repeats)
|
||||
{
|
||||
uint32_t dword = data << 16 | data;
|
||||
_repeat_send(bus,&dword,&repeats, SPI_16BIT);
|
||||
_repeat_send(bus, &dword, &repeats, SPI_16BIT);
|
||||
}
|
||||
|
||||
void spi_repeat_send_32(uint8_t bus, uint32_t data,int32_t repeats)
|
||||
void spi_repeat_send_32(uint8_t bus, uint32_t data, int32_t repeats)
|
||||
{
|
||||
_repeat_send(bus,&data,&repeats, SPI_32BIT);
|
||||
_repeat_send(bus, &data, &repeats, SPI_32BIT);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue