@@ -96,15 +96,16 @@ float nn_top1_output_class_proba;
9696
9797#define ALIGN_TO_16 (value ) (((value) + 15) & ~15)
9898
99- /* for models not multiple of 16; needs a working buffer */
99+ /* When NN input dimensions are not a multiple of 16, the DCMIPP output needs cropping */
100100#if (STAI_NETWORK_IN_1_WIDTH * STAI_NETWORK_IN_1_CHANNEL ) != ALIGN_TO_16 (STAI_NETWORK_IN_1_WIDTH * STAI_NETWORK_IN_1_CHANNEL )
101+ #define DCMIPP_NN_NEEDS_CROP 1
101102#define DCMIPP_OUT_NN_LEN (ALIGN_TO_16(STAI_NETWORK_IN_1_WIDTH * STAI_NETWORK_IN_1_CHANNEL) * STAI_NETWORK_IN_1_HEIGHT)
102103#define DCMIPP_OUT_NN_BUFF_LEN (DCMIPP_OUT_NN_LEN + 32 - DCMIPP_OUT_NN_LEN%32)
103104
104105__attribute__ ((aligned (32 )))
105- uint8_t dcmipp_out_nn [DCMIPP_OUT_NN_BUFF_LEN ];
106+ static uint8_t dcmipp_out_nn [DCMIPP_OUT_NN_BUFF_LEN ];
106107#else
107- uint8_t * dcmipp_out_nn ;
108+ #define DCMIPP_NN_NEEDS_CROP 0
108109#endif
109110
110111/* model */
@@ -133,7 +134,6 @@ static void set_clk_sleep_mode(void);
133134static void IAC_Config (void );
134135static void Display_WelcomeScreen (void );
135136static void Hardware_init (void );
136- static void Run_Inference (stai_network * network_instance );
137137static void NeuralNetwork_init (uint32_t * nn_in_length , stai_ptr * nn_out , stai_size * number_output , int32_t nn_out_len []);
138138
139139
@@ -146,24 +146,7 @@ int main(void)
146146{
147147 Hardware_init ();
148148
149- /*** App header *************************************************************/
150- printf ("========================================\n" );
151- printf ("STM32N6-GettingStarted-ImageClassification %s (%s)\n" , APP_VERSION_STRING , APP_GIT_SHA1_STRING );
152- printf ("Build date & time: %s %s\n" , __DATE__ , __TIME__ );
153- #if defined(__GNUC__ )
154- printf ("Compiler: GCC %d.%d.%d\n" , __GNUC__ , __GNUC_MINOR__ , __GNUC_PATCHLEVEL__ );
155- #elif defined(__ICCARM__ )
156- printf ("Compiler: IAR EWARM %d.%d.%d\n" , __VER__ / 1000000 , (__VER__ / 1000 ) % 1000 ,__VER__ % 1000 );
157- #else
158- printf ("Compiler: Unknown\n" );
159- #endif
160- printf ("HAL: %lu.%lu.%lu\n" , __STM32N6xx_HAL_VERSION_MAIN , __STM32N6xx_HAL_VERSION_SUB1 , __STM32N6xx_HAL_VERSION_SUB2 );
161- printf ("STEdgeAI Tools: %d.%d.%d\n" , STAI_TOOLS_VERSION_MAJOR , STAI_TOOLS_VERSION_MINOR , STAI_TOOLS_VERSION_MICRO );
162- printf ("NN model: %s\n" , STAI_NETWORK_ORIGIN_MODEL_NAME );
163- printf ("========================================\n" );
164-
165149 /*** NN Init ****************************************************************/
166- uint32_t pitch_nn = 0 ;
167150 uint32_t nn_in_len = 0 ;
168151 stai_size number_output = 0 ;
169152 stai_ptr nn_out [STAI_NETWORK_OUT_NUM ] = {0 };
@@ -180,49 +163,62 @@ int main(void)
180163 pp_input = nn_out [0 ];
181164
182165 /*** Camera Init ************************************************************/
166+ uint32_t pitch_nn = 0 ;
183167 CameraPipeline_Init ((uint32_t * [2 ]) {& lcd_bg_area .XSize , & lcd_fg_area .XSize }, (uint32_t * [2 ]) {& lcd_bg_area .YSize , & lcd_fg_area .YSize }, & pitch_nn );
184168
185169 Display_init ();
186170
187171 /* Start LCD Display camera pipe stream */
188172 CameraPipeline_DisplayPipe_Start (lcd_bg_buffer , CMW_MODE_CONTINUOUS );
189173
174+ /*** App header *************************************************************/
175+ printf ("========================================\n" );
176+ printf ("STM32N6-GettingStarted-ImageClassification %s (%s)\n" , APP_VERSION_STRING , APP_GIT_SHA1_STRING );
177+ printf ("Build date & time: %s %s\n" , __DATE__ , __TIME__ );
178+ #if defined(__GNUC__ )
179+ printf ("Compiler: GCC %d.%d.%d\n" , __GNUC__ , __GNUC_MINOR__ , __GNUC_PATCHLEVEL__ );
180+ #elif defined(__ICCARM__ )
181+ printf ("Compiler: IAR EWARM %d.%d.%d\n" , __VER__ / 1000000 , (__VER__ / 1000 ) % 1000 ,__VER__ % 1000 );
182+ #else
183+ printf ("Compiler: Unknown\n" );
184+ #endif
185+ printf ("HAL: %lu.%lu.%lu\n" , __STM32N6xx_HAL_VERSION_MAIN , __STM32N6xx_HAL_VERSION_SUB1 , __STM32N6xx_HAL_VERSION_SUB2 );
186+ printf ("STEdgeAI Tools: %d.%d.%d\n" , STAI_TOOLS_VERSION_MAJOR , STAI_TOOLS_VERSION_MINOR , STAI_TOOLS_VERSION_MICRO );
187+ printf ("NN model: %s\n" , STAI_NETWORK_ORIGIN_MODEL_NAME );
188+ printf ("========================================\n" );
189+
190190 /*** App Loop ***************************************************************/
191191 while (1 )
192192 {
193193 CameraPipeline_IspUpdate ();
194194
195- if (pitch_nn != (STAI_NETWORK_IN_1_WIDTH * STAI_NETWORK_IN_1_CHANNEL ))
196- {
197- /* Start NN camera single capture Snapshot */
198- CameraPipeline_NNPipe_Start (dcmipp_out_nn , CMW_MODE_SNAPSHOT );
199- }
200- else
201- {
202- /* Start NN camera single capture Snapshot */
203- CameraPipeline_NNPipe_Start (nn_in , CMW_MODE_SNAPSHOT );
204- }
195+ #if DCMIPP_NN_NEEDS_CROP
196+ /* Start NN camera single capture Snapshot into intermediate buffer */
197+ CameraPipeline_NNPipe_Start (dcmipp_out_nn , CMW_MODE_SNAPSHOT );
198+ #else
199+ /* Start NN camera single capture Snapshot directly into NN input */
200+ CameraPipeline_NNPipe_Start (nn_in , CMW_MODE_SNAPSHOT );
201+ #endif
205202
206203 while (cameraFrameReceived == 0 ) {};
207204 cameraFrameReceived = 0 ;
208205
209206 uint32_t ts [2 ] = { 0 };
210207
211- if (pitch_nn != (STAI_NETWORK_IN_1_WIDTH * STAI_NETWORK_IN_1_CHANNEL ))
212- {
213- SCB_InvalidateDCache_by_Addr (dcmipp_out_nn , sizeof (dcmipp_out_nn ));
208+ #if DCMIPP_NN_NEEDS_CROP
214209 /*
215- * Crop the image if the neural network (NN) input dimensions are not a multiple of 16.
216- * The DCMIPP hardware requires the output image dimensions to be multiples of 16.
217- * This ensures compatibility with the NN input dimensions.
210+ * Crop the image: the DCMIPP hardware requires output dimensions to be
211+ * multiples of 16, so we crop the padded buffer into the NN input buffer.
218212 */
219- img_crop (dcmipp_out_nn , nn_in , pitch_nn , STAI_NETWORK_IN_1_WIDTH , STAI_NETWORK_IN_1_HEIGHT , STAI_NETWORK_IN_1_CHANNEL );
220- SCB_CleanInvalidateDCache_by_Addr (nn_in , nn_in_len );
221- }
213+ SCB_InvalidateDCache_by_Addr (dcmipp_out_nn , sizeof (dcmipp_out_nn ));
214+ img_crop (dcmipp_out_nn , nn_in , pitch_nn , STAI_NETWORK_IN_1_WIDTH , STAI_NETWORK_IN_1_HEIGHT , STAI_NETWORK_IN_1_CHANNEL );
215+ SCB_CleanInvalidateDCache_by_Addr (nn_in , nn_in_len );
216+ #endif
222217
223218 ts [0 ] = HAL_GetTick ();
224219 /* run ATON inference */
225- Run_Inference (network_context );
220+ ret = stai_network_run (network_context , STAI_MODE_SYNC );
221+ assert (ret == 0 );
226222 ts [1 ] = HAL_GetTick ();
227223
228224 Network_Postprocess ();
@@ -282,19 +278,6 @@ static void Hardware_init(void)
282278
283279}
284280
285- static void Run_Inference (stai_network * network_instance ) {
286- stai_return_code ret ;
287-
288- do {
289- ret = stai_network_run (network_instance , STAI_MODE_ASYNC );
290- if (ret == STAI_RUNNING_WFE )
291- LL_ATON_OSAL_WFE ();
292- } while (ret == STAI_RUNNING_WFE || ret == STAI_RUNNING_NO_WFE );
293-
294- ret = stai_ext_network_new_inference (network_instance );
295- assert (ret == STAI_SUCCESS );
296- }
297-
298281static void NeuralNetwork_init (uint32_t * nn_in_length , stai_ptr * nn_out , stai_size * number_output , int32_t nn_out_len [])
299282{
300283 stai_network_info info ;
0 commit comments