diff options
-rw-r--r-- | libmaple/bkp.c | 85 | ||||
-rw-r--r-- | libmaple/bkp.h | 86 | ||||
-rw-r--r-- | libmaple/dma.c | 4 | ||||
-rw-r--r-- | libmaple/dma.h | 3 | ||||
-rw-r--r-- | libmaple/iwdg.h | 10 | ||||
-rw-r--r-- | libmaple/libmaple.h | 4 | ||||
-rw-r--r-- | libmaple/pwr.h | 46 | ||||
-rw-r--r-- | libmaple/rules.mk | 23 | ||||
-rw-r--r-- | libmaple/systick.c | 4 | ||||
-rw-r--r-- | libmaple/systick.h | 2 | ||||
-rw-r--r-- | libmaple/util.h | 8 |
11 files changed, 256 insertions, 19 deletions
diff --git a/libmaple/bkp.c b/libmaple/bkp.c new file mode 100644 index 0000000..5b0ad4a --- /dev/null +++ b/libmaple/bkp.c @@ -0,0 +1,85 @@ +/****************************************************************************** + * The MIT License + * + * Copyright (c) 2010 LeafLabs, LLC. + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, copy, + * modify, merge, publish, distribute, sublicense, and/or sell copies + * of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + *****************************************************************************/ + +#include "libmaple.h" +#include "bkp.h" +#include "pwr.h" +#include "rcc.h" +#include "util.h" + +/* Data register memory layout is not contiguous. It's split up from + 1--NR_LOW_DRS, beginning at BKP_LOW_OFFSET, through + (NR_LOW_DRS+1)--NR_DRS, beginning at BKP_HIGH_OFFSET. */ +#define NR_LOW_DRS 10 +#define BKP_LOW_OFFSET 0x4 /* start offset for data registers 1--10 */ +#define BKP_HIGH_OFFSET 0x40 /* start offset for data registers 11--42 */ + +inline volatile uint16* reg_addr(uint8 reg) { + if (1 <= reg) { + if (reg <= NR_LOW_DRS) { + return (volatile uint16*)(BKP_BASE + BKP_LOW_OFFSET + + (reg - 1) * 4); + } else if (reg <= NR_BKP_REGS) { + return (volatile uint16*)(BKP_BASE + BKP_HIGH_OFFSET + + (reg - NR_LOW_DRS - 1) * 4); + } + } + return 0; +} + +void bkp_init(void) { + /* Set PWREN (28) and BKPEN (27) bits */ + __set_bits(RCC_APB1ENR, BIT(28) | BIT(27)); +} + +void bkp_disable(void) { + __clear_bits(RCC_APB1ENR, BIT(28) | BIT(27)); +} + +void bkp_enable_writes(void) { + /* Set the DBP bit in PWR_CR */ + __write(BITBAND_PERI(PWR_CR, PWR_CR_DBP), 1); +} + +void bkp_disable_writes(void) { + __write(BITBAND_PERI(PWR_CR, PWR_CR_DBP), 0); +} + +uint16 bkp_read(uint8 reg) { + volatile uint16* addr = reg_addr(reg); + if (addr != 0) { + return *addr; + } + ASSERT(0); + return 0; +} + +void bkp_write(uint8 reg, uint16 val) { + volatile uint16* addr = reg_addr(reg); + if (addr != 0) { + *addr = val; + } +} diff --git a/libmaple/bkp.h b/libmaple/bkp.h new file mode 100644 index 0000000..9ad4c41 --- /dev/null +++ b/libmaple/bkp.h @@ -0,0 +1,86 @@ +/****************************************************************************** + * The MIT License + * + * Copyright (c) 2010 LeafLabs, LLC. + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, copy, + * modify, merge, publish, distribute, sublicense, and/or sell copies + * of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + *****************************************************************************/ + +/** + * @file bkp.h + * @brief Backup register support. + */ + +#ifndef _BKP_H_ +#define _BKP_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#define BKP_BASE 0x40006C00 +#define BKP_RTCCR (BKP_BASE + 0x2C) +#define BKP_CR (BKP_BASE + 0x30) +#define BKP_CSR (BKP_BASE + 0x34) + +/** + * Initialize backup interface. This function enables the power and + * backup interface clocks. It does not enable write access to the + * backup registers. + */ +void bkp_init(void); + +/** Disable power and backup interface clocks. */ +void bkp_disable(void); + +/** + * Enable write access to the backup registers. Backup interface must + * be initialized for subsequent register writes to work. + * @see bkp_init() + */ +void bkp_enable_writes(void); + +/** + * Disable write access to the backup registers. Does not disable + * backup interface clocks. + */ +void bkp_disable_writes(void); + +/** + * Read a value from given backup data register. + * @param reg Data register to read, from 1 to NR_BKP_REGS (10 on Maple). + */ +uint16 bkp_read(uint8 reg); + +/** + * Write a value to given data register. Backup interface must have + * been previously initialized, and write access to backup registers + * must be enabled. + * @param reg Data register to write, from 1 to NR_BKP_REGS (10 on Maple). + * @param val Value to write into the register. + */ +void bkp_write(uint8 reg, uint16 val); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif diff --git a/libmaple/dma.c b/libmaple/dma.c index 4ad7fd2..88c1342 100644 --- a/libmaple/dma.c +++ b/libmaple/dma.c @@ -63,7 +63,8 @@ static dma_regs *dma_get_regs(uint8 channel) { * to memory * @param mode OR of the dma_mode_flags */ -void dma_init(uint8 channel, volatile void *peripheral, bool from_peripheral, enum dma_mode_flags mode) { +void dma_init(uint8 channel, volatile void *peripheral, int from_peripheral, + dma_mode_flags mode) { volatile dma_regs *regs = dma_get_regs(channel); if (regs != NULL) { @@ -79,6 +80,7 @@ void dma_init(uint8 channel, volatile void *peripheral, bool from_peripheral, en | (mode << 5) // Increment and circular modes | (0 << 0); // Not enabled + /* FIXME XXX integer from pointer without a cast. */ regs->CPAR = peripheral; if (!from_peripheral) { diff --git a/libmaple/dma.h b/libmaple/dma.h index 339b826..8a6ca34 100644 --- a/libmaple/dma.h +++ b/libmaple/dma.h @@ -61,7 +61,8 @@ typedef enum dma_mode_flags { DMA_CIRC_MODE = 1, } dma_mode_flags; -void dma_init(uint8 channel, volatile void *peripheral, bool from_peripheral, dma_mode_flags mode); +void dma_init(uint8 channel, volatile void *peripheral, int from_peripheral, + dma_mode_flags mode); void dma_start(uint8 channel, volatile void *buffer, uint16 count); #ifdef __cplusplus diff --git a/libmaple/iwdg.h b/libmaple/iwdg.h index 6002867..4ab0ddf 100644 --- a/libmaple/iwdg.h +++ b/libmaple/iwdg.h @@ -41,6 +41,16 @@ extern "C"{ #define IWDG_RLR (IWDG_BASE + 0x8) #define IWDG_SR (IWDG_BASE + 0xC) +enum { + IWDG_PRE_4, + IWDG_PRE_8, + IWDG_PRE_16, + IWDG_PRE_32, + IWDG_PRE_64, + IWDG_PRE_128, + IWDG_PRE_256 +}; + void iwdg_init(uint8 prescaler, uint16 reload); void iwdg_feed(void); diff --git a/libmaple/libmaple.h b/libmaple/libmaple.h index 6921b63..417d732 100644 --- a/libmaple/libmaple.h +++ b/libmaple/libmaple.h @@ -40,6 +40,9 @@ #ifdef MCU_STM32F103RB // eg, LeafLabs Maple + // Number of 16-bit backup registers + #define NR_BKP_REGS 10 + // Number of GPIO ports (GPIOA, GPIOB, etc), definately used #define NR_GPIO_PORTS 4 @@ -94,6 +97,7 @@ #ifdef MCU_STM32F103ZE // eg, LeafLabs Maple Native + #define NR_BKP_REGS 42 #define NR_GPIO_PORTS 7 #define NR_GPIO_PINS 63 #define NR_TIMERS 8 diff --git a/libmaple/pwr.h b/libmaple/pwr.h new file mode 100644 index 0000000..96a8356 --- /dev/null +++ b/libmaple/pwr.h @@ -0,0 +1,46 @@ +/****************************************************************************** + * The MIT License + * + * Copyright (c) 2010 LeafLabs, LLC. + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, copy, + * modify, merge, publish, distribute, sublicense, and/or sell copies + * of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + *****************************************************************************/ + +/** + * @file pwr.h + * @brief Power control (PWR) defines. + */ + +#define PWR_BASE 0x40007000 + +#define PWR_CR (PWR_BASE + 0x0) +#define PWR_CR_DBP 8 /* Disable backup domain write protection bit */ +#define PWR_CR_PVDE 4 /* Power voltage detector enable bit */ +#define PWR_CR_CSBF 3 /* Clear standby flag bit */ +#define PWR_CR_CWUF 2 /* Clear wakeup flag bit */ +#define PWR_CR_PDDS 1 /* Power down deepsleep bit */ +#define PWR_CR_LPDS 0 /* Low-power deepsleep bit */ + +#define PWR_CSR (PWR_BASE + 0x4) +#define PWR_CSR_EWUP 8 /* Enable wakeup pin bit */ +#define PWR_CSR_PVDO 2 /* PVD output bit */ +#define PWR_CSR_SBF 1 /* Standby flag bit */ +#define PWR_CSR_WUF 0 /* Wakeup flag bit */ diff --git a/libmaple/rules.mk b/libmaple/rules.mk index cd50495..b87595d 100644 --- a/libmaple/rules.mk +++ b/libmaple/rules.mk @@ -12,24 +12,27 @@ LIBMAPLE_INCLUDES := -I$(LIBMAPLE_PATH) -I$(LIBMAPLE_PATH)/usb -I$(LIBMAPLE_PATH CFLAGS_$(d) = -I$(d) $(LIBMAPLE_INCLUDES) -D$(VECT_BASE_ADDR) # Local rules and targets -cSRCS_$(d) := systick.c \ - timers.c \ - adc.c \ - syscalls.c \ +cSRCS_$(d) := adc.c \ + bkp.c \ + dac.c \ + dma.c \ exti.c \ + flash.c \ + fsmc.c \ gpio.c \ + iwdg.c \ nvic.c \ - usart.c \ - util.c \ rcc.c \ - flash.c \ spi.c \ - fsmc.c \ - dac.c \ + syscalls.c \ + systick.c \ + timers.c \ + usart.c \ + util.c \ + usb/descriptors.c \ usb/usb.c \ usb/usb_callbacks.c \ usb/usb_hardware.c \ - usb/descriptors.c \ usb/usb_lib/usb_core.c \ usb/usb_lib/usb_init.c \ usb/usb_lib/usb_int.c \ diff --git a/libmaple/systick.c b/libmaple/systick.c index f6f1c16..961d42b 100644 --- a/libmaple/systick.c +++ b/libmaple/systick.c @@ -59,13 +59,13 @@ void systick_disable() { } void systick_resume() { - /* re-enable init registers without changing relead_val */ + /* re-enable init registers without changing reload val */ __write(SYSTICK_CSR, SYSTICK_SRC_HCLK | SYSTICK_ENABLE | SYSTICK_TICKINT); } /** SysTick interrupt handler. Bumps up the tick counter. */ -void SysTick_Handler(void) { +void SysTickHandler(void) { systick_timer_millis++; } diff --git a/libmaple/systick.h b/libmaple/systick.h index ae1268a..33a3cf8 100644 --- a/libmaple/systick.h +++ b/libmaple/systick.h @@ -43,7 +43,7 @@ extern "C"{ #define SYSTICK_CSR_COUNTFLAG BIT(16) /** System elapsed time in milliseconds */ -volatile uint32 systick_timer_millis; +extern volatile uint32 systick_timer_millis; void systick_init(uint32 reload_val); void systick_disable(); diff --git a/libmaple/util.h b/libmaple/util.h index 2bbd90c..b6074d8 100644 --- a/libmaple/util.h +++ b/libmaple/util.h @@ -52,14 +52,14 @@ #define REG_SET_MASK(reg, mask) (*(volatile uint32*)(reg) |= (uint32)(mask)) #define REG_CLEAR_MASK(reg, mask) (*(volatile uint32*)(reg) &= (uint32)~(mask)) -#define REG_GET(reg) *(volatile uint32*)(reg) +#define REG_GET(reg) (*(volatile uint32*)(reg)) -#define __set_bits(addr, mask) *(volatile uint32*)(addr) |= (uint32)(mask) +#define __set_bits(addr, mask) (*(volatile uint32*)(addr) |= (uint32)(mask)) #define __clear_bits(addr, mask) (*(volatile uint32*)(addr) &= (uint32)~(mask)) #define __get_bits(addr, mask) (*(volatile uint32*)(addr) & (uint32)(mask)) -#define __read(reg) *(volatile uint32*)(reg) -#define __write(reg, value) *(volatile uint32*)(reg) = (value) +#define __read(reg) (*(volatile uint32*)(reg)) +#define __write(reg, value) (*(volatile uint32*)(reg) = (value)) #define IS_POWER_OF_TWO(v) (v && !(v & (v - 1))) |