eCos flash driver.
[mw/openocd-lm32.git] / ecosflash / flash.c
1 #include <string.h>
2 #define _FLASH_PRIVATE_
3 #include <cyg/io/flash.h>
4
5
6
7 int myprintf(char *format, ...)
8 {
9   return 0;
10 }
11
12 extern char _start_bss_clear;
13 extern char __bss_end__;
14
15 int init()
16 {
17         // set up runtime environment
18         char *t;
19         for (t=&_start_bss_clear; t<&__bss_end__; t++)
20         {
21                 *t=0;
22         }
23         return flash_init((_printf *)&myprintf);
24         
25 }
26
27
28 int checkFlash(void *addr, int len)
29 {
30     // Return error for illegal addresses
31     if ((addr<flash_info.start)||(addr>flash_info.end))
32         return FLASH_ERR_INVALID;
33     if ((((cyg_uint8 *)addr)+len)>(cyg_uint8 *)flash_info.end)
34         return FLASH_ERR_INVALID;
35     return FLASH_ERR_OK;
36 }
37
38
39 int erase(void *address, int len)       
40 {
41         int retval;
42         void *failAddress;
43         
44         retval=checkFlash(address, len);
45         if (retval!=0)
46                 return retval;
47         
48         retval=init();
49         if (retval!=0)
50                 return retval;
51         return flash_erase(address, len, &failAddress);
52 }
53
54
55
56 extern char _end;
57
58 // Data follows immediately after program, long word aligned.
59 int program(void *buffer, void *address, int len)       
60 {
61         int retval;
62         void *failAddress;
63         retval=checkFlash(address, len);
64         if (retval!=0)
65                 return retval;
66         
67         retval=init();
68         if (retval!=0)
69                 return retval;
70         //int flash_program(void *_addr, void *_data, int len, void **err_addr)
71         return flash_program(address, buffer, len, &failAddress);
72 }