Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed EasyCAT_lib
main.cpp@2:1e780f9c21b1, 2021-10-04 (annotated)
- Committer:
- neeyoo
- Date:
- Mon Oct 04 15:16:46 2021 +0000
- Revision:
- 2:1e780f9c21b1
- Parent:
- 1:1e7996e6c4a7
for motion;
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| EasyCAT | 0:31a727b65397 | 1 | //******************************************************************************************** |
| EasyCAT | 0:31a727b65397 | 2 | // * |
| EasyCAT | 0:31a727b65397 | 3 | // AB&T Tecnologie Informatiche - Ivrea Italy * |
| EasyCAT | 0:31a727b65397 | 4 | // http://www.bausano.net * |
| EasyCAT | 0:31a727b65397 | 5 | // https://www.ethercat.org/en/products/791FFAA126AD43859920EA64384AD4FD.htm * |
| EasyCAT | 0:31a727b65397 | 6 | // * |
| EasyCAT | 0:31a727b65397 | 7 | //******************************************************************************************** |
| EasyCAT | 0:31a727b65397 | 8 | // * |
| EasyCAT | 0:31a727b65397 | 9 | // This software is distributed as an example, in the hope that it could be useful, * |
| EasyCAT | 0:31a727b65397 | 10 | // WITHOUT ANY WARRANTY, even the implied warranty of FITNESS FOR A PARTICULAR PURPOSE * |
| EasyCAT | 0:31a727b65397 | 11 | // * |
| EasyCAT | 0:31a727b65397 | 12 | //******************************************************************************************** |
| EasyCAT | 0:31a727b65397 | 13 | |
| EasyCAT | 0:31a727b65397 | 14 | |
| EasyCAT | 1:1e7996e6c4a7 | 15 | // revision 2 - moved "DigitalOut Led(LED1)" |
| EasyCAT | 1:1e7996e6c4a7 | 16 | |
| EasyCAT | 1:1e7996e6c4a7 | 17 | |
| EasyCAT | 1:1e7996e6c4a7 | 18 | |
| EasyCAT | 0:31a727b65397 | 19 | //----- EasyCAT shield application basic example for mbed boards 170912 ---------------------- |
| EasyCAT | 0:31a727b65397 | 20 | //----- Derived from the example project TestEasyCAT.ino for the AB&T EasyCAT Arduino shield |
| EasyCAT | 0:31a727b65397 | 21 | |
| EasyCAT | 0:31a727b65397 | 22 | //----- Tested with the STM32 NUCLEO-F767ZI board -------------------------------------------- |
| EasyCAT | 0:31a727b65397 | 23 | |
| EasyCAT | 0:31a727b65397 | 24 | |
| EasyCAT | 0:31a727b65397 | 25 | |
| EasyCAT | 0:31a727b65397 | 26 | #include "mbed.h" |
| EasyCAT | 0:31a727b65397 | 27 | #include "EasyCAT.h" // EasyCAT library to interface the LAN9252 |
| neeyoo | 2:1e780f9c21b1 | 28 | |
| EasyCAT | 0:31a727b65397 | 29 | |
| EasyCAT | 0:31a727b65397 | 30 | void Application (void); |
| EasyCAT | 0:31a727b65397 | 31 | |
| EasyCAT | 0:31a727b65397 | 32 | |
| neeyoo | 2:1e780f9c21b1 | 33 | EasyCAT EASYCAT(DC_SYNC); // EasyCAT istantiation |
| EasyCAT | 0:31a727b65397 | 34 | |
| EasyCAT | 0:31a727b65397 | 35 | // The constructor allow us to choose the pin used for the EasyCAT SPI chip select |
| EasyCAT | 0:31a727b65397 | 36 | // Without any parameter pin 9 will be used |
| EasyCAT | 0:31a727b65397 | 37 | |
| EasyCAT | 0:31a727b65397 | 38 | // for EasyCAT board REV_A we can choose between: |
| EasyCAT | 0:31a727b65397 | 39 | // 8, 9, 10 |
| EasyCAT | 0:31a727b65397 | 40 | // |
| EasyCAT | 0:31a727b65397 | 41 | // for EasyCAT board REV_B we can choose between: |
| EasyCAT | 0:31a727b65397 | 42 | // 8, 9, 10, A5, 6, 7 |
| EasyCAT | 0:31a727b65397 | 43 | |
| EasyCAT | 0:31a727b65397 | 44 | // example: |
| EasyCAT | 0:31a727b65397 | 45 | //EasyCAT EASYCAT(8); // pin 8 will be used as SPI chip select |
| EasyCAT | 0:31a727b65397 | 46 | |
| EasyCAT | 0:31a727b65397 | 47 | |
| EasyCAT | 0:31a727b65397 | 48 | // The chip select chosen by the firmware must match the setting on the board |
| EasyCAT | 0:31a727b65397 | 49 | |
| EasyCAT | 0:31a727b65397 | 50 | // On board REV_A the chip select is set soldering |
| EasyCAT | 0:31a727b65397 | 51 | // a 0 ohm resistor in the appropriate position |
| EasyCAT | 0:31a727b65397 | 52 | |
| EasyCAT | 0:31a727b65397 | 53 | // On board REV_B the chip select is set |
| EasyCAT | 0:31a727b65397 | 54 | // througt a bank of jumpers |
| EasyCAT | 0:31a727b65397 | 55 | |
| EasyCAT | 0:31a727b65397 | 56 | |
| EasyCAT | 0:31a727b65397 | 57 | |
| neeyoo | 2:1e780f9c21b1 | 58 | InterruptIn event(D2); // This pin receive the SM interrupt |
| neeyoo | 2:1e780f9c21b1 | 59 | // generated by the LAN9252 |
| neeyoo | 2:1e780f9c21b1 | 60 | |
| neeyoo | 2:1e780f9c21b1 | 61 | |
| neeyoo | 2:1e780f9c21b1 | 62 | void Campionatura(void); |
| EasyCAT | 0:31a727b65397 | 63 | |
| EasyCAT | 0:31a727b65397 | 64 | |
| EasyCAT | 0:31a727b65397 | 65 | |
| EasyCAT | 0:31a727b65397 | 66 | //---- global variables --------------------------------------------------------------------------- |
| EasyCAT | 0:31a727b65397 | 67 | unsigned long Millis = 0; |
| EasyCAT | 0:31a727b65397 | 68 | unsigned long PreviousCycle = 0; |
| neeyoo | 2:1e780f9c21b1 | 69 | int32_t data_test = 0; |
| neeyoo | 2:1e780f9c21b1 | 70 | int32_t j = 0; |
| EasyCAT | 0:31a727b65397 | 71 | |
| EasyCAT | 0:31a727b65397 | 72 | |
| EasyCAT | 0:31a727b65397 | 73 | //---- declarations for Arduino "millis()" emulation ----------------------- |
| EasyCAT | 0:31a727b65397 | 74 | |
| EasyCAT | 0:31a727b65397 | 75 | static Ticker uS_Tick; |
| EasyCAT | 0:31a727b65397 | 76 | static volatile uint32_t MillisVal = 0; |
| EasyCAT | 0:31a727b65397 | 77 | |
| EasyCAT | 0:31a727b65397 | 78 | void InitMillis(void); |
| EasyCAT | 0:31a727b65397 | 79 | void mS_Tick(void); |
| EasyCAT | 0:31a727b65397 | 80 | |
| EasyCAT | 0:31a727b65397 | 81 | inline static uint32_t millis (void) |
| EasyCAT | 0:31a727b65397 | 82 | { |
| EasyCAT | 0:31a727b65397 | 83 | return MillisVal; |
| EasyCAT | 0:31a727b65397 | 84 | }; |
| EasyCAT | 0:31a727b65397 | 85 | |
| EasyCAT | 0:31a727b65397 | 86 | |
| EasyCAT | 0:31a727b65397 | 87 | |
| EasyCAT | 0:31a727b65397 | 88 | //--------------------------------------------------------------------------------------------- |
| EasyCAT | 0:31a727b65397 | 89 | |
| EasyCAT | 0:31a727b65397 | 90 | int main(void) |
| EasyCAT | 0:31a727b65397 | 91 | { |
| EasyCAT | 0:31a727b65397 | 92 | |
| EasyCAT | 0:31a727b65397 | 93 | printf ("\nEasyCAT - Generic EtherCAT slave\n"); // print the banner |
| EasyCAT | 0:31a727b65397 | 94 | |
| EasyCAT | 0:31a727b65397 | 95 | InitMillis(); // init Arduino "millis()" emulation |
| EasyCAT | 0:31a727b65397 | 96 | |
| EasyCAT | 0:31a727b65397 | 97 | //---- initialize the EasyCAT board ----- |
| EasyCAT | 0:31a727b65397 | 98 | |
| EasyCAT | 0:31a727b65397 | 99 | if (EASYCAT.Init() == true) // initialization |
| EasyCAT | 0:31a727b65397 | 100 | { // succesfully completed |
| EasyCAT | 0:31a727b65397 | 101 | printf ("initialized\n"); // |
| EasyCAT | 0:31a727b65397 | 102 | } |
| EasyCAT | 0:31a727b65397 | 103 | |
| EasyCAT | 0:31a727b65397 | 104 | else // initialization failed |
| EasyCAT | 0:31a727b65397 | 105 | { // the EasyCAT board was not recognized |
| EasyCAT | 0:31a727b65397 | 106 | printf ("initialization failed\n"); // |
| EasyCAT | 0:31a727b65397 | 107 | // The most common reason is that the SPI |
| EasyCAT | 0:31a727b65397 | 108 | // chip select choosen on the board doesn't |
| EasyCAT | 0:31a727b65397 | 109 | // match the one choosen by the firmware |
| EasyCAT | 1:1e7996e6c4a7 | 110 | |
| EasyCAT | 1:1e7996e6c4a7 | 111 | DigitalOut Led(LED1); // |
| EasyCAT | 0:31a727b65397 | 112 | |
| EasyCAT | 0:31a727b65397 | 113 | while (1) // stay in loop for ever |
| EasyCAT | 0:31a727b65397 | 114 | { // with the led blinking |
| EasyCAT | 0:31a727b65397 | 115 | Led = 1; // |
| EasyCAT | 0:31a727b65397 | 116 | wait_ms(125); // |
| EasyCAT | 0:31a727b65397 | 117 | Led = 0; // |
| EasyCAT | 0:31a727b65397 | 118 | wait_ms(125); // |
| EasyCAT | 0:31a727b65397 | 119 | } // |
| neeyoo | 2:1e780f9c21b1 | 120 | } |
| neeyoo | 2:1e780f9c21b1 | 121 | |
| neeyoo | 2:1e780f9c21b1 | 122 | |
| neeyoo | 2:1e780f9c21b1 | 123 | event.fall(&Campionatura); |
| EasyCAT | 0:31a727b65397 | 124 | |
| EasyCAT | 0:31a727b65397 | 125 | |
| neeyoo | 2:1e780f9c21b1 | 126 | while (1) |
| EasyCAT | 0:31a727b65397 | 127 | { |
| neeyoo | 2:1e780f9c21b1 | 128 | // everything happens in interrupt |
| EasyCAT | 0:31a727b65397 | 129 | } |
| EasyCAT | 0:31a727b65397 | 130 | } |
| EasyCAT | 0:31a727b65397 | 131 | |
| neeyoo | 2:1e780f9c21b1 | 132 | //---- interrupt service -------------------------------------------------------------------------------- |
| neeyoo | 2:1e780f9c21b1 | 133 | |
| neeyoo | 2:1e780f9c21b1 | 134 | // The EasyCAT task and the User |
| neeyoo | 2:1e780f9c21b1 | 135 | // Application run in interrupt |
| EasyCAT | 0:31a727b65397 | 136 | // |
| neeyoo | 2:1e780f9c21b1 | 137 | // Take care that the EtherCAT task + User Application |
| neeyoo | 2:1e780f9c21b1 | 138 | // time is less that the master cycle time |
| neeyoo | 2:1e780f9c21b1 | 139 | |
| neeyoo | 2:1e780f9c21b1 | 140 | void Campionatura() // |
| neeyoo | 2:1e780f9c21b1 | 141 | { // |
| neeyoo | 2:1e780f9c21b1 | 142 | EASYCAT.MainTask(); // EtherCAT task |
| EasyCAT | 0:31a727b65397 | 143 | // |
| neeyoo | 2:1e780f9c21b1 | 144 | Application(); |
| neeyoo | 2:1e780f9c21b1 | 145 | printf("%d \n", millis() - PreviousCycle); |
| neeyoo | 2:1e780f9c21b1 | 146 | PreviousCycle = millis(); // User application |
| neeyoo | 2:1e780f9c21b1 | 147 | |
| neeyoo | 2:1e780f9c21b1 | 148 | } |
| neeyoo | 2:1e780f9c21b1 | 149 | |
| neeyoo | 2:1e780f9c21b1 | 150 | // User application |
| EasyCAT | 0:31a727b65397 | 151 | |
| neeyoo | 2:1e780f9c21b1 | 152 | void Application (void) |
| neeyoo | 2:1e780f9c21b1 | 153 | { |
| neeyoo | 2:1e780f9c21b1 | 154 | EASYCAT.BufferIn.Cust.centerXL = data_test; |
| neeyoo | 2:1e780f9c21b1 | 155 | EASYCAT.BufferIn.Cust.centerYL = data_test; |
| neeyoo | 2:1e780f9c21b1 | 156 | EASYCAT.BufferIn.Cust.forceL = data_test; |
| neeyoo | 2:1e780f9c21b1 | 157 | EASYCAT.BufferIn.Cust.yawL = data_test; |
| neeyoo | 2:1e780f9c21b1 | 158 | EASYCAT.BufferIn.Cust.pitchL = data_test; |
| neeyoo | 2:1e780f9c21b1 | 159 | EASYCAT.BufferIn.Cust.rollL = data_test; |
| neeyoo | 2:1e780f9c21b1 | 160 | EASYCAT.BufferIn.Cust.centerXR = data_test; |
| neeyoo | 2:1e780f9c21b1 | 161 | EASYCAT.BufferIn.Cust.centerYR = data_test; |
| neeyoo | 2:1e780f9c21b1 | 162 | EASYCAT.BufferIn.Cust.forceR = data_test; |
| neeyoo | 2:1e780f9c21b1 | 163 | EASYCAT.BufferIn.Cust.yawR = data_test; |
| neeyoo | 2:1e780f9c21b1 | 164 | EASYCAT.BufferIn.Cust.pitchR = data_test; |
| neeyoo | 2:1e780f9c21b1 | 165 | EASYCAT.BufferIn.Cust.rollR = data_test; |
| neeyoo | 2:1e780f9c21b1 | 166 | EASYCAT.BufferIn.Cust.yawH = data_test; |
| neeyoo | 2:1e780f9c21b1 | 167 | EASYCAT.BufferIn.Cust.pitchH = data_test; |
| neeyoo | 2:1e780f9c21b1 | 168 | EASYCAT.BufferIn.Cust.rollH = data_test; |
| neeyoo | 2:1e780f9c21b1 | 169 | |
| neeyoo | 2:1e780f9c21b1 | 170 | data_test++; |
| neeyoo | 2:1e780f9c21b1 | 171 | |
| neeyoo | 2:1e780f9c21b1 | 172 | } |
| EasyCAT | 0:31a727b65397 | 173 | |
| EasyCAT | 0:31a727b65397 | 174 | |
| EasyCAT | 0:31a727b65397 | 175 | //--- functions for Arduino "millis()" emulation ------------------------------------- |
| EasyCAT | 0:31a727b65397 | 176 | |
| EasyCAT | 0:31a727b65397 | 177 | |
| EasyCAT | 0:31a727b65397 | 178 | void InitMillis(void) |
| EasyCAT | 0:31a727b65397 | 179 | { |
| EasyCAT | 0:31a727b65397 | 180 | uS_Tick.attach (&mS_Tick, 0.001); |
| EasyCAT | 0:31a727b65397 | 181 | } |
| EasyCAT | 0:31a727b65397 | 182 | |
| EasyCAT | 0:31a727b65397 | 183 | void mS_Tick(void) |
| EasyCAT | 0:31a727b65397 | 184 | { |
| EasyCAT | 0:31a727b65397 | 185 | MillisVal++; |
| neeyoo | 2:1e780f9c21b1 | 186 | } |