diff options
Diffstat (limited to 'libmaple')
-rw-r--r-- | libmaple/adc.c | 17 | ||||
-rw-r--r-- | libmaple/flash.c | 29 | ||||
-rw-r--r-- | libmaple/flash.h | 16 | ||||
-rw-r--r-- | libmaple/gpio.c | 14 | ||||
-rw-r--r-- | libmaple/nvic.c | 4 | ||||
-rw-r--r-- | libmaple/nvic.h | 2 | ||||
-rw-r--r-- | libmaple/rcc.c | 113 | ||||
-rw-r--r-- | libmaple/rcc.h | 107 | ||||
-rw-r--r-- | libmaple/timers.c | 10 | ||||
-rw-r--r-- | libmaple/usart.c | 10 | ||||
-rw-r--r-- | libmaple/usb.c | 46 | ||||
-rw-r--r-- | libmaple/usb.h | 22 | ||||
-rw-r--r-- | libmaple/usb_regs.h | 79 | ||||
-rw-r--r-- | libmaple/util.h | 6 |
14 files changed, 366 insertions, 109 deletions
diff --git a/libmaple/adc.c b/libmaple/adc.c index 7169824..d584dba 100644 --- a/libmaple/adc.c +++ b/libmaple/adc.c @@ -23,10 +23,9 @@ * @brief Analog to digital converter routines */ -#include "stm32f10x_rcc.h" +#include "libmaple.h" +#include "rcc.h" #include "adc.h" -#include <stdio.h> -#include <inttypes.h> /* The ADC input clock is generated from PCLK2/APB2 divided by a prescaler * and it must not exceed 14MHz. @@ -59,15 +58,9 @@ * At 55.5 cycles/sample, the external input impedance < 50kOhms*/ void adc_init(void) { - /* PCLK2 is the APB2 clock */ - RCC_ADCCLKConfig(RCC_PCLK2_Div6); - - /* Enable ADC1 clock so that we can talk to it */ - RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); - - /* Put everything back to power-on defaults */ - RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1, ENABLE); - RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1, DISABLE); + rcc_set_adc_prescaler(PCLK2_DIV_2); + rcc_enable_clk_adc1(); + rcc_reset_adc1(); ADC_CR1 = 0; ADC_CR2 = CR2_EXTSEL_SWSTART | CR2_EXTTRIG; // Software triggers conversions diff --git a/libmaple/flash.c b/libmaple/flash.c new file mode 100644 index 0000000..a3ee26b --- /dev/null +++ b/libmaple/flash.c @@ -0,0 +1,29 @@ +#include "libmaple.h" +#include "flash.h" + +#define ACR_PRFTBE ((uint32)0xFFFFFFEF) +#define ACR_PRFTBE_ENABLE ((uint32)0x00000010) /* FLASH Prefetch Buffer Enable */ +#define ACR_PRFTBE_DISABLE ((uint32)0x00000000) /* FLASH Prefetch Buffer Disable */ + +#define ACR_LATENCY ((uint32)0x00000038) +#define ACR_LATENCY_0 ((uint32)0x00000000) /* FLASH Zero Latency cycle */ +#define ACR_LATENCY_1 ((uint32)0x00000001) /* FLASH One Latency cycle */ +#define ACR_LATENCY_2 ((uint32)0x00000002) /* FLASH Two Latency cycles */ + +void flash_enable_prefetch(void) { + uint32 acr = __read(FLASH_ACR); + + acr &= ACR_PRFTBE; + acr |= ACR_PRFTBE_ENABLE; + + __write(FLASH_ACR, acr); +} + +void flash_set_latency(void) { + uint32 acr = __read(FLASH_ACR); + + acr &= ACR_LATENCY; + acr |= ACR_LATENCY_2; + + __write(FLASH_ACR, acr); +} diff --git a/libmaple/flash.h b/libmaple/flash.h new file mode 100644 index 0000000..741a444 --- /dev/null +++ b/libmaple/flash.h @@ -0,0 +1,16 @@ +/** + * @brief + */ + +#ifndef _FLASH_H_ +#define _FLASH_H_ + +#define FLASH_BASE 0x40022000 +#define FLASH_ACR FLASH_BASE + +void flash_enable_prefetch(void); +void flash_set_latency(void); + +#endif + + diff --git a/libmaple/gpio.c b/libmaple/gpio.c index facb514..39341af 100644 --- a/libmaple/gpio.c +++ b/libmaple/gpio.c @@ -24,17 +24,15 @@ */ #include "libmaple.h" -#include "stm32f10x_rcc.h" +#include "rcc.h" #include "gpio.h" void gpio_init(void) { - /* Turn on clocks for GPIO */ - RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | - RCC_APB2Periph_GPIOB | - RCC_APB2Periph_GPIOC | - RCC_APB2Periph_GPIOD | - RCC_APB2Periph_AFIO, - ENABLE); + rcc_enable_clk_gpioa(); + rcc_enable_clk_gpiob(); + rcc_enable_clk_gpioc(); + rcc_enable_clk_gpiod(); + rcc_enable_clk_afio(); } void gpio_set_mode(GPIO_Port* port, uint8 gpio_pin, uint8 mode) { diff --git a/libmaple/nvic.c b/libmaple/nvic.c index 9b8c84f..45c1902 100644 --- a/libmaple/nvic.c +++ b/libmaple/nvic.c @@ -38,7 +38,7 @@ void nvic_disable_interrupts(void) { void nvic_set_vector_table(uint32_t *addr, uint32_t offset) { -// SCB->VTOR = NVIC_VectTab | (Offset & (u32)0x1FFFFF80); + __write(SCB_VTOR, (uint32_t)addr | (offset & 0x1FFFFF80)); } @@ -57,3 +57,5 @@ void nvic_enable_interrupt(uint32 n) { REG_SET_BIT(NVIC_ISER1, n - 32); } } + + diff --git a/libmaple/nvic.h b/libmaple/nvic.h index 5908e9c..89b66fc 100644 --- a/libmaple/nvic.h +++ b/libmaple/nvic.h @@ -18,8 +18,6 @@ * ****************************************************************************/ /** - * @file nvic.h - * * @brief Nested interrupt controller defines and prototypes */ diff --git a/libmaple/rcc.c b/libmaple/rcc.c new file mode 100644 index 0000000..98b115a --- /dev/null +++ b/libmaple/rcc.c @@ -0,0 +1,113 @@ +/** + * @file rcc.c + * + * @brief Implements pretty much only the basic clock setup on the maple, + * exposes a handful of clock enable/disable and peripheral reset commands. + */ + +#include "libmaple.h" +#include "flash.h" +#include "rcc.h" + +static void set_ahb_prescaler(uint32_t divider) { + uint32_t cfgr = __read(RCC_CFGR); + + cfgr &= ~HPRE; + + switch (divider) { + case SYSCLK_DIV_1: + cfgr |= SYSCLK_DIV_1; + break; + default: + ASSERT(0); + } + + __write(RCC_CFGR, cfgr); +} + +static void set_apb1_prescaler(uint32_t divider) { + uint32_t cfgr = __read(RCC_CFGR); + + cfgr &= ~PPRE1; + + switch (divider) { + case HCLK_DIV_2: + cfgr |= HCLK_DIV_2; + break; + default: + ASSERT(0); + } + + __write(RCC_CFGR, cfgr); +} + +static void set_apb2_prescaler(uint32_t divider) { + uint32_t cfgr = __read(RCC_CFGR); + + cfgr &= ~PPRE2; + + switch (divider) { + case HCLK_DIV_1: + cfgr |= HCLK_DIV_1; + break; + default: + ASSERT(0); + } + + __write(RCC_CFGR, cfgr); +} + +/* FIXME: magic numbers */ +static void pll_init(void) { + uint32_t cfgr; + + cfgr = __read(RCC_CFGR); + cfgr &= (~PLLMUL | PLL_INPUT_CLK_HSE); + + /* pll multiplier 9, input clock hse */ + __write(RCC_CFGR, cfgr | PLL_MUL_9 | PLL_INPUT_CLK_HSE); + + /* enable pll */ + __set_bits(RCC_CR, PLLON); + while(!__get_bits(RCC_CR, PLLRDY)) { + asm volatile("nop"); + } + + /* select pll for system clock source */ + cfgr = __read(RCC_CFGR); + cfgr &= ~RCC_CFGR_SWS; + __write(RCC_CFGR, cfgr | RCC_CFGR_SWS_PLL); + + while (__get_bits(RCC_CFGR, 0x00000008) != 0x8) { + asm volatile("nop"); + } +} + +static void hse_init(void) { + __set_bits(RCC_CR, HSEON); + while (!HSERDY) { + asm volatile("nop"); + } +} + +void rcc_init(void) { + hse_init(); + + /* Leave this here for now... */ + /* Enable Prefetch Buffer */ + flash_enable_prefetch(); + + /* Flash 2 wait state */ + flash_set_latency(); + + set_ahb_prescaler(SYSCLK_DIV_1); + set_apb1_prescaler(HCLK_DIV_2); + set_apb2_prescaler(HCLK_DIV_1); + pll_init(); +} + +void rcc_set_adc_prescaler(uint32_t divider) { + uint32_t cfgr = __read(RCC_CFGR); + cfgr &= ~ADCPRE; + __write(RCC_CFGR, cfgr | PCLK2_DIV_2); +} diff --git a/libmaple/rcc.h b/libmaple/rcc.h new file mode 100644 index 0000000..5c9591b --- /dev/null +++ b/libmaple/rcc.h @@ -0,0 +1,107 @@ +/** + * @file rcc.h + * + * @brief + */ + +#ifndef _RCC_H_ +#define _RCC_H_ + +#define RCC_BASE 0x40021000 +#define RCC_CR (RCC_BASE + 0x0) +#define RCC_CFGR (RCC_BASE + 0x4) +#define RCC_CIR (RCC_BASE + 0x8) +#define RCC_APB2RSTR (RCC_BASE + 0xC) +#define RCC_APB1RSTR (RCC_BASE + 0x10) +#define RCC_AHBENR (RCC_BASE + 0x14) +#define RCC_APB2ENR (RCC_BASE + 0x18) +#define RCC_APB1ENR (RCC_BASE + 0x1C) +#define RCC_BDCR (RCC_BASE + 0x20) +#define RCC_CSR (RCC_BASE + 0x24) +#define RCC_AHBSTR (RCC_BASE + 0x28) +#define RCC_CFGR2 (RCC_BASE + 0x2C)) + +#define HSEON BIT(16) +#define HSERDY *(volatile uint32_t*)(BITBAND_PERI(RCC_CR + 2, 0)) + +#define ADCPRE 0x0000C000 +#define HPRE 0x000000F0 +#define PPRE2 0x00003800 // apb2 high speed prescaler +#define PPRE1 0x00000700 // apb1 low-speed prescaler + +#define PLLMUL 0x002C0000 +#define PLL_MUL_9 0x001C0000 +#define PLLSRC BIT(16) +#define SYSCLK_DIV_1 (0x0 << 4) +#define HCLK_DIV_1 0 +#define HCLK_DIV_2 0x00000400 +#define PCLK2_DIV_2 0x00008000 + +#define PLLRDY BIT(25) +#define PLLON BIT(24) +#define PLL_INPUT_CLK_HSE BIT(16) + +#define RCC_CFGR_SWS 0x00000003 +#define RCC_CFGR_SWS_PLL 0x00000002 + +/* APB2 reset bits */ +#define RCC_APB2RSTR_USART1RST BIT(14) +#define RCC_APB2RSTR_SPI1RST BIT(12) +#define RCC_APB2RSTR_TIM1RST BIT(11) +#define RCC_APB2RSTR_ADC2RST BIT(10) +#define RCC_APB2RSTR_ADC1RST BIT(9) +#define RCC_APB2RSTR_IOERST BIT(6) +#define RCC_APB2RSTR_IODRST BIT(5) +#define RCC_APB2RSTR_IOCRST BIT(4) +#define RCC_APB2RSTR_IOBRST BIT(3) +#define RCC_APB2RSTR_IOARST BIT(2) +#define RCC_APB2RSTR_AFIORST BIT(0) + +/* APB2 peripheral clock enable bits */ +#define RCC_APB2ENR_USART1EN BIT(14) +#define RCC_APB2ENR_SPI1EN BIT(12) +#define RCC_APB2ENR_TIM1EN BIT(11) +#define RCC_APB2ENR_ADC2EN BIT(10) +#define RCC_APB2ENR_ADC1EN BIT(9) +#define RCC_APB2ENR_IOEEN BIT(6) +#define RCC_APB2ENR_IODEN BIT(5) +#define RCC_APB2ENR_IOCEN BIT(4) +#define RCC_APB2ENR_IOBEN BIT(3) +#define RCC_APB2ENR_IOAEN BIT(2) +#define RCC_APB2ENR_AFIOEN BIT(0) + +/* APB1 peripheral clock enable bits */ +#define RCC_APB1ENR_TIM2EN BIT(0) +#define RCC_APB1ENR_TIM3EN BIT(1) +#define RCC_APB1ENR_TIM4EN BIT(2) +#define RCC_APB1ENR_USART2EN BIT(17) +#define RCC_APB1ENR_USART3EN BIT(18) + +#define rcc_enable_clk_timer1() __set_bits(RCC_APB2ENR, RCC_APB2ENR_TIM1EN) +#define rcc_enable_clk_timer2() __set_bits(RCC_APB1ENR, RCC_APB1ENR_TIM2EN) +#define rcc_enable_clk_timer3() __set_bits(RCC_APB1ENR, RCC_APB1ENR_TIM3EN) +#define rcc_enable_clk_timer4() __set_bits(RCC_APB1ENR, RCC_APB1ENR_TIM4EN) + +#define rcc_enable_clk_gpioa() __set_bits(RCC_APB2ENR, RCC_APB2ENR_IOAEN) +#define rcc_enable_clk_gpiob() __set_bits(RCC_APB2ENR, RCC_APB2ENR_IOBEN) +#define rcc_enable_clk_gpioc() __set_bits(RCC_APB2ENR, RCC_APB2ENR_IOCEN) +#define rcc_enable_clk_gpiod() __set_bits(RCC_APB2ENR, RCC_APB2ENR_IODEN) +#define rcc_enable_clk_afio() __set_bits(RCC_APB2ENR, RCC_APB2ENR_AFIOEN) + +#define rcc_enable_clk_usart1() __set_bits(RCC_APB2ENR, RCC_APB2ENR_USART1EN) +#define rcc_enable_clk_usart2() __set_bits(RCC_APB1ENR, RCC_APB1ENR_USART2EN) +#define rcc_enable_clk_usart3() __set_bits(RCC_APB1ENR, RCC_APB1ENR_USART3EN) + +#define rcc_enable_clk_adc1() __set_bits(RCC_APB2ENR, RCC_APB2ENR_ADC1EN) + +#define rcc_reset_adc1() { __set_bits(RCC_APB2RSTR, RCC_APB2RSTR_ADC1RST); \ + __clear_bits(RCC_APB2RSTR, RCC_APB2RSTR_ADC1RST); \ + } + + +void rcc_init(void); +void rcc_set_adc_prescaler(uint32_t divider); + +#endif + + diff --git a/libmaple/timers.c b/libmaple/timers.c index 773ae36..e187428 100644 --- a/libmaple/timers.c +++ b/libmaple/timers.c @@ -24,7 +24,7 @@ */ #include "libmaple.h" -#include "stm32f10x_rcc.h" +#include "rcc.h" #include "timers.h" typedef struct { @@ -79,20 +79,20 @@ void timer_init(uint8_t timer_num, uint16_t prescale) { switch(timer_num) { case 1: timer = (Timer*)TIMER1_BASE; - RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); + rcc_enable_clk_timer1(); is_advanced = 1; break; case 2: timer = (Timer*)TIMER2_BASE; - RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); + rcc_enable_clk_timer2(); break; case 3: timer = (Timer*)TIMER3_BASE; - RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); + rcc_enable_clk_timer3(); break; case 4: timer = (Timer*)TIMER4_BASE; - RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); + rcc_enable_clk_timer4(); break; } diff --git a/libmaple/usart.c b/libmaple/usart.c index 9987641..545f64a 100644 --- a/libmaple/usart.c +++ b/libmaple/usart.c @@ -24,9 +24,9 @@ */ #include "libmaple.h" -#include "stm32f10x_rcc.h" -#include "usart.h" +#include "rcc.h" #include "nvic.h" +#include "usart.h" #define USART1_BASE 0x40013800 #define USART2_BASE 0x40004400 @@ -123,21 +123,21 @@ void usart_init(uint8 usart_num, uint32 baud) { port = (usart_port*)USART1_BASE; ring_buf = &ring_buf1; clk_speed = USART1_CLK; - RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); + rcc_enable_clk_usart1(); REG_SET(NVIC_ISER1, BIT(5)); break; case 2: port = (usart_port*)USART2_BASE; ring_buf = &ring_buf2; clk_speed = USART2_CLK; - RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); + rcc_enable_clk_usart2(); REG_SET(NVIC_ISER1, BIT(6)); break; case 3: port = (usart_port*)USART3_BASE; ring_buf = &ring_buf3; clk_speed = USART3_CLK; - RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); + rcc_enable_clk_usart3(); REG_SET(NVIC_ISER1, BIT(7)); break; default: diff --git a/libmaple/usb.c b/libmaple/usb.c index 8c312f8..8826858 100644 --- a/libmaple/usb.c +++ b/libmaple/usb.c @@ -1,5 +1,7 @@ -#include <inttypes.h> +#include "libmaple.h" #include "usb.h" +#include "usb_regs.h" +#include "bootVect.h" void usb_lpIRQHandler(void) { @@ -10,38 +12,38 @@ void usb_lpIRQHandler(void) ptrToUsbISR(); } -void usb_userToPMABufferCopy(u8 *pbUsrBuf, u16 wPMABufAddr, u16 wNBytes) +void usb_userToPMABufferCopy(uint8 *pbUsrBuf, uint16 wPMABufAddr, uint16 wNBytes) { - u32 n = (wNBytes + 1) >> 1; /* n = (wNBytes + 1) / 2 */ - u32 i, temp1, temp2; - u16 *pdwVal; - pdwVal = (u16 *)(wPMABufAddr * 2 + PMAAddr); + uint32 n = (wNBytes + 1) >> 1; /* n = (wNBytes + 1) / 2 */ + uint32 i, temp1, temp2; + uint16 *pdwVal; + pdwVal = (uint16 *)(wPMABufAddr * 2 + PMAAddr); for (i = n; i != 0; i--) { - temp1 = (u16) * pbUsrBuf; + temp1 = (uint16) * pbUsrBuf; pbUsrBuf++; - temp2 = temp1 | (u16) * pbUsrBuf << 8; + temp2 = temp1 | (uint16) * pbUsrBuf << 8; *pdwVal++ = temp2; pdwVal++; pbUsrBuf++; } } -void usb_PMAToUserBufferCopy(u8 *pbUsrBuf, u16 wPMABufAddr, u16 wNBytes) +void usb_PMAToUserBufferCopy(uint8 *pbUsrBuf, uint16 wPMABufAddr, uint16 wNBytes) { - u32 n = (wNBytes + 1) >> 1;/* /2*/ - u32 i; - u32 *pdwVal; - pdwVal = (u32 *)(wPMABufAddr * 2 + PMAAddr); + uint32 n = (wNBytes + 1) >> 1;/* /2*/ + uint32 i; + uint32 *pdwVal; + pdwVal = (uint32 *)(wPMABufAddr * 2 + PMAAddr); for (i = n; i != 0; i--) { - *(u16*)pbUsrBuf++ = *pdwVal++; + *(uint16*)pbUsrBuf++ = *pdwVal++; pbUsrBuf++; } } void usb_serialWriteStr(const char* outStr) { - u8 offset=0; + uint8 offset=0; BootVectTable *bootVector = ((BootVectTable*)BOOTLOADER_VECT_TABLE); while ((outStr[offset] != '\0') @@ -51,8 +53,8 @@ void usb_serialWriteStr(const char* outStr) { delay(offset*1); - bootVector->serial_count_in = (u32*) &offset; - usb_userToPMABufferCopy((u8*)outStr,USB_SERIAL_ENDP_TXADDR,offset); + bootVector->serial_count_in = (uint32*) &offset; + usb_userToPMABufferCopy((uint8*)outStr,USB_SERIAL_ENDP_TXADDR,offset); _SetEPTxCount(USB_SERIAL_ENDP_TX,offset); _SetEPTxValid(USB_SERIAL_ENDP_TX); @@ -64,20 +66,20 @@ void usb_serialWriteChar(unsigned char ch) { delay(1); *(bootVector->serial_count_in) = 1; - usb_userToPMABufferCopy((u8*)(&ch),USB_SERIAL_ENDP_TXADDR,1); + usb_userToPMABufferCopy((uint8*)(&ch),USB_SERIAL_ENDP_TXADDR,1); _SetEPTxCount(USB_SERIAL_ENDP_TX,1); _SetEPTxValid(USB_SERIAL_ENDP_TX); } -uint8_t usb_serialGetRecvLen() { - uint8_t count_out =_GetEPRxCount(USB_SERIAL_ENDP_RX); +uint8 usb_serialGetRecvLen() { + uint8 count_out =_GetEPRxCount(USB_SERIAL_ENDP_RX); return count_out; } -void usb_copyRecvBuffer(unsigned char* dest, uint8_t len) { +void usb_copyRecvBuffer(unsigned char* dest, uint8 len) { ASSERT(len < USB_SERIAL_BUF_SIZE); - usb_PMAToUserBufferCopy((u8*)(dest),USB_SERIAL_ENDP_RXADDR,len); + usb_PMAToUserBufferCopy((uint8*)(dest),USB_SERIAL_ENDP_RXADDR,len); _SetEPRxValid(USB_SERIAL_ENDP_RX); } diff --git a/libmaple/usb.h b/libmaple/usb.h index 3b7a971..fa40123 100644 --- a/libmaple/usb.h +++ b/libmaple/usb.h @@ -1,30 +1,24 @@ #ifndef _USB_H_ #define _USB_H_ -#include <inttypes.h> -#include "util.h" -#include "cortexm3_macro.h" -#include "usb_regs.h" -#include "bootVect.h" - #define USB_ISR_ADDR (0x08000090) -#define USB_SERIAL_ENDP_TXADDR ((uint32_t) 0xC0) -#define USB_SERIAL_ENDP_RXADDR ((uint32_t) 0x110) -#define USB_SERIAL_ENDP_TX ((uint16_t) 0x1) -#define USB_SERIAL_ENDP_RX ((uint16_t) 0x3) +#define USB_SERIAL_ENDP_TXADDR ((uint32) 0xC0) +#define USB_SERIAL_ENDP_RXADDR ((uint32) 0x110) +#define USB_SERIAL_ENDP_TX ((uint16) 0x1) +#define USB_SERIAL_ENDP_RX ((uint16) 0x3) #define USB_SERIAL_BUF_SIZE (0x40) #ifdef __cplusplus extern "C" { #endif -void usb_lpIRQHandler(void); -void usb_userToPMABufferCopy(u8 *pbUsrBuf,u16 wPMABufAddr,u16 wNBytes); -void usb_PMAToUserBufferCopy(u8 *pbUsrBuf,u16 wPMABufAddr,u16 wNBytes); +void usb_lpIRQHandler(void); +void usb_userToPMABufferCopy(uint8 *pbUsrBuf,uint16 wPMABufAddr,uint16 wNBytes); +void usb_PMAToUserBufferCopy(uint8 *pbUsrBuf,uint16 wPMABufAddr,uint16 wNBytes); void usb_serialWriteStr(const char *outStr); void usb_serialWriteChar(unsigned char ch); uint8_t usb_serialGetRecvLen(); -void usb_copyRecvBuffer(unsigned char* dest, uint8_t len); +void usb_copyRecvBuffer(unsigned char* dest, uint8 len); #ifdef __cplusplus } // extern "C" diff --git a/libmaple/usb_regs.h b/libmaple/usb_regs.h index 135d645..bf5e10c 100644 --- a/libmaple/usb_regs.h +++ b/libmaple/usb_regs.h @@ -19,7 +19,6 @@ /* Includes ------------------------------------------------------------------*/
/* Exported types ------------------------------------------------------------*/
-#include "stm32f10x_type.h"
typedef enum _EP_DBUF_DIR
{
@@ -61,14 +60,14 @@ enum EP_BUF_NUM #define EP0REG ((volatile unsigned *)(RegBase)) /* endpoint 0 register address */
/* endpoints enumeration */
-#define ENDP0 ((u8)0)
-#define ENDP1 ((u8)1)
-#define ENDP2 ((u8)2)
-#define ENDP3 ((u8)3)
-#define ENDP4 ((u8)4)
-#define ENDP5 ((u8)5)
-#define ENDP6 ((u8)6)
-#define ENDP7 ((u8)7)
+#define ENDP0 ((uint8)0)
+#define ENDP1 ((uint8)1)
+#define ENDP2 ((uint8)2)
+#define ENDP3 ((uint8)3)
+#define ENDP4 ((uint8)4)
+#define ENDP5 ((uint8)5)
+#define ENDP6 ((uint8)6)
+#define ENDP7 ((uint8)7)
/******************************************************************************/
/* ISTR interrupt events */
/******************************************************************************/
@@ -175,38 +174,38 @@ enum EP_BUF_NUM #define EPRX_DTOGMASK (EPRX_STAT|EPREG_MASK)
/* Exported macro ------------------------------------------------------------*/
/* SetCNTR */
-#define _SetCNTR(wRegValue) (*CNTR = (u16)wRegValue)
+#define _SetCNTR(wRegValue) (*CNTR = (uint16)wRegValue)
/* SetISTR */
-#define _SetISTR(wRegValue) (*ISTR = (u16)wRegValue)
+#define _SetISTR(wRegValue) (*ISTR = (uint16)wRegValue)
/* SetDADDR */
-#define _SetDADDR(wRegValue) (*DADDR = (u16)wRegValue)
+#define _SetDADDR(wRegValue) (*DADDR = (uint16)wRegValue)
/* SetBTABLE */
-#define _SetBTABLE(wRegValue)(*BTABLE = (u16)(wRegValue & 0xFFF8))
+#define _SetBTABLE(wRegValue)(*BTABLE = (uint16)(wRegValue & 0xFFF8))
/* GetCNTR */
-#define _GetCNTR() ((u16) *CNTR)
+#define _GetCNTR() ((uint16) *CNTR)
/* GetISTR */
-#define _GetISTR() ((u16) *ISTR)
+#define _GetISTR() ((uint16) *ISTR)
/* GetFNR */
-#define _GetFNR() ((u16) *FNR)
+#define _GetFNR() ((uint16) *FNR)
/* GetDADDR */
-#define _GetDADDR() ((u16) *DADDR)
+#define _GetDADDR() ((uint16) *DADDR)
/* GetBTABLE */
-#define _GetBTABLE() ((u16) *BTABLE)
+#define _GetBTABLE() ((uint16) *BTABLE)
/* SetENDPOINT */
#define _SetENDPOINT(bEpNum,wRegValue) (*(EP0REG + bEpNum)= \
- (u16)wRegValue)
+ (uint16)wRegValue)
/* GetENDPOINT */
-#define _GetENDPOINT(bEpNum) ((u16)(*(EP0REG + bEpNum)))
+#define _GetENDPOINT(bEpNum) ((uint16)(*(EP0REG + bEpNum)))
/*******************************************************************************
* Macro Name : SetEPType
@@ -237,7 +236,7 @@ enum EP_BUF_NUM * Return : None.
*******************************************************************************/
#define _SetEPTxStatus(bEpNum,wState) {\
- register u16 _wRegVal; \
+ register uint16 _wRegVal; \
_wRegVal = _GetENDPOINT(bEpNum) & EPTX_DTOGMASK;\
/* toggle first bit ? */ \
if((EPTX_DTOG1 & wState)!= 0) \
@@ -257,7 +256,7 @@ enum EP_BUF_NUM * Return : None.
*******************************************************************************/
#define _SetEPRxStatus(bEpNum,wState) {\
- register u16 _wRegVal; \
+ register uint16 _wRegVal; \
\
_wRegVal = _GetENDPOINT(bEpNum) & EPRX_DTOGMASK;\
/* toggle first bit ? */ \
@@ -276,9 +275,9 @@ enum EP_BUF_NUM * Output : None.
* Return : status .
*******************************************************************************/
-#define _GetEPTxStatus(bEpNum) ((u16)_GetENDPOINT(bEpNum) & EPTX_STAT)
+#define _GetEPTxStatus(bEpNum) ((uint16)_GetENDPOINT(bEpNum) & EPTX_STAT)
-#define _GetEPRxStatus(bEpNum) ((u16)_GetENDPOINT(bEpNum) & EPRX_STAT)
+#define _GetEPRxStatus(bEpNum) ((uint16)_GetENDPOINT(bEpNum) & EPRX_STAT)
/*******************************************************************************
* Macro Name : SetEPTxValid / SetEPRxValid
@@ -388,12 +387,12 @@ enum EP_BUF_NUM * Output : None.
* Return : None.
*******************************************************************************/
-#define _GetEPAddress(bEpNum) ((u8)(_GetENDPOINT(bEpNum) & EPADDR_FIELD))
+#define _GetEPAddress(bEpNum) ((uint8)(_GetENDPOINT(bEpNum) & EPADDR_FIELD))
-#define _pEPTxAddr(bEpNum) ((u32 *)((_GetBTABLE()+bEpNum*8 )*2 + PMAAddr))
-#define _pEPTxCount(bEpNum) ((u32 *)((_GetBTABLE()+bEpNum*8+2)*2 + PMAAddr))
-#define _pEPRxAddr(bEpNum) ((u32 *)((_GetBTABLE()+bEpNum*8+4)*2 + PMAAddr))
-#define _pEPRxCount(bEpNum) ((u32 *)((_GetBTABLE()+bEpNum*8+6)*2 + PMAAddr))
+#define _pEPTxAddr(bEpNum) ((uint32 *)((_GetBTABLE()+bEpNum*8 )*2 + PMAAddr))
+#define _pEPTxCount(bEpNum) ((uint32 *)((_GetBTABLE()+bEpNum*8+2)*2 + PMAAddr))
+#define _pEPRxAddr(bEpNum) ((uint32 *)((_GetBTABLE()+bEpNum*8+4)*2 + PMAAddr))
+#define _pEPRxCount(bEpNum) ((uint32 *)((_GetBTABLE()+bEpNum*8+6)*2 + PMAAddr))
/*******************************************************************************
* Macro Name : SetEPTxAddr / SetEPRxAddr.
@@ -413,8 +412,8 @@ enum EP_BUF_NUM * Output : None.
* Return : address of the buffer.
*******************************************************************************/
-#define _GetEPTxAddr(bEpNum) ((u16)*_pEPTxAddr(bEpNum))
-#define _GetEPRxAddr(bEpNum) ((u16)*_pEPRxAddr(bEpNum))
+#define _GetEPTxAddr(bEpNum) ((uint16)*_pEPTxAddr(bEpNum))
+#define _GetEPRxAddr(bEpNum) ((uint16)*_pEPRxAddr(bEpNum))
/*******************************************************************************
* Macro Name : SetEPCountRxReg.
@@ -428,18 +427,18 @@ enum EP_BUF_NUM wNBlocks = wCount >> 5;\
if((wCount & 0x1f) == 0)\
wNBlocks--;\
- *pdwReg = (u32)((wNBlocks << 10) | 0x8000);\
+ *pdwReg = (uint32)((wNBlocks << 10) | 0x8000);\
}/* _BlocksOf32 */
#define _BlocksOf2(dwReg,wCount,wNBlocks) {\
wNBlocks = wCount >> 1;\
if((wCount & 0x1) != 0)\
wNBlocks++;\
- *pdwReg = (u32)(wNBlocks << 10);\
+ *pdwReg = (uint32)(wNBlocks << 10);\
}/* _BlocksOf2 */
#define _SetEPCountRxReg(dwReg,wCount) {\
- u16 wNBlocks;\
+ uint16 wNBlocks;\
if(wCount > 62){_BlocksOf32(dwReg,wCount,wNBlocks);}\
else {_BlocksOf2(dwReg,wCount,wNBlocks);}\
}/* _SetEPCountRxReg */
@@ -447,7 +446,7 @@ enum EP_BUF_NUM #define _SetEPRxDblBuf0Count(bEpNum,wCount) {\
- u32 *pdwReg = _pEPTxCount(bEpNum); \
+ uint32 *pdwReg = _pEPTxCount(bEpNum); \
_SetEPCountRxReg(pdwReg, wCount);\
}
/*******************************************************************************
@@ -460,7 +459,7 @@ enum EP_BUF_NUM *******************************************************************************/
#define _SetEPTxCount(bEpNum,wCount) (*_pEPTxCount(bEpNum) = wCount)
#define _SetEPRxCount(bEpNum,wCount) {\
- u32 *pdwReg = _pEPRxCount(bEpNum); \
+ uint32 *pdwReg = _pEPRxCount(bEpNum); \
_SetEPCountRxReg(pdwReg, wCount);\
}
/*******************************************************************************
@@ -470,8 +469,8 @@ enum EP_BUF_NUM * Output : None.
* Return : Counter value.
*******************************************************************************/
-#define _GetEPTxCount(bEpNum)((u16)(*_pEPTxCount(bEpNum)) & 0x3ff)
-#define _GetEPRxCount(bEpNum)((u16)(*_pEPRxCount(bEpNum)) & 0x3ff)
+#define _GetEPTxCount(bEpNum)((uint16)(*_pEPTxCount(bEpNum)) & 0x3ff)
+#define _GetEPRxCount(bEpNum)((uint16)(*_pEPRxCount(bEpNum)) & 0x3ff)
/*******************************************************************************
* Macro Name : SetEPDblBuf0Addr / SetEPDblBuf1Addr.
@@ -524,7 +523,7 @@ enum EP_BUF_NUM {_SetEPRxDblBuf0Count(bEpNum,wCount);} \
else if(bDir == EP_DBUF_IN)\
/* IN endpoint */ \
- *_pEPTxCount(bEpNum) = (u32)wCount; \
+ *_pEPTxCount(bEpNum) = (uint32)wCount; \
} /* SetEPDblBuf0Count*/
#define _SetEPDblBuf1Count(bEpNum, bDir, wCount) { \
@@ -533,7 +532,7 @@ enum EP_BUF_NUM {_SetEPRxCount(bEpNum,wCount);}\
else if(bDir == EP_DBUF_IN)\
/* IN endpoint */\
- *_pEPRxCount(bEpNum) = (u32)wCount; \
+ *_pEPRxCount(bEpNum) = (uint32)wCount; \
} /* SetEPDblBuf1Count */
#define _SetEPDblBuffCount(bEpNum, bDir, wCount) {\
diff --git a/libmaple/util.h b/libmaple/util.h index e425cc0..c1ba15d 100644 --- a/libmaple/util.h +++ b/libmaple/util.h @@ -54,6 +54,12 @@ #define REG_GET(reg) *(volatile uint32_t*)(reg) +#define __set_bits(addr, mask) *(volatile uint32_t*)(addr) |= (uint32_t)(mask) +#define __clear_bits(addr, mask) (*(volatile uint32_t*)(addr) &= (uint32_t)~(mask)) +#define __get_bits(addr, mask) (*(volatile uint32_t*)(addr) & (uint32_t)(mask)) + +#define __read(reg) *(volatile uint32_t*)(reg) +#define __write(reg, value) *(volatile uint32_t*)(reg) = (value) #ifdef __cplusplus extern "C"{ |