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