Commit d0bf252f authored by Alan Wood's avatar Alan Wood

initial SDK files

parent 5fe9fa5e
Instructions mystorm SDK
========================
```
dmesg
lsusb
dfu-util -l
platformio run
platformio run --target clean
platformio run -e nucleo_l476rg -t upload
stm32flash -b 1500000 -w .pioenvs/nucleo_l476rg/firmware.bin -v -g 0x0 /dev/ttyUSB0
sudo dfu-util -a 0 -d 0483:df11 -s 0x8000000:leave -D .pioenvs/nucleo_l476rg/firmware.bin
```
\ No newline at end of file
File added
File added
cp .pioenvs/icestorm/hardware.bin bitmap.bin
~/.platformio/packages/toolchain-gccarmnoneeabi/arm-none-eabi/bin/objcopy -I binary -O elf32-littlearm -B arm --rename-section .data=.rodata bitmap.bin bitmap.o
; PlatformIO Project Configuration File
;
; Build options: build flags, source filter, extra scripting
; Upload options: custom port, speed and extra flags
; Library options: dependencies, extra library storages
;
; Please visit documentation for the other options and examples
; http://docs.platformio.org/en/stable/projectconf.html
[env:icestorm]
platform = lattice_ice40
framework = icestorm
board = mystorm
# ST STM32 Platform
[env:nucleo_l476rg]
platform = ststm32
framework = mbed
board = nucleo_l476rg
upload_protocol = stlink
#build_flags = -Wl,--no-warn-mismatch,bitmap.o
build_flags = -Wl,--no-warn-mismatch,bitmap.o
#extra_script = ld_bitmap.py
module pwm #(parameter CTR_LEN = 8) (
input clk,
input rst,
input [CTR_LEN - 1 : 0] compare,
output pwm
);
reg pwm_d, pwm_q;
reg [CTR_LEN - 1: 0] ctr_d, ctr_q;
assign pwm = pwm_q;
always @(*) begin
ctr_d = ctr_q + 1'b1;
if (compare > ctr_q)
pwm_d = 1'b1;
else
pwm_d = 1'b0;
end
always @(posedge clk) begin
if (rst) begin
ctr_q <= 1'b0;
end else begin
ctr_q <= ctr_d;
end
pwm_q <= pwm_d;
end
endmodule
module shiftreg #(parameter ADDR_LEN = 8, parameter DATA_LEN = 8 ) (
input sck,
input si,
input cs,
output [ADDR_LEN - 1:0] addr,
output [DATA_LEN - 1:0] data
);
reg [ADDR_LEN + DATA_LEN - 1:0] sr;
always @(posedge sck) begin
if(~cs) begin
sr = sr >> 1;
sr[ADDR_LEN + DATA_LEN - 1] = si;
end
end
assign {data,addr} = sr;
// always @(posedge cs) begin
// {data,addr} = sr;
// end
endmodule
/******************************************************************************
* Copyright [2016] [Alan Wood] *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
* You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, software *
* distributed under the License is distributed on an "AS IS" BASIS, *
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
* See the License for the specific language governing permissions and *
* limitations under the License. *
******************************************************************************/
#include "mystorm.h"
// // Digital pins for controlling and serial programming of Ice40
// DigitalIn done(PB_7);
// DigitalOut* rst = new DigitalOut(PB_6);
// DigitalOut* cs = new DigitalOut(PD_2);
// DigitalOut* mosi = new DigitalOut(PB_5);
// DigitalOut* sclk = new DigitalOut(PB_3);
// // Not used, surplus to requirement?
// DigitalIn* miso = new DigitalIn(PB_4);
// // Status indicator led
// DigitalOut *myled = new DigitalOut(PA_15);
MyStorm ms(PB_5, PB_4, PB_3, PB_7, PB_6, PD_2,PA_15);
// Start and end of memory section containing bit image, linked at build time
extern uint8_t _binary_bitmap_bin_start;
extern uint8_t _binary_bitmap_bin_end;
int main() {
SystemCoreClockUpdate();
//Lets get the bit image size
uint32_t len = &_binary_bitmap_bin_end - &_binary_bitmap_bin_start;
//Initialise anfd program Ice40
ms.ice_prog(&_binary_bitmap_bin_start, len);
wait_ms(10);
//Lets light up some leds!
// Light em up one by one
for(uint8_t i = 0 ; i < 4; i++) {
ms.ice_reg_write(i, 255);
wait(1);
}
// Lets sweep them
while(1) {
for(uint8_t i = 0 ; i < 4; i++) {
ms.ice_reg_write(i,(4 << (i+1)) );
wait_ms(100);
}
//ms.toggle();
for(uint8_t i = 4 ; i > 0 ; i--) {
ms.ice_reg_write(i-1,(4 << (5-i )) );
wait_ms(100);
}
}
}
\ No newline at end of file
/******************************************************************************
* Copyright [2016] [Alan Wood] *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
* You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, software *
* distributed under the License is distributed on an "AS IS" BASIS, *
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
* See the License for the specific language governing permissions and *
* limitations under the License. *
******************************************************************************/
#include "mystorm.h"
MyStorm::MyStorm(PinName mosi_pin, PinName miso_pin, PinName sclk_pin,
PinName done_pin, PinName rst_pin, PinName cs_pin,
PinName ld_pin){
mosi = new DigitalOut(mosi_pin);
miso = new DigitalIn(miso_pin);
sclk = new DigitalOut(sclk_pin);
done = new DigitalIn(done_pin);
rst = new DigitalOut(rst_pin);
cs = new DigitalOut(cs_pin);
ld = new DigitalOut(ld_pin);
ldState = 0;
}
MyStorm::~MyStorm(){
delete mosi;
delete miso;
delete sclk;
delete done;
delete rst;
delete cs;
delete ld;
}
/**
* @brief Set led state on (1) or off (0)
* @details Set led state on (1) or off (0)
*
* @param state to set led
*/
void MyStorm::led(int state){
ldState = state;
ld->write(ldState);
}
/**
* @brief toggle led state
* @details toggle led state
* @return led state
*/
int MyStorm::toggle(){
ldState = (ldState) ? 0 : 1;
ld->write(ldState);
return ldState;
}
/**
* @brief FPGA spi chip select
* @details FPGA spi chip select
*/
void MyStorm::select(){
cs->write(0);
}
/**
* @brief FPGA spi chip deselect
* @details FPGA spi chip deselect]
*/
void MyStorm::deselect(){
cs->write(1);
}
/**
* @brief Byte serializer
* @details Byte serializer - serialises the byte passed in over SPI
*
* @param data The byte to write to the serial pin
*/
void MyStorm::ice_write(uint8_t data){
for (int bit = 7; bit >= 0; --bit){
sclk->write(0);
mosi->write(((data >> bit) & 0x01) != 0);
sclk->write(1);
}
}
/**
* @brief Writes to one of 8 interal 8 bit registers
* @details Writes to one of 8 interal 8 bit registers
*
* @param addr register address 1 of 255
* @param data register value 0-255
*/
void MyStorm::ice_reg_write(uint8_t addr, uint8_t data) {
select();
for (int bit = 0; bit < 8; bit++){
sclk->write(0);
mosi->write(((addr >> bit) & 0x01) != 0);
sclk->write(1);
}
for (int bit = 0; bit < 8; bit++){
sclk->write(0);
mosi->write(((data >> bit) & 0x01) != 0);
sclk->write(1);
}
deselect();
}
/**
* @brief Initialise Ice40
* @details Initialise Ice40; reset, place into slave mode and upload bitimage
*
* @param ibuf Pointer to begining of buffer bit image
* @param cnt Number of bytes in bit image
*/
void MyStorm::ice_prog(uint8_t *ibuf, int cnt) {
rst->write(1);
// reset ice40
rst->write(0);
// Place into slave mode
cs->write(0);
wait_ms(1);
rst->write(1);
// Wait for Ice40 to pull done pin low for ack, then wait 2 milliseconds
while(done->read() == 1);
wait_ms(2);
// Programming Ice40 as an SPI slave, transfer bitimage
ld->write(ldState);
for (int i = 0; i < cnt; i++){
// Toggle led every 2^12 bytes to show programming in process
if((i % (1 << 12)) == 0)
toggle();
// Write byte
ice_write(*ibuf++);
}
// Lets make sure ICE40 received bitstream, wait for done to drop
while(done->read() == 0)
ice_write(ANY);
/* Send the ICE40 at least 49 clocks using dummy data */
// for(int i = 0; i < 7;i++) {
// ice_write(ANY);
// }
// Release CS pin now we are done
cs->write(1);
// We have finished, lets indicate that
ld->write(1);
}
\ No newline at end of file
/******************************************************************************
* Copyright [2016] [Alan Wood] *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
* You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, software *
* distributed under the License is distributed on an "AS IS" BASIS, *
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
* See the License for the specific language governing permissions and *
* limitations under the License. *
******************************************************************************/
#ifndef MYSTORM_H
#define MYSTORM_H
#include "mbed.h"
#define ANY 0xFF
class MyStorm {
private:
DigitalOut* mosi;
DigitalIn* miso;
DigitalOut* sclk;
DigitalOut* cs;
DigitalIn* done;
DigitalOut* rst;
DigitalOut* ld;
int ldState;
public:
/**
* @brief MyStorm Class for managing the FPGA and built in features
* @details MyStorm Class for managing the FPGA and built in features
*
* @param mosi_pin serial out to FPGA
* @param miso_pin serial in from FPGA
* @param sclk_pin serial clock pin
* @param done_pin FPGA status pin
* @param rst_pin FPGA reset pin
* @param cs_pin FPGA serial chip select
* @param ld_pin Mystorm status/user led
*/
MyStorm(PinName mosi_pin, PinName miso_pin, PinName sclk_pin,
PinName done_pin, PinName rst_pin, PinName cs_pin,
PinName ld_pin);
~MyStorm();
/**
* @brief Set led state on (1) or off (0)
* @details Set led state on (1) or off (0)
*
* @param state to set led
*/
void led(int state);
/**
* @brief toggle led state
* @details toggle led state
* @return led state
*/
int toggle();
/**
* @brief FPGA spi chip select
* @details FPGA spi chip select
*/
void select();
/**
* @brief FPGA spi chip deselect
* @details FPGA spi chip deselect]
*/
void deselect();
/**
* @brief Byte serializer
* @details Byte serializer - serialises the byte passed in over SPI
*
* @param data The byte to write to the serial pin
*/
void ice_write(uint8_t data);
/**
* @brief Writes to one of 8 interal 8 bit registers
* @details Writes to one of 8 interal 8 bit registers
*
* @param addr register address 1 of 255
* @param data register value 0-255
*/
void ice_reg_write(uint8_t addr,uint8_t data);
/**
* @brief Initialise Ice40
* @details Initialise Ice40; reset, place into slave mode and upload bitimage
*
* @param ibuf Pointer to begining of buffer bit image
* @param cnt Number of bytes in bit image
*/
void ice_prog(uint8_t *ibuf, int cnt);
};
#endif
###############################################################################
# #
# Copyright 2016 myStorm Copyright and related #
# rights are licensed under the Solderpad Hardware License, Version 0.51 #
# (the “License”); you may not use this file except in compliance with #
# the License. You may obtain a copy of the License at #
# http://solderpad.org/licenses/SHL-0.51. Unless required by applicable #
# law or agreed to in writing, software, hardware and materials #
# distributed under this License is distributed on an “AS IS” BASIS, #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or #
# implied. See the License for the specific language governing #
# permissions and limitations under the License. #
# #
###############################################################################
# User Constraint File for myStorm
#myStorm SPI link ARM to ICE40
set_io miso 67
set_io mosi 68
set_io sck 70
set_io cs 71
#myStorm leds
#set_io LED[0] 37
#set_io LED[1] 38
#set_io LED[2] 39
#set_io LED[3] 41
#pmod 1
set_io PMOD[0] 101
set_io PMOD[1] 102
set_io PMOD[2] 104
set_io PMOD[3] 105
#pmod 2
set_io PMOD[4] 98
set_io PMOD[5] 99
set_io PMOD[6] 97
set_io PMOD[7] 96
#pmod 3
set_io PMOD[8] 106
set_io PMOD[9] 107
set_io PMOD[10] 110
set_io PMOD[11] 112
#pmod 4
set_io PMOD[12] 113
set_io PMOD[13] 114
set_io PMOD[14] 143
set_io PMOD[15] 144
#pmod 5
set_io PMOD[16] 10
set_io PMOD[17] 9
set_io PMOD[18] 2
set_io PMOD[19] 1
#pmod 6
set_io PMOD[20] 8
set_io PMOD[21] 7
set_io PMOD[22] 4
set_io PMOD[23] 3
#pmod 7
set_io PMOD[24] 20
set_io PMOD[25] 19
set_io PMOD[26] 16
set_io PMOD[27] 15
#pmod 8
set_io PMOD[28] 18
set_io PMOD[29] 17
set_io PMOD[30] 12
set_io PMOD[31] 11
#pmod 9
set_io PMOD[32] 34
set_io PMOD[33] 33
set_io PMOD[34] 22
set_io PMOD[35] 21
#pmod 10
set_io PMOD[36] 32
set_io PMOD[37] 31
set_io PMOD[38] 26
set_io PMOD[39] 25
#pmod 11
set_io PMOD[40] 23
set_io PMOD[41] 24
set_io PMOD[42] 28
set_io PMOD[43] 29
#pmod 12
set_io PMOD[44] 37
set_io PMOD[45] 38
set_io PMOD[46] 39
set_io PMOD[47] 41
#pmod 13
set_io PMOD[48] 80
set_io PMOD[49] 79
set_io PMOD[50] 64
set_io PMOD[51] 63
# Onboard 12Mhz oscillator
set_io clk 129
module SPI_PWM(
// 50MHz clock input
input clk,
input sck,
input mosi,
output miso,
input cs,
// Outputs to the 8 onboard LEDs
output[51:0]PMOD,
//output mypwm
);
localparam p = 4;
wire rst = 1'b0;
wire [7:0] pwmr[p];
wire [7:0] addr, val;
shiftreg mysr (
//100Mhz clock
.sck(sck),
//.rst(rst),
// .blink(LED[0])
.cs(cs),
.si(mosi),
.addr(addr),
.data(val)
);
genvar i;
generate
for(i = 0; i < p; i = i+1) begin
pwm pwm (
.rst(rst),
.clk(clk),
.compare(pwmr[i]),
.pwm(PMOD[44+i])
);
end
endgenerate
always @(posedge cs)
case(addr[1:0])
3'b00: pwmr[0] = val;
3'b01: pwmr[1] = val;
3'b10: pwmr[2] = val;
3'b11: pwmr[3] = val;
endcase
endmodule
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment