Example showing \"hang\" when reading from bad pointer, and catching this in the HardFault_Handler

Dependencies:   mbed

Committer:
simon
Date:
Sun Mar 04 08:03:59 2012 +0000
Revision:
0:333b2c3ec494

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
simon 0:333b2c3ec494 1 // Example of hanging when reading a bad pointer
simon 0:333b2c3ec494 2
simon 0:333b2c3ec494 3 #include "mbed.h"
simon 0:333b2c3ec494 4
simon 0:333b2c3ec494 5 DigitalOut myled(LED1);
simon 0:333b2c3ec494 6
simon 0:333b2c3ec494 7 int x[10] = {0};
simon 0:333b2c3ec494 8
simon 0:333b2c3ec494 9 extern "C" void HardFault_Handler() {
simon 0:333b2c3ec494 10 error("Hit HardFault handler!\n");
simon 0:333b2c3ec494 11 }
simon 0:333b2c3ec494 12
simon 0:333b2c3ec494 13 int main() {
simon 0:333b2c3ec494 14 int *ok_ptr = x;
simon 0:333b2c3ec494 15 int *bad_ptr = (int*)0xe600b0; // not in RAM!
simon 0:333b2c3ec494 16 printf("ok_ptr = 0x%08X, bad_ptr = 0x%08X\n", ok_ptr, bad_ptr);
simon 0:333b2c3ec494 17
simon 0:333b2c3ec494 18 int ok_read = ok_ptr[0];
simon 0:333b2c3ec494 19 printf("ok_read = %d\n", ok_read);
simon 0:333b2c3ec494 20
simon 0:333b2c3ec494 21 int bad_read = bad_ptr[0];
simon 0:333b2c3ec494 22 printf("bad_read = %d\n", bad_read);
simon 0:333b2c3ec494 23
simon 0:333b2c3ec494 24 while(1) {
simon 0:333b2c3ec494 25 myled = 1;
simon 0:333b2c3ec494 26 wait(0.2);
simon 0:333b2c3ec494 27 myled = 0;
simon 0:333b2c3ec494 28 wait(0.2);
simon 0:333b2c3ec494 29 }
simon 0:333b2c3ec494 30 }