ESPHome  2024.12.2
stm32flash.h
Go to the documentation of this file.
1 /*
2  stm32flash - Open Source ST STM32 flash program for Arduino
3  Copyright (C) 2010 Geoffrey McRae <[email protected]>
4 
5  This program is free software; you can redistribute it and/or
6  modify it under the terms of the GNU General Public License
7  as published by the Free Software Foundation; either version 2
8  of the License, or (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19 
20 #pragma once
21 
22 #include "esphome/core/defines.h"
23 #ifdef USE_SHD_FIRMWARE_DATA
24 
25 #include <cstdint>
26 #include <memory>
28 
29 namespace esphome {
30 namespace shelly_dimmer {
31 
32 /* flags */
33 constexpr auto STREAM_OPT_BYTE = (1 << 0); /* byte (not frame) oriented */
34 constexpr auto STREAM_OPT_GVR_ETX = (1 << 1); /* cmd GVR returns protection status */
35 constexpr auto STREAM_OPT_CMD_INIT = (1 << 2); /* use INIT cmd to autodetect speed */
36 constexpr auto STREAM_OPT_RETRY = (1 << 3); /* allowed read() retry after timeout */
37 constexpr auto STREAM_OPT_I2C = (1 << 4); /* i2c */
38 constexpr auto STREAM_OPT_STRETCH_W = (1 << 5); /* warning for no-stretching commands */
39 
40 constexpr auto STREAM_SERIAL = (STREAM_OPT_BYTE | STREAM_OPT_GVR_ETX | STREAM_OPT_CMD_INIT | STREAM_OPT_RETRY);
41 constexpr auto STREAM_I2C = (STREAM_OPT_I2C | STREAM_OPT_STRETCH_W);
42 
43 constexpr auto STM32_MAX_RX_FRAME = 256; /* cmd read memory */
44 constexpr auto STM32_MAX_TX_FRAME = (1 + 256 + 1); /* cmd write memory */
45 
46 constexpr auto STM32_MAX_PAGES = 0x0000ffff;
47 constexpr auto STM32_MASS_ERASE = 0x00100000; /* > 2 x max_pages */
48 
49 using stm32_err_t = enum Stm32Err {
50  STM32_ERR_OK = 0,
51  STM32_ERR_UNKNOWN, /* Generic error */
52  STM32_ERR_NACK,
53  STM32_ERR_NO_CMD, /* Command not available in bootloader */
54 };
55 
56 using flags_t = enum Flags {
57  F_NO_ME = 1 << 0, /* Mass-Erase not supported */
58  F_OBLL = 1 << 1, /* OBL_LAUNCH required */
59 };
60 
61 using stm32_cmd_t = struct Stm32Cmd {
62  uint8_t get;
63  uint8_t gvr;
64  uint8_t gid;
65  uint8_t rm;
66  uint8_t go;
67  uint8_t wm;
68  uint8_t er; /* this may be extended erase */
69  uint8_t wp;
70  uint8_t uw;
71  uint8_t rp;
72  uint8_t ur;
73  uint8_t crc;
74 };
75 
76 using stm32_dev_t = struct Stm32Dev { // NOLINT
77  const uint16_t id;
78  const char *name;
79  const uint32_t ram_start, ram_end;
80  const uint32_t fl_start, fl_end;
81  const uint16_t fl_pps; // pages per sector
82  const uint32_t *fl_ps; // page size
83  const uint32_t opt_start, opt_end;
84  const uint32_t mem_start, mem_end;
85  const uint32_t flags;
86 };
87 
88 using stm32_t = struct Stm32 {
90  uint8_t flags;
92  uint8_t bl_version;
93  uint8_t version;
94  uint8_t option1, option2;
95  uint16_t pid;
97  const stm32_dev_t *dev;
98 };
99 
100 /*
101  * Specify the length of reply for command GET
102  * This is helpful for frame-oriented protocols, e.g. i2c, to avoid time
103  * consuming try-fail-timeout-retry operation.
104  * On byte-oriented protocols, i.e. UART, this information would be skipped
105  * after read the first byte, so not needed.
106  */
107 struct VarlenCmd {
108  uint8_t version;
109  uint8_t length;
110 };
111 
112 using stm32_unique_ptr = std::unique_ptr<stm32_t, void (*)(stm32_t *)>;
113 
114 stm32_unique_ptr stm32_init(uart::UARTDevice *stream, uint8_t flags, char init);
115 stm32_err_t stm32_read_memory(const stm32_unique_ptr &stm, uint32_t address, uint8_t *data, unsigned int len);
116 stm32_err_t stm32_write_memory(const stm32_unique_ptr &stm, uint32_t address, const uint8_t *data, unsigned int len);
119 stm32_err_t stm32_erase_memory(const stm32_unique_ptr &stm, uint32_t spage, uint32_t pages);
120 stm32_err_t stm32_go(const stm32_unique_ptr &stm, uint32_t address);
124 stm32_err_t stm32_crc_memory(const stm32_unique_ptr &stm, uint32_t address, uint32_t length, uint32_t *crc);
125 stm32_err_t stm32_crc_wrapper(const stm32_unique_ptr &stm, uint32_t address, uint32_t length, uint32_t *crc);
126 uint32_t stm32_sw_crc(uint32_t crc, uint8_t *buf, unsigned int len);
127 
128 } // namespace shelly_dimmer
129 } // namespace esphome
130 
131 #endif // USE_SHD_FIRMWARE_DATA
constexpr auto STREAM_OPT_CMD_INIT
Definition: stm32flash.h:35
const char * name
Definition: stm32flash.h:78
stm32_err_t stm32_wprot_memory(const stm32_unique_ptr &stm)
Definition: stm32flash.cpp:772
enum Stm32Err { STM32_ERR_OK=0, STM32_ERR_UNKNOWN, STM32_ERR_NACK, STM32_ERR_NO_CMD, } stm32_err_t
Definition: stm32flash.h:54
stm32_err_t stm32_wunprot_memory(const stm32_unique_ptr &stm)
Definition: stm32flash.cpp:759
struct VarlenCmd * cmd_get_reply
Definition: stm32flash.h:91
stm32_err_t stm32_crc_wrapper(const stm32_unique_ptr &stm, uint32_t address, uint32_t length, uint32_t *crc)
const uint32_t fl_start
Definition: stm32flash.h:80
stm32_err_t stm32_write_memory(const stm32_unique_ptr &stm, uint32_t address, const uint8_t *data, const unsigned int len)
Definition: stm32flash.cpp:698
const uint32_t opt_end
Definition: stm32flash.h:83
constexpr auto STREAM_OPT_RETRY
Definition: stm32flash.h:36
struct Stm32Cmd { uint8_t get stm32_cmd_t
Definition: stm32flash.h:62
struct Stm32Dev { const uint16_t id stm32_dev_t
Definition: stm32flash.h:77
T id(T value)
Helper function to make id(var) known from lambdas work in custom components.
Definition: helpers.h:728
stm32_err_t stm32_crc_memory(const stm32_unique_ptr &stm, const uint32_t address, const uint32_t length, uint32_t *const crc)
Definition: stm32flash.cpp:933
constexpr auto STREAM_SERIAL
Definition: stm32flash.h:40
constexpr auto STM32_MAX_TX_FRAME
Definition: stm32flash.h:44
const stm32_dev_t * dev
Definition: stm32flash.h:97
stm32_err_t stm32_readprot_memory(const stm32_unique_ptr &stm)
Definition: stm32flash.cpp:798
constexpr auto STREAM_OPT_BYTE
Definition: stm32flash.h:33
const uint32_t ram_start
Definition: stm32flash.h:79
uint32_t stm32_sw_crc(uint32_t crc, uint8_t *buf, unsigned int len)
const uint32_t ram_end
Definition: stm32flash.h:79
stm32_err_t stm32_go(const stm32_unique_ptr &stm, const uint32_t address)
Definition: stm32flash.cpp:899
constexpr auto STREAM_I2C
Definition: stm32flash.h:41
constexpr auto STM32_MAX_PAGES
Definition: stm32flash.h:46
constexpr auto STREAM_OPT_STRETCH_W
Definition: stm32flash.h:38
stm32_unique_ptr stm32_init(uart::UARTDevice *stream, const uint8_t flags, const char init)
Definition: stm32flash.cpp:500
std::unique_ptr< stm32_t, void(*)(stm32_t *)> stm32_unique_ptr
Definition: stm32flash.h:112
const uint16_t fl_pps
Definition: stm32flash.h:81
const uint32_t fl_end
Definition: stm32flash.h:80
constexpr auto STM32_MASS_ERASE
Definition: stm32flash.h:47
constexpr auto STREAM_OPT_GVR_ETX
Definition: stm32flash.h:34
stm32_err_t stm32_read_memory(const stm32_unique_ptr &stm, const uint32_t address, uint8_t *data, const unsigned int len)
Definition: stm32flash.cpp:659
const uint32_t flags
Definition: stm32flash.h:85
enum Flags { F_NO_ME=1<< 0, F_OBLL=1<< 1, } flags_t
Definition: stm32flash.h:59
constexpr auto STREAM_OPT_I2C
Definition: stm32flash.h:37
const uint32_t opt_start
Definition: stm32flash.h:83
stm32_err_t stm32_erase_memory(const stm32_unique_ptr &stm, uint32_t spage, uint32_t pages)
Definition: stm32flash.cpp:811
const uint32_t * fl_ps
Definition: stm32flash.h:82
struct Stm32 { uart::UARTDevice *stream stm32_t
Definition: stm32flash.h:89
std::string size_t len
Definition: helpers.h:293
stm32_err_t stm32_runprot_memory(const stm32_unique_ptr &stm)
Definition: stm32flash.cpp:785
uint16_t length
Definition: tt21100.cpp:12
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
uint8_t address
Definition: bl0906.h:211
void init()
Definition: core.cpp:80
const uint32_t mem_end
Definition: stm32flash.h:84
const uint32_t mem_start
Definition: stm32flash.h:84
stm32_err_t stm32_reset_device(const stm32_unique_ptr &stm)
Definition: stm32flash.cpp:922
stm32_cmd_t * cmd
Definition: stm32flash.h:96
constexpr auto STM32_MAX_RX_FRAME
Definition: stm32flash.h:43