openwch/ch32v307

External Interrupt Example Not Working as I expected for other pins

mjs513 opened this issue · 1 comments

Been playing with the EXI example a couple of days and I think I may be missing something. When I tested it on PA0, PC0, PD0 and PE0 worked as expected. Everytime I hit the button connected to those pins I would see the interrupt fire (print a message).

When I tried to connect it to other pins like PA4, PA5, PA6, PC1 or PC2 it would fire only once even though the interrupt did show the flag cleared.

Here is the code I am using for pin PA1:

#include "debug.h"

/*********************************************************************
 * @fn      EXTI0_INT_INIT
 *
 * @brief   Initializes EXTI0 collection.
 *
 * @return  none
 */

void EXTI_INT_INIT(void)
{
    GPIO_InitTypeDef GPIO_InitStructure = {0};
    EXTI_InitTypeDef EXTI_InitStructure = {0};
    NVIC_InitTypeDef NVIC_InitStructure = {0};

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOA, ENABLE);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;  //pin change
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
    GPIO_Init(GPIOA, &GPIO_InitStructure); //pin change

    /* GPIOA ----> EXTI_Line0 */
    GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource1);    //port pin change

    EXTI_InitStructure.EXTI_Line = EXTI_Line1;    //pin change
    EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
    EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
    EXTI_InitStructure.EXTI_LineCmd = ENABLE;
    EXTI_Init(&EXTI_InitStructure);

    NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQn;     //interrupt change
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);

}

/*********************************************************************
 * @fn      main
 *
 * @brief   Main program.
 *
 * @return  none
 */
int main(void)
{
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
    SystemCoreClockUpdate();
    Delay_Init();
    USART_Printf_Init(115200);	
    printf("SystemClk:%d\r\n", SystemCoreClock);
    printf( "ChipID:%08x\r\n", DBGMCU_GetCHIPID() );
    printf("EXTI0 Test\r\n");
    EXTI_INT_INIT();

    while(1)
    {
        Delay_Ms(1000);
        printf("Run at main\r\n");
    }
}

and the IRQ looks like:

void EXTI1_IRQHandler(void)   //pin change
{
   printf("getflag: %d\r\n", EXTI_GetFlagStatus(EXTI_Line1));
  if(EXTI_GetITStatus(EXTI_Line1)!=RESET)     //pin change
  {
#if 1
    printf("Run at EXTI\r\n");

#endif
    EXTI_ClearFlag(EXTI_Line1);     /* Clear Flag */    //pin change
    printf("flag after clear flag: %d\r\n", EXTI_GetITStatus(EXTI_Line1));
  }
}

The end game to this is to write a function that mimics the arduino attachInterrupt function.

I think I got this resolved but need more testing.

I moved the interrupt from _it.c to the main program and added:

void EXTI1_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));

and at least it seems to be working for PA1 now.