// SPI command header file
// May08 start,
// Oct08, CS as mapped from the 74hc238 on Main Spray V1.0 board

// Oct2008, CS lines are mapped via the 74hc238
// CF2 input lines (CS0-3) are mapped to the following '238 pins:
// CS0 -> '238 OE = OUTPUT ENABLE (needs always to be selected)CS0=0x01
// CS1 -> '238 C 		CS1 = bit 1 = 0x02
// CS2 -> '238 B 		CS2 = bit 2 = 0x04
// CS3 -> '238 A  		CS3 = bit 3 = 0x08

#define CS_Y0      0x01 // CBA = 000 + CS0
#define CS_ROLL    0x09 // CBA = 001 -> CS3=on + CS0
#define CS_PITCH   0x05 // CBA = 010 -> CS2=on + CS0
#define CS_HYD     0x0D // CBA = 011 -> CS2, CS3=on + CS0
#define CS_GPIO    0x03 // CBA = 100 -> CS1=on + CS0
#define CS_Y5      0x0B // CBA = 101 -> CS1, CS3=on + CS0
#define CS_Y6      0x07 // CBA = 110 -> CS1+CS2=on + CS0
#define CS_Y7      0x0F // CBA = 111 -> all on

// Y0, Y5-Y7 are presently open for SPI expansion

//function prototypes ---------------------------------------------------------
void        init_qsm     (  short m_cs ); 
void        init_spi     (  void );
void        setup_spi    ( short cs, struct spi_param *ss );

short       xfr_SPI_byte ( short pcs, uchar b ); // send b, return reply
short       xfr_SPI_hex  ( short pcs, uchar b );

uchar       update_crc    (int data, unsigned char crc );
short       spi_check_CRC ( struct spi_param *ss ); // check sent value of CRC
short       spi_calc_CRC  ( struct spi_param *ss ); // calc CRC value to send
uchar       b2hex         ( short x ); // convert x=0..15 to '0'..'f'
short       hex2b         ( uchar b ); // convert '0'..'f' to 0..15 value
void        spi_compose_hex ( struct spi_param *ss ); // convert binary to hex
void        spi_compose_bin ( struct spi_param *ss ); // convert hex to binary

void        spi_send_stop ( short cs );

short       spi_get_attn  ( short pcs, short ntry  ); // get attn of the slave

short       spi_try_send  ( struct spi_param *ss, short ntry );
short       spi_send_cmd  ( struct spi_param *ss ); // send the cmd in the struct
short       spi_query     ( struct spi_param *ss ); // query for a response

short       parse_query_ss        ( short rstat, struct spi_param *ss );
void        display_gen_query     ( short rstat, struct spi_param *ss );
void        display_dump          ( struct spi_param *ss );
void        display_ver           ( void  );
void        display_general_ad12  ( struct spi_param *ss );
void        display_pwr           ( struct spi_param *ss );

short       spi_cmd_ver   ( short cs ); // get the module's version info.
short       spi_aux_pwr   ( short val, struct spi_param *ss );
short       power_on_off  ( short cs, short sensor, short on );

short       spi_ad12      ( short val, struct spi_param *ss );
short       ant_select    ( short ant );
short       spi_pwr_rst   ( short cs  );
short       spi_pwr_get   ( struct spi_param *ss );
void        hpr_pwr_get   ( short cs  );

short       rqst_a2d      ( short cs, short chan  );
short       get_a2d       ( short cs, short chan  );
short       r_get_a2d     ( short cs, short chan  );
short       last_a2d      ( short cs, short chan  );

short       rqst_flash_0      ( short  cs );
short       spi_flash_dump    ( short b,  struct spi_param *ss );
short       spi_flash_write   ( short b,  short v , struct spi_param *ss );

void  msp430_store_short ( short val, char *addr ); // store val in next 2 bytes


// character constants that are used for the SPI exchange


#define SPI_BAD     -1   // flag that SPI XFR had hardware problem

#define SPI_OK       ' '  // response that master or slave is OK, ready to receive
#define SPI_ATTN     '!'  // Request for attention; used before sending a command
#define SPI_BUSY     '.'  // Response that the cpu is busy: try again later.
#define SPI_RESET    '*'  // Request for aborting the command-line & resetting buffers.
#define SPI_STOP     'S'  // Emergency stop!
  
  // after ATTN, then: Master sends a command, or receives a command
#define SPI_CSEND    '>'  // Start of command-line = command string follows.
#define SPI_QGET     '<'  // Start of Request-line: slave sends the command string
// the above character is followed by the hexadecimal data,
// then the CRC, and then
#define SPI_END      ';'  // End of command-line
// with a reply of either
#define SPI_CRC_OK   '#'  // command-line received and CRC is OK
#define SPI_CRC_ERR  'X'  // CRC error
#define SPI_ERR      '?'  // general parsing error
#define SPI_NO_Q     'q'  // used by spi_query as return value that no message in q


  // periperal-specific commands:
  // this byte follows the SPI_CSEND character
#define SPI_VER       21 // ask for the code version from the slave
#define AUX_PWR       22 // turn on/off necessary sensor power
#define SPI_MEASURE   23 // update any measurements
#define MOTOR_RUN     24 // Run the motor in the specified direction/time
#define FL_BLOCK_RD   25 // Read a 64-byte block of information flash & return
#define FL_WORD_WR    26 // write  a word to flash
#define PWR_RESET     27 // reset the power accum variables
#define PWR_RQST      28 // request for the average power since last reset

   // sub-commands for the a/d
   #define AD_LAST      99  // if ad_chan = AD_LAST, last buffered val is sent

//specific to the GPIO/Watchdog module=========================================
#define SPI_MUX       30 // configures the GPIO serial/mux devices (list in gpio_mux.h)
#define ANT_SWITCH    31 // configures the antenna switch
#define SPI_WDOG      32 // watchdog command request

// sub-commands for the watchdog
#define WDT_RESET     21 // reset the watchdog counter to 0
#define WDT_QUERY     22 // query for the present settings
#define WDT_SET_PRM   23 // set the watchdog parameters

//specific to hydraulic module ================================================
#define SETUP_PUMP        40 // set up the hyd pump parameters
#define GO_PUMP           41 // run the pump for the specific amount of time
#define HYD_MORE_PUMP     42 // pump for an increased time: used at the bottom turn
#define HYD_STOP_PUMP     43 // request to do emergency stop pump
#define GO_VENT           44 // request to run the vent, up to max sec.

//specific to the pitch/roll module ===========================================
#define LAST_RQST     51 // request for last motor operation stats.
#define CLEAR_RQST    52 // clear the buffer for the last request


// flash addr locations in msp430
// !!!WARNING!!! THESE MUST AGREE WITH MSP430 VALUES !!!!!
#define F_SER_NO       0    // serial # of this peripheral
//#define F_1000S       60    // #s actually occurred over a 1000s count

//*** end *********************************************************************

