Alex Andrews / Mbed OS SDP_K1_Rand

Dependencies:   FibonacciLFSR FastPWM

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*****************************************************************************
00002 * Copyright (c) 2019 Analog Devices, Inc.  
00003 * 
00004 * All rights reserved.
00005 * 
00006 * Redistribution and use in source and binary forms, with or without 
00007 * modification, are permitted provided that the following conditions are met:
00008 *   - Redistributions of source code must retain the above copyright notice, 
00009 *     this list of conditions and the following disclaimer.
00010 *   - Redistributions in binary form must reproduce the above copyright notice, 
00011 *     this list of conditions and the following disclaimer in the documentation 
00012 *     and/or other materials provided with the distribution.  
00013 *   - Modified versions of the software must be conspicuously marked as such.
00014 *   - This software is licensed solely and exclusively for use with 
00015 *     processors/products manufactured by or for Analog Devices, Inc.
00016 *   - This software may not be combined or merged with other code in any manner 
00017 *     that would cause the software to become subject to terms and 
00018 *     conditions which differ from those listed here.
00019 *   - Neither the name of Analog Devices, Inc. nor the names of its 
00020 *     contributors may be used to endorse or promote products derived 
00021 *     from this software without specific prior written permission.
00022 *   - The use of this software may or may not infringe the patent rights 
00023 *     of one or more patent holders. This license does not release you from 
00024 *     the requirement that you obtain separate licenses from these patent 
00025 *     holders to use this software.
00026 * 
00027 * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES, INC. AND CONTRIBUTORS "AS IS" 
00028 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
00029 * NON-INFRINGEMENT, TITLE, MERCHANTABILITY AND FITNESS FOR A 
00030 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ANALOG DEVICES, 
00031 * INC. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
00032 * SPECIAL, EXEMPLARY, PUNITIVE OR CONSEQUENTIAL DAMAGES 
00033 * (INCLUDING, BUT NOT LIMITED TO, DAMAGES ARISING OUT OF CLAIMS OF 
00034 * INTELLECTUAL PROPERTY RIGHTS INFRINGEMENT; PROCUREMENT OF SUBSTITUTE 
00035 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
00036 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
00037 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
00038 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
00039 * POSSIBILITY OF SUCH DAMAGE.
00040 * 
00041 * 20180927-7CBSD SLA
00042 *****************************************************************************/
00043 
00044 /* 
00045 The SDP connector is a 120 pin connector used on Analog Devices controller 
00046 boards to interface with ADI evaluation boards, over 450 ADI evaluation boards 
00047 utilise this connector, a list of these boards can be found at 
00048 https://www.analog.com/en/design-center/evaluation-hardware-and-software/evaluation-development-platforms/sdp.html#Compatible-Boards.
00049 
00050 The following is an example program showing how to initialise and use SDP 
00051 connector peripherals (using SPI and GPIOs as an example) on the SDP-K1 in mbed.
00052 Informtion on the pin name definitions for the SDP connector on the SDP-K1 can 
00053 be found at https://os.mbed.com/platforms/SDP_K1/.
00054 The full list of peripherals available on the SDP connector include GPIOs, 
00055 Timers, SPI, I2C, and UART. For further information on how to enable these 
00056 peripherals, consult the mbed documentation https://os.mbed.com/docs/mbed-os/v5.15/apis/drivers.html
00057 */
00058 #include "mbed.h"
00059 #include "platform/mbed_thread.h"
00060 #include <bitset>
00061 #include "FibonacciLFSR.h"
00062 #include "FastPWM.h"
00063 
00064 // Initialise the serial object with TX and RX pins
00065 Serial pc(USBTX, USBRX);
00066 
00067 // Initialise the digital pin 5 as a fast PWM output, prescaler set to 1
00068 FastPWM fastpwm(D5,1);
00069  
00070  // Initialise LFSR random
00071 FibonacciLFSR lfsr(FibonacciLFSR::LFSR_8);
00072       
00073 // main() runs in its own thread in the OS
00074 int main()
00075 {   
00076     uint16_t lfsr_rand1, lfsr_rand2, lfsr_rand3;
00077     int lfsr_rand_test;
00078     uint32_t rando = rand();
00079     
00080     std::bitset<32> rand_bitset(rando);
00081     
00082     //pc.printf("System Core Clock: %d\r\n", SystemCoreClock);  
00083     //pc.printf("Random Number: %d\r\n", rando); 
00084        
00085     while(true){
00086         
00087         //fastpwm.period_ticks(4); // fast PWM allows period to be set in ticks of SystemCoreClock. 4 ticks = 45MHz on SDP-K1
00088         //fastpwm.pulsewidth_ticks(2); // duty cycle 50%
00089         
00090         fastpwm.period_ticks(18); // fast PWM allows period to be set in ticks of SystemCoreClock. 4 ticks = 45MHz on SDP-K1
00091         //fastpwm.pulsewidth_ticks(9);
00092         // cycle pulse width randomly
00093         for (int i = 0; i < 32; ++i) {
00094             if (rand_bitset.test(i))             
00095                 fastpwm.pulsewidth_ticks(9);           
00096             else
00097                  fastpwm.pulsewidth_ticks(0);           
00098         }       
00099     }
00100 }
00101