Commit 18cc7a85 authored by David Mondou's avatar David Mondou

Added busy wait function.

Sending over a large file without waiting for busy flag to clear was causing Out Of Memory Error on Target.
parent f9296af7
......@@ -138,6 +138,46 @@ int send_cmd(int device_fd, struct utp_cmd *cmd, void *dxferp, int dxferp_len, u
return 0;
}
/* busy_wait */
/* Wait for device to clear busy flag */
/* Returns 0 when busy flag is cleared */
/* Returns 1 when BUSY_CHECK_COUNT is exceeded */
int busy_wait(int device_fd)
{
int ret = 0;
int i;
uint8_t reply = UTP_REPLY_BUSY;
// Wait until not busy
for (i = 0; i < BUSY_CHECK_COUNT; i++)
{
usleep(BUSY_SLEEP);
ret = send_cmd(device_fd, &poll, "", 0, &reply);
if (ret)
{
fprintf(stderr, "Send_cmd failed (poll)\n");
ret = 1;
break;
}
if (reply == 0)
{
break;
}
if (i == BUSY_CHECK_COUNT - 1)
{
fprintf(stderr, "Device is busy\n");
ret = 1;
break;
}
}
return ret;
}
int main(int argc, char * argv[])
{
int c;
......@@ -244,6 +284,7 @@ int main(int argc, char * argv[])
memcpy(exec_send.data + FILE_SIZE_OFFSET, &st.st_size, sizeof(uint32_t));
ret = send_cmd(device_fd, &exec_send, command, strlen(command), NULL);
if (ret)
{
fprintf(stderr, "Send_cmd failed (exec_send)\n");
......@@ -257,8 +298,14 @@ int main(int argc, char * argv[])
while (data_written < st.st_size)
{
int write_size = st.st_size - data_written > MAX_SENT_DATA_SIZE ? MAX_SENT_DATA_SIZE : st.st_size - data_written;
uint8_t reply = UTP_REPLY_BUSY;
ret = send_cmd(device_fd, &put_send, file_data + data_written, write_size, &reply);
if( ret == 0 && reply == UTP_REPLY_BUSY)
{
ret = busy_wait(device_fd);
}
ret = send_cmd(device_fd, &put_send, file_data + data_written, write_size, NULL);
if (ret)
{
fprintf(stderr, "Send_cmd failed (put_send)\n");
......@@ -277,31 +324,8 @@ int main(int argc, char * argv[])
fprintf(stderr, "Send_cmd failed (exec)\n");
}
// Wait until not busy
int i;
uint8_t reply = UTP_REPLY_BUSY;
for (i = 0; i < BUSY_CHECK_COUNT; i++)
{
usleep(BUSY_SLEEP);
ret = send_cmd(device_fd, &poll, "", 0, &reply);
if (ret)
{
fprintf(stderr, "Send_cmd failed (poll)\n");
break;
}
if (reply == 0)
{
break;
}
if (i == BUSY_CHECK_COUNT - 1)
{
fprintf(stderr, "Device is busy\n");
ret = 1;
break;
}
if( ret == 0 ){
ret = busy_wait(device_fd);
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment