# [20190519 - Raspberry Pi](https://hackmd.io/s/SkgLJCCnV)
###### tags: `tcfst`
## Author
- Yuan < e61983@gmail.com >
- Wei < demon0303loki@gmail.com >
- Jia-Hao,Hsu < jhs.8901.3737@gmail.com >
## License

## Interrupt Ccontrol
### Soft IRQ
參考 https://hackmd.io/s/S1WKTCFM4
### GPIO - gpiolib Version
gpiolib 約在 Kernel 2.6.21 引入, 不過到了 Kernel 4.18.7 又更新了 API ,雖然兩者的用法十分的相似,但課堂中所使用的 API 已經[不建議使用][gpiolib: document new interface]。
#### Header File
```c
/* GPIO */
#include <linux/gpio.h>
/* interrupt */
#include <linux/interrupt.h>
```
#### Key Functions
```c=
/* GPIO */
gpio_request();
gpio_direction_output();
gpio_direction_input();
gpio_set_debounce();
gpio_to_irq();
gpio_free();
/* interrupt */
request_threaded_irq();
request_irq();
free_irq();
```
### poll
參考 https://gitlab.com/e61983/linux_driver 08.poll
#### Header File
```c
#include <linux/poll.h>
```
#### Key Function
```c
__poll_t xx_poll(struct file *filp, struct poll_table_struct wait);
void poll_wait(struct file * filp, wait_queue_head_t * wait_address, poll_table *p)
```
#### Code
```c
// 參考 07.waitqueue
wait_event_interruptible(button_waitq,ev_press);
```
#### Application
select, poll, epoll 都是在 userspace 使用系統呼叫進行輪詢的介面。
它們都可以同時監看多個 fd 。詳情請問`人`。
```shell
man 2 select
man 2 poll
man 2 epoll
```
### Reference
- [BCM2835 Datasheet][BCM2835 Datasheet]
- [BCM2836-QA][BCM2836-QA]
- [Cortex™-A7 MPCore™ Technical Reference Manual][Cortex™-A7 MPCore™ Technical Reference Manual]
## SPI
- MOSI - master out slave in
- MISO - master in slave out
- CLK - sync clock
- SS - slave select
### 74595 seg module
- [74595 DataSheet ][74595]
- [Buy form here][我要買seg_module]

## I2C
I2C subsystem 在 Linux Kernel 2.6.x 出現並持續的演進。
### I2C Framework
```txt
+-----------------+
| |
| Program |
| |
+---------+-------+
User Space |
--------------------------------------------+-----------------------------------------
|
+------------+ +------+-----+ +-------------+
| | | | | |
| Driver +-------->+ Client +<---------+ I2C-Dev |
| | | | | |
+--------+---+ +-------+----+ +-------+-----+
| | |
| | |
+---+---------------------+-----------------------+--+
| |
| I2C-Core |
| |
+------------+------------------------+--------------+
| |
| |
+-------+-------+ +--------+----+
| | | |
| Algorlthm +------>+ Adapter |
| | | |
+-----------+---+ +----+--------+
| |
| |
+-----+----------------+-------+
| |
| Adapter Specific Code |
| |
+---------------+--------------+
Kernel Space |
----------------------------------------------+---------------------------------------
Hardware |
+-----------+----------+
| |
| Adapter Hardware |
| |
+-----------+----------+
|
|
+-----------------------+---------------------+
| | |
| | |
+--------+-------+ +--------+-------+ +-------+--------+
| | | | | |
| I2C-Device | | I2C-Device | | I2C-Device |
| | | | | |
+----------------+ +----------------+ +----------------+
```
上圖主要可以分成 3 個部分:
1. User Space
2. Kernel Space
3. Hardware
- Utils
```shell=
i2cdetect -y 1
```
### I2C LCD Module
<img style="width:50%; height:auto;" src="https://i.imgur.com/LGE7HjD.jpg" >
- [Buy form here][我要買lcd_module]
## LAMP
LAMP 是 Linux, Apache, MySQL, PHP 各項服務的縮寫
### Install Apache
```shell=
sudo apt install -y apache2
# www dir @ /var/www.html
# If fire wall is enable
sudo ufw allow 'Apache'
# Show service status
sudo systemctl status apache2
```
### Install PHP
```shell
sudo apt install -y php7.0
```
### Install MySQL
```shell=
sudo apt install mysql-server
sudo mysql_secure_installation
# Show service status
sudo systemctl status mysql.service
```
### PHP Hello world
```htmlembedded=
<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php echo "<p>Hello World</p>"; ?>
</body>
</html>
```
### MySQL
- [Try it][trysql]
建立一個新帳號後,你要授與資料庫使用權限給這位使用者,這帳號才能開始連線進去資料庫操作。
```sql=
-- 建立新帳號
CREATE USER 'username'@'hostname' IDENTIFIED BY '密碼';
-- 賦與權限
GRANT type_of_permission ON database_name.table_name TO 'username'@'hostname';
```
SQL 語法範例:
```sql=
-- 建立新的資料表 "表格名"
CREATE TABLE "表格名" ("欄位 1" "欄位 1 資料種類", "欄位 2" "欄位 2 資料種類", ... );
-- 顯示 "表格名" 的所有欄位
SELECT * FROM "表格名";
-- 顯示在 "表格名" 中滿足 "條件" 的 "欄位名" 欄位
SELECT "欄位名" FROM "表格名" WHERE "條件";
-- 插入 "欄位1" "值1" 到 "表格名" 中
INSERT INTO "表格名" ("欄位1", "欄位2", ...) VALUES ("值1", "值2", ...);
-- 更新 "表格名" 中滿足 "條件" 的值
UPDATE "表格名" SET "欄位1" = [新值] WHERE "條件";
-- 刪除 "表格名" 中滿足 "條件" 的資料。
DELETE FROM "表格名" WHERE "條件";
-- 清除 "表格名"
DROP TABLE "表格名";
```
#### Reference
- [grant-privileges][grant-privileges]
#### Ohters
- phpmyadmin
### Https
- Generate Certificate
```shell=
sudo openssl req -x509 ... ...
```
- Enable ssl module
```shell=
sudo a2enmod ssl
sudo ln -s /etc/apache2/sites-available/default-ssl \
/etc/apache2/sites-enabled/000-default-ssl.conf
```
- Restart Apache
```shell=
sudo systemctl restart mysql.service
```
#### Others
- [Letsencrypt][Letsencrypt]
#### Reference
- [Authentication, Authorization and Access Control][Authentication, Authorization and Access Control]
---
## 梗區
> ~~symbol error 可以自幹符號來騙complier~~
> <img style="width:20%; height:auto;" src="https://i.imgur.com/9dEWqZ8.png" >
> [name= 李老師]
> <img style="width:20%; height:auto;" src="https://i.imgur.com/8T9P3a3.jpg" >
>
> 我以前一直以為這是電路, 原來這是clk
> (( 因為士郎是 Master
> [name= Wei]
> > ~~給clk的就是master~~
> > [name= 李老師]
[BCM2835 Datasheet]: https://www.raspberrypi.org/app/uploads/2012/02/BCM2835-ARM-Peripherals.pdf
[BCM2836-QA]:
https://www.raspberrypi.org/documentation/hardware/raspberrypi/bcm2836/QA7_rev3.4.pdf
[Cortex™-A7 MPCore™ Technical Reference Manual]: http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ddi0464f/index.html
[gpiolib: document new interface]:
https://lwn.net/Articles/574055/
[74595]:
https://www.ti.com/lit/ds/symlink/cd74hc595.pdf
[我要買seg_module]:
https://www.banggood.com/4-Bits-Digital-Tube-LED-Display-Module-Board-For-Arduino-p-931236.html?cur_warehouse=CN
[我要買lcd_module]:
https://www.banggood.com/Geekcreit-IICI2C-1602-Yellow-Green-Backlight-LCD-Display-Module-For-Arduino-p-950728.html?rmmds=detail-bottom-alsobought__3&cur_warehouse=CN
[trysql]:
https://www.w3schools.com/sql/trysql.asp?filename=trysql_asc
[Authentication, Authorization and Access Control]:
https://www.who.int/manual/howto/auth.html
[grant-privileges]:
https://www.fooish.com/sql/grant-privileges.html
[Letsencrypt]:
https://youtu.be/YaWnQz1vIFM