1 /*
2 * Copyright (c) 2022 OpenLuat & AirM2M
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy of
5 * this software and associated documentation files (the "Software"), to deal in
6 * the Software without restriction, including without limitation the rights to
7 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
8 * the Software, and to permit persons to whom the Software is furnished to do so,
9 * subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in all
12 * copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
16 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
17 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
18 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 */
21
22 #include "bl_inc.h"
23 #define OTP_KEY1 (0xABCD00A5)
24 #define OTP_KEY2 (0x1234005A)
25 #define OTP_START_ADDRESS (0x40008000)
26
OTP_Write(uint32_t Address,const uint32_t * Data,uint32_t Len)27 void OTP_Write(uint32_t Address, const uint32_t *Data, uint32_t Len)
28 {
29 uint32_t i;
30 SYSCTRL->CG_CTRL2 |= SYSCTRL_AHBPeriph_OTP;
31 OTP->RO = 0;
32 OTP->CFG = 0;
33 for(i = 0; i < Len; i++)
34 {
35 OTP->PDATA = Data[i];
36 OTP->ADDR = Address + (i * 4);
37 OTP->PROT = OTP_KEY1;
38 OTP->PROT = OTP_KEY2;
39 OTP->CS |= 1;
40 while((OTP->CS & 0x00000001)){;}
41 OTP->CS &= ~(0x07 << 1);
42 }
43 SYSCTRL->CG_CTRL2 &= ~SYSCTRL_AHBPeriph_OTP;
44 }
45
OTP_Read(uint32_t Address,uint8_t * Data,uint32_t Len)46 void OTP_Read(uint32_t Address, uint8_t *Data, uint32_t Len)
47 {
48 SYSCTRL->CG_CTRL2 |= SYSCTRL_AHBPeriph_OTP;
49 OTP->CFG = 0x02;
50 while(!(OTP->CS & 0x80000000)){;}
51 memcpy(Data, Address + OTP_START_ADDRESS, Len);
52 SYSCTRL->CG_CTRL2 &= ~SYSCTRL_AHBPeriph_OTP;
53 }
OTP_Lock(void)54 void OTP_Lock(void)
55 {
56 SYSCTRL->CG_CTRL2 |= SYSCTRL_AHBPeriph_OTP;
57 OTP->CFG = 0x02;
58 OTP->RO = 0xffffffff;
59 OTP->ROL = 0xffffffff;
60 OTP->CFG = 0x01;
61 SYSCTRL->CG_CTRL2 &= ~SYSCTRL_AHBPeriph_OTP;
62 }
63
OTP_GetSn(uint8_t * ChipSN)64 void OTP_GetSn(uint8_t *ChipSN)
65 {
66 memcpy(ChipSN, (uint32_t *)(SYSCTRL_CHIP_SN_ADDR), SYSCTRL_CHIP_SN_LEN);
67 }
68