![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
EtherCAT slave based on SOES
soes.cpp@1:5e22bf1a3817, 2014-12-11 (annotated)
- Committer:
- vsluiter
- Date:
- Thu Dec 11 21:16:00 2014 +0000
- Revision:
- 1:5e22bf1a3817
- Child:
- 2:3dd1240eb938
Working slave;
Who changed what in which revision?
User | Revision | Line number | New 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 | 1:5e22bf1a3817 | 42 | |
vsluiter | 1:5e22bf1a3817 | 43 | /* Private typedef -----------------------------------------------------------*/ |
vsluiter | 1:5e22bf1a3817 | 44 | /* Private define ------------------------------------------------------------*/ |
vsluiter | 1:5e22bf1a3817 | 45 | #define wd_reset 1000 |
vsluiter | 1:5e22bf1a3817 | 46 | |
vsluiter | 1:5e22bf1a3817 | 47 | /* Private macro -------------------------------------------------------------*/ |
vsluiter | 1:5e22bf1a3817 | 48 | /* Private variables ---------------------------------------------------------*/ |
vsluiter | 1:5e22bf1a3817 | 49 | _ESCvar ESCvar; |
vsluiter | 1:5e22bf1a3817 | 50 | uint8 APPstate; |
vsluiter | 1:5e22bf1a3817 | 51 | _MBX MBX[MBXBUFFERS]; |
vsluiter | 1:5e22bf1a3817 | 52 | _MBXcontrol MBXcontrol[MBXBUFFERS]; |
vsluiter | 1:5e22bf1a3817 | 53 | uint8 MBXrun=0; |
vsluiter | 1:5e22bf1a3817 | 54 | uint16 SM2_sml,SM3_sml; |
vsluiter | 1:5e22bf1a3817 | 55 | _Rbuffer Rb; |
vsluiter | 1:5e22bf1a3817 | 56 | _Wbuffer Wb; |
vsluiter | 1:5e22bf1a3817 | 57 | _Ebuffer Eb; //EEprom |
vsluiter | 1:5e22bf1a3817 | 58 | uint8 TXPDOsize,RXPDOsize; |
vsluiter | 1:5e22bf1a3817 | 59 | uint16 wd_ok = 1, wd_cnt = wd_reset; |
vsluiter | 1:5e22bf1a3817 | 60 | volatile uint8 digoutput; |
vsluiter | 1:5e22bf1a3817 | 61 | volatile uint8 diginput; |
vsluiter | 1:5e22bf1a3817 | 62 | |
vsluiter | 1:5e22bf1a3817 | 63 | Serial shoe_serial(SHOE_SERIAL_TX,SHOE_SERIAL_RX); |
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 | 1:5e22bf1a3817 | 68 | |
vsluiter | 1:5e22bf1a3817 | 69 | /* Private function prototypes -----------------------------------------------*/ |
vsluiter | 1:5e22bf1a3817 | 70 | /* Private functions ---------------------------------------------------------*/ |
vsluiter | 1:5e22bf1a3817 | 71 | |
vsluiter | 1:5e22bf1a3817 | 72 | /** void ESC_objecthandler(uint16 index, uint8 subindex) |
vsluiter | 1:5e22bf1a3817 | 73 | \brief Object handler, declared from esc.h, as extern function |
vsluiter | 1:5e22bf1a3817 | 74 | \param index |
vsluiter | 1:5e22bf1a3817 | 75 | \param subindex |
vsluiter | 1:5e22bf1a3817 | 76 | */ |
vsluiter | 1:5e22bf1a3817 | 77 | void ESC_objecthandler(uint16 index, uint8 subindex); |
vsluiter | 1:5e22bf1a3817 | 78 | void TXPDO_update(void); |
vsluiter | 1:5e22bf1a3817 | 79 | void RXPDO_update(void); |
vsluiter | 1:5e22bf1a3817 | 80 | void DIG_process(void); |
vsluiter | 1:5e22bf1a3817 | 81 | /* |
vsluiter | 1:5e22bf1a3817 | 82 | void _delay_ms(uint16 number_of_ms) |
vsluiter | 1:5e22bf1a3817 | 83 | { |
vsluiter | 1:5e22bf1a3817 | 84 | volatile uint16_t cpu_counter = 0; |
vsluiter | 1:5e22bf1a3817 | 85 | uint16_t ms_counter; |
vsluiter | 1:5e22bf1a3817 | 86 | for(ms_counter = 0 ; ms_counter < number_of_ms ; ms_counter++) |
vsluiter | 1:5e22bf1a3817 | 87 | { |
vsluiter | 1:5e22bf1a3817 | 88 | cpu_counter = 48000; //48MHz processor |
vsluiter | 1:5e22bf1a3817 | 89 | while(cpu_counter >0) |
vsluiter | 1:5e22bf1a3817 | 90 | cpu_counter--; |
vsluiter | 1:5e22bf1a3817 | 91 | } |
vsluiter | 1:5e22bf1a3817 | 92 | }*/ |
vsluiter | 1:5e22bf1a3817 | 93 | |
vsluiter | 1:5e22bf1a3817 | 94 | void ESC_objecthandler(uint16 index, uint8 subindex) |
vsluiter | 1:5e22bf1a3817 | 95 | { |
vsluiter | 1:5e22bf1a3817 | 96 | uint8 dummy8; |
vsluiter | 1:5e22bf1a3817 | 97 | uint16 dummy16; |
vsluiter | 1:5e22bf1a3817 | 98 | switch (index) |
vsluiter | 1:5e22bf1a3817 | 99 | { |
vsluiter | 1:5e22bf1a3817 | 100 | case 0x8000: |
vsluiter | 1:5e22bf1a3817 | 101 | switch (subindex) |
vsluiter | 1:5e22bf1a3817 | 102 | { |
vsluiter | 1:5e22bf1a3817 | 103 | case 0x01: |
vsluiter | 1:5e22bf1a3817 | 104 | dummy8 = Eb.setting8;//Write value to EEPROM; eeprom_write_byte(&eedat.setting8, Wb.setting8); |
vsluiter | 1:5e22bf1a3817 | 105 | break; |
vsluiter | 1:5e22bf1a3817 | 106 | case 0x02: |
vsluiter | 1:5e22bf1a3817 | 107 | dummy16 = Eb.setting16;//Write value to EEPROM; eeprom_write_word(&eedat.setting16, Wb.setting16); |
vsluiter | 1:5e22bf1a3817 | 108 | break; |
vsluiter | 1:5e22bf1a3817 | 109 | } |
vsluiter | 1:5e22bf1a3817 | 110 | break; |
vsluiter | 1:5e22bf1a3817 | 111 | } |
vsluiter | 1:5e22bf1a3817 | 112 | } |
vsluiter | 1:5e22bf1a3817 | 113 | |
vsluiter | 1:5e22bf1a3817 | 114 | void TXPDO_update(void) |
vsluiter | 1:5e22bf1a3817 | 115 | { |
vsluiter | 1:5e22bf1a3817 | 116 | ESC_write(SM3_sma, &Rb, TXPDOsize, &ESCvar.ALevent); |
vsluiter | 1:5e22bf1a3817 | 117 | } |
vsluiter | 1:5e22bf1a3817 | 118 | |
vsluiter | 1:5e22bf1a3817 | 119 | void RXPDO_update(void) |
vsluiter | 1:5e22bf1a3817 | 120 | { |
vsluiter | 1:5e22bf1a3817 | 121 | ESC_read(SM2_sma, &Wb, RXPDOsize, &ESCvar.ALevent); |
vsluiter | 1:5e22bf1a3817 | 122 | } |
vsluiter | 1:5e22bf1a3817 | 123 | |
vsluiter | 1:5e22bf1a3817 | 124 | void APP_safeoutput(void) |
vsluiter | 1:5e22bf1a3817 | 125 | { |
vsluiter | 1:5e22bf1a3817 | 126 | asm("nop"); |
vsluiter | 1:5e22bf1a3817 | 127 | //Wb.dout = 0; |
vsluiter | 1:5e22bf1a3817 | 128 | //DOUTPORT = (Wb.dout >> 4) & 0xf0; |
vsluiter | 1:5e22bf1a3817 | 129 | } |
vsluiter | 1:5e22bf1a3817 | 130 | |
vsluiter | 1:5e22bf1a3817 | 131 | void DIG_process(void) |
vsluiter | 1:5e22bf1a3817 | 132 | { |
vsluiter | 1:5e22bf1a3817 | 133 | if (APPstate & APPSTATE_OUTPUT) //output enabled |
vsluiter | 1:5e22bf1a3817 | 134 | { |
vsluiter | 1:5e22bf1a3817 | 135 | if (ESCvar.ALevent & ESCREG_ALEVENT_SM2) // SM2 trigger ? |
vsluiter | 1:5e22bf1a3817 | 136 | { |
vsluiter | 1:5e22bf1a3817 | 137 | ESCvar.ALevent &= ~ESCREG_ALEVENT_SM2; |
vsluiter | 1:5e22bf1a3817 | 138 | RXPDO_update(); |
vsluiter | 1:5e22bf1a3817 | 139 | // dummy output point |
vsluiter | 1:5e22bf1a3817 | 140 | digoutput = Wb.digoutput; |
vsluiter | 1:5e22bf1a3817 | 141 | if(digoutput & 0x01) |
vsluiter | 1:5e22bf1a3817 | 142 | led.write(1); |
vsluiter | 1:5e22bf1a3817 | 143 | else |
vsluiter | 1:5e22bf1a3817 | 144 | led.write(0); |
vsluiter | 1:5e22bf1a3817 | 145 | wd_cnt = wd_reset; |
vsluiter | 1:5e22bf1a3817 | 146 | } |
vsluiter | 1:5e22bf1a3817 | 147 | |
vsluiter | 1:5e22bf1a3817 | 148 | if (!wd_cnt) |
vsluiter | 1:5e22bf1a3817 | 149 | { |
vsluiter | 1:5e22bf1a3817 | 150 | ESC_stopoutput(); |
vsluiter | 1:5e22bf1a3817 | 151 | // watchdog, invalid outputs |
vsluiter | 1:5e22bf1a3817 | 152 | ESC_ALerror(ALERR_WATCHDOG); |
vsluiter | 1:5e22bf1a3817 | 153 | // goto safe-op with error bit set |
vsluiter | 1:5e22bf1a3817 | 154 | ESC_ALstatus(ESCsafeop | ESCerror); |
vsluiter | 1:5e22bf1a3817 | 155 | } |
vsluiter | 1:5e22bf1a3817 | 156 | } |
vsluiter | 1:5e22bf1a3817 | 157 | else |
vsluiter | 1:5e22bf1a3817 | 158 | { |
vsluiter | 1:5e22bf1a3817 | 159 | //wd_ok = 1; |
vsluiter | 1:5e22bf1a3817 | 160 | wd_cnt = wd_reset; |
vsluiter | 1:5e22bf1a3817 | 161 | } |
vsluiter | 1:5e22bf1a3817 | 162 | if (APPstate) //input or output enabled |
vsluiter | 1:5e22bf1a3817 | 163 | { |
vsluiter | 1:5e22bf1a3817 | 164 | Rb.timestamp = ESCvar.Time; |
vsluiter | 1:5e22bf1a3817 | 165 | //just some dummy data to test |
vsluiter | 1:5e22bf1a3817 | 166 | Rb.counter++; |
vsluiter | 1:5e22bf1a3817 | 167 | Rb.diginput = diginput; |
vsluiter | 1:5e22bf1a3817 | 168 | Rb.analog[0] = 1; |
vsluiter | 1:5e22bf1a3817 | 169 | Rb.analog[1] = 2; |
vsluiter | 1:5e22bf1a3817 | 170 | Rb.analog[2] = 3; |
vsluiter | 1:5e22bf1a3817 | 171 | Rb.analog[3] = 4; |
vsluiter | 1:5e22bf1a3817 | 172 | Rb.analog[4] = 5; |
vsluiter | 1:5e22bf1a3817 | 173 | Rb.analog[5] = 6; |
vsluiter | 1:5e22bf1a3817 | 174 | Rb.analog[6] = 7; |
vsluiter | 1:5e22bf1a3817 | 175 | Rb.analog[7] = 8; |
vsluiter | 1:5e22bf1a3817 | 176 | |
vsluiter | 1:5e22bf1a3817 | 177 | TXPDO_update(); |
vsluiter | 1:5e22bf1a3817 | 178 | } |
vsluiter | 1:5e22bf1a3817 | 179 | } |
vsluiter | 1:5e22bf1a3817 | 180 | |
vsluiter | 1:5e22bf1a3817 | 181 | |
vsluiter | 1:5e22bf1a3817 | 182 | int main(void) |
vsluiter | 1:5e22bf1a3817 | 183 | { |
vsluiter | 1:5e22bf1a3817 | 184 | /*!< At this stage the microcontroller clock setting is already configured, |
vsluiter | 1:5e22bf1a3817 | 185 | this is done through SystemInit() function which is called from startup |
vsluiter | 1:5e22bf1a3817 | 186 | file (startup_stm32f0xx.s) before to branch to application main. |
vsluiter | 1:5e22bf1a3817 | 187 | To reconfigure the default setting of SystemInit() function, refer to |
vsluiter | 1:5e22bf1a3817 | 188 | system_stm32f0xx.c file |
vsluiter | 1:5e22bf1a3817 | 189 | */ |
vsluiter | 1:5e22bf1a3817 | 190 | cpuinit(); |
vsluiter | 1:5e22bf1a3817 | 191 | TXPDOsize = sizeTXPDO(); |
vsluiter | 1:5e22bf1a3817 | 192 | RXPDOsize = sizeRXPDO(); |
vsluiter | 1:5e22bf1a3817 | 193 | wait_ms(200); |
vsluiter | 1:5e22bf1a3817 | 194 | /*initialize configuration*/ |
vsluiter | 1:5e22bf1a3817 | 195 | Eb.setting16 = 0xABCD; |
vsluiter | 1:5e22bf1a3817 | 196 | Eb.setting8 = 111; |
vsluiter | 1:5e22bf1a3817 | 197 | // wait until ESC is started up |
vsluiter | 1:5e22bf1a3817 | 198 | while ((ESCvar.DLstatus & 0x0001) == 0) |
vsluiter | 1:5e22bf1a3817 | 199 | ESC_read(ESCREG_DLSTATUS, &ESCvar.DLstatus, sizeof(ESCvar.DLstatus), &ESCvar.ALevent); |
vsluiter | 1:5e22bf1a3817 | 200 | |
vsluiter | 1:5e22bf1a3817 | 201 | // reset ESC to init state |
vsluiter | 1:5e22bf1a3817 | 202 | ESC_ALstatus(ESCinit); |
vsluiter | 1:5e22bf1a3817 | 203 | ESC_ALerror(ALERR_NONE); |
vsluiter | 1:5e22bf1a3817 | 204 | ESC_stopmbx(); |
vsluiter | 1:5e22bf1a3817 | 205 | ESC_stopinput(); |
vsluiter | 1:5e22bf1a3817 | 206 | ESC_stopoutput(); |
vsluiter | 1:5e22bf1a3817 | 207 | |
vsluiter | 1:5e22bf1a3817 | 208 | // application run loop |
vsluiter | 1:5e22bf1a3817 | 209 | while (1) |
vsluiter | 1:5e22bf1a3817 | 210 | { |
vsluiter | 1:5e22bf1a3817 | 211 | ESC_read(ESCREG_LOCALTIME, &ESCvar.Time, sizeof(ESCvar.Time), &ESCvar.ALevent); |
vsluiter | 1:5e22bf1a3817 | 212 | ESC_ALevent(); |
vsluiter | 1:5e22bf1a3817 | 213 | ESC_state(); |
vsluiter | 1:5e22bf1a3817 | 214 | if (ESC_mbxprocess()) |
vsluiter | 1:5e22bf1a3817 | 215 | { |
vsluiter | 1:5e22bf1a3817 | 216 | ESC_coeprocess(); |
vsluiter | 1:5e22bf1a3817 | 217 | ESC_xoeprocess(); |
vsluiter | 1:5e22bf1a3817 | 218 | } |
vsluiter | 1:5e22bf1a3817 | 219 | DIG_process(); |
vsluiter | 1:5e22bf1a3817 | 220 | } |
vsluiter | 1:5e22bf1a3817 | 221 | } |
vsluiter | 1:5e22bf1a3817 | 222 | |
vsluiter | 1:5e22bf1a3817 | 223 | /** void ESC_objecthandler(uint16 index, uint8 subindex) |
vsluiter | 1:5e22bf1a3817 | 224 | \brief Object handler, declared from esc.h, as extern function |
vsluiter | 1:5e22bf1a3817 | 225 | \param index |
vsluiter | 1:5e22bf1a3817 | 226 | \param subindex |
vsluiter | 1:5e22bf1a3817 | 227 | */ |
vsluiter | 1:5e22bf1a3817 | 228 | |
vsluiter | 1:5e22bf1a3817 | 229 | |
vsluiter | 1:5e22bf1a3817 | 230 | #ifdef USE_FULL_ASSERT |
vsluiter | 1:5e22bf1a3817 | 231 | |
vsluiter | 1:5e22bf1a3817 | 232 | /** |
vsluiter | 1:5e22bf1a3817 | 233 | * @brief Reports the name of the source file and the source line number |
vsluiter | 1:5e22bf1a3817 | 234 | * where the assert_param error has occurred. |
vsluiter | 1:5e22bf1a3817 | 235 | * @param file: pointer to the source file name |
vsluiter | 1:5e22bf1a3817 | 236 | * @param line: assert_param error line source number |
vsluiter | 1:5e22bf1a3817 | 237 | * @retval None |
vsluiter | 1:5e22bf1a3817 | 238 | */ |
vsluiter | 1:5e22bf1a3817 | 239 | void assert_failed(uint8_t* file, uint32_t line) |
vsluiter | 1:5e22bf1a3817 | 240 | { |
vsluiter | 1:5e22bf1a3817 | 241 | /* User can add his own implementation to report the file name and line number, |
vsluiter | 1:5e22bf1a3817 | 242 | ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ |
vsluiter | 1:5e22bf1a3817 | 243 | |
vsluiter | 1:5e22bf1a3817 | 244 | /* Infinite loop */ |
vsluiter | 1:5e22bf1a3817 | 245 | while (1) |
vsluiter | 1:5e22bf1a3817 | 246 | { |
vsluiter | 1:5e22bf1a3817 | 247 | } |
vsluiter | 1:5e22bf1a3817 | 248 | } |
vsluiter | 1:5e22bf1a3817 | 249 | #endif |
vsluiter | 1:5e22bf1a3817 | 250 | |
vsluiter | 1:5e22bf1a3817 | 251 | /** |
vsluiter | 1:5e22bf1a3817 | 252 | * @} |
vsluiter | 1:5e22bf1a3817 | 253 | */ |
vsluiter | 1:5e22bf1a3817 | 254 | |
vsluiter | 1:5e22bf1a3817 | 255 | /** |
vsluiter | 1:5e22bf1a3817 | 256 | * @} |
vsluiter | 1:5e22bf1a3817 | 257 | */ |
vsluiter | 1:5e22bf1a3817 | 258 | |
vsluiter | 1:5e22bf1a3817 | 259 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |
vsluiter | 1:5e22bf1a3817 | 260 |