kbdupgrade.c File Reference

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <termios.h>
#include "proto.h"
Include dependency graph for kbdupgrade.c:

Go to the source code of this file.

Defines

#define ION_SERIO_FLAGS   CS8
#define ION_SERIO_SPEED   B115200
#define ACTION_FIRMWARE   1
#define ACTION_BOOTLOADER   2
#define ACTION_GASGAUGE   3
#define ACTION_READTYPE   4
#define ACTION_WRITETYPE   5
#define DR1000   0
#define VPRINTF(...)   { if (verbose) printf(__VA_ARGS__); }

Functions

static void usage ()
static int check_firmware (char *filename, uint16_t *version)
static void parse_args (int argc, char **argv)
int device_type_from_name (char *name)
void handle_gasgauge ()
void handle_firmware ()
void handle_devicetype ()
int main (int argc, char **argv)

Variables

static char * device_types []
static int action = ACTION_FIRMWARE
static int force_update = 0
static int only_check = 0
static int gg_req_version = 0
static int verbose = 1
char * filename
uint8_t devicetype
static int new_devicetype

Define Documentation

#define ACTION_BOOTLOADER   2

Definition at line 57 of file kbdupgrade.c.

Referenced by handle_firmware(), main(), and parse_args().

#define ACTION_FIRMWARE   1

Definition at line 56 of file kbdupgrade.c.

Referenced by main().

#define ACTION_GASGAUGE   3

Definition at line 58 of file kbdupgrade.c.

Referenced by main(), and parse_args().

#define ACTION_READTYPE   4

Definition at line 59 of file kbdupgrade.c.

Referenced by handle_devicetype(), main(), and parse_args().

#define ACTION_WRITETYPE   5

Definition at line 60 of file kbdupgrade.c.

Referenced by main(), and parse_args().

#define DR1000   0

Definition at line 62 of file kbdupgrade.c.

Referenced by handle_firmware(), and handle_gasgauge().

#define ION_SERIO_FLAGS   CS8

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

Definition at line 53 of file kbdupgrade.c.

#define ION_SERIO_SPEED   B115200

Definition at line 54 of file kbdupgrade.c.

#define VPRINTF ( ...   )     { if (verbose) printf(__VA_ARGS__); }

Definition at line 64 of file kbdupgrade.c.

Referenced by handle_devicetype(), handle_firmware(), and main().


Function Documentation

static int check_firmware ( char *  filename,
uint16_t *  version 
) [static]

Definition at line 297 of file kbdupgrade.c.

Referenced by handle_firmware().

00298 {
00299     FILE *f = fopen(filename, "rb");
00300     if (f)
00301     {
00302         fread(version, 2, 1, f);
00303         fclose(f);
00304         return 1;
00305     }
00306     return 0;
00307 }

Here is the caller graph for this function:

int device_type_from_name ( char *  name  ) 

Definition at line 105 of file kbdupgrade.c.

References device_types.

Referenced by parse_args().

00106 {
00107     int i = 0; 
00108     while (device_types[i]) {
00109         if (strcasecmp(name, device_types[i]) == 0)
00110         {
00111             return i;
00112         }
00113         i++;
00114     }
00115     return 0xFF;
00116 }

Here is the caller graph for this function:

void handle_devicetype (  ) 

Definition at line 207 of file kbdupgrade.c.

References action, ACTION_READTYPE, device_types, micro_get_mode(), micro_read_devicetype(), micro_write_devicetype(), MODE_APP, new_devicetype, and VPRINTF.

Referenced by main().

00208 {
00209     int mode = micro_get_mode();
00210     VPRINTF("Micro in %s mode\n", mode==MODE_APP ? "app" : "boot");
00211 
00212     if (action == ACTION_READTYPE)
00213     {
00214         int devtype = micro_read_devicetype(mode);
00215         VPRINTF("Device type: ");
00216         printf("%s (%d)\n", 
00217                 devtype == 0xFF ? "Unknown" : device_types[devtype], devtype);
00218     }
00219     else
00220     {
00221         VPRINTF("Writing devicetype %d\n", new_devicetype);
00222         micro_write_devicetype(mode, new_devicetype);
00223     }
00224 }

Here is the call graph for this function:

Here is the caller graph for this function:

void handle_firmware (  ) 

Definition at line 147 of file kbdupgrade.c.

References action, ACTION_BOOTLOADER, check_firmware(), devicetype, DR1000, DR1000_APPL_ADDR_MAX, DR1000_APPL_ADDR_MIN, dr1000_micro_program_application(), DR800_APPL_ADDR_MAX, DR800_APPL_ADDR_MIN, DR800_BOOT_ADDR_MAX, DR800_BOOT_ADDR_MIN, dr800_micro_program_application(), dr800_micro_program_bootloader(), fatal(), filename, force_update, micro_enter_mode(), micro_erase(), micro_get_versions(), MODE_APP, MODE_BOOT, only_check, VPRINTF, and warning().

Referenced by main().

00148 {
00149     uint16_t bver, aver, fver;
00150     // check firware exists
00151     if (!check_firmware(filename, &fver))
00152     {
00153         fatal("Can't open firmware file %s", filename);
00154     } 
00155     // only use this on DR1000 to initialize communication with micro
00156     if (devicetype == DR1000)
00157     {
00158         micro_enter_mode(MODE_BOOT);
00159     }
00160     // check version numbers
00161     micro_get_versions(&bver, &aver);
00162     VPRINTF("Bootloader v%d.%d, app v%d.%d.\n",
00163             bver>>8,bver&0xFF,aver>>8,aver&0xFF);
00164     if (only_check)
00165         return;
00166     if ((!force_update) && 
00167         (((action != ACTION_BOOTLOADER) && fver<=aver && aver!=0xFFFF) ||
00168          ((action == ACTION_BOOTLOADER) && fver<=bver && bver!=0xFFFF)))
00169     {
00170         // firmware file is older than version on micro
00171         warning("Firmware '%s' is v%d.%d, not upgrading.",
00172                 filename, fver>>8, fver&0xFF);
00173     } 
00174     else 
00175     {
00176         // erase and program
00177         if (devicetype == DR1000)
00178         {           
00179             // DR1000 Series
00180             if (action == ACTION_BOOTLOADER)
00181             {
00182                 fatal("DR1000 doesn't support bootloader upgrades");
00183             }
00184             else
00185             {
00186                 micro_erase(MODE_APP, DR1000_APPL_ADDR_MIN, DR1000_APPL_ADDR_MAX);
00187                 dr1000_micro_program_application(filename);
00188             }
00189         }
00190         else
00191         {            
00192             // DR800 Series
00193             if (action == ACTION_BOOTLOADER)
00194             {
00195                 micro_erase(MODE_BOOT, DR800_BOOT_ADDR_MIN, DR800_BOOT_ADDR_MAX);
00196                 dr800_micro_program_bootloader(filename);
00197             }
00198             else
00199             {
00200                 micro_erase(MODE_APP, DR800_APPL_ADDR_MIN, DR800_APPL_ADDR_MAX);
00201                 dr800_micro_program_application(filename);
00202             }
00203         }
00204     }
00205 }

Here is the call graph for this function:

Here is the caller graph for this function:

void handle_gasgauge (  ) 

Definition at line 118 of file kbdupgrade.c.

References devicetype, DR1000, fatal(), fatalnum(), filename, force_update, gasgauge_flash(), gasgauge_get_versions(), gg_req_version, and micro_check_battery_charge().

Referenced by main().

00119 {
00120     // No suppoprt for GasGauge on DR800 series yet    
00121     if(devicetype != DR1000)
00122     {
00123         fatal("No support for GasGauge updates on DR800 series devices.");   
00124     }
00125     // check battery is at least 25% and/or charging
00126     if (!micro_check_battery_charge(25))
00127     {
00128         fatalnum(2, "Battery too low or not charging");
00129     }
00130     if (!force_update)
00131     {
00132         uint16_t gg_ver_fw, gg_ver_hw;
00133         if (gasgauge_get_versions(&gg_ver_fw, &gg_ver_hw))
00134         {
00135             fatal("Can't get gas gauge firmware version");
00136         }
00137         if (gg_ver_fw!=gg_req_version)
00138         {
00139             fatal("Invalid gas gauge firmware: got %04d, need %04d",
00140                     gg_ver_fw, gg_req_version);
00141         }
00142         usleep(1000000);
00143     }
00144     gasgauge_flash(filename);
00145 }

Here is the call graph for this function:

Here is the caller graph for this function:

int main ( int  argc,
char **  argv 
)

Definition at line 226 of file kbdupgrade.c.

References action, ACTION_BOOTLOADER, ACTION_FIRMWARE, ACTION_GASGAUGE, ACTION_READTYPE, ACTION_WRITETYPE, devicetype, fatal(), filename, handle_devicetype(), handle_firmware(), handle_gasgauge(), ION_SERIO_FLAGS, ION_SERIO_SPEED, micro_get_devicetype(), parse_args(), port_open(), usage(), and VPRINTF.

00227 {
00228     char *portname;
00229 
00230     // check arguments
00231     parse_args(argc, argv);
00232     if (((action == ACTION_GASGAUGE || action == ACTION_FIRMWARE || action == ACTION_BOOTLOADER) && 
00233             ((argc-optind)!=2)) ||
00234         (((action == ACTION_WRITETYPE || action == ACTION_READTYPE) && 
00235           ((argc-optind)!=1))))
00236     {
00237         usage();
00238     }
00239     portname = argv[optind];
00240     filename = argv[optind+1];
00241     // open device
00242     port_open(portname, ION_SERIO_FLAGS, ION_SERIO_SPEED);
00243     // Check DeviceType
00244     micro_get_devicetype(&devicetype);
00245     if(devicetype==0)
00246     {    
00247         VPRINTF("Device type is DR1000 Series\n");
00248     }
00249     else
00250     {    
00251         VPRINTF("Device type is DR800 Series\n");
00252     }
00253     // do upgrades
00254     if (action == ACTION_GASGAUGE)
00255     {
00256         handle_gasgauge();
00257     }
00258     else if (action == ACTION_FIRMWARE || action == ACTION_BOOTLOADER)
00259     {
00260         handle_firmware();
00261     }
00262     else if (action == ACTION_WRITETYPE || action == ACTION_READTYPE)
00263     {
00264         handle_devicetype();
00265     }
00266     else
00267     {
00268        fatal("Unknown action: %d\n",action);
00269     }
00270 
00271     return 0;
00272 }

Here is the call graph for this function:

static void parse_args ( int  argc,
char **  argv 
) [static]

Definition at line 310 of file kbdupgrade.c.

References action, ACTION_BOOTLOADER, ACTION_GASGAUGE, ACTION_READTYPE, ACTION_WRITETYPE, device_type_from_name(), force_update, gg_req_version, new_devicetype, only_check, usage(), and verbose.

00311 {
00312     int c;
00313 
00314     while (( c = getopt(argc, argv, "bcfghr:s:tq")) != -1)
00315     {
00316         switch (c)
00317         {
00318             case 'b':
00319                 action = ACTION_BOOTLOADER;
00320                 break;
00321             
00322             case 'g':
00323                 action = ACTION_GASGAUGE;
00324                 break;
00325 
00326             case 's':
00327                 action = ACTION_WRITETYPE;
00328                 new_devicetype = device_type_from_name(optarg);
00329                 break;
00330 
00331             case 't':
00332                 action = ACTION_READTYPE;
00333                 break;
00334 
00335             case 'c':
00336                 only_check = 1;
00337                 break;
00338 
00339             case 'f':
00340                 force_update = 1;
00341                 break;
00342             
00343             case 'q':
00344                 verbose = 0;
00345                 break;
00346 
00347             case 'r':
00348                 gg_req_version = atoi(optarg);
00349                 printf("Required gas gauge version: %04d\n", gg_req_version);
00350                 break;
00351 
00352             case 'h':
00353             default:
00354                 usage();
00355                 break;
00356         }
00357     }
00358 }

Here is the call graph for this function:

static void usage (  )  [static]

Definition at line 279 of file kbdupgrade.c.

00280 {
00281     printf("kbdupgrade - keyboard microcontroller firmware upgrader\n");
00282     printf(" usage: kbdupgrade <options> <serial port> <firmware file>\n");
00283     printf(" options:\n");
00284     printf("  -b       Upgrade bootloader instead of application\n");
00285     printf("  -c       Only do version check\n");
00286     printf("  -f       Ignore version check and force upgrade\n");
00287     printf("  -g       Upgrade gas gauge, instead of micro\n");
00288     printf("  -q       Don't print as much\n");
00289     printf("  -r XXXX  Required gas gauge firmware version\n");
00290     printf("  -s XXXX  Set device type (XXXX = DR[800|1000][SWG])\n");
00291     printf("  -t       Get device type\n");
00292     printf("  -h       This text\n");
00293     exit(1);
00294 }


Variable Documentation

int action = ACTION_FIRMWARE [static]
char* device_types[] [static]
Initial value:
 {
    "DR1000",
    "DR1000S",
    "DR1000SW",
    "DR800",
    "DR800S",
    "DR800SG",
    "DR800SW",
    NULL
}

Definition at line 66 of file kbdupgrade.c.

Referenced by device_type_from_name(), and handle_devicetype().

uint8_t devicetype

Definition at line 88 of file kbdupgrade.c.

Referenced by handle_firmware(), handle_gasgauge(), and main().

char* filename
int force_update = 0 [static]

Definition at line 83 of file kbdupgrade.c.

Referenced by handle_firmware(), handle_gasgauge(), and parse_args().

int gg_req_version = 0 [static]

Definition at line 85 of file kbdupgrade.c.

Referenced by handle_gasgauge(), and parse_args().

int new_devicetype [static]

Definition at line 89 of file kbdupgrade.c.

Referenced by handle_devicetype(), and parse_args().

int only_check = 0 [static]

Definition at line 84 of file kbdupgrade.c.

Referenced by handle_firmware(), and parse_args().

int verbose = 1 [static]

Definition at line 86 of file kbdupgrade.c.

Referenced by parse_args().

Generated by  doxygen 1.6.2-20100208