0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

micropythonでshrike-lite その44

0
Last updated at Posted at 2026-10-01

概要

micropythonでshrike-lite、やってみた。
usb-serialで、fpgaのuart_txを読む。

通信仕様

BaudRate : 115200
Parity : None
DataBits : 8
StopBits : One
Handshake : None
電圧レベル:3.3v

手順

準備するもの

ftd1232(usb-serialトングル)

verilogを書く。

top.v

(* top *) module top #(parameter CLK = 50_000_000, parameter BAUD_RATE = 115200) (
 (* iopad_external_pin, clkbuf_inhibit *) input clk,
 (* iopad_external_pin *) output clk_en,
 (* iopad_external_pin *) input rst,
 (* iopad_external_pin *) input rx,
 (* iopad_external_pin *) output tx,
 (* iopad_external_pin *) output tx_en,
 (* iopad_external_pin *) output led,
 (* iopad_external_pin *) output led_en
);
	localparam IDLE	 = 2'b00,
			 SEND_WAIT = 2'b01,
			 NEXT_CHAR = 2'b10;
	localparam CLK_FREQ = 50_000_000;
	localparam CNT_500MS = CLK_FREQ / 2 - 1;
	reg [31:0] timer_cnt;
	reg timer_done;
	reg [3:0] char_idx;
	reg [7:0] tx_data;
	reg tx_start;
	reg tx_busy_d1;
	reg [1:0] state;
	wire tx_busy;
	wire tx_done = (tx_busy_d1 == 1'b1) && (tx_busy == 1'b0);
	assign clk_en = 1'b1;
	assign tx_en = 1'b1;
	assign led_en = 1'b1;
	uart_tx #(.CLK_FREQ(CLK_FREQ), .BAUD_RATE(115200)) 
		ux (.clk(clk), .rst(rst), .tx_start(tx_start), .tx_data(tx_data), .tx(tx), .tx_busy(tx_busy));
	initial
	begin
		$monitor("state: %d start: %d done: %h", state, tx_start, timer_done);
	end
	always @(*)
	begin
		case (char_idx)
			4'd0: tx_data = "H"; // 8'h48
			4'd1: tx_data = "e"; // 8'h65
			4'd2: tx_data = "l"; // 8'h6C
			4'd3: tx_data = "l"; // 8'h6C
			4'd4: tx_data = "o"; // 8'h6F
			4'd5: tx_data = " "; // 8'h20
			4'd6: tx_data = "W"; // 8'h57
			4'd7: tx_data = "o"; // 8'h6F
			4'd8: tx_data = "r"; // 8'h72
			4'd9: tx_data = "l"; // 8'h6C
			4'd10: tx_data = "d"; // 8'h64
			4'd11: tx_data = 8'h0D;
			4'd12: tx_data = 8'h0A;
			default: tx_data = 8'h00;
		endcase
	end
	always @(posedge clk or posedge rst)
	begin
		if (rst)
		begin
			timer_cnt <= 32'd0;
			timer_done <= 1'b0;
		end
		else
		begin
			if (timer_cnt >= CNT_500MS)
			begin
				timer_cnt <= 32'd0;
				timer_done <= 1'b1;
			end
			else
			begin
				timer_cnt <= timer_cnt + 1'b1;
				timer_done <= 1'b0;
			end
		end
	end
	always @(posedge clk or posedge rst)
	begin
		if (rst)
			tx_busy_d1 <= 1'b0;
		else
			tx_busy_d1 <= tx_busy;
	end
	always @(posedge clk or posedge rst)
	begin
		if (rst)
		begin
			char_idx <= 4'd0;
			tx_start <= 1'b0;
			state <= IDLE;
		end
		else
		begin
			case (state)
			IDLE:
				begin
					tx_start <= 1'b0;
					char_idx <= 4'd0;
					if (timer_done)
					begin
						tx_start <= 1'b1;
						state <= SEND_WAIT;
					end
				end
			SEND_WAIT:
				begin
					tx_start <= 1'b0;
					if (tx_done)
					begin
						if (char_idx == 4'd12)
						begin
							state <= IDLE;
						end
						else
						begin
							char_idx <= char_idx + 1'b1;
							state <= NEXT_CHAR;
						end
					end
				end
			NEXT_CHAR:
				begin
					tx_start <= 1'b1;
					state <= SEND_WAIT;
				end
			default:
				state <= IDLE;
			endcase
		end
	end
endmodule




uart_tx.v


module uart_tx #(
	parameter CLK_FREQ = 50_000_000,
	parameter BAUD_RATE = 115200,
	parameter BIT_CNT_MAX = CLK_FREQ / BAUD_RATE - 1
)(
	input wire clk,
	input wire rst,
	input wire tx_start,
	input wire [7:0] tx_data,
	output reg tx,
	output reg tx_busy
);
	localparam IDLE = 2'b00,
			 START = 2'b01,
			 DATA = 2'b10,
			 STOP = 2'b11;
	reg [15:0] baud_cnt;
	reg [3:0] bit_idx;
	reg [7:0] tx_shifter;
	reg [1:0] state;
	always @(posedge clk or posedge rst)
	begin
		if (rst)
		begin
			tx <= 1'b1;
			tx_busy <= 1'b0;
			baud_cnt <= 16'd0;
			bit_idx <= 4'd0;
			tx_shifter <= 8'd0;
			state <= IDLE;
		end
		else
		begin
			case (state)
			IDLE:
			begin
				tx <= 1'b1;
				tx_busy <= 1'b0;
				if (tx_start)
				begin
					tx_shifter <= tx_data;
					tx_busy <= 1'b1;
					baud_cnt <= 16'd0;
					state <= START;
				end	
			end
			START:
			begin
				tx <= 1'b0;
				if (baud_cnt >= BIT_CNT_MAX)
				begin
					baud_cnt <= 16'd0;
					bit_idx <= 4'd0;
					state <= DATA;
				end
				else
				begin
					baud_cnt <= baud_cnt + 1'b1;
				end
			end
			DATA:
			begin
				tx <= tx_shifter[bit_idx];
				if (baud_cnt >= BIT_CNT_MAX)
				begin
					baud_cnt <= 16'd0;
					if (bit_idx >= 4'd7)
					begin
						state <= STOP;
					end
					else
					begin
						bit_idx <= bit_idx + 1'b1;
					end
				end
				else
				begin
					baud_cnt <= baud_cnt + 1'b1;
				end
			end
			STOP:
			begin
				tx <= 1'b1;
				if (baud_cnt >= BIT_CNT_MAX)
				begin
					state <= IDLE;
				end
				else
				begin
					baud_cnt <= baud_cnt + 1'b1;
				end
			end
			endcase
		end
	end
endmodule




i/o plannerでtxをGPIO7にする。

bitstreamを作る。hello.bin

main.pyを書く。


from machine import UART, Pin
import time
import shrike

shrike.reset()
shrike.flash("hello.bin")

reset_pin = Pin(2, Pin.OUT, value = 0)

uart = UART(0, baudrate = 115200, tx = Pin(0), rx = Pin(1))

print("port open")
print("reset")
reset_pin.value(1)
time.sleep(0.5)
print("release reset & boot")
reset_pin.value(0)


結線

usb-serialのrxと基板のF7を繋ぐ。

c#でTERMを書く。



using System;
using System.IO.Ports;
using System.Timers;

class Pico {
	private static SerialPort port;
	private static Timer sendTimer;
	private static readonly object lockObject = new object();
	public static void Main(string[] args) {
		port = new SerialPort();
		port.PortName = "COM7";
		port.BaudRate = 115200;
		port.DataBits = 8;
		port.Parity = Parity.None;
		port.StopBits = StopBits.One;
		port.Handshake = Handshake.None;
		port.DtrEnable = true;
		port.RtsEnable = true;
		port.DataReceived += OnDataReceived;
		try
		{
			port.Open();
			Console.WriteLine("Port Open: ok");
		}
		catch (Exception ex)
		{
			Console.WriteLine("Fail: Port Open error!! ({ex.Message})");
			return;
		}
		sendTimer = new Timer(500);
		sendTimer.Elapsed += OnSendEvent;
		sendTimer.AutoReset = true;
		sendTimer.Start();
		Console.WriteLine("「UUUUUU」を500msごとに送信中... 何かキーを押すと停止します。");
		Console.ReadKey(true);
		sendTimer.Stop();
		sendTimer.Dispose();
		if (port != null)
		{
			port.DataReceived -= OnDataReceived;
			if (port.IsOpen)
				port.Close();
			port.Dispose();
		}
		Console.WriteLine("\n送信を停止し、ポートを閉じました。");
	}
	private static void OnSendEvent(Object source, ElapsedEventArgs e) {
		// System.Timers.Timerはマルチスレッドで動くため、
		// 前回の送信処理が100ms以内に終わらなかった場合の衝突を防ぐ
		lock (lockObject)
		{
			try
			{
				if (port != null && port.IsOpen)
				{
					port.Write("UUUUUU");
				}
			}
			catch (Exception ex)
			{
				sendTimer.Stop();
				Console.WriteLine("\n送信エラーが発生したため停止しました: {ex.Message}");
			}
		}
	}
	private static void OnDataReceived(object sender, SerialDataReceivedEventArgs e) {
		try
		{
			if (port != null && port.IsOpen)
			{
				//string data = port.ReadExisting();
				//Console.Write(">");
				char data = (char) port.ReadByte();
				Console.Write(data);
			}
		}
		catch (Exception ex)
		{
			Console.WriteLine("\n受信エラー: {ex.Message}");
		}
	}
}




実行結果

Hello Worldが、読めれば、成功です。


>term9
Port Open: ok
「UUUUUU」を500msごとに送信中... 何かキーを押すと停止します。
Hello World
Hello World
Hello World
Hello World
Hello World
He

以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?