cryarchy

A9G Interrupt Test

Jan 11th, 2019
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.12 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. #include <api_os.h>
  6. #include <api_event.h>
  7. #include <api_hal_gpio.h>
  8. #include <api_hal_pm.h>
  9.  
  10. #define MAIN_TASK_STACK_SIZE (1024 * 2)
  11. #define MAIN_TASK_PRIORITY 0
  12. #define MAIN_TASK_NAME "Main Task"
  13.  
  14. static HANDLE mainTaskHandle = NULL;
  15. static uint8_t preSmsEventsCount = 0;
  16.  
  17. int frequency = 0;
  18.  
  19. void EventDispatch(API_Event_t* pEvent) {
  20.   switch(pEvent->id) {
  21.     case API_EVENT_ID_NO_SIMCARD:
  22.         Trace(10,"!!NO SIM CARD%d!!!!",pEvent->param1);
  23.         break;
  24.  
  25.     case API_EVENT_ID_SYSTEM_READY:
  26.         Trace(1,"system initialize complete");
  27.         preSmsEventsCount |= 1;
  28.         break;
  29.  
  30.     case API_EVENT_ID_NETWORK_REGISTERED_HOME:
  31.     case API_EVENT_ID_NETWORK_REGISTERED_ROAMING:
  32.         Trace(2,"network register success");
  33.         preSmsEventsCount |= 2;
  34.         Network_StartAttach();
  35.         break;
  36.  
  37.     case API_EVENT_ID_NETWORK_ATTACHED:
  38.         Trace(2,"network attach success");
  39.         Network_PDP_Context_t context = {
  40.             .apn        ="internet",
  41.             .userName   = "",
  42.             .userPasswd = ""
  43.         };
  44.         Network_StartActive(context);
  45.         break;
  46.  
  47.     case API_EVENT_ID_NETWORK_ACTIVATED:
  48.         Trace(2,"network activate success");
  49.         break;
  50.  
  51.     default:
  52.         break;
  53.   }
  54. }
  55.  
  56. void OnPinFalling(GPIO_INT_callback_param_t* param) {
  57.     // Trace(1,"OnPinFalling");
  58.     switch(param->pin) {
  59.         case GPIO_PIN3:
  60.             frequency++;
  61.             break;
  62.         default:
  63.             break;
  64.     }
  65. }
  66.  
  67. void sampleSpeed(void* param) {
  68.     Trace(1, "Frequency: %d", frequency);
  69.     // reset frequency
  70.     frequency = 0;
  71.     // reschedule the speed sampler for per second
  72.     OS_StartCallbackTimer(mainTaskHandle, 1000, sampleSpeed, NULL);
  73. }
  74.  
  75. void mainTask(void *pData) {
  76.     API_Event_t* event=NULL;
  77.     // enable power for used pins
  78.     PM_PowerEnable(POWER_TYPE_VPAD, true);
  79.     PM_PowerEnable(POWER_TYPE_LCD, true);
  80.     // configure frequency pin
  81.     GPIO_config_t intPin = {
  82.         .mode               = GPIO_MODE_INPUT_INT,
  83.         .pin                = GPIO_PIN3,
  84.         .defaultLevel       = GPIO_LEVEL_LOW,
  85.         .intConfig.debounce = 0,
  86.         .intConfig.type     = GPIO_INT_TYPE_FALLING_EDGE,
  87.         .intConfig.callback = OnPinFalling
  88.     };
  89.     GPIO_Init(intPin);
  90.     // schedule the speed sampler for per second
  91.     OS_StartCallbackTimer(mainTaskHandle, 1000, sampleSpeed, NULL);
  92.     // infinite loop
  93.     while(1) {
  94.         // if an event is received
  95.         if(OS_WaitEvent(mainTaskHandle, (void**)&event, OS_TIME_OUT_WAIT_FOREVER)) {
  96.             // signal the event
  97.             EventDispatch(event);
  98.             // clean up
  99.             OS_Free(event->pParam1);
  100.             OS_Free(event->pParam2);
  101.             OS_Free(event);
  102.         }
  103.     }
  104. }
  105.  
  106. void speed_governor_5_Main(void) {
  107.     // create the main task
  108.     mainTaskHandle = OS_CreateTask(mainTask,
  109.         NULL, NULL, MAIN_TASK_STACK_SIZE, MAIN_TASK_PRIORITY, 0, 0, MAIN_TASK_NAME);
  110.     // set up the main task to receive messages
  111.     OS_SetUserMainHandle(&mainTaskHandle);
  112. }
Advertisement
Add Comment
Please, Sign In to add comment