nrf52840_hal.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #include "nrf52840_hal.h"
  2. #include <stdio.h>
  3. #include "nrf.h"
  4. #include "nrf_drv_clock.h"
  5. #include "nrf_gpio.h"
  6. #include "nrf_delay.h"
  7. #include "nrf_log.h"
  8. #include "nrf_log_ctrl.h"
  9. #include "nrf_rng.h"
  10. #include "nrf_drv_power.h"
  11. #include "nrf_serial.h"
  12. #include "app_timer.h"
  13. #include "ssi_pal_types.h"
  14. #include "ssi_pal_mem.h"
  15. #include "sns_silib.h"
  16. #include "ssi_aes.h"
  17. #include "crys_aesccm.h"
  18. #include "app_error.h"
  19. #include "app_util.h"
  20. #include "boards.h"
  21. #include <stdint.h>
  22. SaSiAesUserContext_t ContextID;
  23. void platform_init(void)
  24. {
  25. ret_code_t ret;
  26. ret = nrf_drv_clock_init();
  27. APP_ERROR_CHECK(ret);
  28. ret = nrf_drv_power_init(NULL);
  29. APP_ERROR_CHECK(ret);
  30. nrf_drv_clock_lfclk_request(NULL);
  31. nrf_drv_clock_hfclk_request(NULL);
  32. ret = app_timer_init();
  33. APP_ERROR_CHECK(ret);
  34. //while (false == nrf_drv_clock_hfclk_is_running());
  35. NRF_GPIOTE->CONFIG[0] = GPIOTE_CONFIG_MODE_Task << GPIOTE_CONFIG_MODE_Pos |
  36. GPIOTE_CONFIG_POLARITY_Toggle << GPIOTE_CONFIG_POLARITY_Pos |
  37. XTAL_OUT << GPIOTE_CONFIG_PSEL_Pos |
  38. GPIOTE_CONFIG_OUTINIT_Low << GPIOTE_CONFIG_OUTINIT_Pos;
  39. NRF_TIMER1->PRESCALER = 0;
  40. NRF_TIMER1->CC[0] = 1;
  41. NRF_TIMER1->SHORTS = TIMER_SHORTS_COMPARE0_CLEAR_Enabled << TIMER_SHORTS_COMPARE0_CLEAR_Pos;
  42. NRF_TIMER1->TASKS_START = 1;
  43. NRF_PPI->CH[0].EEP = (uint32_t) &NRF_TIMER1->EVENTS_COMPARE[0];
  44. NRF_PPI->CH[0].TEP = (uint32_t) &NRF_GPIOTE->TASKS_OUT[0];
  45. NRF_PPI->CHENSET = PPI_CHENSET_CH0_Enabled << PPI_CHENSET_CH0_Pos;
  46. NRF_CLOCK->TRACECONFIG = (CLOCK_TRACECONFIG_TRACEMUX_Parallel << CLOCK_TRACECONFIG_TRACEMUX_Pos);
  47. // Initialize LEDs and buttons.
  48. bsp_board_init(BSP_INIT_LEDS | BSP_INIT_BUTTONS);
  49. NVIC_EnableIRQ(CRYPTOCELL_IRQn);
  50. NRF_CRYPTOCELL->ENABLE = 1;
  51. // Start TRNG
  52. nrf_rng_shorts_disable(NRF_RNG_SHORT_VALRDY_STOP_MASK);
  53. nrf_rng_error_correction_enable();
  54. nrf_rng_event_clear(NRF_RNG_EVENT_VALRDY);
  55. nrf_rng_task_trigger(NRF_RNG_TASK_START);
  56. SaSi_LibInit();
  57. }
  58. NRF_SERIAL_DRV_UART_CONFIG_DEF(m_uart0_drv_config,
  59. RX_PIN_NUMBER, TX_PIN_NUMBER,
  60. 0, 0,
  61. NRF_UART_HWFC_DISABLED, NRF_UART_PARITY_EXCLUDED,
  62. NRF_UART_BAUDRATE_38400,
  63. UART_DEFAULT_CONFIG_IRQ_PRIORITY);
  64. NRF_SERIAL_CONFIG_DEF(serial_config, NRF_SERIAL_MODE_POLLING,
  65. NULL, NULL, NULL, NULL);
  66. NRF_SERIAL_UART_DEF(serial_uart, 0);
  67. void init_uart(void)
  68. {
  69. ret_code_t ret;
  70. ret = nrf_serial_init(&serial_uart, &m_uart0_drv_config, &serial_config);
  71. APP_ERROR_CHECK(ret);
  72. }
  73. void trigger_setup(void)
  74. {
  75. nrf_gpio_cfg_output(NRF_GPIO_PIN_MAP(0,29));
  76. }
  77. void trigger_high(void)
  78. {
  79. nrf_gpio_pin_write(NRF_GPIO_PIN_MAP(0,29), 1);
  80. }
  81. void trigger_low(void)
  82. {
  83. nrf_gpio_pin_write(NRF_GPIO_PIN_MAP(0,29), 0);
  84. }
  85. char getch(void)
  86. {
  87. char c;
  88. nrf_serial_read(&serial_uart, &c, sizeof(c), NULL, 1000);
  89. return c;
  90. }
  91. void putch(char c)
  92. {
  93. nrf_serial_write(&serial_uart,
  94. &c,
  95. 1,
  96. NULL,
  97. NRF_SERIAL_MAX_TIMEOUT);
  98. }
  99. uint32_t get_rand(void)
  100. {
  101. uint32_t value;
  102. int i;
  103. for (i = 0; i < 4; i++)
  104. {
  105. while (!nrf_rng_event_get(NRF_RNG_EVENT_VALRDY)) ;
  106. nrf_rng_event_clear(NRF_RNG_EVENT_VALRDY);
  107. value <<= 8;
  108. value |= nrf_rng_random_value_get();
  109. }
  110. return value;
  111. }
  112. void HW_AES128_Init(void)
  113. {
  114. SaSi_AesInit(&ContextID,SASI_AES_ENCRYPT,SASI_AES_MODE_ECB,SASI_AES_PADDING_NONE);
  115. }
  116. void HW_AES128_LoadKey(uint8_t* key)
  117. {
  118. SaSiAesUserKeyData_t keyData;
  119. keyData.keySize = 16;
  120. keyData.pKey = key;
  121. SaSi_AesSetKey(&ContextID, SASI_AES_USER_KEY, &keyData, sizeof(keyData) );
  122. }
  123. void HW_AES128_Enc_pretrigger(uint8_t* pt)
  124. {
  125. }
  126. void HW_AES128_Enc(uint8_t* pt)
  127. {
  128. SaSi_AesBlock(&ContextID, pt, 16, pt);
  129. }
  130. void HW_AES128_Enc_posttrigger(uint8_t* pt)
  131. {
  132. }
  133. void HW_AES128_Dec(uint8_t *pt)
  134. {
  135. ;
  136. }