PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/RTOSThread/src/main.c

https://gitlab.com/aoscarius/embedded-exercise
C | 98 lines | 73 code | 22 blank | 3 comment | 0 complexity | 242a2b829d2e0a0ed119257ebdce9500 MD5 | raw file
  1. #include "stm32f4xx.h"
  2. #include "stm32f4xx_hal_gpio.h"
  3. #include "cmsis_os.h"
  4. #define LED_GREEN GPIO_PIN_12 // LED Verde (Left)
  5. #define LED_ORANGE GPIO_PIN_13 // LED Arancio (Up)
  6. #define LED_RED GPIO_PIN_14 // LED Rosso (Right)
  7. #define LED_BLUE GPIO_PIN_15 // LED Blu (Down)
  8. #define LEDS (LED_GREEN | LED_ORANGE | LED_RED | LED_BLUE)
  9. void threadGREEN(void *args);
  10. void threadORANGE(void *args);
  11. void threadRED(void *args);
  12. void threadBLUE(void *args);
  13. void threadMAIN(void *args);
  14. osThreadDef(LEDG, threadGREEN, osPriorityNormal, 0, configMINIMAL_STACK_SIZE);
  15. osThreadDef(LEDO, threadORANGE, osPriorityNormal, 0, configMINIMAL_STACK_SIZE);
  16. osThreadDef(LEDR, threadRED, osPriorityNormal, 0, configMINIMAL_STACK_SIZE);
  17. osThreadDef(LEDB, threadBLUE, osPriorityNormal, 0, configMINIMAL_STACK_SIZE);
  18. osThreadDef(MAIN, threadMAIN, osPriorityNormal, 0, configMINIMAL_STACK_SIZE);
  19. void init(void);
  20. void main(void *args)
  21. {
  22. init();
  23. osThreadCreate (osThread(MAIN), NULL);
  24. osKernelStart();
  25. // In this loop i will enter if one thread will stop the kernel
  26. while(1){
  27. }
  28. }
  29. void init(void){
  30. GPIO_InitTypeDef GPIO_InitStruct;
  31. /* Enable the GPIO_LED Clock */
  32. __GPIOD_CLK_ENABLE();
  33. /* Configure the GPIO_LED pins */
  34. GPIO_InitStruct.Pin = LEDS;
  35. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  36. GPIO_InitStruct.Pull = GPIO_PULLDOWN;
  37. GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
  38. HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
  39. HAL_GPIO_WritePin(GPIOD, LEDS, GPIO_PIN_RESET);
  40. }
  41. void threadGREEN(void *args){
  42. while(1){
  43. HAL_GPIO_TogglePin(GPIOD, LED_GREEN);
  44. HAL_Delay(250);
  45. }
  46. }
  47. void threadORANGE(void *args){
  48. while(1){
  49. HAL_GPIO_TogglePin(GPIOD, LED_ORANGE);
  50. HAL_Delay(500);
  51. }
  52. }
  53. void threadRED(void *args){
  54. while(1){
  55. HAL_GPIO_TogglePin(GPIOD, LED_RED);
  56. HAL_Delay(1000);
  57. }
  58. }
  59. void threadBLUE(void *args){
  60. while(1){
  61. HAL_GPIO_TogglePin(GPIOD, LED_BLUE);
  62. HAL_Delay(2000);
  63. }
  64. }
  65. void threadMAIN(void *args){
  66. osThreadCreate (osThread(LEDG), NULL);
  67. HAL_Delay(2000);
  68. osThreadCreate (osThread(LEDO), NULL);
  69. HAL_Delay(2000);
  70. osThreadCreate (osThread(LEDR), NULL);
  71. HAL_Delay(2000);
  72. osThreadCreate (osThread(LEDB), NULL);
  73. HAL_Delay(2000);
  74. while(1){
  75. }
  76. }