From 368e4fc1662c2594b2a0908900713a2555a3ed8e Mon Sep 17 00:00:00 2001 From: Michael Hope Date: Wed, 29 Sep 2010 20:42:18 +1300 Subject: Fixed up the build due to a missing header file. --- libmaple/nvic.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'libmaple/nvic.h') diff --git a/libmaple/nvic.h b/libmaple/nvic.h index 4f4972d..9e81a0e 100644 --- a/libmaple/nvic.h +++ b/libmaple/nvic.h @@ -29,6 +29,10 @@ #ifndef _NVIC_H_ #define _NVIC_H_ +#ifdef __cplusplus +extern "C"{ +#endif + #define NVIC_INT_USBHP 19 #define NVIC_INT_USBLP 20 #define NVIC_EXTI1_OFFSET (NVIC_ISER0 + 0x07) @@ -52,10 +56,6 @@ #define NVIC_VectTab_RAM ((u32)0x20000000) #define NVIC_VectTab_FLASH ((u32)0x08000000) -#ifdef __cplusplus -extern "C"{ -#endif - enum { NVIC_TIMER1 = 27, NVIC_TIMER2 = 28, -- cgit v1.2.3 From 8e973f3d1ef0324e213824dc05af0f9713e7b3cb Mon Sep 17 00:00:00 2001 From: Marti Bolivar Date: Wed, 29 Dec 2010 21:30:42 -0500 Subject: Changed nzmichaelh's initial DMA interface to be more flexible. Some bugfixes in the external interrupt code were found along the way. Defines for nonexistent registers removed from nvic interface. --- libmaple/dma.c | 112 ++++++++++++++++++++++++++++++++++++++------------------ libmaple/dma.h | 57 ++++++++++++++++++++++++---- libmaple/exti.c | 8 ++-- libmaple/exti.h | 5 ++- libmaple/nvic.c | 12 ++++-- libmaple/nvic.h | 18 ++++++--- 6 files changed, 153 insertions(+), 59 deletions(-) (limited to 'libmaple/nvic.h') diff --git a/libmaple/dma.c b/libmaple/dma.c index 88c1342..a6c6ccd 100644 --- a/libmaple/dma.c +++ b/libmaple/dma.c @@ -31,76 +31,118 @@ #include "libmaple.h" #include "dma.h" #include "rcc.h" +#include "nvic.h" #define DMA_EN BIT(0) -typedef struct dma_regs -{ +typedef struct dma_regs { uint32 CCR; uint32 CNDTR; uint32 CPAR; uint32 CMAR; } dma_regs; +typedef struct DMAChannel { + void (*handler)(void); + uint32 irq_line; +} DMAChannel; + +static DMAChannel dma_channels[] = { + { .handler = NULL, .irq_line = NVIC_DMA_CH1 }, + { .handler = NULL, .irq_line = NVIC_DMA_CH2 }, + { .handler = NULL, .irq_line = NVIC_DMA_CH3 }, + { .handler = NULL, .irq_line = NVIC_DMA_CH4 }, + { .handler = NULL, .irq_line = NVIC_DMA_CH5 }, + { .handler = NULL, .irq_line = NVIC_DMA_CH6 }, + { .handler = NULL, .irq_line = NVIC_DMA_CH7 } +}; + /** Get the base address of the given channel, asserting and returning * NULL on illegal */ static dma_regs *dma_get_regs(uint8 channel) { if (channel >= 1 && channel <= 7) { return (dma_regs *)(DMA1_CCR1 + DMA_CHANNEL_STRIDE * (channel-1)); - } - else { + } else { ASSERT(false); return NULL; } } -/** Initialise a DMA channel. Start the transfer using dma_start(). - * - * @param channel the channel number (1..7) - * @param peripheral address of the peripheral - * @param from_peripheral true if transfer goes from the peripheral - * to memory - * @param mode OR of the dma_mode_flags - */ -void dma_init(uint8 channel, volatile void *peripheral, int from_peripheral, +/* Zero-based indexing */ +static inline void dispatch_handler(uint8 channel_idx) { + ASSERT(dma_channels[channel_idx].handler); + if (dma_channels[channel_idx].handler) { + (dma_channels[channel_idx].handler)(); + } +} + +void DMAChannel1_IRQHandler(void) { + dispatch_handler(0); +} + +void DMAChannel2_IRQHandler(void) { + dispatch_handler(1); +} + +void DMAChannel3_IRQHandler(void) { + dispatch_handler(2); +} + +void DMAChannel4_IRQHandler(void) { + dispatch_handler(3); +} + +void DMAChannel5_IRQHandler(void) { + dispatch_handler(4); +} + +void DMAChannel6_IRQHandler(void) { + dispatch_handler(5); +} + +void DMAChannel7_IRQHandler(void) { + dispatch_handler(6); +} + +void dma_init(uint8 channel, volatile void *paddr, + dma_transfer_size psize, dma_transfer_size msize, dma_mode_flags mode) { volatile dma_regs *regs = dma_get_regs(channel); if (regs != NULL) { rcc_clk_enable(RCC_DMA1); - /* Disable the channel. PENDING: May not be needed */ - regs->CCR = 0; + regs->CCR = ((0 << 12) /* Low priority */ + | (msize << 10) + | (psize << 8) + | (0 << 0) /* Disabled (until started) */ + | mode); - uint32 cr = 0 - | (0 << 12) // Low priority - | (0 << 10) // Memory size = 8 bits - | (1 << 8) // Peripheral size = 16 bits - | (mode << 5) // Increment and circular modes - | (0 << 0); // Not enabled - - /* FIXME XXX integer from pointer without a cast. */ - regs->CPAR = peripheral; - - if (!from_peripheral) { - cr |= 1 << 4; // From memory - } - - /* Stay disabled until started */ - regs->CCR = cr; + regs->CPAR = (uint32)paddr; } } -void dma_start(uint8 channel, volatile void *buffer, uint16 count) -{ +void dma_start(uint8 channel, volatile void *buffer, uint16 count) { volatile dma_regs *regs = dma_get_regs(channel); if (regs != NULL) { - regs->CCR &= ~DMA_EN; + regs->CCR &= ~DMA_EN; /* CMAR may not be written with EN set */ regs->CMAR = (uint32)buffer; regs->CNDTR = count; - regs->CCR |= DMA_EN; + regs->CCR |= DMA_EN; /* Start the transfer */ } } + +void dma_attach_interrupt(uint8 channel, voidFuncPtr handler) { + channel--; /* 1-based -> 0-based indexing */ + dma_channels[channel].handler = handler; + nvic_irq_enable(dma_channels[channel].irq_line); +} + +void dma_detach_interrupt(uint8 channel) { + channel--; + nvic_irq_disable(dma_channels[channel].irq_line); + dma_channels[channel].handler = NULL; +} diff --git a/libmaple/dma.h b/libmaple/dma.h index 8a6ca34..7072a1a 100644 --- a/libmaple/dma.h +++ b/libmaple/dma.h @@ -26,11 +26,15 @@ * @file dma.h * * @brief Direct Memory Access peripheral support + * + * TODO: add DMA2 support for high-density devices. */ #ifndef _DMA_H_ #define _DMA_H_ +#include "libmaple_types.h" + #ifdef __cplusplus extern "C"{ #endif @@ -39,7 +43,7 @@ extern "C"{ #define DMA1_BASE 0x40020000 /** DMA Interrupt Status Register */ #define DMA1_ISR (DMA1_BASE + 0x00) -/** DMA Interrput Flag Clear Register */ +/** DMA Interrupt Flag Clear Register */ #define DMA1_IFCR (DMA1_BASE + 0x04) /** DMA Channel Configuration Register */ #define DMA1_CCR1 (DMA1_BASE + 0x08) @@ -52,19 +56,56 @@ extern "C"{ /** Spacing between channel registers */ #define DMA_CHANNEL_STRIDE 20 +/** Flags for DMA transfer configuration. */ typedef enum dma_mode_flags { - /** Auto-increment memory address */ - DMA_MINC_MODE = 4, - /** Auto-increment peripheral address */ - DMA_PINC_MODE = 2, - /** Circular mode */ - DMA_CIRC_MODE = 1, + DMA_MINC_MODE = 1 << 7, /**< Auto-increment memory address */ + DMA_PINC_MODE = 1 << 6, /**< Auto-increment peripheral address */ + DMA_CIRC_MODE = 1 << 5, /**< Circular mode */ + DMA_FROM_MEM = 1 << 4, /**< Read from memory to peripheral */ + DMA_TRNS_ERR = 1 << 3, /**< Interrupt on transfer error */ + DMA_HALF_TRNS = 1 << 2, /**< Interrupt on half-transfer */ + DMA_TRNS_CMPLT = 1 << 1 /**< Interrupt on transfer completion */ } dma_mode_flags; -void dma_init(uint8 channel, volatile void *peripheral, int from_peripheral, +typedef enum dma_transfer_size { + DMA_SIZE_8BITS = 0, + DMA_SIZE_16BITS = 1, + DMA_SIZE_32BITS = 2 +} dma_transfer_size; + +/** + * Initialize a DMA channel. If desired, attach an interrupt handler + * using dma_attach_interrupt(). Start the transfer using + * dma_start(). + * + * @param channel the channel number (1..7) + * @param paddr address of the peripheral + * @param psize peripheral size + * @param msize memory size + * @param mode OR of the dma_mode_flags + * @see dma_mode_flags + * @see dma_attach_interrupt() + * @see dma_start() */ +void dma_init(uint8 channel, volatile void *paddr, + dma_transfer_size psize, dma_transfer_size msize, dma_mode_flags mode); + +/** + * Begin a DMA transfer initialized with dma_init(). You may call + * this function repeatedly after a single call to dma_init(). + * + * @param channel Channel transfer to start. + * @param buffer Buffer to write to (unless DMA_FROM_MEM was set in + * mode argument to dma_init(), in which case, buffer + * to read from). This must be aligned with msize + * argument to dma_init(). + * @param count Number of elements to transfer. + * @see dma_init() */ void dma_start(uint8 channel, volatile void *buffer, uint16 count); +void dma_attach_interrupt(uint8 channel, voidFuncPtr handler); +void dma_detach_interrupt(uint8 channel); + #ifdef __cplusplus } // extern "C" #endif diff --git a/libmaple/exti.c b/libmaple/exti.c index acd7c94..150dd05 100644 --- a/libmaple/exti.c +++ b/libmaple/exti.c @@ -23,8 +23,6 @@ *****************************************************************************/ /** - * @file exti.c - * * @brief External interrupt control routines */ @@ -182,11 +180,11 @@ void exti_attach_interrupt(uint32 port, break; } - /* Configure the enable interrupt bits for the NVIC */ - nvic_irq_enable(exti_channels[channel].irq_line); - /* Register the handler */ exti_channels[channel].handler = handler; + + /* Configure the enable interrupt bits for the NVIC */ + nvic_irq_enable(exti_channels[channel].irq_line); } diff --git a/libmaple/exti.h b/libmaple/exti.h index 89cd986..1765045 100644 --- a/libmaple/exti.h +++ b/libmaple/exti.h @@ -153,8 +153,9 @@ extern "C"{ #endif -void exti_attach_interrupt(uint32, uint32, voidFuncPtr, uint32); -void exti_detach_interrupt(uint32); +void exti_attach_interrupt(uint32 port, uint32 pin, voidFuncPtr handler, + uint32 mode); +void exti_detach_interrupt(uint32 channel); #ifdef __cplusplus } // extern "C" diff --git a/libmaple/nvic.c b/libmaple/nvic.c index 5b32d16..ad816ba 100644 --- a/libmaple/nvic.c +++ b/libmaple/nvic.c @@ -23,8 +23,6 @@ *****************************************************************************/ /** - * @file nvic.c - * * @brief Nested interrupt controller routines */ @@ -41,6 +39,7 @@ void nvic_set_vector_table(uint32 addr, uint32 offset) { * @param n interrupt number */ void nvic_irq_enable(uint32 n) { + /* TODO: bit-banding would be faster */ uint32 *iser = &((uint32*)NVIC_ISER0)[(n/32)]; __write(iser, BIT(n % 32)); } @@ -50,11 +49,16 @@ void nvic_irq_enable(uint32 n) { * @param n interrupt number */ void nvic_irq_disable(uint32 n) { + /* TODO: bit-banding would be faster */ uint32 *icer = &((uint32*)NVIC_ICER0)[(n/32)]; __write(icer, BIT(n % 32)); } void nvic_irq_disable_all(void) { + /* TODO why not: + __write(NVIC_ICER0, 0); + __write(NVIC_ICER1, 0); + */ short n; for(n=0; n<65; n++) { nvic_irq_disable(n); @@ -62,8 +66,8 @@ void nvic_irq_disable_all(void) { } /** - * @brief Initialice the NVIC at address addr - * @param addr Address to set the vector table at + * @brief Initialize the NVIC according to VECT_TAB_FLASH, + * VECT_TAB_RAM, or VECT_TAB_BASE. */ void nvic_init(void) { #ifdef VECT_TAB_FLASH diff --git a/libmaple/nvic.h b/libmaple/nvic.h index e8ca22d..c037a38 100644 --- a/libmaple/nvic.h +++ b/libmaple/nvic.h @@ -23,7 +23,8 @@ *****************************************************************************/ /** - * @brief Nested interrupt controller defines and prototypes + * @file nvic.h + * @brief Nested interrupt controller defines and prototypes */ #ifndef _NVIC_H_ @@ -39,18 +40,17 @@ extern "C"{ /* NVIC Interrupt Enable registers */ #define NVIC_ISER0 0xE000E100 #define NVIC_ISER1 0xE000E104 -#define NVIC_ISER2 0xE000E108 -#define NVIC_ISER3 0xE000E10C // Non existant? +/* NVIC_ISER2 only on connectivity line */ /* NVIC Interrupt Clear registers */ #define NVIC_ICER0 0xE000E180 #define NVIC_ICER1 0xE000E184 -#define NVIC_ICER2 0xE000E188 -#define NVIC_ICER3 0xE000E18C // Non existant? +/* NVIC_ICER2 only on connectivity line */ /* System control registers */ #define SCB_VTOR 0xE000ED08 // Vector table offset register +/* PENDING: aren't these obsolete? they're not used anywhere. */ #define NVIC_VectTab_RAM ((u32)0x20000000) #define NVIC_VectTab_FLASH ((u32)0x08000000) @@ -77,6 +77,14 @@ enum { NVIC_EXTI4 = 10, NVIC_EXTI9_5 = 23, NVIC_EXTI15_10 = 40, + + NVIC_DMA_CH1 = 11, + NVIC_DMA_CH2 = 12, + NVIC_DMA_CH3 = 13, + NVIC_DMA_CH4 = 14, + NVIC_DMA_CH5 = 15, + NVIC_DMA_CH6 = 16, + NVIC_DMA_CH7 = 17 }; -- cgit v1.2.3 From f1b64e707d8aa7548954b110368a7eb46b827794 Mon Sep 17 00:00:00 2001 From: Marti Bolivar Date: Wed, 26 Jan 2011 22:05:41 -0500 Subject: [WIP] Code review picked up some bugs/issues. --- libmaple/bkp.c | 3 +- libmaple/exti.c | 2 +- libmaple/nvic.c | 27 ++++++++-------- libmaple/nvic.h | 16 ++++------ libmaple/ring_buffer.h | 84 ++++++++++++++++++++++++++++++++++++++++---------- libmaple/usart.c | 69 ++++++++++++++++++++++++----------------- libmaple/usart.h | 4 +-- 7 files changed, 131 insertions(+), 74 deletions(-) (limited to 'libmaple/nvic.h') diff --git a/libmaple/bkp.c b/libmaple/bkp.c index 5b0ad4a..e89abd0 100644 --- a/libmaple/bkp.c +++ b/libmaple/bkp.c @@ -73,7 +73,7 @@ uint16 bkp_read(uint8 reg) { if (addr != 0) { return *addr; } - ASSERT(0); + ASSERT(0); /* nonexistent register */ return 0; } @@ -82,4 +82,5 @@ void bkp_write(uint8 reg, uint16 val) { if (addr != 0) { *addr = val; } + ASSERT(0); /* nonexistent register */ } diff --git a/libmaple/exti.c b/libmaple/exti.c index 150dd05..e806df9 100644 --- a/libmaple/exti.c +++ b/libmaple/exti.c @@ -35,7 +35,7 @@ typedef struct ExtIChannel { uint32 irq_line; } ExtIChannel; -static ExtIChannel exti_channels[] = { +volatile static ExtIChannel exti_channels[] = { { .handler = NULL, .irq_line = NVIC_EXTI0 }, // EXTI0 { .handler = NULL, .irq_line = NVIC_EXTI1 }, // EXTI1 { .handler = NULL, .irq_line = NVIC_EXTI2 }, // EXTI2 diff --git a/libmaple/nvic.c b/libmaple/nvic.c index ad816ba..155da27 100644 --- a/libmaple/nvic.c +++ b/libmaple/nvic.c @@ -39,9 +39,8 @@ void nvic_set_vector_table(uint32 addr, uint32 offset) { * @param n interrupt number */ void nvic_irq_enable(uint32 n) { - /* TODO: bit-banding would be faster */ - uint32 *iser = &((uint32*)NVIC_ISER0)[(n/32)]; - __write(iser, BIT(n % 32)); + /* TODO: test */ + __write(BITBAND_PERI(NVIC_ISER0, n), 1); } /** @@ -49,20 +48,20 @@ void nvic_irq_enable(uint32 n) { * @param n interrupt number */ void nvic_irq_disable(uint32 n) { - /* TODO: bit-banding would be faster */ - uint32 *icer = &((uint32*)NVIC_ICER0)[(n/32)]; - __write(icer, BIT(n % 32)); + /* TODO: test */ + __write(BITBAND_PERI(NVIC_ICER0, n), 1); } void nvic_irq_disable_all(void) { - /* TODO why not: - __write(NVIC_ICER0, 0); - __write(NVIC_ICER1, 0); - */ - short n; - for(n=0; n<65; n++) { - nvic_irq_disable(n); - } + /* Each ICER register contains 1 bit per interrupt. Writing a 1 + to that bit disables the corresponding interrupt. So each of + the following lines disables up to 32 interrupts at a time. + Since low, medium, and high-density devices all have less than + 64 interrupts, this suffices. */ + /* TODO: fix for connectivity line: __write(NVIC_ICER2,1), + requires connectivity line support in libmaple.h */ + __write(NVIC_ICER0, 1); + __write(NVIC_ICER1, 1); } /** diff --git a/libmaple/nvic.h b/libmaple/nvic.h index c037a38..2a54b27 100644 --- a/libmaple/nvic.h +++ b/libmaple/nvic.h @@ -50,25 +50,21 @@ extern "C"{ /* System control registers */ #define SCB_VTOR 0xE000ED08 // Vector table offset register -/* PENDING: aren't these obsolete? they're not used anywhere. */ -#define NVIC_VectTab_RAM ((u32)0x20000000) -#define NVIC_VectTab_FLASH ((u32)0x08000000) - enum { NVIC_TIMER1 = 27, NVIC_TIMER2 = 28, NVIC_TIMER3 = 29, NVIC_TIMER4 = 30, - NVIC_TIMER5 = 50, // high density only (Maple Native) - NVIC_TIMER6 = 54, // high density only (Maple Native) - NVIC_TIMER7 = 55, // high density only (Maple Native) - NVIC_TIMER8 = 46, // high density only (Maple Native) + NVIC_TIMER5 = 50, // high density only (Maple Native, Maple Audio) + NVIC_TIMER6 = 54, // high density only + NVIC_TIMER7 = 55, // high density only + NVIC_TIMER8 = 46, // high density only NVIC_USART1 = 37, NVIC_USART2 = 38, NVIC_USART3 = 39, - NVIC_USART4 = 52, // high density only (Maple Native) - NVIC_USART5 = 53, // high density only (Maple Native) + NVIC_USART4 = 52, // high density only + NVIC_USART5 = 53, // high density only NVIC_EXTI0 = 6, NVIC_EXTI1 = 7, diff --git a/libmaple/ring_buffer.h b/libmaple/ring_buffer.h index 43e0b28..a44088e 100644 --- a/libmaple/ring_buffer.h +++ b/libmaple/ring_buffer.h @@ -1,5 +1,9 @@ /** - * @brief simple circular buffer + * @file ring_buffer.h + * @brief Simple circular buffer + * + * This implementation is not thread-safe. In particular, none of + * these functions are guaranteed re-entrant. */ #ifndef _RING_BUFFER_H_ @@ -10,9 +14,11 @@ extern "C"{ #endif /* The buffer is empty when head == tail. - * The buffer is full when the head is one byte in front of the tail - * The total buffer size must be a power of two - * One byte is left free to distinguish empty from full */ + * + * The buffer is full when the head is one byte in front of the tail, + * modulo buffer length. + * + * One byte is left free to distinguish empty from full. */ typedef struct ring_buffer { /** Buffer items are stored into */ volatile uint8 *buf; @@ -24,40 +30,84 @@ typedef struct ring_buffer { uint16 size; } ring_buffer; -/** Initialise a ring buffer. +/** + * Initialise a ring buffer. + * + * @param rb Instance to initialise * - * @param rb instance to initialise - * @param size number of items in the buffer - * @param buf buffer to store items into -*/ -static inline void rb_init(ring_buffer *rb, uint16 size, uint8 *buf) { + * @param size Number of items in buf. The ring buffer will always + * leave one element unoccupied, so the maximum number of + * elements it can store will be size - 1. Thus, size + * must be at least 2. + * + * @param buf Buffer to store items into + */ +__attribute__((unused)) +static void rb_init(ring_buffer *rb, uint16 size, uint8 *buf) { rb->head = 0; rb->tail = 0; rb->size = size - 1; rb->buf = buf; } -/** Append an item onto the end of the ring buffer */ +/** Return the number of elements stored in the ring buffer. */ +static inline uint16 rb_full_count(ring_buffer *rb) { + volatile ring_buffer *arb = rb; + int32 size = arb->tail - arb->head; + if (arb->tail < arb->head) { + size += arb->size + 1; + } + return (uint16)size; +} + +/** Return true if and only if the ring buffer is full. */ +static inline int rb_is_full(ring_buffer *rb) { + return (rb->tail + 1 == rb->head) || + (rb->tail == rb->size && rb->head == 0); +} + +/** Append element onto the end of the ring buffer. */ static inline void rb_insert(ring_buffer *rb, uint8 element) { rb->buf[rb->tail] = element; rb->tail = (rb->tail == rb->size) ? 0 : rb->tail + 1; } -/** Remove and return the first item from the ring buffer */ +/** Remove and return the first item from the ring buffer. */ static inline uint8 rb_remove(ring_buffer *rb) { uint8 ch = rb->buf[rb->head]; rb->head = (rb->head == rb->size) ? 0 : rb->head + 1; return ch; } -static inline uint32 rb_full_count(ring_buffer *rb) { - /* PENDING: Broken */ - volatile ring_buffer *arb = rb; - return arb->tail - arb->head; +/** + * If rb is not full, appends element and returns true; otherwise, + * does nothing and returns false. */ +static inline int rb_safe_insert(ring_buffer *rb, uint8 element) { + if (rb_is_full(rb)) { + return 0; + } + rb_insert(rb, element); + return 1; +} + +/** + * Append an item onto the end of a non-full ring buffer. If the + * buffer is full, removes its first item, then inserts the new + * element at the end. + * + * On success, returns -1. If an element was popped, returns the + * popped value. */ +static inline int rb_push_insert(ring_buffer *rb, uint8 element) { + int ret = -1; + if (rb_is_full(rb)) { + ret = rb_remove(rb); + } + rb_insert(rb, element); + return ret; } /** Discard all items from the buffer */ -static inline void rb_reset(ring_buffer *rb) { +static inline void rb_reset(ring_buffer *rb) { rb->tail = rb->head; } diff --git a/libmaple/usart.c b/libmaple/usart.c index 34095f8..dca7ea5 100644 --- a/libmaple/usart.c +++ b/libmaple/usart.c @@ -36,8 +36,8 @@ #define USART1_BASE 0x40013800 #define USART2_BASE 0x40004400 #define USART3_BASE 0x40004800 -#define UART4_BASE 0x40004C00 // High-density devices only (Maple Native) -#define UART5_BASE 0x40005000 // High-density devices only (Maple Native) +#define USART4_BASE 0x40004C00 // High-density devices only (Maple Native) +#define USART5_BASE 0x40005000 // High-density devices only (Maple Native) #define USART_UE BIT(13) #define USART_M BIT(12) @@ -63,45 +63,56 @@ struct usart_dev usart_dev_table[] = { .rcc_dev_num = RCC_USART3, .nvic_dev_num = NVIC_USART3 }, - /* - #if NR_USART >= 5 - [UART4] = { - .base = (usart_port*)UART4_BASE, - .rcc_dev_num = RCC_UART4, - .nvic_dev_num = NVIC_UART4 - }, - [UART5] = { - .base = (usart_port*)UART5_BASE, - .rcc_dev_num = RCC_UART5, - .nvic_dev_num = NVIC_UART5 - }, - #endif - */ +#if NR_USART >= 5 + /* TODO test */ + [USART4] = { + .base = (usart_port*)USART4_BASE, + .rcc_dev_num = RCC_USART4, + .nvic_dev_num = NVIC_USART4 + }, + [USART5] = { + .base = (usart_port*)USART5_BASE, + .rcc_dev_num = RCC_USART5, + .nvic_dev_num = NVIC_USART5 + }, +#endif }; -/* usart interrupt handlers */ +/* usart interrupt handlers. + + Use of rb_safe_insert() implies that a full buffer ignores new + bytes. */ +__attribute__((always_inline)) static inline void usart_irq(int usart_num) { + /* TODO: use attributes to let GCC know it's always called with + constants, just to force the inliner to do its thing */ +#ifdef USART_SAFE_INSERT + rb_safe_insert(&(usart_dev_table[usart_num].rb), + (uint8)((usart_dev_table[usart_num].base)->DR)); +#else + rb_push_insert(&(usart_dev_table[usart_num].rb), + (uint8)((usart_dev_table[usart_num].base)->DR)); +#endif +} + void USART1_IRQHandler(void) { - rb_insert(&(usart_dev_table[USART1].rb), - (uint8)(((usart_port*)(USART1_BASE))->DR)); + usart_irq(USART1); } void USART2_IRQHandler(void) { - rb_insert(&(usart_dev_table[USART2].rb), - (uint8)(((usart_port*)(USART2_BASE))->DR)); + usart_irq(USART2); } void USART3_IRQHandler(void) { - rb_insert(&usart_dev_table[USART3].rb, - (uint8)(((usart_port*)(USART3_BASE))->DR)); + usart_irq(USART3); } + #if NR_USART >= 5 -void UART4_IRQHandler(void) { - rb_insert(&usart_dev_table[UART4].rb, - (uint8)(((usart_port*)(UART4_BASE))->DR)); +void USART4_IRQHandler(void) { + usart_irq(USART4); } -void UART5_IRQHandler(void) { - rb_insert(&usart_dev_table[UART5].rb, - (uint8)(((usart_port*)(UART5_BASE))->DR)); + +void USART5_IRQHandler(void) { + usart_irq(USART5); } #endif diff --git a/libmaple/usart.h b/libmaple/usart.h index cbc7bde..2735ac6 100644 --- a/libmaple/usart.h +++ b/libmaple/usart.h @@ -42,8 +42,8 @@ enum { USART1, USART2, USART3, - UART4, - UART5, + USART4, + USART5, }; /* peripheral register struct */ -- cgit v1.2.3 From 620740bf1986311041a40bd2992d1b549f84b2ba Mon Sep 17 00:00:00 2001 From: Marti Bolivar Date: Tue, 15 Feb 2011 20:00:10 -0500 Subject: undoing USART[45] -> UART[45] mistake --- libmaple/libmaple.h | 3 ++- libmaple/nvic.h | 4 ++-- libmaple/rcc.c | 4 ++-- libmaple/rcc.h | 4 ++-- libmaple/usart.c | 28 ++++++++++++++-------------- libmaple/usart.h | 4 ++-- 6 files changed, 24 insertions(+), 23 deletions(-) (limited to 'libmaple/nvic.h') diff --git a/libmaple/libmaple.h b/libmaple/libmaple.h index 417d732..664568f 100644 --- a/libmaple/libmaple.h +++ b/libmaple/libmaple.h @@ -101,7 +101,8 @@ #define NR_GPIO_PORTS 7 #define NR_GPIO_PINS 63 #define NR_TIMERS 8 - #define NR_USART 3 + // 3 USART, plus UART4 and UART5 + #define NR_USART 5 #define NR_FSMC 1 #define NR_DAC_PINS 2 diff --git a/libmaple/nvic.h b/libmaple/nvic.h index 2a54b27..6004c36 100644 --- a/libmaple/nvic.h +++ b/libmaple/nvic.h @@ -63,8 +63,8 @@ enum { NVIC_USART1 = 37, NVIC_USART2 = 38, NVIC_USART3 = 39, - NVIC_USART4 = 52, // high density only - NVIC_USART5 = 53, // high density only + NVIC_UART4 = 52, // high density only + NVIC_UART5 = 53, // high density only NVIC_EXTI0 = 6, NVIC_EXTI1 = 7, diff --git a/libmaple/rcc.c b/libmaple/rcc.c index 8914539..313eaf7 100644 --- a/libmaple/rcc.c +++ b/libmaple/rcc.c @@ -57,8 +57,8 @@ static const struct rcc_dev_info rcc_dev_table[] = { [RCC_USART1] = { .clk_domain = APB2, .line_num = 14 }, [RCC_USART2] = { .clk_domain = APB1, .line_num = 17 }, [RCC_USART3] = { .clk_domain = APB1, .line_num = 18 }, - [RCC_USART4] = { .clk_domain = APB1, .line_num = 19 }, // High-density only - [RCC_USART5] = { .clk_domain = APB1, .line_num = 20 }, // High-density only + [RCC_UART4] = { .clk_domain = APB1, .line_num = 19 }, // High-density only + [RCC_UART5] = { .clk_domain = APB1, .line_num = 20 }, // High-density only [RCC_TIMER1] = { .clk_domain = APB2, .line_num = 11 }, [RCC_TIMER2] = { .clk_domain = APB1, .line_num = 0 }, [RCC_TIMER3] = { .clk_domain = APB1, .line_num = 1 }, diff --git a/libmaple/rcc.h b/libmaple/rcc.h index deb567c..a7c4c53 100644 --- a/libmaple/rcc.h +++ b/libmaple/rcc.h @@ -158,8 +158,8 @@ enum { RCC_USART1, RCC_USART2, RCC_USART3, - RCC_USART4, // High-density devices only (Maple Native) - RCC_USART5, // High-density devices only (Maple Native) + RCC_UART4, // High-density devices only (Maple Native) + RCC_UART5, // High-density devices only (Maple Native) RCC_TIMER1, RCC_TIMER2, RCC_TIMER3, diff --git a/libmaple/usart.c b/libmaple/usart.c index dca7ea5..27aab49 100644 --- a/libmaple/usart.c +++ b/libmaple/usart.c @@ -36,8 +36,8 @@ #define USART1_BASE 0x40013800 #define USART2_BASE 0x40004400 #define USART3_BASE 0x40004800 -#define USART4_BASE 0x40004C00 // High-density devices only (Maple Native) -#define USART5_BASE 0x40005000 // High-density devices only (Maple Native) +#define UART4_BASE 0x40004C00 // High-density devices only (Maple Native) +#define UART5_BASE 0x40005000 // High-density devices only (Maple Native) #define USART_UE BIT(13) #define USART_M BIT(12) @@ -65,15 +65,15 @@ struct usart_dev usart_dev_table[] = { }, #if NR_USART >= 5 /* TODO test */ - [USART4] = { - .base = (usart_port*)USART4_BASE, - .rcc_dev_num = RCC_USART4, - .nvic_dev_num = NVIC_USART4 + [UART4] = { + .base = (usart_port*)UART4_BASE, + .rcc_dev_num = RCC_UART4, + .nvic_dev_num = NVIC_UART4 }, - [USART5] = { - .base = (usart_port*)USART5_BASE, - .rcc_dev_num = RCC_USART5, - .nvic_dev_num = NVIC_USART5 + [UART5] = { + .base = (usart_port*)UART5_BASE, + .rcc_dev_num = RCC_UART5, + .nvic_dev_num = NVIC_UART5 }, #endif }; @@ -107,12 +107,12 @@ void USART3_IRQHandler(void) { } #if NR_USART >= 5 -void USART4_IRQHandler(void) { - usart_irq(USART4); +void UART4_IRQHandler(void) { + usart_irq(UART4); } -void USART5_IRQHandler(void) { - usart_irq(USART5); +void UART5_IRQHandler(void) { + usart_irq(UART5); } #endif diff --git a/libmaple/usart.h b/libmaple/usart.h index 2735ac6..cbc7bde 100644 --- a/libmaple/usart.h +++ b/libmaple/usart.h @@ -42,8 +42,8 @@ enum { USART1, USART2, USART3, - USART4, - USART5, + UART4, + UART5, }; /* peripheral register struct */ -- cgit v1.2.3