Generated a random signal out of SDPK1 PWM header 5
Dependencies: FibonacciLFSR FastPWM
main.cpp@0:8c77f47060aa, 2021-01-25 (annotated)
- Committer:
- aandrews
- Date:
- Mon Jan 25 16:13:37 2021 +0000
- Revision:
- 0:8c77f47060aa
test
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| aandrews | 0:8c77f47060aa | 1 | /***************************************************************************** |
| aandrews | 0:8c77f47060aa | 2 | * Copyright (c) 2019 Analog Devices, Inc. |
| aandrews | 0:8c77f47060aa | 3 | * |
| aandrews | 0:8c77f47060aa | 4 | * All rights reserved. |
| aandrews | 0:8c77f47060aa | 5 | * |
| aandrews | 0:8c77f47060aa | 6 | * Redistribution and use in source and binary forms, with or without |
| aandrews | 0:8c77f47060aa | 7 | * modification, are permitted provided that the following conditions are met: |
| aandrews | 0:8c77f47060aa | 8 | * - Redistributions of source code must retain the above copyright notice, |
| aandrews | 0:8c77f47060aa | 9 | * this list of conditions and the following disclaimer. |
| aandrews | 0:8c77f47060aa | 10 | * - Redistributions in binary form must reproduce the above copyright notice, |
| aandrews | 0:8c77f47060aa | 11 | * this list of conditions and the following disclaimer in the documentation |
| aandrews | 0:8c77f47060aa | 12 | * and/or other materials provided with the distribution. |
| aandrews | 0:8c77f47060aa | 13 | * - Modified versions of the software must be conspicuously marked as such. |
| aandrews | 0:8c77f47060aa | 14 | * - This software is licensed solely and exclusively for use with |
| aandrews | 0:8c77f47060aa | 15 | * processors/products manufactured by or for Analog Devices, Inc. |
| aandrews | 0:8c77f47060aa | 16 | * - This software may not be combined or merged with other code in any manner |
| aandrews | 0:8c77f47060aa | 17 | * that would cause the software to become subject to terms and |
| aandrews | 0:8c77f47060aa | 18 | * conditions which differ from those listed here. |
| aandrews | 0:8c77f47060aa | 19 | * - Neither the name of Analog Devices, Inc. nor the names of its |
| aandrews | 0:8c77f47060aa | 20 | * contributors may be used to endorse or promote products derived |
| aandrews | 0:8c77f47060aa | 21 | * from this software without specific prior written permission. |
| aandrews | 0:8c77f47060aa | 22 | * - The use of this software may or may not infringe the patent rights |
| aandrews | 0:8c77f47060aa | 23 | * of one or more patent holders. This license does not release you from |
| aandrews | 0:8c77f47060aa | 24 | * the requirement that you obtain separate licenses from these patent |
| aandrews | 0:8c77f47060aa | 25 | * holders to use this software. |
| aandrews | 0:8c77f47060aa | 26 | * |
| aandrews | 0:8c77f47060aa | 27 | * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES, INC. AND CONTRIBUTORS "AS IS" |
| aandrews | 0:8c77f47060aa | 28 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| aandrews | 0:8c77f47060aa | 29 | * NON-INFRINGEMENT, TITLE, MERCHANTABILITY AND FITNESS FOR A |
| aandrews | 0:8c77f47060aa | 30 | * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ANALOG DEVICES, |
| aandrews | 0:8c77f47060aa | 31 | * INC. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| aandrews | 0:8c77f47060aa | 32 | * SPECIAL, EXEMPLARY, PUNITIVE OR CONSEQUENTIAL DAMAGES |
| aandrews | 0:8c77f47060aa | 33 | * (INCLUDING, BUT NOT LIMITED TO, DAMAGES ARISING OUT OF CLAIMS OF |
| aandrews | 0:8c77f47060aa | 34 | * INTELLECTUAL PROPERTY RIGHTS INFRINGEMENT; PROCUREMENT OF SUBSTITUTE |
| aandrews | 0:8c77f47060aa | 35 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| aandrews | 0:8c77f47060aa | 36 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| aandrews | 0:8c77f47060aa | 37 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
| aandrews | 0:8c77f47060aa | 38 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| aandrews | 0:8c77f47060aa | 39 | * POSSIBILITY OF SUCH DAMAGE. |
| aandrews | 0:8c77f47060aa | 40 | * |
| aandrews | 0:8c77f47060aa | 41 | * 20180927-7CBSD SLA |
| aandrews | 0:8c77f47060aa | 42 | *****************************************************************************/ |
| aandrews | 0:8c77f47060aa | 43 | |
| aandrews | 0:8c77f47060aa | 44 | /* |
| aandrews | 0:8c77f47060aa | 45 | The SDP connector is a 120 pin connector used on Analog Devices controller |
| aandrews | 0:8c77f47060aa | 46 | boards to interface with ADI evaluation boards, over 450 ADI evaluation boards |
| aandrews | 0:8c77f47060aa | 47 | utilise this connector, a list of these boards can be found at |
| aandrews | 0:8c77f47060aa | 48 | https://www.analog.com/en/design-center/evaluation-hardware-and-software/evaluation-development-platforms/sdp.html#Compatible-Boards. |
| aandrews | 0:8c77f47060aa | 49 | |
| aandrews | 0:8c77f47060aa | 50 | The following is an example program showing how to initialise and use SDP |
| aandrews | 0:8c77f47060aa | 51 | connector peripherals (using SPI and GPIOs as an example) on the SDP-K1 in mbed. |
| aandrews | 0:8c77f47060aa | 52 | Informtion on the pin name definitions for the SDP connector on the SDP-K1 can |
| aandrews | 0:8c77f47060aa | 53 | be found at https://os.mbed.com/platforms/SDP_K1/. |
| aandrews | 0:8c77f47060aa | 54 | The full list of peripherals available on the SDP connector include GPIOs, |
| aandrews | 0:8c77f47060aa | 55 | Timers, SPI, I2C, and UART. For further information on how to enable these |
| aandrews | 0:8c77f47060aa | 56 | peripherals, consult the mbed documentation https://os.mbed.com/docs/mbed-os/v5.15/apis/drivers.html |
| aandrews | 0:8c77f47060aa | 57 | */ |
| aandrews | 0:8c77f47060aa | 58 | #include "mbed.h" |
| aandrews | 0:8c77f47060aa | 59 | #include "platform/mbed_thread.h" |
| aandrews | 0:8c77f47060aa | 60 | #include <bitset> |
| aandrews | 0:8c77f47060aa | 61 | #include "FibonacciLFSR.h" |
| aandrews | 0:8c77f47060aa | 62 | #include "FastPWM.h" |
| aandrews | 0:8c77f47060aa | 63 | |
| aandrews | 0:8c77f47060aa | 64 | // Initialise the serial object with TX and RX pins |
| aandrews | 0:8c77f47060aa | 65 | Serial pc(USBTX, USBRX); |
| aandrews | 0:8c77f47060aa | 66 | |
| aandrews | 0:8c77f47060aa | 67 | // Initialise the digital pin 5 as a fast PWM output, prescaler set to 1 |
| aandrews | 0:8c77f47060aa | 68 | FastPWM fastpwm(D5,1); |
| aandrews | 0:8c77f47060aa | 69 | |
| aandrews | 0:8c77f47060aa | 70 | // Initialise LFSR random |
| aandrews | 0:8c77f47060aa | 71 | FibonacciLFSR lfsr(FibonacciLFSR::LFSR_8); |
| aandrews | 0:8c77f47060aa | 72 | |
| aandrews | 0:8c77f47060aa | 73 | // main() runs in its own thread in the OS |
| aandrews | 0:8c77f47060aa | 74 | int main() |
| aandrews | 0:8c77f47060aa | 75 | { |
| aandrews | 0:8c77f47060aa | 76 | uint16_t lfsr_rand1, lfsr_rand2, lfsr_rand3; |
| aandrews | 0:8c77f47060aa | 77 | int lfsr_rand_test; |
| aandrews | 0:8c77f47060aa | 78 | uint32_t rando = rand(); |
| aandrews | 0:8c77f47060aa | 79 | |
| aandrews | 0:8c77f47060aa | 80 | std::bitset<32> rand_bitset(rando); |
| aandrews | 0:8c77f47060aa | 81 | |
| aandrews | 0:8c77f47060aa | 82 | //pc.printf("System Core Clock: %d\r\n", SystemCoreClock); |
| aandrews | 0:8c77f47060aa | 83 | //pc.printf("Random Number: %d\r\n", rando); |
| aandrews | 0:8c77f47060aa | 84 | |
| aandrews | 0:8c77f47060aa | 85 | while(true){ |
| aandrews | 0:8c77f47060aa | 86 | |
| aandrews | 0:8c77f47060aa | 87 | //fastpwm.period_ticks(4); // fast PWM allows period to be set in ticks of SystemCoreClock. 4 ticks = 45MHz on SDP-K1 |
| aandrews | 0:8c77f47060aa | 88 | //fastpwm.pulsewidth_ticks(2); // duty cycle 50% |
| aandrews | 0:8c77f47060aa | 89 | |
| aandrews | 0:8c77f47060aa | 90 | fastpwm.period_ticks(18); // fast PWM allows period to be set in ticks of SystemCoreClock. 4 ticks = 45MHz on SDP-K1 |
| aandrews | 0:8c77f47060aa | 91 | //fastpwm.pulsewidth_ticks(9); |
| aandrews | 0:8c77f47060aa | 92 | // cycle pulse width randomly |
| aandrews | 0:8c77f47060aa | 93 | for (int i = 0; i < 32; ++i) { |
| aandrews | 0:8c77f47060aa | 94 | if (rand_bitset.test(i)) |
| aandrews | 0:8c77f47060aa | 95 | fastpwm.pulsewidth_ticks(9); |
| aandrews | 0:8c77f47060aa | 96 | else |
| aandrews | 0:8c77f47060aa | 97 | fastpwm.pulsewidth_ticks(0); |
| aandrews | 0:8c77f47060aa | 98 | } |
| aandrews | 0:8c77f47060aa | 99 | } |
| aandrews | 0:8c77f47060aa | 100 | } |
| aandrews | 0:8c77f47060aa | 101 |