core_otp.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. #include "bl_inc.h"
  22. #define OTP_KEY1 (0xABCD00A5)
  23. #define OTP_KEY2 (0x1234005A)
  24. #define OTP_START_ADDRESS (0x40008000)
  25. void OTP_Write(uint32_t Address, const uint32_t *Data, uint32_t Len)
  26. {
  27. uint32_t i;
  28. SYSCTRL->CG_CTRL2 |= SYSCTRL_AHBPeriph_OTP;
  29. OTP->RO = 0;
  30. OTP->CFG = 0;
  31. for(i = 0; i < Len; i++)
  32. {
  33. OTP->PDATA = Data[i];
  34. OTP->ADDR = Address + (i * 4);
  35. OTP->PROT = OTP_KEY1;
  36. OTP->PROT = OTP_KEY2;
  37. OTP->CS |= 1;
  38. while((OTP->CS & 0x00000001)){;}
  39. OTP->CS &= ~(0x07 << 1);
  40. }
  41. SYSCTRL->CG_CTRL2 &= ~SYSCTRL_AHBPeriph_OTP;
  42. }
  43. void OTP_Read(uint32_t Address, uint8_t *Data, uint32_t Len)
  44. {
  45. SYSCTRL->CG_CTRL2 |= SYSCTRL_AHBPeriph_OTP;
  46. OTP->CFG = 0x02;
  47. while(!(OTP->CS & 0x80000000)){;}
  48. memcpy(Data, Address + OTP_START_ADDRESS, Len);
  49. SYSCTRL->CG_CTRL2 &= ~SYSCTRL_AHBPeriph_OTP;
  50. }
  51. void OTP_Lock(void)
  52. {
  53. SYSCTRL->CG_CTRL2 |= SYSCTRL_AHBPeriph_OTP;
  54. OTP->CFG = 0x02;
  55. OTP->RO = 0xffffffff;
  56. OTP->ROL = 0xffffffff;
  57. OTP->CFG = 0x01;
  58. SYSCTRL->CG_CTRL2 &= ~SYSCTRL_AHBPeriph_OTP;
  59. }
  60. void OTP_GetSn(uint8_t *ChipSN)
  61. {
  62. SYSCTRL->CG_CTRL2 |= SYSCTRL_AHBPeriph_OTP;
  63. memcpy(ChipSN, (uint32_t *)(SYSCTRL_CHIP_SN_ADDR), SYSCTRL_CHIP_SN_LEN);
  64. SYSCTRL->CG_CTRL2 &= ~SYSCTRL_AHBPeriph_OTP;
  65. }