38
|
1 /**
|
|
2 * |----------------------------------------------------------------------
|
|
3 * | Copyright (C) Tilen Majerle, 2014
|
|
4 * |
|
|
5 * | This program is free software: you can redistribute it and/or modify
|
|
6 * | it under the terms of the GNU General Public License as published by
|
|
7 * | the Free Software Foundation, either version 3 of the License, or
|
|
8 * | 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, see <http://www.gnu.org/licenses/>.
|
|
17 * |----------------------------------------------------------------------
|
|
18 */
|
|
19 #include "tm_stm32f4_otp.h"
|
|
20
|
|
21 HAL_StatusTypeDef TM_OTP_Write(uint8_t block, uint8_t byte, uint8_t data)
|
|
22 {
|
|
23 HAL_StatusTypeDef answer;
|
|
24
|
|
25 /* Check input parameters */
|
|
26 if (
|
|
27 block >= OTP_BLOCKS ||
|
|
28 byte >= OTP_BYTES_IN_BLOCK
|
|
29 ) {
|
|
30 /* Invalid parameters */
|
|
31 return HAL_ERROR;
|
|
32 }
|
|
33
|
|
34 if(*(uint8_t *)(OTP_START_ADDR + block * OTP_BYTES_IN_BLOCK + byte) != 0xFF)
|
|
35 return HAL_ERROR;
|
|
36
|
|
37 /* Unlock FLASH */
|
|
38 HAL_FLASH_Unlock();
|
|
39
|
|
40 answer = HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, (OTP_START_ADDR + block * OTP_BYTES_IN_BLOCK + byte), data);
|
|
41
|
|
42 /* Lock FLASH */
|
|
43 HAL_FLASH_Lock();
|
|
44
|
|
45 return answer;
|
|
46 }
|
|
47
|
|
48
|
|
49 uint8_t TM_OTP_Read(uint8_t block, uint8_t byte) {
|
|
50 uint8_t data;
|
|
51
|
|
52 /* Check input parameters */
|
|
53 if (
|
|
54 block >= OTP_BLOCKS ||
|
|
55 byte >= OTP_BYTES_IN_BLOCK
|
|
56 ) {
|
|
57 /* Invalid parameters */
|
|
58 return HAL_ERROR;
|
|
59 }
|
|
60
|
|
61 /* Get value */
|
|
62 data = *(__IO uint8_t *)(OTP_START_ADDR + block * OTP_BYTES_IN_BLOCK + byte);
|
|
63
|
|
64 /* Return data */
|
|
65 return data;
|
|
66 }
|