aboutsummaryrefslogtreecommitdiffstats
path: root/vex/vexspi.h
blob: c4fc6b16528b1ed755287d34a0050ad23e4fc5c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*-----------------------------------------------------------------------------*/
/*                                                                             */
/*                        Copyright (c) James Pearman                          */
/*                                   2013                                      */
/*                            All Rights Reserved                              */
/*                                                                             */
/*-----------------------------------------------------------------------------*/
/*                                                                             */
/*    Module:     vexspi.h                                                     */
/*    Author:     James Pearman                                                */
/*    Created:    7 May 2013                                                   */
/*                                                                             */
/*    Revisions:                                                               */
/*                V1.00     4 July 2013 - Initial release                      */
/*                                                                             */
/*-----------------------------------------------------------------------------*/
/*                                                                             */
/*    This file is part of ConVEX.                                             */
/*                                                                             */
/*    The author is supplying this software for use with the VEX cortex        */
/*    control system. ConVEX is free software; you can redistribute it         */
/*    and/or modify it under the terms of the GNU General Public License       */
/*    as published by the Free Software Foundation; either version 3 of        */
/*    the License, or (at your option) any later version.                      */
/*                                                                             */
/*    ConVEX is distributed in the hope that it will be useful,                */
/*    but WITHOUT ANY WARRANTY; without even the implied warranty of           */
/*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            */
/*    GNU General Public License for more details.                             */
/*                                                                             */
/*    You should have received a copy of the GNU General Public License        */
/*    along with this program.  If not, see <http://www.gnu.org/licenses/>.    */
/*                                                                             */
/*    A special exception to the GPL can be applied should you wish to         */
/*    distribute a combined work that includes ConVEX, without being obliged   */
/*    to provide the source code for any proprietary components.               */
/*    See the file exception.txt for full details of how and when the          */
/*    exception can be applied.                                                */
/*                                                                             */
/*    The author can be contacted on the vex forums as jpearman                */
/*    or electronic mail using jbpearman_at_mac_dot_com                        */
/*    Mentor for team 8888 RoboLancers, Pasadena CA.                           */
/*                                                                             */
/*-----------------------------------------------------------------------------*/

#ifndef __VEXSPI__
#define __VEXSPI__

/*-----------------------------------------------------------------------------*/
/** @file    vexspi.h
  * @brief   SPI communication to the master processor, macros and prototypes
*//*---------------------------------------------------------------------------*/

#define VEX_SPI_ENABLE_PORT GPIOA
#define VEX_SPI_ENABLE_PIN  11
#define VEX_SPI_CS_PORT     GPIOE
#define VEX_SPI_CS_PIN      0

/*-----------------------------------------------------------------------------*/
/** @brief      default team name                                              */
/*-----------------------------------------------------------------------------*/

#define CONVEX_TEAM_NAME    "ZEPHYR  ";

typedef struct _jsdata {
	int Ch1 :8;
	int Ch2 :8;
	int Ch3 :8;
	int Ch4 :8;

	struct accel_t {
		int y :8;
		int x :8;
		int z :8;
	} __attribute__ ((packed)) accel;

	char Btn5D	:1;
	char Btn5U  :1;
	char Btn6D  :1;
	char Btn6U  :1;

	char Reserved :4;

	char Btn8D	:1;
	char Btn8L  :1;
	char Btn8U  :1;
	char Btn8R  :1;
	char Btn7D	:1;
	char Btn7L  :1;
	char Btn7U  :1;
	char Btn7R  :1;

	char Reserved2[2];

} __attribute__ ((packed)) jsdata;

/*-----------------------------------------------------------------------------*/
/** @brief      SPI transmit data packet                                       */
/*-----------------------------------------------------------------------------*/
/** @details
 *  Format of a SPI transmit packet - 16 words
 */
typedef union _spiTxPacket {
    struct  _spiTxPak {
        unsigned char h1;           ///< Header byte one
        unsigned char h2;           ///< Header byte two
        unsigned char state;        ///< Control state
        unsigned char reserved[2];  ///< unknown data
        unsigned char type;         ///< data type
        unsigned char motor[8];     ///< motor pwm data
        unsigned char pad[15];      ///< not used
        unsigned char rev_lsb;      ///< revision of user code lsb
        unsigned char rev_msb;      ///< revision of user code msb
        unsigned char id;           ///< message id
    } pak;                          ///< access spiTxPacket as named variables

    unsigned char   data[32];       ///< access spiTxPacket as an array of char
} spiTxPacket;

/*-----------------------------------------------------------------------------*/
/** @brief      SPI receive data packet                                        */
/*-----------------------------------------------------------------------------*/
/** @details
 *  Format of a SPI receive packet - 16 words
 */
typedef union _spiRxPacket {
    struct _spiRxPak {
        unsigned char h1;           ///< Header byte one
        unsigned char h2;           ///< Header byte two
        unsigned char status;       ///< status
        unsigned char ctl;          ///< status and control byte
        unsigned char batt1;        ///< main battery level, 59mV per bit
        unsigned char batt2;        ///< backup battery level, 59mV per bit
        jsdata        js_1;         ///< data for main joystick
        unsigned char pad;          ///< 1 byte padding
        jsdata        js_2;         ///< data for partner joystick
        unsigned char rev_lsb;      ///< revision of master code lsb
        unsigned char rev_msb;      ///< revision of master code msb
        unsigned char id;           ///< message id
    } pak;                          ///< access spiRxPacke as named variables

    unsigned char   data[32];       ///< access spiRxPacket as an array of char
} spiRxPacket;

/*-----------------------------------------------------------------------------*/
/** @brief      SPI data                                                       */
/*-----------------------------------------------------------------------------*/
/** @details
 *  All SPI related data collected in this structure
 */
struct SpiData {
    spiTxPacket txdata;             ///< tx data packet
    spiRxPacket rxdata;             ///< valid rx data packet
    spiRxPacket rxdata_t;           ///< receive data packet, may have errors
    uint16_t    online;             ///< online status
    uint32_t    errors;             ///< number of packets received with error
};

namespace spi {
	void init(void);
	void update(void);

	void setMotors(uint8_t *data);

	const jsdata& getJoystick(int num);

	int getBatteryMain(void);
	int getBatteryBackup(void);

	int getState(void);
}

#endif  // __VEXSPI__