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