1.优化读取写入字节保护。
This commit is contained in:
@@ -88,13 +88,7 @@ static mr_ssize_t mr_dac_write(mr_device_t device, mr_pos_t pos, const void *buf
|
||||
mr_uint32_t *write_buffer = (mr_uint32_t *)buffer;
|
||||
mr_size_t write_size = 0;
|
||||
|
||||
/* Check size */
|
||||
if (size < sizeof(*write_buffer))
|
||||
{
|
||||
return -MR_ERR_INVALID;
|
||||
}
|
||||
|
||||
for (write_size = 0; write_size < size; write_size += sizeof(*write_buffer))
|
||||
while ((write_size += sizeof(*write_buffer)) <= size)
|
||||
{
|
||||
dac->ops->write(dac, pos, *write_buffer);
|
||||
write_buffer++;
|
||||
|
||||
@@ -246,9 +246,9 @@ static mr_ssize_t mr_i2c_device_read(mr_device_t device, mr_pos_t pos, void *buf
|
||||
mr_i2c_device_send_address(i2c_device, MR_I2C_RD);
|
||||
|
||||
/* Blocking read */
|
||||
for (read_size = 0; read_size < size; read_size += sizeof(*read_buffer))
|
||||
while ((read_size += sizeof(*read_buffer)) <= size)
|
||||
{
|
||||
*read_buffer = i2c_bus->ops->read(i2c_bus, (mr_state_t)(read_size < (size - sizeof(*read_buffer))));
|
||||
*read_buffer = i2c_bus->ops->read(i2c_bus, (mr_state_t)(read_size != size));
|
||||
read_buffer++;
|
||||
}
|
||||
|
||||
@@ -298,7 +298,8 @@ static mr_ssize_t mr_i2c_device_write(mr_device_t device, mr_pos_t pos, const vo
|
||||
}
|
||||
}
|
||||
|
||||
for (write_size = 0; write_size < size; write_size += sizeof(*write_buffer))
|
||||
/* Block write */
|
||||
while ((write_size += sizeof(*write_buffer)) <= size)
|
||||
{
|
||||
i2c_bus->ops->write(i2c_bus, *write_buffer);
|
||||
write_buffer++;
|
||||
|
||||
@@ -60,7 +60,7 @@ static mr_ssize_t mr_pin_read(mr_device_t device, mr_pos_t pos, void *buffer, mr
|
||||
mr_level_t *read_buffer = (mr_level_t *)buffer;
|
||||
mr_size_t read_size = 0;
|
||||
|
||||
for (read_size = 0; read_size < size; read_size += sizeof(*read_buffer))
|
||||
while ((read_size += sizeof(*read_buffer)) <= size)
|
||||
{
|
||||
*read_buffer = pin->ops->read(pin, pos);
|
||||
read_buffer++;
|
||||
@@ -75,7 +75,7 @@ static mr_ssize_t mr_pin_write(mr_device_t device, mr_pos_t pos, const void *buf
|
||||
mr_level_t *write_buffer = (mr_level_t *)buffer;
|
||||
mr_size_t write_size = 0;
|
||||
|
||||
for (write_size = 0; write_size < size; write_size += sizeof(*write_buffer))
|
||||
while ((write_size += sizeof(*write_buffer)) <= size)
|
||||
{
|
||||
pin->ops->write(pin, pos, *write_buffer);
|
||||
write_buffer++;
|
||||
|
||||
@@ -163,7 +163,7 @@ static mr_ssize_t mr_serial_read(mr_device_t device, mr_pos_t pos, void *buffer,
|
||||
if (serial->rx_fifo.size == 0)
|
||||
{
|
||||
/* Blocking read */
|
||||
for (read_size = 0; read_size < size; read_size += sizeof(*read_buffer))
|
||||
while ((read_size += sizeof(*read_buffer)) <= size)
|
||||
{
|
||||
*read_buffer = serial->ops->read(serial);
|
||||
read_buffer++;
|
||||
@@ -171,7 +171,7 @@ static mr_ssize_t mr_serial_read(mr_device_t device, mr_pos_t pos, void *buffer,
|
||||
} else
|
||||
{
|
||||
/* Non-blocking read */
|
||||
for (read_size = 0; read_size < size;)
|
||||
while (read_size < size)
|
||||
{
|
||||
read_size += mr_rb_read(&serial->rx_fifo, read_buffer + read_size, size - read_size);
|
||||
}
|
||||
@@ -189,7 +189,7 @@ static mr_ssize_t mr_serial_write(mr_device_t device, mr_pos_t pos, const void *
|
||||
if (serial->tx_fifo.size == 0 || ((device->open_flag & MR_OPEN_NONBLOCKING) == MR_FALSE))
|
||||
{
|
||||
/* Blocking write */
|
||||
for (write_size = 0; write_size < size; write_size += sizeof(*write_buffer))
|
||||
while ((write_size += sizeof(*write_buffer)) <= size)
|
||||
{
|
||||
serial->ops->write(serial, *write_buffer);
|
||||
write_buffer++;
|
||||
@@ -197,7 +197,7 @@ static mr_ssize_t mr_serial_write(mr_device_t device, mr_pos_t pos, const void *
|
||||
} else
|
||||
{
|
||||
/* Non-blocking write */
|
||||
for (write_size = 0; write_size < size;)
|
||||
while (write_size < size)
|
||||
{
|
||||
/* If this is the first write, start sending */
|
||||
if (mr_rb_get_data_size(&serial->tx_fifo) != 0)
|
||||
|
||||
@@ -31,7 +31,7 @@ static mr_uint8_t err_io_spi_read(mr_spi_bus_t spi_bus)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void err_io_spi_cs_ctrl(mr_spi_bus_t spi_bus, mr_pos_t cs_number, mr_state_t state)
|
||||
static void err_io_spi_cs_write(mr_spi_bus_t spi_bus, mr_pos_t cs_number, mr_uint8_t value)
|
||||
{
|
||||
|
||||
}
|
||||
@@ -256,7 +256,7 @@ static mr_ssize_t mr_spi_device_read(mr_device_t device, mr_pos_t pos, void *buf
|
||||
if (spi_device->config.host_slave == MR_SPI_HOST)
|
||||
{
|
||||
/* Start the chip-select of the current device */
|
||||
spi_bus->ops->cs_ctrl(spi_bus, spi_device->cs_number, MR_ENABLE);
|
||||
spi_bus->ops->cs_write(spi_bus, spi_device->cs_number, spi_device->config.cs_active);
|
||||
|
||||
/* Send position */
|
||||
if (pos != 0)
|
||||
@@ -271,20 +271,20 @@ static mr_ssize_t mr_spi_device_read(mr_device_t device, mr_pos_t pos, void *buf
|
||||
}
|
||||
|
||||
/* Blocking read */
|
||||
for (read_size = 0; read_size < size; read_size += sizeof(*read_buffer))
|
||||
while ((read_size += sizeof(*read_buffer)) <= size)
|
||||
{
|
||||
*read_buffer = mr_spi_device_transfer(spi_device, 0u);
|
||||
read_buffer++;
|
||||
}
|
||||
|
||||
/* Stop the chip-select of the current device */
|
||||
spi_bus->ops->cs_ctrl(spi_bus, spi_device->cs_number, MR_DISABLE);
|
||||
spi_bus->ops->cs_write(spi_bus, spi_device->cs_number, !spi_device->config.cs_active);
|
||||
} else
|
||||
{
|
||||
if (spi_bus->rx_fifo.size == 0)
|
||||
{
|
||||
/* Blocking read */
|
||||
for (read_size = 0; read_size < size; read_size += sizeof(*read_buffer))
|
||||
while ((read_size += sizeof(*read_buffer)) <= size)
|
||||
{
|
||||
*read_buffer = mr_spi_device_transfer(spi_device, 0u);
|
||||
read_buffer++;
|
||||
@@ -292,7 +292,7 @@ static mr_ssize_t mr_spi_device_read(mr_device_t device, mr_pos_t pos, void *buf
|
||||
} else
|
||||
{
|
||||
/* Non-blocking read */
|
||||
for (read_size = 0; read_size < size;)
|
||||
while (read_size < size)
|
||||
{
|
||||
read_size += mr_rb_read(&spi_bus->rx_fifo, read_buffer + read_size, size - read_size);
|
||||
}
|
||||
@@ -326,7 +326,7 @@ static mr_ssize_t mr_spi_device_write(mr_device_t device, mr_pos_t pos, const vo
|
||||
if (spi_device->config.host_slave == MR_SPI_HOST)
|
||||
{
|
||||
/* Start the chip-select of the current device */
|
||||
spi_bus->ops->cs_ctrl(spi_bus, spi_device->cs_number, MR_ENABLE);
|
||||
spi_bus->ops->cs_write(spi_bus, spi_device->cs_number, spi_device->config.cs_active);
|
||||
|
||||
/* Send position */
|
||||
if (pos != 0)
|
||||
@@ -340,17 +340,19 @@ static mr_ssize_t mr_spi_device_write(mr_device_t device, mr_pos_t pos, const vo
|
||||
}
|
||||
}
|
||||
|
||||
for (write_size = 0; write_size < size; write_size += sizeof(*write_buffer))
|
||||
/* Blocking write */
|
||||
while ((write_size += sizeof(*write_buffer)) <= size)
|
||||
{
|
||||
mr_spi_device_transfer(spi_device, *write_buffer);
|
||||
write_buffer++;
|
||||
}
|
||||
|
||||
/* Stop the chip-select of the current device */
|
||||
spi_bus->ops->cs_ctrl(spi_bus, spi_device->cs_number, MR_DISABLE);
|
||||
spi_bus->ops->cs_write(spi_bus, spi_device->cs_number, !spi_device->config.cs_active);
|
||||
} else
|
||||
{
|
||||
for (write_size = 0; write_size < size; write_size += sizeof(*write_buffer))
|
||||
/* Blocking write */
|
||||
while ((write_size += sizeof(*write_buffer)) <= size)
|
||||
{
|
||||
mr_spi_device_transfer(spi_device, *write_buffer);
|
||||
write_buffer++;
|
||||
@@ -497,7 +499,7 @@ mr_err_t mr_spi_bus_add(mr_spi_bus_t spi_bus, const char *name, struct mr_spi_bu
|
||||
ops->configure = ops->configure ? ops->configure : err_io_spi_configure;
|
||||
ops->write = ops->write ? ops->write : err_io_spi_write;
|
||||
ops->read = ops->read ? ops->read : err_io_spi_read;
|
||||
ops->cs_ctrl = ops->cs_ctrl ? ops->cs_ctrl : err_io_spi_cs_ctrl;
|
||||
ops->cs_write = ops->cs_write ? ops->cs_write : err_io_spi_cs_write;
|
||||
ops->cs_read = ops->cs_read ? ops->cs_read : err_io_spi_cs_read;
|
||||
spi_bus->ops = ops;
|
||||
|
||||
|
||||
@@ -102,10 +102,10 @@ typedef struct mr_spi_device *mr_spi_device_t;
|
||||
*/
|
||||
struct mr_spi_bus_ops
|
||||
{
|
||||
mr_err_t (*configure)(mr_spi_bus_t spi_bus, struct mr_spi_config *config);
|
||||
mr_err_t (*configure)(mr_spi_bus_t spi_bus, mr_spi_config_t config);
|
||||
void (*write)(mr_spi_bus_t spi_bus, mr_uint8_t data);
|
||||
mr_uint8_t (*read)(mr_spi_bus_t spi_bus);
|
||||
void (*cs_ctrl)(mr_spi_bus_t spi_bus, mr_pos_t cs_number, mr_state_t state);
|
||||
void (*cs_write)(mr_spi_bus_t spi_bus, mr_pos_t cs_number, mr_uint8_t value);
|
||||
mr_uint8_t (*cs_read)(mr_spi_bus_t spi_bus, mr_pos_t cs_number);
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user