AFE
Dependencies: mbed-os-retarget-segger-rtt
source/main.cpp@1:0cdad594b7e0, 2020-08-13 (annotated)
- Committer:
- d4rth_j0k3r
- Date:
- Thu Aug 13 11:43:01 2020 +0000
- Revision:
- 1:0cdad594b7e0
- Parent:
- 0:8388b3dcbdf3
- Child:
- 2:04b708fb234b
test
Who changed what in which revision?
User | Revision | Line number | New 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 | // ==== Functions ==== |
d4rth_j0k3r | 0:8388b3dcbdf3 | 51 | void SPI_Write(char address, uint32_t data){ |
d4rth_j0k3r | 0:8388b3dcbdf3 | 52 | cs.write(0); |
d4rth_j0k3r | 0:8388b3dcbdf3 | 53 | spi.write(CONTROL0); |
d4rth_j0k3r | 0:8388b3dcbdf3 | 54 | spi.write(0); |
d4rth_j0k3r | 0:8388b3dcbdf3 | 55 | spi.write(0); |
d4rth_j0k3r | 0:8388b3dcbdf3 | 56 | spi.write(0); |
d4rth_j0k3r | 0:8388b3dcbdf3 | 57 | cs.write(1); |
d4rth_j0k3r | 0:8388b3dcbdf3 | 58 | |
d4rth_j0k3r | 0:8388b3dcbdf3 | 59 | cs.write(0); |
d4rth_j0k3r | 0:8388b3dcbdf3 | 60 | spi.write(address); |
d4rth_j0k3r | 0:8388b3dcbdf3 | 61 | spi.write((data >> 16) & 0xFF); |
d4rth_j0k3r | 0:8388b3dcbdf3 | 62 | spi.write((data >> 8) & 0xFF); |
d4rth_j0k3r | 0:8388b3dcbdf3 | 63 | spi.write(data & 0xFF); |
d4rth_j0k3r | 0:8388b3dcbdf3 | 64 | cs.write(1); |
d4rth_j0k3r | 0:8388b3dcbdf3 | 65 | } |
d4rth_j0k3r | 0:8388b3dcbdf3 | 66 | |
d4rth_j0k3r | 0:8388b3dcbdf3 | 67 | uint32_t SPI_Read(char address){ |
d4rth_j0k3r | 0:8388b3dcbdf3 | 68 | uint32_t data = 0; |
d4rth_j0k3r | 0:8388b3dcbdf3 | 69 | |
d4rth_j0k3r | 0:8388b3dcbdf3 | 70 | cs.write(0); |
d4rth_j0k3r | 0:8388b3dcbdf3 | 71 | spi.write(CONTROL0); |
d4rth_j0k3r | 0:8388b3dcbdf3 | 72 | spi.write(0); |
d4rth_j0k3r | 0:8388b3dcbdf3 | 73 | spi.write(0); |
d4rth_j0k3r | 0:8388b3dcbdf3 | 74 | spi.write(0x1); |
d4rth_j0k3r | 0:8388b3dcbdf3 | 75 | cs.write(1); |
d4rth_j0k3r | 0:8388b3dcbdf3 | 76 | |
d4rth_j0k3r | 0:8388b3dcbdf3 | 77 | cs.write(0); |
d4rth_j0k3r | 0:8388b3dcbdf3 | 78 | spi.write(address); |
d4rth_j0k3r | 0:8388b3dcbdf3 | 79 | data |= (uint32_t)spi.write(0x00) << 16; |
d4rth_j0k3r | 0:8388b3dcbdf3 | 80 | data |= (uint32_t)spi.write(0x00) << 8; |
d4rth_j0k3r | 0:8388b3dcbdf3 | 81 | data |= (uint32_t)spi.write(0x00); |
d4rth_j0k3r | 0:8388b3dcbdf3 | 82 | cs.write(1); |
d4rth_j0k3r | 0:8388b3dcbdf3 | 83 | |
d4rth_j0k3r | 0:8388b3dcbdf3 | 84 | // disable reading from registers |
d4rth_j0k3r | 0:8388b3dcbdf3 | 85 | SPI_Write(CONTROL0,0); |
d4rth_j0k3r | 0:8388b3dcbdf3 | 86 | |
d4rth_j0k3r | 0:8388b3dcbdf3 | 87 | return data; |
d4rth_j0k3r | 0:8388b3dcbdf3 | 88 | } |
d4rth_j0k3r | 0:8388b3dcbdf3 | 89 | |
d4rth_j0k3r | 0:8388b3dcbdf3 | 90 | bool error_check(int index, unsigned char data_in, unsigned char data_out){ |
d4rth_j0k3r | 0:8388b3dcbdf3 | 91 | if (data_in != data_out){ |
d4rth_j0k3r | 0:8388b3dcbdf3 | 92 | printf("[ERROR] Address %d : In = %d / Out = %d\n\r",index ,data_in,data_out); |
d4rth_j0k3r | 0:8388b3dcbdf3 | 93 | return 1; |
d4rth_j0k3r | 0:8388b3dcbdf3 | 94 | }else{ |
d4rth_j0k3r | 0:8388b3dcbdf3 | 95 | return 0; |
d4rth_j0k3r | 0:8388b3dcbdf3 | 96 | } |
d4rth_j0k3r | 0:8388b3dcbdf3 | 97 | } |
d4rth_j0k3r | 0:8388b3dcbdf3 | 98 | |
d4rth_j0k3r | 0:8388b3dcbdf3 | 99 | void blinky(int delay){ |
d4rth_j0k3r | 0:8388b3dcbdf3 | 100 | state = !state; |
d4rth_j0k3r | 0:8388b3dcbdf3 | 101 | myled.write(state); |
d4rth_j0k3r | 0:8388b3dcbdf3 | 102 | ThisThread::sleep_for(delay); |
d4rth_j0k3r | 0:8388b3dcbdf3 | 103 | } |