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 * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
#include <assert.h>
#include "analogin_api.h" #include "analogin_api.h"
#include "cmsis.h" #include "cmsis.h"
#include "pinmap.h" #include "pinmap.h"
#include "error.h"
#include "clk_freqs.h" #include "clk_freqs.h"
static const PinMap PinMap_ADC[] = { static const PinMap PinMap_ADC[] = {
@ -38,8 +38,7 @@ static const PinMap PinMap_ADC[] = {
void analogin_init(analogin_t *obj, PinName pin) { void analogin_init(analogin_t *obj, PinName pin) {
obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC); obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC);
if (obj->adc == (ADCName)NC) assert(obj->adc != (ADCName)NC);
error("ADC pin mapping failed");
SIM->SCGC6 |= SIM_SCGC6_ADC0_MASK; SIM->SCGC6 |= SIM_SCGC6_ADC0_MASK;

View File

@ -13,11 +13,11 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
#include <assert.h>
#include "i2c_api.h" #include "i2c_api.h"
#include "cmsis.h" #include "cmsis.h"
#include "pinmap.h" #include "pinmap.h"
#include "error.h"
#include "clk_freqs.h" #include "clk_freqs.h"
static const PinMap PinMap_I2C_SDA[] = { 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_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA);
I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL); I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL);
obj->i2c = (I2C_Type*)pinmap_merge(i2c_sda, i2c_scl); obj->i2c = (I2C_Type*)pinmap_merge(i2c_sda, i2c_scl);
if ((int)obj->i2c == NC) assert((int)obj->i2c != NC);
error("I2C pin mapping failed");
SIM->SCGC4 |= SIM_SCGC4_I2C0_MASK; SIM->SCGC4 |= SIM_SCGC4_I2C0_MASK;
SIM->SCGC5 |= SIM_SCGC5_PORTB_MASK; SIM->SCGC5 |= SIM_SCGC5_PORTB_MASK;

View File

@ -13,11 +13,11 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
#include <assert.h>
#include "pwmout_api.h" #include "pwmout_api.h"
#include "cmsis.h" #include "cmsis.h"
#include "pinmap.h" #include "pinmap.h"
#include "error.h"
static const PinMap PinMap_PWM[] = { static const PinMap PinMap_PWM[] = {
// LEDs // LEDs
@ -53,8 +53,7 @@ static float pwm_clock = 0;
void pwmout_init(pwmout_t* obj, PinName pin) { void pwmout_init(pwmout_t* obj, PinName pin) {
// determine the channel // determine the channel
PWMName pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM); PWMName pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM);
if (pwm == (PWMName)NC) assert(pwm != (PWMName)NC);
error("PwmOut pin mapping failed");
uint32_t clkdiv = 0; uint32_t clkdiv = 0;
float clkval = SystemCoreClock / 1000000.0f; float clkval = SystemCoreClock / 1000000.0f;

View File

@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
#include <assert.h>
#include "serial_api.h" #include "serial_api.h"
// math.h required for floating point operations for baud rate calculation // math.h required for floating point operations for baud rate calculation
@ -22,7 +23,6 @@
#include "cmsis.h" #include "cmsis.h"
#include "pinmap.h" #include "pinmap.h"
#include "error.h"
static const PinMap PinMap_UART_TX[] = { static const PinMap PinMap_UART_TX[] = {
{PTB17, UART_0, 3}, {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_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX);
UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX); UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX);
UARTName uart = (UARTName)pinmap_merge(uart_tx, uart_rx); UARTName uart = (UARTName)pinmap_merge(uart_tx, uart_rx);
if ((int)uart == NC) assert((int)uart != NC);
error("Serial pinout mapping failed");
obj->uart = (UART_Type *)uart; obj->uart = (UART_Type *)uart;
// enable clk // enable clk

View File

@ -13,13 +13,13 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
#include <assert.h>
#include "spi_api.h" #include "spi_api.h"
#include <math.h> #include <math.h>
#include "cmsis.h" #include "cmsis.h"
#include "pinmap.h" #include "pinmap.h"
#include "error.h"
#include "clk_freqs.h" #include "clk_freqs.h"
static const PinMap PinMap_SPI_SCLK[] = { 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); SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel);
obj->spi = (SPI_Type*)pinmap_merge(spi_data, spi_cntl); obj->spi = (SPI_Type*)pinmap_merge(spi_data, spi_cntl);
if ((int)obj->spi == NC) { assert((int)obj->spi != NC);
error("SPI pinout mapping failed");
}
SIM->SCGC5 |= SIM_SCGC5_PORTC_MASK | SIM_SCGC5_PORTD_MASK; SIM->SCGC5 |= SIM_SCGC5_PORTC_MASK | SIM_SCGC5_PORTD_MASK;
SIM->SCGC6 |= SIM_SCGC6_SPI0_MASK; SIM->SCGC6 |= SIM_SCGC6_SPI0_MASK;

View File

@ -13,11 +13,11 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
#include <assert.h>
#include "analogin_api.h" #include "analogin_api.h"
#include "cmsis.h" #include "cmsis.h"
#include "pinmap.h" #include "pinmap.h"
#include "error.h"
#include "clk_freqs.h" #include "clk_freqs.h"
#include "PeripheralPins.h" #include "PeripheralPins.h"
@ -27,9 +27,7 @@
void analogin_init(analogin_t *obj, PinName pin) { void analogin_init(analogin_t *obj, PinName pin) {
obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC); obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC);
if (obj->adc == (ADCName)NC) { assert(obj->adc != (ADCName)NC);
error("ADC pin mapping failed");
}
SIM->SCGC6 |= SIM_SCGC6_ADC0_MASK; SIM->SCGC6 |= SIM_SCGC6_ADC0_MASK;

View File

@ -13,21 +13,18 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
#include <assert.h>
#include "analogout_api.h" #include "analogout_api.h"
#include "cmsis.h" #include "cmsis.h"
#include "pinmap.h" #include "pinmap.h"
#include "error.h"
#include "PeripheralPins.h" #include "PeripheralPins.h"
#define RANGE_12BIT 0xFFF #define RANGE_12BIT 0xFFF
void analogout_init(dac_t *obj, PinName pin) { void analogout_init(dac_t *obj, PinName pin) {
obj->dac = (DACName)pinmap_peripheral(pin, PinMap_DAC); obj->dac = (DACName)pinmap_peripheral(pin, PinMap_DAC);
if (obj->dac == (DACName)NC) { assert(obj->dac != (DACName)NC);
error("DAC pin mapping failed");
}
SIM->SCGC6 |= SIM_SCGC6_DAC0_MASK; SIM->SCGC6 |= SIM_SCGC6_DAC0_MASK;

View File

@ -13,11 +13,11 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
#include <assert.h>
#include "i2c_api.h" #include "i2c_api.h"
#include "cmsis.h" #include "cmsis.h"
#include "pinmap.h" #include "pinmap.h"
#include "error.h"
#include "clk_freqs.h" #include "clk_freqs.h"
#include "PeripheralPins.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_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA);
I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL); I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL);
obj->i2c = (I2C_Type*)pinmap_merge(i2c_sda, i2c_scl); obj->i2c = (I2C_Type*)pinmap_merge(i2c_sda, i2c_scl);
if ((int)obj->i2c == NC) { assert(obj->i2c != (PinName)NC);
error("I2C pin mapping failed");
}
// enable power // enable power
switch ((int)obj->i2c) { switch ((int)obj->i2c) {

View File

@ -13,11 +13,11 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
#include <assert.h>
#include "pwmout_api.h" #include "pwmout_api.h"
#include "cmsis.h" #include "cmsis.h"
#include "pinmap.h" #include "pinmap.h"
#include "error.h"
#include "clk_freqs.h" #include "clk_freqs.h"
#include "PeripheralPins.h" #include "PeripheralPins.h"
@ -26,9 +26,8 @@ static float pwm_clock;
void pwmout_init(pwmout_t* obj, PinName pin) { void pwmout_init(pwmout_t* obj, PinName pin) {
// determine the channel // determine the channel
PWMName pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM); PWMName pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM);
if (pwm == (PWMName)NC) assert(pwm != (PWMName)NC);
error("PwmOut pin mapping failed");
uint32_t clkdiv = 0; uint32_t clkdiv = 0;
float clkval; float clkval;
if (mcgpllfll_frequency()) { if (mcgpllfll_frequency()) {
@ -37,11 +36,11 @@ void pwmout_init(pwmout_t* obj, PinName pin) {
} else { } else {
SIM->SOPT2 |= SIM_SOPT2_TPMSRC(2); // Clock source: ExtOsc SIM->SOPT2 |= SIM_SOPT2_TPMSRC(2); // Clock source: ExtOsc
clkval = extosc_frequency() / 1000000.0f; clkval = extosc_frequency() / 1000000.0f;
} }
while (clkval > 1) { while (clkval > 1) {
clkdiv++; clkdiv++;
clkval /= 2.0; clkval /= 2.0;
if (clkdiv == 7) if (clkdiv == 7)
break; break;
} }

View File

@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
#include <assert.h>
#include "serial_api.h" #include "serial_api.h"
// math.h required for floating point operations for baud rate calculation // math.h required for floating point operations for baud rate calculation
@ -22,7 +23,6 @@
#include "cmsis.h" #include "cmsis.h"
#include "pinmap.h" #include "pinmap.h"
#include "error.h"
#include "clk_freqs.h" #include "clk_freqs.h"
#include "PeripheralPins.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_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX);
UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX); UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX);
UARTName uart = (UARTName)pinmap_merge(uart_tx, uart_rx); UARTName uart = (UARTName)pinmap_merge(uart_tx, uart_rx);
if ((int)uart == NC) { assert((int)uart != NC);
error("Serial pinout mapping failed");
}
obj->uart = (UARTLP_Type *)uart; obj->uart = (UARTLP_Type *)uart;
// enable clk // enable clk
@ -290,7 +288,7 @@ void serial_pinout_tx(PinName tx) {
} }
void serial_break_set(serial_t *obj) { 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) { void serial_break_clear(serial_t *obj) {

View File

@ -13,11 +13,11 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
#include <assert.h>
#include "analogin_api.h" #include "analogin_api.h"
#include "cmsis.h" #include "cmsis.h"
#include "pinmap.h" #include "pinmap.h"
#include "error.h"
#include "PeripheralNames.h" #include "PeripheralNames.h"
#include "fsl_adc_hal.h" #include "fsl_adc_hal.h"
#include "fsl_clock_manager.h" #include "fsl_clock_manager.h"
@ -49,9 +49,8 @@ static const PinMap PinMap_ADC[] = {
void analogin_init(analogin_t *obj, PinName pin) { void analogin_init(analogin_t *obj, PinName pin) {
obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC); obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC);
if (obj->adc == (ADCName)NC) { assert(obj->adc != (ADCName)NC);
error("ADC pin mapping failed");
}
uint32_t instance = obj->adc >> ADC_INSTANCE_SHIFT; uint32_t instance = obj->adc >> ADC_INSTANCE_SHIFT;
clock_manager_set_gate(kClockModuleADC, instance, true); clock_manager_set_gate(kClockModuleADC, instance, true);

View File

@ -13,11 +13,11 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
#include <assert.h>
#include "i2c_api.h" #include "i2c_api.h"
#include "cmsis.h" #include "cmsis.h"
#include "pinmap.h" #include "pinmap.h"
#include "error.h"
#include "fsl_clock_manager.h" #include "fsl_clock_manager.h"
#include "fsl_i2c_hal.h" #include "fsl_i2c_hal.h"
#include "fsl_port_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_sda = pinmap_peripheral(sda, PinMap_I2C_SDA);
uint32_t i2c_scl = pinmap_peripheral(scl, PinMap_I2C_SCL); uint32_t i2c_scl = pinmap_peripheral(scl, PinMap_I2C_SCL);
obj->instance = pinmap_merge(i2c_sda, i2c_scl); obj->instance = pinmap_merge(i2c_sda, i2c_scl);
if ((int)obj->instance == NC) { assert((int)obj->instance != NC);
error("I2C pin mapping failed");
}
clock_manager_set_gate(kClockModuleI2C, obj->instance, true); clock_manager_set_gate(kClockModuleI2C, obj->instance, true);
clock_manager_set_gate(kClockModulePORT, sda >> GPIO_PORT_SHIFT, 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 * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
#include <assert.h>
#include "pwmout_api.h" #include "pwmout_api.h"
#include "cmsis.h" #include "cmsis.h"
#include "pinmap.h" #include "pinmap.h"
#include "error.h"
#include "fsl_ftm_hal.h" #include "fsl_ftm_hal.h"
#include "fsl_mcg_hal.h" #include "fsl_mcg_hal.h"
#include "fsl_clock_manager.h" #include "fsl_clock_manager.h"
@ -73,9 +73,8 @@ static float pwm_clock_mhz;
void pwmout_init(pwmout_t* obj, PinName pin) { void pwmout_init(pwmout_t* obj, PinName pin) {
PWMName pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM); PWMName pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM);
if (pwm == (PWMName)NC) { assert(pwm != (PWMName)NC);
error("PwmOut pin mapping failed");
}
obj->pwm_name = pwm; obj->pwm_name = pwm;
uint32_t pwm_base_clock; uint32_t pwm_base_clock;

View File

@ -17,12 +17,12 @@
// math.h required for floating point operations for baud rate calculation // math.h required for floating point operations for baud rate calculation
#include <math.h> #include <math.h>
#include <assert.h>
#include <string.h> #include <string.h>
#include "cmsis.h" #include "cmsis.h"
#include "pinmap.h" #include "pinmap.h"
#include "error.h"
#include "fsl_uart_hal.h" #include "fsl_uart_hal.h"
#include "fsl_clock_manager.h" #include "fsl_clock_manager.h"
#include "fsl_uart_features.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_tx = pinmap_peripheral(tx, PinMap_UART_TX);
uint32_t uart_rx = pinmap_peripheral(rx, PinMap_UART_RX); uint32_t uart_rx = pinmap_peripheral(rx, PinMap_UART_RX);
obj->index = (UARTName)pinmap_merge(uart_tx, uart_rx); obj->index = (UARTName)pinmap_merge(uart_tx, uart_rx);
if ((int)obj->index == NC) { assert((int)obj->index != NC);
error("Serial pinout mapping failed");
}
uart_config_t uart_config; uart_config_t uart_config;
uart_config.baudRate = 9600; uart_config.baudRate = 9600;

View File

@ -13,10 +13,10 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
#include "spi_api.h"
#include <math.h> #include <math.h>
#include <assert.h>
#include "spi_api.h"
#include "cmsis.h" #include "cmsis.h"
#include "pinmap.h" #include "pinmap.h"
#include "error.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); uint32_t spi_cntl = pinmap_merge(spi_sclk, spi_ssel);
obj->instance = pinmap_merge(spi_data, spi_cntl); obj->instance = pinmap_merge(spi_data, spi_cntl);
if ((int)obj->instance == NC) { assert((int)obj->instance != NC);
error("SPI pinout mapping failed");
}
// enable power and clocking // enable power and clocking
clock_manager_set_gate(kClockModuleSPI, obj->instance, true); clock_manager_set_gate(kClockModuleSPI, obj->instance, true);

View File

@ -13,10 +13,10 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
#include <assert.h>
#include "analogin_api.h" #include "analogin_api.h"
#include "cmsis.h" #include "cmsis.h"
#include "pinmap.h" #include "pinmap.h"
#include "error.h"
#define ANALOGIN_MEDIAN_FILTER 1 #define ANALOGIN_MEDIAN_FILTER 1
#define ADC_10BIT_RANGE 0x3FF #define ADC_10BIT_RANGE 0x3FF
@ -37,9 +37,7 @@ void analogin_init(analogin_t *obj, PinName pin) {
const PinMap *map = PinMap_ADC; const PinMap *map = PinMap_ADC;
obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC); //(NRF_ADC_Type *) obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC); //(NRF_ADC_Type *)
if (obj->adc == (ADCName)NC) { assert(obj->adc != (ADCName)NC);
error("ADC pin mapping failed");
}
while (map->pin != NC) { while (map->pin != NC) {
if (map->pin == pin){ if (map->pin == pin){
@ -49,12 +47,12 @@ void analogin_init(analogin_t *obj, PinName pin) {
map++; map++;
} }
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) | NRF_ADC->CONFIG = (ADC_CONFIG_RES_10bit << ADC_CONFIG_RES_Pos) |
(ADC_CONFIG_INPSEL_AnalogInputOneThirdPrescaling<< ADC_CONFIG_INPSEL_Pos) | (ADC_CONFIG_INPSEL_AnalogInputOneThirdPrescaling<< ADC_CONFIG_INPSEL_Pos) |
(ADC_CONFIG_REFSEL_SupplyOneThirdPrescaling << ADC_CONFIG_REFSEL_Pos) | (ADC_CONFIG_REFSEL_SupplyOneThirdPrescaling << ADC_CONFIG_REFSEL_Pos) |
(analogInputPin << ADC_CONFIG_PSEL_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) { uint16_t analogin_read_u16(analogin_t *obj) {

View File

@ -13,12 +13,10 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
#include <assert.h>
#include "i2c_api.h" #include "i2c_api.h"
#include "cmsis.h" #include "cmsis.h"
#include "pinmap.h" #include "pinmap.h"
#include "error.h"
static const PinMap PinMap_I2C_SDA[] = { static const PinMap PinMap_I2C_SDA[] = {
{p22, I2C_0, 1}, {p22, I2C_0, 1},
@ -52,7 +50,7 @@ void twi_master_init(i2c_t *obj, PinName sda, PinName scl, int frequency) {
(GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos)); (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos));
obj->i2c->PSELSCL = scl; obj->i2c->PSELSCL = scl;
obj->i2c->PSELSDA = sda; obj->i2c->PSELSDA = sda;
// set default frequency at 100k // set default frequency at 100k
i2c_frequency(obj, frequency); i2c_frequency(obj, frequency);
i2c_interface_enable(obj); i2c_interface_enable(obj);
@ -64,30 +62,28 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
I2CName i2c = (I2CName)pinmap_merge(i2c_sda,i2c_scl); I2CName i2c = (I2CName)pinmap_merge(i2c_sda,i2c_scl);
obj->i2c = (NRF_TWI_Type *)i2c; obj->i2c = (NRF_TWI_Type *)i2c;
if ((int)obj->i2c == NC) { assert((int)obj->i2c != NC);
error("I2C pin mapping failed");
}
obj->scl=scl; obj->scl=scl;
obj->sda=sda; obj->sda=sda;
obj->i2c->EVENTS_ERROR = 0; obj->i2c->EVENTS_ERROR = 0;
obj->i2c->ENABLE = TWI_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos; obj->i2c->ENABLE = TWI_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos;
obj->i2c->POWER = 0; obj->i2c->POWER = 0;
for(int i=0;i<100;i++){ for(int i=0;i<100;i++){
} }
obj->i2c->POWER = 1; obj->i2c->POWER = 1;
twi_master_init(obj,sda,scl,100000); twi_master_init(obj,sda,scl,100000);
} }
void i2c_reset(i2c_t *obj) { void i2c_reset(i2c_t *obj) {
obj->i2c->EVENTS_ERROR = 0; obj->i2c->EVENTS_ERROR = 0;
obj->i2c->ENABLE = TWI_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos; obj->i2c->ENABLE = TWI_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos;
obj->i2c->POWER = 0; obj->i2c->POWER = 0;
for(int i=0;i<100;i++){ 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); twi_master_init(obj,obj->sda,obj->scl,obj->freq);
} }
@ -107,7 +103,7 @@ int i2c_stop(i2c_t *obj) {
timeOut--; timeOut--;
if(timeOut<0) if(timeOut<0)
return 1; return 1;
} }
addrSet = 0; addrSet = 0;
i2c_reset(obj); i2c_reset(obj);
return 0; return 0;
@ -122,7 +118,7 @@ int i2c_do_write(i2c_t *obj, int value) {
if(timeOut<0) if(timeOut<0)
return 1; return 1;
} }
obj->i2c->EVENTS_TXDSENT = 0; obj->i2c->EVENTS_TXDSENT = 0;
return 0; return 0;
} }
@ -166,17 +162,17 @@ void i2c_frequency(i2c_t *obj, int hz) {
} }
int checkError(i2c_t *obj){ int checkError(i2c_t *obj){
if (obj->i2c->EVENTS_ERROR == 1){ if (obj->i2c->EVENTS_ERROR == 1){
if (obj->i2c->ERRORSRC & TWI_ERRORSRC_ANACK_Msk){ if (obj->i2c->ERRORSRC & TWI_ERRORSRC_ANACK_Msk){
obj->i2c->EVENTS_ERROR = 0; obj->i2c->EVENTS_ERROR = 0;
obj->i2c->TASKS_STOP = 1; obj->i2c->TASKS_STOP = 1;
return I2C_ERROR_BUS_BUSY; return I2C_ERROR_BUS_BUSY;
} }
obj->i2c->EVENTS_ERROR = 0; obj->i2c->EVENTS_ERROR = 0;
obj->i2c->TASKS_STOP = 1; obj->i2c->TASKS_STOP = 1;
return I2C_ERROR_NO_SLAVE; return I2C_ERROR_NO_SLAVE;
} }
return 0; return 0;
} }
@ -190,7 +186,7 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
// Read in all except last byte // Read in all except last byte
for (count = 0; count < (length - 1); count++) { for (count = 0; count < (length - 1); count++) {
status = i2c_do_read(obj,&data[count], 0); status = i2c_do_read(obj,&data[count], 0);
if (status) { if (status) {
errorResult = checkError(obj); errorResult = checkError(obj);
i2c_reset(obj); i2c_reset(obj);
if(errorResult<0){ if(errorResult<0){
@ -211,7 +207,7 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
while(!obj->i2c->EVENTS_STOPPED){ while(!obj->i2c->EVENTS_STOPPED){
} }
obj->i2c->EVENTS_STOPPED = 0; obj->i2c->EVENTS_STOPPED = 0;
} }
return length; return length;
} }
@ -219,7 +215,7 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
int status, errorResult; int status, errorResult;
obj->i2c->ADDRESS = (address>>1); obj->i2c->ADDRESS = (address>>1);
obj->i2c->SHORTS = 0; obj->i2c->SHORTS = 0;
obj->i2c->TASKS_STARTTX = 1; obj->i2c->TASKS_STARTTX = 1;
for (int i=0; i<length; i++) { for (int i=0; i<length; i++) {
status = i2c_do_write(obj, data[i]); status = i2c_do_write(obj, data[i]);
@ -264,7 +260,7 @@ int i2c_byte_write(i2c_t *obj, int data) {
obj->i2c->TASKS_STARTRX = 1; obj->i2c->TASKS_STARTRX = 1;
} }
else{ else{
obj->i2c->TASKS_STARTTX = 1; obj->i2c->TASKS_STARTTX = 1;
} }
} }
else{ else{

View File

@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
#include <assert.h>
#include "pwmout_api.h" #include "pwmout_api.h"
#include "cmsis.h" #include "cmsis.h"
#include "pinmap.h" #include "pinmap.h"
@ -43,8 +44,8 @@ static const PinMap PinMap_PWM[] = {
{p19, PWM_1, 1}, {p19, PWM_1, 1},
{p20, PWM_1, 1}, {p20, PWM_1, 1},
{p21, PWM_1, 1}, {p21, PWM_1, 1},
{p22, PWM_1, 1}, {p22, PWM_1, 1},
{p23, PWM_1, 1}, {p23, PWM_1, 1},
{p24, PWM_1, 1}, {p24, PWM_1, 1},
{p25, PWM_1, 1}, {p25, PWM_1, 1},
{p28, PWM_1, 1}, {p28, PWM_1, 1},
@ -54,7 +55,7 @@ static const PinMap PinMap_PWM[] = {
}; };
static NRF_TIMER_Type *Timers[1] = { static NRF_TIMER_Type *Timers[1] = {
NRF_TIMER2 NRF_TIMER2
}; };
uint8_t PWM_taken[NO_PWMS] = {0,0}; uint8_t PWM_taken[NO_PWMS] = {0,0};
@ -67,34 +68,34 @@ uint16_t ACTUAL_PULSE[NO_PWMS] = {0,0};
*/ */
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
void TIMER2_IRQHandler(void) void TIMER2_IRQHandler(void)
{ {
static uint16_t CCVal1 = 2501; static uint16_t CCVal1 = 2501;
static uint16_t CCVal2 = 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->INTENSET & TIMER_INTENSET_COMPARE1_Msk) != 0)){
NRF_TIMER2->CC[0] = CCVal1; NRF_TIMER2->CC[0] = CCVal1;
NRF_TIMER2->EVENTS_COMPARE[1] = 0; NRF_TIMER2->EVENTS_COMPARE[1] = 0;
NRF_TIMER2->CC[1] = (NRF_TIMER2->CC[1] + PERIOD[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->INTENSET & TIMER_INTENSET_COMPARE3_Msk) != 0)){
NRF_TIMER2->CC[2] = CCVal2; NRF_TIMER2->CC[2] = CCVal2;
NRF_TIMER2->EVENTS_COMPARE[3] = 0; NRF_TIMER2->EVENTS_COMPARE[3] = 0;
NRF_TIMER2->CC[3] = (NRF_TIMER2->CC[3] + PERIOD[1]); 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 #ifdef __cplusplus
} }
#endif #endif
/** @brief Function for initializing the Timer peripherals. /** @brief Function for initializing the Timer peripherals.
*/ */
void timer_init(uint8_t pwmChoice) void timer_init(uint8_t pwmChoice)
@ -102,10 +103,10 @@ void timer_init(uint8_t pwmChoice)
NRF_TIMER_Type *timer = Timers[pwmChoice/2]; NRF_TIMER_Type *timer = Timers[pwmChoice/2];
if(!(pwmChoice%2)){ if(!(pwmChoice%2)){
timer->POWER = 0; timer->POWER = 0;
timer->POWER = 1; timer->POWER = 1;
timer->MODE = TIMER_MODE_MODE_Timer; timer->MODE = TIMER_MODE_MODE_Timer;
timer->BITMODE = TIMER_BITMODE_BITMODE_16Bit << TIMER_BITMODE_BITMODE_Pos; timer->BITMODE = TIMER_BITMODE_BITMODE_16Bit << TIMER_BITMODE_BITMODE_Pos;
timer->PRESCALER = 7;//8us ticks timer->PRESCALER = 7;//8us ticks
} }
if(pwmChoice%2){ if(pwmChoice%2){
@ -148,11 +149,11 @@ void gpiote_init(PinName pin,uint8_t channel_number)
/* Three NOPs are required to make sure configuration is written before setting tasks or getting events */ /* Three NOPs are required to make sure configuration is written before setting tasks or getting events */
__NOP(); __NOP();
__NOP(); __NOP();
__NOP(); __NOP();
/* Launch the task to take the GPIOTE channel output to the desired level */ /* Launch the task to take the GPIOTE channel output to the desired level */
NRF_GPIOTE->TASKS_OUT[channel_number] = 1; 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. */ 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) | NRF_GPIOTE->CONFIG[channel_number] = (GPIOTE_CONFIG_MODE_Task << GPIOTE_CONFIG_MODE_Pos) |
((uint32_t)pin << GPIOTE_CONFIG_PSEL_Pos) | ((uint32_t)pin << GPIOTE_CONFIG_PSEL_Pos) |
@ -162,7 +163,7 @@ void gpiote_init(PinName pin,uint8_t channel_number)
/* Three NOPs are required to make sure configuration is written before setting tasks or getting events */ /* Three NOPs are required to make sure configuration is written before setting tasks or getting events */
__NOP(); __NOP();
__NOP(); __NOP();
__NOP(); __NOP();
} }
/** @brief Function for initializing the Programmable Peripheral Interconnect peripheral. /** @brief Function for initializing the Programmable Peripheral Interconnect peripheral.
*/ */
@ -175,8 +176,8 @@ static void ppi_init(uint8_t pwm)
// Configure PPI channel 0 to toggle ADVERTISING_LED_PIN_NO on every TIMER1 COMPARE[0] match // 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].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+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].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+1].EEP = (uint32_t)&timer->EVENTS_COMPARE[channel_number+1-(4*(channel_number/4))];
// Enable PPI channels. // Enable PPI channels.
NRF_PPI->CHEN |= (1 << channel_number) NRF_PPI->CHEN |= (1 << channel_number)
@ -213,10 +214,9 @@ void pwmout_init(pwmout_t* obj, PinName pin) {
// determine the channel // determine the channel
uint8_t pwmOutSuccess = 0; uint8_t pwmOutSuccess = 0;
PWMName pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM); PWMName pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM);
if (pwm == (PWMName)NC){ assert(pwm != (PWMName)NC);
error("PwmOut pin mapping failed");
}
if(PWM_taken[(uint8_t)pwm]){ if(PWM_taken[(uint8_t)pwm]){
for(uint8_t i = 1; !pwmOutSuccess && (i<NO_PWMS) ;i++){ for(uint8_t i = 1; !pwmOutSuccess && (i<NO_PWMS) ;i++){
@ -265,14 +265,14 @@ void pwmout_write(pwmout_t* obj, float value) {
value = 0.0; value = 0.0;
} else if (value > 1.0f) { } else if (value > 1.0f) {
value = 1.0; value = 1.0;
} }
oldPulseWidth = ACTUAL_PULSE[obj->pwm]; oldPulseWidth = ACTUAL_PULSE[obj->pwm];
ACTUAL_PULSE[obj->pwm] = PULSE_WIDTH[obj->pwm] = value* PERIOD[obj->pwm]; ACTUAL_PULSE[obj->pwm] = PULSE_WIDTH[obj->pwm] = value* PERIOD[obj->pwm];
if(PULSE_WIDTH[obj->pwm] == 0){ if(PULSE_WIDTH[obj->pwm] == 0){
PULSE_WIDTH[obj->pwm] = 1; PULSE_WIDTH[obj->pwm] = 1;
setModulation(obj,0,0); setModulation(obj,0,0);
} }
else if(PULSE_WIDTH[obj->pwm] == PERIOD[obj->pwm]){ else if(PULSE_WIDTH[obj->pwm] == PERIOD[obj->pwm]){
PULSE_WIDTH[obj->pwm] = PERIOD[obj->pwm]-1; PULSE_WIDTH[obj->pwm] = PERIOD[obj->pwm]-1;
@ -280,7 +280,7 @@ void pwmout_write(pwmout_t* obj, float value) {
} }
else if( (oldPulseWidth == 0) || (oldPulseWidth == PERIOD[obj->pwm]) ){ else if( (oldPulseWidth == 0) || (oldPulseWidth == PERIOD[obj->pwm]) ){
setModulation(obj,1,oldPulseWidth == PERIOD[obj->pwm]); setModulation(obj,1,oldPulseWidth == PERIOD[obj->pwm]);
} }
} }
float pwmout_read(pwmout_t* obj) { float pwmout_read(pwmout_t* obj) {
@ -308,7 +308,7 @@ void pwmout_period_us(pwmout_t* obj, int us) {
} }
else{ else{
PERIOD[obj->pwm] =periodInTicks; PERIOD[obj->pwm] =periodInTicks;
} }
} }
void pwmout_pulsewidth(pwmout_t* obj, float seconds) { void pwmout_pulsewidth(pwmout_t* obj, float seconds) {
@ -327,7 +327,7 @@ void pwmout_pulsewidth_us(pwmout_t* obj, int us) {
if(PULSE_WIDTH[obj->pwm] == 0){ if(PULSE_WIDTH[obj->pwm] == 0){
PULSE_WIDTH[obj->pwm] = 1; PULSE_WIDTH[obj->pwm] = 1;
setModulation(obj,0,0); setModulation(obj,0,0);
} }
else if(PULSE_WIDTH[obj->pwm] == PERIOD[obj->pwm]){ else if(PULSE_WIDTH[obj->pwm] == PERIOD[obj->pwm]){
PULSE_WIDTH[obj->pwm] = PERIOD[obj->pwm]-1; PULSE_WIDTH[obj->pwm] = PERIOD[obj->pwm]-1;
@ -335,5 +335,5 @@ void pwmout_pulsewidth_us(pwmout_t* obj, int us) {
} }
else if( (oldPulseWidth == 0) || (oldPulseWidth == PERIOD[obj->pwm]) ){ else if( (oldPulseWidth == 0) || (oldPulseWidth == PERIOD[obj->pwm]) ){
setModulation(obj,1,oldPulseWidth == PERIOD[obj->pwm]); setModulation(obj,1,oldPulseWidth == PERIOD[obj->pwm]);
} }
} }

View File

@ -16,11 +16,12 @@
// math.h required for floating point operations for baud rate calculation // math.h required for floating point operations for baud rate calculation
//#include <math.h> //#include <math.h>
#include <string.h> #include <string.h>
#include <assert.h>
#include "serial_api.h" #include "serial_api.h"
#include "cmsis.h" #include "cmsis.h"
#include "pinmap.h" #include "pinmap.h"
#include "error.h"
/****************************************************************************** /******************************************************************************
* INITIALIZATION * INITIALIZATION
******************************************************************************/ ******************************************************************************/
@ -65,14 +66,12 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) {
UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX); UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX);
UARTName uart = (UARTName)pinmap_merge(uart_tx, uart_rx); UARTName uart = (UARTName)pinmap_merge(uart_tx, uart_rx);
if ((int)uart == NC) { assert((int)uart != NC);
error("Serial pinout mapping failed");
}
obj->uart = (NRF_UART_Type *)uart; obj->uart = (NRF_UART_Type *)uart;
//pin configurations -- //pin configurations --
//outputs //outputs
NRF_GPIO->DIR |= (1<<tx);//TX_PIN_NUMBER); NRF_GPIO->DIR |= (1<<tx);//TX_PIN_NUMBER);
NRF_GPIO->DIR |= (1<<RTS_PIN_NUMBER); NRF_GPIO->DIR |= (1<<RTS_PIN_NUMBER);
@ -118,9 +117,9 @@ void serial_baud(serial_t *obj, int baudrate) {
if(baudrate<=1200){ if(baudrate<=1200){
obj->uart->BAUDRATE = UART_BAUDRATE_BAUDRATE_Baud1200; obj->uart->BAUDRATE = UART_BAUDRATE_BAUDRATE_Baud1200;
return; return;
} }
for(int i=1;i<16;i++){ for(int i=1;i<16;i++){
if(baudrate<acceptedSpeeds[i][0]){ if(baudrate<acceptedSpeeds[i][0]){
obj->uart->BAUDRATE = acceptedSpeeds[i-1][1]; obj->uart->BAUDRATE = acceptedSpeeds[i-1][1];
return; return;
@ -133,7 +132,7 @@ void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_b
// 0: 1 stop bits, 1: 2 stop bits // 0: 1 stop bits, 1: 2 stop bits
// int parity_enable, parity_select; // int parity_enable, parity_select;
switch (parity) { switch (parity) {
case ParityNone: case ParityNone:
obj->uart->CONFIG = 0; obj->uart->CONFIG = 0;
break; break;
default: default:
@ -149,11 +148,11 @@ void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_b
static inline void uart_irq(uint32_t iir, uint32_t index) { static inline void uart_irq(uint32_t iir, uint32_t index) {
SerialIrq irq_type; SerialIrq irq_type;
switch (iir) { switch (iir) {
case 1: case 1:
irq_type = TxIrq; irq_type = TxIrq;
break; break;
case 2: case 2:
irq_type = RxIrq; irq_type = RxIrq;
break; break;
default: return; default: return;
@ -165,7 +164,7 @@ static inline void uart_irq(uint32_t iir, uint32_t index) {
} }
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
void UART0_IRQHandler() void UART0_IRQHandler()
{ {
uint32_t irtype =0; uint32_t irtype =0;
@ -180,7 +179,7 @@ void UART0_IRQHandler()
} }
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
void serial_irq_handler(serial_t *obj, uart_irq_handler handler, uint32_t id) { void serial_irq_handler(serial_t *obj, uart_irq_handler handler, uint32_t id) {
irq_handler = handler; irq_handler = handler;
serial_irq_ids[obj->index] = id; serial_irq_ids[obj->index] = id;

View File

@ -14,6 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
//#include <math.h> //#include <math.h>
#include <assert.h>
#include "spi_api.h" #include "spi_api.h"
#include "cmsis.h" #include "cmsis.h"
#include "pinmap.h" #include "pinmap.h"
@ -22,7 +23,7 @@
static const PinMap PinMap_SPI_SCLK[] = { static const PinMap PinMap_SPI_SCLK[] = {
{SPI_PSELSCK0 , SPI_0, 0x01}, {SPI_PSELSCK0 , SPI_0, 0x01},
{SPI_PSELSCK1, SPI_1, 0x02}, {SPI_PSELSCK1, SPI_1, 0x02},
{SPIS_PSELSCK, SPIS, 0x03}, {SPIS_PSELSCK, SPIS, 0x03},
{NC , NC , 0} {NC , NC , 0}
}; };
@ -59,20 +60,18 @@ 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_data = (SPIName)pinmap_merge(spi_mosi, spi_miso);
SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel); SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel);
SPIName spi = (SPIName)pinmap_merge(spi_data, spi_cntl); SPIName spi = (SPIName)pinmap_merge(spi_data, spi_cntl);
//SPIName //SPIName
if(ssel==NC){ if(ssel==NC){
obj->spi = (NRF_SPI_Type*)spi; obj->spi = (NRF_SPI_Type*)spi;
obj->spis = (NRF_SPIS_Type*)NC; obj->spis = (NRF_SPIS_Type*)NC;
} }
else{ else{
obj->spi = (NRF_SPI_Type*)NC; obj->spi = (NRF_SPI_Type*)NC;
obj->spis = (NRF_SPIS_Type*)spi; 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
}
// pin out the spi pins
if (ssel != NC) {//slave if (ssel != NC) {//slave
obj->spis->POWER=0; obj->spis->POWER=0;
obj->spis->POWER=1; obj->spis->POWER=1;
@ -108,7 +107,7 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel
obj->spis->MAXRX=SPIS_MESSAGE_SIZE; obj->spis->MAXRX=SPIS_MESSAGE_SIZE;
obj->spis->MAXTX=SPIS_MESSAGE_SIZE; obj->spis->MAXTX=SPIS_MESSAGE_SIZE;
obj->spis->TXDPTR = (uint32_t)&m_tx_buf[0]; 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); obj->spis->SHORTS = (SPIS_SHORTS_END_ACQUIRE_Enabled<<SPIS_SHORTS_END_ACQUIRE_Pos);
spi_format(obj, 8, 0, 1); // 8 bits, mode 0, slave spi_format(obj, 8, 0, 1); // 8 bits, mode 0, slave
@ -117,7 +116,7 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel
obj->spi->POWER=0; obj->spi->POWER=0;
obj->spi->POWER=1; 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) 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_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos)
| (GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos) | (GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos)
@ -141,10 +140,10 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel
obj->spi->PSELMISO = miso; 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_format(obj, 8, 0, 0); // 8 bits, mode 0, master
spi_frequency(obj, 1000000); spi_frequency(obj, 1000000);
} }
} }
@ -197,10 +196,10 @@ void spi_format(spi_t *obj, int bits, int mode, int slave) {
} }
//default to msb first //default to msb first
if(slave){ 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{ 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); spi_enable(obj,slave);
@ -212,7 +211,7 @@ void spi_frequency(spi_t *obj, int hz) {
spi_disable(obj,0); spi_disable(obj,0);
if(hz<250000) { //125Kbps 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 else if(hz<500000){//250Kbps
obj->spi->FREQUENCY = (uint32_t) SPI_FREQUENCY_FREQUENCY_K250; obj->spi->FREQUENCY = (uint32_t) SPI_FREQUENCY_FREQUENCY_K250;
@ -230,7 +229,7 @@ void spi_frequency(spi_t *obj, int hz) {
obj->spi->FREQUENCY = (uint32_t) SPI_FREQUENCY_FREQUENCY_M4; obj->spi->FREQUENCY = (uint32_t) SPI_FREQUENCY_FREQUENCY_M4;
} }
else{//8Mbps else{//8Mbps
obj->spi->FREQUENCY = (uint32_t) SPI_FREQUENCY_FREQUENCY_M8; obj->spi->FREQUENCY = (uint32_t) SPI_FREQUENCY_FREQUENCY_M8;
} }
spi_enable(obj,0); spi_enable(obj,0);
@ -260,7 +259,7 @@ int spi_master_write(spi_t *obj, int value) {
return spi_read(obj); 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); // return (obj->spis->EVENTS_ACQUIRED==1);
//} //}
@ -268,12 +267,12 @@ int spi_slave_receive(spi_t *obj) {
return obj->spis->EVENTS_END; return obj->spis->EVENTS_END;
}; };
int spi_slave_read(spi_t *obj) { int spi_slave_read(spi_t *obj) {
return m_rx_buf[0]; return m_rx_buf[0];
} }
void spi_slave_write(spi_t *obj, int value) { void spi_slave_write(spi_t *obj, int value) {
m_tx_buf[0]= value & 0xFF; m_tx_buf[0]= value & 0xFF;
obj->spis->TASKS_RELEASE=1; obj->spis->TASKS_RELEASE=1;
obj->spis->EVENTS_ACQUIRED=0; obj->spis->EVENTS_ACQUIRED=0;
obj->spis->EVENTS_END=0; obj->spis->EVENTS_END=0;

View File

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
#include <assert.h>
#include "analogin_api.h" #include "analogin_api.h"
#include "cmsis.h" #include "cmsis.h"
#include "pinmap.h" #include "pinmap.h"
@ -50,9 +50,8 @@ static const PinMap PinMap_ADC[] = {
void analogin_init(analogin_t *obj, PinName pin) { void analogin_init(analogin_t *obj, PinName pin) {
volatile uint32_t tmp; volatile uint32_t tmp;
obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC); obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC);
if (obj->adc == (uint32_t)NC) { assert(obj->adc != (ADCName)NC);
error("ADC pin mapping failed");
}
pinmap_pinout(pin, PinMap_ADC); pinmap_pinout(pin, PinMap_ADC);
// ADC Powered // ADC Powered

View File

@ -13,11 +13,10 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
#include <assert.h>
#include "i2c_api.h" #include "i2c_api.h"
#include "cmsis.h" #include "cmsis.h"
#include "pinmap.h" #include "pinmap.h"
#include "error.h"
#if DEVICE_I2C #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_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA);
I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL); I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL);
obj->i2c = (LPC_I2C0_Type *)pinmap_merge(i2c_sda, i2c_scl); obj->i2c = (LPC_I2C0_Type *)pinmap_merge(i2c_sda, i2c_scl);
assert((int)obj->i2c != NC);
if ((int)obj->i2c == NC) {
error("I2C pin mapping failed");
}
// enable power // enable power
i2c_power_enable(obj); i2c_power_enable(obj);

View File

@ -14,6 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
// math.h required for floating point operations for baud rate calculation // math.h required for floating point operations for baud rate calculation
#include <assert.h>
#include <math.h> #include <math.h>
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
@ -21,7 +22,6 @@
#include "serial_api.h" #include "serial_api.h"
#include "cmsis.h" #include "cmsis.h"
#include "pinmap.h" #include "pinmap.h"
#include "error.h"
#if DEVICE_SERIAL #if DEVICE_SERIAL
#warning "[TODO] support from UART_1 to UART_4" #warning "[TODO] support from UART_1 to UART_4"
@ -68,10 +68,8 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) {
UARTName uart_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX); UARTName uart_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX);
UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX); UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX);
UARTName uart = (UARTName)pinmap_merge(uart_tx, uart_rx); UARTName uart = (UARTName)pinmap_merge(uart_tx, uart_rx);
if ((int)uart == NC) { assert((int)uart != NC);
error("Serial pinout mapping failed");
}
obj->uart = (LPC_USART0_Type *)uart; obj->uart = (LPC_USART0_Type *)uart;
LPC_SYSCON->SYSAHBCLKCTRL |= ((1<<12) | (1<<20) | (1<<21) | (1<<22)); 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) { void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits) {
// 0: 1 stop bits, 1: 2 stop bits assert((stop_bits == 1) || (stop_bits == 2)); // 0: 1 stop bits, 1: 2 stop bits
if (stop_bits != 1 && stop_bits != 2) { assert((data_bits > 4) || (data_bits < 9)); // 0: 5 data bits ... 3: 8 data bits
error("Invalid stop bits specified"); assert(parity < (ParityForced0 + 1));
}
stop_bits -= 1; 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; data_bits -= 5;
int parity_enable, parity_select; 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 ParityForced1: parity_enable = 1; parity_select = 2; break;
case ParityForced0: parity_enable = 1; parity_select = 3; break; case ParityForced0: parity_enable = 1; parity_select = 3; break;
default: default:
error("Invalid serial parity setting"); break;
return;
} }
obj->uart->LCR = data_bits << 0 obj->uart->LCR = data_bits << 0

View File

@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
#include <assert.h>
#include <math.h> #include <math.h>
#include "spi_api.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); SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel);
obj->spi = (LPC_SSP0_Type*)pinmap_merge(spi_data, spi_cntl); obj->spi = (LPC_SSP0_Type*)pinmap_merge(spi_data, spi_cntl);
assert((int)obj->spi == NC);
if ((int)obj->spi == NC) {
error("SPI pinout mapping failed");
}
// enable power and clocking // enable power and clocking
switch ((int)obj->spi) { 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) { void spi_format(spi_t *obj, int bits, int mode, int slave) {
ssp_disable(obj); 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");
}
int polarity = (mode & 0x2) ? 1 : 0; int polarity = (mode & 0x2) ? 1 : 0;
int phase = (mode & 0x1) ? 1 : 0; int phase = (mode & 0x1) ? 1 : 0;

View File

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

View File

@ -13,10 +13,10 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
#include <assert.h>
#include "i2c_api.h" #include "i2c_api.h"
#include "cmsis.h" #include "cmsis.h"
#include "pinmap.h" #include "pinmap.h"
#include "error.h"
static const PinMap PinMap_I2C_SDA[] = { static const PinMap PinMap_I2C_SDA[] = {
{P0_5, I2C_0, 1}, {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_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA);
I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL); I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL);
obj->i2c = (LPC_I2C_Type *)pinmap_merge(i2c_sda, i2c_scl); obj->i2c = (LPC_I2C_Type *)pinmap_merge(i2c_sda, i2c_scl);
assert((int)obj->i2c != NC);
if ((int)obj->i2c == NC) {
error("I2C pin mapping failed");
}
// enable power // enable power
i2c_power_enable(obj); i2c_power_enable(obj);

View File

@ -13,10 +13,10 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
#include <assert.h>
#include "pwmout_api.h" #include "pwmout_api.h"
#include "cmsis.h" #include "cmsis.h"
#include "pinmap.h" #include "pinmap.h"
#include "error.h"
#define TCR_CNT_EN 0x00000001 #define TCR_CNT_EN 0x00000001
#define TCR_RESET 0x00000002 #define TCR_RESET 0x00000002
@ -69,9 +69,8 @@ static LPC_CTxxBx_Type *Timers[4] = {
void pwmout_init(pwmout_t* obj, PinName pin) { void pwmout_init(pwmout_t* obj, PinName pin) {
// determine the channel // determine the channel
PWMName pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM); PWMName pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM);
if (pwm == (PWMName)NC) assert(pwm != (PWMName)NC);
error("PwmOut pin mapping failed");
obj->pwm = pwm; obj->pwm = pwm;
// Timer registers // Timer registers
@ -143,7 +142,7 @@ void pwmout_period_us(pwmout_t* obj, int us) {
// for 16bit timer, set prescaler to avoid overflow // for 16bit timer, set prescaler to avoid overflow
if (timer == LPC_CT16B0 || timer == LPC_CT16B1) { 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; timer->PR = high_period_ticks;
period_ticks /= (high_period_ticks + 1); period_ticks /= (high_period_ticks + 1);
} }

View File

@ -21,7 +21,6 @@
#include "serial_api.h" #include "serial_api.h"
#include "cmsis.h" #include "cmsis.h"
#include "pinmap.h" #include "pinmap.h"
#include "error.h"
/****************************************************************************** /******************************************************************************
* INITIALIZATION * INITIALIZATION
@ -55,10 +54,8 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) {
UARTName uart_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX); UARTName uart_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX);
UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX); UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX);
UARTName uart = (UARTName)pinmap_merge(uart_tx, uart_rx); UARTName uart = (UARTName)pinmap_merge(uart_tx, uart_rx);
if ((int)uart == NC) { assert((int)uart != NC);
error("Serial pinout mapping failed");
}
obj->uart = (LPC_USART_Type *)uart; obj->uart = (LPC_USART_Type *)uart;
LPC_SYSCON->SYSAHBCLKCTRL |= (1<<12); 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) { void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits) {
// 0: 1 stop bits, 1: 2 stop bits assert((stop_bits == 1) || (stop_bits == 2)); // 0: 1 stop bits, 1: 2 stop bits
if (stop_bits != 1 && stop_bits != 2) { assert((data_bits > 4) || (data_bits < 9)); // 0: 5 data bits ... 3: 8 data bits
error("Invalid stop bits specified"); assert(parity < (ParityForced0 + 1));
}
stop_bits -= 1; 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; data_bits -= 5;
int parity_enable, parity_select; 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 ParityForced1: parity_enable = 1; parity_select = 2; break;
case ParityForced0: parity_enable = 1; parity_select = 3; break; case ParityForced0: parity_enable = 1; parity_select = 3; break;
default: default:
error("Invalid serial parity setting"); break;
return;
} }
obj->uart->LCR = data_bits << 0 obj->uart->LCR = data_bits << 0

View File

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

View File

@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
#include <assert.h>
#include "analogin_api.h" #include "analogin_api.h"
#include "cmsis.h" #include "cmsis.h"
#include "pinmap.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) { void analogin_init(analogin_t *obj, PinName pin) {
obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC); obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC);
if (obj->adc == (uint32_t)NC) { assert(obj->adc != (uint32_t)NC);
error("ADC pin mapping failed");
return;
}
// Power up ADC // Power up ADC
LPC_SYSCON->PDRUNCFG &= ~ (1 << 4); LPC_SYSCON->PDRUNCFG &= ~ (1 << 4);

View File

@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
#include <assert.h>
#include "i2c_api.h" #include "i2c_api.h"
#include "cmsis.h" #include "cmsis.h"
#include "pinmap.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_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA);
I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL); I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL);
obj->i2c = (LPC_I2C_TypeDef *)pinmap_merge(i2c_sda, i2c_scl); obj->i2c = (LPC_I2C_TypeDef *)pinmap_merge(i2c_sda, i2c_scl);
assert((int)obj->i2c != NC);
if ((int)obj->i2c == NC) {
error("I2C pin mapping failed");
}
// enable power // enable power
i2c_power_enable(obj); i2c_power_enable(obj);

View File

@ -13,10 +13,10 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
#include <assert.h>
#include "pwmout_api.h" #include "pwmout_api.h"
#include "cmsis.h" #include "cmsis.h"
#include "pinmap.h" #include "pinmap.h"
#include "error.h"
#define TCR_CNT_EN 0x00000001 #define TCR_CNT_EN 0x00000001
#define TCR_RESET 0x00000002 #define TCR_RESET 0x00000002
@ -64,9 +64,8 @@ static LPC_TMR_TypeDef *Timers[3] = {
void pwmout_init(pwmout_t* obj, PinName pin) { void pwmout_init(pwmout_t* obj, PinName pin) {
// determine the channel // determine the channel
PWMName pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM); PWMName pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM);
if (pwm == (uint32_t)NC) assert(pwm != (uint32_t)NC);
error("PwmOut pin mapping failed");
obj->pwm = pwm; obj->pwm = pwm;
// Timer registers // Timer registers

View File

@ -14,6 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
// math.h required for floating point operations for baud rate calculation // math.h required for floating point operations for baud rate calculation
#include <assert.h>
#include <math.h> #include <math.h>
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
@ -21,7 +22,6 @@
#include "serial_api.h" #include "serial_api.h"
#include "cmsis.h" #include "cmsis.h"
#include "pinmap.h" #include "pinmap.h"
#include "error.h"
/****************************************************************************** /******************************************************************************
* INITIALIZATION * 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_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX);
UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX); UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX);
UARTName uart = (UARTName)pinmap_merge(uart_tx, uart_rx); UARTName uart = (UARTName)pinmap_merge(uart_tx, uart_rx);
if ((int)uart == NC) { assert((int)uart != NC);
error("Serial pinout mapping failed");
}
obj->uart = (LPC_UART_TypeDef *)uart; obj->uart = (LPC_UART_TypeDef *)uart;
LPC_SYSCON->SYSAHBCLKCTRL |= (1<<12); 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) { void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits) {
// 0: 1 stop bits, 1: 2 stop bits assert((stop_bits == 1) || (stop_bits == 2)); // 0: 1 stop bits, 1: 2 stop bits
if (stop_bits != 1 && stop_bits != 2) { assert((data_bits > 4) || (data_bits < 9)); // 0: 5 data bits ... 3: 8 data bits
error("Invalid stop bits specified"); assert(parity < (ParityForced0 + 1));
}
stop_bits -= 1; 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; data_bits -= 5;
int parity_enable, parity_select; 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 ParityForced1: parity_enable = 1; parity_select = 2; break;
case ParityForced0: parity_enable = 1; parity_select = 3; break; case ParityForced0: parity_enable = 1; parity_select = 3; break;
default: default:
error("Invalid serial parity setting"); break;
return;
} }
obj->uart->LCR = data_bits << 0 obj->uart->LCR = data_bits << 0

View File

@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
#include <assert.h>
#include <math.h> #include <math.h>
#include "spi_api.h" #include "spi_api.h"
#include "cmsis.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); SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel);
obj->spi = (LPC_SSP_TypeDef*)pinmap_merge(spi_data, spi_cntl); obj->spi = (LPC_SSP_TypeDef*)pinmap_merge(spi_data, spi_cntl);
assert((int)obj->spi != NC);
if ((int)obj->spi == NC) {
error("SPI pinout mapping failed");
}
// enable power and clocking // enable power and clocking
switch ((int)obj->spi) { 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_free(spi_t *obj) {}
void spi_format(spi_t *obj, int bits, int mode, int slave) { void spi_format(spi_t *obj, int bits, int mode, int slave) {
assert((bits >= 4 && bits <= 16) || (mode >= 0 && mode <= 3));
ssp_disable(obj); ssp_disable(obj);
if (!(bits >= 4 && bits <= 16) || !(mode >= 0 && mode <= 3)) {
error("SPI format error");
}
int polarity = (mode & 0x2) ? 1 : 0; int polarity = (mode & 0x2) ? 1 : 0;
int phase = (mode & 0x1) ? 1 : 0; int phase = (mode & 0x1) ? 1 : 0;

View File

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

View File

@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
#include <assert.h>
#include "i2c_api.h" #include "i2c_api.h"
#include "cmsis.h" #include "cmsis.h"
#include "pinmap.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_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA);
I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL); I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL);
obj->i2c = (LPC_I2C_Type *)pinmap_merge(i2c_sda, i2c_scl); obj->i2c = (LPC_I2C_Type *)pinmap_merge(i2c_sda, i2c_scl);
assert((int)obj->i2c != NC);
if ((int)obj->i2c == NC) {
error("I2C pin mapping failed");
}
// enable power // enable power
i2c_power_enable(obj); i2c_power_enable(obj);

View File

@ -13,12 +13,10 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
#include <assert.h>
#include "pwmout_api.h" #include "pwmout_api.h"
#include "cmsis.h" #include "cmsis.h"
#include "pinmap.h" #include "pinmap.h"
#include "error.h"
#define TCR_CNT_EN 0x00000001 #define TCR_CNT_EN 0x00000001
#define TCR_RESET 0x00000002 #define TCR_RESET 0x00000002
@ -73,9 +71,8 @@ static unsigned int pwm_clock_mhz;
void pwmout_init(pwmout_t* obj, PinName pin) { void pwmout_init(pwmout_t* obj, PinName pin) {
// determine the channel // determine the channel
PWMName pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM); PWMName pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM);
if (pwm == (uint32_t)NC) assert(pwm != (uint32_t)NC);
error("PwmOut pin mapping failed");
obj->pwm = pwm; obj->pwm = pwm;
// Timer registers // Timer registers

View File

@ -14,6 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
// math.h required for floating point operations for baud rate calculation // math.h required for floating point operations for baud rate calculation
#include <assert.h>
#include <math.h> #include <math.h>
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
@ -21,7 +22,6 @@
#include "serial_api.h" #include "serial_api.h"
#include "cmsis.h" #include "cmsis.h"
#include "pinmap.h" #include "pinmap.h"
#include "error.h"
/****************************************************************************** /******************************************************************************
* INITIALIZATION * 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_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX);
UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX); UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX);
UARTName uart = (UARTName)pinmap_merge(uart_tx, uart_rx); UARTName uart = (UARTName)pinmap_merge(uart_tx, uart_rx);
if ((int)uart == NC) { assert((int)uart != NC);
error("Serial pinout mapping failed");
}
obj->uart = (LPC_USART_Type *)uart; obj->uart = (LPC_USART_Type *)uart;
LPC_SYSCON->SYSAHBCLKCTRL |= (1<<12); 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) { void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits) {
// 0: 1 stop bits, 1: 2 stop bits assert((stop_bits == 1) || (stop_bits == 2)); // 0: 1 stop bits, 1: 2 stop bits
if (stop_bits != 1 && stop_bits != 2) { assert((data_bits > 4) || (data_bits < 9)); // 0: 5 data bits ... 3: 8 data bits
error("Invalid stop bits specified"); assert(parity < (ParityForced0 + 1));
}
stop_bits -= 1; 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; data_bits -= 5;
int parity_enable, parity_select; 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 ParityForced1: parity_enable = 1; parity_select = 2; break;
case ParityForced0: parity_enable = 1; parity_select = 3; break; case ParityForced0: parity_enable = 1; parity_select = 3; break;
default: default:
error("Invalid serial parity setting"); break;
return;
} }
obj->uart->LCR = data_bits << 0 obj->uart->LCR = data_bits << 0

View File

@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
#include <assert.h>
#include <math.h> #include <math.h>
#include "spi_api.h" #include "spi_api.h"
#include "cmsis.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); SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel);
obj->spi = (LPC_SSPx_Type*)pinmap_merge(spi_data, spi_cntl); obj->spi = (LPC_SSPx_Type*)pinmap_merge(spi_data, spi_cntl);
assert((int)obj->spi != NC);
if ((int)obj->spi == NC) {
error("SPI pinout mapping failed");
}
// enable power and clocking // enable power and clocking
switch ((int)obj->spi) { 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) { void spi_format(spi_t *obj, int bits, int mode, int slave) {
ssp_disable(obj); 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");
}
int polarity = (mode & 0x2) ? 1 : 0; int polarity = (mode & 0x2) ? 1 : 0;
int phase = (mode & 0x1) ? 1 : 0; int phase = (mode & 0x1) ? 1 : 0;

View File

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

View File

@ -13,15 +13,13 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
#include <assert.h>
#include "analogout_api.h" #include "analogout_api.h"
#include "cmsis.h" #include "cmsis.h"
#include "pinmap.h" #include "pinmap.h"
#include "error.h"
void analogout_init(dac_t *obj, PinName pin) { void analogout_init(dac_t *obj, PinName pin) {
if (pin != P0_12) { assert(pin == P0_12);
error("DAC pin mapping failed");
}
LPC_SYSCON->SYSAHBCLKCTRL0 |= (1 << 29); LPC_SYSCON->SYSAHBCLKCTRL0 |= (1 << 29);
LPC_SYSCON->PDRUNCFG &= ~(1 << 12); LPC_SYSCON->PDRUNCFG &= ~(1 << 12);

View File

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

View File

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

View File

@ -14,6 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
// math.h required for floating point operations for baud rate calculation // math.h required for floating point operations for baud rate calculation
#include <assert.h>
#include <math.h> #include <math.h>
#include <string.h> #include <string.h>
@ -91,7 +92,7 @@ static void switch_pin(const SWM_Map *swm, PinName pn)
for (int n = 0; n < sizeof(LPC_SWM->PINASSIGN)/sizeof(*LPC_SWM->PINASSIGN); n ++) { for (int n = 0; n < sizeof(LPC_SWM->PINASSIGN)/sizeof(*LPC_SWM->PINASSIGN); n ++) {
regVal = LPC_SWM->PINASSIGN[n]; regVal = LPC_SWM->PINASSIGN[n];
for (int j = 0; j <= 24; j += 8) { for (int j = 0; j <= 24; j += 8) {
if (((regVal >> j) & 0xFF) == pn) if (((regVal >> j) & 0xFF) == pn)
regVal |= (0xFF << j); regVal |= (0xFF << j);
} }
LPC_SWM->PINASSIGN[n] = regVal; LPC_SWM->PINASSIGN[n] = regVal;
@ -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) { void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits) {
// 0: 1 stop bits, 1: 2 stop bits assert((stop_bits == 1) || (stop_bits == 2)); // 0: 1 stop bits, 1: 2 stop bits
if (stop_bits != 1 && stop_bits != 2) { assert((data_bits > 6) || (data_bits < 10)); // 0: 7 data bits ... 2: 9 data bits
error("Invalid stop bits specified"); assert(parity < (ParityEven + 1));
}
stop_bits -= 1; 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; data_bits -= 7;
int paritysel; 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 ParityEven: paritysel = 2; break;
case ParityOdd : paritysel = 3; break; case ParityOdd : paritysel = 3; break;
default: default:
error("Invalid serial parity setting"); break;
return;
} }
obj->uart->CFG = (data_bits << 2) obj->uart->CFG = (data_bits << 2)

View File

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

View File

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

View File

@ -13,11 +13,11 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
#include <assert.h>
#include "analogout_api.h" #include "analogout_api.h"
#include "cmsis.h" #include "cmsis.h"
#include "pinmap.h" #include "pinmap.h"
#include "error.h"
static const PinMap PinMap_DAC[] = { static const PinMap PinMap_DAC[] = {
{P0_26, DAC_0, 2}, {P0_26, DAC_0, 2},
@ -26,9 +26,7 @@ static const PinMap PinMap_DAC[] = {
void analogout_init(dac_t *obj, PinName pin) { void analogout_init(dac_t *obj, PinName pin) {
obj->dac = (DACName)pinmap_peripheral(pin, PinMap_DAC); obj->dac = (DACName)pinmap_peripheral(pin, PinMap_DAC);
if (obj->dac == (DACName)NC) { assert(obj->dac == (DACName)NC);
error("DAC pin mapping failed");
}
// power is on by default, set DAC clk divider is /4 // power is on by default, set DAC clk divider is /4
LPC_SC->PCLKSEL0 &= ~(0x3 << 22); LPC_SC->PCLKSEL0 &= ~(0x3 << 22);

View File

@ -13,11 +13,11 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
#include <assert.h>
#include "can_api.h" #include "can_api.h"
#include "cmsis.h" #include "cmsis.h"
#include "pinmap.h" #include "pinmap.h"
#include "error.h"
#include <math.h> #include <math.h>
#include <string.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_rd = (CANName)pinmap_peripheral(rd, PinMap_CAN_RD);
CANName can_td = (CANName)pinmap_peripheral(td, PinMap_CAN_TD); CANName can_td = (CANName)pinmap_peripheral(td, PinMap_CAN_TD);
obj->dev = (LPC_CAN_TypeDef *)pinmap_merge(can_rd, can_td); obj->dev = (LPC_CAN_TypeDef *)pinmap_merge(can_rd, can_td);
if ((int)obj->dev == NC) { assert((int)obj->dev != NC);
error("CAN pin mapping failed");
}
switch ((int)obj->dev) { switch ((int)obj->dev) {
case CAN_1: LPC_SC->PCONP |= 1 << 13; break; 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 * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
#include <assert.h>
#include "i2c_api.h" #include "i2c_api.h"
#include "cmsis.h" #include "cmsis.h"
#include "pinmap.h" #include "pinmap.h"
#include "error.h"
static const PinMap PinMap_I2C_SDA[] = { static const PinMap PinMap_I2C_SDA[] = {
{P0_0 , I2C_1, 3}, {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_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA);
I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL); I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL);
obj->i2c = (LPC_I2C_TypeDef *)pinmap_merge(i2c_sda, i2c_scl); obj->i2c = (LPC_I2C_TypeDef *)pinmap_merge(i2c_sda, i2c_scl);
assert((int)obj->i2c != NC);
if ((int)obj->i2c == NC) {
error("I2C pin mapping failed");
}
// enable power // enable power
i2c_power_enable(obj); i2c_power_enable(obj);

View File

@ -13,10 +13,10 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
#include <assert.h>
#include "pwmout_api.h" #include "pwmout_api.h"
#include "cmsis.h" #include "cmsis.h"
#include "pinmap.h" #include "pinmap.h"
#include "error.h"
#define TCR_CNT_EN 0x00000001 #define TCR_CNT_EN 0x00000001
#define TCR_RESET 0x00000002 #define TCR_RESET 0x00000002
@ -57,9 +57,8 @@ static unsigned int pwm_clock_mhz;
void pwmout_init(pwmout_t* obj, PinName pin) { void pwmout_init(pwmout_t* obj, PinName pin) {
// determine the channel // determine the channel
PWMName pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM); PWMName pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM);
if (pwm == (PWMName)NC) assert(pwm != (PWMName)NC);
error("PwmOut pin mapping failed");
obj->pwm = pwm; obj->pwm = pwm;
obj->MR = PWM_MATCH[pwm]; obj->MR = PWM_MATCH[pwm];

View File

@ -14,6 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
// math.h required for floating point operations for baud rate calculation // math.h required for floating point operations for baud rate calculation
#include <assert.h>
#include <math.h> #include <math.h>
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
@ -21,7 +22,6 @@
#include "serial_api.h" #include "serial_api.h"
#include "cmsis.h" #include "cmsis.h"
#include "pinmap.h" #include "pinmap.h"
#include "error.h"
#include "gpio_api.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_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX);
UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX); UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX);
UARTName uart = (UARTName)pinmap_merge(uart_tx, uart_rx); UARTName uart = (UARTName)pinmap_merge(uart_tx, uart_rx);
if ((int)uart == NC) { assert((int)uart != NC);
error("Serial pinout mapping failed");
}
obj->uart = (LPC_UART_TypeDef *)uart; obj->uart = (LPC_UART_TypeDef *)uart;
// enable power // enable power
@ -150,6 +148,7 @@ void serial_free(serial_t *obj) {
// serial_baud // serial_baud
// set the baud rate, taking in to account the current SystemFrequency // set the baud rate, taking in to account the current SystemFrequency
void serial_baud(serial_t *obj, int baudrate) { 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 // The LPC2300 and LPC1700 have a divider and a fractional divider to control the
// baud rate. The formula is: // 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_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_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; 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; 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) { void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits) {
// 0: 1 stop bits, 1: 2 stop bits assert((stop_bits == 1) || (stop_bits == 2)); // 0: 1 stop bits, 1: 2 stop bits
if (stop_bits != 1 && stop_bits != 2) { assert((data_bits > 4) || (data_bits < 9)); // 0: 5 data bits ... 3: 8 data bits
error("Invalid stop bits specified"); assert(parity < (ParityForced0 + 1));
}
stop_bits -= 1; 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; data_bits -= 5;
int parity_enable, parity_select; 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 ParityForced1: parity_enable = 1; parity_select = 2; break;
case ParityForced0: parity_enable = 1; parity_select = 3; break; case ParityForced0: parity_enable = 1; parity_select = 3; break;
default: default:
error("Invalid serial parity setting"); break;
return;
} }
obj->uart->LCR = data_bits << 0 obj->uart->LCR = data_bits << 0

View File

@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
#include <assert.h>
#include <math.h> #include <math.h>
#include "spi_api.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_data = (SPIName)pinmap_merge(spi_mosi, spi_miso);
SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel); SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel);
obj->spi = (LPC_SSP_TypeDef*)pinmap_merge(spi_data, spi_cntl); obj->spi = (LPC_SSP_TypeDef*)pinmap_merge(spi_data, spi_cntl);
if ((int)obj->spi == NC) { assert((int)obj->spi != NC);
error("SPI pinout mapping failed");
}
// enable power and clocking // enable power and clocking
switch ((int)obj->spi) { 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) { void spi_format(spi_t *obj, int bits, int mode, int slave) {
ssp_disable(obj); ssp_disable(obj);
if (!(bits >= 4 && bits <= 16) || !(mode >= 0 && mode <= 3)) { assert((bits >= 4 && bits <= 16) || (mode >= 0 && mode <= 3));
error("SPI format error");
}
int polarity = (mode & 0x2) ? 1 : 0; int polarity = (mode & 0x2) ? 1 : 0;
int phase = (mode & 0x1) ? 1 : 0; int phase = (mode & 0x1) ? 1 : 0;

View File

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

View File

@ -13,10 +13,10 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
#include <assert.h>
#include "analogout_api.h" #include "analogout_api.h"
#include "cmsis.h" #include "cmsis.h"
#include "pinmap.h" #include "pinmap.h"
#include "error.h"
static const PinMap PinMap_DAC[] = { static const PinMap PinMap_DAC[] = {
{P0_26, DAC_0, 2}, {P0_26, DAC_0, 2},
@ -25,9 +25,7 @@ static const PinMap PinMap_DAC[] = {
void analogout_init(dac_t *obj, PinName pin) { void analogout_init(dac_t *obj, PinName pin) {
obj->dac = (DACName)pinmap_peripheral(pin, PinMap_DAC); obj->dac = (DACName)pinmap_peripheral(pin, PinMap_DAC);
if (obj->dac == (DACName)NC) { assert(obj->dac != (DACName)NC);
error("DAC pin mapping failed");
}
// power is on by default, set DAC clk divider is /4 // power is on by default, set DAC clk divider is /4
LPC_SC->PCLKSEL0 &= ~(0x3 << 22); LPC_SC->PCLKSEL0 &= ~(0x3 << 22);

View File

@ -13,10 +13,10 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
#include <assert.h>
#include "can_api.h" #include "can_api.h"
#include "cmsis.h" #include "cmsis.h"
#include "pinmap.h" #include "pinmap.h"
#include "error.h"
#include <math.h> #include <math.h>
#include <string.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_rd = (CANName)pinmap_peripheral(rd, PinMap_CAN_RD);
CANName can_td = (CANName)pinmap_peripheral(td, PinMap_CAN_TD); CANName can_td = (CANName)pinmap_peripheral(td, PinMap_CAN_TD);
obj->dev = (LPC_CAN_TypeDef *)pinmap_merge(can_rd, can_td); obj->dev = (LPC_CAN_TypeDef *)pinmap_merge(can_rd, can_td);
if ((int)obj->dev == NC) { assert((int)obj->dev != NC);
error("CAN pin mapping failed");
}
switch ((int)obj->dev) { switch ((int)obj->dev) {
case CAN_1: LPC_SC->PCONP |= 1 << 13; break; case CAN_1: LPC_SC->PCONP |= 1 << 13; break;

View File

@ -16,7 +16,6 @@
#include "i2c_api.h" #include "i2c_api.h"
#include "cmsis.h" #include "cmsis.h"
#include "pinmap.h" #include "pinmap.h"
#include "error.h"
static const PinMap PinMap_I2C_SDA[] = { static const PinMap PinMap_I2C_SDA[] = {
{P0_0 , I2C_1, 3}, {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_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA);
I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL); I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL);
obj->i2c = (LPC_I2C_TypeDef *)pinmap_merge(i2c_sda, i2c_scl); obj->i2c = (LPC_I2C_TypeDef *)pinmap_merge(i2c_sda, i2c_scl);
assert((int)obj->i2c != NC);
if ((int)obj->i2c == NC) {
error("I2C pin mapping failed");
}
// enable power // enable power
i2c_power_enable(obj); i2c_power_enable(obj);

View File

@ -13,10 +13,10 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
#include <assert.h>
#include "pwmout_api.h" #include "pwmout_api.h"
#include "cmsis.h" #include "cmsis.h"
#include "pinmap.h" #include "pinmap.h"
#include "error.h"
#define TCR_CNT_EN 0x00000001 #define TCR_CNT_EN 0x00000001
#define TCR_RESET 0x00000002 #define TCR_RESET 0x00000002
@ -57,9 +57,8 @@ static unsigned int pwm_clock_mhz;
void pwmout_init(pwmout_t* obj, PinName pin) { void pwmout_init(pwmout_t* obj, PinName pin) {
// determine the channel // determine the channel
PWMName pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM); PWMName pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM);
if (pwm == (PWMName)NC) assert(pwm != (PWMName)NC);
error("PwmOut pin mapping failed");
obj->pwm = pwm; obj->pwm = pwm;
obj->MR = PWM_MATCH[pwm]; obj->MR = PWM_MATCH[pwm];

View File

@ -14,6 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
// math.h required for floating point operations for baud rate calculation // math.h required for floating point operations for baud rate calculation
#include <assert.h>
#include <math.h> #include <math.h>
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
@ -21,7 +22,6 @@
#include "serial_api.h" #include "serial_api.h"
#include "cmsis.h" #include "cmsis.h"
#include "pinmap.h" #include "pinmap.h"
#include "error.h"
/****************************************************************************** /******************************************************************************
* INITIALIZATION * 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_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX);
UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX); UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX);
UARTName uart = (UARTName)pinmap_merge(uart_tx, uart_rx); UARTName uart = (UARTName)pinmap_merge(uart_tx, uart_rx);
if ((int)uart == NC) { assert((int)uart == NC);
error("Serial pinout mapping failed");
}
obj->uart = (LPC_UART_TypeDef *)uart; obj->uart = (LPC_UART_TypeDef *)uart;
// enable power // enable power
@ -123,6 +121,7 @@ void serial_free(serial_t *obj) {
// serial_baud // serial_baud
// set the baud rate, taking in to account the current SystemFrequency // set the baud rate, taking in to account the current SystemFrequency
void serial_baud(serial_t *obj, int baudrate) { 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 // The LPC2300 and LPC1700 have a divider and a fractional divider to control the
// baud rate. The formula is: // 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_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_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; 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; 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) { void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits) {
// 0: 1 stop bits, 1: 2 stop bits assert((stop_bits == 1) || (stop_bits == 2)); // 0: 1 stop bits, 1: 2 stop bits
if (stop_bits != 1 && stop_bits != 2) { assert((data_bits > 4) || (data_bits < 9)); // 0: 5 data bits ... 3: 8 data bits
error("Invalid stop bits specified"); assert(parity < (ParityForced0 + 1));
}
stop_bits -= 1; 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; data_bits -= 5;
int parity_enable, parity_select; 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 ParityForced1: parity_enable = 1; parity_select = 2; break;
case ParityForced0: parity_enable = 1; parity_select = 3; break; case ParityForced0: parity_enable = 1; parity_select = 3; break;
default: default:
error("Invalid serial parity setting"); break;
return;
} }
obj->uart->LCR = data_bits << 0 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_data = (SPIName)pinmap_merge(spi_mosi, spi_miso);
SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel); SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel);
obj->spi = (LPC_SSP_TypeDef*)pinmap_merge(spi_data, spi_cntl); obj->spi = (LPC_SSP_TypeDef*)pinmap_merge(spi_data, spi_cntl);
assert((int)obj->spi != NC);
if ((int)obj->spi == NC) {
error("SPI pinout mapping failed");
}
// enable power and clocking // enable power and clocking
switch ((int)obj->spi) { 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_free(spi_t *obj) {}
void spi_format(spi_t *obj, int bits, int mode, int slave) { void spi_format(spi_t *obj, int bits, int mode, int slave) {
assert((bits >= 4 && bits <= 16) || (mode >= 0 && mode <= 3));
ssp_disable(obj); ssp_disable(obj);
if (!(bits >= 4 && bits <= 16) || !(mode >= 0 && mode <= 3)) {
error("SPI format error");
}
int polarity = (mode & 0x2) ? 1 : 0; int polarity = (mode & 0x2) ? 1 : 0;
int phase = (mode & 0x1) ? 1 : 0; int phase = (mode & 0x1) ? 1 : 0;

View File

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

View File

@ -13,10 +13,10 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
#include <assert.h>
#include "analogout_api.h" #include "analogout_api.h"
#include "cmsis.h" #include "cmsis.h"
#include "pinmap.h" #include "pinmap.h"
#include "error.h"
static const PinMap PinMap_DAC[] = { static const PinMap PinMap_DAC[] = {
{P0_26, DAC_0, 2}, {P0_26, DAC_0, 2},
@ -25,9 +25,7 @@ static const PinMap PinMap_DAC[] = {
void analogout_init(dac_t *obj, PinName pin) { void analogout_init(dac_t *obj, PinName pin) {
obj->dac = (DACName)pinmap_peripheral(pin, PinMap_DAC); obj->dac = (DACName)pinmap_peripheral(pin, PinMap_DAC);
if (obj->dac == (DACName)NC) { assert(obj->dac != (DACName)NC);
error("DAC pin mapping failed");
}
// DAC enable bit must be set // DAC enable bit must be set
LPC_IOCON->P0_26 |= (1 << 16); // DACEN LPC_IOCON->P0_26 |= (1 << 16); // DACEN

View File

@ -17,7 +17,6 @@
#include "cmsis.h" #include "cmsis.h"
#include "pinmap.h" #include "pinmap.h"
#include "error.h"
#include <math.h> #include <math.h>
#include <string.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_rd = (CANName)pinmap_peripheral(rd, PinMap_CAN_RD);
CANName can_td = (CANName)pinmap_peripheral(td, PinMap_CAN_TD); CANName can_td = (CANName)pinmap_peripheral(td, PinMap_CAN_TD);
obj->dev = (LPC_CAN_TypeDef *)pinmap_merge(can_rd, can_td); obj->dev = (LPC_CAN_TypeDef *)pinmap_merge(can_rd, can_td);
if ((int)obj->dev == NC) { assert((int)obj->dev != NC);
error("CAN pin mapping failed");
}
switch ((int)obj->dev) { switch ((int)obj->dev) {
case CAN_1: LPC_SC->PCONP |= 1 << 13; break; 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 * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
#include <assert.h>
#include "i2c_api.h" #include "i2c_api.h"
#include "cmsis.h" #include "cmsis.h"
#include "pinmap.h" #include "pinmap.h"
#include "error.h"
static const PinMap PinMap_I2C_SDA[] = { static const PinMap PinMap_I2C_SDA[] = {
{P0_0 , I2C_1, 3}, {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_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA);
I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL); I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL);
obj->i2c = (LPC_I2C_TypeDef *)pinmap_merge(i2c_sda, i2c_scl); obj->i2c = (LPC_I2C_TypeDef *)pinmap_merge(i2c_sda, i2c_scl);
if ((int)obj->i2c == NC) { assert((int)obj->i2c != NC);
error("I2C pin mapping failed");
}
// enable power // enable power
i2c_power_enable(obj); i2c_power_enable(obj);

View File

@ -13,10 +13,10 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
#include <assert.h>
#include "pwmout_api.h" #include "pwmout_api.h"
#include "cmsis.h" #include "cmsis.h"
#include "pinmap.h" #include "pinmap.h"
#include "error.h"
#define TCR_CNT_EN 0x00000001 #define TCR_CNT_EN 0x00000001
#define TCR_RESET 0x00000002 #define TCR_RESET 0x00000002
@ -66,9 +66,8 @@ static unsigned int pwm_clock_mhz;
void pwmout_init(pwmout_t* obj, PinName pin) { void pwmout_init(pwmout_t* obj, PinName pin) {
// determine the channel // determine the channel
PWMName pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM); PWMName pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM);
if (pwm == (PWMName)NC) assert(pwm != (PWMName)NC);
error("PwmOut pin mapping failed");
obj->channel = pwm; obj->channel = pwm;
obj->pwm = LPC_PWM0; 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_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX);
UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX); UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX);
UARTName uart = (UARTName)pinmap_merge(uart_tx, uart_rx); UARTName uart = (UARTName)pinmap_merge(uart_tx, uart_rx);
if ((int)uart == NC) { assert((int)uart != NC);
error("Serial pinout mapping failed");
}
obj->uart = (LPC_UART_TypeDef *)uart; obj->uart = (LPC_UART_TypeDef *)uart;
// enable power // 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) { void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits) {
// 0: 1 stop bits, 1: 2 stop bits assert((stop_bits == 1) || (stop_bits == 2)); // 0: 1 stop bits, 1: 2 stop bits
if (stop_bits != 1 && stop_bits != 2) { assert((data_bits > 4) || (data_bits < 9)); // 0: 5 data bits ... 3: 8 data bits
error("Invalid stop bits specified"); assert(parity < (ParityForced0 + 1));
}
stop_bits -= 1; 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; data_bits -= 5;
int parity_enable, parity_select; 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 ParityForced1: parity_enable = 1; parity_select = 2; break;
case ParityForced0: parity_enable = 1; parity_select = 3; break; case ParityForced0: parity_enable = 1; parity_select = 3; break;
default: default:
error("Invalid serial parity setting"); break;
return;
} }
obj->uart->LCR = data_bits << 0 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_data = (SPIName)pinmap_merge(spi_mosi, spi_miso);
SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel); SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel);
obj->spi = (LPC_SSP_TypeDef*)pinmap_merge(spi_data, spi_cntl); obj->spi = (LPC_SSP_TypeDef*)pinmap_merge(spi_data, spi_cntl);
if ((int)obj->spi == NC) { assert((int)obj->spi != NC);
error("SPI pinout mapping failed");
}
// enable power and clocking // enable power and clocking
switch ((int)obj->spi) { 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_free(spi_t *obj) {}
void spi_format(spi_t *obj, int bits, int mode, int slave) { void spi_format(spi_t *obj, int bits, int mode, int slave) {
assert((bits >= 4 && bits <= 16) || (mode >= 0 && mode <= 3));
ssp_disable(obj); ssp_disable(obj);
if (!(bits >= 4 && bits <= 16) || !(mode >= 0 && mode <= 3)) {
error("SPI format error");
}
int polarity = (mode & 0x2) ? 1 : 0; int polarity = (mode & 0x2) ? 1 : 0;
int phase = (mode & 0x1) ? 1 : 0; int phase = (mode & 0x1) ? 1 : 0;

View File

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

View File

@ -15,10 +15,10 @@
* *
* Ported to NXP LPC43XX by Micromint USA <support@micromint.com> * Ported to NXP LPC43XX by Micromint USA <support@micromint.com>
*/ */
#include <assert.h>
#include "analogout_api.h" #include "analogout_api.h"
#include "cmsis.h" #include "cmsis.h"
#include "pinmap.h" #include "pinmap.h"
#include "error.h"
static const PinMap PinMap_DAC[] = { static const PinMap PinMap_DAC[] = {
{P_DAC0 , DAC_0, 0x0}, {P_DAC0 , DAC_0, 0x0},
@ -27,9 +27,7 @@ static const PinMap PinMap_DAC[] = {
void analogout_init(dac_t *obj, PinName pin) { void analogout_init(dac_t *obj, PinName pin) {
obj->dac = (DACName)pinmap_peripheral(pin, PinMap_DAC); obj->dac = (DACName)pinmap_peripheral(pin, PinMap_DAC);
if (obj->dac == (DACName)NC) { assert(obj->dac != (DACName)NC);
error("DAC pin mapping failed");
}
// Configure the pin as GPIO input // Configure the pin as GPIO input
pin_function(pin, (SCU_PINIO_PULLNONE | 0x0)); 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) { void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits) {
// 0: 1 stop bits, 1: 2 stop bits assert((stop_bits == 1) || (stop_bits == 2)); // 0: 1 stop bits, 1: 2 stop bits
if (stop_bits != 1 && stop_bits != 2) { assert((data_bits > 4) || (data_bits < 9)); // 0: 5 data bits ... 3: 8 data bits
error("Invalid stop bits specified"); assert(parity < (ParityForced0 + 1));
}
stop_bits -= 1; 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; data_bits -= 5;
int parity_enable, parity_select; 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 ParityForced1: parity_enable = 1; parity_select = 2; break;
case ParityForced0: parity_enable = 1; parity_select = 3; break; case ParityForced0: parity_enable = 1; parity_select = 3; break;
default: default:
error("Invalid serial parity setting"); break;
return;
} }
obj->uart->LCR = data_bits << 0 obj->uart->LCR = data_bits << 0

View File

@ -15,6 +15,7 @@
* *
* Ported to NXP LPC43XX by Micromint USA <support@micromint.com> * Ported to NXP LPC43XX by Micromint USA <support@micromint.com>
*/ */
#include <assert.h>
#include <math.h> #include <math.h>
#include "spi_api.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); SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel);
obj->spi = (LPC_SSP_T*)pinmap_merge(spi_data, spi_cntl); obj->spi = (LPC_SSP_T*)pinmap_merge(spi_data, spi_cntl);
if ((int)obj->spi == NC) { assert((int)obj->spi != NC);
error("SPI pinout mapping failed");
}
// set default format and frequency // set default format and frequency
if (ssel == NC) { 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_free(spi_t *obj) {}
void spi_format(spi_t *obj, int bits, int mode, int slave) { void spi_format(spi_t *obj, int bits, int mode, int slave) {
assert((bits >= 4 && bits <= 16) || (mode >= 0 && mode <= 3));
ssp_disable(obj); ssp_disable(obj);
if (!(bits >= 4 && bits <= 16) || !(mode >= 0 && mode <= 3)) {
error("SPI format error");
}
int polarity = (mode & 0x2) ? 1 : 0; int polarity = (mode & 0x2) ? 1 : 0;
int phase = (mode & 0x1) ? 1 : 0; int phase = (mode & 0x1) ? 1 : 0;

View File

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