All targets except STM - asserts for checking presumptions (function parameters)

pull/316/head
0xc0170 2014-05-16 15:45:12 +01:00
parent 09fe00f041
commit fb90157c9a
71 changed files with 289 additions and 492 deletions

View File

@ -13,11 +13,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <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 @@ static const PinMap PinMap_ADC[] = {
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");
assert(obj->adc != (ADCName)NC);
SIM->SCGC6 |= SIM_SCGC6_ADC0_MASK;

View File

@ -13,11 +13,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <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 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
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");
assert((int)obj->i2c != NC);
SIM->SCGC4 |= SIM_SCGC4_I2C0_MASK;
SIM->SCGC5 |= SIM_SCGC5_PORTB_MASK;

View File

@ -13,11 +13,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <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 @@ static float pwm_clock = 0;
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");
assert(pwm != (PWMName)NC);
uint32_t clkdiv = 0;
float clkval = SystemCoreClock / 1000000.0f;

View File

@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <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 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) {
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");
assert((int)uart != NC);
obj->uart = (UART_Type *)uart;
// enable clk

View File

@ -13,13 +13,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <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 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel
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");
}
assert((int)obj->spi != NC);
SIM->SCGC5 |= SIM_SCGC5_PORTC_MASK | SIM_SCGC5_PORTD_MASK;
SIM->SCGC6 |= SIM_SCGC6_SPI0_MASK;

View File

@ -13,11 +13,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <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");
}
assert(obj->adc != (ADCName)NC);
SIM->SCGC6 |= SIM_SCGC6_ADC0_MASK;

View File

@ -13,21 +13,18 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <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");
}
assert(obj->dac != (DACName)NC);
SIM->SCGC6 |= SIM_SCGC6_DAC0_MASK;

View File

@ -13,11 +13,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <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 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
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");
}
assert(obj->i2c != (PinName)NC);
// enable power
switch ((int)obj->i2c) {

View File

@ -13,11 +13,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <assert.h>
#include "pwmout_api.h"
#include "cmsis.h"
#include "pinmap.h"
#include "error.h"
#include "clk_freqs.h"
#include "PeripheralPins.h"
@ -26,8 +26,7 @@ static float pwm_clock;
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");
assert(pwm != (PWMName)NC);
uint32_t clkdiv = 0;
float clkval;

View File

@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <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 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) {
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");
}
assert((int)uart != NC);
obj->uart = (UARTLP_Type *)uart;
// enable clk

View File

@ -13,11 +13,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <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 @@ static const PinMap PinMap_ADC[] = {
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");
}
assert(obj->adc != (ADCName)NC);
uint32_t instance = obj->adc >> ADC_INSTANCE_SHIFT;
clock_manager_set_gate(kClockModuleADC, instance, true);

View File

@ -13,11 +13,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <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 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
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");
}
assert((int)obj->instance != NC);
clock_manager_set_gate(kClockModuleI2C, obj->instance, true);
clock_manager_set_gate(kClockModulePORT, sda >> GPIO_PORT_SHIFT, true);

View File

@ -13,11 +13,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <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 @@ static float pwm_clock_mhz;
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");
}
assert(pwm != (PWMName)NC);
obj->pwm_name = pwm;
uint32_t pwm_base_clock;

View File

@ -17,12 +17,12 @@
// math.h required for floating point operations for baud rate calculation
#include <math.h>
#include <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 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) {
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");
}
assert((int)obj->index != NC);
uart_config_t uart_config;
uart_config.baudRate = 9600;

View File

@ -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 <assert.h>
#include "spi_api.h"
#include "cmsis.h"
#include "pinmap.h"
#include "error.h"
@ -93,9 +93,7 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel
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");
}
assert((int)obj->instance != NC);
// enable power and clocking
clock_manager_set_gate(kClockModuleSPI, obj->instance, true);

View File

@ -13,10 +13,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <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 @@ void analogin_init(analogin_t *obj, PinName pin) {
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");
}
assert(obj->adc != (ADCName)NC);
while (map->pin != NC) {
if (map->pin == pin){

View File

@ -13,12 +13,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <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},
@ -64,9 +62,7 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
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");
}
assert((int)obj->i2c != NC);
obj->scl=scl;
obj->sda=sda;

View File

@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <assert.h>
#include "pwmout_api.h"
#include "cmsis.h"
#include "pinmap.h"
@ -214,9 +215,8 @@ void pwmout_init(pwmout_t* obj, PinName pin) {
uint8_t pwmOutSuccess = 0;
PWMName pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM);
if (pwm == (PWMName)NC){
error("PwmOut pin mapping failed");
}
assert(pwm != (PWMName)NC);
if(PWM_taken[(uint8_t)pwm]){
for(uint8_t i = 1; !pwmOutSuccess && (i<NO_PWMS) ;i++){

View File

@ -16,11 +16,12 @@
// math.h required for floating point operations for baud rate calculation
//#include <math.h>
#include <string.h>
#include <assert.h>
#include "serial_api.h"
#include "cmsis.h"
#include "pinmap.h"
#include "error.h"
/******************************************************************************
* INITIALIZATION
******************************************************************************/
@ -65,9 +66,7 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) {
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");
}
assert((int)uart != NC);
obj->uart = (NRF_UART_Type *)uart;

View File

@ -14,6 +14,7 @@
* limitations under the License.
*/
//#include <math.h>
#include <assert.h>
#include "spi_api.h"
#include "cmsis.h"
#include "pinmap.h"
@ -68,10 +69,8 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel
obj->spi = (NRF_SPI_Type*)NC;
obj->spis = (NRF_SPIS_Type*)spi;
}
assert((int)obj->spi != NC && (int)obj->spis != NC);
if ((int)obj->spi == NC && (int)obj->spis == NC) {
error("SPI pinout mapping failed");
}
// pin out the spi pins
if (ssel != NC) {//slave
obj->spis->POWER=0;

View File

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <assert.h>
#include "analogin_api.h"
#include "cmsis.h"
#include "pinmap.h"
@ -50,9 +50,8 @@ static const PinMap PinMap_ADC[] = {
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");
}
assert(obj->adc != (ADCName)NC);
pinmap_pinout(pin, PinMap_ADC);
// ADC Powered

View File

@ -13,11 +13,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <assert.h>
#include "i2c_api.h"
#include "cmsis.h"
#include "pinmap.h"
#include "error.h"
#if DEVICE_I2C
@ -96,10 +95,7 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
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");
}
assert((int)obj->i2c != NC);
// enable power
i2c_power_enable(obj);

View File

@ -14,6 +14,7 @@
* limitations under the License.
*/
// math.h required for floating point operations for baud rate calculation
#include <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"
#if DEVICE_SERIAL
#warning "[TODO] support from UART_1 to UART_4"
@ -68,9 +68,7 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) {
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");
}
assert((int)uart != NC);
obj->uart = (LPC_USART0_Type *)uart;
LPC_SYSCON->SYSAHBCLKCTRL |= ((1<<12) | (1<<20) | (1<<21) | (1<<22));
@ -205,16 +203,11 @@ void serial_baud(serial_t *obj, int baudrate) {
}
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");
}
stop_bits -= 1;
assert((stop_bits == 1) || (stop_bits == 2)); // 0: 1 stop bits, 1: 2 stop bits
assert((data_bits > 4) || (data_bits < 9)); // 0: 5 data bits ... 3: 8 data bits
assert(parity < (ParityForced0 + 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);
}
stop_bits -= 1;
data_bits -= 5;
int parity_enable, parity_select;
@ -225,8 +218,7 @@ void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_b
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

View File

@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <assert.h>
#include <math.h>
#include "spi_api.h"
@ -68,10 +69,7 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel
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");
}
assert((int)obj->spi == NC);
// enable power and clocking
switch ((int)obj->spi) {
@ -111,10 +109,7 @@ void spi_free(spi_t *obj) {}
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");
}
assert((bits >= 4 && bits <= 16) || (mode >= 0 && mode <= 3));
int polarity = (mode & 0x2) ? 1 : 0;
int phase = (mode & 0x1) ? 1 : 0;

View File

@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <assert.h>
#include "analogin_api.h"
#include "cmsis.h"
#include "pinmap.h"
@ -46,9 +47,7 @@ static const PinMap PinMap_ADC[] = {
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");
}
assert(obj->adc != (ADCName)NC);
// Power up ADC
LPC_SYSCON->PDRUNCFG &= ~ (1 << 4);

View File

@ -13,10 +13,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <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 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
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");
}
assert((int)obj->i2c != NC);
// enable power
i2c_power_enable(obj);

View File

@ -13,10 +13,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <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,8 +69,7 @@ static LPC_CTxxBx_Type *Timers[4] = {
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");
assert(pwm != (PWMName)NC);
obj->pwm = pwm;

View File

@ -21,7 +21,6 @@
#include "serial_api.h"
#include "cmsis.h"
#include "pinmap.h"
#include "error.h"
/******************************************************************************
* INITIALIZATION
@ -55,9 +54,7 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) {
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");
}
assert((int)uart != NC);
obj->uart = (LPC_USART_Type *)uart;
LPC_SYSCON->SYSAHBCLKCTRL |= (1<<12);
@ -188,16 +185,11 @@ void serial_baud(serial_t *obj, int baudrate) {
}
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");
}
stop_bits -= 1;
assert((stop_bits == 1) || (stop_bits == 2)); // 0: 1 stop bits, 1: 2 stop bits
assert((data_bits > 4) || (data_bits < 9)); // 0: 5 data bits ... 3: 8 data bits
assert(parity < (ParityForced0 + 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);
}
stop_bits -= 1;
data_bits -= 5;
int parity_enable, parity_select;
@ -208,8 +200,7 @@ void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_b
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

View File

@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <assert.h>
#include <math.h>
#include "spi_api.h"
#include "cmsis.h"
@ -62,10 +63,7 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel
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");
}
assert((int)obj->spi != NC);
// enable power and clocking
switch ((int)obj->spi) {
@ -104,11 +102,9 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel
void spi_free(spi_t *obj) {}
void spi_format(spi_t *obj, int bits, int mode, int slave) {
ssp_disable(obj);
assert((bits >= 4 && bits <= 16) || (mode >= 0 && mode <= 3));
if (!(bits >= 4 && bits <= 16) || !(mode >= 0 && mode <= 3)) {
error("SPI format error");
}
ssp_disable(obj);
int polarity = (mode & 0x2) ? 1 : 0;
int phase = (mode & 0x1) ? 1 : 0;
@ -157,7 +153,8 @@ void spi_frequency(spi_t *obj, int hz) {
// divider
obj->spi->CR0 &= ~(0xFFFF << 8);
obj->spi->CR0 |= (divider - 1) << 8;
obj->spi->CR0 |= (divider - 1) << 8
;
ssp_enable(obj);
return;
}

View File

@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <assert.h>
#include "analogin_api.h"
#include "cmsis.h"
#include "pinmap.h"
@ -43,10 +44,7 @@ static inline int div_round_up(int x, int y) {
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;
}
assert(obj->adc != (uint32_t)NC);
// Power up ADC
LPC_SYSCON->PDRUNCFG &= ~ (1 << 4);

View File

@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <assert.h>
#include "i2c_api.h"
#include "cmsis.h"
#include "pinmap.h"
@ -87,10 +88,7 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
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");
}
assert((int)obj->i2c != NC);
// enable power
i2c_power_enable(obj);

View File

@ -13,10 +13,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <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,8 +64,7 @@ static LPC_TMR_TypeDef *Timers[3] = {
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");
assert(pwm != (uint32_t)NC);
obj->pwm = pwm;

View File

@ -14,6 +14,7 @@
* limitations under the License.
*/
// math.h required for floating point operations for baud rate calculation
#include <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 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) {
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");
}
assert((int)uart != NC);
obj->uart = (LPC_UART_TypeDef *)uart;
LPC_SYSCON->SYSAHBCLKCTRL |= (1<<12);
@ -183,16 +181,11 @@ void serial_baud(serial_t *obj, int baudrate) {
}
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");
}
stop_bits -= 1;
assert((stop_bits == 1) || (stop_bits == 2)); // 0: 1 stop bits, 1: 2 stop bits
assert((data_bits > 4) || (data_bits < 9)); // 0: 5 data bits ... 3: 8 data bits
assert(parity < (ParityForced0 + 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);
}
stop_bits -= 1;
data_bits -= 5;
int parity_enable, parity_select;
@ -203,8 +196,7 @@ void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_b
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

View File

@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <assert.h>
#include <math.h>
#include "spi_api.h"
#include "cmsis.h"
@ -58,10 +59,7 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel
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");
}
assert((int)obj->spi != NC);
// enable power and clocking
switch ((int)obj->spi) {
@ -100,12 +98,9 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel
void spi_free(spi_t *obj) {}
void spi_format(spi_t *obj, int bits, int mode, int slave) {
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;

View File

@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <assert.h>
#include "analogin_api.h"
#include "cmsis.h"
#include "pinmap.h"
@ -46,9 +47,7 @@ static const PinMap PinMap_ADC[] = {
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");
}
assert(obj->adc != (ADCName)NC);
// Power up ADC
LPC_SYSCON->PDRUNCFG &= ~ (1 << 4);

View File

@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <assert.h>
#include "i2c_api.h"
#include "cmsis.h"
#include "pinmap.h"
@ -87,10 +88,7 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
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");
}
assert((int)obj->i2c != NC);
// enable power
i2c_power_enable(obj);

View File

@ -13,12 +13,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <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,8 +71,7 @@ static unsigned int pwm_clock_mhz;
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");
assert(pwm != (uint32_t)NC);
obj->pwm = pwm;

View File

@ -14,6 +14,7 @@
* limitations under the License.
*/
// math.h required for floating point operations for baud rate calculation
#include <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 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) {
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");
}
assert((int)uart != NC);
obj->uart = (LPC_USART_Type *)uart;
LPC_SYSCON->SYSAHBCLKCTRL |= (1<<12);
@ -186,16 +184,11 @@ void serial_baud(serial_t *obj, int baudrate) {
}
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");
}
stop_bits -= 1;
assert((stop_bits == 1) || (stop_bits == 2)); // 0: 1 stop bits, 1: 2 stop bits
assert((data_bits > 4) || (data_bits < 9)); // 0: 5 data bits ... 3: 8 data bits
assert(parity < (ParityForced0 + 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);
}
stop_bits -= 1;
data_bits -= 5;
int parity_enable, parity_select;
@ -206,8 +199,7 @@ void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_b
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

View File

@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <assert.h>
#include <math.h>
#include "spi_api.h"
#include "cmsis.h"
@ -62,10 +63,7 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel
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");
}
assert((int)obj->spi != NC);
// enable power and clocking
switch ((int)obj->spi) {
@ -105,10 +103,7 @@ void spi_free(spi_t *obj) {}
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");
}
assert((bits >= 4 && bits <= 16) || (mode >= 0 && mode <= 3));
int polarity = (mode & 0x2) ? 1 : 0;
int phase = (mode & 0x1) ? 1 : 0;

View File

@ -13,11 +13,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <assert.h>
#include "analogin_api.h"
#include "cmsis.h"
#include "pinmap.h"
#include "error.h"
#define ANALOGIN_MEDIAN_FILTER 1
@ -55,9 +54,8 @@ static const PinMap PinMap_ADC[] = {
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");
}
assert(obj->adc != (ADCName)NC);
uint32_t port = (pin >> 5);
// enable clock for GPIOx
LPC_SYSCON->SYSAHBCLKCTRL0 |= (1UL << (14 + port));

View File

@ -13,15 +13,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <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");
}
assert(pin == P0_12);
LPC_SYSCON->SYSAHBCLKCTRL0 |= (1 << 29);
LPC_SYSCON->PDRUNCFG &= ~(1 << 12);

View File

@ -13,11 +13,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <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 @@ static inline void i2c_interface_enable(i2c_t *obj) {
}
void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
if ((sda != P0_23) | (scl != P0_22)) {
error("I2C pin mapping failed");
}
assert((sda == P0_23) || (scl == P0_22));
// Enables clock for I2C0
LPC_SYSCON->SYSAHBCLKCTRL1 |= (1 << 13);

View File

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <assert.h>
#include "pwmout_api.h"
#include "cmsis.h"
#include "pinmap.h"
@ -38,8 +38,7 @@ static int get_available_sct(void) {
}
void pwmout_init(pwmout_t* obj, PinName pin) {
if (pin == (uint32_t)NC)
error("PwmOut pin mapping failed");
assert(pin != (uint32_t)NC);
int sct_n = get_available_sct();
if (sct_n == -1) {

View File

@ -14,6 +14,7 @@
* limitations under the License.
*/
// math.h required for floating point operations for baud rate calculation
#include <assert.h>
#include <math.h>
#include <string.h>
@ -195,16 +196,11 @@ void serial_baud(serial_t *obj, int baudrate) {
}
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");
}
stop_bits -= 1;
assert((stop_bits == 1) || (stop_bits == 2)); // 0: 1 stop bits, 1: 2 stop bits
assert((data_bits > 6) || (data_bits < 10)); // 0: 7 data bits ... 2: 9 data bits
assert(parity < (ParityEven + 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);
}
stop_bits -= 1;
data_bits -= 7;
int paritysel;
@ -213,8 +209,7 @@ void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_b
case ParityEven: paritysel = 2; break;
case ParityOdd : paritysel = 3; break;
default:
error("Invalid serial parity setting");
return;
break;
}
obj->uart->CFG = (data_bits << 2)

View File

@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <assert.h>
#include <math.h>
#include "spi_api.h"
@ -126,10 +127,7 @@ void spi_free(spi_t *obj) {}
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");
}
assert((bits >= 1 && bits <= 16) || (mode >= 0 && mode <= 3));
int polarity = (mode & 0x2) ? 1 : 0;
int phase = (mode & 0x1) ? 1 : 0;

View File

@ -13,11 +13,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <assert.h>
#include "analogin_api.h"
#include "cmsis.h"
#include "pinmap.h"
#include "error.h"
#define ANALOGIN_MEDIAN_FILTER 1
@ -44,9 +44,7 @@ static const PinMap PinMap_ADC[] = {
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");
}
assert(obj->adc != (ADCName)NC);
// ensure power is turned on
LPC_SC->PCONP |= (1 << 12);

View File

@ -13,11 +13,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <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 @@ static const PinMap PinMap_DAC[] = {
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");
}
assert(obj->dac == (DACName)NC);
// power is on by default, set DAC clk divider is /4
LPC_SC->PCLKSEL0 &= ~(0x3 << 22);

View File

@ -13,11 +13,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <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 @@ void can_init(can_t *obj, PinName rd, PinName td) {
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");
}
assert((int)obj->dev != NC);
switch ((int)obj->dev) {
case CAN_1: LPC_SC->PCONP |= 1 << 13; break;

View File

@ -13,10 +13,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <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 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
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");
}
assert((int)obj->i2c != NC);
// enable power
i2c_power_enable(obj);

View File

@ -13,10 +13,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <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,8 +57,7 @@ static unsigned int pwm_clock_mhz;
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");
assert(pwm != (PWMName)NC);
obj->pwm = pwm;
obj->MR = PWM_MATCH[pwm];

View File

@ -14,6 +14,7 @@
* limitations under the License.
*/
// math.h required for floating point operations for baud rate calculation
#include <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 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) {
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");
}
assert((int)uart != NC);
obj->uart = (LPC_UART_TypeDef *)uart;
// enable power
@ -150,6 +148,7 @@ void serial_free(serial_t *obj) {
// serial_baud
// set the baud rate, taking in to account the current SystemFrequency
void serial_baud(serial_t *obj, int baudrate) {
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 @@ void serial_baud(serial_t *obj, int baudrate) {
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,11 @@ void serial_baud(serial_t *obj, int baudrate) {
}
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");
}
stop_bits -= 1;
assert((stop_bits == 1) || (stop_bits == 2)); // 0: 1 stop bits, 1: 2 stop bits
assert((data_bits > 4) || (data_bits < 9)); // 0: 5 data bits ... 3: 8 data bits
assert(parity < (ParityForced0 + 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);
}
stop_bits -= 1;
data_bits -= 5;
int parity_enable, parity_select;
@ -265,8 +259,7 @@ void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_b
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

View File

@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <assert.h>
#include <math.h>
#include "spi_api.h"
@ -64,9 +65,7 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel
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");
}
assert((int)obj->spi != NC);
// enable power and clocking
switch ((int)obj->spi) {
@ -98,9 +97,7 @@ void spi_free(spi_t *obj) {}
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");
}
assert((bits >= 4 && bits <= 16) || (mode >= 0 && mode <= 3));
int polarity = (mode & 0x2) ? 1 : 0;
int phase = (mode & 0x1) ? 1 : 0;

View File

@ -13,10 +13,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <assert.h>
#include "analogin_api.h"
#include "cmsis.h"
#include "pinmap.h"
#include "error.h"
#define ANALOGIN_MEDIAN_FILTER 1
@ -42,9 +42,7 @@ static const PinMap PinMap_ADC[] = {
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");
}
assert(obj->adc != (ADCName)NC);
// ensure power is turned on
LPC_SC->PCONP |= (1 << 12);

View File

@ -13,10 +13,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <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 @@ static const PinMap PinMap_DAC[] = {
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");
}
assert(obj->dac != (DACName)NC);
// power is on by default, set DAC clk divider is /4
LPC_SC->PCLKSEL0 &= ~(0x3 << 22);

View File

@ -13,10 +13,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <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 @@ void can_init(can_t *obj, PinName rd, PinName td) {
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");
}
assert((int)obj->dev != NC);
switch ((int)obj->dev) {
case CAN_1: LPC_SC->PCONP |= 1 << 13; break;

View File

@ -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 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
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");
}
assert((int)obj->i2c != NC);
// enable power
i2c_power_enable(obj);

View File

@ -13,10 +13,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <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,8 +57,7 @@ static unsigned int pwm_clock_mhz;
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");
assert(pwm != (PWMName)NC);
obj->pwm = pwm;
obj->MR = PWM_MATCH[pwm];

View File

@ -14,6 +14,7 @@
* limitations under the License.
*/
// math.h required for floating point operations for baud rate calculation
#include <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 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) {
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");
}
assert((int)uart == NC);
obj->uart = (LPC_UART_TypeDef *)uart;
// enable power
@ -123,6 +121,7 @@ void serial_free(serial_t *obj) {
// serial_baud
// set the baud rate, taking in to account the current SystemFrequency
void serial_baud(serial_t *obj, int baudrate) {
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 @@ void serial_baud(serial_t *obj, int baudrate) {
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,11 @@ void serial_baud(serial_t *obj, int baudrate) {
}
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");
}
stop_bits -= 1;
assert((stop_bits == 1) || (stop_bits == 2)); // 0: 1 stop bits, 1: 2 stop bits
assert((data_bits > 4) || (data_bits < 9)); // 0: 5 data bits ... 3: 8 data bits
assert(parity < (ParityForced0 + 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);
}
stop_bits -= 1;
data_bits -= 5;
int parity_enable, parity_select;
@ -238,8 +232,7 @@ void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_b
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

View File

@ -64,10 +64,7 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel
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");
}
assert((int)obj->spi != NC);
// enable power and clocking
switch ((int)obj->spi) {
@ -98,12 +95,9 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel
void spi_free(spi_t *obj) {}
void spi_format(spi_t *obj, int bits, int mode, int slave) {
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;

View File

@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <assert.h>
#include "analogin_api.h"
#include "cmsis.h"
#include "pinmap.h"
@ -43,9 +44,7 @@ static const PinMap PinMap_ADC[] = {
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");
}
assert(obj->adc != (ADCName)NC);
// ensure power is turned on
LPC_SC->PCONP |= (1 << 12);

View File

@ -13,10 +13,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <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 @@ static const PinMap PinMap_DAC[] = {
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");
}
assert(obj->dac != (DACName)NC);
// DAC enable bit must be set
LPC_IOCON->P0_26 |= (1 << 16); // DACEN

View File

@ -17,7 +17,6 @@
#include "cmsis.h"
#include "pinmap.h"
#include "error.h"
#include <math.h>
#include <string.h>
@ -244,9 +243,7 @@ void can_init(can_t *obj, PinName rd, PinName td) {
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");
}
assert((int)obj->dev != NC);
switch ((int)obj->dev) {
case CAN_1: LPC_SC->PCONP |= 1 << 13; break;

View File

@ -13,10 +13,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <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 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
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");
}
assert((int)obj->i2c != NC);
// enable power
i2c_power_enable(obj);

View File

@ -13,10 +13,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <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,8 +66,7 @@ static unsigned int pwm_clock_mhz;
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");
assert(pwm != (PWMName)NC);
obj->channel = pwm;
obj->pwm = LPC_PWM0;

View File

@ -72,9 +72,7 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) {
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");
}
assert((int)uart != NC);
obj->uart = (LPC_UART_TypeDef *)uart;
// enable power
@ -209,16 +207,11 @@ void serial_baud(serial_t *obj, int baudrate) {
}
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");
}
stop_bits -= 1;
assert((stop_bits == 1) || (stop_bits == 2)); // 0: 1 stop bits, 1: 2 stop bits
assert((data_bits > 4) || (data_bits < 9)); // 0: 5 data bits ... 3: 8 data bits
assert(parity < (ParityForced0 + 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);
}
stop_bits -= 1;
data_bits -= 5;
int parity_enable, parity_select;
@ -229,8 +222,7 @@ void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_b
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

View File

@ -84,9 +84,7 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel
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");
}
assert((int)obj->spi != NC);
// enable power and clocking
switch ((int)obj->spi) {
@ -118,12 +116,9 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel
void spi_free(spi_t *obj) {}
void spi_format(spi_t *obj, int bits, int mode, int slave) {
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;

View File

@ -15,10 +15,10 @@
*
* Ported to NXP LPC43XX by Micromint USA <support@micromint.com>
*/
#include <assert.h>
#include "analogin_api.h"
#include "cmsis.h"
#include "pinmap.h"
#include "error.h"
#define ANALOGIN_MEDIAN_FILTER 1
@ -41,10 +41,7 @@ void analogin_init(analogin_t *obj, PinName pin) {
uint8_t num, chan;
obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC);
if (obj->adc == (uint32_t)NC) {
error("ADC pin mapping failed");
}
assert(obj->adc != (ADCName)NC);
// Configure the pin as GPIO input
if (pin < SFP_AIO0) {

View File

@ -15,10 +15,10 @@
*
* Ported to NXP LPC43XX by Micromint USA <support@micromint.com>
*/
#include <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 @@ static const PinMap PinMap_DAC[] = {
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");
}
assert(obj->dac != (DACName)NC);
// Configure the pin as GPIO input
pin_function(pin, (SCU_PINIO_PULLNONE | 0x0));

View File

@ -192,16 +192,11 @@ void serial_baud(serial_t *obj, int baudrate) {
}
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");
}
stop_bits -= 1;
assert((stop_bits == 1) || (stop_bits == 2)); // 0: 1 stop bits, 1: 2 stop bits
assert((data_bits > 4) || (data_bits < 9)); // 0: 5 data bits ... 3: 8 data bits
assert(parity < (ParityForced0 + 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);
}
stop_bits -= 1;
data_bits -= 5;
int parity_enable, parity_select;
@ -212,8 +207,7 @@ void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_b
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

View File

@ -15,6 +15,7 @@
*
* Ported to NXP LPC43XX by Micromint USA <support@micromint.com>
*/
#include <assert.h>
#include <math.h>
#include "spi_api.h"
@ -59,9 +60,7 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel
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");
}
assert((int)obj->spi != NC);
// set default format and frequency
if (ssel == NC) {
@ -86,12 +85,9 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel
void spi_free(spi_t *obj) {}
void spi_format(spi_t *obj, int bits, int mode, int slave) {
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;

View File

@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <assert.h>
#include <math.h>
#include "spi_api.h"
@ -115,13 +116,9 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel
void spi_free(spi_t *obj) {}
void spi_format(spi_t *obj, int bits, int mode, int slave) {
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;