mirror of https://github.com/ARMmbed/mbed-os.git
[ALL TARGETS] error replaced by assert for preconditions
parent
fb90157c9a
commit
d843d56c08
|
@ -397,11 +397,13 @@ extern "C" WEAK void __cxa_pure_virtual(void) {
|
|||
#endif
|
||||
|
||||
#include "mbed_interface.h"
|
||||
|
||||
// mbed abort invokes only mbed die. The abort function is called from failing assert for example.
|
||||
namespace std {
|
||||
extern "C" void abort(void) {
|
||||
mbed_die();
|
||||
while(1);
|
||||
}
|
||||
}
|
||||
|
||||
// ****************************************************************************
|
||||
// mbed_main is a function that is called before main()
|
||||
|
|
|
@ -116,6 +116,10 @@ void serial_baud(serial_t *obj, int baudrate) {
|
|||
}
|
||||
|
||||
void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits) {
|
||||
assert((stop_bits == 1) || (stop_bits == 2));
|
||||
assert(parity < (ParityEven + 1));
|
||||
assert((data_bits == 8) || (data_bits == 9));
|
||||
assert((data_bits == 2) && (obj->index != 0));
|
||||
|
||||
// save C2 state
|
||||
uint32_t c2_state = (obj->uart->C2 & (UART_C2_RE_MASK | UART_C2_TE_MASK));
|
||||
|
@ -124,9 +128,6 @@ void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_b
|
|||
obj->uart->C2 &= ~(UART_C2_RE_MASK | UART_C2_TE_MASK);
|
||||
|
||||
// 8 data bits = 0 ... 9 data bits = 1
|
||||
if ((data_bits < 8) || (data_bits > 9))
|
||||
error("Invalid number of bits (%d) in serial format, should be 8..9", data_bits);
|
||||
|
||||
data_bits -= 8;
|
||||
|
||||
uint32_t parity_enable, parity_select;
|
||||
|
@ -135,22 +136,15 @@ void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_b
|
|||
case ParityOdd : parity_enable = 1; parity_select = 1; data_bits++; break;
|
||||
case ParityEven: parity_enable = 1; parity_select = 0; data_bits++; break;
|
||||
default:
|
||||
error("Invalid serial parity setting");
|
||||
return;
|
||||
break;
|
||||
}
|
||||
|
||||
// 1 stop bits = 0, 2 stop bits = 1
|
||||
if ((stop_bits != 1) && (stop_bits != 2))
|
||||
error("Invalid stop bits specified");
|
||||
stop_bits -= 1;
|
||||
|
||||
uint32_t m10 = 0;
|
||||
|
||||
// 9 data bits + parity
|
||||
if (data_bits == 2) {
|
||||
// only uart0 supports 10 bit communication
|
||||
if (obj->index != 0)
|
||||
error("Invalid number of bits (9) to be used with parity");
|
||||
data_bits = 0;
|
||||
m10 = 1;
|
||||
}
|
||||
|
|
|
@ -91,13 +91,8 @@ void spi_free(spi_t *obj) {
|
|||
// [TODO]
|
||||
}
|
||||
void spi_format(spi_t *obj, int bits, int mode, int slave) {
|
||||
if ((bits != 8) && (bits != 16)) {
|
||||
error("Only 8/16 bits SPI supported");
|
||||
}
|
||||
|
||||
if ((mode < 0) || (mode > 3)) {
|
||||
error("SPI mode unsupported");
|
||||
}
|
||||
assert((bits == 8) || (bits == 16));
|
||||
assert((mode > -1) && (mode < 4));
|
||||
|
||||
uint32_t polarity = (mode & 0x2) ? 1 : 0;
|
||||
uint32_t phase = (mode & 0x1) ? 1 : 0;
|
||||
|
|
|
@ -19,7 +19,6 @@
|
|||
|
||||
#include "cmsis.h"
|
||||
#include "pinmap.h"
|
||||
#include "error.h"
|
||||
|
||||
static const PinMap PinMap_SPI_SCLK[] = {
|
||||
{PTB0, SPI_0, 3},
|
||||
|
@ -51,9 +50,7 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel
|
|||
SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel);
|
||||
|
||||
obj->spi = (SPI_Type*)pinmap_merge(spi_data, spi_cntl);
|
||||
if ((int)obj->spi == NC) {
|
||||
error("SPI pinout mapping failed");
|
||||
}
|
||||
assert((int)obj->spi != NC);
|
||||
|
||||
// enable power and clocking
|
||||
switch ((int)obj->spi) {
|
||||
|
@ -87,13 +84,8 @@ void spi_free(spi_t *obj) {
|
|||
// [TODO]
|
||||
}
|
||||
void spi_format(spi_t *obj, int bits, int mode, int slave) {
|
||||
if (bits != 8) {
|
||||
error("Only 8bits SPI supported");
|
||||
}
|
||||
|
||||
if ((mode < 0) || (mode > 3)) {
|
||||
error("SPI mode unsupported");
|
||||
}
|
||||
assert(bits == 8);
|
||||
assert((mode > -1) && (mode < 4));
|
||||
|
||||
uint8_t polarity = (mode & 0x2) ? 1 : 0;
|
||||
uint8_t phase = (mode & 0x1) ? 1 : 0;
|
||||
|
|
|
@ -19,7 +19,6 @@
|
|||
|
||||
#include "cmsis.h"
|
||||
#include "pinmap.h"
|
||||
#include "error.h"
|
||||
#include "clk_freqs.h"
|
||||
#include "PeripheralPins.h"
|
||||
|
||||
|
@ -33,9 +32,7 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel
|
|||
SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel);
|
||||
|
||||
obj->spi = (SPI_Type*)pinmap_merge(spi_data, spi_cntl);
|
||||
if ((int)obj->spi == NC) {
|
||||
error("SPI pinout mapping failed");
|
||||
}
|
||||
assert((int)obj->spi != NC);
|
||||
|
||||
// enable power and clocking
|
||||
switch ((int)obj->spi) {
|
||||
|
@ -67,13 +64,8 @@ void spi_free(spi_t *obj) {
|
|||
// [TODO]
|
||||
}
|
||||
void spi_format(spi_t *obj, int bits, int mode, int slave) {
|
||||
if (bits != 8) {
|
||||
error("Only 8bits SPI supported");
|
||||
}
|
||||
|
||||
if ((mode < 0) || (mode > 3)) {
|
||||
error("SPI mode unsupported");
|
||||
}
|
||||
assert(bits == 8);
|
||||
assert((mode > -1) && (mode < 4));
|
||||
|
||||
uint8_t polarity = (mode & 0x2) ? 1 : 0;
|
||||
uint8_t phase = (mode & 0x1) ? 1 : 0;
|
||||
|
|
|
@ -13,13 +13,13 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#include <assert.h>
|
||||
#include "spi_api.h"
|
||||
|
||||
#include <math.h>
|
||||
|
||||
#include "cmsis.h"
|
||||
#include "pinmap.h"
|
||||
#include "error.h"
|
||||
|
||||
static const PinMap PinMap_SPI_SCLK[] = {
|
||||
{PTA15, SPI_0, 2},
|
||||
|
@ -90,9 +90,7 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel
|
|||
SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel);
|
||||
|
||||
obj->spi = (SPI_Type*)pinmap_merge(spi_data, spi_cntl);
|
||||
if ((int)obj->spi == NC) {
|
||||
error("SPI pinout mapping failed");
|
||||
}
|
||||
assert((int)obj->spi != NC);
|
||||
|
||||
// enable power and clocking
|
||||
switch ((int)obj->spi) {
|
||||
|
@ -125,13 +123,8 @@ void spi_free(spi_t *obj) {
|
|||
// [TODO]
|
||||
}
|
||||
void spi_format(spi_t *obj, int bits, int mode, int slave) {
|
||||
if ((bits != 8) && (bits != 16)) {
|
||||
error("Only 8/16 bits SPI supported");
|
||||
}
|
||||
|
||||
if ((mode < 0) || (mode > 3)) {
|
||||
error("SPI mode unsupported");
|
||||
}
|
||||
assert((bits == 8) || (bits == 16));
|
||||
assert((mode > -1) && (mode < 4));
|
||||
|
||||
uint8_t polarity = (mode & 0x2) ? 1 : 0;
|
||||
uint8_t phase = (mode & 0x1) ? 1 : 0;
|
||||
|
|
|
@ -43,7 +43,7 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
|
|||
I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA);
|
||||
I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL);
|
||||
obj->i2c = (I2C_Type*)pinmap_merge(i2c_sda, i2c_scl);
|
||||
assert(obj->i2c != (PinName)NC);
|
||||
assert((int)obj->i2c != NC);
|
||||
|
||||
// enable power
|
||||
switch ((int)obj->i2c) {
|
||||
|
|
|
@ -148,17 +148,16 @@ void serial_baud(serial_t *obj, int baudrate) {
|
|||
}
|
||||
|
||||
void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits) {
|
||||
|
||||
assert((stop_bits == 1) || (stop_bits == 2));
|
||||
assert(parity < (ParityEven + 1));
|
||||
assert(data_bits == 8); // TODO: Support other number of data bits (also in the write method!)
|
||||
|
||||
// save C2 state
|
||||
uint8_t c2_state = (obj->uart->C2 & (UARTLP_C2_RE_MASK | UARTLP_C2_TE_MASK));
|
||||
|
||||
// Disable UART before changing registers
|
||||
obj->uart->C2 &= ~(UARTLP_C2_RE_MASK | UARTLP_C2_TE_MASK);
|
||||
|
||||
// TODO: Support other number of data bits (also in the write method!)
|
||||
if ((data_bits < 8) || (data_bits > 8)) {
|
||||
error("Invalid number of bits (%d) in serial format, should be 8", data_bits);
|
||||
}
|
||||
|
||||
uint8_t parity_enable, parity_select;
|
||||
switch (parity) {
|
||||
|
@ -166,14 +165,9 @@ void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_b
|
|||
case ParityOdd : parity_enable = 1; parity_select = 1; data_bits++; break;
|
||||
case ParityEven: parity_enable = 1; parity_select = 0; data_bits++; break;
|
||||
default:
|
||||
error("Invalid serial parity setting");
|
||||
return;
|
||||
break;
|
||||
}
|
||||
|
||||
// 1 stop bits = 0, 2 stop bits = 1
|
||||
if ((stop_bits != 1) && (stop_bits != 2)) {
|
||||
error("Invalid stop bits specified");
|
||||
}
|
||||
stop_bits -= 1;
|
||||
|
||||
// data bits, parity and parity mode
|
||||
|
|
|
@ -43,7 +43,7 @@ void gpio_mode(gpio_t *obj, PinMode mode) {
|
|||
}
|
||||
|
||||
void gpio_dir(gpio_t *obj, PinDirection direction) {
|
||||
assert(pin != (PinName)NC);
|
||||
assert(obj->pin != (PinName)NC);
|
||||
switch (direction) {
|
||||
case PIN_INPUT :
|
||||
*obj->reg_dir &= ~obj->mask;
|
||||
|
|
|
@ -25,6 +25,7 @@
|
|||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#include <assert.h>
|
||||
#include "analogin_api.h"
|
||||
#include "wait_api.h"
|
||||
|
||||
|
@ -32,7 +33,6 @@
|
|||
|
||||
#include "cmsis.h"
|
||||
#include "pinmap.h"
|
||||
#include "error.h"
|
||||
|
||||
static const PinMap PinMap_ADC[] = {
|
||||
{PA_0, ADC_1, STM_PIN_DATA(GPIO_Mode_AN, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0xFF)}, // ADC_IN0
|
||||
|
@ -53,10 +53,7 @@ void analogin_init(analogin_t *obj, PinName pin) {
|
|||
|
||||
// Get the peripheral name (ADC_1, ADC_2...) from the pin and assign it to the object
|
||||
obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC);
|
||||
|
||||
if (obj->adc == (ADCName)NC) {
|
||||
error("ADC pin mapping failed");
|
||||
}
|
||||
assert(obj->adc != (ADCName)NC);
|
||||
|
||||
// Configure GPIO
|
||||
pinmap_pinout(pin, PinMap_ADC);
|
||||
|
@ -76,7 +73,7 @@ void analogin_init(analogin_t *obj, PinName pin) {
|
|||
|
||||
// Configure ADC
|
||||
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
|
||||
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
|
||||
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
|
||||
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
|
||||
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_TRGO;
|
||||
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
|
||||
|
|
|
@ -27,17 +27,17 @@
|
|||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*******************************************************************************
|
||||
*/
|
||||
#include <assert.h>
|
||||
#include "i2c_api.h"
|
||||
|
||||
#if DEVICE_I2C
|
||||
|
||||
#include "cmsis.h"
|
||||
#include "pinmap.h"
|
||||
#include "error.h"
|
||||
|
||||
/* Timeout values for flags and events waiting loops. These timeouts are
|
||||
not based on accurate values, they just guarantee that the application will
|
||||
not remain stuck if the I2C communication is corrupted. */
|
||||
not based on accurate values, they just guarantee that the application will
|
||||
not remain stuck if the I2C communication is corrupted. */
|
||||
#define FLAG_TIMEOUT ((int)0x1000)
|
||||
#define LONG_TIMEOUT ((int)0x8000)
|
||||
|
||||
|
@ -51,19 +51,16 @@ static const PinMap PinMap_I2C_SCL[] = {
|
|||
{NC, NC, 0}
|
||||
};
|
||||
|
||||
void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
|
||||
void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
|
||||
// Determine the I2C to use
|
||||
I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA);
|
||||
I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL);
|
||||
|
||||
obj->i2c = (I2CName)pinmap_merge(i2c_sda, i2c_scl);
|
||||
|
||||
if (obj->i2c == (I2CName)NC) {
|
||||
error("I2C pin mapping failed");
|
||||
}
|
||||
assert(obj->i2c != (I2CName)NC);
|
||||
|
||||
// Enable I2C clock
|
||||
if (obj->i2c == I2C_1) {
|
||||
if (obj->i2c == I2C_1) {
|
||||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
|
||||
}
|
||||
//if (obj->i2c == I2C_2) {
|
||||
|
@ -80,10 +77,11 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
|
|||
i2c_reset(obj);
|
||||
|
||||
// I2C configuration
|
||||
i2c_frequency(obj, 100000); // 100 kHz per default
|
||||
i2c_frequency(obj, 100000); // 100 kHz per default
|
||||
}
|
||||
|
||||
void i2c_frequency(i2c_t *obj, int hz) {
|
||||
assert((hz == 100000) || (hz == 200000) || (hz == 400000)); //"Only 100kHz, 200kHz and 400kHz I2C frequencies are supported."
|
||||
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
|
||||
I2C_InitTypeDef I2C_InitStructure;
|
||||
uint32_t tim = 0;
|
||||
|
@ -101,7 +99,6 @@ void i2c_frequency(i2c_t *obj, int hz) {
|
|||
tim = 0x0010020A; // Fast mode
|
||||
break;
|
||||
default:
|
||||
error("Only 100kHz, 200kHz and 400kHz I2C frequencies are supported.");
|
||||
break;
|
||||
}
|
||||
|
||||
|
@ -212,7 +209,7 @@ int i2c_byte_read(i2c_t *obj, int last) {
|
|||
int timeout;
|
||||
|
||||
// Wait until the byte is received
|
||||
timeout = FLAG_TIMEOUT;
|
||||
timeout = FLAG_TIMEOUT;
|
||||
while (I2C_GetFlagStatus(i2c, I2C_ISR_RXNE) == RESET) {
|
||||
timeout--;
|
||||
if (timeout == 0) {
|
||||
|
@ -244,13 +241,13 @@ int i2c_byte_write(i2c_t *obj, int data) {
|
|||
}
|
||||
|
||||
void i2c_reset(i2c_t *obj) {
|
||||
if (obj->i2c == I2C_1) {
|
||||
if (obj->i2c == I2C_1) {
|
||||
RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, ENABLE);
|
||||
RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, DISABLE);
|
||||
}
|
||||
//if (obj->i2c == I2C_2) {
|
||||
// RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, ENABLE);
|
||||
// RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, DISABLE);
|
||||
// RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, DISABLE);
|
||||
//}
|
||||
}
|
||||
|
||||
|
|
|
@ -27,10 +27,10 @@
|
|||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*******************************************************************************
|
||||
*/
|
||||
#include <assert.h>
|
||||
#include "serial_api.h"
|
||||
#include "cmsis.h"
|
||||
#include "pinmap.h"
|
||||
#include "error.h"
|
||||
#include <string.h>
|
||||
|
||||
static const PinMap PinMap_UART_TX[] = {
|
||||
|
@ -71,24 +71,21 @@ static void init_usart(serial_t *obj) {
|
|||
USART_Cmd(usart, ENABLE);
|
||||
}
|
||||
|
||||
void serial_init(serial_t *obj, PinName tx, PinName rx) {
|
||||
void serial_init(serial_t *obj, PinName tx, PinName rx) {
|
||||
// Determine the UART to use (UART_1, UART_2, ...)
|
||||
UARTName uart_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX);
|
||||
UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX);
|
||||
|
||||
// Get the peripheral name (UART_1, UART_2, ...) from the pin and assign it to the object
|
||||
obj->uart = (UARTName)pinmap_merge(uart_tx, uart_rx);
|
||||
|
||||
if (obj->uart == (UARTName)NC) {
|
||||
error("Serial pinout mapping failed");
|
||||
}
|
||||
assert(obj->uart != (UARTName)NC);
|
||||
|
||||
// Enable USART clock
|
||||
if (obj->uart == UART_1) {
|
||||
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
|
||||
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
|
||||
}
|
||||
if (obj->uart == UART_2) {
|
||||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
|
||||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
|
||||
}
|
||||
|
||||
// Configure the UART pins
|
||||
|
@ -101,7 +98,7 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) {
|
|||
obj->baudrate = 9600;
|
||||
obj->databits = USART_WordLength_8b;
|
||||
obj->stopbits = USART_StopBits_1;
|
||||
obj->parity = USART_Parity_No;
|
||||
obj->parity = USART_Parity_No;
|
||||
|
||||
init_usart(obj);
|
||||
|
||||
|
@ -140,7 +137,7 @@ void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_b
|
|||
obj->parity = USART_Parity_Odd;
|
||||
break;
|
||||
case ParityEven:
|
||||
case ParityForced1:
|
||||
case ParityForced1:
|
||||
obj->parity = USART_Parity_Even;
|
||||
break;
|
||||
default: // ParityNone
|
||||
|
@ -206,7 +203,7 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) {
|
|||
}
|
||||
else { // TxIrq
|
||||
USART_ITConfig(usart, USART_IT_TC, ENABLE);
|
||||
}
|
||||
}
|
||||
|
||||
NVIC_SetVector(irq_n, vector);
|
||||
NVIC_EnableIRQ(irq_n);
|
||||
|
@ -223,12 +220,12 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) {
|
|||
else { // TxIrq
|
||||
USART_ITConfig(usart, USART_IT_TXE, DISABLE);
|
||||
// Check if RxIrq is disabled too
|
||||
if ((usart->CR1 & USART_CR1_RXNEIE) == 0) all_disabled = 1;
|
||||
if ((usart->CR1 & USART_CR1_RXNEIE) == 0) all_disabled = 1;
|
||||
}
|
||||
|
||||
if (all_disabled) NVIC_DisableIRQ(irq_n);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/******************************************************************************
|
||||
|
|
|
@ -27,6 +27,7 @@
|
|||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*******************************************************************************
|
||||
*/
|
||||
#include <assert.h>
|
||||
#include "spi_api.h"
|
||||
|
||||
#if DEVICE_SPI
|
||||
|
@ -34,7 +35,6 @@
|
|||
#include <math.h>
|
||||
#include "cmsis.h"
|
||||
#include "pinmap.h"
|
||||
#include "error.h"
|
||||
|
||||
static const PinMap PinMap_SPI_MOSI[] = {
|
||||
{PA_7, SPI_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_NOPULL, GPIO_AF_0)},
|
||||
|
@ -64,11 +64,11 @@ static void init_spi(spi_t *obj) {
|
|||
SPI_Cmd(spi, DISABLE);
|
||||
|
||||
SPI_InitStructure.SPI_Mode = obj->mode;
|
||||
SPI_InitStructure.SPI_NSS = obj->nss;
|
||||
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
|
||||
SPI_InitStructure.SPI_NSS = obj->nss;
|
||||
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
|
||||
SPI_InitStructure.SPI_DataSize = obj->bits;
|
||||
SPI_InitStructure.SPI_CPOL = obj->cpol;
|
||||
SPI_InitStructure.SPI_CPHA = obj->cpha;
|
||||
SPI_InitStructure.SPI_CPHA = obj->cpha;
|
||||
SPI_InitStructure.SPI_BaudRatePrescaler = obj->br_presc;
|
||||
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
|
||||
SPI_InitStructure.SPI_CRCPolynomial = 7;
|
||||
|
@ -90,17 +90,14 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel
|
|||
SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel);
|
||||
|
||||
obj->spi = (SPIName)pinmap_merge(spi_data, spi_cntl);
|
||||
|
||||
if (obj->spi == (SPIName)NC) {
|
||||
error("SPI pinout mapping failed");
|
||||
}
|
||||
assert(obj->spi != (SPIName)NC);
|
||||
|
||||
// Enable SPI clock
|
||||
if (obj->spi == SPI_1) {
|
||||
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
|
||||
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
|
||||
}
|
||||
if (obj->spi == SPI_2) {
|
||||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
|
||||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
|
||||
}
|
||||
|
||||
// Configure the SPI pins
|
||||
|
@ -132,7 +129,7 @@ void spi_free(spi_t *obj) {
|
|||
SPI_I2S_DeInit(spi);
|
||||
}
|
||||
|
||||
void spi_format(spi_t *obj, int bits, int mode, int slave) {
|
||||
void spi_format(spi_t *obj, int bits, int mode, int slave) {
|
||||
// Save new values
|
||||
if (bits == 8) {
|
||||
obj->bits = SPI_DataSize_8b;
|
||||
|
@ -152,11 +149,11 @@ void spi_format(spi_t *obj, int bits, int mode, int slave) {
|
|||
break;
|
||||
case 2:
|
||||
obj->cpol = SPI_CPOL_High;
|
||||
obj->cpha = SPI_CPHA_1Edge;
|
||||
obj->cpha = SPI_CPHA_1Edge;
|
||||
break;
|
||||
default:
|
||||
obj->cpol = SPI_CPOL_High;
|
||||
obj->cpha = SPI_CPHA_2Edge;
|
||||
obj->cpha = SPI_CPHA_2Edge;
|
||||
break;
|
||||
}
|
||||
|
||||
|
@ -166,7 +163,7 @@ void spi_format(spi_t *obj, int bits, int mode, int slave) {
|
|||
}
|
||||
else {
|
||||
obj->mode = SPI_Mode_Slave;
|
||||
obj->nss = SPI_NSS_Hard;
|
||||
obj->nss = SPI_NSS_Hard;
|
||||
}
|
||||
|
||||
init_spi(obj);
|
||||
|
@ -201,7 +198,7 @@ static inline int ssp_readable(spi_t *obj) {
|
|||
SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
|
||||
// Check if data is received
|
||||
status = ((SPI_I2S_GetFlagStatus(spi, SPI_I2S_FLAG_RXNE) != RESET) ? 1 : 0);
|
||||
return status;
|
||||
return status;
|
||||
}
|
||||
|
||||
static inline int ssp_writeable(spi_t *obj) {
|
||||
|
@ -213,7 +210,7 @@ static inline int ssp_writeable(spi_t *obj) {
|
|||
}
|
||||
|
||||
static inline void ssp_write(spi_t *obj, int value) {
|
||||
SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
|
||||
SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
|
||||
while (!ssp_writeable(obj));
|
||||
if(obj->bits == SPI_DataSize_8b) // 8 bit mode
|
||||
SPI_SendData8(spi, (uint8_t)value);
|
||||
|
@ -222,12 +219,12 @@ static inline void ssp_write(spi_t *obj, int value) {
|
|||
}
|
||||
|
||||
static inline int ssp_read(spi_t *obj) {
|
||||
SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
|
||||
SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
|
||||
while (!ssp_readable(obj));
|
||||
if(obj->bits == SPI_DataSize_8b) // 8 bit mode
|
||||
return (int)SPI_ReceiveData8(spi);
|
||||
else // 16 bit mode
|
||||
return (int)SPI_I2S_ReceiveData16(spi);
|
||||
return (int)SPI_I2S_ReceiveData16(spi);
|
||||
}
|
||||
|
||||
static inline int ssp_busy(spi_t *obj) {
|
||||
|
@ -250,16 +247,16 @@ int spi_slave_read(spi_t *obj) {
|
|||
SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
|
||||
if(obj->bits == SPI_DataSize_8b) // 8 bit mode
|
||||
return (int)SPI_ReceiveData8(spi);
|
||||
else
|
||||
return (int)SPI_I2S_ReceiveData16(spi);
|
||||
else
|
||||
return (int)SPI_I2S_ReceiveData16(spi);
|
||||
}
|
||||
|
||||
void spi_slave_write(spi_t *obj, int value) {
|
||||
SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
|
||||
while (!ssp_writeable(obj));
|
||||
SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
|
||||
while (!ssp_writeable(obj));
|
||||
if(obj->bits == SPI_DataSize_8b) // 8 bit mode
|
||||
SPI_SendData8(spi, (uint8_t)value);
|
||||
else
|
||||
else
|
||||
SPI_I2S_SendData16(spi, (uint16_t)value);
|
||||
}
|
||||
|
||||
|
|
|
@ -25,6 +25,7 @@
|
|||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#include <assert.h>
|
||||
#include "analogin_api.h"
|
||||
#include "wait_api.h"
|
||||
|
||||
|
@ -32,7 +33,6 @@
|
|||
|
||||
#include "cmsis.h"
|
||||
#include "pinmap.h"
|
||||
#include "error.h"
|
||||
|
||||
static const PinMap PinMap_ADC[] = {
|
||||
{PA_0, ADC_1, STM_PIN_DATA(GPIO_Mode_AIN, 0)},
|
||||
|
@ -53,10 +53,7 @@ void analogin_init(analogin_t *obj, PinName pin) {
|
|||
|
||||
// Get the peripheral name (ADC_1, ADC_2...) from the pin and assign it to the object
|
||||
obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC);
|
||||
|
||||
if (obj->adc == (ADCName)NC) {
|
||||
error("ADC pin mapping failed");
|
||||
}
|
||||
assert(obj->adc != (ADCName)NC);
|
||||
|
||||
// Configure GPIO
|
||||
pinmap_pinout(pin, PinMap_ADC);
|
||||
|
@ -91,7 +88,7 @@ void analogin_init(analogin_t *obj, PinName pin) {
|
|||
ADC_ResetCalibration(adc);
|
||||
while(ADC_GetResetCalibrationStatus(adc));
|
||||
ADC_StartCalibration(adc);
|
||||
while(ADC_GetCalibrationStatus(adc));
|
||||
while(ADC_GetCalibrationStatus(adc));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -27,17 +27,17 @@
|
|||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*******************************************************************************
|
||||
*/
|
||||
#include <assert.h>
|
||||
#include "i2c_api.h"
|
||||
|
||||
#if DEVICE_I2C
|
||||
|
||||
#include "cmsis.h"
|
||||
#include "pinmap.h"
|
||||
#include "error.h"
|
||||
|
||||
/* Timeout values for flags and events waiting loops. These timeouts are
|
||||
not based on accurate values, they just guarantee that the application will
|
||||
not remain stuck if the I2C communication is corrupted. */
|
||||
not based on accurate values, they just guarantee that the application will
|
||||
not remain stuck if the I2C communication is corrupted. */
|
||||
#define FLAG_TIMEOUT ((int)0x1000)
|
||||
#define LONG_TIMEOUT ((int)0x8000)
|
||||
|
||||
|
@ -51,19 +51,16 @@ static const PinMap PinMap_I2C_SCL[] = {
|
|||
{NC, NC, 0}
|
||||
};
|
||||
|
||||
void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
|
||||
void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
|
||||
// Determine the I2C to use
|
||||
I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA);
|
||||
I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL);
|
||||
|
||||
obj->i2c = (I2CName)pinmap_merge(i2c_sda, i2c_scl);
|
||||
|
||||
if (obj->i2c == (I2CName)NC) {
|
||||
error("I2C pin mapping failed");
|
||||
}
|
||||
assert(obj->i2c != (I2CName)NC);
|
||||
|
||||
// Enable I2C clock
|
||||
if (obj->i2c == I2C_1) {
|
||||
if (obj->i2c == I2C_1) {
|
||||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
|
||||
}
|
||||
if (obj->i2c == I2C_2) {
|
||||
|
@ -80,7 +77,7 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
|
|||
i2c_reset(obj);
|
||||
|
||||
// I2C configuration
|
||||
i2c_frequency(obj, 100000); // 100 kHz per default
|
||||
i2c_frequency(obj, 100000); // 100 kHz per default
|
||||
}
|
||||
|
||||
void i2c_frequency(i2c_t *obj, int hz) {
|
||||
|
@ -110,7 +107,7 @@ inline int i2c_start(i2c_t *obj) {
|
|||
I2C_ClearFlag(i2c, I2C_FLAG_AF); // Clear Acknowledge failure flag
|
||||
|
||||
// Generate the START condition
|
||||
I2C_GenerateSTART(i2c, ENABLE);
|
||||
I2C_GenerateSTART(i2c, ENABLE);
|
||||
|
||||
// Wait the START condition has been correctly sent
|
||||
timeout = FLAG_TIMEOUT;
|
||||
|
@ -155,7 +152,7 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
|
|||
i2c_start(obj);
|
||||
|
||||
// Send slave address for read
|
||||
I2C_Send7bitAddress(i2c, address, I2C_Direction_Receiver);
|
||||
I2C_Send7bitAddress(i2c, address, I2C_Direction_Receiver);
|
||||
|
||||
// Wait address is acknowledged
|
||||
timeout = FLAG_TIMEOUT;
|
||||
|
@ -264,7 +261,7 @@ int i2c_byte_write(i2c_t *obj, int data) {
|
|||
I2C_SendData(i2c, (uint8_t)data);
|
||||
|
||||
// Wait until the byte is transmitted
|
||||
timeout = FLAG_TIMEOUT;
|
||||
timeout = FLAG_TIMEOUT;
|
||||
//while (I2C_CheckEvent(i2c, I2C_EVENT_MASTER_BYTE_TRANSMITTED) == ERROR) {
|
||||
while ((I2C_GetFlagStatus(i2c, I2C_FLAG_TXE) == RESET) &&
|
||||
(I2C_GetFlagStatus(i2c, I2C_FLAG_BTF) == RESET)) {
|
||||
|
@ -278,13 +275,13 @@ int i2c_byte_write(i2c_t *obj, int data) {
|
|||
}
|
||||
|
||||
void i2c_reset(i2c_t *obj) {
|
||||
if (obj->i2c == I2C_1) {
|
||||
if (obj->i2c == I2C_1) {
|
||||
RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, ENABLE);
|
||||
RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, DISABLE);
|
||||
}
|
||||
if (obj->i2c == I2C_2) {
|
||||
RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, ENABLE);
|
||||
RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, DISABLE);
|
||||
RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, DISABLE);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -27,11 +27,11 @@
|
|||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*******************************************************************************
|
||||
*/
|
||||
#include <assert.h>
|
||||
#include "pwmout_api.h"
|
||||
|
||||
#include "cmsis.h"
|
||||
#include "pinmap.h"
|
||||
#include "error.h"
|
||||
|
||||
static const PinMap PinMap_PWM[] = {
|
||||
// TIM2 full remap
|
||||
|
@ -46,10 +46,7 @@ static const PinMap PinMap_PWM[] = {
|
|||
void pwmout_init(pwmout_t* obj, PinName pin) {
|
||||
// Get the peripheral name from the pin and assign it to the object
|
||||
obj->pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM);
|
||||
|
||||
if (obj->pwm == (PWMName)NC) {
|
||||
error("PWM pinout mapping failed");
|
||||
}
|
||||
assert(obj->pwm != (PWMName)NC);
|
||||
|
||||
// Enable TIM clock
|
||||
if (obj->pwm == PWM_2) RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
|
||||
|
|
|
@ -27,10 +27,10 @@
|
|||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*******************************************************************************
|
||||
*/
|
||||
#include <assert.h>
|
||||
#include "serial_api.h"
|
||||
#include "cmsis.h"
|
||||
#include "pinmap.h"
|
||||
#include "error.h"
|
||||
#include <string.h>
|
||||
|
||||
static const PinMap PinMap_UART_TX[] = {
|
||||
|
@ -73,21 +73,18 @@ static void init_usart(serial_t *obj) {
|
|||
USART_Cmd(usart, ENABLE);
|
||||
}
|
||||
|
||||
void serial_init(serial_t *obj, PinName tx, PinName rx) {
|
||||
void serial_init(serial_t *obj, PinName tx, PinName rx) {
|
||||
// Determine the UART to use (UART_1, UART_2, ...)
|
||||
UARTName uart_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX);
|
||||
UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX);
|
||||
|
||||
// Get the peripheral name (UART_1, UART_2, ...) from the pin and assign it to the object
|
||||
obj->uart = (UARTName)pinmap_merge(uart_tx, uart_rx);
|
||||
|
||||
if (obj->uart == (UARTName)NC) {
|
||||
error("Serial pinout mapping failed");
|
||||
}
|
||||
assert(obj->uart != (UARTName)NC);
|
||||
|
||||
// Enable USART clock
|
||||
if (obj->uart == UART_1) {
|
||||
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
|
||||
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
|
||||
} else if (obj->uart == UART_2 ) {
|
||||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
|
||||
} else if (obj->uart == UART_3 ) {
|
||||
|
@ -102,7 +99,7 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) {
|
|||
obj->baudrate = 9600;
|
||||
obj->databits = USART_WordLength_8b;
|
||||
obj->stopbits = USART_StopBits_1;
|
||||
obj->parity = USART_Parity_No;
|
||||
obj->parity = USART_Parity_No;
|
||||
|
||||
init_usart(obj);
|
||||
|
||||
|
@ -142,7 +139,7 @@ void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_b
|
|||
obj->parity = USART_Parity_Odd;
|
||||
break;
|
||||
case ParityEven:
|
||||
case ParityForced1:
|
||||
case ParityForced1:
|
||||
obj->parity = USART_Parity_Even;
|
||||
break;
|
||||
default: // ParityNone
|
||||
|
@ -214,7 +211,7 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) {
|
|||
}
|
||||
else { // TxIrq
|
||||
USART_ITConfig(usart, USART_IT_TC, ENABLE);
|
||||
}
|
||||
}
|
||||
|
||||
NVIC_SetVector(irq_n, vector);
|
||||
NVIC_EnableIRQ(irq_n);
|
||||
|
@ -231,12 +228,12 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) {
|
|||
else { // TxIrq
|
||||
USART_ITConfig(usart, USART_IT_TXE, DISABLE);
|
||||
// Check if RxIrq is disabled too
|
||||
if ((usart->CR1 & USART_CR1_RXNEIE) == 0) all_disabled = 1;
|
||||
if ((usart->CR1 & USART_CR1_RXNEIE) == 0) all_disabled = 1;
|
||||
}
|
||||
|
||||
if (all_disabled) NVIC_DisableIRQ(irq_n);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/******************************************************************************
|
||||
|
|
|
@ -27,6 +27,7 @@
|
|||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*******************************************************************************
|
||||
*/
|
||||
#include <assert.h>
|
||||
#include "spi_api.h"
|
||||
|
||||
#if DEVICE_SPI
|
||||
|
@ -34,7 +35,6 @@
|
|||
#include <math.h>
|
||||
#include "cmsis.h"
|
||||
#include "pinmap.h"
|
||||
#include "error.h"
|
||||
|
||||
static const PinMap PinMap_SPI_MOSI[] = {
|
||||
{PA_7, SPI_1, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)},
|
||||
|
@ -67,11 +67,11 @@ static void init_spi(spi_t *obj) {
|
|||
SPI_Cmd(spi, DISABLE);
|
||||
|
||||
SPI_InitStructure.SPI_Mode = obj->mode;
|
||||
SPI_InitStructure.SPI_NSS = obj->nss;
|
||||
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
|
||||
SPI_InitStructure.SPI_NSS = obj->nss;
|
||||
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
|
||||
SPI_InitStructure.SPI_DataSize = obj->bits;
|
||||
SPI_InitStructure.SPI_CPOL = obj->cpol;
|
||||
SPI_InitStructure.SPI_CPHA = obj->cpha;
|
||||
SPI_InitStructure.SPI_CPHA = obj->cpha;
|
||||
SPI_InitStructure.SPI_BaudRatePrescaler = obj->br_presc;
|
||||
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
|
||||
SPI_InitStructure.SPI_CRCPolynomial = 7;
|
||||
|
@ -91,14 +91,11 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel
|
|||
SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel);
|
||||
|
||||
obj->spi = (SPIName)pinmap_merge(spi_data, spi_cntl);
|
||||
|
||||
if (obj->spi == (SPIName)NC) {
|
||||
error("SPI pinout mapping failed");
|
||||
}
|
||||
assert(obj->spi != (SPIName)NC);
|
||||
|
||||
// Enable SPI clock
|
||||
if (obj->spi == SPI_1) {
|
||||
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
|
||||
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
|
||||
}
|
||||
|
||||
// Configure the SPI pins
|
||||
|
@ -130,7 +127,7 @@ void spi_free(spi_t *obj) {
|
|||
SPI_I2S_DeInit(spi);
|
||||
}
|
||||
|
||||
void spi_format(spi_t *obj, int bits, int mode, int slave) {
|
||||
void spi_format(spi_t *obj, int bits, int mode, int slave) {
|
||||
// Save new values
|
||||
if (bits == 8) {
|
||||
obj->bits = SPI_DataSize_8b;
|
||||
|
@ -150,11 +147,11 @@ void spi_format(spi_t *obj, int bits, int mode, int slave) {
|
|||
break;
|
||||
case 2:
|
||||
obj->cpol = SPI_CPOL_High;
|
||||
obj->cpha = SPI_CPHA_1Edge;
|
||||
obj->cpha = SPI_CPHA_1Edge;
|
||||
break;
|
||||
default:
|
||||
obj->cpol = SPI_CPOL_High;
|
||||
obj->cpha = SPI_CPHA_2Edge;
|
||||
obj->cpha = SPI_CPHA_2Edge;
|
||||
break;
|
||||
}
|
||||
|
||||
|
@ -164,7 +161,7 @@ void spi_format(spi_t *obj, int bits, int mode, int slave) {
|
|||
}
|
||||
else {
|
||||
obj->mode = SPI_Mode_Slave;
|
||||
obj->nss = SPI_NSS_Hard;
|
||||
obj->nss = SPI_NSS_Hard;
|
||||
}
|
||||
|
||||
init_spi(obj);
|
||||
|
@ -196,7 +193,7 @@ static inline int ssp_readable(spi_t *obj) {
|
|||
SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
|
||||
// Check if data is received
|
||||
status = ((SPI_I2S_GetFlagStatus(spi, SPI_I2S_FLAG_RXNE) != RESET) ? 1 : 0);
|
||||
return status;
|
||||
return status;
|
||||
}
|
||||
|
||||
static inline int ssp_writeable(spi_t *obj) {
|
||||
|
@ -208,13 +205,13 @@ static inline int ssp_writeable(spi_t *obj) {
|
|||
}
|
||||
|
||||
static inline void ssp_write(spi_t *obj, int value) {
|
||||
SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
|
||||
SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
|
||||
while (!ssp_writeable(obj));
|
||||
SPI_I2S_SendData(spi, (uint16_t)value);
|
||||
}
|
||||
|
||||
static inline int ssp_read(spi_t *obj) {
|
||||
SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
|
||||
SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
|
||||
while (!ssp_readable(obj));
|
||||
return (int)SPI_I2S_ReceiveData(spi);
|
||||
}
|
||||
|
@ -241,8 +238,8 @@ int spi_slave_read(spi_t *obj) {
|
|||
}
|
||||
|
||||
void spi_slave_write(spi_t *obj, int value) {
|
||||
SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
|
||||
while (!ssp_writeable(obj));
|
||||
SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
|
||||
while (!ssp_writeable(obj));
|
||||
SPI_I2S_SendData(spi, (uint16_t)value);
|
||||
}
|
||||
|
||||
|
|
|
@ -25,6 +25,7 @@
|
|||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#include <assert.h>
|
||||
#include "analogin_api.h"
|
||||
#include "wait_api.h"
|
||||
|
||||
|
@ -32,7 +33,6 @@
|
|||
|
||||
#include "cmsis.h"
|
||||
#include "pinmap.h"
|
||||
#include "error.h"
|
||||
|
||||
static const PinMap PinMap_ADC[] = {
|
||||
{PA_0, ADC_1, STM_PIN_DATA(GPIO_Mode_AN, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0xFF)}, // ADC_IN1
|
||||
|
@ -63,10 +63,7 @@ void analogin_init(analogin_t *obj, PinName pin) {
|
|||
|
||||
// Get the peripheral name from the pin and assign it to the object
|
||||
obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC);
|
||||
|
||||
if (obj->adc == (ADCName)NC) {
|
||||
error("ADC pin mapping failed");
|
||||
}
|
||||
assert(obj->adc != (ADCName)NC);
|
||||
|
||||
// Configure GPIO
|
||||
pinmap_pinout(pin, PinMap_ADC);
|
||||
|
@ -168,7 +165,7 @@ static inline uint16_t adc_read(analogin_t *obj) {
|
|||
break;
|
||||
case PA_7:
|
||||
channel = ADC_Channel_15;
|
||||
break;
|
||||
break;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -27,17 +27,17 @@
|
|||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*******************************************************************************
|
||||
*/
|
||||
#include <assert.h>
|
||||
#include "i2c_api.h"
|
||||
|
||||
#if DEVICE_I2C
|
||||
|
||||
#include "cmsis.h"
|
||||
#include "pinmap.h"
|
||||
#include "error.h"
|
||||
|
||||
/* Timeout values for flags and events waiting loops. These timeouts are
|
||||
not based on accurate values, they just guarantee that the application will
|
||||
not remain stuck if the I2C communication is corrupted. */
|
||||
not based on accurate values, they just guarantee that the application will
|
||||
not remain stuck if the I2C communication is corrupted. */
|
||||
#define FLAG_TIMEOUT ((int)0x1000)
|
||||
#define LONG_TIMEOUT ((int)0x8000)
|
||||
|
||||
|
@ -62,19 +62,16 @@ static const PinMap PinMap_I2C_SCL[] = {
|
|||
{NC, NC, 0}
|
||||
};
|
||||
|
||||
void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
|
||||
void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
|
||||
// Determine the I2C to use
|
||||
I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA);
|
||||
I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL);
|
||||
|
||||
obj->i2c = (I2CName)pinmap_merge(i2c_sda, i2c_scl);
|
||||
|
||||
if (obj->i2c == (I2CName)NC) {
|
||||
error("I2C pin mapping failed");
|
||||
}
|
||||
assert(obj->i2c != (I2CName)NC);
|
||||
|
||||
// Enable I2C clock
|
||||
if (obj->i2c == I2C_1) {
|
||||
if (obj->i2c == I2C_1) {
|
||||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
|
||||
}
|
||||
if (obj->i2c == I2C_2) {
|
||||
|
@ -94,10 +91,11 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
|
|||
i2c_reset(obj);
|
||||
|
||||
// I2C configuration
|
||||
i2c_frequency(obj, 100000); // 100 kHz per default
|
||||
i2c_frequency(obj, 100000); // 100 kHz per default
|
||||
}
|
||||
|
||||
void i2c_frequency(i2c_t *obj, int hz) {
|
||||
assert((hz == 100000) || (hz == 200000) || (hz == 400000) || (hz == 1000000));
|
||||
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
|
||||
I2C_InitTypeDef I2C_InitStructure;
|
||||
uint32_t tim;
|
||||
|
@ -140,7 +138,6 @@ void i2c_frequency(i2c_t *obj, int hz) {
|
|||
}
|
||||
break;
|
||||
default:
|
||||
error("Only 100kHz, 200kHz, 400kHz and 1MHz I2C frequencies are supported.");
|
||||
break;
|
||||
}
|
||||
|
||||
|
@ -252,7 +249,7 @@ int i2c_byte_read(i2c_t *obj, int last) {
|
|||
int timeout;
|
||||
|
||||
// Wait until the byte is received
|
||||
timeout = FLAG_TIMEOUT;
|
||||
timeout = FLAG_TIMEOUT;
|
||||
while (I2C_GetFlagStatus(i2c, I2C_ISR_RXNE) == RESET) {
|
||||
timeout--;
|
||||
if (timeout == 0) {
|
||||
|
@ -284,7 +281,7 @@ int i2c_byte_write(i2c_t *obj, int data) {
|
|||
}
|
||||
|
||||
void i2c_reset(i2c_t *obj) {
|
||||
if (obj->i2c == I2C_1) {
|
||||
if (obj->i2c == I2C_1) {
|
||||
RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, ENABLE);
|
||||
RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, DISABLE);
|
||||
}
|
||||
|
@ -294,7 +291,7 @@ void i2c_reset(i2c_t *obj) {
|
|||
}
|
||||
if (obj->i2c == I2C_3) {
|
||||
RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C3, ENABLE);
|
||||
RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C3, DISABLE);
|
||||
RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C3, DISABLE);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -27,11 +27,11 @@
|
|||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*******************************************************************************
|
||||
*/
|
||||
#include <assert.h>
|
||||
#include "pwmout_api.h"
|
||||
|
||||
#include "cmsis.h"
|
||||
#include "pinmap.h"
|
||||
#include "error.h"
|
||||
|
||||
// TIM2 cannot be used because already used by the us_ticker
|
||||
static const PinMap PinMap_PWM[] = {
|
||||
|
@ -85,13 +85,10 @@ static const PinMap PinMap_PWM[] = {
|
|||
{NC, NC, 0}
|
||||
};
|
||||
|
||||
void pwmout_init(pwmout_t* obj, PinName pin) {
|
||||
void pwmout_init(pwmout_t* obj, PinName pin) {
|
||||
// Get the peripheral name from the pin and assign it to the object
|
||||
obj->pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM);
|
||||
|
||||
if (obj->pwm == (PWMName)NC) {
|
||||
error("PWM pinout mapping failed");
|
||||
}
|
||||
assert(obj->pwm != (PWMName)NC);
|
||||
|
||||
// Enable TIM clock
|
||||
if (obj->pwm == PWM_1) RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
|
||||
|
@ -126,7 +123,7 @@ void pwmout_write(pwmout_t* obj, float value) {
|
|||
|
||||
obj->pulse = (uint32_t)((float)obj->period * value);
|
||||
|
||||
// Configure channels
|
||||
// Configure channels
|
||||
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
|
||||
TIM_OCInitStructure.TIM_Pulse = obj->pulse;
|
||||
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
|
||||
|
@ -215,7 +212,7 @@ void pwmout_write(pwmout_t* obj, float value) {
|
|||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
float pwmout_read(pwmout_t* obj) {
|
||||
|
@ -239,7 +236,7 @@ void pwmout_period_us(pwmout_t* obj, int us) {
|
|||
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
|
||||
float dc = pwmout_read(obj);
|
||||
|
||||
TIM_Cmd(tim, DISABLE);
|
||||
TIM_Cmd(tim, DISABLE);
|
||||
|
||||
obj->period = us;
|
||||
|
||||
|
|
|
@ -27,10 +27,10 @@
|
|||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*******************************************************************************
|
||||
*/
|
||||
#include <assert.h>
|
||||
#include "serial_api.h"
|
||||
#include "cmsis.h"
|
||||
#include "pinmap.h"
|
||||
#include "error.h"
|
||||
#include <string.h>
|
||||
|
||||
static const PinMap PinMap_UART_TX[] = {
|
||||
|
@ -85,27 +85,24 @@ static void init_usart(serial_t *obj) {
|
|||
USART_Cmd(usart, ENABLE);
|
||||
}
|
||||
|
||||
void serial_init(serial_t *obj, PinName tx, PinName rx) {
|
||||
void serial_init(serial_t *obj, PinName tx, PinName rx) {
|
||||
// Determine the UART to use
|
||||
UARTName uart_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX);
|
||||
UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX);
|
||||
|
||||
// Get the peripheral name from the pin and assign it to the object
|
||||
obj->uart = (UARTName)pinmap_merge(uart_tx, uart_rx);
|
||||
|
||||
if (obj->uart == (UARTName)NC) {
|
||||
error("Serial pinout mapping failed");
|
||||
}
|
||||
assert(obj->uart != (UARTName)NC);
|
||||
|
||||
// Enable USART clock
|
||||
if (obj->uart == UART_1) {
|
||||
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
|
||||
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
|
||||
}
|
||||
if (obj->uart == UART_2) {
|
||||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
|
||||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
|
||||
}
|
||||
if (obj->uart == UART_3) {
|
||||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
|
||||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
|
||||
}
|
||||
|
||||
// Configure the UART pins
|
||||
|
@ -118,7 +115,7 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) {
|
|||
obj->baudrate = 9600;
|
||||
obj->databits = USART_WordLength_8b;
|
||||
obj->stopbits = USART_StopBits_1;
|
||||
obj->parity = USART_Parity_No;
|
||||
obj->parity = USART_Parity_No;
|
||||
|
||||
init_usart(obj);
|
||||
|
||||
|
@ -157,7 +154,7 @@ void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_b
|
|||
obj->parity = USART_Parity_Odd;
|
||||
break;
|
||||
case ParityEven:
|
||||
case ParityForced1:
|
||||
case ParityForced1:
|
||||
obj->parity = USART_Parity_Even;
|
||||
break;
|
||||
default: // ParityNone
|
||||
|
@ -233,7 +230,7 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) {
|
|||
USART_ITConfig(usart, USART_IT_RXNE, ENABLE);
|
||||
} else { // TxIrq
|
||||
USART_ITConfig(usart, USART_IT_TC, ENABLE);
|
||||
}
|
||||
}
|
||||
|
||||
NVIC_SetVector(irq_n, vector);
|
||||
NVIC_EnableIRQ(irq_n);
|
||||
|
@ -249,12 +246,12 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) {
|
|||
} else { // TxIrq
|
||||
USART_ITConfig(usart, USART_IT_TXE, DISABLE);
|
||||
// Check if RxIrq is disabled too
|
||||
if ((usart->CR1 & USART_CR1_RXNEIE) == 0) all_disabled = 1;
|
||||
if ((usart->CR1 & USART_CR1_RXNEIE) == 0) all_disabled = 1;
|
||||
}
|
||||
|
||||
if (all_disabled) NVIC_DisableIRQ(irq_n);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/******************************************************************************
|
||||
|
|
|
@ -27,6 +27,7 @@
|
|||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*******************************************************************************
|
||||
*/
|
||||
#include <assert.h>
|
||||
#include "spi_api.h"
|
||||
|
||||
#if DEVICE_SPI
|
||||
|
@ -34,7 +35,6 @@
|
|||
#include <math.h>
|
||||
#include "cmsis.h"
|
||||
#include "pinmap.h"
|
||||
#include "error.h"
|
||||
|
||||
static const PinMap PinMap_SPI_MOSI[] = {
|
||||
{PA_11, SPI_2, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_DOWN, GPIO_AF_5)},
|
||||
|
@ -75,17 +75,17 @@ static void init_spi(spi_t *obj) {
|
|||
SPI_Cmd(spi, DISABLE);
|
||||
|
||||
SPI_InitStructure.SPI_Mode = obj->mode;
|
||||
SPI_InitStructure.SPI_NSS = obj->nss;
|
||||
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
|
||||
SPI_InitStructure.SPI_NSS = obj->nss;
|
||||
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
|
||||
SPI_InitStructure.SPI_DataSize = obj->bits;
|
||||
SPI_InitStructure.SPI_CPOL = obj->cpol;
|
||||
SPI_InitStructure.SPI_CPHA = obj->cpha;
|
||||
SPI_InitStructure.SPI_CPHA = obj->cpha;
|
||||
SPI_InitStructure.SPI_BaudRatePrescaler = obj->br_presc;
|
||||
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
|
||||
SPI_InitStructure.SPI_CRCPolynomial = 7;
|
||||
SPI_Init(spi, &SPI_InitStructure);
|
||||
|
||||
SPI_RxFIFOThresholdConfig(spi, SPI_RxFIFOThreshold_QF);
|
||||
SPI_RxFIFOThresholdConfig(spi, SPI_RxFIFOThreshold_QF);
|
||||
|
||||
SPI_Cmd(spi, ENABLE);
|
||||
}
|
||||
|
@ -101,17 +101,14 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel
|
|||
SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel);
|
||||
|
||||
obj->spi = (SPIName)pinmap_merge(spi_data, spi_cntl);
|
||||
|
||||
if (obj->spi == (SPIName)NC) {
|
||||
error("SPI pinout mapping failed");
|
||||
}
|
||||
assert(obj->spi != (SPIName)NC);
|
||||
|
||||
// Enable SPI clock
|
||||
if (obj->spi == SPI_2) {
|
||||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
|
||||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
|
||||
}
|
||||
if (obj->spi == SPI_3) {
|
||||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI3, ENABLE);
|
||||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI3, ENABLE);
|
||||
}
|
||||
|
||||
// Configure the SPI pins
|
||||
|
@ -142,7 +139,7 @@ void spi_free(spi_t *obj) {
|
|||
SPI_I2S_DeInit(spi);
|
||||
}
|
||||
|
||||
void spi_format(spi_t *obj, int bits, int mode, int slave) {
|
||||
void spi_format(spi_t *obj, int bits, int mode, int slave) {
|
||||
// Save new values
|
||||
if (bits == 8) {
|
||||
obj->bits = SPI_DataSize_8b;
|
||||
|
@ -161,11 +158,11 @@ void spi_format(spi_t *obj, int bits, int mode, int slave) {
|
|||
break;
|
||||
case 2:
|
||||
obj->cpol = SPI_CPOL_High;
|
||||
obj->cpha = SPI_CPHA_1Edge;
|
||||
obj->cpha = SPI_CPHA_1Edge;
|
||||
break;
|
||||
default:
|
||||
obj->cpol = SPI_CPOL_High;
|
||||
obj->cpha = SPI_CPHA_2Edge;
|
||||
obj->cpha = SPI_CPHA_2Edge;
|
||||
break;
|
||||
}
|
||||
|
||||
|
@ -174,7 +171,7 @@ void spi_format(spi_t *obj, int bits, int mode, int slave) {
|
|||
obj->nss = SPI_NSS_Soft;
|
||||
} else {
|
||||
obj->mode = SPI_Mode_Slave;
|
||||
obj->nss = SPI_NSS_Hard;
|
||||
obj->nss = SPI_NSS_Hard;
|
||||
}
|
||||
|
||||
init_spi(obj);
|
||||
|
@ -207,7 +204,7 @@ static inline int ssp_readable(spi_t *obj) {
|
|||
SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
|
||||
// Check if data is received
|
||||
status = ((SPI_I2S_GetFlagStatus(spi, SPI_I2S_FLAG_RXNE) != RESET) ? 1 : 0);
|
||||
return status;
|
||||
return status;
|
||||
}
|
||||
|
||||
static inline int ssp_writeable(spi_t *obj) {
|
||||
|
@ -219,7 +216,7 @@ static inline int ssp_writeable(spi_t *obj) {
|
|||
}
|
||||
|
||||
static inline void ssp_write(spi_t *obj, int value) {
|
||||
SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
|
||||
SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
|
||||
while (!ssp_writeable(obj));
|
||||
if (obj->bits == SPI_DataSize_8b) {
|
||||
SPI_SendData8(spi, (uint8_t)value);
|
||||
|
@ -229,7 +226,7 @@ static inline void ssp_write(spi_t *obj, int value) {
|
|||
}
|
||||
|
||||
static inline int ssp_read(spi_t *obj) {
|
||||
SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
|
||||
SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
|
||||
while (!ssp_readable(obj));
|
||||
if (obj->bits == SPI_DataSize_8b) {
|
||||
return (int)SPI_ReceiveData8(spi);
|
||||
|
@ -264,8 +261,8 @@ int spi_slave_read(spi_t *obj) {
|
|||
}
|
||||
|
||||
void spi_slave_write(spi_t *obj, int value) {
|
||||
SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
|
||||
while (!ssp_writeable(obj));
|
||||
SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
|
||||
while (!ssp_writeable(obj));
|
||||
if (obj->bits == SPI_DataSize_8b) {
|
||||
SPI_SendData8(spi, (uint8_t)value);
|
||||
} else {
|
||||
|
|
|
@ -32,7 +32,6 @@
|
|||
|
||||
#include "cmsis.h"
|
||||
#include "pinmap.h"
|
||||
#include "error.h"
|
||||
#include "stm32f4xx_hal.h"
|
||||
|
||||
static const PinMap PinMap_ADC[] = {
|
||||
|
@ -59,13 +58,10 @@ ADC_HandleTypeDef AdcHandle;
|
|||
|
||||
int adc_inited = 0;
|
||||
|
||||
void analogin_init(analogin_t *obj, PinName pin) {
|
||||
void analogin_init(analogin_t *obj, PinName pin) {
|
||||
// Get the peripheral name (ADC_1, ADC_2...) from the pin and assign it to the object
|
||||
obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC);
|
||||
|
||||
if (obj->adc == (ADCName)NC) {
|
||||
error("ADC error: pinout mapping failed.");
|
||||
}
|
||||
assert(obj->adc != (ADCName)NC);
|
||||
|
||||
// Configure GPIO
|
||||
pinmap_pinout(pin, PinMap_ADC);
|
||||
|
@ -93,8 +89,8 @@ void analogin_init(analogin_t *obj, PinName pin) {
|
|||
AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
|
||||
AdcHandle.Init.NbrOfConversion = 1;
|
||||
AdcHandle.Init.DMAContinuousRequests = DISABLE;
|
||||
AdcHandle.Init.EOCSelection = DISABLE;
|
||||
HAL_ADC_Init(&AdcHandle);
|
||||
AdcHandle.Init.EOCSelection = DISABLE;
|
||||
HAL_ADC_Init(&AdcHandle);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -120,7 +116,7 @@ static inline uint16_t adc_read(analogin_t *obj) {
|
|||
break;
|
||||
case PA_3:
|
||||
sConfig.Channel = ADC_CHANNEL_3;
|
||||
break;
|
||||
break;
|
||||
case PA_4:
|
||||
sConfig.Channel = ADC_CHANNEL_4;
|
||||
break;
|
||||
|
@ -132,13 +128,13 @@ static inline uint16_t adc_read(analogin_t *obj) {
|
|||
break;
|
||||
case PA_7:
|
||||
sConfig.Channel = ADC_CHANNEL_7;
|
||||
break;
|
||||
break;
|
||||
case PB_0:
|
||||
sConfig.Channel = ADC_CHANNEL_8;
|
||||
break;
|
||||
case PB_1:
|
||||
sConfig.Channel = ADC_CHANNEL_9;
|
||||
break;
|
||||
break;
|
||||
case PC_0:
|
||||
sConfig.Channel = ADC_CHANNEL_10;
|
||||
break;
|
||||
|
@ -156,7 +152,7 @@ static inline uint16_t adc_read(analogin_t *obj) {
|
|||
break;
|
||||
case PC_5:
|
||||
sConfig.Channel = ADC_CHANNEL_15;
|
||||
break;
|
||||
break;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
|
@ -168,7 +164,7 @@ static inline uint16_t adc_read(analogin_t *obj) {
|
|||
HAL_ADC_PollForConversion(&AdcHandle, 10); // Wait end of conversion
|
||||
|
||||
if (HAL_ADC_GetState(&AdcHandle) == HAL_ADC_STATE_EOC_REG)
|
||||
{
|
||||
{
|
||||
return(HAL_ADC_GetValue(&AdcHandle)); // Get conversion value
|
||||
}
|
||||
else
|
||||
|
|
|
@ -27,18 +27,18 @@
|
|||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*******************************************************************************
|
||||
*/
|
||||
#include <assert.h>
|
||||
#include "i2c_api.h"
|
||||
|
||||
#if DEVICE_I2C
|
||||
|
||||
#include "cmsis.h"
|
||||
#include "pinmap.h"
|
||||
#include "error.h"
|
||||
#include "stm32f4xx_hal.h"
|
||||
|
||||
/* Timeout values for flags and events waiting loops. These timeouts are
|
||||
not based on accurate values, they just guarantee that the application will
|
||||
not remain stuck if the I2C communication is corrupted. */
|
||||
not based on accurate values, they just guarantee that the application will
|
||||
not remain stuck if the I2C communication is corrupted. */
|
||||
#define FLAG_TIMEOUT ((int)0x1000)
|
||||
#define LONG_TIMEOUT ((int)0x8000)
|
||||
|
||||
|
@ -65,19 +65,16 @@ static const PinMap PinMap_I2C_SCL[] = {
|
|||
|
||||
I2C_HandleTypeDef I2cHandle;
|
||||
|
||||
void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
|
||||
void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
|
||||
// Determine the I2C to use
|
||||
I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA);
|
||||
I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL);
|
||||
|
||||
obj->i2c = (I2CName)pinmap_merge(i2c_sda, i2c_scl);
|
||||
|
||||
if (obj->i2c == (I2CName)NC) {
|
||||
error("I2C error: pinout mapping failed.");
|
||||
}
|
||||
assert(obj->i2c != (I2CName)NC);
|
||||
|
||||
// Enable I2C clock
|
||||
if (obj->i2c == I2C_1) {
|
||||
if (obj->i2c == I2C_1) {
|
||||
__I2C1_CLK_ENABLE();
|
||||
}
|
||||
if (obj->i2c == I2C_2) {
|
||||
|
@ -97,14 +94,15 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
|
|||
i2c_reset(obj);
|
||||
|
||||
// I2C configuration
|
||||
i2c_frequency(obj, 100000); // 100 kHz per default
|
||||
i2c_frequency(obj, 100000); // 100 kHz per default
|
||||
}
|
||||
|
||||
void i2c_frequency(i2c_t *obj, int hz) {
|
||||
assert((hz != 0) && (hz <= 400000));
|
||||
I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
|
||||
|
||||
if ((hz != 0) && (hz <= 400000)) {
|
||||
// I2C configuration
|
||||
// I2C configuration
|
||||
I2cHandle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
|
||||
I2cHandle.Init.ClockSpeed = hz;
|
||||
I2cHandle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED;
|
||||
|
@ -113,10 +111,7 @@ void i2c_frequency(i2c_t *obj, int hz) {
|
|||
I2cHandle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED;
|
||||
I2cHandle.Init.OwnAddress1 = 0;
|
||||
I2cHandle.Init.OwnAddress2 = 0;
|
||||
HAL_I2C_Init(&I2cHandle);
|
||||
}
|
||||
else {
|
||||
error("I2C error: frequency setting failed (max 400kHz).");
|
||||
HAL_I2C_Init(&I2cHandle);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -152,7 +147,7 @@ inline int i2c_stop(i2c_t *obj) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
|
||||
int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
|
||||
if (length == 0) return 0;
|
||||
|
||||
I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
|
||||
|
@ -208,7 +203,7 @@ int i2c_byte_write(i2c_t *obj, int data) {
|
|||
i2c->DR = (uint8_t)data;
|
||||
|
||||
// Wait until the byte is transmitted
|
||||
timeout = FLAG_TIMEOUT;
|
||||
timeout = FLAG_TIMEOUT;
|
||||
while ((__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TXE) == RESET) &&
|
||||
(__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BTF) == RESET)) {
|
||||
if ((timeout--) == 0) {
|
||||
|
@ -220,7 +215,7 @@ int i2c_byte_write(i2c_t *obj, int data) {
|
|||
}
|
||||
|
||||
void i2c_reset(i2c_t *obj) {
|
||||
if (obj->i2c == I2C_1) {
|
||||
if (obj->i2c == I2C_1) {
|
||||
__I2C1_FORCE_RESET();
|
||||
__I2C1_RELEASE_RESET();
|
||||
}
|
||||
|
|
|
@ -27,6 +27,7 @@
|
|||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*******************************************************************************
|
||||
*/
|
||||
#include <assert.h>
|
||||
#include "pwmout_api.h"
|
||||
|
||||
#include "cmsis.h"
|
||||
|
@ -87,11 +88,8 @@ static TIM_HandleTypeDef TimHandle;
|
|||
void pwmout_init(pwmout_t* obj, PinName pin) {
|
||||
// Get the peripheral name from the pin and assign it to the object
|
||||
obj->pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM);
|
||||
|
||||
if (obj->pwm == (PWMName)NC) {
|
||||
error("PWM error: pinout mapping failed.");
|
||||
}
|
||||
|
||||
assert(obj->pwm != (PWMName)NC);
|
||||
|
||||
// Enable TIM clock
|
||||
if (obj->pwm == PWM_1) __TIM1_CLK_ENABLE();
|
||||
if (obj->pwm == PWM_2) __TIM2_CLK_ENABLE();
|
||||
|
|
|
@ -30,7 +30,6 @@
|
|||
#include "serial_api.h"
|
||||
#include "cmsis.h"
|
||||
#include "pinmap.h"
|
||||
#include "error.h"
|
||||
#include <string.h>
|
||||
#include "stm32f4xx_hal.h"
|
||||
|
||||
|
@ -73,20 +72,17 @@ static void init_uart(serial_t *obj) {
|
|||
UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
|
||||
UartHandle.Init.Mode = UART_MODE_TX_RX;
|
||||
|
||||
HAL_UART_Init(&UartHandle);
|
||||
HAL_UART_Init(&UartHandle);
|
||||
}
|
||||
|
||||
void serial_init(serial_t *obj, PinName tx, PinName rx) {
|
||||
void serial_init(serial_t *obj, PinName tx, PinName rx) {
|
||||
// Determine the UART to use (UART_1, UART_2, ...)
|
||||
UARTName uart_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX);
|
||||
UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX);
|
||||
|
||||
// Get the peripheral name (UART_1, UART_2, ...) from the pin and assign it to the object
|
||||
obj->uart = (UARTName)pinmap_merge(uart_tx, uart_rx);
|
||||
|
||||
if (obj->uart == (UARTName)NC) {
|
||||
error("Serial error: pinout mapping failed.");
|
||||
}
|
||||
assert(obj->uart != (UARTName)NC);
|
||||
|
||||
// Enable USART clock
|
||||
if (obj->uart == UART_1) {
|
||||
|
@ -109,7 +105,7 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) {
|
|||
obj->baudrate = 9600;
|
||||
obj->databits = UART_WORDLENGTH_8B;
|
||||
obj->stopbits = UART_STOPBITS_1;
|
||||
obj->parity = UART_PARITY_NONE;
|
||||
obj->parity = UART_PARITY_NONE;
|
||||
|
||||
init_uart(obj);
|
||||
|
||||
|
@ -149,7 +145,7 @@ void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_b
|
|||
obj->parity = UART_PARITY_ODD;
|
||||
break;
|
||||
case ParityEven:
|
||||
case ParityForced1:
|
||||
case ParityForced1:
|
||||
obj->parity = UART_PARITY_EVEN;
|
||||
break;
|
||||
default: // ParityNone
|
||||
|
@ -225,7 +221,7 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) {
|
|||
}
|
||||
else { // TxIrq
|
||||
__HAL_UART_ENABLE_IT(&UartHandle, UART_IT_TC);
|
||||
}
|
||||
}
|
||||
|
||||
NVIC_SetVector(irq_n, vector);
|
||||
NVIC_EnableIRQ(irq_n);
|
||||
|
@ -242,12 +238,12 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) {
|
|||
else { // TxIrq
|
||||
__HAL_UART_DISABLE_IT(&UartHandle, UART_IT_TXE);
|
||||
// Check if RxIrq is disabled too
|
||||
if ((UartHandle.Instance->CR1 & USART_CR1_RXNEIE) == 0) all_disabled = 1;
|
||||
if ((UartHandle.Instance->CR1 & USART_CR1_RXNEIE) == 0) all_disabled = 1;
|
||||
}
|
||||
|
||||
if (all_disabled) NVIC_DisableIRQ(irq_n);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/******************************************************************************
|
||||
|
|
|
@ -34,7 +34,6 @@
|
|||
#include <math.h>
|
||||
#include "cmsis.h"
|
||||
#include "pinmap.h"
|
||||
#include "error.h"
|
||||
#include "stm32f4xx_hal.h"
|
||||
|
||||
static const PinMap PinMap_SPI_MOSI[] = {
|
||||
|
@ -112,10 +111,7 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel
|
|||
SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel);
|
||||
|
||||
obj->spi = (SPIName)pinmap_merge(spi_data, spi_cntl);
|
||||
|
||||
if (obj->spi == (SPIName)NC) {
|
||||
error("SPI error: pinout mapping failed.");
|
||||
}
|
||||
assert(obj->spi != (SPIName)NC);
|
||||
|
||||
// Enable SPI clock
|
||||
if (obj->spi == SPI_1) {
|
||||
|
@ -157,7 +153,7 @@ void spi_free(spi_t *obj) {
|
|||
HAL_SPI_DeInit(&SpiHandle);
|
||||
}
|
||||
|
||||
void spi_format(spi_t *obj, int bits, int mode, int slave) {
|
||||
void spi_format(spi_t *obj, int bits, int mode, int slave) {
|
||||
// Save new values
|
||||
if (bits == 8) {
|
||||
obj->bits = SPI_DATASIZE_8BIT;
|
||||
|
@ -177,11 +173,11 @@ void spi_format(spi_t *obj, int bits, int mode, int slave) {
|
|||
break;
|
||||
case 2:
|
||||
obj->cpol = SPI_POLARITY_HIGH;
|
||||
obj->cpha = SPI_PHASE_1EDGE;
|
||||
obj->cpha = SPI_PHASE_1EDGE;
|
||||
break;
|
||||
default:
|
||||
obj->cpol = SPI_POLARITY_HIGH;
|
||||
obj->cpha = SPI_PHASE_2EDGE;
|
||||
obj->cpha = SPI_PHASE_2EDGE;
|
||||
break;
|
||||
}
|
||||
|
||||
|
@ -191,7 +187,7 @@ void spi_format(spi_t *obj, int bits, int mode, int slave) {
|
|||
}
|
||||
else {
|
||||
obj->mode = SPI_MODE_SLAVE;
|
||||
obj->nss = SPI_NSS_HARD_INPUT;
|
||||
obj->nss = SPI_NSS_HARD_INPUT;
|
||||
}
|
||||
|
||||
init_spi(obj);
|
||||
|
@ -231,7 +227,7 @@ static inline int ssp_readable(spi_t *obj) {
|
|||
SpiHandle.Instance = (SPI_TypeDef *)(obj->spi);
|
||||
// Check if data is received
|
||||
status = ((__HAL_SPI_GET_FLAG(&SpiHandle, SPI_FLAG_RXNE) != RESET) ? 1 : 0);
|
||||
return status;
|
||||
return status;
|
||||
}
|
||||
|
||||
static inline int ssp_writeable(spi_t *obj) {
|
||||
|
|
|
@ -25,6 +25,7 @@
|
|||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#include <assert.h>
|
||||
#include "analogin_api.h"
|
||||
|
||||
#if DEVICE_ANALOGIN
|
||||
|
@ -63,10 +64,7 @@ void analogin_init(analogin_t *obj, PinName pin) {
|
|||
|
||||
// Get the peripheral name (ADC_1, ADC_2...) from the pin and assign it to the object
|
||||
obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC);
|
||||
|
||||
if (obj->adc == (ADCName)NC) {
|
||||
error("ADC pin mapping failed");
|
||||
}
|
||||
assert(obj->adc != (ADCName)NC);
|
||||
|
||||
// Configure GPIO
|
||||
pinmap_pinout(pin, PinMap_ADC);
|
||||
|
@ -86,7 +84,7 @@ void analogin_init(analogin_t *obj, PinName pin) {
|
|||
|
||||
// Configure ADC
|
||||
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
|
||||
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
|
||||
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
|
||||
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
|
||||
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_TRGO;
|
||||
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
|
||||
|
|
|
@ -64,7 +64,7 @@ void gpio_mode(gpio_t *obj, PinMode mode) {
|
|||
}
|
||||
|
||||
void gpio_dir(gpio_t *obj, PinDirection direction) {
|
||||
assert(pin != (PinName)NC);
|
||||
assert(obj->pin != (PinName)NC);
|
||||
if (direction == PIN_OUTPUT) {
|
||||
pin_function(obj->pin, STM_PIN_DATA(GPIO_Mode_OUT, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0xFF));
|
||||
}
|
||||
|
|
|
@ -27,17 +27,17 @@
|
|||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*******************************************************************************
|
||||
*/
|
||||
#include <assert.h>
|
||||
#include "i2c_api.h"
|
||||
|
||||
#if DEVICE_I2C
|
||||
|
||||
#include "cmsis.h"
|
||||
#include "pinmap.h"
|
||||
#include "error.h"
|
||||
|
||||
/* Timeout values for flags and events waiting loops. These timeouts are
|
||||
not based on accurate values, they just guarantee that the application will
|
||||
not remain stuck if the I2C communication is corrupted. */
|
||||
not based on accurate values, they just guarantee that the application will
|
||||
not remain stuck if the I2C communication is corrupted. */
|
||||
#define FLAG_TIMEOUT ((int)0x1000)
|
||||
#define LONG_TIMEOUT ((int)0x8000)
|
||||
|
||||
|
@ -55,19 +55,16 @@ static const PinMap PinMap_I2C_SCL[] = {
|
|||
{NC, NC, 0}
|
||||
};
|
||||
|
||||
void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
|
||||
void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
|
||||
// Determine the I2C to use
|
||||
I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA);
|
||||
I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL);
|
||||
|
||||
obj->i2c = (I2CName)pinmap_merge(i2c_sda, i2c_scl);
|
||||
|
||||
if (obj->i2c == (I2CName)NC) {
|
||||
error("I2C pin mapping failed");
|
||||
}
|
||||
assert(obj->i2c != (I2CName)NC);
|
||||
|
||||
// Enable I2C clock
|
||||
if (obj->i2c == I2C_1) {
|
||||
if (obj->i2c == I2C_1) {
|
||||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
|
||||
RCC_I2CCLKConfig(RCC_I2C1CLK_SYSCLK);
|
||||
}
|
||||
|
@ -85,10 +82,11 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
|
|||
i2c_reset(obj);
|
||||
|
||||
// I2C configuration
|
||||
i2c_frequency(obj, 100000); // 100 kHz per default
|
||||
i2c_frequency(obj, 100000); // 100 kHz per default
|
||||
}
|
||||
|
||||
void i2c_frequency(i2c_t *obj, int hz) {
|
||||
assert((hz == 100000) || (hz == 200000) || (hz == 400000) || (hz == 1000000));
|
||||
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
|
||||
I2C_InitTypeDef I2C_InitStructure;
|
||||
uint32_t tim = 0;
|
||||
|
@ -131,7 +129,6 @@ void i2c_frequency(i2c_t *obj, int hz) {
|
|||
}
|
||||
break;
|
||||
default:
|
||||
error("Only 100kHz, 200kHz, 400kHz and 1MHz I2C frequencies are supported.");
|
||||
break;
|
||||
}
|
||||
|
||||
|
@ -227,7 +224,7 @@ int i2c_byte_read(i2c_t *obj, int last) {
|
|||
int timeout;
|
||||
|
||||
// Wait until the byte is received
|
||||
timeout = FLAG_TIMEOUT;
|
||||
timeout = FLAG_TIMEOUT;
|
||||
while (I2C_GetFlagStatus(i2c, I2C_ISR_RXNE) == RESET) {
|
||||
timeout--;
|
||||
if (timeout == 0) {
|
||||
|
@ -259,13 +256,13 @@ int i2c_byte_write(i2c_t *obj, int data) {
|
|||
}
|
||||
|
||||
void i2c_reset(i2c_t *obj) {
|
||||
if (obj->i2c == I2C_1) {
|
||||
if (obj->i2c == I2C_1) {
|
||||
RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, ENABLE);
|
||||
RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, DISABLE);
|
||||
}
|
||||
if (obj->i2c == I2C_2) {
|
||||
RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, ENABLE);
|
||||
RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, DISABLE);
|
||||
RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, DISABLE);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -27,13 +27,13 @@
|
|||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*******************************************************************************
|
||||
*/
|
||||
#include <assert.h>
|
||||
#include "pwmout_api.h"
|
||||
|
||||
#if DEVICE_PWMOUT
|
||||
|
||||
#include "cmsis.h"
|
||||
#include "pinmap.h"
|
||||
#include "error.h"
|
||||
|
||||
// TIM1 cannot be used because already used by the us_ticker
|
||||
static const PinMap PinMap_PWM[] = {
|
||||
|
@ -65,10 +65,7 @@ static const PinMap PinMap_PWM[] = {
|
|||
void pwmout_init(pwmout_t* obj, PinName pin) {
|
||||
// Get the peripheral name from the pin and assign it to the object
|
||||
obj->pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM);
|
||||
|
||||
if (obj->pwm == (PWMName)NC) {
|
||||
error("PWM pinout mapping failed");
|
||||
}
|
||||
assert(obj->pwm != (PWMName)NC);
|
||||
|
||||
// Enable TIM clock
|
||||
if (obj->pwm == TIM_3) RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
|
||||
|
|
|
@ -27,13 +27,13 @@
|
|||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*******************************************************************************
|
||||
*/
|
||||
#include <assert.h>
|
||||
#include "serial_api.h"
|
||||
|
||||
#if DEVICE_SERIAL
|
||||
|
||||
#include "cmsis.h"
|
||||
#include "pinmap.h"
|
||||
#include "error.h"
|
||||
#include <string.h>
|
||||
|
||||
static const PinMap PinMap_UART_TX[] = {
|
||||
|
@ -44,7 +44,7 @@ static const PinMap PinMap_UART_TX[] = {
|
|||
};
|
||||
|
||||
static const PinMap PinMap_UART_RX[] = {
|
||||
{PA_3, UART_2, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_1)},
|
||||
{PA_3, UART_2, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_1)},
|
||||
{PA_10, UART_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_1)},
|
||||
{PA_15, UART_2, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_1)},
|
||||
{PB_7, UART_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_0)},
|
||||
|
@ -77,24 +77,21 @@ static void init_usart(serial_t *obj) {
|
|||
USART_Cmd(usart, ENABLE);
|
||||
}
|
||||
|
||||
void serial_init(serial_t *obj, PinName tx, PinName rx) {
|
||||
void serial_init(serial_t *obj, PinName tx, PinName rx) {
|
||||
// Determine the UART to use (UART_1, UART_2, ...)
|
||||
UARTName uart_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX);
|
||||
UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX);
|
||||
|
||||
// Get the peripheral name (UART_1, UART_2, ...) from the pin and assign it to the object
|
||||
obj->uart = (UARTName)pinmap_merge(uart_tx, uart_rx);
|
||||
|
||||
if (obj->uart == (UARTName)NC) {
|
||||
error("Serial pinout mapping failed");
|
||||
}
|
||||
assert(obj->uart != (UARTName)NC);
|
||||
|
||||
// Enable USART clock
|
||||
if (obj->uart == UART_1) {
|
||||
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
|
||||
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
|
||||
}
|
||||
if (obj->uart == UART_2) {
|
||||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
|
||||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
|
||||
}
|
||||
|
||||
// Configure the UART pins
|
||||
|
@ -107,7 +104,7 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) {
|
|||
obj->baudrate = 9600;
|
||||
obj->databits = USART_WordLength_8b;
|
||||
obj->stopbits = USART_StopBits_1;
|
||||
obj->parity = USART_Parity_No;
|
||||
obj->parity = USART_Parity_No;
|
||||
|
||||
init_usart(obj);
|
||||
|
||||
|
@ -146,7 +143,7 @@ void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_b
|
|||
obj->parity = USART_Parity_Odd;
|
||||
break;
|
||||
case ParityEven:
|
||||
case ParityForced1:
|
||||
case ParityForced1:
|
||||
obj->parity = USART_Parity_Even;
|
||||
break;
|
||||
default: // ParityNone
|
||||
|
@ -212,7 +209,7 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) {
|
|||
}
|
||||
else { // TxIrq
|
||||
USART_ITConfig(usart, USART_IT_TC, ENABLE);
|
||||
}
|
||||
}
|
||||
|
||||
NVIC_SetVector(irq_n, vector);
|
||||
NVIC_EnableIRQ(irq_n);
|
||||
|
@ -229,12 +226,12 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) {
|
|||
else { // TxIrq
|
||||
USART_ITConfig(usart, USART_IT_TXE, DISABLE);
|
||||
// Check if RxIrq is disabled too
|
||||
if ((usart->CR1 & USART_CR1_RXNEIE) == 0) all_disabled = 1;
|
||||
if ((usart->CR1 & USART_CR1_RXNEIE) == 0) all_disabled = 1;
|
||||
}
|
||||
|
||||
if (all_disabled) NVIC_DisableIRQ(irq_n);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/******************************************************************************
|
||||
|
|
|
@ -27,6 +27,7 @@
|
|||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*******************************************************************************
|
||||
*/
|
||||
#include <assert.h>
|
||||
#include "spi_api.h"
|
||||
|
||||
#if DEVICE_SPI
|
||||
|
@ -34,7 +35,6 @@
|
|||
#include <math.h>
|
||||
#include "cmsis.h"
|
||||
#include "pinmap.h"
|
||||
#include "error.h"
|
||||
|
||||
static const PinMap PinMap_SPI_MOSI[] = {
|
||||
{PA_7, SPI_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_NOPULL, GPIO_AF_0)},
|
||||
|
@ -72,11 +72,11 @@ static void init_spi(spi_t *obj) {
|
|||
SPI_Cmd(spi, DISABLE);
|
||||
|
||||
SPI_InitStructure.SPI_Mode = obj->mode;
|
||||
SPI_InitStructure.SPI_NSS = obj->nss;
|
||||
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
|
||||
SPI_InitStructure.SPI_NSS = obj->nss;
|
||||
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
|
||||
SPI_InitStructure.SPI_DataSize = obj->bits;
|
||||
SPI_InitStructure.SPI_CPOL = obj->cpol;
|
||||
SPI_InitStructure.SPI_CPHA = obj->cpha;
|
||||
SPI_InitStructure.SPI_CPHA = obj->cpha;
|
||||
SPI_InitStructure.SPI_BaudRatePrescaler = obj->br_presc;
|
||||
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
|
||||
SPI_InitStructure.SPI_CRCPolynomial = 7;
|
||||
|
@ -98,17 +98,14 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel
|
|||
SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel);
|
||||
|
||||
obj->spi = (SPIName)pinmap_merge(spi_data, spi_cntl);
|
||||
|
||||
if (obj->spi == (SPIName)NC) {
|
||||
error("SPI pinout mapping failed");
|
||||
}
|
||||
|
||||
assert(obj->spi != (SPIName)NC);
|
||||
|
||||
// Enable SPI clock
|
||||
if (obj->spi == SPI_1) {
|
||||
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
|
||||
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
|
||||
}
|
||||
if (obj->spi == SPI_2) {
|
||||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
|
||||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
|
||||
}
|
||||
|
||||
// Configure the SPI pins
|
||||
|
@ -140,7 +137,7 @@ void spi_free(spi_t *obj) {
|
|||
SPI_I2S_DeInit(spi);
|
||||
}
|
||||
|
||||
void spi_format(spi_t *obj, int bits, int mode, int slave) {
|
||||
void spi_format(spi_t *obj, int bits, int mode, int slave) {
|
||||
// Save new values
|
||||
if (bits == 8) {
|
||||
obj->bits = SPI_DataSize_8b;
|
||||
|
@ -160,11 +157,11 @@ void spi_format(spi_t *obj, int bits, int mode, int slave) {
|
|||
break;
|
||||
case 2:
|
||||
obj->cpol = SPI_CPOL_High;
|
||||
obj->cpha = SPI_CPHA_1Edge;
|
||||
obj->cpha = SPI_CPHA_1Edge;
|
||||
break;
|
||||
default:
|
||||
obj->cpol = SPI_CPOL_High;
|
||||
obj->cpha = SPI_CPHA_2Edge;
|
||||
obj->cpha = SPI_CPHA_2Edge;
|
||||
break;
|
||||
}
|
||||
|
||||
|
@ -174,7 +171,7 @@ void spi_format(spi_t *obj, int bits, int mode, int slave) {
|
|||
}
|
||||
else {
|
||||
obj->mode = SPI_Mode_Slave;
|
||||
obj->nss = SPI_NSS_Hard;
|
||||
obj->nss = SPI_NSS_Hard;
|
||||
}
|
||||
|
||||
init_spi(obj);
|
||||
|
@ -214,7 +211,7 @@ static inline int ssp_readable(spi_t *obj) {
|
|||
SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
|
||||
// Check if data is received
|
||||
status = ((SPI_I2S_GetFlagStatus(spi, SPI_I2S_FLAG_RXNE) != RESET) ? 1 : 0);
|
||||
return status;
|
||||
return status;
|
||||
}
|
||||
|
||||
static inline int ssp_writeable(spi_t *obj) {
|
||||
|
@ -226,7 +223,7 @@ static inline int ssp_writeable(spi_t *obj) {
|
|||
}
|
||||
|
||||
static inline void ssp_write(spi_t *obj, int value) {
|
||||
SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
|
||||
SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
|
||||
while (!ssp_writeable(obj));
|
||||
if (obj->bits == SPI_DataSize_8b) {
|
||||
SPI_SendData8(spi, (uint8_t)value);
|
||||
|
@ -237,7 +234,7 @@ static inline void ssp_write(spi_t *obj, int value) {
|
|||
}
|
||||
|
||||
static inline int ssp_read(spi_t *obj) {
|
||||
SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
|
||||
SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
|
||||
while (!ssp_readable(obj));
|
||||
if (obj->bits == SPI_DataSize_8b) {
|
||||
return (int)SPI_ReceiveData8(spi);
|
||||
|
@ -274,8 +271,8 @@ int spi_slave_read(spi_t *obj) {
|
|||
}
|
||||
|
||||
void spi_slave_write(spi_t *obj, int value) {
|
||||
SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
|
||||
while (!ssp_writeable(obj));
|
||||
SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
|
||||
while (!ssp_writeable(obj));
|
||||
if (obj->bits == SPI_DataSize_8b) {
|
||||
SPI_SendData8(spi, (uint8_t)value);
|
||||
}
|
||||
|
|
|
@ -25,13 +25,13 @@
|
|||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#include <assert.h>
|
||||
#include "analogin_api.h"
|
||||
|
||||
#if DEVICE_ANALOGIN
|
||||
|
||||
#include "cmsis.h"
|
||||
#include "pinmap.h"
|
||||
#include "error.h"
|
||||
#include "wait_api.h"
|
||||
|
||||
static const PinMap PinMap_ADC[] = {
|
||||
|
@ -62,10 +62,7 @@ void analogin_init(analogin_t *obj, PinName pin) {
|
|||
|
||||
// Get the peripheral name from the pin and assign it to the object
|
||||
obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC);
|
||||
|
||||
if (obj->adc == (ADCName)NC) {
|
||||
error("ADC pin mapping failed");
|
||||
}
|
||||
assert(obj->adc != (ADCName)NC);
|
||||
|
||||
// Configure GPIO
|
||||
pinmap_pinout(pin, PinMap_ADC);
|
||||
|
|
|
@ -27,13 +27,13 @@
|
|||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*******************************************************************************
|
||||
*/
|
||||
#include <assert.h>
|
||||
#include "i2c_api.h"
|
||||
|
||||
#if DEVICE_I2C
|
||||
|
||||
#include "cmsis.h"
|
||||
#include "pinmap.h"
|
||||
#include "error.h"
|
||||
|
||||
/* Timeout values for flags and events waiting loops. These timeouts are
|
||||
not based on accurate values, they just guarantee that the application will
|
||||
|
@ -61,10 +61,7 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
|
|||
I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL);
|
||||
|
||||
obj->i2c = (I2CName)pinmap_merge(i2c_sda, i2c_scl);
|
||||
|
||||
if (obj->i2c == (I2CName)NC) {
|
||||
error("I2C pin mapping failed");
|
||||
}
|
||||
assert(obj->i2c != (I2CName)NC);
|
||||
|
||||
// Enable I2C clock
|
||||
if (obj->i2c == I2C_1) {
|
||||
|
@ -245,7 +242,7 @@ int i2c_byte_write(i2c_t *obj, int data) {
|
|||
I2C_SendData(i2c, (uint8_t)data);
|
||||
|
||||
// Wait until the byte is transmitted
|
||||
timeout = FLAG_TIMEOUT;
|
||||
timeout = FLAG_TIMEOUT;
|
||||
while ((I2C_GetFlagStatus(i2c, I2C_FLAG_TXE) == RESET) &&
|
||||
(I2C_GetFlagStatus(i2c, I2C_FLAG_BTF) == RESET)) {
|
||||
timeout--;
|
||||
|
@ -317,7 +314,7 @@ int i2c_slave_receive(i2c_t *obj) {
|
|||
break;
|
||||
}
|
||||
|
||||
// clear ADDR
|
||||
// clear ADDR
|
||||
if((retValue == WriteAddressed) || (retValue == ReadAddressed)){
|
||||
// read SR to clear ADDR flag
|
||||
i2c->SR1;
|
||||
|
@ -327,12 +324,12 @@ int i2c_slave_receive(i2c_t *obj) {
|
|||
if(I2C_GetFlagStatus(i2c, I2C_FLAG_STOPF) == SET) {
|
||||
// read SR1 and write CR1 to clear STOP flag
|
||||
i2c->SR1;
|
||||
I2C_Cmd(i2c, ENABLE);
|
||||
I2C_Cmd(i2c, ENABLE);
|
||||
}
|
||||
// clear AF
|
||||
if(I2C_GetFlagStatus(i2c, I2C_FLAG_AF) == SET) {
|
||||
I2C_ClearFlag(i2c, I2C_FLAG_AF);
|
||||
}
|
||||
}
|
||||
}
|
||||
return(retValue);
|
||||
}
|
||||
|
|
|
@ -27,13 +27,13 @@
|
|||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*******************************************************************************
|
||||
*/
|
||||
#include <assert.h>
|
||||
#include "pwmout_api.h"
|
||||
|
||||
#if DEVICE_PWMOUT
|
||||
|
||||
#include "cmsis.h"
|
||||
#include "pinmap.h"
|
||||
#include "error.h"
|
||||
|
||||
// TIM4 cannot be used because already used by the us_ticker
|
||||
static const PinMap PinMap_PWM[] = {
|
||||
|
@ -76,10 +76,7 @@ static const PinMap PinMap_PWM[] = {
|
|||
void pwmout_init(pwmout_t* obj, PinName pin) {
|
||||
// Get the peripheral name from the pin and assign it to the object
|
||||
obj->pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM);
|
||||
|
||||
if (obj->pwm == (PWMName)NC) {
|
||||
error("PWM pinout mapping failed");
|
||||
}
|
||||
assert(obj->pwm != (PWMName)NC);
|
||||
|
||||
// Enable TIM clock
|
||||
if (obj->pwm == PWM_1) RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
|
||||
|
|
|
@ -27,13 +27,13 @@
|
|||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*******************************************************************************
|
||||
*/
|
||||
#include <assert.h>
|
||||
#include "serial_api.h"
|
||||
|
||||
#if DEVICE_SERIAL
|
||||
|
||||
#include "cmsis.h"
|
||||
#include "pinmap.h"
|
||||
#include "error.h"
|
||||
#include <string.h>
|
||||
|
||||
static const PinMap PinMap_UART_TX[] = {
|
||||
|
@ -87,10 +87,7 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) {
|
|||
|
||||
// Get the peripheral name (UART_1, UART_2, ...) from the pin and assign it to the object
|
||||
obj->uart = (UARTName)pinmap_merge(uart_tx, uart_rx);
|
||||
|
||||
if (obj->uart == (UARTName)NC) {
|
||||
error("Serial pinout mapping failed");
|
||||
}
|
||||
assert(obj->uart != (UARTName)NC);
|
||||
|
||||
// Enable USART clock
|
||||
if (obj->uart == UART_1) {
|
||||
|
|
|
@ -27,6 +27,7 @@
|
|||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*******************************************************************************
|
||||
*/
|
||||
#include <assert.h>
|
||||
#include "spi_api.h"
|
||||
|
||||
#if DEVICE_SPI
|
||||
|
@ -34,7 +35,6 @@
|
|||
#include <math.h>
|
||||
#include "cmsis.h"
|
||||
#include "pinmap.h"
|
||||
#include "error.h"
|
||||
|
||||
static const PinMap PinMap_SPI_MOSI[] = {
|
||||
{PA_7, SPI_1, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)},
|
||||
|
@ -95,10 +95,7 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel
|
|||
SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel);
|
||||
|
||||
obj->spi = (SPIName)pinmap_merge(spi_data, spi_cntl);
|
||||
|
||||
if (obj->spi == (SPIName)NC) {
|
||||
error("SPI pinout mapping failed");
|
||||
}
|
||||
assert(obj->spi != (SPIName)NC);
|
||||
|
||||
// Enable SPI clock
|
||||
if (obj->spi == SPI_1) {
|
||||
|
|
|
@ -25,6 +25,7 @@
|
|||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#include <assert.h>
|
||||
#include "analogin_api.h"
|
||||
|
||||
#if DEVICE_ANALOGIN
|
||||
|
@ -32,7 +33,6 @@
|
|||
#include "wait_api.h"
|
||||
#include "cmsis.h"
|
||||
#include "pinmap.h"
|
||||
#include "error.h"
|
||||
|
||||
static const PinMap PinMap_ADC[] = {
|
||||
{PA_0, ADC_1, STM_PIN_DATA(GPIO_Mode_AN, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0xFF)}, // ADC_IN1
|
||||
|
@ -63,10 +63,7 @@ void analogin_init(analogin_t *obj, PinName pin) {
|
|||
|
||||
// Get the peripheral name from the pin and assign it to the object
|
||||
obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC);
|
||||
|
||||
if (obj->adc == (ADCName)NC) {
|
||||
error("ADC pin mapping failed");
|
||||
}
|
||||
assert(obj->adc != (ADCName)NC);
|
||||
|
||||
// Configure GPIO
|
||||
pinmap_pinout(pin, PinMap_ADC);
|
||||
|
|
|
@ -25,13 +25,13 @@
|
|||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#include <assert.h>
|
||||
#include "analogout_api.h"
|
||||
|
||||
#if DEVICE_ANALOGOUT
|
||||
|
||||
#include "cmsis.h"
|
||||
#include "pinmap.h"
|
||||
#include "error.h"
|
||||
|
||||
#define DAC_RANGE (0xFFF) // 12 bits
|
||||
|
||||
|
@ -46,10 +46,7 @@ void analogout_init(dac_t *obj, PinName pin) {
|
|||
|
||||
// Get the peripheral name (DAC_1, ...) from the pin and assign it to the object
|
||||
obj->dac = (DACName)pinmap_peripheral(pin, PinMap_DAC);
|
||||
|
||||
if (obj->dac == (DACName)NC) {
|
||||
error("DAC pin mapping failed");
|
||||
}
|
||||
assert(obj->dac != (DACName)NC);
|
||||
|
||||
dac = (DAC_TypeDef *)(obj->dac);
|
||||
|
||||
|
|
|
@ -27,13 +27,13 @@
|
|||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*******************************************************************************
|
||||
*/
|
||||
#include <assert.h>
|
||||
#include "i2c_api.h"
|
||||
|
||||
#if DEVICE_I2C
|
||||
|
||||
#include "cmsis.h"
|
||||
#include "pinmap.h"
|
||||
#include "error.h"
|
||||
|
||||
/* Timeout values for flags and events waiting loops. These timeouts are
|
||||
not based on accurate values, they just guarantee that the application will
|
||||
|
@ -68,10 +68,7 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
|
|||
I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL);
|
||||
|
||||
obj->i2c = (I2CName)pinmap_merge(i2c_sda, i2c_scl);
|
||||
|
||||
if (obj->i2c == (I2CName)NC) {
|
||||
error("I2C pin mapping failed");
|
||||
}
|
||||
assert(obj->i2c != (I2CName)NC);
|
||||
|
||||
// Enable I2C clock
|
||||
if (obj->i2c == I2C_1) {
|
||||
|
@ -98,6 +95,7 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
|
|||
}
|
||||
|
||||
void i2c_frequency(i2c_t *obj, int hz) {
|
||||
assert((hz == 100000) || (hz == 200000) || (hz == 400000) || (hz == 1000000));
|
||||
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
|
||||
I2C_InitTypeDef I2C_InitStructure;
|
||||
uint32_t tim = 0;
|
||||
|
@ -140,7 +138,6 @@ void i2c_frequency(i2c_t *obj, int hz) {
|
|||
}
|
||||
break;
|
||||
default:
|
||||
error("Only 100kHz, 200kHz, 400kHz and 1MHz I2C frequencies are supported.");
|
||||
break;
|
||||
}
|
||||
|
||||
|
|
|
@ -27,13 +27,13 @@
|
|||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*******************************************************************************
|
||||
*/
|
||||
#include <assert.h>
|
||||
#include "serial_api.h"
|
||||
|
||||
#if DEVICE_SERIAL
|
||||
|
||||
#include "cmsis.h"
|
||||
#include "pinmap.h"
|
||||
#include "error.h"
|
||||
#include <string.h>
|
||||
|
||||
static const PinMap PinMap_UART_TX[] = {
|
||||
|
@ -95,10 +95,7 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) {
|
|||
|
||||
// Get the peripheral name from the pin and assign it to the object
|
||||
obj->uart = (UARTName)pinmap_merge(uart_tx, uart_rx);
|
||||
|
||||
if (obj->uart == (UARTName)NC) {
|
||||
error("Serial pinout mapping failed");
|
||||
}
|
||||
assert(obj->uart != (UARTName)NC);
|
||||
|
||||
// Enable USART clock
|
||||
if (obj->uart == UART_1) {
|
||||
|
|
|
@ -27,6 +27,7 @@
|
|||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*******************************************************************************
|
||||
*/
|
||||
#include <assert.h>
|
||||
#include "spi_api.h"
|
||||
|
||||
#if DEVICE_SPI
|
||||
|
@ -34,7 +35,6 @@
|
|||
#include <math.h>
|
||||
#include "cmsis.h"
|
||||
#include "pinmap.h"
|
||||
#include "error.h"
|
||||
|
||||
static const PinMap PinMap_SPI_MOSI[] = {
|
||||
{PA_11, SPI_2, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_DOWN, GPIO_AF_5)},
|
||||
|
@ -101,10 +101,7 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel
|
|||
SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel);
|
||||
|
||||
obj->spi = (SPIName)pinmap_merge(spi_data, spi_cntl);
|
||||
|
||||
if (obj->spi == (SPIName)NC) {
|
||||
error("SPI pinout mapping failed");
|
||||
}
|
||||
assert(obj->spi != (SPIName)NC);
|
||||
|
||||
// Enable SPI clock
|
||||
if (obj->spi == SPI_2) {
|
||||
|
|
|
@ -25,6 +25,7 @@
|
|||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#include <assert.h>
|
||||
#include "analogin_api.h"
|
||||
|
||||
#if DEVICE_ANALOGIN
|
||||
|
@ -32,7 +33,6 @@
|
|||
#include "wait_api.h"
|
||||
#include "cmsis.h"
|
||||
#include "pinmap.h"
|
||||
#include "error.h"
|
||||
|
||||
static const PinMap PinMap_ADC[] = {
|
||||
{PA_0, ADC_1, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0)}, // ADC1_IN0
|
||||
|
@ -61,10 +61,7 @@ int adc_inited = 0;
|
|||
void analogin_init(analogin_t *obj, PinName pin) {
|
||||
// Get the peripheral name from the pin and assign it to the object
|
||||
obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC);
|
||||
|
||||
if (obj->adc == (ADCName)NC) {
|
||||
error("ADC error: pinout mapping failed.");
|
||||
}
|
||||
assert(obj->adc != (ADCName)NC);
|
||||
|
||||
// Configure GPIO
|
||||
pinmap_pinout(pin, PinMap_ADC);
|
||||
|
|
|
@ -27,13 +27,13 @@
|
|||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*******************************************************************************
|
||||
*/
|
||||
#include <assert.h>
|
||||
#include "i2c_api.h"
|
||||
|
||||
#if DEVICE_I2C
|
||||
|
||||
#include "cmsis.h"
|
||||
#include "pinmap.h"
|
||||
#include "error.h"
|
||||
|
||||
/* Timeout values for flags and events waiting loops. These timeouts are
|
||||
not based on accurate values, they just guarantee that the application will
|
||||
|
@ -66,10 +66,7 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
|
|||
I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL);
|
||||
|
||||
obj->i2c = (I2CName)pinmap_merge(i2c_sda, i2c_scl);
|
||||
|
||||
if (obj->i2c == (I2CName)NC) {
|
||||
error("I2C error: pinout mapping failed.");
|
||||
}
|
||||
assert(obj->i2c != (I2CName)NC);
|
||||
|
||||
// Enable I2C clock
|
||||
if (obj->i2c == I2C_1) {
|
||||
|
@ -99,6 +96,7 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
|
|||
}
|
||||
|
||||
void i2c_frequency(i2c_t *obj, int hz) {
|
||||
assert((hz != 0) && (hz <= 400000));
|
||||
I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
|
||||
|
||||
if ((hz != 0) && (hz <= 400000)) {
|
||||
|
@ -111,13 +109,11 @@ void i2c_frequency(i2c_t *obj, int hz) {
|
|||
I2cHandle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED;
|
||||
I2cHandle.Init.OwnAddress1 = 0;
|
||||
I2cHandle.Init.OwnAddress2 = 0;
|
||||
HAL_I2C_Init(&I2cHandle);
|
||||
HAL_I2C_Init(&I2cHandle);
|
||||
if(obj->slave) {
|
||||
/* Enable Address Acknowledge */
|
||||
I2cHandle.Instance->CR1 |= I2C_CR1_ACK;
|
||||
}
|
||||
} else {
|
||||
error("I2C error: frequency setting failed (max 400kHz).");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -344,12 +340,12 @@ int i2c_slave_receive(i2c_t *obj) {
|
|||
if(__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY) == 1) {
|
||||
if(__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_ADDR) == 1) {
|
||||
if(__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TRA) == 1)
|
||||
retValue = ReadAddressed;
|
||||
retValue = ReadAddressed;
|
||||
else
|
||||
retValue = WriteAddressed;
|
||||
retValue = WriteAddressed;
|
||||
|
||||
__HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_ADDR);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return(retValue);
|
||||
|
@ -372,7 +368,7 @@ int i2c_slave_read(i2c_t *obj, char *data, int length) {
|
|||
if (Timeout == 0) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Read data from DR */
|
||||
(*data++) = I2cHandle.Instance->DR;
|
||||
|
@ -394,7 +390,7 @@ int i2c_slave_read(i2c_t *obj, char *data, int length) {
|
|||
if (Timeout == 0) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Clear STOP flag */
|
||||
__HAL_I2C_CLEAR_STOPFLAG(&I2cHandle);
|
||||
|
@ -427,7 +423,7 @@ int i2c_slave_write(i2c_t *obj, const char *data, int length) {
|
|||
if (Timeout == 0) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* Write data to DR */
|
||||
|
@ -451,7 +447,7 @@ int i2c_slave_write(i2c_t *obj, const char *data, int length) {
|
|||
if (Timeout == 0) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* Clear AF flag */
|
||||
|
|
|
@ -27,13 +27,13 @@
|
|||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*******************************************************************************
|
||||
*/
|
||||
#include <assert.h>
|
||||
#include "serial_api.h"
|
||||
|
||||
#if DEVICE_SERIAL
|
||||
|
||||
#include "cmsis.h"
|
||||
#include "pinmap.h"
|
||||
#include "error.h"
|
||||
#include <string.h>
|
||||
|
||||
static const PinMap PinMap_UART_TX[] = {
|
||||
|
@ -85,10 +85,7 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) {
|
|||
|
||||
// Get the peripheral name (UART_1, UART_2, ...) from the pin and assign it to the object
|
||||
obj->uart = (UARTName)pinmap_merge(uart_tx, uart_rx);
|
||||
|
||||
if (obj->uart == (UARTName)NC) {
|
||||
error("Serial error: pinout mapping failed.");
|
||||
}
|
||||
assert(obj->uart != (UARTName)NC);
|
||||
|
||||
// Enable USART clock
|
||||
if (obj->uart == UART_1) {
|
||||
|
|
|
@ -27,6 +27,7 @@
|
|||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*******************************************************************************
|
||||
*/
|
||||
#include <assert.h>
|
||||
#include "spi_api.h"
|
||||
|
||||
#if DEVICE_SPI
|
||||
|
@ -34,7 +35,6 @@
|
|||
#include <math.h>
|
||||
#include "cmsis.h"
|
||||
#include "pinmap.h"
|
||||
#include "error.h"
|
||||
|
||||
static const PinMap PinMap_SPI_MOSI[] = {
|
||||
{PA_7, SPI_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_SPI1)},
|
||||
|
@ -111,10 +111,7 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel
|
|||
SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel);
|
||||
|
||||
obj->spi = (SPIName)pinmap_merge(spi_data, spi_cntl);
|
||||
|
||||
if (obj->spi == (SPIName)NC) {
|
||||
error("SPI error: pinout mapping failed.");
|
||||
}
|
||||
assert(obj->spi != (SPIName)NC);
|
||||
|
||||
// Enable SPI clock
|
||||
if (obj->spi == SPI_1) {
|
||||
|
|
|
@ -25,6 +25,7 @@
|
|||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#include <assert.h>
|
||||
#include "analogin_api.h"
|
||||
|
||||
#if DEVICE_ANALOGIN
|
||||
|
@ -32,7 +33,6 @@
|
|||
#include "wait_api.h"
|
||||
#include "cmsis.h"
|
||||
#include "pinmap.h"
|
||||
#include "error.h"
|
||||
|
||||
static const PinMap PinMap_ADC[] = {
|
||||
{PA_0, ADC_1, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0)}, // ADC1_IN0
|
||||
|
@ -61,10 +61,7 @@ int adc_inited = 0;
|
|||
void analogin_init(analogin_t *obj, PinName pin) {
|
||||
// Get the peripheral name from the pin and assign it to the object
|
||||
obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC);
|
||||
|
||||
if (obj->adc == (ADCName)NC) {
|
||||
error("ADC error: pinout mapping failed.");
|
||||
}
|
||||
assert(obj->adc != (ADCName)NC);
|
||||
|
||||
// Configure GPIO
|
||||
pinmap_pinout(pin, PinMap_ADC);
|
||||
|
|
|
@ -25,6 +25,7 @@
|
|||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#include <assert.h>
|
||||
#include "analogout_api.h"
|
||||
|
||||
#if DEVICE_ANALOGOUT
|
||||
|
@ -49,10 +50,7 @@ void analogout_init(dac_t *obj, PinName pin) {
|
|||
|
||||
// Get the peripheral name (DAC_1, ...) from the pin and assign it to the object
|
||||
obj->dac = (DACName)pinmap_peripheral(pin, PinMap_DAC);
|
||||
|
||||
if (obj->dac == (DACName)NC) {
|
||||
error("DAC pin mapping failed");
|
||||
}
|
||||
assert(obj->dac != (DACName)NC);
|
||||
|
||||
// Configure GPIO
|
||||
pinmap_pinout(pin, PinMap_DAC);
|
||||
|
|
|
@ -64,7 +64,7 @@ void gpio_mode(gpio_t *obj, PinMode mode) {
|
|||
}
|
||||
|
||||
void gpio_dir(gpio_t *obj, PinDirection direction) {
|
||||
assert(pin != (PinName)NC);
|
||||
assert(obj->pin != (PinName)NC);
|
||||
if (direction == PIN_OUTPUT) {
|
||||
pin_function(obj->pin, STM_PIN_DATA(STM_MODE_OUTPUT_PP, GPIO_NOPULL, 0));
|
||||
} else { // PIN_INPUT
|
||||
|
|
|
@ -27,6 +27,7 @@
|
|||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*******************************************************************************
|
||||
*/
|
||||
#include <assert.h>
|
||||
#include "i2c_api.h"
|
||||
|
||||
#if DEVICE_I2C
|
||||
|
@ -65,10 +66,7 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
|
|||
I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL);
|
||||
|
||||
obj->i2c = (I2CName)pinmap_merge(i2c_sda, i2c_scl);
|
||||
|
||||
if (obj->i2c == (I2CName)NC) {
|
||||
error("I2C error: pinout mapping failed.");
|
||||
}
|
||||
assert(obj->i2c != (I2CName)NC);
|
||||
|
||||
// Enable I2C clock
|
||||
if (obj->i2c == I2C_1) {
|
||||
|
@ -92,6 +90,7 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
|
|||
}
|
||||
|
||||
void i2c_frequency(i2c_t *obj, int hz) {
|
||||
assert((hz == 100000) || (hz == 400000) || (hz == 1000000));
|
||||
I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
|
||||
|
||||
// Common settings: I2C clock = 32 MHz, Analog filter = ON, Digital filter coefficient = 0
|
||||
|
@ -106,7 +105,6 @@ void i2c_frequency(i2c_t *obj, int hz) {
|
|||
I2cHandle.Init.Timing = 0x0030040E; // Fast mode Plus with Rise Time = 60ns and Fall Time = 100ns
|
||||
break;
|
||||
default:
|
||||
error("Only 100kHz, 400kHz and 1MHz I2C frequencies are supported.");
|
||||
break;
|
||||
}
|
||||
|
||||
|
|
|
@ -27,13 +27,13 @@
|
|||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*******************************************************************************
|
||||
*/
|
||||
#include <assert.h>
|
||||
#include "serial_api.h"
|
||||
|
||||
#if DEVICE_SERIAL
|
||||
|
||||
#include "cmsis.h"
|
||||
#include "pinmap.h"
|
||||
#include "error.h"
|
||||
#include <string.h>
|
||||
|
||||
static const PinMap PinMap_UART_TX[] = {
|
||||
|
@ -98,10 +98,7 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) {
|
|||
|
||||
// Get the peripheral name (UART_1, UART_2, ...) from the pin and assign it to the object
|
||||
obj->uart = (UARTName)pinmap_merge(uart_tx, uart_rx);
|
||||
|
||||
if (obj->uart == (UARTName)NC) {
|
||||
error("Serial error: pinout mapping failed.");
|
||||
}
|
||||
assert(obj->uart != (UARTName)NC);
|
||||
|
||||
// Enable UART clock
|
||||
if (obj->uart == UART_1) {
|
||||
|
|
|
@ -27,6 +27,7 @@
|
|||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*******************************************************************************
|
||||
*/
|
||||
#include <assert.h>
|
||||
#include "spi_api.h"
|
||||
|
||||
#if DEVICE_SPI
|
||||
|
@ -34,7 +35,6 @@
|
|||
#include <math.h>
|
||||
#include "cmsis.h"
|
||||
#include "pinmap.h"
|
||||
#include "error.h"
|
||||
|
||||
static const PinMap PinMap_SPI_MOSI[] = {
|
||||
{PA_7, SPI_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF0_SPI1)},
|
||||
|
@ -105,10 +105,7 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel
|
|||
SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel);
|
||||
|
||||
obj->spi = (SPIName)pinmap_merge(spi_data, spi_cntl);
|
||||
|
||||
if (obj->spi == (SPIName)NC) {
|
||||
error("SPI error: pinout mapping failed.");
|
||||
}
|
||||
assert(obj->spi != (SPIName)NC);
|
||||
|
||||
// Enable SPI clock
|
||||
if (obj->spi == SPI_1) {
|
||||
|
|
|
@ -25,13 +25,13 @@
|
|||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#include <assert.h>
|
||||
#include "analogin_api.h"
|
||||
|
||||
#if DEVICE_ANALOGIN
|
||||
|
||||
#include "cmsis.h"
|
||||
#include "pinmap.h"
|
||||
#include "error.h"
|
||||
#include "wait_api.h"
|
||||
|
||||
static const PinMap PinMap_ADC[] = {
|
||||
|
@ -66,10 +66,7 @@ void analogin_init(analogin_t *obj, PinName pin) {
|
|||
|
||||
// Get the peripheral name from the pin and assign it to the object
|
||||
obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC);
|
||||
|
||||
if (obj->adc == (ADCName)NC) {
|
||||
error("ADC pin mapping failed");
|
||||
}
|
||||
assert(obj->adc != (ADCName)NC);
|
||||
|
||||
// Configure GPIO
|
||||
pinmap_pinout(pin, PinMap_ADC);
|
||||
|
|
|
@ -25,13 +25,13 @@
|
|||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#include <assert.h>
|
||||
#include "analogout_api.h"
|
||||
|
||||
#if DEVICE_ANALOGOUT
|
||||
|
||||
#include "cmsis.h"
|
||||
#include "pinmap.h"
|
||||
#include "error.h"
|
||||
|
||||
#define RANGE_12BIT (0xFFF)
|
||||
|
||||
|
@ -46,10 +46,7 @@ void analogout_init(dac_t *obj, PinName pin) {
|
|||
|
||||
// Get the peripheral name (DAC_1, ...) from the pin and assign it to the object
|
||||
obj->dac = (DACName)pinmap_peripheral(pin, PinMap_DAC);
|
||||
|
||||
if (obj->dac == (DACName)NC) {
|
||||
error("DAC pin mapping failed");
|
||||
}
|
||||
assert(obj->dac != (DACName)NC);
|
||||
|
||||
// Configure GPIO
|
||||
pinmap_pinout(pin, PinMap_DAC);
|
||||
|
|
|
@ -27,13 +27,14 @@
|
|||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*******************************************************************************
|
||||
*/
|
||||
#include <assert.h>
|
||||
#include "i2c_api.h"
|
||||
|
||||
#if DEVICE_I2C
|
||||
|
||||
#include "cmsis.h"
|
||||
#include "pinmap.h"
|
||||
#include "error.h"
|
||||
|
||||
|
||||
/* Timeout values for flags and events waiting loops. These timeouts are
|
||||
not based on accurate values, they just guarantee that the application will
|
||||
|
@ -61,10 +62,7 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
|
|||
I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL);
|
||||
|
||||
obj->i2c = (I2CName)pinmap_merge(i2c_sda, i2c_scl);
|
||||
|
||||
if (obj->i2c == (I2CName)NC) {
|
||||
error("I2C pin mapping failed");
|
||||
}
|
||||
assert(obj->i2c != (I2CName)NC);
|
||||
|
||||
// Enable I2C clock
|
||||
if (obj->i2c == I2C_1) {
|
||||
|
|
|
@ -27,13 +27,13 @@
|
|||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*******************************************************************************
|
||||
*/
|
||||
#include <assert.h>
|
||||
#include "pwmout_api.h"
|
||||
|
||||
#if DEVICE_PWMOUT
|
||||
|
||||
#include "cmsis.h"
|
||||
#include "pinmap.h"
|
||||
#include "error.h"
|
||||
|
||||
// TIM5 cannot be used because already used by the us_ticker
|
||||
static const PinMap PinMap_PWM[] = {
|
||||
|
@ -77,10 +77,7 @@ static const PinMap PinMap_PWM[] = {
|
|||
void pwmout_init(pwmout_t* obj, PinName pin) {
|
||||
// Get the peripheral name from the pin and assign it to the object
|
||||
obj->pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM);
|
||||
|
||||
if (obj->pwm == (PWMName)NC) {
|
||||
error("PWM pinout mapping failed");
|
||||
}
|
||||
assert(obj->pwm != (PWMName)NC);
|
||||
|
||||
// Enable TIM clock
|
||||
if (obj->pwm == PWM_2) RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
|
||||
|
|
|
@ -27,13 +27,13 @@
|
|||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*******************************************************************************
|
||||
*/
|
||||
#include <assert.h>
|
||||
#include "serial_api.h"
|
||||
|
||||
#if DEVICE_SERIAL
|
||||
|
||||
#include "cmsis.h"
|
||||
#include "pinmap.h"
|
||||
#include "error.h"
|
||||
#include <string.h>
|
||||
|
||||
static const PinMap PinMap_UART_TX[] = {
|
||||
|
@ -91,10 +91,7 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) {
|
|||
|
||||
// Get the peripheral name (UART_1, UART_2, ...) from the pin and assign it to the object
|
||||
obj->uart = (UARTName)pinmap_merge(uart_tx, uart_rx);
|
||||
|
||||
if (obj->uart == (UARTName)NC) {
|
||||
error("Serial pinout mapping failed");
|
||||
}
|
||||
assert(obj->uart != (UARTName)NC);
|
||||
|
||||
// Enable USART clock
|
||||
if (obj->uart == UART_1) {
|
||||
|
|
|
@ -27,6 +27,7 @@
|
|||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*******************************************************************************
|
||||
*/
|
||||
#include <assert.h>
|
||||
#include "spi_api.h"
|
||||
|
||||
#if DEVICE_SPI
|
||||
|
@ -34,7 +35,6 @@
|
|||
#include <math.h>
|
||||
#include "cmsis.h"
|
||||
#include "pinmap.h"
|
||||
#include "error.h"
|
||||
|
||||
static const PinMap PinMap_SPI_MOSI[] = {
|
||||
{PA_7, SPI_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_SPI1)},
|
||||
|
@ -105,10 +105,7 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel
|
|||
SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel);
|
||||
|
||||
obj->spi = (SPIName)pinmap_merge(spi_data, spi_cntl);
|
||||
|
||||
if (obj->spi == (SPIName)NC) {
|
||||
error("SPI pinout mapping failed");
|
||||
}
|
||||
assert(obj->spi != (SPIName)NC);
|
||||
|
||||
// Enable SPI clock
|
||||
if (obj->spi == SPI_1) {
|
||||
|
|
|
@ -25,6 +25,7 @@
|
|||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#include <assert.h>
|
||||
#include "analogin_api.h"
|
||||
#include "wait_api.h"
|
||||
|
||||
|
@ -32,7 +33,6 @@
|
|||
|
||||
#include "cmsis.h"
|
||||
#include "pinmap.h"
|
||||
#include "error.h"
|
||||
|
||||
static const PinMap PinMap_ADC[] = {
|
||||
{PA_0, ADC_1, STM_PIN_DATA(GPIO_Mode_AN, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0xFF)}, // ADC_IN1
|
||||
|
@ -63,10 +63,7 @@ void analogin_init(analogin_t *obj, PinName pin) {
|
|||
|
||||
// Get the peripheral name from the pin and assign it to the object
|
||||
obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC);
|
||||
|
||||
if (obj->adc == (ADCName)NC) {
|
||||
error("ADC pin mapping failed");
|
||||
}
|
||||
assert(obj->adc == (ADCName)NC);
|
||||
|
||||
// Configure GPIO
|
||||
pinmap_pinout(pin, PinMap_ADC);
|
||||
|
|
|
@ -25,13 +25,13 @@
|
|||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#include <assert.h>
|
||||
#include "analogout_api.h"
|
||||
|
||||
#if DEVICE_ANALOGOUT
|
||||
|
||||
#include "cmsis.h"
|
||||
#include "pinmap.h"
|
||||
#include "error.h"
|
||||
|
||||
#define RANGE_12BIT (0xFFF)
|
||||
|
||||
|
@ -46,10 +46,7 @@ void analogout_init(dac_t *obj, PinName pin) {
|
|||
|
||||
// Get the peripheral name (DAC_1, ...) from the pin and assign it to the object
|
||||
obj->dac = (DACName)pinmap_peripheral(pin, PinMap_DAC);
|
||||
|
||||
if (obj->dac == (DACName)NC) {
|
||||
error("DAC pin mapping failed");
|
||||
}
|
||||
assert(obj->dac == (DACName)NC);
|
||||
|
||||
dac = (DAC_TypeDef *)(obj->dac);
|
||||
|
||||
|
|
|
@ -27,13 +27,13 @@
|
|||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*******************************************************************************
|
||||
*/
|
||||
#include <assert.h>
|
||||
#include "i2c_api.h"
|
||||
|
||||
#if DEVICE_I2C
|
||||
|
||||
#include "cmsis.h"
|
||||
#include "pinmap.h"
|
||||
#include "error.h"
|
||||
|
||||
/* Timeout values for flags and events waiting loops. These timeouts are
|
||||
not based on accurate values, they just guarantee that the application will
|
||||
|
@ -68,10 +68,7 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
|
|||
I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL);
|
||||
|
||||
obj->i2c = (I2CName)pinmap_merge(i2c_sda, i2c_scl);
|
||||
|
||||
if (obj->i2c == (I2CName)NC) {
|
||||
error("I2C pin mapping failed");
|
||||
}
|
||||
assert(obj->i2c != (I2CName)NC);
|
||||
|
||||
// Enable I2C clock
|
||||
if (obj->i2c == I2C_1) {
|
||||
|
@ -98,6 +95,7 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
|
|||
}
|
||||
|
||||
void i2c_frequency(i2c_t *obj, int hz) {
|
||||
assert((hz == 100000) || (hz == 200000) || (hz == 400000) || (hz == 1000000));
|
||||
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
|
||||
I2C_InitTypeDef I2C_InitStructure;
|
||||
uint32_t tim;
|
||||
|
@ -140,7 +138,6 @@ void i2c_frequency(i2c_t *obj, int hz) {
|
|||
}
|
||||
break;
|
||||
default:
|
||||
error("Only 100kHz, 200kHz, 400kHz and 1MHz I2C frequencies are supported.");
|
||||
break;
|
||||
}
|
||||
|
||||
|
|
|
@ -27,11 +27,11 @@
|
|||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*******************************************************************************
|
||||
*/
|
||||
#include <assert.h>
|
||||
#include "pwmout_api.h"
|
||||
|
||||
#include "cmsis.h"
|
||||
#include "pinmap.h"
|
||||
#include "error.h"
|
||||
|
||||
// TIM2 cannot be used because already used by the us_ticker
|
||||
static const PinMap PinMap_PWM[] = {
|
||||
|
@ -88,10 +88,7 @@ static const PinMap PinMap_PWM[] = {
|
|||
void pwmout_init(pwmout_t* obj, PinName pin) {
|
||||
// Get the peripheral name from the pin and assign it to the object
|
||||
obj->pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM);
|
||||
|
||||
if (obj->pwm == (PWMName)NC) {
|
||||
error("PWM pinout mapping failed");
|
||||
}
|
||||
assert(obj->pwm == (PWMName)NC);
|
||||
|
||||
// Enable TIM clock
|
||||
if (obj->pwm == PWM_1) RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
|
||||
|
|
|
@ -27,10 +27,10 @@
|
|||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*******************************************************************************
|
||||
*/
|
||||
#include <assert.h>
|
||||
#include "serial_api.h"
|
||||
#include "cmsis.h"
|
||||
#include "pinmap.h"
|
||||
#include "error.h"
|
||||
#include <string.h>
|
||||
|
||||
static const PinMap PinMap_UART_TX[] = {
|
||||
|
@ -92,10 +92,7 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) {
|
|||
|
||||
// Get the peripheral name from the pin and assign it to the object
|
||||
obj->uart = (UARTName)pinmap_merge(uart_tx, uart_rx);
|
||||
|
||||
if (obj->uart == (UARTName)NC) {
|
||||
error("Serial pinout mapping failed");
|
||||
}
|
||||
assert(obj->uart != (UARTName)NC);
|
||||
|
||||
// Enable USART clock
|
||||
if (obj->uart == UART_1) {
|
||||
|
|
|
@ -27,6 +27,7 @@
|
|||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*******************************************************************************
|
||||
*/
|
||||
#include <assert.h>
|
||||
#include "spi_api.h"
|
||||
|
||||
#if DEVICE_SPI
|
||||
|
@ -34,7 +35,6 @@
|
|||
#include <math.h>
|
||||
#include "cmsis.h"
|
||||
#include "pinmap.h"
|
||||
#include "error.h"
|
||||
|
||||
static const PinMap PinMap_SPI_MOSI[] = {
|
||||
{PA_11, SPI_2, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_DOWN, GPIO_AF_5)},
|
||||
|
@ -102,9 +102,7 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel
|
|||
|
||||
obj->spi = (SPIName)pinmap_merge(spi_data, spi_cntl);
|
||||
|
||||
if (obj->spi == (SPIName)NC) {
|
||||
error("SPI pinout mapping failed");
|
||||
}
|
||||
assert(obj->spi != (SPIName)NC);
|
||||
|
||||
// Enable SPI clock
|
||||
if (obj->spi == SPI_2) {
|
||||
|
|
|
@ -13,6 +13,7 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#include <assert.h>
|
||||
#include "analogin_api.h"
|
||||
|
||||
#if DEVICE_ANALOGIN
|
||||
|
@ -48,9 +49,7 @@ static const PinMap PinMap_ADC[] = {
|
|||
|
||||
void analogin_init(analogin_t *obj, PinName pin) {
|
||||
obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC);
|
||||
if (obj->adc == (uint32_t)NC) {
|
||||
error("ADC pin mapping failed");
|
||||
}
|
||||
assert(obj->adc != (uint32_t)NC);
|
||||
|
||||
// ensure power is turned on
|
||||
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN | RCC_AHB1ENR_GPIOBEN |
|
||||
|
|
|
@ -13,6 +13,7 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#include <assert.h>
|
||||
#include "i2c_api.h"
|
||||
|
||||
#if DEVICE_I2C
|
||||
|
@ -124,10 +125,7 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
|
|||
I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA);
|
||||
I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL);
|
||||
obj->i2c = (I2C_TypeDef *)pinmap_merge(i2c_sda, i2c_scl);
|
||||
|
||||
if ((int)obj->i2c == NC) {
|
||||
error("I2C pin mapping failed");
|
||||
}
|
||||
assert((int)obj->i2c != NC);
|
||||
|
||||
// enable power
|
||||
i2c_power_enable(obj);
|
||||
|
|
|
@ -13,6 +13,7 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#include <assert.h>
|
||||
#include "spi_api.h"
|
||||
|
||||
#if DEVICE_SPI
|
||||
|
@ -20,7 +21,6 @@
|
|||
|
||||
#include "cmsis.h"
|
||||
#include "pinmap.h"
|
||||
#include "error.h"
|
||||
|
||||
static const PinMap PinMap_SPI_SCLK[] = {
|
||||
{PA_5, SPI_1, STM_PIN_DATA(2, 5)},
|
||||
|
@ -57,7 +57,7 @@ static const PinMap PinMap_SPI_SSEL[] = {
|
|||
{PA_4, SPI_3, STM_PIN_DATA(2, 6)},
|
||||
{PA_15, SPI_1, STM_PIN_DATA(2, 5)},
|
||||
{PA_15, SPI_3, STM_PIN_DATA(2, 6)},
|
||||
{PB_9, SPI_2, STM_PIN_DATA(2, 5)},
|
||||
{PB_9, SPI_2, STM_PIN_DATA(2, 5)},
|
||||
{PB_12, SPI_2, STM_PIN_DATA(2, 5)},
|
||||
{NC, NC, 0}
|
||||
};
|
||||
|
@ -75,9 +75,7 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel
|
|||
SPIName spi_data = (SPIName)pinmap_merge(spi_mosi, spi_miso);
|
||||
SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel);
|
||||
obj->spi = (SPI_TypeDef*)pinmap_merge(spi_data, spi_cntl);
|
||||
if ((int)obj->spi == NC) {
|
||||
error("SPI pinout mapping failed");
|
||||
}
|
||||
assert((int)obj->spi != NC)
|
||||
|
||||
// enable power and clocking
|
||||
switch ((int)obj->spi) {
|
||||
|
@ -123,12 +121,8 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel
|
|||
void spi_free(spi_t *obj) {}
|
||||
|
||||
void spi_format(spi_t *obj, int bits, int mode, int slave) {
|
||||
assert((bits == 8 || bits == 16) || (mode >= 0 && mode <= 3));
|
||||
ssp_disable(obj);
|
||||
|
||||
if (!(bits == 8 || bits == 16) || !(mode >= 0 && mode <= 3)) {
|
||||
error("SPI format error");
|
||||
}
|
||||
|
||||
|
||||
int polarity = (mode & 0x2) ? 1 : 0;
|
||||
int phase = (mode & 0x1) ? 1 : 0;
|
||||
|
|
Loading…
Reference in New Issue