# Week1 Report - MIMXRT1060_EVK Evaluation Kit
## Introdution
Board:**MIMXRT1060-EVK**
Processor:**MIMXRT1062DVL6A**
* Block diagram

* Board front side

* Board back side

## Quick Start Tutorial
### Plug In
* 配置Boot Mode,並搭配實體switch指撥開關


* 接上USB Cable(micro type)

* 上電並確認Board狀態
- 將會利用SDK裡面的demo blink_led程式
- 當board power on,綠色LED將會持續閃滅
- 執行該步驟前需先完成"下載SDK" "執行Build & Debug"

* [Getting Started - Plug it in](https://www.nxp.com/document/guide/getting-started-with-the-mimxrt1060-evk:GS-MIMXRT1060-EVK?section=plug-it-in)
### Get Software
下載MIMXRT1060-EVK **SDK Builder**包、應用build code軟體**MCUXpresso IDE**,以及**serial port driver**
* Download & Install
- SDK filename: `SDK_2_14_0_EVK-MIMXRT1060.zip`
- SDK Location: `https://moxa.sharepoint.com/sites/NETSW2Team2/Shared%20Documents/General/ProtocolFeature/GPS_MIMXRT1060-EVK/SDK_2_14_0_EVK-MIMXRT1060.zip`
- MCUXpresso IDE filename: `MCUXpressoIDE_11.8.0_1165.exe`
- MCUXpresso IDE location: `https://moxa.sharepoint.com/sites/NETSW2Team2/Shared%20Documents/General/ProtocolFeature/GPS_MIMXRT1060-EVK/MCUXpressoIDE_11.8.0_1165.exe`
- Serial port driver: `這步驟會於安裝MCUXpresso IDE過程一併完成,僅注意於安裝過程確保PC與MIMXRT1060連接即可`

* [Getting Started - Get software ](https://www.nxp.com/document/guide/getting-started-with-the-mimxrt1060-evk:GS-MIMXRT1060-EVK?section=get-software)
### Build Run
待安裝完get software步驟後,開啟MCUXpresso IDE
* Toturial
- Launch MCUXpresso IDE, and setting workspace directory

- Close welcome page

- Installed SDK
將下載SDK檔 `SDK_2_14_0_EVK-MIMXRT1060.zip` 拖曳到該位置裡面,如下圖為拖曳完成示意

- Import SDK example
該步驟為啟用SDK中的範例程式,透由執行各個範例程式可驗證公版(evaluation kit)功能
- 點擊import SDK example(s)..

- 點擊evkmimxrt1060圖示,並點擊下一步

- 點擊欲使用的範例程式,如下圖為**demo_app**中的**hello world**

- 選擇載入完成的hello world sample code,則會出現提示字眼**Debug**

- 下載Terminal工具,如:**Putty**;開啟Putty並等待檢視執行程式後的結果

- 依序點擊**Build**, **Debug**

- 點擊build後,confirm no error

- 點擊debug後,再點擊執行圖示

- Hello world 執行結果

- 該範例程式除了hello world外,可從source code發現另有套接字可測試

```clike=
int main(void)
{
char ch;
/* Init board hardware. */
BOARD_ConfigMPU();
BOARD_InitBootPins();
BOARD_InitBootClocks();
BOARD_InitDebugConsole();
/* Just enable the trace clock, leave coresight initialization to IDE debugger */
SystemCoreClockUpdate();
CLOCK_EnableClock(kCLOCK_Trace);
PRINTF("hello world.\r\n");
while (1)
{
ch = GETCHAR();
PUTCHAR(ch);
}
}
```
* [Getting Started - Build run](https://www.nxp.com/document/guide/getting-started-with-the-mimxrt1060-evk:GS-MIMXRT1060-EVK?section=build-run)
## Application & function
### GPIO & LED
* Load example code
- 載入driver_example, click finish

- click Build, Debug, and show terminal monitor

- 將會發現USB通訊燈與GPIO LED燈都會不間斷閃爍

* Test situation
- 透由變更時間clock,將可調整閃爍頻率
```clike=
// ...
// ...
/* Print a note to terminal. */
PRINTF("\r\n GPIO Driver example\r\n");
PRINTF("\r\n The LED is blinking.\r\n");
/* Init output LED GPIO. */
GPIO_PinInit(EXAMPLE_LED_GPIO, EXAMPLE_LED_GPIO_PIN, &led_config);
while (1)
{
//SDK_DelayAtLeastUs(1000000, SDK_DEVICE_MAXIMUM_CPU_CLOCK_FREQUENCY); // per 1 second
SDK_DelayAtLeastUs(100000, SDK_DEVICE_MAXIMUM_CPU_CLOCK_FREQUENCY); // per 100 millisecond
// ...
// ...
```
- 於調試GPIO過程中,發現Ethernet LED會與GPIO LED以相同頻率同時閃爍
* 並下載MIMXRT1060-EVK電路圖(schmetic),經調查後發現兩者的線路是接在同一處
* GPIO LED可從Board上對應其名稱為**D18**

* 透由追查 pin **USER_LED**,可發現與**ENET_RST**在硬體線路上是相連接
如下圖該Pin與Ethernet線路有關,故推斷Ethernet LED會同步閃爍的原因於此

* 另外,透由查找**USER_LED**,可追到關鍵字**JTAG_TDI**, **GPIO_AD_B0_09**, **MIMXRT1062DVL6A**

* 接著,從程式碼中對應呼叫的GPIO pin為**GPIO_AD_B0_09**
`source/gpio_led_out.c`
```=clike
#define EXAMPLE_LED_GPIO BOARD_USER_LED_GPIO
#define EXAMPLE_LED_GPIO_PIN BOARD_USER_LED_GPIO_PIN
```
`board/board.h`
```=clike
#ifndef BOARD_USER_LED_GPIO
#define BOARD_USER_LED_GPIO GPIO1
#endif
#ifndef BOARD_USER_LED_GPIO_PIN
#define BOARD_USER_LED_GPIO_PIN (9U)
#endif
```
`device/MIMXRT1062.h`
```=clike
/* Peripheral GPIO1 base address */
#define GPIO1_BASE (0x401B8000u)
/* Peripheral GPIO1 base pointer */
#define GPIO1 ((GPIO_Type *)GPIO1_BASE)
```
* 對照datasheet(i_MX RT1060 Processor Reference Manual)
並確認GPIO1 register address **0x401B8000u**是一致

* 再從function **BOARD_InitBootPins()** 查找 **GPIO_AD_B0_09**
```=clike
int main(void)
{
/* Define the init structure for the output LED pin*/
gpio_pin_config_t led_config = {kGPIO_DigitalOutput, 0, kGPIO_NoIntmode};
/* Board pin, clock, debug console init */
BOARD_ConfigMPU();
BOARD_InitBootPins();
// ...
```
* 可確認到**GPIO_AD_B0_09**在這範例程式中被作為GPIO LED使用
```=clike
void BOARD_InitPins(void) {
CLOCK_EnableClock(kCLOCK_Iomuxc);
IOMUXC_SetPinMux(IOMUXC_GPIO_AD_B0_09_GPIO1_IO09, 0U);
// ...
```
* 最後,**JTAG_TDI**則跟能在Putty上顯示有關,此部分細節將於UART時做說明
```=clike
void BOARD_InitPins(void) {
CLOCK_EnableClock(kCLOCK_Iomuxc);
IOMUXC_SetPinMux(IOMUXC_GPIO_AD_B0_09_GPIO1_IO09, 0U);
IOMUXC_SetPinMux(IOMUXC_GPIO_AD_B0_12_LPUART1_TX, 0U);
IOMUXC_SetPinMux(IOMUXC_GPIO_AD_B0_13_LPUART1_RX, 0U);
// ...
```
### UART
* Load example code
- 載入driver_example, click finish

- click Build, Debug, and show terminal monitor

* Test situation
- 增加套接字測試,確認UART傳輸

- view main function
```=clike
#define DEMO_LPUART LPUART1 // check UART register address
int main(void)
{
uint8_t ch;
uint8_t ch_3;
lpuart_config_t config;
BOARD_ConfigMPU();
BOARD_InitBootPins(); // check pin define
BOARD_InitBootClocks();
BOARD_InitDebugConsole();
LPUART_GetDefaultConfig(&config);
config.baudRate_Bps = BOARD_DEBUG_UART_BAUDRATE;
config.enableTx = true;
config.enableRx = true;
LPUART_Init(DEMO_LPUART, &config, DEMO_LPUART_CLK_FREQ);
LPUART_WriteBlocking(DEMO_LPUART, txbuff, sizeof(txbuff) - 1);
while (1)
{
LPUART_ReadBlocking(DEMO_LPUART, &ch, 1);
PRINTF("\r\n get1 %c\r\n", ch); // add
LPUART_WriteBlocking(DEMO_LPUART, &ch, 1);
}
}
```
* 透由查找**DEMO_LPUART**,並確認UART的register與pin定義
- 程式與datasheet的register address是一致的
`source/lpuart.c`
```=clike
#define DEMO_LPUART LPUART1
```
`device/MIMXRT1062.h`
```=clike
#define LPUART1_BASE (0x40184000u)
/** Peripheral LPUART1 base pointer */
#define LPUART1 (LPUART_Type*)LPUART1_BASE)
```
`IMXRT1060RM_rev1_Processor_Manual.pdf`

- 確認UART1程式之pin define與schematic是一致的
`board/pin_mux.c`
```
IOMUXC_SetPinMux(IOMUXC_GPIO_AD_B0_12_LPUART1_TX, 0U);
IOMUXC_SetPinMux(IOMUXC_GPIO_AD_B0_13_LPUART1_RX, 0U);
```

- **進階應用**為增加**UART3**pin define,並透由terminal驗證UART3功能
先找到**GPIO_AD_B1_07**, **GPIO_AD_B1_06**這兩根pin,並推測可能可用

- 並於UART1相對應的位置都增加UART3 function
`source/lpuart.c`, `board/pin_mux.c`
- 新增一組UART3 define,並於init, main處增加UART3 function
`source/lpuart.c`
```=clike
#define DEMO_LPUART_3 LPUART3
// ...
int main(void)
{
// ...
LPUART_WriteBlocking(DEMO_LPUART, txbuff, sizeof(txbuff) - 1);
LPUART_WriteBlocking(DEMO_LPUART_3, txbuff_3, sizeof(txbuff_3) - 1);
// ...
while (1)
{
LPUART_ReadBlocking(DEMO_LPUART_3, &ch_3, 1);
PRINTF("\r\n get3 %c\r\n", ch_3);
LPUART_WriteBlocking(DEMO_LPUART_3, &ch_3, 1);
// ...
```
`board/pin_mux.c`
```=clike
void BOARD_InitPins(void) {
CLOCK_EnableClock(kCLOCK_Iomuxc);
IOMUXC_SetPinMux(IOMUXC_GPIO_AD_B1_06_LPUART3_TX, 0U);
IOMUXC_SetPinMux(IOMUXC_GPIO_AD_B1_07_LPUART3_RX, 0U);
IOMUXC_SetPinConfig(IOMUXC_GPIO_AD_B1_06_LPUART3_TX, 0x10B0U);
IOMUXC_SetPinConfig(IOMUXC_GPIO_AD_B1_07_LPUART3_RX, 0x10B0U);
}
```
- Terminal顯示畫面
COM4為UART1, COM5為UART3

- Schematic接線方法
USB to TTL (CP2100 module)

MIMXRT1060_EVK J22 UART pin

利用隔壁J24的接地線

完成接線圖(CP2102端)

完成接線圖(J22, J24端)

### Ethernet
* Load example code
- 載入driver_example, click finish

- click Build, Debug, and show terminal monitor

* Test situation
- 設定**IP**, **Netmask**, **Gateway**
於`source/lwip_httpsrv_bm.c`中設定
- 開啟Chrome頁面,並確認http server已正常啟動

### To Do
* UART:
- [ ] 需再增加multi task function,為因應UART1, UART3在main底下blocking彼此的佇列
* Zephyr OS:
- [ ] 因目前找到的實作皆是屬於Bare Metal的開發方式,近期將著手進行Zephyr OS的研究與應用
- [ ] 可能需要一張micro SD card, 作為開機用

- [ ] 找尋iMX-RT1060 BSP source
註:
```
Bare Metal or Bare Machine
開發指的是在沒有OS作業系統的環境下來開發程式,
在這種環境下所開發出來的產物, 像是BIOS程式, Boot Loader這類的程式。
```
Link:
1.[i.MX RT1060 Evaluation Kit](https://www.nxp.com/design/development-boards/i-mx-evaluation-and-development-boards/i-mx-rt1060-evaluation-kit:MIMXRT1060-EVKB)
2.[MIMXRT1060 Evaluation Kit Board Hardware User's Guide](https://www.mouser.com/pdfdocs/NXP_MIMXRT1060-EVK_UG.pdf)
3.[NXP Community Schematic for MIMXRT1060-EVK](https://community.nxp.com/t5/i-MX-RT/Schematic-for-MIMXRT1060-EVK/td-p/852868)
4.[Plug In - Getting Started with the MIMXRT1060-EVK](https://www.nxp.com/document/guide/getting-started-with-the-mimxrt1060-evk:GS-MIMXRT1060-EVK?)
5.[i.MX RT1060 Crossover MCU with Arm® Cortex®-M7 Relative Document Download Location](https://www.nxp.com/products/processors-and-microcontrollers/arm-microcontrollers/i-mx-rt-crossover-mcus/i-mx-rt1060-crossover-mcu-with-arm-cortex-m7-core-operating-up-to-600-mhz-with-1-mb-ram:i.MX-RT1060)
6.[Get Software - Getting Started with the MIMXRT1060-EVK](https://www.nxp.com/document/guide/getting-started-with-the-mimxrt1060-evk:GS-MIMXRT1060-EVK?section=get-software)
7.[Build Run - Getting Started with the MIMXRT1060-EVK](https://www.nxp.com/document/guide/getting-started-with-the-mimxrt1060-evk:GS-MIMXRT1060-EVK?section=build-run)