EtherCAT slave that reads 3 Xsens IMU's connected to a Xbus Master

Dependencies:   MODSERIAL mbed KL25Z_ClockControl

Fork of EtherCAT by First Last

Committer:
vsluiter
Date:
Thu Mar 05 22:22:47 2015 +0000
Revision:
17:c5946a0fde83
Parent:
16:bfc7ea6bd1af
Child:
18:6629e8c5d59e
BusMaster working. Added code to copy sensor data, not tested yet.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vsluiter 13:5e4dcbd44786 1 #include "xbus.h"
vsluiter 13:5e4dcbd44786 2
vsluiter 13:5e4dcbd44786 3 #define ARRAY(x) x, sizeof(x)
vsluiter 13:5e4dcbd44786 4 static inline void XbusReceiveState(struct xbus_struct *, uint8_t );
vsluiter 16:bfc7ea6bd1af 5 uint8_t XbusImuMsgGoToMeasurement[] = { 0xFA,
vsluiter 16:bfc7ea6bd1af 6 0xFF,
vsluiter 16:bfc7ea6bd1af 7 0x10,
vsluiter 16:bfc7ea6bd1af 8 0x00,
vsluiter 16:bfc7ea6bd1af 9 0xF1
vsluiter 16:bfc7ea6bd1af 10 };
vsluiter 13:5e4dcbd44786 11
vsluiter 13:5e4dcbd44786 12 uint8_t XbusImuMsgGoToConfig[] = { 0xFA,
vsluiter 16:bfc7ea6bd1af 13 0xFF,
vsluiter 16:bfc7ea6bd1af 14 0x30,
vsluiter 16:bfc7ea6bd1af 15 0x00,
vsluiter 16:bfc7ea6bd1af 16 0xD1
vsluiter 16:bfc7ea6bd1af 17 };
vsluiter 13:5e4dcbd44786 18
vsluiter 16:bfc7ea6bd1af 19 uint8_t XbusImuMsgSetPeriod[] = { 0xFA,
vsluiter 16:bfc7ea6bd1af 20 0xFF,
vsluiter 16:bfc7ea6bd1af 21 0x04, /* MID: SetPeriod */
vsluiter 16:bfc7ea6bd1af 22 0x02,
vsluiter 16:bfc7ea6bd1af 23 0x04, /* MSB time */
vsluiter 16:bfc7ea6bd1af 24 0x80, /* LSB time */ /*time in 8.68us*/ /*0480 eq. 10ms*/
vsluiter 16:bfc7ea6bd1af 25 0x77
vsluiter 16:bfc7ea6bd1af 26 };
vsluiter 13:5e4dcbd44786 27
vsluiter 16:bfc7ea6bd1af 28 uint8_t XbusImuMsgSetOutputMode[] = { 0xFA,
vsluiter 16:bfc7ea6bd1af 29 0xFF,
vsluiter 16:bfc7ea6bd1af 30 0xD0, /* MID: SetOutpuMode */
vsluiter 16:bfc7ea6bd1af 31 0x02,
vsluiter 16:bfc7ea6bd1af 32 0x40, /* MSB */
vsluiter 16:bfc7ea6bd1af 33 0x00, /* LSB */ /*4000 eq. raw data mode*/
vsluiter 16:bfc7ea6bd1af 34 0xEF
vsluiter 16:bfc7ea6bd1af 35 };
vsluiter 13:5e4dcbd44786 36
vsluiter 16:bfc7ea6bd1af 37 uint8_t XbusImuMsgSetSyncIn_SetSync[] = {0xFA,
vsluiter 16:bfc7ea6bd1af 38 0xFF,
vsluiter 16:bfc7ea6bd1af 39 0xD6, /* MID: SetSyncIn */
vsluiter 16:bfc7ea6bd1af 40 0x03,
vsluiter 16:bfc7ea6bd1af 41 0x00, /* SyncInMode*/
vsluiter 16:bfc7ea6bd1af 42 0x00, /* Reserved */
vsluiter 16:bfc7ea6bd1af 43 0x01, /* Sample ADC, RisingEdge*/
vsluiter 16:bfc7ea6bd1af 44 0x27
vsluiter 16:bfc7ea6bd1af 45 };
vsluiter 13:5e4dcbd44786 46
vsluiter 16:bfc7ea6bd1af 47 uint8_t XbusImuMsgSetSyncIn_SetSkip[] = {0xFA,
vsluiter 16:bfc7ea6bd1af 48 0xFF,
vsluiter 16:bfc7ea6bd1af 49 0xD6, /* MID: SetSyncIn */
vsluiter 16:bfc7ea6bd1af 50 0x03,
vsluiter 16:bfc7ea6bd1af 51 0x01, /* SkipFactor*/
vsluiter 16:bfc7ea6bd1af 52 0x00, /* MSB SkipFactor */
vsluiter 16:bfc7ea6bd1af 53 0x00, /* LSB SkipFactor */
vsluiter 16:bfc7ea6bd1af 54 0x27
vsluiter 16:bfc7ea6bd1af 55 };
vsluiter 13:5e4dcbd44786 56
vsluiter 16:bfc7ea6bd1af 57 uint8_t XbusImuMsgSetSyncIn_SetOffs[] = {0xFA,
vsluiter 16:bfc7ea6bd1af 58 0xFF,
vsluiter 16:bfc7ea6bd1af 59 0xD6, /* MID: SetSyncIn */
vsluiter 16:bfc7ea6bd1af 60 0x05,
vsluiter 16:bfc7ea6bd1af 61 0x02, /* Offset*/
vsluiter 16:bfc7ea6bd1af 62 0x00, /* Bit 31..24 */
vsluiter 16:bfc7ea6bd1af 63 0x01, /* Bit 23..16 */
vsluiter 16:bfc7ea6bd1af 64 0x06, /* Bit 15..8 */
vsluiter 16:bfc7ea6bd1af 65 0xB9, /* Bit 7..0 */ /*minimum value: 264*33.9ns*/ /*E675 eq. 2ms*/
vsluiter 16:bfc7ea6bd1af 66 0x64
vsluiter 16:bfc7ea6bd1af 67 };
vsluiter 13:5e4dcbd44786 68
vsluiter 16:bfc7ea6bd1af 69 uint8_t XbusImuMsgSetSyncOut_SetSync[] = {0xFA,
vsluiter 16:bfc7ea6bd1af 70 0xFF,
vsluiter 16:bfc7ea6bd1af 71 0xD8, /* MID: SetSyncOut */
vsluiter 16:bfc7ea6bd1af 72 0x03,
vsluiter 16:bfc7ea6bd1af 73 0x00, /* SyncOutMode*/
vsluiter 16:bfc7ea6bd1af 74 0x00, /* Reserved */
vsluiter 16:bfc7ea6bd1af 75 0x12, /* Postitive Pulse, Pulse Mode*/
vsluiter 16:bfc7ea6bd1af 76 0x14
vsluiter 16:bfc7ea6bd1af 77 };
vsluiter 13:5e4dcbd44786 78
vsluiter 16:bfc7ea6bd1af 79 uint8_t XbusImuMsgSetSyncOut_SetSkip[] = {0xFA,
vsluiter 16:bfc7ea6bd1af 80 0xFF,
vsluiter 16:bfc7ea6bd1af 81 0xD8, /* MID: SetSyncOut */
vsluiter 16:bfc7ea6bd1af 82 0x03,
vsluiter 16:bfc7ea6bd1af 83 0x01, /* SkipFactor*/
vsluiter 16:bfc7ea6bd1af 84 0x00, /* MSB SkipFactor */
vsluiter 16:bfc7ea6bd1af 85 0x00, /* LSB SkipFactor */
vsluiter 16:bfc7ea6bd1af 86 0x25
vsluiter 16:bfc7ea6bd1af 87 };
vsluiter 13:5e4dcbd44786 88
vsluiter 16:bfc7ea6bd1af 89 uint8_t XbusImuMsgSetSyncOut_SetOffs[] = {0xFA,
vsluiter 16:bfc7ea6bd1af 90 0xFF,
vsluiter 16:bfc7ea6bd1af 91 0xD8, /* MID: SetSyncOut */
vsluiter 16:bfc7ea6bd1af 92 0x05,
vsluiter 16:bfc7ea6bd1af 93 0x02, /* Offset*/
vsluiter 16:bfc7ea6bd1af 94 0x00, /* Bit 31..24 */
vsluiter 16:bfc7ea6bd1af 95 0x00, /* Bit 23..16 */
vsluiter 16:bfc7ea6bd1af 96 0xE6, /* Bit 15..8 */
vsluiter 16:bfc7ea6bd1af 97 0x75, /* Bit 7..0 */ /*minimum value: 513*33.9ns*/ /*E675 eq. 2ms*/
vsluiter 16:bfc7ea6bd1af 98 0xC7
vsluiter 16:bfc7ea6bd1af 99 };
vsluiter 13:5e4dcbd44786 100
vsluiter 16:bfc7ea6bd1af 101 uint8_t XbusImuMsgSetSyncOut_SetWidth[] = {0xFA,
vsluiter 16:bfc7ea6bd1af 102 0xFF,
vsluiter 16:bfc7ea6bd1af 103 0xD8, /* MID: SetSyncOut */
vsluiter 16:bfc7ea6bd1af 104 0x05,
vsluiter 16:bfc7ea6bd1af 105 0x03, /* Width*/
vsluiter 16:bfc7ea6bd1af 106 0x00, /* Bit 31..24 */
vsluiter 16:bfc7ea6bd1af 107 0x00, /* Bit 23..16 */
vsluiter 16:bfc7ea6bd1af 108 0xE6, /* Bit 15..8 */
vsluiter 16:bfc7ea6bd1af 109 0x75, /* Bit 7..0 */ /*minimum value: 1700*33.9ns*/ /*E675 eq. 2ms*/
vsluiter 16:bfc7ea6bd1af 110 0xC6
vsluiter 16:bfc7ea6bd1af 111 };
vsluiter 13:5e4dcbd44786 112
vsluiter 17:c5946a0fde83 113 static void XbusSendArray(uint8_t *array)
vsluiter 13:5e4dcbd44786 114 {
vsluiter 17:c5946a0fde83 115 for(int i = 0; i < array[3]+5 ; i++ ) { //make use of len
vsluiter 16:bfc7ea6bd1af 116 xbus.putc(array[i]);
vsluiter 16:bfc7ea6bd1af 117 }
vsluiter 16:bfc7ea6bd1af 118 wait_ms(10);
vsluiter 13:5e4dcbd44786 119 }
vsluiter 13:5e4dcbd44786 120
vsluiter 13:5e4dcbd44786 121 void XbusInitializeXbusMaster(void)
vsluiter 13:5e4dcbd44786 122 {
vsluiter 17:c5946a0fde83 123 uint8_t msg[30];
vsluiter 17:c5946a0fde83 124 uint8_t data[10];
vsluiter 17:c5946a0fde83 125
vsluiter 17:c5946a0fde83 126 //XbusCreateMessage(0xFF,0x40,0,msg,msg);//Reset
vsluiter 17:c5946a0fde83 127 //XbusSendArray(msg);
vsluiter 17:c5946a0fde83 128 //wait_ms(1000);
vsluiter 17:c5946a0fde83 129
vsluiter 17:c5946a0fde83 130 XbusCreateMessage(0xFF,0x30,0,msg,msg);//GoToConfig
vsluiter 17:c5946a0fde83 131 XbusSendArray(msg);
vsluiter 17:c5946a0fde83 132
vsluiter 17:c5946a0fde83 133
vsluiter 17:c5946a0fde83 134 data[0] = 0x04;
vsluiter 17:c5946a0fde83 135 data[1] = 0x80;
vsluiter 17:c5946a0fde83 136 XbusCreateMessage(0xFF,0x04,2,data,msg);//SetPeriod
vsluiter 17:c5946a0fde83 137 XbusSendArray(msg);
vsluiter 17:c5946a0fde83 138
vsluiter 17:c5946a0fde83 139 data[0] = 0x40;
vsluiter 17:c5946a0fde83 140 data[1] = 0x00;
vsluiter 17:c5946a0fde83 141 for (int i = 0 ; i<3 ; i++)
vsluiter 17:c5946a0fde83 142 {
vsluiter 17:c5946a0fde83 143 XbusCreateMessage(i+1,0xD0,2,data,msg);//Set Output Data Format
vsluiter 17:c5946a0fde83 144 XbusSendArray(msg);
vsluiter 17:c5946a0fde83 145 }
vsluiter 17:c5946a0fde83 146 wait_ms(10);
vsluiter 17:c5946a0fde83 147 XbusCreateMessage(0xFF,0x10,0,msg,msg);//GoToMeasurement
vsluiter 17:c5946a0fde83 148 XbusSendArray(msg);
vsluiter 13:5e4dcbd44786 149 }
vsluiter 13:5e4dcbd44786 150
vsluiter 13:5e4dcbd44786 151 void XbusReceiveState(struct xbus_struct * xbus, uint8_t rxdata)
vsluiter 13:5e4dcbd44786 152 {
vsluiter 16:bfc7ea6bd1af 153 switch(xbus->rx.state) {
vsluiter 16:bfc7ea6bd1af 154 case XBUS_IDLE: {
vsluiter 16:bfc7ea6bd1af 155 if(rxdata == 0xFA) {
vsluiter 13:5e4dcbd44786 156 xbus->rx.counter = 0;
vsluiter 13:5e4dcbd44786 157 xbus->rx.buffer[0] = 0xFA;
vsluiter 13:5e4dcbd44786 158 xbus->rx.message_complete = 0;
vsluiter 13:5e4dcbd44786 159 xbus->rx.state = XBUS_BID;
vsluiter 13:5e4dcbd44786 160 }
vsluiter 13:5e4dcbd44786 161 break;
vsluiter 13:5e4dcbd44786 162 }
vsluiter 16:bfc7ea6bd1af 163 case XBUS_BID: {
vsluiter 13:5e4dcbd44786 164 xbus->rx.counter = 1;
vsluiter 13:5e4dcbd44786 165 xbus->rx.buffer[xbus->rx.counter] = rxdata;
vsluiter 13:5e4dcbd44786 166 xbus->rx.checksum = rxdata;
vsluiter 13:5e4dcbd44786 167 xbus->rx.state = XBUS_MID;
vsluiter 13:5e4dcbd44786 168 break;
vsluiter 13:5e4dcbd44786 169 }
vsluiter 16:bfc7ea6bd1af 170 case XBUS_MID: {
vsluiter 13:5e4dcbd44786 171 xbus->rx.counter = 2;
vsluiter 13:5e4dcbd44786 172 xbus->rx.buffer[xbus->rx.counter] = rxdata;
vsluiter 13:5e4dcbd44786 173 xbus->rx.checksum = xbus->rx.checksum + rxdata;
vsluiter 13:5e4dcbd44786 174 xbus->rx.state = XBUS_LEN;
vsluiter 13:5e4dcbd44786 175 break;
vsluiter 13:5e4dcbd44786 176 }
vsluiter 16:bfc7ea6bd1af 177 case XBUS_LEN: {
vsluiter 13:5e4dcbd44786 178 if(rxdata > XBUS_BUFFER_LENGTH - 4) // if message longer than buffer can contain
vsluiter 13:5e4dcbd44786 179 xbus->rx.state = XBUS_IDLE; // EXLEN not supported!
vsluiter 16:bfc7ea6bd1af 180 else {
vsluiter 13:5e4dcbd44786 181 xbus->rx.counter = 3;
vsluiter 13:5e4dcbd44786 182 xbus->rx.buffer[xbus->rx.counter] = rxdata;
vsluiter 13:5e4dcbd44786 183 xbus->rx.checksum = xbus->rx.checksum + rxdata;
vsluiter 13:5e4dcbd44786 184 if(rxdata == 0) // no data sent
vsluiter 13:5e4dcbd44786 185 xbus->rx.state = XBUS_CS; // go to checksum
vsluiter 13:5e4dcbd44786 186 else
vsluiter 13:5e4dcbd44786 187 xbus->rx.state = XBUS_DATA;
vsluiter 13:5e4dcbd44786 188 }
vsluiter 13:5e4dcbd44786 189 break;
vsluiter 13:5e4dcbd44786 190 }
vsluiter 16:bfc7ea6bd1af 191 case XBUS_DATA: {
vsluiter 13:5e4dcbd44786 192 xbus->rx.checksum += rxdata;
vsluiter 13:5e4dcbd44786 193 xbus->rx.counter++;
vsluiter 13:5e4dcbd44786 194 xbus->rx.buffer[xbus->rx.counter] = rxdata;
vsluiter 13:5e4dcbd44786 195 if(xbus->rx.counter == (xbus->rx.buffer[3] + 3) ) // if all data received (calculated from LEN
vsluiter 13:5e4dcbd44786 196 xbus->rx.state = XBUS_CS; // go to checksum
vsluiter 13:5e4dcbd44786 197 break;
vsluiter 13:5e4dcbd44786 198 }
vsluiter 16:bfc7ea6bd1af 199 case XBUS_CS: {
vsluiter 16:bfc7ea6bd1af 200 volatile uint16_t cs_calc;
vsluiter 13:5e4dcbd44786 201 xbus->rx.checksum_ok = 0;
vsluiter 13:5e4dcbd44786 202 xbus->rx.message_complete = 1;
vsluiter 13:5e4dcbd44786 203 xbus->rx.counter++;
vsluiter 13:5e4dcbd44786 204 xbus->rx.buffer[xbus->rx.counter] = rxdata;
vsluiter 13:5e4dcbd44786 205 //xbus->rx.checksum = 255 - xbus->rx.checksum;
vsluiter 16:bfc7ea6bd1af 206 xbus->rx.state = XBUS_IDLE;
vsluiter 13:5e4dcbd44786 207 cs_calc = xbus->rx.checksum + rxdata;
vsluiter 16:bfc7ea6bd1af 208 if( (cs_calc & 0x00FF) == 0)
vsluiter 13:5e4dcbd44786 209 xbus->rx.checksum_ok = 1;
vsluiter 16:bfc7ea6bd1af 210 else
vsluiter 16:bfc7ea6bd1af 211 asm("nop");
vsluiter 13:5e4dcbd44786 212 break;
vsluiter 13:5e4dcbd44786 213 }
vsluiter 16:bfc7ea6bd1af 214 default: {
vsluiter 13:5e4dcbd44786 215 xbus->rx.state = XBUS_IDLE;
vsluiter 13:5e4dcbd44786 216 break;
vsluiter 13:5e4dcbd44786 217 }
vsluiter 13:5e4dcbd44786 218 }
vsluiter 13:5e4dcbd44786 219 }
vsluiter 13:5e4dcbd44786 220
vsluiter 13:5e4dcbd44786 221
vsluiter 13:5e4dcbd44786 222 void XbusSetupReceiver(struct xbus_struct * xbus)
vsluiter 13:5e4dcbd44786 223 {
vsluiter 16:bfc7ea6bd1af 224 //xbus_pc.uart = &XBUS_UART_PC;
vsluiter 13:5e4dcbd44786 225 xbus->rx.message_complete = 0;
vsluiter 13:5e4dcbd44786 226 xbus->rx.state = XBUS_IDLE;
vsluiter 13:5e4dcbd44786 227 xbus->rx.counter = 0;
vsluiter 13:5e4dcbd44786 228 }
vsluiter 13:5e4dcbd44786 229
vsluiter 17:c5946a0fde83 230 void XbusCreateMessage(uint8_t bid, uint8_t mid, uint8_t message_size, uint8_t *message, uint8_t *buffer )
vsluiter 17:c5946a0fde83 231 {
vsluiter 17:c5946a0fde83 232 buffer[0] = 0xFA;
vsluiter 17:c5946a0fde83 233 buffer[1] = bid;
vsluiter 17:c5946a0fde83 234 buffer[2] = mid;
vsluiter 17:c5946a0fde83 235 buffer[3] = message_size;
vsluiter 17:c5946a0fde83 236 if(message_size > 0)
vsluiter 17:c5946a0fde83 237 {
vsluiter 17:c5946a0fde83 238 for( int i = 0; i< message_size ; i++)
vsluiter 17:c5946a0fde83 239 {
vsluiter 17:c5946a0fde83 240 buffer[i+4] = message[i];
vsluiter 17:c5946a0fde83 241 }
vsluiter 17:c5946a0fde83 242 }
vsluiter 17:c5946a0fde83 243 buffer[message_size + 4] = XbusCreateChecksum(buffer, message_size + 5);
vsluiter 17:c5946a0fde83 244
vsluiter 17:c5946a0fde83 245 }
vsluiter 17:c5946a0fde83 246
vsluiter 13:5e4dcbd44786 247 /*Calculate xbus checksum from received message*/
vsluiter 13:5e4dcbd44786 248 uint8_t XbusCreateChecksum(uint8_t * array, uint8_t arraysize)
vsluiter 13:5e4dcbd44786 249 {
vsluiter 16:bfc7ea6bd1af 250 uint8_t counter;
vsluiter 16:bfc7ea6bd1af 251 uint16_t temp =0;
vsluiter 16:bfc7ea6bd1af 252 uint8_t checksum;
vsluiter 16:bfc7ea6bd1af 253 for(counter = 1; counter < (arraysize-1) ; counter++) { //start at BID, end before checksum
vsluiter 16:bfc7ea6bd1af 254 temp += array[counter];
vsluiter 16:bfc7ea6bd1af 255 }
vsluiter 16:bfc7ea6bd1af 256 checksum = (uint8_t)(0x100 - temp);
vsluiter 16:bfc7ea6bd1af 257 //checksum++;
vsluiter 16:bfc7ea6bd1af 258 return checksum;
vsluiter 13:5e4dcbd44786 259 }