EasyCAT LAB - EtherCAT master simple example

Dependencies:   SOEM SPI_TFT_ILI9341 TFT_fonts

  • This repository contains a simple example for the EasyCAT LAB , a complete educational and experimental EtherCAT® system, composed of one master and two slaves.

Note

  • This example uses two LAB 2 slaves.

Note

  • In this example only two bytes of data are exchanged between the slaves and are also visualized on the TFT display.
Committer:
EasyCAT
Date:
Wed Oct 25 14:42:52 2023 +0000
Revision:
4:cbef7fa67d5f
Parent:
3:9c8c179d1f8a
Bug fix

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sulymarco 0:13be39911caf 1 //********************************************************************************************
sulymarco 0:13be39911caf 2 // *
sulymarco 0:13be39911caf 3 // This software is distributed as an example, "AS IS", in the hope that it could *
sulymarco 0:13be39911caf 4 // be useful, WITHOUT ANY WARRANTY of any kind, express or implied, included, but *
sulymarco 0:13be39911caf 5 // not limited, to the warranties of merchantability, fitness for a particular *
sulymarco 0:13be39911caf 6 // purpose, and non infringiment. In no event shall the authors be liable for any *
sulymarco 0:13be39911caf 7 // claim, damages or other liability, arising from, or in connection with this software. *
sulymarco 0:13be39911caf 8 // *
sulymarco 0:13be39911caf 9 //********************************************************************************************
sulymarco 0:13be39911caf 10
sulymarco 0:13be39911caf 11
sulymarco 0:13be39911caf 12 // The AB&T EasyCAT LAB is a complete experimental EtherCAT® system, composed by
sulymarco 0:13be39911caf 13 // one master and two slaves.
sulymarco 0:13be39911caf 14 // The EasyCAT LAB software is provided free of charge and its pourpose is to allow
sulymarco 0:13be39911caf 15 // makers and educational institutes to experiment with the EtherCAT® protocol.
sulymarco 0:13be39911caf 16 //
sulymarco 0:13be39911caf 17 // The EasyCAT LAB is developed by "AB&T Tecnologie Informatiche" Via dell'About 2A Ivrea Italy.
sulymarco 0:13be39911caf 18 // www.bausano.net
sulymarco 0:13be39911caf 19 // www.easycatshield.com
sulymarco 0:13be39911caf 20 //
sulymarco 0:13be39911caf 21 // The EasyCAT LAB uses the SOEM library by rt:labs
sulymarco 0:13be39911caf 22 // https://rt-labs.com/products/soem-ethercat-master-stack/
sulymarco 0:13be39911caf 23 //
sulymarco 0:13be39911caf 24 // EtherCAT® is a registered trademark and patented technology, licensed by Beckhoff Automation GmbH.
sulymarco 0:13be39911caf 25 // www.beckhoff.com
EasyCAT 3:9c8c179d1f8a 26 // www.ethercat.org
sulymarco 2:a0e017115fc5 27
sulymarco 2:a0e017115fc5 28
sulymarco 2:a0e017115fc5 29 //******************************************************************************
sulymarco 2:a0e017115fc5 30
EasyCAT 3:9c8c179d1f8a 31 // IMPORTANT!!!
sulymarco 2:a0e017115fc5 32
EasyCAT 3:9c8c179d1f8a 33 #define ADA_TFT // If your EasyCAT LAB uses the Adafruit TFT
sulymarco 2:a0e017115fc5 34 // you must uncomment this define
sulymarco 2:a0e017115fc5 35
EasyCAT 3:9c8c179d1f8a 36 //#define SEEED_TFT // If your EasyCAT LAB uses the Seeed Studio TFT
EasyCAT 3:9c8c179d1f8a 37 // you must uncomment this define
EasyCAT 3:9c8c179d1f8a 38
EasyCAT 3:9c8c179d1f8a 39 //#define PARA_TFT // If your EasyCAT LAB uses the parallel interface TFT
EasyCAT 3:9c8c179d1f8a 40 // you must uncomment this define
EasyCAT 3:9c8c179d1f8a 41
EasyCAT 3:9c8c179d1f8a 42 //******************************************************************************
sulymarco 0:13be39911caf 43
sulymarco 0:13be39911caf 44 #define ETH_TXBUFNB 16
sulymarco 0:13be39911caf 45 #define ETH_RXBUFNB 16
sulymarco 0:13be39911caf 46
sulymarco 0:13be39911caf 47 #include "mbed.h"
sulymarco 0:13be39911caf 48
sulymarco 0:13be39911caf 49 #ifndef __align
sulymarco 0:13be39911caf 50 #define __align MBED_ALIGN
sulymarco 0:13be39911caf 51 #endif
sulymarco 0:13be39911caf 52
sulymarco 0:13be39911caf 53 #include "config.h"
sulymarco 0:13be39911caf 54 #include "SPI_TFT_ILI9341.h"
sulymarco 0:13be39911caf 55 #include "Arial12x12.h"
sulymarco 0:13be39911caf 56 #include "Arial24x23.h"
sulymarco 0:13be39911caf 57 #include "Arial28x28.h"
sulymarco 0:13be39911caf 58 #include "font_big.h"
sulymarco 0:13be39911caf 59 #include "soem_start.h"
sulymarco 0:13be39911caf 60
sulymarco 0:13be39911caf 61 #define CYCLE_TIME 1000 // master cycle time in uS
sulymarco 0:13be39911caf 62 // 1000 = 1mS
sulymarco 0:13be39911caf 63
sulymarco 0:13be39911caf 64
sulymarco 0:13be39911caf 65 #define SysMilliS() (uint32_t)Kernel::get_ms_count()
sulymarco 0:13be39911caf 66
EasyCAT 1:385bdd2b4cd6 67 UnbufferedSerial pc(USBTX,USBRX,115200); // set the debug serial line speed to 115200
sulymarco 0:13be39911caf 68
sulymarco 0:13be39911caf 69
sulymarco 0:13be39911caf 70 //---- TFT with resistive touchscreen pins -------------------------------------
sulymarco 0:13be39911caf 71
sulymarco 0:13be39911caf 72 // the display used is the SeeedStudio 2.8 inch TFT v2.0
sulymarco 0:13be39911caf 73 // http://wiki.seeedstudio.com/2.8inch_TFT_Touch_Shield_v2.0/
sulymarco 2:a0e017115fc5 74 //
sulymarco 2:a0e017115fc5 75 // or the Adafruit 2.8" with resistive touchscreen
sulymarco 2:a0e017115fc5 76 // https://www.adafruit.com/product/1651
EasyCAT 3:9c8c179d1f8a 77 //
EasyCAT 3:9c8c179d1f8a 78 // or the parallel interface ARD SHD 2,8TD
sulymarco 2:a0e017115fc5 79
sulymarco 2:a0e017115fc5 80 // the touchscreen is not used in this example
sulymarco 0:13be39911caf 81
EasyCAT 3:9c8c179d1f8a 82 #ifdef ADA_TFT // pins for the Adafruit TFT
EasyCAT 3:9c8c179d1f8a 83 #define PIN_CS_TFT D10 //
sulymarco 2:a0e017115fc5 84 #define PIN_DC_TFT D9 //
sulymarco 2:a0e017115fc5 85 #define PIN_CS_TSC D8 //
EasyCAT 3:9c8c179d1f8a 86
EasyCAT 3:9c8c179d1f8a 87 #define PIN_MOSI D11 // SPI
EasyCAT 3:9c8c179d1f8a 88 #define PIN_MISO D12 //
EasyCAT 3:9c8c179d1f8a 89 #define PIN_SCLK D13 //
EasyCAT 3:9c8c179d1f8a 90 #endif
sulymarco 2:a0e017115fc5 91
EasyCAT 3:9c8c179d1f8a 92 #ifdef SEEED_TFT // pins for the SeeedStudio TFT
sulymarco 2:a0e017115fc5 93 #define PIN_CS_TFT D5 //
sulymarco 2:a0e017115fc5 94 #define PIN_DC_TFT D6 //
EasyCAT 3:9c8c179d1f8a 95
EasyCAT 3:9c8c179d1f8a 96 #define PIN_MOSI D11 // SPI
EasyCAT 3:9c8c179d1f8a 97 #define PIN_MISO D12 //
EasyCAT 3:9c8c179d1f8a 98 #define PIN_SCLK D13 //
sulymarco 2:a0e017115fc5 99 #endif //
sulymarco 0:13be39911caf 100
EasyCAT 3:9c8c179d1f8a 101 #ifdef PARA_TFT // pins for the parallel interface TFT
EasyCAT 3:9c8c179d1f8a 102 #define PIN_D0_TFT D8 //
EasyCAT 3:9c8c179d1f8a 103 #define PIN_D1_TFT D9 //
EasyCAT 3:9c8c179d1f8a 104 #define PIN_D2_TFT D2 //
EasyCAT 3:9c8c179d1f8a 105 #define PIN_D3_TFT D3 //
EasyCAT 3:9c8c179d1f8a 106 #define PIN_D4_TFT D4 //
EasyCAT 3:9c8c179d1f8a 107 #define PIN_D5_TFT D5 //
EasyCAT 3:9c8c179d1f8a 108 #define PIN_D6_TFT D6 //
EasyCAT 3:9c8c179d1f8a 109 #define PIN_D7_TFT D7 //
EasyCAT 3:9c8c179d1f8a 110
EasyCAT 3:9c8c179d1f8a 111 #define PIN_RD_TFT A0 //
EasyCAT 3:9c8c179d1f8a 112 #define PIN_WR_TFT A1 //
EasyCAT 3:9c8c179d1f8a 113 #define PIN_DC_TFT A2 //
EasyCAT 3:9c8c179d1f8a 114 #define PIN_CS_TFT A3 //
EasyCAT 3:9c8c179d1f8a 115 #define PIN_RES_TFT A4 //
EasyCAT 3:9c8c179d1f8a 116 #endif
EasyCAT 3:9c8c179d1f8a 117
EasyCAT 3:9c8c179d1f8a 118 #ifdef SEEED_TFT
EasyCAT 3:9c8c179d1f8a 119 #define PIN_YP A3 // resistive touchscreen
EasyCAT 3:9c8c179d1f8a 120 #define PIN_YM A1 //
EasyCAT 3:9c8c179d1f8a 121 #define PIN_XM A2 //
EasyCAT 3:9c8c179d1f8a 122 #define PIN_XP A0 //
EasyCAT 3:9c8c179d1f8a 123 #else //
EasyCAT 3:9c8c179d1f8a 124 #define PIN_XP A3 //
EasyCAT 3:9c8c179d1f8a 125 #define PIN_YP A2 //
EasyCAT 3:9c8c179d1f8a 126 #define PIN_XM D9 //
EasyCAT 3:9c8c179d1f8a 127 #define PIN_YM D8 //
EasyCAT 3:9c8c179d1f8a 128 #endif
EasyCAT 3:9c8c179d1f8a 129 //
EasyCAT 3:9c8c179d1f8a 130
sulymarco 0:13be39911caf 131
sulymarco 0:13be39911caf 132
sulymarco 0:13be39911caf 133 //---- visualization parameters ------------------------------------------------
sulymarco 0:13be39911caf 134
sulymarco 0:13be39911caf 135 #define BUTTONS_1_X 80
sulymarco 0:13be39911caf 136 #define BUTTONS_1_Y 60
sulymarco 0:13be39911caf 137
sulymarco 0:13be39911caf 138 #define BUTTONS_2_X 80
sulymarco 0:13be39911caf 139 #define BUTTONS_2_Y 180
sulymarco 0:13be39911caf 140
sulymarco 0:13be39911caf 141
sulymarco 0:13be39911caf 142 #define BUTTONS_1_WIDTH 26
sulymarco 0:13be39911caf 143 #define BUTTONS_1_R 3
sulymarco 0:13be39911caf 144 #define BUTTONS_1_STEP 60
sulymarco 0:13be39911caf 145
sulymarco 0:13be39911caf 146 #define BUTTONS_2_WIDTH BUTTONS_1_WIDTH
sulymarco 0:13be39911caf 147 #define BUTTONS_2_R BUTTONS_1_R
sulymarco 0:13be39911caf 148 #define BUTTONS_2_STEP BUTTONS_1_STEP
sulymarco 0:13be39911caf 149
sulymarco 0:13be39911caf 150
sulymarco 0:13be39911caf 151 //---- local functions ---------------------------------------------------------
sulymarco 0:13be39911caf 152
sulymarco 0:13be39911caf 153 void DrawBanner();
sulymarco 0:13be39911caf 154 void DrawSlaveFixedParts();
sulymarco 0:13be39911caf 155 void DrawButtons_1_Value(uint8_t Value);
sulymarco 0:13be39911caf 156 void DrawButtons_2_Value(uint8_t Value);
sulymarco 0:13be39911caf 157
sulymarco 0:13be39911caf 158 void Application();
sulymarco 0:13be39911caf 159
sulymarco 0:13be39911caf 160
sulymarco 0:13be39911caf 161 //---- global variables --------------------------------------------------------
sulymarco 0:13be39911caf 162
sulymarco 0:13be39911caf 163
sulymarco 0:13be39911caf 164 bool FirstRound;
sulymarco 0:13be39911caf 165
sulymarco 0:13be39911caf 166 //------------------------------------------------------------------------------
sulymarco 0:13be39911caf 167
EasyCAT 3:9c8c179d1f8a 168 #ifdef PARA_TFT
EasyCAT 3:9c8c179d1f8a 169 SPI_TFT_ILI9341 TFT(PIN_D0_TFT, PIN_D1_TFT, PIN_D2_TFT, PIN_D3_TFT, PIN_D4_TFT, PIN_D5_TFT,
EasyCAT 3:9c8c179d1f8a 170 PIN_D6_TFT, PIN_D7_TFT, PIN_RD_TFT, PIN_WR_TFT, PIN_CS_TFT, PIN_DC_TFT, PIN_RES_TFT, "PARA");
EasyCAT 3:9c8c179d1f8a 171 #endif
EasyCAT 3:9c8c179d1f8a 172
EasyCAT 3:9c8c179d1f8a 173 #ifdef ADA_TFT
EasyCAT 3:9c8c179d1f8a 174 SPI_TFT_ILI9341 TFT(PIN_MOSI, PIN_MISO, PIN_SCLK, PIN_CS_TFT, NC, PIN_DC_TFT, 27000000, "ADA");
EasyCAT 3:9c8c179d1f8a 175 #endif
EasyCAT 3:9c8c179d1f8a 176
EasyCAT 3:9c8c179d1f8a 177 #ifdef SEEED_TFT
EasyCAT 3:9c8c179d1f8a 178 SPI_TFT_ILI9341 TFT(PIN_MOSI, PIN_MISO, PIN_SCLK, PIN_CS_TFT, NC, PIN_DC_TFT, 27000000, "SEEED");
EasyCAT 3:9c8c179d1f8a 179 #endif
sulymarco 0:13be39911caf 180
sulymarco 0:13be39911caf 181 Ticker SampleTicker;
sulymarco 0:13be39911caf 182 Thread thread;
sulymarco 0:13be39911caf 183
sulymarco 0:13be39911caf 184 DigitalOut Test_1(D1); // debug test points
sulymarco 0:13be39911caf 185 DigitalOut Test_2(D2); //
sulymarco 0:13be39911caf 186 DigitalOut Test_3(D3); //
sulymarco 0:13be39911caf 187 DigitalOut Test_4(D4); //
sulymarco 0:13be39911caf 188
sulymarco 0:13be39911caf 189
sulymarco 0:13be39911caf 190 //------------------------------------------------------------------------------
sulymarco 0:13be39911caf 191
sulymarco 0:13be39911caf 192 uint8_t Buttons_1;
sulymarco 0:13be39911caf 193 uint8_t PrevButtons_1;
sulymarco 0:13be39911caf 194 uint8_t Segments_1;
sulymarco 0:13be39911caf 195 uint8_t PrevSegments_1;
sulymarco 0:13be39911caf 196
sulymarco 0:13be39911caf 197 uint8_t Buttons_2;
sulymarco 0:13be39911caf 198 uint8_t PrevButtons_2;
sulymarco 0:13be39911caf 199 uint8_t Segments_2;
sulymarco 0:13be39911caf 200 uint8_t PrevSegments_2;
sulymarco 0:13be39911caf 201
sulymarco 0:13be39911caf 202
sulymarco 0:13be39911caf 203 //------------------------------------------------------------------------------
sulymarco 0:13be39911caf 204
sulymarco 0:13be39911caf 205 int ExpectWorkCounter;
sulymarco 0:13be39911caf 206 int WorkCounter;
sulymarco 0:13be39911caf 207 int WorkCounterSafe;
sulymarco 0:13be39911caf 208 bool NetworkError;
sulymarco 0:13be39911caf 209 bool NetworkErrorSafe;
sulymarco 0:13be39911caf 210
sulymarco 0:13be39911caf 211 #define DATA_EXCHANGE_FLAG (1UL << 0)
sulymarco 0:13be39911caf 212 #define APPLICATION_FLAG (1UL << 1)
sulymarco 0:13be39911caf 213
sulymarco 0:13be39911caf 214 EventFlags event_flags;
sulymarco 0:13be39911caf 215
sulymarco 0:13be39911caf 216 Mutex IO_data;
sulymarco 0:13be39911caf 217
sulymarco 0:13be39911caf 218
sulymarco 0:13be39911caf 219 //---- data exchange thread ----------------------------------------------------
sulymarco 0:13be39911caf 220
sulymarco 0:13be39911caf 221 void ExchangeMaster()
sulymarco 0:13be39911caf 222 {
sulymarco 0:13be39911caf 223 while (true)
sulymarco 0:13be39911caf 224 {
sulymarco 0:13be39911caf 225 event_flags.wait_any(DATA_EXCHANGE_FLAG); // the thread waits for the synchronization flag
sulymarco 0:13be39911caf 226
sulymarco 0:13be39911caf 227 //Test_1 = 1;
sulymarco 0:13be39911caf 228
sulymarco 0:13be39911caf 229 IO_data.lock(); // Ethercat data exchange
sulymarco 0:13be39911caf 230 ec_send_processdata(); //
sulymarco 0:13be39911caf 231 WorkCounter = ec_receive_processdata(EC_TIMEOUTRET);
sulymarco 0:13be39911caf 232
sulymarco 0:13be39911caf 233 if (WorkCounter != ExpectWorkCounter)
sulymarco 0:13be39911caf 234 NetworkError = true;
sulymarco 0:13be39911caf 235 else
sulymarco 0:13be39911caf 236 NetworkError = false;
sulymarco 0:13be39911caf 237
sulymarco 0:13be39911caf 238 IO_data.unlock(); //
sulymarco 0:13be39911caf 239 event_flags.set(APPLICATION_FLAG); // synchronize the application
sulymarco 0:13be39911caf 240
sulymarco 0:13be39911caf 241 //Test_1 = 0;
sulymarco 0:13be39911caf 242 }
sulymarco 0:13be39911caf 243 }
sulymarco 0:13be39911caf 244
sulymarco 0:13be39911caf 245
sulymarco 0:13be39911caf 246 //----- thicker generated sample time ------------------------------------------
sulymarco 0:13be39911caf 247
sulymarco 0:13be39911caf 248 void SampleIsr() // set the event that starts
sulymarco 0:13be39911caf 249 { // the data exchange
sulymarco 0:13be39911caf 250 event_flags.set(DATA_EXCHANGE_FLAG); //
sulymarco 0:13be39911caf 251 } //
sulymarco 0:13be39911caf 252
sulymarco 0:13be39911caf 253
sulymarco 0:13be39911caf 254 //****** initialization ********************************************************
sulymarco 0:13be39911caf 255
sulymarco 0:13be39911caf 256 int main()
sulymarco 0:13be39911caf 257 {
sulymarco 0:13be39911caf 258 int i;
sulymarco 0:13be39911caf 259
sulymarco 0:13be39911caf 260 printf("Start \n");
sulymarco 0:13be39911caf 261
sulymarco 0:13be39911caf 262 Test_1 = 0;
sulymarco 0:13be39911caf 263 Test_2 = 0;
sulymarco 0:13be39911caf 264 Test_3 = 0;
sulymarco 0:13be39911caf 265 Test_4 = 0;
sulymarco 0:13be39911caf 266
sulymarco 0:13be39911caf 267 TFT.background(Black); // init TFT
sulymarco 0:13be39911caf 268 TFT.cls(); //
sulymarco 2:a0e017115fc5 269
EasyCAT 3:9c8c179d1f8a 270 #ifdef ADA_TFT
sulymarco 2:a0e017115fc5 271 TFT.set_orientation(1);
EasyCAT 3:9c8c179d1f8a 272 #endif
EasyCAT 3:9c8c179d1f8a 273 #ifdef SEEED_TFT
sulymarco 2:a0e017115fc5 274 TFT.set_orientation(3);
EasyCAT 3:9c8c179d1f8a 275 #endif
EasyCAT 3:9c8c179d1f8a 276 #ifdef PARA_TFT
EasyCAT 3:9c8c179d1f8a 277 TFT.set_orientation(1);
EasyCAT 3:9c8c179d1f8a 278 #endif
sulymarco 0:13be39911caf 279
sulymarco 0:13be39911caf 280 DrawBanner();
sulymarco 0:13be39911caf 281
sulymarco 0:13be39911caf 282 NetworkError = false;
sulymarco 0:13be39911caf 283
sulymarco 0:13be39911caf 284 if (ec_init(NULL)) // init SOEM
sulymarco 0:13be39911caf 285 {
sulymarco 0:13be39911caf 286 printf("ec_init succeeded.\n");
sulymarco 0:13be39911caf 287 printf("Scanning the network\n");
sulymarco 0:13be39911caf 288
sulymarco 0:13be39911caf 289 TFT.cls();
sulymarco 0:13be39911caf 290
sulymarco 0:13be39911caf 291 TFT.set_font((unsigned char*) Arial12x12);
sulymarco 0:13be39911caf 292 TFT.foreground(Green);
sulymarco 0:13be39911caf 293 TFT.locate(0, 0);
sulymarco 0:13be39911caf 294
sulymarco 0:13be39911caf 295 TFT.printf("Scanning the network\n");
sulymarco 0:13be39911caf 296
sulymarco 0:13be39911caf 297 if (network_scanning())
sulymarco 0:13be39911caf 298 {
sulymarco 0:13be39911caf 299 if (network_configuration()) // check network configuration
sulymarco 0:13be39911caf 300 {
sulymarco 0:13be39911caf 301 ec_config_map(&IOmap); // map the I/O
sulymarco 0:13be39911caf 302 MapLocalStructures();
sulymarco 0:13be39911caf 303
sulymarco 0:13be39911caf 304 printf("\nSlaves mapped, state to SAFE_OP.\n");
sulymarco 0:13be39911caf 305 // wait for all slaves to reach SAFE_OP state
sulymarco 0:13be39911caf 306 ec_statecheck(0, EC_STATE_SAFE_OP, EC_TIMEOUTSTATE);
sulymarco 0:13be39911caf 307
sulymarco 0:13be39911caf 308 printf("Request operational state for all slaves\n");
sulymarco 0:13be39911caf 309 ec_slave[0].state = EC_STATE_OPERATIONAL;
sulymarco 0:13be39911caf 310
sulymarco 0:13be39911caf 311 ec_send_processdata(); // send one valid process data to make outputs in slaves happy
sulymarco 0:13be39911caf 312 ExpectWorkCounter = ec_receive_processdata(EC_TIMEOUTRET);
sulymarco 0:13be39911caf 313
sulymarco 0:13be39911caf 314 ec_writestate(0); // request OP state for all slaves
sulymarco 0:13be39911caf 315
sulymarco 0:13be39911caf 316 // wait for all slaves to reach OP state
sulymarco 0:13be39911caf 317 ec_statecheck(0, EC_STATE_OPERATIONAL, EC_TIMEOUTSTATE);
sulymarco 0:13be39911caf 318 if (ec_slave[0].state == EC_STATE_OPERATIONAL )
sulymarco 0:13be39911caf 319 {
sulymarco 0:13be39911caf 320 printf("Operational state reached for all slaves.\n");
sulymarco 0:13be39911caf 321 }
sulymarco 0:13be39911caf 322 else
sulymarco 0:13be39911caf 323 {
sulymarco 0:13be39911caf 324 printf("Not all slaves reached operational state.\n");
sulymarco 0:13be39911caf 325 ec_readstate();
sulymarco 0:13be39911caf 326 for(i = 1; i<=ec_slavecount ; i++)
sulymarco 0:13be39911caf 327 {
sulymarco 0:13be39911caf 328 if(ec_slave[i].state != EC_STATE_OPERATIONAL)
sulymarco 0:13be39911caf 329 {
sulymarco 0:13be39911caf 330 printf("Slave %d State=0x%04x StatusCode=0x%04x\n",
sulymarco 0:13be39911caf 331 i, ec_slave[i].state, ec_slave[i].ALstatuscode);
sulymarco 0:13be39911caf 332 }
sulymarco 0:13be39911caf 333 }
sulymarco 0:13be39911caf 334
sulymarco 0:13be39911caf 335 TFT.foreground(Red);
sulymarco 0:13be39911caf 336 TFT.locate(0, 0);
sulymarco 0:13be39911caf 337 TFT.printf("Not all slaves reached operational state!");
sulymarco 0:13be39911caf 338 while(1){}
sulymarco 0:13be39911caf 339 }
sulymarco 0:13be39911caf 340
sulymarco 0:13be39911caf 341 DrawSlaveFixedParts();
sulymarco 0:13be39911caf 342
sulymarco 0:13be39911caf 343 thread.start(ExchangeMaster);
sulymarco 0:13be39911caf 344 thread.set_priority(osPriorityRealtime);
sulymarco 0:13be39911caf 345
sulymarco 0:13be39911caf 346 SampleTicker.attach_us(&SampleIsr, CYCLE_TIME);
sulymarco 0:13be39911caf 347
sulymarco 0:13be39911caf 348 Application();
sulymarco 0:13be39911caf 349 }
sulymarco 0:13be39911caf 350
sulymarco 0:13be39911caf 351 else
sulymarco 0:13be39911caf 352 {
sulymarco 0:13be39911caf 353 printf("Mismatch of network units!\n");
sulymarco 0:13be39911caf 354 TFT.foreground(Red);
sulymarco 0:13be39911caf 355 TFT.locate(0, 0);
sulymarco 0:13be39911caf 356 TFT.printf("Mismatch of network units!");
sulymarco 0:13be39911caf 357
sulymarco 0:13be39911caf 358 while(1){}
sulymarco 0:13be39911caf 359 }
sulymarco 0:13be39911caf 360 }
sulymarco 0:13be39911caf 361
sulymarco 0:13be39911caf 362 else
sulymarco 0:13be39911caf 363 {
sulymarco 0:13be39911caf 364 printf("No slaves found!\n");
sulymarco 0:13be39911caf 365 TFT.foreground(Red);
sulymarco 0:13be39911caf 366 TFT.printf("No slaves found!");
sulymarco 0:13be39911caf 367
sulymarco 0:13be39911caf 368 while(1){}
sulymarco 0:13be39911caf 369 }
sulymarco 0:13be39911caf 370 }
sulymarco 0:13be39911caf 371 else
sulymarco 0:13be39911caf 372 {
sulymarco 0:13be39911caf 373 printf("Ethernet interface init failed!");
sulymarco 0:13be39911caf 374 TFT.foreground(Red);
sulymarco 0:13be39911caf 375 TFT.locate(0, 0);
sulymarco 0:13be39911caf 376 TFT.printf("Ethernet interface init failed!");
sulymarco 0:13be39911caf 377 while(1){}
sulymarco 0:13be39911caf 378 }
sulymarco 0:13be39911caf 379 }
sulymarco 0:13be39911caf 380
sulymarco 0:13be39911caf 381
sulymarco 0:13be39911caf 382 //****** user master application **********************************************
sulymarco 0:13be39911caf 383
sulymarco 0:13be39911caf 384 void Application()
sulymarco 0:13be39911caf 385 {
sulymarco 0:13be39911caf 386
sulymarco 0:13be39911caf 387 while(1)
sulymarco 0:13be39911caf 388 {
sulymarco 0:13be39911caf 389 event_flags.wait_any(APPLICATION_FLAG); // the application waits for the synchronization flag
sulymarco 0:13be39911caf 390
sulymarco 0:13be39911caf 391 //Test_2 = 1;
sulymarco 0:13be39911caf 392
sulymarco 0:13be39911caf 393 IO_data.lock(); // copy the Ethercat data to a safe buffer
sulymarco 0:13be39911caf 394 memcpy(&IOmapSafe[0], &IOmap[0], IO_MAP_SIZE); //
sulymarco 0:13be39911caf 395 //
sulymarco 0:13be39911caf 396 if (NetworkError) //
sulymarco 0:13be39911caf 397 { //
sulymarco 0:13be39911caf 398 NetworkErrorSafe = NetworkError; //
sulymarco 0:13be39911caf 399 WorkCounterSafe = WorkCounter; //
sulymarco 0:13be39911caf 400 } //
sulymarco 0:13be39911caf 401 IO_data.unlock(); //
sulymarco 0:13be39911caf 402
sulymarco 0:13be39911caf 403 if (NetworkErrorSafe)
sulymarco 0:13be39911caf 404 {
sulymarco 0:13be39911caf 405 TFT.rect(35,50, 285, 182, Red);
sulymarco 0:13be39911caf 406 TFT.fillrect(36,51, 284, 181, Black);
sulymarco 0:13be39911caf 407 TFT.foreground(Red);
sulymarco 0:13be39911caf 408 TFT.set_font((unsigned char*) Arial28x28);
sulymarco 0:13be39911caf 409 TFT.locate(58, 65);
sulymarco 0:13be39911caf 410 TFT.printf("Network error!");
sulymarco 0:13be39911caf 411 printf("Network error!\n");
sulymarco 0:13be39911caf 412 TFT.foreground(Magenta);
sulymarco 0:13be39911caf 413 TFT.set_font((unsigned char*) Arial12x12);
sulymarco 0:13be39911caf 414 TFT.locate(58, 106);
sulymarco 0:13be39911caf 415
sulymarco 0:13be39911caf 416 if(WorkCounterSafe >= 0)
sulymarco 0:13be39911caf 417 {
sulymarco 0:13be39911caf 418 TFT.printf("Expected working counter %d", ExpectWorkCounter);
sulymarco 0:13be39911caf 419 TFT.locate(58, 118);
sulymarco 0:13be39911caf 420 TFT.printf("Actual working counter %d", WorkCounterSafe);
sulymarco 0:13be39911caf 421 printf("Expected working counter %d\n", ExpectWorkCounter);
sulymarco 0:13be39911caf 422 printf("Actual working counter %d\n", WorkCounterSafe);
sulymarco 0:13be39911caf 423 }
sulymarco 0:13be39911caf 424 else
sulymarco 0:13be39911caf 425 {
sulymarco 0:13be39911caf 426 TFT.printf("Timeout");
sulymarco 0:13be39911caf 427 printf("Timeout\n");
sulymarco 0:13be39911caf 428 }
sulymarco 0:13be39911caf 429
sulymarco 0:13be39911caf 430 TFT.foreground(Green);
sulymarco 0:13be39911caf 431 TFT.locate(58, 142);
sulymarco 0:13be39911caf 432 TFT.printf("Please fix the error and");
sulymarco 0:13be39911caf 433 TFT.locate(58, 154);
sulymarco 0:13be39911caf 434 TFT.printf("press the reset button");
sulymarco 0:13be39911caf 435 printf("Please fix the error and press the reset button \n");
sulymarco 0:13be39911caf 436
sulymarco 0:13be39911caf 437 SampleTicker.detach(); // stop the sample interrupt
sulymarco 0:13be39911caf 438 while(1){} // and loop for ever
sulymarco 0:13be39911caf 439 }
sulymarco 0:13be39911caf 440
sulymarco 0:13be39911caf 441
sulymarco 0:13be39911caf 442 //----- slave LAB_2_1 data management ------
sulymarco 0:13be39911caf 443 //
EasyCAT 4:cbef7fa67d5f 444 Buttons_1 = in_LAB_1->Buttons; // read the buttons status from the slave
sulymarco 0:13be39911caf 445 //
sulymarco 0:13be39911caf 446 if (Buttons_1 != PrevButtons_1) // check if the buttons value has changed
sulymarco 0:13be39911caf 447 { //
sulymarco 0:13be39911caf 448 PrevButtons_1 = Buttons_1; // remember the current buttons value
sulymarco 0:13be39911caf 449 DrawButtons_1_Value(Buttons_1); // draw the current buttons value on the TFT
sulymarco 0:13be39911caf 450 } //
sulymarco 0:13be39911caf 451 //
EasyCAT 4:cbef7fa67d5f 452 out_LAB_1->Segments = Buttons_2; // send to the slave the buttons status
sulymarco 0:13be39911caf 453 // from the slave LAB_2_2
sulymarco 0:13be39911caf 454
sulymarco 0:13be39911caf 455
sulymarco 0:13be39911caf 456
sulymarco 0:13be39911caf 457 //----- slave 2_2 data management ----------
sulymarco 0:13be39911caf 458 //
EasyCAT 4:cbef7fa67d5f 459 Buttons_2 = in_LAB_2->Buttons; // read the buttons status from the slave
sulymarco 0:13be39911caf 460 //
sulymarco 0:13be39911caf 461 if (Buttons_2 != PrevButtons_2) // check if the buttons value has changed
sulymarco 0:13be39911caf 462 { //
sulymarco 0:13be39911caf 463 PrevButtons_2 = Buttons_2; // remember the current buttons value
sulymarco 0:13be39911caf 464 DrawButtons_2_Value(Buttons_2); // draw the current buttons value on the TFT
sulymarco 0:13be39911caf 465 } //
sulymarco 0:13be39911caf 466 //
EasyCAT 4:cbef7fa67d5f 467 out_LAB_2->Segments = Buttons_1; // send to the slave the buttons status
sulymarco 0:13be39911caf 468 // from the slave LAB_2_1
sulymarco 0:13be39911caf 469
sulymarco 0:13be39911caf 470
sulymarco 0:13be39911caf 471
sulymarco 0:13be39911caf 472 IO_data.lock(); // copy the IO data from the safe area
sulymarco 0:13be39911caf 473 memcpy(&IOmap[0], &IOmapSafe[0], IO_MAP_SIZE); // to the EtherCAT buffer
sulymarco 0:13be39911caf 474 IO_data.unlock(); //
sulymarco 0:13be39911caf 475
sulymarco 0:13be39911caf 476 //Test_2 = 0;
sulymarco 0:13be39911caf 477 }
sulymarco 0:13be39911caf 478 }
sulymarco 0:13be39911caf 479
sulymarco 0:13be39911caf 480
sulymarco 0:13be39911caf 481 //******************************************************************************
sulymarco 0:13be39911caf 482
sulymarco 0:13be39911caf 483
sulymarco 0:13be39911caf 484
sulymarco 0:13be39911caf 485 //******* general functions ****************************************************
sulymarco 0:13be39911caf 486
sulymarco 0:13be39911caf 487
sulymarco 0:13be39911caf 488
sulymarco 0:13be39911caf 489 //----- draw the fixed part of the slaves visualization ------------------------
sulymarco 0:13be39911caf 490
sulymarco 0:13be39911caf 491 void DrawSlaveFixedParts()
sulymarco 0:13be39911caf 492 {
sulymarco 0:13be39911caf 493 int i;
sulymarco 0:13be39911caf 494
sulymarco 0:13be39911caf 495 TFT.cls(); // clear screen
sulymarco 0:13be39911caf 496 TFT.line(0, 120, 340, 120, Green); // draw the separation line
sulymarco 0:13be39911caf 497
sulymarco 0:13be39911caf 498 for (i=0; i<3; i++) // draw the buttons fixed parts
sulymarco 0:13be39911caf 499 { // for the LAB_2_1 slave
sulymarco 0:13be39911caf 500 TFT.circle(BUTTONS_1_X+(i*BUTTONS_1_STEP), BUTTONS_1_Y, BUTTONS_1_R, Red);
sulymarco 0:13be39911caf 501 TFT.circle(BUTTONS_1_X+BUTTONS_1_WIDTH+(i*BUTTONS_1_STEP), BUTTONS_1_Y, BUTTONS_1_R, Red);
sulymarco 0:13be39911caf 502 //
sulymarco 0:13be39911caf 503 TFT.line(BUTTONS_1_X+(i*BUTTONS_1_STEP)-BUTTONS_1_R, BUTTONS_1_Y, BUTTONS_1_X+(i*BUTTONS_1_STEP)-BUTTONS_1_R-4, BUTTONS_1_Y, Red);
sulymarco 0:13be39911caf 504 TFT.line(BUTTONS_1_X+BUTTONS_1_WIDTH+(i*BUTTONS_1_STEP)+BUTTONS_1_R, BUTTONS_1_Y, BUTTONS_1_X+BUTTONS_1_WIDTH+(i*BUTTONS_1_STEP)+BUTTONS_1_R+4 , BUTTONS_1_Y, Red);
sulymarco 0:13be39911caf 505 } //
sulymarco 0:13be39911caf 506 //
sulymarco 0:13be39911caf 507 TFT.set_font((unsigned char*) Arial12x12); //
sulymarco 0:13be39911caf 508 TFT.locate(BUTTONS_1_X+12, BUTTONS_1_Y+20); //
sulymarco 0:13be39911caf 509 TFT.printf("LAB_2_1 Buttons"); //
sulymarco 0:13be39911caf 510
sulymarco 0:13be39911caf 511 for (i=0; i<3; i++) // draw the buttons fixed parts
sulymarco 0:13be39911caf 512 { // for the LAB_2_2 slave
sulymarco 0:13be39911caf 513 TFT.circle(BUTTONS_2_X+(i*BUTTONS_2_STEP), BUTTONS_2_Y, BUTTONS_2_R, Red);
sulymarco 0:13be39911caf 514 TFT.circle(BUTTONS_2_X+BUTTONS_2_WIDTH+(i*BUTTONS_2_STEP), BUTTONS_2_Y, BUTTONS_2_R, Red);
sulymarco 0:13be39911caf 515 //
sulymarco 0:13be39911caf 516 TFT.line(BUTTONS_2_X+(i*BUTTONS_2_STEP)-BUTTONS_2_R, BUTTONS_2_Y, BUTTONS_2_X+(i*BUTTONS_2_STEP)-BUTTONS_2_R-4, BUTTONS_2_Y, Red);
sulymarco 0:13be39911caf 517 TFT.line(BUTTONS_2_X+BUTTONS_2_WIDTH+(i*BUTTONS_2_STEP)+BUTTONS_2_R, BUTTONS_2_Y, BUTTONS_2_X+BUTTONS_2_WIDTH+(i*BUTTONS_2_STEP)+BUTTONS_2_R+4 , BUTTONS_2_Y, Red);
sulymarco 0:13be39911caf 518 } //
sulymarco 0:13be39911caf 519 //
sulymarco 0:13be39911caf 520 TFT.set_font((unsigned char*) Arial12x12); //
sulymarco 0:13be39911caf 521 TFT.locate(BUTTONS_2_X+12, BUTTONS_2_Y+20); //
sulymarco 0:13be39911caf 522 TFT.printf("LAB_2_2 Buttons"); //
sulymarco 0:13be39911caf 523
sulymarco 0:13be39911caf 524 DrawButtons_1_Value (Buttons_1); // draw the buttons status
sulymarco 0:13be39911caf 525 DrawButtons_2_Value (Buttons_2); //
sulymarco 0:13be39911caf 526
sulymarco 0:13be39911caf 527 FirstRound = true;
sulymarco 0:13be39911caf 528 }
sulymarco 0:13be39911caf 529
sulymarco 0:13be39911caf 530
sulymarco 0:13be39911caf 531 //---- draw the starting banner ------------------------------------------------
sulymarco 0:13be39911caf 532
sulymarco 0:13be39911caf 533 void DrawBanner()
sulymarco 0:13be39911caf 534 {
sulymarco 0:13be39911caf 535 TFT.set_font((unsigned char*) Arial24x23);
sulymarco 0:13be39911caf 536 TFT.foreground(Red);
sulymarco 0:13be39911caf 537 TFT.locate(30, 30);
sulymarco 0:13be39911caf 538 TFT.printf("EasyCAT");
sulymarco 0:13be39911caf 539 TFT.locate(30, 60);
sulymarco 0:13be39911caf 540 TFT.printf("SOEM MASTER");
sulymarco 0:13be39911caf 541
sulymarco 0:13be39911caf 542 TFT.foreground(Magenta);
sulymarco 0:13be39911caf 543 TFT.locate(30, 100);
sulymarco 0:13be39911caf 544 TFT.printf("simple example");
sulymarco 0:13be39911caf 545
sulymarco 0:13be39911caf 546 TFT.set_font((unsigned char*) Arial12x12);
sulymarco 0:13be39911caf 547 TFT.foreground(Green);
sulymarco 0:13be39911caf 548 TFT.locate(30, 150);
sulymarco 0:13be39911caf 549 TFT.printf("www.bausano.net");
sulymarco 0:13be39911caf 550
sulymarco 0:13be39911caf 551 TFT.foreground(Green);
sulymarco 0:13be39911caf 552 TFT.locate(30, 170);
sulymarco 0:13be39911caf 553 TFT.printf("www.easycatshield.com");
sulymarco 0:13be39911caf 554
sulymarco 0:13be39911caf 555 TFT.locate(30, 190);
sulymarco 2:a0e017115fc5 556 TFT.printf("https://openethercatsociety.github.io/");
sulymarco 2:a0e017115fc5 557
sulymarco 2:a0e017115fc5 558 TFT.foreground(Red);
sulymarco 2:a0e017115fc5 559 TFT.locate(30, 220);
EasyCAT 3:9c8c179d1f8a 560 #ifdef ADA_TFT
sulymarco 2:a0e017115fc5 561 TFT.printf("Adafruit TFT");
EasyCAT 3:9c8c179d1f8a 562 #endif
EasyCAT 3:9c8c179d1f8a 563 #ifdef SEEED_TFT
sulymarco 2:a0e017115fc5 564 TFT.printf("Seeed Studio TFT");
EasyCAT 3:9c8c179d1f8a 565 #endif
EasyCAT 3:9c8c179d1f8a 566 #ifdef PARA_TFT
EasyCAT 3:9c8c179d1f8a 567 TFT.printf("Parallel TFT");
EasyCAT 3:9c8c179d1f8a 568 #endif
sulymarco 0:13be39911caf 569 }
sulymarco 0:13be39911caf 570
sulymarco 0:13be39911caf 571
sulymarco 0:13be39911caf 572 //---- slaves data visualization functions -------------------------------------
sulymarco 0:13be39911caf 573
sulymarco 0:13be39911caf 574 void DrawButtons_1_Value (uint8_t Value) // visualize on the TFT the
sulymarco 0:13be39911caf 575 { // slave LAB_2_1 buttons status
sulymarco 0:13be39911caf 576 uint8_t Slope;
sulymarco 0:13be39911caf 577 int i;
sulymarco 0:13be39911caf 578
sulymarco 0:13be39911caf 579 for (i=0; i<3; i++)
sulymarco 0:13be39911caf 580 {
sulymarco 0:13be39911caf 581 if ((Value & 0x04) == 0x04)
sulymarco 0:13be39911caf 582 Slope = BUTTONS_1_R;
sulymarco 0:13be39911caf 583 else
sulymarco 0:13be39911caf 584 Slope = 16;
sulymarco 0:13be39911caf 585
sulymarco 0:13be39911caf 586 TFT.fillrect(BUTTONS_1_X+(i*BUTTONS_1_STEP), BUTTONS_1_Y-16-1, BUTTONS_1_X+BUTTONS_1_WIDTH+(i*BUTTONS_1_STEP), BUTTONS_1_Y-BUTTONS_1_R-1, Black);
sulymarco 0:13be39911caf 587
sulymarco 0:13be39911caf 588 TFT.line(BUTTONS_1_X+(i*BUTTONS_1_STEP), BUTTONS_1_Y-BUTTONS_1_R-1, BUTTONS_1_X+BUTTONS_1_WIDTH+(i*BUTTONS_1_STEP), BUTTONS_1_Y-Slope-1, Red);
sulymarco 0:13be39911caf 589
sulymarco 0:13be39911caf 590 Value = Value << 1;
sulymarco 0:13be39911caf 591 }
sulymarco 0:13be39911caf 592 }
sulymarco 0:13be39911caf 593
sulymarco 0:13be39911caf 594
sulymarco 0:13be39911caf 595 void DrawButtons_2_Value (uint8_t Value) // visualize on the TFT the
sulymarco 0:13be39911caf 596 { // slave LAB_2_2 buttons status
sulymarco 0:13be39911caf 597 uint8_t Slope;
sulymarco 0:13be39911caf 598 int i;
sulymarco 0:13be39911caf 599
sulymarco 0:13be39911caf 600 for (i=0; i<3; i++)
sulymarco 0:13be39911caf 601 {
sulymarco 0:13be39911caf 602 if ((Value & 0x04) == 0x04)
sulymarco 0:13be39911caf 603 Slope = BUTTONS_1_R;
sulymarco 0:13be39911caf 604 else
sulymarco 0:13be39911caf 605 Slope = 16;
sulymarco 0:13be39911caf 606
sulymarco 0:13be39911caf 607 TFT.fillrect(BUTTONS_2_X+(i*BUTTONS_2_STEP), BUTTONS_2_Y-16-1, BUTTONS_2_X+BUTTONS_2_WIDTH+(i*BUTTONS_2_STEP), BUTTONS_2_Y-BUTTONS_2_R-1, Black);
sulymarco 0:13be39911caf 608
sulymarco 0:13be39911caf 609 TFT.line(BUTTONS_2_X+(i*BUTTONS_2_STEP), BUTTONS_2_Y-BUTTONS_2_R-1, BUTTONS_2_X+BUTTONS_2_WIDTH+(i*BUTTONS_2_STEP), BUTTONS_2_Y-Slope-1, Red);
sulymarco 0:13be39911caf 610
sulymarco 0:13be39911caf 611 Value = Value << 1;
sulymarco 0:13be39911caf 612 }
sulymarco 0:13be39911caf 613 }
sulymarco 0:13be39911caf 614