![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
ak;jglif;awjd
Sentral.cpp@4:1e5639a2a3ae, 2016-05-20 (annotated)
- Committer:
- awmiller
- Date:
- Fri May 20 17:24:57 2016 +0000
- Revision:
- 4:1e5639a2a3ae
sentral tester
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
awmiller | 4:1e5639a2a3ae | 1 | //STM32 Tracler ports |
awmiller | 4:1e5639a2a3ae | 2 | #include "mbed.h" |
awmiller | 4:1e5639a2a3ae | 3 | |
awmiller | 4:1e5639a2a3ae | 4 | #define SENTRAL_DI02 |
awmiller | 4:1e5639a2a3ae | 5 | |
awmiller | 4:1e5639a2a3ae | 6 | #define TRUE 1 |
awmiller | 4:1e5639a2a3ae | 7 | #define FALSE !TRUE |
awmiller | 4:1e5639a2a3ae | 8 | |
awmiller | 4:1e5639a2a3ae | 9 | #include "Sentral.h" |
awmiller | 4:1e5639a2a3ae | 10 | |
awmiller | 4:1e5639a2a3ae | 11 | DigitalOut SentralPower(PTA5); |
awmiller | 4:1e5639a2a3ae | 12 | DigitalOut SA0(PTA4); |
awmiller | 4:1e5639a2a3ae | 13 | |
awmiller | 4:1e5639a2a3ae | 14 | void SA0_init() |
awmiller | 4:1e5639a2a3ae | 15 | { |
awmiller | 4:1e5639a2a3ae | 16 | // SA0.mode(PullDown); |
awmiller | 4:1e5639a2a3ae | 17 | Set_SA0(0); |
awmiller | 4:1e5639a2a3ae | 18 | } |
awmiller | 4:1e5639a2a3ae | 19 | |
awmiller | 4:1e5639a2a3ae | 20 | int Set_SA0(int val){ |
awmiller | 4:1e5639a2a3ae | 21 | |
awmiller | 4:1e5639a2a3ae | 22 | SA0 = (val > 0); |
awmiller | 4:1e5639a2a3ae | 23 | return SA0; |
awmiller | 4:1e5639a2a3ae | 24 | } |
awmiller | 4:1e5639a2a3ae | 25 | |
awmiller | 4:1e5639a2a3ae | 26 | int Sentral_Power(int val){ |
awmiller | 4:1e5639a2a3ae | 27 | |
awmiller | 4:1e5639a2a3ae | 28 | SentralPower = (val > 0); |
awmiller | 4:1e5639a2a3ae | 29 | return SentralPower; |
awmiller | 4:1e5639a2a3ae | 30 | } |
awmiller | 4:1e5639a2a3ae | 31 | |
awmiller | 4:1e5639a2a3ae | 32 | void Sentral_Reset(){ |
awmiller | 4:1e5639a2a3ae | 33 | |
awmiller | 4:1e5639a2a3ae | 34 | Sentral_Power(0); |
awmiller | 4:1e5639a2a3ae | 35 | wait(1); |
awmiller | 4:1e5639a2a3ae | 36 | Sentral_Power(1); |
awmiller | 4:1e5639a2a3ae | 37 | |
awmiller | 4:1e5639a2a3ae | 38 | } |
awmiller | 4:1e5639a2a3ae | 39 | int Sentral_pass_through(Serial &pc,I2C &master,int recursion) |
awmiller | 4:1e5639a2a3ae | 40 | { |
awmiller | 4:1e5639a2a3ae | 41 | if(recursion > 20) |
awmiller | 4:1e5639a2a3ae | 42 | { |
awmiller | 4:1e5639a2a3ae | 43 | pc.printf("Recursion limit reached \r\n"); |
awmiller | 4:1e5639a2a3ae | 44 | return 0; |
awmiller | 4:1e5639a2a3ae | 45 | } |
awmiller | 4:1e5639a2a3ae | 46 | |
awmiller | 4:1e5639a2a3ae | 47 | char cmd[3]; |
awmiller | 4:1e5639a2a3ae | 48 | char chk[3]; |
awmiller | 4:1e5639a2a3ae | 49 | |
awmiller | 4:1e5639a2a3ae | 50 | |
awmiller | 4:1e5639a2a3ae | 51 | //Serial_PutString("Sentral Reset Reg", 20); |
awmiller | 4:1e5639a2a3ae | 52 | cmd[0] = PASSTHROUGH_CONFIG_REG; |
awmiller | 4:1e5639a2a3ae | 53 | cmd[1]=0x01; |
awmiller | 4:1e5639a2a3ae | 54 | int check = master.write(SA0 ? AddrSA0_hi : SENTRAL_ADDRESS,cmd,2); |
awmiller | 4:1e5639a2a3ae | 55 | |
awmiller | 4:1e5639a2a3ae | 56 | //cmd[0] = 0; |
awmiller | 4:1e5639a2a3ae | 57 | |
awmiller | 4:1e5639a2a3ae | 58 | if(check) |
awmiller | 4:1e5639a2a3ae | 59 | pc.printf("Pass through request fail, I2C write fail \r\n",check); |
awmiller | 4:1e5639a2a3ae | 60 | else |
awmiller | 4:1e5639a2a3ae | 61 | { |
awmiller | 4:1e5639a2a3ae | 62 | check = master.write(SA0 ? AddrSA0_hi : SENTRAL_ADDRESS,cmd,1); |
awmiller | 4:1e5639a2a3ae | 63 | check = master.read(SA0 ? AddrSA0_hi : SENTRAL_ADDRESS,chk,1); |
awmiller | 4:1e5639a2a3ae | 64 | |
awmiller | 4:1e5639a2a3ae | 65 | if(check) |
awmiller | 4:1e5639a2a3ae | 66 | pc.printf("Pass through register read fail. \r\n"); |
awmiller | 4:1e5639a2a3ae | 67 | else |
awmiller | 4:1e5639a2a3ae | 68 | { |
awmiller | 4:1e5639a2a3ae | 69 | if(chk[0] == 0x01) |
awmiller | 4:1e5639a2a3ae | 70 | { |
awmiller | 4:1e5639a2a3ae | 71 | pc.printf("Pass through Success \r\n"); |
awmiller | 4:1e5639a2a3ae | 72 | return 1; |
awmiller | 4:1e5639a2a3ae | 73 | } |
awmiller | 4:1e5639a2a3ae | 74 | else |
awmiller | 4:1e5639a2a3ae | 75 | { |
awmiller | 4:1e5639a2a3ae | 76 | pc.printf("Recursion %d failed \r\n", recursion); |
awmiller | 4:1e5639a2a3ae | 77 | return Sentral_pass_through(pc,master,++recursion); |
awmiller | 4:1e5639a2a3ae | 78 | } |
awmiller | 4:1e5639a2a3ae | 79 | } |
awmiller | 4:1e5639a2a3ae | 80 | |
awmiller | 4:1e5639a2a3ae | 81 | |
awmiller | 4:1e5639a2a3ae | 82 | |
awmiller | 4:1e5639a2a3ae | 83 | } |
awmiller | 4:1e5639a2a3ae | 84 | } |
awmiller | 4:1e5639a2a3ae | 85 | |
awmiller | 4:1e5639a2a3ae | 86 | |
awmiller | 4:1e5639a2a3ae | 87 | //I2C master(p28, p27); |
awmiller | 4:1e5639a2a3ae | 88 | //Serial pc(USBTX,USBRX); |
awmiller | 4:1e5639a2a3ae | 89 | |
awmiller | 4:1e5639a2a3ae | 90 | |
awmiller | 4:1e5639a2a3ae | 91 | /******************************************************************************* |
awmiller | 4:1e5639a2a3ae | 92 | * Function Name : SentralSetup |
awmiller | 4:1e5639a2a3ae | 93 | * Description : code snippet for setting up sentral |
awmiller | 4:1e5639a2a3ae | 94 | * to run. |
awmiller | 4:1e5639a2a3ae | 95 | * Return : None |
awmiller | 4:1e5639a2a3ae | 96 | *******************************************************************************/ |
awmiller | 4:1e5639a2a3ae | 97 | unsigned int SentralSetup(Serial &pc,I2C &master){ |
awmiller | 4:1e5639a2a3ae | 98 | |
awmiller | 4:1e5639a2a3ae | 99 | |
awmiller | 4:1e5639a2a3ae | 100 | Sentral_Reset(); |
awmiller | 4:1e5639a2a3ae | 101 | pc.printf("Reset"); |
awmiller | 4:1e5639a2a3ae | 102 | |
awmiller | 4:1e5639a2a3ae | 103 | unsigned char /*temp[1],*/boot_timeout=0,count=0x00; |
awmiller | 4:1e5639a2a3ae | 104 | //unsigned char test = 0, ReturnedByte=0x00; |
awmiller | 4:1e5639a2a3ae | 105 | //int ret_status= 0x00; |
awmiller | 4:1e5639a2a3ae | 106 | |
awmiller | 4:1e5639a2a3ae | 107 | char cmd[10]; |
awmiller | 4:1e5639a2a3ae | 108 | |
awmiller | 4:1e5639a2a3ae | 109 | |
awmiller | 4:1e5639a2a3ae | 110 | //Serial_PutString("Sentral Reset Reg", 20); |
awmiller | 4:1e5639a2a3ae | 111 | cmd[0] = RESET_REQ_REG; |
awmiller | 4:1e5639a2a3ae | 112 | cmd[1]=0x01; |
awmiller | 4:1e5639a2a3ae | 113 | int check = master.write(SA0 ? AddrSA0_hi : SENTRAL_ADDRESS,cmd,2); |
awmiller | 4:1e5639a2a3ae | 114 | |
awmiller | 4:1e5639a2a3ae | 115 | cmd[0] = 0; |
awmiller | 4:1e5639a2a3ae | 116 | |
awmiller | 4:1e5639a2a3ae | 117 | if(check) |
awmiller | 4:1e5639a2a3ae | 118 | pc.printf("I2C Reset Request Fail, Check pull-ups and Reset \r\n",check); |
awmiller | 4:1e5639a2a3ae | 119 | else |
awmiller | 4:1e5639a2a3ae | 120 | pc.printf("Reset Request Pass \r\n"); |
awmiller | 4:1e5639a2a3ae | 121 | |
awmiller | 4:1e5639a2a3ae | 122 | pc.printf("Status Check: "); |
awmiller | 4:1e5639a2a3ae | 123 | //while(((ReturnedByte & 0x06) != 2)&&(boot_timeout==0)){ |
awmiller | 4:1e5639a2a3ae | 124 | while(((cmd[0] & 0x06) != 2)&&(boot_timeout==0)){ |
awmiller | 4:1e5639a2a3ae | 125 | |
awmiller | 4:1e5639a2a3ae | 126 | //test=SentralRead(SENTRAL_STATUS_REG,&ReturnedByte); |
awmiller | 4:1e5639a2a3ae | 127 | cmd[0] = SENTRAL_STATUS_REG; |
awmiller | 4:1e5639a2a3ae | 128 | //cmd[1]= 0x01; |
awmiller | 4:1e5639a2a3ae | 129 | check = master.write(SA0 ? AddrSA0_hi : SENTRAL_ADDRESS, cmd, 1); |
awmiller | 4:1e5639a2a3ae | 130 | if(check) |
awmiller | 4:1e5639a2a3ae | 131 | pc.printf("i2c write fail %d \r\n",check); |
awmiller | 4:1e5639a2a3ae | 132 | cmd[0] = 0; |
awmiller | 4:1e5639a2a3ae | 133 | master.read(SA0 ? AddrSA0_hi : SENTRAL_ADDRESS, cmd, 1); |
awmiller | 4:1e5639a2a3ae | 134 | if(check) |
awmiller | 4:1e5639a2a3ae | 135 | pc.printf("i2c read fail %d \r\n",check); |
awmiller | 4:1e5639a2a3ae | 136 | |
awmiller | 4:1e5639a2a3ae | 137 | //pc.printf(" %d,%X",count,cmd[0]); |
awmiller | 4:1e5639a2a3ae | 138 | |
awmiller | 4:1e5639a2a3ae | 139 | count++; |
awmiller | 4:1e5639a2a3ae | 140 | if(count==30) |
awmiller | 4:1e5639a2a3ae | 141 | boot_timeout=1; |
awmiller | 4:1e5639a2a3ae | 142 | |
awmiller | 4:1e5639a2a3ae | 143 | wait_ms(100); |
awmiller | 4:1e5639a2a3ae | 144 | |
awmiller | 4:1e5639a2a3ae | 145 | } |
awmiller | 4:1e5639a2a3ae | 146 | if(check) |
awmiller | 4:1e5639a2a3ae | 147 | pc.printf("[*I2C Bus Failure, try repositioning the module*] \r\n",check); |
awmiller | 4:1e5639a2a3ae | 148 | |
awmiller | 4:1e5639a2a3ae | 149 | if(boot_timeout){ |
awmiller | 4:1e5639a2a3ae | 150 | //PrintChars("Timeout occured, sentral not present or took too long to boot from the EEPROM\n\r"); |
awmiller | 4:1e5639a2a3ae | 151 | pc.printf("Timeout occured, sentral not present or took too long to boot from the EEPROM\n\r"); |
awmiller | 4:1e5639a2a3ae | 152 | |
awmiller | 4:1e5639a2a3ae | 153 | //SerialPutChars("ReturnByte: 0x"); |
awmiller | 4:1e5639a2a3ae | 154 | pc.printf("Timeout ReturnByte: %X \r\n", cmd[0]); |
awmiller | 4:1e5639a2a3ae | 155 | //char testbuff[5]; |
awmiller | 4:1e5639a2a3ae | 156 | //snprintf(testbuff, 5, "%x,", ReturnedByte); |
awmiller | 4:1e5639a2a3ae | 157 | //SerialPutString(testbuff,5); |
awmiller | 4:1e5639a2a3ae | 158 | //SerialPutChars("Force it"); |
awmiller | 4:1e5639a2a3ae | 159 | //SerialPutChar('\n'); |
awmiller | 4:1e5639a2a3ae | 160 | //SerialPutChar('\r'); |
awmiller | 4:1e5639a2a3ae | 161 | |
awmiller | 4:1e5639a2a3ae | 162 | return RETURN_FAILURE; |
awmiller | 4:1e5639a2a3ae | 163 | } |
awmiller | 4:1e5639a2a3ae | 164 | |
awmiller | 4:1e5639a2a3ae | 165 | pc.printf("Success ReturnByte: %X \r\n", cmd[0]); |
awmiller | 4:1e5639a2a3ae | 166 | // SerialPutChars("ReturnByte: 0x"); |
awmiller | 4:1e5639a2a3ae | 167 | // char testbuff[5]; |
awmiller | 4:1e5639a2a3ae | 168 | // snprintf(testbuff, 5, "%x,", ReturnedByte); |
awmiller | 4:1e5639a2a3ae | 169 | // SerialPutString(testbuff,5); |
awmiller | 4:1e5639a2a3ae | 170 | // //SerialPutChars("Force it"); |
awmiller | 4:1e5639a2a3ae | 171 | // SerialPutChar('\n'); |
awmiller | 4:1e5639a2a3ae | 172 | // SerialPutChar('\r'); |
awmiller | 4:1e5639a2a3ae | 173 | |
awmiller | 4:1e5639a2a3ae | 174 | //force a times out after 3 seconds |
awmiller | 4:1e5639a2a3ae | 175 | //count = 0; |
awmiller | 4:1e5639a2a3ae | 176 | //boot_timeout = FALSE; |
awmiller | 4:1e5639a2a3ae | 177 | //while(boot_timeout==FALSE){ |
awmiller | 4:1e5639a2a3ae | 178 | //count++; |
awmiller | 4:1e5639a2a3ae | 179 | //if(count==30) |
awmiller | 4:1e5639a2a3ae | 180 | // boot_timeout=TRUE; |
awmiller | 4:1e5639a2a3ae | 181 | |
awmiller | 4:1e5639a2a3ae | 182 | wait(1); |
awmiller | 4:1e5639a2a3ae | 183 | //} |
awmiller | 4:1e5639a2a3ae | 184 | |
awmiller | 4:1e5639a2a3ae | 185 | |
awmiller | 4:1e5639a2a3ae | 186 | //SentralWrite(ENABLE_EVENTS_REG,0x20); // enable gyro event only (for data logging function) |
awmiller | 4:1e5639a2a3ae | 187 | //SentralWrite(MAG_RATE_REG,0x64); //set mag rate |
awmiller | 4:1e5639a2a3ae | 188 | //SentralWrite(ACCEL_RATE_REG,0x0a); //set accel rate |
awmiller | 4:1e5639a2a3ae | 189 | //SentralWrite(GYRO_RATE_REG,0x46); //set gyro rate |
awmiller | 4:1e5639a2a3ae | 190 | //SentralWrite(ALGORITHM_CONTROL,0x02); //update registers with RAW sensor data |
awmiller | 4:1e5639a2a3ae | 191 | //SentralWrite(ALGORITHM_CONTROL,0x00); //update registers with Scaled sensor data |
awmiller | 4:1e5639a2a3ae | 192 | |
awmiller | 4:1e5639a2a3ae | 193 | //SentralWrite(HOST_CONTROL_REG,0x01); //Request CPU to run |
awmiller | 4:1e5639a2a3ae | 194 | cmd[0] = HOST_CONTROL_REG; |
awmiller | 4:1e5639a2a3ae | 195 | cmd[1]=0x01; |
awmiller | 4:1e5639a2a3ae | 196 | master.write(SA0 ? AddrSA0_hi : SENTRAL_ADDRESS,cmd,2); |
awmiller | 4:1e5639a2a3ae | 197 | pc.printf("Sentral Startup Success. \r\n"); |
awmiller | 4:1e5639a2a3ae | 198 | |
awmiller | 4:1e5639a2a3ae | 199 | return 1; |
awmiller | 4:1e5639a2a3ae | 200 | } |
awmiller | 4:1e5639a2a3ae | 201 | |
awmiller | 4:1e5639a2a3ae | 202 | |
awmiller | 4:1e5639a2a3ae | 203 |