Changes done in ECG and BT
Dependencies: SDFileSystem ds3231 eeprom_Nikita mbed testUniGraphic_150217
Fork of merged_code2_20sept_2017_4th_oct_2017 by
ecgg.cpp@1:8316c23ec6b9, 2017-01-30 (annotated)
- Committer:
- nikitateggi
- Date:
- Mon Jan 30 07:45:41 2017 +0000
- Revision:
- 1:8316c23ec6b9
- Child:
- 3:9a06c2bed650
POC code
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nikitateggi | 1:8316c23ec6b9 | 1 | /** ECG ADS1291 Test program. |
nikitateggi | 1:8316c23ec6b9 | 2 | ADS1291 is a single channel ECG chip |
nikitateggi | 1:8316c23ec6b9 | 3 | with a 24 bit Sigma-Delta ADC |
nikitateggi | 1:8316c23ec6b9 | 4 | */ |
nikitateggi | 1:8316c23ec6b9 | 5 | |
nikitateggi | 1:8316c23ec6b9 | 6 | #include "mbed.h" |
nikitateggi | 1:8316c23ec6b9 | 7 | //#include "timer.h" |
nikitateggi | 1:8316c23ec6b9 | 8 | #include <string.h> |
nikitateggi | 1:8316c23ec6b9 | 9 | #include <stdio.h> |
nikitateggi | 1:8316c23ec6b9 | 10 | #include "ds3231.h" |
nikitateggi | 1:8316c23ec6b9 | 11 | #include "SDFileSystem.h" |
nikitateggi | 1:8316c23ec6b9 | 12 | #include "ecg_dec.h" |
nikitateggi | 1:8316c23ec6b9 | 13 | #include "rtc.h" |
nikitateggi | 1:8316c23ec6b9 | 14 | #include "sdcard.h" |
nikitateggi | 1:8316c23ec6b9 | 15 | //#include "rtos.h" |
nikitateggi | 1:8316c23ec6b9 | 16 | |
nikitateggi | 1:8316c23ec6b9 | 17 | //SPI SETTINGS |
nikitateggi | 1:8316c23ec6b9 | 18 | #define baud_rate 115200 |
nikitateggi | 1:8316c23ec6b9 | 19 | #define freq 1000000 |
nikitateggi | 1:8316c23ec6b9 | 20 | #define bits 8 |
nikitateggi | 1:8316c23ec6b9 | 21 | #define mode 1 |
nikitateggi | 1:8316c23ec6b9 | 22 | #define loop 1000000 |
nikitateggi | 1:8316c23ec6b9 | 23 | |
nikitateggi | 1:8316c23ec6b9 | 24 | //PIN DECLARATIONS |
nikitateggi | 1:8316c23ec6b9 | 25 | Serial pc(USBTX,USBRX); |
nikitateggi | 1:8316c23ec6b9 | 26 | DigitalIn DRDY_BAR(PTC8); |
nikitateggi | 1:8316c23ec6b9 | 27 | DigitalOut CHIPSEL_BAR(PTD0); |
nikitateggi | 1:8316c23ec6b9 | 28 | DigitalOut ADS_START(PTC16); |
nikitateggi | 1:8316c23ec6b9 | 29 | DigitalOut RESET_BAR(PTC17); |
nikitateggi | 1:8316c23ec6b9 | 30 | |
nikitateggi | 1:8316c23ec6b9 | 31 | //FILE *fp = NULL; |
nikitateggi | 1:8316c23ec6b9 | 32 | |
nikitateggi | 1:8316c23ec6b9 | 33 | |
nikitateggi | 1:8316c23ec6b9 | 34 | //unsigned char chk2= 0; |
nikitateggi | 1:8316c23ec6b9 | 35 | float value = 0; |
nikitateggi | 1:8316c23ec6b9 | 36 | unsigned int value1 = 0; |
nikitateggi | 1:8316c23ec6b9 | 37 | unsigned int value2 = 0; |
nikitateggi | 1:8316c23ec6b9 | 38 | unsigned int value3 = 0; |
nikitateggi | 1:8316c23ec6b9 | 39 | unsigned int value4 = 0; |
nikitateggi | 1:8316c23ec6b9 | 40 | unsigned int value5 = 0; |
nikitateggi | 1:8316c23ec6b9 | 41 | unsigned int value6 = 0; |
nikitateggi | 1:8316c23ec6b9 | 42 | unsigned int data1 = 0; |
nikitateggi | 1:8316c23ec6b9 | 43 | unsigned int count = 0; |
nikitateggi | 1:8316c23ec6b9 | 44 | |
nikitateggi | 1:8316c23ec6b9 | 45 | //static int32_t ecg_x[500]; |
nikitateggi | 1:8316c23ec6b9 | 46 | SPI mySpi(PIN_MOSI, PIN_MISO, PIN_SCLK) ; /* SPI is the spi function written in mbed and mySpi is the instance name*/ |
nikitateggi | 1:8316c23ec6b9 | 47 | |
nikitateggi | 1:8316c23ec6b9 | 48 | typedef void (*func_ptr)(void) ; /*creates typedef to function pointer, does not take any arguement and returns void*/ |
nikitateggi | 1:8316c23ec6b9 | 49 | |
nikitateggi | 1:8316c23ec6b9 | 50 | void doHelp(void) ; |
nikitateggi | 1:8316c23ec6b9 | 51 | void doStatus(void) ; |
nikitateggi | 1:8316c23ec6b9 | 52 | void doFreq(void) ; |
nikitateggi | 1:8316c23ec6b9 | 53 | void doMode(void) ; |
nikitateggi | 1:8316c23ec6b9 | 54 | void doBit(void) ; |
nikitateggi | 1:8316c23ec6b9 | 55 | void doWrite(void) ; |
nikitateggi | 1:8316c23ec6b9 | 56 | void doWrite2(void) ; |
nikitateggi | 1:8316c23ec6b9 | 57 | void doRead(void) ; |
nikitateggi | 1:8316c23ec6b9 | 58 | void doLoop(void) ; |
nikitateggi | 1:8316c23ec6b9 | 59 | void setup(void); |
nikitateggi | 1:8316c23ec6b9 | 60 | void testsetup(void); |
nikitateggi | 1:8316c23ec6b9 | 61 | void ecgsetup(void); |
nikitateggi | 1:8316c23ec6b9 | 62 | void regWrite(int,int); |
nikitateggi | 1:8316c23ec6b9 | 63 | void cmdWrite(int); |
nikitateggi | 1:8316c23ec6b9 | 64 | void regRead(int); |
nikitateggi | 1:8316c23ec6b9 | 65 | void lpf_coef(void); |
nikitateggi | 1:8316c23ec6b9 | 66 | float lpf( float coeff[5], float); |
nikitateggi | 1:8316c23ec6b9 | 67 | void drdy_int(void); |
nikitateggi | 1:8316c23ec6b9 | 68 | /*FILE *sd_openfile(char *buf); |
nikitateggi | 1:8316c23ec6b9 | 69 | void sd_write (FILE *n,int value); |
nikitateggi | 1:8316c23ec6b9 | 70 | void sd_close(FILE *n); |
nikitateggi | 1:8316c23ec6b9 | 71 | time_t rtc_read();*/ |
nikitateggi | 1:8316c23ec6b9 | 72 | |
nikitateggi | 1:8316c23ec6b9 | 73 | typedef struct _cmd_func { |
nikitateggi | 1:8316c23ec6b9 | 74 | char *name ; |
nikitateggi | 1:8316c23ec6b9 | 75 | func_ptr func ; |
nikitateggi | 1:8316c23ec6b9 | 76 | } cmd_func_type ; |
nikitateggi | 1:8316c23ec6b9 | 77 | |
nikitateggi | 1:8316c23ec6b9 | 78 | cmd_func_type cmd_list[] = { /*"cmd_func_type cmd_list[]" is same as "struct cmd_list[]"*/ |
nikitateggi | 1:8316c23ec6b9 | 79 | {"help", doHelp}, |
nikitateggi | 1:8316c23ec6b9 | 80 | {"status", doStatus}, |
nikitateggi | 1:8316c23ec6b9 | 81 | {"freq", doFreq}, |
nikitateggi | 1:8316c23ec6b9 | 82 | {"mode", doMode}, |
nikitateggi | 1:8316c23ec6b9 | 83 | {"bit", doBit}, |
nikitateggi | 1:8316c23ec6b9 | 84 | {"write", doWrite}, |
nikitateggi | 1:8316c23ec6b9 | 85 | {"read", doRead}, |
nikitateggi | 1:8316c23ec6b9 | 86 | {"loop", doLoop}, |
nikitateggi | 1:8316c23ec6b9 | 87 | { 0, 0 } |
nikitateggi | 1:8316c23ec6b9 | 88 | } ; |
nikitateggi | 1:8316c23ec6b9 | 89 | |
nikitateggi | 1:8316c23ec6b9 | 90 | func_ptr getFunc(char *cmd) /*here "func_ptr is same as void*/ |
nikitateggi | 1:8316c23ec6b9 | 91 | { |
nikitateggi | 1:8316c23ec6b9 | 92 | int i = 0 ; |
nikitateggi | 1:8316c23ec6b9 | 93 | while(cmd_list[i].name != 0) { |
nikitateggi | 1:8316c23ec6b9 | 94 | if (strcmp(cmd, cmd_list[i].name) == 0) { |
nikitateggi | 1:8316c23ec6b9 | 95 | return(cmd_list[i].func) ; ; |
nikitateggi | 1:8316c23ec6b9 | 96 | } |
nikitateggi | 1:8316c23ec6b9 | 97 | i++ ; |
nikitateggi | 1:8316c23ec6b9 | 98 | } |
nikitateggi | 1:8316c23ec6b9 | 99 | return(0) ; |
nikitateggi | 1:8316c23ec6b9 | 100 | } |
nikitateggi | 1:8316c23ec6b9 | 101 | |
nikitateggi | 1:8316c23ec6b9 | 102 | void doHello() |
nikitateggi | 1:8316c23ec6b9 | 103 | { |
nikitateggi | 1:8316c23ec6b9 | 104 | printf("=== spi test program ===\n\r") ; |
nikitateggi | 1:8316c23ec6b9 | 105 | printf("please set your terminal program\n\r") ; |
nikitateggi | 1:8316c23ec6b9 | 106 | printf("local echo on\n\r") ; |
nikitateggi | 1:8316c23ec6b9 | 107 | printf("\n\r") ; |
nikitateggi | 1:8316c23ec6b9 | 108 | } |
nikitateggi | 1:8316c23ec6b9 | 109 | |
nikitateggi | 1:8316c23ec6b9 | 110 | int ecg(int pid) { |
nikitateggi | 1:8316c23ec6b9 | 111 | //time_t epoch_time; |
nikitateggi | 1:8316c23ec6b9 | 112 | //int fp; |
nikitateggi | 1:8316c23ec6b9 | 113 | int concatenate_value = 0; |
nikitateggi | 1:8316c23ec6b9 | 114 | |
nikitateggi | 1:8316c23ec6b9 | 115 | //uint32_t ecg_buf[1500]; |
nikitateggi | 1:8316c23ec6b9 | 116 | //int32_t ecg_der[1500]; |
nikitateggi | 1:8316c23ec6b9 | 117 | //int buff1[15]= {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; |
nikitateggi | 1:8316c23ec6b9 | 118 | int32_t sample_sum; // static int32_t avg[1500]; |
nikitateggi | 1:8316c23ec6b9 | 119 | count = 0; int32_t fs = 500; int32_t w=0,w1=0; |
nikitateggi | 1:8316c23ec6b9 | 120 | Timer t; |
nikitateggi | 1:8316c23ec6b9 | 121 | unsigned char chk = 1; |
nikitateggi | 1:8316c23ec6b9 | 122 | pc.baud(baud_rate); |
nikitateggi | 1:8316c23ec6b9 | 123 | mySpi.frequency(freq) ; |
nikitateggi | 1:8316c23ec6b9 | 124 | mySpi.format(bits, mode) ; |
nikitateggi | 1:8316c23ec6b9 | 125 | // Ds3231 rtc(PTC11, PTC10); //sda,scl2yy |
nikitateggi | 1:8316c23ec6b9 | 126 | |
nikitateggi | 1:8316c23ec6b9 | 127 | |
nikitateggi | 1:8316c23ec6b9 | 128 | |
nikitateggi | 1:8316c23ec6b9 | 129 | //Initialize ADS1291 |
nikitateggi | 1:8316c23ec6b9 | 130 | setup() ; |
nikitateggi | 1:8316c23ec6b9 | 131 | |
nikitateggi | 1:8316c23ec6b9 | 132 | //Put device back in RDATAC mode |
nikitateggi | 1:8316c23ec6b9 | 133 | cmdWrite(CMD_RDATAC) ; |
nikitateggi | 1:8316c23ec6b9 | 134 | // printf("The device is in RDATAC mode\n"); |
nikitateggi | 1:8316c23ec6b9 | 135 | //Activate conversion, At this point DRDY should toggle |
nikitateggi | 1:8316c23ec6b9 | 136 | ADS_START = 1; |
nikitateggi | 1:8316c23ec6b9 | 137 | |
nikitateggi | 1:8316c23ec6b9 | 138 | // printf( "ADC noise data is= \n"); |
nikitateggi | 1:8316c23ec6b9 | 139 | for(int i=0;i<N_ERR;i++) |
nikitateggi | 1:8316c23ec6b9 | 140 | { |
nikitateggi | 1:8316c23ec6b9 | 141 | while(DRDY_BAR); |
nikitateggi | 1:8316c23ec6b9 | 142 | doWrite() ; |
nikitateggi | 1:8316c23ec6b9 | 143 | //printf( "ADC status is= %X %X %X\n", value1,value2,value3); |
nikitateggi | 1:8316c23ec6b9 | 144 | concatenate_value = ((value4<< LSHIFT_16)|(value5 <<LSHIFT_8) |(value6)); |
nikitateggi | 1:8316c23ec6b9 | 145 | // printf( "%X\n", concatenate_value); |
nikitateggi | 1:8316c23ec6b9 | 146 | } |
nikitateggi | 1:8316c23ec6b9 | 147 | |
nikitateggi | 1:8316c23ec6b9 | 148 | |
nikitateggi | 1:8316c23ec6b9 | 149 | /*testsetup(); // To set the ADS1291 registers for internal test signal generation |
nikitateggi | 1:8316c23ec6b9 | 150 | wait(0.2); |
nikitateggi | 1:8316c23ec6b9 | 151 | cmdWrite(CMD_RDATAC) ; |
nikitateggi | 1:8316c23ec6b9 | 152 | //printf("The device is in RDATAC mode\n"); |
nikitateggi | 1:8316c23ec6b9 | 153 | // printf("Test data is\n"); |
nikitateggi | 1:8316c23ec6b9 | 154 | ADS_START = 1; |
nikitateggi | 1:8316c23ec6b9 | 155 | //printf("Test data is\n"); |
nikitateggi | 1:8316c23ec6b9 | 156 | sd_open_file(); |
nikitateggi | 1:8316c23ec6b9 | 157 | |
nikitateggi | 1:8316c23ec6b9 | 158 | for(int j=0;j<N_TEST;j++) |
nikitateggi | 1:8316c23ec6b9 | 159 | { |
nikitateggi | 1:8316c23ec6b9 | 160 | while(DRDY_BAR); |
nikitateggi | 1:8316c23ec6b9 | 161 | doWrite() ; |
nikitateggi | 1:8316c23ec6b9 | 162 | concatenate_value = ((value4<< LSHIFT_16)|(value5 <<LSHIFT_8) |(value6)); |
nikitateggi | 1:8316c23ec6b9 | 163 | |
nikitateggi | 1:8316c23ec6b9 | 164 | sd_write(concatenate_value); |
nikitateggi | 1:8316c23ec6b9 | 165 | pc.printf("%d\n",concatenate_value); |
nikitateggi | 1:8316c23ec6b9 | 166 | // fprintf(fp,"%d\n",concatenate_value) ; |
nikitateggi | 1:8316c23ec6b9 | 167 | } |
nikitateggi | 1:8316c23ec6b9 | 168 | sd_close();*/ |
nikitateggi | 1:8316c23ec6b9 | 169 | |
nikitateggi | 1:8316c23ec6b9 | 170 | ecgsetup(); // To set the ADS1291 registers for ECG signal generation |
nikitateggi | 1:8316c23ec6b9 | 171 | // printf("\n Reading****"); |
nikitateggi | 1:8316c23ec6b9 | 172 | //DRDY_BAR1.enable_irq(); |
nikitateggi | 1:8316c23ec6b9 | 173 | cmdWrite(CMD_RDATAC) ; |
nikitateggi | 1:8316c23ec6b9 | 174 | // printf("The device is in RDATAC mode\n"); |
nikitateggi | 1:8316c23ec6b9 | 175 | ADS_START = 1; |
nikitateggi | 1:8316c23ec6b9 | 176 | chk = 1; |
nikitateggi | 1:8316c23ec6b9 | 177 | t.start(); |
nikitateggi | 1:8316c23ec6b9 | 178 | sd_open_ECGfile(pid); |
nikitateggi | 1:8316c23ec6b9 | 179 | for(int i=0; i<N_ECG; i++) |
nikitateggi | 1:8316c23ec6b9 | 180 | { |
nikitateggi | 1:8316c23ec6b9 | 181 | while(DRDY_BAR); |
nikitateggi | 1:8316c23ec6b9 | 182 | doWrite() ; |
nikitateggi | 1:8316c23ec6b9 | 183 | concatenate_value = ((value4<< 16)|(value5 <<8) |(value6)); |
nikitateggi | 1:8316c23ec6b9 | 184 | sd_write(concatenate_value); |
nikitateggi | 1:8316c23ec6b9 | 185 | //printf( "%d\n", concatenate_value); |
nikitateggi | 1:8316c23ec6b9 | 186 | } |
nikitateggi | 1:8316c23ec6b9 | 187 | sd_close(); |
nikitateggi | 1:8316c23ec6b9 | 188 | t.stop(); |
nikitateggi | 1:8316c23ec6b9 | 189 | printf( "%f\n", t.read()); |
nikitateggi | 1:8316c23ec6b9 | 190 | /* |
nikitateggi | 1:8316c23ec6b9 | 191 | // remove DC of ecg signal |
nikitateggi | 1:8316c23ec6b9 | 192 | uint32_t ecg_mean=0, buf_length = 500; |
nikitateggi | 1:8316c23ec6b9 | 193 | printf("DC removed\ ECG Data\n"); |
nikitateggi | 1:8316c23ec6b9 | 194 | for (int k=0 ; k<buf_length ; k++ ) |
nikitateggi | 1:8316c23ec6b9 | 195 | {ecg_mean = ecg_mean+ ecg_buf[k] ; |
nikitateggi | 1:8316c23ec6b9 | 196 | /// printf("ecg_mean is %d\n",ecg_mean); |
nikitateggi | 1:8316c23ec6b9 | 197 | } |
nikitateggi | 1:8316c23ec6b9 | 198 | ecg_mean =ecg_mean/buf_length ; |
nikitateggi | 1:8316c23ec6b9 | 199 | //printf("ecg_mean is %d\n",ecg_mean); |
nikitateggi | 1:8316c23ec6b9 | 200 | for (k=0 ; k<buf_length ; k++ ) |
nikitateggi | 1:8316c23ec6b9 | 201 | { |
nikitateggi | 1:8316c23ec6b9 | 202 | ecg_x[k] = ecg_buf[k] - ecg_mean ; |
nikitateggi | 1:8316c23ec6b9 | 203 | printf("%d\n",(ecg_x[k])); |
nikitateggi | 1:8316c23ec6b9 | 204 | } |
nikitateggi | 1:8316c23ec6b9 | 205 | */ |
nikitateggi | 1:8316c23ec6b9 | 206 | // 4 pt Moving Average |
nikitateggi | 1:8316c23ec6b9 | 207 | /*printf("Averaged ECG Data\n"); |
nikitateggi | 1:8316c23ec6b9 | 208 | for(int k=0; k<N_ECG; k++) |
nikitateggi | 1:8316c23ec6b9 | 209 | { |
nikitateggi | 1:8316c23ec6b9 | 210 | // sample_sum= ( ecg_x[k]+ecg_x[k+1]+ ecg_x[k+2]+ ecg_x[k+3]); |
nikitateggi | 1:8316c23ec6b9 | 211 | sample_sum= ( ecg_buf[k]+ecg_buf[k+1]+ ecg_buf[k+2]+ ecg_buf[k+3]); |
nikitateggi | 1:8316c23ec6b9 | 212 | ecg_buf[k]= sample_sum/(int32_t)4; |
nikitateggi | 1:8316c23ec6b9 | 213 | |
nikitateggi | 1:8316c23ec6b9 | 214 | // printf("%d\n",(ecg_buf[k])); |
nikitateggi | 1:8316c23ec6b9 | 215 | } |
nikitateggi | 1:8316c23ec6b9 | 216 | |
nikitateggi | 1:8316c23ec6b9 | 217 | // Derivative filtering of the averaged ECG signal |
nikitateggi | 1:8316c23ec6b9 | 218 | // printf("Derivative filtered ECG Data\n"); |
nikitateggi | 1:8316c23ec6b9 | 219 | ecg_der[0]=0; |
nikitateggi | 1:8316c23ec6b9 | 220 | for(int k=0; k<N_ECG-3; k++) |
nikitateggi | 1:8316c23ec6b9 | 221 | { |
nikitateggi | 1:8316c23ec6b9 | 222 | w1= (fs*(ecg_buf[k+1]-ecg_buf[k])); |
nikitateggi | 1:8316c23ec6b9 | 223 | //w1=1; |
nikitateggi | 1:8316c23ec6b9 | 224 | // w=0.995; |
nikitateggi | 1:8316c23ec6b9 | 225 | // ecg_der[k]=(w+w1); |
nikitateggi | 1:8316c23ec6b9 | 226 | w=(1*ecg_der[k]); |
nikitateggi | 1:8316c23ec6b9 | 227 | ecg_der[k+1]=(w1+w); |
nikitateggi | 1:8316c23ec6b9 | 228 | // printf("ecg_der[k+1]=%d\n",(ecg_der[k+1])); |
nikitateggi | 1:8316c23ec6b9 | 229 | // printf("w=%f\n", w); |
nikitateggi | 1:8316c23ec6b9 | 230 | // printf("w1=%f\n", w1); |
nikitateggi | 1:8316c23ec6b9 | 231 | // printf("%d\n",(ecg_der[k])); |
nikitateggi | 1:8316c23ec6b9 | 232 | //printf("k=%d\n", k); |
nikitateggi | 1:8316c23ec6b9 | 233 | }*/ |
nikitateggi | 1:8316c23ec6b9 | 234 | } // End of main function |
nikitateggi | 1:8316c23ec6b9 | 235 | |
nikitateggi | 1:8316c23ec6b9 | 236 | //for initial configurations |
nikitateggi | 1:8316c23ec6b9 | 237 | void setup() |
nikitateggi | 1:8316c23ec6b9 | 238 | { |
nikitateggi | 1:8316c23ec6b9 | 239 | //initially make all inputs low until power is up and stabilized |
nikitateggi | 1:8316c23ec6b9 | 240 | CHIPSEL_BAR = 0; |
nikitateggi | 1:8316c23ec6b9 | 241 | ADS_START = 0; |
nikitateggi | 1:8316c23ec6b9 | 242 | RESET_BAR = 0; |
nikitateggi | 1:8316c23ec6b9 | 243 | //wait for oscillator to wake up |
nikitateggi | 1:8316c23ec6b9 | 244 | wait(1); |
nikitateggi | 1:8316c23ec6b9 | 245 | |
nikitateggi | 1:8316c23ec6b9 | 246 | CHIPSEL_BAR = 1; |
nikitateggi | 1:8316c23ec6b9 | 247 | RESET_BAR = 1; //wait for tpor time |
nikitateggi | 1:8316c23ec6b9 | 248 | //wait for power on reset |
nikitateggi | 1:8316c23ec6b9 | 249 | wait(1); |
nikitateggi | 1:8316c23ec6b9 | 250 | RESET_BAR = 0; // send a reset pulse and wait for t_reset amount of time |
nikitateggi | 1:8316c23ec6b9 | 251 | wait(1); |
nikitateggi | 1:8316c23ec6b9 | 252 | RESET_BAR = 1;//release the reset |
nikitateggi | 1:8316c23ec6b9 | 253 | //Wait for 18 tclks = 36 us |
nikitateggi | 1:8316c23ec6b9 | 254 | wait_us(36); |
nikitateggi | 1:8316c23ec6b9 | 255 | |
nikitateggi | 1:8316c23ec6b9 | 256 | //Device wakes up in RDATAC mode so send SDATAC command to write to registers |
nikitateggi | 1:8316c23ec6b9 | 257 | cmdWrite(CMD_SDATAC) ; |
nikitateggi | 1:8316c23ec6b9 | 258 | // printf("Device is in SDATAC mode\n"); |
nikitateggi | 1:8316c23ec6b9 | 259 | |
nikitateggi | 1:8316c23ec6b9 | 260 | regRead(REG_ID); |
nikitateggi | 1:8316c23ec6b9 | 261 | printf("DEVICE ID register read from ADS is= 0x%X\n",data1); |
nikitateggi | 1:8316c23ec6b9 | 262 | wait(1); |
nikitateggi | 1:8316c23ec6b9 | 263 | |
nikitateggi | 1:8316c23ec6b9 | 264 | //Since we are using internal 2.42V reference and enable clock on the CLK PIN |
nikitateggi | 1:8316c23ec6b9 | 265 | //Write 0xA8 to CONFIG2 register |
nikitateggi | 1:8316c23ec6b9 | 266 | regWrite(REG_CONFIG2,no_mode); |
nikitateggi | 1:8316c23ec6b9 | 267 | |
nikitateggi | 1:8316c23ec6b9 | 268 | //Set continuous sampling mode, 500 SPS |
nikitateggi | 1:8316c23ec6b9 | 269 | regWrite(REG_CONFIG1,sps500); |
nikitateggi | 1:8316c23ec6b9 | 270 | |
nikitateggi | 1:8316c23ec6b9 | 271 | //PGA Gain = 6, inputs shorted for noise measurements |
nikitateggi | 1:8316c23ec6b9 | 272 | regWrite(REG_CH1SET, offset_meas); |
nikitateggi | 1:8316c23ec6b9 | 273 | |
nikitateggi | 1:8316c23ec6b9 | 274 | //read data from CONFIG2 register |
nikitateggi | 1:8316c23ec6b9 | 275 | regRead(REG_CONFIG2); |
nikitateggi | 1:8316c23ec6b9 | 276 | // printf("REG_CONFIG2 register read from ADS for initial setup is= 0x%X\n",data1); |
nikitateggi | 1:8316c23ec6b9 | 277 | |
nikitateggi | 1:8316c23ec6b9 | 278 | //read data from CONFIG1 register |
nikitateggi | 1:8316c23ec6b9 | 279 | regRead(REG_CONFIG1); |
nikitateggi | 1:8316c23ec6b9 | 280 | // printf("REG_CONFIG1 register read from ADS for initial setup is= 0x%X\n",data1); |
nikitateggi | 1:8316c23ec6b9 | 281 | |
nikitateggi | 1:8316c23ec6b9 | 282 | //read data from CONFIG1 register |
nikitateggi | 1:8316c23ec6b9 | 283 | regRead(REG_CH1SET); |
nikitateggi | 1:8316c23ec6b9 | 284 | // printf("REG_CH1SET register read from ADS for initial setup is= 0x%X\n",data1); |
nikitateggi | 1:8316c23ec6b9 | 285 | } |
nikitateggi | 1:8316c23ec6b9 | 286 | |
nikitateggi | 1:8316c23ec6b9 | 287 | void testsetup() |
nikitateggi | 1:8316c23ec6b9 | 288 | { |
nikitateggi | 1:8316c23ec6b9 | 289 | //Send SDATAC command to write to registers to set test signals |
nikitateggi | 1:8316c23ec6b9 | 290 | cmdWrite(CMD_SDATAC) ; |
nikitateggi | 1:8316c23ec6b9 | 291 | printf("Device is in SDATAC mode\n"); |
nikitateggi | 1:8316c23ec6b9 | 292 | |
nikitateggi | 1:8316c23ec6b9 | 293 | //Since we are using internal 2.42V reference and enable clock on the CLK PIN |
nikitateggi | 1:8316c23ec6b9 | 294 | //Write 0xA3 to CONFIG2 register to set test signal and its freq = 1Hz |
nikitateggi | 1:8316c23ec6b9 | 295 | regWrite(REG_CONFIG2,test_mode); |
nikitateggi | 1:8316c23ec6b9 | 296 | |
nikitateggi | 1:8316c23ec6b9 | 297 | //PGA Gain = 6, test signal selected |
nikitateggi | 1:8316c23ec6b9 | 298 | regWrite(REG_CH1SET, test_inp); |
nikitateggi | 1:8316c23ec6b9 | 299 | |
nikitateggi | 1:8316c23ec6b9 | 300 | //read data from CONFIG2 register |
nikitateggi | 1:8316c23ec6b9 | 301 | regRead(REG_CONFIG2); |
nikitateggi | 1:8316c23ec6b9 | 302 | printf("REG_CONFIG2 register read from ADS for test signal setup is= 0x%X\n",data1); |
nikitateggi | 1:8316c23ec6b9 | 303 | |
nikitateggi | 1:8316c23ec6b9 | 304 | //read data from CONFIG1 register |
nikitateggi | 1:8316c23ec6b9 | 305 | regRead(REG_CH1SET); |
nikitateggi | 1:8316c23ec6b9 | 306 | printf("REG_CH1SET register read from ADS for test signal setup is= 0x%X\n",data1); |
nikitateggi | 1:8316c23ec6b9 | 307 | } |
nikitateggi | 1:8316c23ec6b9 | 308 | |
nikitateggi | 1:8316c23ec6b9 | 309 | void ecgsetup() |
nikitateggi | 1:8316c23ec6b9 | 310 | { |
nikitateggi | 1:8316c23ec6b9 | 311 | cmdWrite(CMD_SDATAC) ; //Set to SDATAC mode to set CH1SET register |
nikitateggi | 1:8316c23ec6b9 | 312 | |
nikitateggi | 1:8316c23ec6b9 | 313 | regWrite(REG_CONFIG1,sps500); |
nikitateggi | 1:8316c23ec6b9 | 314 | regWrite(REG_CONFIG2, default_mode);//put INT_TEST and TEST_FREQ bits to default mode(set those bits to '0') |
nikitateggi | 1:8316c23ec6b9 | 315 | regWrite(REG_LOFF, loff_conf); |
nikitateggi | 1:8316c23ec6b9 | 316 | regWrite(REG_CH1SET, elec_inp);// Set to read normal electrode input |
nikitateggi | 1:8316c23ec6b9 | 317 | regWrite(REG_RLD_SENS, rld_sens_sig);//Set RLD_SENS |
nikitateggi | 1:8316c23ec6b9 | 318 | regWrite(REG_LOFF_SENS, loff_sens_sig);//Set LOFF_SENS |
nikitateggi | 1:8316c23ec6b9 | 319 | regWrite(REG_MISC1, misc1_inp );//Set RESP1 |
nikitateggi | 1:8316c23ec6b9 | 320 | regWrite(REG_MISC2, misc2_inp );//Set RESP2 |
nikitateggi | 1:8316c23ec6b9 | 321 | //printf("connect ECG leads\n"); |
nikitateggi | 1:8316c23ec6b9 | 322 | wait(0.5); |
nikitateggi | 1:8316c23ec6b9 | 323 | regRead(REG_LOFF_STAT);//Read LOFF_STAT register |
nikitateggi | 1:8316c23ec6b9 | 324 | printf("REG_LOFF_STAT register read from ADS for ecg setup is= 0x%X\n",data1); |
nikitateggi | 1:8316c23ec6b9 | 325 | } |
nikitateggi | 1:8316c23ec6b9 | 326 | |
nikitateggi | 1:8316c23ec6b9 | 327 | void doHelp(void) |
nikitateggi | 1:8316c23ec6b9 | 328 | { |
nikitateggi | 1:8316c23ec6b9 | 329 | printf("=== spi test ===\n\r") ; |
nikitateggi | 1:8316c23ec6b9 | 330 | printf("commands available\n\r") ; |
nikitateggi | 1:8316c23ec6b9 | 331 | printf("help\n\r") ; |
nikitateggi | 1:8316c23ec6b9 | 332 | printf("status\n\r") ; |
nikitateggi | 1:8316c23ec6b9 | 333 | printf("freq freq_in_hz\n\r") ; |
nikitateggi | 1:8316c23ec6b9 | 334 | printf("mode (0 | 1 | 2 | 3)\n\r") ; |
nikitateggi | 1:8316c23ec6b9 | 335 | printf("bit (4 - 16)\n\r") ; |
nikitateggi | 1:8316c23ec6b9 | 336 | printf("write value\n\r") ; |
nikitateggi | 1:8316c23ec6b9 | 337 | printf("read\n\r") ; |
nikitateggi | 1:8316c23ec6b9 | 338 | printf("loop number (set repeat number for read/write)\n\r") ; |
nikitateggi | 1:8316c23ec6b9 | 339 | } |
nikitateggi | 1:8316c23ec6b9 | 340 | |
nikitateggi | 1:8316c23ec6b9 | 341 | void doStatus(void) |
nikitateggi | 1:8316c23ec6b9 | 342 | { |
nikitateggi | 1:8316c23ec6b9 | 343 | printf("=== Status Report ===\n\r") ; |
nikitateggi | 1:8316c23ec6b9 | 344 | printf("bits: %d\n\r", bits) ; |
nikitateggi | 1:8316c23ec6b9 | 345 | printf("mode: %d\n\r", mode) ; |
nikitateggi | 1:8316c23ec6b9 | 346 | printf("freq: %d Hz\n\r", freq) ; |
nikitateggi | 1:8316c23ec6b9 | 347 | printf("loop: %d\n\r", loop) ; |
nikitateggi | 1:8316c23ec6b9 | 348 | } |
nikitateggi | 1:8316c23ec6b9 | 349 | |
nikitateggi | 1:8316c23ec6b9 | 350 | void doFreq(void) |
nikitateggi | 1:8316c23ec6b9 | 351 | { |
nikitateggi | 1:8316c23ec6b9 | 352 | // int freq = 0 ; |
nikitateggi | 1:8316c23ec6b9 | 353 | // scanf("%d", &freq) ; |
nikitateggi | 1:8316c23ec6b9 | 354 | printf("setting frequency to %d\n\r", freq) ; |
nikitateggi | 1:8316c23ec6b9 | 355 | mySpi.frequency(freq) ; |
nikitateggi | 1:8316c23ec6b9 | 356 | } |
nikitateggi | 1:8316c23ec6b9 | 357 | |
nikitateggi | 1:8316c23ec6b9 | 358 | void doMode(void) |
nikitateggi | 1:8316c23ec6b9 | 359 | { |
nikitateggi | 1:8316c23ec6b9 | 360 | //scanf("%d", &mode) ; |
nikitateggi | 1:8316c23ec6b9 | 361 | printf("setting format(%d, %d)\n\r",bits, mode) ; |
nikitateggi | 1:8316c23ec6b9 | 362 | mySpi.format(bits, mode) ; |
nikitateggi | 1:8316c23ec6b9 | 363 | } |
nikitateggi | 1:8316c23ec6b9 | 364 | |
nikitateggi | 1:8316c23ec6b9 | 365 | void doBit(void) |
nikitateggi | 1:8316c23ec6b9 | 366 | { |
nikitateggi | 1:8316c23ec6b9 | 367 | // scanf("%d", &bits) ; |
nikitateggi | 1:8316c23ec6b9 | 368 | printf("setting format(%d, %d)\n\r",bits, mode) ; |
nikitateggi | 1:8316c23ec6b9 | 369 | mySpi.format(bits, mode) ; |
nikitateggi | 1:8316c23ec6b9 | 370 | } |
nikitateggi | 1:8316c23ec6b9 | 371 | |
nikitateggi | 1:8316c23ec6b9 | 372 | void doWrite(void) |
nikitateggi | 1:8316c23ec6b9 | 373 | { |
nikitateggi | 1:8316c23ec6b9 | 374 | |
nikitateggi | 1:8316c23ec6b9 | 375 | //DRDY_BAR1.disable_irq(); |
nikitateggi | 1:8316c23ec6b9 | 376 | |
nikitateggi | 1:8316c23ec6b9 | 377 | CHIPSEL_BAR = 0; |
nikitateggi | 1:8316c23ec6b9 | 378 | // int freq1 = 50000; |
nikitateggi | 1:8316c23ec6b9 | 379 | // mySpi.frequency(freq1) ; |
nikitateggi | 1:8316c23ec6b9 | 380 | value1 = mySpi.write(0x00); |
nikitateggi | 1:8316c23ec6b9 | 381 | value2 = mySpi.write(0x00); |
nikitateggi | 1:8316c23ec6b9 | 382 | value3 = mySpi.write(0x00); |
nikitateggi | 1:8316c23ec6b9 | 383 | value4 = mySpi.write(0x00); |
nikitateggi | 1:8316c23ec6b9 | 384 | value5 = mySpi.write(0x00); |
nikitateggi | 1:8316c23ec6b9 | 385 | value6 = mySpi.write(0x00); |
nikitateggi | 1:8316c23ec6b9 | 386 | |
nikitateggi | 1:8316c23ec6b9 | 387 | CHIPSEL_BAR = 0; |
nikitateggi | 1:8316c23ec6b9 | 388 | //DRDY_BAR1.enable_irq(); |
nikitateggi | 1:8316c23ec6b9 | 389 | } |
nikitateggi | 1:8316c23ec6b9 | 390 | |
nikitateggi | 1:8316c23ec6b9 | 391 | void doRead(void) |
nikitateggi | 1:8316c23ec6b9 | 392 | { |
nikitateggi | 1:8316c23ec6b9 | 393 | int dummy = 0 ; |
nikitateggi | 1:8316c23ec6b9 | 394 | |
nikitateggi | 1:8316c23ec6b9 | 395 | while(!DRDY_BAR) |
nikitateggi | 1:8316c23ec6b9 | 396 | ADS_START = 0; |
nikitateggi | 1:8316c23ec6b9 | 397 | |
nikitateggi | 1:8316c23ec6b9 | 398 | value = mySpi.write(dummy) ; |
nikitateggi | 1:8316c23ec6b9 | 399 | |
nikitateggi | 1:8316c23ec6b9 | 400 | } |
nikitateggi | 1:8316c23ec6b9 | 401 | |
nikitateggi | 1:8316c23ec6b9 | 402 | void doLoop(void) |
nikitateggi | 1:8316c23ec6b9 | 403 | { |
nikitateggi | 1:8316c23ec6b9 | 404 | // scanf("%d", &loop) ; |
nikitateggi | 1:8316c23ec6b9 | 405 | printf("repeat number has been set to %d\n\r", loop) ; |
nikitateggi | 1:8316c23ec6b9 | 406 | } |
nikitateggi | 1:8316c23ec6b9 | 407 | |
nikitateggi | 1:8316c23ec6b9 | 408 | void cmdWrite(int data) |
nikitateggi | 1:8316c23ec6b9 | 409 | { |
nikitateggi | 1:8316c23ec6b9 | 410 | CHIPSEL_BAR = 0; |
nikitateggi | 1:8316c23ec6b9 | 411 | mySpi.write(data) ; |
nikitateggi | 1:8316c23ec6b9 | 412 | wait_ms(1); |
nikitateggi | 1:8316c23ec6b9 | 413 | CHIPSEL_BAR = 1; |
nikitateggi | 1:8316c23ec6b9 | 414 | } |
nikitateggi | 1:8316c23ec6b9 | 415 | |
nikitateggi | 1:8316c23ec6b9 | 416 | void regWrite(int address, int data) |
nikitateggi | 1:8316c23ec6b9 | 417 | { |
nikitateggi | 1:8316c23ec6b9 | 418 | int data_to_send = CMD_WREG << 5; |
nikitateggi | 1:8316c23ec6b9 | 419 | data_to_send = data_to_send | address; |
nikitateggi | 1:8316c23ec6b9 | 420 | CHIPSEL_BAR = 0; |
nikitateggi | 1:8316c23ec6b9 | 421 | mySpi.write(data_to_send); |
nikitateggi | 1:8316c23ec6b9 | 422 | wait_ms(1); |
nikitateggi | 1:8316c23ec6b9 | 423 | mySpi.write(0x00) ; |
nikitateggi | 1:8316c23ec6b9 | 424 | wait_ms(1); |
nikitateggi | 1:8316c23ec6b9 | 425 | mySpi.write(data) ; |
nikitateggi | 1:8316c23ec6b9 | 426 | wait_ms(1); |
nikitateggi | 1:8316c23ec6b9 | 427 | CHIPSEL_BAR = 1; |
nikitateggi | 1:8316c23ec6b9 | 428 | } |
nikitateggi | 1:8316c23ec6b9 | 429 | |
nikitateggi | 1:8316c23ec6b9 | 430 | void regRead(int address) |
nikitateggi | 1:8316c23ec6b9 | 431 | { |
nikitateggi | 1:8316c23ec6b9 | 432 | int data_to_receive = CMD_RREG << 5; |
nikitateggi | 1:8316c23ec6b9 | 433 | data_to_receive = data_to_receive | address; |
nikitateggi | 1:8316c23ec6b9 | 434 | CHIPSEL_BAR = 0; |
nikitateggi | 1:8316c23ec6b9 | 435 | mySpi.write(data_to_receive); |
nikitateggi | 1:8316c23ec6b9 | 436 | wait_ms(1); |
nikitateggi | 1:8316c23ec6b9 | 437 | mySpi.write(0x00) ; |
nikitateggi | 1:8316c23ec6b9 | 438 | wait_ms(1); |
nikitateggi | 1:8316c23ec6b9 | 439 | data1 = mySpi.write(0x00) ; |
nikitateggi | 1:8316c23ec6b9 | 440 | |
nikitateggi | 1:8316c23ec6b9 | 441 | //printf("DEVICE ID register read from ADS is= 0x%X\n",data1); |
nikitateggi | 1:8316c23ec6b9 | 442 | |
nikitateggi | 1:8316c23ec6b9 | 443 | //wait_ms(1); |
nikitateggi | 1:8316c23ec6b9 | 444 | CHIPSEL_BAR = 1; |
nikitateggi | 1:8316c23ec6b9 | 445 | } |