STM32N6 NPU Deployment — Politecnico di Milano  1.0
Documentation for Neural Network Deployment on STM32N6 NPU - Politecnico di Milano 2024-2025
app_camerapipeline.c
Go to the documentation of this file.
1 
19 #include <assert.h>
20 #include "cmw_camera.h"
21 #include "app_camerapipeline.h"
22 #include "app_config.h"
23 #include "crop_img.h"
24 
25 #if defined(USE_IMX335_SENSOR)
26  #define GAMMA_CONVERSION 0
27 #elif defined(USE_VD66GY_SENSOR)
28  #define GAMMA_CONVERSION 0
29 #elif defined(USE_VD55G1_SENSOR)
30  #define GAMMA_CONVERSION 0
31 #endif
32 
33 /* Leave the driver use the default resolution */
34 #define CAMERA_WIDTH 0
35 #define CAMERA_HEIGHT 0
36 #define CAMERA_FPS 30
37 
38 extern int32_t cameraFrameReceived;
39 
40 static void DCMIPP_PipeInitDisplay(CMW_CameraInit_t *camConf, uint32_t *bg_width, uint32_t *bg_height)
41 {
42  CMW_Aspect_Ratio_Mode_t aspect_ratio;
43  CMW_DCMIPP_Conf_t dcmipp_conf = {0};
44  int ret;
45 
47  {
48  aspect_ratio = CMW_Aspect_ratio_crop;
49  }
51  {
52  aspect_ratio = CMW_Aspect_ratio_fit;
53  }
55  {
56  aspect_ratio = CMW_Aspect_ratio_fullscreen;
57  }
58 
59  int lcd_bg_width;
60  int lcd_bg_height;
61 
62  lcd_bg_height = (camConf->height <= SCREEN_HEIGHT) ? camConf->height : SCREEN_HEIGHT;
63 
65  lcd_bg_width = (((camConf->width*lcd_bg_height)/camConf->height) - ((camConf->width*lcd_bg_height)/camConf->height) % 16);
66 #else
67  lcd_bg_width = (camConf->height <= SCREEN_HEIGHT) ? camConf->height : SCREEN_HEIGHT;
68 #endif
69 
70  *bg_width = lcd_bg_width;
71  *bg_height = lcd_bg_height;
72 
73  dcmipp_conf.output_width = lcd_bg_width;
74  dcmipp_conf.output_height = lcd_bg_height;
75  dcmipp_conf.output_format = DCMIPP_PIXEL_PACKER_FORMAT_RGB565_1;
76  dcmipp_conf.output_bpp = 2;
77  dcmipp_conf.mode = aspect_ratio;
78  dcmipp_conf.enable_gamma_conversion = GAMMA_CONVERSION;
79  uint32_t pitch;
80  ret = CMW_CAMERA_SetPipeConfig(DCMIPP_PIPE1, &dcmipp_conf, &pitch);
81  assert(ret == HAL_OK);
82  assert(dcmipp_conf.output_width * dcmipp_conf.output_bpp == pitch);
83 }
84 
85 static void DCMIPP_PipeInitNn(uint32_t *pitch)
86 {
87  CMW_Aspect_Ratio_Mode_t aspect_ratio;
88  CMW_DCMIPP_Conf_t dcmipp_conf;
89  int ret;
90 
92  {
93  aspect_ratio = CMW_Aspect_ratio_crop;
94  }
96  {
97  aspect_ratio = CMW_Aspect_ratio_fit;
98  }
100  {
101  aspect_ratio = CMW_Aspect_ratio_fit;
102  }
103 
104  dcmipp_conf.output_width = NN_WIDTH;
105  dcmipp_conf.output_height = NN_HEIGHT;
106  dcmipp_conf.output_format = DCMIPP_PIXEL_PACKER_FORMAT_RGB888_YUV444_1;
107  dcmipp_conf.output_bpp = NN_BPP;
108  dcmipp_conf.mode = aspect_ratio;
109  dcmipp_conf.enable_swap = COLOR_MODE;
110  dcmipp_conf.enable_gamma_conversion = GAMMA_CONVERSION;
111  ret = CMW_CAMERA_SetPipeConfig(DCMIPP_PIPE2, &dcmipp_conf, pitch);
112  assert(ret == HAL_OK);
113 }
114 
121 void CameraPipeline_Init(uint32_t *lcd_bg_width, uint32_t *lcd_bg_height, uint32_t *pitch_nn)
122 {
123  int ret;
124  CMW_CameraInit_t cam_conf;
125 
126  cam_conf.width = CAMERA_WIDTH;
127  cam_conf.height = CAMERA_HEIGHT;
128  cam_conf.fps = CAMERA_FPS;
129  cam_conf.pixel_format = 0; /* Default; Not implemented yet */
130  cam_conf.anti_flicker = 0;
131  cam_conf.mirror_flip = CAMERA_FLIP;
132 
133  ret = CMW_CAMERA_Init(&cam_conf);
134  assert(ret == CMW_ERROR_NONE);
135  DCMIPP_PipeInitDisplay(&cam_conf, lcd_bg_width, lcd_bg_height);
136  DCMIPP_PipeInitNn(pitch_nn);
137 }
138 
140 {
141  int ret;
142  ret = CMW_CAMERA_DeInit();
143  assert(ret == CMW_ERROR_NONE);
144 }
145 
146 void CameraPipeline_DisplayPipe_Start(uint8_t *display_pipe_dst, uint32_t cam_mode)
147 {
148  int ret;
149  ret = CMW_CAMERA_Start(DCMIPP_PIPE1, display_pipe_dst, cam_mode);
150  assert(ret == CMW_ERROR_NONE);
151 }
152 
153 void CameraPipeline_NNPipe_Start(uint8_t *nn_pipe_dst, uint32_t cam_mode)
154 {
155  int ret;
156 
157  ret = CMW_CAMERA_Start(DCMIPP_PIPE2, nn_pipe_dst, cam_mode);
158  assert(ret == CMW_ERROR_NONE);
159 }
160 
162 {
163  int ret;
164  ret = CMW_CAMERA_Suspend(DCMIPP_PIPE1);
165  assert(ret == CMW_ERROR_NONE);
166 }
167 
169 {
170  int ret = CMW_ERROR_NONE;
171  ret = CMW_CAMERA_Run();
172  assert(ret == CMW_ERROR_NONE);
173 }
174 
181 {
182  switch (pipe)
183  {
184  case DCMIPP_PIPE2 :
186  break;
187  }
188  return 0;
189 }
int CMW_CAMERA_PIPE_FrameEventCallback(uint32_t pipe)
Frame event callback.
void CameraPipeline_Init(uint32_t *lcd_bg_width, uint32_t *lcd_bg_height, uint32_t *pitch_nn)
Init the camera and the 2 DCMIPP pipes.
#define CAMERA_WIDTH
void CameraPipeline_NNPipe_Start(uint8_t *nn_pipe_dst, uint32_t cam_mode)
void CameraPipeline_DisplayPipe_Stop()
#define CAMERA_HEIGHT
#define CAMERA_FPS
void CameraPipeline_IspUpdate(void)
int32_t cameraFrameReceived
Camera frame ready flag.
Definition: main.c:179
void CameraPipeline_DeInit(void)
static void DCMIPP_PipeInitNn(uint32_t *pitch)
static void DCMIPP_PipeInitDisplay(CMW_CameraInit_t *camConf, uint32_t *bg_width, uint32_t *bg_height)
void CameraPipeline_DisplayPipe_Start(uint8_t *display_pipe_dst, uint32_t cam_mode)
#define SCREEN_HEIGHT
Central configuration header for the STM32N6570-DK pose estimation firmware application.
#define NN_BPP
Bytes per pixel of the NN input tensor.
Definition: app_config.h:207
#define COLOR_MODE
Active color format: RGB.
Definition: app_config.h:225
#define NN_HEIGHT
Neural network input image height in pixels.
Definition: app_config.h:182
#define CAMERA_FLIP
Camera mirror/flip mode — no transformation applied.
Definition: app_config.h:88
#define ASPECT_RATIO_MODE
Active aspect ratio mode for this deployment.
Definition: app_config.h:145
#define NN_WIDTH
Neural network input image width in pixels.
Definition: app_config.h:195
#define ASPECT_RATIO_CROP
Crop mode — both camera pipes are cropped to the NN input aspect ratio.
Definition: app_config.h:110
#define ASPECT_RATIO_FIT
Fit mode — both pipes are resized to the NN input aspect ratio.
Definition: app_config.h:120
#define ASPECT_RATIO_FULLSCREEN
Fullscreen mode — camera image is resized to fill the entire LCD.
Definition: app_config.h:129