efm32tg11b_hal.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #include <stdint.h>
  2. #include "efm32tg11b_hal.h"
  3. #include "em_device.h"
  4. //make flashing device lock bootloader
  5. const uint32_t __attribute__((section (".debug_lock"))) user_lock_word[1] = {0xFFFFFFFD};
  6. static uint8_t PT_KEY[16];
  7. void platform_init(void)
  8. {
  9. CMU->HFXOCTRL = 3; //xtal is ext clk
  10. CMU->OSCENCMD = 1 << 2; //enable hfxo
  11. while (!(CMU->STATUS & 1 << 2)); //wait for hfxo to be enabled
  12. while (!(CMU->STATUS & 1 << 3)); //wait for hfxo to be ready
  13. CMU->HFCLKSEL = 2; //select HFCLK as HFXO
  14. }
  15. void init_uart(void)
  16. {
  17. CMU->HFPERCLKEN0 |= 1; //enable UART clock
  18. CMU->HFBUSCLKEN0 |= 0b100; //enable GPIO clock
  19. GPIO->P[4].MODEH = (4 << 8) | (1 << 12); //pin 10 push-pull, enable pin 11 input
  20. GPIO->P[4].DOUT = 1 << 10; //set PE10 high
  21. USART0->CTRL = 0; //UART mode
  22. USART0->FRAME = 0x05 | (1 << 12); //8n1
  23. USART0->CLKDIV = 2815; //38400 with 7.37MHz perip clock (I think this is right?)
  24. USART0->ROUTEPEN = 0b11; //enable TX and RX pins
  25. USART0->ROUTELOC0 = 0; //location 0 for tx and rx
  26. USART0->CMD = 0b101; //enable TX and RX
  27. uint32_t *status = &USART0->STATUS;
  28. while (!(*status & 0b11)) {
  29. USART0->CMD = 0b101;
  30. }; //wait for tx and rx enable
  31. while (*status & (1 << 4)); //check for transmitter tristated
  32. }
  33. void putch(char c)
  34. {
  35. while (!(USART0->STATUS & (1 << 6))); //wait for tx buf to be empty
  36. USART0->TXDATA = c; //transmit data
  37. }
  38. char getch(void)
  39. {
  40. while (!(USART0->STATUS & (1 << 7))); //wait for data
  41. return USART0->RXDATA;
  42. }
  43. void trigger_setup(void)
  44. {
  45. CMU->HFBUSCLKEN0 |= 0b100; //enable GPIO clock
  46. GPIO->P[1].MODEL = 4 << (4 * 6); //PB6 push pull
  47. GPIO->P[1].DOUT &= ~(1 << 6); //PB6 low
  48. }
  49. void trigger_low(void)
  50. {
  51. GPIO->P[1].DOUT &= ~(1 << 6); //PB6 low
  52. }
  53. void trigger_high(void)
  54. {
  55. GPIO->P[1].DOUT |= (1 << 6); //PB6 high
  56. }
  57. void HW_AES128_Init(void)
  58. {
  59. //enable CRYPT0 clock
  60. CMU->HFBUSCLKEN0 |= 0b10;
  61. }
  62. void HW_AES128_LoadKey(uint8_t* key)
  63. {
  64. for(int i = 0; i < 16; i++)
  65. {
  66. PT_KEY[i] = key[i];
  67. }
  68. }
  69. void HW_AES128_Enc_pretrigger(uint8_t* pt)
  70. {
  71. for(int i = 0; i < 4; i++)
  72. {
  73. uint32_t key32 = (PT_KEY[4*i + 3] << 24) | (PT_KEY[4*i + 2] << 16) | (PT_KEY[4*i + 1] << 8) | (PT_KEY[4*i]);
  74. CRYPTO0->KEYBUF = key32;
  75. }
  76. for(int i = 0; i < 4; i++)
  77. {
  78. uint32_t pt32 = (pt[4*i + 3] << 24) | (pt[4*i + 2] << 16) | (pt[4*i + 1] << 8) | (pt[4*i]);
  79. CRYPTO0->DATA0 = pt32;
  80. }
  81. }
  82. void HW_AES128_Enc(uint8_t* pt)
  83. {
  84. CRYPTO0->CMD = 5;
  85. while (!(CRYPTO0->IF & 1)); //wait for aes to finish
  86. CRYPTO0->IFC = 1; //clear aes finished flag
  87. }
  88. void HW_AES128_Enc_posttrigger(uint8_t* pt)
  89. {
  90. for(int i = 0; i < 4; i++)
  91. {
  92. uint32_t ct = CRYPTO0->DATA0;
  93. pt[4*i] = ct & 0xFF;
  94. pt[4*i + 1] = (ct >> 8) & 0xFF;
  95. pt[4*i + 2] = (ct >> 16) & 0xFF;
  96. pt[4*i + 3] = (ct >> 24) & 0xFF;
  97. }
  98. }
  99. //not working for now, need cypher key to decrypt, so it needs to be stored
  100. void HW_AES128_Dec(uint8_t *pt)
  101. {
  102. //do aes to get cypher key
  103. for(int i = 0; i < 4; i++)
  104. {
  105. uint32_t key32 = (PT_KEY[4*i + 3] << 24) | (PT_KEY[4*i + 2] << 16) | (PT_KEY[4*i + 1] << 8) | (PT_KEY[4*i]);
  106. CRYPTO0->KEYBUF = key32;
  107. }
  108. CRYPTO0->CMD = 5;
  109. while (!(CRYPTO0->IF & 1)); //wait for aes to finish
  110. //shift cypher key into keybuf
  111. for(int i = 0; i < 4; i++)
  112. {
  113. uint32_t key32 = (PT_KEY[4*i + 3] << 24) | (PT_KEY[4*i + 2] << 16) | (PT_KEY[4*i + 1] << 8) | (PT_KEY[4*i]);
  114. CRYPTO0->KEYBUF = CRYPTO0->KEY;
  115. }
  116. for(int i = 0; i < 4; i++)
  117. {
  118. uint32_t pt32 = (pt[4*i + 3] << 24) | (pt[4*i + 2] << 16) | (pt[4*i + 1] << 8) | (pt[4*i]);
  119. CRYPTO0->DATA0 = pt32;
  120. }
  121. CRYPTO0->CMD = 6;
  122. while (!(CRYPTO0->IF & 1)); //wait for aes to finish
  123. CRYPTO0->IFC = 1; //clear aes finished flag
  124. for(int i = 0; i < 4; i++)
  125. {
  126. uint32_t ct = CRYPTO0->DATA0;
  127. pt[4*i] = ct & 0xFF;
  128. pt[4*i + 1] = (ct >> 8) & 0xFF;
  129. pt[4*i + 2] = (ct >> 16) & 0xFF;
  130. pt[4*i + 3] = (ct >> 24) & 0xFF;
  131. }
  132. }