Added the initial implementation for the Oceanic Atom 2.

This commit is contained in:
Jef Driesen 2008-06-11 15:23:12 +00:00
parent ab5a3ffa55
commit 209aa53894
6 changed files with 398 additions and 2 deletions

View File

@ -9,7 +9,8 @@ bin_PROGRAMS = \
sensuspro \
sensusultra \
aladin \
memomouse
memomouse \
atom2
if IRDA
bin_PROGRAMS += smart
@ -31,6 +32,8 @@ aladin_SOURCES = uwatec_aladin_test.c
memomouse_SOURCES = uwatec_memomouse_test.c
atom2_SOURCES = oceanic_atom2_test.c
if IRDA
smart_SOURCES = uwatec_smart_test.c
endif

View File

@ -0,0 +1,102 @@
#include <stdio.h> // fopen, fwrite, fclose
#include "oceanic.h"
#include "utils.h"
#define WARNING(expr) \
{ \
message ("%s:%d: %s\n", __FILE__, __LINE__, expr); \
}
int test_dump_memory (const char* name, const char* filename)
{
unsigned char data[OCEANIC_ATOM2_MEMORY_SIZE] = {0};
atom2 *device = NULL;
message ("oceanic_atom2_open\n");
int rc = oceanic_atom2_open (&device, name);
if (rc != OCEANIC_SUCCESS) {
WARNING ("Error opening serial port.");
return rc;
}
message ("oceanic_atom2_read_version\n");
unsigned char version[OCEANIC_ATOM2_PACKET_SIZE] = {0};
rc = oceanic_atom2_read_version (device, version, sizeof (version));
if (rc != OCEANIC_SUCCESS) {
WARNING ("Cannot identify computer.");
oceanic_atom2_close (device);
return rc;
}
message ("oceanic_atom2_read_memory\n");
rc = oceanic_atom2_read_memory (device, 0x00, data, sizeof (data));
if (rc != OCEANIC_SUCCESS) {
WARNING ("Cannot read memory.");
oceanic_atom2_close (device);
return rc;
}
message ("Dumping data\n");
FILE* fp = fopen (filename, "wb");
if (fp != NULL) {
fwrite (data, sizeof (unsigned char), sizeof (data), fp);
fclose (fp);
}
message ("oceanic_atom2_close\n");
rc = oceanic_atom2_close (device);
if (rc != OCEANIC_SUCCESS) {
WARNING ("Cannot close device.");
return rc;
}
return OCEANIC_SUCCESS;
}
const char* errmsg (int rc)
{
switch (rc) {
case OCEANIC_SUCCESS:
return "Success";
case OCEANIC_ERROR:
return "Generic error";
case OCEANIC_ERROR_IO:
return "Input/output error";
case OCEANIC_ERROR_MEMORY:
return "Memory error";
case OCEANIC_ERROR_PROTOCOL:
return "Protocol error";
case OCEANIC_ERROR_TIMEOUT:
return "Timeout";
default:
return "Unknown error";
}
}
int main(int argc, char *argv[])
{
message_set_logfile ("ATOM2.LOG");
#ifdef _WIN32
const char* name = "COM1";
#else
const char* name = "/dev/ttyS0";
#endif
if (argc > 1) {
name = argv[1];
}
message ("DEVICE=%s\n", name);
int a = test_dump_memory (name, "ATOM2.DMP");
message ("\nSUMMARY\n");
message ("-------\n");
message ("test_dump_memory: %s\n", errmsg (a));
message_set_logfile (NULL);
return 0;
}

View File

@ -16,7 +16,9 @@ libdivecomputer_HEADERS = \
uwatec.h \
uwatec_aladin.h \
uwatec_memomouse.h \
uwatec_smart.h
uwatec_smart.h \
oceanic.h \
oceanic_atom2.h
#
# Source files.
@ -39,6 +41,8 @@ libdivecomputer_la_SOURCES = \
uwatec.h \
uwatec_aladin.h uwatec_aladin.c \
uwatec_memomouse.h uwatec_memomouse.c \
oceanic.h \
oceanic_atom2.h oceanic_atom2.c \
utils.h utils.c
if OS_WIN32

15
src/oceanic.h Normal file
View File

@ -0,0 +1,15 @@
#ifndef OCEANIC_H
#define OCEANIC_H
#define OCEANIC_SUCCESS 0
#define OCEANIC_ERROR -1
#define OCEANIC_ERROR_IO -2
#define OCEANIC_ERROR_MEMORY -3
#define OCEANIC_ERROR_PROTOCOL -4
#define OCEANIC_ERROR_TIMEOUT -5
typedef void (*dive_callback_t) (const unsigned char *data, unsigned int size, void *userdata);
#include "oceanic_atom2.h"
#endif /* OCEANIC_H */

248
src/oceanic_atom2.c Normal file
View File

@ -0,0 +1,248 @@
#include <string.h> // memcpy
#include <stdlib.h> // malloc, free
#include <assert.h> // assert
#include "oceanic.h"
#include "serial.h"
#include "utils.h"
#define MAXRETRIES 2
#define WARNING(expr) \
{ \
message ("%s:%d: %s\n", __FILE__, __LINE__, expr); \
}
struct atom2 {
struct serial *port;
};
static unsigned char
oceanic_atom2_checksum (const unsigned char data[], unsigned int size, unsigned char init)
{
unsigned char crc = init;
for (unsigned int i = 0; i < size; ++i)
crc += data[i];
return crc;
}
static int
oceanic_atom2_send (atom2 *device, const unsigned char command[], unsigned int csize)
{
// Send the command to the dive computer and
// wait until all data has been transmitted.
serial_write (device->port, command, csize);
serial_drain (device->port);
return OCEANIC_SUCCESS;
}
static int
oceanic_atom2_transfer (atom2 *device, const unsigned char command[], unsigned int csize, unsigned char answer[], unsigned int asize, int handshake)
{
assert (asize >= 2);
// Occasionally, the dive computer does not respond to a command.
// In that case we retry the command a number of times before
// returning an error. Usually the dive computer will respond
// again during one of the retries.
for (unsigned int i = 0;; ++i) {
// Send the command to the dive computer.
int rc = oceanic_atom2_send (device, command, csize);
if (rc != OCEANIC_SUCCESS) {
WARNING ("Failed to send the command.");
return rc;
}
// Receive the answer of the dive computer.
rc = serial_read (device->port, answer, asize);
if (rc != asize) {
WARNING ("Failed to receive the answer.");
if (rc == -1)
return OCEANIC_ERROR_IO;
if (i < MAXRETRIES)
continue; // Retry.
return OCEANIC_ERROR_TIMEOUT;
}
// Verify the header of the package.
unsigned char header = (handshake ? 0xA5 : 0x5A);
if (answer[0] != header) {
WARNING ("Unexpected answer start byte(s).");
return OCEANIC_ERROR_PROTOCOL;
}
// Verify the checksum of the package.
unsigned char crc = answer[asize - 1];
unsigned char ccrc = oceanic_atom2_checksum (answer + 1, asize - 2, 0x00);
if (crc != ccrc) {
WARNING ("Unexpected answer CRC.");
return OCEANIC_ERROR_PROTOCOL;
}
return OCEANIC_SUCCESS;
}
}
int
oceanic_atom2_open (atom2 **out, const char* name)
{
if (out == NULL)
return OCEANIC_ERROR;
// Allocate memory.
struct atom2 *device = malloc (sizeof (struct atom2));
if (device == NULL) {
WARNING ("Failed to allocate memory.");
return OCEANIC_ERROR_MEMORY;
}
// Set the default values.
device->port = NULL;
// Open the device.
int rc = serial_open (&device->port, name);
if (rc == -1) {
WARNING ("Failed to open the serial port.");
free (device);
return OCEANIC_ERROR_IO;
}
// Set the serial communication protocol (38400 8N1).
rc = serial_configure (device->port, 38400, 8, SERIAL_PARITY_NONE, 1, SERIAL_FLOWCONTROL_NONE);
if (rc == -1) {
WARNING ("Failed to set the terminal attributes.");
serial_close (device->port);
free (device);
return OCEANIC_ERROR_IO;
}
// Set the timeout for receiving data (3000 ms).
if (serial_set_timeout (device->port, 3000) == -1) {
WARNING ("Failed to set the timeout.");
serial_close (device->port);
free (device);
return OCEANIC_ERROR_IO;
}
// Make sure everything is in a sane state.
serial_flush (device->port, SERIAL_QUEUE_BOTH);
// Send the handshake to connect to the device.
unsigned char answer[3] = {0};
unsigned char command[3] = {0xA8, 0x99, 0x00};
rc = oceanic_atom2_transfer (device, command, sizeof (command), answer, sizeof (answer), 1);
if (rc != OCEANIC_SUCCESS) {
serial_close (device->port);
free (device);
return rc;
}
// Verify the handshake.
if (answer[1] != 0xA5) {
WARNING ("Unexpected handshake byte(s).");
serial_close (device->port);
free (device);
return OCEANIC_ERROR_PROTOCOL;
}
*out = device;
return OCEANIC_SUCCESS;
}
int
oceanic_atom2_close (atom2 *device)
{
if (device == NULL)
return OCEANIC_SUCCESS;
// Close the device.
if (serial_close (device->port) == -1) {
free (device);
return OCEANIC_ERROR_IO;
}
// Free memory.
free (device);
return OCEANIC_SUCCESS;
}
int
oceanic_atom2_read_version (atom2 *device, unsigned char data[], unsigned int size)
{
if (device == NULL)
return OCEANIC_ERROR;
if (size < OCEANIC_ATOM2_PACKET_SIZE)
return OCEANIC_ERROR_MEMORY;
unsigned char answer[OCEANIC_ATOM2_PACKET_SIZE + 2] = {0};
unsigned char command[2] = {0x84, 0x00};
int rc = oceanic_atom2_transfer (device, command, sizeof (command), answer, sizeof (answer), 0);
if (rc != OCEANIC_SUCCESS)
return rc;
memcpy (data, answer + 1, OCEANIC_ATOM2_PACKET_SIZE);
#ifndef NDEBUG
answer[OCEANIC_ATOM2_PACKET_SIZE + 1] = 0;
message ("ATOM2ReadVersion()=\"%s\"\n", answer + 1);
#endif
return OCEANIC_SUCCESS;
}
int
oceanic_atom2_read_memory (atom2 *device, unsigned int address, unsigned char data[], unsigned int size)
{
if (device == NULL)
return OCEANIC_ERROR;
assert (address % OCEANIC_ATOM2_PACKET_SIZE == 0);
assert (size % OCEANIC_ATOM2_PACKET_SIZE == 0);
// The data transmission is split in packages
// of maximum $OCEANIC_ATOM2_PACKET_SIZE bytes.
unsigned int nbytes = 0;
while (nbytes < size) {
// Read the package.
unsigned int number = address / OCEANIC_ATOM2_PACKET_SIZE;
unsigned char answer[OCEANIC_ATOM2_PACKET_SIZE + 2] = {0};
unsigned char command[4] = {0xB1,
(number >> 8) & 0xFF, // high
(number ) & 0xFF, // low
0};
int rc = oceanic_atom2_transfer (device, command, sizeof (command), answer, sizeof (answer), 0);
if (rc != OCEANIC_SUCCESS)
return rc;
memcpy (data, answer + 1, OCEANIC_ATOM2_PACKET_SIZE);
#ifndef NDEBUG
message ("ATOM2Read(0x%04x,%d)=\"", address, OCEANIC_ATOM2_PACKET_SIZE);
for (unsigned int i = 0; i < OCEANIC_ATOM2_PACKET_SIZE; ++i) {
message("%02x", data[i]);
}
message("\"\n");
#endif
nbytes += OCEANIC_ATOM2_PACKET_SIZE;
address += OCEANIC_ATOM2_PACKET_SIZE;
data += OCEANIC_ATOM2_PACKET_SIZE;
}
return OCEANIC_SUCCESS;
}

24
src/oceanic_atom2.h Normal file
View File

@ -0,0 +1,24 @@
#ifndef OCEANIC_ATOM2_H
#define OCEANIC_ATOM2_H
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
typedef struct atom2 atom2;
#define OCEANIC_ATOM2_MEMORY_SIZE 0xFFF0
#define OCEANIC_ATOM2_PACKET_SIZE 0x10
int oceanic_atom2_open (atom2 **device, const char* name);
int oceanic_atom2_close (atom2 *device);
int oceanic_atom2_read_version (atom2 *device, unsigned char data[], unsigned int size);
int oceanic_atom2_read_memory (atom2 *device, unsigned int address, unsigned char data[], unsigned int size);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* OCEANIC_ATOM2_H */