![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
AFE
Dependencies: mbed-os-retarget-segger-rtt
Diff: source/main.cpp
- Revision:
- 2:04b708fb234b
- Parent:
- 1:0cdad594b7e0
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