/* Writes a number of bytes to memory provided the data does not exceed page boundaries. At the start of the buffer is placed the address at which the write will take place. The write size cannot exceed remaining page size. Maximum page size is 32 bytes. If the size of the write would exceed the remaining size of the page, the function exits with negative error code. */
int32_t memory_write_within_page(struct memory_desc *dev, uint16_t address, uint8_t *data, uint32_t size)
/* Writes a number of bytes to memory provided the data does not exceed page boundaries. At the start of the buffer is placed the address at which the write will take place. The write size cannot exceed remaining page size. Maximum page size is 32 bytes. If the size of the write would exceed the remaining size of the page, the function exits with negative error code. */
int32_t memory_write_within_page(struct memory_desc *dev, uint16_t address, uint8_t *data, uint32_t size)
{
	int32_t ret;
	uint8_t *transmit_array;
	uint32_t page_remainder;
	uint8_t i;
	page_remainder = address % WRITE_SIZE_LIMIT;
	page_remainder = WRITE_SIZE_LIMIT - page_remainder;
	if(page_remainder < size)
		return WRITE_SIZE_ERROR;
	transmit_array = calloc((size + 2), sizeof *transmit_array);
	if (!transmit_array)
		return -1;
	transmit_array[0] = (address & 0xff00) >> 8;
	transmit_array[1] = (address & 0x00ff) >> 0;
	for(i = 0; i < size; i++)
		transmit_array[i + 2] = data[i];
	ret = i2c_write(dev->i2c_dev, transmit_array, size + 2, true);
	free(transmit_array);
	mdelay(5);
	return ret;
}