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

Dependencies:   KL25Z_ClockControl MODSERIAL mbed

Fork of EtherCAT-XbusMaster 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 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 1:5e22bf1a3817 63 DigitalOut led(LED_PIN);
vsluiter 1:5e22bf1a3817 64 DigitalOut et1100_ss(ET1100_SS);
vsluiter 1:5e22bf1a3817 65 DigitalIn et1100_miso(ET1100_MISO);
vsluiter 1:5e22bf1a3817 66 SPI et1100_spi(ET1100_MOSI,ET1100_MISO,ET1100_SCK);
vsluiter 17:c5946a0fde83 67 MODSERIAL xbus(PTC4,PTC3);
vsluiter 16:bfc7ea6bd1af 68 struct xbus_struct xbus_master;
vsluiter 16:bfc7ea6bd1af 69
vsluiter 1:5e22bf1a3817 70 /* Private function prototypes -----------------------------------------------*/
vsluiter 1:5e22bf1a3817 71 /* Private functions ---------------------------------------------------------*/
vsluiter 1:5e22bf1a3817 72
vsluiter 1:5e22bf1a3817 73 /** void ESC_objecthandler(uint16 index, uint8 subindex)
vsluiter 1:5e22bf1a3817 74 \brief Object handler, declared from esc.h, as extern function
vsluiter 1:5e22bf1a3817 75 \param index
vsluiter 1:5e22bf1a3817 76 \param subindex
vsluiter 1:5e22bf1a3817 77 */
vsluiter 1:5e22bf1a3817 78 void ESC_objecthandler(uint16 index, uint8 subindex);
vsluiter 1:5e22bf1a3817 79 void TXPDO_update(void);
vsluiter 1:5e22bf1a3817 80 void RXPDO_update(void);
vsluiter 1:5e22bf1a3817 81 void DIG_process(void);
vsluiter 5:6d75f432a41f 82 void sample(void);
vsluiter 1:5e22bf1a3817 83
vsluiter 1:5e22bf1a3817 84 void ESC_objecthandler(uint16 index, uint8 subindex)
vsluiter 1:5e22bf1a3817 85 {
vsluiter 1:5e22bf1a3817 86 uint8 dummy8;
vsluiter 1:5e22bf1a3817 87 uint16 dummy16;
vsluiter 1:5e22bf1a3817 88 switch (index)
vsluiter 1:5e22bf1a3817 89 {
vsluiter 1:5e22bf1a3817 90 case 0x8000:
vsluiter 1:5e22bf1a3817 91 switch (subindex)
vsluiter 1:5e22bf1a3817 92 {
vsluiter 1:5e22bf1a3817 93 case 0x01:
vsluiter 5:6d75f432a41f 94 dummy8 = 0;//Eb.setting8;//Write value to EEPROM; eeprom_write_byte(&eedat.setting8, Wb.setting8);
vsluiter 1:5e22bf1a3817 95 break;
vsluiter 1:5e22bf1a3817 96 case 0x02:
vsluiter 5:6d75f432a41f 97 dummy16 = 0;//Eb.setting16;//Write value to EEPROM; eeprom_write_word(&eedat.setting16, Wb.setting16);
vsluiter 1:5e22bf1a3817 98 break;
vsluiter 1:5e22bf1a3817 99 }
vsluiter 1:5e22bf1a3817 100 break;
vsluiter 1:5e22bf1a3817 101 }
vsluiter 1:5e22bf1a3817 102 }
vsluiter 1:5e22bf1a3817 103
vsluiter 1:5e22bf1a3817 104 void TXPDO_update(void)
vsluiter 1:5e22bf1a3817 105 {
vsluiter 1:5e22bf1a3817 106 ESC_write(SM3_sma, &Rb, TXPDOsize, &ESCvar.ALevent);
vsluiter 1:5e22bf1a3817 107 }
vsluiter 1:5e22bf1a3817 108
vsluiter 1:5e22bf1a3817 109 void RXPDO_update(void)
vsluiter 1:5e22bf1a3817 110 {
vsluiter 1:5e22bf1a3817 111 ESC_read(SM2_sma, &Wb, RXPDOsize, &ESCvar.ALevent);
vsluiter 1:5e22bf1a3817 112 }
vsluiter 1:5e22bf1a3817 113
vsluiter 1:5e22bf1a3817 114 void APP_safeoutput(void)
vsluiter 1:5e22bf1a3817 115 {
vsluiter 1:5e22bf1a3817 116 asm("nop");
vsluiter 1:5e22bf1a3817 117 //Wb.dout = 0;
vsluiter 1:5e22bf1a3817 118 //DOUTPORT = (Wb.dout >> 4) & 0xf0;
vsluiter 1:5e22bf1a3817 119 }
vsluiter 1:5e22bf1a3817 120
vsluiter 1:5e22bf1a3817 121 void DIG_process(void)
vsluiter 1:5e22bf1a3817 122 {
vsluiter 16:bfc7ea6bd1af 123 uint8_t correct_offset;
vsluiter 1:5e22bf1a3817 124 if (APPstate & APPSTATE_OUTPUT) //output enabled
vsluiter 1:5e22bf1a3817 125 {
vsluiter 1:5e22bf1a3817 126 if (ESCvar.ALevent & ESCREG_ALEVENT_SM2) // SM2 trigger ?
vsluiter 1:5e22bf1a3817 127 {
vsluiter 1:5e22bf1a3817 128 ESCvar.ALevent &= ~ESCREG_ALEVENT_SM2;
vsluiter 8:09dcef3ed467 129 RXPDO_update();
vsluiter 1:5e22bf1a3817 130 // dummy output point
vsluiter 5:6d75f432a41f 131 correct_offset = Wb.correct_offset;
vsluiter 5:6d75f432a41f 132 if(correct_offset & 0x01) {
vsluiter 8:09dcef3ed467 133 // led.write(1);
vsluiter 14:e3c49b75fee9 134 asm("nop");
vsluiter 8:09dcef3ed467 135 }
vsluiter 8:09dcef3ed467 136 // else
vsluiter 8:09dcef3ed467 137 // led.write(0);
vsluiter 1:5e22bf1a3817 138 wd_cnt = wd_reset;
vsluiter 1:5e22bf1a3817 139 }
vsluiter 1:5e22bf1a3817 140
vsluiter 5:6d75f432a41f 141 if (!wd_cnt) {
vsluiter 1:5e22bf1a3817 142 ESC_stopoutput();
vsluiter 1:5e22bf1a3817 143 // watchdog, invalid outputs
vsluiter 1:5e22bf1a3817 144 ESC_ALerror(ALERR_WATCHDOG);
vsluiter 1:5e22bf1a3817 145 // goto safe-op with error bit set
vsluiter 1:5e22bf1a3817 146 ESC_ALstatus(ESCsafeop | ESCerror);
vsluiter 1:5e22bf1a3817 147 }
vsluiter 1:5e22bf1a3817 148 }
vsluiter 1:5e22bf1a3817 149 else
vsluiter 1:5e22bf1a3817 150 {
vsluiter 1:5e22bf1a3817 151 //wd_ok = 1;
vsluiter 1:5e22bf1a3817 152 wd_cnt = wd_reset;
vsluiter 1:5e22bf1a3817 153 }
vsluiter 1:5e22bf1a3817 154 if (APPstate) //input or output enabled
vsluiter 5:6d75f432a41f 155 {
vsluiter 1:5e22bf1a3817 156 Rb.timestamp = ESCvar.Time;
vsluiter 1:5e22bf1a3817 157 //just some dummy data to test
vsluiter 4:bb72df6dce33 158 //Rb.counter++;
vsluiter 4:bb72df6dce33 159 //Rb.diginput = diginput;
vsluiter 4:bb72df6dce33 160 //Rb.analog[0] = 1;
vsluiter 4:bb72df6dce33 161 //Rb.analog[1] = 2;
vsluiter 14:e3c49b75fee9 162
vsluiter 1:5e22bf1a3817 163
vsluiter 1:5e22bf1a3817 164 TXPDO_update();
vsluiter 5:6d75f432a41f 165 }
vsluiter 1:5e22bf1a3817 166 }
vsluiter 1:5e22bf1a3817 167
vsluiter 17:c5946a0fde83 168
vsluiter 17:c5946a0fde83 169 void rxCallback(MODSERIAL_IRQ_INFO *info) {
vsluiter 17:c5946a0fde83 170
vsluiter 17:c5946a0fde83 171 // Get the pointer to our MODSERIAL object that invoked this callback.
vsluiter 17:c5946a0fde83 172 MODSERIAL *pc = info->serial;
vsluiter 17:c5946a0fde83 173
vsluiter 17:c5946a0fde83 174 // info->serial points at the MODSERIAL instance so we can use it to call
vsluiter 17:c5946a0fde83 175 // any of the public MODSERIAL functions that are normally available. So
vsluiter 17:c5946a0fde83 176 // there's now no need to use the global version (pc in our case) inside
vsluiter 17:c5946a0fde83 177 // callback functions.
vsluiter 17:c5946a0fde83 178 char c = pc->rxGetLastChar(); // Where local pc variable is a pointer to the global MODSERIAL pc object.
vsluiter 17:c5946a0fde83 179 // The following is rather daft but demos the point.
vsluiter 17:c5946a0fde83 180 // Don't allow the letter "A" go into the RX buffer.
vsluiter 17:c5946a0fde83 181 // Basically acts as a filter to remove the letter "A"
vsluiter 17:c5946a0fde83 182 // if it goes into the RX buffer.
vsluiter 17:c5946a0fde83 183 XbusReceiveState(&xbus_master,c);
vsluiter 17:c5946a0fde83 184 // Note, we call the MODSERIAL_IRQ_INFO::rxDiscardLastChar() public function which
vsluiter 17:c5946a0fde83 185 // is permitted access to the protected version of MODSERIAL::rxDiscardLastChar()
vsluiter 17:c5946a0fde83 186 // within MODSERIAL (because they are friends). This ensures rxDiscardLastChar()
vsluiter 17:c5946a0fde83 187 // can only be called within an rxCallback function.
vsluiter 17:c5946a0fde83 188 info->rxDiscardLastChar();
vsluiter 17:c5946a0fde83 189 if(xbus_master.rx.message_complete && xbus_master.rx.checksum_ok)
vsluiter 17:c5946a0fde83 190 {
vsluiter 17:c5946a0fde83 191 xbus_master.rx.checksum_ok = 0;
vsluiter 17:c5946a0fde83 192 if(xbus_master.rx.buffer[2] == 0x32)
vsluiter 17:c5946a0fde83 193 {
vsluiter 17:c5946a0fde83 194 memcpy(&Rb.first.acc[0],&buffer[4],60);
vsluiter 17:c5946a0fde83 195 }
vsluiter 17:c5946a0fde83 196 }
vsluiter 17:c5946a0fde83 197 }
vsluiter 17:c5946a0fde83 198
vsluiter 1:5e22bf1a3817 199 int main(void)
vsluiter 1:5e22bf1a3817 200 {
vsluiter 1:5e22bf1a3817 201 /*!< At this stage the microcontroller clock setting is already configured,
vsluiter 1:5e22bf1a3817 202 this is done through SystemInit() function which is called from startup
vsluiter 1:5e22bf1a3817 203 file (startup_stm32f0xx.s) before to branch to application main.
vsluiter 1:5e22bf1a3817 204 To reconfigure the default setting of SystemInit() function, refer to
vsluiter 1:5e22bf1a3817 205 system_stm32f0xx.c file
vsluiter 1:5e22bf1a3817 206 */
vsluiter 1:5e22bf1a3817 207 cpuinit();
vsluiter 1:5e22bf1a3817 208 TXPDOsize = sizeTXPDO();
vsluiter 1:5e22bf1a3817 209 RXPDOsize = sizeRXPDO();
vsluiter 1:5e22bf1a3817 210 wait_ms(200);
vsluiter 1:5e22bf1a3817 211 /*initialize configuration*/
vsluiter 5:6d75f432a41f 212 //Eb.setting16 = 0xABCD;
vsluiter 5:6d75f432a41f 213 //Eb.setting8 = 111;
vsluiter 1:5e22bf1a3817 214 // wait until ESC is started up
vsluiter 1:5e22bf1a3817 215 while ((ESCvar.DLstatus & 0x0001) == 0)
vsluiter 1:5e22bf1a3817 216 ESC_read(ESCREG_DLSTATUS, &ESCvar.DLstatus, sizeof(ESCvar.DLstatus), &ESCvar.ALevent);
vsluiter 1:5e22bf1a3817 217
vsluiter 1:5e22bf1a3817 218 // reset ESC to init state
vsluiter 1:5e22bf1a3817 219 ESC_ALstatus(ESCinit);
vsluiter 1:5e22bf1a3817 220 ESC_ALerror(ALERR_NONE);
vsluiter 1:5e22bf1a3817 221 ESC_stopmbx();
vsluiter 1:5e22bf1a3817 222 ESC_stopinput();
vsluiter 1:5e22bf1a3817 223 ESC_stopoutput();
vsluiter 1:5e22bf1a3817 224
vsluiter 1:5e22bf1a3817 225 // application run loop
vsluiter 1:5e22bf1a3817 226 while (1)
vsluiter 1:5e22bf1a3817 227 {
vsluiter 1:5e22bf1a3817 228 ESC_read(ESCREG_LOCALTIME, &ESCvar.Time, sizeof(ESCvar.Time), &ESCvar.ALevent);
vsluiter 1:5e22bf1a3817 229 ESC_ALevent();
vsluiter 1:5e22bf1a3817 230 ESC_state();
vsluiter 1:5e22bf1a3817 231 if (ESC_mbxprocess())
vsluiter 1:5e22bf1a3817 232 {
vsluiter 1:5e22bf1a3817 233 ESC_coeprocess();
vsluiter 1:5e22bf1a3817 234 ESC_xoeprocess();
vsluiter 1:5e22bf1a3817 235 }
vsluiter 1:5e22bf1a3817 236 DIG_process();
vsluiter 1:5e22bf1a3817 237 }
vsluiter 1:5e22bf1a3817 238 }
vsluiter 1:5e22bf1a3817 239
vsluiter 1:5e22bf1a3817 240 /** void ESC_objecthandler(uint16 index, uint8 subindex)
vsluiter 1:5e22bf1a3817 241 \brief Object handler, declared from esc.h, as extern function
vsluiter 1:5e22bf1a3817 242 \param index
vsluiter 1:5e22bf1a3817 243 \param subindex
vsluiter 1:5e22bf1a3817 244 */
vsluiter 1:5e22bf1a3817 245
vsluiter 1:5e22bf1a3817 246
vsluiter 1:5e22bf1a3817 247 #ifdef USE_FULL_ASSERT
vsluiter 1:5e22bf1a3817 248
vsluiter 1:5e22bf1a3817 249 /**
vsluiter 1:5e22bf1a3817 250 * @brief Reports the name of the source file and the source line number
vsluiter 1:5e22bf1a3817 251 * where the assert_param error has occurred.
vsluiter 1:5e22bf1a3817 252 * @param file: pointer to the source file name
vsluiter 1:5e22bf1a3817 253 * @param line: assert_param error line source number
vsluiter 1:5e22bf1a3817 254 * @retval None
vsluiter 1:5e22bf1a3817 255 */
vsluiter 1:5e22bf1a3817 256 void assert_failed(uint8_t* file, uint32_t line)
vsluiter 1:5e22bf1a3817 257 {
vsluiter 1:5e22bf1a3817 258 /* User can add his own implementation to report the file name and line number,
vsluiter 1:5e22bf1a3817 259 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
vsluiter 1:5e22bf1a3817 260
vsluiter 1:5e22bf1a3817 261 /* Infinite loop */
vsluiter 1:5e22bf1a3817 262 while (1)
vsluiter 1:5e22bf1a3817 263 {
vsluiter 1:5e22bf1a3817 264 }
vsluiter 1:5e22bf1a3817 265 }
vsluiter 1:5e22bf1a3817 266 #endif
vsluiter 1:5e22bf1a3817 267
vsluiter 1:5e22bf1a3817 268 /**
vsluiter 1:5e22bf1a3817 269 * @}
vsluiter 1:5e22bf1a3817 270 */
vsluiter 1:5e22bf1a3817 271
vsluiter 1:5e22bf1a3817 272 /**
vsluiter 1:5e22bf1a3817 273 * @}
vsluiter 1:5e22bf1a3817 274 */
vsluiter 1:5e22bf1a3817 275
vsluiter 1:5e22bf1a3817 276 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
vsluiter 1:5e22bf1a3817 277