uboot/board/digsy_mtc/cmd_mtc.c
<<
>>
Prefs
   1/*
   2 * (C) Copyright 2009
   3 * Werner Pfister <Pfister_Werner@intercontrol.de>
   4 *
   5 * (C) Copyright 2009 Semihalf, Grzegorz Bernacki
   6 *
   7 * See file CREDITS for list of people who contributed to this
   8 * project.
   9 *
  10 * This program is free software; you can redistribute it and/or
  11 * modify it under the terms of the GNU General Public License as
  12 * published by the Free Software Foundation; either version 2 of
  13 * the License, or (at your option) any later version.
  14 *
  15 * This program is distributed in the hope that it will be useful,
  16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18 * GNU General Public License for more details.
  19 *
  20 * You should have received a copy of the GNU General Public License
  21 * along with this program; if not, write to the Free Software
  22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  23 * MA 02111-1307 USA
  24 */
  25
  26#include <common.h>
  27#include <command.h>
  28#include <mpc5xxx.h>
  29#include "spi.h"
  30#include "cmd_mtc.h"
  31
  32DECLARE_GLOBAL_DATA_PTR;
  33
  34static const char *led_names[] = {
  35        "diag",
  36        "can1",
  37        "can2",
  38        "can3",
  39        "can4",
  40        "usbpwr",
  41        "usbbusy",
  42        "user1",
  43        "user2",
  44        ""
  45};
  46
  47static int msp430_xfer(const void *dout, void *din)
  48{
  49        int err;
  50
  51        err = spi_xfer(NULL, MTC_TRANSFER_SIZE, dout, din,
  52                       SPI_XFER_BEGIN | SPI_XFER_END);
  53
  54        /* The MSP chip needs time to ready itself for the next command */
  55        udelay(1000);
  56
  57        return err;
  58}
  59
  60static void mtc_calculate_checksum(tx_msp_cmd *packet)
  61{
  62        int i;
  63        uchar *buff;
  64
  65        buff = (uchar *) packet;
  66
  67        for (i = 0; i < 6; i++)
  68                packet->cks += buff[i];
  69}
  70
  71static int do_mtc_led(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  72{
  73        tx_msp_cmd pcmd;
  74        rx_msp_cmd prx;
  75        int err;
  76        int i;
  77
  78        if (argc < 2)
  79                return cmd_usage(cmdtp);
  80
  81        memset(&pcmd, 0, sizeof(pcmd));
  82        memset(&prx, 0, sizeof(prx));
  83
  84        pcmd.cmd = CMD_SET_LED;
  85
  86        pcmd.cmd_val0 = 0xff;
  87        for (i = 0; strlen(led_names[i]) != 0; i++) {
  88                if (strncmp(argv[1], led_names[i], strlen(led_names[i])) == 0) {
  89                        pcmd.cmd_val0 = i;
  90                        break;
  91                }
  92        }
  93
  94        if (pcmd.cmd_val0 == 0xff) {
  95                printf("Usage:\n%s\n", cmdtp->help);
  96                return -1;
  97        }
  98
  99        if (argc >= 3) {
 100                if (strncmp(argv[2], "red", 3) == 0)
 101                        pcmd.cmd_val1 = 1;
 102                else if (strncmp(argv[2], "green", 5) == 0)
 103                        pcmd.cmd_val1 = 2;
 104                else if (strncmp(argv[2], "orange", 6) == 0)
 105                        pcmd.cmd_val1 = 3;
 106                else
 107                        pcmd.cmd_val1 = 0;
 108        }
 109
 110        if (argc >= 4)
 111                pcmd.cmd_val2 = simple_strtol(argv[3], NULL, 10);
 112        else
 113                pcmd.cmd_val2 = 0;
 114
 115        mtc_calculate_checksum(&pcmd);
 116        err = msp430_xfer(&pcmd, &prx);
 117
 118        return err;
 119}
 120
 121static int do_mtc_key(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 122{
 123        tx_msp_cmd pcmd;
 124        rx_msp_cmd prx;
 125        int err;
 126
 127        memset(&pcmd, 0, sizeof(pcmd));
 128        memset(&prx, 0, sizeof(prx));
 129
 130        pcmd.cmd = CMD_GET_VIM;
 131
 132        mtc_calculate_checksum(&pcmd);
 133        err = msp430_xfer(&pcmd, &prx);
 134
 135        if (!err) {
 136                /* function returns '0' if key is pressed */
 137                err = (prx.input & 0x80) ? 0 : 1;
 138        }
 139
 140        return err;
 141}
 142
 143static int do_mtc_digout(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 144{
 145        tx_msp_cmd pcmd;
 146        rx_msp_cmd prx;
 147        int err;
 148        uchar channel_mask = 0;
 149
 150        if (argc < 3)
 151                return cmd_usage(cmdtp);
 152
 153        if (strncmp(argv[1], "on", 2) == 0)
 154                channel_mask |= 1;
 155        if (strncmp(argv[2], "on", 2) == 0)
 156                channel_mask |= 2;
 157
 158        memset(&pcmd, 0, sizeof(pcmd));
 159        memset(&prx, 0, sizeof(prx));
 160
 161        pcmd.cmd = CMD_GET_VIM;
 162        pcmd.user_out = channel_mask;
 163
 164        mtc_calculate_checksum(&pcmd);
 165        err = msp430_xfer(&pcmd, &prx);
 166
 167        return err;
 168}
 169
 170static int do_mtc_digin(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 171{
 172        tx_msp_cmd pcmd;
 173        rx_msp_cmd prx;
 174        int err;
 175        uchar channel_num = 0;
 176
 177        if (argc < 2)
 178                return cmd_usage(cmdtp);
 179
 180        channel_num = simple_strtol(argv[1], NULL, 10);
 181        if ((channel_num != 1) && (channel_num != 2)) {
 182                printf("mtc digin: invalid parameter - must be '1' or '2'\n");
 183                return -1;
 184        }
 185
 186        memset(&pcmd, 0, sizeof(pcmd));
 187        memset(&prx, 0, sizeof(prx));
 188
 189        pcmd.cmd = CMD_GET_VIM;
 190
 191        mtc_calculate_checksum(&pcmd);
 192        err = msp430_xfer(&pcmd, &prx);
 193
 194        if (!err) {
 195                /* function returns '0' when digin is on */
 196                err = (prx.input & channel_num) ? 0 : 1;
 197        }
 198
 199        return err;
 200}
 201
 202static int do_mtc_appreg(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 203{
 204        tx_msp_cmd pcmd;
 205        rx_msp_cmd prx;
 206        int err;
 207        char buf[5];
 208
 209        /* read appreg */
 210        memset(&pcmd, 0, sizeof(pcmd));
 211        memset(&prx, 0, sizeof(prx));
 212
 213        pcmd.cmd = CMD_WD_PARA;
 214        pcmd.cmd_val0 = 5;      /* max. Count */
 215        pcmd.cmd_val1 = 5;      /* max. Time */
 216        pcmd.cmd_val2 = 0;      /* =0 means read appreg */
 217
 218        mtc_calculate_checksum(&pcmd);
 219        err = msp430_xfer(&pcmd, &prx);
 220
 221        if (!err) {
 222                sprintf(buf, "%d", prx.ack2);
 223                setenv("appreg", buf);
 224        }
 225
 226        return err;
 227}
 228
 229static int do_mtc_version(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 230{
 231        tx_msp_cmd pcmd;
 232        rx_msp_cmd prx;
 233        int err;
 234
 235        memset(&pcmd, 0, sizeof(pcmd));
 236        memset(&prx, 0, sizeof(prx));
 237
 238        pcmd.cmd = CMD_FW_VERSION;
 239
 240        mtc_calculate_checksum(&pcmd);
 241        err = msp430_xfer(&pcmd, &prx);
 242
 243        if (!err) {
 244                printf("FW V%d.%d.%d / HW %d\n",
 245                       prx.ack0, prx.ack1, prx.ack3, prx.ack2);
 246        }
 247
 248        return err;
 249}
 250
 251static int do_mtc_state(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 252{
 253        tx_msp_cmd pcmd;
 254        rx_msp_cmd prx;
 255        int err;
 256
 257        memset(&pcmd, 0, sizeof(pcmd));
 258        memset(&prx, 0, sizeof(prx));
 259
 260        pcmd.cmd = CMD_WD_WDSTATE;
 261        pcmd.cmd_val2 = 1;
 262
 263        mtc_calculate_checksum(&pcmd);
 264        err = msp430_xfer(&pcmd, &prx);
 265
 266        if (!err) {
 267                printf("State     %02Xh\n", prx.state);
 268                printf("Input     %02Xh\n", prx.input);
 269                printf("UserWD    %02Xh\n", prx.ack2);
 270                printf("Sys WD    %02Xh\n", prx.ack3);
 271                printf("WD Timout %02Xh\n", prx.ack0);
 272                printf("eSysState %02Xh\n", prx.ack1);
 273        }
 274
 275        return err;
 276}
 277
 278static int do_mtc_help(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
 279
 280cmd_tbl_t cmd_mtc_sub[] = {
 281        U_BOOT_CMD_MKENT(led, 3, 1, do_mtc_led,
 282        "set state of leds",
 283        "[ledname] [state] [blink]\n"
 284        " - lednames: diag can1 can2 can3 can4 usbpwr usbbusy user1 user2\n"
 285        " - state: off red green orange\n"
 286        " - blink: blink interval in 100ms steps (1 - 10; 0 = static)\n"),
 287        U_BOOT_CMD_MKENT(key, 0, 1, do_mtc_key,
 288        "returns state of user key", ""),
 289        U_BOOT_CMD_MKENT(version, 0, 1, do_mtc_version,
 290        "returns firmware version of supervisor uC", ""),
 291        U_BOOT_CMD_MKENT(appreg, 0, 1, do_mtc_appreg,
 292        "reads appreg value and stores in environment variable 'appreg'", ""),
 293        U_BOOT_CMD_MKENT(digin, 1, 1, do_mtc_digin,
 294        "returns state of digital input",
 295        "<channel_num> - get state of digital input (1 or 2)\n"),
 296        U_BOOT_CMD_MKENT(digout, 2, 1, do_mtc_digout,
 297        "sets digital outputs",
 298        "<on|off> <on|off>- set state of digital output 1 and 2\n"),
 299        U_BOOT_CMD_MKENT(state, 0, 1, do_mtc_state,
 300        "displays state", ""),
 301        U_BOOT_CMD_MKENT(help, 4, 1, do_mtc_help, "get help",
 302        "[command] - get help for command\n"),
 303};
 304
 305static int do_mtc_help(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 306{
 307        extern int _do_help(cmd_tbl_t *cmd_start, int cmd_items,
 308                            cmd_tbl_t *cmdtp, int flag,
 309                            int argc, char * const argv[]);
 310#ifdef CONFIG_SYS_LONGHELP
 311        puts("mtc ");
 312#endif
 313        return _do_help(&cmd_mtc_sub[0],
 314                        ARRAY_SIZE(cmd_mtc_sub), cmdtp, flag, argc, argv);
 315}
 316
 317int cmd_mtc(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 318{
 319        cmd_tbl_t *c;
 320        int err = 0;
 321
 322        c = find_cmd_tbl(argv[1], &cmd_mtc_sub[0], ARRAY_SIZE(cmd_mtc_sub));
 323        if (c) {
 324                argc--;
 325                argv++;
 326                return c->cmd(c, flag, argc, argv);
 327        } else {
 328                /* Unrecognized command */
 329                return cmd_usage(cmdtp);
 330        }
 331
 332        return err;
 333}
 334
 335U_BOOT_CMD(mtc, 5, 1, cmd_mtc,
 336        "special commands for digsyMTC",
 337        "[subcommand] [args...]\n"
 338        "Subcommands list:\n"
 339        "led [ledname] [state] [blink] - set state of leds\n"
 340        "  [ledname]: diag can1 can2 can3 can4 usbpwr usbbusy user1 user2\n"
 341        "  [state]: off red green orange\n"
 342        "  [blink]: blink interval in 100ms steps (1 - 10; 0 = static)\n"
 343        "key - returns state of user key\n"
 344        "version - returns firmware version of supervisor uC\n"
 345        "appreg - reads appreg value and stores in environment variable"
 346        " 'appreg'\n"
 347        "digin [channel] - returns state of digital input (1 or 2)\n"
 348        "digout <on|off> <on|off> - sets state of two digital outputs\n"
 349        "state - displays state\n"
 350        "help [subcommand] - get help for subcommand\n"
 351);
 352