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