exti working but something is fucked up in the job queue or tinyframe

This commit is contained in:
2018-01-26 23:01:19 +01:00
parent 255f96c754
commit 45b4fb45e9
10 changed files with 310 additions and 66 deletions
+77 -11
View File
@@ -2,14 +2,67 @@
// Created by MightyPork on 2017/11/26.
//
#include "platform.h"
#include <utils/avrlibc.h>
#include "hw_utils.h"
#include "macro.h"
#define PINS_COUNT 16
const uint32_t LL_SYSCFG_EXTI_PORTS[PORTS_COUNT] = {
LL_SYSCFG_EXTI_PORTA,
LL_SYSCFG_EXTI_PORTB,
LL_SYSCFG_EXTI_PORTC,
LL_SYSCFG_EXTI_PORTD,
LL_SYSCFG_EXTI_PORTE,
#if PORTS_COUNT>5
LL_SYSCFG_EXTI_PORTF,
#endif
#if PORTS_COUNT>6
LL_SYSCFG_EXTI_PORTG,
#endif
};
const uint32_t LL_SYSCFG_EXTI_LINES[16] = {
LL_SYSCFG_EXTI_LINE0,
LL_SYSCFG_EXTI_LINE1,
LL_SYSCFG_EXTI_LINE2,
LL_SYSCFG_EXTI_LINE3,
LL_SYSCFG_EXTI_LINE4,
LL_SYSCFG_EXTI_LINE5,
LL_SYSCFG_EXTI_LINE6,
LL_SYSCFG_EXTI_LINE7,
LL_SYSCFG_EXTI_LINE8,
LL_SYSCFG_EXTI_LINE9,
LL_SYSCFG_EXTI_LINE10,
LL_SYSCFG_EXTI_LINE11,
LL_SYSCFG_EXTI_LINE12,
LL_SYSCFG_EXTI_LINE13,
LL_SYSCFG_EXTI_LINE14,
LL_SYSCFG_EXTI_LINE15,
};
COMPILER_ASSERT(16 == ELEMENTS_IN_ARRAY(LL_SYSCFG_EXTI_LINES));
const uint32_t LL_EXTI_LINES[16] = {
LL_EXTI_LINE_0,
LL_EXTI_LINE_1,
LL_EXTI_LINE_2,
LL_EXTI_LINE_3,
LL_EXTI_LINE_4,
LL_EXTI_LINE_5,
LL_EXTI_LINE_6,
LL_EXTI_LINE_7,
LL_EXTI_LINE_8,
LL_EXTI_LINE_9,
LL_EXTI_LINE_10,
LL_EXTI_LINE_11,
LL_EXTI_LINE_12,
LL_EXTI_LINE_13,
LL_EXTI_LINE_14,
LL_EXTI_LINE_15,
};
COMPILER_ASSERT(16 == ELEMENTS_IN_ARRAY(LL_EXTI_LINES));
/** Pin number to LL bitfield mapping */
static const uint32_t ll_pins[PINS_COUNT] = {
const uint32_t LL_GPIO_PINS[16] = {
LL_GPIO_PIN_0,
LL_GPIO_PIN_1,
LL_GPIO_PIN_2,
@@ -27,10 +80,10 @@ static const uint32_t ll_pins[PINS_COUNT] = {
LL_GPIO_PIN_14,
LL_GPIO_PIN_15,
};
COMPILER_ASSERT(16 == ELEMENTS_IN_ARRAY(ll_pins));
COMPILER_ASSERT(16 == ELEMENTS_IN_ARRAY(LL_GPIO_PINS));
/** Port number (A=0) to config struct pointer mapping */
static GPIO_TypeDef * const port_periphs[] = {
GPIO_TypeDef * const GPIO_PERIPHS[PORTS_COUNT] = {
GPIOA,
GPIOB,
GPIOC,
@@ -43,20 +96,20 @@ static GPIO_TypeDef * const port_periphs[] = {
GPIOG,
#endif
};
COMPILER_ASSERT(PORTS_COUNT == ELEMENTS_IN_ARRAY(port_periphs));
COMPILER_ASSERT(PORTS_COUNT == ELEMENTS_IN_ARRAY(GPIO_PERIPHS));
/** Convert pin number to LL bitfield */
uint32_t hw_pin2ll(uint8_t pin_number, bool *suc)
{
assert_param(suc != NULL);
if(pin_number >= PINS_COUNT) {
if(pin_number > 15) {
dbg("Bad pin: %d", pin_number);
// TODO proper report
*suc = false;
return 0;
}
return ll_pins[pin_number];
return LL_GPIO_PINS[pin_number];
}
/** Convert port name (A,B,C...) to peripheral struct pointer */
@@ -71,7 +124,7 @@ GPIO_TypeDef *hw_port2periph(char port_name, bool *suc)
}
uint8_t num = (uint8_t) (port_name - 'A');
return port_periphs[num];
return GPIO_PERIPHS[num];
}
/** Convert a pin to resource handle */
@@ -85,7 +138,7 @@ Resource hw_pin2resource(char port_name, uint8_t pin_number, bool *suc)
return R_NONE;
}
if(pin_number >= PINS_COUNT) {
if(pin_number > 15) {
dbg("Bad pin: %d", pin_number); // TODO proper report
*suc = false;
return R_NONE;
@@ -261,14 +314,27 @@ uint16_t pinmask_pack(uint16_t spread, uint16_t mask)
return result;
}
/** Convert spread port pin number to a packed index using a mask */
uint8_t pinmask_translate(uint16_t mask, uint8_t index)
{
int cnt = 0;
for (int i = 0; i<16; i++) {
if (mask & (1<<i)) {
if (i == index) return (uint8_t) cnt;
cnt++;
}
}
return 0;
}
/** Configure unit pins as analog (part of unit teardown) */
void hw_deinit_unit_pins(Unit *unit)
{
for (uint32_t rsc = R_PA0; rsc <= R_PF15; rsc++) {
if (RSC_IS_HELD(unit->resources, rsc)) {
rsc_dbg("Freeing pin %s", rsc_get_name((Resource)rsc));
GPIO_TypeDef *port = port_periphs[(rsc-R_PA0) / 16];
uint32_t ll_pin = ll_pins[(rsc-R_PA0)%16];
GPIO_TypeDef *port = GPIO_PERIPHS[(rsc-R_PA0) / 16];
uint32_t ll_pin = LL_GPIO_PINS[(rsc-R_PA0)%16];
LL_GPIO_SetPinMode(port, ll_pin, LL_GPIO_MODE_ANALOG);
}
}
+13
View File
@@ -11,6 +11,12 @@
#include "platform.h"
#include "resources.h"
extern const uint32_t LL_SYSCFG_EXTI_PORTS[PORTS_COUNT];
extern const uint32_t LL_SYSCFG_EXTI_LINES[16];
extern GPIO_TypeDef * const GPIO_PERIPHS[PORTS_COUNT];
extern const uint32_t LL_GPIO_PINS[16];
extern const uint32_t LL_EXTI_LINES[16];
/**
* Convert pin number to LL driver bitfield for working with the pin.
*
@@ -97,6 +103,13 @@ uint16_t pinmask_spread(uint16_t packed, uint16_t mask);
*/
uint16_t pinmask_pack(uint16_t spread, uint16_t mask);
/**
* Convert spread port pin number to a packed index using a mask
*
* eg. with a mask 0b1010 and index 3, the result is 1 (bit 1 of the packed - 0bX0)
*/
uint8_t pinmask_translate(uint16_t mask, uint8_t index);
/**
* Set all GPIO resources held by unit to analog.
* This is a part of unit teardown.
+68 -46
View File
@@ -2,9 +2,29 @@
// Created by MightyPork on 2018/01/14.
//
#include <utils/hexdump.h>
#include "platform.h"
#include "irq_dispatcher.h"
#include "hw_utils.h"
void * const EXTIS[16] = {
// dumym values for easier debug - those are used instead of periph addresses because EXTIs have no dedicated periph control blocks
(void *const) 0x9000,
(void *const) 0x9001,
(void *const) 0x9002,
(void *const) 0x9003,
(void *const) 0x9004,
(void *const) 0x9005,
(void *const) 0x9006,
(void *const) 0x9007,
(void *const) 0x9008,
(void *const) 0x9009,
(void *const) 0x900A,
(void *const) 0x900B,
(void *const) 0x900C,
(void *const) 0x900D,
(void *const) 0x900E,
(void *const) 0x900F,
};
struct cbslot {
IrqCallback callback;
@@ -12,6 +32,8 @@ struct cbslot {
};
static struct callbacks_ {
struct cbslot exti[16];
struct cbslot usart1;
struct cbslot usart2;
struct cbslot usart3;
@@ -51,9 +73,9 @@ void irqd_init(void)
// NVIC_EnableIRQ(RTC_IRQn); /*!< RTC Interrupt through EXTI Lines 17, 19 and 20 */
// NVIC_EnableIRQ(FLASH_IRQn); /*!< FLASH global Interrupt */
// NVIC_EnableIRQ(RCC_CRS_IRQn); /*!< RCC & CRS global Interrupt */
// NVIC_EnableIRQ(EXTI0_1_IRQn); /*!< EXTI Line 0 and 1 Interrupt */
// NVIC_EnableIRQ(EXTI2_3_IRQn); /*!< EXTI Line 2 and 3 Interrupt */
// NVIC_EnableIRQ(EXTI4_15_IRQn); /*!< EXTI Line 4 to 15 Interrupt */
NVIC_EnableIRQ(EXTI0_1_IRQn); /*!< EXTI Line 0 and 1 Interrupt */
NVIC_EnableIRQ(EXTI2_3_IRQn); /*!< EXTI Line 2 and 3 Interrupt */
NVIC_EnableIRQ(EXTI4_15_IRQn); /*!< EXTI Line 4 to 15 Interrupt */
// NVIC_EnableIRQ(TSC_IRQn); /*!< Touch Sensing Controller Interrupts */
NVIC_EnableIRQ(DMA1_Channel1_IRQn); /*!< DMA1 Channel 1 Interrupt */
NVIC_EnableIRQ(DMA1_Channel2_3_IRQn); /*!< DMA1 Channel 2 and Channel 3 Interrupt */
@@ -107,6 +129,10 @@ static struct cbslot *get_slot_for_periph(void *periph)
else if (periph == USART5) slot = &callbacks.usart5;
#endif
else if (periph >= EXTIS[0] && periph <= EXTIS[15]) {
slot = &callbacks.exti[periph - EXTIS[0]];
}
// --- DMA1 ---
else if (periph == DMA1_Channel1) slot = &callbacks.dma1_1;
else if (periph == DMA1_Channel2) slot = &callbacks.dma1_2;
@@ -154,12 +180,16 @@ void irqd_attach(void *periph, IrqCallback callback, void *arg)
slot->arg = arg;
}
void irqd_detach(void *periph, IrqCallback callback)
void* irqd_detach(void *periph, IrqCallback callback)
{
void *oldArg = NULL;
struct cbslot *slot = get_slot_for_periph(periph);
if (slot->callback == callback) {
oldArg = slot->arg;
slot->callback = NULL;
slot->arg = NULL;
return oldArg;
} else {
trap("Detach IRQ %p() from %p but %p() bound instead", callback, periph, slot->callback);
}
@@ -167,6 +197,8 @@ void irqd_detach(void *periph, IrqCallback callback)
#define CALL_IRQ_HANDLER(slot) do { if (slot.callback) slot.callback(slot.arg); } while (0)
// ------------ USARTS ------------
void USART1_IRQHandler(void)
{
CALL_IRQ_HANDLER(callbacks.usart1);
@@ -177,32 +209,6 @@ void USART2_IRQHandler(void)
CALL_IRQ_HANDLER(callbacks.usart2);
}
#if 0
static bool usart_check_irq(USART_TypeDef *USARTx)
{
uint32_t isrflags = USARTx->ISR;
uint32_t cr1its = USARTx->CR1;
uint32_t cr2its = USARTx->CR2;
uint32_t cr3its = USARTx->CR3;
if ((cr1its & USART_CR1_RTOIE) && (isrflags & USART_ISR_RTOF)) return true;
if ((cr1its & USART_CR1_RXNEIE) && (isrflags & USART_ISR_RXNE)) return true;
if ((cr1its & USART_CR1_TCIE) && (isrflags & USART_ISR_TC)) return true;
if ((cr1its & USART_CR1_TXEIE) && (isrflags & USART_ISR_TXE)) return true;
if ((cr3its & USART_CR3_EIE) && (isrflags & (USART_ISR_PE | USART_ISR_FE | USART_ISR_ORE | USART_ISR_NE))) return true;
if ((cr1its & USART_CR1_IDLEIE) && (isrflags & USART_ISR_IDLE)) return true;
if ((cr1its & USART_CR1_PEIE) && (isrflags & USART_ISR_PE)) return true;
if ((cr1its & USART_CR1_CMIE) && (isrflags & USART_ISR_CMF)) return true;
if ((cr1its & USART_CR1_EOBIE) && (isrflags & USART_ISR_EOBF)) return true;
if ((cr2its & USART_CR2_LBDIE) && (isrflags & USART_ISR_LBDF)) return true;
if ((cr3its & USART_CR3_CTSIE) && (isrflags & USART_ISR_CTS)) return true;
if ((cr3its & USART_CR3_WUFIE) && (isrflags & USART_ISR_WUF)) return true;
return false;
}
#endif
void USART3_4_IRQHandler(void)
{
// we won't check the flags here, both can be pending and it's better to let the handler deal with it
@@ -210,6 +216,8 @@ void USART3_4_IRQHandler(void)
CALL_IRQ_HANDLER(callbacks.usart4);
}
// ------------ DMA --------------
void DMA1_Channel1_IRQHandler(void)
{
CALL_IRQ_HANDLER(callbacks.dma1_1);
@@ -229,6 +237,35 @@ void DMA1_Channel4_5_6_7_IRQHandler(void)
if (LL_DMA_IsActiveFlag_GI7(DMA1)) CALL_IRQ_HANDLER(callbacks.dma1_7);
}
// ------------ EXTI ------------
static void fire_exti_cb(uint8_t start, uint8_t end)
{
for (int i = start; i <= end; i++) {
if (LL_EXTI_IsActiveFlag_0_31(LL_EXTI_LINES[i])) {
CALL_IRQ_HANDLER(callbacks.exti[i]);
}
}
}
void EXTI0_1_IRQHandler(void)
{
fire_exti_cb(0, 1);
}
void EXTI2_3_IRQHandler(void)
{
fire_exti_cb(2, 3);
}
void EXTI4_15_IRQHandler(void)
{
fire_exti_cb(4, 15);
}
// other ISRs...
#if 0
void WWDG_IRQHandler(void)
{
@@ -255,21 +292,6 @@ void RCC_CRS_IRQHandler(void)
//
}
void EXTI0_1_IRQHandler(void)
{
//
}
void EXTI2_3_IRQHandler(void)
{
//
}
void EXTI4_15_IRQHandler(void)
{
//
}
void TSC_IRQHandler(void)
{
//
+5 -1
View File
@@ -7,6 +7,9 @@
#ifndef GEX_F072_IRQ_DISPATCHER_H
#define GEX_F072_IRQ_DISPATCHER_H
// Dummy peripherals for use with the
extern void * const EXTIS[16];
/**
* Initialize the interrupt dispatcher
*/
@@ -35,7 +38,8 @@ void irqd_attach(void *periph, IrqCallback callback, void *data);
*
* @param periph - peripheral we're attaching to
* @param callback - callback to remove, if it doesn't match, do nothing
* @return the arg, if any
*/
void irqd_detach(void *periph, IrqCallback callback);
void* irqd_detach(void *periph, IrqCallback callback);
#endif //GEX_F072_IRQ_DISPATCHER_H
+1 -1
View File
@@ -24,7 +24,7 @@
#define FLASH_SAVE_BUF_LEN 128 // Malloc'd buffer for saving to flash
#define MSG_QUE_SLOT_SIZE 64 // FIXME this should be possible to lower, but there's some bug with bulk transfer / INI parser
#define RX_QUE_CAPACITY 8 // TinyFrame rx queue size (64 bytes each)
#define RX_QUE_CAPACITY 16 // TinyFrame rx queue size (64 bytes each)
#define TF_MAX_PAYLOAD_RX 512 // TF max Rx payload
#define TF_SENDBUF_LEN 64 // TF transmit buffer (can be less than a full frame)