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