Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // exti.c
- //
- #include "exti.h"
- #include <stm32f4xx.h>
- #include <string.h>
- typedef struct
- {
- TPin Pin;
- bool Enabled;
- TSignalEdge Edge;
- void (* Handler)(void);
- } TEXTIRecord;
- static const int HandlerCount = 16;
- static TEXTIRecord EXTIHandlerList[HandlerCount];
- static void EXTI_IRQHandler(TEXTIRecord * Record)
- {
- if(Record->Handler) Record->Handler();
- }
- void EXTI0_IRQHandler(void) { EXTI->PR = (1 << 0); EXTI_IRQHandler(&EXTIHandlerList[0]); }
- void EXTI1_IRQHandler(void) { EXTI->PR = (1 << 1); EXTI_IRQHandler(&EXTIHandlerList[1]); }
- void EXTI2_IRQHandler(void) { EXTI->PR = (1 << 2); EXTI_IRQHandler(&EXTIHandlerList[2]); }
- void EXTI3_IRQHandler(void) { EXTI->PR = (1 << 3); EXTI_IRQHandler(&EXTIHandlerList[3]); }
- void EXTI4_IRQHandler(void) { EXTI->PR = (1 << 4); EXTI_IRQHandler(&EXTIHandlerList[4]); }
- void EXTI5_9_IRQHandler(void)
- {
- int i;
- for(i = 5; i < 10; i++)
- {
- if(EXTI->PR & (1 << i))
- {
- EXTI->PR = (1 << i);
- EXTI_IRQHandler(&EXTIHandlerList[i]);
- }
- }
- }
- void EXTI10_15_IRQHandler(void)
- {
- int i;
- for(i = 10; i < 16; i++)
- {
- if(EXTI->PR & (1 << i))
- {
- EXTI->PR = (1 << i);
- EXTI_IRQHandler(&EXTIHandlerList[i]);
- }
- }
- }
- void exti_Init(void)
- {
- RCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN;
- }
- void exti_Main(void)
- {
- }
- static void exti_EnableInterrupt(uint8_t Index)
- {
- EXTI->IMR |= (1 << Index);
- switch(Index)
- {
- case 0: NVIC_EnableIRQ(EXTI0_IRQn); break;
- case 1: NVIC_EnableIRQ(EXTI1_IRQn); break;
- case 2: NVIC_EnableIRQ(EXTI2_IRQn); break;
- case 3: NVIC_EnableIRQ(EXTI3_IRQn); break;
- case 4: NVIC_EnableIRQ(EXTI4_IRQn); break;
- case 5:
- case 6:
- case 7:
- case 8:
- case 9: NVIC_EnableIRQ(EXTI9_5_IRQn); break;
- case 10:
- case 11:
- case 12:
- case 13:
- case 14:
- case 15: NVIC_EnableIRQ(EXTI15_10_IRQn); break;
- }
- }
- // void Handler(void);
- bool exti_Listen(const TPin * Pin, TSignalEdge Edge, void * Handler)
- {
- uint8_t Register;
- uint8_t Shift;
- uint32_t Temp;
- uint8_t Index;
- Index = Pin->Pin;
- if(Index > 15) return false;
- if(Pin->Port > PI) return false;
- Register = Index >> 2;
- Shift = (Index & 0x03) * 4;
- // Выберем порт для прослушивания
- Temp = SYSCFG->EXTICR[Register];
- Temp &= ~(0xF << Shift);
- Temp |= (Pin->Port << Shift);
- SYSCFG->EXTICR[Register] = Temp;
- EXTIHandlerList[Index].Edge = Edge;
- EXTIHandlerList[Index].Enabled = true;
- EXTIHandlerList[Index].Handler = (void(*)(void))Handler;
- memcpy(&EXTIHandlerList[Index].Pin, Pin, sizeof(TPin));
- switch(Edge)
- {
- case EDGE_RISING:
- EXTI->RTSR |= (1 << Index);
- EXTI->FTSR &= ~(1 << Index);
- break;
- case EDGE_FALLING:
- EXTI->RTSR &= ~(1 << Index);
- EXTI->FTSR |= (1 << Index);
- break;
- case EDGE_BOTH:
- EXTI->RTSR |= (1 << Index);
- EXTI->FTSR |= (1 << Index);
- break;
- }
- exti_EnableInterrupt(Index);
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement