mbed lib with startup delay fixed for Nucleo401RE
Fork of mbed-src by
Revision 227:7bd0639b8911, committed 2014-06-11
- Comitter:
- mbed_official
- Date:
- Wed Jun 11 16:00:09 2014 +0100
- Parent:
- 226:b062af740e40
- Child:
- 228:85a676113daa
- Commit message:
- Synchronized with git revision d58d532ebc0e0a96f4fffb8edefc082b71b964af
Full URL: https://github.com/mbedmicro/mbed/commit/d58d532ebc0e0a96f4fffb8edefc082b71b964af/
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/api/mbed_assert.h Wed Jun 11 16:00:09 2014 +0100 @@ -0,0 +1,50 @@ +/* mbed Microcontroller Library + * Copyright (c) 2006-2013 ARM Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef MBED_ASSERT_H +#define MBED_ASSERT_H + +#ifdef __cplusplus +extern "C" { +#endif + +/** Internal mbed assert function which is invoked when MBED_ASSERT macro failes. + * This function is active only if NDEBUG is not defined prior to including this + * assert header file. + * In case of MBED_ASSERT failing condition, the assertation message is printed + * to stderr and mbed_die() is called. + * @param expr Expresion to be checked. + * @param file File where assertation failed. + * @param line Failing assertation line number. + */ +void mbed_assert_internal(const char *expr, const char *file, int line); + +#ifdef __cplusplus +} +#endif + +#ifdef NDEBUG +#define MBED_ASSERT(expr) ((void)0) + +#else +#define MBED_ASSERT(expr) \ +do { \ + if (!(expr)) { \ + mbed_assert_internal(#expr, __FILE__, __LINE__); \ + } \ +} while (0) +#endif + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/common/assert.c Wed Jun 11 16:00:09 2014 +0100 @@ -0,0 +1,32 @@ +/* mbed Microcontroller Library + * Copyright (c) 2006-2013 ARM Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "mbed_assert.h" +#include "device.h" + +#if DEVICE_STDIO_MESSAGES +#include <stdio.h> +#endif + +#include <stdlib.h> +#include "mbed_interface.h" + +void mbed_assert_internal(const char *expr, const char *file, int line) +{ +#if DEVICE_STDIO_MESSAGES + fprintf(stderr, "mbed assertation failed: %s, file: %s, line %d \n", expr, file, line); +#endif + mbed_die(); +}
--- a/common/pinmap_common.c Wed Jun 11 09:45:09 2014 +0100 +++ b/common/pinmap_common.c Wed Jun 11 16:00:09 2014 +0100 @@ -17,7 +17,8 @@ #include "error.h" void pinmap_pinout(PinName pin, const PinMap *map) { - if (pin == NC) return; + if (pin == NC) + return; while (map->pin != NC) { if (map->pin == pin) { @@ -33,11 +34,14 @@ uint32_t pinmap_merge(uint32_t a, uint32_t b) { // both are the same (inc both NC) - if (a == b) return a; + if (a == b) + return a; // one (or both) is not connected - if (a == (uint32_t)NC) return b; - if (b == (uint32_t)NC) return a; + if (a == (uint32_t)NC) + return b; + if (b == (uint32_t)NC) + return a; // mis-match error case error("pinmap mis-match");
--- a/targets/hal/TARGET_Freescale/TARGET_K20D5M/analogin_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_Freescale/TARGET_K20D5M/analogin_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,11 +13,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "mbed_assert.h" #include "analogin_api.h" #include "cmsis.h" #include "pinmap.h" -#include "error.h" #include "clk_freqs.h" static const PinMap PinMap_ADC[] = { @@ -38,8 +38,7 @@ void analogin_init(analogin_t *obj, PinName pin) { obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC); - if (obj->adc == (ADCName)NC) - error("ADC pin mapping failed"); + MBED_ASSERT(obj->adc != (ADCName)NC); SIM->SCGC6 |= SIM_SCGC6_ADC0_MASK;
--- a/targets/hal/TARGET_Freescale/TARGET_K20D5M/gpio_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_Freescale/TARGET_K20D5M/gpio_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,19 +13,21 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "mbed_assert.h" #include "gpio_api.h" #include "pinmap.h" uint32_t gpio_set(PinName pin) { + MBED_ASSERT(pin != (PinName)NC); pin_function(pin, 1); return 1 << ((pin & 0x7F) >> 2); } void gpio_init(gpio_t *obj, PinName pin) { - if(pin == NC) + obj->pin = pin; + if (pin == (PinName)NC) return; - obj->pin = pin; obj->mask = gpio_set(pin); unsigned int port = (unsigned int)pin >> PORT_SHIFT; @@ -42,6 +44,7 @@ } void gpio_dir(gpio_t *obj, PinDirection direction) { + MBED_ASSERT(obj->pin != (PinName)NC); switch (direction) { case PIN_INPUT : *obj->reg_dir &= ~obj->mask;
--- a/targets/hal/TARGET_Freescale/TARGET_K20D5M/gpio_object.h Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_Freescale/TARGET_K20D5M/gpio_object.h Wed Jun 11 16:00:09 2014 +0100 @@ -16,6 +16,8 @@ #ifndef MBED_GPIO_OBJECT_H #define MBED_GPIO_OBJECT_H +#include "mbed_assert.h" + #ifdef __cplusplus extern "C" { #endif @@ -31,6 +33,7 @@ } gpio_t; static inline void gpio_write(gpio_t *obj, int value) { + MBED_ASSERT(obj->pin != (PinName)NC); if (value) { *obj->reg_set = obj->mask; } else { @@ -39,6 +42,7 @@ } static inline int gpio_read(gpio_t *obj) { + MBED_ASSERT(obj->pin != (PinName)NC); return ((*obj->reg_in & obj->mask) ? 1 : 0); }
--- a/targets/hal/TARGET_Freescale/TARGET_K20D5M/i2c_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_Freescale/TARGET_K20D5M/i2c_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,11 +13,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "mbed_assert.h" #include "i2c_api.h" #include "cmsis.h" #include "pinmap.h" -#include "error.h" #include "clk_freqs.h" static const PinMap PinMap_I2C_SDA[] = { @@ -54,8 +54,7 @@ I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA); I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL); obj->i2c = (I2C_Type*)pinmap_merge(i2c_sda, i2c_scl); - if ((int)obj->i2c == NC) - error("I2C pin mapping failed"); + MBED_ASSERT((int)obj->i2c != NC); SIM->SCGC4 |= SIM_SCGC4_I2C0_MASK; SIM->SCGC5 |= SIM_SCGC5_PORTB_MASK;
--- a/targets/hal/TARGET_Freescale/TARGET_K20D5M/pinmap.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_Freescale/TARGET_K20D5M/pinmap.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,12 +13,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "mbed_assert.h" #include "pinmap.h" -#include "error.h" void pin_function(PinName pin, int function) { - if (pin == (PinName)NC) - return; + MBED_ASSERT(pin != (PinName)NC); uint32_t port_n = (uint32_t)pin >> PORT_SHIFT; uint32_t pin_n = (uint32_t)(pin & 0x7C) >> 2; @@ -31,8 +30,7 @@ } void pin_mode(PinName pin, PinMode mode) { - if (pin == (PinName)NC) { return; } - + MBED_ASSERT(pin != (PinName)NC); __IO uint32_t* pin_pcr = (__IO uint32_t*)(PORTA_BASE + pin); // pin pullup bits: [1:0] -> 11 = (0x3)
--- a/targets/hal/TARGET_Freescale/TARGET_K20D5M/pwmout_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_Freescale/TARGET_K20D5M/pwmout_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,11 +13,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "mbed_assert.h" #include "pwmout_api.h" #include "cmsis.h" #include "pinmap.h" -#include "error.h" static const PinMap PinMap_PWM[] = { // LEDs @@ -53,8 +53,7 @@ void pwmout_init(pwmout_t* obj, PinName pin) { // determine the channel PWMName pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM); - if (pwm == (PWMName)NC) - error("PwmOut pin mapping failed"); + MBED_ASSERT(pwm != (PWMName)NC); uint32_t clkdiv = 0; float clkval = SystemCoreClock / 1000000.0f;
--- a/targets/hal/TARGET_Freescale/TARGET_K20D5M/serial_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_Freescale/TARGET_K20D5M/serial_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "mbed_assert.h" #include "serial_api.h" // math.h required for floating point operations for baud rate calculation @@ -22,7 +23,6 @@ #include "cmsis.h" #include "pinmap.h" -#include "error.h" static const PinMap PinMap_UART_TX[] = { {PTB17, UART_0, 3}, @@ -47,8 +47,7 @@ UARTName uart_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX); UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX); UARTName uart = (UARTName)pinmap_merge(uart_tx, uart_rx); - if ((int)uart == NC) - error("Serial pinout mapping failed"); + MBED_ASSERT((int)uart != NC); obj->uart = (UART_Type *)uart; // enable clk @@ -117,6 +116,9 @@ } void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits) { + MBED_ASSERT((stop_bits == 1) || (stop_bits == 2)); + MBED_ASSERT((parity == ParityNone) || (parity == ParityOdd) || (parity == ParityEven)); + MBED_ASSERT((data_bits == 8) || (data_bits == 9)); // save C2 state uint32_t c2_state = (obj->uart->C2 & (UART_C2_RE_MASK | UART_C2_TE_MASK)); @@ -125,9 +127,6 @@ obj->uart->C2 &= ~(UART_C2_RE_MASK | UART_C2_TE_MASK); // 8 data bits = 0 ... 9 data bits = 1 - if ((data_bits < 8) || (data_bits > 9)) - error("Invalid number of bits (%d) in serial format, should be 8..9", data_bits); - data_bits -= 8; uint32_t parity_enable, parity_select; @@ -136,22 +135,16 @@ case ParityOdd : parity_enable = 1; parity_select = 1; data_bits++; break; case ParityEven: parity_enable = 1; parity_select = 0; data_bits++; break; default: - error("Invalid serial parity setting"); - return; + break; } - // 1 stop bits = 0, 2 stop bits = 1 - if ((stop_bits != 1) && (stop_bits != 2)) - error("Invalid stop bits specified"); stop_bits -= 1; uint32_t m10 = 0; - // 9 data bits + parity + // 9 data bits + parity - only uart0 support if (data_bits == 2) { - // only uart0 supports 10 bit communication - if (obj->index != 0) - error("Invalid number of bits (9) to be used with parity"); + MBED_ASSERT(obj->index == 0); data_bits = 0; m10 = 1; }
--- a/targets/hal/TARGET_Freescale/TARGET_K20D5M/spi_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_Freescale/TARGET_K20D5M/spi_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,13 +13,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "mbed_assert.h" #include "spi_api.h" #include <math.h> #include "cmsis.h" #include "pinmap.h" -#include "error.h" #include "clk_freqs.h" static const PinMap PinMap_SPI_SCLK[] = { @@ -56,9 +56,7 @@ SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel); obj->spi = (SPI_Type*)pinmap_merge(spi_data, spi_cntl); - if ((int)obj->spi == NC) { - error("SPI pinout mapping failed"); - } + MBED_ASSERT((int)obj->spi != NC); SIM->SCGC5 |= SIM_SCGC5_PORTC_MASK | SIM_SCGC5_PORTD_MASK; SIM->SCGC6 |= SIM_SCGC6_SPI0_MASK; @@ -90,13 +88,8 @@ // [TODO] } void spi_format(spi_t *obj, int bits, int mode, int slave) { - if ((bits < 4) || (bits > 16)) - error("SPI: Only frames between 4 and 16-bit supported"); - - - if ((mode < 0) || (mode > 3)) { - error("SPI mode unsupported"); - } + MBED_ASSERT((bits > 4) || (bits < 16)); + MBED_ASSERT((mode >= 0) && (mode <= 3)); uint32_t polarity = (mode & 0x2) ? 1 : 0; uint32_t phase = (mode & 0x1) ? 1 : 0;
--- a/targets/hal/TARGET_Freescale/TARGET_KLXX/TARGET_KL05Z/spi_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_Freescale/TARGET_KLXX/TARGET_KL05Z/spi_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -19,7 +19,6 @@ #include "cmsis.h" #include "pinmap.h" -#include "error.h" static const PinMap PinMap_SPI_SCLK[] = { {PTB0, SPI_0, 3}, @@ -51,9 +50,7 @@ SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel); obj->spi = (SPI_Type*)pinmap_merge(spi_data, spi_cntl); - if ((int)obj->spi == NC) { - error("SPI pinout mapping failed"); - } + MBED_ASSERT((int)obj->spi != NC); // enable power and clocking switch ((int)obj->spi) { @@ -87,13 +84,8 @@ // [TODO] } void spi_format(spi_t *obj, int bits, int mode, int slave) { - if (bits != 8) { - error("Only 8bits SPI supported"); - } - - if ((mode < 0) || (mode > 3)) { - error("SPI mode unsupported"); - } + MBED_ASSERT(bits == 8); + MBED_ASSERT((mode >= 0) && (mode <= 3)); uint8_t polarity = (mode & 0x2) ? 1 : 0; uint8_t phase = (mode & 0x1) ? 1 : 0;
--- a/targets/hal/TARGET_Freescale/TARGET_KLXX/TARGET_KL25Z/spi_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_Freescale/TARGET_KLXX/TARGET_KL25Z/spi_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -19,7 +19,6 @@ #include "cmsis.h" #include "pinmap.h" -#include "error.h" #include "clk_freqs.h" #include "PeripheralPins.h" @@ -33,9 +32,7 @@ SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel); obj->spi = (SPI_Type*)pinmap_merge(spi_data, spi_cntl); - if ((int)obj->spi == NC) { - error("SPI pinout mapping failed"); - } + MBED_ASSERT((int)obj->spi != NC); // enable power and clocking switch ((int)obj->spi) { @@ -67,13 +64,8 @@ // [TODO] } void spi_format(spi_t *obj, int bits, int mode, int slave) { - if (bits != 8) { - error("Only 8bits SPI supported"); - } - - if ((mode < 0) || (mode > 3)) { - error("SPI mode unsupported"); - } + MBED_ASSERT(bits == 8); + MBED_ASSERT((mode >= 0) && (mode <= 3)); uint8_t polarity = (mode & 0x2) ? 1 : 0; uint8_t phase = (mode & 0x1) ? 1 : 0;
--- a/targets/hal/TARGET_Freescale/TARGET_KLXX/TARGET_KL46Z/spi_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_Freescale/TARGET_KLXX/TARGET_KL46Z/spi_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,13 +13,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "mbed_assert.h" #include "spi_api.h" #include <math.h> #include "cmsis.h" #include "pinmap.h" -#include "error.h" static const PinMap PinMap_SPI_SCLK[] = { {PTA15, SPI_0, 2}, @@ -90,9 +90,7 @@ SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel); obj->spi = (SPI_Type*)pinmap_merge(spi_data, spi_cntl); - if ((int)obj->spi == NC) { - error("SPI pinout mapping failed"); - } + MBED_ASSERT((int)obj->spi != NC); // enable power and clocking switch ((int)obj->spi) { @@ -125,13 +123,8 @@ // [TODO] } void spi_format(spi_t *obj, int bits, int mode, int slave) { - if ((bits != 8) && (bits != 16)) { - error("Only 8/16 bits SPI supported"); - } - - if ((mode < 0) || (mode > 3)) { - error("SPI mode unsupported"); - } + MBED_ASSERT((bits == 8) || (bits == 16)); + MBED_ASSERT((mode >= 0) && (mode <= 3)); uint8_t polarity = (mode & 0x2) ? 1 : 0; uint8_t phase = (mode & 0x1) ? 1 : 0;
--- a/targets/hal/TARGET_Freescale/TARGET_KLXX/analogin_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_Freescale/TARGET_KLXX/analogin_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,11 +13,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "mbed_assert.h" #include "analogin_api.h" #include "cmsis.h" #include "pinmap.h" -#include "error.h" #include "clk_freqs.h" #include "PeripheralPins.h" @@ -27,9 +27,7 @@ void analogin_init(analogin_t *obj, PinName pin) { obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC); - if (obj->adc == (ADCName)NC) { - error("ADC pin mapping failed"); - } + MBED_ASSERT(obj->adc != (ADCName)NC); SIM->SCGC6 |= SIM_SCGC6_ADC0_MASK;
--- a/targets/hal/TARGET_Freescale/TARGET_KLXX/analogout_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_Freescale/TARGET_KLXX/analogout_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,21 +13,18 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "mbed_assert.h" #include "analogout_api.h" #include "cmsis.h" #include "pinmap.h" -#include "error.h" #include "PeripheralPins.h" #define RANGE_12BIT 0xFFF - void analogout_init(dac_t *obj, PinName pin) { obj->dac = (DACName)pinmap_peripheral(pin, PinMap_DAC); - if (obj->dac == (DACName)NC) { - error("DAC pin mapping failed"); - } + MBED_ASSERT(obj->dac != (DACName)NC); SIM->SCGC6 |= SIM_SCGC6_DAC0_MASK;
--- a/targets/hal/TARGET_Freescale/TARGET_KLXX/gpio_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_Freescale/TARGET_KLXX/gpio_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,18 +13,21 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "mbed_assert.h" #include "gpio_api.h" #include "pinmap.h" uint32_t gpio_set(PinName pin) { + MBED_ASSERT(pin != (PinName)NC); pin_function(pin, 1); return 1 << ((pin & 0x7F) >> 2); } void gpio_init(gpio_t *obj, PinName pin) { - if(pin == (PinName)NC) return; + obj->pin = pin; + if (pin == (PinName)NC) + return; - obj->pin = pin; obj->mask = gpio_set(pin); unsigned int port = (unsigned int)pin >> PORT_SHIFT; @@ -41,8 +44,13 @@ } void gpio_dir(gpio_t *obj, PinDirection direction) { + MBED_ASSERT(obj->pin != (PinName)NC); switch (direction) { - case PIN_INPUT : *obj->reg_dir &= ~obj->mask; break; - case PIN_OUTPUT: *obj->reg_dir |= obj->mask; break; + case PIN_INPUT : + *obj->reg_dir &= ~obj->mask; + break; + case PIN_OUTPUT: + *obj->reg_dir |= obj->mask; + break; } }
--- a/targets/hal/TARGET_Freescale/TARGET_KLXX/gpio_object.h Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_Freescale/TARGET_KLXX/gpio_object.h Wed Jun 11 16:00:09 2014 +0100 @@ -16,6 +16,8 @@ #ifndef MBED_GPIO_OBJECT_H #define MBED_GPIO_OBJECT_H +#include "mbed_assert.h" + #ifdef __cplusplus extern "C" { #endif @@ -31,6 +33,7 @@ } gpio_t; static inline void gpio_write(gpio_t *obj, int value) { + MBED_ASSERT(obj->pin != (PinName)NC); if (value) *obj->reg_set = obj->mask; else @@ -38,6 +41,7 @@ } static inline int gpio_read(gpio_t *obj) { + MBED_ASSERT(obj->pin != (PinName)NC); return ((*obj->reg_in & obj->mask) ? 1 : 0); }
--- a/targets/hal/TARGET_Freescale/TARGET_KLXX/i2c_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_Freescale/TARGET_KLXX/i2c_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,11 +13,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "mbed_assert.h" #include "i2c_api.h" #include "cmsis.h" #include "pinmap.h" -#include "error.h" #include "clk_freqs.h" #include "PeripheralPins.h" @@ -43,9 +43,7 @@ I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA); I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL); obj->i2c = (I2C_Type*)pinmap_merge(i2c_sda, i2c_scl); - if ((int)obj->i2c == NC) { - error("I2C pin mapping failed"); - } + MBED_ASSERT((int)obj->i2c != NC); // enable power switch ((int)obj->i2c) {
--- a/targets/hal/TARGET_Freescale/TARGET_KLXX/pinmap.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_Freescale/TARGET_KLXX/pinmap.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,11 +13,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "mbed_assert.h" #include "pinmap.h" -#include "error.h" void pin_function(PinName pin, int function) { - if (pin == (PinName)NC) return; + MBED_ASSERT(pin != (PinName)NC); uint32_t port_n = (uint32_t)pin >> PORT_SHIFT; uint32_t pin_n = (uint32_t)(pin & 0x7C) >> 2; @@ -30,7 +30,7 @@ } void pin_mode(PinName pin, PinMode mode) { - if (pin == (PinName)NC) { return; } + MBED_ASSERT(pin != (PinName)NC); __IO uint32_t* pin_pcr = (__IO uint32_t*)(PORTA_BASE + pin);
--- a/targets/hal/TARGET_Freescale/TARGET_KLXX/pwmout_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_Freescale/TARGET_KLXX/pwmout_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,11 +13,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "mbed_assert.h" #include "pwmout_api.h" #include "cmsis.h" #include "pinmap.h" -#include "error.h" #include "clk_freqs.h" #include "PeripheralPins.h" @@ -26,9 +26,8 @@ void pwmout_init(pwmout_t* obj, PinName pin) { // determine the channel PWMName pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM); - if (pwm == (PWMName)NC) - error("PwmOut pin mapping failed"); - + MBED_ASSERT(pwm != (PWMName)NC); + uint32_t clkdiv = 0; float clkval; if (mcgpllfll_frequency()) { @@ -37,11 +36,11 @@ } else { SIM->SOPT2 |= SIM_SOPT2_TPMSRC(2); // Clock source: ExtOsc clkval = extosc_frequency() / 1000000.0f; - } + } while (clkval > 1) { clkdiv++; - clkval /= 2.0; + clkval /= 2.0; if (clkdiv == 7) break; }
--- a/targets/hal/TARGET_Freescale/TARGET_KLXX/serial_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_Freescale/TARGET_KLXX/serial_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "mbed_assert.h" #include "serial_api.h" // math.h required for floating point operations for baud rate calculation @@ -22,7 +23,6 @@ #include "cmsis.h" #include "pinmap.h" -#include "error.h" #include "clk_freqs.h" #include "PeripheralPins.h" @@ -61,9 +61,7 @@ UARTName uart_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX); UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX); UARTName uart = (UARTName)pinmap_merge(uart_tx, uart_rx); - if ((int)uart == NC) { - error("Serial pinout mapping failed"); - } + MBED_ASSERT((int)uart != NC); obj->uart = (UARTLP_Type *)uart; // enable clk @@ -150,17 +148,16 @@ } void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits) { - + MBED_ASSERT((stop_bits == 1) || (stop_bits == 2)); + MBED_ASSERT((parity == ParityNone) || (parity == ParityOdd) || (parity == ParityEven)); + MBED_ASSERT(data_bits == 8); // TODO: Support other number of data bits (also in the write method!) + // save C2 state uint8_t c2_state = (obj->uart->C2 & (UARTLP_C2_RE_MASK | UARTLP_C2_TE_MASK)); // Disable UART before changing registers obj->uart->C2 &= ~(UARTLP_C2_RE_MASK | UARTLP_C2_TE_MASK); - // TODO: Support other number of data bits (also in the write method!) - if ((data_bits < 8) || (data_bits > 8)) { - error("Invalid number of bits (%d) in serial format, should be 8", data_bits); - } uint8_t parity_enable, parity_select; switch (parity) { @@ -168,14 +165,9 @@ case ParityOdd : parity_enable = 1; parity_select = 1; data_bits++; break; case ParityEven: parity_enable = 1; parity_select = 0; data_bits++; break; default: - error("Invalid serial parity setting"); - return; + break; } - // 1 stop bits = 0, 2 stop bits = 1 - if ((stop_bits != 1) && (stop_bits != 2)) { - error("Invalid stop bits specified"); - } stop_bits -= 1; // data bits, parity and parity mode @@ -290,7 +282,7 @@ } void serial_break_set(serial_t *obj) { - obj->uart->C2 |= UARTLP_C2_SBK_MASK; + obj->uart->C2 |= UARTLP_C2_SBK_MASK; } void serial_break_clear(serial_t *obj) {
--- a/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_K64F/analogin_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_K64F/analogin_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,11 +13,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "mbed_assert.h" #include "analogin_api.h" #include "cmsis.h" #include "pinmap.h" -#include "error.h" #include "PeripheralNames.h" #include "fsl_adc_hal.h" #include "fsl_clock_manager.h" @@ -49,9 +49,8 @@ void analogin_init(analogin_t *obj, PinName pin) { obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC); - if (obj->adc == (ADCName)NC) { - error("ADC pin mapping failed"); - } + MBED_ASSERT(obj->adc != (ADCName)NC); + uint32_t instance = obj->adc >> ADC_INSTANCE_SHIFT; clock_manager_set_gate(kClockModuleADC, instance, true);
--- a/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_K64F/gpio_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_K64F/gpio_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "mbed_assert.h" #include "gpio_api.h" #include "pinmap.h" #include "fsl_port_hal.h" @@ -20,6 +21,7 @@ #include "fsl_sim_hal.h" uint32_t gpio_set(PinName pin) { + MBED_ASSERT(pin != (PinName)NC); uint32_t pin_num = pin & 0xFF; pin_function(pin, (int)kPortMuxAsGpio); @@ -27,11 +29,10 @@ } void gpio_init(gpio_t *obj, PinName pin) { - if (pin == NC) { + obj->pinName = pin; + if (pin == (PinName)NC) return; - } - obj->pinName = pin; uint32_t port = pin >> GPIO_PORT_SHIFT; uint32_t pin_num = pin & 0xFF; clock_hal_set_gate(kSimClockModulePORT, port, true); @@ -43,6 +44,7 @@ } void gpio_dir(gpio_t *obj, PinDirection direction) { + MBED_ASSERT(obj->pinName != (PinName)NC); uint32_t port = obj->pinName >> GPIO_PORT_SHIFT; uint32_t pin_num = obj->pinName & 0xFF;
--- a/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_K64F/gpio_object.h Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_K64F/gpio_object.h Wed Jun 11 16:00:09 2014 +0100 @@ -16,6 +16,7 @@ #ifndef MBED_GPIO_OBJECT_H #define MBED_GPIO_OBJECT_H +#include "mbed_assert.h" #include "fsl_gpio_hal.h" #ifdef __cplusplus @@ -27,6 +28,7 @@ } gpio_t; static inline void gpio_write(gpio_t *obj, int value) { + MBED_ASSERT(obj->pinName != (PinName)NC); uint32_t port = obj->pinName >> GPIO_PORT_SHIFT; uint32_t pin = obj->pinName & 0xFF; @@ -34,6 +36,7 @@ } static inline int gpio_read(gpio_t *obj) { + MBED_ASSERT(obj->pinName != (PinName)NC); uint32_t port = obj->pinName >> GPIO_PORT_SHIFT; uint32_t pin = obj->pinName & 0xFF;
--- a/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_K64F/i2c_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_K64F/i2c_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,11 +13,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "mbed_assert.h" #include "i2c_api.h" #include "cmsis.h" #include "pinmap.h" -#include "error.h" #include "fsl_clock_manager.h" #include "fsl_i2c_hal.h" #include "fsl_port_hal.h" @@ -50,9 +50,7 @@ uint32_t i2c_sda = pinmap_peripheral(sda, PinMap_I2C_SDA); uint32_t i2c_scl = pinmap_peripheral(scl, PinMap_I2C_SCL); obj->instance = pinmap_merge(i2c_sda, i2c_scl); - if ((int)obj->instance == NC) { - error("I2C pin mapping failed"); - } + MBED_ASSERT((int)obj->instance != NC); clock_manager_set_gate(kClockModuleI2C, obj->instance, true); clock_manager_set_gate(kClockModulePORT, sda >> GPIO_PORT_SHIFT, true);
--- a/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_K64F/pinmap.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_K64F/pinmap.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,25 +13,20 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "mbed_assert.h" #include "pinmap.h" #include "error.h" #include "fsl_clock_manager.h" #include "fsl_port_hal.h" void pin_function(PinName pin, int function) { - if (pin == (PinName)NC) { - return; - } - + MBED_ASSERT(pin != (PinName)NC); clock_manager_set_gate(kClockModulePORT, pin >> GPIO_PORT_SHIFT, true); port_hal_mux_control(pin >> GPIO_PORT_SHIFT, pin & 0xFF, (port_mux_t)function); } void pin_mode(PinName pin, PinMode mode) { - if (pin == (PinName)NC) { - return; - } - + MBED_ASSERT(pin != (PinName)NC); uint32_t instance = pin >> GPIO_PORT_SHIFT; uint32_t pinName = pin & 0xFF;
--- a/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_K64F/pwmout_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_K64F/pwmout_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,11 +13,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "mbed_assert.h" #include "pwmout_api.h" #include "cmsis.h" #include "pinmap.h" -#include "error.h" #include "fsl_ftm_hal.h" #include "fsl_mcg_hal.h" #include "fsl_clock_manager.h" @@ -73,9 +73,8 @@ void pwmout_init(pwmout_t* obj, PinName pin) { PWMName pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM); - if (pwm == (PWMName)NC) { - error("PwmOut pin mapping failed"); - } + MBED_ASSERT(pwm != (PWMName)NC); + obj->pwm_name = pwm; uint32_t pwm_base_clock;
--- a/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_K64F/serial_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_K64F/serial_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -17,12 +17,12 @@ // math.h required for floating point operations for baud rate calculation #include <math.h> +#include "mbed_assert.h" #include <string.h> #include "cmsis.h" #include "pinmap.h" -#include "error.h" #include "fsl_uart_hal.h" #include "fsl_clock_manager.h" #include "fsl_uart_features.h" @@ -85,9 +85,7 @@ uint32_t uart_tx = pinmap_peripheral(tx, PinMap_UART_TX); uint32_t uart_rx = pinmap_peripheral(rx, PinMap_UART_RX); obj->index = (UARTName)pinmap_merge(uart_tx, uart_rx); - if ((int)obj->index == NC) { - error("Serial pinout mapping failed"); - } + MBED_ASSERT((int)obj->index != NC); uart_config_t uart_config; uart_config.baudRate = 9600;
--- a/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_K64F/spi_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_K64F/spi_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,10 +13,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "spi_api.h" +#include <math.h> +#include "mbed_assert.h" -#include <math.h> - +#include "spi_api.h" #include "cmsis.h" #include "pinmap.h" #include "error.h" @@ -93,9 +93,7 @@ uint32_t spi_cntl = pinmap_merge(spi_sclk, spi_ssel); obj->instance = pinmap_merge(spi_data, spi_cntl); - if ((int)obj->instance == NC) { - error("SPI pinout mapping failed"); - } + MBED_ASSERT((int)obj->instance != NC); // enable power and clocking clock_manager_set_gate(kClockModuleSPI, obj->instance, true);
--- a/targets/hal/TARGET_NORDIC/TARGET_NRF51822/analogin_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NORDIC/TARGET_NRF51822/analogin_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,10 +13,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "mbed_assert.h" #include "analogin_api.h" #include "cmsis.h" #include "pinmap.h" -#include "error.h" #define ANALOGIN_MEDIAN_FILTER 1 #define ADC_10BIT_RANGE 0x3FF @@ -37,9 +37,7 @@ const PinMap *map = PinMap_ADC; obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC); //(NRF_ADC_Type *) - if (obj->adc == (ADCName)NC) { - error("ADC pin mapping failed"); - } + MBED_ASSERT(obj->adc != (ADCName)NC); while (map->pin != NC) { if (map->pin == pin){ @@ -50,12 +48,12 @@ } obj->adc_pin = (uint8_t)analogInputPin; - NRF_ADC->ENABLE = ADC_ENABLE_ENABLE_Enabled; + NRF_ADC->ENABLE = ADC_ENABLE_ENABLE_Enabled; NRF_ADC->CONFIG = (ADC_CONFIG_RES_10bit << ADC_CONFIG_RES_Pos) | (ADC_CONFIG_INPSEL_AnalogInputOneThirdPrescaling<< ADC_CONFIG_INPSEL_Pos) | (ADC_CONFIG_REFSEL_SupplyOneThirdPrescaling << ADC_CONFIG_REFSEL_Pos) | (analogInputPin << ADC_CONFIG_PSEL_Pos) | - (ADC_CONFIG_EXTREFSEL_None << ADC_CONFIG_EXTREFSEL_Pos); + (ADC_CONFIG_EXTREFSEL_None << ADC_CONFIG_EXTREFSEL_Pos); } uint16_t analogin_read_u16(analogin_t *obj) {
--- a/targets/hal/TARGET_NORDIC/TARGET_NRF51822/gpio_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NORDIC/TARGET_NRF51822/gpio_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,14 +13,16 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "mbed_assert.h" #include "gpio_api.h" #include "pinmap.h" void gpio_init(gpio_t *obj, PinName pin) { - if(pin == NC) return; + obj->pin = pin; + if (pin == (PinName)NC) + return; - obj->pin = pin; - obj->mask = (1ul<<pin); + obj->mask = (1ul << pin); obj->reg_set = &NRF_GPIO->OUTSET; obj->reg_clr = &NRF_GPIO->OUTCLR; @@ -33,20 +35,21 @@ } void gpio_dir(gpio_t *obj, PinDirection direction) { + MBED_ASSERT(obj->pin != (PinName)NC); switch (direction) { case PIN_INPUT : - NRF_GPIO->PIN_CNF[obj->pin] = (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos) + NRF_GPIO->PIN_CNF[obj->pin] = (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos) | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos) | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) | (GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos); - break; - case PIN_OUTPUT: - NRF_GPIO->PIN_CNF[obj->pin] = (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos) + break; + case PIN_OUTPUT: + NRF_GPIO->PIN_CNF[obj->pin] = (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos) | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos) | (GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos) | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) | (GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos); - break; + break; } }
--- a/targets/hal/TARGET_NORDIC/TARGET_NRF51822/gpio_object.h Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NORDIC/TARGET_NRF51822/gpio_object.h Wed Jun 11 16:00:09 2014 +0100 @@ -16,6 +16,8 @@ #ifndef MBED_GPIO_OBJECT_H #define MBED_GPIO_OBJECT_H +#include "mbed_assert.h" + #ifdef __cplusplus extern "C" { #endif @@ -31,6 +33,7 @@ } gpio_t; static inline void gpio_write(gpio_t *obj, int value) { + MBED_ASSERT(obj->pin != (PinName)NC); if (value) *obj->reg_set = obj->mask; else @@ -38,6 +41,7 @@ } static inline int gpio_read(gpio_t *obj) { + MBED_ASSERT(obj->pin != (PinName)NC); return ((*obj->reg_in & obj->mask) ? 1 : 0); }
--- a/targets/hal/TARGET_NORDIC/TARGET_NRF51822/i2c_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NORDIC/TARGET_NRF51822/i2c_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,12 +13,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "mbed_assert.h" #include "i2c_api.h" #include "cmsis.h" #include "pinmap.h" -#include "error.h" - - static const PinMap PinMap_I2C_SDA[] = { {p22, I2C_0, 1}, @@ -52,7 +50,7 @@ (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos)); obj->i2c->PSELSCL = scl; - obj->i2c->PSELSDA = sda; + obj->i2c->PSELSDA = sda; // set default frequency at 100k i2c_frequency(obj, frequency); i2c_interface_enable(obj); @@ -64,30 +62,28 @@ I2CName i2c = (I2CName)pinmap_merge(i2c_sda,i2c_scl); obj->i2c = (NRF_TWI_Type *)i2c; - if ((int)obj->i2c == NC) { - error("I2C pin mapping failed"); - } + MBED_ASSERT((int)obj->i2c != NC); obj->scl=scl; obj->sda=sda; obj->i2c->EVENTS_ERROR = 0; - obj->i2c->ENABLE = TWI_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos; - obj->i2c->POWER = 0; + obj->i2c->ENABLE = TWI_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos; + obj->i2c->POWER = 0; for(int i=0;i<100;i++){ } - obj->i2c->POWER = 1; + obj->i2c->POWER = 1; twi_master_init(obj,sda,scl,100000); } void i2c_reset(i2c_t *obj) { obj->i2c->EVENTS_ERROR = 0; - obj->i2c->ENABLE = TWI_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos; - obj->i2c->POWER = 0; + obj->i2c->ENABLE = TWI_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos; + obj->i2c->POWER = 0; for(int i=0;i<100;i++){ } - obj->i2c->POWER = 1; + obj->i2c->POWER = 1; twi_master_init(obj,obj->sda,obj->scl,obj->freq); } @@ -107,7 +103,7 @@ timeOut--; if(timeOut<0) return 1; - } + } addrSet = 0; i2c_reset(obj); return 0; @@ -122,7 +118,7 @@ if(timeOut<0) return 1; } - obj->i2c->EVENTS_TXDSENT = 0; + obj->i2c->EVENTS_TXDSENT = 0; return 0; } @@ -166,17 +162,17 @@ } int checkError(i2c_t *obj){ - if (obj->i2c->EVENTS_ERROR == 1){ - if (obj->i2c->ERRORSRC & TWI_ERRORSRC_ANACK_Msk){ + if (obj->i2c->EVENTS_ERROR == 1){ + if (obj->i2c->ERRORSRC & TWI_ERRORSRC_ANACK_Msk){ obj->i2c->EVENTS_ERROR = 0; - obj->i2c->TASKS_STOP = 1; + obj->i2c->TASKS_STOP = 1; return I2C_ERROR_BUS_BUSY; } obj->i2c->EVENTS_ERROR = 0; obj->i2c->TASKS_STOP = 1; - return I2C_ERROR_NO_SLAVE; - } + return I2C_ERROR_NO_SLAVE; + } return 0; } @@ -190,7 +186,7 @@ // Read in all except last byte for (count = 0; count < (length - 1); count++) { status = i2c_do_read(obj,&data[count], 0); - if (status) { + if (status) { errorResult = checkError(obj); i2c_reset(obj); if(errorResult<0){ @@ -211,7 +207,7 @@ while(!obj->i2c->EVENTS_STOPPED){ } obj->i2c->EVENTS_STOPPED = 0; - } + } return length; } @@ -219,7 +215,7 @@ int status, errorResult; obj->i2c->ADDRESS = (address>>1); obj->i2c->SHORTS = 0; - obj->i2c->TASKS_STARTTX = 1; + obj->i2c->TASKS_STARTTX = 1; for (int i=0; i<length; i++) { status = i2c_do_write(obj, data[i]); @@ -264,7 +260,7 @@ obj->i2c->TASKS_STARTRX = 1; } else{ - obj->i2c->TASKS_STARTTX = 1; + obj->i2c->TASKS_STARTTX = 1; } } else{
--- a/targets/hal/TARGET_NORDIC/TARGET_NRF51822/pinmap.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NORDIC/TARGET_NRF51822/pinmap.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "mbed_assert.h" #include "pinmap.h" #include "error.h" @@ -20,10 +21,10 @@ } void pin_mode(PinName pin, PinMode mode) { - if (pin == (PinName)NC) { return; } + MBED_ASSERT(pin != (PinName)NC); uint32_t pin_number = (uint32_t)pin; NRF_GPIO->PIN_CNF[pin_number] &= ~GPIO_PIN_CNF_PULL_Msk; - NRF_GPIO->PIN_CNF[pin_number] |= (mode<<GPIO_PIN_CNF_PULL_Pos); + NRF_GPIO->PIN_CNF[pin_number] |= (mode << GPIO_PIN_CNF_PULL_Pos); }
--- a/targets/hal/TARGET_NORDIC/TARGET_NRF51822/pwmout_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NORDIC/TARGET_NRF51822/pwmout_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "mbed_assert.h" #include "pwmout_api.h" #include "cmsis.h" #include "pinmap.h" @@ -43,8 +44,8 @@ {p19, PWM_1, 1}, {p20, PWM_1, 1}, {p21, PWM_1, 1}, - {p22, PWM_1, 1}, - {p23, PWM_1, 1}, + {p22, PWM_1, 1}, + {p23, PWM_1, 1}, {p24, PWM_1, 1}, {p25, PWM_1, 1}, {p28, PWM_1, 1}, @@ -54,7 +55,7 @@ }; static NRF_TIMER_Type *Timers[1] = { - NRF_TIMER2 + NRF_TIMER2 }; uint8_t PWM_taken[NO_PWMS] = {0,0}; @@ -67,34 +68,34 @@ */ #ifdef __cplusplus extern "C" { -#endif +#endif void TIMER2_IRQHandler(void) { static uint16_t CCVal1 = 2501; static uint16_t CCVal2 = 2501; - if ((NRF_TIMER2->EVENTS_COMPARE[1] != 0) && + if ((NRF_TIMER2->EVENTS_COMPARE[1] != 0) && ((NRF_TIMER2->INTENSET & TIMER_INTENSET_COMPARE1_Msk) != 0)){ - NRF_TIMER2->CC[0] = CCVal1; + NRF_TIMER2->CC[0] = CCVal1; NRF_TIMER2->EVENTS_COMPARE[1] = 0; NRF_TIMER2->CC[1] = (NRF_TIMER2->CC[1] + PERIOD[0]); - CCVal1 = NRF_TIMER2->CC[1] + PULSE_WIDTH[0]; + CCVal1 = NRF_TIMER2->CC[1] + PULSE_WIDTH[0]; } - if ((NRF_TIMER2->EVENTS_COMPARE[3] != 0) && + if ((NRF_TIMER2->EVENTS_COMPARE[3] != 0) && ((NRF_TIMER2->INTENSET & TIMER_INTENSET_COMPARE3_Msk) != 0)){ NRF_TIMER2->CC[2] = CCVal2; NRF_TIMER2->EVENTS_COMPARE[3] = 0; NRF_TIMER2->CC[3] = (NRF_TIMER2->CC[3] + PERIOD[1]); - CCVal2 = NRF_TIMER2->CC[3] + PULSE_WIDTH[1]; - } + CCVal2 = NRF_TIMER2->CC[3] + PULSE_WIDTH[1]; + } } #ifdef __cplusplus } -#endif +#endif /** @brief Function for initializing the Timer peripherals. */ void timer_init(uint8_t pwmChoice) @@ -102,10 +103,10 @@ NRF_TIMER_Type *timer = Timers[pwmChoice/2]; if(!(pwmChoice%2)){ timer->POWER = 0; - timer->POWER = 1; - timer->MODE = TIMER_MODE_MODE_Timer; + timer->POWER = 1; + timer->MODE = TIMER_MODE_MODE_Timer; timer->BITMODE = TIMER_BITMODE_BITMODE_16Bit << TIMER_BITMODE_BITMODE_Pos; - timer->PRESCALER = 7;//8us ticks + timer->PRESCALER = 7;//8us ticks } if(pwmChoice%2){ @@ -148,11 +149,11 @@ /* Three NOPs are required to make sure configuration is written before setting tasks or getting events */ __NOP(); __NOP(); - __NOP(); + __NOP(); /* Launch the task to take the GPIOTE channel output to the desired level */ NRF_GPIOTE->TASKS_OUT[channel_number] = 1; - /* Finally configure the channel as the caller expects. If OUTINIT works, the channel is configured properly. + /* Finally configure the channel as the caller expects. If OUTINIT works, the channel is configured properly. If it does not, the channel output inheritance sets the proper level. */ NRF_GPIOTE->CONFIG[channel_number] = (GPIOTE_CONFIG_MODE_Task << GPIOTE_CONFIG_MODE_Pos) | ((uint32_t)pin << GPIOTE_CONFIG_PSEL_Pos) | @@ -162,7 +163,7 @@ /* Three NOPs are required to make sure configuration is written before setting tasks or getting events */ __NOP(); __NOP(); - __NOP(); + __NOP(); } /** @brief Function for initializing the Programmable Peripheral Interconnect peripheral. */ @@ -175,8 +176,8 @@ // Configure PPI channel 0 to toggle ADVERTISING_LED_PIN_NO on every TIMER1 COMPARE[0] match NRF_PPI->CH[channel_number].TEP = (uint32_t)&NRF_GPIOTE->TASKS_OUT[pwm]; NRF_PPI->CH[channel_number+1].TEP = (uint32_t)&NRF_GPIOTE->TASKS_OUT[pwm]; - NRF_PPI->CH[channel_number].EEP = (uint32_t)&timer->EVENTS_COMPARE[channel_number-(4*(channel_number/4))]; - NRF_PPI->CH[channel_number+1].EEP = (uint32_t)&timer->EVENTS_COMPARE[channel_number+1-(4*(channel_number/4))]; + NRF_PPI->CH[channel_number].EEP = (uint32_t)&timer->EVENTS_COMPARE[channel_number-(4*(channel_number/4))]; + NRF_PPI->CH[channel_number+1].EEP = (uint32_t)&timer->EVENTS_COMPARE[channel_number+1-(4*(channel_number/4))]; // Enable PPI channels. NRF_PPI->CHEN |= (1 << channel_number) @@ -213,10 +214,9 @@ // determine the channel uint8_t pwmOutSuccess = 0; PWMName pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM); - - if (pwm == (PWMName)NC){ - error("PwmOut pin mapping failed"); - } + + MBED_ASSERT(pwm != (PWMName)NC); + if(PWM_taken[(uint8_t)pwm]){ for(uint8_t i = 1; !pwmOutSuccess && (i<NO_PWMS) ;i++){ @@ -265,14 +265,14 @@ value = 0.0; } else if (value > 1.0f) { value = 1.0; - } + } oldPulseWidth = ACTUAL_PULSE[obj->pwm]; ACTUAL_PULSE[obj->pwm] = PULSE_WIDTH[obj->pwm] = value* PERIOD[obj->pwm]; if(PULSE_WIDTH[obj->pwm] == 0){ PULSE_WIDTH[obj->pwm] = 1; - setModulation(obj,0,0); + setModulation(obj,0,0); } else if(PULSE_WIDTH[obj->pwm] == PERIOD[obj->pwm]){ PULSE_WIDTH[obj->pwm] = PERIOD[obj->pwm]-1; @@ -280,7 +280,7 @@ } else if( (oldPulseWidth == 0) || (oldPulseWidth == PERIOD[obj->pwm]) ){ setModulation(obj,1,oldPulseWidth == PERIOD[obj->pwm]); - } + } } float pwmout_read(pwmout_t* obj) { @@ -308,7 +308,7 @@ } else{ PERIOD[obj->pwm] =periodInTicks; - } + } } void pwmout_pulsewidth(pwmout_t* obj, float seconds) { @@ -327,7 +327,7 @@ if(PULSE_WIDTH[obj->pwm] == 0){ PULSE_WIDTH[obj->pwm] = 1; - setModulation(obj,0,0); + setModulation(obj,0,0); } else if(PULSE_WIDTH[obj->pwm] == PERIOD[obj->pwm]){ PULSE_WIDTH[obj->pwm] = PERIOD[obj->pwm]-1; @@ -335,5 +335,5 @@ } else if( (oldPulseWidth == 0) || (oldPulseWidth == PERIOD[obj->pwm]) ){ setModulation(obj,1,oldPulseWidth == PERIOD[obj->pwm]); - } + } }
--- a/targets/hal/TARGET_NORDIC/TARGET_NRF51822/serial_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NORDIC/TARGET_NRF51822/serial_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -16,11 +16,12 @@ // math.h required for floating point operations for baud rate calculation //#include <math.h> #include <string.h> +#include "mbed_assert.h" #include "serial_api.h" #include "cmsis.h" #include "pinmap.h" -#include "error.h" + /****************************************************************************** * INITIALIZATION ******************************************************************************/ @@ -65,14 +66,12 @@ UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX); UARTName uart = (UARTName)pinmap_merge(uart_tx, uart_rx); - if ((int)uart == NC) { - error("Serial pinout mapping failed"); - } + MBED_ASSERT((int)uart != NC); obj->uart = (NRF_UART_Type *)uart; - //pin configurations -- - //outputs + //pin configurations -- + //outputs NRF_GPIO->DIR |= (1<<tx);//TX_PIN_NUMBER); NRF_GPIO->DIR |= (1<<RTS_PIN_NUMBER); @@ -118,9 +117,9 @@ if(baudrate<=1200){ obj->uart->BAUDRATE = UART_BAUDRATE_BAUDRATE_Baud1200; return; - } + } - for(int i=1;i<16;i++){ + for(int i=1;i<16;i++){ if(baudrate<acceptedSpeeds[i][0]){ obj->uart->BAUDRATE = acceptedSpeeds[i-1][1]; return; @@ -133,7 +132,7 @@ // 0: 1 stop bits, 1: 2 stop bits // int parity_enable, parity_select; switch (parity) { - case ParityNone: + case ParityNone: obj->uart->CONFIG = 0; break; default: @@ -149,11 +148,11 @@ static inline void uart_irq(uint32_t iir, uint32_t index) { SerialIrq irq_type; switch (iir) { - case 1: - irq_type = TxIrq; + case 1: + irq_type = TxIrq; break; - case 2: - irq_type = RxIrq; + case 2: + irq_type = RxIrq; break; default: return; @@ -165,7 +164,7 @@ } #ifdef __cplusplus extern "C" { -#endif +#endif void UART0_IRQHandler() { uint32_t irtype =0; @@ -180,7 +179,7 @@ } #ifdef __cplusplus } -#endif +#endif void serial_irq_handler(serial_t *obj, uart_irq_handler handler, uint32_t id) { irq_handler = handler; serial_irq_ids[obj->index] = id;
--- a/targets/hal/TARGET_NORDIC/TARGET_NRF51822/spi_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NORDIC/TARGET_NRF51822/spi_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -14,6 +14,7 @@ * limitations under the License. */ //#include <math.h> +#include "mbed_assert.h" #include "spi_api.h" #include "cmsis.h" #include "pinmap.h" @@ -22,7 +23,7 @@ static const PinMap PinMap_SPI_SCLK[] = { {SPI_PSELSCK0 , SPI_0, 0x01}, {SPI_PSELSCK1, SPI_1, 0x02}, - {SPIS_PSELSCK, SPIS, 0x03}, + {SPIS_PSELSCK, SPIS, 0x03}, {NC , NC , 0} }; @@ -59,20 +60,18 @@ SPIName spi_data = (SPIName)pinmap_merge(spi_mosi, spi_miso); SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel); SPIName spi = (SPIName)pinmap_merge(spi_data, spi_cntl); - //SPIName + //SPIName if(ssel==NC){ obj->spi = (NRF_SPI_Type*)spi; obj->spis = (NRF_SPIS_Type*)NC; } else{ obj->spi = (NRF_SPI_Type*)NC; - obj->spis = (NRF_SPIS_Type*)spi; + obj->spis = (NRF_SPIS_Type*)spi; } - - if ((int)obj->spi == NC && (int)obj->spis == NC) { - error("SPI pinout mapping failed"); - } - // pin out the spi pins + MBED_ASSERT((int)obj->spi != NC && (int)obj->spis != NC); + + // pin out the spi pins if (ssel != NC) {//slave obj->spis->POWER=0; obj->spis->POWER=1; @@ -108,7 +107,7 @@ obj->spis->MAXRX=SPIS_MESSAGE_SIZE; obj->spis->MAXTX=SPIS_MESSAGE_SIZE; obj->spis->TXDPTR = (uint32_t)&m_tx_buf[0]; - obj->spis->RXDPTR = (uint32_t)&m_rx_buf[0]; + obj->spis->RXDPTR = (uint32_t)&m_rx_buf[0]; obj->spis->SHORTS = (SPIS_SHORTS_END_ACQUIRE_Enabled<<SPIS_SHORTS_END_ACQUIRE_Pos); spi_format(obj, 8, 0, 1); // 8 bits, mode 0, slave @@ -117,7 +116,7 @@ obj->spi->POWER=0; obj->spi->POWER=1; - //NRF_GPIO->DIR |= (1<<mosi); + //NRF_GPIO->DIR |= (1<<mosi); NRF_GPIO->PIN_CNF[mosi] = (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos) | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos) | (GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos) @@ -141,10 +140,10 @@ obj->spi->PSELMISO = miso; - obj->spi->EVENTS_READY = 0U; + obj->spi->EVENTS_READY = 0U; spi_format(obj, 8, 0, 0); // 8 bits, mode 0, master - spi_frequency(obj, 1000000); + spi_frequency(obj, 1000000); } } @@ -197,10 +196,10 @@ } //default to msb first if(slave){ - obj->spis->CONFIG = (config_mode | (SPI_CONFIG_ORDER_MsbFirst << SPI_CONFIG_ORDER_Pos) ); + obj->spis->CONFIG = (config_mode | (SPI_CONFIG_ORDER_MsbFirst << SPI_CONFIG_ORDER_Pos) ); } else{ - obj->spi->CONFIG = (config_mode | (SPI_CONFIG_ORDER_MsbFirst << SPI_CONFIG_ORDER_Pos) ); + obj->spi->CONFIG = (config_mode | (SPI_CONFIG_ORDER_MsbFirst << SPI_CONFIG_ORDER_Pos) ); } spi_enable(obj,slave); @@ -212,7 +211,7 @@ spi_disable(obj,0); if(hz<250000) { //125Kbps - obj->spi->FREQUENCY = (uint32_t) SPI_FREQUENCY_FREQUENCY_K125; + obj->spi->FREQUENCY = (uint32_t) SPI_FREQUENCY_FREQUENCY_K125; } else if(hz<500000){//250Kbps obj->spi->FREQUENCY = (uint32_t) SPI_FREQUENCY_FREQUENCY_K250; @@ -230,7 +229,7 @@ obj->spi->FREQUENCY = (uint32_t) SPI_FREQUENCY_FREQUENCY_M4; } else{//8Mbps - obj->spi->FREQUENCY = (uint32_t) SPI_FREQUENCY_FREQUENCY_M8; + obj->spi->FREQUENCY = (uint32_t) SPI_FREQUENCY_FREQUENCY_M8; } spi_enable(obj,0); @@ -260,7 +259,7 @@ return spi_read(obj); } -//static inline int spis_writeable(spi_t *obj) { +//static inline int spis_writeable(spi_t *obj) { // return (obj->spis->EVENTS_ACQUIRED==1); //} @@ -268,12 +267,12 @@ return obj->spis->EVENTS_END; }; -int spi_slave_read(spi_t *obj) { +int spi_slave_read(spi_t *obj) { return m_rx_buf[0]; } -void spi_slave_write(spi_t *obj, int value) { - m_tx_buf[0]= value & 0xFF; +void spi_slave_write(spi_t *obj, int value) { + m_tx_buf[0]= value & 0xFF; obj->spis->TASKS_RELEASE=1; obj->spis->EVENTS_ACQUIRED=0; obj->spis->EVENTS_END=0;
--- a/targets/hal/TARGET_NXP/TARGET_LPC11U6X/analogin_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC11U6X/analogin_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - +#include "mbed_assert.h" #include "analogin_api.h" #include "cmsis.h" #include "pinmap.h" @@ -50,9 +50,8 @@ void analogin_init(analogin_t *obj, PinName pin) { volatile uint32_t tmp; obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC); - if (obj->adc == (uint32_t)NC) { - error("ADC pin mapping failed"); - } + MBED_ASSERT(obj->adc != (ADCName)NC); + pinmap_pinout(pin, PinMap_ADC); __IO uint32_t *reg = (__IO uint32_t*)(LPC_IOCON_BASE + (pin & 0x1FF));
--- a/targets/hal/TARGET_NXP/TARGET_LPC11U6X/gpio_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC11U6X/gpio_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "mbed_assert.h" #include "gpio_api.h" #include "pinmap.h" @@ -26,6 +27,7 @@ } uint32_t gpio_set(PinName pin) { + MBED_ASSERT(pin != (PinName)NC); if (!gpio_enabled) gpio_enable(); @@ -39,13 +41,14 @@ } void gpio_init(gpio_t *obj, PinName pin) { - if(pin == NC) return; - obj->pin = pin; + if (pin == (PinName)NC) + return; + obj->mask = gpio_set(pin); unsigned int port = (unsigned int)(pin >> PORT_SHIFT); - + obj->reg_set = &LPC_GPIO_PORT->SET[port]; obj->reg_clr = &LPC_GPIO_PORT->CLR[port]; obj->reg_in = &LPC_GPIO_PORT->PIN[port]; @@ -57,8 +60,13 @@ } void gpio_dir(gpio_t *obj, PinDirection direction) { + MBED_ASSERT(obj->pin != (PinName)NC); switch (direction) { - case PIN_INPUT : *obj->reg_dir &= ~obj->mask; break; - case PIN_OUTPUT: *obj->reg_dir |= obj->mask; break; + case PIN_INPUT : + *obj->reg_dir &= ~obj->mask; + break; + case PIN_OUTPUT: + *obj->reg_dir |= obj->mask; + break; } }
--- a/targets/hal/TARGET_NXP/TARGET_LPC11U6X/gpio_object.h Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC11U6X/gpio_object.h Wed Jun 11 16:00:09 2014 +0100 @@ -16,6 +16,8 @@ #ifndef MBED_GPIO_OBJECT_H #define MBED_GPIO_OBJECT_H +#include "mbed_assert.h" + #ifdef __cplusplus extern "C" { #endif @@ -31,6 +33,7 @@ } gpio_t; static inline void gpio_write(gpio_t *obj, int value) { + MBED_ASSERT(obj->pin != (PinName)NC); if (value) *obj->reg_set = obj->mask; else @@ -38,6 +41,7 @@ } static inline int gpio_read(gpio_t *obj) { + MBED_ASSERT(obj->pin != (PinName)NC); return ((*obj->reg_in & obj->mask) ? 1 : 0); }
--- a/targets/hal/TARGET_NXP/TARGET_LPC11U6X/i2c_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC11U6X/i2c_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,11 +13,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - +#include "mbed_assert.h" #include "i2c_api.h" #include "cmsis.h" #include "pinmap.h" -#include "error.h" #if DEVICE_I2C @@ -96,10 +95,7 @@ I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA); I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL); obj->i2c = (LPC_I2C0_Type *)pinmap_merge(i2c_sda, i2c_scl); - - if ((int)obj->i2c == NC) { - error("I2C pin mapping failed"); - } + MBED_ASSERT((int)obj->i2c != NC); // enable power i2c_power_enable(obj);
--- a/targets/hal/TARGET_NXP/TARGET_LPC11U6X/pinmap.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC11U6X/pinmap.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,15 +13,12 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "mbed_assert.h" #include "pinmap.h" #include "error.h" void pin_function(PinName pin, int function) { - if (pin == (uint32_t)NC) - { - return; - } - + MBED_ASSERT(pin != (PinName)NC); __IO uint32_t *reg = (__IO uint32_t*)(LPC_IOCON_BASE + (pin & 0x1FF)); // pin function bits: [2:0] -> 111 = (0x7) @@ -29,11 +26,7 @@ } void pin_mode(PinName pin, PinMode mode) { - if (pin == (uint32_t)NC) - { - return; - } - + MBED_ASSERT(pin != (PinName)NC); if ((pin == P0_4) || (pin == P0_5)) { // The true open-drain pins PIO0_4 and PIO0_5 can be configured for different I2C-bus speeds. return;
--- a/targets/hal/TARGET_NXP/TARGET_LPC11U6X/serial_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC11U6X/serial_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -15,6 +15,7 @@ */ // math.h required for floating point operations for baud rate calculation +#include "mbed_assert.h" #include <math.h> #include <string.h> #include <stdlib.h> @@ -22,7 +23,6 @@ #include "serial_api.h" #include "cmsis.h" #include "pinmap.h" -#include "error.h" #if DEVICE_SERIAL @@ -82,9 +82,7 @@ UARTName uart_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX); UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX); UARTName uart = (UARTName)pinmap_merge(uart_tx, uart_rx); - if ((int)uart == NC) { - error("Serial pinout mapping failed"); - } + MBED_ASSERT((int)uart != NC); switch (uart) { case UART_0: @@ -252,17 +250,14 @@ } void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits) { - // 0: 1 stop bits, 1: 2 stop bits - if (stop_bits != 1 && stop_bits != 2) { - error("Invalid stop bits specified"); - } + MBED_ASSERT((stop_bits == 1) || (stop_bits == 2)); // 0: 1 stop bits, 1: 2 stop bits + stop_bits -= 1; - + if (obj->index == 0) { - // 0: 5 data bits ... 3: 8 data bits - if (data_bits < 5 || data_bits > 8) { - error("Invalid number of bits (%d) in serial format, should be 5..8", data_bits); - } + MBED_ASSERT((data_bits > 4) && (data_bits < 9)); // 0: 5 data bits ... 3: 8 data bits + MBED_ASSERT((parity == ParityNone) || (parity == ParityOdd) || (parity == ParityEven) || + (parity == ParityForced1) || (parity == ParityForced0)); data_bits -= 5; int parity_enable, parity_select; @@ -273,7 +268,6 @@ case ParityForced1: parity_enable = 1; parity_select = 2; break; case ParityForced0: parity_enable = 1; parity_select = 3; break; default: - error("Invalid serial parity setting"); return; } @@ -284,18 +278,16 @@ } else { // 0: 7 data bits ... 2: 9 data bits - if (data_bits < 7 || data_bits > 9) { - error("Invalid number of bits (%d) in serial format, should be 7..9", data_bits); - } + MBED_ASSERT((data_bits > 6) && (data_bits < 10)); + MBED_ASSERT((parity == ParityNone) || (parity == ParityOdd) || (parity == ParityEven)); data_bits -= 7; - + int paritysel; switch (parity) { case ParityNone: paritysel = 0; break; case ParityEven: paritysel = 2; break; case ParityOdd : paritysel = 3; break; default: - error("Invalid serial parity setting"); return; } obj->mini_uart->CFG = (data_bits << 2)
--- a/targets/hal/TARGET_NXP/TARGET_LPC11U6X/spi_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC11U6X/spi_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "mbed_assert.h" #include <math.h> #include "spi_api.h" @@ -68,10 +69,7 @@ SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel); obj->spi = (LPC_SSP0_Type*)pinmap_merge(spi_data, spi_cntl); - - if ((int)obj->spi == NC) { - error("SPI pinout mapping failed"); - } + MBED_ASSERT((int)obj->spi != NC); // enable power and clocking switch ((int)obj->spi) { @@ -111,10 +109,7 @@ void spi_format(spi_t *obj, int bits, int mode, int slave) { ssp_disable(obj); - - if (!(bits >= 4 && bits <= 16) || !(mode >= 0 && mode <= 3)) { - error("SPI format error"); - } + MBED_ASSERT(((bits >= 4) && (bits <= 16)) || ((mode >= 0) && (mode <= 3))); int polarity = (mode & 0x2) ? 1 : 0; int phase = (mode & 0x1) ? 1 : 0;
--- a/targets/hal/TARGET_NXP/TARGET_LPC11UXX/analogin_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC11UXX/analogin_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "mbed_assert.h" #include "analogin_api.h" #include "cmsis.h" #include "pinmap.h" @@ -46,9 +47,7 @@ void analogin_init(analogin_t *obj, PinName pin) { obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC); - if (obj->adc == (ADCName)NC) { - error("ADC pin mapping failed"); - } + MBED_ASSERT(obj->adc != (ADCName)NC); // Power up ADC LPC_SYSCON->PDRUNCFG &= ~ (1 << 4);
--- a/targets/hal/TARGET_NXP/TARGET_LPC11UXX/gpio_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC11UXX/gpio_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,10 +13,12 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "mbed_assert.h" #include "gpio_api.h" #include "pinmap.h" uint32_t gpio_set(PinName pin) { + MBED_ASSERT(pin != (PinName)NC); int f = ((pin == P0_0) || (pin == P0_10) || (pin == P0_11) || @@ -31,9 +33,10 @@ } void gpio_init(gpio_t *obj, PinName pin) { - if(pin == NC) return; + obj->pin = pin; + if (pin == (PinName)NC) + return; - obj->pin = pin; obj->mask = gpio_set(pin); unsigned int port = (unsigned int)pin >> PORT_SHIFT; @@ -49,8 +52,13 @@ } void gpio_dir(gpio_t *obj, PinDirection direction) { + MBED_ASSERT(obj->pin != (PinName)NC); switch (direction) { - case PIN_INPUT : *obj->reg_dir &= ~obj->mask; break; - case PIN_OUTPUT: *obj->reg_dir |= obj->mask; break; + case PIN_INPUT : + *obj->reg_dir &= ~obj->mask; + break; + case PIN_OUTPUT: + *obj->reg_dir |= obj->mask; + break; } }
--- a/targets/hal/TARGET_NXP/TARGET_LPC11UXX/gpio_object.h Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC11UXX/gpio_object.h Wed Jun 11 16:00:09 2014 +0100 @@ -16,6 +16,8 @@ #ifndef MBED_GPIO_OBJECT_H #define MBED_GPIO_OBJECT_H +#include "mbed_assert.h" + #ifdef __cplusplus extern "C" { #endif @@ -31,6 +33,7 @@ } gpio_t; static inline void gpio_write(gpio_t *obj, int value) { + MBED_ASSERT(obj->pin != (PinName)NC); if (value) *obj->reg_set = obj->mask; else @@ -38,6 +41,7 @@ } static inline int gpio_read(gpio_t *obj) { + MBED_ASSERT(obj->pin != (PinName)NC); return ((*obj->reg_in & obj->mask) ? 1 : 0); }
--- a/targets/hal/TARGET_NXP/TARGET_LPC11UXX/i2c_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC11UXX/i2c_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,10 +13,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "mbed_assert.h" #include "i2c_api.h" #include "cmsis.h" #include "pinmap.h" -#include "error.h" static const PinMap PinMap_I2C_SDA[] = { {P0_5, I2C_0, 1}, @@ -87,10 +87,7 @@ I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA); I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL); obj->i2c = (LPC_I2C_Type *)pinmap_merge(i2c_sda, i2c_scl); - - if ((int)obj->i2c == NC) { - error("I2C pin mapping failed"); - } + MBED_ASSERT((int)obj->i2c != NC); // enable power i2c_power_enable(obj);
--- a/targets/hal/TARGET_NXP/TARGET_LPC11UXX/pinmap.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC11UXX/pinmap.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "mbed_assert.h" #include "pinmap.h" #include "error.h" @@ -20,27 +21,27 @@ #define LPC_IOCON1_BASE (LPC_IOCON_BASE + 0x60) void pin_function(PinName pin, int function) { + MBED_ASSERT(pin != (PinName)NC); if (pin == (PinName)NC) return; uint32_t pin_number = (uint32_t)pin; __IO uint32_t *reg = (pin_number < 32) ? - (__IO uint32_t*)(LPC_IOCON0_BASE + 4 * pin_number) : - (__IO uint32_t*)(LPC_IOCON1_BASE + 4 * (pin_number - 32)); + (__IO uint32_t*)(LPC_IOCON0_BASE + 4 * pin_number) : + (__IO uint32_t*)(LPC_IOCON1_BASE + 4 * (pin_number - 32)); // pin function bits: [2:0] -> 111 = (0x7) *reg = (*reg & ~0x7) | (function & 0x7); } void pin_mode(PinName pin, PinMode mode) { - if (pin == (PinName)NC) { return; } - + MBED_ASSERT(pin != (PinName)NC); uint32_t pin_number = (uint32_t)pin; uint32_t drain = ((uint32_t) mode & (uint32_t) OpenDrain) >> 2; __IO uint32_t *reg = (pin_number < 32) ? - (__IO uint32_t*)(LPC_IOCON0_BASE + 4 * pin_number) : - (__IO uint32_t*)(LPC_IOCON1_BASE + 4 * (pin_number - 32)); + (__IO uint32_t*)(LPC_IOCON0_BASE + 4 * pin_number) : + (__IO uint32_t*)(LPC_IOCON1_BASE + 4 * (pin_number - 32)); uint32_t tmp = *reg; // pin mode bits: [4:3] -> 11000 = (0x3 << 3)
--- a/targets/hal/TARGET_NXP/TARGET_LPC11UXX/pwmout_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC11UXX/pwmout_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,10 +13,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "mbed_assert.h" #include "pwmout_api.h" #include "cmsis.h" #include "pinmap.h" -#include "error.h" #define TCR_CNT_EN 0x00000001 #define TCR_RESET 0x00000002 @@ -69,9 +69,8 @@ void pwmout_init(pwmout_t* obj, PinName pin) { // determine the channel PWMName pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM); - if (pwm == (PWMName)NC) - error("PwmOut pin mapping failed"); - + MBED_ASSERT(pwm != (PWMName)NC); + obj->pwm = pwm; // Timer registers @@ -143,7 +142,7 @@ // for 16bit timer, set prescaler to avoid overflow if (timer == LPC_CT16B0 || timer == LPC_CT16B1) { - uint16_t high_period_ticks = period_ticks >> 16; + uint16_t high_period_ticks = period_ticks >> 16; timer->PR = high_period_ticks; period_ticks /= (high_period_ticks + 1); }
--- a/targets/hal/TARGET_NXP/TARGET_LPC11UXX/serial_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC11UXX/serial_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -21,7 +21,6 @@ #include "serial_api.h" #include "cmsis.h" #include "pinmap.h" -#include "error.h" /****************************************************************************** * INITIALIZATION @@ -55,10 +54,8 @@ UARTName uart_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX); UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX); UARTName uart = (UARTName)pinmap_merge(uart_tx, uart_rx); - if ((int)uart == NC) { - error("Serial pinout mapping failed"); - } - + MBED_ASSERT((int)uart != NC); + obj->uart = (LPC_USART_Type *)uart; LPC_SYSCON->SYSAHBCLKCTRL |= (1<<12); @@ -188,16 +185,12 @@ } void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits) { - // 0: 1 stop bits, 1: 2 stop bits - if (stop_bits != 1 && stop_bits != 2) { - error("Invalid stop bits specified"); - } + MBED_ASSERT((stop_bits == 1) || (stop_bits == 2)); // 0: 1 stop bits, 1: 2 stop bits + MBED_ASSERT((data_bits > 4) && (data_bits < 9)); // 0: 5 data bits ... 3: 8 data bits + MBED_ASSERT((parity == ParityNone) || (parity == ParityOdd) || (parity == ParityEven) || + (parity == ParityForced1) || (parity == ParityForced0)); + stop_bits -= 1; - - // 0: 5 data bits ... 3: 8 data bits - if (data_bits < 5 || data_bits > 8) { - error("Invalid number of bits (%d) in serial format, should be 5..8", data_bits); - } data_bits -= 5; int parity_enable, parity_select; @@ -208,8 +201,7 @@ case ParityForced1: parity_enable = 1; parity_select = 2; break; case ParityForced0: parity_enable = 1; parity_select = 3; break; default: - error("Invalid serial parity setting"); - return; + break; } obj->uart->LCR = data_bits << 0
--- a/targets/hal/TARGET_NXP/TARGET_LPC11UXX/spi_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC11UXX/spi_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "mbed_assert.h" #include <math.h> #include "spi_api.h" #include "cmsis.h" @@ -62,10 +63,7 @@ SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel); obj->spi = (LPC_SSPx_Type*)pinmap_merge(spi_data, spi_cntl); - - if ((int)obj->spi == NC) { - error("SPI pinout mapping failed"); - } + MBED_ASSERT((int)obj->spi != NC); // enable power and clocking switch ((int)obj->spi) { @@ -104,12 +102,10 @@ void spi_free(spi_t *obj) {} void spi_format(spi_t *obj, int bits, int mode, int slave) { + MBED_ASSERT((bits >= 4 && bits <= 16) || (mode >= 0 && mode <= 3)); + ssp_disable(obj); - if (!(bits >= 4 && bits <= 16) || !(mode >= 0 && mode <= 3)) { - error("SPI format error"); - } - int polarity = (mode & 0x2) ? 1 : 0; int phase = (mode & 0x1) ? 1 : 0;
--- a/targets/hal/TARGET_NXP/TARGET_LPC11XX_11CXX/analogin_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC11XX_11CXX/analogin_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "mbed_assert.h" #include "analogin_api.h" #include "cmsis.h" #include "pinmap.h" @@ -43,10 +44,7 @@ void analogin_init(analogin_t *obj, PinName pin) { obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC); - if (obj->adc == (uint32_t)NC) { - error("ADC pin mapping failed"); - return; - } + MBED_ASSERT(obj->adc != (uint32_t)NC); // Power up ADC LPC_SYSCON->PDRUNCFG &= ~ (1 << 4);
--- a/targets/hal/TARGET_NXP/TARGET_LPC11XX_11CXX/gpio_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC11XX_11CXX/gpio_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "mbed_assert.h" #include "gpio_api.h" #include "pinmap.h" #include "reserved_pins.h" @@ -20,24 +21,26 @@ static const PinName reserved_pins[] = TARGET_RESERVED_PINS; uint32_t gpio_set(PinName pin) { + MBED_ASSERT(pin != (PinName)NC); // PIO default value of following ports are not same as others unsigned i; int f = 0; - for (i = 0; i < sizeof(reserved_pins) / sizeof(PinName); i ++) + for (i = 0; i < sizeof(reserved_pins) / sizeof(PinName); i ++) { if (pin == reserved_pins[i]) { f = 1; break; } - + } pin_function(pin, f); return ((pin & 0x0F00) >> 8); } void gpio_init(gpio_t *obj, PinName pin) { - if(pin == NC) return; + obj->pin = pin; + if (pin == (PinName)NC) + return; - obj->pin = pin; LPC_GPIO_TypeDef *port_reg = ((LPC_GPIO_TypeDef *) (LPC_GPIO0_BASE + (((pin & 0xF000) >> PORT_SHIFT) * 0x10000))); obj->reg_mask_read = &port_reg->MASKED_ACCESS[1 << gpio_set(pin)]; @@ -50,9 +53,14 @@ } void gpio_dir(gpio_t *obj, PinDirection direction) { + MBED_ASSERT(obj->pin != (PinName)NC); int pin_number = ((obj->pin & 0x0F00) >> 8); switch (direction) { - case PIN_INPUT : *obj->reg_dir &= ~(1 << pin_number); break; - case PIN_OUTPUT: *obj->reg_dir |= (1 << pin_number); break; + case PIN_INPUT : + *obj->reg_dir &= ~(1 << pin_number); + break; + case PIN_OUTPUT: + *obj->reg_dir |= (1 << pin_number); + break; } }
--- a/targets/hal/TARGET_NXP/TARGET_LPC11XX_11CXX/gpio_object.h Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC11XX_11CXX/gpio_object.h Wed Jun 11 16:00:09 2014 +0100 @@ -16,6 +16,8 @@ #ifndef MBED_GPIO_OBJECT_H #define MBED_GPIO_OBJECT_H +#include "mbed_assert.h" + #ifdef __cplusplus extern "C" { #endif @@ -28,6 +30,7 @@ } gpio_t; static inline void gpio_write(gpio_t *obj, int value) { + MBED_ASSERT(obj->pin != (PinName)NC); uint32_t pin_number = ((obj->pin & 0x0F00) >> 8); if (value) *obj->reg_write |= (1 << pin_number); @@ -36,6 +39,7 @@ } static inline int gpio_read(gpio_t *obj) { + MBED_ASSERT(obj->pin != (PinName)NC); return ((*obj->reg_mask_read) ? 1 : 0); }
--- a/targets/hal/TARGET_NXP/TARGET_LPC11XX_11CXX/i2c_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC11XX_11CXX/i2c_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "mbed_assert.h" #include "i2c_api.h" #include "cmsis.h" #include "pinmap.h" @@ -87,10 +88,7 @@ I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA); I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL); obj->i2c = (LPC_I2C_TypeDef *)pinmap_merge(i2c_sda, i2c_scl); - - if ((int)obj->i2c == NC) { - error("I2C pin mapping failed"); - } + MBED_ASSERT((int)obj->i2c != NC); // enable power i2c_power_enable(obj);
--- a/targets/hal/TARGET_NXP/TARGET_LPC11XX_11CXX/pinmap.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC11XX_11CXX/pinmap.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,12 +13,12 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "mbed_assert.h" #include "pinmap.h" #include "error.h" void pin_function(PinName pin, int function) { - if (pin == (uint32_t)NC) return; - + MBED_ASSERT(pin != (PinName)NC); uint32_t offset = (uint32_t)pin & 0xff; __IO uint32_t *reg = (__IO uint32_t*)(LPC_IOCON_BASE + offset); @@ -27,10 +27,9 @@ } void pin_mode(PinName pin, PinMode mode) { - if (pin == (uint32_t)NC) { return; } - + MBED_ASSERT(pin != (PinName)NC); uint32_t offset = (uint32_t)pin & 0xff; - uint32_t drain = ((uint32_t) mode & (uint32_t) OpenDrain) >> 2; + uint32_t drain = ((uint32_t)mode & (uint32_t)OpenDrain) >> 2; __IO uint32_t *reg = (__IO uint32_t*)(LPC_IOCON_BASE + offset); uint32_t tmp = *reg;
--- a/targets/hal/TARGET_NXP/TARGET_LPC11XX_11CXX/pwmout_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC11XX_11CXX/pwmout_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,10 +13,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "mbed_assert.h" #include "pwmout_api.h" #include "cmsis.h" #include "pinmap.h" -#include "error.h" #define TCR_CNT_EN 0x00000001 #define TCR_RESET 0x00000002 @@ -64,9 +64,8 @@ void pwmout_init(pwmout_t* obj, PinName pin) { // determine the channel PWMName pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM); - if (pwm == (uint32_t)NC) - error("PwmOut pin mapping failed"); - + MBED_ASSERT(pwm != (uint32_t)NC); + obj->pwm = pwm; // Timer registers
--- a/targets/hal/TARGET_NXP/TARGET_LPC11XX_11CXX/serial_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC11XX_11CXX/serial_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -14,6 +14,7 @@ * limitations under the License. */ // math.h required for floating point operations for baud rate calculation +#include "mbed_assert.h" #include <math.h> #include <string.h> #include <stdlib.h> @@ -21,7 +22,6 @@ #include "serial_api.h" #include "cmsis.h" #include "pinmap.h" -#include "error.h" /****************************************************************************** * INITIALIZATION @@ -57,9 +57,7 @@ UARTName uart_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX); UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX); UARTName uart = (UARTName)pinmap_merge(uart_tx, uart_rx); - if ((int)uart == NC) { - error("Serial pinout mapping failed"); - } + MBED_ASSERT((int)uart != NC); obj->uart = (LPC_UART_TypeDef *)uart; LPC_SYSCON->SYSAHBCLKCTRL |= (1<<12); @@ -183,16 +181,12 @@ } void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits) { - // 0: 1 stop bits, 1: 2 stop bits - if (stop_bits != 1 && stop_bits != 2) { - error("Invalid stop bits specified"); - } + MBED_ASSERT((stop_bits == 1) || (stop_bits == 2)); // 0: 1 stop bits, 1: 2 stop bits + MBED_ASSERT((data_bits > 4) && (data_bits < 9)); // 0: 5 data bits ... 3: 8 data bits + MBED_ASSERT((parity == ParityNone) || (parity == ParityOdd) || (parity == ParityEven) || + (parity == ParityForced1) || (parity == ParityForced0)); + stop_bits -= 1; - - // 0: 5 data bits ... 3: 8 data bits - if (data_bits < 5 || data_bits > 8) { - error("Invalid number of bits (%d) in serial format, should be 5..8", data_bits); - } data_bits -= 5; int parity_enable, parity_select; @@ -203,8 +197,7 @@ case ParityForced1: parity_enable = 1; parity_select = 2; break; case ParityForced0: parity_enable = 1; parity_select = 3; break; default: - error("Invalid serial parity setting"); - return; + break; } obj->uart->LCR = data_bits << 0
--- a/targets/hal/TARGET_NXP/TARGET_LPC11XX_11CXX/spi_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC11XX_11CXX/spi_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "mbed_assert.h" #include <math.h> #include "spi_api.h" #include "cmsis.h" @@ -58,10 +59,7 @@ SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel); obj->spi = (LPC_SSP_TypeDef*)pinmap_merge(spi_data, spi_cntl); - - if ((int)obj->spi == NC) { - error("SPI pinout mapping failed"); - } + MBED_ASSERT((int)obj->spi != NC); // enable power and clocking switch ((int)obj->spi) { @@ -100,12 +98,9 @@ void spi_free(spi_t *obj) {} void spi_format(spi_t *obj, int bits, int mode, int slave) { + MBED_ASSERT((bits >= 4 && bits <= 16) || (mode >= 0 && mode <= 3)); ssp_disable(obj); - if (!(bits >= 4 && bits <= 16) || !(mode >= 0 && mode <= 3)) { - error("SPI format error"); - } - int polarity = (mode & 0x2) ? 1 : 0; int phase = (mode & 0x1) ? 1 : 0;
--- a/targets/hal/TARGET_NXP/TARGET_LPC13XX/analogin_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC13XX/analogin_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "mbed_assert.h" #include "analogin_api.h" #include "cmsis.h" #include "pinmap.h" @@ -46,9 +47,7 @@ void analogin_init(analogin_t *obj, PinName pin) { obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC); - if (obj->adc == (uint32_t)NC) { - error("ADC pin mapping failed"); - } + MBED_ASSERT(obj->adc != (ADCName)NC); // Power up ADC LPC_SYSCON->PDRUNCFG &= ~ (1 << 4);
--- a/targets/hal/TARGET_NXP/TARGET_LPC13XX/gpio_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC13XX/gpio_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,24 +13,24 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "mbed_assert.h" #include "gpio_api.h" #include "pinmap.h" uint32_t gpio_set(PinName pin) { - int f = ((pin == P0_11) || - (pin == P0_12) || - (pin == P0_13) || - (pin == P0_14)) ? (1) : (0); - + MBED_ASSERT(pin != (PinName)NC); + int f = ((pin == P0_11) || (pin == P0_12) || + (pin == P0_13) || (pin == P0_14)) ? (1) : (0); pin_function(pin, f); - + return (1 << ((int)pin & 0x1F)); } void gpio_init(gpio_t *obj, PinName pin) { - if(pin == NC) return; + obj->pin = pin; + if (pin == (PinName)NC) + return; - obj->pin = pin; obj->mask = gpio_set(pin); unsigned int port = (unsigned int)pin >> PORT_SHIFT; @@ -46,8 +46,13 @@ } void gpio_dir(gpio_t *obj, PinDirection direction) { + MBED_ASSERT(obj->pin != (PinName)NC); switch (direction) { - case PIN_INPUT : *obj->reg_dir &= ~obj->mask; break; - case PIN_OUTPUT: *obj->reg_dir |= obj->mask; break; + case PIN_INPUT : + *obj->reg_dir &= ~obj->mask; + break; + case PIN_OUTPUT: + *obj->reg_dir |= obj->mask; + break; } }
--- a/targets/hal/TARGET_NXP/TARGET_LPC13XX/gpio_object.h Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC13XX/gpio_object.h Wed Jun 11 16:00:09 2014 +0100 @@ -16,6 +16,8 @@ #ifndef MBED_GPIO_OBJECT_H #define MBED_GPIO_OBJECT_H +#include "mbed_assert.h" + #ifdef __cplusplus extern "C" { #endif @@ -31,6 +33,7 @@ } gpio_t; static inline void gpio_write(gpio_t *obj, int value) { + MBED_ASSERT(obj->pin != (PinName)NC); if (value) *obj->reg_set = obj->mask; else @@ -38,6 +41,7 @@ } static inline int gpio_read(gpio_t *obj) { + MBED_ASSERT(obj->pin != (PinName)NC); return ((*obj->reg_in & obj->mask) ? 1 : 0); }
--- a/targets/hal/TARGET_NXP/TARGET_LPC13XX/i2c_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC13XX/i2c_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "mbed_assert.h" #include "i2c_api.h" #include "cmsis.h" #include "pinmap.h" @@ -87,10 +88,7 @@ I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA); I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL); obj->i2c = (LPC_I2C_Type *)pinmap_merge(i2c_sda, i2c_scl); - - if ((int)obj->i2c == NC) { - error("I2C pin mapping failed"); - } + MBED_ASSERT((int)obj->i2c != NC); // enable power i2c_power_enable(obj);
--- a/targets/hal/TARGET_NXP/TARGET_LPC13XX/pinmap.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC13XX/pinmap.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "mbed_assert.h" #include "pinmap.h" #include "error.h" @@ -20,7 +21,7 @@ #define LPC_IOCON1_BASE (LPC_IOCON_BASE + 0x60) void pin_function(PinName pin, int function) { - if (pin == (uint32_t)NC) return; + MBED_ASSERT(pin != (PinName)NC); uint32_t pin_number = (uint32_t)pin; @@ -33,7 +34,7 @@ } void pin_mode(PinName pin, PinMode mode) { - if (pin == (uint32_t)NC) { return; } + MBED_ASSERT(pin != (PinName)NC); uint32_t pin_number = (uint32_t)pin; uint32_t drain = ((uint32_t) mode & (uint32_t) OpenDrain) >> 2;
--- a/targets/hal/TARGET_NXP/TARGET_LPC13XX/pwmout_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC13XX/pwmout_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,12 +13,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - - +#include "mbed_assert.h" #include "pwmout_api.h" #include "cmsis.h" #include "pinmap.h" -#include "error.h" #define TCR_CNT_EN 0x00000001 #define TCR_RESET 0x00000002 @@ -73,9 +71,8 @@ void pwmout_init(pwmout_t* obj, PinName pin) { // determine the channel PWMName pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM); - if (pwm == (uint32_t)NC) - error("PwmOut pin mapping failed"); - + MBED_ASSERT(pwm != (uint32_t)NC); + obj->pwm = pwm; // Timer registers
--- a/targets/hal/TARGET_NXP/TARGET_LPC13XX/serial_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC13XX/serial_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -14,6 +14,7 @@ * limitations under the License. */ // math.h required for floating point operations for baud rate calculation +#include "mbed_assert.h" #include <math.h> #include <string.h> #include <stdlib.h> @@ -21,7 +22,6 @@ #include "serial_api.h" #include "cmsis.h" #include "pinmap.h" -#include "error.h" /****************************************************************************** * INITIALIZATION @@ -55,9 +55,7 @@ UARTName uart_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX); UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX); UARTName uart = (UARTName)pinmap_merge(uart_tx, uart_rx); - if ((int)uart == NC) { - error("Serial pinout mapping failed"); - } + MBED_ASSERT((int)uart != NC); obj->uart = (LPC_USART_Type *)uart; LPC_SYSCON->SYSAHBCLKCTRL |= (1<<12); @@ -186,16 +184,12 @@ } void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits) { - // 0: 1 stop bits, 1: 2 stop bits - if (stop_bits != 1 && stop_bits != 2) { - error("Invalid stop bits specified"); - } + MBED_ASSERT((stop_bits == 1) || (stop_bits == 2)); // 0: 1 stop bits, 1: 2 stop bits + MBED_ASSERT((data_bits > 4) && (data_bits < 9)); // 0: 5 data bits ... 3: 8 data bits + MBED_ASSERT((parity == ParityNone) || (parity == ParityOdd) || (parity == ParityEven) || + (parity == ParityForced1) || (parity == ParityForced0)); + stop_bits -= 1; - - // 0: 5 data bits ... 3: 8 data bits - if (data_bits < 5 || data_bits > 8) { - error("Invalid number of bits (%d) in serial format, should be 5..8", data_bits); - } data_bits -= 5; int parity_enable, parity_select; @@ -206,8 +200,7 @@ case ParityForced1: parity_enable = 1; parity_select = 2; break; case ParityForced0: parity_enable = 1; parity_select = 3; break; default: - error("Invalid serial parity setting"); - return; + break; } obj->uart->LCR = data_bits << 0
--- a/targets/hal/TARGET_NXP/TARGET_LPC13XX/spi_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC13XX/spi_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "mbed_assert.h" #include <math.h> #include "spi_api.h" #include "cmsis.h" @@ -62,10 +63,7 @@ SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel); obj->spi = (LPC_SSPx_Type*)pinmap_merge(spi_data, spi_cntl); - - if ((int)obj->spi == NC) { - error("SPI pinout mapping failed"); - } + MBED_ASSERT((int)obj->spi != NC); // enable power and clocking switch ((int)obj->spi) { @@ -105,10 +103,7 @@ void spi_format(spi_t *obj, int bits, int mode, int slave) { ssp_disable(obj); - - if (!(bits >= 4 && bits <= 16) || !(mode >= 0 && mode <= 3)) { - error("SPI format error"); - } + MBED_ASSERT((bits >= 4 && bits <= 16) || (mode >= 0 && mode <= 3)); int polarity = (mode & 0x2) ? 1 : 0; int phase = (mode & 0x1) ? 1 : 0;
--- a/targets/hal/TARGET_NXP/TARGET_LPC15XX/analogin_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC15XX/analogin_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,11 +13,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - +#include "mbed_assert.h" #include "analogin_api.h" #include "cmsis.h" #include "pinmap.h" -#include "error.h" #define ANALOGIN_MEDIAN_FILTER 1 @@ -55,9 +54,8 @@ void analogin_init(analogin_t *obj, PinName pin) { obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC); - if (obj->adc == (uint32_t)NC) { - error("ADC pin mapping failed"); - } + MBED_ASSERT(obj->adc != (ADCName)NC); + uint32_t port = (pin >> 5); // enable clock for GPIOx LPC_SYSCON->SYSAHBCLKCTRL0 |= (1UL << (14 + port));
--- a/targets/hal/TARGET_NXP/TARGET_LPC15XX/analogout_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC15XX/analogout_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,15 +13,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "mbed_assert.h" #include "analogout_api.h" #include "cmsis.h" #include "pinmap.h" -#include "error.h" void analogout_init(dac_t *obj, PinName pin) { - if (pin != P0_12) { - error("DAC pin mapping failed"); - } + MBED_ASSERT(pin == P0_12); LPC_SYSCON->SYSAHBCLKCTRL0 |= (1 << 29); LPC_SYSCON->PDRUNCFG &= ~(1 << 12);
--- a/targets/hal/TARGET_NXP/TARGET_LPC15XX/gpio_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC15XX/gpio_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "mbed_assert.h" #include "gpio_api.h" #include "pinmap.h" @@ -26,7 +27,7 @@ } uint32_t gpio_set(PinName pin) { - + MBED_ASSERT(pin != (PinName)NC); if (!gpio_enabled) gpio_enable(); @@ -34,9 +35,10 @@ } void gpio_init(gpio_t *obj, PinName pin) { - if(pin == NC) return; - obj->pin = pin; + if (pin == (PinName)NC) + return; + obj->mask = gpio_set(pin); unsigned int port = (unsigned int)(pin >> 5); @@ -52,8 +54,13 @@ } void gpio_dir(gpio_t *obj, PinDirection direction) { + MBED_ASSERT(obj->pin != (PinName)NC); switch (direction) { - case PIN_INPUT : *obj->reg_dir &= ~obj->mask; break; - case PIN_OUTPUT: *obj->reg_dir |= obj->mask; break; + case PIN_INPUT : + *obj->reg_dir &= ~obj->mask; + break; + case PIN_OUTPUT: + *obj->reg_dir |= obj->mask; + break; } }
--- a/targets/hal/TARGET_NXP/TARGET_LPC15XX/gpio_object.h Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC15XX/gpio_object.h Wed Jun 11 16:00:09 2014 +0100 @@ -16,6 +16,8 @@ #ifndef MBED_GPIO_OBJECT_H #define MBED_GPIO_OBJECT_H +#include "mbed_assert.h" + #ifdef __cplusplus extern "C" { #endif @@ -31,6 +33,8 @@ } gpio_t; static inline void gpio_write(gpio_t *obj, int value) { + MBED_ASSERT(obj->pin != (PinName)NC); + if (value) *obj->reg_set = obj->mask; else @@ -38,6 +42,7 @@ } static inline int gpio_read(gpio_t *obj) { + MBED_ASSERT(obj->pin != (PinName)NC); return ((*obj->reg_in & obj->mask) ? 1 : 0); }
--- a/targets/hal/TARGET_NXP/TARGET_LPC15XX/i2c_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC15XX/i2c_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,11 +13,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - +#include "mbed_assert.h" #include "i2c_api.h" #include "cmsis.h" #include "pinmap.h" -#include "error.h" static uint8_t repeated_start = 0; @@ -42,9 +41,7 @@ } void i2c_init(i2c_t *obj, PinName sda, PinName scl) { - if ((sda != P0_23) | (scl != P0_22)) { - error("I2C pin mapping failed"); - } + MBED_ASSERT((sda == P0_23) || (scl == P0_22)); // Enables clock for I2C0 LPC_SYSCON->SYSAHBCLKCTRL1 |= (1 << 13);
--- a/targets/hal/TARGET_NXP/TARGET_LPC15XX/pinmap.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC15XX/pinmap.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "mbed_assert.h" #include "pinmap.h" #include "error.h" @@ -20,7 +21,7 @@ } void pin_mode(PinName pin, PinMode mode) { - if (pin == (uint32_t)NC) { return; } + MBED_ASSERT(pin != (PinName)NC); if ((pin == P0_22) || (pin == P0_23)) { // The true open-drain pins PIO0_22 and PIO0_23 can be configured for different I2C-bus speeds.
--- a/targets/hal/TARGET_NXP/TARGET_LPC15XX/pwmout_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC15XX/pwmout_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - +#include "mbed_assert.h" #include "pwmout_api.h" #include "cmsis.h" #include "pinmap.h" @@ -38,9 +38,8 @@ } void pwmout_init(pwmout_t* obj, PinName pin) { - if (pin == (uint32_t)NC) - error("PwmOut pin mapping failed"); - + MBED_ASSERT(pin != (uint32_t)NC); + int sct_n = get_available_sct(); if (sct_n == -1) { error("No available SCT");
--- a/targets/hal/TARGET_NXP/TARGET_LPC15XX/serial_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC15XX/serial_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -14,6 +14,7 @@ * limitations under the License. */ // math.h required for floating point operations for baud rate calculation +#include "mbed_assert.h" #include <math.h> #include <string.h> @@ -91,7 +92,7 @@ for (int n = 0; n < sizeof(LPC_SWM->PINASSIGN)/sizeof(*LPC_SWM->PINASSIGN); n ++) { regVal = LPC_SWM->PINASSIGN[n]; for (int j = 0; j <= 24; j += 8) { - if (((regVal >> j) & 0xFF) == pn) + if (((regVal >> j) & 0xFF) == pn) regVal |= (0xFF << j); } LPC_SWM->PINASSIGN[n] = regVal; @@ -195,16 +196,11 @@ } void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits) { - // 0: 1 stop bits, 1: 2 stop bits - if (stop_bits != 1 && stop_bits != 2) { - error("Invalid stop bits specified"); - } + MBED_ASSERT((stop_bits == 1) || (stop_bits == 2)); // 0: 1 stop bits, 1: 2 stop bits + MBED_ASSERT((data_bits > 6) && (data_bits < 10)); // 0: 7 data bits ... 2: 9 data bits + MBED_ASSERT((parity == ParityNone) || (parity == ParityEven) || (parity == ParityOdd)); + stop_bits -= 1; - - // 0: 7 data bits ... 2: 9 data bits - if (data_bits < 7 || data_bits > 9) { - error("Invalid number of bits (%d) in serial format, should be 7..9", data_bits); - } data_bits -= 7; int paritysel; @@ -213,8 +209,7 @@ case ParityEven: paritysel = 2; break; case ParityOdd : paritysel = 3; break; default: - error("Invalid serial parity setting"); - return; + break; } obj->uart->CFG = (data_bits << 2)
--- a/targets/hal/TARGET_NXP/TARGET_LPC15XX/spi_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC15XX/spi_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "mbed_assert.h" #include <math.h> #include "spi_api.h" @@ -126,10 +127,7 @@ void spi_format(spi_t *obj, int bits, int mode, int slave) { spi_disable(obj); - - if (!(bits >= 1 && bits <= 16) || !(mode >= 0 && mode <= 3)) { - error("SPI format error"); - } + MBED_ASSERT((bits >= 1 && bits <= 16) && (mode >= 0 && mode <= 3)); int polarity = (mode & 0x2) ? 1 : 0; int phase = (mode & 0x1) ? 1 : 0;
--- a/targets/hal/TARGET_NXP/TARGET_LPC176X/analogin_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC176X/analogin_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,11 +13,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "mbed_assert.h" #include "analogin_api.h" #include "cmsis.h" #include "pinmap.h" -#include "error.h" #define ANALOGIN_MEDIAN_FILTER 1 @@ -44,9 +44,7 @@ void analogin_init(analogin_t *obj, PinName pin) { obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC); - if (obj->adc == (ADCName)NC) { - error("ADC pin mapping failed"); - } + MBED_ASSERT(obj->adc != (ADCName)NC); // ensure power is turned on LPC_SC->PCONP |= (1 << 12);
--- a/targets/hal/TARGET_NXP/TARGET_LPC176X/analogout_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC176X/analogout_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,11 +13,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "mbed_assert.h" #include "analogout_api.h" #include "cmsis.h" #include "pinmap.h" -#include "error.h" static const PinMap PinMap_DAC[] = { {P0_26, DAC_0, 2}, @@ -26,9 +26,7 @@ void analogout_init(dac_t *obj, PinName pin) { obj->dac = (DACName)pinmap_peripheral(pin, PinMap_DAC); - if (obj->dac == (DACName)NC) { - error("DAC pin mapping failed"); - } + MBED_ASSERT(obj->dac != (DACName)NC); // power is on by default, set DAC clk divider is /4 LPC_SC->PCLKSEL0 &= ~(0x3 << 22);
--- a/targets/hal/TARGET_NXP/TARGET_LPC176X/can_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC176X/can_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,11 +13,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "mbed_assert.h" #include "can_api.h" #include "cmsis.h" #include "pinmap.h" -#include "error.h" #include <math.h> #include <string.h> @@ -258,9 +258,7 @@ CANName can_rd = (CANName)pinmap_peripheral(rd, PinMap_CAN_RD); CANName can_td = (CANName)pinmap_peripheral(td, PinMap_CAN_TD); obj->dev = (LPC_CAN_TypeDef *)pinmap_merge(can_rd, can_td); - if ((int)obj->dev == NC) { - error("CAN pin mapping failed"); - } + MBED_ASSERT((int)obj->dev != NC); switch ((int)obj->dev) { case CAN_1: LPC_SC->PCONP |= 1 << 13; break;
--- a/targets/hal/TARGET_NXP/TARGET_LPC176X/gpio_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC176X/gpio_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,21 +13,23 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "mbed_assert.h" #include "gpio_api.h" #include "pinmap.h" uint32_t gpio_set(PinName pin) { + MBED_ASSERT(pin != (PinName)NC); pin_function(pin, 0); return (1 << ((int)pin & 0x1F)); } void gpio_init(gpio_t *obj, PinName pin) { - if(pin == NC) return; - obj->pin = pin; + if (pin == (PinName)NC) + return; + obj->mask = gpio_set(pin); - - LPC_GPIO_TypeDef *port_reg = (LPC_GPIO_TypeDef *) ((int)pin & ~0x1F); + LPC_GPIO_TypeDef *port_reg = (LPC_GPIO_TypeDef *)((int)pin & ~0x1F); obj->reg_set = &port_reg->FIOSET; obj->reg_clr = &port_reg->FIOCLR; obj->reg_in = &port_reg->FIOPIN; @@ -39,8 +41,13 @@ } void gpio_dir(gpio_t *obj, PinDirection direction) { + MBED_ASSERT(obj->pin != (PinName)NC); switch (direction) { - case PIN_INPUT : *obj->reg_dir &= ~obj->mask; break; - case PIN_OUTPUT: *obj->reg_dir |= obj->mask; break; + case PIN_INPUT : + *obj->reg_dir &= ~obj->mask; + break; + case PIN_OUTPUT: + *obj->reg_dir |= obj->mask; + break; } }
--- a/targets/hal/TARGET_NXP/TARGET_LPC176X/gpio_object.h Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC176X/gpio_object.h Wed Jun 11 16:00:09 2014 +0100 @@ -16,6 +16,8 @@ #ifndef MBED_GPIO_OBJECT_H #define MBED_GPIO_OBJECT_H +#include "mbed_assert.h" + #ifdef __cplusplus extern "C" { #endif @@ -31,6 +33,7 @@ } gpio_t; static inline void gpio_write(gpio_t *obj, int value) { + MBED_ASSERT(obj->pin != (PinName)NC); if (value) *obj->reg_set = obj->mask; else @@ -38,6 +41,7 @@ } static inline int gpio_read(gpio_t *obj) { + MBED_ASSERT(obj->pin != (PinName)NC); return ((*obj->reg_in & obj->mask) ? 1 : 0); }
--- a/targets/hal/TARGET_NXP/TARGET_LPC176X/i2c_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC176X/i2c_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,10 +13,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "mbed_assert.h" #include "i2c_api.h" #include "cmsis.h" #include "pinmap.h" -#include "error.h" static const PinMap PinMap_I2C_SDA[] = { {P0_0 , I2C_1, 3}, @@ -96,10 +96,7 @@ I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA); I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL); obj->i2c = (LPC_I2C_TypeDef *)pinmap_merge(i2c_sda, i2c_scl); - - if ((int)obj->i2c == NC) { - error("I2C pin mapping failed"); - } + MBED_ASSERT((int)obj->i2c != NC); // enable power i2c_power_enable(obj);
--- a/targets/hal/TARGET_NXP/TARGET_LPC176X/pinmap.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC176X/pinmap.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,12 +13,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "mbed_assert.h" #include "pinmap.h" #include "error.h" void pin_function(PinName pin, int function) { - if (pin == (PinName)NC) return; - + MBED_ASSERT(pin != (PinName)NC); + uint32_t pin_number = (uint32_t)pin - (uint32_t)P0_0; int index = pin_number >> 4; int offset = (pin_number & 0xF) << 1; @@ -28,7 +29,7 @@ } void pin_mode(PinName pin, PinMode mode) { - if (pin == (PinName)NC) { return; } + MBED_ASSERT(pin != (PinName)NC); uint32_t pin_number = (uint32_t)pin - (uint32_t)P0_0; int index = pin_number >> 5;
--- a/targets/hal/TARGET_NXP/TARGET_LPC176X/pwmout_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC176X/pwmout_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,10 +13,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "mbed_assert.h" #include "pwmout_api.h" #include "cmsis.h" #include "pinmap.h" -#include "error.h" #define TCR_CNT_EN 0x00000001 #define TCR_RESET 0x00000002 @@ -57,9 +57,8 @@ void pwmout_init(pwmout_t* obj, PinName pin) { // determine the channel PWMName pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM); - if (pwm == (PWMName)NC) - error("PwmOut pin mapping failed"); - + MBED_ASSERT(pwm != (PWMName)NC); + obj->pwm = pwm; obj->MR = PWM_MATCH[pwm];
--- a/targets/hal/TARGET_NXP/TARGET_LPC176X/serial_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC176X/serial_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -14,6 +14,7 @@ * limitations under the License. */ // math.h required for floating point operations for baud rate calculation +#include "mbed_assert.h" #include <math.h> #include <string.h> #include <stdlib.h> @@ -21,7 +22,6 @@ #include "serial_api.h" #include "cmsis.h" #include "pinmap.h" -#include "error.h" #include "gpio_api.h" /****************************************************************************** @@ -89,9 +89,7 @@ UARTName uart_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX); UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX); UARTName uart = (UARTName)pinmap_merge(uart_tx, uart_rx); - if ((int)uart == NC) { - error("Serial pinout mapping failed"); - } + MBED_ASSERT((int)uart != NC); obj->uart = (LPC_UART_TypeDef *)uart; // enable power @@ -150,6 +148,7 @@ // serial_baud // set the baud rate, taking in to account the current SystemFrequency void serial_baud(serial_t *obj, int baudrate) { + MBED_ASSERT((int)obj->uart <= UART_3); // The LPC2300 and LPC1700 have a divider and a fractional divider to control the // baud rate. The formula is: // @@ -165,7 +164,7 @@ case UART_1: LPC_SC->PCLKSEL0 &= ~(0x3 << 8); LPC_SC->PCLKSEL0 |= (0x1 << 8); break; case UART_2: LPC_SC->PCLKSEL1 &= ~(0x3 << 16); LPC_SC->PCLKSEL1 |= (0x1 << 16); break; case UART_3: LPC_SC->PCLKSEL1 &= ~(0x3 << 18); LPC_SC->PCLKSEL1 |= (0x1 << 18); break; - default: error("serial_baud"); break; + default: break; } uint32_t PCLK = SystemCoreClock; @@ -245,16 +244,12 @@ } void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits) { - // 0: 1 stop bits, 1: 2 stop bits - if (stop_bits != 1 && stop_bits != 2) { - error("Invalid stop bits specified"); - } + MBED_ASSERT((stop_bits == 1) || (stop_bits == 2)); // 0: 1 stop bits, 1: 2 stop bits + MBED_ASSERT((data_bits > 4) && (data_bits < 9)); // 0: 5 data bits ... 3: 8 data bits + MBED_ASSERT((parity == ParityNone) || (parity == ParityOdd) || (parity == ParityEven) || + (parity == ParityForced1) || (parity == ParityForced0)); + stop_bits -= 1; - - // 0: 5 data bits ... 3: 8 data bits - if (data_bits < 5 || data_bits > 8) { - error("Invalid number of bits (%d) in serial format, should be 5..8", data_bits); - } data_bits -= 5; int parity_enable, parity_select; @@ -265,8 +260,7 @@ case ParityForced1: parity_enable = 1; parity_select = 2; break; case ParityForced0: parity_enable = 1; parity_select = 3; break; default: - error("Invalid serial parity setting"); - return; + break; } obj->uart->LCR = data_bits << 0
--- a/targets/hal/TARGET_NXP/TARGET_LPC176X/spi_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC176X/spi_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "mbed_assert.h" #include <math.h> #include "spi_api.h" @@ -64,9 +65,7 @@ SPIName spi_data = (SPIName)pinmap_merge(spi_mosi, spi_miso); SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel); obj->spi = (LPC_SSP_TypeDef*)pinmap_merge(spi_data, spi_cntl); - if ((int)obj->spi == NC) { - error("SPI pinout mapping failed"); - } + MBED_ASSERT((int)obj->spi != NC); // enable power and clocking switch ((int)obj->spi) { @@ -98,9 +97,7 @@ void spi_format(spi_t *obj, int bits, int mode, int slave) { ssp_disable(obj); - if (!(bits >= 4 && bits <= 16) || !(mode >= 0 && mode <= 3)) { - error("SPI format error"); - } + MBED_ASSERT(((bits >= 4) && (bits <= 16)) && (mode >= 0 && mode <= 3)); int polarity = (mode & 0x2) ? 1 : 0; int phase = (mode & 0x1) ? 1 : 0;
--- a/targets/hal/TARGET_NXP/TARGET_LPC23XX/analogin_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC23XX/analogin_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,10 +13,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "mbed_assert.h" #include "analogin_api.h" #include "cmsis.h" #include "pinmap.h" -#include "error.h" #define ANALOGIN_MEDIAN_FILTER 1 @@ -42,9 +42,7 @@ void analogin_init(analogin_t *obj, PinName pin) { obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC); - if (obj->adc == (ADCName)NC) { - error("ADC pin mapping failed"); - } + MBED_ASSERT(obj->adc != (ADCName)NC); // ensure power is turned on LPC_SC->PCONP |= (1 << 12);
--- a/targets/hal/TARGET_NXP/TARGET_LPC23XX/analogout_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC23XX/analogout_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,10 +13,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "mbed_assert.h" #include "analogout_api.h" #include "cmsis.h" #include "pinmap.h" -#include "error.h" static const PinMap PinMap_DAC[] = { {P0_26, DAC_0, 2}, @@ -25,9 +25,7 @@ void analogout_init(dac_t *obj, PinName pin) { obj->dac = (DACName)pinmap_peripheral(pin, PinMap_DAC); - if (obj->dac == (DACName)NC) { - error("DAC pin mapping failed"); - } + MBED_ASSERT(obj->dac != (DACName)NC); // power is on by default, set DAC clk divider is /4 LPC_SC->PCLKSEL0 &= ~(0x3 << 22);
--- a/targets/hal/TARGET_NXP/TARGET_LPC23XX/can_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC23XX/can_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,10 +13,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "mbed_assert.h" #include "can_api.h" #include "cmsis.h" #include "pinmap.h" -#include "error.h" #include <math.h> #include <string.h> @@ -161,9 +161,7 @@ CANName can_rd = (CANName)pinmap_peripheral(rd, PinMap_CAN_RD); CANName can_td = (CANName)pinmap_peripheral(td, PinMap_CAN_TD); obj->dev = (LPC_CAN_TypeDef *)pinmap_merge(can_rd, can_td); - if ((int)obj->dev == NC) { - error("CAN pin mapping failed"); - } + MBED_ASSERT((int)obj->dev != NC); switch ((int)obj->dev) { case CAN_1: LPC_SC->PCONP |= 1 << 13; break;
--- a/targets/hal/TARGET_NXP/TARGET_LPC23XX/gpio_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC23XX/gpio_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "mbed_assert.h" #include "gpio_api.h" #include "pinmap.h" @@ -25,8 +26,8 @@ } void gpio_init(gpio_t *obj, PinName pin) { - if (pin == NC) return; - + if (pin == (PinName)NC) + return; obj->pin = pin; obj->mask = gpio_set(pin); @@ -43,6 +44,8 @@ } void gpio_dir(gpio_t *obj, PinDirection direction) { + if (obj->pin == (PinName)NC) + return; switch (direction) { case PIN_INPUT : *obj->reg_dir &= ~obj->mask; break; case PIN_OUTPUT: *obj->reg_dir |= obj->mask; break;
--- a/targets/hal/TARGET_NXP/TARGET_LPC23XX/gpio_object.h Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC23XX/gpio_object.h Wed Jun 11 16:00:09 2014 +0100 @@ -16,6 +16,8 @@ #ifndef MBED_GPIO_OBJECT_H #define MBED_GPIO_OBJECT_H +#include "mbed_assert.h" + #ifdef __cplusplus extern "C" { #endif @@ -31,6 +33,7 @@ } gpio_t; static inline void gpio_write(gpio_t *obj, int value) { + MBED_ASSERT(obj->pin != (PinName)NC); if (value) *obj->reg_set = obj->mask; else @@ -38,6 +41,7 @@ } static inline int gpio_read(gpio_t *obj) { + MBED_ASSERT(obj->pin != (PinName)NC); return ((*obj->reg_in & obj->mask) ? 1 : 0); }
--- a/targets/hal/TARGET_NXP/TARGET_LPC23XX/i2c_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC23XX/i2c_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -16,7 +16,6 @@ #include "i2c_api.h" #include "cmsis.h" #include "pinmap.h" -#include "error.h" static const PinMap PinMap_I2C_SDA[] = { {P0_0 , I2C_1, 3}, @@ -96,10 +95,7 @@ I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA); I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL); obj->i2c = (LPC_I2C_TypeDef *)pinmap_merge(i2c_sda, i2c_scl); - - if ((int)obj->i2c == NC) { - error("I2C pin mapping failed"); - } + MBED_ASSERT((int)obj->i2c != NC); // enable power i2c_power_enable(obj);
--- a/targets/hal/TARGET_NXP/TARGET_LPC23XX/pinmap.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC23XX/pinmap.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,11 +13,12 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "mbed_assert.h" #include "pinmap.h" #include "error.h" void pin_function(PinName pin, int function) { - if (pin == (PinName)NC) return; + MBED_ASSERT(pin != (PinName)NC); uint32_t pin_number = (uint32_t)pin - (uint32_t)P0_0; int index = pin_number >> 4; @@ -28,15 +29,13 @@ } void pin_mode(PinName pin, PinMode mode) { - if (pin == (PinName)NC) { return; } - + MBED_ASSERT((pin != (PinName)NC) && (mode != OpenDrain)); + uint32_t pin_number = (uint32_t)pin - (uint32_t)P0_0; int index = pin_number >> 5; int offset = pin_number & 0x1F; uint32_t drain = ((uint32_t) mode & (uint32_t) OpenDrain) >> 2; - - if (mode == OpenDrain) error("OpenDrain not supported on LPC2368"); - + if (!drain) { index = pin_number >> 4; offset = (pin_number & 0xF) << 1;
--- a/targets/hal/TARGET_NXP/TARGET_LPC23XX/pwmout_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC23XX/pwmout_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,10 +13,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "mbed_assert.h" #include "pwmout_api.h" #include "cmsis.h" #include "pinmap.h" -#include "error.h" #define TCR_CNT_EN 0x00000001 #define TCR_RESET 0x00000002 @@ -57,9 +57,8 @@ void pwmout_init(pwmout_t* obj, PinName pin) { // determine the channel PWMName pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM); - if (pwm == (PWMName)NC) - error("PwmOut pin mapping failed"); - + MBED_ASSERT(pwm != (PWMName)NC); + obj->pwm = pwm; obj->MR = PWM_MATCH[pwm];
--- a/targets/hal/TARGET_NXP/TARGET_LPC23XX/serial_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC23XX/serial_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -14,6 +14,7 @@ * limitations under the License. */ // math.h required for floating point operations for baud rate calculation +#include "mbed_assert.h" #include <math.h> #include <string.h> #include <stdlib.h> @@ -21,7 +22,6 @@ #include "serial_api.h" #include "cmsis.h" #include "pinmap.h" -#include "error.h" /****************************************************************************** * INITIALIZATION @@ -65,9 +65,7 @@ UARTName uart_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX); UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX); UARTName uart = (UARTName)pinmap_merge(uart_tx, uart_rx); - if ((int)uart == NC) { - error("Serial pinout mapping failed"); - } + MBED_ASSERT((int)uart != NC); obj->uart = (LPC_UART_TypeDef *)uart; // enable power @@ -123,6 +121,7 @@ // serial_baud // set the baud rate, taking in to account the current SystemFrequency void serial_baud(serial_t *obj, int baudrate) { + MBED_ASSERT((int)obj->uart <= UART_3); // The LPC2300 and LPC1700 have a divider and a fractional divider to control the // baud rate. The formula is: // @@ -138,7 +137,7 @@ case UART_1: LPC_SC->PCLKSEL0 &= ~(0x3 << 8); LPC_SC->PCLKSEL0 |= (0x1 << 8); break; case UART_2: LPC_SC->PCLKSEL1 &= ~(0x3 << 16); LPC_SC->PCLKSEL1 |= (0x1 << 16); break; case UART_3: LPC_SC->PCLKSEL1 &= ~(0x3 << 18); LPC_SC->PCLKSEL1 |= (0x1 << 18); break; - default: error("serial_baud"); break; + default: break; } uint32_t PCLK = SystemCoreClock; @@ -218,16 +217,12 @@ } void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits) { - // 0: 1 stop bits, 1: 2 stop bits - if (stop_bits != 1 && stop_bits != 2) { - error("Invalid stop bits specified"); - } + MBED_ASSERT((stop_bits == 1) || (stop_bits == 2)); // 0: 1 stop bits, 1: 2 stop bits + MBED_ASSERT((data_bits > 4) && (data_bits < 9)); // 0: 5 data bits ... 3: 8 data bits + MBED_ASSERT((parity == ParityNone) || (parity == ParityOdd) || (parity == ParityEven) || + (parity == ParityForced1) || (parity == ParityForced0)); + stop_bits -= 1; - - // 0: 5 data bits ... 3: 8 data bits - if (data_bits < 5 || data_bits > 8) { - error("Invalid number of bits (%d) in serial format, should be 5..8", data_bits); - } data_bits -= 5; int parity_enable, parity_select; @@ -238,8 +233,7 @@ case ParityForced1: parity_enable = 1; parity_select = 2; break; case ParityForced0: parity_enable = 1; parity_select = 3; break; default: - error("Invalid serial parity setting"); - return; + break; } obj->uart->LCR = data_bits << 0
--- a/targets/hal/TARGET_NXP/TARGET_LPC23XX/spi_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC23XX/spi_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "mbed_assert.h" #include <math.h> #include "spi_api.h" @@ -64,10 +65,7 @@ SPIName spi_data = (SPIName)pinmap_merge(spi_mosi, spi_miso); SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel); obj->spi = (LPC_SSP_TypeDef*)pinmap_merge(spi_data, spi_cntl); - - if ((int)obj->spi == NC) { - error("SPI pinout mapping failed"); - } + MBED_ASSERT((int)obj->spi != NC); // enable power and clocking switch ((int)obj->spi) { @@ -98,12 +96,9 @@ void spi_free(spi_t *obj) {} void spi_format(spi_t *obj, int bits, int mode, int slave) { + MBED_ASSERT(((bits >= 4) && (bits <= 16)) && ((mode >= 0) && (mode <= 3))); ssp_disable(obj); - if (!(bits >= 4 && bits <= 16) || !(mode >= 0 && mode <= 3)) { - error("SPI format error"); - } - int polarity = (mode & 0x2) ? 1 : 0; int phase = (mode & 0x1) ? 1 : 0;
--- a/targets/hal/TARGET_NXP/TARGET_LPC408X/analogin_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC408X/analogin_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "mbed_assert.h" #include "analogin_api.h" #include "cmsis.h" #include "pinmap.h" @@ -43,9 +44,7 @@ void analogin_init(analogin_t *obj, PinName pin) { obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC); - if (obj->adc == (ADCName)NC) { - error("ADC pin mapping failed"); - } + MBED_ASSERT(obj->adc != (ADCName)NC); // ensure power is turned on LPC_SC->PCONP |= (1 << 12);
--- a/targets/hal/TARGET_NXP/TARGET_LPC408X/analogout_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC408X/analogout_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,10 +13,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "mbed_assert.h" #include "analogout_api.h" #include "cmsis.h" #include "pinmap.h" -#include "error.h" static const PinMap PinMap_DAC[] = { {P0_26, DAC_0, 2}, @@ -25,9 +25,7 @@ void analogout_init(dac_t *obj, PinName pin) { obj->dac = (DACName)pinmap_peripheral(pin, PinMap_DAC); - if (obj->dac == (DACName)NC) { - error("DAC pin mapping failed"); - } + MBED_ASSERT(obj->dac != (DACName)NC); // DAC enable bit must be set LPC_IOCON->P0_26 |= (1 << 16); // DACEN
--- a/targets/hal/TARGET_NXP/TARGET_LPC408X/can_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC408X/can_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -17,7 +17,6 @@ #include "cmsis.h" #include "pinmap.h" -#include "error.h" #include <math.h> #include <string.h> @@ -244,9 +243,7 @@ CANName can_rd = (CANName)pinmap_peripheral(rd, PinMap_CAN_RD); CANName can_td = (CANName)pinmap_peripheral(td, PinMap_CAN_TD); obj->dev = (LPC_CAN_TypeDef *)pinmap_merge(can_rd, can_td); - if ((int)obj->dev == NC) { - error("CAN pin mapping failed"); - } + MBED_ASSERT((int)obj->dev != NC); switch ((int)obj->dev) { case CAN_1: LPC_SC->PCONP |= 1 << 13; break;
--- a/targets/hal/TARGET_NXP/TARGET_LPC408X/gpio_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC408X/gpio_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,18 +13,21 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "mbed_assert.h" #include "gpio_api.h" #include "pinmap.h" uint32_t gpio_set(PinName pin) { + MBED_ASSERT(pin != (PinName)NC); pin_function(pin, 0); return (1 << ((int)pin & 0x1F)); } void gpio_init(gpio_t *obj, PinName pin) { - if (pin == NC) return; - obj->pin = pin; + if (pin == (PinName)NC) + return; + obj->mask = gpio_set(pin); LPC_GPIO_TypeDef *port_reg = (LPC_GPIO_TypeDef *) ((int)(LPC_GPIO0_BASE+pin) & ~0x1F); @@ -40,8 +43,14 @@ } void gpio_dir(gpio_t *obj, PinDirection direction) { + MBED_ASSERT(obj->pin != (PinName)NC); + switch (direction) { - case PIN_INPUT : *obj->reg_dir &= ~obj->mask; break; - case PIN_OUTPUT: *obj->reg_dir |= obj->mask; break; + case PIN_INPUT : + *obj->reg_dir &= ~obj->mask; + break; + case PIN_OUTPUT: + *obj->reg_dir |= obj->mask; + break; } }
--- a/targets/hal/TARGET_NXP/TARGET_LPC408X/gpio_object.h Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC408X/gpio_object.h Wed Jun 11 16:00:09 2014 +0100 @@ -16,6 +16,8 @@ #ifndef MBED_GPIO_OBJECT_H #define MBED_GPIO_OBJECT_H +#include "mbed_assert.h" + #ifdef __cplusplus extern "C" { #endif @@ -31,6 +33,7 @@ } gpio_t; static inline void gpio_write(gpio_t *obj, int value) { + MBED_ASSERT(obj->pin != (PinName)NC); if (value) *obj->reg_set = obj->mask; else @@ -38,6 +41,7 @@ } static inline int gpio_read(gpio_t *obj) { + MBED_ASSERT(obj->pin != (PinName)NC); return ((*obj->reg_in & obj->mask) ? 1 : 0); }
--- a/targets/hal/TARGET_NXP/TARGET_LPC408X/i2c_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC408X/i2c_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,10 +13,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "mbed_assert.h" #include "i2c_api.h" #include "cmsis.h" #include "pinmap.h" -#include "error.h" static const PinMap PinMap_I2C_SDA[] = { {P0_0 , I2C_1, 3}, @@ -108,9 +108,7 @@ I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA); I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL); obj->i2c = (LPC_I2C_TypeDef *)pinmap_merge(i2c_sda, i2c_scl); - if ((int)obj->i2c == NC) { - error("I2C pin mapping failed"); - } + MBED_ASSERT((int)obj->i2c != NC); // enable power i2c_power_enable(obj);
--- a/targets/hal/TARGET_NXP/TARGET_LPC408X/pinmap.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC408X/pinmap.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,12 +13,12 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "mbed_assert.h" #include "pinmap.h" #include "error.h" void pin_function(PinName pin, int function) { - if (pin == (PinName)NC) return; - + MBED_ASSERT(pin != (PinName)NC); __IO uint32_t *reg = (__IO uint32_t*) (LPC_IOCON_BASE + 4 * pin); // pin function bits: [2:0] -> 111 = (0x7) @@ -26,7 +26,7 @@ } void pin_mode(PinName pin, PinMode mode) { - if (pin == (PinName)NC) { return; } + MBED_ASSERT(pin != (PinName)NC); uint32_t drain = ((uint32_t) mode & (uint32_t) OpenDrain) >> 2;
--- a/targets/hal/TARGET_NXP/TARGET_LPC408X/pwmout_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC408X/pwmout_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,10 +13,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "mbed_assert.h" #include "pwmout_api.h" #include "cmsis.h" #include "pinmap.h" -#include "error.h" #define TCR_CNT_EN 0x00000001 #define TCR_RESET 0x00000002 @@ -66,9 +66,8 @@ void pwmout_init(pwmout_t* obj, PinName pin) { // determine the channel PWMName pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM); - if (pwm == (PWMName)NC) - error("PwmOut pin mapping failed"); - + MBED_ASSERT(pwm != (PWMName)NC); + obj->channel = pwm; obj->pwm = LPC_PWM0;
--- a/targets/hal/TARGET_NXP/TARGET_LPC408X/serial_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC408X/serial_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -72,9 +72,7 @@ UARTName uart_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX); UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX); UARTName uart = (UARTName)pinmap_merge(uart_tx, uart_rx); - if ((int)uart == NC) { - error("Serial pinout mapping failed"); - } + MBED_ASSERT((int)uart != NC); obj->uart = (LPC_UART_TypeDef *)uart; // enable power @@ -209,16 +207,12 @@ } void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits) { - // 0: 1 stop bits, 1: 2 stop bits - if (stop_bits != 1 && stop_bits != 2) { - error("Invalid stop bits specified"); - } + MBED_ASSERT((stop_bits == 1) || (stop_bits == 2)); // 0: 1 stop bits, 1: 2 stop bits + MBED_ASSERT((data_bits > 4) && (data_bits < 9)); // 0: 5 data bits ... 3: 8 data bits + MBED_ASSERT((parity == ParityNone) || (parity == ParityOdd) || (parity == ParityEven) || + (parity == ParityForced1) || (parity == ParityForced0)); + stop_bits -= 1; - - // 0: 5 data bits ... 3: 8 data bits - if (data_bits < 5 || data_bits > 8) { - error("Invalid number of bits (%d) in serial format, should be 5..8", data_bits); - } data_bits -= 5; int parity_enable, parity_select; @@ -229,8 +223,7 @@ case ParityForced1: parity_enable = 1; parity_select = 2; break; case ParityForced0: parity_enable = 1; parity_select = 3; break; default: - error("Invalid serial parity setting"); - return; + break; } obj->uart->LCR = data_bits << 0
--- a/targets/hal/TARGET_NXP/TARGET_LPC408X/spi_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC408X/spi_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -84,9 +84,7 @@ SPIName spi_data = (SPIName)pinmap_merge(spi_mosi, spi_miso); SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel); obj->spi = (LPC_SSP_TypeDef*)pinmap_merge(spi_data, spi_cntl); - if ((int)obj->spi == NC) { - error("SPI pinout mapping failed"); - } + MBED_ASSERT((int)obj->spi != NC); // enable power and clocking switch ((int)obj->spi) { @@ -118,12 +116,9 @@ void spi_free(spi_t *obj) {} void spi_format(spi_t *obj, int bits, int mode, int slave) { + MBED_ASSERT(((bits >= 4) && (bits <= 16)) && ((mode >= 0) && (mode <= 3))); ssp_disable(obj); - if (!(bits >= 4 && bits <= 16) || !(mode >= 0 && mode <= 3)) { - error("SPI format error"); - } - int polarity = (mode & 0x2) ? 1 : 0; int phase = (mode & 0x1) ? 1 : 0;
--- a/targets/hal/TARGET_NXP/TARGET_LPC43XX/analogin_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC43XX/analogin_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -15,10 +15,10 @@ * * Ported to NXP LPC43XX by Micromint USA <support@micromint.com> */ +#include "mbed_assert.h" #include "analogin_api.h" #include "cmsis.h" #include "pinmap.h" -#include "error.h" #define ANALOGIN_MEDIAN_FILTER 1 @@ -41,10 +41,7 @@ uint8_t num, chan; obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC); - if (obj->adc == (uint32_t)NC) { - error("ADC pin mapping failed"); - } - + MBED_ASSERT(obj->adc != (ADCName)NC); // Configure the pin as GPIO input if (pin < SFP_AIO0) {
--- a/targets/hal/TARGET_NXP/TARGET_LPC43XX/analogout_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC43XX/analogout_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -15,10 +15,10 @@ * * Ported to NXP LPC43XX by Micromint USA <support@micromint.com> */ +#include "mbed_assert.h" #include "analogout_api.h" #include "cmsis.h" #include "pinmap.h" -#include "error.h" static const PinMap PinMap_DAC[] = { {P_DAC0 , DAC_0, 0x0}, @@ -27,9 +27,7 @@ void analogout_init(dac_t *obj, PinName pin) { obj->dac = (DACName)pinmap_peripheral(pin, PinMap_DAC); - if (obj->dac == (DACName)NC) { - error("DAC pin mapping failed"); - } + MBED_ASSERT(obj->dac != (DACName)NC); // Configure the pin as GPIO input pin_function(pin, (SCU_PINIO_PULLNONE | 0x0));
--- a/targets/hal/TARGET_NXP/TARGET_LPC43XX/gpio_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC43XX/gpio_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -15,10 +15,12 @@ * * Ported to NXP LPC43XX by Micromint USA <support@micromint.com> */ +#include "mbed_assert.h" #include "gpio_api.h" #include "pinmap.h" uint32_t gpio_set(PinName pin) { + MBED_ASSERT(pin != (PinName)NC); int f = 0; unsigned int port = (unsigned int)MBED_GPIO_PORT(pin); @@ -29,9 +31,10 @@ } void gpio_init(gpio_t *obj, PinName pin) { - if (pin == NC) return; - obj->pin = pin; + if (pin == (PinName)NC) + return; + obj->mask = gpio_set(pin); LPC_GPIO_T *port_reg = (LPC_GPIO_T *) (LPC_GPIO_PORT_BASE); @@ -48,8 +51,13 @@ } void gpio_dir(gpio_t *obj, PinDirection direction) { + MBED_ASSERT(obj->pin != (PinName)NC); switch (direction) { - case PIN_INPUT : *obj->reg_dir &= ~obj->mask; break; - case PIN_OUTPUT: *obj->reg_dir |= obj->mask; break; + case PIN_INPUT : + *obj->reg_dir &= ~obj->mask; + break; + case PIN_OUTPUT: + *obj->reg_dir |= obj->mask; + break; } }
--- a/targets/hal/TARGET_NXP/TARGET_LPC43XX/gpio_object.h Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC43XX/gpio_object.h Wed Jun 11 16:00:09 2014 +0100 @@ -16,6 +16,8 @@ #ifndef MBED_GPIO_OBJECT_H #define MBED_GPIO_OBJECT_H +#include "mbed_assert.h" + #ifdef __cplusplus extern "C" { #endif @@ -31,6 +33,7 @@ } gpio_t; static inline void gpio_write(gpio_t *obj, int value) { + MBED_ASSERT(obj->pin != (PinName)NC); if (value) *obj->reg_set = obj->mask; else @@ -38,6 +41,7 @@ } static inline int gpio_read(gpio_t *obj) { + MBED_ASSERT(obj->pin != (PinName)NC); return ((*obj->reg_in & obj->mask) ? 1 : 0); }
--- a/targets/hal/TARGET_NXP/TARGET_LPC43XX/pinmap.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC43XX/pinmap.c Wed Jun 11 16:00:09 2014 +0100 @@ -15,11 +15,12 @@ * * Ported to NXP LPC43XX by Micromint USA <support@micromint.com> */ +#include "mbed_assert.h" #include "pinmap.h" #include "error.h" void pin_function(PinName pin, int function) { - if (pin == (uint32_t)NC) return; + MBED_ASSERT(pin != (PinName)NC); __IO uint32_t *reg = (__IO uint32_t*) MBED_SCU_REG(pin); @@ -28,9 +29,7 @@ } void pin_mode(PinName pin, PinMode mode) { - if (pin == (uint32_t)NC) { return; } - - if (mode == OpenDrain) error("OpenDrain not supported on LPC43XX"); + MBED_ASSERT((pin != (PinName)NC) && (mode == OpenDrain)); __IO uint32_t *reg = (__IO uint32_t*) MBED_SCU_REG(pin); uint32_t tmp = *reg;
--- a/targets/hal/TARGET_NXP/TARGET_LPC43XX/serial_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC43XX/serial_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -192,16 +192,12 @@ } void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits) { - // 0: 1 stop bits, 1: 2 stop bits - if (stop_bits != 1 && stop_bits != 2) { - error("Invalid stop bits specified"); - } + MBED_ASSERT((stop_bits == 1) || (stop_bits == 2)); // 0: 1 stop bits, 1: 2 stop bits + MBED_ASSERT((data_bits > 4) && (data_bits < 9)); // 0: 5 data bits ... 3: 8 data bits + MBED_ASSERT((parity == ParityNone) || (parity == ParityOdd) || (parity == ParityEven) || + (parity == ParityForced1) || (parity == ParityForced0)); + stop_bits -= 1; - - // 0: 5 data bits ... 3: 8 data bits - if (data_bits < 5 || data_bits > 8) { - error("Invalid number of bits (%d) in serial format, should be 5..8", data_bits); - } data_bits -= 5; int parity_enable, parity_select; @@ -212,8 +208,7 @@ case ParityForced1: parity_enable = 1; parity_select = 2; break; case ParityForced0: parity_enable = 1; parity_select = 3; break; default: - error("Invalid serial parity setting"); - return; + break; } obj->uart->LCR = data_bits << 0
--- a/targets/hal/TARGET_NXP/TARGET_LPC43XX/spi_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC43XX/spi_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -15,6 +15,7 @@ * * Ported to NXP LPC43XX by Micromint USA <support@micromint.com> */ +#include "mbed_assert.h" #include <math.h> #include "spi_api.h" @@ -59,9 +60,7 @@ SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel); obj->spi = (LPC_SSP_T*)pinmap_merge(spi_data, spi_cntl); - if ((int)obj->spi == NC) { - error("SPI pinout mapping failed"); - } + MBED_ASSERT((int)obj->spi != NC); // set default format and frequency if (ssel == NC) { @@ -86,12 +85,9 @@ void spi_free(spi_t *obj) {} void spi_format(spi_t *obj, int bits, int mode, int slave) { + MBED_ASSERT(((bits >= 4) && (bits <= 16)) || ((mode >= 0) && (mode <= 3))); ssp_disable(obj); - if (!(bits >= 4 && bits <= 16) || !(mode >= 0 && mode <= 3)) { - error("SPI format error"); - } - int polarity = (mode & 0x2) ? 1 : 0; int phase = (mode & 0x1) ? 1 : 0;
--- a/targets/hal/TARGET_NXP/TARGET_LPC81X/gpio_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC81X/gpio_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "mbed_assert.h" #include "gpio_api.h" #include "pinmap.h" @@ -40,9 +41,10 @@ } void gpio_init(gpio_t *obj, PinName pin) { - if(pin == NC) return; - obj->pin = pin; + if (pin == (PinName)NC) + return; + obj->mask = gpio_set(pin); obj->reg_set = &LPC_GPIO_PORT->SET0; @@ -56,8 +58,13 @@ } void gpio_dir(gpio_t *obj, PinDirection direction) { + MBED_ASSERT(obj->pin != (PinName)NC); switch (direction) { - case PIN_INPUT : *obj->reg_dir &= ~obj->mask; break; - case PIN_OUTPUT: *obj->reg_dir |= obj->mask; break; + case PIN_INPUT : + *obj->reg_dir &= ~obj->mask; + break; + case PIN_OUTPUT: + *obj->reg_dir |= obj->mask; + break; } }
--- a/targets/hal/TARGET_NXP/TARGET_LPC81X/gpio_object.h Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC81X/gpio_object.h Wed Jun 11 16:00:09 2014 +0100 @@ -16,6 +16,8 @@ #ifndef MBED_GPIO_OBJECT_H #define MBED_GPIO_OBJECT_H +#include "mbed_assert.h" + #ifdef __cplusplus extern "C" { #endif @@ -31,6 +33,7 @@ } gpio_t; static inline void gpio_write(gpio_t *obj, int value) { + MBED_ASSERT(obj->pin != (PinName)NC); if (value) *obj->reg_set = obj->mask; else @@ -38,6 +41,7 @@ } static inline int gpio_read(gpio_t *obj) { + MBED_ASSERT(obj->pin != (PinName)NC); return ((*obj->reg_in & obj->mask) ? 1 : 0); }
--- a/targets/hal/TARGET_NXP/TARGET_LPC81X/i2c_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC81X/i2c_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -16,7 +16,6 @@ #include "i2c_api.h" #include "cmsis.h" #include "pinmap.h" -#include "error.h" static const SWM_Map SWM_I2C_SDA[] = { {7, 24}, @@ -50,7 +49,7 @@ } static inline void i2c_power_enable(i2c_t *obj) { - LPC_SYSCON->SYSAHBCLKCTRL |= (1<<5); + LPC_SYSCON->SYSAHBCLKCTRL |= (1<<5); LPC_SYSCON->PRESETCTRL &= ~(0x1<<6); LPC_SYSCON->PRESETCTRL |= (0x1<<6); }
--- a/targets/hal/TARGET_NXP/TARGET_LPC81X/pinmap.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC81X/pinmap.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "mbed_assert.h" #include "pinmap.h" #include "error.h" @@ -30,8 +31,8 @@ } void pin_mode(PinName pin, PinMode mode) { - if (pin == (uint32_t)NC) { return; } - + MBED_ASSERT(pin != (PinName)NC); + if ((pin == 10) || (pin == 11)) { // True open-drain pins can be configured for different I2C-bus speeds return;
--- a/targets/hal/TARGET_NXP/TARGET_LPC81X/serial_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC81X/serial_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -14,6 +14,7 @@ * limitations under the License. */ // math.h required for floating point operations for baud rate calculation +#include "mbed_assert.h" #include <math.h> #include <string.h> @@ -180,15 +181,10 @@ void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits) { // 0: 1 stop bits, 1: 2 stop bits - if (stop_bits != 1 && stop_bits != 2) { - error("Invalid stop bits specified"); - } + MBED_ASSERT((stop_bits == 1) || (stop_bits == 2)); + MBED_ASSERT((data_bits > 6) && (data_bits < 10)); // 0: 7 data bits ... 2: 9 data bits + MBED_ASSERT((parity == ParityNone) || (parity == ParityEven) || (parity == ParityOdd)); stop_bits -= 1; - - // 0: 7 data bits ... 2: 9 data bits - if (data_bits < 7 || data_bits > 9) { - error("Invalid number of bits (%d) in serial format, should be 7..9", data_bits); - } data_bits -= 7; int paritysel; @@ -197,8 +193,7 @@ case ParityEven: paritysel = 2; break; case ParityOdd : paritysel = 3; break; default: - error("Invalid serial parity setting"); - return; + break; } obj->uart->CFG = (data_bits << 2) @@ -296,7 +291,7 @@ uint32_t regVal_rts, regVal_cts; swm_rts = &SWM_UART_RTS[obj->index]; - swm_cts = &SWM_UART_CTS[obj->index]; + swm_cts = &SWM_UART_CTS[obj->index]; regVal_rts = LPC_SWM->PINASSIGN[swm_rts->n] & ~(0xFF << swm_rts->offset); regVal_cts = LPC_SWM->PINASSIGN[swm_cts->n] & ~(0xFF << swm_cts->offset); @@ -310,15 +305,15 @@ LPC_SWM->PINASSIGN[swm_rts->n] = regVal_rts | (rxflow << swm_rts->offset); if (FlowControlRTS == type) { LPC_SWM->PINASSIGN[swm_cts->n] = regVal_cts | (0xFF << swm_cts->offset); - obj->uart->CFG &= ~CTSEN; + obj->uart->CFG &= ~CTSEN; } } if ((FlowControlCTS == type || FlowControlRTSCTS == type) && (txflow != NC)) { LPC_SWM->PINASSIGN[swm_cts->n] = regVal_cts | (txflow << swm_cts->offset); obj->uart->CFG |= CTSEN; if (FlowControlCTS == type) { - LPC_SWM->PINASSIGN[swm_rts->n] = regVal_rts | (0xFF << swm_rts->offset); + LPC_SWM->PINASSIGN[swm_rts->n] = regVal_rts | (0xFF << swm_rts->offset); } - } + } }
--- a/targets/hal/TARGET_NXP/TARGET_LPC81X/spi_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_NXP/TARGET_LPC81X/spi_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "mbed_assert.h" #include <math.h> #include "spi_api.h" @@ -115,13 +116,9 @@ void spi_free(spi_t *obj) {} void spi_format(spi_t *obj, int bits, int mode, int slave) { + MBED_ASSERT(((bits >= 1) && (bits <= 16)) && ((mode >= 0) && (mode <= 3))); ssp_disable(obj); - if (!(bits >= 1 && bits <= 16) || !(mode >= 0 && mode <= 3)) { - error("SPI format error"); - } - - int polarity = (mode & 0x2) ? 1 : 0; int phase = (mode & 0x1) ? 1 : 0;
--- a/targets/hal/TARGET_STM/TARGET_DISCO_F051R8/analogin_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_DISCO_F051R8/analogin_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -25,6 +25,7 @@ * 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. */ +#include "mbed_assert.h" #include "analogin_api.h" #include "wait_api.h" @@ -32,7 +33,6 @@ #include "cmsis.h" #include "pinmap.h" -#include "error.h" static const PinMap PinMap_ADC[] = { {PA_0, ADC_1, STM_PIN_DATA(GPIO_Mode_AN, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0xFF)}, // ADC_IN0 @@ -53,10 +53,7 @@ // Get the peripheral name (ADC_1, ADC_2...) from the pin and assign it to the object obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC); - - if (obj->adc == (ADCName)NC) { - error("ADC pin mapping failed"); - } + MBED_ASSERT(obj->adc != (ADCName)NC); // Configure GPIO pinmap_pinout(pin, PinMap_ADC); @@ -76,7 +73,7 @@ // Configure ADC ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; - ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; + ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_TRGO; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
--- a/targets/hal/TARGET_STM/TARGET_DISCO_F051R8/gpio_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_DISCO_F051R8/gpio_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -27,22 +27,24 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************************************************************* */ +#include "mbed_assert.h" #include "gpio_api.h" #include "pinmap.h" #include "error.h" extern uint32_t Set_GPIO_Clock(uint32_t port_idx); -uint32_t gpio_set(PinName pin) { - if (pin == NC) return 0; +uint32_t gpio_set(PinName pin) { + MBED_ASSERT(pin != (PinName)NC); pin_function(pin, STM_PIN_DATA(GPIO_Mode_IN, 0, GPIO_PuPd_NOPULL, 0xFF)); - return (uint32_t)(1 << ((uint32_t)pin & 0xF)); // Return the pin mask } void gpio_init(gpio_t *obj, PinName pin) { - if (pin == NC) return; + obj->pin = pin; + if (pin == (PinName)NC) + return; uint32_t port_index = STM_PORT(pin); @@ -51,7 +53,6 @@ GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add; // Fill GPIO object structure for future use - obj->pin = pin; obj->mask = gpio_set(pin); obj->reg_in = &gpio->IDR; obj->reg_set = &gpio->BSRR; @@ -63,6 +64,7 @@ } 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, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0xFF)); }
--- a/targets/hal/TARGET_STM/TARGET_DISCO_F051R8/gpio_object.h Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_DISCO_F051R8/gpio_object.h Wed Jun 11 16:00:09 2014 +0100 @@ -30,6 +30,7 @@ #ifndef MBED_GPIO_OBJECT_H #define MBED_GPIO_OBJECT_H +#include "mbed_assert.h" #include "cmsis.h" #include "PortNames.h" #include "PeripheralNames.h" @@ -48,6 +49,7 @@ } gpio_t; static inline void gpio_write(gpio_t *obj, int value) { + MBED_ASSERT(obj->pin != (PinName)NC); if (value) { *obj->reg_set = obj->mask; } @@ -57,6 +59,7 @@ } static inline int gpio_read(gpio_t *obj) { + MBED_ASSERT(obj->pin != (PinName)NC); return ((*obj->reg_in & obj->mask) ? 1 : 0); }
--- a/targets/hal/TARGET_STM/TARGET_DISCO_F051R8/i2c_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_DISCO_F051R8/i2c_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -27,17 +27,17 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************************************************************* */ +#include "mbed_assert.h" #include "i2c_api.h" #if DEVICE_I2C #include "cmsis.h" #include "pinmap.h" -#include "error.h" /* Timeout values for flags and events waiting loops. These timeouts are - not based on accurate values, they just guarantee that the application will - not remain stuck if the I2C communication is corrupted. */ + not based on accurate values, they just guarantee that the application will + not remain stuck if the I2C communication is corrupted. */ #define FLAG_TIMEOUT ((int)0x1000) #define LONG_TIMEOUT ((int)0x8000) @@ -51,19 +51,16 @@ {NC, NC, 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); obj->i2c = (I2CName)pinmap_merge(i2c_sda, i2c_scl); - - if (obj->i2c == (I2CName)NC) { - error("I2C pin mapping failed"); - } + MBED_ASSERT(obj->i2c != (I2CName)NC); // Enable I2C clock - if (obj->i2c == I2C_1) { + if (obj->i2c == I2C_1) { RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE); } //if (obj->i2c == I2C_2) { @@ -80,10 +77,11 @@ i2c_reset(obj); // I2C configuration - i2c_frequency(obj, 100000); // 100 kHz per default + i2c_frequency(obj, 100000); // 100 kHz per default } void i2c_frequency(i2c_t *obj, int hz) { + MBED_ASSERT((hz == 100000) || (hz == 200000) || (hz == 400000)); //"Only 100kHz, 200kHz and 400kHz I2C frequencies are supported." I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); I2C_InitTypeDef I2C_InitStructure; uint32_t tim = 0; @@ -101,7 +99,6 @@ tim = 0x0010020A; // Fast mode break; default: - error("Only 100kHz, 200kHz and 400kHz I2C frequencies are supported."); break; } @@ -212,7 +209,7 @@ int timeout; // Wait until the byte is received - timeout = FLAG_TIMEOUT; + timeout = FLAG_TIMEOUT; while (I2C_GetFlagStatus(i2c, I2C_ISR_RXNE) == RESET) { timeout--; if (timeout == 0) { @@ -244,13 +241,13 @@ } void i2c_reset(i2c_t *obj) { - if (obj->i2c == I2C_1) { + if (obj->i2c == I2C_1) { RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, ENABLE); RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, DISABLE); } //if (obj->i2c == I2C_2) { // RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, ENABLE); - // RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, DISABLE); + // RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, DISABLE); //} }
--- a/targets/hal/TARGET_STM/TARGET_DISCO_F051R8/pinmap.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_DISCO_F051R8/pinmap.c Wed Jun 11 16:00:09 2014 +0100 @@ -27,6 +27,7 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************************************************************* */ +#include "mbed_assert.h" #include "device.h" #include "pinmap.h" #include "error.h" @@ -66,8 +67,7 @@ * Configure pin (mode, speed, output type and pull-up/pull-down) */ void pin_function(PinName pin, int data) { - if (pin == NC) return; - + MBED_ASSERT(pin != (PinName)NC); // Get the pin informations uint32_t mode = STM_PIN_MODE(data); uint32_t otype = STM_PIN_OTYPE(data); @@ -104,15 +104,14 @@ //} //if ((pin == PA_15) || (pin == PB_3) || (pin == PB_4)) { // - //} + //} } /** * Configure pin pull-up/pull-down */ void pin_mode(PinName pin, PinMode mode) { - if (pin == NC) return; - + MBED_ASSERT(pin != (PinName)NC); uint32_t port_index = STM_PORT(pin); uint32_t pin_index = STM_PIN(pin); @@ -122,7 +121,8 @@ // Configure pull-up/pull-down resistors uint32_t pupd = (uint32_t)mode; - if (pupd > 2) pupd = 0; // Open-drain = No pull-up/No pull-down + if (pupd > 2) + pupd = 0; // Open-drain = No pull-up/No pull-down gpio->PUPDR &= (uint32_t)(~(GPIO_PUPDR_PUPDR0 << (pin_index * 2))); gpio->PUPDR |= (uint32_t)(pupd << (pin_index * 2));
--- a/targets/hal/TARGET_STM/TARGET_DISCO_F051R8/serial_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_DISCO_F051R8/serial_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -27,10 +27,10 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************************************************************* */ +#include "mbed_assert.h" #include "serial_api.h" #include "cmsis.h" #include "pinmap.h" -#include "error.h" #include <string.h> static const PinMap PinMap_UART_TX[] = { @@ -71,24 +71,21 @@ 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); // Get the peripheral name (UART_1, UART_2, ...) from the pin and assign it to the object obj->uart = (UARTName)pinmap_merge(uart_tx, uart_rx); - - if (obj->uart == (UARTName)NC) { - error("Serial pinout mapping failed"); - } + MBED_ASSERT(obj->uart != (UARTName)NC); // Enable USART clock if (obj->uart == UART_1) { - RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); + RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); } if (obj->uart == UART_2) { - RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); + RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); } // Configure the UART pins @@ -101,7 +98,7 @@ obj->baudrate = 9600; obj->databits = USART_WordLength_8b; obj->stopbits = USART_StopBits_1; - obj->parity = USART_Parity_No; + obj->parity = USART_Parity_No; init_usart(obj); @@ -140,7 +137,7 @@ obj->parity = USART_Parity_Odd; break; case ParityEven: - case ParityForced1: + case ParityForced1: obj->parity = USART_Parity_Even; break; default: // ParityNone @@ -206,7 +203,7 @@ } else { // TxIrq USART_ITConfig(usart, USART_IT_TC, ENABLE); - } + } NVIC_SetVector(irq_n, vector); NVIC_EnableIRQ(irq_n); @@ -223,12 +220,12 @@ else { // TxIrq USART_ITConfig(usart, USART_IT_TXE, DISABLE); // 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; } if (all_disabled) NVIC_DisableIRQ(irq_n); - } + } } /******************************************************************************
--- a/targets/hal/TARGET_STM/TARGET_DISCO_F051R8/spi_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_DISCO_F051R8/spi_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -27,6 +27,7 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************************************************************* */ +#include "mbed_assert.h" #include "spi_api.h" #if DEVICE_SPI @@ -34,7 +35,6 @@ #include <math.h> #include "cmsis.h" #include "pinmap.h" -#include "error.h" static const PinMap PinMap_SPI_MOSI[] = { {PA_7, SPI_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_NOPULL, GPIO_AF_0)}, @@ -64,11 +64,11 @@ SPI_Cmd(spi, DISABLE); SPI_InitStructure.SPI_Mode = obj->mode; - SPI_InitStructure.SPI_NSS = obj->nss; - SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; + SPI_InitStructure.SPI_NSS = obj->nss; + SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; SPI_InitStructure.SPI_DataSize = obj->bits; SPI_InitStructure.SPI_CPOL = obj->cpol; - SPI_InitStructure.SPI_CPHA = obj->cpha; + SPI_InitStructure.SPI_CPHA = obj->cpha; SPI_InitStructure.SPI_BaudRatePrescaler = obj->br_presc; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 7; @@ -90,17 +90,14 @@ SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel); obj->spi = (SPIName)pinmap_merge(spi_data, spi_cntl); - - if (obj->spi == (SPIName)NC) { - error("SPI pinout mapping failed"); - } + MBED_ASSERT(obj->spi != (SPIName)NC); // Enable SPI clock if (obj->spi == SPI_1) { - RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE); + RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE); } if (obj->spi == SPI_2) { - RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE); + RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE); } // Configure the SPI pins @@ -132,7 +129,7 @@ SPI_I2S_DeInit(spi); } -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 == 8) { obj->bits = SPI_DataSize_8b; @@ -152,11 +149,11 @@ break; case 2: obj->cpol = SPI_CPOL_High; - obj->cpha = SPI_CPHA_1Edge; + obj->cpha = SPI_CPHA_1Edge; break; default: obj->cpol = SPI_CPOL_High; - obj->cpha = SPI_CPHA_2Edge; + obj->cpha = SPI_CPHA_2Edge; break; } @@ -166,7 +163,7 @@ } else { obj->mode = SPI_Mode_Slave; - obj->nss = SPI_NSS_Hard; + obj->nss = SPI_NSS_Hard; } init_spi(obj); @@ -201,7 +198,7 @@ SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); // Check if data is received status = ((SPI_I2S_GetFlagStatus(spi, SPI_I2S_FLAG_RXNE) != RESET) ? 1 : 0); - return status; + return status; } static inline int ssp_writeable(spi_t *obj) { @@ -213,7 +210,7 @@ } static inline void ssp_write(spi_t *obj, int value) { - SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); + SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); while (!ssp_writeable(obj)); if(obj->bits == SPI_DataSize_8b) // 8 bit mode SPI_SendData8(spi, (uint8_t)value); @@ -222,12 +219,12 @@ } static inline int ssp_read(spi_t *obj) { - SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); + SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); while (!ssp_readable(obj)); if(obj->bits == SPI_DataSize_8b) // 8 bit mode return (int)SPI_ReceiveData8(spi); else // 16 bit mode - return (int)SPI_I2S_ReceiveData16(spi); + return (int)SPI_I2S_ReceiveData16(spi); } static inline int ssp_busy(spi_t *obj) { @@ -250,16 +247,16 @@ SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); if(obj->bits == SPI_DataSize_8b) // 8 bit mode return (int)SPI_ReceiveData8(spi); - else - return (int)SPI_I2S_ReceiveData16(spi); + else + return (int)SPI_I2S_ReceiveData16(spi); } void spi_slave_write(spi_t *obj, int value) { - SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); - while (!ssp_writeable(obj)); + SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); + while (!ssp_writeable(obj)); if(obj->bits == SPI_DataSize_8b) // 8 bit mode SPI_SendData8(spi, (uint8_t)value); - else + else SPI_I2S_SendData16(spi, (uint16_t)value); }
--- a/targets/hal/TARGET_STM/TARGET_DISCO_F100RB/analogin_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_DISCO_F100RB/analogin_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -25,6 +25,7 @@ * 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. */ +#include "mbed_assert.h" #include "analogin_api.h" #include "wait_api.h" @@ -32,7 +33,6 @@ #include "cmsis.h" #include "pinmap.h" -#include "error.h" static const PinMap PinMap_ADC[] = { {PA_0, ADC_1, STM_PIN_DATA(GPIO_Mode_AIN, 0)}, @@ -53,10 +53,7 @@ // Get the peripheral name (ADC_1, ADC_2...) from the pin and assign it to the object obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC); - - if (obj->adc == (ADCName)NC) { - error("ADC pin mapping failed"); - } + MBED_ASSERT(obj->adc != (ADCName)NC); // Configure GPIO pinmap_pinout(pin, PinMap_ADC); @@ -91,7 +88,7 @@ ADC_ResetCalibration(adc); while(ADC_GetResetCalibrationStatus(adc)); ADC_StartCalibration(adc); - while(ADC_GetCalibrationStatus(adc)); + while(ADC_GetCalibrationStatus(adc)); } }
--- a/targets/hal/TARGET_STM/TARGET_DISCO_F100RB/gpio_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_DISCO_F100RB/gpio_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -27,22 +27,24 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************************************************************* */ +#include "mbed_assert.h" #include "gpio_api.h" #include "pinmap.h" #include "error.h" extern uint32_t Set_GPIO_Clock(uint32_t port_idx); -uint32_t gpio_set(PinName pin) { - if (pin == NC) return 0; +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) { - if (pin == NC) return; + obj->pin = pin; + if (pin == (PinName)NC) + return; uint32_t port_index = STM_PORT(pin); @@ -51,7 +53,6 @@ GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add; // Fill GPIO object structure for future use - obj->pin = pin; obj->mask = gpio_set(pin); obj->reg_in = &gpio->IDR; obj->reg_set = &gpio->BSRR; @@ -63,6 +64,7 @@ } 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)); }
--- a/targets/hal/TARGET_STM/TARGET_DISCO_F100RB/gpio_object.h Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_DISCO_F100RB/gpio_object.h Wed Jun 11 16:00:09 2014 +0100 @@ -30,6 +30,7 @@ #ifndef MBED_GPIO_OBJECT_H #define MBED_GPIO_OBJECT_H +#include "mbed_assert.h" #include "cmsis.h" #include "PortNames.h" #include "PeripheralNames.h" @@ -48,6 +49,7 @@ } gpio_t; static inline void gpio_write(gpio_t *obj, int value) { + MBED_ASSERT(obj->pin != (PinName)NC); if (value) { *obj->reg_set = obj->mask; } @@ -57,6 +59,7 @@ } static inline int gpio_read(gpio_t *obj) { + MBED_ASSERT(obj->pin != (PinName)NC); return ((*obj->reg_in & obj->mask) ? 1 : 0); }
--- a/targets/hal/TARGET_STM/TARGET_DISCO_F100RB/i2c_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_DISCO_F100RB/i2c_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -27,17 +27,17 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************************************************************* */ +#include "mbed_assert.h" #include "i2c_api.h" #if DEVICE_I2C #include "cmsis.h" #include "pinmap.h" -#include "error.h" /* Timeout values for flags and events waiting loops. These timeouts are - not based on accurate values, they just guarantee that the application will - not remain stuck if the I2C communication is corrupted. */ + not based on accurate values, they just guarantee that the application will + not remain stuck if the I2C communication is corrupted. */ #define FLAG_TIMEOUT ((int)0x1000) #define LONG_TIMEOUT ((int)0x8000) @@ -51,19 +51,16 @@ {NC, NC, 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); obj->i2c = (I2CName)pinmap_merge(i2c_sda, i2c_scl); - - if (obj->i2c == (I2CName)NC) { - error("I2C pin mapping failed"); - } + MBED_ASSERT(obj->i2c != (I2CName)NC); // Enable I2C clock - if (obj->i2c == I2C_1) { + if (obj->i2c == I2C_1) { RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE); } if (obj->i2c == I2C_2) { @@ -80,7 +77,7 @@ i2c_reset(obj); // I2C configuration - i2c_frequency(obj, 100000); // 100 kHz per default + i2c_frequency(obj, 100000); // 100 kHz per default } void i2c_frequency(i2c_t *obj, int hz) { @@ -110,7 +107,7 @@ I2C_ClearFlag(i2c, I2C_FLAG_AF); // Clear Acknowledge failure flag // Generate the START condition - I2C_GenerateSTART(i2c, ENABLE); + I2C_GenerateSTART(i2c, ENABLE); // Wait the START condition has been correctly sent timeout = FLAG_TIMEOUT; @@ -155,7 +152,7 @@ i2c_start(obj); // Send slave address for read - I2C_Send7bitAddress(i2c, address, I2C_Direction_Receiver); + I2C_Send7bitAddress(i2c, address, I2C_Direction_Receiver); // Wait address is acknowledged timeout = FLAG_TIMEOUT; @@ -264,7 +261,7 @@ I2C_SendData(i2c, (uint8_t)data); // 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) && (I2C_GetFlagStatus(i2c, I2C_FLAG_BTF) == RESET)) { @@ -278,13 +275,13 @@ } void i2c_reset(i2c_t *obj) { - if (obj->i2c == I2C_1) { + if (obj->i2c == I2C_1) { RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, ENABLE); RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, DISABLE); } if (obj->i2c == I2C_2) { RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, ENABLE); - RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, DISABLE); + RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, DISABLE); } }
--- a/targets/hal/TARGET_STM/TARGET_DISCO_F100RB/pinmap.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_DISCO_F100RB/pinmap.c Wed Jun 11 16:00:09 2014 +0100 @@ -27,6 +27,7 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************************************************************* */ +#include "mbed_assert.h" #include "device.h" #include "pinmap.h" #include "error.h" @@ -75,8 +76,7 @@ * Configure pin (input, output, alternate function or analog) + output speed + AF */ void pin_function(PinName pin, int data) { - if (pin == NC) return; - + MBED_ASSERT(pin != (PinName)NC); // Get the pin informations uint32_t mode = STM_PIN_MODE(data); uint32_t afnum = STM_PIN_AFNUM(data); @@ -111,17 +111,16 @@ } if ((pin == PA_15) || (pin == PB_3) || (pin == PB_4)) { GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE); - } + } } /** * Configure pin pull-up/pull-down */ void pin_mode(PinName pin, PinMode mode) { + MBED_ASSERT(pin != (PinName)NC); GPIO_InitTypeDef GPIO_InitStructure; - if (pin == NC) return; - uint32_t port_index = STM_PORT(pin); uint32_t pin_index = STM_PIN(pin); @@ -131,32 +130,31 @@ // Configure open-drain and pull-up/down switch (mode) { - case PullNone: - return; - case PullUp: - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; - break; - case PullDown: - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; - break; - case OpenDrain: - if (pin_index < 8) { - if ((gpio->CRL & (0x03 << (pin_index * 4))) > 0) { // MODE bits = Output mode - gpio->CRL |= (0x04 << (pin_index * 4)); // Set open-drain + case PullNone: + return; + case PullUp: + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; + break; + case PullDown: + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; + break; + case OpenDrain: + if (pin_index < 8) { + if ((gpio->CRL & (0x03 << (pin_index * 4))) > 0) { // MODE bits = Output mode + gpio->CRL |= (0x04 << (pin_index * 4)); // Set open-drain + } + } else { + if ((gpio->CRH & (0x03 << ((pin_index % 8) * 4))) > 0) { // MODE bits = Output mode + gpio->CRH |= (0x04 << ((pin_index % 8) * 4)); // Set open-drain + } } - } - else { - if ((gpio->CRH & (0x03 << ((pin_index % 8) * 4))) > 0) { // MODE bits = Output mode - gpio->CRH |= (0x04 << ((pin_index % 8) * 4)); // Set open-drain - } - } - return; - default: - break; + return; + default: + break; } // Configure GPIO GPIO_InitStructure.GPIO_Pin = (uint16_t)(1 << pin_index); GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; - GPIO_Init(gpio, &GPIO_InitStructure); + GPIO_Init(gpio, &GPIO_InitStructure); }
--- a/targets/hal/TARGET_STM/TARGET_DISCO_F100RB/pwmout_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_DISCO_F100RB/pwmout_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -27,11 +27,11 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************************************************************* */ +#include "mbed_assert.h" #include "pwmout_api.h" #include "cmsis.h" #include "pinmap.h" -#include "error.h" static const PinMap PinMap_PWM[] = { // TIM2 full remap @@ -46,10 +46,7 @@ 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); - - if (obj->pwm == (PWMName)NC) { - error("PWM pinout mapping failed"); - } + MBED_ASSERT(obj->pwm != (PWMName)NC); // Enable TIM clock if (obj->pwm == PWM_2) RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
--- a/targets/hal/TARGET_STM/TARGET_DISCO_F100RB/serial_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_DISCO_F100RB/serial_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -27,10 +27,10 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************************************************************* */ +#include "mbed_assert.h" #include "serial_api.h" #include "cmsis.h" #include "pinmap.h" -#include "error.h" #include <string.h> static const PinMap PinMap_UART_TX[] = { @@ -73,21 +73,18 @@ 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); // Get the peripheral name (UART_1, UART_2, ...) from the pin and assign it to the object obj->uart = (UARTName)pinmap_merge(uart_tx, uart_rx); - - if (obj->uart == (UARTName)NC) { - error("Serial pinout mapping failed"); - } + MBED_ASSERT(obj->uart != (UARTName)NC); // Enable USART clock if (obj->uart == UART_1) { - RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); + RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); } else if (obj->uart == UART_2 ) { RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); } else if (obj->uart == UART_3 ) { @@ -102,7 +99,7 @@ obj->baudrate = 9600; obj->databits = USART_WordLength_8b; obj->stopbits = USART_StopBits_1; - obj->parity = USART_Parity_No; + obj->parity = USART_Parity_No; init_usart(obj); @@ -142,7 +139,7 @@ obj->parity = USART_Parity_Odd; break; case ParityEven: - case ParityForced1: + case ParityForced1: obj->parity = USART_Parity_Even; break; default: // ParityNone @@ -214,7 +211,7 @@ } else { // TxIrq USART_ITConfig(usart, USART_IT_TC, ENABLE); - } + } NVIC_SetVector(irq_n, vector); NVIC_EnableIRQ(irq_n); @@ -231,12 +228,12 @@ else { // TxIrq USART_ITConfig(usart, USART_IT_TXE, DISABLE); // 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; } if (all_disabled) NVIC_DisableIRQ(irq_n); - } + } } /******************************************************************************
--- a/targets/hal/TARGET_STM/TARGET_DISCO_F100RB/spi_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_DISCO_F100RB/spi_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -27,6 +27,7 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************************************************************* */ +#include "mbed_assert.h" #include "spi_api.h" #if DEVICE_SPI @@ -34,7 +35,6 @@ #include <math.h> #include "cmsis.h" #include "pinmap.h" -#include "error.h" static const PinMap PinMap_SPI_MOSI[] = { {PA_7, SPI_1, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)}, @@ -67,11 +67,11 @@ SPI_Cmd(spi, DISABLE); SPI_InitStructure.SPI_Mode = obj->mode; - SPI_InitStructure.SPI_NSS = obj->nss; - SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; + SPI_InitStructure.SPI_NSS = obj->nss; + SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; SPI_InitStructure.SPI_DataSize = obj->bits; SPI_InitStructure.SPI_CPOL = obj->cpol; - SPI_InitStructure.SPI_CPHA = obj->cpha; + SPI_InitStructure.SPI_CPHA = obj->cpha; SPI_InitStructure.SPI_BaudRatePrescaler = obj->br_presc; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 7; @@ -91,14 +91,11 @@ SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel); obj->spi = (SPIName)pinmap_merge(spi_data, spi_cntl); - - if (obj->spi == (SPIName)NC) { - error("SPI pinout mapping failed"); - } + MBED_ASSERT(obj->spi != (SPIName)NC); // Enable SPI clock if (obj->spi == SPI_1) { - RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE); + RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE); } // Configure the SPI pins @@ -130,7 +127,7 @@ SPI_I2S_DeInit(spi); } -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 == 8) { obj->bits = SPI_DataSize_8b; @@ -150,11 +147,11 @@ break; case 2: obj->cpol = SPI_CPOL_High; - obj->cpha = SPI_CPHA_1Edge; + obj->cpha = SPI_CPHA_1Edge; break; default: obj->cpol = SPI_CPOL_High; - obj->cpha = SPI_CPHA_2Edge; + obj->cpha = SPI_CPHA_2Edge; break; } @@ -164,7 +161,7 @@ } else { obj->mode = SPI_Mode_Slave; - obj->nss = SPI_NSS_Hard; + obj->nss = SPI_NSS_Hard; } init_spi(obj); @@ -196,7 +193,7 @@ SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); // Check if data is received status = ((SPI_I2S_GetFlagStatus(spi, SPI_I2S_FLAG_RXNE) != RESET) ? 1 : 0); - return status; + return status; } static inline int ssp_writeable(spi_t *obj) { @@ -208,13 +205,13 @@ } static inline void ssp_write(spi_t *obj, int value) { - SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); + 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) { - SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); + SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); while (!ssp_readable(obj)); return (int)SPI_I2S_ReceiveData(spi); } @@ -241,8 +238,8 @@ } void spi_slave_write(spi_t *obj, int value) { - SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); - while (!ssp_writeable(obj)); + SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); + while (!ssp_writeable(obj)); SPI_I2S_SendData(spi, (uint16_t)value); }
--- a/targets/hal/TARGET_STM/TARGET_DISCO_F303VC/analogin_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_DISCO_F303VC/analogin_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -25,6 +25,7 @@ * 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. */ +#include "mbed_assert.h" #include "analogin_api.h" #include "wait_api.h" @@ -32,7 +33,6 @@ #include "cmsis.h" #include "pinmap.h" -#include "error.h" static const PinMap PinMap_ADC[] = { {PA_0, ADC_1, STM_PIN_DATA(GPIO_Mode_AN, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0xFF)}, // ADC_IN1 @@ -63,10 +63,7 @@ // Get the peripheral name from the pin and assign it to the object obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC); - - if (obj->adc == (ADCName)NC) { - error("ADC pin mapping failed"); - } + MBED_ASSERT(obj->adc != (ADCName)NC); // Configure GPIO pinmap_pinout(pin, PinMap_ADC); @@ -168,7 +165,7 @@ break; case PA_7: channel = ADC_Channel_15; - break; + break; default: return 0; }
--- a/targets/hal/TARGET_STM/TARGET_DISCO_F303VC/gpio_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_DISCO_F303VC/gpio_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -27,31 +27,32 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************************************************************* */ +#include "mbed_assert.h" #include "gpio_api.h" #include "pinmap.h" #include "error.h" extern uint32_t Set_GPIO_Clock(uint32_t port_idx); -uint32_t gpio_set(PinName pin) { - if (pin == NC) return 0; +uint32_t gpio_set(PinName pin) { + MBED_ASSERT(pin != (PinName)NC); pin_function(pin, STM_PIN_DATA(GPIO_Mode_IN, 0, GPIO_PuPd_NOPULL, 0xFF)); - return (uint32_t)(1 << ((uint32_t)pin & 0xF)); // Return the pin mask } void gpio_init(gpio_t *obj, PinName pin) { - if (pin == NC) return; + obj->pin = pin; + if (pin == (PinName)NC) + return; uint32_t port_index = STM_PORT(pin); - + // Enable GPIO clock uint32_t gpio_add = Set_GPIO_Clock(port_index); GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add; - + // Fill GPIO object structure for future use - obj->pin = pin; obj->mask = gpio_set(pin); obj->reg_in = &gpio->IDR; obj->reg_set = &gpio->BSRR; @@ -63,6 +64,7 @@ } 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, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0xFF)); } else { // PIN_INPUT
--- a/targets/hal/TARGET_STM/TARGET_DISCO_F303VC/gpio_object.h Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_DISCO_F303VC/gpio_object.h Wed Jun 11 16:00:09 2014 +0100 @@ -30,6 +30,7 @@ #ifndef MBED_GPIO_OBJECT_H #define MBED_GPIO_OBJECT_H +#include "mbed_assert.h" #include "cmsis.h" #include "PortNames.h" #include "PeripheralNames.h" @@ -48,6 +49,7 @@ } gpio_t; static inline void gpio_write(gpio_t *obj, int value) { + MBED_ASSERT(obj->pin != (PinName)NC); if (value) { *obj->reg_set = obj->mask; } else { @@ -56,6 +58,7 @@ } static inline int gpio_read(gpio_t *obj) { + MBED_ASSERT(obj->pin != (PinName)NC); return ((*obj->reg_in & obj->mask) ? 1 : 0); }
--- a/targets/hal/TARGET_STM/TARGET_DISCO_F303VC/i2c_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_DISCO_F303VC/i2c_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -27,17 +27,17 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************************************************************* */ +#include "mbed_assert.h" #include "i2c_api.h" #if DEVICE_I2C #include "cmsis.h" #include "pinmap.h" -#include "error.h" /* Timeout values for flags and events waiting loops. These timeouts are - not based on accurate values, they just guarantee that the application will - not remain stuck if the I2C communication is corrupted. */ + not based on accurate values, they just guarantee that the application will + not remain stuck if the I2C communication is corrupted. */ #define FLAG_TIMEOUT ((int)0x1000) #define LONG_TIMEOUT ((int)0x8000) @@ -62,19 +62,16 @@ {NC, NC, 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); obj->i2c = (I2CName)pinmap_merge(i2c_sda, i2c_scl); - - if (obj->i2c == (I2CName)NC) { - error("I2C pin mapping failed"); - } + MBED_ASSERT(obj->i2c != (I2CName)NC); // Enable I2C clock - if (obj->i2c == I2C_1) { + if (obj->i2c == I2C_1) { RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE); } if (obj->i2c == I2C_2) { @@ -94,10 +91,11 @@ i2c_reset(obj); // I2C configuration - i2c_frequency(obj, 100000); // 100 kHz per default + i2c_frequency(obj, 100000); // 100 kHz per default } void i2c_frequency(i2c_t *obj, int hz) { + MBED_ASSERT((hz == 100000) || (hz == 200000) || (hz == 400000) || (hz == 1000000)); I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); I2C_InitTypeDef I2C_InitStructure; uint32_t tim; @@ -140,7 +138,6 @@ } break; default: - error("Only 100kHz, 200kHz, 400kHz and 1MHz I2C frequencies are supported."); break; } @@ -252,7 +249,7 @@ int timeout; // Wait until the byte is received - timeout = FLAG_TIMEOUT; + timeout = FLAG_TIMEOUT; while (I2C_GetFlagStatus(i2c, I2C_ISR_RXNE) == RESET) { timeout--; if (timeout == 0) { @@ -284,7 +281,7 @@ } void i2c_reset(i2c_t *obj) { - if (obj->i2c == I2C_1) { + if (obj->i2c == I2C_1) { RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, ENABLE); RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, DISABLE); } @@ -294,7 +291,7 @@ } if (obj->i2c == I2C_3) { RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C3, ENABLE); - RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C3, DISABLE); + RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C3, DISABLE); } }
--- a/targets/hal/TARGET_STM/TARGET_DISCO_F303VC/pinmap.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_DISCO_F303VC/pinmap.c Wed Jun 11 16:00:09 2014 +0100 @@ -27,6 +27,7 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************************************************************* */ +#include "mbed_assert.h" #include "device.h" #include "pinmap.h" #include "error.h" @@ -71,7 +72,7 @@ * Configure pin (mode, speed, output type and pull-up/pull-down) */ void pin_function(PinName pin, int data) { - if (pin == NC) return; + MBED_ASSERT(pin != (PinName)NC); // Get the pin informations uint32_t mode = STM_PIN_MODE(data); @@ -108,14 +109,14 @@ //} //if ((pin == PA_15) || (pin == PB_3) || (pin == PB_4)) { // - //} + //} } /** * Configure pin pull-up/pull-down */ void pin_mode(PinName pin, PinMode mode) { - if (pin == NC) return; + MBED_ASSERT(pin != (PinName)NC); uint32_t port_index = STM_PORT(pin); uint32_t pin_index = STM_PIN(pin); @@ -126,7 +127,8 @@ // Configure pull-up/pull-down resistors uint32_t pupd = (uint32_t)mode; - if (pupd > 2) pupd = 0; // Open-drain = No pull-up/No pull-down + if (pupd > 2) + pupd = 0; // Open-drain = No pull-up/No pull-down gpio->PUPDR &= (uint32_t)(~(GPIO_PUPDR_PUPDR0 << (pin_index * 2))); gpio->PUPDR |= (uint32_t)(pupd << (pin_index * 2));
--- a/targets/hal/TARGET_STM/TARGET_DISCO_F303VC/pwmout_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_DISCO_F303VC/pwmout_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -27,11 +27,11 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************************************************************* */ +#include "mbed_assert.h" #include "pwmout_api.h" #include "cmsis.h" #include "pinmap.h" -#include "error.h" // TIM2 cannot be used because already used by the us_ticker static const PinMap PinMap_PWM[] = { @@ -85,13 +85,10 @@ {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); - - if (obj->pwm == (PWMName)NC) { - error("PWM pinout mapping failed"); - } + MBED_ASSERT(obj->pwm != (PWMName)NC); // Enable TIM clock if (obj->pwm == PWM_1) RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); @@ -126,7 +123,7 @@ obj->pulse = (uint32_t)((float)obj->period * value); - // Configure channels + // Configure channels TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_Pulse = obj->pulse; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; @@ -215,7 +212,7 @@ break; default: return; - } + } } float pwmout_read(pwmout_t* obj) { @@ -239,7 +236,7 @@ TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; float dc = pwmout_read(obj); - TIM_Cmd(tim, DISABLE); + TIM_Cmd(tim, DISABLE); obj->period = us;
--- a/targets/hal/TARGET_STM/TARGET_DISCO_F303VC/serial_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_DISCO_F303VC/serial_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -27,10 +27,10 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************************************************************* */ +#include "mbed_assert.h" #include "serial_api.h" #include "cmsis.h" #include "pinmap.h" -#include "error.h" #include <string.h> static const PinMap PinMap_UART_TX[] = { @@ -85,27 +85,24 @@ 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 UARTName uart_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX); UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX); // Get the peripheral name from the pin and assign it to the object obj->uart = (UARTName)pinmap_merge(uart_tx, uart_rx); - - if (obj->uart == (UARTName)NC) { - error("Serial pinout mapping failed"); - } + MBED_ASSERT(obj->uart != (UARTName)NC); // Enable USART clock if (obj->uart == UART_1) { - RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); + RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); } if (obj->uart == UART_2) { - RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); + RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); } if (obj->uart == UART_3) { - RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); + RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); } // Configure the UART pins @@ -118,7 +115,7 @@ obj->baudrate = 9600; obj->databits = USART_WordLength_8b; obj->stopbits = USART_StopBits_1; - obj->parity = USART_Parity_No; + obj->parity = USART_Parity_No; init_usart(obj); @@ -157,7 +154,7 @@ obj->parity = USART_Parity_Odd; break; case ParityEven: - case ParityForced1: + case ParityForced1: obj->parity = USART_Parity_Even; break; default: // ParityNone @@ -233,7 +230,7 @@ USART_ITConfig(usart, USART_IT_RXNE, ENABLE); } else { // TxIrq USART_ITConfig(usart, USART_IT_TC, ENABLE); - } + } NVIC_SetVector(irq_n, vector); NVIC_EnableIRQ(irq_n); @@ -249,12 +246,12 @@ } else { // TxIrq USART_ITConfig(usart, USART_IT_TXE, DISABLE); // 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; } if (all_disabled) NVIC_DisableIRQ(irq_n); - } + } } /******************************************************************************
--- a/targets/hal/TARGET_STM/TARGET_DISCO_F303VC/spi_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_DISCO_F303VC/spi_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -27,6 +27,7 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************************************************************* */ +#include "mbed_assert.h" #include "spi_api.h" #if DEVICE_SPI @@ -34,7 +35,6 @@ #include <math.h> #include "cmsis.h" #include "pinmap.h" -#include "error.h" static const PinMap PinMap_SPI_MOSI[] = { {PA_11, SPI_2, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_DOWN, GPIO_AF_5)}, @@ -75,17 +75,17 @@ SPI_Cmd(spi, DISABLE); SPI_InitStructure.SPI_Mode = obj->mode; - SPI_InitStructure.SPI_NSS = obj->nss; - SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; + SPI_InitStructure.SPI_NSS = obj->nss; + SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; SPI_InitStructure.SPI_DataSize = obj->bits; SPI_InitStructure.SPI_CPOL = obj->cpol; - SPI_InitStructure.SPI_CPHA = obj->cpha; + SPI_InitStructure.SPI_CPHA = obj->cpha; SPI_InitStructure.SPI_BaudRatePrescaler = obj->br_presc; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 7; SPI_Init(spi, &SPI_InitStructure); - SPI_RxFIFOThresholdConfig(spi, SPI_RxFIFOThreshold_QF); + SPI_RxFIFOThresholdConfig(spi, SPI_RxFIFOThreshold_QF); SPI_Cmd(spi, ENABLE); } @@ -101,17 +101,14 @@ SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel); obj->spi = (SPIName)pinmap_merge(spi_data, spi_cntl); - - if (obj->spi == (SPIName)NC) { - error("SPI pinout mapping failed"); - } + MBED_ASSERT(obj->spi != (SPIName)NC); // Enable SPI clock if (obj->spi == SPI_2) { - RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE); + RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE); } if (obj->spi == SPI_3) { - RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI3, ENABLE); + RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI3, ENABLE); } // Configure the SPI pins @@ -142,7 +139,7 @@ SPI_I2S_DeInit(spi); } -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 == 8) { obj->bits = SPI_DataSize_8b; @@ -161,11 +158,11 @@ break; case 2: obj->cpol = SPI_CPOL_High; - obj->cpha = SPI_CPHA_1Edge; + obj->cpha = SPI_CPHA_1Edge; break; default: obj->cpol = SPI_CPOL_High; - obj->cpha = SPI_CPHA_2Edge; + obj->cpha = SPI_CPHA_2Edge; break; } @@ -174,7 +171,7 @@ obj->nss = SPI_NSS_Soft; } else { obj->mode = SPI_Mode_Slave; - obj->nss = SPI_NSS_Hard; + obj->nss = SPI_NSS_Hard; } init_spi(obj); @@ -207,7 +204,7 @@ SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); // Check if data is received status = ((SPI_I2S_GetFlagStatus(spi, SPI_I2S_FLAG_RXNE) != RESET) ? 1 : 0); - return status; + return status; } static inline int ssp_writeable(spi_t *obj) { @@ -219,7 +216,7 @@ } static inline void ssp_write(spi_t *obj, int value) { - SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); + SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); while (!ssp_writeable(obj)); if (obj->bits == SPI_DataSize_8b) { SPI_SendData8(spi, (uint8_t)value); @@ -229,7 +226,7 @@ } static inline int ssp_read(spi_t *obj) { - SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); + SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); while (!ssp_readable(obj)); if (obj->bits == SPI_DataSize_8b) { return (int)SPI_ReceiveData8(spi); @@ -264,8 +261,8 @@ } void spi_slave_write(spi_t *obj, int value) { - SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); - while (!ssp_writeable(obj)); + SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); + while (!ssp_writeable(obj)); if (obj->bits == SPI_DataSize_8b) { SPI_SendData8(spi, (uint8_t)value); } else {
--- a/targets/hal/TARGET_STM/TARGET_DISCO_F407VG/analogin_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_DISCO_F407VG/analogin_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -32,7 +32,6 @@ #include "cmsis.h" #include "pinmap.h" -#include "error.h" #include "stm32f4xx_hal.h" static const PinMap PinMap_ADC[] = { @@ -59,13 +58,10 @@ int adc_inited = 0; -void analogin_init(analogin_t *obj, PinName pin) { +void analogin_init(analogin_t *obj, PinName pin) { // Get the peripheral name (ADC_1, ADC_2...) from the pin and assign it to the object obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC); - - if (obj->adc == (ADCName)NC) { - error("ADC error: pinout mapping failed."); - } + MBED_ASSERT(obj->adc != (ADCName)NC); // Configure GPIO pinmap_pinout(pin, PinMap_ADC); @@ -93,8 +89,8 @@ AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT; AdcHandle.Init.NbrOfConversion = 1; AdcHandle.Init.DMAContinuousRequests = DISABLE; - AdcHandle.Init.EOCSelection = DISABLE; - HAL_ADC_Init(&AdcHandle); + AdcHandle.Init.EOCSelection = DISABLE; + HAL_ADC_Init(&AdcHandle); } } @@ -120,7 +116,7 @@ break; case PA_3: sConfig.Channel = ADC_CHANNEL_3; - break; + break; case PA_4: sConfig.Channel = ADC_CHANNEL_4; break; @@ -132,13 +128,13 @@ break; case PA_7: sConfig.Channel = ADC_CHANNEL_7; - break; + break; case PB_0: sConfig.Channel = ADC_CHANNEL_8; break; case PB_1: sConfig.Channel = ADC_CHANNEL_9; - break; + break; case PC_0: sConfig.Channel = ADC_CHANNEL_10; break; @@ -156,7 +152,7 @@ break; case PC_5: sConfig.Channel = ADC_CHANNEL_15; - break; + break; default: return 0; } @@ -168,7 +164,7 @@ HAL_ADC_PollForConversion(&AdcHandle, 10); // Wait end of conversion if (HAL_ADC_GetState(&AdcHandle) == HAL_ADC_STATE_EOC_REG) - { + { return(HAL_ADC_GetValue(&AdcHandle)); // Get conversion value } else
--- a/targets/hal/TARGET_STM/TARGET_DISCO_F407VG/gpio_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_DISCO_F407VG/gpio_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -27,6 +27,7 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************************************************************* */ +#include "mbed_assert.h" #include "gpio_api.h" #include "pinmap.h" #include "error.h" @@ -34,17 +35,17 @@ extern uint32_t Set_GPIO_Clock(uint32_t port_idx); -uint32_t gpio_set(PinName pin) { - if (pin == NC) return 0; +uint32_t gpio_set(PinName pin) { + MBED_ASSERT(pin != (PinName)NC); pin_function(pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); - return (uint32_t)(1 << ((uint32_t)pin & 0xF)); // Return the pin mask } void gpio_init(gpio_t *obj, PinName pin) { - if (pin == NC) return; - + obj->pin = pin; + if (pin == (PinName)NC) + return; uint32_t port_index = STM_PORT(pin); // Enable GPIO clock @@ -52,7 +53,6 @@ GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add; // Fill GPIO object structure for future use - obj->pin = pin; obj->mask = gpio_set(pin); obj->reg_in = &gpio->IDR; obj->reg_set = &gpio->BSRRL; @@ -64,6 +64,7 @@ } 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(STM_MODE_OUTPUT_PP, GPIO_NOPULL, 0)); }
--- a/targets/hal/TARGET_STM/TARGET_DISCO_F407VG/gpio_object.h Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_DISCO_F407VG/gpio_object.h Wed Jun 11 16:00:09 2014 +0100 @@ -30,6 +30,7 @@ #ifndef MBED_GPIO_OBJECT_H #define MBED_GPIO_OBJECT_H +#include "mbed_assert.h" #include "cmsis.h" #include "PortNames.h" #include "PeripheralNames.h" @@ -48,6 +49,7 @@ } gpio_t; static inline void gpio_write(gpio_t *obj, int value) { + MBED_ASSERT(obj->pin != (PinName)NC); if (value) { *obj->reg_set = obj->mask; } @@ -57,6 +59,7 @@ } static inline int gpio_read(gpio_t *obj) { + MBED_ASSERT(obj->pin != (PinName)NC); return ((*obj->reg_in & obj->mask) ? 1 : 0); }
--- a/targets/hal/TARGET_STM/TARGET_DISCO_F407VG/i2c_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_DISCO_F407VG/i2c_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -27,18 +27,18 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************************************************************* */ +#include "mbed_assert.h" #include "i2c_api.h" #if DEVICE_I2C #include "cmsis.h" #include "pinmap.h" -#include "error.h" #include "stm32f4xx_hal.h" /* Timeout values for flags and events waiting loops. These timeouts are - not based on accurate values, they just guarantee that the application will - not remain stuck if the I2C communication is corrupted. */ + not based on accurate values, they just guarantee that the application will + not remain stuck if the I2C communication is corrupted. */ #define FLAG_TIMEOUT ((int)0x1000) #define LONG_TIMEOUT ((int)0x8000) @@ -65,19 +65,16 @@ I2C_HandleTypeDef I2cHandle; -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); obj->i2c = (I2CName)pinmap_merge(i2c_sda, i2c_scl); - - if (obj->i2c == (I2CName)NC) { - error("I2C error: pinout mapping failed."); - } + MBED_ASSERT(obj->i2c != (I2CName)NC); // Enable I2C clock - if (obj->i2c == I2C_1) { + if (obj->i2c == I2C_1) { __I2C1_CLK_ENABLE(); } if (obj->i2c == I2C_2) { @@ -97,27 +94,25 @@ i2c_reset(obj); // I2C configuration - i2c_frequency(obj, 100000); // 100 kHz per default + i2c_frequency(obj, 100000); // 100 kHz per default } void i2c_frequency(i2c_t *obj, int hz) { + MBED_ASSERT((hz != 0) && (hz <= 400000)); I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c); - if ((hz != 0) && (hz <= 400000)) { - // I2C configuration - I2cHandle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; - I2cHandle.Init.ClockSpeed = hz; - I2cHandle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED; - I2cHandle.Init.DutyCycle = I2C_DUTYCYCLE_2; - I2cHandle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED; - I2cHandle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED; - I2cHandle.Init.OwnAddress1 = 0; - I2cHandle.Init.OwnAddress2 = 0; - HAL_I2C_Init(&I2cHandle); - } - else { - error("I2C error: frequency setting failed (max 400kHz)."); - } + + // I2C configuration + I2cHandle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; + I2cHandle.Init.ClockSpeed = hz; + I2cHandle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED; + I2cHandle.Init.DutyCycle = I2C_DUTYCYCLE_2; + I2cHandle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED; + I2cHandle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED; + I2cHandle.Init.OwnAddress1 = 0; + I2cHandle.Init.OwnAddress2 = 0; + HAL_I2C_Init(&I2cHandle); + } inline int i2c_start(i2c_t *obj) { @@ -152,7 +147,7 @@ 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) { if (length == 0) return 0; I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c); @@ -208,7 +203,7 @@ i2c->DR = (uint8_t)data; // Wait until the byte is transmitted - timeout = FLAG_TIMEOUT; + timeout = FLAG_TIMEOUT; while ((__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TXE) == RESET) && (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BTF) == RESET)) { if ((timeout--) == 0) { @@ -220,7 +215,7 @@ } void i2c_reset(i2c_t *obj) { - if (obj->i2c == I2C_1) { + if (obj->i2c == I2C_1) { __I2C1_FORCE_RESET(); __I2C1_RELEASE_RESET(); }
--- a/targets/hal/TARGET_STM/TARGET_DISCO_F407VG/pinmap.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_DISCO_F407VG/pinmap.c Wed Jun 11 16:00:09 2014 +0100 @@ -27,6 +27,7 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************************************************************* */ +#include "mbed_assert.h" #include "device.h" #include "pinmap.h" #include "error.h" @@ -74,7 +75,7 @@ break; default: error("Pinmap error: wrong port number."); - break; + break; } return gpio_add; } @@ -83,8 +84,7 @@ * Configure pin (mode, speed, output type and pull-up/pull-down) */ void pin_function(PinName pin, int data) { - if (pin == NC) return; - + MBED_ASSERT(pin != (PinName)NC); // Get the pin informations uint32_t mode = STM_PIN_MODE(data); uint32_t pupd = STM_PIN_PUPD(data); @@ -113,15 +113,14 @@ //} //if ((pin == PA_15) || (pin == PB_3) || (pin == PB_4)) { // - //} + //} } /** * Configure pin pull-up/pull-down */ void pin_mode(PinName pin, PinMode mode) { - if (pin == NC) return; - + MBED_ASSERT(pin != (PinName)NC); uint32_t port_index = STM_PORT(pin); uint32_t pin_index = STM_PIN(pin); @@ -131,7 +130,8 @@ // Configure pull-up/pull-down resistors uint32_t pupd = (uint32_t)mode; - if (pupd > 2) pupd = 0; // Open-drain = No pull-up/No pull-down + if (pupd > 2) + pupd = 0; // Open-drain = No pull-up/No pull-down gpio->PUPDR &= (uint32_t)(~(GPIO_PUPDR_PUPDR0 << (pin_index * 2))); gpio->PUPDR |= (uint32_t)(pupd << (pin_index * 2));
--- a/targets/hal/TARGET_STM/TARGET_DISCO_F407VG/pwmout_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_DISCO_F407VG/pwmout_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -27,6 +27,7 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************************************************************* */ +#include "mbed_assert.h" #include "pwmout_api.h" #include "cmsis.h" @@ -87,11 +88,8 @@ 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); - - if (obj->pwm == (PWMName)NC) { - error("PWM error: pinout mapping failed."); - } - + MBED_ASSERT(obj->pwm != (PWMName)NC); + // Enable TIM clock if (obj->pwm == PWM_1) __TIM1_CLK_ENABLE(); if (obj->pwm == PWM_2) __TIM2_CLK_ENABLE();
--- a/targets/hal/TARGET_STM/TARGET_DISCO_F407VG/serial_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_DISCO_F407VG/serial_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -30,7 +30,6 @@ #include "serial_api.h" #include "cmsis.h" #include "pinmap.h" -#include "error.h" #include <string.h> #include "stm32f4xx_hal.h" @@ -73,20 +72,17 @@ UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE; UartHandle.Init.Mode = UART_MODE_TX_RX; - HAL_UART_Init(&UartHandle); + HAL_UART_Init(&UartHandle); } -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); // Get the peripheral name (UART_1, UART_2, ...) from the pin and assign it to the object obj->uart = (UARTName)pinmap_merge(uart_tx, uart_rx); - - if (obj->uart == (UARTName)NC) { - error("Serial error: pinout mapping failed."); - } + MBED_ASSERT(obj->uart != (UARTName)NC); // Enable USART clock if (obj->uart == UART_1) { @@ -109,7 +105,7 @@ obj->baudrate = 9600; obj->databits = UART_WORDLENGTH_8B; obj->stopbits = UART_STOPBITS_1; - obj->parity = UART_PARITY_NONE; + obj->parity = UART_PARITY_NONE; init_uart(obj); @@ -149,7 +145,7 @@ obj->parity = UART_PARITY_ODD; break; case ParityEven: - case ParityForced1: + case ParityForced1: obj->parity = UART_PARITY_EVEN; break; default: // ParityNone @@ -225,7 +221,7 @@ } else { // TxIrq __HAL_UART_ENABLE_IT(&UartHandle, UART_IT_TC); - } + } NVIC_SetVector(irq_n, vector); NVIC_EnableIRQ(irq_n); @@ -242,12 +238,12 @@ else { // TxIrq __HAL_UART_DISABLE_IT(&UartHandle, UART_IT_TXE); // Check if RxIrq is disabled too - if ((UartHandle.Instance->CR1 & USART_CR1_RXNEIE) == 0) all_disabled = 1; + if ((UartHandle.Instance->CR1 & USART_CR1_RXNEIE) == 0) all_disabled = 1; } if (all_disabled) NVIC_DisableIRQ(irq_n); - } + } } /******************************************************************************
--- a/targets/hal/TARGET_STM/TARGET_DISCO_F407VG/spi_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_DISCO_F407VG/spi_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -34,7 +34,6 @@ #include <math.h> #include "cmsis.h" #include "pinmap.h" -#include "error.h" #include "stm32f4xx_hal.h" static const PinMap PinMap_SPI_MOSI[] = { @@ -112,10 +111,7 @@ SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel); obj->spi = (SPIName)pinmap_merge(spi_data, spi_cntl); - - if (obj->spi == (SPIName)NC) { - error("SPI error: pinout mapping failed."); - } + MBED_ASSERT(obj->spi != (SPIName)NC); // Enable SPI clock if (obj->spi == SPI_1) { @@ -157,7 +153,7 @@ HAL_SPI_DeInit(&SpiHandle); } -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 == 8) { obj->bits = SPI_DATASIZE_8BIT; @@ -177,11 +173,11 @@ break; case 2: obj->cpol = SPI_POLARITY_HIGH; - obj->cpha = SPI_PHASE_1EDGE; + obj->cpha = SPI_PHASE_1EDGE; break; default: obj->cpol = SPI_POLARITY_HIGH; - obj->cpha = SPI_PHASE_2EDGE; + obj->cpha = SPI_PHASE_2EDGE; break; } @@ -191,7 +187,7 @@ } else { obj->mode = SPI_MODE_SLAVE; - obj->nss = SPI_NSS_HARD_INPUT; + obj->nss = SPI_NSS_HARD_INPUT; } init_spi(obj); @@ -231,7 +227,7 @@ SpiHandle.Instance = (SPI_TypeDef *)(obj->spi); // Check if data is received status = ((__HAL_SPI_GET_FLAG(&SpiHandle, SPI_FLAG_RXNE) != RESET) ? 1 : 0); - return status; + return status; } static inline int ssp_writeable(spi_t *obj) {
--- a/targets/hal/TARGET_STM/TARGET_NUCLEO_F030R8/analogin_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_NUCLEO_F030R8/analogin_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -25,6 +25,7 @@ * 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. */ +#include "mbed_assert.h" #include "analogin_api.h" #if DEVICE_ANALOGIN @@ -63,10 +64,7 @@ // Get the peripheral name (ADC_1, ADC_2...) from the pin and assign it to the object obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC); - - if (obj->adc == (ADCName)NC) { - error("ADC pin mapping failed"); - } + MBED_ASSERT(obj->adc != (ADCName)NC); // Configure GPIO pinmap_pinout(pin, PinMap_ADC);
--- a/targets/hal/TARGET_STM/TARGET_NUCLEO_F030R8/gpio_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_NUCLEO_F030R8/gpio_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -27,6 +27,7 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************************************************************* */ +#include "mbed_assert.h" #include "gpio_api.h" #include "pinmap.h" #include "error.h" @@ -34,15 +35,16 @@ extern uint32_t Set_GPIO_Clock(uint32_t port_idx); uint32_t gpio_set(PinName pin) { - if (pin == NC) return 0; + MBED_ASSERT(pin != (PinName)NC); pin_function(pin, STM_PIN_DATA(GPIO_Mode_IN, 0, GPIO_PuPd_NOPULL, 0xFF)); - return (uint32_t)(1 << ((uint32_t)pin & 0xF)); // Return the pin mask } void gpio_init(gpio_t *obj, PinName pin) { - if (pin == NC) return; + obj->pin = pin; + if (pin == (PinName)NC) + return; uint32_t port_index = STM_PORT(pin); @@ -51,7 +53,6 @@ GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add; // Fill GPIO object structure for future use - obj->pin = pin; obj->mask = gpio_set(pin); obj->reg_in = &gpio->IDR; obj->reg_set = &gpio->BSRR; @@ -63,6 +64,7 @@ } 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, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0xFF)); } else { // PIN_INPUT
--- a/targets/hal/TARGET_STM/TARGET_NUCLEO_F030R8/gpio_object.h Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_NUCLEO_F030R8/gpio_object.h Wed Jun 11 16:00:09 2014 +0100 @@ -30,6 +30,7 @@ #ifndef MBED_GPIO_OBJECT_H #define MBED_GPIO_OBJECT_H +#include "mbed_assert.h" #include "cmsis.h" #include "PortNames.h" #include "PeripheralNames.h" @@ -48,6 +49,7 @@ } gpio_t; static inline void gpio_write(gpio_t *obj, int value) { + MBED_ASSERT(obj->pin != (PinName)NC); if (value) { *obj->reg_set = obj->mask; } else { @@ -56,6 +58,7 @@ } static inline int gpio_read(gpio_t *obj) { + MBED_ASSERT(obj->pin != (PinName)NC); return ((*obj->reg_in & obj->mask) ? 1 : 0); }
--- a/targets/hal/TARGET_STM/TARGET_NUCLEO_F030R8/i2c_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_NUCLEO_F030R8/i2c_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -27,13 +27,13 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************************************************************* */ +#include "mbed_assert.h" #include "i2c_api.h" #if DEVICE_I2C #include "cmsis.h" #include "pinmap.h" -#include "error.h" /* Timeout values for flags and events waiting loops. These timeouts are not based on accurate values, they just guarantee that the application will @@ -61,10 +61,7 @@ I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL); obj->i2c = (I2CName)pinmap_merge(i2c_sda, i2c_scl); - - if (obj->i2c == (I2CName)NC) { - error("I2C pin mapping failed"); - } + MBED_ASSERT(obj->i2c != (I2CName)NC); // Enable I2C clock if (obj->i2c == I2C_1) { @@ -89,6 +86,7 @@ } void i2c_frequency(i2c_t *obj, int hz) { + MBED_ASSERT((hz == 100000) || (hz == 200000) || (hz == 400000) || (hz == 1000000)); I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); I2C_InitTypeDef I2C_InitStructure; uint32_t tim = 0; @@ -111,28 +109,27 @@ - Fall time = 10ns */ switch (hz) { - case 100000: - tim = 0x10805E89; // Standard mode - break; - case 200000: - tim = 0x00905E82; // Fast Mode - break; - case 400000: - tim = 0x00901850; // Fast Mode - break; - case 1000000: - tim = 0x00700818; // Fast Mode Plus - // Enable the Fast Mode Plus capability - if (obj->i2c == I2C_1) { - SYSCFG_I2CFastModePlusConfig(SYSCFG_I2CFastModePlus_I2C1, ENABLE); - } - if (obj->i2c == I2C_2) { - SYSCFG_I2CFastModePlusConfig(SYSCFG_I2CFastModePlus_I2C2, ENABLE); - } - break; - default: - error("Only 100kHz, 200kHz, 400kHz and 1MHz I2C frequencies are supported."); - break; + case 100000: + tim = 0x10805E89; // Standard mode + break; + case 200000: + tim = 0x00905E82; // Fast Mode + break; + case 400000: + tim = 0x00901850; // Fast Mode + break; + case 1000000: + tim = 0x00700818; // Fast Mode Plus + // Enable the Fast Mode Plus capability + if (obj->i2c == I2C_1) { + SYSCFG_I2CFastModePlusConfig(SYSCFG_I2CFastModePlus_I2C1, ENABLE); + } + if (obj->i2c == I2C_2) { + SYSCFG_I2CFastModePlusConfig(SYSCFG_I2CFastModePlus_I2C2, ENABLE); + } + break; + default: + break; } // I2C configuration
--- a/targets/hal/TARGET_STM/TARGET_NUCLEO_F030R8/pinmap.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_NUCLEO_F030R8/pinmap.c Wed Jun 11 16:00:09 2014 +0100 @@ -27,6 +27,7 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************************************************************* */ +#include "mbed_assert.h" #include "pinmap.h" #include "PortNames.h" #include "error.h" @@ -66,7 +67,7 @@ * Configure pin (mode, speed, output type and pull-up/pull-down) */ void pin_function(PinName pin, int data) { - if (pin == NC) return; + MBED_ASSERT(pin != (PinName)NC); // Get the pin informations uint32_t mode = STM_PIN_MODE(data); @@ -111,7 +112,7 @@ * Configure pin pull-up/pull-down */ void pin_mode(PinName pin, PinMode mode) { - if (pin == NC) return; + MBED_ASSERT(pin != (PinName)NC); uint32_t port_index = STM_PORT(pin); uint32_t pin_index = STM_PIN(pin);
--- a/targets/hal/TARGET_STM/TARGET_NUCLEO_F030R8/pwmout_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_NUCLEO_F030R8/pwmout_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -27,13 +27,13 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************************************************************* */ +#include "mbed_assert.h" #include "pwmout_api.h" #if DEVICE_PWMOUT #include "cmsis.h" #include "pinmap.h" -#include "error.h" // TIM1 cannot be used because already used by the us_ticker static const PinMap PinMap_PWM[] = { @@ -65,10 +65,7 @@ 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); - - if (obj->pwm == (PWMName)NC) { - error("PWM pinout mapping failed"); - } + MBED_ASSERT(obj->pwm != (PWMName)NC); // Enable TIM clock if (obj->pwm == TIM_3) RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
--- a/targets/hal/TARGET_STM/TARGET_NUCLEO_F030R8/serial_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_NUCLEO_F030R8/serial_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -27,13 +27,13 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************************************************************* */ +#include "mbed_assert.h" #include "serial_api.h" #if DEVICE_SERIAL #include "cmsis.h" #include "pinmap.h" -#include "error.h" #include <string.h> static const PinMap PinMap_UART_TX[] = { @@ -84,10 +84,7 @@ // Get the peripheral name (UART_1, UART_2, ...) from the pin and assign it to the object obj->uart = (UARTName)pinmap_merge(uart_tx, uart_rx); - - if (obj->uart == (UARTName)NC) { - error("Serial pinout mapping failed"); - } + MBED_ASSERT(obj->uart != (UARTName)NC); // Enable USART clock if (obj->uart == UART_1) {
--- a/targets/hal/TARGET_STM/TARGET_NUCLEO_F030R8/spi_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_NUCLEO_F030R8/spi_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -27,6 +27,7 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************************************************************* */ +#include "mbed_assert.h" #include "spi_api.h" #if DEVICE_SPI @@ -34,7 +35,6 @@ #include <math.h> #include "cmsis.h" #include "pinmap.h" -#include "error.h" static const PinMap PinMap_SPI_MOSI[] = { {PA_7, SPI_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_NOPULL, GPIO_AF_0)}, @@ -98,10 +98,7 @@ SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel); obj->spi = (SPIName)pinmap_merge(spi_data, spi_cntl); - - if (obj->spi == (SPIName)NC) { - error("SPI pinout mapping failed"); - } + MBED_ASSERT(obj->spi != (SPIName)NC); // Enable SPI clock if (obj->spi == SPI_1) {
--- a/targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/analogin_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/analogin_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -25,13 +25,13 @@ * 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. */ +#include "mbed_assert.h" #include "analogin_api.h" #if DEVICE_ANALOGIN #include "cmsis.h" #include "pinmap.h" -#include "error.h" #include "wait_api.h" static const PinMap PinMap_ADC[] = { @@ -62,10 +62,7 @@ // Get the peripheral name from the pin and assign it to the object obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC); - - if (obj->adc == (ADCName)NC) { - error("ADC pin mapping failed"); - } + MBED_ASSERT(obj->adc != (ADCName)NC); // Configure GPIO pinmap_pinout(pin, PinMap_ADC);
--- a/targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/gpio_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/gpio_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -27,6 +27,7 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************************************************************* */ +#include "mbed_assert.h" #include "gpio_api.h" #include "pinmap.h" #include "error.h" @@ -34,15 +35,16 @@ extern uint32_t Set_GPIO_Clock(uint32_t port_idx); uint32_t gpio_set(PinName pin) { - if (pin == NC) return 0; + 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) { - if (pin == NC) return; + obj->pin = pin; + if (pin == (PinName)NC) + return; uint32_t port_index = STM_PORT(pin); @@ -51,7 +53,6 @@ GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add; // Fill GPIO object structure for future use - obj->pin = pin; obj->mask = gpio_set(pin); obj->reg_in = &gpio->IDR; obj->reg_set = &gpio->BSRR; @@ -63,6 +64,7 @@ } 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)); } else { // PIN_INPUT
--- a/targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/gpio_object.h Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/gpio_object.h Wed Jun 11 16:00:09 2014 +0100 @@ -30,6 +30,7 @@ #ifndef MBED_GPIO_OBJECT_H #define MBED_GPIO_OBJECT_H +#include "mbed_assert.h" #include "cmsis.h" #include "PortNames.h" #include "PeripheralNames.h" @@ -48,6 +49,7 @@ } gpio_t; static inline void gpio_write(gpio_t *obj, int value) { + MBED_ASSERT(obj->pin != (PinName)NC); if (value) { *obj->reg_set = obj->mask; } else { @@ -56,6 +58,7 @@ } static inline int gpio_read(gpio_t *obj) { + MBED_ASSERT(obj->pin != (PinName)NC); return ((*obj->reg_in & obj->mask) ? 1 : 0); }
--- a/targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/i2c_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/i2c_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -27,13 +27,13 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************************************************************* */ +#include "mbed_assert.h" #include "i2c_api.h" #if DEVICE_I2C #include "cmsis.h" #include "pinmap.h" -#include "error.h" /* Timeout values for flags and events waiting loops. These timeouts are not based on accurate values, they just guarantee that the application will @@ -61,10 +61,7 @@ I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL); obj->i2c = (I2CName)pinmap_merge(i2c_sda, i2c_scl); - - if (obj->i2c == (I2CName)NC) { - error("I2C pin mapping failed"); - } + MBED_ASSERT(obj->i2c != (I2CName)NC); // Enable I2C clock if (obj->i2c == I2C_1) {
--- a/targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/pinmap.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/pinmap.c Wed Jun 11 16:00:09 2014 +0100 @@ -27,6 +27,7 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************************************************************* */ +#include "mbed_assert.h" #include "pinmap.h" #include "PortNames.h" #include "error.h" @@ -77,8 +78,7 @@ * Configure pin (input, output, alternate function or analog) + output speed + AF */ void pin_function(PinName pin, int data) { - if (pin == NC) return; - + MBED_ASSERT(pin != (PinName)NC); // Get the pin informations uint32_t mode = STM_PIN_MODE(data); uint32_t afnum = STM_PIN_AFNUM(data); @@ -120,10 +120,9 @@ * Configure pin pull-up/pull-down */ void pin_mode(PinName pin, PinMode mode) { + MBED_ASSERT(pin != (PinName)NC); GPIO_InitTypeDef GPIO_InitStructure; - if (pin == NC) return; - uint32_t port_index = STM_PORT(pin); uint32_t pin_index = STM_PIN(pin);
--- a/targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/pwmout_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/pwmout_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -27,13 +27,13 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************************************************************* */ +#include "mbed_assert.h" #include "pwmout_api.h" #if DEVICE_PWMOUT #include "cmsis.h" #include "pinmap.h" -#include "error.h" // TIM4 cannot be used because already used by the us_ticker static const PinMap PinMap_PWM[] = { @@ -76,10 +76,7 @@ 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); - - if (obj->pwm == (PWMName)NC) { - error("PWM pinout mapping failed"); - } + MBED_ASSERT(obj->pwm != (PWMName)NC); // Enable TIM clock if (obj->pwm == PWM_1) RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
--- a/targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/serial_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/serial_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -27,13 +27,13 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************************************************************* */ +#include "mbed_assert.h" #include "serial_api.h" #if DEVICE_SERIAL #include "cmsis.h" #include "pinmap.h" -#include "error.h" #include <string.h> static const PinMap PinMap_UART_TX[] = { @@ -87,10 +87,7 @@ // Get the peripheral name (UART_1, UART_2, ...) from the pin and assign it to the object obj->uart = (UARTName)pinmap_merge(uart_tx, uart_rx); - - if (obj->uart == (UARTName)NC) { - error("Serial pinout mapping failed"); - } + MBED_ASSERT(obj->uart != (UARTName)NC); // Enable USART clock if (obj->uart == UART_1) {
--- a/targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/spi_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/spi_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -27,6 +27,7 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************************************************************* */ +#include "mbed_assert.h" #include "spi_api.h" #if DEVICE_SPI @@ -34,7 +35,6 @@ #include <math.h> #include "cmsis.h" #include "pinmap.h" -#include "error.h" static const PinMap PinMap_SPI_MOSI[] = { {PA_7, SPI_1, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)}, @@ -95,10 +95,7 @@ SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel); obj->spi = (SPIName)pinmap_merge(spi_data, spi_cntl); - - if (obj->spi == (SPIName)NC) { - error("SPI pinout mapping failed"); - } + MBED_ASSERT(obj->spi != (SPIName)NC); // Enable SPI clock if (obj->spi == SPI_1) {
--- a/targets/hal/TARGET_STM/TARGET_NUCLEO_F302R8/analogin_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_NUCLEO_F302R8/analogin_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -25,6 +25,7 @@ * 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. */ +#include "mbed_assert.h" #include "analogin_api.h" #if DEVICE_ANALOGIN @@ -32,7 +33,6 @@ #include "wait_api.h" #include "cmsis.h" #include "pinmap.h" -#include "error.h" static const PinMap PinMap_ADC[] = { {PA_0, ADC_1, STM_PIN_DATA(GPIO_Mode_AN, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0xFF)}, // ADC_IN1 @@ -63,10 +63,7 @@ // Get the peripheral name from the pin and assign it to the object obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC); - - if (obj->adc == (ADCName)NC) { - error("ADC pin mapping failed"); - } + MBED_ASSERT(obj->adc != (ADCName)NC); // Configure GPIO pinmap_pinout(pin, PinMap_ADC);
--- a/targets/hal/TARGET_STM/TARGET_NUCLEO_F302R8/analogout_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_NUCLEO_F302R8/analogout_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -25,13 +25,13 @@ * 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. */ +#include "mbed_assert.h" #include "analogout_api.h" #if DEVICE_ANALOGOUT #include "cmsis.h" #include "pinmap.h" -#include "error.h" #define DAC_RANGE (0xFFF) // 12 bits @@ -46,10 +46,7 @@ // Get the peripheral name (DAC_1, ...) from the pin and assign it to the object obj->dac = (DACName)pinmap_peripheral(pin, PinMap_DAC); - - if (obj->dac == (DACName)NC) { - error("DAC pin mapping failed"); - } + MBED_ASSERT(obj->dac != (DACName)NC); dac = (DAC_TypeDef *)(obj->dac);
--- a/targets/hal/TARGET_STM/TARGET_NUCLEO_F302R8/gpio_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_NUCLEO_F302R8/gpio_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -27,6 +27,7 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************************************************************* */ +#include "mbed_assert.h" #include "gpio_api.h" #include "pinmap.h" #include "error.h" @@ -34,15 +35,16 @@ extern uint32_t Set_GPIO_Clock(uint32_t port_idx); uint32_t gpio_set(PinName pin) { - if (pin == NC) return 0; + MBED_ASSERT(pin != (PinName)NC); pin_function(pin, STM_PIN_DATA(GPIO_Mode_IN, 0, GPIO_PuPd_NOPULL, 0xFF)); - return (uint32_t)(1 << ((uint32_t)pin & 0xF)); // Return the pin mask } void gpio_init(gpio_t *obj, PinName pin) { - if (pin == NC) return; + obj->pin = pin; + if (pin == (PinName)NC) + return; uint32_t port_index = STM_PORT(pin); @@ -51,7 +53,6 @@ GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add; // Fill GPIO object structure for future use - obj->pin = pin; obj->mask = gpio_set(pin); obj->reg_in = &gpio->IDR; obj->reg_set = &gpio->BSRR; @@ -63,6 +64,7 @@ } 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, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0xFF)); } else { // PIN_INPUT
--- a/targets/hal/TARGET_STM/TARGET_NUCLEO_F302R8/gpio_object.h Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_NUCLEO_F302R8/gpio_object.h Wed Jun 11 16:00:09 2014 +0100 @@ -30,6 +30,7 @@ #ifndef MBED_GPIO_OBJECT_H #define MBED_GPIO_OBJECT_H +#include "mbed_assert.h" #include "cmsis.h" #include "PortNames.h" #include "PeripheralNames.h" @@ -48,6 +49,7 @@ } gpio_t; static inline void gpio_write(gpio_t *obj, int value) { + MBED_ASSERT(obj->pin != (PinName)NC); if (value) { *obj->reg_set = obj->mask; } else { @@ -56,6 +58,7 @@ } static inline int gpio_read(gpio_t *obj) { + MBED_ASSERT(obj->pin != (PinName)NC); return ((*obj->reg_in & obj->mask) ? 1 : 0); }
--- a/targets/hal/TARGET_STM/TARGET_NUCLEO_F302R8/i2c_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_NUCLEO_F302R8/i2c_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -27,6 +27,7 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************************************************************* */ +#include "mbed_assert.h" #include "i2c_api.h" #if DEVICE_I2C @@ -68,10 +69,7 @@ I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL); obj->i2c = (I2CName)pinmap_merge(i2c_sda, i2c_scl); - - if (obj->i2c == (I2CName)NC) { - error("I2C pin mapping failed"); - } + MBED_ASSERT(obj->i2c != (I2CName)NC); // Enable I2C clock if (obj->i2c == I2C_1) {
--- a/targets/hal/TARGET_STM/TARGET_NUCLEO_F302R8/pinmap.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_NUCLEO_F302R8/pinmap.c Wed Jun 11 16:00:09 2014 +0100 @@ -27,6 +27,7 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************************************************************* */ +#include "mbed_assert.h" #include "pinmap.h" #include "PortNames.h" #include "error.h" @@ -67,8 +68,7 @@ * Configure pin (mode, speed, output type and pull-up/pull-down) */ void pin_function(PinName pin, int data) { - if (pin == NC) return; - + MBED_ASSERT(pin != (PinName)NC); // Get the pin informations uint32_t mode = STM_PIN_MODE(data); uint32_t otype = STM_PIN_OTYPE(data); @@ -111,8 +111,7 @@ * Configure pin pull-up/pull-down */ void pin_mode(PinName pin, PinMode mode) { - if (pin == NC) return; - + MBED_ASSERT(pin != (PinName)NC); uint32_t port_index = STM_PORT(pin); uint32_t pin_index = STM_PIN(pin); @@ -122,7 +121,8 @@ // Configure pull-up/pull-down resistors uint32_t pupd = (uint32_t)mode; - if (pupd > 2) pupd = 0; // Open-drain = No pull-up/No pull-down + if (pupd > 2) + pupd = 0; // Open-drain = No pull-up/No pull-down gpio->PUPDR &= (uint32_t)(~(GPIO_PUPDR_PUPDR0 << (pin_index * 2))); gpio->PUPDR |= (uint32_t)(pupd << (pin_index * 2));
--- a/targets/hal/TARGET_STM/TARGET_NUCLEO_F302R8/serial_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_NUCLEO_F302R8/serial_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -27,13 +27,13 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************************************************************* */ +#include "mbed_assert.h" #include "serial_api.h" #if DEVICE_SERIAL #include "cmsis.h" #include "pinmap.h" -#include "error.h" #include <string.h> static const PinMap PinMap_UART_TX[] = { @@ -95,10 +95,7 @@ // Get the peripheral name from the pin and assign it to the object obj->uart = (UARTName)pinmap_merge(uart_tx, uart_rx); - - if (obj->uart == (UARTName)NC) { - error("Serial pinout mapping failed"); - } + MBED_ASSERT(obj->uart != (UARTName)NC); // Enable USART clock if (obj->uart == UART_1) {
--- a/targets/hal/TARGET_STM/TARGET_NUCLEO_F302R8/spi_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_NUCLEO_F302R8/spi_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -27,6 +27,7 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************************************************************* */ +#include "mbed_assert.h" #include "spi_api.h" #if DEVICE_SPI @@ -34,7 +35,6 @@ #include <math.h> #include "cmsis.h" #include "pinmap.h" -#include "error.h" static const PinMap PinMap_SPI_MOSI[] = { {PA_11, SPI_2, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_DOWN, GPIO_AF_5)}, @@ -101,10 +101,7 @@ SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel); obj->spi = (SPIName)pinmap_merge(spi_data, spi_cntl); - - if (obj->spi == (SPIName)NC) { - error("SPI pinout mapping failed"); - } + MBED_ASSERT(obj->spi != (SPIName)NC); // Enable SPI clock if (obj->spi == SPI_2) {
--- a/targets/hal/TARGET_STM/TARGET_NUCLEO_F401RE/analogin_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_NUCLEO_F401RE/analogin_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -25,6 +25,7 @@ * 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. */ +#include "mbed_assert.h" #include "analogin_api.h" #if DEVICE_ANALOGIN @@ -32,7 +33,6 @@ #include "wait_api.h" #include "cmsis.h" #include "pinmap.h" -#include "error.h" static const PinMap PinMap_ADC[] = { {PA_0, ADC_1, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0)}, // ADC1_IN0 @@ -61,10 +61,7 @@ void analogin_init(analogin_t *obj, PinName pin) { // Get the peripheral name from the pin and assign it to the object obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC); - - if (obj->adc == (ADCName)NC) { - error("ADC error: pinout mapping failed."); - } + MBED_ASSERT(obj->adc != (ADCName)NC); // Configure GPIO pinmap_pinout(pin, PinMap_ADC);
--- a/targets/hal/TARGET_STM/TARGET_NUCLEO_F401RE/gpio_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_NUCLEO_F401RE/gpio_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -27,6 +27,7 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************************************************************* */ +#include "mbed_assert.h" #include "gpio_api.h" #include "pinmap.h" #include "error.h" @@ -34,15 +35,16 @@ extern uint32_t Set_GPIO_Clock(uint32_t port_idx); uint32_t gpio_set(PinName pin) { - if (pin == NC) return 0; + MBED_ASSERT(pin != (PinName)NC); pin_function(pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); - return (uint32_t)(1 << ((uint32_t)pin & 0xF)); // Return the pin mask } void gpio_init(gpio_t *obj, PinName pin) { - if (pin == NC) return; + obj->pin = pin; + if (pin == (PinName)NC) + return; uint32_t port_index = STM_PORT(pin); @@ -51,7 +53,6 @@ GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add; // Fill GPIO object structure for future use - obj->pin = pin; obj->mask = gpio_set(pin); obj->reg_in = &gpio->IDR; obj->reg_set = &gpio->BSRRL; @@ -63,6 +64,7 @@ } 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(STM_MODE_OUTPUT_PP, GPIO_NOPULL, 0)); } else { // PIN_INPUT
--- a/targets/hal/TARGET_STM/TARGET_NUCLEO_F401RE/gpio_object.h Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_NUCLEO_F401RE/gpio_object.h Wed Jun 11 16:00:09 2014 +0100 @@ -30,6 +30,7 @@ #ifndef MBED_GPIO_OBJECT_H #define MBED_GPIO_OBJECT_H +#include "mbed_assert.h" #include "cmsis.h" #include "PortNames.h" #include "PeripheralNames.h" @@ -48,6 +49,7 @@ } gpio_t; static inline void gpio_write(gpio_t *obj, int value) { + MBED_ASSERT(obj->pin != (PinName)NC); if (value) { *obj->reg_set = obj->mask; } else { @@ -56,6 +58,7 @@ } static inline int gpio_read(gpio_t *obj) { + MBED_ASSERT(obj->pin != (PinName)NC); return ((*obj->reg_in & obj->mask) ? 1 : 0); }
--- a/targets/hal/TARGET_STM/TARGET_NUCLEO_F401RE/i2c_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_NUCLEO_F401RE/i2c_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -27,13 +27,13 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************************************************************* */ +#include "mbed_assert.h" #include "i2c_api.h" #if DEVICE_I2C #include "cmsis.h" #include "pinmap.h" -#include "error.h" /* Timeout values for flags and events waiting loops. These timeouts are not based on accurate values, they just guarantee that the application will @@ -66,10 +66,7 @@ I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL); obj->i2c = (I2CName)pinmap_merge(i2c_sda, i2c_scl); - - if (obj->i2c == (I2CName)NC) { - error("I2C error: pinout mapping failed."); - } + MBED_ASSERT(obj->i2c != (I2CName)NC); // Enable I2C clock if (obj->i2c == I2C_1) { @@ -99,26 +96,24 @@ } void i2c_frequency(i2c_t *obj, int hz) { + MBED_ASSERT((hz != 0) && (hz <= 400000)); I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c); - if ((hz != 0) && (hz <= 400000)) { - // I2C configuration - I2cHandle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; - I2cHandle.Init.ClockSpeed = hz; - I2cHandle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED; - I2cHandle.Init.DutyCycle = I2C_DUTYCYCLE_2; - I2cHandle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED; - I2cHandle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED; - I2cHandle.Init.OwnAddress1 = 0; - I2cHandle.Init.OwnAddress2 = 0; - HAL_I2C_Init(&I2cHandle); - if (obj->slave) { - /* Enable Address Acknowledge */ - I2cHandle.Instance->CR1 |= I2C_CR1_ACK; - } - } else { - error("I2C error: frequency setting failed (max 400kHz)."); + // I2C configuration + I2cHandle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; + I2cHandle.Init.ClockSpeed = hz; + I2cHandle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED; + I2cHandle.Init.DutyCycle = I2C_DUTYCYCLE_2; + I2cHandle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED; + I2cHandle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED; + I2cHandle.Init.OwnAddress1 = 0; + I2cHandle.Init.OwnAddress2 = 0; + HAL_I2C_Init(&I2cHandle); + if (obj->slave) { + /* Enable Address Acknowledge */ + I2cHandle.Instance->CR1 |= I2C_CR1_ACK; } + } inline int i2c_start(i2c_t *obj) {
--- a/targets/hal/TARGET_STM/TARGET_NUCLEO_F401RE/pinmap.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_NUCLEO_F401RE/pinmap.c Wed Jun 11 16:00:09 2014 +0100 @@ -27,6 +27,7 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************************************************************* */ +#include "mbed_assert.h" #include "pinmap.h" #include "PortNames.h" #include "error.h" @@ -82,8 +83,7 @@ * Configure pin (mode, speed, output type and pull-up/pull-down) */ void pin_function(PinName pin, int data) { - if (pin == NC) return; - + MBED_ASSERT(pin != (PinName)NC); // Get the pin informations uint32_t mode = STM_PIN_MODE(data); uint32_t pupd = STM_PIN_PUPD(data); @@ -119,8 +119,7 @@ * Configure pin pull-up/pull-down */ void pin_mode(PinName pin, PinMode mode) { - if (pin == NC) return; - + MBED_ASSERT(pin != (PinName)NC); uint32_t port_index = STM_PORT(pin); uint32_t pin_index = STM_PIN(pin); @@ -130,7 +129,8 @@ // Configure pull-up/pull-down resistors uint32_t pupd = (uint32_t)mode; - if (pupd > 2) pupd = 0; // Open-drain = No pull-up/No pull-down + if (pupd > 2) + pupd = 0; // Open-drain = No pull-up/No pull-down gpio->PUPDR &= (uint32_t)(~(GPIO_PUPDR_PUPDR0 << (pin_index * 2))); gpio->PUPDR |= (uint32_t)(pupd << (pin_index * 2));
--- a/targets/hal/TARGET_STM/TARGET_NUCLEO_F401RE/serial_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_NUCLEO_F401RE/serial_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -27,13 +27,13 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************************************************************* */ +#include "mbed_assert.h" #include "serial_api.h" #if DEVICE_SERIAL #include "cmsis.h" #include "pinmap.h" -#include "error.h" #include <string.h> static const PinMap PinMap_UART_TX[] = { @@ -85,10 +85,7 @@ // Get the peripheral name (UART_1, UART_2, ...) from the pin and assign it to the object obj->uart = (UARTName)pinmap_merge(uart_tx, uart_rx); - - if (obj->uart == (UARTName)NC) { - error("Serial error: pinout mapping failed."); - } + MBED_ASSERT(obj->uart != (UARTName)NC); // Enable USART clock if (obj->uart == UART_1) {
--- a/targets/hal/TARGET_STM/TARGET_NUCLEO_F401RE/spi_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_NUCLEO_F401RE/spi_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -27,6 +27,7 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************************************************************* */ +#include "mbed_assert.h" #include "spi_api.h" #if DEVICE_SPI @@ -34,7 +35,6 @@ #include <math.h> #include "cmsis.h" #include "pinmap.h" -#include "error.h" static const PinMap PinMap_SPI_MOSI[] = { {PA_7, SPI_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_SPI1)}, @@ -111,10 +111,7 @@ SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel); obj->spi = (SPIName)pinmap_merge(spi_data, spi_cntl); - - if (obj->spi == (SPIName)NC) { - error("SPI error: pinout mapping failed."); - } + MBED_ASSERT(obj->spi != (SPIName)NC); // Enable SPI clock if (obj->spi == SPI_1) {
--- a/targets/hal/TARGET_STM/TARGET_NUCLEO_L053R8/analogin_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_NUCLEO_L053R8/analogin_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -25,6 +25,7 @@ * 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. */ +#include "mbed_assert.h" #include "analogin_api.h" #if DEVICE_ANALOGIN @@ -32,7 +33,6 @@ #include "wait_api.h" #include "cmsis.h" #include "pinmap.h" -#include "error.h" static const PinMap PinMap_ADC[] = { {PA_0, ADC_1, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0)}, // ADC1_IN0 @@ -61,10 +61,7 @@ void analogin_init(analogin_t *obj, PinName pin) { // Get the peripheral name from the pin and assign it to the object obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC); - - if (obj->adc == (ADCName)NC) { - error("ADC error: pinout mapping failed."); - } + MBED_ASSERT(obj->adc != (ADCName)NC); // Configure GPIO pinmap_pinout(pin, PinMap_ADC);
--- a/targets/hal/TARGET_STM/TARGET_NUCLEO_L053R8/analogout_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_NUCLEO_L053R8/analogout_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -25,6 +25,7 @@ * 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. */ +#include "mbed_assert.h" #include "analogout_api.h" #if DEVICE_ANALOGOUT @@ -49,10 +50,7 @@ // Get the peripheral name (DAC_1, ...) from the pin and assign it to the object obj->dac = (DACName)pinmap_peripheral(pin, PinMap_DAC); - - if (obj->dac == (DACName)NC) { - error("DAC pin mapping failed"); - } + MBED_ASSERT(obj->dac != (DACName)NC); // Configure GPIO pinmap_pinout(pin, PinMap_DAC);
--- a/targets/hal/TARGET_STM/TARGET_NUCLEO_L053R8/gpio_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_NUCLEO_L053R8/gpio_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -27,6 +27,7 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************************************************************* */ +#include "mbed_assert.h" #include "gpio_api.h" #include "pinmap.h" #include "error.h" @@ -34,15 +35,16 @@ extern uint32_t Set_GPIO_Clock(uint32_t port_idx); uint32_t gpio_set(PinName pin) { - if (pin == NC) return 0; + MBED_ASSERT(pin != (PinName)NC); pin_function(pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); - return (uint32_t)(1 << ((uint32_t)pin & 0xF)); // Return the pin mask } void gpio_init(gpio_t *obj, PinName pin) { - if (pin == NC) return; + obj->pin = pin; + if (pin == (PinName)NC) + return; uint32_t port_index = STM_PORT(pin); @@ -51,7 +53,6 @@ GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add; // Fill GPIO object structure for future use - obj->pin = pin; obj->mask = gpio_set(pin); obj->reg_in = &gpio->IDR; obj->reg_set = &gpio->BSRR; @@ -63,6 +64,7 @@ } 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(STM_MODE_OUTPUT_PP, GPIO_NOPULL, 0)); } else { // PIN_INPUT
--- a/targets/hal/TARGET_STM/TARGET_NUCLEO_L053R8/gpio_object.h Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_NUCLEO_L053R8/gpio_object.h Wed Jun 11 16:00:09 2014 +0100 @@ -30,6 +30,7 @@ #ifndef MBED_GPIO_OBJECT_H #define MBED_GPIO_OBJECT_H +#include "mbed_assert.h" #include "cmsis.h" #include "PortNames.h" #include "PeripheralNames.h" @@ -48,6 +49,7 @@ } gpio_t; static inline void gpio_write(gpio_t *obj, int value) { + MBED_ASSERT(obj->pin != (PinName)NC); if (value) { *obj->reg_set = obj->mask; } else { @@ -56,6 +58,7 @@ } static inline int gpio_read(gpio_t *obj) { + MBED_ASSERT(obj->pin != (PinName)NC); return ((*obj->reg_in & obj->mask) ? 1 : 0); }
--- a/targets/hal/TARGET_STM/TARGET_NUCLEO_L053R8/i2c_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_NUCLEO_L053R8/i2c_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -27,6 +27,7 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************************************************************* */ +#include "mbed_assert.h" #include "i2c_api.h" #if DEVICE_I2C @@ -65,10 +66,7 @@ I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL); obj->i2c = (I2CName)pinmap_merge(i2c_sda, i2c_scl); - - if (obj->i2c == (I2CName)NC) { - error("I2C error: pinout mapping failed."); - } + MBED_ASSERT(obj->i2c != (I2CName)NC); // Enable I2C clock if (obj->i2c == I2C_1) { @@ -93,6 +91,7 @@ } void i2c_frequency(i2c_t *obj, int hz) { + MBED_ASSERT((hz == 100000) || (hz == 400000) || (hz == 1000000)); I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c); // Common settings: I2C clock = 32 MHz, Analog filter = ON, Digital filter coefficient = 0 @@ -107,7 +106,6 @@ I2cHandle.Init.Timing = 0x0030040E; // Fast mode Plus with Rise Time = 60ns and Fall Time = 100ns break; default: - error("Only 100kHz, 400kHz and 1MHz I2C frequencies are supported."); break; }
--- a/targets/hal/TARGET_STM/TARGET_NUCLEO_L053R8/pinmap.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_NUCLEO_L053R8/pinmap.c Wed Jun 11 16:00:09 2014 +0100 @@ -27,6 +27,7 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************************************************************* */ +#include "mbed_assert.h" #include "pinmap.h" #include "PortNames.h" #include "error.h" @@ -82,8 +83,7 @@ * Configure pin (mode, speed, output type and pull-up/pull-down) */ void pin_function(PinName pin, int data) { - if (pin == NC) return; - + MBED_ASSERT(pin != (PinName)NC); // Get the pin informations uint32_t mode = STM_PIN_MODE(data); uint32_t pupd = STM_PIN_PUPD(data); @@ -119,8 +119,7 @@ * Configure pin pull-up/pull-down */ void pin_mode(PinName pin, PinMode mode) { - if (pin == NC) return; - + MBED_ASSERT(pin != (PinName)NC); uint32_t port_index = STM_PORT(pin); uint32_t pin_index = STM_PIN(pin); @@ -130,7 +129,8 @@ // Configure pull-up/pull-down resistors uint32_t pupd = (uint32_t)mode; - if (pupd > 2) pupd = 0; // Open-drain = No pull-up/No pull-down + if (pupd > 2) + pupd = 0; // Open-drain = No pull-up/No pull-down gpio->PUPDR &= (uint32_t)(~(GPIO_PUPDR_PUPD0 << (pin_index * 2))); gpio->PUPDR |= (uint32_t)(pupd << (pin_index * 2));
--- a/targets/hal/TARGET_STM/TARGET_NUCLEO_L053R8/serial_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_NUCLEO_L053R8/serial_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -27,13 +27,13 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************************************************************* */ +#include "mbed_assert.h" #include "serial_api.h" #if DEVICE_SERIAL #include "cmsis.h" #include "pinmap.h" -#include "error.h" #include <string.h> static const PinMap PinMap_UART_TX[] = { @@ -101,10 +101,7 @@ // Get the peripheral name (UART_1, UART_2, ...) from the pin and assign it to the object obj->uart = (UARTName)pinmap_merge(uart_tx, uart_rx); - - if (obj->uart == (UARTName)NC) { - error("Serial error: pinout mapping failed."); - } + MBED_ASSERT(obj->uart != (UARTName)NC); // Enable UART clock if (obj->uart == UART_1) {
--- a/targets/hal/TARGET_STM/TARGET_NUCLEO_L053R8/spi_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_NUCLEO_L053R8/spi_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -27,6 +27,7 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************************************************************* */ +#include "mbed_assert.h" #include "spi_api.h" #if DEVICE_SPI @@ -34,7 +35,6 @@ #include <math.h> #include "cmsis.h" #include "pinmap.h" -#include "error.h" static const PinMap PinMap_SPI_MOSI[] = { {PA_7, SPI_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF0_SPI1)}, @@ -105,10 +105,7 @@ SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel); obj->spi = (SPIName)pinmap_merge(spi_data, spi_cntl); - - if (obj->spi == (SPIName)NC) { - error("SPI error: pinout mapping failed."); - } + MBED_ASSERT(obj->spi != (SPIName)NC); // Enable SPI clock if (obj->spi == SPI_1) {
--- a/targets/hal/TARGET_STM/TARGET_NUCLEO_L152RE/analogin_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_NUCLEO_L152RE/analogin_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -25,13 +25,13 @@ * 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. */ +#include "mbed_assert.h" #include "analogin_api.h" #if DEVICE_ANALOGIN #include "cmsis.h" #include "pinmap.h" -#include "error.h" #include "wait_api.h" static const PinMap PinMap_ADC[] = { @@ -66,10 +66,7 @@ // Get the peripheral name from the pin and assign it to the object obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC); - - if (obj->adc == (ADCName)NC) { - error("ADC pin mapping failed"); - } + MBED_ASSERT(obj->adc != (ADCName)NC); // Configure GPIO pinmap_pinout(pin, PinMap_ADC);
--- a/targets/hal/TARGET_STM/TARGET_NUCLEO_L152RE/analogout_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_NUCLEO_L152RE/analogout_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -25,13 +25,13 @@ * 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. */ +#include "mbed_assert.h" #include "analogout_api.h" #if DEVICE_ANALOGOUT #include "cmsis.h" #include "pinmap.h" -#include "error.h" #define RANGE_12BIT (0xFFF) @@ -46,10 +46,7 @@ // Get the peripheral name (DAC_1, ...) from the pin and assign it to the object obj->dac = (DACName)pinmap_peripheral(pin, PinMap_DAC); - - if (obj->dac == (DACName)NC) { - error("DAC pin mapping failed"); - } + MBED_ASSERT(obj->dac != (DACName)NC); // Configure GPIO pinmap_pinout(pin, PinMap_DAC);
--- a/targets/hal/TARGET_STM/TARGET_NUCLEO_L152RE/gpio_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_NUCLEO_L152RE/gpio_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -27,6 +27,7 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************************************************************* */ +#include "mbed_assert.h" #include "gpio_api.h" #include "pinmap.h" #include "error.h" @@ -34,16 +35,16 @@ extern uint32_t Set_GPIO_Clock(uint32_t port_idx); uint32_t gpio_set(PinName pin) { - if (pin == NC) return 0; + MBED_ASSERT(pin != (PinName)NC); pin_function(pin, STM_PIN_DATA(GPIO_Mode_IN, 0, GPIO_PuPd_NOPULL, 0xFF)); - return (uint32_t)(1 << ((uint32_t)pin & 0xF)); // Return the pin mask } void gpio_init(gpio_t *obj, PinName pin) { - if (pin == NC) return; - + obj->pin = pin; + if (pin == (PinName)NC) + return; uint32_t port_index = STM_PORT(pin); // Enable GPIO clock @@ -51,7 +52,6 @@ GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add; // Fill GPIO object structure for future use - obj->pin = pin; obj->mask = gpio_set(pin); obj->reg_in = &gpio->IDR; obj->reg_set = &gpio->BSRRL; @@ -63,6 +63,7 @@ } 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, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0xFF)); } else { // PIN_INPUT
--- a/targets/hal/TARGET_STM/TARGET_NUCLEO_L152RE/gpio_object.h Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_NUCLEO_L152RE/gpio_object.h Wed Jun 11 16:00:09 2014 +0100 @@ -30,6 +30,7 @@ #ifndef MBED_GPIO_OBJECT_H #define MBED_GPIO_OBJECT_H +#include "mbed_assert.h" #include "cmsis.h" #include "PortNames.h" #include "PeripheralNames.h" @@ -48,6 +49,7 @@ } gpio_t; static inline void gpio_write(gpio_t *obj, int value) { + MBED_ASSERT(obj->pin != (PinName)NC); if (value) { *obj->reg_set = obj->mask; } else { @@ -56,6 +58,7 @@ } static inline int gpio_read(gpio_t *obj) { + MBED_ASSERT(obj->pin != (PinName)NC); return ((*obj->reg_in & obj->mask) ? 1 : 0); }
--- a/targets/hal/TARGET_STM/TARGET_NUCLEO_L152RE/i2c_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_NUCLEO_L152RE/i2c_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -27,13 +27,14 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************************************************************* */ +#include "mbed_assert.h" #include "i2c_api.h" #if DEVICE_I2C #include "cmsis.h" #include "pinmap.h" -#include "error.h" + /* Timeout values for flags and events waiting loops. These timeouts are not based on accurate values, they just guarantee that the application will @@ -61,10 +62,7 @@ I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL); obj->i2c = (I2CName)pinmap_merge(i2c_sda, i2c_scl); - - if (obj->i2c == (I2CName)NC) { - error("I2C pin mapping failed"); - } + MBED_ASSERT(obj->i2c != (I2CName)NC); // Enable I2C clock if (obj->i2c == I2C_1) {
--- a/targets/hal/TARGET_STM/TARGET_NUCLEO_L152RE/pinmap.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_NUCLEO_L152RE/pinmap.c Wed Jun 11 16:00:09 2014 +0100 @@ -27,6 +27,7 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************************************************************* */ +#include "mbed_assert.h" #include "pinmap.h" #include "PortNames.h" #include "error.h" @@ -66,7 +67,7 @@ * Configure pin (mode, speed, output type and pull-up/pull-down) */ void pin_function(PinName pin, int data) { - if (pin == NC) return; + MBED_ASSERT(pin != (PinName)NC); // Get the pin informations uint32_t mode = STM_PIN_MODE(data); @@ -110,7 +111,7 @@ * Configure pin pull-up/pull-down */ void pin_mode(PinName pin, PinMode mode) { - if (pin == NC) return; + MBED_ASSERT(pin != (PinName)NC); uint32_t port_index = STM_PORT(pin); uint32_t pin_index = STM_PIN(pin); @@ -121,7 +122,8 @@ // Configure pull-up/pull-down resistors uint32_t pupd = (uint32_t)mode; - if (pupd > 2) pupd = 0; // Open-drain = No pull-up/No pull-down + if (pupd > 2) + pupd = 0; // Open-drain = No pull-up/No pull-down gpio->PUPDR &= (uint32_t)(~(GPIO_PUPDR_PUPDR0 << (pin_index * 2))); gpio->PUPDR |= (uint32_t)(pupd << (pin_index * 2));
--- a/targets/hal/TARGET_STM/TARGET_NUCLEO_L152RE/pwmout_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_NUCLEO_L152RE/pwmout_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -27,13 +27,13 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************************************************************* */ +#include "mbed_assert.h" #include "pwmout_api.h" #if DEVICE_PWMOUT #include "cmsis.h" #include "pinmap.h" -#include "error.h" // TIM5 cannot be used because already used by the us_ticker static const PinMap PinMap_PWM[] = { @@ -77,10 +77,7 @@ 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); - - if (obj->pwm == (PWMName)NC) { - error("PWM pinout mapping failed"); - } + MBED_ASSERT(obj->pwm != (PWMName)NC); // Enable TIM clock if (obj->pwm == PWM_2) RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
--- a/targets/hal/TARGET_STM/TARGET_NUCLEO_L152RE/serial_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_NUCLEO_L152RE/serial_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -27,13 +27,13 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************************************************************* */ +#include "mbed_assert.h" #include "serial_api.h" #if DEVICE_SERIAL #include "cmsis.h" #include "pinmap.h" -#include "error.h" #include <string.h> static const PinMap PinMap_UART_TX[] = { @@ -91,10 +91,7 @@ // Get the peripheral name (UART_1, UART_2, ...) from the pin and assign it to the object obj->uart = (UARTName)pinmap_merge(uart_tx, uart_rx); - - if (obj->uart == (UARTName)NC) { - error("Serial pinout mapping failed"); - } + MBED_ASSERT(obj->uart != (UARTName)NC); // Enable USART clock if (obj->uart == UART_1) {
--- a/targets/hal/TARGET_STM/TARGET_NUCLEO_L152RE/spi_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_NUCLEO_L152RE/spi_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -27,6 +27,7 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************************************************************* */ +#include "mbed_assert.h" #include "spi_api.h" #if DEVICE_SPI @@ -34,7 +35,6 @@ #include <math.h> #include "cmsis.h" #include "pinmap.h" -#include "error.h" static const PinMap PinMap_SPI_MOSI[] = { {PA_7, SPI_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_SPI1)}, @@ -105,10 +105,7 @@ SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel); obj->spi = (SPIName)pinmap_merge(spi_data, spi_cntl); - - if (obj->spi == (SPIName)NC) { - error("SPI pinout mapping failed"); - } + MBED_ASSERT(obj->spi != (SPIName)NC); // Enable SPI clock if (obj->spi == SPI_1) {
--- a/targets/hal/TARGET_STM/TARGET_STM32F3XX/analogin_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_STM32F3XX/analogin_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -25,6 +25,7 @@ * 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. */ +#include "mbed_assert.h" #include "analogin_api.h" #include "wait_api.h" @@ -32,7 +33,6 @@ #include "cmsis.h" #include "pinmap.h" -#include "error.h" static const PinMap PinMap_ADC[] = { {PA_0, ADC_1, STM_PIN_DATA(GPIO_Mode_AN, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0xFF)}, // ADC_IN1 @@ -63,10 +63,7 @@ // Get the peripheral name from the pin and assign it to the object obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC); - - if (obj->adc == (ADCName)NC) { - error("ADC pin mapping failed"); - } + MBED_ASSERT(obj->adc == (ADCName)NC); // Configure GPIO pinmap_pinout(pin, PinMap_ADC);
--- a/targets/hal/TARGET_STM/TARGET_STM32F3XX/analogout_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_STM32F3XX/analogout_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -25,13 +25,13 @@ * 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. */ +#include "mbed_assert.h" #include "analogout_api.h" #if DEVICE_ANALOGOUT #include "cmsis.h" #include "pinmap.h" -#include "error.h" #define RANGE_12BIT (0xFFF) @@ -46,10 +46,7 @@ // Get the peripheral name (DAC_1, ...) from the pin and assign it to the object obj->dac = (DACName)pinmap_peripheral(pin, PinMap_DAC); - - if (obj->dac == (DACName)NC) { - error("DAC pin mapping failed"); - } + MBED_ASSERT(obj->dac == (DACName)NC); dac = (DAC_TypeDef *)(obj->dac);
--- a/targets/hal/TARGET_STM/TARGET_STM32F3XX/gpio_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_STM32F3XX/gpio_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -27,6 +27,7 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************************************************************* */ +#include "mbed_assert.h" #include "gpio_api.h" #include "pinmap.h" #include "error.h" @@ -34,7 +35,7 @@ extern uint32_t Set_GPIO_Clock(uint32_t port_idx); uint32_t gpio_set(PinName pin) { - if (pin == NC) return 0; + MBED_ASSERT(pin != (PinName)NC); pin_function(pin, STM_PIN_DATA(GPIO_Mode_IN, 0, GPIO_PuPd_NOPULL, 0xFF)); @@ -42,7 +43,9 @@ } void gpio_init(gpio_t *obj, PinName pin) { - if (pin == NC) return; + obj->pin = pin; + if (pin == (PinName)NC) + return; uint32_t port_index = STM_PORT(pin); @@ -51,7 +54,6 @@ GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add; // Fill GPIO object structure for future use - obj->pin = pin; obj->mask = gpio_set(pin); obj->reg_in = &gpio->IDR; obj->reg_set = &gpio->BSRR; @@ -63,6 +65,7 @@ } 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, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0xFF)); } else { // PIN_INPUT
--- a/targets/hal/TARGET_STM/TARGET_STM32F3XX/gpio_object.h Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_STM32F3XX/gpio_object.h Wed Jun 11 16:00:09 2014 +0100 @@ -30,6 +30,7 @@ #ifndef MBED_GPIO_OBJECT_H #define MBED_GPIO_OBJECT_H +#include "mbed_assert.h" #include "cmsis.h" #include "PortNames.h" #include "PeripheralNames.h" @@ -48,6 +49,7 @@ } gpio_t; static inline void gpio_write(gpio_t *obj, int value) { + MBED_ASSERT(obj->pin != (PinName)NC); if (value) { *obj->reg_set = obj->mask; } else { @@ -56,6 +58,7 @@ } static inline int gpio_read(gpio_t *obj) { + MBED_ASSERT(obj->pin != (PinName)NC); return ((*obj->reg_in & obj->mask) ? 1 : 0); }
--- a/targets/hal/TARGET_STM/TARGET_STM32F3XX/i2c_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_STM32F3XX/i2c_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -27,13 +27,13 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************************************************************* */ +#include "mbed_assert.h" #include "i2c_api.h" #if DEVICE_I2C #include "cmsis.h" #include "pinmap.h" -#include "error.h" /* Timeout values for flags and events waiting loops. These timeouts are not based on accurate values, they just guarantee that the application will @@ -68,10 +68,7 @@ I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL); obj->i2c = (I2CName)pinmap_merge(i2c_sda, i2c_scl); - - if (obj->i2c == (I2CName)NC) { - error("I2C pin mapping failed"); - } + MBED_ASSERT(obj->i2c != (I2CName)NC); // Enable I2C clock if (obj->i2c == I2C_1) { @@ -98,6 +95,7 @@ } void i2c_frequency(i2c_t *obj, int hz) { + MBED_ASSERT((hz == 100000) || (hz == 200000) || (hz == 400000) || (hz == 1000000)); I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); I2C_InitTypeDef I2C_InitStructure; uint32_t tim; @@ -140,7 +138,6 @@ } break; default: - error("Only 100kHz, 200kHz, 400kHz and 1MHz I2C frequencies are supported."); break; }
--- a/targets/hal/TARGET_STM/TARGET_STM32F3XX/pinmap.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_STM32F3XX/pinmap.c Wed Jun 11 16:00:09 2014 +0100 @@ -27,6 +27,7 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************************************************************* */ +#include "mbed_assert.h" #include "pinmap.h" #include "PortNames.h" #include "error.h" @@ -71,7 +72,7 @@ * Configure pin (mode, speed, output type and pull-up/pull-down) */ void pin_function(PinName pin, int data) { - if (pin == NC) return; + MBED_ASSERT(pin != (PinName)NC); // Get the pin informations uint32_t mode = STM_PIN_MODE(data); @@ -115,7 +116,7 @@ * Configure pin pull-up/pull-down */ void pin_mode(PinName pin, PinMode mode) { - if (pin == NC) return; + MBED_ASSERT(pin != (PinName)NC); uint32_t port_index = STM_PORT(pin); uint32_t pin_index = STM_PIN(pin); @@ -126,7 +127,8 @@ // Configure pull-up/pull-down resistors uint32_t pupd = (uint32_t)mode; - if (pupd > 2) pupd = 0; // Open-drain = No pull-up/No pull-down + if (pupd > 2) + pupd = 0; // Open-drain = No pull-up/No pull-down gpio->PUPDR &= (uint32_t)(~(GPIO_PUPDR_PUPDR0 << (pin_index * 2))); gpio->PUPDR |= (uint32_t)(pupd << (pin_index * 2));
--- a/targets/hal/TARGET_STM/TARGET_STM32F3XX/pwmout_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_STM32F3XX/pwmout_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -27,11 +27,11 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************************************************************* */ +#include "mbed_assert.h" #include "pwmout_api.h" #include "cmsis.h" #include "pinmap.h" -#include "error.h" // TIM2 cannot be used because already used by the us_ticker static const PinMap PinMap_PWM[] = { @@ -88,10 +88,7 @@ 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); - - if (obj->pwm == (PWMName)NC) { - error("PWM pinout mapping failed"); - } + MBED_ASSERT(obj->pwm == (PWMName)NC); // Enable TIM clock if (obj->pwm == PWM_1) RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
--- a/targets/hal/TARGET_STM/TARGET_STM32F3XX/serial_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_STM32F3XX/serial_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -27,10 +27,10 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************************************************************* */ +#include "mbed_assert.h" #include "serial_api.h" #include "cmsis.h" #include "pinmap.h" -#include "error.h" #include <string.h> static const PinMap PinMap_UART_TX[] = { @@ -92,10 +92,7 @@ // Get the peripheral name from the pin and assign it to the object obj->uart = (UARTName)pinmap_merge(uart_tx, uart_rx); - - if (obj->uart == (UARTName)NC) { - error("Serial pinout mapping failed"); - } + MBED_ASSERT(obj->uart != (UARTName)NC); // Enable USART clock if (obj->uart == UART_1) {
--- a/targets/hal/TARGET_STM/TARGET_STM32F3XX/spi_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_STM32F3XX/spi_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -27,6 +27,7 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************************************************************* */ +#include "mbed_assert.h" #include "spi_api.h" #if DEVICE_SPI @@ -34,7 +35,6 @@ #include <math.h> #include "cmsis.h" #include "pinmap.h" -#include "error.h" static const PinMap PinMap_SPI_MOSI[] = { {PA_11, SPI_2, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_DOWN, GPIO_AF_5)}, @@ -102,9 +102,7 @@ obj->spi = (SPIName)pinmap_merge(spi_data, spi_cntl); - if (obj->spi == (SPIName)NC) { - error("SPI pinout mapping failed"); - } + MBED_ASSERT(obj->spi != (SPIName)NC); // Enable SPI clock if (obj->spi == SPI_2) {
--- a/targets/hal/TARGET_STM/TARGET_STM32F4XX/analogin_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_STM32F4XX/analogin_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "mbed_assert.h" #include "analogin_api.h" #if DEVICE_ANALOGIN @@ -48,9 +49,7 @@ void analogin_init(analogin_t *obj, PinName pin) { obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC); - if (obj->adc == (uint32_t)NC) { - error("ADC pin mapping failed"); - } + MBED_ASSERT(obj->adc != (uint32_t)NC); // ensure power is turned on RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN | RCC_AHB1ENR_GPIOBEN |
--- a/targets/hal/TARGET_STM/TARGET_STM32F4XX/gpio_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_STM32F4XX/gpio_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,10 +13,12 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "mbed_assert.h" #include "gpio_api.h" #include "pinmap.h" uint32_t gpio_set(PinName pin) { + MBED_ASSERT(pin != (PinName)NC); uint32_t port_index = (uint32_t) pin >> 4; // Enable GPIO peripheral clock @@ -27,9 +29,10 @@ } void gpio_init(gpio_t *obj, PinName pin) { - if(pin == NC) return; + obj->pin = pin; + if (pin == (PinName)NC) + return; - obj->pin = pin; obj->mask = gpio_set(pin); uint32_t port_index = (uint32_t) pin >> 4; @@ -46,8 +49,13 @@ } void gpio_dir(gpio_t *obj, PinDirection direction) { + MBED_ASSERT(obj->pin != (PinName)NC); switch (direction) { - case PIN_INPUT : pin_function(obj->pin, STM_PIN_DATA(0, 0)); break; - case PIN_OUTPUT: pin_function(obj->pin, STM_PIN_DATA(1, 0)); break; + case PIN_INPUT : + pin_function(obj->pin, STM_PIN_DATA(0, 0)); + break; + case PIN_OUTPUT: + pin_function(obj->pin, STM_PIN_DATA(1, 0)); + break; } }
--- a/targets/hal/TARGET_STM/TARGET_STM32F4XX/gpio_object.h Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_STM32F4XX/gpio_object.h Wed Jun 11 16:00:09 2014 +0100 @@ -16,6 +16,8 @@ #ifndef MBED_GPIO_OBJECT_H #define MBED_GPIO_OBJECT_H +#include "mbed_assert.h" + #ifdef __cplusplus extern "C" { #endif @@ -32,6 +34,7 @@ } gpio_t; static inline void gpio_write(gpio_t *obj, int value) { + MBED_ASSERT(obj->pin != (PinName)NC); if (value) *obj->reg_set = obj->mask; else @@ -39,6 +42,7 @@ } static inline int gpio_read(gpio_t *obj) { + MBED_ASSERT(obj->pin != (PinName)NC); return ((*obj->reg_in & obj->mask) ? 1 : 0); }
--- a/targets/hal/TARGET_STM/TARGET_STM32F4XX/i2c_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_STM32F4XX/i2c_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "mbed_assert.h" #include "i2c_api.h" #if DEVICE_I2C @@ -124,10 +125,7 @@ I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA); I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL); obj->i2c = (I2C_TypeDef *)pinmap_merge(i2c_sda, i2c_scl); - - if ((int)obj->i2c == NC) { - error("I2C pin mapping failed"); - } + MBED_ASSERT((int)obj->i2c != NC); // enable power i2c_power_enable(obj);
--- a/targets/hal/TARGET_STM/TARGET_STM32F4XX/pinmap.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_STM32F4XX/pinmap.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "mbed_assert.h" #include "pinmap.h" #include "error.h" @@ -20,7 +21,7 @@ * Set the pin into input, output, alternate function or analog mode */ void pin_function(PinName pin, int data) { - if (pin == (uint32_t)NC) return; + MBED_ASSERT(pin != (PinName)NC); int mode = STM_PIN_MODE(data); int func = STM_PIN_FUNC(data); @@ -53,7 +54,7 @@ } void pin_mode(PinName pin, PinMode mode) { - if (pin == (uint32_t)NC) { return; } + MBED_ASSERT(pin != (PinName)NC); uint32_t pin_number = (uint32_t)pin; int port_index = pin_number >> 4;
--- a/targets/hal/TARGET_STM/TARGET_STM32F4XX/spi_api.c Wed Jun 11 09:45:09 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_STM32F4XX/spi_api.c Wed Jun 11 16:00:09 2014 +0100 @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "mbed_assert.h" #include "spi_api.h" #if DEVICE_SPI @@ -20,7 +21,6 @@ #include "cmsis.h" #include "pinmap.h" -#include "error.h" static const PinMap PinMap_SPI_SCLK[] = { {PA_5, SPI_1, STM_PIN_DATA(2, 5)}, @@ -57,7 +57,7 @@ {PA_4, SPI_3, STM_PIN_DATA(2, 6)}, {PA_15, SPI_1, STM_PIN_DATA(2, 5)}, {PA_15, SPI_3, STM_PIN_DATA(2, 6)}, - {PB_9, SPI_2, STM_PIN_DATA(2, 5)}, + {PB_9, SPI_2, STM_PIN_DATA(2, 5)}, {PB_12, SPI_2, STM_PIN_DATA(2, 5)}, {NC, NC, 0} }; @@ -75,9 +75,7 @@ SPIName spi_data = (SPIName)pinmap_merge(spi_mosi, spi_miso); SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel); obj->spi = (SPI_TypeDef*)pinmap_merge(spi_data, spi_cntl); - if ((int)obj->spi == NC) { - error("SPI pinout mapping failed"); - } + MBED_ASSERT((int)obj->spi != NC) // enable power and clocking switch ((int)obj->spi) { @@ -123,12 +121,8 @@ void spi_free(spi_t *obj) {} void spi_format(spi_t *obj, int bits, int mode, int slave) { + MBED_ASSERT(((bits == 8) || (bits == 16)) && ((mode >= 0) && (mode <= 3))); ssp_disable(obj); - - if (!(bits == 8 || bits == 16) || !(mode >= 0 && mode <= 3)) { - error("SPI format error"); - } - int polarity = (mode & 0x2) ? 1 : 0; int phase = (mode & 0x1) ? 1 : 0;