diff options
Diffstat (limited to 'system-clock/include')
-rw-r--r-- | system-clock/include/clock.h | 61 | ||||
-rw-r--r-- | system-clock/include/common.h | 13 | ||||
-rw-r--r-- | system-clock/include/gpio.h | 128 |
3 files changed, 202 insertions, 0 deletions
diff --git a/system-clock/include/clock.h b/system-clock/include/clock.h new file mode 100644 index 0000000..2ba6880 --- /dev/null +++ b/system-clock/include/clock.h @@ -0,0 +1,61 @@ +#ifndef CLOCK_H__ +#define CLOCK_H__ + +#include <stdint.h> + +#define PERIPH_BASE ((uint32_t) 0x40000000) +#define AHBPERIPH_BASE (PERIPH_BASE + 0x00020000) +#define RCC_BASE (AHBPERIPH_BASE + 0x00001000) +#define FLASH_R_BASE (AHBPERIPH_BASE + 0x00003C00) +#define PWR_BASE (PERIPH_BASE + 0x7000) +#define PWR_CSR_VOSF ((uint16_t)0x0010) /*!< Voltage Scaling select flag */ + + +#ifndef __IO +#define __IO volatile +#endif + +typedef struct { + __IO uint32_t cr; + __IO uint32_t csr; +} pwr_t; + +typedef struct { + __IO uint32_t acr; + __IO uint32_t pecr; + __IO uint32_t pdkeyr; + __IO uint32_t pekeyr; + __IO uint32_t prgkeyr; + __IO uint32_t optkeyr; + __IO uint32_t sr; + __IO uint32_t obr; + __IO uint32_t wrpr; +} flash_t; + +typedef struct { + __IO uint32_t cr; + __IO uint32_t icscr; + __IO uint32_t cfgr; + __IO uint32_t cir; + __IO uint32_t ahbrstr; + __IO uint32_t apb2rstr; + __IO uint32_t apb1rstr; + __IO uint32_t ahbenr; + __IO uint32_t apb2enr; + __IO uint32_t apb1enr; + __IO uint32_t ahblpenr; + __IO uint32_t apb2lpenr; + __IO uint32_t apb1lpenr; + __IO uint32_t csr; +} rcc_t; + +#define RCC (*(rcc_t*) (RCC_BASE)) +#define FLASH (*(flash_t*) (FLASH_R_BASE)) +#define PWR (*(pwr_t*) (PWR_BASE)) + +/* + * Sets the system clock to a full 80Mhz. + */ +int set_sys_clock(); + +#endif /* CLOCK_H__ */ diff --git a/system-clock/include/common.h b/system-clock/include/common.h new file mode 100644 index 0000000..f58f179 --- /dev/null +++ b/system-clock/include/common.h @@ -0,0 +1,13 @@ +#ifndef COMMON__H +#define COMMON__H + +/* Define __IO to be volatile if it's not already. */ +#ifndef __IO +#define __IO volatile +#endif + +#define bool int + +#define PACKED __attribute__((packed)) + +#endif /* COMMON_H */ diff --git a/system-clock/include/gpio.h b/system-clock/include/gpio.h new file mode 100644 index 0000000..53ece32 --- /dev/null +++ b/system-clock/include/gpio.h @@ -0,0 +1,128 @@ +#ifndef GPIO_H__ +#define GPIO_H__ + +#include "common.h" + +#include <stdint.h> + +/* + * Possible GPIO ports. + */ +typedef enum { + GPIO_PORT_A = 0, + GPIO_PORT_B = 1, + GPIO_PORT_C = 2, + GPIO_PORT_D = 3 +} gpio_port_number_t; + +/* + * Structure defining the layout of the layout of the GPIO registers on the + * stm32l432 development board. + */ +typedef struct GPIO_PORT_STR { + __IO uint32_t mode_r; /* Mode register */ + __IO uint32_t pupd_r; /* Pull up/pull down/none register */ + __IO uint32_t speed_r; /* Speed register */ + __IO uint32_t type_r; /* Type register */ + __IO uint32_t input_r; /* Input data register */ + __IO uint32_t output_r; /* Output data register */ + __IO uint32_t bsr_r; /* Bit set/reset register */ + __IO uint32_t lock_r; /* Lock register */ + __IO uint32_t altfn_r; /* Alternate function register */ +} PACKED gpio_port_t; + +/* + * Enum defining the PINs in a GPIO port. Each port has 16 pins to use in + * the stm32l432. + */ +typedef enum GPIO_PIN_ENUM { + PIN_0 = 0, + PIN_1 = 1, + PIN_2 = 2, + PIN_3 = 3, + PIN_4 = 4, + PIN_5 = 5, + PIN_6 = 6, + PIN_7 = 7, + PIN_8 = 8, + PIN_9 = 9, + PIN_10 = 10, + PIN_11 = 11, + PIN_12 = 12, + PIN_13 = 13, + PIN_14 = 14, + PIN_15 = 15 +} gpio_pin_t; + +/* + * Enum defining the pin modes that are possible. + */ +typedef enum { + MODE_INPUT = 0, + MODE_OUTPUT = 1, + MODE_ALTERNATE = 2, + MODE_ANALOG = 3 +} gpio_pin_mode_t; + +/* + * Enum defining the pin speeds that are possible. + */ +typedef enum { + SPEED_2MHZ = 0, + SPEED_10MHZ = 1, + SPEED_50MHZ = 3, +} speed_t; + +/* + * Structure defining an OUTPUT pin. Structurally equivalent to the input pin, + * but can be used in a slightly type-safe manner. + */ +typedef struct { + __IO gpio_port_t* gpio_port; + gpio_pin_t pin; +} gpio_output_pin_t; + +/* + * Sets the mode on a GPIO pin. + * + * gpio_port: the gpio port to use. + * pin: the pin number to set. + * pin_mode: the mode to set the pin to. + */ +void set_gpio_pin_mode( + __IO gpio_port_t* gpio_port, + gpio_pin_t pin, + gpio_pin_mode_t pin_mode); + +/* + * Sets the given GPIO pin to be an output pin. Returns an output_pin struct + * corresponding to + */ +gpio_output_pin_t set_gpio_pin_output( + __IO gpio_port_t* gpio_port, + gpio_pin_t pin); + +/* + * Sets an output pin on or off. + * + * pin: the pin to toggle. + * onoff: 0 for off, non-zero of on. + */ +void set_gpio_output_pin( + gpio_output_pin_t pin, + bool onoff); + +#define pin_on(p) \ + set_gpio_output_pin(p, 1) + +#define pin_off(p) \ + set_gpio_output_pin(p, 0) + +/* + * Enables a GPIO port and returns a reference to the register definition + * of that GPIO port. + */ +__IO gpio_port_t* enable_gpio(gpio_port_number_t number); + + +#endif /* GPIO_H__ */ |