embedded smtf7 based scope

Dependencies:   BSP_DISCO_F746NG_patch_fixed mbed

Committer:
the_sz
Date:
Wed Nov 18 14:31:49 2015 +0000
Revision:
0:0babd18ae684
1.01

Who changed what in which revision?

UserRevisionLine numberNew contents of line
the_sz 0:0babd18ae684 1 #include "mbed.h"
the_sz 0:0babd18ae684 2 #include "stm32746g_discovery_lcd.h"
the_sz 0:0babd18ae684 3
the_sz 0:0babd18ae684 4 #include "Interface.h"
the_sz 0:0babd18ae684 5
the_sz 0:0babd18ae684 6 DigitalOut led(LED1);
the_sz 0:0babd18ae684 7 Serial serial(PA_9,PB_7); // serial 1
the_sz 0:0babd18ae684 8 AnalogIn analogIn1(A0);
the_sz 0:0babd18ae684 9 AnalogIn analogIn2(A1);
the_sz 0:0babd18ae684 10 VALUES_STRUCT buffer[BUFFER_SIZE];
the_sz 0:0babd18ae684 11 uint8_t commandBuffer[100];
the_sz 0:0babd18ae684 12 uint32_t commandLength;
the_sz 0:0babd18ae684 13
the_sz 0:0babd18ae684 14 void sendBuffer(uint8_t *buffer,uint32_t length)
the_sz 0:0babd18ae684 15 {
the_sz 0:0babd18ae684 16 while (length>0)
the_sz 0:0babd18ae684 17 {
the_sz 0:0babd18ae684 18 serial.putc(*buffer);
the_sz 0:0babd18ae684 19
the_sz 0:0babd18ae684 20 buffer++;
the_sz 0:0babd18ae684 21 length--;
the_sz 0:0babd18ae684 22 }
the_sz 0:0babd18ae684 23 }
the_sz 0:0babd18ae684 24
the_sz 0:0babd18ae684 25 void sendCommand(HEADER_TYPE_ENUM type,uint8_t *buffer,uint32_t length)
the_sz 0:0babd18ae684 26 {
the_sz 0:0babd18ae684 27 HEADER_STRUCT header;
the_sz 0:0babd18ae684 28
the_sz 0:0babd18ae684 29 header.magic=HEADER_MAGIC;
the_sz 0:0babd18ae684 30 header.type=type;
the_sz 0:0babd18ae684 31 header.length=length;
the_sz 0:0babd18ae684 32
the_sz 0:0babd18ae684 33 sendBuffer((uint8_t *)&header,sizeof(header));
the_sz 0:0babd18ae684 34 sendBuffer(buffer,length);
the_sz 0:0babd18ae684 35 }
the_sz 0:0babd18ae684 36
the_sz 0:0babd18ae684 37 bool readWithTimeout(uint8_t *buffer)
the_sz 0:0babd18ae684 38 {
the_sz 0:0babd18ae684 39 uint32_t counter;
the_sz 0:0babd18ae684 40
the_sz 0:0babd18ae684 41 counter=0;
the_sz 0:0babd18ae684 42 while (counter<10000)
the_sz 0:0babd18ae684 43 {
the_sz 0:0babd18ae684 44 if (serial.readable())
the_sz 0:0babd18ae684 45 {
the_sz 0:0babd18ae684 46 (*buffer)=serial.getc();
the_sz 0:0babd18ae684 47 return true;
the_sz 0:0babd18ae684 48 }
the_sz 0:0babd18ae684 49
the_sz 0:0babd18ae684 50 wait_us(50);
the_sz 0:0babd18ae684 51
the_sz 0:0babd18ae684 52 counter++;
the_sz 0:0babd18ae684 53 }
the_sz 0:0babd18ae684 54
the_sz 0:0babd18ae684 55 // re-init serial port
the_sz 0:0babd18ae684 56 serial.baud(115200);
the_sz 0:0babd18ae684 57
the_sz 0:0babd18ae684 58 return false;
the_sz 0:0babd18ae684 59 }
the_sz 0:0babd18ae684 60
the_sz 0:0babd18ae684 61 HEADER_TYPE_ENUM readCommand(void)
the_sz 0:0babd18ae684 62 {
the_sz 0:0babd18ae684 63 HEADER_STRUCT *header;
the_sz 0:0babd18ae684 64 uint32_t index;
the_sz 0:0babd18ae684 65 HEADER_TYPE_ENUM type;
the_sz 0:0babd18ae684 66
the_sz 0:0babd18ae684 67 for (index=0;index<sizeof(HEADER_STRUCT);index++)
the_sz 0:0babd18ae684 68 {
the_sz 0:0babd18ae684 69 if (readWithTimeout(&commandBuffer[index])==false)
the_sz 0:0babd18ae684 70 return HT_NONE;
the_sz 0:0babd18ae684 71 }
the_sz 0:0babd18ae684 72
the_sz 0:0babd18ae684 73 header=(HEADER_STRUCT *)commandBuffer;
the_sz 0:0babd18ae684 74 if (header->magic!=HEADER_MAGIC)
the_sz 0:0babd18ae684 75 return HT_NONE;
the_sz 0:0babd18ae684 76
the_sz 0:0babd18ae684 77 type=(HEADER_TYPE_ENUM)header->type;
the_sz 0:0babd18ae684 78
the_sz 0:0babd18ae684 79 if (header->length>0)
the_sz 0:0babd18ae684 80 {
the_sz 0:0babd18ae684 81 commandLength=header->length;
the_sz 0:0babd18ae684 82
the_sz 0:0babd18ae684 83 for (index=0;index<commandLength;index++)
the_sz 0:0babd18ae684 84 {
the_sz 0:0babd18ae684 85 if (readWithTimeout(&commandBuffer[index])==false)
the_sz 0:0babd18ae684 86 return HT_NONE;
the_sz 0:0babd18ae684 87 }
the_sz 0:0babd18ae684 88 }
the_sz 0:0babd18ae684 89
the_sz 0:0babd18ae684 90 return type;
the_sz 0:0babd18ae684 91 }
the_sz 0:0babd18ae684 92
the_sz 0:0babd18ae684 93 int main()
the_sz 0:0babd18ae684 94 {
the_sz 0:0babd18ae684 95 int32_t index;
the_sz 0:0babd18ae684 96 int32_t indexPrevious;
the_sz 0:0babd18ae684 97 int32_t stop;
the_sz 0:0babd18ae684 98 HEADER_TYPE_ENUM type;
the_sz 0:0babd18ae684 99 bool capture;
the_sz 0:0babd18ae684 100 float triggerLevel;
the_sz 0:0babd18ae684 101 uint8_t triggerMode;
the_sz 0:0babd18ae684 102 uint16_t delay;
the_sz 0:0babd18ae684 103 bool triggered;
the_sz 0:0babd18ae684 104
the_sz 0:0babd18ae684 105 serial.baud(115200);
the_sz 0:0babd18ae684 106
the_sz 0:0babd18ae684 107 BSP_LCD_Init();
the_sz 0:0babd18ae684 108 BSP_LCD_DisplayOff();
the_sz 0:0babd18ae684 109
the_sz 0:0babd18ae684 110 capture=false;
the_sz 0:0babd18ae684 111 triggerLevel=0.0;
the_sz 0:0babd18ae684 112 triggerMode=TM_NONE;
the_sz 0:0babd18ae684 113 delay=0;
the_sz 0:0babd18ae684 114
the_sz 0:0babd18ae684 115 for (;;)
the_sz 0:0babd18ae684 116 {
the_sz 0:0babd18ae684 117 if (serial.readable())
the_sz 0:0babd18ae684 118 {
the_sz 0:0babd18ae684 119 type=readCommand();
the_sz 0:0babd18ae684 120 switch (type)
the_sz 0:0babd18ae684 121 {
the_sz 0:0babd18ae684 122 case HT_INFO_REQUEST:
the_sz 0:0babd18ae684 123 {
the_sz 0:0babd18ae684 124 INTERFACE_INFO_RESPONSE_STRUCT InfoResponse;
the_sz 0:0babd18ae684 125
the_sz 0:0babd18ae684 126 memset(&InfoResponse,0,sizeof(InfoResponse));
the_sz 0:0babd18ae684 127 InfoResponse.versionMajor=1;
the_sz 0:0babd18ae684 128 InfoResponse.versionMinor=0;
the_sz 0:0babd18ae684 129 snprintf(InfoResponse.name,sizeof(InfoResponse.name),"Scope");
the_sz 0:0babd18ae684 130 sendCommand(HT_INFO_RESPONSE,(uint8_t *)&InfoResponse,sizeof(InfoResponse));
the_sz 0:0babd18ae684 131 }
the_sz 0:0babd18ae684 132 break;
the_sz 0:0babd18ae684 133
the_sz 0:0babd18ae684 134 case HT_CAPTURE_START:
the_sz 0:0babd18ae684 135 {
the_sz 0:0babd18ae684 136 INTERFACE_CAPTURE_START_STRUCT *captuerStart;
the_sz 0:0babd18ae684 137
the_sz 0:0babd18ae684 138 if (commandLength==sizeof(INTERFACE_CAPTURE_START_STRUCT))
the_sz 0:0babd18ae684 139 {
the_sz 0:0babd18ae684 140 captuerStart=(INTERFACE_CAPTURE_START_STRUCT *)commandBuffer;
the_sz 0:0babd18ae684 141
the_sz 0:0babd18ae684 142 triggerLevel=captuerStart->triggerLevel;
the_sz 0:0babd18ae684 143 triggerMode=captuerStart->triggerMode;
the_sz 0:0babd18ae684 144 delay=captuerStart->delay;
the_sz 0:0babd18ae684 145
the_sz 0:0babd18ae684 146 // seems our measurement loop needs 5 µs per loop
the_sz 0:0babd18ae684 147 if (delay>5)
the_sz 0:0babd18ae684 148 delay-=5;
the_sz 0:0babd18ae684 149
the_sz 0:0babd18ae684 150 capture=true;
the_sz 0:0babd18ae684 151 }
the_sz 0:0babd18ae684 152 }
the_sz 0:0babd18ae684 153 break;
the_sz 0:0babd18ae684 154 }
the_sz 0:0babd18ae684 155 }
the_sz 0:0babd18ae684 156
the_sz 0:0babd18ae684 157 if (capture==true)
the_sz 0:0babd18ae684 158 {
the_sz 0:0babd18ae684 159 led=1;
the_sz 0:0babd18ae684 160
the_sz 0:0babd18ae684 161 // dummy read for nicer results
the_sz 0:0babd18ae684 162 for (index=0;index<5;index++)
the_sz 0:0babd18ae684 163 {
the_sz 0:0babd18ae684 164 analogIn1.read();
the_sz 0:0babd18ae684 165 analogIn2.read();
the_sz 0:0babd18ae684 166 }
the_sz 0:0babd18ae684 167
the_sz 0:0babd18ae684 168 stop=0;
the_sz 0:0babd18ae684 169 index=0;
the_sz 0:0babd18ae684 170 indexPrevious=0;
the_sz 0:0babd18ae684 171 triggered=false;
the_sz 0:0babd18ae684 172 for (;;)
the_sz 0:0babd18ae684 173 {
the_sz 0:0babd18ae684 174 buffer[index].values[0]=analogIn1.read();
the_sz 0:0babd18ae684 175 buffer[index].values[1]=analogIn2.read();
the_sz 0:0babd18ae684 176
the_sz 0:0babd18ae684 177 // check for trigger
the_sz 0:0babd18ae684 178 if ((triggered==false) && (index>(BUFFER_SIZE/2)))
the_sz 0:0babd18ae684 179 {
the_sz 0:0babd18ae684 180 // we trigger only on channel 1
the_sz 0:0babd18ae684 181 if ((triggerMode & TM_RAISING)!=0)
the_sz 0:0babd18ae684 182 if (buffer[indexPrevious].values[0]<triggerLevel)
the_sz 0:0babd18ae684 183 if (buffer[index].values[0]>=triggerLevel)
the_sz 0:0babd18ae684 184 triggered=true;
the_sz 0:0babd18ae684 185 if ((triggerMode & TM_FALLING)!=0)
the_sz 0:0babd18ae684 186 if (buffer[indexPrevious].values[0]>triggerLevel)
the_sz 0:0babd18ae684 187 if (buffer[index].values[0]<=triggerLevel)
the_sz 0:0babd18ae684 188 triggered=true;
the_sz 0:0babd18ae684 189
the_sz 0:0babd18ae684 190 if (triggered==true)
the_sz 0:0babd18ae684 191 stop=(index + (BUFFER_SIZE / 2)) % BUFFER_SIZE;
the_sz 0:0babd18ae684 192 }
the_sz 0:0babd18ae684 193
the_sz 0:0babd18ae684 194 indexPrevious=index;
the_sz 0:0babd18ae684 195
the_sz 0:0babd18ae684 196 index++;
the_sz 0:0babd18ae684 197 if (index>=BUFFER_SIZE)
the_sz 0:0babd18ae684 198 index=0;
the_sz 0:0babd18ae684 199
the_sz 0:0babd18ae684 200 if (index==stop)
the_sz 0:0babd18ae684 201 break;
the_sz 0:0babd18ae684 202
the_sz 0:0babd18ae684 203 wait_us(delay);
the_sz 0:0babd18ae684 204 }
the_sz 0:0babd18ae684 205
the_sz 0:0babd18ae684 206 led=0;
the_sz 0:0babd18ae684 207 capture=false;
the_sz 0:0babd18ae684 208
the_sz 0:0babd18ae684 209 if ((triggered==true) || ((triggerMode & TM_AUTO)!=0))
the_sz 0:0babd18ae684 210 {
the_sz 0:0babd18ae684 211 HEADER_STRUCT header;
the_sz 0:0babd18ae684 212
the_sz 0:0babd18ae684 213 header.magic=HEADER_MAGIC;
the_sz 0:0babd18ae684 214 header.type=HT_DATA;
the_sz 0:0babd18ae684 215 header.length=sizeof(buffer);
the_sz 0:0babd18ae684 216
the_sz 0:0babd18ae684 217 // send header
the_sz 0:0babd18ae684 218 sendBuffer((uint8_t *)&header,sizeof(header));
the_sz 0:0babd18ae684 219 // send part 1
the_sz 0:0babd18ae684 220 sendBuffer((uint8_t *)&buffer[stop],((BUFFER_SIZE-stop)*sizeof(VALUES_STRUCT)));
the_sz 0:0babd18ae684 221 // send part 2
the_sz 0:0babd18ae684 222 if (stop>0)
the_sz 0:0babd18ae684 223 sendBuffer((uint8_t *)&buffer[0],(stop*sizeof(VALUES_STRUCT)));
the_sz 0:0babd18ae684 224 }
the_sz 0:0babd18ae684 225 else
the_sz 0:0babd18ae684 226 sendCommand(HT_DATA,(uint8_t *)&buffer,0);
the_sz 0:0babd18ae684 227 }
the_sz 0:0babd18ae684 228 }
the_sz 0:0babd18ae684 229 }