概要
micropythonでshrike-lite、やってみた。
ラジオを鳴らせてみた。
ラジオ鳴らせる手順
準備する物
アンテナ用銅線
AMラジオ
top.vを書く。
(* top *) module top ((* iopad_external_pin, clkbuf_inhibit *) input clk,
(* iopad_external_pin *) output clk_en,
(* iopad_external_pin *) input rst_n,
(* iopad_external_pin *) input spi_ss_n,
(* iopad_external_pin *) input spi_sck,
(* iopad_external_pin *) input spi_mosi,
(* iopad_external_pin *) output spi_miso,
(* iopad_external_pin *) output spi_miso_en,
(* iopad_external_pin *) output reg led,
(* iopad_external_pin *) output led_en,
(* iopad_external_pin *) output reg antena,
(* iopad_external_pin *) output antena_en
);
localparam CARRIER_HALF_PERIOD = 6'd25;
localparam CNT_DOHI = 16'd23901; // C5 (1046 Hz)
localparam CNT_REHI = 16'd21295; // D5 (1174 Hz)
localparam CNT_MIHI = 16'd18968; // E5 (1318 Hz)
localparam CNT_FAHI = 16'd17908; // F5 (1396 Hz)
localparam CNT_SOHI = 16'd15964; // G5 (1568 Hz)
localparam CNT_LAHI = 16'd14205; // A5 (1760 Hz)
localparam CNT_OFF = 16'd0; // 消音
reg [15:0] tone_counter;
reg audio_wave;
reg [15:0] current_tone;
reg [3:0] tone_reg;
reg [5:0] carrier_counter;
reg carrier_1mhz;
reg [7:0] tx_data_reg;
wire [7:0] rx_data_wire;
wire rx_valid_pulse;
assign led_en = 1'b1;
assign antena_en = 1'b1;
assign clk_en = 1'b1;
spi_target #(.CPOL(1'b0), .CPHA(1'b0), .WIDTH(8), .LSB(1'b0))
u_spi_target (.i_clk(clk), .i_rst_n(rst_n), .i_enable(1'b1), .i_ss_n(spi_ss_n), .i_sck(spi_sck),
.i_mosi(spi_mosi), .o_miso(spi_miso), .o_miso_oe(spi_miso_en), .o_rx_data(rx_data_wire),
.o_rx_data_valid(rx_valid_pulse), .i_tx_data(tx_data_reg), .o_tx_data_hold());
always @(posedge clk or negedge rst_n)
begin
if (!rst_n)
begin
tone_reg <= 4'h0;
tx_data_reg <= 8'h55;
end
else if (rx_valid_pulse)
begin
tone_reg <= rx_data_wire[3:0];
tx_data_reg <= rx_data_wire;
end
end
always @(posedge clk or negedge rst_n)
begin
if (!rst_n)
begin
carrier_counter <= 6'd0;
carrier_1mhz <= 1'b0;
end
else
begin
if (carrier_counter >= (CARRIER_HALF_PERIOD - 1))
begin
carrier_counter <= 6'd0;
carrier_1mhz <= ~carrier_1mhz;
end
else
begin
carrier_counter <= carrier_counter + 1'b1;
end
end
end
always @(*)
begin
case (tone_reg)
4'd0:
current_tone = CNT_DOHI;
4'd1:
current_tone = CNT_REHI;
4'd2:
current_tone = CNT_MIHI;
4'd3:
current_tone = CNT_FAHI;
4'd4:
current_tone = CNT_SOHI;
4'd5:
current_tone = CNT_LAHI;
default:
current_tone = CNT_OFF;
endcase
end
always @(posedge clk or negedge rst_n)
begin
if (!rst_n)
begin
tone_counter <= 16'd0;
audio_wave <= 1'b0;
end
else if (current_tone == CNT_OFF)
begin
tone_counter <= 16'd0;
audio_wave <= 1'b0;
end
else
begin
if (tone_counter >= (current_tone - 1'b1))
begin
tone_counter <= 16'd0;
audio_wave <= ~audio_wave;
end
else
begin
tone_counter <= tone_counter + 1'b1;
end
end
end
always @(posedge clk or negedge rst_n)
begin
if (!rst_n)
begin
led <= 1'b0;
antena <= 1'b0;
end
else
begin
if (current_tone == CNT_OFF)
begin
led <= 1'b0;
antena <= 1'b0;
end
else if (audio_wave)
begin
led <= carrier_1mhz;
antena <= carrier_1mhz;
end
else
begin
led <= 1'b0;
antena <= 1'b0;
end
end
end
endmodule
bitstearmを作る。
rajio.binに、リネームする。
usbメモリーに、コピーする。
main.pyを書く。
from machine import Pin, SPI
import time
import shrike
shrike.reset()
shrike.flash("rajio.bin")
reset_pin = Pin(14, Pin.OUT, value = 1)
reset_pin.value(0)
time.sleep(1)
reset_pin.value(1)
time.sleep(1)
SCK = 2
CS = 1
MOSI = 3
MISO = 0
cs = Pin(CS, Pin.OUT, value = 1)
spi = SPI(0, baudrate = 1_000_000, polarity = 0, phase = 0, bits = 8, firstbit = SPI.MSB, sck = Pin(SCK), mosi = Pin(MOSI), miso = Pin(MISO))
def send(byte_to_send):
tx = bytes([byte_to_send])
rx = bytearray(1)
cs.value(0)
spi.write_readinto(tx, rx)
cs.value(1)
print(rx[0])
def main():
# 「365日の紙飛行機」サビ全体のデータ(0=ド, 1=レ, 2=ミ, 3=ファ, 4=ソ, 5=ラ, 13=消音)
music_data = [
# ♪ 人生は紙飛行機
2, 1, 0, 1, 2, 4, 2, 1, 0, 1, 1, 0, 13,
# ♪ 願い乗せて飛んでゆくよ
2, 1, 0, 1, 2, 4, 5, 4, 2, 1, 1, 0, 13, 13,
# ♪ 風が吹いていない時は
2, 1, 0, 1, 2, 4, 2, 1, 0, 1, 1, 0, 13,
# ♪ 誰もが歩くしかない(「誰もが」で少し低い音を再現)
0, 0, 1, 2, 4, 2, 1, 0, 1, 1, 13, 13,
# ♪ だから試してみるんだ
2, 1, 0, 1, 2, 4, 5, 4, 2, 1, 1, 0, 13,
# ♪ 羽を広げて
4, 5, 4, 2, 1, 0, 13,
# ♪ 飛んでゆけ(終わり)
1, 2, 1, 0, 0, 13
]
# 1音あたり0.3秒で再生
for d in music_data:
send(d)
time.sleep(0.3)
send(13)
main()
F7ピンに10cmくらいの銅線を付ける。送信アンテナとなる。
AMラジオを用意して、1MHzに合わせる。
usbを抜いて、入れる。
音が聞こえれば、成功です。
以上。