saml11_hal.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #include "sam.h"
  2. static uint8_t hw_key[16];
  3. void platform_init(void)
  4. {
  5. SystemInit();
  6. //enable ext clock and clock failure detection
  7. OSCCTRL->XOSCCTRL.reg = (1 << 1) | (1 << 3);
  8. //wait for clock to be ready and check for failure
  9. while (!OSCCTRL->STATUS.bit.XOSCRDY);
  10. while (OSCCTRL->STATUS.bit.XOSCFAIL);
  11. GCLK->GENCTRL[0].bit.SRC = 0; //switch to xosc
  12. PORT_SEC->Group[0].DIRSET.reg = 1 << 23;
  13. PORT_SEC->Group[0].OUTSET.reg = 1 << 23;
  14. }
  15. void init_uart(void)
  16. {
  17. //PORT_SEC->Group[0].DIRSET.reg = 1 << 8;
  18. //multiplexing needs to be enabled before setting where stuff is mux'd to
  19. PORT_SEC->Group[0].PINCFG[16].reg = 0b11; //enable input read, mux
  20. PORT_SEC->Group[0].PINCFG[17].reg = 0b11; //enable input read, mux
  21. PORT_SEC->Group[0].PMUX[8].reg = 3 | (3 << 4); //SERCOM_alt mux for PORT
  22. //DON"T FORGET TO ENABLE PERIPH CLOCK
  23. MCLK->APBCMASK.bit.SERCOM1_ = 1; //on by default?
  24. GCLK->PCHCTRL[11].reg = (1 << 6) | 0; //enable generic clock, use src 0
  25. //LSB first, RX on pad[1] (PA9), Tx on pad[0], internal clock
  26. SERCOM0->USART.CTRLA.reg = (1 << 30) | (1 << 20) | (0 << 16) | (1 << 2);
  27. //enable TX and RX
  28. SERCOM0->USART.BAUD.reg = 60073;
  29. //DEFAULT FOR ABOVE IS 8n1
  30. SERCOM0->USART.CTRLA.bit.ENABLE = 1; //enable
  31. SERCOM0->USART.CTRLB.reg = (1 << 17) | (1 << 16);
  32. while(SERCOM0->USART.SYNCBUSY.bit.CTRLB); //wait for tx and rx to be enabled
  33. while(SERCOM0->USART.SYNCBUSY.bit.ENABLE); //wait for enable to complete
  34. while(!(SERCOM0->USART.CTRLB.reg & (0b11 << 16))); //check for tx and rx enable
  35. }
  36. void putch(char c)
  37. {
  38. //wait for data register to be empty
  39. while(!SERCOM0->USART.INTFLAG.bit.DRE);
  40. SERCOM0->USART.DATA.reg = c;
  41. }
  42. char getch()
  43. {
  44. //wait for data
  45. while(!SERCOM0->USART.INTFLAG.bit.RXC);
  46. return SERCOM0->USART.DATA.reg;
  47. }
  48. void trigger_setup(void)
  49. {
  50. PORT_SEC->Group[0].DIRSET.reg = 1 << 22;
  51. PORT_SEC->Group[0].OUTCLR.reg = 1 << 22;
  52. }
  53. void trigger_low(void)
  54. {
  55. PORT_SEC->Group[0].OUTCLR.reg = 1 << 22;
  56. }
  57. void trigger_high(void)
  58. {
  59. PORT_SEC->Group[0].OUTSET.reg = 1 << 22;
  60. }
  61. typedef void (*IDAU_AES_FUNC)(const uint8_t *keys, uint32_t key_len,
  62. const uint8_t *src, uint8_t *dst);
  63. //or with 0x01 because thumb instructions are all on odd PC
  64. IDAU_AES_FUNC idau_aes_enc = (IDAU_AES_FUNC)(IDAU_CRYA_AES_ENCRYPT_T | 0x01);
  65. IDAU_AES_FUNC idau_aes_dec = (IDAU_AES_FUNC)(IDAU_CRYA_AES_DECRYPT_T | 0x01);
  66. void HW_AES128_Init(void)
  67. {
  68. }
  69. void HW_AES128_LoadKey(uint8_t* key)
  70. {
  71. for(int i = 0; i < 16; i++)
  72. {
  73. hw_key[i] = key[i];
  74. }
  75. }
  76. void HW_AES128_Enc(uint8_t* pt)
  77. {
  78. idau_aes_enc(hw_key, 4, pt, pt);
  79. }
  80. void HW_AES128_Enc_pretrigger(uint8_t* pt)
  81. {
  82. ;
  83. }
  84. void HW_AES128_Enc_posttrigger(uint8_t* pt)
  85. {
  86. ;
  87. }
  88. void HW_AES128_Dec(uint8_t *pt)
  89. {
  90. idau_aes_dec(hw_key, 4, pt, pt);
  91. }