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.
common_task.cpp@0:e7352f4f3dcb, 2020-06-18 (annotated)
- Committer:
- thomashaine
- Date:
- Thu Jun 18 12:36:05 2020 +0000
- Revision:
- 0:e7352f4f3dcb
- Child:
- 1:5060c8ea0ccd
rev1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
thomashaine | 0:e7352f4f3dcb | 1 | #include "mbed.h" |
thomashaine | 0:e7352f4f3dcb | 2 | #include "register_address.h" |
thomashaine | 0:e7352f4f3dcb | 3 | #include "common_task.h" |
thomashaine | 0:e7352f4f3dcb | 4 | #include "EthernetInterface.h" |
thomashaine | 0:e7352f4f3dcb | 5 | #include "TCPServer.h" |
thomashaine | 0:e7352f4f3dcb | 6 | #include "TCPSocket.h" |
thomashaine | 0:e7352f4f3dcb | 7 | |
thomashaine | 0:e7352f4f3dcb | 8 | |
thomashaine | 0:e7352f4f3dcb | 9 | extern Serial pc; |
thomashaine | 0:e7352f4f3dcb | 10 | extern DigitalOut led3; |
thomashaine | 0:e7352f4f3dcb | 11 | extern DigitalOut led2; |
thomashaine | 0:e7352f4f3dcb | 12 | extern SPI spi; // mosi, miso, sclk |
thomashaine | 0:e7352f4f3dcb | 13 | extern DigitalOut SSN; |
thomashaine | 0:e7352f4f3dcb | 14 | |
thomashaine | 0:e7352f4f3dcb | 15 | extern InterruptIn button_capture; |
thomashaine | 0:e7352f4f3dcb | 16 | extern InterruptIn irq_cis; |
thomashaine | 0:e7352f4f3dcb | 17 | |
thomashaine | 0:e7352f4f3dcb | 18 | extern DigitalOut RSTN; |
thomashaine | 0:e7352f4f3dcb | 19 | extern DigitalOut SLEEPN; |
thomashaine | 0:e7352f4f3dcb | 20 | extern DigitalOut CAPTURE; |
thomashaine | 0:e7352f4f3dcb | 21 | |
thomashaine | 0:e7352f4f3dcb | 22 | extern uint8_t DCMI_8b_mode; |
thomashaine | 0:e7352f4f3dcb | 23 | |
thomashaine | 0:e7352f4f3dcb | 24 | extern EthernetInterface eth; |
thomashaine | 0:e7352f4f3dcb | 25 | extern TCPSocket cs; //com socket |
thomashaine | 0:e7352f4f3dcb | 26 | extern char ServerIP[17]; |
thomashaine | 0:e7352f4f3dcb | 27 | extern int portnumber; |
thomashaine | 0:e7352f4f3dcb | 28 | extern int portnumber_com; |
thomashaine | 0:e7352f4f3dcb | 29 | |
thomashaine | 0:e7352f4f3dcb | 30 | char toprint[64]; |
thomashaine | 0:e7352f4f3dcb | 31 | |
thomashaine | 0:e7352f4f3dcb | 32 | int myprint(const char* mystring ){ |
thomashaine | 0:e7352f4f3dcb | 33 | |
thomashaine | 0:e7352f4f3dcb | 34 | int byte_sent=-1; |
thomashaine | 0:e7352f4f3dcb | 35 | int status=0; |
thomashaine | 0:e7352f4f3dcb | 36 | |
thomashaine | 0:e7352f4f3dcb | 37 | pc.printf("%s",mystring); |
thomashaine | 0:e7352f4f3dcb | 38 | byte_sent=cs.send(mystring, strlen(mystring)); |
thomashaine | 0:e7352f4f3dcb | 39 | return byte_sent; |
thomashaine | 0:e7352f4f3dcb | 40 | |
thomashaine | 0:e7352f4f3dcb | 41 | } |
thomashaine | 0:e7352f4f3dcb | 42 | |
thomashaine | 0:e7352f4f3dcb | 43 | |
thomashaine | 0:e7352f4f3dcb | 44 | void usage(void){ |
thomashaine | 0:e7352f4f3dcb | 45 | |
thomashaine | 0:e7352f4f3dcb | 46 | myprint("Usage\n\r"); |
thomashaine | 0:e7352f4f3dcb | 47 | myprint("----------------------------------------\n\r"); |
thomashaine | 0:e7352f4f3dcb | 48 | myprint("H -> help\n\r"); |
thomashaine | 0:e7352f4f3dcb | 49 | myprint("--------------SPI--------------------------\n\r"); |
thomashaine | 0:e7352f4f3dcb | 50 | myprint("spi -> test SPI\n\r"); |
thomashaine | 0:e7352f4f3dcb | 51 | myprint("G -> get register configurations\n\r"); |
thomashaine | 0:e7352f4f3dcb | 52 | myprint("--------------IMAGER--------------------------\n\r"); |
thomashaine | 0:e7352f4f3dcb | 53 | myprint("F -> take frame 8b\n\r"); |
thomashaine | 0:e7352f4f3dcb | 54 | myprint("FQ -> take frame ion QQVGA mode 8b\n\r"); |
thomashaine | 0:e7352f4f3dcb | 55 | myprint("sp -> suspend frame\n\r"); |
thomashaine | 0:e7352f4f3dcb | 56 | myprint("st -> stop frame\n\r"); |
thomashaine | 0:e7352f4f3dcb | 57 | myprint("r -> resume frame\n\r"); |
thomashaine | 0:e7352f4f3dcb | 58 | myprint("s -> send frame tp teraterm\n\r"); |
thomashaine | 0:e7352f4f3dcb | 59 | myprint("e -> set exposure time SPI\n\r"); |
thomashaine | 0:e7352f4f3dcb | 60 | myprint("m -> set mode via SPI\n\r"); |
thomashaine | 0:e7352f4f3dcb | 61 | myprint("AG -> set analog gain SPI\n\r"); |
thomashaine | 0:e7352f4f3dcb | 62 | myprint("A -> test all analog gain configurations SPI\n\r"); |
thomashaine | 0:e7352f4f3dcb | 63 | myprint("CC -> set cclk value SPI\n\r"); |
thomashaine | 0:e7352f4f3dcb | 64 | myprint("C -> auto calibration of cclk SPI\n\r"); |
thomashaine | 0:e7352f4f3dcb | 65 | myprint("IO -> set IO SR and driving strength SPI\n\r"); |
thomashaine | 0:e7352f4f3dcb | 66 | myprint("pclk -> set PCLK mode\n\r"); |
thomashaine | 0:e7352f4f3dcb | 67 | myprint("pclkd -> set PCLK delay\n\r"); |
thomashaine | 0:e7352f4f3dcb | 68 | myprint("md -> set motion detection cycle time delay\n\r"); |
thomashaine | 0:e7352f4f3dcb | 69 | myprint("oen -> (dis)enable oen\n\r"); |
thomashaine | 0:e7352f4f3dcb | 70 | myprint("pbp -> (dis)enable pbp\n\r"); |
thomashaine | 0:e7352f4f3dcb | 71 | myprint("sleepn -> (dis)enable sleepn\n\r"); |
thomashaine | 0:e7352f4f3dcb | 72 | |
thomashaine | 0:e7352f4f3dcb | 73 | myprint("------------- ethernet-----------------------\n\r"); |
thomashaine | 0:e7352f4f3dcb | 74 | myprint("si -> set ip server\n\r"); |
thomashaine | 0:e7352f4f3dcb | 75 | myprint("gi -> get ip server\n\r"); |
thomashaine | 0:e7352f4f3dcb | 76 | myprint("------------- DCMI-----------------------\n\r"); |
thomashaine | 0:e7352f4f3dcb | 77 | myprint("gs -> get status DCMI\n\r"); |
thomashaine | 0:e7352f4f3dcb | 78 | myprint("ge -> get error DCMI\n\r"); |
thomashaine | 0:e7352f4f3dcb | 79 | myprint("gsd -> get status DMA\n\r"); |
thomashaine | 0:e7352f4f3dcb | 80 | myprint("ged -> get error DMA\n\r"); |
thomashaine | 0:e7352f4f3dcb | 81 | myprint("gl -> get nb line received on DCMI\n\r"); |
thomashaine | 0:e7352f4f3dcb | 82 | myprint("gf -> get nb frame received on DCMI\n\r"); |
thomashaine | 0:e7352f4f3dcb | 83 | myprint("------------- SCVR-----------------------\n\r"); |
thomashaine | 0:e7352f4f3dcb | 84 | myprint("vdda -> set vdda register\n\r"); |
thomashaine | 0:e7352f4f3dcb | 85 | myprint("vddd -> set vddd register\n\r"); |
thomashaine | 0:e7352f4f3dcb | 86 | myprint("pm -> set power mode register\n\r"); |
thomashaine | 0:e7352f4f3dcb | 87 | myprint("pma -> set pwd vddla low medium high register\n\r"); |
thomashaine | 0:e7352f4f3dcb | 88 | myprint("pmd -> set pwd vddld low medium high register\n\r"); |
thomashaine | 0:e7352f4f3dcb | 89 | myprint("----------------------------------------\n\r\n\r"); |
thomashaine | 0:e7352f4f3dcb | 90 | |
thomashaine | 0:e7352f4f3dcb | 91 | } |
thomashaine | 0:e7352f4f3dcb | 92 | |
thomashaine | 0:e7352f4f3dcb | 93 | |
thomashaine | 0:e7352f4f3dcb | 94 | void CAPTURE_fall(){ |
thomashaine | 0:e7352f4f3dcb | 95 | CAPTURE=0; |
thomashaine | 0:e7352f4f3dcb | 96 | } |
thomashaine | 0:e7352f4f3dcb | 97 | |
thomashaine | 0:e7352f4f3dcb | 98 | void CAPTURE_rise(){ |
thomashaine | 0:e7352f4f3dcb | 99 | CAPTURE=1; |
thomashaine | 0:e7352f4f3dcb | 100 | } |
thomashaine | 0:e7352f4f3dcb | 101 | |
thomashaine | 0:e7352f4f3dcb | 102 | void TASK_INIT(){ |
thomashaine | 0:e7352f4f3dcb | 103 | |
thomashaine | 0:e7352f4f3dcb | 104 | // Enable reset |
thomashaine | 0:e7352f4f3dcb | 105 | RSTN=0; //0 => reset OK |
thomashaine | 0:e7352f4f3dcb | 106 | wait_us(5000); |
thomashaine | 0:e7352f4f3dcb | 107 | |
thomashaine | 0:e7352f4f3dcb | 108 | |
thomashaine | 0:e7352f4f3dcb | 109 | //DEFAULT SIGNAL OUTPUT |
thomashaine | 0:e7352f4f3dcb | 110 | // Output enable |
thomashaine | 0:e7352f4f3dcb | 111 | |
thomashaine | 0:e7352f4f3dcb | 112 | SLEEPN=0; //1 => active |
thomashaine | 0:e7352f4f3dcb | 113 | CAPTURE=0; //0 => no capture |
thomashaine | 0:e7352f4f3dcb | 114 | wait_us(5000); |
thomashaine | 0:e7352f4f3dcb | 115 | |
thomashaine | 0:e7352f4f3dcb | 116 | |
thomashaine | 0:e7352f4f3dcb | 117 | // link interrupt |
thomashaine | 0:e7352f4f3dcb | 118 | // capture interrupt |
thomashaine | 0:e7352f4f3dcb | 119 | button_capture.enable_irq(); |
thomashaine | 0:e7352f4f3dcb | 120 | button_capture.fall(&CAPTURE_fall); |
thomashaine | 0:e7352f4f3dcb | 121 | button_capture.rise(&CAPTURE_rise); |
thomashaine | 0:e7352f4f3dcb | 122 | |
thomashaine | 0:e7352f4f3dcb | 123 | // Disable reset |
thomashaine | 0:e7352f4f3dcb | 124 | RSTN=1; |
thomashaine | 0:e7352f4f3dcb | 125 | wait_us(5000); |
thomashaine | 0:e7352f4f3dcb | 126 | |
thomashaine | 0:e7352f4f3dcb | 127 | |
thomashaine | 0:e7352f4f3dcb | 128 | |
thomashaine | 0:e7352f4f3dcb | 129 | |
thomashaine | 0:e7352f4f3dcb | 130 | } |
thomashaine | 0:e7352f4f3dcb | 131 | |
thomashaine | 0:e7352f4f3dcb | 132 | void TASK_RSTN(){ |
thomashaine | 0:e7352f4f3dcb | 133 | RSTN=0; |
thomashaine | 0:e7352f4f3dcb | 134 | wait_us(2000); |
thomashaine | 0:e7352f4f3dcb | 135 | RSTN=1; |
thomashaine | 0:e7352f4f3dcb | 136 | wait_us(1000); |
thomashaine | 0:e7352f4f3dcb | 137 | |
thomashaine | 0:e7352f4f3dcb | 138 | } |
thomashaine | 0:e7352f4f3dcb | 139 | |
thomashaine | 0:e7352f4f3dcb | 140 | |
thomashaine | 0:e7352f4f3dcb | 141 | void TASK_SPI_WRITE(int adr, int data){ |
thomashaine | 0:e7352f4f3dcb | 142 | |
thomashaine | 0:e7352f4f3dcb | 143 | //sprintf(toprint,"writing addr %d data %d\n\r",adr,data); |
thomashaine | 0:e7352f4f3dcb | 144 | //sprintf(toprint,"sending addr %02X data %02X\n\r",adr,data); |
thomashaine | 0:e7352f4f3dcb | 145 | //myprint(toprint); |
thomashaine | 0:e7352f4f3dcb | 146 | |
thomashaine | 0:e7352f4f3dcb | 147 | int packet = (adr<<8 | data); |
thomashaine | 0:e7352f4f3dcb | 148 | SSN=0; |
thomashaine | 0:e7352f4f3dcb | 149 | |
thomashaine | 0:e7352f4f3dcb | 150 | spi.write(packet); |
thomashaine | 0:e7352f4f3dcb | 151 | |
thomashaine | 0:e7352f4f3dcb | 152 | wait_us(50); |
thomashaine | 0:e7352f4f3dcb | 153 | SSN=1; |
thomashaine | 0:e7352f4f3dcb | 154 | wait_us(100); |
thomashaine | 0:e7352f4f3dcb | 155 | |
thomashaine | 0:e7352f4f3dcb | 156 | } |
thomashaine | 0:e7352f4f3dcb | 157 | |
thomashaine | 0:e7352f4f3dcb | 158 | int TASK_SPI_READ(int adr){ |
thomashaine | 0:e7352f4f3dcb | 159 | |
thomashaine | 0:e7352f4f3dcb | 160 | //sprintf(toprint,"reading addr %d \n\r",adr); |
thomashaine | 0:e7352f4f3dcb | 161 | //sprintf(toprint,"reading addr %02X \n\r",adr); |
thomashaine | 0:e7352f4f3dcb | 162 | //myprint(toprint); |
thomashaine | 0:e7352f4f3dcb | 163 | |
thomashaine | 0:e7352f4f3dcb | 164 | int rx_buffer=0; |
thomashaine | 0:e7352f4f3dcb | 165 | int packet = (adr|0x80)<<8; |
thomashaine | 0:e7352f4f3dcb | 166 | |
thomashaine | 0:e7352f4f3dcb | 167 | SSN=0; |
thomashaine | 0:e7352f4f3dcb | 168 | rx_buffer=spi.write(packet); //--ok le spi repond mais je sais pas choper la data. |
thomashaine | 0:e7352f4f3dcb | 169 | wait_us(50); |
thomashaine | 0:e7352f4f3dcb | 170 | SSN=1; |
thomashaine | 0:e7352f4f3dcb | 171 | wait_us(100); |
thomashaine | 0:e7352f4f3dcb | 172 | |
thomashaine | 0:e7352f4f3dcb | 173 | //sprintf(toprint,"read at add %d -> data %d \n\r",adr,rx_buffer); |
thomashaine | 0:e7352f4f3dcb | 174 | //myprint(toprint); |
thomashaine | 0:e7352f4f3dcb | 175 | return (int) rx_buffer; |
thomashaine | 0:e7352f4f3dcb | 176 | } |
thomashaine | 0:e7352f4f3dcb | 177 | |
thomashaine | 0:e7352f4f3dcb | 178 | void TASK_TEST_SPI(){ |
thomashaine | 0:e7352f4f3dcb | 179 | |
thomashaine | 0:e7352f4f3dcb | 180 | uint8_t a; |
thomashaine | 0:e7352f4f3dcb | 181 | |
thomashaine | 0:e7352f4f3dcb | 182 | |
thomashaine | 0:e7352f4f3dcb | 183 | myprint("**************\r\n"); |
thomashaine | 0:e7352f4f3dcb | 184 | myprint("PING SPI\r\n"); |
thomashaine | 0:e7352f4f3dcb | 185 | myprint("**************\r\n"); |
thomashaine | 0:e7352f4f3dcb | 186 | |
thomashaine | 0:e7352f4f3dcb | 187 | TASK_SPI_WRITE(test_add,0x55); |
thomashaine | 0:e7352f4f3dcb | 188 | a=TASK_SPI_READ(test_add); |
thomashaine | 0:e7352f4f3dcb | 189 | sprintf(toprint,"write 0x%02x at 0x%02x. read 0x%02x at 0x%02x\r\n",0x55,test_add,a,test_add); |
thomashaine | 0:e7352f4f3dcb | 190 | myprint(toprint); |
thomashaine | 0:e7352f4f3dcb | 191 | |
thomashaine | 0:e7352f4f3dcb | 192 | TASK_SPI_WRITE(test_add,0xAA); |
thomashaine | 0:e7352f4f3dcb | 193 | a=TASK_SPI_READ(test_add); |
thomashaine | 0:e7352f4f3dcb | 194 | sprintf(toprint,"write 0x%02x at 0x%02x. read 0x%02x at 0x%02x\r\n",0xAA,test_add,a,test_add); |
thomashaine | 0:e7352f4f3dcb | 195 | myprint(toprint); |
thomashaine | 0:e7352f4f3dcb | 196 | |
thomashaine | 0:e7352f4f3dcb | 197 | TASK_SPI_WRITE(test_add,0xFF); |
thomashaine | 0:e7352f4f3dcb | 198 | a=TASK_SPI_READ(test_add); |
thomashaine | 0:e7352f4f3dcb | 199 | sprintf(toprint,"write 0x%02x at 0x%02x. read 0x%02x at 0x%02x\r\n",0xFF,test_add,a,test_add); |
thomashaine | 0:e7352f4f3dcb | 200 | myprint(toprint); |
thomashaine | 0:e7352f4f3dcb | 201 | |
thomashaine | 0:e7352f4f3dcb | 202 | TASK_SPI_WRITE(test_add,0x00); |
thomashaine | 0:e7352f4f3dcb | 203 | a=TASK_SPI_READ(test_add); |
thomashaine | 0:e7352f4f3dcb | 204 | sprintf(toprint,"write 0x%02x at 0x%02x. read 0x%02x at 0x%02x\r\n",0x00,test_add,a,test_add); |
thomashaine | 0:e7352f4f3dcb | 205 | myprint(toprint); |
thomashaine | 0:e7352f4f3dcb | 206 | } |
thomashaine | 0:e7352f4f3dcb | 207 | |
thomashaine | 0:e7352f4f3dcb | 208 | |
thomashaine | 0:e7352f4f3dcb | 209 | |
thomashaine | 0:e7352f4f3dcb | 210 | void TASK_ANALOG_CALIB(){ |
thomashaine | 0:e7352f4f3dcb | 211 | |
thomashaine | 0:e7352f4f3dcb | 212 | uint8_t a,b,c,e; |
thomashaine | 0:e7352f4f3dcb | 213 | int d; |
thomashaine | 0:e7352f4f3dcb | 214 | float average; |
thomashaine | 0:e7352f4f3dcb | 215 | |
thomashaine | 0:e7352f4f3dcb | 216 | myprint("**************\r\n"); |
thomashaine | 0:e7352f4f3dcb | 217 | myprint("ANALOG CALIB\r\n"); |
thomashaine | 0:e7352f4f3dcb | 218 | myprint("**************\r\n"); |
thomashaine | 0:e7352f4f3dcb | 219 | |
thomashaine | 0:e7352f4f3dcb | 220 | e=TASK_SPI_READ(analog_gain_add); |
thomashaine | 0:e7352f4f3dcb | 221 | |
thomashaine | 0:e7352f4f3dcb | 222 | TASK_SPI_WRITE(mode_add,0x02); //mode SO+calib |
thomashaine | 0:e7352f4f3dcb | 223 | myprint("mode SO+calib\r\n"); |
thomashaine | 0:e7352f4f3dcb | 224 | |
thomashaine | 0:e7352f4f3dcb | 225 | for (int i=0;i<16;i++){ |
thomashaine | 0:e7352f4f3dcb | 226 | |
thomashaine | 0:e7352f4f3dcb | 227 | TASK_SPI_WRITE(analog_gain_add,i); |
thomashaine | 0:e7352f4f3dcb | 228 | |
thomashaine | 0:e7352f4f3dcb | 229 | TASK_SPI_WRITE(capture_add,0xff); //capture on |
thomashaine | 0:e7352f4f3dcb | 230 | thread_sleep_for(1); |
thomashaine | 0:e7352f4f3dcb | 231 | TASK_SPI_WRITE(capture_add,0x00); //capture off |
thomashaine | 0:e7352f4f3dcb | 232 | thread_sleep_for(200); |
thomashaine | 0:e7352f4f3dcb | 233 | |
thomashaine | 0:e7352f4f3dcb | 234 | |
thomashaine | 0:e7352f4f3dcb | 235 | a=TASK_SPI_READ(CAL_RAMP_COUNT_0_add); //read value LSB |
thomashaine | 0:e7352f4f3dcb | 236 | b=TASK_SPI_READ(CAL_RAMP_COUNT_1_add); |
thomashaine | 0:e7352f4f3dcb | 237 | c=TASK_SPI_READ(CAL_RAMP_COUNT_2_add); //MSB |
thomashaine | 0:e7352f4f3dcb | 238 | |
thomashaine | 0:e7352f4f3dcb | 239 | d=(int) c*256*256+b*256+a; |
thomashaine | 0:e7352f4f3dcb | 240 | average=(float) d/480; |
thomashaine | 0:e7352f4f3dcb | 241 | |
thomashaine | 0:e7352f4f3dcb | 242 | sprintf(toprint,"ramp cal: %i -> %i\taverage %f\r\n",i,d,average); |
thomashaine | 0:e7352f4f3dcb | 243 | myprint(toprint); |
thomashaine | 0:e7352f4f3dcb | 244 | |
thomashaine | 0:e7352f4f3dcb | 245 | wait_us(5000); |
thomashaine | 0:e7352f4f3dcb | 246 | } |
thomashaine | 0:e7352f4f3dcb | 247 | TASK_SPI_WRITE(mode_add,0x03); //mode DRS+calib |
thomashaine | 0:e7352f4f3dcb | 248 | myprint("\r\nmode DRS+calib\r\n"); |
thomashaine | 0:e7352f4f3dcb | 249 | |
thomashaine | 0:e7352f4f3dcb | 250 | for (int i=0;i<16;i++){ |
thomashaine | 0:e7352f4f3dcb | 251 | |
thomashaine | 0:e7352f4f3dcb | 252 | TASK_SPI_WRITE(analog_gain_add,i); |
thomashaine | 0:e7352f4f3dcb | 253 | |
thomashaine | 0:e7352f4f3dcb | 254 | TASK_SPI_WRITE(capture_add,0xff); //capture on |
thomashaine | 0:e7352f4f3dcb | 255 | wait_us(1000); |
thomashaine | 0:e7352f4f3dcb | 256 | TASK_SPI_WRITE(capture_add,0x00); //capture off |
thomashaine | 0:e7352f4f3dcb | 257 | wait_us(200000); |
thomashaine | 0:e7352f4f3dcb | 258 | |
thomashaine | 0:e7352f4f3dcb | 259 | |
thomashaine | 0:e7352f4f3dcb | 260 | a=TASK_SPI_READ(CAL_RAMP_COUNT_0_add); //read value LSB |
thomashaine | 0:e7352f4f3dcb | 261 | b=TASK_SPI_READ(CAL_RAMP_COUNT_1_add); |
thomashaine | 0:e7352f4f3dcb | 262 | c=TASK_SPI_READ(CAL_RAMP_COUNT_2_add); //MSB |
thomashaine | 0:e7352f4f3dcb | 263 | |
thomashaine | 0:e7352f4f3dcb | 264 | d=(int) c*256*256+b*256+a; |
thomashaine | 0:e7352f4f3dcb | 265 | average=(float) d/480; |
thomashaine | 0:e7352f4f3dcb | 266 | |
thomashaine | 0:e7352f4f3dcb | 267 | |
thomashaine | 0:e7352f4f3dcb | 268 | sprintf(toprint,"ramp cal: %i -> %i\taverage %f\r\n",i,d,average); |
thomashaine | 0:e7352f4f3dcb | 269 | myprint(toprint); |
thomashaine | 0:e7352f4f3dcb | 270 | //wait_ms(50); |
thomashaine | 0:e7352f4f3dcb | 271 | } |
thomashaine | 0:e7352f4f3dcb | 272 | |
thomashaine | 0:e7352f4f3dcb | 273 | TASK_SPI_WRITE(0x03,0x01); //mode drs |
thomashaine | 0:e7352f4f3dcb | 274 | TASK_SPI_WRITE(analog_gain_add,e); |
thomashaine | 0:e7352f4f3dcb | 275 | |
thomashaine | 0:e7352f4f3dcb | 276 | } |
thomashaine | 0:e7352f4f3dcb | 277 | |
thomashaine | 0:e7352f4f3dcb | 278 | |
thomashaine | 0:e7352f4f3dcb | 279 | |
thomashaine | 0:e7352f4f3dcb | 280 | |
thomashaine | 0:e7352f4f3dcb | 281 | void TASK_auto_ccal(){ |
thomashaine | 0:e7352f4f3dcb | 282 | |
thomashaine | 0:e7352f4f3dcb | 283 | uint8_t a,b; |
thomashaine | 0:e7352f4f3dcb | 284 | uint8_t cclk=10; |
thomashaine | 0:e7352f4f3dcb | 285 | uint8_t upper=255; |
thomashaine | 0:e7352f4f3dcb | 286 | uint8_t lower=0; |
thomashaine | 0:e7352f4f3dcb | 287 | int res; |
thomashaine | 0:e7352f4f3dcb | 288 | int iter=0; |
thomashaine | 0:e7352f4f3dcb | 289 | |
thomashaine | 0:e7352f4f3dcb | 290 | int lb=1020; |
thomashaine | 0:e7352f4f3dcb | 291 | int ub=1024; |
thomashaine | 0:e7352f4f3dcb | 292 | |
thomashaine | 0:e7352f4f3dcb | 293 | myprint("**************\r\n"); |
thomashaine | 0:e7352f4f3dcb | 294 | myprint("AUTO CCLK CALIB\r\n"); |
thomashaine | 0:e7352f4f3dcb | 295 | myprint("**************\r\n"); |
thomashaine | 0:e7352f4f3dcb | 296 | TASK_SPI_WRITE(mode_add,0x05); //mode cclk calib |
thomashaine | 0:e7352f4f3dcb | 297 | |
thomashaine | 0:e7352f4f3dcb | 298 | |
thomashaine | 0:e7352f4f3dcb | 299 | |
thomashaine | 0:e7352f4f3dcb | 300 | TASK_SPI_WRITE(cclk_cal_add,cclk); //default value of cclk reg |
thomashaine | 0:e7352f4f3dcb | 301 | TASK_SPI_WRITE(capture_add,0xff); //capture on |
thomashaine | 0:e7352f4f3dcb | 302 | wait_us(100); |
thomashaine | 0:e7352f4f3dcb | 303 | TASK_SPI_WRITE(capture_add,0x00); //capture off |
thomashaine | 0:e7352f4f3dcb | 304 | wait_us(100); |
thomashaine | 0:e7352f4f3dcb | 305 | a=TASK_SPI_READ(CAL_CLK_BUFF_0_add); //read value LSB |
thomashaine | 0:e7352f4f3dcb | 306 | b=TASK_SPI_READ(CAL_CLK_BUFF_1_add); //MSB |
thomashaine | 0:e7352f4f3dcb | 307 | res=(int) b*256+a; |
thomashaine | 0:e7352f4f3dcb | 308 | |
thomashaine | 0:e7352f4f3dcb | 309 | while( (res <lb || res > ub) && iter<8){ |
thomashaine | 0:e7352f4f3dcb | 310 | |
thomashaine | 0:e7352f4f3dcb | 311 | TASK_SPI_WRITE(cclk_cal_add,cclk); //default value of cclk reg |
thomashaine | 0:e7352f4f3dcb | 312 | a=TASK_SPI_READ(cclk_cal_add); |
thomashaine | 0:e7352f4f3dcb | 313 | //myprint("write 0x%02x at 0x%02x. read 0x%02x at 0x%02x\r\n",cclk,cclk_cal_add,a,cclk_cal_add); |
thomashaine | 0:e7352f4f3dcb | 314 | |
thomashaine | 0:e7352f4f3dcb | 315 | TASK_SPI_WRITE(capture_add,0xff); //capture on |
thomashaine | 0:e7352f4f3dcb | 316 | wait_us(1000); |
thomashaine | 0:e7352f4f3dcb | 317 | TASK_SPI_WRITE(capture_add,0x00); //capture off |
thomashaine | 0:e7352f4f3dcb | 318 | wait_us(1000); |
thomashaine | 0:e7352f4f3dcb | 319 | a=TASK_SPI_READ(CAL_CLK_BUFF_0_add); //read value LSB |
thomashaine | 0:e7352f4f3dcb | 320 | b=TASK_SPI_READ(CAL_CLK_BUFF_1_add); //MSB |
thomashaine | 0:e7352f4f3dcb | 321 | res=(int) b*256+ a; |
thomashaine | 0:e7352f4f3dcb | 322 | |
thomashaine | 0:e7352f4f3dcb | 323 | //myprint("cclk cal: %i -> cclk %i\r\n",cclk, b*256+a); |
thomashaine | 0:e7352f4f3dcb | 324 | sprintf(toprint,"cclk cal: %i -> cclk %i\r\n",cclk,res); |
thomashaine | 0:e7352f4f3dcb | 325 | myprint(toprint); |
thomashaine | 0:e7352f4f3dcb | 326 | |
thomashaine | 0:e7352f4f3dcb | 327 | //updated stuff |
thomashaine | 0:e7352f4f3dcb | 328 | if(res<lb){ |
thomashaine | 0:e7352f4f3dcb | 329 | //myprint("too low: %i %i %i %i",res,cclk,lower,upper); |
thomashaine | 0:e7352f4f3dcb | 330 | lower=cclk; |
thomashaine | 0:e7352f4f3dcb | 331 | cclk=(lower+upper)/2; |
thomashaine | 0:e7352f4f3dcb | 332 | //myprint(" -> %i %i %i\r\n\r\n",cclk,lower,upper); |
thomashaine | 0:e7352f4f3dcb | 333 | } else if(res>ub){ |
thomashaine | 0:e7352f4f3dcb | 334 | //myprint("too high: %i %i %i %i",res,cclk,lower,upper); |
thomashaine | 0:e7352f4f3dcb | 335 | upper=cclk; |
thomashaine | 0:e7352f4f3dcb | 336 | cclk=(lower+upper)/2; |
thomashaine | 0:e7352f4f3dcb | 337 | //myprint(" -> %i %i %i \r\n\r\n",cclk,lower,upper); |
thomashaine | 0:e7352f4f3dcb | 338 | } else { |
thomashaine | 0:e7352f4f3dcb | 339 | myprint("ok !!\r\n"); |
thomashaine | 0:e7352f4f3dcb | 340 | } |
thomashaine | 0:e7352f4f3dcb | 341 | iter++; |
thomashaine | 0:e7352f4f3dcb | 342 | } |
thomashaine | 0:e7352f4f3dcb | 343 | |
thomashaine | 0:e7352f4f3dcb | 344 | sprintf(toprint,"cclk cal: %i -> cclk %i\r\n",cclk,res); |
thomashaine | 0:e7352f4f3dcb | 345 | myprint(toprint); |
thomashaine | 0:e7352f4f3dcb | 346 | TASK_SPI_WRITE(0x03,0x01); //mode drs |
thomashaine | 0:e7352f4f3dcb | 347 | |
thomashaine | 0:e7352f4f3dcb | 348 | } |
thomashaine | 0:e7352f4f3dcb | 349 |