1 /*
2   stm32flash - Open Source ST STM32 flash program for *nix
3   Copyright (C) 2010 Geoffrey McRae <geoff@spacevs.com>
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 
21 #ifndef _STM32_H
22 #define _STM32_H
23 
24 #include <stdint.h>
25 #include "serial.h"
26 
27 #define STM32_MAX_RX_FRAME	256	/* cmd read memory */
28 #define STM32_MAX_TX_FRAME	(1 + 256 + 1)	/* cmd write memory */
29 
30 #define STM32_MAX_PAGES		0x0000ffff
31 #define STM32_MASS_ERASE	0x00100000 /* > 2 x max_pages */
32 
33 typedef enum {
34 	STM32_ERR_OK = 0,
35 	STM32_ERR_UNKNOWN,	/* Generic error */
36 	STM32_ERR_NACK,
37 	STM32_ERR_NO_CMD,	/* Command not available in bootloader */
38 } stm32_err_t;
39 
40 typedef enum {
41 	F_NO_ME  = 1 << 0,	/* Mass-Erase not supported */
42 	F_OBLL   = 1 << 1,	/* OBL_LAUNCH required */
43 	F_PEMPTY = 1 << 2,	/* clear PEMPTY bit required */
44 } flags_t;
45 
46 typedef struct stm32		stm32_t;
47 typedef struct stm32_cmd	stm32_cmd_t;
48 typedef struct stm32_dev	stm32_dev_t;
49 
50 struct stm32 {
51 	const serial_t		*serial;
52 	struct port_interface	*port;
53 	uint8_t			bl_version;
54 	uint8_t			version;
55 	uint8_t			option1, option2;
56 	uint16_t		pid;
57 	stm32_cmd_t		*cmd;
58 	const stm32_dev_t	*dev;
59 };
60 
61 struct stm32_dev {
62 	uint16_t	id;
63 	const char	*name;
64 	uint32_t	ram_start, ram_end;
65 	uint32_t	fl_start, fl_end;
66 	uint16_t	fl_pps; // pages per sector
67 	uint32_t	*fl_ps;  // page size
68 	uint32_t	opt_start, opt_end;
69 	uint32_t	mem_start, mem_end;
70 	uint32_t	flags;
71 };
72 
73 stm32_t *stm32_init(struct port_interface *port, const char init);
74 void stm32_close(stm32_t *stm);
75 stm32_err_t stm32_read_memory(const stm32_t *stm, uint32_t address,
76 			      uint8_t data[], unsigned int len);
77 stm32_err_t stm32_write_memory(const stm32_t *stm, uint32_t address,
78 			       const uint8_t data[], unsigned int len);
79 stm32_err_t stm32_wunprot_memory(const stm32_t *stm);
80 stm32_err_t stm32_wprot_memory(const stm32_t *stm);
81 stm32_err_t stm32_erase_memory(const stm32_t *stm, uint32_t spage,
82 			       uint32_t pages);
83 stm32_err_t stm32_go(const stm32_t *stm, uint32_t address);
84 stm32_err_t stm32_reset_device(const stm32_t *stm);
85 stm32_err_t stm32_readprot_memory(const stm32_t *stm);
86 stm32_err_t stm32_runprot_memory(const stm32_t *stm);
87 stm32_err_t stm32_crc_memory(const stm32_t *stm, uint32_t address,
88 			     uint32_t length, uint32_t *crc);
89 stm32_err_t stm32_crc_wrapper(const stm32_t *stm, uint32_t address,
90 			      uint32_t length, uint32_t *crc);
91 uint32_t stm32_sw_crc(uint32_t crc, uint8_t *buf, unsigned int len);
92 
93 #endif
94 
95