dev0.2_halRevision (#1)
- Реализация части функций в библиотеках hal перенесена в заголовочный файл, функции определены как inline. - В модулях Tone, SPI, Wire работа с регистрами вынесена в библиотеку hal или в платозависимые файлы pins_arduino.h, variants.c - В модуле wiring_analog при отключении работы ШИМ сначала проверяется, включен ли ШИМ на указанной ноге. Если включен, вывод после отключения таймера конфигурируется на вход. Reviewed-on: #1
This commit was merged in pull request #1.
This commit is contained in:
1 parent
f020720e1f
commit
8f0a5b0a94
17 files changed
+433
-313
No files matched your search
@@ -32,8 +32,6 @@ extern "C" {
|
||||
#include "mik32_hal_gpio.h"
|
||||
#include "mik32_hal_timer32.h"
|
||||
|
||||
extern bool spiNssPinIsBlocked;
|
||||
|
||||
// analog pins
|
||||
#define PIN_A0 (14)
|
||||
#define PIN_A1 (15)
|
||||
@@ -78,6 +76,7 @@ uint32_t analogInputToChannelNumber(uint32_t PinNumber);
|
||||
|
||||
// PWM
|
||||
bool digitalPinHasPWM(uint8_t p);
|
||||
bool digitalPinPwmIsOn(uint8_t digitalPin); // use only if digitalPinHasPWM() == true
|
||||
// determines which timer the pin belongs to
|
||||
TIMER32_TypeDef* pwmPinToTimer(uint32_t digPinNumber);
|
||||
// determines which timer channel the pin belongs to
|
||||
@@ -94,19 +93,13 @@ static const uint8_t MISO = PIN_SPI_MISO;
|
||||
static const uint8_t SCK = PIN_SPI_SCK;
|
||||
// config SEL_NSS1 to replace D10 to different controller pin,
|
||||
// because pin 1.3 which is D10 by default is needed to spi
|
||||
inline void blockSpiPin(void)
|
||||
{
|
||||
spiNssPinIsBlocked = true;
|
||||
}
|
||||
inline void unblockSpiPin(void)
|
||||
{
|
||||
spiNssPinIsBlocked = false;
|
||||
}
|
||||
void spi_onBegin(void);
|
||||
void spi_onEnd(void);
|
||||
|
||||
// I2C
|
||||
#define PIN_WIRE_SDA (18)
|
||||
#define PIN_WIRE_SCL (19)
|
||||
#define I2C_NUM (1) // i2c number 0 or 1
|
||||
#define I2C_NUM (1) // i2c number 1
|
||||
static const uint8_t SDA = PIN_WIRE_SDA;
|
||||
static const uint8_t SCL = PIN_WIRE_SCL;
|
||||
// available frequencies
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
|
||||
#include "pins_arduino.h"
|
||||
#include "mik32_hal_adc.h"
|
||||
#include "wiring_analog.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
@@ -106,6 +107,10 @@ volatile uint32_t* portInputRegister(GPIO_TypeDef* GPIO_x)
|
||||
return &GPIO_x->STATE;
|
||||
}
|
||||
|
||||
// return config of pin with pinShift(0...16) in portReg (config, pupd, ds for ports 0...2)
|
||||
#define PIN_PAD_CONFIG(portReg, pinShift) ((PAD_CONFIG->portReg >> (pinShift<<1)) & 0b11)
|
||||
|
||||
|
||||
// ---------------------- ADC ---------------------- //
|
||||
// determines the ADC channel number by the board pin number
|
||||
uint32_t analogInputToChannelNumber(uint32_t PinNumber)
|
||||
@@ -141,6 +146,38 @@ uint32_t analogInputToChannelNumber(uint32_t PinNumber)
|
||||
}
|
||||
|
||||
// ---------------------- PWM ---------------------- //
|
||||
// use only if digitalPinHasPWM() == true
|
||||
#define PWM_PIN_TO_PORT_NUMBER(pin) (((pin==10)||(pin==11)) ? 1:0)
|
||||
// use only if digitalPinHasPWM() == true
|
||||
static inline uint8_t pwmPinToGpioPinShift(uint8_t digitalPin)
|
||||
{
|
||||
if (digitalPin == 3)
|
||||
return 0;
|
||||
else if ((digitalPin == 5) || (digitalPin == 11))
|
||||
return 1;
|
||||
else if (digitalPin == 6)
|
||||
return 2;
|
||||
else // pins 9 10
|
||||
return 3;
|
||||
}
|
||||
// use only if digitalPinHasPWM() == true
|
||||
// return true if digitalPin configured as pwm
|
||||
bool digitalPinPwmIsOn(uint8_t digitalPin)
|
||||
{
|
||||
uint8_t config = 0;
|
||||
uint8_t pinShift = pwmPinToGpioPinShift(digitalPin);
|
||||
|
||||
if (PWM_PIN_TO_PORT_NUMBER(digitalPin) == 0)
|
||||
config = PIN_PAD_CONFIG(PORT_0_CFG, pinShift);
|
||||
else
|
||||
config = PIN_PAD_CONFIG(PORT_1_CFG, pinShift);
|
||||
|
||||
if (config == 2)
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
bool digitalPinHasPWM(uint8_t p)
|
||||
{
|
||||
bool ret = false;
|
||||
@@ -225,4 +262,52 @@ int8_t digitalPinToInterrupt(uint32_t digPinNumber)
|
||||
return i;
|
||||
}
|
||||
return NOT_AN_INTERRUPT;
|
||||
}
|
||||
|
||||
// ---------------------- SPI ---------------------- //
|
||||
// pins shift in registers
|
||||
#define PIN_3_SHIFT 3
|
||||
#define PIN_4_SHIFT 4
|
||||
#define PORT1_GET_PAD_PUPD(pinShift) ((PAD_CONFIG->PORT_1_PUPD >> (pinShift<<1)) & 0b11)
|
||||
#define PORT1_GET_GPIO_STATE(pinShift) ((GPIO_1->OUTPUT_ >> pinShift) & 0b1)
|
||||
|
||||
void spi_onBegin(void)
|
||||
{
|
||||
// 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
|
||||
|
||||
// replace config from 1.3 to 1.4
|
||||
uint8_t config = PIN_PAD_CONFIG(PORT_1_CFG, PIN_3_SHIFT);
|
||||
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)PORT1_GET_PAD_PUPD(PIN_3_SHIFT), HAL_GPIO_DS_2MA);
|
||||
HAL_GPIO_WritePin(GPIO_1, GPIO_PIN_4, (GPIO_PinState)PORT1_GET_GPIO_STATE(PIN_3_SHIFT));
|
||||
|
||||
// pin D10 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");
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
void spi_onEnd(void)
|
||||
{
|
||||
// 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)PORT1_GET_PAD_PUPD(PIN_4_SHIFT), HAL_GPIO_DS_2MA);
|
||||
HAL_GPIO_WritePin(GPIO_1, GPIO_PIN_3, (GPIO_PinState)PORT1_GET_GPIO_STATE(PIN_4_SHIFT));
|
||||
|
||||
// switch seller back to pin 1.3
|
||||
HAL_GPIO_WritePin(GPIO_1, GPIO_PIN_6, GPIO_PIN_LOW);
|
||||
spiNssPinIsBlocked = false; // unblock spi pin
|
||||
}
|
||||
Reference in new issue
Block a user