px-fwlib 0.10.0
Cross-platform embedded library and documentation for 8/16/32-bit microcontrollers generated with Doxygen 1.9.2
px_flash.c
1/* =============================================================================
2 ____ ___ ____ ___ _ _ ___ __ __ ___ __ __ TM
3 | _ \ |_ _| / ___| / _ \ | \ | | / _ \ | \/ | |_ _| \ \/ /
4 | |_) | | | | | | | | | | \| | | | | | | |\/| | | | \ /
5 | __/ | | | |___ | |_| | | |\ | | |_| | | | | | | | / \
6 |_| |___| \____| \___/ |_| \_| \___/ |_| |_| |___| /_/\_\
7
8 Copyright (c) 2006 Pieter Conradie <https://piconomix.com>
9
10 License: MIT
11 https://github.com/piconomix/px-fwlib/blob/master/LICENSE.md
12
13 Title: px_flash.h : Internal FLASH write routines
14 Author(s): Pieter Conradie
15 Creation Date: 2007-03-31
16
17============================================================================= */
18
19/* _____STANDARD INCLUDES____________________________________________________ */
20#include <avr/boot.h>
21#include <avr/interrupt.h>
22
23/* _____PROJECT INCLUDES_____________________________________________________ */
24#include "px_flash.h"
25
26/* _____LOCAL DEFINITIONS____________________________________________________ */
27
28/* _____MACROS_______________________________________________________________ */
29
30/* _____GLOBAL VARIABLES_____________________________________________________ */
31
32/* _____LOCAL VARIABLES______________________________________________________ */
33
34/* _____LOCAL FUNCTION DECLARATIONS__________________________________________ */
35
36/* _____LOCAL FUNCTIONS______________________________________________________ */
37
38/* _____GLOBAL FUNCTIONS_____________________________________________________ */
39BOOTLOADER_SECTION void px_flash_erase_page(const uint16_t page)
40{
41 // Calculate 32-bit address from specified page
42 uint32_t adr = ((uint32_t)page) * PX_FLASH_PAGE_SIZE;
43
44 // Save status of global interrupts
45 uint8_t sreg = SREG;
46 // Disable interrupts
47 cli();
48
49 // Erase page
50 boot_page_erase(adr);
51 // Wait until SPM command has finished
52 boot_spm_busy_wait();
53 // Re-enable RWW-section
54 boot_rww_enable();
55
56 // Restore status of global interrupts
57 SREG = sreg;
58}
59
60BOOTLOADER_SECTION void px_flash_wr_page(const uint16_t page, const uint8_t * data)
61{
62 uint16_t i;
63
64 // Calculate 32-bit address from specified page
65 uint32_t adr = ((uint32_t)page) * PX_FLASH_PAGE_SIZE;
66
67 // Save status of global interrupts
68 uint8_t sreg = SREG;
69 // Disable interrupts
70 cli();
71
72 // Clear the page first
74
75 // Fill write data with 16-bit data
76 for(i = 0; i < PX_FLASH_PAGE_SIZE; i += 2)
77 {
78 // Fetch little endian word of data
79 uint16_t word;
80 word = *data++;
81 word += (*data++) << 8;
82
83 boot_page_fill(adr+i, word);
84 }
85
86 // Commit data to flash page
87 boot_page_write(adr);
88 // Wait until SPM command has finished
89 boot_spm_busy_wait();
90 // Re-enable RWW-section
91 boot_rww_enable();
92
93 // Restore status of global interrupts
94 SREG = sreg;
95}
96
BOOTLOADER_SECTION void px_flash_wr_page(const uint16_t page, const uint8_t *data)
Function to write a page.
Definition: px_flash.c:60
#define PX_FLASH_PAGE_SIZE
FLASH Page size in bytes.
Definition: px_flash.h:56
BOOTLOADER_SECTION void px_flash_erase_page(const uint16_t page)
Function to erase a page.
Definition: px_flash.c:39