Skip to content

Commit

Permalink
clang-format
Browse files Browse the repository at this point in the history
  • Loading branch information
Jake-Carter committed Sep 21, 2023
1 parent cfdf74b commit 326e689
Showing 1 changed file with 65 additions and 64 deletions.
129 changes: 65 additions & 64 deletions Examples/MAX78000/CNN/cats-dogs/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,48 +47,50 @@ volatile uint32_t cnn_time; // Stopwatch

void fail(void)
{
printf("\n*** FAIL ***\n\n");
while (1);
printf("\n*** FAIL ***\n\n");
while (1)
;
}

// Data input: HWC 3x128x128 (49152 bytes total / 16384 bytes per channel):
static const uint32_t input_0[] = SAMPLE_INPUT_0;
void load_input(void)
{
// This function loads the sample data input -- replace with actual data
// This function loads the sample data input -- replace with actual data

int i;
const uint32_t *in0 = input_0;
int i;
const uint32_t *in0 = input_0;

for (i = 0; i < 16384; i++) {
// Remove the following line if there is no risk that the source would overrun the FIFO:
while (((*((volatile uint32_t *) 0x50000004) & 1)) != 0); // Wait for FIFO 0
*((volatile uint32_t *) 0x50000008) = *in0++; // Write FIFO 0
}
for (i = 0; i < 16384; i++) {
// Remove the following line if there is no risk that the source would overrun the FIFO:
while (((*((volatile uint32_t *)0x50000004) & 1)) != 0)
; // Wait for FIFO 0
*((volatile uint32_t *)0x50000008) = *in0++; // Write FIFO 0
}
}

// Expected output of layer 6 for cats-dogs given the sample input (known-answer test)
// Delete this function for production code
static const uint32_t sample_output[] = SAMPLE_OUTPUT;
int check_output(void)
{
int i;
uint32_t mask, len;
volatile uint32_t *addr;
const uint32_t *ptr = sample_output;

while ((addr = (volatile uint32_t *) *ptr++) != 0) {
mask = *ptr++;
len = *ptr++;
for (i = 0; i < len; i++)
if ((*addr++ & mask) != *ptr++) {
printf("Data mismatch (%d/%d) at address 0x%08x: Expected 0x%08x, read 0x%08x.\n",
i + 1, len, addr - 1, *(ptr - 1), *(addr - 1) & mask);
return CNN_FAIL;
}
}

return CNN_OK;
int i;
uint32_t mask, len;
volatile uint32_t *addr;
const uint32_t *ptr = sample_output;

while ((addr = (volatile uint32_t *)*ptr++) != 0) {
mask = *ptr++;
len = *ptr++;
for (i = 0; i < len; i++)
if ((*addr++ & mask) != *ptr++) {
printf("Data mismatch (%d/%d) at address 0x%08x: Expected 0x%08x, read 0x%08x.\n",
i + 1, len, addr - 1, *(ptr - 1), *(addr - 1) & mask);
return CNN_FAIL;
}
}

return CNN_OK;
}

// Classification layer:
Expand All @@ -97,62 +99,62 @@ static q15_t ml_softmax[CNN_NUM_OUTPUTS];

void softmax_layer(void)
{
cnn_unload((uint32_t *) ml_data);
softmax_q17p14_q15((const q31_t *) ml_data, CNN_NUM_OUTPUTS, ml_softmax);
cnn_unload((uint32_t *)ml_data);
softmax_q17p14_q15((const q31_t *)ml_data, CNN_NUM_OUTPUTS, ml_softmax);
}

int main(void)
{
int i;
int digs, tens;
int i;
int digs, tens;

MXC_ICC_Enable(MXC_ICC0); // Enable cache
MXC_ICC_Enable(MXC_ICC0); // Enable cache

// Switch to 100 MHz clock
MXC_SYS_Clock_Select(MXC_SYS_CLOCK_IPO);
SystemCoreClockUpdate();
// Switch to 100 MHz clock
MXC_SYS_Clock_Select(MXC_SYS_CLOCK_IPO);
SystemCoreClockUpdate();

printf("Waiting...\n");
printf("Waiting...\n");

// DO NOT DELETE THIS LINE:
MXC_Delay(SEC(2)); // Let debugger interrupt if needed
// DO NOT DELETE THIS LINE:
MXC_Delay(SEC(2)); // Let debugger interrupt if needed

// Enable peripheral, enable CNN interrupt, turn on CNN clock
// CNN clock: APB (50 MHz) div 1
cnn_enable(MXC_S_GCR_PCLKDIV_CNNCLKSEL_PCLK, MXC_S_GCR_PCLKDIV_CNNCLKDIV_DIV1);
// Enable peripheral, enable CNN interrupt, turn on CNN clock
// CNN clock: APB (50 MHz) div 1
cnn_enable(MXC_S_GCR_PCLKDIV_CNNCLKSEL_PCLK, MXC_S_GCR_PCLKDIV_CNNCLKDIV_DIV1);

printf("\n*** CNN Inference Test cats-dogs ***\n");
printf("\n*** CNN Inference Test cats-dogs ***\n");

cnn_init(); // Bring state machine into consistent state
cnn_load_weights(); // Load kernels
cnn_load_bias();
cnn_configure(); // Configure state machine
cnn_start(); // Start CNN processing
load_input(); // Load data input via FIFO
cnn_init(); // Bring state machine into consistent state
cnn_load_weights(); // Load kernels
cnn_load_bias();
cnn_configure(); // Configure state machine
cnn_start(); // Start CNN processing
load_input(); // Load data input via FIFO

while (cnn_time == 0)
MXC_LP_EnterSleepMode(); // Wait for CNN
while (cnn_time == 0) MXC_LP_EnterSleepMode(); // Wait for CNN

if (check_output() != CNN_OK) fail();
softmax_layer();
if (check_output() != CNN_OK)
fail();
softmax_layer();

printf("\n*** PASS ***\n\n");
printf("\n*** PASS ***\n\n");

#ifdef CNN_INFERENCE_TIMER
printf("Approximate data loading and inference time: %u us\n\n", cnn_time);
printf("Approximate data loading and inference time: %u us\n\n", cnn_time);
#endif

cnn_disable(); // Shut down CNN clock, disable peripheral
cnn_disable(); // Shut down CNN clock, disable peripheral

printf("Classification results:\n");
for (i = 0; i < CNN_NUM_OUTPUTS; i++) {
digs = (1000 * ml_softmax[i] + 0x4000) >> 15;
tens = digs % 10;
digs = digs / 10;
printf("[%7d] -> Class %d: %d.%d%%\n", ml_data[i], i, digs, tens);
}
printf("Classification results:\n");
for (i = 0; i < CNN_NUM_OUTPUTS; i++) {
digs = (1000 * ml_softmax[i] + 0x4000) >> 15;
tens = digs % 10;
digs = digs / 10;
printf("[%7d] -> Class %d: %d.%d%%\n", ml_data[i], i, digs, tens);
}

return 0;
return 0;
}

/*
Expand All @@ -170,4 +172,3 @@ int main(void)
Weight memory: 57,776 bytes out of 442,368 bytes total (13.1%)
Bias memory: 2 bytes out of 2,048 bytes total (0.1%)
*/

0 comments on commit 326e689

Please sign in to comment.