Simple first code for BX


#1

Greetings!

I have been fantasizing about getting into FPGAs for a couple years and once I found out about the TinyFPGA my fate was sealed. I purchased the BX and got it up and running.

I am attaching very simple verilog code I wrote that turns on the LED when voltage is applied at PIN_1. Also attached is my circuit diagram. I feel this board is a great intro board for FPGAs, but it is really hard for me to find noob level projects and examples. I’d love any pointers or links to good instructions.switch-led

// look in pins.pcf for all the pin names on the TinyFPGA BX board

module top (

input CLK,    // 16MHz clock

input PIN_1,  // First PIN on board

output LED,   // User/boot LED next to power LED

output USBPU  // USB pull-up resistor

);

// drive USB pull-up resistor to '0' to disable USB

assign USBPU = 0;

reg ledon; //register ledon

always @(posedge CLK) //whenever clock goes from 0 to 1

if (PIN_1) begin

  ledon <= 1;

end else begin

  ledon <= 0;

end

assign LED = ledon; //light up the led with state of register

endmodule


#2

Nice one! :). I’m sure most (if not all) people here have been through the “make LED turn on” phase of our learning journey :slight_smile: It’s always a thrill getting something to work in an “alien” environment :).

What sort of projects are you looking for? What is your general level of experience / background?

At the moment your LED is switched on based on one of the external inputs of the FPGA. If you want some ideas for things to try next, here are a few thoughts (I’m happy to help with hints if you get stuck):

  • maybe you could try to make your switch toggle the state of the LED rather than have the LED follow what the switch is doing. What sort of issues do you think you might come across here?
  • maybe you could try to make the LED turn itself on and off automatically based on the clock input. Pick a frequency (eg. 1.5Hz) and see if you can get the LED flashing at that frequency.
  • once you’ve got a flashing LED, you’re only a few steps away from making an LED that you can “dim” using pulse width (or pulse density) modulation. Rather than making the LED turn on and off in a binary fashion, see if you can make it appear to fade in and out smoothly.

Beyond that, the sky is the limit. I believe that it’s important to follow your passion when learning things. I enjoy audio/DSP stuff, so my natural inclination is to try to make things make noise. You probably find other stuff interesting, so chase whatever it is that makes you happy! :slight_smile:

Although I’m still a total n00b at electronics and FPGA’s, I feel like I’ve learnt quite a lot just through building random little projects and plodding away until they kind of work :slight_smile:

I wish you the best of luck! it’s a fun and rewarding hobby, and it’s a great feeling seeing custom-made hardware that you’ve dreamed up coming to life!

D.


#3

Hi,

I am quite new to the TinyFPGA scene but last year I did a similar project on a different FPGA board (and in VHDL v.s. Verilog). During that project I stared trying to make things work directly on the board but as soon as thing did not work I was in big trouble as to what was going wrong.

I used the following techniques that I think should also apply here

  • Get a paper book on verilog, and just read about the basics of logic cells and creating a counter or adder
  • Use a simulator. This way you can learn about verilog and understand what concepts are different without having doubts about the hardware changes you made
  • Pick a protocol (UART for example) to learn the basics and connect this to your pc. the icestick has a nice example to toggling LEDS pins from the UART.

My project currently is doing pwm on 20 leds. I like it because it is well suited for doing it on the FPGA.


#4

Hi,

In the most simple form it should be possible to do the following
assign LED = PIN_1;

The code you have is inside a block and in that block programming pretty much looks like procedural programing, the code gets evaluated on the rising edge of the clock and all changes that happened inside this block will be “released” at once but for you example … you do not need this.


#5

This one also looks very nice:

http://dangerousprototypes.com/docs/CPLD_Verilog_intro_2:_Toggle_a_LED_with_a_button


#6

Thanks gundy, I really appreciate the encouragement. My interests and experience are mostly with robotics and controlling RC servos, although I also find audio quite interesting as well as analog video. I have done a little programming on the Pololu Maestro board, and built servo controllers with 555 timers, nor gates, and decade counters/dividers. I would love to build a hexapod robot using the neural network developed by Joseph Ayers.

I think pulsing an LED from dim to bright would be a great next project. For servos, I need to get 1-2ms pulses every 20ms. If I were an expert, I’d probably try getting my Maestro 24channel board communicating directly with the FPGA, but it is the parallel element to fpgas which I think is just so cool!


#7

Hi Keesj,

Great tips thank you! I will check out those resources. Also, your project sounds fun. I want to control 20 or so servos which use their own flavor of PWM. I am glad to hear larger numbers of I/O are possible.


#8

Hello @organicelectrics,

Controlling 20 servos should really be a breeze (in terms of generating the required signals) the challenge is probably how to send the required positions to the FPGA (and if you are really fancy play acceleration and such!). Implementing a UART at first and controlling the servo position sound like a good plan to me.

In my previous project I used a soft cpu core for this (but as mentioned , different language, different fpga board)

This is my first TinyFPGA project (the pwm ) and the code … is still very ugly but works :stuck_out_tongue: (https://pastebin.com/7snhE8cN )


#9

Great! You’re definitely not a n00b then! :). FPGA’s are great for things where tight timing is required, and controlling lots of servos at once is a good example.

As a “stretch target”, maybe you could consider creating a multiple servo controller peripheral for PicoSoC (an open-source system-on-chip design that includes a RISC-V microcontroller and a UART, and can be extended with other peripherals)… That way you could build your hexapod robot controller entirely in the BX without any need for the maestro or external microcontroller :).

It might sound a little daunting, but PicoSoC is quite well put together, and I’m sure it will be a very rewarding project! :slight_smile:

D.


#10

@keesj Nice project! I like the pcb design. I also recognize part of the SOS code from the examples.

@gundy is also recommending UART so clearly I need to read up on that. I saw a panel discussion about Western Digital switching over to RISC-V so that seems pretty hot right now. I am anticipating needing analog to digital converters down the road, so communication with microcontrollers will be important.

I just got through part of the music box tutorial on fpga4fun.com. I feel more comfortable about bits and counting now. I plan to keep working through these.

Thank you both for the tips!!