efr32mg21a_hal.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #include <stdint.h>
  2. #include <string.h>
  3. #include "em_device.h"
  4. #include "em_chip.h"
  5. #include "em_cmu.h"
  6. #include "em_gpio.h"
  7. #include "em_usart.h"
  8. #include "em_se.h"
  9. #include "efr32mg21a_hal.h"
  10. //make flashing device lock bootloader
  11. //const uint32_t __attribute__((section (".debug_lock"))) user_lock_word[1] = {0xFFFFFFFD};
  12. void platform_init(void)
  13. {
  14. CHIP_Init();
  15. CMU_HFXOInit_TypeDef hfxoInit = CMU_HFXOINIT_DEFAULT;
  16. hfxoInit.mode = _HFXO_CFG_MODE_EXTCLK;
  17. CMU_HFXOInit(&hfxoInit);
  18. CMU_ClockSelectSet(cmuClock_SYSCLK, cmuSelect_HFXO);
  19. CMU_OscillatorEnable(cmuOsc_HFRCODPLL, false, false);
  20. CMU_ClockEnable(cmuClock_GPIO, true);
  21. CMU_ClockEnable(cmuClock_USART1, true);
  22. }
  23. void init_uart(void)
  24. {
  25. GPIO_PinModeSet(gpioPortC, 0, gpioModePushPull, 1); //PC0 = Tx
  26. GPIO_PinModeSet(gpioPortC, 1, gpioModeInput, 0); //PC1 = RX
  27. GPIO->USARTROUTE[0].TXROUTE = (gpioPortC << _GPIO_USART_TXROUTE_PORT_SHIFT)
  28. | (0 << _GPIO_USART_TXROUTE_PIN_SHIFT);
  29. GPIO->USARTROUTE[0].RXROUTE = (gpioPortC << _GPIO_USART_RXROUTE_PORT_SHIFT)
  30. | (1 << _GPIO_USART_RXROUTE_PIN_SHIFT);
  31. GPIO->USARTROUTE[0].ROUTEEN = GPIO_USART_ROUTEEN_RXPEN | GPIO_USART_ROUTEEN_TXPEN;
  32. USART_InitAsync_TypeDef init = USART_INITASYNC_DEFAULT;
  33. init.baudrate = 38400;
  34. init.refFreq = 7.3728E6;
  35. USART_InitAsync(USART0, &init);
  36. }
  37. void putch(char c)
  38. {
  39. USART_Tx(USART0, c);
  40. }
  41. char getch(void)
  42. {
  43. return USART_Rx(USART0);
  44. }
  45. void trigger_setup(void)
  46. {
  47. GPIO_PinModeSet(gpioPortC, 3, gpioModePushPull, 0);
  48. }
  49. void trigger_low(void)
  50. {
  51. GPIO_PinOutClear(gpioPortC, 3);
  52. }
  53. void trigger_high(void)
  54. {
  55. GPIO_PinOutSet(gpioPortC, 3);
  56. }
  57. void HW_AES128_Init(void)
  58. {
  59. ;
  60. }
  61. static uint8_t hw_aes_key[16];
  62. static uint8_t cipher_output[16];
  63. void HW_AES128_LoadKey(uint8_t* key)
  64. {
  65. for(unsigned int i = 0; i < 16; i++){
  66. hw_aes_key[i] = key[i];
  67. }
  68. }
  69. static SE_Command_t se_command = SE_COMMAND_DEFAULT(SE_COMMAND_AES_ENCRYPT | SE_COMMAND_OPTION_MODE_ECB | SE_COMMAND_OPTION_CONTEXT_WHOLE);
  70. static SE_DataTransfer_t se_key;
  71. static SE_DataTransfer_t se_in;
  72. static SE_DataTransfer_t se_out;
  73. void HW_AES128_Enc_pretrigger(uint8_t* pt)
  74. {
  75. const unsigned int keysize = 16;
  76. memset(cipher_output, 0, sizeof(cipher_output));
  77. //Temp ones so we can use same init as from se_aes.c reference file
  78. SE_Command_t tempcommand = SE_COMMAND_DEFAULT(SE_COMMAND_AES_ENCRYPT | SE_COMMAND_OPTION_MODE_ECB | SE_COMMAND_OPTION_CONTEXT_WHOLE);
  79. SE_DataTransfer_t tempkey = SE_DATATRANSFER_DEFAULT(hw_aes_key, keysize);
  80. SE_DataTransfer_t tempin = SE_DATATRANSFER_DEFAULT((void*)pt, 16);
  81. SE_DataTransfer_t tempout = SE_DATATRANSFER_DEFAULT(cipher_output, 16);
  82. memcpy(&se_command, &tempcommand, sizeof(tempcommand));
  83. memcpy(&se_key, &tempkey, sizeof(tempkey));
  84. memcpy(&se_in, &tempin, sizeof(tempin));
  85. memcpy(&se_out, &tempout, sizeof(tempout));
  86. SE_addDataInput(&se_command, &tempkey);
  87. SE_addDataInput(&se_command, &tempin);
  88. SE_addDataOutput(&se_command, &tempout);
  89. SE_addParameter(&se_command, keysize);
  90. SE_addParameter(&se_command, 16);
  91. }
  92. void HW_AES128_Enc_posttrigger(uint8_t* pt)
  93. {
  94. SE_Response_t command_status;
  95. command_status = SE_readCommandResponse();
  96. for(unsigned int i = 0; i < 16; i++){
  97. pt[i] = cipher_output[i];
  98. }
  99. if ( command_status == SE_RESPONSE_OK ) {
  100. return;
  101. } else {
  102. //We don't have way to report back, just block for now!
  103. putch('F');
  104. putch('A');
  105. putch('I');
  106. putch('L');
  107. //while(1);
  108. return;
  109. }
  110. }
  111. void HW_AES128_Enc(uint8_t* pt)
  112. {
  113. SE_executeCommand(&se_command);
  114. while(!SE_isCommandCompleted());
  115. }
  116. //not working for now, need cypher key to decrypt, so it needs to be stored
  117. void HW_AES128_Dec(uint8_t *pt)
  118. {
  119. ;
  120. }