#define __MAIN_C__ #include // Define the raw base address values for the i/o devices #define AHB_SW_BASE 0x40000000 #define AHB_OUT_BASE 0x50000000 // Define pointers with correct type for access to 32-bit i/o devices // // The locations in the devices can then be accessed as: // SW_REGS[0] // OUT_REGS[0] // OUT_REGS[1] // volatile uint32_t* SW_REGS = (volatile uint32_t*) AHB_SW_BASE; volatile uint32_t* OUT_REGS = (volatile uint32_t*) AHB_OUT_BASE; #include ////////////////////////////////////////////////////////////////// // Functions provided to access i/o devices ////////////////////////////////////////////////////////////////// void write_out(uint32_t value) { OUT_REGS[1] = 1; OUT_REGS[0] = value; } void set_out_invalid(void) { OUT_REGS[1] = 0; OUT_REGS[0] = 0; } uint32_t read_out(void) { return OUT_REGS[0]; } uint32_t read_switches(void) { return SW_REGS[0]; } ////////////////////////////////////////////////////////////////// // Other Functions ////////////////////////////////////////////////////////////////// int factorial(int value) { if ( value == 0 ) return 1; else return ( value * factorial(value - 1) ); } ////////////////////////////////////////////////////////////////// // Main Function ////////////////////////////////////////////////////////////////// int main(void) { int switch_temp; write_out( 0x00000400 ); // wait for all the switches to go to zero while( read_switches() != 0x0000 ){} write_out( 0x00100000 ); // wait for the least significant switch to go to 1 while( ( read_switches() & 0x0001 ) != 0x0001 ){} // repeat forever (embedded programs generally do not terminate) while(1){ switch_temp = read_switches(); if ( switch_temp < 8 ) { // if the switch value < 8 retun the factorial write_out( factorial(switch_temp) ); } else { // otherwise flag an error set_out_invalid(); } } }