v0.5.0
- добавлена поддержка платы ELBEAR ACE-NANO; - добавлена поддержка плат ELSOMIK OEM и SE; - добавлена возможность работы в режиме отладки для всех плат, входящих в состав пакета. Доступно для версии ArduinoIDE 2 и выше; - добавлена поддержка библиотеки FreeRTOS; - добавлена поддержка библиотеки IRremote; - добавлена поддержка библиотеки OneWire; - добавлена поддержка аппаратного I2C0 для плат START-MIK32 и ELSOMIK. Для работы с ним доступен экземпляр класса Wire1; - добавлена поддержка аппаратного SPI0 для всех плат, входящих в пакет. Для работы с ним доступен экземпляр класса SPI1; - увеличено быстродействие функций digitalWrite, digitalRead; - исправлены известные ошибки. Co-authored-by: KlassenTS <klassen@elron.tech> Co-committed-by: KlassenTS <klassen@elron.tech>
This commit was merged in pull request #19.
This commit is contained in:
1 parent
de1f0d7fda
commit
1c8e06634c
211 files changed
+97525
-591
No files matched your search
@@ -0,0 +1,169 @@
|
||||
/*
|
||||
pins_arduino.h - Pin definition functions for Arduino
|
||||
Part of Arduino - http://www.arduino.cc/
|
||||
|
||||
Copyright (c) 2007 David A. Mellis
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General
|
||||
Public License along with this library; if not, write to the
|
||||
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
|
||||
Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
#ifndef Pins_Arduino_h
|
||||
#define Pins_Arduino_h
|
||||
|
||||
#include "wiring_constants.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "mik32_hal_gpio.h"
|
||||
#include "mik32_hal_timer32.h"
|
||||
|
||||
// total number of pins available for initialization
|
||||
#define PINS_COMMON_QTY 26
|
||||
#define pinCommonQty() (PINS_COMMON_QTY)
|
||||
|
||||
extern bool spi0NssPinIsBlocked;
|
||||
extern bool spi1NssPinIsBlocked;
|
||||
|
||||
// analog pins
|
||||
#define PIN_A0 (14)
|
||||
#define PIN_A1 (15)
|
||||
#define PIN_A2 (16)
|
||||
#define PIN_A3 (17)
|
||||
// there are D18 and D19 between them for SDA and SCL
|
||||
#define PIN_A4 (20)
|
||||
#define PIN_A5 (21)
|
||||
// there are led (22) and btn (23) on the ace-uno between them
|
||||
#define PIN_A6 (24)
|
||||
#define PIN_A7 (25)
|
||||
static const uint8_t A0 = PIN_A0;
|
||||
static const uint8_t A1 = PIN_A1;
|
||||
static const uint8_t A2 = PIN_A2;
|
||||
static const uint8_t A3 = PIN_A3;
|
||||
static const uint8_t A4 = PIN_A4;
|
||||
static const uint8_t A5 = PIN_A5;
|
||||
static const uint8_t A6 = PIN_A6;
|
||||
static const uint8_t A7 = PIN_A7;
|
||||
|
||||
|
||||
// digital pins
|
||||
// D0...D13, D18, D19
|
||||
|
||||
// User led and button
|
||||
#define LED_BUILTIN (22)
|
||||
|
||||
// determines the address of the port by the board pin number to which this pin belongs on the MCU
|
||||
GPIO_TypeDef* digitalPinToPort(uint32_t digPinNumber);
|
||||
// determines the pin address inside the port by the board pin number
|
||||
HAL_PinsTypeDef digitalPinToBitMask(uint32_t digPinNumber);
|
||||
// the function returns a reference to the OUTPUT address of the GPIO register
|
||||
volatile uint32_t* portOutputRegister(GPIO_TypeDef* GPIO_x);
|
||||
// the function returns a reference to the STATE address of the GPIO register
|
||||
volatile uint32_t* portInputRegister(GPIO_TypeDef* GPIO_x);
|
||||
// the function initializes additional MCU pins depending on the specified pin number
|
||||
void additionalPinsInit(uint32_t PinNumber);
|
||||
void additionalPinsDeinit(uint32_t PinNumber);
|
||||
|
||||
// UART
|
||||
// available uarts quantity
|
||||
#define SERIAL_PORT_QTY 2
|
||||
|
||||
// ADC
|
||||
#define MCU_ADC_RESOLUTION 12 // bits
|
||||
// determines the ADC channel number by the board pin number
|
||||
uint32_t analogInputToChannelNumber(uint32_t PinNumber);
|
||||
|
||||
// PWM
|
||||
#define PWM_FREQUENCY_MAX 1000000 // Hz
|
||||
static inline __attribute__((always_inline)) bool digitalPinHasPWM(uint8_t p)
|
||||
{
|
||||
// if spi is in use D9 or D10 cannot work as pwm
|
||||
if (p == 3 || p == 5 || p == 6 || (p >= 11 && p <= 13) || ((p == 9) && !spi0NssPinIsBlocked) || ((p == 10) && !spi1NssPinIsBlocked))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
// return true if digitalPin configured as pwm
|
||||
bool digitalPinPwmIsOn(uint8_t digitalPin);
|
||||
// determines which timer the pin belongs to
|
||||
TIMER32_TypeDef* pwmPinToTimer(uint32_t digPinNumber);
|
||||
// determines which timer channel the pin belongs to
|
||||
HAL_TIMER32_CHANNEL_IndexTypeDef pwmPinToTimerChannel(uint32_t digPinNumber);
|
||||
|
||||
// SPI
|
||||
#define SPI_COMMON_QTY 2
|
||||
|
||||
#define PIN_SPI_SS 10
|
||||
#define PIN_SPI_MOSI 11
|
||||
#define PIN_SPI_MISO 12
|
||||
#define PIN_SPI_SCK 13
|
||||
static const uint8_t SS = PIN_SPI_SS;
|
||||
static const uint8_t MOSI = PIN_SPI_MOSI;
|
||||
static const uint8_t MISO = PIN_SPI_MISO;
|
||||
static const uint8_t SCK = PIN_SPI_SCK;
|
||||
|
||||
|
||||
#define PIN_SPI1_SS 9
|
||||
#define PIN_SPI1_MOSI 5
|
||||
#define PIN_SPI1_MISO 3
|
||||
#define PIN_SPI1_SCK 6
|
||||
static const uint8_t SS1 = PIN_SPI1_SS;
|
||||
static const uint8_t MOSI1 = PIN_SPI1_MOSI;
|
||||
static const uint8_t MISO1 = PIN_SPI1_MISO;
|
||||
static const uint8_t SCK1 = PIN_SPI1_SCK;
|
||||
|
||||
// config SEL_NSS1 to replace D10 to different controller pin,
|
||||
// because NSS which is D9/D10 by default is needed to spi
|
||||
void spi_onBegin(uint8_t spiNum);
|
||||
void spi_onEnd(uint8_t spiNum);
|
||||
|
||||
// I2C
|
||||
#define PIN_WIRE_SDA 18
|
||||
#define PIN_WIRE_SCL 19
|
||||
#define I2C_COMMON_QTY 1
|
||||
static const uint8_t SDA = PIN_WIRE_SDA;
|
||||
static const uint8_t SCL = PIN_WIRE_SCL;
|
||||
// available frequencies
|
||||
#define WIRE_FREQ_100K 100000
|
||||
#define WIRE_FREQ_400K 400000
|
||||
#define WIRE_FREQ_1000K 1000000
|
||||
|
||||
// interrupts
|
||||
#define EXTERNAL_INTERRUPTS_QTY 8
|
||||
extern uint8_t interruptInfo[EXTERNAL_INTERRUPTS_QTY][3];
|
||||
// determines the board pin number by interrupt number
|
||||
#define interruptToDigitalPin(interruptNum) (interruptInfo[interruptNum][0])
|
||||
// determines gpio interrupt line by interrupt number
|
||||
#define interruptToGpioIntLine(interruptNum) ((uint8_t)interruptInfo[interruptNum][1])
|
||||
// determines gpio interrupt mux by interrupt number
|
||||
#define interruptToGpioIntMux(interruptNum) ((uint8_t)interruptInfo[interruptNum][2])
|
||||
// determines interrupt number by the board pin number
|
||||
int8_t digitalPinToInterrupt(uint32_t digPinNumber);
|
||||
// determines interrupt number by the gpio interrupt line
|
||||
int8_t gpioIntLineToInterrupt(uint32_t gpioIntLine);
|
||||
// determines gpio interrupt mux by the board pin number
|
||||
int8_t digitalPinToGpioIntMux(uint8_t digPinNumber);
|
||||
// determines gpio interrupt line by the board pin number
|
||||
int8_t digitalPinToGpioIntLine(uint8_t digPinNumber);
|
||||
|
||||
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,401 @@
|
||||
/**
|
||||
*******************************************************************************
|
||||
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This software component is licensed by WCH under BSD 3-Clause license,
|
||||
* the "License"; You may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at:
|
||||
* opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
*******************************************************************************
|
||||
*/
|
||||
|
||||
#include "pins_arduino.h"
|
||||
#include "mik32_hal_adc.h"
|
||||
#include "wiring_analog.h"
|
||||
#include "wiring_LL.h"
|
||||
|
||||
#define SPI0_SWITCH_PORT GPIO_1
|
||||
#define SPI0_SWITCH_PIN GPIO_PIN_10
|
||||
#define SPI0_NSS_IN_PORT GPIO_0
|
||||
#define SPI0_NSS_IN_PIN GPIO_PIN_3
|
||||
#define SPI0_NSS_OUT_PORT GPIO_1
|
||||
#define SPI0_NSS_OUT_PIN GPIO_PIN_14
|
||||
|
||||
#define SPI1_SWITCH_PORT GPIO_1
|
||||
#define SPI1_SWITCH_PIN GPIO_PIN_6
|
||||
#define SPI1_NSS_IN_PORT GPIO_1
|
||||
#define SPI1_NSS_IN_PIN GPIO_PIN_3
|
||||
#define SPI1_NSS_OUT_PORT GPIO_1
|
||||
#define SPI1_NSS_OUT_PIN GPIO_PIN_4
|
||||
|
||||
bool spi0NssPinIsBlocked = false;
|
||||
bool spi1NssPinIsBlocked = false;
|
||||
|
||||
// list of pin numbers from both ports with pins inside the port
|
||||
const HAL_PinsTypeDef digitalPinToGpioPinArray[] =
|
||||
{
|
||||
GPIO_PIN_5, // D0
|
||||
GPIO_PIN_6, // D1
|
||||
GPIO_PIN_10,// D2
|
||||
GPIO_PIN_0, // D3
|
||||
GPIO_PIN_8, // D4
|
||||
GPIO_PIN_1, // D5
|
||||
GPIO_PIN_2, // D6
|
||||
GPIO_PIN_8, // D7
|
||||
GPIO_PIN_9, // D8
|
||||
GPIO_PIN_3, // D9
|
||||
GPIO_PIN_3, // D10
|
||||
GPIO_PIN_1, // D11
|
||||
GPIO_PIN_0, // D12
|
||||
GPIO_PIN_2, // D13
|
||||
GPIO_PIN_5, // D14/A0
|
||||
GPIO_PIN_7, // D15/A1
|
||||
GPIO_PIN_4, // D16/A2
|
||||
GPIO_PIN_7, // D17/A3
|
||||
GPIO_PIN_12,// D18
|
||||
GPIO_PIN_13,// D19
|
||||
GPIO_PIN_9, // A4 (pin 20)
|
||||
GPIO_PIN_9, // A5 (pin 21)
|
||||
GPIO_PIN_7, // LED(pin 22)
|
||||
0, // dummy pin for backward compatibility with ace-uno
|
||||
GPIO_PIN_9, // A6 (pin 24)
|
||||
GPIO_PIN_9, // A7 (pin 25)
|
||||
};
|
||||
|
||||
// determines the address of the port by the board pin number to which this pin belongs on the MCU
|
||||
__attribute__((noinline, section(".ram_text"))) GPIO_TypeDef* digitalPinToPort(uint32_t digPinNumber)
|
||||
{
|
||||
if (digPinNumber >= pinCommonQty())
|
||||
{
|
||||
ErrorMsgHandler("digitalPinToPort(): pin number exceeds the total number of pins");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// port 2 - led (22)
|
||||
if (digPinNumber == LED_BUILTIN)
|
||||
return GPIO_2;
|
||||
// port 1 - board pins 7, 8, 10...13, 14(А0), 15(А1), 18,19
|
||||
else if (digPinNumber == 7 || digPinNumber == 8 || (digPinNumber >= 10 && digPinNumber <= 15)
|
||||
|| digPinNumber == 18 || digPinNumber == 19 || (digPinNumber == 9 && spi0NssPinIsBlocked))
|
||||
return GPIO_1;
|
||||
// port 0 - board pins 0...6, 9, 16(A2), 17(A3), 20(A4), 21(A5), 24(A6), 25(A7)
|
||||
else
|
||||
return GPIO_0;
|
||||
}
|
||||
|
||||
// determines the pin address inside the port by the board pin number
|
||||
__attribute__((noinline, section(".ram_text"))) HAL_PinsTypeDef digitalPinToBitMask(uint32_t digPinNumber)
|
||||
{
|
||||
if (digPinNumber >= pinCommonQty())
|
||||
{
|
||||
ErrorMsgHandler("digitalPinToBitMask(): pin number exceeds the total number of pins");
|
||||
return NOT_A_PIN;
|
||||
}
|
||||
|
||||
// if spi is on default pin NSS_IN is needed for spi, board pin is replaced to pin NSS_OUT
|
||||
if ((digPinNumber == 10) && spi1NssPinIsBlocked)
|
||||
return SPI1_NSS_OUT_PIN;
|
||||
else if ((digPinNumber == 9) && spi0NssPinIsBlocked)
|
||||
return SPI0_NSS_OUT_PIN;
|
||||
else
|
||||
return digitalPinToGpioPinArray[digPinNumber];
|
||||
}
|
||||
|
||||
// the function returns a reference to the OUTPUT address of the GPIO register
|
||||
volatile uint32_t* portOutputRegister(GPIO_TypeDef* GPIO_x)
|
||||
{
|
||||
return &GPIO_x->OUTPUT_;
|
||||
}
|
||||
|
||||
// the function returns a reference to the STATE address of the GPIO register
|
||||
volatile uint32_t* portInputRegister(GPIO_TypeDef* GPIO_x)
|
||||
{
|
||||
return &GPIO_x->STATE;
|
||||
}
|
||||
|
||||
// the function initializes additional MCU pins depending on the specified pin number
|
||||
#define SELA_A_PORT GPIO_1
|
||||
#define SELA_A_PIN GPIO_PIN_15
|
||||
#define SELA_B_PORT GPIO_1
|
||||
#define SELA_B_PIN GPIO_PIN_11
|
||||
static bool selaSwitchesIsInited = false;
|
||||
|
||||
void additionalPinsInit(uint32_t PinNumber)
|
||||
{
|
||||
// init switches
|
||||
if (!selaSwitchesIsInited)
|
||||
{
|
||||
// SELA_A
|
||||
HAL_GPIO_PinConfig(SELA_A_PORT, SELA_A_PIN, HAL_GPIO_MODE_GPIO_OUTPUT, HAL_GPIO_PULL_NONE, HAL_GPIO_DS_2MA);
|
||||
// SELA_B
|
||||
HAL_GPIO_PinConfig(SELA_B_PORT, SELA_B_PIN, HAL_GPIO_MODE_GPIO_OUTPUT, HAL_GPIO_PULL_NONE, HAL_GPIO_DS_2MA);
|
||||
selaSwitchesIsInited = true;
|
||||
}
|
||||
|
||||
if (PinNumber == A4)
|
||||
{
|
||||
HAL_GPIO_WritePin(SELA_A_PORT, SELA_A_PIN, GPIO_PIN_LOW);
|
||||
HAL_GPIO_WritePin(SELA_B_PORT, SELA_B_PIN, GPIO_PIN_LOW);
|
||||
}
|
||||
else if(PinNumber == A5)
|
||||
{
|
||||
HAL_GPIO_WritePin(SELA_A_PORT, SELA_A_PIN, GPIO_PIN_HIGH);
|
||||
HAL_GPIO_WritePin(SELA_B_PORT, SELA_B_PIN, GPIO_PIN_LOW);
|
||||
}
|
||||
else if(PinNumber == A6)
|
||||
{
|
||||
HAL_GPIO_WritePin(SELA_A_PORT, SELA_A_PIN, GPIO_PIN_LOW);
|
||||
HAL_GPIO_WritePin(SELA_B_PORT, SELA_B_PIN, GPIO_PIN_HIGH);
|
||||
}
|
||||
else if(PinNumber == A7)
|
||||
{
|
||||
HAL_GPIO_WritePin(SELA_A_PORT, SELA_A_PIN, GPIO_PIN_HIGH);
|
||||
HAL_GPIO_WritePin(SELA_B_PORT, SELA_B_PIN, GPIO_PIN_HIGH);
|
||||
}
|
||||
}
|
||||
|
||||
void additionalPinsDeinit(uint32_t PinNumber)
|
||||
{
|
||||
if (PinNumber == A4 || PinNumber == A5)
|
||||
{
|
||||
// select A7 via switches selaA, selaB to allow using A4 and A5 as digital outputs
|
||||
HAL_GPIO_WritePin(SELA_A_PORT, SELA_A_PIN, GPIO_PIN_HIGH);
|
||||
HAL_GPIO_WritePin(SELA_B_PORT, SELA_B_PIN, GPIO_PIN_HIGH);
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------- ADC ---------------------- //
|
||||
// determines the ADC channel number by the board pin number
|
||||
uint32_t analogInputToChannelNumber(uint32_t PinNumber)
|
||||
{
|
||||
uint32_t adcChannel = 0;
|
||||
|
||||
// if get a value 0...5 instead of A0...A7
|
||||
if (PinNumber < 4) PinNumber += 14; // A0...A3
|
||||
else if (PinNumber < 6) PinNumber += 16; // A4, A5
|
||||
else if (PinNumber < 8) PinNumber += 18; // A6, A7
|
||||
|
||||
switch (PinNumber)
|
||||
{
|
||||
case PIN_A0:
|
||||
adcChannel = ADC_CHANNEL0;
|
||||
break;
|
||||
case PIN_A1:
|
||||
adcChannel = ADC_CHANNEL1;
|
||||
break;
|
||||
case PIN_A2:
|
||||
adcChannel = ADC_CHANNEL3;
|
||||
break;
|
||||
case PIN_A3:
|
||||
adcChannel = ADC_CHANNEL4;
|
||||
break;
|
||||
case PIN_A4:
|
||||
case PIN_A5:
|
||||
case PIN_A6:
|
||||
case PIN_A7:
|
||||
adcChannel = ADC_CHANNEL5;
|
||||
break;
|
||||
default:
|
||||
adcChannel = NC;
|
||||
}
|
||||
return adcChannel;
|
||||
}
|
||||
|
||||
// ---------------------- PWM ---------------------- //
|
||||
// use only if digitalPinHasPWM() == true
|
||||
#define PWM_PIN_TO_PORT_NUMBER(pin) (((pin==10)||(pin==11)||(pin==12)||(pin==13)) ? 1:0)
|
||||
|
||||
// use only if digitalPinHasPWM() == true
|
||||
// return true if digitalPin configured as pwm
|
||||
__attribute__((noinline, section(".ram_text"))) bool digitalPinPwmIsOn(uint8_t digitalPin)
|
||||
{
|
||||
if (digitalPinHasPWM(digitalPin))
|
||||
{
|
||||
uint8_t config = 0;
|
||||
uint8_t pinShift = PIN_MASK_TO_PIN_NUMBER(digitalPinToBitMask(digitalPin));
|
||||
|
||||
if (PWM_PIN_TO_PORT_NUMBER(digitalPin) == 0)
|
||||
config = PIN_GET_PAD_CONFIG(PORT_0_CFG, pinShift);
|
||||
else
|
||||
config = PIN_GET_PAD_CONFIG(PORT_1_CFG, pinShift);
|
||||
|
||||
if (config == 2)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// function is used only if digitalPinHasPWM() is true
|
||||
TIMER32_TypeDef* pwmPinToTimer(uint32_t digPinNumber)
|
||||
{
|
||||
TIMER32_TypeDef* timerNum = NULL;
|
||||
|
||||
// timer 1
|
||||
if (digPinNumber == 3 || digPinNumber == 5 || digPinNumber == 6 || digPinNumber == 9)
|
||||
timerNum = TIMER32_1;
|
||||
// timer 2
|
||||
else if ((digPinNumber >= 10) && (digPinNumber <= 13))
|
||||
timerNum = TIMER32_2;
|
||||
|
||||
return timerNum;
|
||||
}
|
||||
|
||||
// function is used only if digitalPinHasPWM() is true
|
||||
HAL_TIMER32_CHANNEL_IndexTypeDef pwmPinToTimerChannel(uint32_t digPinNumber)
|
||||
{
|
||||
HAL_TIMER32_CHANNEL_IndexTypeDef channel = 0;
|
||||
if (digPinNumber == 3 || digPinNumber == 12) channel = TIMER32_CHANNEL_0;
|
||||
else if (digPinNumber == 5 || digPinNumber == 11) channel = TIMER32_CHANNEL_1;
|
||||
else if (digPinNumber == 6 || digPinNumber == 13) channel = TIMER32_CHANNEL_2;
|
||||
else if (digPinNumber == 9 || digPinNumber == 10) channel = TIMER32_CHANNEL_3;
|
||||
return channel;
|
||||
}
|
||||
|
||||
// ---------------------- interrupts ---------------------- //
|
||||
// interrupt table is stored in ram to improve performance
|
||||
// index = interrupt number. In each row {digitalPinNumber, IntLineValue, IntMuxValue}
|
||||
uint8_t interruptInfo[EXTERNAL_INTERRUPTS_QTY][3] =
|
||||
{
|
||||
{ 2, GPIO_LINE_2, GPIO_MUX_LINE_2_PORT0_10}, // INT0
|
||||
{ 3, GPIO_LINE_0, GPIO_MUX_LINE_0_PORT0_0}, // INT1
|
||||
{ 4, GPIO_LINE_4, GPIO_MUX_LINE_4_PORT0_8}, // INT2
|
||||
{ 5, GPIO_LINE_1, GPIO_MUX_LINE_1_PORT0_1}, // INT3
|
||||
{ 6, GPIO_LINE_6, GPIO_MUX_LINE_6_PORT0_2}, // INT4
|
||||
{ 8, GPIO_LINE_5, GPIO_MUX_LINE_5_PORT1_9}, // INT5
|
||||
{ 9, GPIO_LINE_3, GPIO_MUX_LINE_3_PORT0_3}, // INT6
|
||||
{ A1, GPIO_LINE_7, GPIO_MUX_LINE_7_PORT1_7}, // INT7
|
||||
};
|
||||
|
||||
int8_t digitalPinToGpioIntMux(uint8_t digPinNumber)
|
||||
{
|
||||
for (uint8_t i = 0; i < EXTERNAL_INTERRUPTS_QTY; i++)
|
||||
{
|
||||
if (interruptInfo[i][0] == digPinNumber)
|
||||
return interruptInfo[i][2];
|
||||
}
|
||||
return NOT_AN_INTERRUPT;
|
||||
}
|
||||
int8_t digitalPinToGpioIntLine(uint8_t digPinNumber)
|
||||
{
|
||||
for (uint8_t i = 0; i < EXTERNAL_INTERRUPTS_QTY; i++)
|
||||
{
|
||||
if (interruptInfo[i][0] == digPinNumber)
|
||||
return interruptInfo[i][1];
|
||||
}
|
||||
return NOT_AN_INTERRUPT;
|
||||
}
|
||||
|
||||
int8_t gpioIntLineToInterrupt(uint32_t gpioIntLine)
|
||||
{
|
||||
for (uint8_t i = 0; i < EXTERNAL_INTERRUPTS_QTY; i++)
|
||||
{
|
||||
if (interruptInfo[i][1] == gpioIntLine)
|
||||
return i;
|
||||
}
|
||||
return NOT_AN_INTERRUPT;
|
||||
}
|
||||
|
||||
int8_t digitalPinToInterrupt(uint32_t digPinNumber)
|
||||
{
|
||||
for (uint8_t i = 0; i < EXTERNAL_INTERRUPTS_QTY; i++)
|
||||
{
|
||||
if (interruptInfo[i][0] == digPinNumber)
|
||||
return i;
|
||||
}
|
||||
return NOT_AN_INTERRUPT;
|
||||
}
|
||||
|
||||
// ---------------------- SPI ---------------------- //
|
||||
void spi_onBegin(uint8_t spiNum)
|
||||
{
|
||||
// On Elbear Nano spi0 needs pin 0.3, spi1 needs pin 1.3 for correct work (NSS_IN).
|
||||
// This pins are connected to board digital pins D9 and D10. There is a seller on each pin which
|
||||
// replace board digital pin from NSS_IN pin to different free pin (NSS_OUT)
|
||||
|
||||
// replace config from NSS_IN to NSS_OUT
|
||||
uint8_t config;
|
||||
if (spiNum == 1)
|
||||
config = PIN_GET_PAD_CONFIG(PORT_1_CFG, PIN_MASK_TO_PIN_NUMBER(SPI1_NSS_IN_PIN));
|
||||
else
|
||||
config = PIN_GET_PAD_CONFIG(PORT_0_CFG, PIN_MASK_TO_PIN_NUMBER(SPI0_NSS_IN_PIN));
|
||||
|
||||
if (config == 0) // common gpio
|
||||
{
|
||||
if (spiNum == 1)
|
||||
{
|
||||
// get info from pin NSS_IN and set config to NSS_OUT
|
||||
HAL_GPIO_PinConfig(SPI1_NSS_OUT_PORT, SPI1_NSS_OUT_PIN, HAL_GPIO_GetPinDirection(SPI1_NSS_IN_PORT, SPI1_NSS_IN_PIN),
|
||||
(HAL_GPIO_PullTypeDef)PIN_GET_PAD_CONFIG(PORT_1_PUPD, PIN_MASK_TO_PIN_NUMBER(SPI1_NSS_IN_PIN)),
|
||||
HAL_GPIO_DS_2MA);
|
||||
HAL_GPIO_WritePin(SPI1_NSS_OUT_PORT, SPI1_NSS_OUT_PIN, (GPIO_PinState)GPIO_GET_PIN_STATE(SPI1_NSS_IN_PORT, PIN_MASK_TO_PIN_NUMBER(SPI1_NSS_IN_PIN)));
|
||||
|
||||
// pin D10 was switched to different gpio and can be used further
|
||||
}
|
||||
else
|
||||
{
|
||||
// get info from pin NSS_IN and set config to NSS_OUT
|
||||
HAL_GPIO_PinConfig(SPI0_NSS_OUT_PORT, SPI0_NSS_OUT_PIN, HAL_GPIO_GetPinDirection(SPI0_NSS_IN_PORT, SPI0_NSS_IN_PIN),
|
||||
(HAL_GPIO_PullTypeDef)PIN_GET_PAD_CONFIG(PORT_0_PUPD, PIN_MASK_TO_PIN_NUMBER(SPI0_NSS_IN_PIN)),
|
||||
HAL_GPIO_DS_2MA);
|
||||
HAL_GPIO_WritePin(SPI0_NSS_OUT_PORT, SPI0_NSS_OUT_PIN, (GPIO_PinState)GPIO_GET_PIN_STATE(SPI0_NSS_IN_PORT, PIN_MASK_TO_PIN_NUMBER(SPI0_NSS_IN_PIN)));
|
||||
// pin D9 was switched to different gpio and can be used further
|
||||
}
|
||||
}
|
||||
else if(config == 2) // timer for pwm
|
||||
{
|
||||
// if spi NSS_IN pin was used as pwm, we need to stop timer, because another pins don't support it
|
||||
if (spiNum == 1)
|
||||
{
|
||||
analogWriteStop(10);
|
||||
ErrorMsgHandler("analogWrite(): D10 cannot be used as PWM pin while SPI is running");
|
||||
}
|
||||
else
|
||||
{
|
||||
analogWriteStop(9);
|
||||
ErrorMsgHandler("analogWrite(): D9 cannot be used as PWM pin while SPI1 is running");
|
||||
}
|
||||
}
|
||||
|
||||
// switch seller to pin NSS_OUT
|
||||
if (spiNum == 1)
|
||||
{
|
||||
HAL_GPIO_PinConfig(SPI1_SWITCH_PORT, SPI1_SWITCH_PIN, HAL_GPIO_MODE_GPIO_OUTPUT, HAL_GPIO_PULL_NONE, HAL_GPIO_DS_2MA);
|
||||
HAL_GPIO_WritePin (SPI1_SWITCH_PORT, SPI1_SWITCH_PIN, GPIO_PIN_HIGH);
|
||||
spi1NssPinIsBlocked = true; // block spi pin
|
||||
}
|
||||
else
|
||||
{
|
||||
HAL_GPIO_PinConfig(SPI0_SWITCH_PORT, SPI0_SWITCH_PIN, HAL_GPIO_MODE_GPIO_OUTPUT, HAL_GPIO_PULL_NONE, HAL_GPIO_DS_2MA);
|
||||
HAL_GPIO_WritePin (SPI0_SWITCH_PORT, SPI0_SWITCH_PIN, GPIO_PIN_HIGH);
|
||||
spi0NssPinIsBlocked = true; // block spi pin
|
||||
}
|
||||
}
|
||||
|
||||
void spi_onEnd(uint8_t spiNum)
|
||||
{
|
||||
// get info from pin NSS_OUT and set config to NSS_IN
|
||||
if (spiNum == 1)
|
||||
{
|
||||
HAL_GPIO_PinConfig(SPI1_NSS_IN_PORT, SPI1_NSS_IN_PIN, HAL_GPIO_GetPinDirection(SPI1_NSS_OUT_PORT, SPI1_NSS_OUT_PIN),
|
||||
(HAL_GPIO_PullTypeDef)PIN_GET_PAD_CONFIG(PORT_1_PUPD, PIN_MASK_TO_PIN_NUMBER(SPI1_NSS_OUT_PIN)), HAL_GPIO_DS_2MA);
|
||||
HAL_GPIO_WritePin (SPI1_NSS_IN_PORT, SPI1_NSS_IN_PIN,
|
||||
(GPIO_PinState)GPIO_GET_PIN_STATE(SPI1_NSS_OUT_PORT, PIN_MASK_TO_PIN_NUMBER(SPI1_NSS_OUT_PIN)));
|
||||
|
||||
// switch seller back to NSS_IN
|
||||
HAL_GPIO_WritePin(SPI1_SWITCH_PORT, SPI1_SWITCH_PIN, GPIO_PIN_LOW);
|
||||
spi1NssPinIsBlocked = false; // unblock spi pin
|
||||
}
|
||||
else
|
||||
{
|
||||
HAL_GPIO_PinConfig(SPI0_NSS_IN_PORT, SPI0_NSS_IN_PIN, HAL_GPIO_GetPinDirection(SPI0_NSS_OUT_PORT, SPI0_NSS_OUT_PIN),
|
||||
(HAL_GPIO_PullTypeDef)PIN_GET_PAD_CONFIG(PORT_1_PUPD, PIN_MASK_TO_PIN_NUMBER(SPI0_NSS_OUT_PIN)), HAL_GPIO_DS_2MA);
|
||||
HAL_GPIO_WritePin (SPI0_NSS_IN_PORT, SPI0_NSS_IN_PIN,
|
||||
(GPIO_PinState)GPIO_GET_PIN_STATE(SPI0_NSS_OUT_PORT, PIN_MASK_TO_PIN_NUMBER(SPI0_NSS_OUT_PIN)));
|
||||
|
||||
// switch seller back to NSS_IN
|
||||
HAL_GPIO_WritePin(SPI0_SWITCH_PORT, SPI0_SWITCH_PIN, GPIO_PIN_LOW);
|
||||
spi0NssPinIsBlocked = false; // unblock spi pin
|
||||
}
|
||||
}
|
||||
@@ -32,6 +32,13 @@ extern "C" {
|
||||
#include "mik32_hal_gpio.h"
|
||||
#include "mik32_hal_timer32.h"
|
||||
|
||||
// total number of pins available for initialization
|
||||
#define PINS_COMMON_QTY 24
|
||||
#define pinCommonQty() (PINS_COMMON_QTY)
|
||||
|
||||
extern bool spi0NssPinIsBlocked;
|
||||
extern bool spi1NssPinIsBlocked;
|
||||
|
||||
// analog pins
|
||||
#define PIN_A0 (14)
|
||||
#define PIN_A1 (15)
|
||||
@@ -47,7 +54,6 @@ static const uint8_t A3 = PIN_A3;
|
||||
static const uint8_t A4 = PIN_A4;
|
||||
static const uint8_t A5 = PIN_A5;
|
||||
|
||||
|
||||
// digital pins
|
||||
// D0...D13, D18, D19
|
||||
|
||||
@@ -55,18 +61,17 @@ static const uint8_t A5 = PIN_A5;
|
||||
#define LED_BUILTIN (22)
|
||||
#define BTN_BUILTIN (23)
|
||||
|
||||
// determines the address of the port by the board pin number to which this pin belongs on the MCU
|
||||
GPIO_TypeDef* digitalPinToPort(uint32_t digPinNumber);
|
||||
// determines the port address by the board pin number to which this pin belongs on the MCU
|
||||
GPIO_TypeDef* digitalPinToPort(uint32_t digPinNumber);
|
||||
// determines the pin address inside the port by the board pin number
|
||||
HAL_PinsTypeDef digitalPinToBitMask(uint32_t digPinNumber);
|
||||
// total number of pins available for initialization
|
||||
uint16_t pinCommonQty(void);
|
||||
// the function returns a reference to the OUTPUT address of the GPIO register
|
||||
volatile uint32_t* portOutputRegister(GPIO_TypeDef* GPIO_x);
|
||||
// the function returns a reference to the STATE address of the GPIO register
|
||||
volatile uint32_t* portInputRegister(GPIO_TypeDef* GPIO_x);
|
||||
// the function initializes additional MCU pins depending on the specified pin number
|
||||
void additionalPinsInit(uint32_t PinNumber);
|
||||
static inline void additionalPinsDeinit(uint32_t PinNumber){}
|
||||
|
||||
// UART
|
||||
// available uarts quantity
|
||||
@@ -78,31 +83,51 @@ void additionalPinsInit(uint32_t PinNumber);
|
||||
uint32_t analogInputToChannelNumber(uint32_t PinNumber);
|
||||
|
||||
// PWM
|
||||
bool digitalPinHasPWM(uint8_t p);
|
||||
bool digitalPinPwmIsOn(uint8_t digitalPin); // use only if digitalPinHasPWM() == true
|
||||
#define PWM_FREQUENCY_MAX 1000000 // Hz
|
||||
static inline __attribute__((always_inline)) bool digitalPinHasPWM(uint8_t p)
|
||||
{
|
||||
// if spi is in use D9 or D10 cannot work as pwm
|
||||
if (p == 3 || p == 5 || p == 6 || (p >= 11 && p <= 13) || ((p == 9) && !spi0NssPinIsBlocked) || ((p == 10) && !spi1NssPinIsBlocked))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
// return true if digitalPin configured as pwm
|
||||
bool digitalPinPwmIsOn(uint8_t digitalPin);
|
||||
// determines which timer the pin belongs to
|
||||
TIMER32_TypeDef* pwmPinToTimer(uint32_t digPinNumber);
|
||||
// determines which timer channel the pin belongs to
|
||||
HAL_TIMER32_CHANNEL_IndexTypeDef pwmPinToTimerChannel(uint32_t digPinNumber);
|
||||
|
||||
// SPI
|
||||
#define PIN_SPI_SS (10)
|
||||
#define PIN_SPI_MOSI (11)
|
||||
#define PIN_SPI_MISO (12)
|
||||
#define PIN_SPI_SCK (13)
|
||||
#define SPI_COMMON_QTY 2
|
||||
|
||||
#define PIN_SPI_SS 10
|
||||
#define PIN_SPI_MOSI 11
|
||||
#define PIN_SPI_MISO 12
|
||||
#define PIN_SPI_SCK 13
|
||||
static const uint8_t SS = PIN_SPI_SS;
|
||||
static const uint8_t MOSI = PIN_SPI_MOSI;
|
||||
static const uint8_t MISO = PIN_SPI_MISO;
|
||||
static const uint8_t SCK = PIN_SPI_SCK;
|
||||
|
||||
#define PIN_SPI1_SS 9
|
||||
#define PIN_SPI1_MOSI 5
|
||||
#define PIN_SPI1_MISO 3
|
||||
#define PIN_SPI1_SCK 6
|
||||
static const uint8_t SS1 = PIN_SPI1_SS;
|
||||
static const uint8_t MOSI1 = PIN_SPI1_MOSI;
|
||||
static const uint8_t MISO1 = PIN_SPI1_MISO;
|
||||
static const uint8_t SCK1 = PIN_SPI1_SCK;
|
||||
|
||||
// config SEL_NSS1 to replace D10 to different controller pin,
|
||||
// because pin 1.3 which is D10 by default is needed to spi
|
||||
void spi_onBegin(void);
|
||||
void spi_onEnd(void);
|
||||
// because NSS which is D9/D10 by default is needed to spi
|
||||
void spi_onBegin(uint8_t spiNum);
|
||||
void spi_onEnd(uint8_t spiNum);
|
||||
|
||||
// I2C
|
||||
#define PIN_WIRE_SDA (18)
|
||||
#define PIN_WIRE_SCL (19)
|
||||
#define I2C_NUM (1) // i2c number 1
|
||||
#define PIN_WIRE_SDA (18)
|
||||
#define PIN_WIRE_SCL (19)
|
||||
#define I2C_COMMON_QTY (1)
|
||||
static const uint8_t SDA = PIN_WIRE_SDA;
|
||||
static const uint8_t SCL = PIN_WIRE_SCL;
|
||||
// available frequencies
|
||||
|
||||
@@ -16,10 +16,25 @@
|
||||
#include "wiring_analog.h"
|
||||
#include "wiring_LL.h"
|
||||
|
||||
bool spiNssPinIsBlocked = false;
|
||||
#define SPI0_SWITCH_PORT GPIO_1
|
||||
#define SPI0_SWITCH_PIN GPIO_PIN_10
|
||||
#define SPI0_NSS_IN_PORT GPIO_0
|
||||
#define SPI0_NSS_IN_PIN GPIO_PIN_3
|
||||
#define SPI0_NSS_OUT_PORT GPIO_1
|
||||
#define SPI0_NSS_OUT_PIN GPIO_PIN_14
|
||||
|
||||
#define SPI1_SWITCH_PORT GPIO_1
|
||||
#define SPI1_SWITCH_PIN GPIO_PIN_6
|
||||
#define SPI1_NSS_IN_PORT GPIO_1
|
||||
#define SPI1_NSS_IN_PIN GPIO_PIN_3
|
||||
#define SPI1_NSS_OUT_PORT GPIO_1
|
||||
#define SPI1_NSS_OUT_PIN GPIO_PIN_4
|
||||
|
||||
bool spi0NssPinIsBlocked = false;
|
||||
bool spi1NssPinIsBlocked = false;
|
||||
|
||||
// list of pin numbers from both ports with pins inside the port
|
||||
const HAL_PinsTypeDef digitalPinToGpioPinArray[] =
|
||||
const HAL_PinsTypeDef digitalPinToGpioPinArray[PINS_COMMON_QTY] =
|
||||
{
|
||||
GPIO_PIN_5, // D0
|
||||
GPIO_PIN_6, // D1
|
||||
@@ -47,45 +62,43 @@ const HAL_PinsTypeDef digitalPinToGpioPinArray[] =
|
||||
GPIO_PIN_6 // BTN(pin 23)
|
||||
};
|
||||
|
||||
// etermines the address of the port by the board pin number to which this pin belongs on the MCU
|
||||
GPIO_TypeDef* digitalPinToPort(uint32_t digPinNumber)
|
||||
// determines the address of the port by the board pin number to which this pin belongs on the MCU
|
||||
__attribute__((noinline, section(".ram_text"))) GPIO_TypeDef* digitalPinToPort(uint32_t digPinNumber)
|
||||
{
|
||||
GPIO_TypeDef* gpioNum = 0;
|
||||
// port 0 - board pins 0...6, 9, 16(A2), 17(A3), 20(A4), 21(A5)
|
||||
if ((digPinNumber >= 0 && digPinNumber <= 6) || digPinNumber == 9 || digPinNumber == 16
|
||||
|| digPinNumber == 17 || digPinNumber == 20 || digPinNumber == 21) // 12 pieces
|
||||
gpioNum = GPIO_0;
|
||||
if (digPinNumber >= pinCommonQty())
|
||||
{
|
||||
ErrorMsgHandler("digitalPinToPort(): pin number exceeds the total number of pins");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// port 2 - led and button
|
||||
if (digPinNumber == LED_BUILTIN || digPinNumber == BTN_BUILTIN)
|
||||
return GPIO_2;
|
||||
// port 1 - board pins 7, 8, 10...13, 14(А0), 15(А1), 18,19
|
||||
else if (digPinNumber == 7 || digPinNumber == 8 || (digPinNumber >= 10 && digPinNumber <= 15)
|
||||
|| digPinNumber == 18 || digPinNumber == 19) // 10 pieces
|
||||
gpioNum = GPIO_1;
|
||||
// port 2 - led and button
|
||||
else if (digPinNumber == LED_BUILTIN || digPinNumber == BTN_BUILTIN)
|
||||
gpioNum = GPIO_2;
|
||||
|
||||
return gpioNum;
|
||||
}
|
||||
|| digPinNumber == 18 || digPinNumber == 19 || (digPinNumber == 9 && spi0NssPinIsBlocked)) // 10 pieces
|
||||
return GPIO_1;
|
||||
// port 0 - board pins 0...6, 9, 16(A2), 17(A3), 20(A4), 21(A5)
|
||||
else
|
||||
return GPIO_0;
|
||||
}
|
||||
|
||||
// determines the pin address inside the port by the board pin number
|
||||
HAL_PinsTypeDef digitalPinToBitMask(uint32_t digPinNumber)
|
||||
__attribute__((noinline, section(".ram_text"))) HAL_PinsTypeDef digitalPinToBitMask(uint32_t digPinNumber)
|
||||
{
|
||||
if (digPinNumber < pinCommonQty())
|
||||
if (digPinNumber >= pinCommonQty())
|
||||
{
|
||||
HAL_PinsTypeDef mask;
|
||||
// if spi is on default pin 1.3 is needed for spi, D10 is replaced to pin 1.4
|
||||
if (spiNssPinIsBlocked && (digPinNumber == 10))
|
||||
mask = GPIO_PIN_4;
|
||||
else
|
||||
mask = digitalPinToGpioPinArray[digPinNumber];
|
||||
return mask;
|
||||
ErrorMsgHandler("digitalPinToBitMask(): pin number exceeds the total number of pins");
|
||||
return NOT_A_PIN;
|
||||
}
|
||||
else
|
||||
return NC;
|
||||
}
|
||||
|
||||
uint16_t pinCommonQty(void)
|
||||
{
|
||||
return (uint16_t)(sizeof(digitalPinToGpioPinArray)/sizeof(digitalPinToGpioPinArray[0]));
|
||||
// if spi is on default pin NSS_IN is needed for spi, board pin is replaced to pin NSS_OUT
|
||||
if ((digPinNumber == 10) && spi1NssPinIsBlocked)
|
||||
return SPI1_NSS_OUT_PIN;
|
||||
else if ((digPinNumber == 9) && spi0NssPinIsBlocked)
|
||||
return SPI0_NSS_OUT_PIN;
|
||||
else
|
||||
return digitalPinToGpioPinArray[digPinNumber];
|
||||
}
|
||||
|
||||
// the function returns a reference to the OUTPUT address of the GPIO register
|
||||
@@ -101,20 +114,22 @@ volatile uint32_t* portInputRegister(GPIO_TypeDef* GPIO_x)
|
||||
}
|
||||
|
||||
// the function initializes additional MCU pins depending on the specified pin number
|
||||
#define SELA45_PORT GPIO_1
|
||||
#define SELA45_PIN GPIO_PIN_15
|
||||
static bool selaSwitchIsInited = false;
|
||||
void additionalPinsInit(uint32_t PinNumber)
|
||||
{
|
||||
// if we use pin A5, we need to set SELA45 (1.15) to 1 to switch the output from A4 to A5
|
||||
if (!selaSwitchIsInited)
|
||||
{
|
||||
HAL_GPIO_PinConfig(SELA45_PORT, SELA45_PIN, HAL_GPIO_MODE_GPIO_OUTPUT, HAL_GPIO_PULL_NONE, HAL_GPIO_DS_2MA);
|
||||
selaSwitchIsInited = true;
|
||||
}
|
||||
// if we use pin A5, we need to set SELA45 to 1 to switch the output from A4 to A5
|
||||
if (PinNumber == A5)
|
||||
{
|
||||
HAL_GPIO_PinConfig(GPIO_1, GPIO_PIN_15, HAL_GPIO_MODE_GPIO_OUTPUT, HAL_GPIO_PULL_NONE, HAL_GPIO_DS_2MA);
|
||||
HAL_GPIO_WritePin(GPIO_1, GPIO_PIN_15, GPIO_PIN_HIGH);
|
||||
}
|
||||
HAL_GPIO_WritePin(SELA45_PORT, SELA45_PIN, GPIO_PIN_HIGH);
|
||||
else if(PinNumber == A4)
|
||||
{
|
||||
// return the switch to A4 in case A5 was previously read
|
||||
HAL_GPIO_PinConfig(GPIO_1, GPIO_PIN_15, HAL_GPIO_MODE_GPIO_OUTPUT, HAL_GPIO_PULL_NONE, HAL_GPIO_DS_2MA);
|
||||
HAL_GPIO_WritePin(GPIO_1, GPIO_PIN_15, GPIO_PIN_LOW);
|
||||
}
|
||||
HAL_GPIO_WritePin(SELA45_PORT, SELA45_PIN, GPIO_PIN_LOW);
|
||||
}
|
||||
|
||||
// ---------------------- ADC ---------------------- //
|
||||
@@ -155,33 +170,23 @@ uint32_t analogInputToChannelNumber(uint32_t PinNumber)
|
||||
// use only if digitalPinHasPWM() == true
|
||||
#define PWM_PIN_TO_PORT_NUMBER(pin) (((pin==10)||(pin==11)||(pin==12)||(pin==13)) ? 1:0)
|
||||
|
||||
// use only if digitalPinHasPWM() == true
|
||||
// return true if digitalPin configured as pwm
|
||||
bool digitalPinPwmIsOn(uint8_t digitalPin)
|
||||
__attribute__((noinline, section(".ram_text"))) bool digitalPinPwmIsOn(uint8_t digitalPin)
|
||||
{
|
||||
uint8_t config = 0;
|
||||
uint8_t pinShift = PIN_MASK_TO_PIN_NUMBER(digitalPinToBitMask(digitalPin));
|
||||
if (digitalPinHasPWM(digitalPin))
|
||||
{
|
||||
uint8_t config = 0;
|
||||
uint8_t pinShift = PIN_MASK_TO_PIN_NUMBER(digitalPinToBitMask(digitalPin));
|
||||
|
||||
if (PWM_PIN_TO_PORT_NUMBER(digitalPin) == 0)
|
||||
config = PIN_GET_PAD_CONFIG(PORT_0_CFG, pinShift);
|
||||
else
|
||||
config = PIN_GET_PAD_CONFIG(PORT_1_CFG, pinShift);
|
||||
if (PWM_PIN_TO_PORT_NUMBER(digitalPin) == 0)
|
||||
config = PIN_GET_PAD_CONFIG(PORT_0_CFG, pinShift);
|
||||
else
|
||||
config = PIN_GET_PAD_CONFIG(PORT_1_CFG, pinShift);
|
||||
|
||||
if (config == 2)
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
bool digitalPinHasPWM(uint8_t p)
|
||||
{
|
||||
bool ret = false;
|
||||
// if spi is in use D10 cannot work as pwm
|
||||
if (spiNssPinIsBlocked && (p == 10))
|
||||
ret = false;
|
||||
else if (p == 3 || p == 5 || p == 6 || (p >= 9 && p <= 13))
|
||||
ret = true;
|
||||
return ret;
|
||||
if (config == 2)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// function is used only if digitalPinHasPWM() is true
|
||||
@@ -266,44 +271,94 @@ int8_t digitalPinToInterrupt(uint32_t digPinNumber)
|
||||
}
|
||||
|
||||
// ---------------------- SPI ---------------------- //
|
||||
void spi_onBegin(void)
|
||||
void spi_onBegin(uint8_t spiNum)
|
||||
{
|
||||
// On Elbear Ace-Uno rev1.1.0 there is a seller on pin 1.6 which replace D10 from spi NSS pin 1.3 to pin 1.4,
|
||||
// because spi needs pin 1.3 for correct work
|
||||
// On Elbear Ace-Uno rev1.1.0 spi0 needs pin 0.3, spi1 needs pin 1.3 for correct work (NSS_IN).
|
||||
// This pins are connected to board digital pins D9 and D10. There is a seller on each pin which
|
||||
// replace board digital pin from NSS_IN pin to different free pin (NSS_OUT)
|
||||
|
||||
// replace config from NSS_IN to NSS_OUT
|
||||
uint8_t config;
|
||||
if (spiNum == 1)
|
||||
config = PIN_GET_PAD_CONFIG(PORT_1_CFG, PIN_MASK_TO_PIN_NUMBER(SPI1_NSS_IN_PIN));
|
||||
else
|
||||
config = PIN_GET_PAD_CONFIG(PORT_0_CFG, PIN_MASK_TO_PIN_NUMBER(SPI0_NSS_IN_PIN));
|
||||
|
||||
// replace config from 1.3 to 1.4
|
||||
uint8_t config = PIN_GET_PAD_CONFIG(PORT_1_CFG, PIN_MASK_TO_PIN_NUMBER(GPIO_PIN_3));
|
||||
if (config == 0) // common gpio
|
||||
{
|
||||
// get info from pin gpio1.3 and set config to gpio1.4
|
||||
HAL_GPIO_PinConfig(GPIO_1, GPIO_PIN_4, HAL_GPIO_GetPinDirection(GPIO_1, GPIO_PIN_3),
|
||||
(HAL_GPIO_PullTypeDef)PIN_GET_PAD_CONFIG(PORT_1_PUPD, PIN_MASK_TO_PIN_NUMBER(GPIO_PIN_3)),
|
||||
HAL_GPIO_DS_2MA);
|
||||
HAL_GPIO_WritePin(GPIO_1, GPIO_PIN_4, (GPIO_PinState)GPIO_GET_PIN_STATE(GPIO_1, PIN_MASK_TO_PIN_NUMBER(GPIO_PIN_3)));
|
||||
|
||||
// pin D10 was switched to different gpio and can be used further
|
||||
if (spiNum == 1)
|
||||
{
|
||||
// get info from pin NSS_IN and set config to NSS_OUT
|
||||
HAL_GPIO_PinConfig(SPI1_NSS_OUT_PORT, SPI1_NSS_OUT_PIN, HAL_GPIO_GetPinDirection(SPI1_NSS_IN_PORT, SPI1_NSS_IN_PIN),
|
||||
(HAL_GPIO_PullTypeDef)PIN_GET_PAD_CONFIG(PORT_1_PUPD, PIN_MASK_TO_PIN_NUMBER(SPI1_NSS_IN_PIN)),
|
||||
HAL_GPIO_DS_2MA);
|
||||
HAL_GPIO_WritePin(SPI1_NSS_OUT_PORT, SPI1_NSS_OUT_PIN, (GPIO_PinState)GPIO_GET_PIN_STATE(SPI1_NSS_IN_PORT, PIN_MASK_TO_PIN_NUMBER(SPI1_NSS_IN_PIN)));
|
||||
|
||||
// pin D10 was switched to different gpio and can be used further
|
||||
}
|
||||
else
|
||||
{
|
||||
// get info from pin NSS_IN and set config to NSS_OUT
|
||||
HAL_GPIO_PinConfig(SPI0_NSS_OUT_PORT, SPI0_NSS_OUT_PIN, HAL_GPIO_GetPinDirection(SPI0_NSS_IN_PORT, SPI0_NSS_IN_PIN),
|
||||
(HAL_GPIO_PullTypeDef)PIN_GET_PAD_CONFIG(PORT_0_PUPD, PIN_MASK_TO_PIN_NUMBER(SPI0_NSS_IN_PIN)),
|
||||
HAL_GPIO_DS_2MA);
|
||||
HAL_GPIO_WritePin(SPI0_NSS_OUT_PORT, SPI0_NSS_OUT_PIN, (GPIO_PinState)GPIO_GET_PIN_STATE(SPI0_NSS_IN_PORT, PIN_MASK_TO_PIN_NUMBER(SPI0_NSS_IN_PIN)));
|
||||
// pin D9 was switched to different gpio and can be used further
|
||||
}
|
||||
}
|
||||
else if(config == 2) // timer for pwm
|
||||
{
|
||||
// if D10 (spi NSS pin) was used as pwm, we need to stop timer, because 1.4 don't support it
|
||||
analogWriteStop(10);
|
||||
ErrorMsgHandler("analogWrite(): D10 cannot be used as PWM pin while SPI is running");
|
||||
// if spi NSS_IN pin was used as pwm, we need to stop timer, because another pins don't support it
|
||||
if (spiNum == 1)
|
||||
{
|
||||
analogWriteStop(10);
|
||||
ErrorMsgHandler("analogWrite(): D10 cannot be used as PWM pin while SPI is running");
|
||||
}
|
||||
else
|
||||
{
|
||||
analogWriteStop(9);
|
||||
ErrorMsgHandler("analogWrite(): D9 cannot be used as PWM pin while SPI1 is running");
|
||||
}
|
||||
}
|
||||
|
||||
// switch seller to pin 1.4
|
||||
HAL_GPIO_PinConfig(GPIO_1, GPIO_PIN_6, HAL_GPIO_MODE_GPIO_OUTPUT, HAL_GPIO_PULL_NONE, HAL_GPIO_DS_2MA);
|
||||
HAL_GPIO_WritePin(GPIO_1, GPIO_PIN_6, GPIO_PIN_HIGH);
|
||||
spiNssPinIsBlocked = true; // block spi pin
|
||||
// switch seller to pin NSS_OUT
|
||||
if (spiNum == 1)
|
||||
{
|
||||
HAL_GPIO_PinConfig(SPI1_SWITCH_PORT, SPI1_SWITCH_PIN, HAL_GPIO_MODE_GPIO_OUTPUT, HAL_GPIO_PULL_NONE, HAL_GPIO_DS_2MA);
|
||||
HAL_GPIO_WritePin (SPI1_SWITCH_PORT, SPI1_SWITCH_PIN, GPIO_PIN_HIGH);
|
||||
spi1NssPinIsBlocked = true; // block spi pin
|
||||
}
|
||||
else
|
||||
{
|
||||
HAL_GPIO_PinConfig(SPI0_SWITCH_PORT, SPI0_SWITCH_PIN, HAL_GPIO_MODE_GPIO_OUTPUT, HAL_GPIO_PULL_NONE, HAL_GPIO_DS_2MA);
|
||||
HAL_GPIO_WritePin (SPI0_SWITCH_PORT, SPI0_SWITCH_PIN, GPIO_PIN_HIGH);
|
||||
spi0NssPinIsBlocked = true; // block spi pin
|
||||
}
|
||||
}
|
||||
|
||||
void spi_onEnd(void)
|
||||
void spi_onEnd(uint8_t spiNum)
|
||||
{
|
||||
// get info from pin gpio1.4 and set config to gpio1.3
|
||||
HAL_GPIO_PinConfig(GPIO_1, GPIO_PIN_3, HAL_GPIO_GetPinDirection(GPIO_1, GPIO_PIN_4),
|
||||
(HAL_GPIO_PullTypeDef)PIN_GET_PAD_CONFIG(PORT_1_PUPD, PIN_MASK_TO_PIN_NUMBER(GPIO_PIN_4)), HAL_GPIO_DS_2MA);
|
||||
HAL_GPIO_WritePin(GPIO_1, GPIO_PIN_3, (GPIO_PinState)GPIO_GET_PIN_STATE(GPIO_1, PIN_MASK_TO_PIN_NUMBER(GPIO_PIN_4)));
|
||||
// get info from pin NSS_OUT and set config to NSS_IN
|
||||
if (spiNum == 1)
|
||||
{
|
||||
HAL_GPIO_PinConfig(SPI1_NSS_IN_PORT, SPI1_NSS_IN_PIN, HAL_GPIO_GetPinDirection(SPI1_NSS_OUT_PORT, SPI1_NSS_OUT_PIN),
|
||||
(HAL_GPIO_PullTypeDef)PIN_GET_PAD_CONFIG(PORT_1_PUPD, PIN_MASK_TO_PIN_NUMBER(SPI1_NSS_OUT_PIN)), HAL_GPIO_DS_2MA);
|
||||
HAL_GPIO_WritePin (SPI1_NSS_IN_PORT, SPI1_NSS_IN_PIN,
|
||||
(GPIO_PinState)GPIO_GET_PIN_STATE(SPI1_NSS_OUT_PORT, PIN_MASK_TO_PIN_NUMBER(SPI1_NSS_OUT_PIN)));
|
||||
|
||||
// switch seller back to pin 1.3
|
||||
HAL_GPIO_WritePin(GPIO_1, GPIO_PIN_6, GPIO_PIN_LOW);
|
||||
spiNssPinIsBlocked = false; // unblock spi pin
|
||||
// switch seller back to NSS_IN
|
||||
HAL_GPIO_WritePin(SPI1_SWITCH_PORT, SPI1_SWITCH_PIN, GPIO_PIN_LOW);
|
||||
spi1NssPinIsBlocked = false; // unblock spi pin
|
||||
}
|
||||
else
|
||||
{
|
||||
HAL_GPIO_PinConfig(SPI0_NSS_IN_PORT, SPI0_NSS_IN_PIN, HAL_GPIO_GetPinDirection(SPI0_NSS_OUT_PORT, SPI0_NSS_OUT_PIN),
|
||||
(HAL_GPIO_PullTypeDef)PIN_GET_PAD_CONFIG(PORT_1_PUPD, PIN_MASK_TO_PIN_NUMBER(SPI0_NSS_OUT_PIN)), HAL_GPIO_DS_2MA);
|
||||
HAL_GPIO_WritePin (SPI0_NSS_IN_PORT, SPI0_NSS_IN_PIN,
|
||||
(GPIO_PinState)GPIO_GET_PIN_STATE(SPI0_NSS_OUT_PORT, PIN_MASK_TO_PIN_NUMBER(SPI0_NSS_OUT_PIN)));
|
||||
|
||||
// switch seller back to NSS_IN
|
||||
HAL_GPIO_WritePin(SPI0_SWITCH_PORT, SPI0_SWITCH_PIN, GPIO_PIN_LOW);
|
||||
spi0NssPinIsBlocked = false; // unblock spi pin
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,196 @@
|
||||
/*
|
||||
pins_arduino.h - Pin definition functions for Arduino
|
||||
Part of Arduino - http://www.arduino.cc/
|
||||
|
||||
Copyright (c) 2007 David A. Mellis
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General
|
||||
Public License along with this library; if not, write to the
|
||||
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
|
||||
Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
#ifndef Pins_Arduino_h
|
||||
#define Pins_Arduino_h
|
||||
|
||||
#include "wiring_constants.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "mik32_hal_gpio.h"
|
||||
#include "mik32_hal_timer32.h"
|
||||
|
||||
// digital pins
|
||||
typedef enum
|
||||
{
|
||||
P0_0 = 0, // 0
|
||||
P0_1, // 1
|
||||
P0_2, // 2
|
||||
P0_3, // 3
|
||||
P0_4, // 4
|
||||
P0_5, // 5
|
||||
P0_6, // 6
|
||||
P0_7, // 7
|
||||
P0_8, // 8
|
||||
P0_9, // 9
|
||||
P0_10, // 10
|
||||
P0_11, // 11 // TDI
|
||||
P0_12, // 12 // TCK
|
||||
P0_13, // 13 // TMS
|
||||
P0_14, // 14 // TRST
|
||||
P0_15, // 15 // TDO
|
||||
P1_0, // 16
|
||||
P1_1, // 17
|
||||
P1_2, // 18
|
||||
P1_3, // 19
|
||||
P1_4, // 20
|
||||
P1_5, // 21
|
||||
P1_6, // 22
|
||||
P1_7, // 23
|
||||
P1_8, // 24
|
||||
P1_9, // 25
|
||||
P1_10, // 26
|
||||
P1_11, // 27
|
||||
P1_12, // 28
|
||||
P1_13, // 29
|
||||
P1_14, // 30
|
||||
P1_15, // 31
|
||||
P2_6, // 32
|
||||
P2_7, // 33
|
||||
PINS_COMMON_QTY, // 34
|
||||
} DigitalPinsTypeDef;
|
||||
// total number of pins available for initialization
|
||||
#define pinCommonQty() (PINS_COMMON_QTY)
|
||||
|
||||
// analog pins
|
||||
#define PIN_A0 (P1_5)
|
||||
#define PIN_A1 (P1_7)
|
||||
#define PIN_A2 (P0_2)
|
||||
#define PIN_A3 (P0_4)
|
||||
#define PIN_A4 (P0_7)
|
||||
#define PIN_A5 (P0_9)
|
||||
#define PIN_A6 (P0_11)
|
||||
#define PIN_A7 (P0_13)
|
||||
|
||||
static const uint8_t A0 = PIN_A0;
|
||||
static const uint8_t A1 = PIN_A1;
|
||||
static const uint8_t A2 = PIN_A2;
|
||||
static const uint8_t A3 = PIN_A3;
|
||||
static const uint8_t A4 = PIN_A4;
|
||||
static const uint8_t A5 = PIN_A5;
|
||||
static const uint8_t A6 = PIN_A6;
|
||||
static const uint8_t A7 = PIN_A7;
|
||||
|
||||
|
||||
// determines the address of the port by the board pin number to which this pin belongs on the MCU
|
||||
GPIO_TypeDef* digitalPinToPort(uint32_t digPinNumber);
|
||||
// determines the pin address inside the port by the board pin number
|
||||
HAL_PinsTypeDef digitalPinToBitMask(uint32_t digitalPinNumber);
|
||||
// the function returns a reference to the OUTPUT address of the GPIO register
|
||||
volatile uint32_t* portOutputRegister(GPIO_TypeDef* GPIO_x);
|
||||
// the function returns a reference to the STATE address of the GPIO register
|
||||
volatile uint32_t* portInputRegister(GPIO_TypeDef* GPIO_x);
|
||||
// functions are needed for compatibility with other boards
|
||||
static inline void additionalPinsInit(uint32_t PinNumber) {}
|
||||
static inline void additionalPinsDeinit(uint32_t PinNumber){}
|
||||
|
||||
// UART
|
||||
// available uarts quantity
|
||||
#define SERIAL_PORT_QTY 2
|
||||
|
||||
// ADC
|
||||
#define MCU_ADC_RESOLUTION 12 // bits
|
||||
// determines the ADC channel number by the board pin number
|
||||
uint32_t analogInputToChannelNumber(uint32_t PinNumber);
|
||||
|
||||
// PWM
|
||||
#define PWM_FREQUENCY_MAX 1000000 // Hz
|
||||
static inline __attribute__((always_inline)) bool digitalPinHasPWM(uint8_t p)
|
||||
{
|
||||
return (p <= P1_15) && ((p & 0xF) < 4);
|
||||
}
|
||||
// return true if digitalPin configured as pwm
|
||||
bool digitalPinPwmIsOn(uint8_t digitalPin);
|
||||
// determines which timer the pin belongs to
|
||||
TIMER32_TypeDef* pwmPinToTimer(uint32_t digPinNumber);
|
||||
// determines which timer channel the pin belongs to
|
||||
HAL_TIMER32_CHANNEL_IndexTypeDef pwmPinToTimerChannel(uint32_t digPinNumber);
|
||||
|
||||
// SPI
|
||||
#define SPI_COMMON_QTY 2
|
||||
|
||||
#define PIN_SPI_SS P1_3
|
||||
#define PIN_SPI_MOSI P1_1
|
||||
#define PIN_SPI_MISO P1_0
|
||||
#define PIN_SPI_SCK P1_2
|
||||
static const uint8_t SS = PIN_SPI_SS;
|
||||
static const uint8_t MOSI = PIN_SPI_MOSI;
|
||||
static const uint8_t MISO = PIN_SPI_MISO;
|
||||
static const uint8_t SCK = PIN_SPI_SCK;
|
||||
|
||||
#define PIN_SPI1_SS P0_3
|
||||
#define PIN_SPI1_MOSI P0_1
|
||||
#define PIN_SPI1_MISO P0_0
|
||||
#define PIN_SPI1_SCK P0_2
|
||||
static const uint8_t SS1 = PIN_SPI1_SS;
|
||||
static const uint8_t MOSI1 = PIN_SPI1_MOSI;
|
||||
static const uint8_t MISO1 = PIN_SPI1_MISO;
|
||||
static const uint8_t SCK1 = PIN_SPI1_SCK;
|
||||
|
||||
// check if pwm is used on spi pins
|
||||
void spi_onBegin(uint8_t spiNum);
|
||||
static inline void spi_onEnd(uint8_t spiNum){}
|
||||
|
||||
// I2C
|
||||
#define I2C_COMMON_QTY 2
|
||||
// Wire (I2C1)
|
||||
#define PIN_WIRE_SDA P1_12
|
||||
#define PIN_WIRE_SCL P1_13
|
||||
static const uint8_t SDA = PIN_WIRE_SDA;
|
||||
static const uint8_t SCL = PIN_WIRE_SCL;
|
||||
// Wire1 (I2C0)
|
||||
#define PIN_WIRE_SDA1 P0_9
|
||||
#define PIN_WIRE_SCL1 P0_10
|
||||
static const uint8_t SDA1 = PIN_WIRE_SDA1;
|
||||
static const uint8_t SCL1 = PIN_WIRE_SCL1;
|
||||
// available frequencies
|
||||
#define WIRE_FREQ_100K 100000
|
||||
#define WIRE_FREQ_400K 400000
|
||||
#define WIRE_FREQ_1000K 1000000
|
||||
|
||||
// interrupts
|
||||
#define EXTERNAL_INTERRUPTS_QTY 8
|
||||
extern uint8_t interruptInfo[EXTERNAL_INTERRUPTS_QTY][3];
|
||||
// determines the board pin number by interrupt number
|
||||
#define interruptToDigitalPin(interruptNum) (interruptInfo[interruptNum][0])
|
||||
// determines gpio interrupt line by interrupt number
|
||||
#define interruptToGpioIntLine(interruptNum) ((uint8_t)interruptInfo[interruptNum][1])
|
||||
// determines gpio interrupt mux by interrupt number
|
||||
#define interruptToGpioIntMux(interruptNum) ((uint8_t)interruptInfo[interruptNum][2])
|
||||
// determines interrupt number by the board pin number
|
||||
int8_t digitalPinToInterrupt(uint32_t digPinNumber);
|
||||
// determines interrupt number by the gpio interrupt line
|
||||
int8_t gpioIntLineToInterrupt(uint32_t gpioIntLine);
|
||||
// determines gpio interrupt mux by the board pin number
|
||||
int8_t digitalPinToGpioIntMux(uint8_t digPinNumber);
|
||||
// determines gpio interrupt line by the board pin number
|
||||
int8_t digitalPinToGpioIntLine(uint8_t digPinNumber);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,240 @@
|
||||
/**
|
||||
*******************************************************************************
|
||||
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This software component is licensed by WCH under BSD 3-Clause license,
|
||||
* the "License"; You may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at:
|
||||
* opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
*******************************************************************************
|
||||
*/
|
||||
|
||||
#include "pins_arduino.h"
|
||||
#include "mik32_hal_adc.h"
|
||||
#include "wiring_analog.h"
|
||||
#include "wiring_LL.h"
|
||||
|
||||
/**
|
||||
* @brief Determines the address of the port by the board pin number to which this pin belongs on the MCU
|
||||
* @return The address of the port corresponding to the board pin number. Can return 0 if the pin not exists
|
||||
*/
|
||||
|
||||
__attribute__((noinline, section(".ram_text"))) GPIO_TypeDef *digitalPinToPort(uint32_t digitalPinNumber)
|
||||
{
|
||||
if (digitalPinNumber >= pinCommonQty())
|
||||
{
|
||||
ErrorMsgHandler("digitalPinToPort(): pin number exceeds the total number of pins");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (digitalPinNumber <= P0_15)
|
||||
return GPIO_0;
|
||||
else if ((digitalPinNumber >= P1_0) && (digitalPinNumber <= P1_15))
|
||||
return GPIO_1;
|
||||
else if ((digitalPinNumber >= P2_6) && (digitalPinNumber < PINS_COMMON_QTY))
|
||||
return GPIO_2;
|
||||
else
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// determines the pin address inside the port by the board pin number
|
||||
__attribute__((noinline, section(".ram_text"))) HAL_PinsTypeDef digitalPinToBitMask(uint32_t digitalPinNumber)
|
||||
{
|
||||
if (digitalPinNumber >= PINS_COMMON_QTY)
|
||||
{
|
||||
ErrorMsgHandler("digitalPinToBitMask(): pin number exceeds the total number of pins");
|
||||
return NOT_A_PIN;
|
||||
}
|
||||
|
||||
if (digitalPinNumber >= P2_6)
|
||||
return (HAL_PinsTypeDef)(1 << ((digitalPinNumber+6) & 0xF));
|
||||
else
|
||||
return (HAL_PinsTypeDef)(1 << (digitalPinNumber & 0xF));
|
||||
}
|
||||
|
||||
// the function returns a reference to the OUTPUT address of the GPIO register
|
||||
volatile uint32_t *portOutputRegister(GPIO_TypeDef *GPIO_x)
|
||||
{
|
||||
return &GPIO_x->OUTPUT_;
|
||||
}
|
||||
|
||||
// the function returns a reference to the STATE address of the GPIO register
|
||||
volatile uint32_t *portInputRegister(GPIO_TypeDef *GPIO_x)
|
||||
{
|
||||
return &GPIO_x->STATE;
|
||||
}
|
||||
|
||||
// ---------------------- ADC ---------------------- //
|
||||
// determines the ADC channel number by the board pin number
|
||||
uint32_t analogInputToChannelNumber(uint32_t PinNumber)
|
||||
{
|
||||
uint32_t adcChannel = 0;
|
||||
|
||||
switch (PinNumber)
|
||||
{
|
||||
case PIN_A0:
|
||||
adcChannel = ADC_CHANNEL0;
|
||||
break;
|
||||
case PIN_A1:
|
||||
adcChannel = ADC_CHANNEL1;
|
||||
break;
|
||||
case PIN_A2:
|
||||
adcChannel = ADC_CHANNEL2;
|
||||
break;
|
||||
case PIN_A3:
|
||||
adcChannel = ADC_CHANNEL3;
|
||||
break;
|
||||
case PIN_A4:
|
||||
adcChannel = ADC_CHANNEL4;
|
||||
break;
|
||||
case PIN_A5:
|
||||
adcChannel = ADC_CHANNEL5;
|
||||
break;
|
||||
case PIN_A6:
|
||||
adcChannel = ADC_CHANNEL6;
|
||||
break;
|
||||
case PIN_A7:
|
||||
adcChannel = ADC_CHANNEL7;
|
||||
break;
|
||||
default:
|
||||
adcChannel = NC;
|
||||
}
|
||||
return adcChannel;
|
||||
}
|
||||
|
||||
// ---------------------- PWM ---------------------- //
|
||||
// use only if digitalPinHasPWM() == true
|
||||
#define PWM_PIN_TO_PORT_NUMBER(pin) (((pin) & 16) ? 1 : 0)
|
||||
|
||||
// use only if digitalPinHasPWM() == true
|
||||
// return true if digitalPin configured as pwm
|
||||
__attribute__((noinline, section(".ram_text"))) bool digitalPinPwmIsOn(uint8_t digitalPin)
|
||||
{
|
||||
if (digitalPinHasPWM(digitalPin))
|
||||
{
|
||||
uint8_t config = 0;
|
||||
uint8_t pinShift = digitalPin & 0x3;
|
||||
|
||||
if (PWM_PIN_TO_PORT_NUMBER(digitalPin) == 0)
|
||||
config = PIN_GET_PAD_CONFIG(PORT_0_CFG, pinShift);
|
||||
else
|
||||
config = PIN_GET_PAD_CONFIG(PORT_1_CFG, pinShift);
|
||||
|
||||
if (config == 2)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// function is used only if digitalPinHasPWM() is true
|
||||
TIMER32_TypeDef *pwmPinToTimer(uint32_t digPinNumber)
|
||||
{
|
||||
if (digPinNumber <= P0_15)
|
||||
return TIMER32_1;
|
||||
else if ((digPinNumber >= P1_0) && (digPinNumber <= P1_15))
|
||||
return TIMER32_2;
|
||||
else
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// function is used only if digitalPinHasPWM() is true
|
||||
HAL_TIMER32_CHANNEL_IndexTypeDef pwmPinToTimerChannel(uint32_t digPinNumber)
|
||||
{
|
||||
switch (digPinNumber & 0x3)
|
||||
{
|
||||
case 0:
|
||||
return TIMER32_CHANNEL_0;
|
||||
case 1:
|
||||
return TIMER32_CHANNEL_1;
|
||||
case 2:
|
||||
return TIMER32_CHANNEL_2;
|
||||
case 3:
|
||||
return TIMER32_CHANNEL_3;
|
||||
default:
|
||||
return 255;
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------- interrupts ---------------------- //
|
||||
// interrupt table is stored in ram to improve performance
|
||||
// index = interrupt number. In each row {digitalPinNumber, IntLineValue, IntMuxValue}
|
||||
uint8_t interruptInfo[EXTERNAL_INTERRUPTS_QTY][3] =
|
||||
{
|
||||
{P0_8, GPIO_LINE_0, GPIO_MUX_LINE_0_PORT0_8}, // INT0
|
||||
{P1_4, GPIO_LINE_4, GPIO_MUX_LINE_4_PORT1_4}, // INT1
|
||||
{P1_5, GPIO_LINE_5, GPIO_MUX_LINE_5_PORT1_5}, // INT2
|
||||
{P1_6, GPIO_LINE_6, GPIO_MUX_LINE_6_PORT1_6}, // INT3
|
||||
{P1_9, GPIO_LINE_1, GPIO_MUX_LINE_1_PORT1_9}, // INT4
|
||||
{P1_10, GPIO_LINE_2, GPIO_MUX_LINE_2_PORT1_10}, // INT5
|
||||
{P1_15, GPIO_LINE_3, GPIO_MUX_LINE_3_PORT1_15}, // INT6
|
||||
{P2_7, GPIO_LINE_7, GPIO_MUX_LINE_7_PORT2_7}, // INT7
|
||||
};
|
||||
|
||||
int8_t digitalPinToGpioIntMux(uint8_t digPinNumber)
|
||||
{
|
||||
for (uint8_t i = 0; i < EXTERNAL_INTERRUPTS_QTY; i++)
|
||||
{
|
||||
if (interruptInfo[i][0] == digPinNumber)
|
||||
return interruptInfo[i][2];
|
||||
}
|
||||
return NOT_AN_INTERRUPT;
|
||||
}
|
||||
int8_t digitalPinToGpioIntLine(uint8_t digPinNumber)
|
||||
{
|
||||
for (uint8_t i = 0; i < EXTERNAL_INTERRUPTS_QTY; i++)
|
||||
{
|
||||
if (interruptInfo[i][0] == digPinNumber)
|
||||
return interruptInfo[i][1];
|
||||
}
|
||||
return NOT_AN_INTERRUPT;
|
||||
}
|
||||
|
||||
int8_t gpioIntLineToInterrupt(uint32_t gpioIntLine)
|
||||
{
|
||||
for (uint8_t i = 0; i < EXTERNAL_INTERRUPTS_QTY; i++)
|
||||
{
|
||||
if (interruptInfo[i][1] == gpioIntLine)
|
||||
return i;
|
||||
}
|
||||
return NOT_AN_INTERRUPT;
|
||||
}
|
||||
|
||||
int8_t digitalPinToInterrupt(uint32_t digPinNumber)
|
||||
{
|
||||
for (uint8_t i = 0; i < EXTERNAL_INTERRUPTS_QTY; i++)
|
||||
{
|
||||
if (interruptInfo[i][0] == digPinNumber)
|
||||
return i;
|
||||
}
|
||||
return NOT_AN_INTERRUPT;
|
||||
}
|
||||
|
||||
// ---------------------- SPI ---------------------- //
|
||||
void spi_onBegin(uint8_t spiNum)
|
||||
{
|
||||
// On mik32 spi0 needs pin 0.3, spi1 needs pin 1.3 for correct work.
|
||||
// This pins also can be used as pwm. If pwm is working when spi begins, we should stop the pwm channel
|
||||
|
||||
// get pin NSS_IN config
|
||||
uint8_t config;
|
||||
if (spiNum == 1)
|
||||
config = PIN_GET_PAD_CONFIG(PORT_1_CFG, PIN_MASK_TO_PIN_NUMBER(GPIO_PIN_3));
|
||||
else
|
||||
config = PIN_GET_PAD_CONFIG(PORT_0_CFG, PIN_MASK_TO_PIN_NUMBER(GPIO_PIN_3));
|
||||
|
||||
if(config == 2) // timer for pwm
|
||||
{
|
||||
if (spiNum == 1)
|
||||
{
|
||||
analogWriteStop(P1_3);
|
||||
ErrorMsgHandler("analogWrite(): P1_3 cannot be used as PWM pin while SPI is running");
|
||||
}
|
||||
else
|
||||
{
|
||||
analogWriteStop(P0_3);
|
||||
ErrorMsgHandler("analogWrite(): P0_3 cannot be used as PWM pin while SPI1 is running");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -78,6 +78,9 @@ typedef enum
|
||||
PINS_COMMON_QTY, // 40
|
||||
} DigitalPinsTypeDef;
|
||||
|
||||
// total number of pins available for initialization
|
||||
#define pinCommonQty() (PINS_COMMON_QTY)
|
||||
|
||||
// analog pins
|
||||
#define PIN_A0 (P1_5)
|
||||
#define PIN_A1 (P1_7)
|
||||
@@ -107,21 +110,14 @@ static const uint8_t A7 = PIN_A7;
|
||||
// determines the address of the port by the board pin number to which this pin belongs on the MCU
|
||||
GPIO_TypeDef* digitalPinToPort(uint32_t digPinNumber);
|
||||
// determines the pin address inside the port by the board pin number
|
||||
static inline HAL_PinsTypeDef digitalPinToBitMask(uint32_t digitalPinNumber)
|
||||
{
|
||||
return (HAL_PinsTypeDef)(1 << (digitalPinNumber & 0xF));
|
||||
}
|
||||
// total number of pins available for initialization
|
||||
static inline uint16_t pinCommonQty(void)
|
||||
{
|
||||
return (uint16_t)PINS_COMMON_QTY;
|
||||
}
|
||||
HAL_PinsTypeDef digitalPinToBitMask(uint32_t digitalPinNumber);
|
||||
// the function returns a reference to the OUTPUT address of the GPIO register
|
||||
volatile uint32_t* portOutputRegister(GPIO_TypeDef* GPIO_x);
|
||||
// the function returns a reference to the STATE address of the GPIO register
|
||||
volatile uint32_t* portInputRegister(GPIO_TypeDef* GPIO_x);
|
||||
// the function is needed for compatibility with other boards
|
||||
// functions are needed for compatibility with other boards
|
||||
static inline void additionalPinsInit(uint32_t PinNumber) {}
|
||||
static inline void additionalPinsDeinit(uint32_t PinNumber){}
|
||||
|
||||
// UART
|
||||
// available uarts quantity
|
||||
@@ -133,32 +129,55 @@ static inline void additionalPinsInit(uint32_t PinNumber) {}
|
||||
uint32_t analogInputToChannelNumber(uint32_t PinNumber);
|
||||
|
||||
// PWM
|
||||
bool digitalPinHasPWM(uint8_t p);
|
||||
bool digitalPinPwmIsOn(uint8_t digitalPin); // use only if digitalPinHasPWM() == true
|
||||
#define PWM_FREQUENCY_MAX 1000000 // Hz
|
||||
static inline __attribute__((always_inline)) bool digitalPinHasPWM(uint8_t p)
|
||||
{
|
||||
return (p < 32) && ((p & 0xF) < 4);
|
||||
}
|
||||
// return true if digitalPin configured as pwm
|
||||
bool digitalPinPwmIsOn(uint8_t digitalPin);
|
||||
// determines which timer the pin belongs to
|
||||
TIMER32_TypeDef* pwmPinToTimer(uint32_t digPinNumber);
|
||||
// determines which timer channel the pin belongs to
|
||||
HAL_TIMER32_CHANNEL_IndexTypeDef pwmPinToTimerChannel(uint32_t digPinNumber);
|
||||
|
||||
// SPI
|
||||
#define PIN_SPI_SS (P1_3)
|
||||
#define PIN_SPI_MOSI (P1_1)
|
||||
#define PIN_SPI_MISO (P1_0)
|
||||
#define PIN_SPI_SCK (P1_2)
|
||||
#define SPI_COMMON_QTY 2
|
||||
|
||||
#define PIN_SPI_SS P1_3
|
||||
#define PIN_SPI_MOSI P1_1
|
||||
#define PIN_SPI_MISO P1_0
|
||||
#define PIN_SPI_SCK P1_2
|
||||
static const uint8_t SS = PIN_SPI_SS;
|
||||
static const uint8_t MOSI = PIN_SPI_MOSI;
|
||||
static const uint8_t MISO = PIN_SPI_MISO;
|
||||
static const uint8_t SCK = PIN_SPI_SCK;
|
||||
// functions is needed for compatibility with other boards
|
||||
static inline void spi_onBegin(void) {}
|
||||
static inline void spi_onEnd(void) {}
|
||||
|
||||
#define PIN_SPI1_SS P0_3
|
||||
#define PIN_SPI1_MOSI P0_1
|
||||
#define PIN_SPI1_MISO P0_0
|
||||
#define PIN_SPI1_SCK P0_2
|
||||
static const uint8_t SS1 = PIN_SPI1_SS;
|
||||
static const uint8_t MOSI1 = PIN_SPI1_MOSI;
|
||||
static const uint8_t MISO1 = PIN_SPI1_MISO;
|
||||
static const uint8_t SCK1 = PIN_SPI1_SCK;
|
||||
|
||||
// check if pwm is used on spi pins
|
||||
void spi_onBegin(uint8_t spiNum);
|
||||
static inline void spi_onEnd(uint8_t spiNum){}
|
||||
|
||||
// I2C
|
||||
#define PIN_WIRE_SDA (P1_12)
|
||||
#define PIN_WIRE_SCL (P1_13)
|
||||
#define I2C_NUM (1) // i2c number 1
|
||||
static const uint8_t SDA = PIN_WIRE_SDA;
|
||||
static const uint8_t SCL = PIN_WIRE_SCL;
|
||||
#define I2C_COMMON_QTY 2
|
||||
// Wire (I2C1)
|
||||
#define PIN_WIRE_SDA P1_12
|
||||
#define PIN_WIRE_SCL P1_13
|
||||
static const uint8_t SDA = PIN_WIRE_SDA;
|
||||
static const uint8_t SCL = PIN_WIRE_SCL;
|
||||
// Wire1 (I2C0)
|
||||
#define PIN_WIRE_SDA1 P0_9
|
||||
#define PIN_WIRE_SCL1 P0_10
|
||||
static const uint8_t SDA1 = PIN_WIRE_SDA1;
|
||||
static const uint8_t SCL1 = PIN_WIRE_SCL1;
|
||||
// available frequencies
|
||||
#define WIRE_FREQ_100K 100000
|
||||
#define WIRE_FREQ_400K 400000
|
||||
|
||||
+61
-27
@@ -21,24 +21,33 @@
|
||||
* @return The address of the port corresponding to the board pin number. Can return 0 if the pin not exists
|
||||
*/
|
||||
|
||||
GPIO_TypeDef *digitalPinToPort(uint32_t digitalPinNumber)
|
||||
__attribute__((noinline, section(".ram_text"))) GPIO_TypeDef *digitalPinToPort(uint32_t digitalPinNumber)
|
||||
{
|
||||
if (digitalPinNumber < 16u)
|
||||
GPIO_TypeDef* port = NULL;
|
||||
if (digitalPinNumber < pinCommonQty())
|
||||
{
|
||||
return GPIO_0;
|
||||
}
|
||||
else if ((digitalPinNumber >= 16u) && (digitalPinNumber < 32u))
|
||||
{
|
||||
return GPIO_1;
|
||||
}
|
||||
else if ((digitalPinNumber >= 32u) && (digitalPinNumber < 40u))
|
||||
{
|
||||
return GPIO_2;
|
||||
if (digitalPinNumber < 16u)
|
||||
port = GPIO_0;
|
||||
else if ((digitalPinNumber >= 16u) && (digitalPinNumber < 32u))
|
||||
port = GPIO_1;
|
||||
else if ((digitalPinNumber >= 32u) && (digitalPinNumber < 40u))
|
||||
port = GPIO_2;
|
||||
}
|
||||
else
|
||||
ErrorMsgHandler("digitalPinToPort(): pin number exceeds the total number of pins");
|
||||
return port;
|
||||
}
|
||||
|
||||
// determines the pin address inside the port by the board pin number
|
||||
__attribute__((noinline, section(".ram_text"))) HAL_PinsTypeDef digitalPinToBitMask(uint32_t digitalPinNumber)
|
||||
{
|
||||
if (digitalPinNumber >= pinCommonQty())
|
||||
{
|
||||
return NULL;
|
||||
ErrorMsgHandler("digitalPinToBitMask(): pin number exceeds the total number of pins");
|
||||
return NOT_A_PIN;
|
||||
}
|
||||
|
||||
return (HAL_PinsTypeDef)(1 << (digitalPinNumber & 0xF));
|
||||
}
|
||||
|
||||
// the function returns a reference to the OUTPUT address of the GPIO register
|
||||
@@ -97,27 +106,24 @@ uint32_t analogInputToChannelNumber(uint32_t PinNumber)
|
||||
|
||||
// use only if digitalPinHasPWM() == true
|
||||
// return true if digitalPin configured as pwm
|
||||
bool digitalPinPwmIsOn(uint8_t digitalPin)
|
||||
__attribute__((noinline, section(".ram_text"))) bool digitalPinPwmIsOn(uint8_t digitalPin)
|
||||
{
|
||||
uint8_t config = 0;
|
||||
uint8_t pinShift = digitalPin & 3;
|
||||
if (digitalPinHasPWM(digitalPin))
|
||||
{
|
||||
uint8_t config = 0;
|
||||
uint8_t pinShift = digitalPin & 3;
|
||||
|
||||
if (PWM_PIN_TO_PORT_NUMBER(digitalPin) == 0)
|
||||
config = PIN_GET_PAD_CONFIG(PORT_0_CFG, pinShift);
|
||||
else
|
||||
config = PIN_GET_PAD_CONFIG(PORT_1_CFG, pinShift);
|
||||
if (PWM_PIN_TO_PORT_NUMBER(digitalPin) == 0)
|
||||
config = PIN_GET_PAD_CONFIG(PORT_0_CFG, pinShift);
|
||||
else
|
||||
config = PIN_GET_PAD_CONFIG(PORT_1_CFG, pinShift);
|
||||
|
||||
if (config == 2)
|
||||
return true;
|
||||
else
|
||||
if (config == 2)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool digitalPinHasPWM(uint8_t p)
|
||||
{
|
||||
return (p < 32) && ((p & 0xF) < 4);
|
||||
}
|
||||
|
||||
// function is used only if digitalPinHasPWM() is true
|
||||
TIMER32_TypeDef *pwmPinToTimer(uint32_t digPinNumber)
|
||||
{
|
||||
@@ -205,4 +211,32 @@ int8_t digitalPinToInterrupt(uint32_t digPinNumber)
|
||||
return i;
|
||||
}
|
||||
return NOT_AN_INTERRUPT;
|
||||
}
|
||||
|
||||
// ---------------------- SPI ---------------------- //
|
||||
void spi_onBegin(uint8_t spiNum)
|
||||
{
|
||||
// On mik32 spi0 needs pin 0.3, spi1 needs pin 1.3 for correct work.
|
||||
// This pins also can be used as pwm. If pwm is working when spi begins, we should stop the pwm channel
|
||||
|
||||
// get pin NSS_IN config
|
||||
uint8_t config;
|
||||
if (spiNum == 1)
|
||||
config = PIN_GET_PAD_CONFIG(PORT_1_CFG, PIN_MASK_TO_PIN_NUMBER(GPIO_PIN_3));
|
||||
else
|
||||
config = PIN_GET_PAD_CONFIG(PORT_0_CFG, PIN_MASK_TO_PIN_NUMBER(GPIO_PIN_3));
|
||||
|
||||
if(config == 2) // timer for pwm
|
||||
{
|
||||
if (spiNum == 1)
|
||||
{
|
||||
analogWriteStop(P1_3);
|
||||
ErrorMsgHandler("analogWrite(): P1_3 cannot be used as PWM pin while SPI is running");
|
||||
}
|
||||
else
|
||||
{
|
||||
analogWriteStop(P0_3);
|
||||
ErrorMsgHandler("analogWrite(): P0_3 cannot be used as PWM pin while SPI1 is running");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user