forked from alexforencich/xboot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flash.h
121 lines (98 loc) · 4.45 KB
/
flash.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/************************************************************************/
/* Generic ATMEL Flash Driver */
/* */
/* flash.h */
/* */
/* Alex Forencich <[email protected]> */
/* */
/* Copyright (c) 2011 Alex Forencich */
/* */
/* Permission is hereby granted, free of charge, to any person */
/* obtaining a copy of this software and associated documentation */
/* files(the "Software"), to deal in the Software without restriction, */
/* including without limitation the rights to use, copy, modify, merge, */
/* publish, distribute, sublicense, and/or sell copies of the Software, */
/* and to permit persons to whom the Software is furnished to do so, */
/* subject to the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */
/* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS */
/* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN */
/* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN */
/* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */
/* SOFTWARE. */
/* */
/************************************************************************/
#ifndef __FLASH_H
#define __FLASH_H
#include <avr/io.h>
#include <avr/interrupt.h>
#ifdef __AVR_XMEGA__
#include "sp_driver.h"
#else // __AVR_XMEGA__
#include <avr/boot.h>
#endif // __AVR_XMEGA__
#include "xboot.h"
// offsets and addresses
#ifndef PROGMEM_SIZE
#define PROGMEM_SIZE (FLASHEND + 1UL)
#endif
#ifndef BOOT_SECTION_SIZE
#error BOOT_SECTION_SIZE not defined!
#endif
#ifndef BOOT_SECTION_START
#define BOOT_SECTION_START (PROGMEM_SIZE - BOOT_SECTION_SIZE)
#endif
#ifndef APP_SECTION_START
#define APP_SECTION_START 0
#endif
#ifndef APP_SECTION_SIZE
#define APP_SECTION_SIZE (PROGMEM_SIZE - BOOT_SECTION_SIZE)
#endif
#ifndef APP_SECTION_END
#define APP_SECTION_END (APP_SECTION_START + APP_SECTION_SIZE - 1UL)
#endif
#if PROGMEM_SIZE > 0x010000
#define PGM_READ_BYTE pgm_read_byte_far
#define PGM_READ_WORD pgm_read_word_far
#define PGM_READ_DWORD pgm_read_dword_far
#else
#define PGM_READ_BYTE pgm_read_byte_near
#define PGM_READ_WORD pgm_read_word_near
#define PGM_READ_DWORD pgm_read_dword_near
#endif
#ifdef __AVR_XMEGA__
// XMega functions
// (sp_driver wrapper)
#define Flash_ReadByte SP_ReadByte
#define Flash_ReadWord SP_ReadWord
#define Flash_LoadFlashWord SP_LoadFlashWord
#define Flash_EraseApplicationSection SP_EraseApplicationSection
#define Flash_EraseApplicationPage SP_EraseApplicationPage
#define Flash_EraseWriteApplicationPage SP_EraseWriteApplicationPage
#define Flash_WriteApplicationPage SP_WriteApplicationPage
#define Flash_EraseUserSignatureRow SP_EraseUserSignatureRow
#define Flash_WriteUserSignatureRow SP_WriteUserSignatureRow
#define Flash_LoadFlashPage SP_LoadFlashPage
#define Flash_ReadFlashPage SP_ReadFlashPage
#define Flash_WaitForSPM SP_WaitForSPM
#else
// ATMega Functions
#define Flash_ReadByte PGM_READ_BYTE
#define Flash_ReadWord PGM_READ_WORD
#define Flash_LoadFlashWord boot_page_fill
void Flash_EraseApplicationSection(void);
#define Flash_EraseApplicationPage boot_page_erase
void Flash_EraseWriteApplicationPage(uint32_t addr);
#define Flash_WriteApplicationPage boot_page_write
void Flash_LoadFlashPage(uint8_t *data);
void Flash_ReadFlashPage(uint8_t *data, uint32_t addr);
#define Flash_WaitForSPM boot_spm_busy_wait
#endif // __AVR_XMEGA__
void Flash_ProgramPage(uint32_t page, uint8_t *buf, uint8_t erase);
#endif // __FLASH_H