# RT-Thread Peripherals
Author: Tang Cheuk Yu, Astral (cytangar@connect.ust.hk)
Board Schematic: https://gitlab.hkustracing.com/chleeay/internal_board
Core Board:
## Packages
We use the package system in rt-thread to install our custom libraries to drive the peripherals.
To install our packages,
1. go to https://github.com/LeeChunHei/custom_rtt_package_list
2. Download the files and place inside /env/packages
3. Change the directory name from custom_rtt_package_list to custom_packages
4. Edit the Kconfig file next to custom_packages (/env/packages/Kconfig), add a line
```source "$PKGS_DIR/custom_packages/Kconfig"```
5. In VSCode, click RT-Thread Settings inside your project

6. Check all custom packages (under RT-Thread custom online packages/peripheral lib)

7. Save and close the setting
8. In the terminal, enter ```pkgs --update```

The packages will be installed after performing these steps, we can find the packages in the packages folder

If you can not find the folder, click reload
## Configure the packages
We need to configure the packages before using them
1. open RT-Thread Settings inside your project
2. In RT-Thread kernel
- Tick frequency, Hz : 1000
3. RT-Thread custom online packages
- peripheral libraries and drivers
1. st7735r tft lcd driver package
- Setup st7735r tft in menuconfig: Check
1. SPI bus connected to the tft lcd: spi4
2. GPIO port number for the chip select pin 2
3. GPIO pin number for the chip select pin 0
4. Adjustable backlight: Unchecked
5. GPIO port number for the backlight pin 2
6. GPIO pin number for the backlight pin 27
7. GPIO port number for the dc pin 2
8. GPIO pin number for the dc pin 20
9. GPIO port number for the reset pin 2
10. GPIO pin number for the reset pin 22
11. Width of the tft lcd 128
12. Height of the tft lcd 160
13. Orientation of the tft lcd 0
2. MT9V034 camera driver package
- Enable auto explosure control: Check
- Enable auto gain control: Check
- HDR: 100dB
- Width: 184
- Height: 120
- Camera configuration IC2/SCCB bus name: i2c10
- Assert the program when faced configuration failure: Check
3. Soft I2C driver package
- Setup soft I2C in menuconfig: check
1. Soft I2C bus name: i2c10
2. Baudrate: 100000
3. GPIO port number for the SCL pin: 3
4. GPIO pin number for the SCL pin: 0
5. GPIO port number for the SDA pin 3
6. GPIO pin number for the SDA pin 1
7. Use rt_hw_us_delay function: Check
4. Hardware Drivers Config
- On-chip Peripheral Drivers
1. Enable DMA: check
2. Enable SPI: Check
- Enable SPI4: check
## ST7735R LCD
Find the device
```c=
static rt_device_t lcd = rt_device_find("lcd0");
```
Start the device
```c=
rt_device_open(lcd,0);
```
```c=
for (rt_uint8_t y = 0; y < 120; ++y)
{
/* Set the TFT LCD rect region */
struct rt_st7735r_rect rect =
{
.x = 0,
.y = y,
.width = 120,
.height = 1,
};
rt_device_control(lcd, RT_ST7735R_SET_RECT, &rect);
/* Fill grayscale pxiels */
rt_uint8_t pixels[120];
rt_memset(pixels, y, 120);
rt_device_write(lcd,RT_ST7735R_WRITE_GRAYSCALE_PIXEL, pixels, 120);
}
```
For more details check the readme of the package
## MT9V034
```c=
int main(void)
{
/* Find the camera device */
rt_device_t cam = rt_device_find("MT9V034");
rt_device_open(cam, 0);
rt_uint8_t* buffer;
rt_size_t buffer_size;
while (1)
{
/* request a frame buffer */
buffer_size = rt_device_read(cam, 0, &buffer, 0);
if (buffer_size != 0)
{
/* Loop through the camera image */
for (rt_uint16_t i = 0; i < buffer_size; ++i)
{
/* Access one pixel in the frame */
buffer[i];
}
/* return the frame buffer back to queue */
rt_device_write(cam, 0, &buffer, 0);
}
else
{
rt_thread_mdelay(1);
}
}
}
```
## PWM
To enable pwm, go to rt-thread settings