GlobLib
HAL and API libraries for MCUs and hardware.
FlashWrite

Functions for converting data types (float, uint16_t..) to byte streams for usage with EEPROM memory. More...

Files

file  flashWrite.h
 Header file for flashWrite library.
 

Macros

#define flash_put(DATA, ADDRESS)
 Flash a given datatype to the output stream.
 
#define flash_get(DATA, ADDRESS)
 Read a given datatype from the input stream.
 
#define MAX_DEC   4
 Maximum number of decimal places a float will contain.
 

Typedefs

typedef void(* v_fp_u8_u16) (uint8_t, uint16_t)
 Function pointer typedef for void function with uint8_t parameter.
 

Functions

void FLASHWRITE_setOutput (void(*out)(uint8_t, uint16_t))
 Set the target output stream for flash_put functions. More...
 
void FLASHWRITE_setInput (uint8_t(*in)(uint16_t))
 Set the target input stream for flash_get functions. More...
 
v_fp_u8_u16 FLASHWRITE_getOutput (void)
 Get the current target output stream. More...
 
u8_fp_u16 FLASHWRITE_getInput (void)
 Get the current target input stream. More...
 
uint16_t FLASHWRITE_write_u8 (uint8_t data, uint16_t address)
 Write a unsigned 8 bit integer to address given. More...
 
uint16_t FLASHWRITE_read_u8 (uint8_t *data, uint16_t address)
 Read a unsigned 8 bit integer from address given. More...
 
uint16_t FLASHWRITE_write_8 (int8_t data, uint16_t address)
 Write a signed 8 bit integer to address given. More...
 
uint16_t FLASHWRITE_read_8 (int8_t *data, uint16_t address)
 Read a signed 8 bit integer from address given. More...
 
uint16_t FLASHWRITE_write_c (char data, uint16_t address)
 Write a char to address given. More...
 
uint16_t FLASHWRITE_read_c (char *data, uint16_t address)
 Read a char from address give. More...
 
uint16_t FLASHWRITE_write_u16 (uint16_t data, uint16_t address)
 Write a unsigned 16 bit integer to address given. More...
 
uint16_t FLASHWRITE_read_u16 (uint16_t *data, uint16_t address)
 Read a unsigned 16 bit integer from address give. More...
 
uint16_t FLASHWRITE_write_16 (int16_t data, uint16_t address)
 Write a signed 16 bit integer to address give. More...
 
uint16_t FLASHWRITE_read_16 (int16_t *data, uint16_t address)
 Read a signed 16 bit integer from address give. More...
 
uint16_t FLASHWRITE_write_u32 (uint32_t data, uint16_t address)
 Write a unsigned 32 bit integer to address given. More...
 
uint16_t FLASHWRITE_read_u32 (uint32_t *data, uint16_t address)
 Read a unsigned 32 bit integer from address given. More...
 
uint16_t FLASHWRITE_write_32 (int32_t data, uint16_t address)
 Write a signed 32 bit integer to address given. More...
 
uint16_t FLASHWRITE_read_32 (int32_t *data, uint16_t address)
 Read a signed 32 bit integer from address given. More...
 
uint16_t FLASHWRITE_write_float (float data, uint16_t address)
 Write a float to address given. More...
 
uint16_t FLASHWRITE_read_float (float *data, uint16_t address)
 Read a float from address given. More...
 

Detailed Description

Functions for converting data types (float, uint16_t..) to byte streams for usage with EEPROM memory.

This module is compatable with version C11 of C standard. It implements the _Generic keyword so common flash_put() and flash_get() functions can be used to read and write flash data from multiple data types.

Two main functions exist for usage:

Current limitations

Author
Stuart Ianna
Version
0.1
Date
December 2018
Warning
Both the read and write function do not implement a delay between byte read/write operations. Many EEPROM require a set time to complete these operations (~10ms). A delay or should be used in the main program.
Bug:
None
Todo:
Verified Compilers
  • arm-none-eabi-gcc (15:4.9.3+svn231177-1) 4.9.3 20150529 (prerelease)
Example
#include <stm32f103cb_core.h>
#include <flashWrite.h>
//This example uses the STM32f1 internal flash memory.
//STM32f1 requires flash page address to write to
//See example_flash.h
#define FLASH_PAGE 120
//STM32f1 Flash input and output functions don't match requried flash format
//Use function wrappers to obtain the required types
void flashWrite(uint8_t byte, uint16_t address);
uint8_t flashRead(uint16_t address);
int main(void){
//Set the input and output function pointers
FLASHWRITE_setOutput(&flashWrite);
FLASHWRITE_setInput(&flashRead);
//Example variables
float ex_f;
char ex_c;
uint16_t ex_u16;
int32_t ex_32;
//Current memory address location
uint16_t addressLocation = 0;
ex_f = 12.34;
ex_c = 'A';
ex_u16 = 12345;
ex_32 = -235001;
//Put the float at memory address
//The new memory address is retured by flash put based on the size of the data written
addressLocation = flash_put(ex_f,addressLocation);
addressLocation = flash_put(ex_c,addressLocation);
addressLocation = flash_put(ex_u16,addressLocation);
addressLocation = flash_put(ex_32,addressLocation);
//Reset the address location and read data back
//Address is incremented similarly to flash_put
addressLocation = 0;
addressLocation = flash_get(&ex_f,addressLocation);
addressLocation = flash_get(&ex_c,addressLocation);
addressLocation = flash_get(&ex_u16,addressLocation);
addressLocation = flash_get(&ex_32,addressLocation);
//Done
while(1);
return 0;
}
//Function wrapper for flash write function
//This is called by FLASHWRITE API when byte write operations occur
void flashWrite(uint8_t byte, uint16_t address){
//A different method for writing to other flash EEPROM could be used here
FLASH_write(FLASH_PAGE,address,byte);
//Need to implement a delay here to allow for write cycles
delayms(10);
return;
}
//Function wrapper for flash read function
//This is called by FLAHSWRITE API when byte read operation occur
uint8_t flashRead(uint16_t address){
//A different method for reading to other flash EEPROM could be used here
return FLASH_read(FLASH_PAGE,address);
}

Function Documentation

u8_fp_u16 FLASHWRITE_getInput ( void  )

Get the current target input stream.

Returns
Function pointer to input stream or Null if not defined.

Definition at line 43 of file flashWrite.c.

v_fp_u8_u16 FLASHWRITE_getOutput ( void  )

Get the current target output stream.

Returns
Function pointer to output stream or Null if not defined.

Definition at line 34 of file flashWrite.c.

uint16_t FLASHWRITE_read_16 ( int16_t *  data,
uint16_t  address 
)

Read a signed 16 bit integer from address give.

This function is invoked by macro definition flash_put

Parameters
dataThe container to store the read data
addressThe address to read from
Returns
The memory address given plus one.

Definition at line 109 of file flashWrite.c.

uint16_t FLASHWRITE_read_32 ( int32_t *  data,
uint16_t  address 
)

Read a signed 32 bit integer from address given.

This function is invoked by macro definition flash_put

Parameters
dataThe container to store the read data
addressThe address to read from
Returns
The memory address given plus one.

Definition at line 142 of file flashWrite.c.

uint16_t FLASHWRITE_read_8 ( int8_t *  data,
uint16_t  address 
)

Read a signed 8 bit integer from address given.

This function is invoked by macro definition flash_put

Parameters
dataThe container to store the read data
addressThe address to read from
Returns
The memory address given plus one.

Definition at line 70 of file flashWrite.c.

uint16_t FLASHWRITE_read_c ( char *  data,
uint16_t  address 
)

Read a char from address give.

This function is invoked by macro definition flash_put

Parameters
dataThe container to store the read data
addressThe address to read from
Returns
The memory address given plus one.

Definition at line 82 of file flashWrite.c.

uint16_t FLASHWRITE_read_float ( float *  data,
uint16_t  address 
)

Read a float from address given.

This function is invoked by macro definition flash_put

Parameters
dataThe container to store the read data
addressThe address to read from
Returns
The memory address given plus one.

Definition at line 168 of file flashWrite.c.

uint16_t FLASHWRITE_read_u16 ( uint16_t *  data,
uint16_t  address 
)

Read a unsigned 16 bit integer from address give.

This function is invoked by macro definition flash_put

Parameters
dataThe container to store the read data
addressThe address to read from
Returns
The memory address given plus one.

Definition at line 95 of file flashWrite.c.

uint16_t FLASHWRITE_read_u32 ( uint32_t *  data,
uint16_t  address 
)

Read a unsigned 32 bit integer from address given.

This function is invoked by macro definition flash_put

Parameters
dataThe container to store the read data
addressThe address to read from
Returns
The memory address given plus one.

Definition at line 124 of file flashWrite.c.

uint16_t FLASHWRITE_read_u8 ( uint8_t *  data,
uint16_t  address 
)

Read a unsigned 8 bit integer from address given.

This function is invoked by macro definition flash_put

Parameters
dataThe container to store the read data
addressThe address to read from
Returns
The memory address given plus one.

Definition at line 58 of file flashWrite.c.

void FLASHWRITE_setInput ( uint8_t(*)(uint16_t)  in)

Set the target input stream for flash_get functions.

This function must be called before flash_get() will work.

Warning
Undefined behaviour will occur if flash_get() is used before the input function is set.
Parameters
inPointer to the input stream function.
Returns
none.

Definition at line 28 of file flashWrite.c.

void FLASHWRITE_setOutput ( void(*)(uint8_t, uint16_t)  out)

Set the target output stream for flash_put functions.

This function must be called before flash_put() will work

Warning
Undefined behaviour will occur if flash_put() is used before the output function is set.
Parameters
outPointer to the output stream function.
Returns
none.

Definition at line 22 of file flashWrite.c.

uint16_t FLASHWRITE_write_16 ( int16_t  data,
uint16_t  address 
)

Write a signed 16 bit integer to address give.

This function is invoked by macro definition flash_put

Parameters
dataThe data to be written
addressThe address to be written to
Returns
The memory address given plus one.

Definition at line 102 of file flashWrite.c.

uint16_t FLASHWRITE_write_32 ( int32_t  data,
uint16_t  address 
)

Write a signed 32 bit integer to address given.

This function is invoked by macro definition flash_put

Parameters
dataThe data to be written
addressThe address to be written to
Returns
The memory address given plus one.

Definition at line 133 of file flashWrite.c.

uint16_t FLASHWRITE_write_8 ( int8_t  data,
uint16_t  address 
)

Write a signed 8 bit integer to address given.

This function is invoked by macro definition flash_put

Parameters
dataThe data to be written
addressThe address to be written to
Returns
The memory address given plus one.

Definition at line 64 of file flashWrite.c.

uint16_t FLASHWRITE_write_c ( char  data,
uint16_t  address 
)

Write a char to address given.

This function is invoked by macro definition flash_put

Parameters
dataThe data to be written
addressThe address to be written to
Returns
The memory address given plus one.

Definition at line 76 of file flashWrite.c.

uint16_t FLASHWRITE_write_float ( float  data,
uint16_t  address 
)

Write a float to address given.

This function is invoked by macro definition flash_put

Parameters
dataThe data to be written
addressThe address to be written to
Returns
The memory address given plus one.

Definition at line 151 of file flashWrite.c.

uint16_t FLASHWRITE_write_u16 ( uint16_t  data,
uint16_t  address 
)

Write a unsigned 16 bit integer to address given.

This function is invoked by macro definition flash_put

Parameters
dataThe data to be written
addressThe address to be written to
Returns
The memory address given plus one.

Definition at line 88 of file flashWrite.c.

uint16_t FLASHWRITE_write_u32 ( uint32_t  data,
uint16_t  address 
)

Write a unsigned 32 bit integer to address given.

This function is invoked by macro definition flash_put

Parameters
dataThe data to be written
addressThe address to be written to
Returns
The memory address given plus one.

Definition at line 115 of file flashWrite.c.

uint16_t FLASHWRITE_write_u8 ( uint8_t  data,
uint16_t  address 
)

Write a unsigned 8 bit integer to address given.

This function is invoked by macro definition flash_put()

Parameters
dataThe data to be written
addressThe address to be written to
Returns
The memory address given plus one.

Definition at line 52 of file flashWrite.c.