2014-12-22 12:35:05 +01:00
|
|
|
// Common code for all backends
|
|
|
|
|
|
|
|
#include "platform.h"
|
|
|
|
#include "common.h"
|
2019-07-21 23:58:21 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
2014-12-22 12:35:05 +01:00
|
|
|
|
2015-01-05 03:09:51 +01:00
|
|
|
void cmn_platform_init(void)
|
2014-12-22 12:35:05 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// ****************************************************************************
|
|
|
|
// Internal flash support functions
|
|
|
|
|
|
|
|
// This symbol must be exported by the linker command file and must reflect the
|
|
|
|
// TOTAL size of flash used by the eLua image (not only the code and constants,
|
|
|
|
// but also .data and whatever else ends up in the eLua image). FS will start
|
Add New Tasking I/F and rework GPIO, UART, etc to support it
As with the last commit this rolls up the follwowing, but include the various
review comments on the PR.
- **Documentation changes**. I've added the taks FAQ as a stub new Extension
developer FAQ, and split the old FAQ into a Lua Developer FAQ and a Hardware
FAQ.
- **Tasking I/F**. New `app/task/Makefile`, `app/task/task.c`,
`app/include/task/task.h` and `app/Makefile` as per previous commit. Cascade
changes to `app/driver/uart.c`, `app/include/driver/uart.h`,
`app/user/user_main.c` and `app/modules/node.c`
- **GPIO Rework** to `app/modules/gpio.c` and `pin_map.[hc]`, `platform.[hc]`
in `app/platform`
- **Other Optimisations** Move the `platform_*_exists()` from
`app/platform/common.c` to static inline declarations in `platform.h` as
this generates faster, smaller code. Move lgc.a routines out of iram0.
2016-02-17 18:13:17 +01:00
|
|
|
// at the next usable (aligned to a flash sector boundary) address after
|
2014-12-22 12:35:05 +01:00
|
|
|
// flash_used_size.
|
|
|
|
|
|
|
|
// extern char flash_used_size[];
|
|
|
|
extern char _flash_used_end[];
|
|
|
|
|
|
|
|
// Helper function: find the flash sector in which an address resides
|
|
|
|
// Return the sector number, as well as the start and end address of the sector
|
2018-06-22 23:29:16 +02:00
|
|
|
static uint32_t flash_find_sector( uint32_t address, uint32_t *pstart, uint32_t *pend )
|
2014-12-22 12:35:05 +01:00
|
|
|
{
|
|
|
|
#ifdef INTERNAL_FLASH_SECTOR_SIZE
|
|
|
|
// All the sectors in the flash have the same size, so just align the address
|
|
|
|
uint32_t sect_id = address / INTERNAL_FLASH_SECTOR_SIZE;
|
|
|
|
|
|
|
|
if( pstart )
|
2015-12-03 02:03:22 +01:00
|
|
|
*pstart = sect_id * INTERNAL_FLASH_SECTOR_SIZE ;
|
2014-12-22 12:35:05 +01:00
|
|
|
if( pend )
|
2015-12-03 02:03:22 +01:00
|
|
|
*pend = ( sect_id + 1 ) * INTERNAL_FLASH_SECTOR_SIZE - 1;
|
2014-12-22 12:35:05 +01:00
|
|
|
return sect_id;
|
|
|
|
#else // #ifdef INTERNAL_FLASH_SECTOR_SIZE
|
|
|
|
// The flash has blocks of different size
|
|
|
|
// Their size is decribed in the INTERNAL_FLASH_SECTOR_ARRAY macro
|
|
|
|
const uint32_t flash_sect_size[] = INTERNAL_FLASH_SECTOR_ARRAY;
|
|
|
|
uint32_t total = 0, i = 0;
|
|
|
|
|
|
|
|
while( ( total <= address ) && ( i < sizeof( flash_sect_size ) / sizeof( uint32_t ) ) )
|
|
|
|
total += flash_sect_size[ i ++ ];
|
|
|
|
if( pstart )
|
2015-12-03 02:03:22 +01:00
|
|
|
*pstart = ( total - flash_sect_size[ i - 1 ] );
|
2014-12-22 12:35:05 +01:00
|
|
|
if( pend )
|
2015-12-03 02:03:22 +01:00
|
|
|
*pend = total - 1;
|
2014-12-22 12:35:05 +01:00
|
|
|
return i - 1;
|
|
|
|
#endif // #ifdef INTERNAL_FLASH_SECTOR_SIZE
|
|
|
|
}
|
|
|
|
|
2015-01-05 03:09:51 +01:00
|
|
|
uint32_t platform_flash_get_sector_of_address( uint32_t addr )
|
2014-12-22 12:35:05 +01:00
|
|
|
{
|
2018-06-22 23:29:16 +02:00
|
|
|
return flash_find_sector( addr, NULL, NULL );
|
2014-12-22 12:35:05 +01:00
|
|
|
}
|
|
|
|
|
2015-01-05 03:09:51 +01:00
|
|
|
uint32_t platform_flash_get_num_sectors(void)
|
2014-12-22 12:35:05 +01:00
|
|
|
{
|
|
|
|
#ifdef INTERNAL_FLASH_SECTOR_SIZE
|
|
|
|
return INTERNAL_FLASH_SIZE / INTERNAL_FLASH_SECTOR_SIZE;
|
|
|
|
#else // #ifdef INTERNAL_FLASH_SECTOR_SIZE
|
|
|
|
const uint32_t flash_sect_size[] = INTERNAL_FLASH_SECTOR_ARRAY;
|
|
|
|
|
|
|
|
return sizeof( flash_sect_size ) / sizeof( uint32_t );
|
|
|
|
#endif // #ifdef INTERNAL_FLASH_SECTOR_SIZE
|
|
|
|
}
|
|
|
|
|
2015-01-05 03:09:51 +01:00
|
|
|
uint32_t platform_flash_get_first_free_block_address( uint32_t *psect )
|
2014-12-22 12:35:05 +01:00
|
|
|
{
|
|
|
|
// Round the total used flash size to the closest flash block address
|
|
|
|
uint32_t start, end, sect;
|
|
|
|
NODE_DBG("_flash_used_end:%08x\n", (uint32_t)_flash_used_end);
|
2018-03-08 01:20:59 +01:00
|
|
|
if(_flash_used_end>0){ // find the used sector
|
2018-06-22 23:29:16 +02:00
|
|
|
sect = flash_find_sector( platform_flash_mapped2phys ( (uint32_t)_flash_used_end - 1), NULL, &end );
|
|
|
|
if( psect )
|
|
|
|
*psect = sect + 1;
|
|
|
|
return end + 1;
|
|
|
|
} else {
|
|
|
|
sect = flash_find_sector( 0, &start, NULL ); // find the first free sector
|
|
|
|
if( psect )
|
|
|
|
*psect = sect;
|
|
|
|
return start;
|
2018-03-08 01:20:59 +01:00
|
|
|
}
|
2014-12-22 12:35:05 +01:00
|
|
|
}
|
|
|
|
|
2015-01-05 03:09:51 +01:00
|
|
|
uint32_t platform_flash_write( const void *from, uint32_t toaddr, uint32_t size )
|
2014-12-22 12:35:05 +01:00
|
|
|
{
|
|
|
|
#ifndef INTERNAL_FLASH_WRITE_UNIT_SIZE
|
|
|
|
return platform_s_flash_write( from, toaddr, size );
|
|
|
|
#else // #ifindef INTERNAL_FLASH_WRITE_UNIT_SIZE
|
|
|
|
uint32_t temp, rest, ssize = size;
|
|
|
|
unsigned i;
|
|
|
|
char tmpdata[ INTERNAL_FLASH_WRITE_UNIT_SIZE ];
|
|
|
|
const uint8_t *pfrom = ( const uint8_t* )from;
|
|
|
|
const uint32_t blksize = INTERNAL_FLASH_WRITE_UNIT_SIZE;
|
|
|
|
const uint32_t blkmask = INTERNAL_FLASH_WRITE_UNIT_SIZE - 1;
|
|
|
|
|
|
|
|
// Align the start
|
|
|
|
if( toaddr & blkmask )
|
|
|
|
{
|
|
|
|
rest = toaddr & blkmask;
|
|
|
|
temp = toaddr & ~blkmask; // this is the actual aligned address
|
2019-07-21 23:58:21 +02:00
|
|
|
// memcpy( tmpdata, ( const void* )temp, blksize );
|
2014-12-22 12:35:05 +01:00
|
|
|
platform_s_flash_read( tmpdata, temp, blksize );
|
|
|
|
for( i = rest; size && ( i < blksize ); i ++, size --, pfrom ++ )
|
|
|
|
tmpdata[ i ] = *pfrom;
|
|
|
|
platform_s_flash_write( tmpdata, temp, blksize );
|
|
|
|
if( size == 0 )
|
|
|
|
return ssize;
|
|
|
|
toaddr = temp + blksize;
|
|
|
|
}
|
|
|
|
// The start address is now a multiple of blksize
|
|
|
|
// Compute how many bytes we can write as multiples of blksize
|
|
|
|
rest = size & blkmask;
|
|
|
|
temp = size & ~blkmask;
|
|
|
|
// Program the blocks now
|
|
|
|
if( temp )
|
|
|
|
{
|
|
|
|
platform_s_flash_write( pfrom, toaddr, temp );
|
|
|
|
toaddr += temp;
|
|
|
|
pfrom += temp;
|
|
|
|
}
|
|
|
|
// And the final part of a block if needed
|
|
|
|
if( rest )
|
|
|
|
{
|
2019-07-21 23:58:21 +02:00
|
|
|
// memcpy( tmpdata, ( const void* )toaddr, blksize );
|
2014-12-22 12:35:05 +01:00
|
|
|
platform_s_flash_read( tmpdata, toaddr, blksize );
|
|
|
|
for( i = 0; size && ( i < rest ); i ++, size --, pfrom ++ )
|
|
|
|
tmpdata[ i ] = *pfrom;
|
|
|
|
platform_s_flash_write( tmpdata, toaddr, blksize );
|
|
|
|
}
|
|
|
|
return ssize;
|
|
|
|
#endif // #ifndef INTERNAL_FLASH_WRITE_UNIT_SIZE
|
|
|
|
}
|
|
|
|
|
2015-01-05 03:09:51 +01:00
|
|
|
uint32_t platform_flash_read( void *to, uint32_t fromaddr, uint32_t size )
|
2014-12-22 12:35:05 +01:00
|
|
|
{
|
|
|
|
#ifndef INTERNAL_FLASH_READ_UNIT_SIZE
|
|
|
|
return platform_s_flash_read( to, fromaddr, size );
|
|
|
|
#else // #ifindef INTERNAL_FLASH_READ_UNIT_SIZE
|
|
|
|
uint32_t temp, rest, ssize = size;
|
|
|
|
unsigned i;
|
2015-08-04 04:59:59 +02:00
|
|
|
char tmpdata[ INTERNAL_FLASH_READ_UNIT_SIZE ] __attribute__ ((aligned(INTERNAL_FLASH_READ_UNIT_SIZE)));
|
2014-12-22 12:35:05 +01:00
|
|
|
uint8_t *pto = ( uint8_t* )to;
|
|
|
|
const uint32_t blksize = INTERNAL_FLASH_READ_UNIT_SIZE;
|
|
|
|
const uint32_t blkmask = INTERNAL_FLASH_READ_UNIT_SIZE - 1;
|
|
|
|
|
|
|
|
// Align the start
|
|
|
|
if( fromaddr & blkmask )
|
|
|
|
{
|
|
|
|
rest = fromaddr & blkmask;
|
|
|
|
temp = fromaddr & ~blkmask; // this is the actual aligned address
|
|
|
|
platform_s_flash_read( tmpdata, temp, blksize );
|
|
|
|
for( i = rest; size && ( i < blksize ); i ++, size --, pto ++ )
|
|
|
|
*pto = tmpdata[ i ];
|
Add New Tasking I/F and rework GPIO, UART, etc to support it
As with the last commit this rolls up the follwowing, but include the various
review comments on the PR.
- **Documentation changes**. I've added the taks FAQ as a stub new Extension
developer FAQ, and split the old FAQ into a Lua Developer FAQ and a Hardware
FAQ.
- **Tasking I/F**. New `app/task/Makefile`, `app/task/task.c`,
`app/include/task/task.h` and `app/Makefile` as per previous commit. Cascade
changes to `app/driver/uart.c`, `app/include/driver/uart.h`,
`app/user/user_main.c` and `app/modules/node.c`
- **GPIO Rework** to `app/modules/gpio.c` and `pin_map.[hc]`, `platform.[hc]`
in `app/platform`
- **Other Optimisations** Move the `platform_*_exists()` from
`app/platform/common.c` to static inline declarations in `platform.h` as
this generates faster, smaller code. Move lgc.a routines out of iram0.
2016-02-17 18:13:17 +01:00
|
|
|
|
2014-12-22 12:35:05 +01:00
|
|
|
if( size == 0 )
|
|
|
|
return ssize;
|
|
|
|
fromaddr = temp + blksize;
|
|
|
|
}
|
|
|
|
// The start address is now a multiple of blksize
|
|
|
|
// Compute how many bytes we can read as multiples of blksize
|
|
|
|
rest = size & blkmask;
|
|
|
|
temp = size & ~blkmask;
|
|
|
|
// Program the blocks now
|
|
|
|
if( temp )
|
|
|
|
{
|
|
|
|
platform_s_flash_read( pto, fromaddr, temp );
|
|
|
|
fromaddr += temp;
|
|
|
|
pto += temp;
|
|
|
|
}
|
|
|
|
// And the final part of a block if needed
|
|
|
|
if( rest )
|
|
|
|
{
|
|
|
|
platform_s_flash_read( tmpdata, fromaddr, blksize );
|
|
|
|
for( i = 0; size && ( i < rest ); i ++, size --, pto ++ )
|
|
|
|
*pto = tmpdata[ i ];
|
|
|
|
}
|
|
|
|
return ssize;
|
|
|
|
#endif // #ifndef INTERNAL_FLASH_READ_UNIT_SIZE
|
|
|
|
}
|