[NUCLEO_F103RB] Typo corrections (astyle)

pull/697/head
bcostm 2014-11-13 16:42:32 +01:00
parent 9d02d52994
commit 285977d068
14 changed files with 215 additions and 111 deletions

View File

@ -56,7 +56,8 @@ static const PinMap PinMap_ADC[] = {
int adc_inited = 0;
void analogin_init(analogin_t *obj, PinName pin) {
void analogin_init(analogin_t *obj, PinName pin)
{
ADC_TypeDef *adc;
ADC_InitTypeDef ADC_InitStructure;
@ -102,7 +103,8 @@ void analogin_init(analogin_t *obj, PinName pin) {
}
}
static inline uint16_t adc_read(analogin_t *obj) {
static inline uint16_t adc_read(analogin_t *obj)
{
// Get ADC registers structure address
ADC_TypeDef *adc = (ADC_TypeDef *)(obj->adc);
int channel = 0;
@ -170,14 +172,16 @@ static inline uint16_t adc_read(analogin_t *obj) {
return (ADC_GetConversionValue(adc)); // Get conversion value
}
uint16_t analogin_read_u16(analogin_t *obj) {
uint16_t analogin_read_u16(analogin_t *obj)
{
uint16_t value = adc_read(obj);
// 12-bit to 16-bit conversion
value = ((value << 4) & (uint16_t)0xFFF0) | ((value >> 8) & (uint16_t)0x000F);
return value;
}
float analogin_read(analogin_t *obj) {
float analogin_read(analogin_t *obj)
{
uint16_t value = adc_read(obj);
return (float)value * (1.0f / (float)0xFFF); // 12 bits range
}

View File

@ -34,14 +34,16 @@
extern uint32_t Set_GPIO_Clock(uint32_t port_idx);
uint32_t gpio_set(PinName pin) {
uint32_t gpio_set(PinName pin)
{
MBED_ASSERT(pin != (PinName)NC);
pin_function(pin, STM_PIN_DATA(GPIO_Mode_IN_FLOATING, 0));
return (uint32_t)(1 << ((uint32_t)pin & 0xF)); // Return the pin mask
}
void gpio_init(gpio_t *obj, PinName pin) {
void gpio_init(gpio_t *obj, PinName pin)
{
obj->pin = pin;
if (pin == (PinName)NC)
return;
@ -59,11 +61,13 @@ void gpio_init(gpio_t *obj, PinName pin) {
obj->reg_clr = &gpio->BRR;
}
void gpio_mode(gpio_t *obj, PinMode mode) {
void gpio_mode(gpio_t *obj, PinMode mode)
{
pin_mode(obj->pin, mode);
}
void gpio_dir(gpio_t *obj, PinDirection direction) {
void gpio_dir(gpio_t *obj, PinDirection direction)
{
MBED_ASSERT(obj->pin != (PinName)NC);
if (direction == PIN_OUTPUT) {
pin_function(obj->pin, STM_PIN_DATA(GPIO_Mode_Out_PP, 0));

View File

@ -46,7 +46,8 @@ static uint32_t channel_pin[CHANNEL_NUM] = {0, 0, 0, 0, 0, 0, 0};
static gpio_irq_handler irq_handler;
static void handle_interrupt_in(uint32_t irq_index) {
static void handle_interrupt_in(uint32_t irq_index)
{
// Retrieve the gpio and pin that generate the irq
GPIO_TypeDef *gpio = (GPIO_TypeDef *)(channel_gpio[irq_index]);
uint32_t pin = (uint32_t)(1 << channel_pin[irq_index]);
@ -67,31 +68,39 @@ static void handle_interrupt_in(uint32_t irq_index) {
}
// The irq_index is passed to the function
static void gpio_irq0(void) {
static void gpio_irq0(void)
{
handle_interrupt_in(0); // EXTI line 0
}
static void gpio_irq1(void) {
static void gpio_irq1(void)
{
handle_interrupt_in(1); // EXTI line 1
}
static void gpio_irq2(void) {
static void gpio_irq2(void)
{
handle_interrupt_in(2); // EXTI line 2
}
static void gpio_irq3(void) {
static void gpio_irq3(void)
{
handle_interrupt_in(3); // EXTI line 3
}
static void gpio_irq4(void) {
static void gpio_irq4(void)
{
handle_interrupt_in(4); // EXTI line 4
}
static void gpio_irq5(void) {
static void gpio_irq5(void)
{
handle_interrupt_in(5); // EXTI lines 5 to 9
}
static void gpio_irq6(void) {
static void gpio_irq6(void)
{
handle_interrupt_in(6); // EXTI lines 10 to 15
}
extern uint32_t Set_GPIO_Clock(uint32_t port_idx);
int gpio_irq_init(gpio_irq_t *obj, PinName pin, gpio_irq_handler handler, uint32_t id) {
int gpio_irq_init(gpio_irq_t *obj, PinName pin, gpio_irq_handler handler, uint32_t id)
{
IRQn_Type irq_n = (IRQn_Type)0;
uint32_t vector = 0;
uint32_t irq_index;
@ -193,7 +202,8 @@ int gpio_irq_init(gpio_irq_t *obj, PinName pin, gpio_irq_handler handler, uint32
return 0;
}
void gpio_irq_free(gpio_irq_t *obj) {
void gpio_irq_free(gpio_irq_t *obj)
{
channel_ids[obj->irq_index] = 0;
channel_gpio[obj->irq_index] = 0;
channel_pin[obj->irq_index] = 0;
@ -204,7 +214,8 @@ void gpio_irq_free(gpio_irq_t *obj) {
obj->event = EDGE_NONE;
}
void gpio_irq_set(gpio_irq_t *obj, gpio_irq_event event, uint32_t enable) {
void gpio_irq_set(gpio_irq_t *obj, gpio_irq_event event, uint32_t enable)
{
EXTI_InitTypeDef EXTI_InitStructure;
uint32_t pin_index = channel_pin[obj->irq_index];
@ -234,8 +245,7 @@ void gpio_irq_set(gpio_irq_t *obj, gpio_irq_event event, uint32_t enable) {
obj->event = EDGE_FALL;
}
}
}
else { // Disable
} else { // Disable
if (event == IRQ_RISE) {
if ((obj->event == EDGE_FALL) || (obj->event == EDGE_BOTH)) {
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
@ -255,17 +265,19 @@ void gpio_irq_set(gpio_irq_t *obj, gpio_irq_event event, uint32_t enable) {
EXTI_InitStructure.EXTI_LineCmd = DISABLE;
obj->event = EDGE_NONE;
}
}
}
}
EXTI_Init(&EXTI_InitStructure);
}
void gpio_irq_enable(gpio_irq_t *obj) {
void gpio_irq_enable(gpio_irq_t *obj)
{
NVIC_EnableIRQ(obj->irq_n);
}
void gpio_irq_disable(gpio_irq_t *obj) {
void gpio_irq_disable(gpio_irq_t *obj)
{
NVIC_DisableIRQ(obj->irq_n);
obj->event = EDGE_NONE;
}

View File

@ -48,7 +48,8 @@ typedef struct {
__IO uint32_t *reg_clr;
} gpio_t;
static inline void gpio_write(gpio_t *obj, int value) {
static inline void gpio_write(gpio_t *obj, int value)
{
MBED_ASSERT(obj->pin != (PinName)NC);
if (value) {
*obj->reg_set = obj->mask;
@ -57,7 +58,8 @@ static inline void gpio_write(gpio_t *obj, int value) {
}
}
static inline int gpio_read(gpio_t *obj) {
static inline int gpio_read(gpio_t *obj)
{
MBED_ASSERT(obj->pin != (PinName)NC);
return ((*obj->reg_in & obj->mask) ? 1 : 0);
}

View File

@ -58,7 +58,8 @@ static const PinMap PinMap_I2C_SCL[] = {
int i2c1_inited = 0;
int i2c2_inited = 0;
void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
void i2c_init(i2c_t *obj, PinName sda, PinName scl)
{
// Determine the I2C to use
I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA);
I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL);
@ -67,7 +68,7 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
MBED_ASSERT(obj->i2c != (I2CName)NC);
// Enable I2C clock and configure I2C pins if not done before
if ((obj->i2c == I2C_1)&& !i2c1_inited) {
if ((obj->i2c == I2C_1) && !i2c1_inited) {
i2c1_inited = 1;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
// Configure I2C pins
@ -76,7 +77,7 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
pinmap_pinout(sda, PinMap_I2C_SDA);
pin_mode(sda, OpenDrain);
}
if ((obj->i2c == I2C_2)&& !i2c2_inited) {
if ((obj->i2c == I2C_2) && !i2c2_inited) {
i2c2_inited = 1;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2, ENABLE);
// Configure I2C pins
@ -93,7 +94,8 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
i2c_frequency(obj, 100000); // 100 kHz per default
}
void i2c_frequency(i2c_t *obj, int hz) {
void i2c_frequency(i2c_t *obj, int hz)
{
int timeout;
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
@ -120,7 +122,8 @@ void i2c_frequency(i2c_t *obj, int hz) {
}
}
inline int i2c_start(i2c_t *obj) {
inline int i2c_start(i2c_t *obj)
{
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
int timeout;
@ -141,7 +144,8 @@ inline int i2c_start(i2c_t *obj) {
return 0;
}
inline int i2c_stop(i2c_t *obj) {
inline int i2c_stop(i2c_t *obj)
{
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
I2C_GenerateSTOP(i2c, ENABLE);
@ -149,7 +153,8 @@ inline int i2c_stop(i2c_t *obj) {
return 0;
}
int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
int i2c_read(i2c_t *obj, int address, char *data, int length, int stop)
{
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
int timeout;
int count;
@ -188,7 +193,8 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
return length;
}
int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop)
{
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
int timeout;
int count;
@ -222,7 +228,8 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
return count;
}
int i2c_byte_read(i2c_t *obj, int last) {
int i2c_byte_read(i2c_t *obj, int last)
{
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
uint8_t data;
int timeout;
@ -249,7 +256,8 @@ int i2c_byte_read(i2c_t *obj, int last) {
return (int)data;
}
int i2c_byte_write(i2c_t *obj, int data) {
int i2c_byte_write(i2c_t *obj, int data)
{
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
int timeout;
@ -258,7 +266,7 @@ int i2c_byte_write(i2c_t *obj, int data) {
// Wait until the byte is transmitted
timeout = FLAG_TIMEOUT;
while ((I2C_GetFlagStatus(i2c, I2C_FLAG_TXE) == RESET) &&
(I2C_GetFlagStatus(i2c, I2C_FLAG_BTF) == RESET)) {
(I2C_GetFlagStatus(i2c, I2C_FLAG_BTF) == RESET)) {
timeout--;
if (timeout == 0) {
return 0;
@ -268,7 +276,8 @@ int i2c_byte_write(i2c_t *obj, int data) {
return 1;
}
void i2c_reset(i2c_t *obj) {
void i2c_reset(i2c_t *obj)
{
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
int timeout;
@ -289,7 +298,8 @@ void i2c_reset(i2c_t *obj) {
#if DEVICE_I2CSLAVE
void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask) {
void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask)
{
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
uint16_t tmpreg;
@ -303,7 +313,8 @@ void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask) {
i2c->OAR1 = tmpreg;
}
void i2c_slave_mode(i2c_t *obj, int enable_slave) {
void i2c_slave_mode(i2c_t *obj, int enable_slave)
{
// Nothing to do
}
@ -313,7 +324,8 @@ void i2c_slave_mode(i2c_t *obj, int enable_slave) {
#define WriteGeneral 2 // the master is writing to all slave
#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)
{
int retValue = NoData;
uint32_t event;
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
@ -355,7 +367,8 @@ int i2c_slave_receive(i2c_t *obj) {
return (retValue);
}
int i2c_slave_read(i2c_t *obj, char *data, int length) {
int i2c_slave_read(i2c_t *obj, char *data, int length)
{
int count = 0;
// Read all bytes
@ -366,7 +379,8 @@ int i2c_slave_read(i2c_t *obj, char *data, int length) {
return count;
}
int i2c_slave_write(i2c_t *obj, const char *data, int length) {
int i2c_slave_write(i2c_t *obj, const char *data, int length)
{
int count = 0;
// Write all bytes

View File

@ -28,7 +28,8 @@
#include "cmsis.h"
// This function is called after RAM initialization and before main.
void mbed_sdk_init() {
void mbed_sdk_init()
{
// Update the SystemCoreClock variable.
SystemCoreClockUpdate();
}

View File

@ -48,7 +48,8 @@ static const uint32_t AF_mapping[AF_NUM] = {
};
// Enable GPIO clock and return GPIO base address
uint32_t Set_GPIO_Clock(uint32_t port_idx) {
uint32_t Set_GPIO_Clock(uint32_t port_idx)
{
uint32_t gpio_add = 0;
switch (port_idx) {
case PortA:
@ -77,7 +78,8 @@ uint32_t Set_GPIO_Clock(uint32_t port_idx) {
/**
* Configure pin (input, output, alternate function or analog) + output speed + AF
*/
void pin_function(PinName pin, int data) {
void pin_function(PinName pin, int data)
{
MBED_ASSERT(pin != (PinName)NC);
// Get the pin informations
uint32_t mode = STM_PIN_MODE(data);
@ -119,7 +121,8 @@ void pin_function(PinName pin, int data) {
/**
* Configure pin pull-up/pull-down
*/
void pin_mode(PinName pin, PinMode mode) {
void pin_mode(PinName pin, PinMode mode)
{
MBED_ASSERT(pin != (PinName)NC);
GPIO_InitTypeDef GPIO_InitStructure;

View File

@ -39,11 +39,13 @@ 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, ...)
// low nibble = pin number
PinName port_pin(PortName port, int pin_n) {
PinName port_pin(PortName port, int pin_n)
{
return (PinName)(pin_n + (port << 4));
}
void port_init(port_t *obj, PortName port, int mask, PinDirection dir) {
void port_init(port_t *obj, PortName port, int mask, PinDirection dir)
{
uint32_t port_index = (uint32_t)port;
// Enable GPIO clock
@ -60,7 +62,8 @@ void port_init(port_t *obj, PortName port, int mask, PinDirection dir) {
port_dir(obj, dir);
}
void port_dir(port_t *obj, PinDirection dir) {
void port_dir(port_t *obj, PinDirection dir)
{
uint32_t i;
obj->direction = dir;
for (i = 0; i < 16; i++) { // Process all pins
@ -74,7 +77,8 @@ void port_dir(port_t *obj, PinDirection dir) {
}
}
void port_mode(port_t *obj, PinMode mode) {
void port_mode(port_t *obj, PinMode mode)
{
uint32_t i;
for (i = 0; i < 16; i++) { // Process all pins
if (obj->mask & (1 << i)) { // If the pin is used
@ -83,11 +87,13 @@ void port_mode(port_t *obj, PinMode mode) {
}
}
void port_write(port_t *obj, int value) {
void port_write(port_t *obj, int value)
{
*obj->reg_out = (*obj->reg_out & ~obj->mask) | (value & obj->mask);
}
int port_read(port_t *obj) {
int port_read(port_t *obj)
{
if (obj->direction == PIN_OUTPUT) {
return (*obj->reg_out & obj->mask);
} else { // PIN_INPUT

View File

@ -73,7 +73,8 @@ static const PinMap PinMap_PWM[] = {
{NC, NC, 0}
};
void pwmout_init(pwmout_t* obj, PinName pin) {
void pwmout_init(pwmout_t* obj, PinName pin)
{
// Get the peripheral name from the pin and assign it to the object
obj->pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM);
MBED_ASSERT(obj->pwm != (PWMName)NC);
@ -94,12 +95,14 @@ void pwmout_init(pwmout_t* obj, PinName pin) {
pwmout_period_us(obj, 20000); // 20 ms per default
}
void pwmout_free(pwmout_t* obj) {
void pwmout_free(pwmout_t* obj)
{
// Configure GPIO
pin_function(obj->pin, STM_PIN_DATA(GPIO_Mode_IN_FLOATING, 0));
}
void pwmout_write(pwmout_t* obj, float value) {
void pwmout_write(pwmout_t* obj, float value)
{
TIM_TypeDef *tim = (TIM_TypeDef *)(obj->pwm);
TIM_OCInitTypeDef TIM_OCInitStructure;
@ -193,7 +196,8 @@ void pwmout_write(pwmout_t* obj, float value) {
TIM_CtrlPWMOutputs(tim, ENABLE);
}
float pwmout_read(pwmout_t* obj) {
float pwmout_read(pwmout_t* obj)
{
float value = 0;
if (obj->period > 0) {
value = (float)(obj->pulse) / (float)(obj->period);
@ -201,15 +205,18 @@ float pwmout_read(pwmout_t* obj) {
return ((value > 1.0) ? (1.0) : (value));
}
void pwmout_period(pwmout_t* obj, float seconds) {
void pwmout_period(pwmout_t* obj, float seconds)
{
pwmout_period_us(obj, seconds * 1000000.0f);
}
void pwmout_period_ms(pwmout_t* obj, int ms) {
void pwmout_period_ms(pwmout_t* obj, int ms)
{
pwmout_period_us(obj, ms * 1000);
}
void pwmout_period_us(pwmout_t* obj, int us) {
void pwmout_period_us(pwmout_t* obj, int us)
{
TIM_TypeDef *tim = (TIM_TypeDef *)(obj->pwm);
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
float dc = pwmout_read(obj);
@ -231,15 +238,18 @@ void pwmout_period_us(pwmout_t* obj, int us) {
TIM_Cmd(tim, ENABLE);
}
void pwmout_pulsewidth(pwmout_t* obj, float seconds) {
void pwmout_pulsewidth(pwmout_t* obj, float seconds)
{
pwmout_pulsewidth_us(obj, seconds * 1000000.0f);
}
void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) {
void pwmout_pulsewidth_ms(pwmout_t* obj, int ms)
{
pwmout_pulsewidth_us(obj, ms * 1000);
}
void pwmout_pulsewidth_us(pwmout_t* obj, int us) {
void pwmout_pulsewidth_us(pwmout_t* obj, int us)
{
float value = (float)us / (float)obj->period;
pwmout_write(obj, value);
}

View File

@ -37,7 +37,8 @@
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;
@ -86,7 +87,8 @@ void rtc_init(void) {
rtc_inited = 1;
}
void rtc_free(void) {
void rtc_free(void)
{
// Disable RTC, LSE and LSI clocks
PWR_BackupAccessCmd(ENABLE); // Allow access to Backup Domain
RCC_RTCCLKCmd(DISABLE);
@ -96,15 +98,18 @@ void rtc_free(void) {
rtc_inited = 0;
}
int rtc_isenabled(void) {
int rtc_isenabled(void)
{
return rtc_inited;
}
time_t rtc_read(void) {
time_t rtc_read(void)
{
return (time_t)RTC_GetCounter();
}
void rtc_write(time_t t) {
void rtc_write(time_t t)
{
RTC_WaitForLastTask(); // Wait until last write operation on RTC registers has finished
RTC_SetCounter(t); // Change the current time
RTC_WaitForLastTask(); // Wait until last write operation on RTC registers has finished

View File

@ -63,7 +63,8 @@ static uart_irq_handler irq_handler;
int stdio_uart_inited = 0;
serial_t stdio_uart;
static void init_usart(serial_t *obj) {
static void init_usart(serial_t *obj)
{
USART_TypeDef *usart = (USART_TypeDef *)(obj->uart);
USART_InitTypeDef USART_InitStructure;
@ -88,7 +89,8 @@ static void init_usart(serial_t *obj) {
USART_Cmd(usart, ENABLE);
}
void serial_init(serial_t *obj, PinName tx, PinName rx) {
void serial_init(serial_t *obj, PinName tx, PinName rx)
{
// Determine the UART to use (UART_1, UART_2, ...)
UARTName uart_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX);
UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX);
@ -133,7 +135,8 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) {
}
}
void serial_free(serial_t *obj) {
void serial_free(serial_t *obj)
{
// Reset UART and disable clock
if (obj->uart == UART_1) {
RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, ENABLE);
@ -158,12 +161,14 @@ void serial_free(serial_t *obj) {
serial_irq_ids[obj->index] = 0;
}
void serial_baud(serial_t *obj, int baudrate) {
void serial_baud(serial_t *obj, int baudrate)
{
obj->baudrate = baudrate;
init_usart(obj);
}
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 == 9) {
obj->databits = USART_WordLength_9b;
} else {
@ -198,7 +203,8 @@ void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_b
******************************************************************************/
// not api
static void uart_irq(USART_TypeDef* usart, int id) {
static void uart_irq(USART_TypeDef* usart, int id)
{
if (serial_irq_ids[id] != 0) {
if (USART_GetITStatus(usart, USART_IT_TC) != RESET) {
irq_handler(serial_irq_ids[id], TxIrq);
@ -211,22 +217,27 @@ static void uart_irq(USART_TypeDef* usart, int id) {
}
}
static void uart1_irq(void) {
static void uart1_irq(void)
{
uart_irq((USART_TypeDef*)UART_1, 0);
}
static void uart2_irq(void) {
static void uart2_irq(void)
{
uart_irq((USART_TypeDef*)UART_2, 1);
}
static void uart3_irq(void) {
static void uart3_irq(void)
{
uart_irq((USART_TypeDef*)UART_3, 2);
}
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;
serial_irq_ids[obj->index] = id;
}
void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) {
void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable)
{
IRQn_Type irq_n = (IRQn_Type)0;
uint32_t vector = 0;
USART_TypeDef *usart = (USART_TypeDef *)(obj->uart);
@ -280,19 +291,22 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) {
* READ/WRITE
******************************************************************************/
int serial_getc(serial_t *obj) {
int serial_getc(serial_t *obj)
{
USART_TypeDef *usart = (USART_TypeDef *)(obj->uart);
while (!serial_readable(obj));
return (int)(USART_ReceiveData(usart));
}
void serial_putc(serial_t *obj, int c) {
void serial_putc(serial_t *obj, int c)
{
USART_TypeDef *usart = (USART_TypeDef *)(obj->uart);
while (!serial_writable(obj));
USART_SendData(usart, (uint16_t)c);
}
int serial_readable(serial_t *obj) {
int serial_readable(serial_t *obj)
{
int status;
USART_TypeDef *usart = (USART_TypeDef *)(obj->uart);
// Check if data is received
@ -300,7 +314,8 @@ int serial_readable(serial_t *obj) {
return status;
}
int serial_writable(serial_t *obj) {
int serial_writable(serial_t *obj)
{
int status;
USART_TypeDef *usart = (USART_TypeDef *)(obj->uart);
// Check if data is transmitted
@ -308,22 +323,26 @@ int serial_writable(serial_t *obj) {
return status;
}
void serial_clear(serial_t *obj) {
void serial_clear(serial_t *obj)
{
USART_TypeDef *usart = (USART_TypeDef *)(obj->uart);
USART_ClearFlag(usart, USART_FLAG_TXE);
USART_ClearFlag(usart, USART_FLAG_RXNE);
}
void serial_pinout_tx(PinName tx) {
void serial_pinout_tx(PinName tx)
{
pinmap_pinout(tx, PinMap_UART_TX);
}
void serial_break_set(serial_t *obj) {
void serial_break_set(serial_t *obj)
{
USART_TypeDef *usart = (USART_TypeDef *)(obj->uart);
USART_SendBreak(usart);
}
void serial_break_clear(serial_t *obj) {
void serial_break_clear(serial_t *obj)
{
}
#endif

View File

@ -33,7 +33,8 @@
#include "cmsis.h"
void sleep(void) {
void sleep(void)
{
// Disable us_ticker update interrupt
TIM_ITConfig(TIM1, TIM_IT_Update, DISABLE);
@ -44,7 +45,8 @@ void sleep(void) {
TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE);
}
void deepsleep(void) {
void deepsleep(void)
{
// Disable us_ticker update interrupt
TIM_ITConfig(TIM1, TIM_IT_Update, DISABLE);

View File

@ -64,7 +64,8 @@ static const PinMap PinMap_SPI_SSEL[] = {
{NC, NC, 0}
};
static void init_spi(spi_t *obj) {
static void init_spi(spi_t *obj)
{
SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
SPI_InitTypeDef SPI_InitStructure;
@ -84,7 +85,8 @@ static void init_spi(spi_t *obj) {
SPI_Cmd(spi, ENABLE);
}
void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel) {
void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel)
{
// Determine the SPI to use
SPIName spi_mosi = (SPIName)pinmap_peripheral(mosi, PinMap_SPI_MOSI);
SPIName spi_miso = (SPIName)pinmap_peripheral(miso, PinMap_SPI_MISO);
@ -133,7 +135,8 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel
init_spi(obj);
}
void spi_free(spi_t *obj) {
void spi_free(spi_t *obj)
{
// Reset SPI and disable clock
if (obj->spi == SPI_1) {
RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1, ENABLE);
@ -154,7 +157,8 @@ void spi_free(spi_t *obj) {
pin_function(obj->pin_ssel, STM_PIN_DATA(GPIO_Mode_IN_FLOATING, 0));
}
void spi_format(spi_t *obj, int bits, int mode, int slave) {
void spi_format(spi_t *obj, int bits, int mode, int slave)
{
// Save new values
if (bits == 16) {
obj->bits = SPI_DataSize_16b;
@ -192,7 +196,8 @@ void spi_format(spi_t *obj, int bits, int mode, int slave) {
init_spi(obj);
}
void spi_frequency(spi_t *obj, int hz) {
void spi_frequency(spi_t *obj, int hz)
{
if (obj->spi == SPI_1) {
// Values depend of PCLK2: 64 MHz if HSI is used, 72 MHz if HSE is used
if (hz < 500000) {
@ -238,7 +243,8 @@ void spi_frequency(spi_t *obj, int hz) {
init_spi(obj);
}
static inline int ssp_readable(spi_t *obj) {
static inline int ssp_readable(spi_t *obj)
{
int status;
SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
// Check if data is received
@ -246,7 +252,8 @@ static inline int ssp_readable(spi_t *obj) {
return status;
}
static inline int ssp_writeable(spi_t *obj) {
static inline int ssp_writeable(spi_t *obj)
{
int status;
SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
// Check if data is transmitted
@ -254,46 +261,54 @@ static inline int ssp_writeable(spi_t *obj) {
return status;
}
static inline void ssp_write(spi_t *obj, int value) {
static inline void ssp_write(spi_t *obj, int value)
{
SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
while (!ssp_writeable(obj));
SPI_I2S_SendData(spi, (uint16_t)value);
}
static inline int ssp_read(spi_t *obj) {
static inline int ssp_read(spi_t *obj)
{
SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
while (!ssp_readable(obj));
return (int)SPI_I2S_ReceiveData(spi);
}
static inline int ssp_busy(spi_t *obj) {
static inline int ssp_busy(spi_t *obj)
{
int status;
SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
status = ((SPI_I2S_GetFlagStatus(spi, SPI_I2S_FLAG_BSY) != RESET) ? 1 : 0);
return status;
}
int spi_master_write(spi_t *obj, int value) {
int spi_master_write(spi_t *obj, int value)
{
ssp_write(obj, value);
return ssp_read(obj);
}
int spi_slave_receive(spi_t *obj) {
int spi_slave_receive(spi_t *obj)
{
return ((ssp_readable(obj) && !ssp_busy(obj)) ? 1 : 0);
};
int spi_slave_read(spi_t *obj) {
int spi_slave_read(spi_t *obj)
{
SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
return (int)SPI_I2S_ReceiveData(spi);
}
void spi_slave_write(spi_t *obj, int value) {
void spi_slave_write(spi_t *obj, int value)
{
SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
while (!ssp_writeable(obj));
SPI_I2S_SendData(spi, (uint16_t)value);
}
int spi_busy(spi_t *obj) {
int spi_busy(spi_t *obj)
{
return ssp_busy(obj);
}

View File

@ -39,14 +39,16 @@ static volatile uint32_t SlaveCounter = 0;
static volatile uint32_t oc_int_part = 0;
static volatile uint16_t oc_rem_part = 0;
void set_compare(uint16_t count) {
void set_compare(uint16_t count)
{
// Set new output compare value
TIM_SetCompare1(TIM_MST, count);
// Enable IT
TIM_ITConfig(TIM_MST, TIM_IT_CC1, ENABLE);
}
static void tim_irq_handler(void) {
static void tim_irq_handler(void)
{
uint16_t cval = TIM_MST->CNT;
if (TIM_GetITStatus(TIM_MST, TIM_IT_Update) == SET) {
@ -71,7 +73,8 @@ static void tim_irq_handler(void) {
}
}
void us_ticker_init(void) {
void us_ticker_init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
if (us_ticker_inited) return;
@ -100,7 +103,8 @@ void us_ticker_init(void) {
TIM_Cmd(TIM_MST, ENABLE);
}
uint32_t us_ticker_read() {
uint32_t us_ticker_read()
{
uint32_t counter, counter2;
if (!us_ticker_inited) us_ticker_init();
// A situation might appear when Master overflows right after Slave is read and before the
@ -121,7 +125,8 @@ uint32_t us_ticker_read() {
return counter2;
}
void us_ticker_set_interrupt(timestamp_t timestamp) {
void us_ticker_set_interrupt(timestamp_t timestamp)
{
int delta = (int)((uint32_t)timestamp - us_ticker_read());
uint16_t cval = TIM_MST->CNT;
@ -140,10 +145,12 @@ void us_ticker_set_interrupt(timestamp_t timestamp) {
}
}
void us_ticker_disable_interrupt(void) {
void us_ticker_disable_interrupt(void)
{
TIM_ITConfig(TIM_MST, TIM_IT_CC1, DISABLE);
}
void us_ticker_clear_interrupt(void) {
void us_ticker_clear_interrupt(void)
{
TIM_ClearITPendingBit(TIM_MST, TIM_IT_CC1);
}