Factor irq methods

Now methods that need interrupts turned off are wrapped.
Make genbackupisr.py executable.
New resources for bootloader-related information.
This commit is contained in:
Ralf Schlatterbeck 2017-08-20 11:47:13 +02:00
parent 6eced34422
commit 954da749eb
6 changed files with 151 additions and 32 deletions

0
examples/osd/ota-update/genbackupisr.py Normal file → Executable file
View file

View file

@ -54,7 +54,7 @@
* The build system automatically compiles the resources in the
* corresponding sub-directory.
*/
static size_t
part_count (const char *name, const char *uri, char *buf, size_t bsize)
{
@ -85,3 +85,66 @@ GENERIC_RESOURCE
, part_size
);
static size_t
get_boot_default (const char *name, const char *uri, char *buf, size_t bsize)
{
return snprintf (buf, bsize, "%ld", bootloader_get_boot_default ());
}
static int
set_boot_default (const char *name, const char *uri, const char *s)
{
uint32_t tmp = strtoul (s, NULL, 10);
bootloader_set_boot_default (tmp);
return 0;
}
GENERIC_RESOURCE
( boot_default
, Default boot partition
, count
, 0
, set_boot_default
, get_boot_default
);
static size_t
get_boot_next (const char *name, const char *uri, char *buf, size_t bsize)
{
return snprintf (buf, bsize, "%ld", bootloader_get_boot_next ());
}
static int
set_boot_next (const char *name, const char *uri, const char *s)
{
uint32_t tmp = strtoul (s, NULL, 10);
bootloader_set_boot_next (tmp);
return 0;
}
GENERIC_RESOURCE
( boot_next
, Next boot partition
, count
, 0
, set_boot_next
, get_boot_next
);
static size_t
get_callers_part (const char *name, const char *uri, char *buf, size_t bsize)
{
return snprintf (buf, bsize, "%ld", bootloader_get_callers_part ());
}
GENERIC_RESOURCE
( callers_part
, Currently booted partition
, count
, 0
, NULL
, get_callers_part
);
// FIXME: Find out how to pass two parameters, for set/clr_part_ok and
// for get_part_start

View file

@ -70,7 +70,6 @@ res_put_handler(void *request, void *response, uint8_t *buffer, uint16_t preferr
coap_packet_t *const packet = (coap_packet_t *)request;
uint8_t *in_data = NULL;
size_t len = 0;
uint8_t sreg = SREG;
const uint32_t partition_start = bootloader_get_part_start (1);
const uint32_t partition_size = bootloader_get_part_size ();
@ -154,28 +153,19 @@ res_put_handler(void *request, void *response, uint8_t *buffer, uint16_t preferr
if (current_offset % PAGESIZE == 0) {
uint32_t dst_address = partition_start + current_offset - PAGESIZE;
PRINTF (("Flashing: %lu to %lu\n", (uint32_t)PAGESIZE, dst_address));
sreg = SREG;
cli ();
bootloader_write_page_to_flash (dst_address, PAGESIZE, current_page);
SREG = sreg;
} else if (!packet->block1_more) {
uint32_t dst_address =
partition_start + (current_offset / PAGESIZE) * PAGESIZE;
PRINTF (("Flashing: last page %lu to %lu\n", (uint32_t)PAGESIZE, dst_address));
sreg = SREG;
cli ();
bootloader_write_page_to_flash (dst_address, PAGESIZE, current_page);
SREG = sreg;
}
if (!packet->block1_more) {
// we are finished
sreg = SREG;
cli ();
bootloader_backup_irq_table (1); // FIXME: 1 is hardcoded
bootloader_set_boot_next (1);
SREG = sreg;
}
REST.set_response_status(response, REST.status.CHANGED);

View file

@ -11,7 +11,8 @@ extern "C" {
#include "contiki.h"
#include "contiki-net.h"
#include "er-coap.h"
extern resource_t res_upload_image;
#include "bootloader_if.h"
#include "resources.h"
char resname[] = "update";
}
@ -20,9 +21,20 @@ void setup (void)
{
rest_init_engine ();
rest_activate_resource (&res_upload_image, resname);
rest_activate_resource (&res_part_count, (char *)"part_count");
rest_activate_resource (&res_part_size, (char *)"part_size");
rest_activate_resource (&res_boot_default, (char *)"boot_default");
rest_activate_resource (&res_boot_next, (char *)"boot_next");
rest_activate_resource (&res_callers_part, (char *)"callers_part");
}
void loop (void)
{
//printf ("Huhu\n");
printf ("part-count: %ld\n", bootloader_get_part_count ());
printf ("part-size: %ld\n", bootloader_get_part_size ());
printf ("part-start 0: 0x%lx\n", bootloader_get_part_start (0));
printf ("part-start 1: 0x%lx\n", bootloader_get_part_start (1));
printf ("boot-default: %ld\n", bootloader_get_boot_default ());
printf ("boot-next: %ld\n", bootloader_get_boot_next ());
printf ("callers-part: %ld\n", bootloader_get_callers_part ());
}

View file

@ -100,15 +100,15 @@ TEXT_SECTION_START=0x1ef00
endif
LDFLAGS += -Wl,--defsym,bootloader_get_mac=$(BOOTLOADER_GET_MAC) \
-Wl,--defsym,bootloader_write_page_to_flash=$(BOOTLOADER_WRITE_PAGE_TO_FLASH) \
-Wl,--defsym,_bootloader_write_page_to_flash=$(BOOTLOADER_WRITE_PAGE_TO_FLASH) \
-Wl,--defsym,bootloader_get_part_count=$(BOOTLOADER_GET_PART_COUNT) \
-Wl,--defsym,bootloader_get_part_size=$(BOOTLOADER_GET_PART_SIZE) \
-Wl,--defsym,bootloader_get_part_start=$(BOOTLOADER_GET_PART_START) \
-Wl,--defsym,bootloader_set_part_ok=$(BOOTLOADER_SET_PART_OK) \
-Wl,--defsym,bootloader_clr_part_ok=$(BOOTLOADER_CLR_PART_OK) \
-Wl,--defsym,bootloader_set_boot_default=$(BOOTLOADER_SET_BOOT_DEFAULT) \
-Wl,--defsym,bootloader_set_boot_next=$(BOOTLOADER_SET_BOOT_NEXT) \
-Wl,--defsym,bootloader_backup_irq_table=$(BOOTLOADER_BACKUP_IRQ_TABLE) \
-Wl,--defsym,_bootloader_set_part_ok=$(BOOTLOADER_SET_PART_OK) \
-Wl,--defsym,_bootloader_clr_part_ok=$(BOOTLOADER_CLR_PART_OK) \
-Wl,--defsym,_bootloader_set_boot_default=$(BOOTLOADER_SET_BOOT_DEFAULT) \
-Wl,--defsym,_bootloader_set_boot_next=$(BOOTLOADER_SET_BOOT_NEXT) \
-Wl,--defsym,_bootloader_backup_irq_table=$(BOOTLOADER_BACKUP_IRQ_TABLE) \
-Wl,--defsym,bootloader_get_boot_default=$(BOOTLOADER_GET_BOOT_DEFAULT) \
-Wl,--defsym,bootloader_get_boot_next=$(BOOTLOADER_GET_BOOT_NEXT) \
-Wl,--defsym,bootloader_get_callers_part=$(BOOTLOADER_GET_CALLERS_PART) \

View file

@ -1,20 +1,74 @@
#ifndef BOOTLOADER_IF_H_
#define BOOTLOADER_IF_H_
extern int bootloader_write_page_to_flash
extern uint8_t bootloader_get_mac (uint8_t);
extern uint32_t bootloader_get_part_count (void);
extern uint32_t bootloader_get_part_size (void);
extern uint32_t bootloader_get_part_start (uint32_t part_index);
extern uint32_t bootloader_get_boot_default (void);
extern uint32_t bootloader_get_boot_next (void);
extern uint32_t bootloader_get_callers_part (void);
/* These write to flash and need to turn off interrupts before start */
extern void _bootloader_set_part_ok (uint32_t part_index);
extern void _bootloader_clr_part_ok (uint32_t part_index);
extern void _bootloader_set_boot_default (uint32_t part_index);
extern void _bootloader_set_boot_next (uint32_t part_index);
extern void _bootloader_backup_irq_table (uint32_t part_index);
extern int _bootloader_write_page_to_flash
(uint32_t address, unsigned int size, unsigned char *p);
extern uint8_t bootloader_get_mac (uint8_t);
extern uint32_t bootloader_get_part_count (void);
extern uint32_t bootloader_get_part_size (void);
extern uint32_t bootloader_get_part_start (uint32_t part_index);
extern void bootloader_set_part_ok (uint32_t part_index);
extern void bootloader_clr_part_ok (uint32_t part_index);
extern void bootloader_set_boot_default (uint32_t part_index);
extern void bootloader_set_boot_next (uint32_t part_index);
extern void bootloader_backup_irq_table (uint32_t part_index);
extern uint32_t bootloader_get_boot_default (void);
extern uint32_t bootloader_get_boot_next (void);
extern uint32_t bootloader_get_callers_part (void);
/* Wrap the routines that write to flash (see above) */
static inline void bootloader_set_part_ok (uint32_t part_index)
{
uint8_t sreg = SREG;
cli ();
_bootloader_set_part_ok (part_index);
SREG = sreg;
}
static inline void bootloader_clr_part_ok (uint32_t part_index)
{
uint8_t sreg = SREG;
cli ();
_bootloader_clr_part_ok (part_index);
SREG = sreg;
}
static inline void bootloader_set_boot_default (uint32_t part_index)
{
uint8_t sreg = SREG;
cli ();
_bootloader_set_boot_default (part_index);
SREG = sreg;
}
static inline void bootloader_set_boot_next (uint32_t part_index)
{
uint8_t sreg = SREG;
cli ();
_bootloader_set_boot_next (part_index);
SREG = sreg;
}
static inline void bootloader_backup_irq_table (uint32_t part_index)
{
uint8_t sreg = SREG;
cli ();
_bootloader_backup_irq_table (part_index);
SREG = sreg;
}
static inline int bootloader_write_page_to_flash
(uint32_t address, unsigned int size, unsigned char *p)
{
int ret = 0;
uint8_t sreg = SREG;
cli ();
ret = _bootloader_write_page_to_flash (address, size, p);
SREG = sreg;
return ret;
}
#endif /* BOOTLOADER_IF_H_ */