---
# System prepended metadata

title: AT45DB161D(raspberry pi)
tags: [raspberry pi]

---

# AT45DB161D(raspberry pi)
硬體平台:raspberry zero
目的:讀取AT45DB161D的ID READ資料

## 接線圖
![](https://i.imgur.com/iyQPbSc.png)

CS-GPIO8
SI-GPIO10
SO-GPIO9
SCK-GPIO11
WP-GPIO4
RESET-GPIO17
VCC-3.3V
VSS-GND

## 腳本測試
使用以下腳本程式測試
```bash=
#!/bin/bash

cs=/sys/class/gpio/gpio8/value
clk=/sys/class/gpio/gpio11/value
miso="cat /sys/class/gpio/gpio9/value"
mosi=/sys/class/gpio/gpio10/value
wp=/sys/class/gpio/gpio4/value
reset=/sys/class/gpio/gpio17/value

op_code=(1 0 0 1 1 1 1 1)
array_rd=(1 1 0 0 0 0 0 0 0 0 0)
dat=(1 0 1 0 1 0 1 1 1 0 1 1 1 0 1 0)

function spi_init(){
echo 4 > /sys/class/gpio/export
echo 8 > /sys/class/gpio/export
echo 9 > /sys/class/gpio/export
echo 10 > /sys/class/gpio/export
echo 11 > /sys/class/gpio/export
echo 17 > /sys/class/gpio/export
sleep 0.5
echo in > /sys/class/gpio/gpio9/direction
echo out > /sys/class/gpio/gpio4/direction
echo out > /sys/class/gpio/gpio8/direction
echo out > /sys/class/gpio/gpio10/direction
echo out > /sys/class/gpio/gpio11/direction
echo out > /sys/class/gpio/gpio17/direction
echo 1 > $cs
echo 0 > $wp
echo 0 > $reset
echo 0 > $clk
echo 0 > $mosi
}

function spi_exit(){
echo 4 > /sys/class/gpio/unexport
echo 8 > /sys/class/gpio/unexport
echo 9 > /sys/class/gpio/unexport
echo 10 > /sys/class/gpio/unexport
echo 11 > /sys/class/gpio/unexport
echo 17 > /sys/class/gpio/unexport
}

function write(){
echo "write start"
echo 1 > $cs
for i in {0..10} 
do
  echo 1 > $clk
#  echo ${array[$i]}
  echo ${array[$i]} > $mosi
  sleep 0.000010
  echo 0 > $clk
  sleep 0.000010
done
for i in {0..15}
do
  echo 1 > $clk
#  echo ${dat[$i]}
  echo "${dat[$i]}" > $mosi
  sleep 0.000010
  echo 0 > $clk
  sleep 0.000010
done
echo 0 > $cs
sleep 0.000010
#while [ $($miso)==1 ]
#do
#echo busy
#done
}
function read_mem(){
echo "read start"
echo 0 > $cs
for i in {0..7} 
do
#  echo ${array[$i]}
  echo ${op_code[$i]} > $mosi
  echo 1 > $clk
  sleep 0.000010
  echo 0 > $clk
  sleep 0.000010
done
for i in {0..31}
do
  echo 1 > $clk
#  echo ${dat[$i]}
#  echo "${dat[$i]}" > $mosi
  echo $($miso)
  sleep 0.000010
  echo 0 > $clk
  sleep 0.000010
done
echo 1 > $cs
}

spi_exit
sleep 0.5
spi_init
echo 1 > $wp
echo 1 > $reset
read_mem
echo 0 > $wp
echo 0 > $reset
```
使用**ID READ**指令讀取包含IC ID的四個bytes。在用完上面的測試腳本後，第一個byte為0x1F，因此硬體讀取的部份成功運作

[datasheet](https://www.sparkfun.com/datasheets/IC/AT45DB161D.pdf)


###### tags: `raspberry pi`