Displaying Camera Images on K230 Development Board
-
Preparation
1.1 Wiring Instructions
The K230 development board is equipped with three channels of MIPI-CSI camera interfaces, specifically CAM0, CAM1, and CAM2. Among them, CAM2 serves as the default camera interface pre-installed at the factory. The other two interfaces are FPC interfaces of 22p-0.5mm specifications, where FPC stands for flexible flat cable. Each camera can output three channels with different resolutions and formats.
- Hardware Connection Steps
MIPI-CSI Camera Connection:
-
Power-off operation: Disconnect all power sources from the development board.
-
Open the interface: Use tweezers to gently lift the metal latch of the CSI interface, where vertical connectors are pulled straight out, and horizontal connectors are flipped open.
-
Insert the camera: Insert the FPC flat cable flatly into the connector, ensuring the golden fingers are fully inserted.
-
Secure the interface: Press down the latch to lock the flat cable, or close the cover for horizontal connectors.
-
Power-on test: Connect the power supply and boot the development board.
-
Program Analysis
The program source code is shown below:
import time, os, sys
from media.sensor import * # Import sensor module to use camera-related interfaces
from media.display import * # Import display module to use display-related interfaces
from media.media import * # Import media module to use media-related interfaces
# Default camera GC2093
sensor2 = Sensor(id=2) # Construct camera object
sensor2.reset() # Reset and initialize camera
sensor2.set_framesize(width = 960, height = 540) # Set frame size
sensor2.set_pixformat(Sensor.RGB565) # Set output image format, default channel 0
# CSI1 interface
sensor1 = Sensor(id=1) # Construct camera object
sensor1.reset() # Reset and initialize camera
sensor1.set_framesize(width = 960, height = 540) # Set frame size
sensor1.set_pixformat(Sensor.YUV420SP) # Set output image format bind_info = sensor1.bind_info(x = 960, y = 0)
Display.bind_layer(**bind_info, layer = Display.LAYER_VIDEO1) # Output channel 1
# CSI0 interface
sensor0 = Sensor(id=0) # Construct camera object
sensor0.reset() # Reset and initialize camera
sensor0.set_framesize(width = 960, height = 540) # Set frame size FHD(1920x1080), default channel 0
sensor0.set_pixformat(Sensor.YUV420SP) # Set output image format bind_info = sensor0.bind_info(x = 0, y = 540)
Display.bind_layer(**bind_info, layer = Display.LAYER_VIDEO2) # Output channel 2
# Use IDE buffer to output images, display size is consistent with sensor configuration. Display.init(Display.LT9611, to_ide = True)
MediaManager.init() # Initialize media resource manager
sensor2.run() # Start sensor2
sensor1.run() # Start sensor1
sensor0.run() # Start sensor0
clock = time.clock()
while True:
# Other functions can be added as needed clock.tick()
img = sensor2.snapshot() # Take a picture
Display.show_image(img) # Display image
print(clock.fps()) # Print FPS
- Module Import Layer: Importing Hardware Interfaces and Basic Tools
System modules: The time module is used for frame rate calculation, and the os and sys modules provide system-level resource calls, such as path manipulation and exception handling.
Hardware modules: Interfaces such as a sensor for camera operations, a display for display device control, and media for media resource management are imported from the low-level media library of the K230, serving as the core entry points for code-to-hardware interaction.
- Initialization of Three-Way Cameras: Parameter Configuration and Display Layer Binding
This is the core hardware configuration section of the code. The three MIPI-CSI interfaces of the K230 development board, which are CSI2, CSI1, and CSI0, are initialized in sequence. The camera object of each interface is configured independently, and different display layer binding strategies are applied, as detailed below:
Camera Interface ID Resolution Pixel Format Display Layer Binding Core Features
CAM2 2 960×540 RGB565 Unbound Default camera, color format, main source
CAM1 1 960×540 YUV420SP LAYER_VIDEO1 Video encoding or AI friendly, sub-layer 1
CAM0 0 960×540 YUV420SP LAYER_VIDEO2 Same format as CSI1, sub-layer 2
-
Object Construction and Reset: The camera object corresponding to the CSI interface is created via
Sensor with id=N, andreset()is called to perform the hardware reset and initialization, which is a mandatory step because the camera cannot communicate otherwise. -
Resolution Setting: The
set_framesizefunction with parameterswidth=960andheight=540is used to configure all three cameras to a resolution of 960x540, balancing acquisition efficiency and hardware performance. -
CSI2 Uses RGB565: This color format utilizes 2 bytes per pixel and is suitable for direct image display without requiring format conversion.
-
CSI1 and CSI0 Use YUV420SP: This brightness-chrominance separation format utilizes 12 bits per pixel and is suitable for video encoding and AI inference due to smaller data volume and higher hardware processing efficiency.
-
The coordinates of the camera image on the display device are set via
bind_infousingxandyparameters. -
The
Display.bind_layerfunction is called to output images from CSI1 and CSI0 to theLAYER_VIDEO1andLAYER_VIDEO2display layers of the development board, enabling split-screen display of multiple images. -
CSI2 Unbound to Display Layer: The image is output manually in the subsequent main loop via the
Display.show_imagefunction, which serves as the main display source of the program.
- Initialization of Display and Media Resources: Hardware Co-Scheduling
-
Display Driver Initialization: The display driver of the K230 development board is specified as LT9611, which is a mainstream display driver. Setting
to_ide=Trueenables the IDE buffer output function, allowing the camera image to be viewed directly within the programming IDE during the development stage without connecting an external physical screen. -
Media Resource Management: Calling the
MediaManager.initfunction is a core step in the K230 media layer, which is responsible for allocating hardware buses, memory buffers, and other resources for cameras and display devices, ensuring that multiple peripherals work collaboratively without conflicts.
- Camera Startup and Main Loop: Image Acquisition and Frame Rate Monitoring
-
Camera Startup: The
run()method is called to activate the image acquisition function of the three cameras, at which point CSI2, CSI1, and CSI0 all begin outputting image data streams. -
Frame Rate Calculation: A clock object is created via
time.clock(),clock.tick()records the timestamp of each loop, andclock.fps()calculates and returns the number of loops per second, which represents the frame rate. -
Main Loop Logic: After entering the infinite loop, the program only acquires and displays the images from the three camera interfaces.
If a compatible camera such as GC2093 is not physically connected to the designated CSI2 or CSI0 interface in the program, running the program will trigger a hardware interface exception, leading directly to a program crash or freeze. Commenting out the initialization code of the relevant camera interface enables successful execution.