Mini projet LOO
Dependencies: mbed APDS_9960 mbed-rtos
Home du projet
src/cSPI.cpp@30:c647da947bd9, 2016-04-21 (annotated)
- Committer:
- clementdoreau
- Date:
- Thu Apr 21 12:33:58 2016 +0000
- Revision:
- 30:c647da947bd9
- Parent:
- 29:95469b25e187
- Child:
- 31:7313366789f2
marche pas
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Willheisen | 9:6f3d8b714a59 | 1 | /* |
Willheisen | 9:6f3d8b714a59 | 2 | * cSPI.cpp |
Willheisen | 9:6f3d8b714a59 | 3 | * |
Willheisen | 9:6f3d8b714a59 | 4 | * Created on: 5 avr. 2016 |
Willheisen | 9:6f3d8b714a59 | 5 | * Author: clement |
Willheisen | 9:6f3d8b714a59 | 6 | */ |
Willheisen | 9:6f3d8b714a59 | 7 | |
Willheisen | 9:6f3d8b714a59 | 8 | #include "cSPI.h" |
Willheisen | 9:6f3d8b714a59 | 9 | #include "cMatrice.h" |
Willheisen | 9:6f3d8b714a59 | 10 | |
clementdoreau | 29:95469b25e187 | 11 | DigitalOut _cs(p14); |
clementdoreau | 29:95469b25e187 | 12 | SPI _spi(p11,p12, p13); |
Willheisen | 9:6f3d8b714a59 | 13 | |
clementdoreau | 28:e932eb039271 | 14 | // CONSTRUCTEURS |
Willheisen | 9:6f3d8b714a59 | 15 | |
clementdoreau | 28:e932eb039271 | 16 | cSPI::cSPI() |
clementdoreau | 28:e932eb039271 | 17 | { |
clementdoreau | 30:c647da947bd9 | 18 | |
clementdoreau | 28:e932eb039271 | 19 | } |
Willheisen | 9:6f3d8b714a59 | 20 | |
clementdoreau | 29:95469b25e187 | 21 | cSPI::cSPI(unsigned int f, unsigned int m, unsigned int b) |
clementdoreau | 29:95469b25e187 | 22 | { |
clementdoreau | 29:95469b25e187 | 23 | _freq = f; |
clementdoreau | 29:95469b25e187 | 24 | _mode = m; |
clementdoreau | 29:95469b25e187 | 25 | _bits = b; |
clementdoreau | 29:95469b25e187 | 26 | |
clementdoreau | 29:95469b25e187 | 27 | _cs = 0; |
clementdoreau | 29:95469b25e187 | 28 | wait(0.5); |
clementdoreau | 29:95469b25e187 | 29 | _spi.frequency(_freq); |
clementdoreau | 29:95469b25e187 | 30 | _spi.format(_bits, _mode); |
clementdoreau | 29:95469b25e187 | 31 | wait(0.5); |
clementdoreau | 29:95469b25e187 | 32 | _cs = 1; |
clementdoreau | 29:95469b25e187 | 33 | } |
clementdoreau | 29:95469b25e187 | 34 | |
clementdoreau | 28:e932eb039271 | 35 | // DESTRUCTEUR |
Willheisen | 9:6f3d8b714a59 | 36 | |
clementdoreau | 28:e932eb039271 | 37 | cSPI::~cSPI() |
clementdoreau | 28:e932eb039271 | 38 | { |
Willheisen | 9:6f3d8b714a59 | 39 | |
clementdoreau | 28:e932eb039271 | 40 | } |
clementdoreau | 28:e932eb039271 | 41 | // GETTERS |
Willheisen | 9:6f3d8b714a59 | 42 | |
clementdoreau | 28:e932eb039271 | 43 | unsigned int cSPI::getFrequence() |
clementdoreau | 28:e932eb039271 | 44 | { |
clementdoreau | 28:e932eb039271 | 45 | return _freq; |
clementdoreau | 28:e932eb039271 | 46 | } |
Willheisen | 9:6f3d8b714a59 | 47 | |
clementdoreau | 28:e932eb039271 | 48 | unsigned int cSPI::getMode() |
clementdoreau | 28:e932eb039271 | 49 | { |
clementdoreau | 28:e932eb039271 | 50 | return _mode; |
clementdoreau | 28:e932eb039271 | 51 | } |
Willheisen | 9:6f3d8b714a59 | 52 | |
clementdoreau | 29:95469b25e187 | 53 | unsigned int cSPI::getBits() |
clementdoreau | 28:e932eb039271 | 54 | { |
clementdoreau | 28:e932eb039271 | 55 | return _bits; |
clementdoreau | 28:e932eb039271 | 56 | } |
clementdoreau | 28:e932eb039271 | 57 | |
clementdoreau | 28:e932eb039271 | 58 | // SETTERS |
Willheisen | 9:6f3d8b714a59 | 59 | |
clementdoreau | 28:e932eb039271 | 60 | void cSPI::setFrequence(unsigned int freq) |
clementdoreau | 28:e932eb039271 | 61 | { |
clementdoreau | 28:e932eb039271 | 62 | _freq = freq; |
clementdoreau | 28:e932eb039271 | 63 | } |
Willheisen | 9:6f3d8b714a59 | 64 | |
clementdoreau | 28:e932eb039271 | 65 | void cSPI::setMode(unsigned int mode) |
clementdoreau | 28:e932eb039271 | 66 | { |
clementdoreau | 28:e932eb039271 | 67 | _mode = mode; |
clementdoreau | 28:e932eb039271 | 68 | } |
Willheisen | 9:6f3d8b714a59 | 69 | |
clementdoreau | 29:95469b25e187 | 70 | void cSPI::setBits(unsigned int bits) |
clementdoreau | 28:e932eb039271 | 71 | { |
clementdoreau | 28:e932eb039271 | 72 | _bits = bits; |
clementdoreau | 28:e932eb039271 | 73 | } |
Willheisen | 9:6f3d8b714a59 | 74 | |
clementdoreau | 28:e932eb039271 | 75 | // METHODES |
Willheisen | 9:6f3d8b714a59 | 76 | |
clementdoreau | 28:e932eb039271 | 77 | void cSPI::initSPI(unsigned int frequence, unsigned int bits, unsigned int mode) |
clementdoreau | 28:e932eb039271 | 78 | { |
clementdoreau | 30:c647da947bd9 | 79 | Serial pc(USBTX, USBRX); |
clementdoreau | 30:c647da947bd9 | 80 | _freq = frequence; |
clementdoreau | 28:e932eb039271 | 81 | _cs = 0; |
clementdoreau | 30:c647da947bd9 | 82 | wait_us(500); |
clementdoreau | 30:c647da947bd9 | 83 | _spi.frequency(_freq); |
clementdoreau | 30:c647da947bd9 | 84 | //_spi.format(bits, mode); |
clementdoreau | 30:c647da947bd9 | 85 | wait_us(500); |
clementdoreau | 28:e932eb039271 | 86 | _cs = 1; |
clementdoreau | 30:c647da947bd9 | 87 | pc.printf("Liaison SPI Initialisee\n"); |
clementdoreau | 30:c647da947bd9 | 88 | |
clementdoreau | 28:e932eb039271 | 89 | } |
Willheisen | 9:6f3d8b714a59 | 90 | |
Willheisen | 27:fdcff6af71b5 | 91 | int cSPI::envoyerMatrice(cMatrice & mat) // Renvoi la reponse SPI |
Willheisen | 27:fdcff6af71b5 | 92 | { |
clementdoreau | 30:c647da947bd9 | 93 | |
clementdoreau | 28:e932eb039271 | 94 | _cs = 0; |
clementdoreau | 30:c647da947bd9 | 95 | wait_us(500); |
clementdoreau | 30:c647da947bd9 | 96 | for(int i = 0; i < mat.getLig(); i++) { |
clementdoreau | 29:95469b25e187 | 97 | for(int j = 0; j < mat.getCol(); j ++) { |
clementdoreau | 28:e932eb039271 | 98 | _spi.write(mat.getValTab(i, j)); |
Willheisen | 9:6f3d8b714a59 | 99 | } |
Willheisen | 27:fdcff6af71b5 | 100 | } |
clementdoreau | 30:c647da947bd9 | 101 | wait_us(500); |
clementdoreau | 28:e932eb039271 | 102 | _cs = 1; //avant 0 |
Willheisen | 27:fdcff6af71b5 | 103 | return 1; // temporaire |
clementdoreau | 28:e932eb039271 | 104 | } |
Willheisen | 9:6f3d8b714a59 | 105 | |
clementdoreau | 29:95469b25e187 | 106 | void cSPI::configurerNbMatrices(const int nb) |
clementdoreau | 28:e932eb039271 | 107 | { |
clementdoreau | 28:e932eb039271 | 108 | _cs = 0; |
clementdoreau | 30:c647da947bd9 | 109 | wait_us(500); |
clementdoreau | 30:c647da947bd9 | 110 | _spi.write('%'); |
clementdoreau | 29:95469b25e187 | 111 | _spi.write(nb); |
clementdoreau | 30:c647da947bd9 | 112 | wait_us(500); |
clementdoreau | 28:e932eb039271 | 113 | _cs = 1; |
clementdoreau | 28:e932eb039271 | 114 | } |