手動機アーム、mbed基盤のspiをおくるだけのプログラムです(9/4)

Dependencies:   SPI_master_arm_shudouki mbed

Fork of SPI_master_arm_shudouki2 by F^3 RC 2班

Committer:
yoka06
Date:
Mon Aug 21 08:49:06 2017 +0000
Revision:
0:76d1c7f13415
a

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yoka06 0:76d1c7f13415 1 /*
yoka06 0:76d1c7f13415 2 Copyright (c) 2011 Bart Janssens
yoka06 0:76d1c7f13415 3
yoka06 0:76d1c7f13415 4 Permission is hereby granted, free of charge, to any person obtaining a copy
yoka06 0:76d1c7f13415 5 of this software and associated documentation files (the "Software"), to deal
yoka06 0:76d1c7f13415 6 in the Software without restriction, including without limitation the rights
yoka06 0:76d1c7f13415 7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
yoka06 0:76d1c7f13415 8 copies of the Software, and to permit persons to whom the Software is
yoka06 0:76d1c7f13415 9 furnished to do so, subject to the following conditions:
yoka06 0:76d1c7f13415 10
yoka06 0:76d1c7f13415 11 The above copyright notice and this permission notice shall be included in
yoka06 0:76d1c7f13415 12 all copies or substantial portions of the Software.
yoka06 0:76d1c7f13415 13
yoka06 0:76d1c7f13415 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
yoka06 0:76d1c7f13415 15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
yoka06 0:76d1c7f13415 16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
yoka06 0:76d1c7f13415 17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
yoka06 0:76d1c7f13415 18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
yoka06 0:76d1c7f13415 19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
yoka06 0:76d1c7f13415 20 THE SOFTWARE.
yoka06 0:76d1c7f13415 21 */
yoka06 0:76d1c7f13415 22
yoka06 0:76d1c7f13415 23 #include <stdio.h>
yoka06 0:76d1c7f13415 24 #include <stdlib.h>
yoka06 0:76d1c7f13415 25 #include <string.h>
yoka06 0:76d1c7f13415 26
yoka06 0:76d1c7f13415 27 #include "USBHost.h"
yoka06 0:76d1c7f13415 28 #include "hci.h"
yoka06 0:76d1c7f13415 29 #include "Utils.h"
yoka06 0:76d1c7f13415 30 #include "ps3.h"
yoka06 0:76d1c7f13415 31 #include "User.h"
yoka06 0:76d1c7f13415 32
yoka06 0:76d1c7f13415 33 #include "mbed.h"
yoka06 0:76d1c7f13415 34
yoka06 0:76d1c7f13415 35 #define AUTOEVT(_class,_subclass,_protocol) (((_class) << 16) | ((_subclass) << 8) | _protocol)
yoka06 0:76d1c7f13415 36 #define PS3EVT AUTOEVT(CLASS_HID,0,0)
yoka06 0:76d1c7f13415 37 #define byteswap(x) ((x >> 8) | (x << 8))
yoka06 0:76d1c7f13415 38
yoka06 0:76d1c7f13415 39 u8 ps3_data[48];
yoka06 0:76d1c7f13415 40
yoka06 0:76d1c7f13415 41 Ps3USB::Ps3USB(int device, int configuration, int interfaceNumber)
yoka06 0:76d1c7f13415 42 {
yoka06 0:76d1c7f13415 43 printf("Creating new sixaxis \r\n");
yoka06 0:76d1c7f13415 44 _device = device;
yoka06 0:76d1c7f13415 45 _configuration = configuration;
yoka06 0:76d1c7f13415 46 _interfaceNumber = interfaceNumber;
yoka06 0:76d1c7f13415 47 printf("device = %d configuration = %d interfaceNumber = %d\r\n", device, configuration, interfaceNumber);
yoka06 0:76d1c7f13415 48 int result;
yoka06 0:76d1c7f13415 49 int err;
yoka06 0:76d1c7f13415 50
yoka06 0:76d1c7f13415 51 _count = 1;
yoka06 0:76d1c7f13415 52
yoka06 0:76d1c7f13415 53 u8 abuffer[48] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
yoka06 0:76d1c7f13415 54 0x00, 0x02, 0xff, 0x27, 0x10, 0x00, 0x32, 0xff,
yoka06 0:76d1c7f13415 55 0x27, 0x10, 0x00, 0x32, 0xff, 0x27, 0x10, 0x00,
yoka06 0:76d1c7f13415 56 0x32, 0xff, 0x27, 0x10, 0x00, 0x32, 0x00, 0x00,
yoka06 0:76d1c7f13415 57 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
yoka06 0:76d1c7f13415 58 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
yoka06 0:76d1c7f13415 59 memcpy(ledrumble,abuffer,48);
yoka06 0:76d1c7f13415 60
yoka06 0:76d1c7f13415 61 EndpointDescriptor* ep;
yoka06 0:76d1c7f13415 62
yoka06 0:76d1c7f13415 63 u8 buffer[255];
yoka06 0:76d1c7f13415 64 err = GetDescriptor(_device,DESCRIPTOR_TYPE_CONFIGURATION,0,buffer,255);
yoka06 0:76d1c7f13415 65 if (err < 0)
yoka06 0:76d1c7f13415 66 printf("Failed to get descriptor\r\n");
yoka06 0:76d1c7f13415 67
yoka06 0:76d1c7f13415 68
yoka06 0:76d1c7f13415 69 int len = buffer[2] | (buffer[3] << 8);
yoka06 0:76d1c7f13415 70 u8* d = buffer;
yoka06 0:76d1c7f13415 71 u8* end = d + len;
yoka06 0:76d1c7f13415 72 while (d < end)
yoka06 0:76d1c7f13415 73 {
yoka06 0:76d1c7f13415 74 if (d[1] == DESCRIPTOR_TYPE_INTERFACE)
yoka06 0:76d1c7f13415 75 {
yoka06 0:76d1c7f13415 76 InterfaceDescriptor* id = (InterfaceDescriptor*)d;
yoka06 0:76d1c7f13415 77 if (id->bInterfaceNumber == _interfaceNumber)
yoka06 0:76d1c7f13415 78 {
yoka06 0:76d1c7f13415 79 d += d[0];
yoka06 0:76d1c7f13415 80 while (d < end && d[1] != DESCRIPTOR_TYPE_INTERFACE)
yoka06 0:76d1c7f13415 81 {
yoka06 0:76d1c7f13415 82 if (d[1] == DESCRIPTOR_TYPE_ENDPOINT)
yoka06 0:76d1c7f13415 83 ep = (EndpointDescriptor*)d;
yoka06 0:76d1c7f13415 84
yoka06 0:76d1c7f13415 85 if (ep->bEndpointAddress == 0x02) {
yoka06 0:76d1c7f13415 86 printf("PS3 input endpoint (0x02) found\r\n");
yoka06 0:76d1c7f13415 87 input_ep = 0x02;
yoka06 0:76d1c7f13415 88
yoka06 0:76d1c7f13415 89 }
yoka06 0:76d1c7f13415 90 if (ep->bEndpointAddress == 0x81) {
yoka06 0:76d1c7f13415 91 printf("PS3 output endpoint (0x81) found\r\n");
yoka06 0:76d1c7f13415 92 output_ep = 0x81;
yoka06 0:76d1c7f13415 93 //AddAutoEvent(device,id,(EndpointDescriptor*)d);
yoka06 0:76d1c7f13415 94 }
yoka06 0:76d1c7f13415 95 d += d[0];
yoka06 0:76d1c7f13415 96 }
yoka06 0:76d1c7f13415 97 }
yoka06 0:76d1c7f13415 98 }
yoka06 0:76d1c7f13415 99 d += d[0];
yoka06 0:76d1c7f13415 100 }
yoka06 0:76d1c7f13415 101
yoka06 0:76d1c7f13415 102 }
yoka06 0:76d1c7f13415 103
yoka06 0:76d1c7f13415 104 int Ps3USB::Enable()
yoka06 0:76d1c7f13415 105 {
yoka06 0:76d1c7f13415 106 int err;
yoka06 0:76d1c7f13415 107
yoka06 0:76d1c7f13415 108 u8 enable[4] = {0x42,0x0c,0x00,0x00};
yoka06 0:76d1c7f13415 109
yoka06 0:76d1c7f13415 110
yoka06 0:76d1c7f13415 111 err = USBControlTransfer(_device, HOST_TO_DEVICE|REQUEST_TYPE_CLASS|RECIPIENT_INTERFACE, HID_REQUEST_SET_REPORT, 0x03f4,0, enable, sizeof(enable), 0, 0 );
yoka06 0:76d1c7f13415 112 //printf("set report result = %d\r\n", err);
yoka06 0:76d1c7f13415 113 _count ++;
yoka06 0:76d1c7f13415 114 if (_count == 25) _count = 1;
yoka06 0:76d1c7f13415 115
yoka06 0:76d1c7f13415 116 err = USBInterruptTransfer(_device,output_ep,ps3_data,sizeof(ps3_data),PS3EventCallback,this);
yoka06 0:76d1c7f13415 117 wait_ms(4);
yoka06 0:76d1c7f13415 118
yoka06 0:76d1c7f13415 119 return 0;
yoka06 0:76d1c7f13415 120
yoka06 0:76d1c7f13415 121 }
yoka06 0:76d1c7f13415 122
yoka06 0:76d1c7f13415 123
yoka06 0:76d1c7f13415 124
yoka06 0:76d1c7f13415 125
yoka06 0:76d1c7f13415 126 int Ps3USB::SetPair(u8* bdAddr)
yoka06 0:76d1c7f13415 127 {
yoka06 0:76d1c7f13415 128 int err;
yoka06 0:76d1c7f13415 129
yoka06 0:76d1c7f13415 130 u8 buf[8];
yoka06 0:76d1c7f13415 131 u8 buf2[6];
yoka06 0:76d1c7f13415 132
yoka06 0:76d1c7f13415 133 memcpy(buf2,bdAddr,6);
yoka06 0:76d1c7f13415 134
yoka06 0:76d1c7f13415 135 buf[0] = 0x01;
yoka06 0:76d1c7f13415 136 buf[1] = 0x00;
yoka06 0:76d1c7f13415 137 buf[2] = buf2[0];
yoka06 0:76d1c7f13415 138 buf[3] = buf2[1];
yoka06 0:76d1c7f13415 139 buf[4] = buf2[2];
yoka06 0:76d1c7f13415 140 buf[5] = buf2[3];
yoka06 0:76d1c7f13415 141 buf[6] = buf2[4];
yoka06 0:76d1c7f13415 142 buf[7] = buf2[5];
yoka06 0:76d1c7f13415 143
yoka06 0:76d1c7f13415 144 //set Mac address
yoka06 0:76d1c7f13415 145 err = USBControlTransfer(_device, HOST_TO_DEVICE|REQUEST_TYPE_CLASS|RECIPIENT_INTERFACE, HID_REQUEST_SET_REPORT, 0x03f5, 0, buf, sizeof(buf), 0, 0 );
yoka06 0:76d1c7f13415 146 wait_ms(4);
yoka06 0:76d1c7f13415 147 printf("set Mac address to %02X:%02X:%02X:%02X:%02X:%02X , result = %d\r\n", buf[2], buf[3], buf[4], buf[5], buf[6], buf[7], err);
yoka06 0:76d1c7f13415 148
yoka06 0:76d1c7f13415 149 return 0;
yoka06 0:76d1c7f13415 150 }
yoka06 0:76d1c7f13415 151
yoka06 0:76d1c7f13415 152 int Ps3USB::ShowPair()
yoka06 0:76d1c7f13415 153 {
yoka06 0:76d1c7f13415 154 int err;
yoka06 0:76d1c7f13415 155
yoka06 0:76d1c7f13415 156 u8 buf[8];
yoka06 0:76d1c7f13415 157 //get Mac address
yoka06 0:76d1c7f13415 158 err = USBControlTransfer(_device, DEVICE_TO_HOST|REQUEST_TYPE_CLASS|RECIPIENT_INTERFACE, HID_REQUEST_GET_REPORT, 0x03f5, 0, buf, sizeof(buf), 0, 0 );
yoka06 0:76d1c7f13415 159 wait_ms(4);
yoka06 0:76d1c7f13415 160 printf("Mac address is set to %02X:%02X:%02X:%02X:%02X:%02X , result = %d\r\n",buf[2], buf[3], buf[4], buf[5], buf[6], buf[7], err);
yoka06 0:76d1c7f13415 161
yoka06 0:76d1c7f13415 162
yoka06 0:76d1c7f13415 163 return 0;
yoka06 0:76d1c7f13415 164 }
yoka06 0:76d1c7f13415 165
yoka06 0:76d1c7f13415 166 int Ps3USB::Led(int i)
yoka06 0:76d1c7f13415 167 {
yoka06 0:76d1c7f13415 168 int err;
yoka06 0:76d1c7f13415 169 u8 ledpattern[7] = {0x02, 0x04, 0x08, 0x10, 0x12, 0x14, 0x18 };
yoka06 0:76d1c7f13415 170 u8 buf[48];
yoka06 0:76d1c7f13415 171
yoka06 0:76d1c7f13415 172 if (i < 7) ledrumble[9] = ledpattern[i];
yoka06 0:76d1c7f13415 173 memcpy(buf, ledrumble, 48);
yoka06 0:76d1c7f13415 174
yoka06 0:76d1c7f13415 175 err = USBControlTransfer(_device, HOST_TO_DEVICE|REQUEST_TYPE_CLASS|RECIPIENT_INTERFACE, HID_REQUEST_SET_REPORT, 0x0201,0, buf, sizeof(buf), 0, 0 );
yoka06 0:76d1c7f13415 176 wait_ms(4);
yoka06 0:76d1c7f13415 177
yoka06 0:76d1c7f13415 178 return 0;
yoka06 0:76d1c7f13415 179 }
yoka06 0:76d1c7f13415 180
yoka06 0:76d1c7f13415 181 // left and right: duration and power, both from 0 to 255
yoka06 0:76d1c7f13415 182 int Ps3USB::Rumble(u8 duration_right, u8 power_right, u8 duration_left, u8 power_left)
yoka06 0:76d1c7f13415 183 {
yoka06 0:76d1c7f13415 184 int err;
yoka06 0:76d1c7f13415 185 u8 buf[48];
yoka06 0:76d1c7f13415 186
yoka06 0:76d1c7f13415 187 memcpy(buf, ledrumble, 48);
yoka06 0:76d1c7f13415 188 buf[1] = duration_right;
yoka06 0:76d1c7f13415 189 buf[2] = power_right;
yoka06 0:76d1c7f13415 190 buf[3] = duration_left;
yoka06 0:76d1c7f13415 191 buf[4] = power_left;
yoka06 0:76d1c7f13415 192
yoka06 0:76d1c7f13415 193 err = USBControlTransfer(_device, HOST_TO_DEVICE|REQUEST_TYPE_CLASS|RECIPIENT_INTERFACE, HID_REQUEST_SET_REPORT, 0x0201,0, buf, sizeof(buf), 0, 0 );
yoka06 0:76d1c7f13415 194 wait_ms(4);
yoka06 0:76d1c7f13415 195
yoka06 0:76d1c7f13415 196 return 0;
yoka06 0:76d1c7f13415 197 }
yoka06 0:76d1c7f13415 198
yoka06 0:76d1c7f13415 199
yoka06 0:76d1c7f13415 200 void PS3EventCallback(int device, int endpoint, int status, u8* data, int len, void* userData)
yoka06 0:76d1c7f13415 201 {
yoka06 0:76d1c7f13415 202 Ps3USB* t = (Ps3USB*)userData;
yoka06 0:76d1c7f13415 203
yoka06 0:76d1c7f13415 204 t->_count ++;
yoka06 0:76d1c7f13415 205 if (t->_count == 25) t->_count = 1;
yoka06 0:76d1c7f13415 206
yoka06 0:76d1c7f13415 207 ParsePs3Result(data, sizeof(ps3report),t->_count);
yoka06 0:76d1c7f13415 208 USBInterruptTransfer(device, endpoint , data, len, PS3EventCallback, userData);
yoka06 0:76d1c7f13415 209 wait_ms(4);
yoka06 0:76d1c7f13415 210
yoka06 0:76d1c7f13415 211 }
yoka06 0:76d1c7f13415 212
yoka06 0:76d1c7f13415 213 int ParsePs3Result(const u8* data,int len,int count)
yoka06 0:76d1c7f13415 214 {
yoka06 0:76d1c7f13415 215 ps3report* _ps3report = (ps3report*)data;
yoka06 0:76d1c7f13415 216 /* if (count == 24) printf("LSX LSY RSX RSY UPA RPA DPA RPA L2 R2 L1 R1 TRI CIR CRO SQU ACX ACY ACZ GYZ \r\n");
yoka06 0:76d1c7f13415 217 printf("%3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %4d %4d %4d %4d \r\n",
yoka06 0:76d1c7f13415 218 _ps3report->LeftStickX,
yoka06 0:76d1c7f13415 219 _ps3report->LeftStickY,
yoka06 0:76d1c7f13415 220 _ps3report->RightStickX,
yoka06 0:76d1c7f13415 221 _ps3report->RightStickY,
yoka06 0:76d1c7f13415 222 _ps3report->PressureUp,
yoka06 0:76d1c7f13415 223 _ps3report->PressureRight,
yoka06 0:76d1c7f13415 224 _ps3report->PressureDown,
yoka06 0:76d1c7f13415 225 _ps3report->PressureLeft,
yoka06 0:76d1c7f13415 226 _ps3report->PressureL2,
yoka06 0:76d1c7f13415 227 _ps3report->PressureR2,
yoka06 0:76d1c7f13415 228 _ps3report->PressureL1,
yoka06 0:76d1c7f13415 229 _ps3report->PressureR1,
yoka06 0:76d1c7f13415 230 _ps3report->PressureTriangle,
yoka06 0:76d1c7f13415 231 _ps3report->PressureCircle,
yoka06 0:76d1c7f13415 232 _ps3report->PressureCross,
yoka06 0:76d1c7f13415 233 _ps3report->PressureSquare,
yoka06 0:76d1c7f13415 234 (_ps3report->AccelX),
yoka06 0:76d1c7f13415 235 (_ps3report->AccelY),
yoka06 0:76d1c7f13415 236 (_ps3report->AccelZ),
yoka06 0:76d1c7f13415 237 (_ps3report->GyroZ));
yoka06 0:76d1c7f13415 238 */
yoka06 0:76d1c7f13415 239 UserLoop(0,data);
yoka06 0:76d1c7f13415 240 //printfBytes("data",data,len);
yoka06 0:76d1c7f13415 241 }
yoka06 0:76d1c7f13415 242
yoka06 0:76d1c7f13415 243