Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
spi_functions.cpp@9:81e2caa80fdf, 2021-01-21 (annotated)
- Committer:
- cussans
- Date:
- Thu Jan 21 15:42:00 2021 +0000
- Revision:
- 9:81e2caa80fdf
- Parent:
- 8:2196f2f75947
Committing any changes
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
PaoloB | 0:15d9f9050d21 | 1 | #include "mbed.h" |
PaoloB | 0:15d9f9050d21 | 2 | #include <spi_functions.h> |
PaoloB | 0:15d9f9050d21 | 3 | |
PaoloB | 2:ac580453441f | 4 | //////////////////////////////////////////////////////// |
PaoloB | 0:15d9f9050d21 | 5 | int sendConfiguration(Serial &serialOut, SPI &dac_port, DigitalOut &sync) |
PaoloB | 0:15d9f9050d21 | 6 | { |
PaoloB | 0:15d9f9050d21 | 7 | int nWords= 12; |
cussans | 9:81e2caa80fdf | 8 | int returnedData; |
PaoloB | 0:15d9f9050d21 | 9 | unsigned short dac_data[nWords]; |
PaoloB | 0:15d9f9050d21 | 10 | |
PaoloB | 0:15d9f9050d21 | 11 | //CONFIGURE ALL PORTS AS OUTPUTS |
PaoloB | 0:15d9f9050d21 | 12 | dac_data[0] = 0x0955; // |
PaoloB | 0:15d9f9050d21 | 13 | dac_data[1] = 0x0A55; // |
PaoloB | 0:15d9f9050d21 | 14 | dac_data[2] = 0x0B55; // |
PaoloB | 0:15d9f9050d21 | 15 | dac_data[3] = 0x0C55; // |
PaoloB | 0:15d9f9050d21 | 16 | dac_data[4] = 0x0D55; // |
PaoloB | 0:15d9f9050d21 | 17 | dac_data[5] = 0x0E55; // |
PaoloB | 0:15d9f9050d21 | 18 | dac_data[6] = 0x0F55; // |
PaoloB | 0:15d9f9050d21 | 19 | |
PaoloB | 2:ac580453441f | 20 | //SET ALL OUTPUTS TO 0 |
PaoloB | 2:ac580453441f | 21 | dac_data[7] = 0x4400; // P4 - P11 |
PaoloB | 2:ac580453441f | 22 | dac_data[8] = 0x4C00; // P12 - P19 |
PaoloB | 2:ac580453441f | 23 | dac_data[9] = 0x5400; // P20 - P27 |
PaoloB | 2:ac580453441f | 24 | dac_data[10]= 0x5C00; // P28 - P31 |
PaoloB | 0:15d9f9050d21 | 25 | |
PaoloB | 0:15d9f9050d21 | 26 | //EXIT SHUTDOWN MODE |
PaoloB | 0:15d9f9050d21 | 27 | dac_data[11]= 0x0401; |
PaoloB | 0:15d9f9050d21 | 28 | for (int i = 0; i < nWords; i++) |
PaoloB | 0:15d9f9050d21 | 29 | { |
PaoloB | 0:15d9f9050d21 | 30 | serialOut.printf("WRITING %d (0x%04x).\n", dac_data[i], dac_data[i]); |
cussans | 9:81e2caa80fdf | 31 | returnedData = mySPISend(dac_data[i], dac_port, sync); |
cussans | 9:81e2caa80fdf | 32 | //returnedData = mySPIRead(serialOut, dac_data[i], dac_port, sync); |
cussans | 9:81e2caa80fdf | 33 | serialOut.printf("Read back 0x%04x \n" , returnedData ); |
PaoloB | 0:15d9f9050d21 | 34 | } |
PaoloB | 0:15d9f9050d21 | 35 | return 0; |
PaoloB | 0:15d9f9050d21 | 36 | } |
PaoloB | 0:15d9f9050d21 | 37 | |
PaoloB | 2:ac580453441f | 38 | //////////////////////////////////////////////////////// |
PaoloB | 0:15d9f9050d21 | 39 | int sendTransfer(unsigned short data, SPI &dac_port, DigitalOut &sync) |
PaoloB | 0:15d9f9050d21 | 40 | { |
PaoloB | 2:ac580453441f | 41 | |
PaoloB | 0:15d9f9050d21 | 42 | return 0; |
PaoloB | 0:15d9f9050d21 | 43 | } |
PaoloB | 0:15d9f9050d21 | 44 | |
PaoloB | 2:ac580453441f | 45 | //////////////////////////////////////////////////////// |
PaoloB | 0:15d9f9050d21 | 46 | int mySPISend( unsigned short data, SPI &dac_port, DigitalOut &sync) |
PaoloB | 0:15d9f9050d21 | 47 | // This is the basic function used to send the signals. |
PaoloB | 0:15d9f9050d21 | 48 | { |
cussans | 9:81e2caa80fdf | 49 | unsigned short return1, return2; |
cussans | 9:81e2caa80fdf | 50 | |
PaoloB | 0:15d9f9050d21 | 51 | //sync = 1; |
cussans | 6:4d64960c7908 | 52 | wait (0.001); // Changed from 0.5, DGC, 1 Oct 18 |
PaoloB | 0:15d9f9050d21 | 53 | |
cussans | 9:81e2caa80fdf | 54 | sync = 0; // drive chip select |
PaoloB | 0:15d9f9050d21 | 55 | |
cussans | 9:81e2caa80fdf | 56 | wait(0.0001); // wait 100us for select line to go fully low after selecting chip . |
cussans | 6:4d64960c7908 | 57 | |
cussans | 9:81e2caa80fdf | 58 | return1 = dac_port.write(data >> 8);// We have a 8 bit interface but need to write 16 bits. Two write in sequence (or use "transfer" instead) |
cussans | 9:81e2caa80fdf | 59 | return2 = dac_port.write(data); |
PaoloB | 0:15d9f9050d21 | 60 | |
cussans | 6:4d64960c7908 | 61 | wait(0.0001); // wait 100us for select line to go fully high. |
PaoloB | 0:15d9f9050d21 | 62 | sync = 1; |
PaoloB | 0:15d9f9050d21 | 63 | |
cussans | 9:81e2caa80fdf | 64 | wait (0.001); // changed from 0.5 , DGC, May 2019 |
cussans | 9:81e2caa80fdf | 65 | return ( return2 | (return1 << 8) ); |
cussans | 9:81e2caa80fdf | 66 | |
PaoloB | 0:15d9f9050d21 | 67 | } |
PaoloB | 0:15d9f9050d21 | 68 | |
PaoloB | 2:ac580453441f | 69 | //////////////////////////////////////////////////////// |
PaoloB | 2:ac580453441f | 70 | int mySPIRead(Serial &serialOut, unsigned short data, SPI &dac_port, DigitalOut &sync) |
PaoloB | 2:ac580453441f | 71 | // This is the basic function used to send the signals. |
PaoloB | 2:ac580453441f | 72 | { |
PaoloB | 2:ac580453441f | 73 | unsigned short nopdata= 0x0000; |
PaoloB | 2:ac580453441f | 74 | unsigned short return1, return2; |
cussans | 9:81e2caa80fdf | 75 | wait (0.05); |
PaoloB | 2:ac580453441f | 76 | |
PaoloB | 2:ac580453441f | 77 | sync = 0; |
PaoloB | 2:ac580453441f | 78 | |
PaoloB | 2:ac580453441f | 79 | dac_port.write(data >> 8);// We have a 8 bit interface but need to write 16 bits. Two write in sequence (or use "transfer" instead) |
PaoloB | 2:ac580453441f | 80 | dac_port.write(data); |
PaoloB | 2:ac580453441f | 81 | |
PaoloB | 2:ac580453441f | 82 | wait(0.000005); // |
cussans | 9:81e2caa80fdf | 83 | // sync = 1; |
PaoloB | 2:ac580453441f | 84 | |
cussans | 9:81e2caa80fdf | 85 | // wait (0.00002); |
cussans | 9:81e2caa80fdf | 86 | // sync = 0; |
PaoloB | 2:ac580453441f | 87 | |
PaoloB | 2:ac580453441f | 88 | return1= dac_port.write(nopdata >> 8);// No operation. Used to flush the 16 bits. |
PaoloB | 2:ac580453441f | 89 | return2= dac_port.write(nopdata); |
PaoloB | 2:ac580453441f | 90 | |
PaoloB | 2:ac580453441f | 91 | wait(0.000005); // |
PaoloB | 2:ac580453441f | 92 | sync = 1; |
cussans | 9:81e2caa80fdf | 93 | wait (0.05); |
PaoloB | 2:ac580453441f | 94 | serialOut.printf("Returned data = %d (0x%04x), %d (0x%04x).\n", return1, return1, return2, return2); |
cussans | 9:81e2caa80fdf | 95 | return ( return2 | (return1 << 8) ); |
PaoloB | 2:ac580453441f | 96 | } |
PaoloB | 2:ac580453441f | 97 | |
PaoloB | 2:ac580453441f | 98 | //////////////////////////////////////////////////////// |
PaoloB | 2:ac580453441f | 99 | int powerMode(SPI &dac_port, DigitalOut &sync, int mode) |
PaoloB | 2:ac580453441f | 100 | { |
PaoloB | 2:ac580453441f | 101 | int nWords= 2; |
PaoloB | 2:ac580453441f | 102 | unsigned short dac_data[nWords]; |
PaoloB | 2:ac580453441f | 103 | |
PaoloB | 2:ac580453441f | 104 | |
PaoloB | 2:ac580453441f | 105 | dac_data[0]= 0x0400;//ENTER SHUTDOWN MODE |
PaoloB | 2:ac580453441f | 106 | dac_data[1]= 0x0401;//EXIT SHUTDOWN MODE |
PaoloB | 2:ac580453441f | 107 | |
PaoloB | 2:ac580453441f | 108 | if ((mode==0) || (mode==1)) |
PaoloB | 2:ac580453441f | 109 | { |
PaoloB | 2:ac580453441f | 110 | mySPISend(dac_data[mode], dac_port, sync); |
PaoloB | 2:ac580453441f | 111 | } |
PaoloB | 2:ac580453441f | 112 | |
PaoloB | 2:ac580453441f | 113 | return 0; |
PaoloB | 2:ac580453441f | 114 | } |
PaoloB | 2:ac580453441f | 115 | |
PaoloB | 2:ac580453441f | 116 | //////////////////////////////////////////////////////// |
PaoloB | 2:ac580453441f | 117 | int setPort(SPI &dac_port, DigitalOut &sync, int myPort, int newState) |
PaoloB | 2:ac580453441f | 118 | { |
PaoloB | 2:ac580453441f | 119 | int nWords= 1; |
PaoloB | 2:ac580453441f | 120 | unsigned short dac_data[nWords]; |
PaoloB | 2:ac580453441f | 121 | |
PaoloB | 2:ac580453441f | 122 | dac_data[0]= (myPort + 0x0020)*0x0100 + newState; |
PaoloB | 2:ac580453441f | 123 | mySPISend(dac_data[0], dac_port, sync); |
PaoloB | 2:ac580453441f | 124 | /* |
PaoloB | 2:ac580453441f | 125 | dac_data[0]= 0x0400;//ENTER SHUTDOWN MODE |
PaoloB | 2:ac580453441f | 126 | dac_data[1]= 0x0401;//EXIT SHUTDOWN MODE |
PaoloB | 2:ac580453441f | 127 | |
PaoloB | 2:ac580453441f | 128 | if ((mode==0) || (mode==1)) |
PaoloB | 2:ac580453441f | 129 | { |
PaoloB | 2:ac580453441f | 130 | mySPISend(dac_data[mode], dac_port, sync); |
PaoloB | 2:ac580453441f | 131 | }*/ |
PaoloB | 2:ac580453441f | 132 | return 0; |
PaoloB | 2:ac580453441f | 133 | } |