SOES implementation i.c.w. ET1100 (Beckhoff ASIC)

Dependencies:   KL25Z_ClockControl MODSERIAL mbed

Fork of EtherCAT-XbusMaster by First Last

Committer:
vsluiter
Date:
Tue Mar 10 09:43:41 2015 +0000
Revision:
28:8505285f65ed
Parent:
27:93c0e4ae943e
Child:
29:95ef6b54ec8e
Child:
32:1f6a705cd794
Changed to other serial port; Only UART0 runs of PLL Clock!

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vsluiter 1:5e22bf1a3817 1 /*
vsluiter 1:5e22bf1a3817 2 * SOES Simple Open EtherCAT Slave
vsluiter 1:5e22bf1a3817 3 *
vsluiter 1:5e22bf1a3817 4 * File : soes.c
vsluiter 1:5e22bf1a3817 5 * Version : 0.9.2
vsluiter 1:5e22bf1a3817 6 * Date : 22-02-2010
vsluiter 1:5e22bf1a3817 7 * Copyright (C) 2007-2010 Arthur Ketels
vsluiter 1:5e22bf1a3817 8 *
vsluiter 1:5e22bf1a3817 9 * SOES is free software; you can redistribute it and/or modify it under
vsluiter 1:5e22bf1a3817 10 * the terms of the GNU General Public License version 2 as published by the Free
vsluiter 1:5e22bf1a3817 11 * Software Foundation.
vsluiter 1:5e22bf1a3817 12 *
vsluiter 1:5e22bf1a3817 13 * SOES is distributed in the hope that it will be useful, but WITHOUT ANY
vsluiter 1:5e22bf1a3817 14 * WARRANTY; without even the implied warranty of MERCHANTABILITY or
vsluiter 1:5e22bf1a3817 15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
vsluiter 1:5e22bf1a3817 16 * for more details.
vsluiter 1:5e22bf1a3817 17 *
vsluiter 1:5e22bf1a3817 18 * As a special exception, if other files instantiate templates or use macros
vsluiter 1:5e22bf1a3817 19 * or inline functions from this file, or you compile this file and link it
vsluiter 1:5e22bf1a3817 20 * with other works to produce a work based on this file, this file does not
vsluiter 1:5e22bf1a3817 21 * by itself cause the resulting work to be covered by the GNU General Public
vsluiter 1:5e22bf1a3817 22 * License. However the source code for this file must still be made available
vsluiter 1:5e22bf1a3817 23 * in accordance with section (3) of the GNU General Public License.
vsluiter 1:5e22bf1a3817 24 *
vsluiter 1:5e22bf1a3817 25 * This exception does not invalidate any other reasons why a work based on
vsluiter 1:5e22bf1a3817 26 * this file might be covered by the GNU General Public License.
vsluiter 1:5e22bf1a3817 27 *
vsluiter 1:5e22bf1a3817 28 * The EtherCAT Technology, the trade name and logo "EtherCAT" are the intellectual
vsluiter 1:5e22bf1a3817 29 * property of, and protected by Beckhoff Automation GmbH.
vsluiter 1:5e22bf1a3817 30 */
vsluiter 1:5e22bf1a3817 31
vsluiter 1:5e22bf1a3817 32 /****************************************************
vsluiter 1:5e22bf1a3817 33 Chip type : STM32F051R8
vsluiter 1:5e22bf1a3817 34 Clock frequency : 48 MHz
vsluiter 1:5e22bf1a3817 35 *****************************************************/
vsluiter 1:5e22bf1a3817 36
vsluiter 1:5e22bf1a3817 37 /* Includes ------------------------------------------------------------------*/
vsluiter 1:5e22bf1a3817 38 #include "mbed.h"
vsluiter 1:5e22bf1a3817 39 #include "cpuinit.h"
vsluiter 1:5e22bf1a3817 40 #include "utypes.h"
vsluiter 1:5e22bf1a3817 41 #include "esc.h"
vsluiter 12:cac4e7c2bd0f 42 #include "MODSERIAL.h"
vsluiter 17:c5946a0fde83 43 #include "xbus.h"
vsluiter 1:5e22bf1a3817 44
vsluiter 1:5e22bf1a3817 45 /* Private typedef -----------------------------------------------------------*/
vsluiter 1:5e22bf1a3817 46 /* Private define ------------------------------------------------------------*/
vsluiter 1:5e22bf1a3817 47 #define wd_reset 1000
vsluiter 1:5e22bf1a3817 48
vsluiter 1:5e22bf1a3817 49 /* Private macro -------------------------------------------------------------*/
vsluiter 1:5e22bf1a3817 50 /* Private variables ---------------------------------------------------------*/
vsluiter 1:5e22bf1a3817 51 _ESCvar ESCvar;
vsluiter 1:5e22bf1a3817 52 uint8 APPstate;
vsluiter 1:5e22bf1a3817 53 _MBX MBX[MBXBUFFERS];
vsluiter 1:5e22bf1a3817 54 _MBXcontrol MBXcontrol[MBXBUFFERS];
vsluiter 1:5e22bf1a3817 55 uint8 MBXrun=0;
vsluiter 1:5e22bf1a3817 56 uint16 SM2_sml,SM3_sml;
vsluiter 1:5e22bf1a3817 57 _Rbuffer Rb;
vsluiter 1:5e22bf1a3817 58 _Wbuffer Wb;
vsluiter 11:166353137b95 59 _Ebuffer Eb; //EEprom
vsluiter 1:5e22bf1a3817 60 uint8 TXPDOsize,RXPDOsize;
vsluiter 1:5e22bf1a3817 61 uint16 wd_ok = 1, wd_cnt = wd_reset;
vsluiter 1:5e22bf1a3817 62
vsluiter 25:829af6f3429f 63 _Rbuffer local_Rb; //to prevent issues when updating
vsluiter 1:5e22bf1a3817 64 DigitalOut led(LED_PIN);
vsluiter 1:5e22bf1a3817 65 DigitalOut et1100_ss(ET1100_SS);
vsluiter 1:5e22bf1a3817 66 DigitalIn et1100_miso(ET1100_MISO);
vsluiter 1:5e22bf1a3817 67 SPI et1100_spi(ET1100_MOSI,ET1100_MISO,ET1100_SCK);
vsluiter 28:8505285f65ed 68 MODSERIAL xbus_serial(PTA2,PTA1, 512);
vsluiter 18:6629e8c5d59e 69 xbus_t xbus_master;
vsluiter 28:8505285f65ed 70 //MODSERIAL pc(USBTX,USBRX,512);
vsluiter 16:bfc7ea6bd1af 71
vsluiter 22:3f3a9f55054c 72 DigitalOut ploep(PTA13);
vsluiter 22:3f3a9f55054c 73 #define PLOEP do{ploep = !(ploep);}while(0);
vsluiter 19:8ead12aec8d5 74
vsluiter 1:5e22bf1a3817 75 /* Private function prototypes -----------------------------------------------*/
vsluiter 1:5e22bf1a3817 76 /* Private functions ---------------------------------------------------------*/
vsluiter 1:5e22bf1a3817 77
vsluiter 1:5e22bf1a3817 78 /** void ESC_objecthandler(uint16 index, uint8 subindex)
vsluiter 1:5e22bf1a3817 79 \brief Object handler, declared from esc.h, as extern function
vsluiter 1:5e22bf1a3817 80 \param index
vsluiter 1:5e22bf1a3817 81 \param subindex
vsluiter 1:5e22bf1a3817 82 */
vsluiter 1:5e22bf1a3817 83 void ESC_objecthandler(uint16 index, uint8 subindex);
vsluiter 1:5e22bf1a3817 84 void TXPDO_update(void);
vsluiter 1:5e22bf1a3817 85 void RXPDO_update(void);
vsluiter 1:5e22bf1a3817 86 void DIG_process(void);
vsluiter 5:6d75f432a41f 87 void sample(void);
vsluiter 1:5e22bf1a3817 88
vsluiter 1:5e22bf1a3817 89 void ESC_objecthandler(uint16 index, uint8 subindex)
vsluiter 1:5e22bf1a3817 90 {
vsluiter 1:5e22bf1a3817 91 uint8 dummy8;
vsluiter 1:5e22bf1a3817 92 uint16 dummy16;
vsluiter 1:5e22bf1a3817 93 switch (index)
vsluiter 1:5e22bf1a3817 94 {
vsluiter 1:5e22bf1a3817 95 case 0x8000:
vsluiter 1:5e22bf1a3817 96 switch (subindex)
vsluiter 1:5e22bf1a3817 97 {
vsluiter 1:5e22bf1a3817 98 case 0x01:
vsluiter 26:c7959f1fd09a 99 {
vsluiter 26:c7959f1fd09a 100 uint32_t rates[] = {460800,230400,115200,76800,57600,38400,28800,19200,14400,9600};
vsluiter 28:8505285f65ed 101 if(Eb.setting8 < (sizeof(rates)/sizeof(uint32_t)) )
vsluiter 27:93c0e4ae943e 102 {
vsluiter 27:93c0e4ae943e 103 XbusGoToConfig();
vsluiter 26:c7959f1fd09a 104 XbusSetBaudRate(Eb.setting8);
vsluiter 26:c7959f1fd09a 105 XbusReset();
vsluiter 27:93c0e4ae943e 106 wait(2);
vsluiter 26:c7959f1fd09a 107 xbus_serial.baud(rates[Eb.setting8]);
vsluiter 26:c7959f1fd09a 108 XbusInitializeXbusMaster();
vsluiter 26:c7959f1fd09a 109
vsluiter 26:c7959f1fd09a 110 }
vsluiter 28:8505285f65ed 111 else if(Eb.setting8 == 0x80)
vsluiter 28:8505285f65ed 112 {
vsluiter 28:8505285f65ed 113 XbusGoToConfig();
vsluiter 28:8505285f65ed 114 XbusSetBaudRate(0x80);
vsluiter 28:8505285f65ed 115 XbusReset();
vsluiter 28:8505285f65ed 116 wait(2);
vsluiter 28:8505285f65ed 117 xbus_serial.baud(960800);
vsluiter 28:8505285f65ed 118 XbusInitializeXbusMaster();
vsluiter 28:8505285f65ed 119
vsluiter 28:8505285f65ed 120 }
vsluiter 1:5e22bf1a3817 121 break;
vsluiter 26:c7959f1fd09a 122 }
vsluiter 1:5e22bf1a3817 123 case 0x02:
vsluiter 5:6d75f432a41f 124 dummy16 = 0;//Eb.setting16;//Write value to EEPROM; eeprom_write_word(&eedat.setting16, Wb.setting16);
vsluiter 1:5e22bf1a3817 125 break;
vsluiter 1:5e22bf1a3817 126 }
vsluiter 1:5e22bf1a3817 127 break;
vsluiter 1:5e22bf1a3817 128 }
vsluiter 1:5e22bf1a3817 129 }
vsluiter 1:5e22bf1a3817 130
vsluiter 1:5e22bf1a3817 131 void TXPDO_update(void)
vsluiter 1:5e22bf1a3817 132 {
vsluiter 1:5e22bf1a3817 133 ESC_write(SM3_sma, &Rb, TXPDOsize, &ESCvar.ALevent);
vsluiter 1:5e22bf1a3817 134 }
vsluiter 1:5e22bf1a3817 135
vsluiter 1:5e22bf1a3817 136 void RXPDO_update(void)
vsluiter 1:5e22bf1a3817 137 {
vsluiter 1:5e22bf1a3817 138 ESC_read(SM2_sma, &Wb, RXPDOsize, &ESCvar.ALevent);
vsluiter 1:5e22bf1a3817 139 }
vsluiter 1:5e22bf1a3817 140
vsluiter 1:5e22bf1a3817 141 void APP_safeoutput(void)
vsluiter 1:5e22bf1a3817 142 {
vsluiter 1:5e22bf1a3817 143 asm("nop");
vsluiter 1:5e22bf1a3817 144 //Wb.dout = 0;
vsluiter 1:5e22bf1a3817 145 //DOUTPORT = (Wb.dout >> 4) & 0xf0;
vsluiter 1:5e22bf1a3817 146 }
vsluiter 1:5e22bf1a3817 147
vsluiter 1:5e22bf1a3817 148 void DIG_process(void)
vsluiter 1:5e22bf1a3817 149 {
vsluiter 16:bfc7ea6bd1af 150 uint8_t correct_offset;
vsluiter 1:5e22bf1a3817 151 if (APPstate & APPSTATE_OUTPUT) //output enabled
vsluiter 1:5e22bf1a3817 152 {
vsluiter 1:5e22bf1a3817 153 if (ESCvar.ALevent & ESCREG_ALEVENT_SM2) // SM2 trigger ?
vsluiter 1:5e22bf1a3817 154 {
vsluiter 1:5e22bf1a3817 155 ESCvar.ALevent &= ~ESCREG_ALEVENT_SM2;
vsluiter 8:09dcef3ed467 156 RXPDO_update();
vsluiter 1:5e22bf1a3817 157 // dummy output point
vsluiter 5:6d75f432a41f 158 correct_offset = Wb.correct_offset;
vsluiter 5:6d75f432a41f 159 if(correct_offset & 0x01) {
vsluiter 8:09dcef3ed467 160 // led.write(1);
vsluiter 14:e3c49b75fee9 161 asm("nop");
vsluiter 8:09dcef3ed467 162 }
vsluiter 8:09dcef3ed467 163 // else
vsluiter 8:09dcef3ed467 164 // led.write(0);
vsluiter 1:5e22bf1a3817 165 wd_cnt = wd_reset;
vsluiter 1:5e22bf1a3817 166 }
vsluiter 1:5e22bf1a3817 167
vsluiter 5:6d75f432a41f 168 if (!wd_cnt) {
vsluiter 1:5e22bf1a3817 169 ESC_stopoutput();
vsluiter 1:5e22bf1a3817 170 // watchdog, invalid outputs
vsluiter 1:5e22bf1a3817 171 ESC_ALerror(ALERR_WATCHDOG);
vsluiter 1:5e22bf1a3817 172 // goto safe-op with error bit set
vsluiter 1:5e22bf1a3817 173 ESC_ALstatus(ESCsafeop | ESCerror);
vsluiter 1:5e22bf1a3817 174 }
vsluiter 1:5e22bf1a3817 175 }
vsluiter 1:5e22bf1a3817 176 else
vsluiter 1:5e22bf1a3817 177 {
vsluiter 1:5e22bf1a3817 178 //wd_ok = 1;
vsluiter 1:5e22bf1a3817 179 wd_cnt = wd_reset;
vsluiter 1:5e22bf1a3817 180 }
vsluiter 1:5e22bf1a3817 181 if (APPstate) //input or output enabled
vsluiter 5:6d75f432a41f 182 {
vsluiter 25:829af6f3429f 183 //Rb.timestamp = ESCvar.Time;
vsluiter 1:5e22bf1a3817 184 //just some dummy data to test
vsluiter 4:bb72df6dce33 185 //Rb.counter++;
vsluiter 4:bb72df6dce33 186 //Rb.diginput = diginput;
vsluiter 4:bb72df6dce33 187 //Rb.analog[0] = 1;
vsluiter 4:bb72df6dce33 188 //Rb.analog[1] = 2;
vsluiter 14:e3c49b75fee9 189
vsluiter 25:829af6f3429f 190 memcpy(&Rb, &local_Rb, sizeof(Rb));
vsluiter 1:5e22bf1a3817 191 TXPDO_update();
vsluiter 5:6d75f432a41f 192 }
vsluiter 1:5e22bf1a3817 193 }
vsluiter 1:5e22bf1a3817 194
vsluiter 21:6150ca891301 195 //Watch out, this is an uggly fix; for odd numbers of num_bytes, still the trailing byte will be used too.
vsluiter 21:6150ca891301 196 //No memory protection what so ever.
vsluiter 21:6150ca891301 197 void memcpy_byteswap(uint8_t * dest, uint8_t * source, uint16_t num_bytes)
vsluiter 21:6150ca891301 198 {
vsluiter 21:6150ca891301 199 for( int i = 0 ; i < num_bytes ; i+=2 )
vsluiter 21:6150ca891301 200 {
vsluiter 21:6150ca891301 201 dest[i] = source[i+1];
vsluiter 21:6150ca891301 202 dest[i+1] = source[i];
vsluiter 21:6150ca891301 203 }
vsluiter 21:6150ca891301 204 }
vsluiter 17:c5946a0fde83 205
vsluiter 1:5e22bf1a3817 206 int main(void)
vsluiter 1:5e22bf1a3817 207 {
vsluiter 25:829af6f3429f 208
vsluiter 1:5e22bf1a3817 209 /*!< At this stage the microcontroller clock setting is already configured,
vsluiter 1:5e22bf1a3817 210 this is done through SystemInit() function which is called from startup
vsluiter 1:5e22bf1a3817 211 file (startup_stm32f0xx.s) before to branch to application main.
vsluiter 1:5e22bf1a3817 212 To reconfigure the default setting of SystemInit() function, refer to
vsluiter 1:5e22bf1a3817 213 system_stm32f0xx.c file
vsluiter 1:5e22bf1a3817 214 */
vsluiter 1:5e22bf1a3817 215 cpuinit();
vsluiter 1:5e22bf1a3817 216 TXPDOsize = sizeTXPDO();
vsluiter 1:5e22bf1a3817 217 RXPDOsize = sizeRXPDO();
vsluiter 1:5e22bf1a3817 218 wait_ms(200);
vsluiter 1:5e22bf1a3817 219 /*initialize configuration*/
vsluiter 5:6d75f432a41f 220 //Eb.setting16 = 0xABCD;
vsluiter 28:8505285f65ed 221 Eb.setting8 = 1;
vsluiter 1:5e22bf1a3817 222 // wait until ESC is started up
vsluiter 1:5e22bf1a3817 223 while ((ESCvar.DLstatus & 0x0001) == 0)
vsluiter 1:5e22bf1a3817 224 ESC_read(ESCREG_DLSTATUS, &ESCvar.DLstatus, sizeof(ESCvar.DLstatus), &ESCvar.ALevent);
vsluiter 1:5e22bf1a3817 225
vsluiter 1:5e22bf1a3817 226 // reset ESC to init state
vsluiter 1:5e22bf1a3817 227 ESC_ALstatus(ESCinit);
vsluiter 1:5e22bf1a3817 228 ESC_ALerror(ALERR_NONE);
vsluiter 1:5e22bf1a3817 229 ESC_stopmbx();
vsluiter 1:5e22bf1a3817 230 ESC_stopinput();
vsluiter 1:5e22bf1a3817 231 ESC_stopoutput();
vsluiter 25:829af6f3429f 232
vsluiter 25:829af6f3429f 233 // pc.baud(115200);
vsluiter 1:5e22bf1a3817 234 // application run loop
vsluiter 1:5e22bf1a3817 235 while (1)
vsluiter 1:5e22bf1a3817 236 {
vsluiter 20:ad13d4f5fd98 237 while(xbus_serial.readable())
vsluiter 20:ad13d4f5fd98 238 {
vsluiter 20:ad13d4f5fd98 239 XbusReceiveState(&xbus_master, xbus_serial.getc());
vsluiter 20:ad13d4f5fd98 240 if(xbus_master.rx.checksum_ok)
vsluiter 20:ad13d4f5fd98 241 {
vsluiter 20:ad13d4f5fd98 242 if(xbus_master.rx.buffer[2] == 0x32)
vsluiter 20:ad13d4f5fd98 243 {
vsluiter 25:829af6f3429f 244 memcpy_byteswap((uint8_t *)&local_Rb.timestamp ,&xbus_master.rx.buffer[4], 2);
vsluiter 25:829af6f3429f 245 memcpy_byteswap((uint8_t *)&local_Rb.first.acc[0],&xbus_master.rx.buffer[6],60);
vsluiter 25:829af6f3429f 246 //pc.printf("%x %x\n",Rb.timestamp, *((uint16_t *)(&xbus_master.rx.buffer[4])));
vsluiter 27:93c0e4ae943e 247 PLOEP;
vsluiter 20:ad13d4f5fd98 248 }
vsluiter 20:ad13d4f5fd98 249 xbus_master.rx.checksum_ok = 0;
vsluiter 20:ad13d4f5fd98 250 }
vsluiter 18:6629e8c5d59e 251 }
vsluiter 1:5e22bf1a3817 252 ESC_read(ESCREG_LOCALTIME, &ESCvar.Time, sizeof(ESCvar.Time), &ESCvar.ALevent);
vsluiter 1:5e22bf1a3817 253 ESC_ALevent();
vsluiter 1:5e22bf1a3817 254 ESC_state();
vsluiter 1:5e22bf1a3817 255 if (ESC_mbxprocess())
vsluiter 1:5e22bf1a3817 256 {
vsluiter 1:5e22bf1a3817 257 ESC_coeprocess();
vsluiter 1:5e22bf1a3817 258 ESC_xoeprocess();
vsluiter 1:5e22bf1a3817 259 }
vsluiter 1:5e22bf1a3817 260 DIG_process();
vsluiter 1:5e22bf1a3817 261 }
vsluiter 1:5e22bf1a3817 262 }
vsluiter 1:5e22bf1a3817 263
vsluiter 1:5e22bf1a3817 264 /** void ESC_objecthandler(uint16 index, uint8 subindex)
vsluiter 1:5e22bf1a3817 265 \brief Object handler, declared from esc.h, as extern function
vsluiter 1:5e22bf1a3817 266 \param index
vsluiter 1:5e22bf1a3817 267 \param subindex
vsluiter 1:5e22bf1a3817 268 */
vsluiter 1:5e22bf1a3817 269
vsluiter 1:5e22bf1a3817 270
vsluiter 1:5e22bf1a3817 271 #ifdef USE_FULL_ASSERT
vsluiter 1:5e22bf1a3817 272
vsluiter 1:5e22bf1a3817 273 /**
vsluiter 1:5e22bf1a3817 274 * @brief Reports the name of the source file and the source line number
vsluiter 1:5e22bf1a3817 275 * where the assert_param error has occurred.
vsluiter 1:5e22bf1a3817 276 * @param file: pointer to the source file name
vsluiter 1:5e22bf1a3817 277 * @param line: assert_param error line source number
vsluiter 1:5e22bf1a3817 278 * @retval None
vsluiter 1:5e22bf1a3817 279 */
vsluiter 1:5e22bf1a3817 280 void assert_failed(uint8_t* file, uint32_t line)
vsluiter 1:5e22bf1a3817 281 {
vsluiter 1:5e22bf1a3817 282 /* User can add his own implementation to report the file name and line number,
vsluiter 1:5e22bf1a3817 283 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
vsluiter 1:5e22bf1a3817 284
vsluiter 1:5e22bf1a3817 285 /* Infinite loop */
vsluiter 1:5e22bf1a3817 286 while (1)
vsluiter 1:5e22bf1a3817 287 {
vsluiter 1:5e22bf1a3817 288 }
vsluiter 1:5e22bf1a3817 289 }
vsluiter 1:5e22bf1a3817 290 #endif
vsluiter 1:5e22bf1a3817 291
vsluiter 1:5e22bf1a3817 292 /**
vsluiter 1:5e22bf1a3817 293 * @}
vsluiter 1:5e22bf1a3817 294 */
vsluiter 1:5e22bf1a3817 295
vsluiter 1:5e22bf1a3817 296 /**
vsluiter 1:5e22bf1a3817 297 * @}
vsluiter 1:5e22bf1a3817 298 */
vsluiter 1:5e22bf1a3817 299
vsluiter 1:5e22bf1a3817 300 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
vsluiter 1:5e22bf1a3817 301