u16 mip_interface_update(mip_interface *device_interface) { u32 num_bytes, bytes_read = 0, bytes_written = 0; u8 local_buffer[MIP_INTERFACE_INPUT_RING_BUFFER_SIZE]; u32 port_bytes; //The parser must be initialized if(device_interface->state != MIP_INTERFACE_INITIALIZED) return MIP_INTERFACE_ERROR; //Determine the max number of bytes to read num_bytes = MIP_INTERFACE_INPUT_RING_BUFFER_SIZE; if(ring_buffer_remaining_entries(&device_interface->input_buffer) < num_bytes) num_bytes = ring_buffer_remaining_entries(&device_interface->input_buffer); port_bytes = mip_sdk_port_read_count(device_interface->port_handle); if(num_bytes > port_bytes) num_bytes = port_bytes; //Read up to max ring buffer size from the port if(num_bytes > 0) mip_sdk_port_read(device_interface->port_handle, local_buffer, num_bytes, &bytes_read, MIP_INTERFACE_PORT_READ_TIMEOUT_MS); //Write the local buffer to the ring buffer if(bytes_read > 0) { //printf("Got %d bytes\n", bytes_read); ring_buffer_write_multi(&device_interface->input_buffer, local_buffer, bytes_read, &bytes_written); } //Parse the data __mip_interface_parse_input_buffer(device_interface); return MIP_INTERFACE_OK; }
u16 mip_interface_write_blocking(mip_interface *device_interface, u8 *data, u32 num_bytes, u32 *bytes_written, u32 timeout_ms) { u32 initial_time = mip_sdk_get_time_ms(); //Check that the parser is initialized if(device_interface->state != MIP_INTERFACE_INITIALIZED) return MIP_INTERFACE_ERROR; //Cannot write more bytes than the buffer size if(num_bytes > MIP_INTERFACE_INPUT_RING_BUFFER_SIZE) return MIP_INTERFACE_ERROR; //Spin until the buffer has enough room while(num_bytes > ring_buffer_remaining_entries(&device_interface->input_buffer)) { if(__mip_interface_time_timeout(initial_time, timeout_ms) == MIP_INTERFACE_TIMEOUT) return MIP_INTERFACE_ERROR; } return mip_interface_write(device_interface, data, num_bytes, bytes_written); }