/** Attempt to synchronize with an Analog Device ARM micro. Sends a backspace and reads back the microcontrollers response. Performs multiple retries. Exits the program on error, returns to caller in the case of success. */ static void AnalogDevicesSync(ISP_ENVIRONMENT *IspEnvironment) { BINARY sync; /* Holds sync command. */ AD_SYNC_RESPONSE response; /* Response from micro. */ int sync_attempts; /* Number of retries. */ /* Make sure we don't read garbage later instead of the */ /* response we expect from the micro. */ ClearSerialPortBuffers(IspEnvironment); DebugPrintf(2, "Synchronizing\n"); /* Progress report. */ sync = ANALOG_DEVICES_SYNC_CHAR; /* Build up sync command. */ /* Perform the actual sync attempt. First send the sync */ /* character, the attempt to read back the response. For the */ /* AD ARM micro this is a fixed length block. If response is */ /* received attempt to validate it by comparing the first */ /* characters to those expected. If the received block does */ /* not validate or is incomplete empty the serial buffer and */ /* retry. */ for (sync_attempts = 0; sync_attempts < 5; sync_attempts++) { SendComPortBlock(IspEnvironment, &sync, 1); if (ReceiveComPortBlockComplete(IspEnvironment, &response, sizeof(response), 500) == 0) { if (memcmp(response.product_id, ANALOG_DEVICES_SYNC_RESPONSE, ANALOG_DEVICES_SYNC_SIZE) == 0) { return; } else { DumpString(3, &response, sizeof(response), "Unexpected response to sync attempt "); } } else { DebugPrintf(3, "No (or incomplete) answer on sync attempt\n"); } ClearSerialPortBuffers(IspEnvironment); } DebugPrintf(1, "No (or unacceptable) answer on sync attempt\n"); exit(4); }
/** Send a previously form Analog Devices communication. Retry a couple of times if needed but fail by exiting the program if no ACK is forthcoming. \param [in] packet the packet to send. */ static void AnalogDevicesSendPacket(ISP_ENVIRONMENT *IspEnvironment, const AD_PACKET * packet) { BINARY response; int retry = 0; do { retry++; /* Make sure we don't read garbage later instead of */ /* the response we expect from the micro. */ ClearSerialPortBuffers(IspEnvironment); /* Send the packet, the size is the number of data */ /* bytes in the packet plus 3 bytes worth of header */ /* plus checksum. */ SendComPortBlock(IspEnvironment, packet, packet->bytes + 4); /* Receive the response and check, return to caller */ /* if successful. */ if (ReceiveComPortBlockComplete(IspEnvironment, &response, 1, 5000) == 0) { if (response == ANALOG_DEVICES_ACK) { DebugPrintf(3, "Packet Sent\n"); return; } if (response != ANALOG_DEVICES_NAK) { DebugPrintf(3, "Unexpected response to packet (%x)\n", (int)response); } DebugPrintf(2, "*"); } } while (retry < 3); DebugPrintf(1, "Send packet failed\n"); exit(-1); }
/** Sends a string out the opened com port. \param [in] s string to send. */ void SendComPort(const char *s) { SendComPortBlock(s, strlen(s)); }