mbed library sources with internal temperature sensor for nucleo f401
api/SPISlave.h@0:7e2bd16f80af, 2015-01-17 (annotated)
- Committer:
- elessair
- Date:
- Sat Jan 17 18:03:58 2015 +0000
- Revision:
- 0:7e2bd16f80af
nucleo f401re internal temperature added
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
elessair | 0:7e2bd16f80af | 1 | /* mbed Microcontroller Library |
elessair | 0:7e2bd16f80af | 2 | * Copyright (c) 2006-2013 ARM Limited |
elessair | 0:7e2bd16f80af | 3 | * |
elessair | 0:7e2bd16f80af | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
elessair | 0:7e2bd16f80af | 5 | * you may not use this file except in compliance with the License. |
elessair | 0:7e2bd16f80af | 6 | * You may obtain a copy of the License at |
elessair | 0:7e2bd16f80af | 7 | * |
elessair | 0:7e2bd16f80af | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
elessair | 0:7e2bd16f80af | 9 | * |
elessair | 0:7e2bd16f80af | 10 | * Unless required by applicable law or agreed to in writing, software |
elessair | 0:7e2bd16f80af | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
elessair | 0:7e2bd16f80af | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
elessair | 0:7e2bd16f80af | 13 | * See the License for the specific language governing permissions and |
elessair | 0:7e2bd16f80af | 14 | * limitations under the License. |
elessair | 0:7e2bd16f80af | 15 | */ |
elessair | 0:7e2bd16f80af | 16 | #ifndef MBED_SPISLAVE_H |
elessair | 0:7e2bd16f80af | 17 | #define MBED_SPISLAVE_H |
elessair | 0:7e2bd16f80af | 18 | |
elessair | 0:7e2bd16f80af | 19 | #include "platform.h" |
elessair | 0:7e2bd16f80af | 20 | |
elessair | 0:7e2bd16f80af | 21 | #if DEVICE_SPISLAVE |
elessair | 0:7e2bd16f80af | 22 | |
elessair | 0:7e2bd16f80af | 23 | #include "spi_api.h" |
elessair | 0:7e2bd16f80af | 24 | |
elessair | 0:7e2bd16f80af | 25 | namespace mbed { |
elessair | 0:7e2bd16f80af | 26 | |
elessair | 0:7e2bd16f80af | 27 | /** A SPI slave, used for communicating with a SPI Master device |
elessair | 0:7e2bd16f80af | 28 | * |
elessair | 0:7e2bd16f80af | 29 | * The default format is set to 8-bits, mode 0, and a clock frequency of 1MHz |
elessair | 0:7e2bd16f80af | 30 | * |
elessair | 0:7e2bd16f80af | 31 | * Example: |
elessair | 0:7e2bd16f80af | 32 | * @code |
elessair | 0:7e2bd16f80af | 33 | * // Reply to a SPI master as slave |
elessair | 0:7e2bd16f80af | 34 | * |
elessair | 0:7e2bd16f80af | 35 | * #include "mbed.h" |
elessair | 0:7e2bd16f80af | 36 | * |
elessair | 0:7e2bd16f80af | 37 | * SPISlave device(p5, p6, p7, p8); // mosi, miso, sclk, ssel |
elessair | 0:7e2bd16f80af | 38 | * |
elessair | 0:7e2bd16f80af | 39 | * int main() { |
elessair | 0:7e2bd16f80af | 40 | * device.reply(0x00); // Prime SPI with first reply |
elessair | 0:7e2bd16f80af | 41 | * while(1) { |
elessair | 0:7e2bd16f80af | 42 | * if(device.receive()) { |
elessair | 0:7e2bd16f80af | 43 | * int v = device.read(); // Read byte from master |
elessair | 0:7e2bd16f80af | 44 | * v = (v + 1) % 0x100; // Add one to it, modulo 256 |
elessair | 0:7e2bd16f80af | 45 | * device.reply(v); // Make this the next reply |
elessair | 0:7e2bd16f80af | 46 | * } |
elessair | 0:7e2bd16f80af | 47 | * } |
elessair | 0:7e2bd16f80af | 48 | * } |
elessair | 0:7e2bd16f80af | 49 | * @endcode |
elessair | 0:7e2bd16f80af | 50 | */ |
elessair | 0:7e2bd16f80af | 51 | class SPISlave { |
elessair | 0:7e2bd16f80af | 52 | |
elessair | 0:7e2bd16f80af | 53 | public: |
elessair | 0:7e2bd16f80af | 54 | |
elessair | 0:7e2bd16f80af | 55 | /** Create a SPI slave connected to the specified pins |
elessair | 0:7e2bd16f80af | 56 | * |
elessair | 0:7e2bd16f80af | 57 | * Pin Options: |
elessair | 0:7e2bd16f80af | 58 | * (5, 6, 7i, 8) or (11, 12, 13, 14) |
elessair | 0:7e2bd16f80af | 59 | * |
elessair | 0:7e2bd16f80af | 60 | * mosi or miso can be specfied as NC if not used |
elessair | 0:7e2bd16f80af | 61 | * |
elessair | 0:7e2bd16f80af | 62 | * @param mosi SPI Master Out, Slave In pin |
elessair | 0:7e2bd16f80af | 63 | * @param miso SPI Master In, Slave Out pin |
elessair | 0:7e2bd16f80af | 64 | * @param sclk SPI Clock pin |
elessair | 0:7e2bd16f80af | 65 | * @param ssel SPI chip select pin |
elessair | 0:7e2bd16f80af | 66 | * @param name (optional) A string to identify the object |
elessair | 0:7e2bd16f80af | 67 | */ |
elessair | 0:7e2bd16f80af | 68 | SPISlave(PinName mosi, PinName miso, PinName sclk, PinName ssel); |
elessair | 0:7e2bd16f80af | 69 | |
elessair | 0:7e2bd16f80af | 70 | /** Configure the data transmission format |
elessair | 0:7e2bd16f80af | 71 | * |
elessair | 0:7e2bd16f80af | 72 | * @param bits Number of bits per SPI frame (4 - 16) |
elessair | 0:7e2bd16f80af | 73 | * @param mode Clock polarity and phase mode (0 - 3) |
elessair | 0:7e2bd16f80af | 74 | * |
elessair | 0:7e2bd16f80af | 75 | * @code |
elessair | 0:7e2bd16f80af | 76 | * mode | POL PHA |
elessair | 0:7e2bd16f80af | 77 | * -----+-------- |
elessair | 0:7e2bd16f80af | 78 | * 0 | 0 0 |
elessair | 0:7e2bd16f80af | 79 | * 1 | 0 1 |
elessair | 0:7e2bd16f80af | 80 | * 2 | 1 0 |
elessair | 0:7e2bd16f80af | 81 | * 3 | 1 1 |
elessair | 0:7e2bd16f80af | 82 | * @endcode |
elessair | 0:7e2bd16f80af | 83 | */ |
elessair | 0:7e2bd16f80af | 84 | void format(int bits, int mode = 0); |
elessair | 0:7e2bd16f80af | 85 | |
elessair | 0:7e2bd16f80af | 86 | /** Set the spi bus clock frequency |
elessair | 0:7e2bd16f80af | 87 | * |
elessair | 0:7e2bd16f80af | 88 | * @param hz SCLK frequency in hz (default = 1MHz) |
elessair | 0:7e2bd16f80af | 89 | */ |
elessair | 0:7e2bd16f80af | 90 | void frequency(int hz = 1000000); |
elessair | 0:7e2bd16f80af | 91 | |
elessair | 0:7e2bd16f80af | 92 | /** Polls the SPI to see if data has been received |
elessair | 0:7e2bd16f80af | 93 | * |
elessair | 0:7e2bd16f80af | 94 | * @returns |
elessair | 0:7e2bd16f80af | 95 | * 0 if no data, |
elessair | 0:7e2bd16f80af | 96 | * 1 otherwise |
elessair | 0:7e2bd16f80af | 97 | */ |
elessair | 0:7e2bd16f80af | 98 | int receive(void); |
elessair | 0:7e2bd16f80af | 99 | |
elessair | 0:7e2bd16f80af | 100 | /** Retrieve data from receive buffer as slave |
elessair | 0:7e2bd16f80af | 101 | * |
elessair | 0:7e2bd16f80af | 102 | * @returns |
elessair | 0:7e2bd16f80af | 103 | * the data in the receive buffer |
elessair | 0:7e2bd16f80af | 104 | */ |
elessair | 0:7e2bd16f80af | 105 | int read(void); |
elessair | 0:7e2bd16f80af | 106 | |
elessair | 0:7e2bd16f80af | 107 | /** Fill the transmission buffer with the value to be written out |
elessair | 0:7e2bd16f80af | 108 | * as slave on the next received message from the master. |
elessair | 0:7e2bd16f80af | 109 | * |
elessair | 0:7e2bd16f80af | 110 | * @param value the data to be transmitted next |
elessair | 0:7e2bd16f80af | 111 | */ |
elessair | 0:7e2bd16f80af | 112 | void reply(int value); |
elessair | 0:7e2bd16f80af | 113 | |
elessair | 0:7e2bd16f80af | 114 | protected: |
elessair | 0:7e2bd16f80af | 115 | spi_t _spi; |
elessair | 0:7e2bd16f80af | 116 | |
elessair | 0:7e2bd16f80af | 117 | int _bits; |
elessair | 0:7e2bd16f80af | 118 | int _mode; |
elessair | 0:7e2bd16f80af | 119 | int _hz; |
elessair | 0:7e2bd16f80af | 120 | }; |
elessair | 0:7e2bd16f80af | 121 | |
elessair | 0:7e2bd16f80af | 122 | } // namespace mbed |
elessair | 0:7e2bd16f80af | 123 | |
elessair | 0:7e2bd16f80af | 124 | #endif |
elessair | 0:7e2bd16f80af | 125 | |
elessair | 0:7e2bd16f80af | 126 | #endif |