main.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. #include "ch.h"
  14. #include "hal.h"
  15. #include "chprintf.h"
  16. #include "portab.h"
  17. /*
  18. * LED blinker thread, times are in milliseconds.
  19. */
  20. static THD_WORKING_AREA(waThread1, 128);
  21. static THD_FUNCTION(Thread1, arg) {
  22. (void)arg;
  23. chRegSetThreadName("blinker");
  24. while (true) {
  25. palToggleLine(PORTAB_LINE_LED1);
  26. chThdSleepMilliseconds(500);
  27. palToggleLine(PORTAB_LINE_LED1);
  28. chThdSleepMilliseconds(500);
  29. }
  30. }
  31. /*
  32. * Application entry point.
  33. */
  34. int main(void) {
  35. /*
  36. * System initializations.
  37. * - HAL initialization, this also initializes the configured device drivers
  38. * and performs the board-specific initializations.
  39. * - Kernel initialization, the main() function becomes a thread and the
  40. * RTOS is active.
  41. */
  42. halInit();
  43. chSysInit();
  44. /* Board-dependent GPIO setup code.*/
  45. portab_setup();
  46. /* Starting a serial port for test report output.*/
  47. sdStart(&PORTAB_SD1, NULL);
  48. /* Creates the blinker thread.*/
  49. chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
  50. /* Normal main() thread activity, in this demo it does nothing.*/
  51. while (true) {
  52. if (palReadLine(PORTAB_LINE_BUTTON) == PORTAB_BUTTON_PRESSED) {
  53. bool err;
  54. uint8_t rand[32];
  55. trngStart(&TRNGD1, NULL);
  56. err = trngGenerate(&TRNGD1, 32, rand);
  57. if (err) {
  58. chprintf((BaseSequentialStream *)&PORTAB_SD1, "Error!\r\n");
  59. }
  60. else {
  61. unsigned i;
  62. for (i = 0; i < 32; i++) {
  63. chprintf((BaseSequentialStream *)&PORTAB_SD1, "%02x", rand[i]);
  64. }
  65. chprintf((BaseSequentialStream *)&PORTAB_SD1, "\r\n");
  66. }
  67. err = trngGenerate(&TRNGD1, 15, rand);
  68. if (err) {
  69. chprintf((BaseSequentialStream *)&PORTAB_SD1, "Error!\r\n");
  70. }
  71. else {
  72. unsigned i;
  73. for (i = 0; i < 15; i++) {
  74. chprintf((BaseSequentialStream *)&PORTAB_SD1, "%02x", rand[i]);
  75. }
  76. chprintf((BaseSequentialStream *)&PORTAB_SD1, "\r\n");
  77. }
  78. err = trngGenerate(&TRNGD1, 2, rand);
  79. if (err) {
  80. chprintf((BaseSequentialStream *)&PORTAB_SD1, "Error!\r\n");
  81. }
  82. else {
  83. unsigned i;
  84. for (i = 0; i < 2; i++) {
  85. chprintf((BaseSequentialStream *)&PORTAB_SD1, "%02x", rand[i]);
  86. }
  87. chprintf((BaseSequentialStream *)&PORTAB_SD1, "\r\n");
  88. }
  89. trngStop(&TRNGD1);
  90. }
  91. chThdSleepMilliseconds(500);
  92. }
  93. return 0;
  94. }