Raysoft

Untitled

Apr 7th, 2020
633
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.35 KB | None | 0 0
  1. /**
  2.   ******************************************************************************
  3.   * @file    main.c
  4.   * @author  Raymond Mango
  5.   * @version V1.0
  6.   * @date    01-December-2013
  7.   * @brief   Default main function.
  8.   ******************************************************************************
  9. */
  10.  
  11. #include <stdio.h>
  12. #include <stdint.h>
  13. #include <string.h>
  14. #include "stm32f4xx.h"
  15. #include "FreeRTOS.h"
  16. #include "task.h"
  17. #include "queue.h"
  18. #include "timers.h"
  19.  
  20. TaskHandle_t xTaskhHandle1 = NULL;
  21. TaskHandle_t xTaskhHandle2 = NULL;
  22. TaskHandle_t xTaskhHandle3 = NULL;
  23. TaskHandle_t xTaskhHandle4 = NULL;
  24.  
  25. TimerHandle_t LedTimerHandle = NULL;
  26. void Led_Toggle( TimerHandle_t xTimer);
  27.  
  28. //Queue handle
  29. QueueHandle_t command_queue = NULL;
  30. QueueHandle_t uart_write_queue = NULL;
  31.  
  32. void vTask_menudisplay_Handler(void *params);
  33. void vTask_cmd_Handler(void *params);
  34. void vTask_cmd_processing_Handler(void *params);
  35. void vTask_uart_write_Handler(void *params);
  36.  
  37. void prvSetup_Hardware(void);
  38. void prvSetupGPIO();
  39. void rtos_delay(uint32_t delay_in_ms);
  40. uint8_t getCommandCode(uint8_t *buffer);
  41. void getArguments(uint8_t *buffer);
  42. void makeLedOn();
  43. void makeLedOff();
  44. void startToggle(uint32_t duration);
  45. void stopToggle();
  46. void readLedStatus(char *taskMsg);
  47. void readRtc(char *taskMsg);
  48. void printErrorMsg(char *taskMsg);
  49.  
  50. void printMsg(char *msg);
  51.  
  52. #define TRUE 1
  53. #define FALSE 0
  54. #define AVAILABLE TRUE
  55. #define NOT_AVAILABLE FALSE
  56. uint8_t UART_ACCESS_KEY = AVAILABLE;
  57.  
  58. #ifdef USE_SEMIHOSTING
  59. //enable arm semihosting
  60. extern void initialise_monitor_handles();
  61. #endif
  62.  
  63. char userbuf[250];
  64.  
  65. //command structure
  66. typedef struct APP_CMD
  67. {
  68.     uint8_t CommandNum;
  69.     uint8_t CommandArgs[10];
  70. }APP_CMD_t;
  71.  
  72. uint8_t commandBuffer[20];
  73. uint8_t commandLen = 0;
  74.  
  75. char menu[] = {"\
  76. \r\nLED_ON-------------------------> 1\
  77. \r\nLED_OFF------------------------> 2\
  78. \r\nLED_TOGGLE---------------------> 3\
  79. \r\nLED_TOGGLE_OFF-----------------> 4\
  80. \r\nLED_READ_STATUS----------------> 5\
  81. \r\nRTC_PRINT_DATETIME-------------> 6\
  82. \r\nEXIT_APP-----------------------> 0\
  83. \r\nType your option here:       "};
  84.  
  85.  
  86. #define LED_ON_COMMAND          1
  87. #define LED_OFF_COMMAND         2
  88. #define LED_TOGGLE_COMMAND      3
  89. #define LED_TOGGLE_OFF_COMMAND  4
  90. #define LED_READ_STATUS_COMMAND 5
  91. #define RTC_PRINT_DATETIME      6
  92.  
  93.  
  94. int main(void)
  95. {
  96. #ifdef USE_SEMIHOSTING
  97.     initialise_monitor_handles();
  98.  
  99.     printf("This is a test!!!!\n");
  100. #endif
  101.  
  102.     //enable the cycle counter
  103.     DWT->CTRL |= (1<<0);
  104. //reset RCC clock to default reset state,HSI ON, HSE OFF, SYS clock and CPU clock = 16MHz
  105.     RCC_DeInit();
  106.  
  107.     //update the system call clock variable
  108.     SystemCoreClockUpdate();
  109.  
  110.     prvSetup_Hardware();
  111.  
  112.  
  113.  
  114.     //Start Recording
  115.     SEGGER_SYSVIEW_Conf();
  116.     SEGGER_SYSVIEW_Start();
  117.  
  118.     //creating the queues;
  119.     command_queue = xQueueCreate(10, sizeof(APP_CMD_t*)); //this helps save heap memory
  120.     uart_write_queue = xQueueCreate(10, sizeof(char*));
  121.  
  122.     if((command_queue != NULL)&&(uart_write_queue != NULL)){
  123.         //creating tasks
  124.         xTaskCreate(vTask_menudisplay_Handler,
  125.                 "Task1",        /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
  126.                 500,
  127.                 NULL,
  128.                 2,
  129.                 &xTaskhHandle1 );
  130.  
  131.         xTaskCreate(vTask_cmd_Handler,
  132.                 "Task2",        /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
  133.                 500,
  134.                 NULL,
  135.                 2,
  136.                 &xTaskhHandle2 );
  137.  
  138.         xTaskCreate(vTask_cmd_processing_Handler,
  139.                 "Task3",        /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
  140.                 500,
  141.                 NULL,
  142.                 2,
  143.                 &xTaskhHandle3 );
  144.  
  145.         xTaskCreate(vTask_uart_write_Handler,
  146.                 "Task4",        /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
  147.                 500,
  148.                 NULL,
  149.                 2,
  150.                 &xTaskhHandle4 );
  151.  
  152.         vTaskStartScheduler();
  153.  
  154.  
  155.  
  156.     }else
  157.     {
  158.         printMsg("Queue Creation failed\r\n");
  159.  
  160.     }
  161.  
  162.  
  163.  
  164.     //will never reach here
  165.     for(;;);
  166. }
  167.  
  168. void vTask_menudisplay_Handler(void *params){
  169.  
  170.     char *pData = menu;
  171.  
  172.     while(1){
  173.  
  174.         xQueueSend(uart_write_queue, &pData, portMAX_DELAY );
  175.  
  176.         //lets wait here until someone notifies
  177.         xTaskNotifyWait(0,0,NULL,portMAX_DELAY);
  178.  
  179.  
  180.  
  181.     }
  182.  
  183.    //-specs=rdimon.specs -1c -1rdimon
  184. }
  185. void vTask_cmd_Handler(void *params){
  186.  
  187.     uint8_t command_code = 0;
  188.     APP_CMD_t *newCmd;
  189.  
  190.     while(1){
  191.         xTaskNotifyWait(0,0,NULL,portMAX_DELAY);
  192.         //send command to the queue
  193.         taskENTER_CRITICAL();
  194.         command_code = getCommandCode(commandBuffer);
  195.         newCmd = (APP_CMD_t*)pvPortMalloc(sizeof(APP_CMD_t));
  196.         newCmd->CommandNum = command_code;
  197.         getArguments(newCmd->CommandArgs);
  198.         taskEXIT_CRITICAL();
  199.         //send the command to the command queue
  200.         xQueueSend(command_queue, &newCmd, portMAX_DELAY );
  201.  
  202.  
  203.        }
  204.  
  205.  
  206. }
  207.  
  208. void vTask_cmd_processing_Handler(void *params){
  209.     APP_CMD_t *newCmd;
  210.     char taskMsg[50];
  211.     uint8_t toggle_duration = pdMS_TO_TICKS(500);
  212.     while(1){
  213.  
  214.         xQueueReceive(command_queue, (void*)&newCmd,portMAX_DELAY);
  215.         switch(newCmd->CommandNum)
  216.         {
  217.         case LED_ON_COMMAND:
  218.             makeLedOn();
  219.             break;
  220.         case LED_OFF_COMMAND:
  221.             makeLedOff();
  222.             break;
  223.         case LED_TOGGLE_COMMAND:
  224.             startToggle(500);
  225.             break;
  226.         case LED_TOGGLE_OFF_COMMAND:
  227.             stopToggle();
  228.             break;
  229.         case LED_READ_STATUS_COMMAND:
  230.             readLedStatus(taskMsg);
  231.             break;
  232.         case RTC_PRINT_DATETIME:
  233.             readRtc(taskMsg);
  234.             break;
  235.         default:
  236.             printErrorMsg(taskMsg);
  237.  
  238.         }
  239.  
  240.         //free dynamic memory
  241.         vPortFree(newCmd);
  242.  
  243.         }
  244.  
  245. }
  246.  
  247. void vTask_uart_write_Handler(void *params){
  248.  
  249.     char *pData = NULL;
  250.  
  251.     while(1){
  252.  
  253.         xQueueReceive(uart_write_queue, &pData, portMAX_DELAY);
  254.         printMsg(pData);
  255.  
  256.  
  257.         }
  258.  
  259.  
  260. }
  261.  
  262. void prvSetup_Hardware(){
  263.  
  264.     GPIO_InitTypeDef gpio_uart3_pins;
  265.     USART_InitTypeDef uart3Init;
  266.     //enable Uart3 and GPIOD Peripheral clock
  267.     RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
  268.     RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
  269.  
  270.     //configure alternate function of pins PD8-TX and PD9-RX
  271.     //zeroing each member of gpio structure
  272.     memset(&gpio_uart3_pins,0, sizeof(gpio_uart3_pins));
  273.  
  274.     gpio_uart3_pins.GPIO_Pin = GPIO_Pin_9;
  275.     gpio_uart3_pins.GPIO_Mode = GPIO_Mode_AF;
  276.     gpio_uart3_pins.GPIO_PuPd = GPIO_PuPd_UP;
  277.  
  278.     gpio_uart3_pins.GPIO_Pin = GPIO_Pin_8;
  279.     gpio_uart3_pins.GPIO_Mode = GPIO_Mode_AF;
  280.     gpio_uart3_pins.GPIO_PuPd = GPIO_PuPd_UP;
  281.  
  282.     GPIO_Init(GPIOD, &gpio_uart3_pins);
  283.  
  284.     //AF mode settings for the pins
  285.     GPIO_PinAFConfig(GPIOD, GPIO_PinSource8, GPIO_AF_USART3);
  286.     GPIO_PinAFConfig(GPIOD, GPIO_PinSource9, GPIO_AF_USART3);
  287.  
  288.     //Uart Parameter Initialization
  289.     //memset(&uart3Init,0, sizeof(uart3Init));
  290.     uart3Init.USART_BaudRate = 115200;
  291.     uart3Init.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  292.     uart3Init.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  293.     uart3Init.USART_Parity = USART_Parity_No;
  294.     uart3Init.USART_StopBits = USART_StopBits_1;
  295.     uart3Init.USART_WordLength = USART_WordLength_8b;
  296.     USART_Init(USART3, &uart3Init);
  297.  
  298.     //USART_CR1_UE = 1;
  299.     // enable uart byte reception interrupt
  300.     USART_ITConfig(USART3, USART_IT_RXNE, ENABLE );
  301.     //set priority
  302.     NVIC_SetPriority(USART3_IRQn, 6);
  303.     //enable the uart3 irq in the nvic
  304.     NVIC_EnableIRQ(USART3_IRQn);
  305.     //enable the usart3 peripheral
  306.     USART_Cmd(USART3, ENABLE);
  307.  
  308. }
  309.  
  310. void prvSetupGPIO(){
  311.  
  312.     GPIO_InitTypeDef Led_pin, Button_pin;
  313.     //enable GPIO-B&C Peripheral clocks
  314.     RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
  315.     RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
  316.  
  317.  
  318.  
  319.  
  320.  
  321.     Led_pin.GPIO_Mode = GPIO_Mode_OUT;
  322.     Led_pin.GPIO_OType = GPIO_OType_PP;
  323.     Led_pin.GPIO_Pin = GPIO_Pin_14;
  324.     Led_pin.GPIO_Speed = GPIO_Low_Speed;
  325.     Led_pin.GPIO_PuPd = GPIO_PuPd_NOPULL;
  326.     GPIO_Init(GPIOB, &Led_pin);
  327.  
  328.  
  329.     Button_pin.GPIO_Mode = GPIO_Mode_IN;
  330.     Button_pin.GPIO_OType = GPIO_OType_PP;
  331.     Button_pin.GPIO_Pin = GPIO_Pin_13;
  332.     Button_pin.GPIO_Speed = GPIO_Low_Speed;
  333.     Button_pin.GPIO_PuPd = GPIO_PuPd_NOPULL;
  334.     GPIO_Init(GPIOC, &Button_pin);
  335.  
  336. }
  337.  
  338. void rtos_delay(uint32_t delay_in_ms)
  339. {
  340.     uint32_t current_tick_count = xTaskGetTickCount();
  341.  
  342.     uint32_t delay_in_ticks = (delay_in_ms * configTICK_RATE_HZ)/1000;
  343.  
  344.     while(xTaskGetTickCount()<(current_tick_count + delay_in_ticks));
  345. }
  346.  
  347. void printMsg(char *msg){
  348.  
  349.     for(int i = 0; i < strlen(msg); i++ ){
  350.  
  351.         while(USART_GetFlagStatus(USART3, USART_FLAG_TXE)!= SET);//wait till data register is empty
  352.         USART_SendData(USART3, msg[i]);
  353.  
  354.  
  355.     }
  356.  
  357.  
  358. }
  359.  
  360. void USART3_IRQHandler(void){
  361.  
  362.     char taskMsg[20];
  363.     uint16_t rxByte;
  364.     BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  365.     char *pData = "ISR Awoken";
  366.     xQueueSend(uart_write_queue,&pData,portMAX_DELAY);
  367.     if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET){
  368.  
  369.         USART_ClearFlag(USART3, USART_FLAG_RXNE);
  370.         rxByte = USART_ReceiveData(USART3);
  371.         sprintf(taskMsg, "\r\nLED status is: ");
  372.         xQueueSend(uart_write_queue,&taskMsg,portMAX_DELAY);
  373.         commandBuffer[commandLen++] = (rxByte & 0xFF);
  374.  
  375.         if(rxByte == '\r'){
  376.  
  377.             commandLen = 0;
  378.             //if the enter key is pressed, the user is done entering the data
  379.             //notify the command handling task
  380.             xTaskNotifyFromISR(xTaskhHandle2,0,eNoAction,&xHigherPriorityTaskWoken);
  381.             xTaskNotifyFromISR(xTaskhHandle1,0,eNoAction,&xHigherPriorityTaskWoken);
  382.  
  383.         }
  384.  
  385.     }
  386.  
  387.     //if the above freertos apis woke up any higher priority task, then yield the processor to the higher priority task which was woken up
  388.  
  389.     if(xHigherPriorityTaskWoken)
  390.     {
  391.         taskYIELD();
  392.     }
  393.  
  394. }
  395.  
  396. uint8_t getCommandCode(uint8_t *buffer){
  397.  
  398.     return buffer[0]-48;
  399. }
  400.  
  401. void getArguments(uint8_t *buffer){
  402.  
  403.  
  404. }
  405.  
  406. void makeLedOn(){
  407.  
  408.     GPIO_WriteBit(GPIOB, GPIO_Pin_14, Bit_SET);
  409.  
  410. }
  411. void makeLedOff(){
  412.  
  413.     GPIO_WriteBit(GPIOB, GPIO_Pin_14, Bit_RESET);
  414.  
  415. }
  416.  
  417. void startToggle(uint32_t duration){
  418.  
  419.     if (LedTimerHandle == NULL)
  420.     {
  421.         //1.//create the software timer
  422.         //LedTimerHandle = xTimerCreate("LedTimer",duration, pdTRUE, Led_Toggle );
  423.     }else
  424.     {
  425.         //2.Start the software timer
  426.         xTimerStart(LedTimerHandle, portMAX_DELAY );
  427.     }
  428. }
  429.  
  430.  
  431. void stopToggle(){
  432.  
  433.     xTimerStop(LedTimerHandle, portMAX_DELAY );
  434.  
  435. }
  436.  
  437. void readLedStatus(char *taskMsg){
  438.  
  439.     char count[3];
  440.     sprintf(taskMsg, "\r\nLED status is: ");
  441.     itoa(GPIO_ReadOutputDataBit(GPIOB, GPIO_Pin_14),count,10);
  442.     count[1] = '\r';
  443.     count[2] = '\n';
  444.     xQueueSend(uart_write_queue,&taskMsg,portMAX_DELAY);
  445.     xQueueSend(uart_write_queue,&count,portMAX_DELAY);
  446. }
  447.  
  448. void readRtc(char *taskMsg){
  449.  
  450.  
  451.  
  452. }
  453.  
  454. void printErrorMsg(char *taskMsg){
  455.  
  456. }
  457. void Led_Toggle( TimerHandle_t xTimer){
  458.  
  459.     GPIO_ToggleBits(GPIOB, GPIO_Pin_14);
  460.  
  461. }
Advertisement
Add Comment
Please, Sign In to add comment