Merge pull request #281 from bcostm/master

[NUCLEO_L152RE/F103RB] Add LSE configuration for RTC
pull/283/head^2
Bogdan Marinescu 2014-04-29 11:05:21 +01:00
commit 5bf985ebc6
35 changed files with 865 additions and 847 deletions

View File

@ -147,6 +147,10 @@
#define HSI_VALUE ((uint32_t)8000000) /*!< Value of the Internal oscillator in Hz*/ #define HSI_VALUE ((uint32_t)8000000) /*!< Value of the Internal oscillator in Hz*/
#endif /* HSI_VALUE */ #endif /* HSI_VALUE */
#if !defined (LSE_VALUE)
#define LSE_VALUE ((uint32_t)32768) /*!< Value of the External Low Speed oscillator in Hz */
#endif
/** /**
* @brief STM32F10x Standard Peripheral Library version number * @brief STM32F10x Standard Peripheral Library version number
*/ */

View File

@ -94,6 +94,8 @@ extern uint32_t SystemCoreClock; /*!< System Clock Frequency (Core Cloc
extern void SystemInit(void); extern void SystemInit(void);
extern void SystemCoreClockUpdate(void); extern void SystemCoreClockUpdate(void);
extern void SetSysClock(void);
/** /**
* @} * @}
*/ */

View File

@ -26,13 +26,13 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#include "analogin_api.h" #include "analogin_api.h"
#include "wait_api.h"
#if DEVICE_ANALOGIN #if DEVICE_ANALOGIN
#include "cmsis.h" #include "cmsis.h"
#include "pinmap.h" #include "pinmap.h"
#include "error.h" #include "error.h"
#include "wait_api.h"
static const PinMap PinMap_ADC[] = { static const PinMap PinMap_ADC[] = {
{PA_0, ADC_1, STM_PIN_DATA(GPIO_Mode_AIN, 0)}, // ADC12_IN0 {PA_0, ADC_1, STM_PIN_DATA(GPIO_Mode_AIN, 0)}, // ADC12_IN0
@ -57,7 +57,6 @@ static const PinMap PinMap_ADC[] = {
int adc_inited = 0; int adc_inited = 0;
void analogin_init(analogin_t *obj, PinName pin) { void analogin_init(analogin_t *obj, PinName pin) {
ADC_TypeDef *adc; ADC_TypeDef *adc;
ADC_InitTypeDef ADC_InitStructure; ADC_InitTypeDef ADC_InitStructure;

View File

@ -37,15 +37,15 @@
#define DEVICE_INTERRUPTIN 1 #define DEVICE_INTERRUPTIN 1
#define DEVICE_ANALOGIN 1 #define DEVICE_ANALOGIN 1
#define DEVICE_ANALOGOUT 0 #define DEVICE_ANALOGOUT 0 // Not present on this device
#define DEVICE_SERIAL 1 #define DEVICE_SERIAL 1
#define DEVICE_I2C 1 #define DEVICE_I2C 1
#define DEVICE_I2CSLAVE 0 #define DEVICE_I2CSLAVE 0 // Not yet supported
#define DEVICE_SPI 1 #define DEVICE_SPI 1
#define DEVICE_SPISLAVE 0 #define DEVICE_SPISLAVE 0 // Not yet supported
#define DEVICE_RTC 1 #define DEVICE_RTC 1
@ -63,7 +63,7 @@
#define DEVICE_STDIO_MESSAGES 1 #define DEVICE_STDIO_MESSAGES 1
//#define DEVICE_ERROR_RED 0 #define DEVICE_ERROR_RED 0
#include "objects.h" #include "objects.h"

View File

@ -65,8 +65,7 @@ void gpio_mode(gpio_t *obj, PinMode mode) {
void gpio_dir(gpio_t *obj, PinDirection direction) { void gpio_dir(gpio_t *obj, PinDirection direction) {
if (direction == PIN_OUTPUT) { if (direction == PIN_OUTPUT) {
pin_function(obj->pin, STM_PIN_DATA(GPIO_Mode_Out_PP, 0)); pin_function(obj->pin, STM_PIN_DATA(GPIO_Mode_Out_PP, 0));
} } else { // PIN_INPUT
else { // PIN_INPUT
pin_function(obj->pin, STM_PIN_DATA(GPIO_Mode_IN_FLOATING, 0)); pin_function(obj->pin, STM_PIN_DATA(GPIO_Mode_IN_FLOATING, 0));
} }
} }

View File

@ -29,7 +29,6 @@
*/ */
#include <stddef.h> #include <stddef.h>
#include "cmsis.h" #include "cmsis.h"
#include "gpio_irq_api.h" #include "gpio_irq_api.h"
#include "pinmap.h" #include "pinmap.h"
#include "error.h" #include "error.h"
@ -53,8 +52,7 @@ static void handle_interrupt_in(uint32_t irq_index) {
uint32_t pin = (uint32_t)(1 << channel_pin[irq_index]); uint32_t pin = (uint32_t)(1 << channel_pin[irq_index]);
// Clear interrupt flag // Clear interrupt flag
if (EXTI_GetITStatus(pin) != RESET) if (EXTI_GetITStatus(pin) != RESET) {
{
EXTI_ClearITPendingBit(pin); EXTI_ClearITPendingBit(pin);
} }
@ -63,20 +61,33 @@ static void handle_interrupt_in(uint32_t irq_index) {
// Check which edge has generated the irq // Check which edge has generated the irq
if ((gpio->IDR & pin) == 0) { if ((gpio->IDR & pin) == 0) {
irq_handler(channel_ids[irq_index], IRQ_FALL); irq_handler(channel_ids[irq_index], IRQ_FALL);
} } else {
else {
irq_handler(channel_ids[irq_index], IRQ_RISE); irq_handler(channel_ids[irq_index], IRQ_RISE);
} }
} }
// The irq_index is passed to the function // The irq_index is passed to the function
static void gpio_irq0(void) {handle_interrupt_in(0);} // EXTI line 0 static void gpio_irq0(void) {
static void gpio_irq1(void) {handle_interrupt_in(1);} // EXTI line 1 handle_interrupt_in(0); // EXTI line 0
static void gpio_irq2(void) {handle_interrupt_in(2);} // EXTI line 2 }
static void gpio_irq3(void) {handle_interrupt_in(3);} // EXTI line 3 static void gpio_irq1(void) {
static void gpio_irq4(void) {handle_interrupt_in(4);} // EXTI line 4 handle_interrupt_in(1); // EXTI line 1
static void gpio_irq5(void) {handle_interrupt_in(5);} // EXTI lines 5 to 9 }
static void gpio_irq6(void) {handle_interrupt_in(6);} // EXTI lines 10 to 15 static void gpio_irq2(void) {
handle_interrupt_in(2); // EXTI line 2
}
static void gpio_irq3(void) {
handle_interrupt_in(3); // EXTI line 3
}
static void gpio_irq4(void) {
handle_interrupt_in(4); // EXTI line 4
}
static void gpio_irq5(void) {
handle_interrupt_in(5); // EXTI lines 5 to 9
}
static void gpio_irq6(void) {
handle_interrupt_in(6); // EXTI lines 10 to 15
}
extern uint32_t Set_GPIO_Clock(uint32_t port_idx); extern uint32_t Set_GPIO_Clock(uint32_t port_idx);
@ -205,8 +216,7 @@ void gpio_irq_set(gpio_irq_t *obj, gpio_irq_event event, uint32_t enable) {
if ((obj->event == EDGE_FALL) || (obj->event == EDGE_BOTH)) { if ((obj->event == EDGE_FALL) || (obj->event == EDGE_BOTH)) {
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
obj->event = EDGE_BOTH; obj->event = EDGE_BOTH;
} } else { // NONE or RISE
else { // NONE or RISE
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
obj->event = EDGE_RISE; obj->event = EDGE_RISE;
} }
@ -216,8 +226,7 @@ void gpio_irq_set(gpio_irq_t *obj, gpio_irq_event event, uint32_t enable) {
if ((obj->event == EDGE_RISE) || (obj->event == EDGE_BOTH)) { if ((obj->event == EDGE_RISE) || (obj->event == EDGE_BOTH)) {
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
obj->event = EDGE_BOTH; obj->event = EDGE_BOTH;
} } else { // NONE or FALL
else { // NONE or FALL
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
obj->event = EDGE_FALL; obj->event = EDGE_FALL;
} }
@ -225,8 +234,7 @@ void gpio_irq_set(gpio_irq_t *obj, gpio_irq_event event, uint32_t enable) {
if (enable) { if (enable) {
EXTI_InitStructure.EXTI_LineCmd = ENABLE; EXTI_InitStructure.EXTI_LineCmd = ENABLE;
} } else {
else {
EXTI_InitStructure.EXTI_LineCmd = DISABLE; EXTI_InitStructure.EXTI_LineCmd = DISABLE;
} }

View File

@ -50,8 +50,7 @@ typedef struct {
static inline void gpio_write(gpio_t *obj, int value) { static inline void gpio_write(gpio_t *obj, int value) {
if (value) { if (value) {
*obj->reg_set = obj->mask; *obj->reg_set = obj->mask;
} } else {
else {
*obj->reg_clr = obj->mask; *obj->reg_clr = obj->mask;
} }
} }

View File

@ -118,7 +118,6 @@ inline int i2c_start(i2c_t *obj) {
// Wait the START condition has been correctly sent // Wait the START condition has been correctly sent
timeout = FLAG_TIMEOUT; timeout = FLAG_TIMEOUT;
//while (I2C_CheckEvent(i2c, I2C_EVENT_MASTER_MODE_SELECT) == ERROR) {
while (I2C_GetFlagStatus(i2c, I2C_FLAG_SB) == RESET) { while (I2C_GetFlagStatus(i2c, I2C_FLAG_SB) == RESET) {
timeout--; timeout--;
if (timeout == 0) { if (timeout == 0) {
@ -145,17 +144,6 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
if (length == 0) return 0; if (length == 0) return 0;
/*
// Wait until the bus is not busy anymore
timeout = LONG_TIMEOUT;
while (I2C_GetFlagStatus(i2c, I2C_FLAG_BUSY) == SET) {
timeout--;
if (timeout == 0) {
return 0;
}
}
*/
i2c_start(obj); i2c_start(obj);
// Send slave address for read // Send slave address for read
@ -194,17 +182,6 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
int timeout; int timeout;
int count; int count;
/*
// Wait until the bus is not busy anymore
timeout = LONG_TIMEOUT;
while (I2C_GetFlagStatus(i2c, I2C_FLAG_BUSY) == SET) {
timeout--;
if (timeout == 0) {
return 0;
}
}
*/
i2c_start(obj); i2c_start(obj);
// Send slave address for write // Send slave address for write
@ -269,7 +246,6 @@ int i2c_byte_write(i2c_t *obj, int data) {
// Wait until the byte is transmitted // Wait until the byte is transmitted
timeout = FLAG_TIMEOUT; timeout = FLAG_TIMEOUT;
//while (I2C_CheckEvent(i2c, I2C_EVENT_MASTER_BYTE_TRANSMITTED) == ERROR) {
while ((I2C_GetFlagStatus(i2c, I2C_FLAG_TXE) == RESET) && while ((I2C_GetFlagStatus(i2c, I2C_FLAG_TXE) == RESET) &&
(I2C_GetFlagStatus(i2c, I2C_FLAG_BTF) == RESET)) { (I2C_GetFlagStatus(i2c, I2C_FLAG_BTF) == RESET)) {
timeout--; timeout--;
@ -319,7 +295,6 @@ void i2c_slave_mode(i2c_t *obj, int enable_slave) {
#define WriteAddressed 3 // the master is writing to this slave (slave = receiver) #define WriteAddressed 3 // the master is writing to this slave (slave = receiver)
int i2c_slave_receive(i2c_t *obj) { int i2c_slave_receive(i2c_t *obj) {
// TO BE DONE
return (0); return (0);
} }

View File

@ -146,8 +146,7 @@ void pin_mode(PinName pin, PinMode mode) {
if ((gpio->CRL & (0x03 << (pin_index * 4))) > 0) { // MODE bits = Output mode if ((gpio->CRL & (0x03 << (pin_index * 4))) > 0) { // MODE bits = Output mode
gpio->CRL |= (0x04 << (pin_index * 4)); // Set open-drain gpio->CRL |= (0x04 << (pin_index * 4)); // Set open-drain
} }
} } else {
else {
if ((gpio->CRH & (0x03 << ((pin_index % 8) * 4))) > 0) { // MODE bits = Output mode if ((gpio->CRH & (0x03 << ((pin_index % 8) * 4))) > 0) { // MODE bits = Output mode
gpio->CRH |= (0x04 << ((pin_index % 8) * 4)); // Set open-drain gpio->CRH |= (0x04 << ((pin_index % 8) * 4)); // Set open-drain
} }

View File

@ -28,12 +28,13 @@
******************************************************************************* *******************************************************************************
*/ */
#include "port_api.h" #include "port_api.h"
#if DEVICE_PORTIN || DEVICE_PORTOUT
#include "pinmap.h" #include "pinmap.h"
#include "gpio_api.h" #include "gpio_api.h"
#include "error.h" #include "error.h"
#if DEVICE_PORTIN || DEVICE_PORTOUT
extern uint32_t Set_GPIO_Clock(uint32_t port_idx); extern uint32_t Set_GPIO_Clock(uint32_t port_idx);
// high nibble = port number (0=A, 1=B, 2=C, 3=D, 4=E, 5=F, ...) // high nibble = port number (0=A, 1=B, 2=C, 3=D, 4=E, 5=F, ...)
@ -66,8 +67,7 @@ void port_dir(port_t *obj, PinDirection dir) {
if (obj->mask & (1 << i)) { // If the pin is used if (obj->mask & (1 << i)) { // If the pin is used
if (dir == PIN_OUTPUT) { if (dir == PIN_OUTPUT) {
pin_function(port_pin(obj->port, i), STM_PIN_DATA(GPIO_Mode_Out_PP, 0)); pin_function(port_pin(obj->port, i), STM_PIN_DATA(GPIO_Mode_Out_PP, 0));
} } else { // PIN_INPUT
else { // PIN_INPUT
pin_function(port_pin(obj->port, i), STM_PIN_DATA(GPIO_Mode_IN_FLOATING, 0)); pin_function(port_pin(obj->port, i), STM_PIN_DATA(GPIO_Mode_IN_FLOATING, 0));
} }
} }
@ -90,8 +90,7 @@ void port_write(port_t *obj, int value) {
int port_read(port_t *obj) { int port_read(port_t *obj) {
if (obj->direction == PIN_OUTPUT) { if (obj->direction == PIN_OUTPUT) {
return (*obj->reg_out & obj->mask); return (*obj->reg_out & obj->mask);
} } else { // PIN_INPUT
else { // PIN_INPUT
return (*obj->reg_in & obj->mask); return (*obj->reg_in & obj->mask);
} }
} }

View File

@ -29,6 +29,8 @@
*/ */
#include "pwmout_api.h" #include "pwmout_api.h"
#if DEVICE_PWMOUT
#include "cmsis.h" #include "cmsis.h"
#include "pinmap.h" #include "pinmap.h"
#include "error.h" #include "error.h"
@ -244,3 +246,5 @@ void pwmout_pulsewidth_us(pwmout_t* obj, int us) {
float value = (float)us / (float)obj->period; float value = (float)us / (float)obj->period;
pwmout_write(obj, value); pwmout_write(obj, value);
} }
#endif

View File

@ -29,26 +29,48 @@
*/ */
#include "rtc_api.h" #include "rtc_api.h"
#if DEVICE_RTC
#include "wait_api.h"
#define LSE_STARTUP_TIMEOUT ((uint16_t)700) // delay in ms
static int rtc_inited = 0; static int rtc_inited = 0;
void rtc_init(void) { void rtc_init(void) {
uint32_t StartUpCounter = 0;
uint32_t LSEStatus = 0;
uint32_t rtc_freq = 0;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); // Enable PWR and Backup clock RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); // Enable PWR and Backup clock
PWR_BackupAccessCmd(ENABLE); // Allow access to Backup Domain PWR_BackupAccessCmd(ENABLE); // Allow access to Backup Domain
BKP_DeInit(); // Reset Backup Domain BKP_DeInit(); // Reset Backup Domain
// Uncomment these lines if you use the LSE // Enable LSE clock
// Enable LSE and wait till it's ready RCC_LSEConfig(RCC_LSE_ON);
//RCC_LSEConfig(RCC_LSE_ON);
//while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET) {}
//RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); // Select LSE as RTC Clock Source
// Uncomment these lines if you use the LSI // Wait till LSE is ready
// Enable LSI and wait till it's ready do {
RCC_LSICmd(ENABLE); LSEStatus = RCC_GetFlagStatus(RCC_FLAG_LSERDY);
while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET) {} wait_ms(1);
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI); // Select LSI as RTC Clock Source StartUpCounter++;
} while ((LSEStatus == 0) && (StartUpCounter <= LSE_STARTUP_TIMEOUT));
if (StartUpCounter > LSE_STARTUP_TIMEOUT) {
// The LSE has not started, use LSI instead.
// The RTC Clock may vary due to LSI frequency dispersion.
RCC_LSEConfig(RCC_LSE_OFF);
RCC_LSICmd(ENABLE); // Enable LSI
while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET) {} // Wait until ready
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI); // Select the RTC Clock Source
rtc_freq = 40000; // [TODO] To be measured precisely using a timer input capture
} else {
// The LSE has correctly started
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); // Select the RTC Clock Source
rtc_freq = LSE_VALUE;
}
RCC_RTCCLKCmd(ENABLE); // Enable RTC Clock RCC_RTCCLKCmd(ENABLE); // Enable RTC Clock
@ -57,9 +79,7 @@ void rtc_init(void) {
RTC_WaitForLastTask(); // Wait until last write operation on RTC registers has finished RTC_WaitForLastTask(); // Wait until last write operation on RTC registers has finished
// Set RTC period to 1 sec // Set RTC period to 1 sec
// For LSE: prescaler = RTCCLK/RTC period = 32768Hz/1Hz = 32768 RTC_SetPrescaler(rtc_freq - 1);
// For LSI: prescaler = RTCCLK/RTC period = 40000Hz/1Hz = 40000
RTC_SetPrescaler(39999);
RTC_WaitForLastTask(); // Wait until last write operation on RTC registers has finished RTC_WaitForLastTask(); // Wait until last write operation on RTC registers has finished
@ -84,3 +104,5 @@ void rtc_write(time_t t) {
RTC_SetCounter(t); // Change the current time RTC_SetCounter(t); // Change the current time
RTC_WaitForLastTask(); // Wait until last write operation on RTC registers has finished RTC_WaitForLastTask(); // Wait until last write operation on RTC registers has finished
} }
#endif

View File

@ -28,6 +28,9 @@
******************************************************************************* *******************************************************************************
*/ */
#include "serial_api.h" #include "serial_api.h"
#if DEVICE_SERIAL
#include "cmsis.h" #include "cmsis.h"
#include "pinmap.h" #include "pinmap.h"
#include "error.h" #include "error.h"
@ -122,7 +125,6 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) {
stdio_uart_inited = 1; stdio_uart_inited = 1;
memcpy(&stdio_uart, obj, sizeof(serial_t)); memcpy(&stdio_uart, obj, sizeof(serial_t));
} }
} }
void serial_free(serial_t *obj) { void serial_free(serial_t *obj) {
@ -137,8 +139,7 @@ void serial_baud(serial_t *obj, int baudrate) {
void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits) { void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits) {
if (data_bits == 8) { if (data_bits == 8) {
obj->databits = USART_WordLength_8b; obj->databits = USART_WordLength_8b;
} } else {
else {
obj->databits = USART_WordLength_9b; obj->databits = USART_WordLength_9b;
} }
@ -158,8 +159,7 @@ void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_b
if (stop_bits == 2) { if (stop_bits == 2) {
obj->stopbits = USART_StopBits_2; obj->stopbits = USART_StopBits_2;
} } else {
else {
obj->stopbits = USART_StopBits_1; obj->stopbits = USART_StopBits_1;
} }
@ -223,8 +223,7 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) {
if (irq == RxIrq) { if (irq == RxIrq) {
USART_ITConfig(usart, USART_IT_RXNE, ENABLE); USART_ITConfig(usart, USART_IT_RXNE, ENABLE);
} } else { // TxIrq
else { // TxIrq
USART_ITConfig(usart, USART_IT_TC, ENABLE); USART_ITConfig(usart, USART_IT_TC, ENABLE);
} }
@ -239,8 +238,7 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) {
USART_ITConfig(usart, USART_IT_RXNE, DISABLE); USART_ITConfig(usart, USART_IT_RXNE, DISABLE);
// Check if TxIrq is disabled too // Check if TxIrq is disabled too
if ((usart->CR1 & USART_CR1_TXEIE) == 0) all_disabled = 1; if ((usart->CR1 & USART_CR1_TXEIE) == 0) all_disabled = 1;
} } else { // TxIrq
else { // TxIrq
USART_ITConfig(usart, USART_IT_TXE, DISABLE); USART_ITConfig(usart, USART_IT_TXE, DISABLE);
// Check if RxIrq is disabled too // Check if RxIrq is disabled too
if ((usart->CR1 & USART_CR1_RXNEIE) == 0) all_disabled = 1; if ((usart->CR1 & USART_CR1_RXNEIE) == 0) all_disabled = 1;
@ -300,3 +298,5 @@ void serial_break_set(serial_t *obj) {
void serial_break_clear(serial_t *obj) { void serial_break_clear(serial_t *obj) {
} }
#endif

View File

@ -28,13 +28,12 @@
******************************************************************************* *******************************************************************************
*/ */
#include "sleep_api.h" #include "sleep_api.h"
#if DEVICE_SLEEP
#include "cmsis.h" #include "cmsis.h"
// This function is in the system_stm32f10x.c file void sleep(void) {
extern void SetSysClock(void);
void sleep(void)
{
// Disable us_ticker update interrupt // Disable us_ticker update interrupt
TIM_ITConfig(TIM1, TIM_IT_Update, DISABLE); TIM_ITConfig(TIM1, TIM_IT_Update, DISABLE);
@ -45,8 +44,7 @@ void sleep(void)
TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE); TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE);
} }
void deepsleep(void) void deepsleep(void) {
{
// Disable us_ticker update interrupt // Disable us_ticker update interrupt
TIM_ITConfig(TIM1, TIM_IT_Update, DISABLE); TIM_ITConfig(TIM1, TIM_IT_Update, DISABLE);
@ -62,3 +60,5 @@ void deepsleep(void)
// Re-enable us_ticker update interrupt // Re-enable us_ticker update interrupt
TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE); TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE);
} }
#endif

View File

@ -122,8 +122,7 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel
if (ssel == NC) { // Master if (ssel == NC) { // Master
obj->mode = SPI_Mode_Master; obj->mode = SPI_Mode_Master;
obj->nss = SPI_NSS_Soft; obj->nss = SPI_NSS_Soft;
} } else { // Slave
else { // Slave
pinmap_pinout(ssel, PinMap_SPI_SSEL); pinmap_pinout(ssel, PinMap_SPI_SSEL);
obj->mode = SPI_Mode_Slave; obj->mode = SPI_Mode_Slave;
obj->nss = SPI_NSS_Soft; obj->nss = SPI_NSS_Soft;
@ -141,8 +140,7 @@ void spi_format(spi_t *obj, int bits, int mode, int slave) {
// Save new values // Save new values
if (bits == 8) { if (bits == 8) {
obj->bits = SPI_DataSize_8b; obj->bits = SPI_DataSize_8b;
} } else {
else {
obj->bits = SPI_DataSize_16b; obj->bits = SPI_DataSize_16b;
} }
@ -168,8 +166,7 @@ void spi_format(spi_t *obj, int bits, int mode, int slave) {
if (slave == 0) { if (slave == 0) {
obj->mode = SPI_Mode_Master; obj->mode = SPI_Mode_Master;
obj->nss = SPI_NSS_Soft; obj->nss = SPI_NSS_Soft;
} } else {
else {
obj->mode = SPI_Mode_Slave; obj->mode = SPI_Mode_Slave;
obj->nss = SPI_NSS_Hard; obj->nss = SPI_NSS_Hard;
} }
@ -182,26 +179,19 @@ void spi_frequency(spi_t *obj, int hz) {
// Values depend of PCLK2: 64 MHz if HSI is used, 72 MHz if HSE is used // Values depend of PCLK2: 64 MHz if HSI is used, 72 MHz if HSE is used
if (hz < 500000) { if (hz < 500000) {
obj->br_presc = SPI_BaudRatePrescaler_256; // 250 kHz - 281 kHz obj->br_presc = SPI_BaudRatePrescaler_256; // 250 kHz - 281 kHz
} } else if ((hz >= 500000) && (hz < 1000000)) {
else if ((hz >= 500000) && (hz < 1000000)) {
obj->br_presc = SPI_BaudRatePrescaler_128; // 500 kHz - 563 kHz obj->br_presc = SPI_BaudRatePrescaler_128; // 500 kHz - 563 kHz
} } else if ((hz >= 1000000) && (hz < 2000000)) {
else if ((hz >= 1000000) && (hz < 2000000)) {
obj->br_presc = SPI_BaudRatePrescaler_64; // 1 MHz - 1.13 MHz obj->br_presc = SPI_BaudRatePrescaler_64; // 1 MHz - 1.13 MHz
} } else if ((hz >= 2000000) && (hz < 4000000)) {
else if ((hz >= 2000000) && (hz < 4000000)) {
obj->br_presc = SPI_BaudRatePrescaler_32; // 2 MHz - 2.25 MHz obj->br_presc = SPI_BaudRatePrescaler_32; // 2 MHz - 2.25 MHz
} } else if ((hz >= 4000000) && (hz < 8000000)) {
else if ((hz >= 4000000) && (hz < 8000000)) {
obj->br_presc = SPI_BaudRatePrescaler_16; // 4 MHz - 4.5 MHz obj->br_presc = SPI_BaudRatePrescaler_16; // 4 MHz - 4.5 MHz
} } else if ((hz >= 8000000) && (hz < 16000000)) {
else if ((hz >= 8000000) && (hz < 16000000)) {
obj->br_presc = SPI_BaudRatePrescaler_8; // 8 MHz - 9 MHz obj->br_presc = SPI_BaudRatePrescaler_8; // 8 MHz - 9 MHz
} } else if ((hz >= 16000000) && (hz < 32000000)) {
else if ((hz >= 16000000) && (hz < 32000000)) {
obj->br_presc = SPI_BaudRatePrescaler_4; // 16 MHz - 18 MHz obj->br_presc = SPI_BaudRatePrescaler_4; // 16 MHz - 18 MHz
} } else { // >= 32000000
else { // >= 32000000
obj->br_presc = SPI_BaudRatePrescaler_2; // 32 MHz - 36 MHz obj->br_presc = SPI_BaudRatePrescaler_2; // 32 MHz - 36 MHz
} }
} }
@ -210,26 +200,19 @@ void spi_frequency(spi_t *obj, int hz) {
// Values depend of PCLK1: 32 MHz if HSI is used, 36 MHz if HSE is used // Values depend of PCLK1: 32 MHz if HSI is used, 36 MHz if HSE is used
if (hz < 250000) { if (hz < 250000) {
obj->br_presc = SPI_BaudRatePrescaler_256; // 125 kHz - 141 kHz obj->br_presc = SPI_BaudRatePrescaler_256; // 125 kHz - 141 kHz
} } else if ((hz >= 250000) && (hz < 500000)) {
else if ((hz >= 250000) && (hz < 500000)) {
obj->br_presc = SPI_BaudRatePrescaler_128; // 250 kHz - 281 kHz obj->br_presc = SPI_BaudRatePrescaler_128; // 250 kHz - 281 kHz
} } else if ((hz >= 500000) && (hz < 1000000)) {
else if ((hz >= 500000) && (hz < 1000000)) {
obj->br_presc = SPI_BaudRatePrescaler_64; // 500 kHz - 563 kHz obj->br_presc = SPI_BaudRatePrescaler_64; // 500 kHz - 563 kHz
} } else if ((hz >= 1000000) && (hz < 2000000)) {
else if ((hz >= 1000000) && (hz < 2000000)) {
obj->br_presc = SPI_BaudRatePrescaler_32; // 1 MHz - 1.13 MHz obj->br_presc = SPI_BaudRatePrescaler_32; // 1 MHz - 1.13 MHz
} } else if ((hz >= 2000000) && (hz < 4000000)) {
else if ((hz >= 2000000) && (hz < 4000000)) {
obj->br_presc = SPI_BaudRatePrescaler_16; // 2 MHz - 2.25 MHz obj->br_presc = SPI_BaudRatePrescaler_16; // 2 MHz - 2.25 MHz
} } else if ((hz >= 4000000) && (hz < 8000000)) {
else if ((hz >= 4000000) && (hz < 8000000)) {
obj->br_presc = SPI_BaudRatePrescaler_8; // 4 MHz - 4.5 MHz obj->br_presc = SPI_BaudRatePrescaler_8; // 4 MHz - 4.5 MHz
} } else if ((hz >= 8000000) && (hz < 16000000)) {
else if ((hz >= 8000000) && (hz < 16000000)) {
obj->br_presc = SPI_BaudRatePrescaler_4; // 8 MHz - 9 MHz obj->br_presc = SPI_BaudRatePrescaler_4; // 8 MHz - 9 MHz
} } else { // >= 16000000
else { // >= 16000000
obj->br_presc = SPI_BaudRatePrescaler_2; // 16 MHz - 18 MHz obj->br_presc = SPI_BaudRatePrescaler_2; // 16 MHz - 18 MHz
} }
} }

View File

@ -59,14 +59,12 @@ static void tim_irq_handler(void) {
if (oc_rem_part > 0) { if (oc_rem_part > 0) {
set_compare(oc_rem_part); // Finish the remaining time left set_compare(oc_rem_part); // Finish the remaining time left
oc_rem_part = 0; oc_rem_part = 0;
} } else {
else {
if (oc_int_part > 0) { if (oc_int_part > 0) {
set_compare(0xFFFF); set_compare(0xFFFF);
oc_rem_part = cval; // To finish the counter loop the next time oc_rem_part = cval; // To finish the counter loop the next time
oc_int_part--; oc_int_part--;
} } else {
else {
us_ticker_irq_handler(); us_ticker_irq_handler();
} }
} }
@ -79,7 +77,7 @@ void us_ticker_init(void) {
if (us_ticker_inited) return; if (us_ticker_inited) return;
us_ticker_inited = 1; us_ticker_inited = 1;
// Enable Timer clock // Enable timer clock
TIM_MST_RCC; TIM_MST_RCC;
// Configure time base // Configure time base
@ -129,8 +127,7 @@ void us_ticker_set_interrupt(unsigned int timestamp) {
if (delta <= 0) { // This event was in the past if (delta <= 0) { // This event was in the past
us_ticker_irq_handler(); us_ticker_irq_handler();
} } else {
else {
oc_int_part = (uint32_t)(delta >> 16); oc_int_part = (uint32_t)(delta >> 16);
oc_rem_part = (uint16_t)(delta & 0xFFFF); oc_rem_part = (uint16_t)(delta & 0xFFFF);
if (oc_rem_part <= (0xFFFF - cval)) { if (oc_rem_part <= (0xFFFF - cval)) {

View File

@ -26,13 +26,13 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#include "analogin_api.h" #include "analogin_api.h"
#include "wait_api.h"
#if DEVICE_ANALOGIN #if DEVICE_ANALOGIN
#include "cmsis.h" #include "cmsis.h"
#include "pinmap.h" #include "pinmap.h"
#include "error.h" #include "error.h"
#include "wait_api.h"
static const PinMap PinMap_ADC[] = { static const PinMap PinMap_ADC[] = {
{PA_0, ADC_1, STM_PIN_DATA(GPIO_Mode_AN, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0xFF)}, // ADC_IN0 {PA_0, ADC_1, STM_PIN_DATA(GPIO_Mode_AN, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0xFF)}, // ADC_IN0

View File

@ -113,8 +113,7 @@ void analogout_write(dac_t *obj, float value) {
void analogout_write_u16(dac_t *obj, uint16_t value) { void analogout_write_u16(dac_t *obj, uint16_t value) {
if (value > (uint16_t)RANGE_12BIT) { if (value > (uint16_t)RANGE_12BIT) {
dac_write(obj, (uint16_t)RANGE_12BIT); // Max value dac_write(obj, (uint16_t)RANGE_12BIT); // Max value
} } else {
else {
dac_write(obj, value); dac_write(obj, value);
} }
} }

View File

@ -65,8 +65,7 @@ void gpio_mode(gpio_t *obj, PinMode mode) {
void gpio_dir(gpio_t *obj, PinDirection direction) { void gpio_dir(gpio_t *obj, PinDirection direction) {
if (direction == PIN_OUTPUT) { if (direction == PIN_OUTPUT) {
pin_function(obj->pin, STM_PIN_DATA(GPIO_Mode_OUT, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0xFF)); pin_function(obj->pin, STM_PIN_DATA(GPIO_Mode_OUT, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0xFF));
} } else { // PIN_INPUT
else { // PIN_INPUT
pin_function(obj->pin, STM_PIN_DATA(GPIO_Mode_IN, 0, GPIO_PuPd_NOPULL, 0xFF)); pin_function(obj->pin, STM_PIN_DATA(GPIO_Mode_IN, 0, GPIO_PuPd_NOPULL, 0xFF));
} }
} }

View File

@ -29,7 +29,6 @@
*/ */
#include <stddef.h> #include <stddef.h>
#include "cmsis.h" #include "cmsis.h"
#include "gpio_irq_api.h" #include "gpio_irq_api.h"
#include "pinmap.h" #include "pinmap.h"
#include "error.h" #include "error.h"
@ -53,8 +52,7 @@ static void handle_interrupt_in(uint32_t irq_index) {
uint32_t pin = (uint32_t)(1 << channel_pin[irq_index]); uint32_t pin = (uint32_t)(1 << channel_pin[irq_index]);
// Clear interrupt flag // Clear interrupt flag
if (EXTI_GetITStatus(pin) != RESET) if (EXTI_GetITStatus(pin) != RESET) {
{
EXTI_ClearITPendingBit(pin); EXTI_ClearITPendingBit(pin);
} }
@ -63,20 +61,33 @@ static void handle_interrupt_in(uint32_t irq_index) {
// Check which edge has generated the irq // Check which edge has generated the irq
if ((gpio->IDR & pin) == 0) { if ((gpio->IDR & pin) == 0) {
irq_handler(channel_ids[irq_index], IRQ_FALL); irq_handler(channel_ids[irq_index], IRQ_FALL);
} } else {
else {
irq_handler(channel_ids[irq_index], IRQ_RISE); irq_handler(channel_ids[irq_index], IRQ_RISE);
} }
} }
// The irq_index is passed to the function // The irq_index is passed to the function
static void gpio_irq0(void) {handle_interrupt_in(0);} // EXTI line 0 static void gpio_irq0(void) {
static void gpio_irq1(void) {handle_interrupt_in(1);} // EXTI line 1 handle_interrupt_in(0); // EXTI line 0
static void gpio_irq2(void) {handle_interrupt_in(2);} // EXTI line 2 }
static void gpio_irq3(void) {handle_interrupt_in(3);} // EXTI line 3 static void gpio_irq1(void) {
static void gpio_irq4(void) {handle_interrupt_in(4);} // EXTI line 4 handle_interrupt_in(1); // EXTI line 1
static void gpio_irq5(void) {handle_interrupt_in(5);} // EXTI lines 5 to 9 }
static void gpio_irq6(void) {handle_interrupt_in(6);} // EXTI lines 10 to 15 static void gpio_irq2(void) {
handle_interrupt_in(2); // EXTI line 2
}
static void gpio_irq3(void) {
handle_interrupt_in(3); // EXTI line 3
}
static void gpio_irq4(void) {
handle_interrupt_in(4); // EXTI line 4
}
static void gpio_irq5(void) {
handle_interrupt_in(5); // EXTI lines 5 to 9
}
static void gpio_irq6(void) {
handle_interrupt_in(6); // EXTI lines 10 to 15
}
extern uint32_t Set_GPIO_Clock(uint32_t port_idx); extern uint32_t Set_GPIO_Clock(uint32_t port_idx);
@ -205,8 +216,7 @@ void gpio_irq_set(gpio_irq_t *obj, gpio_irq_event event, uint32_t enable) {
if ((obj->event == EDGE_FALL) || (obj->event == EDGE_BOTH)) { if ((obj->event == EDGE_FALL) || (obj->event == EDGE_BOTH)) {
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
obj->event = EDGE_BOTH; obj->event = EDGE_BOTH;
} } else { // NONE or RISE
else { // NONE or RISE
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
obj->event = EDGE_RISE; obj->event = EDGE_RISE;
} }
@ -216,8 +226,7 @@ void gpio_irq_set(gpio_irq_t *obj, gpio_irq_event event, uint32_t enable) {
if ((obj->event == EDGE_RISE) || (obj->event == EDGE_BOTH)) { if ((obj->event == EDGE_RISE) || (obj->event == EDGE_BOTH)) {
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
obj->event = EDGE_BOTH; obj->event = EDGE_BOTH;
} } else { // NONE or FALL
else { // NONE or FALL
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
obj->event = EDGE_FALL; obj->event = EDGE_FALL;
} }
@ -225,8 +234,7 @@ void gpio_irq_set(gpio_irq_t *obj, gpio_irq_event event, uint32_t enable) {
if (enable) { if (enable) {
EXTI_InitStructure.EXTI_LineCmd = ENABLE; EXTI_InitStructure.EXTI_LineCmd = ENABLE;
} } else {
else {
EXTI_InitStructure.EXTI_LineCmd = DISABLE; EXTI_InitStructure.EXTI_LineCmd = DISABLE;
} }

View File

@ -50,8 +50,7 @@ typedef struct {
static inline void gpio_write(gpio_t *obj, int value) { static inline void gpio_write(gpio_t *obj, int value) {
if (value) { if (value) {
*obj->reg_set = obj->mask; *obj->reg_set = obj->mask;
} } else {
else {
*obj->reg_clr = obj->mask; *obj->reg_clr = obj->mask;
} }
} }

View File

@ -147,8 +147,7 @@ inline int i2c_stop(i2c_t *obj) {
} }
temp = i2c->SR1; temp = i2c->SR1;
I2C_Cmd(i2c, ENABLE); I2C_Cmd(i2c, ENABLE);
} } else {
else {
I2C_GenerateSTOP(i2c, ENABLE); I2C_GenerateSTOP(i2c, ENABLE);
} }

View File

@ -25,8 +25,7 @@
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#include "cmsis.h"
extern void SystemCoreClockUpdate(void);
// This function is called after RAM initialization and before main. // This function is called after RAM initialization and before main.
void mbed_sdk_init() { void mbed_sdk_init() {

View File

@ -28,12 +28,13 @@
******************************************************************************* *******************************************************************************
*/ */
#include "port_api.h" #include "port_api.h"
#if DEVICE_PORTIN || DEVICE_PORTOUT
#include "pinmap.h" #include "pinmap.h"
#include "gpio_api.h" #include "gpio_api.h"
#include "error.h" #include "error.h"
#if DEVICE_PORTIN || DEVICE_PORTOUT
extern uint32_t Set_GPIO_Clock(uint32_t port_idx); extern uint32_t Set_GPIO_Clock(uint32_t port_idx);
// high nibble = port number (0=A, 1=B, 2=C, 3=D, 4=E, 5=F, ...) // high nibble = port number (0=A, 1=B, 2=C, 3=D, 4=E, 5=F, ...)
@ -66,8 +67,7 @@ void port_dir(port_t *obj, PinDirection dir) {
if (obj->mask & (1 << i)) { // If the pin is used if (obj->mask & (1 << i)) { // If the pin is used
if (dir == PIN_OUTPUT) { if (dir == PIN_OUTPUT) {
pin_function(port_pin(obj->port, i), STM_PIN_DATA(GPIO_Mode_OUT, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0xFF)); pin_function(port_pin(obj->port, i), STM_PIN_DATA(GPIO_Mode_OUT, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0xFF));
} } else { // PIN_INPUT
else { // PIN_INPUT
pin_function(port_pin(obj->port, i), STM_PIN_DATA(GPIO_Mode_IN, 0, GPIO_PuPd_NOPULL, 0xFF)); pin_function(port_pin(obj->port, i), STM_PIN_DATA(GPIO_Mode_IN, 0, GPIO_PuPd_NOPULL, 0xFF));
} }
} }
@ -90,8 +90,7 @@ void port_write(port_t *obj, int value) {
int port_read(port_t *obj) { int port_read(port_t *obj) {
if (obj->direction == PIN_OUTPUT) { if (obj->direction == PIN_OUTPUT) {
return (*obj->reg_out & obj->mask); return (*obj->reg_out & obj->mask);
} } else { // PIN_INPUT
else { // PIN_INPUT
return (*obj->reg_in & obj->mask); return (*obj->reg_in & obj->mask);
} }
} }

View File

@ -29,6 +29,8 @@
*/ */
#include "pwmout_api.h" #include "pwmout_api.h"
#if DEVICE_PWMOUT
#include "cmsis.h" #include "cmsis.h"
#include "pinmap.h" #include "pinmap.h"
#include "error.h" #include "error.h"
@ -225,3 +227,5 @@ void pwmout_pulsewidth_us(pwmout_t* obj, int us) {
float value = (float)us / (float)obj->period; float value = (float)us / (float)obj->period;
pwmout_write(obj, value); pwmout_write(obj, value);
} }
#endif

View File

@ -29,35 +29,62 @@
*/ */
#include "rtc_api.h" #include "rtc_api.h"
#if DEVICE_RTC
#include "wait_api.h"
#define LSE_STARTUP_TIMEOUT ((uint16_t)400) // delay in ms
static int rtc_inited = 0; static int rtc_inited = 0;
void rtc_init(void) { void rtc_init(void) {
uint32_t StartUpCounter = 0;
uint32_t LSEStatus = 0;
uint32_t rtc_freq = 0;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); // Enable PWR clock RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); // Enable PWR clock
PWR_RTCAccessCmd(ENABLE); // Enable access to RTC PWR_RTCAccessCmd(ENABLE); // Enable access to Backup domain
// Note: the LSI is used as RTC source clock // Reset RTC and Backup registers
RCC_RTCResetCmd(ENABLE);
RCC_RTCResetCmd(DISABLE);
// Enable LSE clock
RCC_LSEConfig(RCC_LSE_ON);
// Wait till LSE is ready
do {
LSEStatus = RCC_GetFlagStatus(RCC_FLAG_LSERDY);
wait_ms(1);
StartUpCounter++;
} while ((LSEStatus == 0) && (StartUpCounter <= LSE_STARTUP_TIMEOUT));
if (StartUpCounter > LSE_STARTUP_TIMEOUT) {
// The LSE has not started, use LSI instead.
// The RTC Clock may vary due to LSI frequency dispersion. // The RTC Clock may vary due to LSI frequency dispersion.
RCC_LSEConfig(RCC_LSE_OFF);
RCC_LSICmd(ENABLE); // Enable LSI RCC_LSICmd(ENABLE); // Enable LSI
while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET) {} // Wait until ready while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET) {} // Wait until ready
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI); // Select the RTC Clock Source
rtc_freq = 40000; // [TODO] To be measured precisely using a timer input capture
} else {
// The LSE has correctly started
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); // Select the RTC Clock Source
rtc_freq = LSE_VALUE;
}
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI); // Select LSI as RTC Clock Source RTC_InitTypeDef RTC_InitStructure;
RTC_InitStructure.RTC_AsynchPrediv = 127;
RTC_InitStructure.RTC_SynchPrediv = (rtc_freq / 128) - 1;
RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24;
RTC_Init(&RTC_InitStructure);
RCC_RTCCLKCmd(ENABLE); // Enable RTC Clock RCC_RTCCLKCmd(ENABLE); // Enable RTC Clock
RTC_WaitForSynchro(); // Wait for RTC registers synchronization RTC_WaitForSynchro(); // Wait for RTC registers synchronization
uint32_t lsi_freq = 40000; // [TODO] To be measured precisely using a timer input capture PWR_RTCAccessCmd(DISABLE); // Disable access to Backup domain
RTC_InitTypeDef RTC_InitStructure;
RTC_InitStructure.RTC_AsynchPrediv = 127;
RTC_InitStructure.RTC_SynchPrediv = (lsi_freq / 128) - 1;
RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24;
RTC_Init(&RTC_InitStructure);
PWR_RTCAccessCmd(DISABLE); // Disable access to RTC
rtc_inited = 1; rtc_inited = 1;
} }
@ -130,8 +157,10 @@ void rtc_write(time_t t) {
timeStruct.RTC_H12 = RTC_HourFormat_24; timeStruct.RTC_H12 = RTC_HourFormat_24;
// Change the RTC current date/time // Change the RTC current date/time
PWR_RTCAccessCmd(ENABLE); // Enable access to RTC PWR_RTCAccessCmd(ENABLE); // Enable access to Backup domain
RTC_SetDate(RTC_Format_BIN, &dateStruct); RTC_SetDate(RTC_Format_BIN, &dateStruct);
RTC_SetTime(RTC_Format_BIN, &timeStruct); RTC_SetTime(RTC_Format_BIN, &timeStruct);
PWR_RTCAccessCmd(DISABLE); // Disable access to RTC PWR_RTCAccessCmd(DISABLE); // Disable access to Backup domain
} }
#endif

View File

@ -28,6 +28,9 @@
******************************************************************************* *******************************************************************************
*/ */
#include "serial_api.h" #include "serial_api.h"
#if DEVICE_SERIAL
#include "cmsis.h" #include "cmsis.h"
#include "pinmap.h" #include "pinmap.h"
#include "error.h" #include "error.h"
@ -150,8 +153,7 @@ void serial_baud(serial_t *obj, int baudrate) {
void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits) { void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits) {
if (data_bits == 8) { if (data_bits == 8) {
obj->databits = USART_WordLength_8b; obj->databits = USART_WordLength_8b;
} } else {
else {
obj->databits = USART_WordLength_9b; obj->databits = USART_WordLength_9b;
} }
@ -171,8 +173,7 @@ void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_b
if (stop_bits == 2) { if (stop_bits == 2) {
obj->stopbits = USART_StopBits_2; obj->stopbits = USART_StopBits_2;
} } else {
else {
obj->stopbits = USART_StopBits_1; obj->stopbits = USART_StopBits_1;
} }
@ -197,11 +198,21 @@ static void uart_irq(USART_TypeDef* usart, int id) {
} }
} }
static void uart1_irq(void) {uart_irq((USART_TypeDef*)UART_1, 0);} static void uart1_irq(void) {
static void uart2_irq(void) {uart_irq((USART_TypeDef*)UART_2, 1);} uart_irq((USART_TypeDef*)UART_1, 0);
static void uart3_irq(void) {uart_irq((USART_TypeDef*)UART_3, 2);} }
static void uart4_irq(void) {uart_irq((USART_TypeDef*)UART_4, 3);} static void uart2_irq(void) {
static void uart5_irq(void) {uart_irq((USART_TypeDef*)UART_5, 4);} uart_irq((USART_TypeDef*)UART_2, 1);
}
static void uart3_irq(void) {
uart_irq((USART_TypeDef*)UART_3, 2);
}
static void uart4_irq(void) {
uart_irq((USART_TypeDef*)UART_4, 3);
}
static void uart5_irq(void) {
uart_irq((USART_TypeDef*)UART_5, 4);
}
void serial_irq_handler(serial_t *obj, uart_irq_handler handler, uint32_t id) { void serial_irq_handler(serial_t *obj, uart_irq_handler handler, uint32_t id) {
irq_handler = handler; irq_handler = handler;
@ -242,8 +253,7 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) {
if (irq == RxIrq) { if (irq == RxIrq) {
USART_ITConfig(usart, USART_IT_RXNE, ENABLE); USART_ITConfig(usart, USART_IT_RXNE, ENABLE);
} } else { // TxIrq
else { // TxIrq
USART_ITConfig(usart, USART_IT_TC, ENABLE); USART_ITConfig(usart, USART_IT_TC, ENABLE);
} }
@ -258,8 +268,7 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) {
USART_ITConfig(usart, USART_IT_RXNE, DISABLE); USART_ITConfig(usart, USART_IT_RXNE, DISABLE);
// Check if TxIrq is disabled too // Check if TxIrq is disabled too
if ((usart->CR1 & USART_CR1_TXEIE) == 0) all_disabled = 1; if ((usart->CR1 & USART_CR1_TXEIE) == 0) all_disabled = 1;
} } else { // TxIrq
else { // TxIrq
USART_ITConfig(usart, USART_IT_TXE, DISABLE); USART_ITConfig(usart, USART_IT_TXE, DISABLE);
// Check if RxIrq is disabled too // Check if RxIrq is disabled too
if ((usart->CR1 & USART_CR1_RXNEIE) == 0) all_disabled = 1; if ((usart->CR1 & USART_CR1_RXNEIE) == 0) all_disabled = 1;
@ -319,3 +328,5 @@ void serial_break_set(serial_t *obj) {
void serial_break_clear(serial_t *obj) { void serial_break_clear(serial_t *obj) {
} }
#endif

View File

@ -28,14 +28,13 @@
******************************************************************************* *******************************************************************************
*/ */
#include "sleep_api.h" #include "sleep_api.h"
#if DEVICE_SLEEP
#include "cmsis.h" #include "cmsis.h"
// This function is in the system_stm32l1xx.c file
extern void SetSysClock(void);
// MCU SLEEP mode // MCU SLEEP mode
void sleep(void) void sleep(void) {
{
// Enable PWR clock // Enable PWR clock
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
@ -44,8 +43,7 @@ void sleep(void)
} }
// MCU STOP mode (Regulator in LP mode, LSI, HSI and HSE OFF) // MCU STOP mode (Regulator in LP mode, LSI, HSI and HSE OFF)
void deepsleep(void) void deepsleep(void) {
{
// Enable PWR clock // Enable PWR clock
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
@ -58,3 +56,5 @@ void deepsleep(void)
// After wake-up from STOP reconfigure the PLL // After wake-up from STOP reconfigure the PLL
SetSysClock(); SetSysClock();
} }
#endif

View File

@ -135,8 +135,7 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel
if (ssel == NC) { // Master if (ssel == NC) { // Master
obj->mode = SPI_Mode_Master; obj->mode = SPI_Mode_Master;
obj->nss = SPI_NSS_Soft; obj->nss = SPI_NSS_Soft;
} } else { // Slave
else { // Slave
pinmap_pinout(ssel, PinMap_SPI_SSEL); pinmap_pinout(ssel, PinMap_SPI_SSEL);
obj->mode = SPI_Mode_Slave; obj->mode = SPI_Mode_Slave;
obj->nss = SPI_NSS_Soft; obj->nss = SPI_NSS_Soft;
@ -154,8 +153,7 @@ void spi_format(spi_t *obj, int bits, int mode, int slave) {
// Save new values // Save new values
if (bits == 8) { if (bits == 8) {
obj->bits = SPI_DataSize_8b; obj->bits = SPI_DataSize_8b;
} } else {
else {
obj->bits = SPI_DataSize_16b; obj->bits = SPI_DataSize_16b;
} }
@ -181,8 +179,7 @@ void spi_format(spi_t *obj, int bits, int mode, int slave) {
if (slave == 0) { if (slave == 0) {
obj->mode = SPI_Mode_Master; obj->mode = SPI_Mode_Master;
obj->nss = SPI_NSS_Soft; obj->nss = SPI_NSS_Soft;
} } else {
else {
obj->mode = SPI_Mode_Slave; obj->mode = SPI_Mode_Slave;
obj->nss = SPI_NSS_Hard; obj->nss = SPI_NSS_Hard;
} }
@ -195,52 +192,37 @@ void spi_frequency(spi_t *obj, int hz) {
if (SystemCoreClock == 32000000) { // HSI if (SystemCoreClock == 32000000) { // HSI
if (hz < 250000) { if (hz < 250000) {
obj->br_presc = SPI_BaudRatePrescaler_256; // 125 kHz obj->br_presc = SPI_BaudRatePrescaler_256; // 125 kHz
} } else if ((hz >= 250000) && (hz < 500000)) {
else if ((hz >= 250000) && (hz < 500000)) {
obj->br_presc = SPI_BaudRatePrescaler_128; // 250 kHz obj->br_presc = SPI_BaudRatePrescaler_128; // 250 kHz
} } else if ((hz >= 500000) && (hz < 1000000)) {
else if ((hz >= 500000) && (hz < 1000000)) {
obj->br_presc = SPI_BaudRatePrescaler_64; // 500 kHz obj->br_presc = SPI_BaudRatePrescaler_64; // 500 kHz
} } else if ((hz >= 1000000) && (hz < 2000000)) {
else if ((hz >= 1000000) && (hz < 2000000)) {
obj->br_presc = SPI_BaudRatePrescaler_32; // 1 MHz obj->br_presc = SPI_BaudRatePrescaler_32; // 1 MHz
} } else if ((hz >= 2000000) && (hz < 4000000)) {
else if ((hz >= 2000000) && (hz < 4000000)) {
obj->br_presc = SPI_BaudRatePrescaler_16; // 2 MHz obj->br_presc = SPI_BaudRatePrescaler_16; // 2 MHz
} } else if ((hz >= 4000000) && (hz < 8000000)) {
else if ((hz >= 4000000) && (hz < 8000000)) {
obj->br_presc = SPI_BaudRatePrescaler_8; // 4 MHz obj->br_presc = SPI_BaudRatePrescaler_8; // 4 MHz
} } else if ((hz >= 8000000) && (hz < 16000000)) {
else if ((hz >= 8000000) && (hz < 16000000)) {
obj->br_presc = SPI_BaudRatePrescaler_4; // 8 MHz obj->br_presc = SPI_BaudRatePrescaler_4; // 8 MHz
} } else { // >= 16000000
else { // >= 16000000
obj->br_presc = SPI_BaudRatePrescaler_2; // 16 MHz obj->br_presc = SPI_BaudRatePrescaler_2; // 16 MHz
} }
} } else { // 24 MHz - HSE
else { // 24 MHz - HSE
if (hz < 180000) { if (hz < 180000) {
obj->br_presc = SPI_BaudRatePrescaler_256; // 94 kHz obj->br_presc = SPI_BaudRatePrescaler_256; // 94 kHz
} } else if ((hz >= 180000) && (hz < 350000)) {
else if ((hz >= 180000) && (hz < 350000)) {
obj->br_presc = SPI_BaudRatePrescaler_128; // 188 kHz obj->br_presc = SPI_BaudRatePrescaler_128; // 188 kHz
} } else if ((hz >= 350000) && (hz < 750000)) {
else if ((hz >= 350000) && (hz < 750000)) {
obj->br_presc = SPI_BaudRatePrescaler_64; // 375 kHz obj->br_presc = SPI_BaudRatePrescaler_64; // 375 kHz
} } else if ((hz >= 750000) && (hz < 1000000)) {
else if ((hz >= 750000) && (hz < 1000000)) {
obj->br_presc = SPI_BaudRatePrescaler_32; // 750 kHz obj->br_presc = SPI_BaudRatePrescaler_32; // 750 kHz
} } else if ((hz >= 1000000) && (hz < 3000000)) {
else if ((hz >= 1000000) && (hz < 3000000)) {
obj->br_presc = SPI_BaudRatePrescaler_16; // 1.5 MHz obj->br_presc = SPI_BaudRatePrescaler_16; // 1.5 MHz
} } else if ((hz >= 3000000) && (hz < 6000000)) {
else if ((hz >= 3000000) && (hz < 6000000)) {
obj->br_presc = SPI_BaudRatePrescaler_8; // 3 MHz obj->br_presc = SPI_BaudRatePrescaler_8; // 3 MHz
} } else if ((hz >= 6000000) && (hz < 12000000)) {
else if ((hz >= 6000000) && (hz < 12000000)) {
obj->br_presc = SPI_BaudRatePrescaler_4; // 6 MHz obj->br_presc = SPI_BaudRatePrescaler_4; // 6 MHz
} } else { // >= 12000000
else { // >= 12000000
obj->br_presc = SPI_BaudRatePrescaler_2; // 12 MHz obj->br_presc = SPI_BaudRatePrescaler_2; // 12 MHz
} }
} }