Welcome to mirror list, hosted at ThFree Co, Russian Federation.

flash_emulation.c « platform « zigbee « STM32_WPAN « ST « Middlewares - github.com/Flipper-Zero/STM32CubeWB.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 89fa295a9159662f2a02f017d0e12f3d1388bab4 (plain)
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
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include "stm32wbxx.h"
#include "stm32wbxx_hal_conf.h"
#include "stm32_wpan_common.h"
#include "flash_emulation.h"

static inline uint32_t
mapAddress(uint32_t aAddress)
{
    return (aAddress + TMP_STORAGE_BUF_ADDR);
} /* mapAddress */

uint32_t
utilsFlashGetSize(void)
{
    return TMP_STORAGE_BUF_SIZE;
} /* utilsFlashGetSize */

/**
 * Write flash. The write operation only clears bits, but never set bits.
 *
 * The flash address starts from 0, and this function maps the input address to the physical address of flash for writing.
 * 0 is always mapped to the beginning of one flash page.
 * The input address should never be mapped to the firmware space or any other protected flash space.
 *
 * @param[in]  aAddress  The start address of the flash to write.
 * @param[in]  aData     The pointer of the data to write.
 * @param[in]  aSize     The size of the data to write.
 *
 * @returns The actual size of octets write to flash.
 *          It is expected the same as aSize, and may be less than aSize.
 *          0 indicates that something wrong happens when writing.
 */
uint32_t
utilsFlashWrite(uint32_t aAddress, uint8_t *aData, uint32_t aSize)
{
    uint32_t nbOfBytesToWrite = MIN(aSize, TMP_STORAGE_BUF_SIZE);

    memcpy((uint8_t *)mapAddress(aAddress), aData, nbOfBytesToWrite);

    return nbOfBytesToWrite;
} /* utilsFlashWrite */

/**
 * Read flash.
 *
 * The flash address starts from 0, and this function maps the input address to the physical address of flash for reading.
 * 0 is always mapped to the beginning of one flash page.
 * The input address should never be mapped to the firmware space or any other protected flash space.
 *
 * @param[in]   aAddress  The start address of the flash to read.
 * @param[Out]  aData     The pointer of buffer for reading.
 * @param[in]   aSize     The size of the data to read.
 *
 * @returns The actual size of octets read to buffer.
 *          It is expected the same as aSize, and may be less than aSize.
 *          0 indicates that something wrong happens when reading.
 */
uint32_t
utilsFlashRead(uint32_t aAddress, uint8_t *aData, uint32_t aSize)
{
    uint32_t nbOfBytesToRead = 0;

    /* Check the number of bytes to read */
    if ((aAddress + aSize) > TMP_STORAGE_BUF_SIZE) {
        nbOfBytesToRead = TMP_STORAGE_BUF_SIZE - aAddress;
    }
    else {
        nbOfBytesToRead = aSize;
    }

    /* Perform the copy */
    memcpy(aData, (uint8_t *)mapAddress(aAddress), nbOfBytesToRead);

    return nbOfBytesToRead;
} /* utilsFlashRead */