Library(Beta) for Gameduino 2
GDTransport.h@0:9c211972beb2, 2014-04-11 (annotated)
- Committer:
- aluqard
- Date:
- Fri Apr 11 07:24:23 2014 +0000
- Revision:
- 0:9c211972beb2
Initial release for Gameduino 2
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
aluqard | 0:9c211972beb2 | 1 | #ifndef _GDTRANSPORT_H_ |
aluqard | 0:9c211972beb2 | 2 | #define _GDTRANSPORT_H_ |
aluqard | 0:9c211972beb2 | 3 | |
aluqard | 0:9c211972beb2 | 4 | #include "mbed.h" |
aluqard | 0:9c211972beb2 | 5 | #include "arduino.h" |
aluqard | 0:9c211972beb2 | 6 | |
aluqard | 0:9c211972beb2 | 7 | #ifndef DEBUGOUT |
aluqard | 0:9c211972beb2 | 8 | //#define DEBUGOUT (x,...) std::printf(x, ##y); |
aluqard | 0:9c211972beb2 | 9 | #define DEBUGOUT(x, ...) std::printf(""x"\r\n", ##__VA_ARGS__); |
aluqard | 0:9c211972beb2 | 10 | #endif |
aluqard | 0:9c211972beb2 | 11 | |
aluqard | 0:9c211972beb2 | 12 | // Convert degrees to Furmans |
aluqard | 0:9c211972beb2 | 13 | #define DEGREES(n) ((65536UL * (n)) / 360) |
aluqard | 0:9c211972beb2 | 14 | |
aluqard | 0:9c211972beb2 | 15 | #define NEVER 0 |
aluqard | 0:9c211972beb2 | 16 | #define LESS 1 |
aluqard | 0:9c211972beb2 | 17 | #define LEQUAL 2 |
aluqard | 0:9c211972beb2 | 18 | #define GREATER 3 |
aluqard | 0:9c211972beb2 | 19 | #define GEQUAL 4 |
aluqard | 0:9c211972beb2 | 20 | #define EQUAL 5 |
aluqard | 0:9c211972beb2 | 21 | #define NOTEQUAL 6 |
aluqard | 0:9c211972beb2 | 22 | #define ALWAYS 7 |
aluqard | 0:9c211972beb2 | 23 | |
aluqard | 0:9c211972beb2 | 24 | #define ARGB1555 0 |
aluqard | 0:9c211972beb2 | 25 | #define L1 1 |
aluqard | 0:9c211972beb2 | 26 | #define L4 2 |
aluqard | 0:9c211972beb2 | 27 | #define L8 3 |
aluqard | 0:9c211972beb2 | 28 | #define RGB332 4 |
aluqard | 0:9c211972beb2 | 29 | #define ARGB2 5 |
aluqard | 0:9c211972beb2 | 30 | #define ARGB4 6 |
aluqard | 0:9c211972beb2 | 31 | #define RGB565 7 |
aluqard | 0:9c211972beb2 | 32 | #define PALETTED 8 |
aluqard | 0:9c211972beb2 | 33 | #define TEXT8X8 9 |
aluqard | 0:9c211972beb2 | 34 | #define TEXTVGA 10 |
aluqard | 0:9c211972beb2 | 35 | #define BARGRAPH 11 |
aluqard | 0:9c211972beb2 | 36 | |
aluqard | 0:9c211972beb2 | 37 | #define NEAREST 0 |
aluqard | 0:9c211972beb2 | 38 | #define BILINEAR 1 |
aluqard | 0:9c211972beb2 | 39 | |
aluqard | 0:9c211972beb2 | 40 | #define BORDER 0 |
aluqard | 0:9c211972beb2 | 41 | #define REPEAT 1 |
aluqard | 0:9c211972beb2 | 42 | |
aluqard | 0:9c211972beb2 | 43 | #define KEEP 1 |
aluqard | 0:9c211972beb2 | 44 | #define REPLACE 2 |
aluqard | 0:9c211972beb2 | 45 | #define INCR 3 |
aluqard | 0:9c211972beb2 | 46 | #define DECR 4 |
aluqard | 0:9c211972beb2 | 47 | #define INVERT 5 |
aluqard | 0:9c211972beb2 | 48 | |
aluqard | 0:9c211972beb2 | 49 | #define DLSWAP_DONE 0 |
aluqard | 0:9c211972beb2 | 50 | #define DLSWAP_LINE 1 |
aluqard | 0:9c211972beb2 | 51 | #define DLSWAP_FRAME 2 |
aluqard | 0:9c211972beb2 | 52 | |
aluqard | 0:9c211972beb2 | 53 | #define INT_SWAP 1 |
aluqard | 0:9c211972beb2 | 54 | #define INT_TOUCH 2 |
aluqard | 0:9c211972beb2 | 55 | #define INT_TAG 4 |
aluqard | 0:9c211972beb2 | 56 | #define INT_SOUND 8 |
aluqard | 0:9c211972beb2 | 57 | #define INT_PLAYBACK 16 |
aluqard | 0:9c211972beb2 | 58 | #define INT_CMDEMPTY 32 |
aluqard | 0:9c211972beb2 | 59 | #define INT_CMDFLAG 64 |
aluqard | 0:9c211972beb2 | 60 | #define INT_CONVCOMPLETE 128 |
aluqard | 0:9c211972beb2 | 61 | |
aluqard | 0:9c211972beb2 | 62 | #define TOUCHMODE_OFF 0 |
aluqard | 0:9c211972beb2 | 63 | #define TOUCHMODE_ONESHOT 1 |
aluqard | 0:9c211972beb2 | 64 | #define TOUCHMODE_FRAME 2 |
aluqard | 0:9c211972beb2 | 65 | #define TOUCHMODE_CONTINUOUS 3 |
aluqard | 0:9c211972beb2 | 66 | |
aluqard | 0:9c211972beb2 | 67 | #define ZERO 0 |
aluqard | 0:9c211972beb2 | 68 | #define ONE 1 |
aluqard | 0:9c211972beb2 | 69 | #define SRC_ALPHA 2 |
aluqard | 0:9c211972beb2 | 70 | #define DST_ALPHA 3 |
aluqard | 0:9c211972beb2 | 71 | #define ONE_MINUS_SRC_ALPHA 4 |
aluqard | 0:9c211972beb2 | 72 | #define ONE_MINUS_DST_ALPHA 5 |
aluqard | 0:9c211972beb2 | 73 | |
aluqard | 0:9c211972beb2 | 74 | #define BITMAPS 1 |
aluqard | 0:9c211972beb2 | 75 | #define POINTS 2 |
aluqard | 0:9c211972beb2 | 76 | #define LINES 3 |
aluqard | 0:9c211972beb2 | 77 | #define LINE_STRIP 4 |
aluqard | 0:9c211972beb2 | 78 | #define EDGE_STRIP_R 5 |
aluqard | 0:9c211972beb2 | 79 | #define EDGE_STRIP_L 6 |
aluqard | 0:9c211972beb2 | 80 | #define EDGE_STRIP_A 7 |
aluqard | 0:9c211972beb2 | 81 | #define EDGE_STRIP_B 8 |
aluqard | 0:9c211972beb2 | 82 | #define RECTS 9 |
aluqard | 0:9c211972beb2 | 83 | |
aluqard | 0:9c211972beb2 | 84 | #define OPT_MONO 1 |
aluqard | 0:9c211972beb2 | 85 | #define OPT_NODL 2 |
aluqard | 0:9c211972beb2 | 86 | #define OPT_FLAT 256 |
aluqard | 0:9c211972beb2 | 87 | #define OPT_CENTERX 512 |
aluqard | 0:9c211972beb2 | 88 | #define OPT_CENTERY 1024 |
aluqard | 0:9c211972beb2 | 89 | #define OPT_CENTER (OPT_CENTERX | OPT_CENTERY) |
aluqard | 0:9c211972beb2 | 90 | #define OPT_NOBACK 4096 |
aluqard | 0:9c211972beb2 | 91 | #define OPT_NOTICKS 8192 |
aluqard | 0:9c211972beb2 | 92 | #define OPT_NOHM 16384 |
aluqard | 0:9c211972beb2 | 93 | #define OPT_NOPOINTER 16384 |
aluqard | 0:9c211972beb2 | 94 | #define OPT_NOSECS 32768 |
aluqard | 0:9c211972beb2 | 95 | #define OPT_NOHANDS 49152 |
aluqard | 0:9c211972beb2 | 96 | #define OPT_RIGHTX 2048 |
aluqard | 0:9c211972beb2 | 97 | #define OPT_SIGNED 256 |
aluqard | 0:9c211972beb2 | 98 | |
aluqard | 0:9c211972beb2 | 99 | #define LINEAR_SAMPLES 0 |
aluqard | 0:9c211972beb2 | 100 | #define ULAW_SAMPLES 1 |
aluqard | 0:9c211972beb2 | 101 | #define ADPCM_SAMPLES 2 |
aluqard | 0:9c211972beb2 | 102 | |
aluqard | 0:9c211972beb2 | 103 | // 'instrument' argument to GD.play() |
aluqard | 0:9c211972beb2 | 104 | |
aluqard | 0:9c211972beb2 | 105 | #define SILENCE 0x00 |
aluqard | 0:9c211972beb2 | 106 | |
aluqard | 0:9c211972beb2 | 107 | #define SQUAREWAVE 0x01 |
aluqard | 0:9c211972beb2 | 108 | #define SINEWAVE 0x02 |
aluqard | 0:9c211972beb2 | 109 | #define SAWTOOTH 0x03 |
aluqard | 0:9c211972beb2 | 110 | #define TRIANGLE 0x04 |
aluqard | 0:9c211972beb2 | 111 | |
aluqard | 0:9c211972beb2 | 112 | #define BEEPING 0x05 |
aluqard | 0:9c211972beb2 | 113 | #define ALARM 0x06 |
aluqard | 0:9c211972beb2 | 114 | #define WARBLE 0x07 |
aluqard | 0:9c211972beb2 | 115 | #define CAROUSEL 0x08 |
aluqard | 0:9c211972beb2 | 116 | |
aluqard | 0:9c211972beb2 | 117 | #define PIPS(n) (0x0f + (n)) |
aluqard | 0:9c211972beb2 | 118 | |
aluqard | 0:9c211972beb2 | 119 | #define HARP 0x40 |
aluqard | 0:9c211972beb2 | 120 | #define XYLOPHONE 0x41 |
aluqard | 0:9c211972beb2 | 121 | #define TUBA 0x42 |
aluqard | 0:9c211972beb2 | 122 | #define GLOCKENSPIEL 0x43 |
aluqard | 0:9c211972beb2 | 123 | #define ORGAN 0x44 |
aluqard | 0:9c211972beb2 | 124 | #define TRUMPET 0x45 |
aluqard | 0:9c211972beb2 | 125 | #define PIANO 0x46 |
aluqard | 0:9c211972beb2 | 126 | #define CHIMES 0x47 |
aluqard | 0:9c211972beb2 | 127 | #define MUSICBOX 0x48 |
aluqard | 0:9c211972beb2 | 128 | #define BELL 0x49 |
aluqard | 0:9c211972beb2 | 129 | |
aluqard | 0:9c211972beb2 | 130 | #define CLICK 0x50 |
aluqard | 0:9c211972beb2 | 131 | #define SWITCH 0x51 |
aluqard | 0:9c211972beb2 | 132 | #define COWBELL 0x52 |
aluqard | 0:9c211972beb2 | 133 | #define NOTCH 0x53 |
aluqard | 0:9c211972beb2 | 134 | #define HIHAT 0x54 |
aluqard | 0:9c211972beb2 | 135 | #define KICKDRUM 0x55 |
aluqard | 0:9c211972beb2 | 136 | #define POP 0x56 |
aluqard | 0:9c211972beb2 | 137 | #define CLACK 0x57 |
aluqard | 0:9c211972beb2 | 138 | #define CHACK 0x58 |
aluqard | 0:9c211972beb2 | 139 | |
aluqard | 0:9c211972beb2 | 140 | #define MUTE 0x60 |
aluqard | 0:9c211972beb2 | 141 | #define UNMUTE 0x61 |
aluqard | 0:9c211972beb2 | 142 | |
aluqard | 0:9c211972beb2 | 143 | #define RAM_CMD 1081344UL |
aluqard | 0:9c211972beb2 | 144 | #define RAM_DL 1048576UL |
aluqard | 0:9c211972beb2 | 145 | #define RAM_PAL 1056768UL |
aluqard | 0:9c211972beb2 | 146 | |
aluqard | 0:9c211972beb2 | 147 | #define REG_CLOCK 1057800UL |
aluqard | 0:9c211972beb2 | 148 | #define REG_CMD_DL 1058028UL |
aluqard | 0:9c211972beb2 | 149 | #define REG_CMD_READ 1058020UL |
aluqard | 0:9c211972beb2 | 150 | #define REG_CMD_WRITE 1058024UL |
aluqard | 0:9c211972beb2 | 151 | #define REG_CPURESET 1057820UL |
aluqard | 0:9c211972beb2 | 152 | #define REG_CSPREAD 1057892UL |
aluqard | 0:9c211972beb2 | 153 | #define REG_DITHER 1057884UL |
aluqard | 0:9c211972beb2 | 154 | #define REG_DLSWAP 1057872UL |
aluqard | 0:9c211972beb2 | 155 | #define REG_FRAMES 1057796UL |
aluqard | 0:9c211972beb2 | 156 | #define REG_FREQUENCY 1057804UL |
aluqard | 0:9c211972beb2 | 157 | #define REG_GPIO 1057936UL |
aluqard | 0:9c211972beb2 | 158 | #define REG_GPIO_DIR 1057932UL |
aluqard | 0:9c211972beb2 | 159 | #define REG_HCYCLE 1057832UL |
aluqard | 0:9c211972beb2 | 160 | #define REG_HOFFSET 1057836UL |
aluqard | 0:9c211972beb2 | 161 | #define REG_HSIZE 1057840UL |
aluqard | 0:9c211972beb2 | 162 | #define REG_HSYNC0 1057844UL |
aluqard | 0:9c211972beb2 | 163 | #define REG_HSYNC1 1057848UL |
aluqard | 0:9c211972beb2 | 164 | #define REG_ID 1057792UL |
aluqard | 0:9c211972beb2 | 165 | #define REG_INT_EN 1057948UL |
aluqard | 0:9c211972beb2 | 166 | #define REG_INT_FLAGS 1057944UL |
aluqard | 0:9c211972beb2 | 167 | #define REG_INT_MASK 1057952UL |
aluqard | 0:9c211972beb2 | 168 | #define REG_MACRO_0 1057992UL |
aluqard | 0:9c211972beb2 | 169 | #define REG_MACRO_1 1057996UL |
aluqard | 0:9c211972beb2 | 170 | #define REG_OUTBITS 1057880UL |
aluqard | 0:9c211972beb2 | 171 | #define REG_PCLK 1057900UL |
aluqard | 0:9c211972beb2 | 172 | #define REG_PCLK_POL 1057896UL |
aluqard | 0:9c211972beb2 | 173 | #define REG_PLAY 1057928UL |
aluqard | 0:9c211972beb2 | 174 | #define REG_PLAYBACK_FORMAT 1057972UL |
aluqard | 0:9c211972beb2 | 175 | #define REG_PLAYBACK_FREQ 1057968UL |
aluqard | 0:9c211972beb2 | 176 | #define REG_PLAYBACK_LENGTH 1057960UL |
aluqard | 0:9c211972beb2 | 177 | #define REG_PLAYBACK_LOOP 1057976UL |
aluqard | 0:9c211972beb2 | 178 | #define REG_PLAYBACK_PLAY 1057980UL |
aluqard | 0:9c211972beb2 | 179 | #define REG_PLAYBACK_READPTR 1057964UL |
aluqard | 0:9c211972beb2 | 180 | #define REG_PLAYBACK_START 1057956UL |
aluqard | 0:9c211972beb2 | 181 | #define REG_PWM_DUTY 1057988UL |
aluqard | 0:9c211972beb2 | 182 | #define REG_PWM_HZ 1057984UL |
aluqard | 0:9c211972beb2 | 183 | #define REG_ROTATE 1057876UL |
aluqard | 0:9c211972beb2 | 184 | #define REG_SOUND 1057924UL |
aluqard | 0:9c211972beb2 | 185 | #define REG_SWIZZLE 1057888UL |
aluqard | 0:9c211972beb2 | 186 | #define REG_TAG 1057912UL |
aluqard | 0:9c211972beb2 | 187 | #define REG_TAG_X 1057904UL |
aluqard | 0:9c211972beb2 | 188 | #define REG_TAG_Y 1057908UL |
aluqard | 0:9c211972beb2 | 189 | #define REG_TOUCH_ADC_MODE 1058036UL |
aluqard | 0:9c211972beb2 | 190 | #define REG_TOUCH_CHARGE 1058040UL |
aluqard | 0:9c211972beb2 | 191 | #define REG_TOUCH_DIRECT_XY 1058164UL |
aluqard | 0:9c211972beb2 | 192 | #define REG_TOUCH_DIRECT_Z1Z2 1058168UL |
aluqard | 0:9c211972beb2 | 193 | #define REG_TOUCH_MODE 1058032UL |
aluqard | 0:9c211972beb2 | 194 | #define REG_TOUCH_OVERSAMPLE 1058048UL |
aluqard | 0:9c211972beb2 | 195 | #define REG_TOUCH_RAW_XY 1058056UL |
aluqard | 0:9c211972beb2 | 196 | #define REG_TOUCH_RZ 1058060UL |
aluqard | 0:9c211972beb2 | 197 | #define REG_TOUCH_RZTHRESH 1058052UL |
aluqard | 0:9c211972beb2 | 198 | #define REG_TOUCH_SCREEN_XY 1058064UL |
aluqard | 0:9c211972beb2 | 199 | #define REG_TOUCH_SETTLE 1058044UL |
aluqard | 0:9c211972beb2 | 200 | #define REG_TOUCH_TAG 1058072UL |
aluqard | 0:9c211972beb2 | 201 | #define REG_TOUCH_TAG_XY 1058068UL |
aluqard | 0:9c211972beb2 | 202 | #define REG_TOUCH_TRANSFORM_A 1058076UL |
aluqard | 0:9c211972beb2 | 203 | #define REG_TOUCH_TRANSFORM_B 1058080UL |
aluqard | 0:9c211972beb2 | 204 | #define REG_TOUCH_TRANSFORM_C 1058084UL |
aluqard | 0:9c211972beb2 | 205 | #define REG_TOUCH_TRANSFORM_D 1058088UL |
aluqard | 0:9c211972beb2 | 206 | #define REG_TOUCH_TRANSFORM_E 1058092UL |
aluqard | 0:9c211972beb2 | 207 | #define REG_TOUCH_TRANSFORM_F 1058096UL |
aluqard | 0:9c211972beb2 | 208 | #define REG_TRACKER 1085440UL |
aluqard | 0:9c211972beb2 | 209 | #define REG_VCYCLE 1057852UL |
aluqard | 0:9c211972beb2 | 210 | #define REG_VOFFSET 1057856UL |
aluqard | 0:9c211972beb2 | 211 | #define REG_VOL_PB 1057916UL |
aluqard | 0:9c211972beb2 | 212 | #define REG_VOL_SOUND 1057920UL |
aluqard | 0:9c211972beb2 | 213 | #define REG_VSIZE 1057860UL |
aluqard | 0:9c211972beb2 | 214 | #define REG_VSYNC0 1057864UL |
aluqard | 0:9c211972beb2 | 215 | #define REG_VSYNC1 1057868UL |
aluqard | 0:9c211972beb2 | 216 | |
aluqard | 0:9c211972beb2 | 217 | #define VERTEX2II(x, y, handle, cell) \ |
aluqard | 0:9c211972beb2 | 218 | ((2UL << 30) | (((x) & 511UL) << 21) | (((y) & 511UL) << 12) | (((handle) & 31) << 7) | (((cell) & 127) << 0)) |
aluqard | 0:9c211972beb2 | 219 | |
aluqard | 0:9c211972beb2 | 220 | #define ROM_PIXEL_FF 0xc0400UL |
aluqard | 0:9c211972beb2 | 221 | |
aluqard | 0:9c211972beb2 | 222 | class GDTransport { |
aluqard | 0:9c211972beb2 | 223 | protected: |
aluqard | 0:9c211972beb2 | 224 | SPI _spi; |
aluqard | 0:9c211972beb2 | 225 | DigitalOut _cs; |
aluqard | 0:9c211972beb2 | 226 | |
aluqard | 0:9c211972beb2 | 227 | byte streaming; |
aluqard | 0:9c211972beb2 | 228 | uint16_t wp; |
aluqard | 0:9c211972beb2 | 229 | uint16_t freespace; |
aluqard | 0:9c211972beb2 | 230 | |
aluqard | 0:9c211972beb2 | 231 | public: |
aluqard | 0:9c211972beb2 | 232 | GDTransport(PinName mosi, PinName miso, PinName sclk, PinName cs) |
aluqard | 0:9c211972beb2 | 233 | : _spi(mosi, miso, sclk) |
aluqard | 0:9c211972beb2 | 234 | , _cs(cs) |
aluqard | 0:9c211972beb2 | 235 | { |
aluqard | 0:9c211972beb2 | 236 | } |
aluqard | 0:9c211972beb2 | 237 | |
aluqard | 0:9c211972beb2 | 238 | void begin() |
aluqard | 0:9c211972beb2 | 239 | { |
aluqard | 0:9c211972beb2 | 240 | _spi.format(8, 0); |
aluqard | 0:9c211972beb2 | 241 | _spi.frequency(10000000); |
aluqard | 0:9c211972beb2 | 242 | |
aluqard | 0:9c211972beb2 | 243 | _cs = 1; |
aluqard | 0:9c211972beb2 | 244 | |
aluqard | 0:9c211972beb2 | 245 | hostcmd(0x00); // FT_GPU_ACTIVE_M |
aluqard | 0:9c211972beb2 | 246 | |
aluqard | 0:9c211972beb2 | 247 | hostcmd(0x62); // Switch PLL output to 48MHz |
aluqard | 0:9c211972beb2 | 248 | |
aluqard | 0:9c211972beb2 | 249 | hostcmd(0x68); // FT_GPU_CORE_RESET |
aluqard | 0:9c211972beb2 | 250 | |
aluqard | 0:9c211972beb2 | 251 | _spi.frequency(16000000); |
aluqard | 0:9c211972beb2 | 252 | |
aluqard | 0:9c211972beb2 | 253 | byte chipid = rd(REG_ID); |
aluqard | 0:9c211972beb2 | 254 | |
aluqard | 0:9c211972beb2 | 255 | DEBUGOUT("0x%x", chipid); |
aluqard | 0:9c211972beb2 | 256 | |
aluqard | 0:9c211972beb2 | 257 | wp = 0; |
aluqard | 0:9c211972beb2 | 258 | freespace = 4096 - 4; |
aluqard | 0:9c211972beb2 | 259 | |
aluqard | 0:9c211972beb2 | 260 | stream(); |
aluqard | 0:9c211972beb2 | 261 | } |
aluqard | 0:9c211972beb2 | 262 | |
aluqard | 0:9c211972beb2 | 263 | SPI* SPI() |
aluqard | 0:9c211972beb2 | 264 | { |
aluqard | 0:9c211972beb2 | 265 | return &_spi; |
aluqard | 0:9c211972beb2 | 266 | } |
aluqard | 0:9c211972beb2 | 267 | |
aluqard | 0:9c211972beb2 | 268 | void cmd32(uint32_t x) |
aluqard | 0:9c211972beb2 | 269 | { |
aluqard | 0:9c211972beb2 | 270 | if (freespace < 4) |
aluqard | 0:9c211972beb2 | 271 | { |
aluqard | 0:9c211972beb2 | 272 | getfree(4); |
aluqard | 0:9c211972beb2 | 273 | } |
aluqard | 0:9c211972beb2 | 274 | wp += 4; |
aluqard | 0:9c211972beb2 | 275 | freespace -= 4; |
aluqard | 0:9c211972beb2 | 276 | union |
aluqard | 0:9c211972beb2 | 277 | { |
aluqard | 0:9c211972beb2 | 278 | uint32_t c; |
aluqard | 0:9c211972beb2 | 279 | uint8_t b[4]; |
aluqard | 0:9c211972beb2 | 280 | }; |
aluqard | 0:9c211972beb2 | 281 | c = x; |
aluqard | 0:9c211972beb2 | 282 | _spi.write(b[0]); |
aluqard | 0:9c211972beb2 | 283 | _spi.write(b[1]); |
aluqard | 0:9c211972beb2 | 284 | _spi.write(b[2]); |
aluqard | 0:9c211972beb2 | 285 | _spi.write(b[3]); |
aluqard | 0:9c211972beb2 | 286 | } |
aluqard | 0:9c211972beb2 | 287 | void cmdbyte(byte x) |
aluqard | 0:9c211972beb2 | 288 | { |
aluqard | 0:9c211972beb2 | 289 | if (freespace == 0) |
aluqard | 0:9c211972beb2 | 290 | { |
aluqard | 0:9c211972beb2 | 291 | getfree(1); |
aluqard | 0:9c211972beb2 | 292 | } |
aluqard | 0:9c211972beb2 | 293 | wp++; |
aluqard | 0:9c211972beb2 | 294 | freespace--; |
aluqard | 0:9c211972beb2 | 295 | _spi.write(x); |
aluqard | 0:9c211972beb2 | 296 | } |
aluqard | 0:9c211972beb2 | 297 | void cmd_n(byte *s, uint16_t n) |
aluqard | 0:9c211972beb2 | 298 | { |
aluqard | 0:9c211972beb2 | 299 | if (freespace < n) |
aluqard | 0:9c211972beb2 | 300 | { |
aluqard | 0:9c211972beb2 | 301 | getfree(n); |
aluqard | 0:9c211972beb2 | 302 | } |
aluqard | 0:9c211972beb2 | 303 | wp += n; |
aluqard | 0:9c211972beb2 | 304 | freespace -= n; |
aluqard | 0:9c211972beb2 | 305 | while (n > 8) |
aluqard | 0:9c211972beb2 | 306 | { |
aluqard | 0:9c211972beb2 | 307 | n -= 8; |
aluqard | 0:9c211972beb2 | 308 | _spi.write(*s++); |
aluqard | 0:9c211972beb2 | 309 | _spi.write(*s++); |
aluqard | 0:9c211972beb2 | 310 | _spi.write(*s++); |
aluqard | 0:9c211972beb2 | 311 | _spi.write(*s++); |
aluqard | 0:9c211972beb2 | 312 | _spi.write(*s++); |
aluqard | 0:9c211972beb2 | 313 | _spi.write(*s++); |
aluqard | 0:9c211972beb2 | 314 | _spi.write(*s++); |
aluqard | 0:9c211972beb2 | 315 | _spi.write(*s++); |
aluqard | 0:9c211972beb2 | 316 | } |
aluqard | 0:9c211972beb2 | 317 | while (n--) |
aluqard | 0:9c211972beb2 | 318 | _spi.write(*s++); |
aluqard | 0:9c211972beb2 | 319 | } |
aluqard | 0:9c211972beb2 | 320 | |
aluqard | 0:9c211972beb2 | 321 | void flush() |
aluqard | 0:9c211972beb2 | 322 | { |
aluqard | 0:9c211972beb2 | 323 | getfree(0); |
aluqard | 0:9c211972beb2 | 324 | } |
aluqard | 0:9c211972beb2 | 325 | uint16_t rp() |
aluqard | 0:9c211972beb2 | 326 | { |
aluqard | 0:9c211972beb2 | 327 | uint16_t r = __rd16(REG_CMD_READ); |
aluqard | 0:9c211972beb2 | 328 | |
aluqard | 0:9c211972beb2 | 329 | if (r == 0xfff) |
aluqard | 0:9c211972beb2 | 330 | { |
aluqard | 0:9c211972beb2 | 331 | for (;;) ; |
aluqard | 0:9c211972beb2 | 332 | } |
aluqard | 0:9c211972beb2 | 333 | return r; |
aluqard | 0:9c211972beb2 | 334 | } |
aluqard | 0:9c211972beb2 | 335 | void finish() |
aluqard | 0:9c211972beb2 | 336 | { |
aluqard | 0:9c211972beb2 | 337 | wp &= 0xffc; |
aluqard | 0:9c211972beb2 | 338 | __end(); |
aluqard | 0:9c211972beb2 | 339 | __wr16(REG_CMD_WRITE, wp); |
aluqard | 0:9c211972beb2 | 340 | |
aluqard | 0:9c211972beb2 | 341 | while (rp() != wp); |
aluqard | 0:9c211972beb2 | 342 | |
aluqard | 0:9c211972beb2 | 343 | stream(); |
aluqard | 0:9c211972beb2 | 344 | } |
aluqard | 0:9c211972beb2 | 345 | |
aluqard | 0:9c211972beb2 | 346 | byte rd(uint32_t addr) |
aluqard | 0:9c211972beb2 | 347 | { |
aluqard | 0:9c211972beb2 | 348 | __end(); // stop streaming |
aluqard | 0:9c211972beb2 | 349 | __start(addr); |
aluqard | 0:9c211972beb2 | 350 | byte r = _spi.write(0); |
aluqard | 0:9c211972beb2 | 351 | stream(); |
aluqard | 0:9c211972beb2 | 352 | |
aluqard | 0:9c211972beb2 | 353 | return r; |
aluqard | 0:9c211972beb2 | 354 | } |
aluqard | 0:9c211972beb2 | 355 | |
aluqard | 0:9c211972beb2 | 356 | void wr(uint32_t addr, byte v) |
aluqard | 0:9c211972beb2 | 357 | { |
aluqard | 0:9c211972beb2 | 358 | __end(); // stop streaming |
aluqard | 0:9c211972beb2 | 359 | __wstart(addr); |
aluqard | 0:9c211972beb2 | 360 | _spi.write(v); |
aluqard | 0:9c211972beb2 | 361 | stream(); |
aluqard | 0:9c211972beb2 | 362 | } |
aluqard | 0:9c211972beb2 | 363 | |
aluqard | 0:9c211972beb2 | 364 | uint16_t rd16(uint32_t addr) |
aluqard | 0:9c211972beb2 | 365 | { |
aluqard | 0:9c211972beb2 | 366 | uint16_t r = 0; |
aluqard | 0:9c211972beb2 | 367 | __end(); // stop streaming |
aluqard | 0:9c211972beb2 | 368 | __start(addr); |
aluqard | 0:9c211972beb2 | 369 | r = _spi.write(0); |
aluqard | 0:9c211972beb2 | 370 | r |= (_spi.write(0) << 8); |
aluqard | 0:9c211972beb2 | 371 | stream(); |
aluqard | 0:9c211972beb2 | 372 | return r; |
aluqard | 0:9c211972beb2 | 373 | } |
aluqard | 0:9c211972beb2 | 374 | |
aluqard | 0:9c211972beb2 | 375 | void wr16(uint32_t addr, uint32_t v) |
aluqard | 0:9c211972beb2 | 376 | { |
aluqard | 0:9c211972beb2 | 377 | __end(); // stop streaming |
aluqard | 0:9c211972beb2 | 378 | __wstart(addr); |
aluqard | 0:9c211972beb2 | 379 | |
aluqard | 0:9c211972beb2 | 380 | _spi.write(v); |
aluqard | 0:9c211972beb2 | 381 | _spi.write(v >> 8); |
aluqard | 0:9c211972beb2 | 382 | stream(); |
aluqard | 0:9c211972beb2 | 383 | } |
aluqard | 0:9c211972beb2 | 384 | |
aluqard | 0:9c211972beb2 | 385 | uint32_t rd32(uint32_t addr) |
aluqard | 0:9c211972beb2 | 386 | { |
aluqard | 0:9c211972beb2 | 387 | __end(); // stop streaming |
aluqard | 0:9c211972beb2 | 388 | __start(addr); |
aluqard | 0:9c211972beb2 | 389 | // _spi.write(0); |
aluqard | 0:9c211972beb2 | 390 | union { |
aluqard | 0:9c211972beb2 | 391 | uint32_t c; |
aluqard | 0:9c211972beb2 | 392 | uint8_t b[4]; |
aluqard | 0:9c211972beb2 | 393 | }; |
aluqard | 0:9c211972beb2 | 394 | b[0] = _spi.write(0); |
aluqard | 0:9c211972beb2 | 395 | b[1] = _spi.write(0); |
aluqard | 0:9c211972beb2 | 396 | b[2] = _spi.write(0); |
aluqard | 0:9c211972beb2 | 397 | b[3] = _spi.write(0); |
aluqard | 0:9c211972beb2 | 398 | stream(); |
aluqard | 0:9c211972beb2 | 399 | return c; |
aluqard | 0:9c211972beb2 | 400 | } |
aluqard | 0:9c211972beb2 | 401 | void rd_n(byte *dst, uint32_t addr, uint16_t n) |
aluqard | 0:9c211972beb2 | 402 | { |
aluqard | 0:9c211972beb2 | 403 | __end(); // stop streaming |
aluqard | 0:9c211972beb2 | 404 | __start(addr); |
aluqard | 0:9c211972beb2 | 405 | // _spi.write(0); |
aluqard | 0:9c211972beb2 | 406 | while (n--) |
aluqard | 0:9c211972beb2 | 407 | *dst++ = _spi.write(0); |
aluqard | 0:9c211972beb2 | 408 | stream(); |
aluqard | 0:9c211972beb2 | 409 | } |
aluqard | 0:9c211972beb2 | 410 | void wr_n(uint32_t addr, byte *src, uint16_t n) |
aluqard | 0:9c211972beb2 | 411 | { |
aluqard | 0:9c211972beb2 | 412 | __end(); // stop streaming |
aluqard | 0:9c211972beb2 | 413 | __wstart(addr); |
aluqard | 0:9c211972beb2 | 414 | while (n--) |
aluqard | 0:9c211972beb2 | 415 | _spi.write(*src++); |
aluqard | 0:9c211972beb2 | 416 | stream(); |
aluqard | 0:9c211972beb2 | 417 | } |
aluqard | 0:9c211972beb2 | 418 | |
aluqard | 0:9c211972beb2 | 419 | void wr32(uint32_t addr, unsigned long v) |
aluqard | 0:9c211972beb2 | 420 | { |
aluqard | 0:9c211972beb2 | 421 | __end(); // stop streaming |
aluqard | 0:9c211972beb2 | 422 | __wstart(addr); |
aluqard | 0:9c211972beb2 | 423 | _spi.write(v); |
aluqard | 0:9c211972beb2 | 424 | _spi.write(v >> 8); |
aluqard | 0:9c211972beb2 | 425 | _spi.write(v >> 16); |
aluqard | 0:9c211972beb2 | 426 | _spi.write(v >> 24); |
aluqard | 0:9c211972beb2 | 427 | stream(); |
aluqard | 0:9c211972beb2 | 428 | } |
aluqard | 0:9c211972beb2 | 429 | |
aluqard | 0:9c211972beb2 | 430 | uint32_t getwp(void) |
aluqard | 0:9c211972beb2 | 431 | { |
aluqard | 0:9c211972beb2 | 432 | return RAM_CMD + (wp & 0xffc); |
aluqard | 0:9c211972beb2 | 433 | } |
aluqard | 0:9c211972beb2 | 434 | |
aluqard | 0:9c211972beb2 | 435 | void bulk(uint32_t addr) |
aluqard | 0:9c211972beb2 | 436 | { |
aluqard | 0:9c211972beb2 | 437 | __end(); // stop streaming |
aluqard | 0:9c211972beb2 | 438 | __start(addr); |
aluqard | 0:9c211972beb2 | 439 | } |
aluqard | 0:9c211972beb2 | 440 | void resume(void) |
aluqard | 0:9c211972beb2 | 441 | { |
aluqard | 0:9c211972beb2 | 442 | stream(); |
aluqard | 0:9c211972beb2 | 443 | } |
aluqard | 0:9c211972beb2 | 444 | |
aluqard | 0:9c211972beb2 | 445 | void __start(uint32_t addr) // start an SPI transaction to addr |
aluqard | 0:9c211972beb2 | 446 | { |
aluqard | 0:9c211972beb2 | 447 | _cs = 0; |
aluqard | 0:9c211972beb2 | 448 | _spi.write(addr >> 16); |
aluqard | 0:9c211972beb2 | 449 | _spi.write(addr >> 8); |
aluqard | 0:9c211972beb2 | 450 | _spi.write(addr & 0xff ); |
aluqard | 0:9c211972beb2 | 451 | _spi.write(0); |
aluqard | 0:9c211972beb2 | 452 | } |
aluqard | 0:9c211972beb2 | 453 | |
aluqard | 0:9c211972beb2 | 454 | void __wstart(uint32_t addr) // start an SPI write transaction to addr |
aluqard | 0:9c211972beb2 | 455 | { |
aluqard | 0:9c211972beb2 | 456 | _cs = 0; |
aluqard | 0:9c211972beb2 | 457 | _spi.write(0x80 | (addr >> 16)); |
aluqard | 0:9c211972beb2 | 458 | _spi.write(addr >> 8); |
aluqard | 0:9c211972beb2 | 459 | _spi.write(addr & 0xff); |
aluqard | 0:9c211972beb2 | 460 | } |
aluqard | 0:9c211972beb2 | 461 | |
aluqard | 0:9c211972beb2 | 462 | void __end() // end the SPI transaction |
aluqard | 0:9c211972beb2 | 463 | { |
aluqard | 0:9c211972beb2 | 464 | _cs = 1; |
aluqard | 0:9c211972beb2 | 465 | } |
aluqard | 0:9c211972beb2 | 466 | |
aluqard | 0:9c211972beb2 | 467 | void stop() // end the SPI transaction |
aluqard | 0:9c211972beb2 | 468 | { |
aluqard | 0:9c211972beb2 | 469 | wp &= 0xffc; |
aluqard | 0:9c211972beb2 | 470 | __end(); |
aluqard | 0:9c211972beb2 | 471 | __wr16(REG_CMD_WRITE, wp); |
aluqard | 0:9c211972beb2 | 472 | } |
aluqard | 0:9c211972beb2 | 473 | |
aluqard | 0:9c211972beb2 | 474 | void stream(void) { |
aluqard | 0:9c211972beb2 | 475 | __end(); |
aluqard | 0:9c211972beb2 | 476 | __wstart(RAM_CMD + (wp & 0xfff)); |
aluqard | 0:9c211972beb2 | 477 | } |
aluqard | 0:9c211972beb2 | 478 | |
aluqard | 0:9c211972beb2 | 479 | uint32_t __rd16(uint32_t addr) |
aluqard | 0:9c211972beb2 | 480 | { |
aluqard | 0:9c211972beb2 | 481 | uint32_t r; |
aluqard | 0:9c211972beb2 | 482 | |
aluqard | 0:9c211972beb2 | 483 | __start(addr); |
aluqard | 0:9c211972beb2 | 484 | r = _spi.write(0); |
aluqard | 0:9c211972beb2 | 485 | r |= (_spi.write(0) << 8); |
aluqard | 0:9c211972beb2 | 486 | __end(); |
aluqard | 0:9c211972beb2 | 487 | return r; |
aluqard | 0:9c211972beb2 | 488 | } |
aluqard | 0:9c211972beb2 | 489 | |
aluqard | 0:9c211972beb2 | 490 | void __wr16(uint32_t addr, uint32_t v) |
aluqard | 0:9c211972beb2 | 491 | { |
aluqard | 0:9c211972beb2 | 492 | __wstart(addr); |
aluqard | 0:9c211972beb2 | 493 | _spi.write(lowByte(v)); |
aluqard | 0:9c211972beb2 | 494 | _spi.write(highByte(v)); |
aluqard | 0:9c211972beb2 | 495 | __end(); |
aluqard | 0:9c211972beb2 | 496 | } |
aluqard | 0:9c211972beb2 | 497 | |
aluqard | 0:9c211972beb2 | 498 | void hostcmd(byte a) |
aluqard | 0:9c211972beb2 | 499 | { |
aluqard | 0:9c211972beb2 | 500 | _cs = 0; |
aluqard | 0:9c211972beb2 | 501 | |
aluqard | 0:9c211972beb2 | 502 | _spi.write(a); |
aluqard | 0:9c211972beb2 | 503 | _spi.write(0x00); |
aluqard | 0:9c211972beb2 | 504 | _spi.write(0x00); |
aluqard | 0:9c211972beb2 | 505 | |
aluqard | 0:9c211972beb2 | 506 | _cs = 1; |
aluqard | 0:9c211972beb2 | 507 | |
aluqard | 0:9c211972beb2 | 508 | delay(60); |
aluqard | 0:9c211972beb2 | 509 | } |
aluqard | 0:9c211972beb2 | 510 | |
aluqard | 0:9c211972beb2 | 511 | void getfree(uint16_t n) |
aluqard | 0:9c211972beb2 | 512 | { |
aluqard | 0:9c211972beb2 | 513 | wp &= 0xfff; |
aluqard | 0:9c211972beb2 | 514 | __end(); |
aluqard | 0:9c211972beb2 | 515 | __wr16(REG_CMD_WRITE, wp & 0xffc); |
aluqard | 0:9c211972beb2 | 516 | do { |
aluqard | 0:9c211972beb2 | 517 | uint16_t fullness = (wp - rp()) & 4095; |
aluqard | 0:9c211972beb2 | 518 | freespace = (4096 - 4) - fullness; |
aluqard | 0:9c211972beb2 | 519 | } while (freespace < n); |
aluqard | 0:9c211972beb2 | 520 | stream(); |
aluqard | 0:9c211972beb2 | 521 | } |
aluqard | 0:9c211972beb2 | 522 | }; |
aluqard | 0:9c211972beb2 | 523 | |
aluqard | 0:9c211972beb2 | 524 | #endif |