![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
AFE
Dependencies: mbed-os-retarget-segger-rtt
Revision 2:04b708fb234b, committed 2020-09-04
- Comitter:
- d4rth_j0k3r
- Date:
- Fri Sep 04 12:35:01 2020 +0000
- Parent:
- 1:0cdad594b7e0
- Commit message:
- Finish
Changed in this revision
diff -r 0cdad594b7e0 -r 04b708fb234b mbed-os-retarget-segger-rtt.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-os-retarget-segger-rtt.lib Fri Sep 04 12:35:01 2020 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/0x6d61726b/code/mbed-os-retarget-segger-rtt/#7fca1bf48117
diff -r 0cdad594b7e0 -r 04b708fb234b source/main.cpp --- a/source/main.cpp Thu Aug 13 11:43:01 2020 +0000 +++ b/source/main.cpp Fri Sep 04 12:35:01 2020 +0000 @@ -7,47 +7,139 @@ // ==== Main ==== int main() { - uint32_t data_in, data_out; - char buffer[4]; + printf("\n\r==== AFE TestBench ====\n\n\r"); + + AFE_begin(); + // Send Defaut Register Values + for (int i = 0; i< 49; i++){ + SPI_Write(i,AFE_Default[i]); + } + AFE_set_LED_current(255,255); + + // Print Register State + printf("== Default Register State ==\n\r"); + AFE_register_state(); + + // Register State Control + AFE_control_register(); + + // Mesurements + AFE_measurement(); + // Diagnostic Flags + AFE_diag_flag(); + + // Results + AFE_Results(); +} + + + +// =================== +// ==== Functions ==== +// =================== + +void AFE_reset(){ + SPI_Write_Bit(CONTROL0, 3, true); + while((SPI_Read(CONTROL0) >> 3) & 0x1); // Waiting for Reset bit back to 0 +} + +void AFE_begin(){ + // Supply Enable SUP_EN.write(1); - myled.write(1); - spi.frequency(8000000); - SPI_Write(CONTROL0,0x8); // Soft Reset - while((SPI_Read(CONTROL0) >> 3) & 0x1){} // Waiting for Reset bit back to 0 - SPI_Write(CONTROL2,0x20100); - SPI_Write(CONTROL1,0x2); - ThisThread::sleep_for(1000); + // Power Up + AFE_PDN.write(1); - // Chip must be deselected - for (char address = 1; address < 10; address++){ - // Generating data to be written - data_in = rand() % 65535; - data_out = 0; + // Hard Reset + AFE_nRST.write(0); + ThisThread::sleep_for(500); + AFE_nRST.write(1); + myled.write(1); + + // Soft Reset + AFE_reset(); +} + +void AFE_set_LED_current(uint8_t led1_current, uint8_t led2_current){ + // read the reg + uint32_t current_val = SPI_Read(LEDCNTRL); + + // set led 1 value + led1_current = led1_current & 0xFF; + current_val = current_val & ~(0xFF << 8); + current_val = current_val | (led1_current << 8); + + // set led 2 value + led2_current = led2_current & 0xFF; + current_val = current_val & ~(0xFF); + current_val = current_val | (led2_current); - // Send data - SPI_Write(address, data_in); - - // Read data written - data_out = SPI_Read(address); - nr_error = error_check(address, data_in, data_out); - while (nr_error){ - blinky(100); + // write reg + SPI_Write(LEDCNTRL, current_val); +} + +void AFE_measurement(){ + printf("== Start Measures ==\n\r"); + SPI_Write_Bit(CONTROL2, 11, false); //Set default led state + SPI_Write_Bit(LEDCNTRL, 17, true); + SPI_Write_Bit(CONTROL1, 8, true); + printf("Done\n\r"); + + printf("== Diagnostic ==\n\r"); + SPI_Write_Bit(CONTROL0, 2, true); // Start Diagnosis + ThisThread::sleep_for(500); + //while(!AFE_DIAG_END); // Waiting for Diag_En bit going high + printf("Done\n\r"); +} + +void AFE_register_state(){ + uint8_t address = 0; + uint32_t data_out = 0; + for (int i = 0 ; address < 34 ; i++ ) { + printf("\n\r%d : ", address ) ; + for (int j = 0 ; j < 8 ; j++ ) { + data_out = SPI_Read(address) ; + printf("%lu ", data_out) ; + //printf("%x ", data_out); + address++; } } - - if (!nr_error){ - printf("Test passed successfully\n\r"); - while(1){ - myled.write(1); - //blinky(500); + printf("\n\n\r"); +} + +void AFE_control_register(){ + uint32_t data_out = 0; + bool nr_error = 0; + for (char address = 1; address < 34; address++){ + data_out = SPI_Read(address); + nr_error = error_check(address, AFE_Default[address], data_out); + } +} + +void AFE_diag_flag(){ + uint16_t flags = 0; + flags = SPI_Read(0x30) & 0x1FFF; + if (flags){ + for (int i = 0; i < 13; i++){ + if ((flags >> i) & 0x1){ + printf("[ERROR] Default detected - Code : %d\n\r",i); + } } } } +void AFE_Results(){ + printf("== Results ==\n\r"); + printf("LED2VAL : %d\n\r",(SPI_Read(0x2A) & 0xFFFFFF)); + printf("ALED2VAL : %d\n\r",(SPI_Read(0x2B) & 0xFFFFFF)); + printf("LED1VAL : %d\n\r",(SPI_Read(0x2C) & 0xFFFFFF)); + printf("ALED1VAL : %d\n\r",(SPI_Read(0x2D) & 0xFFFFFF)); + printf("LED2-ALED2VAL : %d\n\r",(SPI_Read(0x2E) & 0xFFFFFF)); + printf("LED1-ALED1VAL : %d\n\r",(SPI_Read(0x2F) & 0xFFFFFF)); +} -// ==== Functions ==== + void SPI_Write(char address, uint32_t data){ cs.write(0); spi.write(CONTROL0); @@ -64,6 +156,17 @@ cs.write(1); } +void SPI_Write_Bit(char address, uint8_t bit, bool bit_high){ + unsigned long current_val = SPI_Read(address); + if (bit_high & !(current_val & 1 << bit)){ + current_val = current_val | (bit_high << bit); + SPI_Write(address, current_val); + } else if (!bit_high & (current_val & 1 << bit)) { + current_val = current_val & ~(bit_high << bit); + SPI_Write(address, current_val); + } +} + uint32_t SPI_Read(char address){ uint32_t data = 0; @@ -72,7 +175,7 @@ spi.write(0); spi.write(0); spi.write(0x1); - cs.write(1); + cs.write(1); cs.write(0); spi.write(address); @@ -82,22 +185,21 @@ cs.write(1); // disable reading from registers - SPI_Write(CONTROL0,0); + cs.write(0); + spi.write(CONTROL0); + spi.write(0); + spi.write(0); + spi.write(0); + cs.write(1); return data; } -bool error_check(int index, unsigned char data_in, unsigned char data_out){ +bool error_check(int index, uint32_t data_in, uint32_t data_out){ if (data_in != data_out){ printf("[ERROR] Address %d : In = %d / Out = %d\n\r",index ,data_in,data_out); return 1; }else{ return 0; } -} - -void blinky(int delay){ - state = !state; - myled.write(state); - ThisThread::sleep_for(delay); } \ No newline at end of file
diff -r 0cdad594b7e0 -r 04b708fb234b source/main.h --- a/source/main.h Thu Aug 13 11:43:01 2020 +0000 +++ b/source/main.h Fri Sep 04 12:35:01 2020 +0000 @@ -68,11 +68,121 @@ DigitalOut AFE_nRST(P1_7); DigitalIn AFE_LED_ALM(P1_8); DigitalIn AFE_PD_ALM(P1_9); -int nr_error = 0; -bool state = 0; + +unsigned long AFE_Default[49] = { +//Reg0: CONTROL0: CONTROL REGISTER 0 + 0x00000, +//Reg1:REDSTARTCOUNT: SAMPLE RED START COUNT + 6000, +//Reg2:REDENDCOUNT: SAMPLE RED END COUNT + 7999, +//Reg3:REDLEDSTARTCOUNT: RED LED START COUNT + 6000, +//Reg4:REDLEDENDCOUNT: RED LED END COUNT + 7998, +//Reg5:AMBREDSTARTCOUNT: SAMPLE AMBIENT RED START COUNT + 0000, +//Reg6:AMBREDENDCOUNT: SAMPLE AMBIENT RED END COUNT + 1998, +//Reg7:IRSTARTCOUNT: SAMPLE IR START COUNT + 2000, +//Reg8:IRENDCOUNT: SAMPLE IR END COUNT + 3999, +//Reg9:IRLEDSTARTCOUNT: IR LED START COUNT + 2000, +//Reg10:IRLEDENDCOUNT: IR LED END COUNT + 3999, +//Reg11:AMBIRSTARTCOUNT: SAMPLE AMBIENT IR START COUNT + 4000, +//Reg12:AMBIRENDCOUNT: SAMPLE AMBIENT IR END COUNT + 5998, +//Reg13:REDCONVSTART: REDCONVST + 2, +//Reg14:REDCONVEND: RED CONVERT END COUNT + 1999, +//Reg15:AMBREDCONVSTART: RED AMBIENT CONVERT START COUNT + 2002, +//Reg16:AMBREDCONVEND: RED AMBIENT CONVERT END COUNT + 3999, +//Reg17:IRCONVSTART: IR CONVERT START COUNT + 4002, +//Reg18:IRCONVEND: IR CONVERT END COUNT + 5999, +//Reg19:AMBIRCONVSTART: IR AMBIENT CONVERT START COUNT + 6002, +//Reg20:AMBIRCONVEND: IR AMBIENT CONVERT END COUNT + 7999, +//Reg21:ADCRESETSTCOUNT0: ADC RESET 0 START COUNT + 0, +//Reg22:ADCRESETENDCOUNT0: ADC RESET 0 END COUNT + 2, +//Reg23:ADCRESETSTCOUNT1: ADC RESET 1 START COUNT + 2000, +//Reg24:ADCRESETENDCOUNT1: ADC RESET 1 END COUNT + 2002, +//Reg25:ADCRESETENDCOUNT2: ADC RESET 2 START COUNT + 4000, +//Reg26:ADCRESETENDCOUNT2: ADC RESET 2 END COUNT + 4002, +//Reg27:ADCRESETENDCOUNT3: ADC RESET 3 START COUNT + 6000, +//Reg28:ADCRESETENDCOUNT3: ADC RESET 3 END COUNT + 6002, +//Reg29:PRPCOUNT: PULSE REPETITION PERIOD COUNT + 7999, +//Reg30:CONTROL1: CONTROL REGISTER 1 + 0x00107, //timer enabled, averages=3, RED and IR LED pulse ON PD_ALM AND LED_ALM pins +//Reg31:?: ?? + 0x00000, +//Reg32:TIAGAIN: TRANS IMPEDANCE AMPLIFIER GAIN SETTING REGISTER + 0x00000, +//Reg33:TIA_AMB_GAIN: TRANS IMPEDANCE AAMPLIFIER AND AMBIENT CANELLATION STAGE GAIN + 0x00000, +//Reg34:LEDCNTRL: LED CONTROL REGISTER + 0x11414, +//Reg35:CONTROL2: CONTROL REGISTER 2 + 0x00000, //bit 9 +//Reg36:?: ?? + 0x00000, +//Reg37:?: ?? + 0x00000, +//Reg38:?: ?? + 0x00000, +//Reg39:?: ?? + 0x00000, +//Reg40:: ?? + 0x00000, +//Reg41:ALARM: ?? + 0x00000, +//Reg42:REDVALUE: RED DIGITAL SAMPLE VALUE + 0x00000, +//Reg43:AMBREDVALUE: Ambient RED Digital Sample Value + 0x00000, +//Reg44:IRVALUE: IR Digital Sample Value + 0x00000, +//Reg45:AMBIRVALUE: Ambient IR Digital Sample Value + 0x00000, +//Reg46:RED-AMBREDVALUE: RED-AMBIENT RED DIGITAL SAMPLE VALUE + 0x00000, +//Reg47:IR-AMBIRVALUE: IR-AMBIENT IR DIGITAL SAMPLE VALUE + 0x00000, +//Reg48:DIGNOSTICS: DIAGNOSTICS FLAGS REGISTER + 0x00000 +}; // ==== Prototypes ==== void SPI_Write(char address, uint32_t data); uint32_t SPI_Read(char raddress); -bool error_check(int index, unsigned char data_in, unsigned char data_out); +void SPI_Write_Bit(char address, uint8_t bit, bool bit_high); + +void AFE_reset(); +void AFE_begin(); +void AFE_set_LED_current(uint8_t led1_current, uint8_t led2_current); +void AFE_measurement(); +void AFE_register_state(); +void AFE_control_register(); +void AFE_diag_flag(); +void AFE_Results(); + +bool error_check(int index, uint32_t data_in, uint32_t data_out); void blinky(int delay); \ No newline at end of file