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@8:2196f2f75947, 2019-06-28 (annotated)
- Committer:
- cussans
- Date:
- Fri Jun 28 14:43:03 2019 +0000
- Revision:
- 8:2196f2f75947
- Parent:
- 6:4d64960c7908
- Child:
- 9:81e2caa80fdf
Pushing to repo. before hacking on readback
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; |
PaoloB | 0:15d9f9050d21 | 8 | unsigned short dac_data[nWords]; |
PaoloB | 0:15d9f9050d21 | 9 | |
PaoloB | 0:15d9f9050d21 | 10 | //CONFIGURE ALL PORTS AS OUTPUTS |
PaoloB | 0:15d9f9050d21 | 11 | dac_data[0] = 0x0955; // |
PaoloB | 0:15d9f9050d21 | 12 | dac_data[1] = 0x0A55; // |
PaoloB | 0:15d9f9050d21 | 13 | dac_data[2] = 0x0B55; // |
PaoloB | 0:15d9f9050d21 | 14 | dac_data[3] = 0x0C55; // |
PaoloB | 0:15d9f9050d21 | 15 | dac_data[4] = 0x0D55; // |
PaoloB | 0:15d9f9050d21 | 16 | dac_data[5] = 0x0E55; // |
PaoloB | 0:15d9f9050d21 | 17 | dac_data[6] = 0x0F55; // |
PaoloB | 0:15d9f9050d21 | 18 | |
PaoloB | 2:ac580453441f | 19 | //SET ALL OUTPUTS TO 0 |
PaoloB | 2:ac580453441f | 20 | dac_data[7] = 0x4400; // P4 - P11 |
PaoloB | 2:ac580453441f | 21 | dac_data[8] = 0x4C00; // P12 - P19 |
PaoloB | 2:ac580453441f | 22 | dac_data[9] = 0x5400; // P20 - P27 |
PaoloB | 2:ac580453441f | 23 | dac_data[10]= 0x5C00; // P28 - P31 |
PaoloB | 0:15d9f9050d21 | 24 | |
PaoloB | 0:15d9f9050d21 | 25 | //EXIT SHUTDOWN MODE |
PaoloB | 0:15d9f9050d21 | 26 | dac_data[11]= 0x0401; |
PaoloB | 0:15d9f9050d21 | 27 | for (int i = 0; i < nWords; i++) |
PaoloB | 0:15d9f9050d21 | 28 | { |
PaoloB | 0:15d9f9050d21 | 29 | serialOut.printf("WRITING %d (0x%04x).\n", dac_data[i], dac_data[i]); |
PaoloB | 0:15d9f9050d21 | 30 | mySPISend(dac_data[i], dac_port, sync); |
PaoloB | 0:15d9f9050d21 | 31 | } |
PaoloB | 0:15d9f9050d21 | 32 | return 0; |
PaoloB | 0:15d9f9050d21 | 33 | } |
PaoloB | 0:15d9f9050d21 | 34 | |
PaoloB | 2:ac580453441f | 35 | //////////////////////////////////////////////////////// |
PaoloB | 0:15d9f9050d21 | 36 | int sendTransfer(unsigned short data, SPI &dac_port, DigitalOut &sync) |
PaoloB | 0:15d9f9050d21 | 37 | { |
PaoloB | 2:ac580453441f | 38 | |
PaoloB | 0:15d9f9050d21 | 39 | return 0; |
PaoloB | 0:15d9f9050d21 | 40 | } |
PaoloB | 0:15d9f9050d21 | 41 | |
PaoloB | 2:ac580453441f | 42 | //////////////////////////////////////////////////////// |
PaoloB | 0:15d9f9050d21 | 43 | int mySPISend( unsigned short data, SPI &dac_port, DigitalOut &sync) |
PaoloB | 0:15d9f9050d21 | 44 | // This is the basic function used to send the signals. |
PaoloB | 0:15d9f9050d21 | 45 | { |
PaoloB | 0:15d9f9050d21 | 46 | //sync = 1; |
cussans | 6:4d64960c7908 | 47 | wait (0.001); // Changed from 0.5, DGC, 1 Oct 18 |
PaoloB | 0:15d9f9050d21 | 48 | |
PaoloB | 0:15d9f9050d21 | 49 | sync = 0; |
PaoloB | 0:15d9f9050d21 | 50 | |
cussans | 6:4d64960c7908 | 51 | wait(0.0001); // wait 100us for select line to go fully low after . |
cussans | 6:4d64960c7908 | 52 | |
PaoloB | 0:15d9f9050d21 | 53 | 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 | 0:15d9f9050d21 | 54 | dac_port.write(data); |
PaoloB | 0:15d9f9050d21 | 55 | |
cussans | 6:4d64960c7908 | 56 | wait(0.0001); // wait 100us for select line to go fully high. |
PaoloB | 0:15d9f9050d21 | 57 | sync = 1; |
PaoloB | 0:15d9f9050d21 | 58 | |
cussans | 8:2196f2f75947 | 59 | wait (0.05); // changed from 0.5 , DGC, May 2019 |
PaoloB | 0:15d9f9050d21 | 60 | return 0; |
PaoloB | 0:15d9f9050d21 | 61 | } |
PaoloB | 0:15d9f9050d21 | 62 | |
PaoloB | 2:ac580453441f | 63 | //////////////////////////////////////////////////////// |
PaoloB | 2:ac580453441f | 64 | int mySPIRead(Serial &serialOut, unsigned short data, SPI &dac_port, DigitalOut &sync) |
PaoloB | 2:ac580453441f | 65 | // This is the basic function used to send the signals. |
PaoloB | 2:ac580453441f | 66 | { |
PaoloB | 2:ac580453441f | 67 | unsigned short nopdata= 0x0000; |
PaoloB | 2:ac580453441f | 68 | unsigned short return1, return2; |
PaoloB | 2:ac580453441f | 69 | wait (0.5); |
PaoloB | 2:ac580453441f | 70 | |
PaoloB | 2:ac580453441f | 71 | sync = 0; |
PaoloB | 2:ac580453441f | 72 | |
PaoloB | 2:ac580453441f | 73 | 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 | 74 | dac_port.write(data); |
PaoloB | 2:ac580453441f | 75 | |
PaoloB | 2:ac580453441f | 76 | wait(0.000005); // |
PaoloB | 2:ac580453441f | 77 | sync = 1; |
PaoloB | 2:ac580453441f | 78 | |
PaoloB | 2:ac580453441f | 79 | wait (0.2); |
PaoloB | 2:ac580453441f | 80 | sync = 0; |
PaoloB | 2:ac580453441f | 81 | |
PaoloB | 2:ac580453441f | 82 | return1= dac_port.write(nopdata >> 8);// No operation. Used to flush the 16 bits. |
PaoloB | 2:ac580453441f | 83 | return2= dac_port.write(nopdata); |
PaoloB | 2:ac580453441f | 84 | |
PaoloB | 2:ac580453441f | 85 | wait(0.000005); // |
PaoloB | 2:ac580453441f | 86 | sync = 1; |
PaoloB | 2:ac580453441f | 87 | wait (0.5); |
PaoloB | 2:ac580453441f | 88 | serialOut.printf("Returned data = %d (0x%04x), %d (0x%04x).\n", return1, return1, return2, return2); |
PaoloB | 2:ac580453441f | 89 | return 0; |
PaoloB | 2:ac580453441f | 90 | } |
PaoloB | 2:ac580453441f | 91 | |
PaoloB | 2:ac580453441f | 92 | //////////////////////////////////////////////////////// |
PaoloB | 2:ac580453441f | 93 | int powerMode(SPI &dac_port, DigitalOut &sync, int mode) |
PaoloB | 2:ac580453441f | 94 | { |
PaoloB | 2:ac580453441f | 95 | int nWords= 2; |
PaoloB | 2:ac580453441f | 96 | unsigned short dac_data[nWords]; |
PaoloB | 2:ac580453441f | 97 | |
PaoloB | 2:ac580453441f | 98 | |
PaoloB | 2:ac580453441f | 99 | dac_data[0]= 0x0400;//ENTER SHUTDOWN MODE |
PaoloB | 2:ac580453441f | 100 | dac_data[1]= 0x0401;//EXIT SHUTDOWN MODE |
PaoloB | 2:ac580453441f | 101 | |
PaoloB | 2:ac580453441f | 102 | if ((mode==0) || (mode==1)) |
PaoloB | 2:ac580453441f | 103 | { |
PaoloB | 2:ac580453441f | 104 | mySPISend(dac_data[mode], dac_port, sync); |
PaoloB | 2:ac580453441f | 105 | } |
PaoloB | 2:ac580453441f | 106 | |
PaoloB | 2:ac580453441f | 107 | return 0; |
PaoloB | 2:ac580453441f | 108 | } |
PaoloB | 2:ac580453441f | 109 | |
PaoloB | 2:ac580453441f | 110 | //////////////////////////////////////////////////////// |
PaoloB | 2:ac580453441f | 111 | int setPort(SPI &dac_port, DigitalOut &sync, int myPort, int newState) |
PaoloB | 2:ac580453441f | 112 | { |
PaoloB | 2:ac580453441f | 113 | int nWords= 1; |
PaoloB | 2:ac580453441f | 114 | unsigned short dac_data[nWords]; |
PaoloB | 2:ac580453441f | 115 | |
PaoloB | 2:ac580453441f | 116 | dac_data[0]= (myPort + 0x0020)*0x0100 + newState; |
PaoloB | 2:ac580453441f | 117 | mySPISend(dac_data[0], dac_port, sync); |
PaoloB | 2:ac580453441f | 118 | /* |
PaoloB | 2:ac580453441f | 119 | dac_data[0]= 0x0400;//ENTER SHUTDOWN MODE |
PaoloB | 2:ac580453441f | 120 | dac_data[1]= 0x0401;//EXIT SHUTDOWN MODE |
PaoloB | 2:ac580453441f | 121 | |
PaoloB | 2:ac580453441f | 122 | if ((mode==0) || (mode==1)) |
PaoloB | 2:ac580453441f | 123 | { |
PaoloB | 2:ac580453441f | 124 | mySPISend(dac_data[mode], dac_port, sync); |
PaoloB | 2:ac580453441f | 125 | }*/ |
PaoloB | 2:ac580453441f | 126 | return 0; |
PaoloB | 2:ac580453441f | 127 | } |