AFE

Dependencies:   mbed-os-retarget-segger-rtt

Committer:
d4rth_j0k3r
Date:
Thu Aug 13 11:37:58 2020 +0000
Revision:
0:8388b3dcbdf3
Child:
1:0cdad594b7e0
Test AFE

Who changed what in which revision?

UserRevisionLine numberNew contents of line
d4rth_j0k3r 0:8388b3dcbdf3 1 /* Pneumoscope Version 1.0
d4rth_j0k3r 0:8388b3dcbdf3 2 Florian CHAYS
d4rth_j0k3r 0:8388b3dcbdf3 3 */
d4rth_j0k3r 0:8388b3dcbdf3 4
d4rth_j0k3r 0:8388b3dcbdf3 5 #include "mbed.h"
d4rth_j0k3r 0:8388b3dcbdf3 6 #include <main.h>
d4rth_j0k3r 0:8388b3dcbdf3 7
d4rth_j0k3r 0:8388b3dcbdf3 8 // ==== Main ====
d4rth_j0k3r 0:8388b3dcbdf3 9 int main() {
d4rth_j0k3r 0:8388b3dcbdf3 10 uint32_t data_in, data_out;
d4rth_j0k3r 0:8388b3dcbdf3 11 char buffer[4];
d4rth_j0k3r 0:8388b3dcbdf3 12
d4rth_j0k3r 0:8388b3dcbdf3 13 SUP_EN.write(1);
d4rth_j0k3r 0:8388b3dcbdf3 14 myled.write(1);
d4rth_j0k3r 0:8388b3dcbdf3 15 spi.frequency(8000000);
d4rth_j0k3r 0:8388b3dcbdf3 16
d4rth_j0k3r 0:8388b3dcbdf3 17 SPI_Write(CONTROL0,0x8); // Soft Reset
d4rth_j0k3r 0:8388b3dcbdf3 18 while((SPI_Read(CONTROL0) >> 3) & 0x1){} // Waiting for Reset bit back to 0
d4rth_j0k3r 0:8388b3dcbdf3 19 SPI_Write(CONTROL2,0x20100);
d4rth_j0k3r 0:8388b3dcbdf3 20 SPI_Write(CONTROL1,0x2);
d4rth_j0k3r 0:8388b3dcbdf3 21 ThisThread::sleep_for(1000);
d4rth_j0k3r 0:8388b3dcbdf3 22
d4rth_j0k3r 0:8388b3dcbdf3 23 // Chip must be deselected
d4rth_j0k3r 0:8388b3dcbdf3 24 for (char address = 1; address < 10; address++){
d4rth_j0k3r 0:8388b3dcbdf3 25 // Generating data to be written
d4rth_j0k3r 0:8388b3dcbdf3 26 data_in = rand() % 65535;
d4rth_j0k3r 0:8388b3dcbdf3 27 data_out = 0;
d4rth_j0k3r 0:8388b3dcbdf3 28
d4rth_j0k3r 0:8388b3dcbdf3 29 // Send data
d4rth_j0k3r 0:8388b3dcbdf3 30 SPI_Write(address, data_in);
d4rth_j0k3r 0:8388b3dcbdf3 31
d4rth_j0k3r 0:8388b3dcbdf3 32 // Read data written
d4rth_j0k3r 0:8388b3dcbdf3 33 data_out = SPI_Read(address);
d4rth_j0k3r 0:8388b3dcbdf3 34 nr_error = error_check(address, data_in, data_out);
d4rth_j0k3r 0:8388b3dcbdf3 35 while (nr_error){
d4rth_j0k3r 0:8388b3dcbdf3 36 blinky(100);
d4rth_j0k3r 0:8388b3dcbdf3 37 }
d4rth_j0k3r 0:8388b3dcbdf3 38 }
d4rth_j0k3r 0:8388b3dcbdf3 39
d4rth_j0k3r 0:8388b3dcbdf3 40 if (!nr_error){
d4rth_j0k3r 0:8388b3dcbdf3 41 printf("Test passed successfully\n\r");
d4rth_j0k3r 0:8388b3dcbdf3 42 while(1){
d4rth_j0k3r 0:8388b3dcbdf3 43 myled.write(1);
d4rth_j0k3r 0:8388b3dcbdf3 44 //blinky(500);
d4rth_j0k3r 0:8388b3dcbdf3 45 }
d4rth_j0k3r 0:8388b3dcbdf3 46 }
d4rth_j0k3r 0:8388b3dcbdf3 47 }
d4rth_j0k3r 0:8388b3dcbdf3 48
d4rth_j0k3r 0:8388b3dcbdf3 49
d4rth_j0k3r 0:8388b3dcbdf3 50
d4rth_j0k3r 0:8388b3dcbdf3 51 // ==== Functions ====
d4rth_j0k3r 0:8388b3dcbdf3 52 void SPI_Write(char address, uint32_t data){
d4rth_j0k3r 0:8388b3dcbdf3 53 cs.write(0);
d4rth_j0k3r 0:8388b3dcbdf3 54 spi.write(CONTROL0);
d4rth_j0k3r 0:8388b3dcbdf3 55 spi.write(0);
d4rth_j0k3r 0:8388b3dcbdf3 56 spi.write(0);
d4rth_j0k3r 0:8388b3dcbdf3 57 spi.write(0);
d4rth_j0k3r 0:8388b3dcbdf3 58 cs.write(1);
d4rth_j0k3r 0:8388b3dcbdf3 59
d4rth_j0k3r 0:8388b3dcbdf3 60 cs.write(0);
d4rth_j0k3r 0:8388b3dcbdf3 61 spi.write(address);
d4rth_j0k3r 0:8388b3dcbdf3 62 spi.write((data >> 16) & 0xFF);
d4rth_j0k3r 0:8388b3dcbdf3 63 spi.write((data >> 8) & 0xFF);
d4rth_j0k3r 0:8388b3dcbdf3 64 spi.write(data & 0xFF);
d4rth_j0k3r 0:8388b3dcbdf3 65 cs.write(1);
d4rth_j0k3r 0:8388b3dcbdf3 66 }
d4rth_j0k3r 0:8388b3dcbdf3 67
d4rth_j0k3r 0:8388b3dcbdf3 68 uint32_t SPI_Read(char address){
d4rth_j0k3r 0:8388b3dcbdf3 69 uint32_t data = 0;
d4rth_j0k3r 0:8388b3dcbdf3 70
d4rth_j0k3r 0:8388b3dcbdf3 71 cs.write(0);
d4rth_j0k3r 0:8388b3dcbdf3 72 spi.write(CONTROL0);
d4rth_j0k3r 0:8388b3dcbdf3 73 spi.write(0);
d4rth_j0k3r 0:8388b3dcbdf3 74 spi.write(0);
d4rth_j0k3r 0:8388b3dcbdf3 75 spi.write(0x1);
d4rth_j0k3r 0:8388b3dcbdf3 76 cs.write(1);
d4rth_j0k3r 0:8388b3dcbdf3 77
d4rth_j0k3r 0:8388b3dcbdf3 78 cs.write(0);
d4rth_j0k3r 0:8388b3dcbdf3 79 spi.write(address);
d4rth_j0k3r 0:8388b3dcbdf3 80 data |= (uint32_t)spi.write(0x00) << 16;
d4rth_j0k3r 0:8388b3dcbdf3 81 data |= (uint32_t)spi.write(0x00) << 8;
d4rth_j0k3r 0:8388b3dcbdf3 82 data |= (uint32_t)spi.write(0x00);
d4rth_j0k3r 0:8388b3dcbdf3 83 cs.write(1);
d4rth_j0k3r 0:8388b3dcbdf3 84
d4rth_j0k3r 0:8388b3dcbdf3 85 // disable reading from registers
d4rth_j0k3r 0:8388b3dcbdf3 86 SPI_Write(CONTROL0,0);
d4rth_j0k3r 0:8388b3dcbdf3 87
d4rth_j0k3r 0:8388b3dcbdf3 88 return data;
d4rth_j0k3r 0:8388b3dcbdf3 89 }
d4rth_j0k3r 0:8388b3dcbdf3 90
d4rth_j0k3r 0:8388b3dcbdf3 91 bool error_check(int index, unsigned char data_in, unsigned char data_out){
d4rth_j0k3r 0:8388b3dcbdf3 92 if (data_in != data_out){
d4rth_j0k3r 0:8388b3dcbdf3 93 printf("[ERROR] Address %d : In = %d / Out = %d\n\r",index ,data_in,data_out);
d4rth_j0k3r 0:8388b3dcbdf3 94 return 1;
d4rth_j0k3r 0:8388b3dcbdf3 95 }else{
d4rth_j0k3r 0:8388b3dcbdf3 96 return 0;
d4rth_j0k3r 0:8388b3dcbdf3 97 }
d4rth_j0k3r 0:8388b3dcbdf3 98 }
d4rth_j0k3r 0:8388b3dcbdf3 99
d4rth_j0k3r 0:8388b3dcbdf3 100 void blinky(int delay){
d4rth_j0k3r 0:8388b3dcbdf3 101 state = !state;
d4rth_j0k3r 0:8388b3dcbdf3 102 myled.write(state);
d4rth_j0k3r 0:8388b3dcbdf3 103 ThisThread::sleep_for(delay);
d4rth_j0k3r 0:8388b3dcbdf3 104 }