proto.h File Reference

#include <stdint.h>
Include dependency graph for proto.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Defines

#define IONKBD_START   0xC0
#define IONKBD_ESC   0xDB
#define IONKBD_ESC_START   0xDC
#define IONKBD_ESC_ESC   0xDD
#define IONKBD_REQ_GET_STATUS   0x04
#define IONKBD_REQ_SET_POWER   0x06
#define IONKBD_REQ_SET_MODE   0x07
#define IONKBD_REQ_GET_VERSION   0x08
#define IONKBD_REQ_GET_BATTERY   0x0A
#define IONKBD_REQ_ERASE_FLASH   0x0E
#define IONKBD_REQ_PROGRAM_FLASH   0x0F
#define IONKBD_REQ_READ_FLASH   0x10
#define IONKBD_REQ_FLASH_GASGAUGE   0x13
#define IONKBD_REQ_GET_GG_VERSION   0x14
#define IONKBD_REQ_GET_DEVICETYPE   0x19
#define IONKBD_REQ_ERASE_FLASH_BOOT   0x1F
#define IONKBD_REQ_PROGRAM_FLASH_BOOT   0x20
#define IONKBD_REQ_READ_FLASH_BOOT   0x21
#define IONKBD_RESP_STATUS   0x40
#define IONKBD_RESP_VERSION   0x41
#define IONKBD_RESP_BATTERY   0x43
#define IONKBD_RESP_ERASE   0x45
#define IONKBD_RESP_FLASH   0x46
#define IONKBD_RESP_READ   0x47
#define IONKBD_RESP_GASGAUGE   0x4A
#define IONKBD_RESP_GG_VERSION   0x4B
#define IONKBD_RESP_DEVICETYPE   0x4E
#define GASGAUGE_INIT   0x00
#define GASGAUGE_START   0x01
#define GASGAUGE_SAVE_SETTINGS   0x02
#define GASGAUGE_ERASE   0x03
#define GASGAUGE_SETUP_ROW   0x04
#define GASGAUGE_PROGRAM_ROW   0x05
#define GASGAUGE_RESTORE_SETTINGS   0x06
#define GASGAUGE_STOP   0x07
#define DR1000_APPL_ADDR_MIN   0x1800
#define DR1000_APPL_ADDR_MAX   0xE000
#define DR800_APPL_ADDR_MIN   0xC000
#define DR800_APPL_ADDR_MAX   0xE7FF
#define DR800_BOOT_ADDR_MIN   0xE800
#define DR800_BOOT_ADDR_MAX   0xFFFF
#define DEVICETYPE_ADDR   0x1040
#define MODE_BOOT   0
#define MODE_APP   1

Functions

void fatalnum (int num, char *message,...)
void fatal (char *message,...)
void warning (char *message,...)
void ionkbd_send_packet (uint8_t cmd, uint8_t length, uint8_t *data)
int ionkbd_get_response (uint8_t *command, uint8_t *length, uint8_t *data)
int ionkbd_send_request (uint8_t cmd, uint8_t length, uint8_t *data, uint8_t *rcmd, uint8_t *rlen, uint8_t *rdata)
int sector_erase (int mode, uint16_t addr)
void micro_erase (int mode, uint16_t start, uint16_t end)
int chunk_program (int mode, uint16_t addr, uint8_t len, uint8_t *chunk)
int dr1000_micro_program_application (char *filename)
int dr800_micro_program_application (char *filename)
int dr800_micro_program_bootloader (char *filename)
int micro_read_devicetype (int mode)
int micro_write_devicetype (int mode, int devtype)
int micro_check_battery_charge (uint16_t min_level)
int micro_get_versions (uint16_t *bootloader, uint16_t *application)
void micro_enter_mode (uint8_t boot)
int micro_get_mode ()
void micro_get_devicetype (uint8_t *type)
int gasgauge_get_versions (uint16_t *firmware, uint16_t *hardware)
int gasgauge_flash (char *filename)
void port_open (char *name, int flags, int speed)

Define Documentation

#define DEVICETYPE_ADDR   0x1040

Definition at line 107 of file proto.h.

Referenced by micro_read_devicetype(), and micro_write_devicetype().

#define DR1000_APPL_ADDR_MAX   0xE000

Definition at line 99 of file proto.h.

Referenced by dr1000_micro_program_application(), and handle_firmware().

#define DR1000_APPL_ADDR_MIN   0x1800

Definition at line 98 of file proto.h.

Referenced by dr1000_micro_program_application(), and handle_firmware().

#define DR800_APPL_ADDR_MAX   0xE7FF

Definition at line 102 of file proto.h.

Referenced by handle_firmware().

#define DR800_APPL_ADDR_MIN   0xC000

Definition at line 101 of file proto.h.

Referenced by dr800_micro_program_application(), and handle_firmware().

#define DR800_BOOT_ADDR_MAX   0xFFFF

Definition at line 105 of file proto.h.

Referenced by handle_firmware().

#define DR800_BOOT_ADDR_MIN   0xE800

Definition at line 104 of file proto.h.

Referenced by dr800_micro_program_bootloader(), and handle_firmware().

#define GASGAUGE_ERASE   0x03

Definition at line 92 of file proto.h.

Referenced by gasgauge_flash().

#define GASGAUGE_INIT   0x00

Definition at line 89 of file proto.h.

Referenced by gasgauge_flash().

#define GASGAUGE_PROGRAM_ROW   0x05

Definition at line 94 of file proto.h.

Referenced by gasgauge_do_program_row().

#define GASGAUGE_RESTORE_SETTINGS   0x06

Definition at line 95 of file proto.h.

Referenced by gasgauge_flash().

#define GASGAUGE_SAVE_SETTINGS   0x02

Definition at line 91 of file proto.h.

Referenced by gasgauge_flash().

#define GASGAUGE_SETUP_ROW   0x04

Definition at line 93 of file proto.h.

Referenced by gasgauge_do_setup_row().

#define GASGAUGE_START   0x01

Definition at line 90 of file proto.h.

Referenced by gasgauge_flash().

#define GASGAUGE_STOP   0x07

Definition at line 96 of file proto.h.

Referenced by gasgauge_do_command_base(), and gasgauge_flash().

#define IONKBD_ESC   0xDB

Definition at line 59 of file proto.h.

Referenced by encode_send_byte(), and serial_readEncoded().

#define IONKBD_ESC_ESC   0xDD

Definition at line 61 of file proto.h.

Referenced by encode_send_byte(), and serial_readEncoded().

#define IONKBD_ESC_START   0xDC

Definition at line 60 of file proto.h.

Referenced by encode_send_byte(), and serial_readEncoded().

#define IONKBD_REQ_ERASE_FLASH   0x0E

Definition at line 68 of file proto.h.

Referenced by sector_erase().

#define IONKBD_REQ_ERASE_FLASH_BOOT   0x1F

Definition at line 75 of file proto.h.

Referenced by sector_erase().

#define IONKBD_REQ_FLASH_GASGAUGE   0x13

Definition at line 71 of file proto.h.

Referenced by gasgauge_do_command_base().

#define IONKBD_REQ_GET_BATTERY   0x0A

Definition at line 67 of file proto.h.

Referenced by micro_check_battery_charge().

#define IONKBD_REQ_GET_DEVICETYPE   0x19

Definition at line 73 of file proto.h.

Referenced by micro_get_devicetype().

#define IONKBD_REQ_GET_GG_VERSION   0x14

Definition at line 72 of file proto.h.

Referenced by gasgauge_get_versions().

#define IONKBD_REQ_GET_STATUS   0x04

Definition at line 63 of file proto.h.

Referenced by micro_enter_mode(), and micro_get_mode().

#define IONKBD_REQ_GET_VERSION   0x08

Definition at line 66 of file proto.h.

Referenced by micro_get_versions().

#define IONKBD_REQ_PROGRAM_FLASH   0x0F

Definition at line 69 of file proto.h.

Referenced by chunk_program(), and micro_write_devicetype().

#define IONKBD_REQ_PROGRAM_FLASH_BOOT   0x20

Definition at line 76 of file proto.h.

Referenced by chunk_program(), and micro_write_devicetype().

#define IONKBD_REQ_READ_FLASH   0x10

Definition at line 70 of file proto.h.

Referenced by micro_read_devicetype().

#define IONKBD_REQ_READ_FLASH_BOOT   0x21

Definition at line 77 of file proto.h.

Referenced by micro_read_devicetype().

#define IONKBD_REQ_SET_MODE   0x07

Definition at line 65 of file proto.h.

Referenced by micro_enter_mode().

#define IONKBD_REQ_SET_POWER   0x06

Definition at line 64 of file proto.h.

Referenced by main().

#define IONKBD_RESP_BATTERY   0x43

Definition at line 81 of file proto.h.

Referenced by micro_check_battery_charge().

#define IONKBD_RESP_DEVICETYPE   0x4E

Definition at line 87 of file proto.h.

Referenced by micro_get_devicetype().

#define IONKBD_RESP_ERASE   0x45

Definition at line 82 of file proto.h.

Referenced by sector_erase().

#define IONKBD_RESP_FLASH   0x46

Definition at line 83 of file proto.h.

Referenced by chunk_program(), and micro_write_devicetype().

#define IONKBD_RESP_GASGAUGE   0x4A

Definition at line 85 of file proto.h.

Referenced by gasgauge_do_command_base().

#define IONKBD_RESP_GG_VERSION   0x4B

Definition at line 86 of file proto.h.

Referenced by gasgauge_get_versions().

#define IONKBD_RESP_READ   0x47

Definition at line 84 of file proto.h.

Referenced by micro_read_devicetype().

#define IONKBD_RESP_STATUS   0x40

Definition at line 79 of file proto.h.

Referenced by main(), micro_enter_mode(), micro_get_devicetype(), and micro_get_mode().

#define IONKBD_RESP_VERSION   0x41

Definition at line 80 of file proto.h.

Referenced by micro_get_versions().

#define IONKBD_START   0xC0

Copyright (C) 2008 iRex Technologies B.V. All rights reserved.

Definition at line 58 of file proto.h.

Referenced by encode_send_byte(), ionkbd_get_response(), ionkbd_send_packet(), micro_enter_mode(), and serial_readEncoded().

#define MODE_APP   1
#define MODE_BOOT   0

Function Documentation

int chunk_program ( int  mode,
uint16_t  addr,
uint8_t  len,
uint8_t *  chunk 
)

Program a chunk of flash memory

Definition at line 235 of file proto.c.

References data, fatal(), IONKBD_REQ_PROGRAM_FLASH, IONKBD_REQ_PROGRAM_FLASH_BOOT, IONKBD_RESP_FLASH, ionkbd_send_request(), and MODE_BOOT.

Referenced by dr1000_micro_program_application(), dr800_micro_program_application(), and dr800_micro_program_bootloader().

00236 {
00237     uint8_t data[32], rcmd, rlen, rdata[16];
00238     data[0] = addr>>8;
00239     data[1] = addr&0xFF;
00240     uint8_t req = (mode == MODE_BOOT) ? IONKBD_REQ_PROGRAM_FLASH_BOOT : 
00241                                         IONKBD_REQ_PROGRAM_FLASH;
00242     memcpy(&data[2], chunk, len);
00243     if (ionkbd_send_request(req, 2+len, data, &rcmd, &rlen, rdata))
00244     {
00245         fatal("Micro failed to respond to flash request (0x%02X)", req);
00246     }
00247     if (rcmd!=IONKBD_RESP_FLASH) 
00248     {
00249         fatal("Invalid flash response");
00250     }
00251     if (!rdata[0]) 
00252     {
00253         fatal("Flash write failed at 0x%04X", (rdata[1]<<8)|rdata[2]);
00254     }
00255     return 0;
00256 }

Here is the call graph for this function:

Here is the caller graph for this function:

int dr1000_micro_program_application ( char *  filename  ) 

Definition at line 258 of file proto.c.

References chunk_program(), DR1000_APPL_ADDR_MAX, DR1000_APPL_ADDR_MIN, fatal(), and MODE_APP.

Referenced by handle_firmware().

00259 {
00260     int size, pos=0;
00261     uint8_t bsize, bdata[16];
00262     uint16_t baddr; //, i;
00263 
00264     // open file
00265     FILE *f = fopen(filename, "rb");
00266     // check file size
00267    fseek(f, 0, SEEK_END);
00268    size = ftell(f)-2;
00269    fseek(f, 2, SEEK_SET);
00270     // read chunks
00271     while (pos<size)
00272     {
00273         fread(&bsize, 1, 1, f);
00274         fread(&baddr, 2, 1, f);
00275         baddr = ((baddr&0xFF)<<8) | (baddr>>8);
00276         bsize -= 2;
00277         fread(bdata, bsize, 1, f);
00278         if ((bsize>0x0E) ||
00279             (baddr<DR1000_APPL_ADDR_MIN) ||
00280             (baddr>DR1000_APPL_ADDR_MAX)) 
00281         {
00282             fatal("Invalid firmware params: size: 0x%02X, addr: 0x%04X", 
00283                   bsize, baddr);
00284             exit(1);
00285         }
00286         chunk_program(MODE_APP, baddr, bsize, bdata);
00287         pos += (bsize+3);
00288     }
00289     fclose(f);
00290     return 0;
00291 }

Here is the call graph for this function:

Here is the caller graph for this function:

int dr800_micro_program_application ( char *  filename  ) 

Definition at line 293 of file proto.c.

References chunk_program(), DR800_APPL_ADDR_MIN, and MODE_APP.

Referenced by handle_firmware().

00294 {
00295     int size, pos=0;
00296     uint8_t bdata[16];
00297 
00298     // open file
00299     FILE *f = fopen(filename, "rb");
00300     // check file size
00301     fseek(f, 0, SEEK_END);
00302     size = ftell(f)-2;
00303     fseek(f, 0, SEEK_SET);
00304     // read chunks
00305     while (pos<size)
00306     {
00307         fread(bdata, 16, 1, f);
00308         printf("Programming address 0x%04X\r", DR800_APPL_ADDR_MIN + pos);
00309         chunk_program(MODE_APP, DR800_APPL_ADDR_MIN + pos, 16, bdata);
00310         pos += 16;
00311     }
00312     printf("\nProgramming done\n");
00313     fclose(f);
00314     return 0;
00315 }

Here is the call graph for this function:

Here is the caller graph for this function:

int dr800_micro_program_bootloader ( char *  filename  ) 

Definition at line 317 of file proto.c.

References chunk_program(), DR800_BOOT_ADDR_MIN, and MODE_BOOT.

Referenced by handle_firmware().

00318 {
00319     int size, pos=0;
00320     uint8_t bdata[16];
00321 
00322     // open file
00323     FILE *f = fopen(filename, "rb");
00324     // check file size
00325     fseek(f, 0, SEEK_END);
00326     size = ftell(f)-2;
00327     fseek(f, 0, SEEK_SET);
00328     // read chunks
00329     while (pos<size)
00330     {
00331         fread(bdata, 16, 1, f);
00332         printf("Programming address 0x%04X\r", DR800_BOOT_ADDR_MIN + pos);
00333         chunk_program(MODE_BOOT, DR800_BOOT_ADDR_MIN + pos, 16, bdata);
00334         pos += 16;
00335     }
00336     printf("\nProgramming done\n");
00337     fclose(f);
00338     return 0;
00339 }

Here is the call graph for this function:

Here is the caller graph for this function:

void fatal ( char *  message,
  ... 
)

Definition at line 91 of file proto.c.

References buffer.

Referenced by chunk_program(), dr1000_micro_program_application(), handle_firmware(), handle_gasgauge(), ionkbd_get_response(), main(), micro_check_battery_charge(), micro_enter_mode(), micro_erase(), micro_get_devicetype(), micro_get_mode(), micro_get_versions(), micro_read_devicetype(), micro_write_devicetype(), and port_open().

00092 {
00093    char buffer[512];
00094    va_list ap;
00095    va_start(ap, message);
00096    vsprintf(buffer, message, ap);
00097     fprintf(stderr, "Error: %s\n", buffer);
00098    va_end(ap);
00099     exit(1);
00100 }

Here is the caller graph for this function:

void fatalnum ( int  num,
char *  message,
  ... 
)

Definition at line 79 of file proto.c.

References buffer.

Referenced by handle_gasgauge().

00080 {
00081    char buffer[512];
00082    va_list ap;
00083    va_start(ap, message);
00084    vsprintf(buffer, message, ap);
00085     fprintf(stderr, "Error: %s\n", buffer);
00086    va_end(ap);
00087     exit(num);
00088 }

Here is the caller graph for this function:

int gasgauge_flash ( char *  filename  ) 

Definition at line 613 of file proto.c.

References gasgauge_do_command(), gasgauge_do_program_row(), gasgauge_do_setup_row(), GASGAUGE_ERASE, GASGAUGE_INIT, GASGAUGE_RESTORE_SETTINGS, GASGAUGE_SAVE_SETTINGS, GASGAUGE_START, GASGAUGE_STOP, read_timeout, and warning().

Referenced by handle_gasgauge().

00614 {
00615     uint8_t image[1024];
00616     uint8_t *imgptr;
00617     int row, i;
00618     int attempts=0;
00619     FILE *f;
00620 
00621     read_timeout = 1000;
00622 
00623     printf("Loading gasgauge firmware\n");
00624     // open file
00625     f = fopen(filename, "rb");
00626     if (!f)
00627     {
00628         warning("Couldn't open firmware file");
00629         return 1;
00630     }
00631     // check file size
00632    fseek(f, 0, SEEK_END);
00633    if (ftell(f)!=1025)
00634     {
00635         warning("Invalid firmware size");
00636         return 1;
00637     }
00638    fseek(f, 0, SEEK_SET);
00639     // read file
00640     fread(image, 1024, 1, f);
00641     fclose(f);
00642 
00643     printf("Initialising gasgauge programming\n");
00644     // Step 1 - Init
00645     for (i=0;i<3;i++)
00646     {
00647         if (!gasgauge_do_command(GASGAUGE_INIT))
00648             break;
00649         warning("Retrying gas gauge init");
00650     }
00651     if (i==3)
00652     {
00653         warning("Failed to initialise gas gauge programming");
00654         return 1;
00655     }
00656     printf("starting\n");
00657     // Step 2 - Start
00658     for (i=0;i<3;i++)
00659     {
00660         if (!gasgauge_do_command(GASGAUGE_START))
00661             break;
00662         warning("Retrying gas gauge start");
00663     }
00664     if (i==3)
00665     {
00666         warning("Failed to start gas gauge programming");
00667         return 1;
00668     }
00669     printf("saving settings\n");
00670     // Step 3 - Save Settings
00671     while (gasgauge_do_command(GASGAUGE_SAVE_SETTINGS))
00672     {
00673         warning("Failed to save gasgauge settings, retrying");
00674     }
00675     // main program loop
00676     while (attempts<20)
00677     {
00678         imgptr = image;
00679         printf("Starting flash loop (attempt %d)\n",attempts+1);
00680         printf("Erasing gasgauge flash\n");
00681         // Step 4 - Erase
00682         if (gasgauge_do_command(GASGAUGE_ERASE))
00683         {
00684             warning("Failed to erase gasgauge flash, restarting");
00685             continue;
00686         }
00687         // program rows
00688         for (row=0;row<32;row++) 
00689         {
00690             // Step 5 - Setup row
00691             printf("Setting up row  %2d\n",row);
00692             for (i=0;i<4;i++)
00693             {
00694                 gasgauge_do_setup_row(i*8, imgptr, 8);
00695                 imgptr += 8;
00696             }
00697 
00698             // Step 6 - Program row
00699             printf("Programming row %2d\n", row);
00700             if (gasgauge_do_program_row(row))
00701             {
00702                 warning("Failed programming, restarting");
00703                 break;
00704             }
00705         }
00706         // check if we're done
00707         if (row==32)
00708         {
00709             printf("Restoring settings\n");
00710             // Step 7 - Restore Settings
00711             if (!gasgauge_do_command(GASGAUGE_RESTORE_SETTINGS))
00712             {
00713                 printf("All OK, finishing\n");
00714                 break;
00715             }
00716         }
00717         attempts++;
00718     }
00719 
00720     if (attempts==20)
00721     {
00722         warning("Failed to program gasgauge");
00723         return 1;
00724     }
00725 
00726     printf("Stopping\n");
00727     // Step 8 - Stop
00728     if (gasgauge_do_command(GASGAUGE_STOP))
00729     {
00730         warning("Failed to stop gas gauge programming");
00731         return 1;
00732     }
00733     
00734     return 0;
00735 }

Here is the call graph for this function:

Here is the caller graph for this function:

int gasgauge_get_versions ( uint16_t *  firmware,
uint16_t *  hardware 
)

Definition at line 514 of file proto.c.

References IONKBD_REQ_GET_GG_VERSION, IONKBD_RESP_GG_VERSION, ionkbd_send_request(), and warning().

Referenced by handle_gasgauge().

00515 {
00516     uint8_t rcmd, rlen, rdata[16];
00517     int i;
00518     *firmware = 0x0;
00519     *hardware = 0x0;
00520     for (i=0;i<3;i++)
00521     {
00522         if (!ionkbd_send_request(IONKBD_REQ_GET_GG_VERSION, 0, NULL,
00523                     &rcmd, &rlen, rdata))
00524         {
00525             break;
00526         }
00527         warning("Retrying gas gauge version request");
00528     }
00529     if (i==3)
00530     {
00531         warning("Micro failed to respond to gasgauge version request");
00532         return 1;
00533     }
00534     if ((rcmd != IONKBD_RESP_GG_VERSION) || (rlen != 4))
00535     {
00536         warning("Invalid response to gasgauge version request: %d (%d)",rcmd, rlen);
00537         return 1;
00538     }
00539     *firmware = (rdata[0]*100)|rdata[1];
00540     *hardware = (rdata[2]*100)|rdata[3];
00541     return 0;
00542 }

Here is the call graph for this function:

Here is the caller graph for this function:

int ionkbd_get_response ( uint8_t *  command,
uint8_t *  length,
uint8_t *  data 
)

Wait for a response from the microcontroller

Definition at line 134 of file proto.c.

References fatal(), IONKBD_START, serial_read(), and serial_readEncoded().

Referenced by ionkbd_send_request().

00135 {
00136     uint8_t cmd, len, crc, mycrc, c = 0;
00137     uint8_t i = 0;
00138     while(c != IONKBD_START)
00139     {
00140         if (serial_read(&c)) return 1;
00141     }
00142     if (serial_readEncoded(&cmd)) return 1;
00143     if (serial_readEncoded(&len)) return 1;
00144     mycrc = cmd+len;
00145     while (i<len)
00146     {
00147         if (serial_readEncoded(&data[i])) return 1;
00148         mycrc += data[i++];
00149     }
00150     if (serial_readEncoded(&crc)) return 1;
00151     if (crc != mycrc) 
00152     {
00153         fatal("Response packet CRC failed");
00154     }
00155     *command = cmd;
00156     *length = len;
00157     return 0;
00158 }

Here is the call graph for this function:

Here is the caller graph for this function:

void ionkbd_send_packet ( uint8_t  cmd,
uint8_t  length,
uint8_t *  data 
)

Send a packet to the microcontroller

Definition at line 116 of file proto.c.

References encode_send_byte(), IONKBD_START, and serial_write().

Referenced by ionkbd_send_request().

00117 {
00118     uint8_t i, crc = cmd + length;
00119     serial_write(IONKBD_START);
00120     encode_send_byte(cmd);
00121     encode_send_byte(length);
00122     for (i=0;i<length;i++)
00123     {
00124         crc += data[i];
00125         encode_send_byte(data[i]);
00126     }
00127     encode_send_byte(crc);
00128 }

Here is the call graph for this function:

Here is the caller graph for this function:

int ionkbd_send_request ( uint8_t  cmd,
uint8_t  length,
uint8_t *  data,
uint8_t *  rcmd,
uint8_t *  rlen,
uint8_t *  rdata 
)

Send a request to the microcontroller and wait for a response

Definition at line 164 of file proto.c.

References ionkbd_get_response(), and ionkbd_send_packet().

Referenced by chunk_program(), gasgauge_do_command_base(), gasgauge_get_versions(), main(), micro_check_battery_charge(), micro_enter_mode(), micro_get_devicetype(), micro_get_mode(), micro_get_versions(), micro_read_devicetype(), micro_write_devicetype(), and sector_erase().

00171 {
00172     ionkbd_send_packet(cmd, length, data);
00173     return ionkbd_get_response(rcmd, rlen, rdata);
00174 }

Here is the call graph for this function:

Here is the caller graph for this function:

int micro_check_battery_charge ( uint16_t  min_level  ) 

Definition at line 383 of file proto.c.

References fatal(), IONKBD_REQ_GET_BATTERY, IONKBD_RESP_BATTERY, and ionkbd_send_request().

Referenced by handle_gasgauge().

00384 {
00385     uint8_t rcmd, rlen, rdata[16];
00386     uint8_t level, charging;
00387     if (ionkbd_send_request(IONKBD_REQ_GET_BATTERY, 0, NULL, 
00388                             &rcmd, &rlen, rdata))
00389     {
00390         fatal("Micro failed to respond to battery request");
00391     }
00392     if (rcmd!=IONKBD_RESP_BATTERY) 
00393     {
00394         fatal("Invalid flash response");
00395     }
00396     level = rdata[0];
00397     charging = rdata[1];
00398     printf("Micro battery at %d%%%s\n", level, charging?", charging":"");
00399     if (level>=min_level)
00400     {
00401         return 1;
00402     }
00403     else 
00404     {
00405         return ((level>5) && charging);
00406     }
00407 }

Here is the call graph for this function:

Here is the caller graph for this function:

void micro_enter_mode ( uint8_t  boot  ) 

Definition at line 426 of file proto.c.

References data, fatal(), IONKBD_REQ_GET_STATUS, IONKBD_REQ_SET_MODE, IONKBD_RESP_STATUS, ionkbd_send_request(), IONKBD_START, serial_write(), and warning().

Referenced by handle_firmware().

00427 {
00428     uint8_t data, rcmd, rlen, rdata[16];
00429     uint8_t tries = 0;
00430     int i;
00431 
00432     printf("Entering %s mode.\n", boot ? "app" : "boot");
00433     while (tries<5)
00434     {
00435         for (i=0;i<10;i++)
00436         {
00437             serial_write(IONKBD_START);
00438             usleep(1 * 1000);
00439         }
00440         usleep(5 * 1000);
00441         if (ionkbd_send_request(IONKBD_REQ_GET_STATUS, 0, NULL, 
00442                                 &rcmd, &rlen, rdata))
00443         {
00444             warning("Micro failed to respond to status request");
00445         }
00446         else
00447         {
00448             if (rcmd!=IONKBD_RESP_STATUS) 
00449             {
00450                 warning("Invalid response from status request: 0x%02X", rcmd);
00451             }
00452             else
00453             {
00454                 if ((rdata[0]&1)==boot)
00455                 {
00456                     return;
00457                 }
00458                 data = boot;
00459                 if (ionkbd_send_request(IONKBD_REQ_SET_MODE, 1, &data, 
00460                                         &rcmd, &rlen, rdata))
00461                 {
00462                     warning("Micro failed to respond to set mode request");
00463                 }
00464                 usleep(1000 * 1000);
00465             }
00466         }
00467         tries++;
00468     }
00469     fatal("Failed to enter %s mode.", boot ? "app" : "boot");
00470 }

Here is the call graph for this function:

Here is the caller graph for this function:

void micro_erase ( int  mode,
uint16_t  start,
uint16_t  end 
)

Erase the entire flash space

Definition at line 209 of file proto.c.

References fatal(), sector_erase(), and warning().

Referenced by handle_firmware().

00210 {
00211     uint32_t pos = start;
00212     uint8_t tries;
00213     while (pos < end)
00214     {
00215         printf("Erasing sector @ 0x%04X\r", pos);
00216         tries = 0;
00217         while ((sector_erase(mode, pos)) && (tries<3))
00218         {
00219             tries++;
00220             warning("Erase attempt %d failed", tries);
00221         }
00222         if (tries==3)
00223         { 
00224             fatal("Error erasing sector 0x%04X", pos);
00225         }
00226         pos += 512;
00227     }
00228     printf("\nErasing done\n");
00229 }

Here is the call graph for this function:

Here is the caller graph for this function:

void micro_get_devicetype ( uint8_t *  type  ) 

Definition at line 494 of file proto.c.

References fatal(), IONKBD_REQ_GET_DEVICETYPE, IONKBD_RESP_DEVICETYPE, IONKBD_RESP_STATUS, ionkbd_send_request(), and warning().

Referenced by main().

00495 {
00496     uint8_t rcmd, rlen, rdata[16];
00497     if (ionkbd_send_request(IONKBD_REQ_GET_DEVICETYPE, 0, NULL, 
00498                             &rcmd, &rlen, rdata))
00499     {
00500         fatal("Micro failed to respond to devicetype request");
00501     }
00502     if (rcmd!=IONKBD_RESP_DEVICETYPE) 
00503     {
00504         if (rcmd!=IONKBD_RESP_STATUS)
00505         {
00506             fatal("Invalid devicetype response");
00507         }
00508         warning("Received status instead of devicetype respons. Assuming DR1000-series");
00509         rdata[0] = 0;
00510     }
00511     *type = rdata[0];
00512 }

Here is the call graph for this function:

Here is the caller graph for this function:

int micro_get_mode (  ) 

Definition at line 472 of file proto.c.

References fatal(), IONKBD_REQ_GET_STATUS, IONKBD_RESP_STATUS, ionkbd_send_request(), and MODE_BOOT.

Referenced by handle_devicetype().

00473 {
00474     uint8_t rcmd, rlen, rdata[16];
00475     if (ionkbd_send_request(IONKBD_REQ_GET_STATUS, 0, NULL, 
00476                             &rcmd, &rlen, rdata))
00477     {
00478         fatal("Micro failed to respond to status request");
00479     }
00480     else
00481     {
00482         if (rcmd!=IONKBD_RESP_STATUS) 
00483         {
00484             fatal("Invalid response from status request: 0x%02X", rcmd);
00485         }
00486         else
00487         {
00488             return (rdata[0]&1);
00489         }
00490     }
00491     return MODE_BOOT;
00492 }

Here is the call graph for this function:

Here is the caller graph for this function:

int micro_get_versions ( uint16_t *  bootloader,
uint16_t *  application 
)

Definition at line 409 of file proto.c.

References fatal(), IONKBD_REQ_GET_VERSION, IONKBD_RESP_VERSION, and ionkbd_send_request().

Referenced by handle_firmware().

00410 {
00411     uint8_t rcmd, rlen, rdata[16];
00412     if (ionkbd_send_request(IONKBD_REQ_GET_VERSION, 0, NULL, 
00413                             &rcmd, &rlen, rdata))
00414     {
00415         fatal("Micro failed to respond to version request");
00416     }
00417     if (rcmd!=IONKBD_RESP_VERSION) 
00418     {
00419         fatal("Invalid version response");
00420     }
00421     *bootloader = (rdata[0]<<8)|rdata[1];
00422     *application = (rdata[2]<<8)|rdata[3];
00423     return 0;
00424 }

Here is the call graph for this function:

Here is the caller graph for this function:

int micro_read_devicetype ( int  mode  ) 

Definition at line 341 of file proto.c.

References data, DEVICETYPE_ADDR, fatal(), IONKBD_REQ_READ_FLASH, IONKBD_REQ_READ_FLASH_BOOT, IONKBD_RESP_READ, ionkbd_send_request(), and MODE_BOOT.

Referenced by handle_devicetype().

00342 {
00343     uint8_t data[4], rcmd, rlen, rdata[16];
00344     data[0] = DEVICETYPE_ADDR>>8;
00345     data[1] = DEVICETYPE_ADDR&0xFF;
00346     data[2] = 1;
00347     uint8_t req = (mode==MODE_BOOT) ? IONKBD_REQ_READ_FLASH : 
00348                                       IONKBD_REQ_READ_FLASH_BOOT;
00349     if (ionkbd_send_request(req, 3, data, &rcmd, &rlen, rdata))
00350     {
00351         fatal("Micro failed to respond to flash request");
00352     }
00353     if (rcmd!=IONKBD_RESP_READ)
00354     {
00355         fatal("Invalid flash response: 0x%02X", rcmd);
00356     }
00357     return rdata[0];
00358 }

Here is the call graph for this function:

Here is the caller graph for this function:

int micro_write_devicetype ( int  mode,
int  devtype 
)

Definition at line 360 of file proto.c.

References data, DEVICETYPE_ADDR, fatal(), IONKBD_REQ_PROGRAM_FLASH, IONKBD_REQ_PROGRAM_FLASH_BOOT, IONKBD_RESP_FLASH, ionkbd_send_request(), and MODE_BOOT.

Referenced by handle_devicetype().

00361 {
00362     uint8_t data[3], rcmd, rlen, rdata[16];
00363     data[0] = DEVICETYPE_ADDR>>8;
00364     data[1] = DEVICETYPE_ADDR&0xFF;
00365     data[2] = devtype;
00366     uint8_t req = (mode==MODE_BOOT) ? IONKBD_REQ_PROGRAM_FLASH : 
00367                                       IONKBD_REQ_PROGRAM_FLASH_BOOT;
00368     if (ionkbd_send_request(req, 3, data, &rcmd, &rlen, rdata))
00369     {
00370         fatal("Micro failed to respond to flash request");
00371     }
00372     if (rcmd!=IONKBD_RESP_FLASH) 
00373     {
00374         fatal("Invalid flash response");
00375     }
00376     if (!rdata[0]) 
00377     {
00378         fatal("Flash write failed at 0x%04X", (rdata[1]<<8)|rdata[2]);
00379     }
00380     return 0;
00381 }

Here is the call graph for this function:

Here is the caller graph for this function:

void port_open ( char *  name,
int  flags,
int  speed 
)

Definition at line 737 of file proto.c.

References fatal(), and serial_fd.

Referenced by main(), and send_command().

00738 {
00739     struct termios t;
00740     
00741     if ((serial_fd = open(name, O_RDWR | O_NOCTTY)) < 0) 
00742     {
00743         fatal("Can't open device %s", name);
00744     }
00745 
00746     tcgetattr(serial_fd, &t);
00747 
00748     t.c_cflag = flags | CREAD | HUPCL | CLOCAL;
00749     t.c_iflag = IGNBRK | IGNPAR;
00750     t.c_oflag = 0;
00751     t.c_lflag = 0;
00752     t.c_cc[VMIN ] = 1;
00753     t.c_cc[VTIME] = 0;
00754 
00755     cfsetispeed(&t, speed);
00756     cfsetospeed(&t, speed);
00757     tcflush(serial_fd, TCIFLUSH);
00758     tcsetattr(serial_fd, TCSANOW, &t);
00759 }

Here is the call graph for this function:

Here is the caller graph for this function:

int sector_erase ( int  mode,
uint16_t  addr 
)

Erase a sector

Definition at line 180 of file proto.c.

References data, IONKBD_REQ_ERASE_FLASH, IONKBD_REQ_ERASE_FLASH_BOOT, IONKBD_RESP_ERASE, ionkbd_send_request(), MODE_BOOT, and warning().

Referenced by micro_erase().

00181 {
00182     uint8_t data[2], rcmd, rlen, rdata[16];
00183     data[0] = addr>>8;
00184     data[1] = addr&0xFF;
00185     uint8_t req = (mode == MODE_BOOT) ? IONKBD_REQ_ERASE_FLASH_BOOT : 
00186                                         IONKBD_REQ_ERASE_FLASH;
00187     if (ionkbd_send_request(req, 2, data, &rcmd, &rlen, rdata))
00188     {
00189         warning("Micro failed to respond to erase request (0x%02X)", req);
00190         return 1;
00191     }
00192     if (rcmd!=IONKBD_RESP_ERASE) 
00193     {
00194         warning("Invalid erase response");
00195         return 1;
00196     }
00197     if (!rdata[0]) 
00198     {
00199         warning("Flash erase failed");
00200         return 1;
00201     }
00202     return 0;
00203 }

Here is the call graph for this function:

Here is the caller graph for this function:

void warning ( char *  message,
  ... 
)

Definition at line 102 of file proto.c.

References buffer.

Referenced by gasgauge_do_command_base(), gasgauge_flash(), gasgauge_get_versions(), handle_firmware(), micro_enter_mode(), micro_erase(), micro_get_devicetype(), sector_erase(), and serial_read().

00103 {
00104    char buffer[512];
00105    va_list ap;
00106    va_start(ap, message);
00107    vsprintf(buffer, message, ap);
00108     fprintf(stderr, "Warning: %s\n", buffer);
00109    va_end(ap);
00110 }

Here is the caller graph for this function:

Generated by  doxygen 1.6.2-20100208