Impedance Fast Circuitry Software

Dependencies:   mbed-dsp mbed

Fork of DSP_200kHz by Mazzeo Research Group

Committer:
timmey9
Date:
Fri Jan 09 20:07:09 2015 +0000
Revision:
33:9806eb964362
Parent:
32:bf1e079a2ee3
Child:
34:44cc9b76a507
cases 1,2,3 altered to test reset of motors

Who changed what in which revision?

UserRevisionLine numberNew contents of line
timmey9 20:f533b3c9296f 1 // Server code
donatien 0:bb128f0e952f 2 #include "mbed.h"
timmey9 24:a5891669afc5 3 #include <stdio.h>
timmey9 22:523e316cbe70 4
timmey9 22:523e316cbe70 5 // Ethernet
donatien 0:bb128f0e952f 6 #include "EthernetInterface.h"
timmey9 17:2f978f823020 7 #include "NetworkAPI/buffer.hpp"
timmey9 17:2f978f823020 8 #include "NetworkAPI/select.hpp"
timmey9 17:2f978f823020 9 #include "NetworkAPI/ip/address.hpp"
timmey9 17:2f978f823020 10 #include "NetworkAPI/tcp/socket.hpp"
timmey9 18:b17ddeeb1c09 11
timmey9 22:523e316cbe70 12 // Angle encoder and motor control
timmey9 22:523e316cbe70 13 #include "AngleEncoder.h"
timmey9 22:523e316cbe70 14 #include "MotorControl.h"
timmey9 22:523e316cbe70 15
timmey9 22:523e316cbe70 16 // Analog sampling
timmey9 22:523e316cbe70 17 #include "PeripheralNames.h"
timmey9 22:523e316cbe70 18 #include "PeripheralPins.h"
timmey9 22:523e316cbe70 19 #include "fsl_adc_hal.h"
timmey9 22:523e316cbe70 20 #include "fsl_clock_manager.h"
timmey9 22:523e316cbe70 21 #include "fsl_dspi_hal.h"
timmey9 22:523e316cbe70 22 #include "AngleEncoder.h"
timmey9 22:523e316cbe70 23
timmey9 22:523e316cbe70 24 /*****************************************
timmey9 22:523e316cbe70 25 *
timmey9 22:523e316cbe70 26 * Configuration
timmey9 22:523e316cbe70 27 *
timmey9 22:523e316cbe70 28 * Take the time to set these constants
timmey9 22:523e316cbe70 29 *
timmey9 22:523e316cbe70 30 *****************************************/
timmey9 31:09e7a8b3c7e7 31 #define MALLET 5 // set mallet to a value between 1-7
timmey9 21:1fb5023b72af 32 #define STATIC 1 // set STATIC to 1 for static ip, set STATIC to 0 for dynamic
timmey9 21:1fb5023b72af 33 #define PORT 22 // set to a random port number. All the mallets can use the same port number.
timmey9 21:1fb5023b72af 34 #define MAX_CLIENTS 2 // set the max number of clients to at least 2 (first client is MATLAB, second is the distance unit)
timmey9 22:523e316cbe70 35 #define INVERT_ANGLE 0 // inverts whether the angle encoder counts up or down
timmey9 22:523e316cbe70 36
timmey9 28:4a833d59897b 37
timmey9 22:523e316cbe70 38 // Analog sampling
timmey9 22:523e316cbe70 39 #define MAX_FADC 6000000
timmey9 29:e6309316c35d 40 #define SAMPLING_RATE 10 // In microseconds, so 10 us will be a sampling rate of 100 kHz
timmey9 32:bf1e079a2ee3 41 #define TOTAL_SAMPLES 10000 // originally 30000 for 0.3 ms of sampling.
timmey9 22:523e316cbe70 42
timmey9 22:523e316cbe70 43 #define LAST_SAMPLE_INDEX (TOTAL_SAMPLES-1) // If sampling time is 25 us, then 2000 corresponds to 50 ms
timmey9 22:523e316cbe70 44 #define FIRST_SAMPLE_INDEX 0
timmey9 22:523e316cbe70 45 #define BEGIN_SAMPLING 0xFFFFFFFF
timmey9 22:523e316cbe70 46 #define WAITING_TO_BEGIN (BEGIN_SAMPLING-1)
timmey9 22:523e316cbe70 47
timmey9 22:523e316cbe70 48 #define CHANNEL_STORAGE_OFFSET 16 // For storing the 16 bits and the 16 bits in a single 32 bit array
timmey9 22:523e316cbe70 49 #define PERIOD 3000 // make sure PERIOD >= ON_OFF_TIME
timmey9 22:523e316cbe70 50 #define ON_OFF_TIME 300 // time it takes for relay to turn on
timmey9 22:523e316cbe70 51
timmey9 22:523e316cbe70 52
timmey9 22:523e316cbe70 53 // Ethernet
timmey9 22:523e316cbe70 54 #define GATEWAY "169.254.225.1"
timmey9 22:523e316cbe70 55 #define MASK "255.255.0.0"
timmey9 22:523e316cbe70 56
timmey9 22:523e316cbe70 57 // used for assign different mallets their ip addresses
timmey9 21:1fb5023b72af 58 #if MALLET == 1
timmey9 22:523e316cbe70 59 #define IP "169.254.225.206"
timmey9 21:1fb5023b72af 60 #define NAME "Mallet1\n\r"
timmey9 21:1fb5023b72af 61
timmey9 21:1fb5023b72af 62 #elif MALLET == 2
timmey9 22:523e316cbe70 63 #define IP "169.254.225.207"
timmey9 21:1fb5023b72af 64 #define NAME "Mallet2\n\r"
timmey9 21:1fb5023b72af 65
timmey9 21:1fb5023b72af 66 #elif MALLET == 3
timmey9 22:523e316cbe70 67 #define IP "169.254.225.208"
timmey9 21:1fb5023b72af 68 #define NAME "Mallet3\n\r"
timmey9 21:1fb5023b72af 69
timmey9 21:1fb5023b72af 70 #elif MALLET == 4
timmey9 22:523e316cbe70 71 #define IP "169.254.225.209"
timmey9 21:1fb5023b72af 72 #define NAME "Mallet4\n\r"
timmey9 21:1fb5023b72af 73
timmey9 21:1fb5023b72af 74 #elif MALLET == 5
timmey9 22:523e316cbe70 75 #define IP "169.254.225.210"
timmey9 21:1fb5023b72af 76 #define NAME "Mallet5\n\r"
timmey9 21:1fb5023b72af 77
timmey9 21:1fb5023b72af 78 #elif MALLET == 6
timmey9 22:523e316cbe70 79 #define IP "169.254.225.211"
timmey9 21:1fb5023b72af 80 #define NAME "Mallet6\n\r"
timmey9 21:1fb5023b72af 81
timmey9 21:1fb5023b72af 82 #elif MALLET == 7
timmey9 22:523e316cbe70 83 #define IP "169.254.225.212"
timmey9 21:1fb5023b72af 84 #define NAME "Mallet7\n\r"
timmey9 21:1fb5023b72af 85
timmey9 20:f533b3c9296f 86 #endif
timmey9 20:f533b3c9296f 87
timmey9 21:1fb5023b72af 88
timmey9 22:523e316cbe70 89 // for debug purposes
timmey9 18:b17ddeeb1c09 90 Serial pc(USBTX, USBRX);
timmey9 18:b17ddeeb1c09 91 DigitalOut led_red(LED_RED);
timmey9 18:b17ddeeb1c09 92 DigitalOut led_green(LED_GREEN);
timmey9 18:b17ddeeb1c09 93 DigitalOut led_blue(LED_BLUE);
timmey9 18:b17ddeeb1c09 94
timmey9 22:523e316cbe70 95 // motor control and angle encoder
timmey9 31:09e7a8b3c7e7 96 PwmOut motorA(PTC2);
timmey9 31:09e7a8b3c7e7 97 PwmOut motorB(PTA2);
timmey9 31:09e7a8b3c7e7 98 //MotorControl motor(PTC2, PTA2, PERIOD, ON_OFF_TIME); // forward, backward, period, safetyPeriod
timmey9 22:523e316cbe70 99 AngleEncoder angle_encoder(PTD2, PTD3, PTD1, PTD0, 8, 0, 1000000); // mosi, miso, sclk, cs, bit_width, mode, hz
timmey9 22:523e316cbe70 100 DigitalIn AMT20_A(PTC0); // input for quadrature encoding from angle encoder
timmey9 22:523e316cbe70 101 DigitalIn AMT20_B(PTC1); // input for quadrature encoding from angle encoder
timmey9 22:523e316cbe70 102
timmey9 22:523e316cbe70 103 // Analog sampling
timmey9 28:4a833d59897b 104 //Ticker Sampler;
timmey9 23:9e5141647775 105 //Timer timer;
timmey9 23:9e5141647775 106 //Timer timeStamp;
timmey9 22:523e316cbe70 107 AnalogIn A0_pin(A0);
timmey9 22:523e316cbe70 108 AnalogIn A2_pin(A2);
timmey9 22:523e316cbe70 109
timmey9 22:523e316cbe70 110 //DigitalIn SW3_switch(PTA4);
timmey9 22:523e316cbe70 111 //DigitalIn SW2_switch(PTC6);
timmey9 28:4a833d59897b 112 DigitalOut TotalInd(PTC4);
timmey9 28:4a833d59897b 113 DigitalOut SampleInd(PTC5); // originally PTD0 but needed for CS for spi
timmey9 22:523e316cbe70 114
timmey9 22:523e316cbe70 115 uint32_t current_sample_index = WAITING_TO_BEGIN;
timmey9 25:abbc19af13f9 116 uint16_t sample_array1[TOTAL_SAMPLES];
timmey9 25:abbc19af13f9 117 uint16_t sample_array2[TOTAL_SAMPLES];
timmey9 22:523e316cbe70 118 uint16_t angle_array[TOTAL_SAMPLES];
timmey9 22:523e316cbe70 119
timmey9 22:523e316cbe70 120 // Declaration of functions
timmey9 22:523e316cbe70 121 void analog_initialization(PinName pin);
timmey9 22:523e316cbe70 122 void output_data(uint32_t iteration_number);
timmey9 22:523e316cbe70 123 void timed_sampling();
timmey9 22:523e316cbe70 124
timmey9 32:bf1e079a2ee3 125
timmey9 33:9806eb964362 126 int motorVal = 0;
timmey9 32:bf1e079a2ee3 127 void releaseMallet()
timmey9 32:bf1e079a2ee3 128 {
timmey9 32:bf1e079a2ee3 129 motorA = 0;
timmey9 32:bf1e079a2ee3 130 motorB = 1;
timmey9 32:bf1e079a2ee3 131 wait_ms(10);
timmey9 32:bf1e079a2ee3 132 motorB = 0;
timmey9 32:bf1e079a2ee3 133 wait_ms(75);
timmey9 32:bf1e079a2ee3 134
timmey9 32:bf1e079a2ee3 135 motorA = 0.7;
timmey9 32:bf1e079a2ee3 136 wait_ms(8);
timmey9 32:bf1e079a2ee3 137 motorA = 0;
timmey9 32:bf1e079a2ee3 138 }
timmey9 32:bf1e079a2ee3 139
timmey9 32:bf1e079a2ee3 140 void resetMallet()
timmey9 32:bf1e079a2ee3 141 {
timmey9 32:bf1e079a2ee3 142 motorA = 0;
timmey9 32:bf1e079a2ee3 143 motorB = 1;
timmey9 32:bf1e079a2ee3 144 wait_ms(153);
timmey9 32:bf1e079a2ee3 145 motorB = 0;
timmey9 32:bf1e079a2ee3 146 }
timmey9 32:bf1e079a2ee3 147
timmey9 32:bf1e079a2ee3 148
timmey9 22:523e316cbe70 149 // Important globabl variables necessary for the sampling every interval
timmey9 22:523e316cbe70 150 int rotary_count = 0;
timmey9 22:523e316cbe70 151 uint32_t last_AMT20_AB_read = 0;
timmey9 23:9e5141647775 152
timmey9 22:523e316cbe70 153 using namespace std;
timmey9 17:2f978f823020 154
emilmont 7:65188f4a8c25 155 int main() {
timmey9 25:abbc19af13f9 156 //for(int i = 0; i < TOTAL_SAMPLES; i++) {sample_array[i] = i;}
timmey9 28:4a833d59897b 157 TotalInd = 0;
timmey9 28:4a833d59897b 158 SampleInd = 0;
timmey9 22:523e316cbe70 159 led_blue = 1;
timmey9 30:6a4ef939a93e 160 led_green = 0;
timmey9 18:b17ddeeb1c09 161 led_red = 1;
timmey9 31:09e7a8b3c7e7 162 motorA = 0;
timmey9 31:09e7a8b3c7e7 163 motorB = 0;
timmey9 31:09e7a8b3c7e7 164 motorA.period_ms(8);
timmey9 31:09e7a8b3c7e7 165 motorB.period_ms(8);
timmey9 18:b17ddeeb1c09 166 pc.baud(230400);
timmey9 24:a5891669afc5 167 pc.printf("Starting %s\r\n",NAME);
timmey9 23:9e5141647775 168
timmey9 31:09e7a8b3c7e7 169
timmey9 31:09e7a8b3c7e7 170
timmey9 28:4a833d59897b 171 for(int i = 0; i < 86; i++)
timmey9 28:4a833d59897b 172 {
timmey9 28:4a833d59897b 173 if(NVIC_GetPriority((IRQn_Type) i) == 0) NVIC_SetPriority((IRQn_Type) i, 2);
timmey9 28:4a833d59897b 174 }
timmey9 28:4a833d59897b 175
timmey9 28:4a833d59897b 176 NVIC_SetPriority(PIT3_IRQn,0);
timmey9 28:4a833d59897b 177
timmey9 28:4a833d59897b 178 NVIC_SetPriority(ADC1_IRQn,0);
timmey9 28:4a833d59897b 179 NVIC_SetPriority(ADC0_IRQn,0);
timmey9 28:4a833d59897b 180 NVIC_SetPriority(ENET_1588_Timer_IRQn,0);
timmey9 28:4a833d59897b 181 NVIC_SetPriority(ENET_Transmit_IRQn,0);
timmey9 28:4a833d59897b 182 NVIC_SetPriority(ENET_Receive_IRQn,0);
timmey9 28:4a833d59897b 183 NVIC_SetPriority(ENET_Error_IRQn,0);
timmey9 28:4a833d59897b 184
timmey9 28:4a833d59897b 185
timmey9 27:8c2b30c855d1 186 // The ethernet setup must be within the first few lines of code, otherwise the program hangs
timmey9 17:2f978f823020 187 EthernetInterface interface;
timmey9 21:1fb5023b72af 188 #if STATIC == 1
timmey9 20:f533b3c9296f 189 interface.init(IP, MASK, GATEWAY);
timmey9 20:f533b3c9296f 190 #else
timmey9 17:2f978f823020 191 interface.init();
timmey9 20:f533b3c9296f 192 #endif
timmey9 17:2f978f823020 193 interface.connect();
timmey9 20:f533b3c9296f 194 pc.printf("IP Address is: %s\n\r", interface.getIPAddress());
timmey9 20:f533b3c9296f 195 pc.printf("Network Mask is: %s\n\r", interface.getNetworkMask());
timmey9 20:f533b3c9296f 196 pc.printf("MAC address is: %s\n\r", interface.getMACAddress());
timmey9 20:f533b3c9296f 197 pc.printf("Gateway is: %s\n\r", interface.getGateway());
timmey9 21:1fb5023b72af 198 pc.printf("Port is: %i\n\r", PORT);
timmey9 20:f533b3c9296f 199
timmey9 28:4a833d59897b 200
timmey9 22:523e316cbe70 201 // ethernet setup failed for some reason. Flash yellow light then uC resets itself
timmey9 23:9e5141647775 202 if(interface.getIPAddress() == 0)
timmey9 22:523e316cbe70 203 {
timmey9 22:523e316cbe70 204 for(int i = 0; i < 5; i++)
timmey9 22:523e316cbe70 205 {
timmey9 22:523e316cbe70 206 led_red = 0;
timmey9 22:523e316cbe70 207 led_green = 0;
timmey9 22:523e316cbe70 208 wait_ms(500);
timmey9 22:523e316cbe70 209 led_red = 1;
timmey9 22:523e316cbe70 210 led_green = 1;
timmey9 22:523e316cbe70 211 wait_ms(1000);
timmey9 22:523e316cbe70 212 }
timmey9 22:523e316cbe70 213 NVIC_SystemReset();
timmey9 23:9e5141647775 214 }
timmey9 22:523e316cbe70 215
timmey9 27:8c2b30c855d1 216
timmey9 27:8c2b30c855d1 217 analog_initialization(A0);
timmey9 27:8c2b30c855d1 218 analog_initialization(A2);
timmey9 27:8c2b30c855d1 219
timmey9 27:8c2b30c855d1 220
timmey9 27:8c2b30c855d1 221 // Start the sampling loop
timmey9 27:8c2b30c855d1 222 current_sample_index = WAITING_TO_BEGIN;
timmey9 28:4a833d59897b 223 //Sampler.attach_us(&timed_sampling, SAMPLING_RATE);
timmey9 27:8c2b30c855d1 224
timmey9 28:4a833d59897b 225 //NVIC_SetPriority(TIMER3_IRQn,0);
timmey9 28:4a833d59897b 226 //pc.printf("Ticker IRQ: %i\r\n", Sampler.irq());
timmey9 27:8c2b30c855d1 227
timmey9 27:8c2b30c855d1 228
timmey9 22:523e316cbe70 229 network::Select select;
timmey9 22:523e316cbe70 230 network::tcp::Socket server;
timmey9 22:523e316cbe70 231 network::tcp::Socket client[MAX_CLIENTS];
timmey9 22:523e316cbe70 232 network::tcp::Socket *socket = NULL;
timmey9 17:2f978f823020 233
timmey9 17:2f978f823020 234 int result = 0;
timmey9 17:2f978f823020 235 int index = 0;
timmey9 17:2f978f823020 236
timmey9 27:8c2b30c855d1 237 network::Buffer buffer(50);
timmey9 21:1fb5023b72af 238 std::string message(NAME);
timmey9 17:2f978f823020 239
timmey9 21:1fb5023b72af 240 // Configure the server socket (assume every thing works)
timmey9 17:2f978f823020 241 server.open();
timmey9 20:f533b3c9296f 242 server.bind(PORT);
timmey9 17:2f978f823020 243 server.listen(MAX_CLIENTS);
timmey9 27:8c2b30c855d1 244
timmey9 17:2f978f823020 245 // Add sockets to the select api
timmey9 22:523e316cbe70 246 select.set(&server, network::Select::Read);
timmey9 17:2f978f823020 247 for (index = 0; index < MAX_CLIENTS; index++) {
timmey9 22:523e316cbe70 248 select.set(&client[index], network::Select::Read);
timmey9 17:2f978f823020 249 }
timmey9 22:523e316cbe70 250
timmey9 22:523e316cbe70 251 led_red = 1;
timmey9 22:523e316cbe70 252 led_green = 1;
timmey9 22:523e316cbe70 253 led_blue = 0;
timmey9 23:9e5141647775 254 wait_ms(500);
timmey9 22:523e316cbe70 255 led_blue = 1;
timmey9 23:9e5141647775 256 wait_ms(200);
timmey9 23:9e5141647775 257 led_blue = 0;
timmey9 23:9e5141647775 258 wait_ms(500);
timmey9 23:9e5141647775 259 led_blue = 1;
timmey9 22:523e316cbe70 260
timmey9 28:4a833d59897b 261 NVIC_SetPriority(ENET_1588_Timer_IRQn,1);
timmey9 28:4a833d59897b 262 NVIC_SetPriority(ENET_Transmit_IRQn,1);
timmey9 28:4a833d59897b 263 NVIC_SetPriority(ENET_Receive_IRQn,1);
timmey9 28:4a833d59897b 264 NVIC_SetPriority(ENET_Error_IRQn,1);
timmey9 28:4a833d59897b 265
timmey9 28:4a833d59897b 266 //for(int i = 0; i < 86; i++) pc.printf("%i: %i\r\n", i, NVIC_GetPriority((IRQn_Type) i));
timmey9 28:4a833d59897b 267
timmey9 17:2f978f823020 268 do {
timmey9 17:2f978f823020 269 // Wait for activity
timmey9 17:2f978f823020 270 result = select.wait();
timmey9 17:2f978f823020 271 if (result < -1) {
timmey9 18:b17ddeeb1c09 272 pc.printf("Failed to select\n\r");
emilmont 7:65188f4a8c25 273 break;
timmey9 16:c3f922f61b8f 274 }
timmey9 17:2f978f823020 275
timmey9 17:2f978f823020 276 // Get the first socket
timmey9 22:523e316cbe70 277 socket = (network::tcp::Socket *)select.getReadable();
timmey9 17:2f978f823020 278
timmey9 22:523e316cbe70 279 for (; socket != NULL; socket = (network::tcp::Socket *)select.getReadable()) {
timmey9 17:2f978f823020 280 // Check if there was a connection request.
timmey9 17:2f978f823020 281 if (socket->getHandle() == server.getHandle()) {
timmey9 17:2f978f823020 282 // Find an unused client
timmey9 17:2f978f823020 283 for (index = 0; index < MAX_CLIENTS; index++) {
timmey9 17:2f978f823020 284 if (client[index].getStatus() == network::Socket::Closed) {
timmey9 17:2f978f823020 285 break;
timmey9 17:2f978f823020 286 }
timmey9 17:2f978f823020 287 }
timmey9 17:2f978f823020 288
timmey9 17:2f978f823020 289 // Maximum connections reached
timmey9 17:2f978f823020 290 if (index == MAX_CLIENTS) {
timmey9 18:b17ddeeb1c09 291 pc.printf("Maximum connections reached\n\r");
timmey9 21:1fb5023b72af 292 wait(1);
timmey9 17:2f978f823020 293 continue;
timmey9 17:2f978f823020 294 }
timmey9 20:f533b3c9296f 295
timmey9 17:2f978f823020 296 // Accept the client
timmey9 17:2f978f823020 297 socket->accept(client[index]);
timmey9 18:b17ddeeb1c09 298 pc.printf("Client connected %s:%d\n\r",
timmey9 17:2f978f823020 299 client[index].getRemoteEndpoint().getAddress().toString().c_str(),
timmey9 17:2f978f823020 300 client[index].getRemoteEndpoint().getPort());
timmey9 17:2f978f823020 301
timmey9 21:1fb5023b72af 302 // Send a nice message to the client (tell MATLAB your name
timmey9 17:2f978f823020 303 client[index].write((void *)message.data(), message.size());
timmey9 20:f533b3c9296f 304
timmey9 17:2f978f823020 305 continue;
timmey9 16:c3f922f61b8f 306 }
timmey9 20:f533b3c9296f 307
timmey9 17:2f978f823020 308 // It was not the server socket, so it must be a client talking to us.
timmey9 17:2f978f823020 309 switch (socket->read(buffer)) {
timmey9 17:2f978f823020 310 case 0:
timmey9 17:2f978f823020 311 // Remote end disconnected
timmey9 18:b17ddeeb1c09 312 pc.printf("Client disconnected %s:%d\n\r",
timmey9 17:2f978f823020 313 socket->getRemoteEndpoint().getAddress().toString().c_str(),
timmey9 17:2f978f823020 314 socket->getRemoteEndpoint().getPort());
timmey9 17:2f978f823020 315
timmey9 17:2f978f823020 316 // Close socket
timmey9 17:2f978f823020 317 socket->close();
timmey9 17:2f978f823020 318 break;
timmey9 20:f533b3c9296f 319
timmey9 17:2f978f823020 320 case -1:
timmey9 18:b17ddeeb1c09 321 pc.printf("Error while reading data from socket\n\r");
timmey9 17:2f978f823020 322 socket->close();
timmey9 17:2f978f823020 323 break;
timmey9 32:bf1e079a2ee3 324
timmey9 32:bf1e079a2ee3 325
timmey9 32:bf1e079a2ee3 326
timmey9 20:f533b3c9296f 327 //************* this is where data is printed to the screen
timmey9 17:2f978f823020 328 default:
timmey9 18:b17ddeeb1c09 329 pc.printf("Message from %s:%d\n\r",
timmey9 17:2f978f823020 330 socket->getRemoteEndpoint().getAddress().toString().c_str(),
timmey9 17:2f978f823020 331 socket->getRemoteEndpoint().getPort());
timmey9 17:2f978f823020 332
timmey9 21:1fb5023b72af 333 pc.printf("%s\n\r", (char *)buffer.data());
timmey9 21:1fb5023b72af 334
timmey9 21:1fb5023b72af 335 // read first character for command
timmey9 21:1fb5023b72af 336 char command[2];
timmey9 21:1fb5023b72af 337 buffer.read(command,2,0);
timmey9 21:1fb5023b72af 338 if(command[1] == ':') {
timmey9 21:1fb5023b72af 339 switch(command[0])
timmey9 21:1fb5023b72af 340 {
timmey9 21:1fb5023b72af 341 case 'b':
timmey9 21:1fb5023b72af 342 led_blue = !led_blue;
timmey9 21:1fb5023b72af 343 client[index].write((void *)"Blue LED\n",9);
timmey9 21:1fb5023b72af 344 break;
timmey9 25:abbc19af13f9 345
timmey9 21:1fb5023b72af 346 case 'r':
timmey9 21:1fb5023b72af 347 led_red = !led_red;
timmey9 21:1fb5023b72af 348 client[index].write((void *)"Red LED\n",8);
timmey9 21:1fb5023b72af 349 break;
timmey9 25:abbc19af13f9 350
timmey9 21:1fb5023b72af 351 case 'p':
timmey9 21:1fb5023b72af 352 led_green = !led_green;
timmey9 21:1fb5023b72af 353 client[index].write((void *)"Data\n",5);
timmey9 27:8c2b30c855d1 354 for(int i = 0; i < 99; i++) sample_array1[i] = i;
timmey9 27:8c2b30c855d1 355 client[index].write((void *)&sample_array1,2*99);
timmey9 24:a5891669afc5 356 break;
timmey9 28:4a833d59897b 357 case 't':
timmey9 28:4a833d59897b 358 {
timmey9 28:4a833d59897b 359 for(int i = 0; i < 86; i++) pc.printf("%i: %i\r\n", i, NVIC_GetPriority((IRQn_Type) i));
timmey9 32:bf1e079a2ee3 360 }
timmey9 28:4a833d59897b 361 break;
timmey9 25:abbc19af13f9 362
timmey9 33:9806eb964362 363 case '3': // run motor and sample
timmey9 25:abbc19af13f9 364 {
timmey9 33:9806eb964362 365 // set angle to 0
timmey9 33:9806eb964362 366 led_blue = 0;
timmey9 33:9806eb964362 367 while(!angle_encoder.set_zero()) {}
timmey9 33:9806eb964362 368 led_blue = 1;
timmey9 28:4a833d59897b 369
timmey9 33:9806eb964362 370 // release mallet
timmey9 33:9806eb964362 371 wait(1);
timmey9 33:9806eb964362 372
timmey9 26:a00bf9837e03 373
timmey9 32:bf1e079a2ee3 374 // reset mallet
timmey9 32:bf1e079a2ee3 375 motorA = 0;
timmey9 33:9806eb964362 376 int angleVar = 1;
timmey9 33:9806eb964362 377 int counter = 0;
timmey9 33:9806eb964362 378 while(angleVar)
timmey9 33:9806eb964362 379 {
timmey9 33:9806eb964362 380 counter++;
timmey9 33:9806eb964362 381 angleVar = angle_encoder.absolute_angle();
timmey9 33:9806eb964362 382 //angleVar -= motorVal;
timmey9 33:9806eb964362 383 if(angleVar == 0x00ff0000) {} // do nothing
timmey9 33:9806eb964362 384 else if(angleVar > 340) motorB.pulsewidth_us(8000); // max speed
timmey9 33:9806eb964362 385 else if(angleVar < motorVal) {
timmey9 33:9806eb964362 386 angleVar = 0; // exit loop
timmey9 33:9806eb964362 387 motorB.pulsewidth_us(0);} // min speed
timmey9 33:9806eb964362 388 else motorB.pulsewidth_us(angleVar*800/34);
timmey9 33:9806eb964362 389 }
timmey9 32:bf1e079a2ee3 390 motorB = 0;
timmey9 32:bf1e079a2ee3 391
timmey9 33:9806eb964362 392 char buf[29];
timmey9 33:9806eb964362 393 sprintf(buf,"MotorVal: %i\n",motorVal);
timmey9 33:9806eb964362 394 client[index].write((void *) buf,29);
timmey9 33:9806eb964362 395
timmey9 32:bf1e079a2ee3 396
timmey9 32:bf1e079a2ee3 397
timmey9 33:9806eb964362 398 /*
timmey9 33:9806eb964362 399 int tempDutyCycle = 2000;
timmey9 33:9806eb964362 400 for(int i = counter; i > 0; i--)
timmey9 33:9806eb964362 401 {
timmey9 33:9806eb964362 402 motorA.pulsewidth_us(tempDutyCycle);
timmey9 33:9806eb964362 403 }
timmey9 33:9806eb964362 404 */
timmey9 33:9806eb964362 405 // make sure motors are off
timmey9 33:9806eb964362 406 motorA = 0;
timmey9 33:9806eb964362 407 motorB = 0;
timmey9 33:9806eb964362 408 //wait_ms(138);
timmey9 33:9806eb964362 409 motorB = 0;
timmey9 25:abbc19af13f9 410 }
timmey9 24:a5891669afc5 411 break;
timmey9 33:9806eb964362 412 case '1':
timmey9 33:9806eb964362 413 {
timmey9 33:9806eb964362 414 // set angle to 0
timmey9 33:9806eb964362 415 led_blue = 0;
timmey9 33:9806eb964362 416 while(!angle_encoder.set_zero()) {}
timmey9 33:9806eb964362 417 led_blue = 1;
timmey9 25:abbc19af13f9 418
timmey9 33:9806eb964362 419 // release mallet
timmey9 33:9806eb964362 420 wait(1);
timmey9 33:9806eb964362 421
timmey9 33:9806eb964362 422
timmey9 33:9806eb964362 423 // reset mallet
timmey9 33:9806eb964362 424 motorA = 0;
timmey9 33:9806eb964362 425 int angleVar = 1;
timmey9 33:9806eb964362 426 int counter = 0;
timmey9 33:9806eb964362 427 while(angleVar)
timmey9 33:9806eb964362 428 {
timmey9 33:9806eb964362 429 counter++;
timmey9 33:9806eb964362 430 angleVar = angle_encoder.absolute_angle();
timmey9 33:9806eb964362 431 angleVar -= motorVal;
timmey9 33:9806eb964362 432 if(angleVar == 0x00ff0000) {} // do nothing
timmey9 33:9806eb964362 433 else if(angleVar > 340) motorB.pulsewidth_us(8000); // max speed
timmey9 33:9806eb964362 434 else if(angleVar < 43) {
timmey9 33:9806eb964362 435 angleVar = 0; // exit loop
timmey9 33:9806eb964362 436 motorB.pulsewidth_us(0);} // min speed
timmey9 33:9806eb964362 437 else motorB.pulsewidth_us(angleVar*800/34);
timmey9 33:9806eb964362 438 }
timmey9 33:9806eb964362 439 motorB = 0;
timmey9 33:9806eb964362 440
timmey9 33:9806eb964362 441 char buf[29];
timmey9 33:9806eb964362 442 sprintf(buf,"MotorVal: %i\n",motorVal);
timmey9 33:9806eb964362 443 client[index].write((void *) buf,29);
timmey9 33:9806eb964362 444
timmey9 33:9806eb964362 445
timmey9 33:9806eb964362 446
timmey9 33:9806eb964362 447 /*
timmey9 33:9806eb964362 448 int tempDutyCycle = 2000;
timmey9 33:9806eb964362 449 for(int i = counter; i > 0; i--)
timmey9 33:9806eb964362 450 {
timmey9 33:9806eb964362 451 motorA.pulsewidth_us(tempDutyCycle);
timmey9 33:9806eb964362 452 }
timmey9 33:9806eb964362 453 */
timmey9 33:9806eb964362 454 // make sure motors are off
timmey9 33:9806eb964362 455 motorA = 0;
timmey9 33:9806eb964362 456 motorB = 0;
timmey9 33:9806eb964362 457 }
timmey9 33:9806eb964362 458 break;
timmey9 33:9806eb964362 459 case '2':
timmey9 25:abbc19af13f9 460 {
timmey9 32:bf1e079a2ee3 461 // release mallet
timmey9 32:bf1e079a2ee3 462 motorA = 0;
timmey9 32:bf1e079a2ee3 463 motorB = 1;
timmey9 32:bf1e079a2ee3 464 wait_ms(10);
timmey9 32:bf1e079a2ee3 465 motorB = 0;
timmey9 32:bf1e079a2ee3 466 wait_ms(75);
timmey9 32:bf1e079a2ee3 467
timmey9 32:bf1e079a2ee3 468 motorA = 0.7;
timmey9 32:bf1e079a2ee3 469 wait_ms(8);
timmey9 32:bf1e079a2ee3 470 motorA = 0;
timmey9 32:bf1e079a2ee3 471
timmey9 32:bf1e079a2ee3 472 // wait for angle to set to zero
timmey9 32:bf1e079a2ee3 473 led_blue = 0;
timmey9 32:bf1e079a2ee3 474 while(!angle_encoder.set_zero()) {}
timmey9 32:bf1e079a2ee3 475 led_blue = 1;
timmey9 32:bf1e079a2ee3 476
timmey9 32:bf1e079a2ee3 477 // wait for mallet to drop to certin point before beginning to sample
timmey9 32:bf1e079a2ee3 478 led_red = 0;
timmey9 32:bf1e079a2ee3 479 int angleVal;
timmey9 32:bf1e079a2ee3 480 do {
timmey9 32:bf1e079a2ee3 481 angleVal = angle_encoder.absolute_angle();
timmey9 32:bf1e079a2ee3 482 }
timmey9 32:bf1e079a2ee3 483 while(!(angleVal > 150 && angleVal < 400));
timmey9 32:bf1e079a2ee3 484 led_red = 1;
timmey9 32:bf1e079a2ee3 485
timmey9 32:bf1e079a2ee3 486 //char buf[29];
timmey9 32:bf1e079a2ee3 487 //sprintf(buf,"AngleVal: %i\n",angleVal);
timmey9 32:bf1e079a2ee3 488 //client[index].write((void *) buf,29);
timmey9 32:bf1e079a2ee3 489
timmey9 32:bf1e079a2ee3 490
timmey9 32:bf1e079a2ee3 491 // begin sampling
timmey9 32:bf1e079a2ee3 492 BW_ADC_SC1n_ADCH(0, 0, kAdcChannel12); // This corresponds to starting an ADC conversion on channel 12 of ADC 0 - which is A0 (PTB2)
timmey9 32:bf1e079a2ee3 493 BW_ADC_SC1n_ADCH(1, 0, kAdcChannel14); // This corresponds to starting an ADC conversion on channel 14 of ADC 1 - which is A2 (PTB10)
timmey9 32:bf1e079a2ee3 494 client[index].write((void *)"Data\n",5);
timmey9 32:bf1e079a2ee3 495
timmey9 32:bf1e079a2ee3 496 TotalInd = 1;
timmey9 32:bf1e079a2ee3 497
timmey9 32:bf1e079a2ee3 498 uint32_t AMT20_AB;
timmey9 32:bf1e079a2ee3 499 rotary_count = 0;
timmey9 32:bf1e079a2ee3 500 __disable_irq();
timmey9 32:bf1e079a2ee3 501 SampleInd = 0;
timmey9 32:bf1e079a2ee3 502 for(int i = 0; i < TOTAL_SAMPLES; i++)
timmey9 32:bf1e079a2ee3 503 {
timmey9 32:bf1e079a2ee3 504 SampleInd = !SampleInd;
timmey9 32:bf1e079a2ee3 505 sample_array1[i] = adc_hal_get_conversion_value(0, 0);
timmey9 32:bf1e079a2ee3 506 sample_array2[i] = adc_hal_get_conversion_value(1, 0);
timmey9 32:bf1e079a2ee3 507 BW_ADC_SC1n_ADCH(0, 0, kAdcChannel12); // This corresponds to starting an ADC conversion on channel 12 of ADC 0 - which is A0 (PTB2)
timmey9 32:bf1e079a2ee3 508 BW_ADC_SC1n_ADCH(1, 0, kAdcChannel14); // This corresponds to starting an ADC conversion on channel 14 of ADC 1 - which is A2 (PTB10)
timmey9 32:bf1e079a2ee3 509
timmey9 32:bf1e079a2ee3 510 // The following updates the rotary counter for the AMT20 sensor
timmey9 32:bf1e079a2ee3 511 // Put A on PTC0
timmey9 32:bf1e079a2ee3 512 // Put B on PTC1
timmey9 32:bf1e079a2ee3 513 AMT20_AB = HW_GPIO_PDIR_RD(HW_PORTC) & 0x03;
timmey9 32:bf1e079a2ee3 514
timmey9 32:bf1e079a2ee3 515 if (AMT20_AB != last_AMT20_AB_read)
timmey9 32:bf1e079a2ee3 516 {
timmey9 32:bf1e079a2ee3 517 // change "INVERT_ANGLE" to change whether relative angle counts up or down.
timmey9 32:bf1e079a2ee3 518 if ((AMT20_AB >> 1)^(last_AMT20_AB_read) & 1U)
timmey9 32:bf1e079a2ee3 519 #if INVERT_ANGLE == 1
timmey9 32:bf1e079a2ee3 520 {rotary_count--;}
timmey9 32:bf1e079a2ee3 521 else
timmey9 32:bf1e079a2ee3 522 {rotary_count++;}
timmey9 32:bf1e079a2ee3 523 #else
timmey9 32:bf1e079a2ee3 524 {rotary_count++;}
timmey9 32:bf1e079a2ee3 525 else
timmey9 32:bf1e079a2ee3 526 {rotary_count--;}
timmey9 32:bf1e079a2ee3 527 #endif
timmey9 32:bf1e079a2ee3 528
timmey9 32:bf1e079a2ee3 529 last_AMT20_AB_read = AMT20_AB;
timmey9 32:bf1e079a2ee3 530 }
timmey9 32:bf1e079a2ee3 531 angle_array[i] = rotary_count;
timmey9 32:bf1e079a2ee3 532 wait_us(8);
timmey9 25:abbc19af13f9 533 }
timmey9 32:bf1e079a2ee3 534 __enable_irq();
timmey9 32:bf1e079a2ee3 535
timmey9 32:bf1e079a2ee3 536 // reset mallet
timmey9 32:bf1e079a2ee3 537 motorA = 0;
timmey9 32:bf1e079a2ee3 538 motorB = 1;
timmey9 32:bf1e079a2ee3 539 wait_ms(138);
timmey9 32:bf1e079a2ee3 540 motorB = 0;
timmey9 32:bf1e079a2ee3 541
timmey9 32:bf1e079a2ee3 542
timmey9 32:bf1e079a2ee3 543
timmey9 32:bf1e079a2ee3 544 NVIC_SetPriority(ENET_1588_Timer_IRQn,0);
timmey9 32:bf1e079a2ee3 545 NVIC_SetPriority(ENET_Transmit_IRQn,0);
timmey9 32:bf1e079a2ee3 546 NVIC_SetPriority(ENET_Receive_IRQn,0);
timmey9 32:bf1e079a2ee3 547 NVIC_SetPriority(ENET_Error_IRQn,0);
timmey9 32:bf1e079a2ee3 548 TotalInd = 1;
timmey9 32:bf1e079a2ee3 549 client[index].write((void *)&sample_array1,2*TOTAL_SAMPLES);
timmey9 32:bf1e079a2ee3 550 client[index].write((void *)&sample_array2,2*TOTAL_SAMPLES);
timmey9 32:bf1e079a2ee3 551 client[index].write((void *)&angle_array,2*TOTAL_SAMPLES);
timmey9 32:bf1e079a2ee3 552 TotalInd = 0;
timmey9 32:bf1e079a2ee3 553
timmey9 32:bf1e079a2ee3 554 NVIC_SetPriority(ENET_1588_Timer_IRQn,1);
timmey9 32:bf1e079a2ee3 555 NVIC_SetPriority(ENET_Transmit_IRQn,1);
timmey9 32:bf1e079a2ee3 556 NVIC_SetPriority(ENET_Receive_IRQn,1);
timmey9 32:bf1e079a2ee3 557 NVIC_SetPriority(ENET_Error_IRQn,1);
timmey9 32:bf1e079a2ee3 558
timmey9 32:bf1e079a2ee3 559 }
timmey9 32:bf1e079a2ee3 560 break;
timmey9 32:bf1e079a2ee3 561 case '+':
timmey9 32:bf1e079a2ee3 562 motorVal++;
timmey9 33:9806eb964362 563 if(motorVal > 8000) motorVal = 43;
timmey9 33:9806eb964362 564 char buf[29];
timmey9 33:9806eb964362 565 sprintf(buf,"MotorVal: %i\n",motorVal);
timmey9 33:9806eb964362 566 client[index].write((void *) buf,29);
timmey9 33:9806eb964362 567
timmey9 32:bf1e079a2ee3 568 break;
timmey9 32:bf1e079a2ee3 569 case '-':
timmey9 32:bf1e079a2ee3 570 motorVal--;
timmey9 32:bf1e079a2ee3 571 if(motorVal < 0) motorVal = 0;
timmey9 33:9806eb964362 572 char buf[29];
timmey9 33:9806eb964362 573 sprintf(buf,"MotorVal: %i\n",motorVal);
timmey9 33:9806eb964362 574 client[index].write((void *) buf,29);
timmey9 33:9806eb964362 575
timmey9 24:a5891669afc5 576 break;
timmey9 24:a5891669afc5 577 case 'a':
timmey9 24:a5891669afc5 578 if(angle_encoder.set_zero(&rotary_count)) {
timmey9 24:a5891669afc5 579 client[index].write((void *) "Zero set\n",9);
timmey9 24:a5891669afc5 580 }
timmey9 24:a5891669afc5 581 else {
timmey9 24:a5891669afc5 582 client[index].write((void *) "Zero NOT set\n",13);
timmey9 24:a5891669afc5 583 }
timmey9 24:a5891669afc5 584 break;
timmey9 24:a5891669afc5 585 case 's':
timmey9 24:a5891669afc5 586 {
timmey9 24:a5891669afc5 587 char buf[16];
timmey9 24:a5891669afc5 588 sprintf(buf,"NOP: %x\n",angle_encoder.nop());
timmey9 24:a5891669afc5 589 client[index].write((void *) buf,16);
timmey9 24:a5891669afc5 590 break;
timmey9 24:a5891669afc5 591 }
timmey9 24:a5891669afc5 592 case 'd':
timmey9 24:a5891669afc5 593 {
timmey9 24:a5891669afc5 594 char buf[29];
timmey9 24:a5891669afc5 595 sprintf(buf,"Angle: %i %i\n",angle_encoder.absolute_angle(), rotary_count);
timmey9 24:a5891669afc5 596 client[index].write((void *) buf,29);
timmey9 24:a5891669afc5 597 break;
timmey9 28:4a833d59897b 598 }
timmey9 21:1fb5023b72af 599 }
timmey9 21:1fb5023b72af 600 }
timmey9 17:2f978f823020 601 break;
timmey9 17:2f978f823020 602 }
timmey9 16:c3f922f61b8f 603 }
timmey9 17:2f978f823020 604
timmey9 17:2f978f823020 605 } while (server.getStatus() == network::Socket::Listening);
timmey9 22:523e316cbe70 606 }
timmey9 23:9e5141647775 607
timmey9 22:523e316cbe70 608 void timed_sampling() {
timmey9 28:4a833d59897b 609 SampleInd = 1;
timmey9 27:8c2b30c855d1 610 //__disable_irq(); // Disable Interrupts
timmey9 23:9e5141647775 611 //timeStamp.start();
timmey9 28:4a833d59897b 612 /*
timmey9 22:523e316cbe70 613 // The following performs analog-to-digital conversions - first reading the last conversion - then initiating another
timmey9 22:523e316cbe70 614 uint32_t A0_value = adc_hal_get_conversion_value(0, 0);
timmey9 22:523e316cbe70 615 uint32_t A2_value = adc_hal_get_conversion_value(1, 0);
timmey9 22:523e316cbe70 616 BW_ADC_SC1n_ADCH(0, 0, kAdcChannel12); // This corresponds to starting an ADC conversion on channel 12 of ADC 0 - which is A0 (PTB2)
timmey9 22:523e316cbe70 617 BW_ADC_SC1n_ADCH(1, 0, kAdcChannel14); // This corresponds to starting an ADC conversion on channel 14 of ADC 1 - which is A2 (PTB10)
timmey9 22:523e316cbe70 618
timmey9 22:523e316cbe70 619 // The following updates the rotary counter for the AMT20 sensor
timmey9 22:523e316cbe70 620 // Put A on PTC0
timmey9 22:523e316cbe70 621 // Put B on PTC1
timmey9 22:523e316cbe70 622 uint32_t AMT20_AB = HW_GPIO_PDIR_RD(HW_PORTC) & 0x03;
timmey9 22:523e316cbe70 623 //AMT20_AB = ~last_AMT20_AB_read; // Used to force a count - extend time.
timmey9 22:523e316cbe70 624 if (AMT20_AB != last_AMT20_AB_read)
timmey9 22:523e316cbe70 625 {
timmey9 22:523e316cbe70 626 // change "INVERT_ANGLE" to change whether relative angle counts up or down.
timmey9 22:523e316cbe70 627 if ((AMT20_AB >> 1)^(last_AMT20_AB_read) & 1U)
timmey9 22:523e316cbe70 628 #if INVERT_ANGLE == 1
timmey9 22:523e316cbe70 629 {rotary_count--;}
timmey9 22:523e316cbe70 630 else
timmey9 22:523e316cbe70 631 {rotary_count++;}
timmey9 22:523e316cbe70 632 #else
timmey9 22:523e316cbe70 633 {rotary_count++;}
timmey9 22:523e316cbe70 634 else
timmey9 22:523e316cbe70 635 {rotary_count--;}
timmey9 22:523e316cbe70 636 #endif
timmey9 22:523e316cbe70 637
timmey9 22:523e316cbe70 638 last_AMT20_AB_read = AMT20_AB;
timmey9 22:523e316cbe70 639 }
timmey9 22:523e316cbe70 640 //current_sample_index = BEGIN_SAMPLING; // Used to force extra time.
timmey9 22:523e316cbe70 641 if (current_sample_index == WAITING_TO_BEGIN) {}
timmey9 22:523e316cbe70 642 else
timmey9 28:4a833d59897b 643 {
timmey9 22:523e316cbe70 644 if (current_sample_index == BEGIN_SAMPLING) {
timmey9 22:523e316cbe70 645 current_sample_index = FIRST_SAMPLE_INDEX;
timmey9 22:523e316cbe70 646 }
timmey9 22:523e316cbe70 647
timmey9 27:8c2b30c855d1 648 sample_array1[current_sample_index] = A0_value;
timmey9 27:8c2b30c855d1 649 sample_array2[current_sample_index] = A2_value;
timmey9 22:523e316cbe70 650 angle_array[current_sample_index] = rotary_count;
timmey9 22:523e316cbe70 651
timmey9 22:523e316cbe70 652 if (current_sample_index == LAST_SAMPLE_INDEX) {
timmey9 22:523e316cbe70 653 current_sample_index = WAITING_TO_BEGIN;
timmey9 22:523e316cbe70 654 }
timmey9 22:523e316cbe70 655 else { current_sample_index++; }
timmey9 28:4a833d59897b 656 }
timmey9 22:523e316cbe70 657
timmey9 23:9e5141647775 658 //int tempVar = timeStamp.read_us();
timmey9 23:9e5141647775 659 //timeStamp.stop();
timmey9 23:9e5141647775 660 //timeStamp.reset();
timmey9 23:9e5141647775 661 //pc.printf("TimeStamp: %i\r\n", tempVar);
timmey9 27:8c2b30c855d1 662 //__enable_irq(); // Enable Interrupts
timmey9 27:8c2b30c855d1 663 */
timmey9 28:4a833d59897b 664 SampleInd = 0;
timmey9 22:523e316cbe70 665 }
timmey9 22:523e316cbe70 666
timmey9 22:523e316cbe70 667 void analog_initialization(PinName pin)
timmey9 22:523e316cbe70 668 {
timmey9 22:523e316cbe70 669 ADCName adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC);
timmey9 22:523e316cbe70 670 // MBED_ASSERT(adc != (ADCName)NC);
timmey9 22:523e316cbe70 671
timmey9 22:523e316cbe70 672 uint32_t instance = adc >> ADC_INSTANCE_SHIFT;
timmey9 22:523e316cbe70 673
timmey9 22:523e316cbe70 674 clock_manager_set_gate(kClockModuleADC, instance, true);
timmey9 22:523e316cbe70 675
timmey9 22:523e316cbe70 676 uint32_t bus_clock;
timmey9 22:523e316cbe70 677 clock_manager_get_frequency(kBusClock, &bus_clock);
timmey9 22:523e316cbe70 678 uint32_t clkdiv;
timmey9 22:523e316cbe70 679 for (clkdiv = 0; clkdiv < 4; clkdiv++) {
timmey9 22:523e316cbe70 680 if ((bus_clock >> clkdiv) <= MAX_FADC)
timmey9 22:523e316cbe70 681 break;
timmey9 22:523e316cbe70 682 }
timmey9 22:523e316cbe70 683 if (clkdiv == 4) {
timmey9 22:523e316cbe70 684 clkdiv = 0x7; //Set max div
timmey9 22:523e316cbe70 685 }
timmey9 22:523e316cbe70 686 // adc is enabled/triggered when reading.
timmey9 22:523e316cbe70 687 adc_hal_set_clock_source_mode(instance, (adc_clock_source_mode_t)(clkdiv >> 2));
timmey9 22:523e316cbe70 688 adc_hal_set_clock_divider_mode(instance, (adc_clock_divider_mode_t)(clkdiv & 0x3));
timmey9 22:523e316cbe70 689 adc_hal_set_reference_voltage_mode(instance, kAdcVoltageVref);
timmey9 22:523e316cbe70 690 adc_hal_set_resolution_mode(instance, kAdcSingleDiff16);
timmey9 22:523e316cbe70 691 adc_hal_configure_continuous_conversion(instance, false);
timmey9 22:523e316cbe70 692 adc_hal_configure_hw_trigger(instance, false); // sw trigger
timmey9 22:523e316cbe70 693 adc_hal_configure_hw_average(instance, false);
timmey9 22:523e316cbe70 694 adc_hal_set_hw_average_mode(instance, kAdcHwAverageCount4);
timmey9 22:523e316cbe70 695 adc_hal_set_group_mux(instance, kAdcChannelMuxB); // only B channels are avail
timmey9 22:523e316cbe70 696
timmey9 22:523e316cbe70 697 pinmap_pinout(pin, PinMap_ADC);
timmey9 22:523e316cbe70 698 }
timmey9 22:523e316cbe70 699
timmey9 22:523e316cbe70 700 void output_data(uint32_t iteration_number)
timmey9 22:523e316cbe70 701 {
timmey9 22:523e316cbe70 702 pc.printf("Iteration: %i\n\r", iteration_number);
timmey9 22:523e316cbe70 703 pc.printf("Sampling rate: %i\n\r", SAMPLING_RATE);
timmey9 22:523e316cbe70 704 pc.printf("Data length: %i\n\r", TOTAL_SAMPLES);
timmey9 27:8c2b30c855d1 705
timmey9 27:8c2b30c855d1 706 //for (int n = FIRST_SAMPLE_INDEX; n <= LAST_SAMPLE_INDEX; n++) {
timmey9 27:8c2b30c855d1 707 // pc.printf("%i\t%i\t%i\r\n", sample_array1[n], sample_array2[n], angle_array[n]);
timmey9 27:8c2b30c855d1 708 // }
timmey9 22:523e316cbe70 709
timmey9 22:523e316cbe70 710 }
timmey9 27:8c2b30c855d1 711
timmey9 27:8c2b30c855d1 712 // read some registers for some info.
timmey9 27:8c2b30c855d1 713 //uint32_t* rcr = (uint32_t*) 0x400C0084;
timmey9 27:8c2b30c855d1 714 //uint32_t* ecr = (uint32_t*) 0x400C0024;
timmey9 27:8c2b30c855d1 715 //pc.printf("RCR register: %x\r\n", *rcr);
timmey9 27:8c2b30c855d1 716 //pc.printf("ECR register: %x\r\n", *ecr);