Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend (& deprecate) lib_random #4

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions examples/AN002xx_random_number_generation/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
TARGET = SMART-MIC-4-TILE-1V0
APP_NAME =
USED_MODULES = lib_random

XCC_FLAGS = -O2 -g -Wunused -fxscope
VERBOSE = 0

XMOS_MAKE_PATH ?= ../..
-include $(XMOS_MAKE_PATH)/xcommon/module_xcommon/build/Makefile.common
34 changes: 34 additions & 0 deletions examples/AN002xx_random_number_generation/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
Noise Suppression and Automatic Gain Control example
====================================================

.. version:: 0.0.1

Summary
-------

This example demonstrates how to use the automatic gain control library,
the noise suppression library, and the microphone array library in order to
create a meaningful AGC

Required tools and libraries
............................

.. appdeps::

Required hardware
.................

A smart microphone board.

Prerequisites
.............

* This document assumes familiarity with the XMOS xCORE architecture,
the XMOS tool chain and the xC language. Documentation related to these
aspects which are not specific to this application note are linked to in
the references appendix.

* For a description of XMOS related terms found in this document
please see the XMOS Glossary [#]_.

.. [#] http://www.xmos.com/published/glossary
153 changes: 153 additions & 0 deletions examples/AN002xx_random_number_generation/SMART-MIC-4-TILE-1V0.xn
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
<?xml version="1.0" encoding="UTF-8"?>
<Network xmlns="http://www.xmos.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.xmos.com http://www.xmos.com">
<Type>Device</Type>
<Name>Mic Array Quad Tile</Name>

<Declarations>
<Declaration>tileref tile[4]</Declaration>
<Declaration>tileref usb_tile</Declaration>
</Declarations>

<Packages>
<Package id="0" Type="XS2-UFnA-512-TQ128">
<Nodes>
<Node Id="0" InPackageId="0" Type="XS2-L16A-512" Oscillator="24MHz" SystemFrequency="500MHz" ReferenceFrequency="100MHz">

<Boot>
<Source Location="bootFlash0"/>
<Bootee NodeId="2"/>
</Boot>
<Tile Number="0" Reference="tile[0]">
<!-- Quad flash ports -->
<Port Location="XS1_PORT_1B" Name="PORT_SQI_CS"/>
<Port Location="XS1_PORT_1C" Name="PORT_SQI_SCLK"/>
<Port Location="XS1_PORT_4B" Name="PORT_SQI_SIO"/>

<Port Location="XS1_PORT_1M" Name="PORT_PLL_MOD"/>
<Port Location="XS1_PORT_1P" Name="PORT_BGT_SPI_CLK"/>
<Port Location="XS1_PORT_1A" Name="PORT_BGT_SPI_CS_N"/>
<Port Location="XS1_PORT_1F" Name="PORT_BGT_SPI_MISO"/>
<Port Location="XS1_PORT_1E" Name="PORT_BGT_SPI_MOSI"/>
<Port Location="XS1_PORT_1D" Name="PORT_BGT_IRQ"/>
<Port Location="XS1_PORT_1O" Name="PORT_I2C_SCL"/>
<Port Location="XS1_PORT_1N" Name="PORT_I2C_SDA"/>
</Tile>
<Tile Number="1" Reference="tile[1]">
<!-- USB -->
<Port Location="XS1_PORT_1H" Name="PORT_USB_TX_READYIN"/>
<Port Location="XS1_PORT_1J" Name="PORT_USB_CLK"/>
<Port Location="XS1_PORT_1K" Name="PORT_USB_TX_READYOUT"/>
<Port Location="XS1_PORT_1I" Name="PORT_USB_RX_READY"/>
<Port Location="XS1_PORT_1E" Name="PORT_USB_FLAG0"/>
<Port Location="XS1_PORT_1F" Name="PORT_USB_FLAG1"/>
<Port Location="XS1_PORT_1G" Name="PORT_USB_FLAG2"/>
<Port Location="XS1_PORT_8A" Name="PORT_USB_TXD"/>
<Port Location="XS1_PORT_8B" Name="PORT_USB_RXD"/>

<!-- I2C -->
<!-- Note: Bit 1 of PORT 4E is I2C_SCL -->
<!-- Bit 2 of PORT 4E is I2C_SDA -->
<Port Location="XS1_PORT_4E" Name="PORT_I2C_SCL_SDA"/>
<Port Location="XS1_PORT_4F" Name="PORT_DAC_RST_N"/>

<!-- I2S -->
<Port Location="XS1_PORT_1M" Name="PORT_I2S_BCLK"/>
<Port Location="XS1_PORT_1N" Name="PORT_I2S_LRCLK"/>
<Port Location="XS1_PORT_1O" Name="PORT_MCLK_IN"/>
<Port Location="XS1_PORT_1P" Name="PORT_I2S_DAC0"/>
<Port Location="XS1_PORT_1L" Name="PORT_I2S_DAC1"/>
<Port Location="XS1_PORT_1C" Name="PORT_I2S_ADC0"/>
<Port Location="XS1_PORT_16B" Name="PORT_MCLK_COUNT"/>
</Tile>
</Node>
<Node Id="1" InPackageId="1" Type="periph:XS1-SU" Reference="usb_tile" Oscillator="24MHz"/>
</Nodes>
<Links>
<Link Encoding="5wire">
<LinkEndpoint NodeId="0" Link="8" Delays="52clk,52clk"/>
<LinkEndpoint NodeId="1" Link="XL0" Delays="1clk,1clk"/>
</Link>
</Links>
</Package>

<Package id="1" Type="XS2-LnA-512-TQ128">
<Nodes>
<Node Id="2" InPackageId="0" Type="XS2-L16A-512" SystemFrequency="500MHz" Oscillator="24MHz" ReferenceFrequency="100MHz">
<Boot>
<Source Location="LINK" BootMode="4"/>
</Boot>
<Tile Number="0" Reference="tile[2]">
<!-- Mics -->
<!-- Notes: XS1_PORT_4C is for mics 0, 1, 6 and 7 -->
<!-- XS1_PORT_4D is for mics 2, 3, 4 and 5 -->
<Port Location="XS1_PORT_8B" Name="PORT_PDM_DATA"/>
<Port Location="XS1_PORT_1G" Name="PORT_PDM_MCLK"/>
<Port Location="XS1_PORT_1H" Name="PORT_PDM_CLK"/>
</Tile>
<Tile Number="1" Reference="tile[3]">
<!-- Wireless LAN -->
<Port Location="XS1_PORT_1A" Name="PORT_WLAN_SPI_MOSI"/>
<Port Location="XS1_PORT_1B" Name="PORT_WLAN_SPI_MISO"/>
<Port Location="XS1_PORT_4A" Name="PORT_WLAN_WLAN_SPI_IRQ_N"/>
<!-- Note: Bit 1 of PORT 4B is WLAN_3V3_EN -->
<!-- Bit 2 of PORT 4B is WLAN_RST_N -->
<Port Location="XS1_PORT_4B" Name="PORT_WLAN_3V3_EN_RST_N"/>
<Port Location="XS1_PORT_1C" Name="PORT_WLAN_SPI_CLK"/>
<Port Location="XS1_PORT_1D" Name="PORT_WLAN_SPI_CS_N"/>

<!-- Button ports -->
<Port Location="XS1_PORT_4C" Name="PORT_BUT_A_TO_D"/>

<!-- LED ports -->
<Port Location="XS1_PORT_8C" Name="PORT_LED0_TO_7"/>
<!-- Note: Only bits 4-7 of PORT 8D are for LEDs -->
<Port Location="XS1_PORT_8D" Name="PORT_LED8_TO_11"/>
<Port Location="XS1_PORT_1J" Name="PORT_LED_12"/>
</Tile>
</Node>
</Nodes>
</Package>
</Packages>

<Links>
<Link Encoding="2wire" Delays="3clk">
<LinkEndpoint NodeId="0" Link="7"/>
<LinkEndpoint NodeId="2" Link="0"/>
</Link>
<Link Encoding="5wire" Delays="3clk">
<LinkEndpoint NodeId="0" Link="4"/>
<LinkEndpoint NodeId="2" Link="3"/>
</Link>
</Links>

<!-- XSCOPE -->
<Nodes>
<Node Id="3" Type="device:" RoutingId="0x8000">
<Service Id="0" Proto="xscope_host_data(chanend c);">
<Chanend Identifier="c" end="3"/>
</Service>
</Node>
</Nodes>
<Links>
<Link Encoding="2wire" Delays="4,4" Flags="XSCOPE">
<LinkEndpoint NodeId="0" Link="XL0"/>
<LinkEndpoint NodeId="3" Chanend="1"/>
</Link>
</Links>

<ExternalDevices>
<Device NodeId="0" Tile="0" Class="SQIFlash" Name="bootFlash0">
<Attribute Name="PORT_SQI_CS" Value="PORT_SQI_CS"/>
<Attribute Name="PORT_SQI_SCLK" Value="PORT_SQI_SCLK"/>
<Attribute Name="PORT_SQI_SIO" Value="PORT_SQI_SIO"/>
</Device>
</ExternalDevices>

<JTAGChain>
<JTAGDevice NodeId="0"/>
<JTAGDevice NodeId="2"/>
</JTAGChain>

</Network>
23 changes: 23 additions & 0 deletions examples/AN002xx_random_number_generation/config.xscope
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>

<!-- ======================================================= -->
<!-- The 'ioMode' attribute on the xSCOPEconfig -->
<!-- element can take the following values: -->
<!-- "none", "basic", "timed" -->
<!-- -->
<!-- The 'type' attribute on Probe -->
<!-- elements can take the following values: -->
<!-- "STARTSTOP", "CONTINUOUS", "DISCRETE", "STATEMACHINE" -->
<!-- -->
<!-- The 'datatype' attribute on Probe -->
<!-- elements can take the following values: -->
<!-- "NONE", "UINT", "INT", "FLOAT" -->
<!-- ======================================================= -->

<xSCOPEconfig ioMode="basic" enabled="false">

<!-- For example: -->
<Probe name="CH0" type="CONTINUOUS" datatype="INT" units="Value" enabled="true"/>
<!-- From the target code, call: xscope_int(PROBE_NAME, value); -->

</xSCOPEconfig>
11 changes: 11 additions & 0 deletions examples/AN002xx_random_number_generation/doc/rst/AN002xx.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.. include:: ../../README.rst

|newpage|

Overview
--------

Introduction
............

This demo application shows how to use random numbers.
2 changes: 2 additions & 0 deletions examples/AN002xx_random_number_generation/doc/rst/xdoc.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
XMOSNEWSTYLE=1
SOURCE_INCLUDE_DIRS=../../src
136 changes: 136 additions & 0 deletions examples/AN002xx_random_number_generation/src/random_numbers.xc
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
// Copyright (c) 2016-2017, XMOS Ltd, All rights reserved

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <xs1.h>
#include "random_bit.h"

#define MAXX 1024
#define MAXY 1024
#define MAXBITS (MAXX * MAXY)

unsigned char thebits[MAXBITS/8];

unsigned int getbit(int i) {
return (thebits[i>>3] >> (i&7)) & 1;
}

unsigned int setbit(int i, int val) {
return thebits[i>>3] |= val << (i&7);
}

void blocktest(int n) {
int hist[256];
for(int i = 0; i < 256; i++) {
hist[i] = 0;
}
for(int i = 0; i < MAXBITS - n; i++) {
int val = 0;
for(int j = 0; j < n; j++) {
val = val << 1 | getbit(i+j);
}
hist[val]++;
}
printf("Blocktest %d\n", n);
for(int i = 0; i < (1<<n); i++) {
printf(" Bin %d: %d %s\n", i, hist[i], abs(hist[i]-(MAXBITS >> n)) < MAXX ? "Ok" : "Hmm");
}
printf("\n");
}

void runtest() {
int hist[200];
int old = 0;
int run = 0;
for(int i = 0; i < 200; i++) {
hist[i] = 0;
}
for(int i = 0; i < MAXBITS; i++) {
if(getbit(i) == old) {
run++;
} else {
hist[run]++;
run = 1;
}
}
printf("Runtest:\n");
for(int i = 1; i < 10; i++) {
printf(" Bin %d: %d %s\n", i, hist[i], abs(hist[i] - (MAXBITS >> (i+1))) < (MAXX >> (i/2)) ? "Ok" : "Hmm" );
}
printf("\n");
}

void fillmetrue(void) {
timer tmr;
int t0, t1;
int sum = 0;
tmr :> t0;
random_bit_start();
while(sum < MAXBITS) {
uint32_t bit_time;
if (random_bit(bit_time)) {
//assert((bit_time & ~1UL)==0);
setbit(sum, bit_time);
sum++;
if ((sum & 0xffff) == 0) {
int time;
asm volatile ("gettime %0" : "=r" (time));
printf("%11d %d\n", time, sum);
}
}
//else
// tmr when timerafter(bit_time) :> void;
}
random_bit_stop();
tmr :> t1;
printf("Ticks taken: %d, %d per bit\n", t1-t0, (t1-t0)/sum);
}

void xmain(chanend done[7]) {
for(int i = 0; i < 7; i++) {
done[i] <: 0;
}
fillmetrue();
blocktest(1);
blocktest(2);
blocktest(3);
runtest();
return;
printf("P2\n%d %d\n1\n", MAXX, MAXY);
for(int i = 0; i < MAXX; i++) {
for(int j = 0; j < MAXY; j++) {
printf("%d ", getbit(i*MAXY+j));
}
printf("\n");
}
}

int busy(int x, chanend y) {
while(1) {
for(int i = 0; i < 1000; i++) {
x = x * 1234567 + 1;
}
select {
case y :> int _: return x;
default: break;
}
}
return x;
}


int main(void) {
chan done[7];
par {
busy(0x3, done[0]);
busy(0x4, done[1]);
busy(0x5, done[2]);
busy(0x6, done[3]);
busy(0x7, done[4]);
busy(0x8, done[5]);
busy(0x9, done[6]);
xmain(done);
}
return 0;
}
10 changes: 10 additions & 0 deletions examples/dieharder/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
TARGET = STARTKIT
APP_NAME =
USED_MODULES = lib_random

XCC_FLAGS = -Os -g -fcmdline-buffer-bytes=100
#XCC_FLAGS += -save-temps -v


XMOS_MAKE_PATH ?= ../..
-include $(XMOS_MAKE_PATH)/xcommon/module_xcommon/build/Makefile.common
11 changes: 11 additions & 0 deletions examples/dieharder/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
This is a wrapper for testing the quality of the PRNG using dieharder.
http://webhome.phy.duke.edu/~rgb/General/dieharder.php

It is slow as it runs the random_prng.h library code under simulation.

If this is a problem, copy the lfsr57(), lfsr88(), & lfsr113() functions
into a host application and run nativly, viz test just the algorithm!

The expected usage is:
axe --args bin/dieharder.xe -g prng57 -n -1 -o - -B | dieharder -g 200 -a
(see dieharder.xe and dieharder help files for details)
Loading