работа с регистрами перенесена в hal, новые функции типа inline
This commit is contained in:
parent
46f68aef2f
commit
670c48acab
6
.vscode/settings.json
vendored
Normal file
6
.vscode/settings.json
vendored
Normal file
@ -0,0 +1,6 @@
|
||||
{
|
||||
"files.associations": {
|
||||
"mik32_hal_irq.h": "c",
|
||||
"mik32_hal_i2c.h": "c"
|
||||
}
|
||||
}
|
||||
@ -380,9 +380,6 @@ typedef struct
|
||||
|
||||
|
||||
void HAL_I2C_MspInit(I2C_HandleTypeDef* hi2c);
|
||||
void HAL_I2C_Disable(I2C_HandleTypeDef *hi2c);
|
||||
void HAL_I2C_Reset(I2C_HandleTypeDef *hi2c);
|
||||
void HAL_I2C_Enable(I2C_HandleTypeDef *hi2c);
|
||||
void HAL_I2C_AnalogFilterInit(I2C_HandleTypeDef *hi2c, HAL_I2C_AnalogFilterTypeDef AnalogFilter);
|
||||
void HAL_I2C_DigitalFilterInit(I2C_HandleTypeDef *hi2c, HAL_I2C_DigitalFilterTypeDef DigitalFilter);
|
||||
void HAL_I2C_SetClockSpeed(I2C_HandleTypeDef *hi2c);
|
||||
@ -394,6 +391,7 @@ void HAL_I2C_SBCMode(I2C_HandleTypeDef *hi2c, HAL_I2C_SBCModeTypeDef SBCMode);
|
||||
void HAL_I2C_SlaveInit(I2C_HandleTypeDef *hi2c);
|
||||
void HAL_I2C_MasterInit(I2C_HandleTypeDef *hi2c);
|
||||
HAL_StatusTypeDef HAL_I2C_Init(I2C_HandleTypeDef *hi2c);
|
||||
HAL_StatusTypeDef HAL_I2C_Deinit(I2C_HandleTypeDef *hi2c);
|
||||
void HAL_I2C_AutoEnd(I2C_HandleTypeDef *hi2c, HAL_I2C_AutoEndModeTypeDef AutoEnd);
|
||||
HAL_StatusTypeDef HAL_I2C_Master_WaitTXIS(I2C_HandleTypeDef *hi2c, uint32_t Timeout);
|
||||
HAL_StatusTypeDef HAL_I2C_Master_WaitRXNE(I2C_HandleTypeDef *hi2c, uint32_t Timeout);
|
||||
@ -427,6 +425,60 @@ HAL_StatusTypeDef HAL_I2C_Slave_Receive_IT(I2C_HandleTypeDef *hi2c, uint8_t *pDa
|
||||
HAL_StatusTypeDef HAL_I2C_Slave_Receive_NOSTRETCH_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t DataSize);
|
||||
HAL_StatusTypeDef HAL_I2C_Slave_ReceiveSBC_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t DataSize);
|
||||
|
||||
|
||||
static inline __attribute__((always_inline)) void HAL_I2C_Disable(I2C_HandleTypeDef *hi2c)
|
||||
{
|
||||
hi2c->Instance->CR1 &= ~I2C_CR1_PE_M;
|
||||
}
|
||||
|
||||
static inline __attribute__((always_inline)) void HAL_I2C_Reset(I2C_HandleTypeDef *hi2c)
|
||||
{
|
||||
hi2c->ErrorCode = I2C_ERROR_NONE;
|
||||
|
||||
hi2c->Instance->CR1 &= ~I2C_CR1_PE_M;
|
||||
hi2c->Instance->CR1 |= I2C_CR1_PE_M;
|
||||
}
|
||||
|
||||
static inline __attribute__((always_inline)) void HAL_I2C_Enable(I2C_HandleTypeDef *hi2c)
|
||||
{
|
||||
hi2c->Instance->CR1 |= I2C_CR1_PE_M;
|
||||
}
|
||||
|
||||
static inline __attribute__((always_inline)) void HAL_I2C_Reset_Interrupt_Flag(I2C_HandleTypeDef *hi2c, uint32_t mask)
|
||||
{
|
||||
hi2c->Instance->ICR |= mask;
|
||||
}
|
||||
|
||||
static inline __attribute__((always_inline)) void HAL_I2C_Reset_TXDR_Content(I2C_HandleTypeDef *hi2c)
|
||||
{
|
||||
hi2c->Instance->ISR |= I2C_ISR_TXE_M;
|
||||
}
|
||||
|
||||
static inline __attribute__((always_inline)) uint32_t HAL_I2C_Get_Interrupts_Status(I2C_HandleTypeDef *hi2c)
|
||||
{
|
||||
return hi2c->Instance->ISR;
|
||||
}
|
||||
|
||||
static inline __attribute__((always_inline)) uint32_t HAL_I2C_Get_CR1_Content(I2C_HandleTypeDef *hi2c)
|
||||
{
|
||||
return hi2c->Instance->CR1;
|
||||
}
|
||||
|
||||
static inline __attribute__((always_inline)) void HAL_I2C_Write_TXDR(I2C_HandleTypeDef *hi2c, uint8_t value)
|
||||
{
|
||||
hi2c->Instance->TXDR = value;
|
||||
}
|
||||
|
||||
static inline __attribute__((always_inline)) uint8_t HAL_I2C_Get_RXDR(I2C_HandleTypeDef *hi2c)
|
||||
{
|
||||
return hi2c->Instance->RXDR;
|
||||
}
|
||||
|
||||
static inline __attribute__((always_inline)) void HAL_I2C_Clear_Reload(I2C_HandleTypeDef *hi2c)
|
||||
{
|
||||
hi2c->Instance->CR2 &= ~I2C_CR2_RELOAD_M;
|
||||
}
|
||||
|
||||
static inline __attribute__((always_inline)) void HAL_I2C_ADDR_IRQ(I2C_HandleTypeDef *hi2c)
|
||||
{
|
||||
if (hi2c->Instance->CR1 & I2C_CR1_SBC_M)
|
||||
|
||||
@ -26,24 +26,6 @@ __attribute__((weak)) void HAL_I2C_MspInit(I2C_HandleTypeDef* hi2c)
|
||||
|
||||
}
|
||||
|
||||
void HAL_I2C_Disable(I2C_HandleTypeDef *hi2c)
|
||||
{
|
||||
hi2c->Instance->CR1 &= ~I2C_CR1_PE_M;
|
||||
}
|
||||
|
||||
void HAL_I2C_Reset(I2C_HandleTypeDef *hi2c)
|
||||
{
|
||||
hi2c->ErrorCode = I2C_ERROR_NONE;
|
||||
|
||||
hi2c->Instance->CR1 &= ~I2C_CR1_PE_M;
|
||||
hi2c->Instance->CR1 |= I2C_CR1_PE_M;
|
||||
}
|
||||
|
||||
void HAL_I2C_Enable(I2C_HandleTypeDef *hi2c)
|
||||
{
|
||||
hi2c->Instance->CR1 |= I2C_CR1_PE_M;
|
||||
}
|
||||
|
||||
void HAL_I2C_AnalogFilterInit(I2C_HandleTypeDef *hi2c, HAL_I2C_AnalogFilterTypeDef AnalogFilter)
|
||||
{
|
||||
hi2c->Init.AnalogFilter = AnalogFilter;
|
||||
@ -244,6 +226,24 @@ HAL_StatusTypeDef HAL_I2C_Init(I2C_HandleTypeDef *hi2c)
|
||||
return HAL_OK;
|
||||
}
|
||||
|
||||
HAL_StatusTypeDef HAL_I2C_Deinit(I2C_HandleTypeDef *hi2c)
|
||||
{
|
||||
HAL_I2C_Disable(hi2c);
|
||||
|
||||
if (hi2c->Instance == I2C_0)
|
||||
{
|
||||
__HAL_PCC_I2C_0_CLK_DISABLE();
|
||||
HAL_GPIO_PinConfig(GPIO_0, GPIO_PIN_9 | GPIO_PIN_10, HAL_GPIO_MODE_GPIO_INPUT, HAL_GPIO_PULL_NONE, HAL_GPIO_DS_2MA);
|
||||
}
|
||||
else if(hi2c->Instance == I2C_1)
|
||||
{
|
||||
__HAL_PCC_I2C_1_CLK_DISABLE();
|
||||
HAL_GPIO_PinConfig(GPIO_1, GPIO_PIN_12 | GPIO_PIN_13, HAL_GPIO_MODE_GPIO_INPUT, HAL_GPIO_PULL_NONE, HAL_GPIO_DS_2MA);
|
||||
}
|
||||
|
||||
return HAL_OK;
|
||||
}
|
||||
|
||||
void HAL_I2C_AutoEnd(I2C_HandleTypeDef *hi2c, HAL_I2C_AutoEndModeTypeDef AutoEnd)
|
||||
{
|
||||
hi2c->Instance->CR2 &= ~I2C_CR2_AUTOEND_M;
|
||||
|
||||
@ -88,36 +88,22 @@ void twi_deinit(void)
|
||||
{
|
||||
uint32_t EPICmask;
|
||||
|
||||
HAL_I2C_Disable(&hi2c);
|
||||
// disable clock
|
||||
#if I2C_NUM == 0
|
||||
hi2c.Instance = I2C_0;
|
||||
EPICmask = HAL_EPIC_I2C_0_MASK;
|
||||
__HAL_PCC_I2C_0_CLK_DISABLE();
|
||||
#elif I2C_NUM == 1
|
||||
hi2c.Instance = I2C_1;
|
||||
EPICmask = HAL_EPIC_I2C_1_MASK;
|
||||
__HAL_PCC_I2C_1_CLK_DISABLE();
|
||||
#else
|
||||
#error "Unsupported I2C_NUM value in pins_arduino.h"
|
||||
#endif
|
||||
|
||||
HAL_I2C_Deinit(&hi2c);
|
||||
|
||||
// for slave mode disable interrupts from i2c
|
||||
if (hi2c.Init.Mode == HAL_I2C_MODE_SLAVE)
|
||||
HAL_EPIC_MaskLevelClear(EPICmask);
|
||||
|
||||
// reconfigure pins to z state
|
||||
GPIO_InitTypeDef GPIO_InitStruct;
|
||||
memset(&GPIO_InitStruct, 0, sizeof(GPIO_InitStruct));
|
||||
// SDA
|
||||
GPIO_InitStruct.Pin = (HAL_PinsTypeDef)digitalPinToBitMask(PIN_WIRE_SDA);
|
||||
GPIO_InitStruct.Mode = HAL_GPIO_MODE_GPIO_INPUT;
|
||||
GPIO_InitStruct.Pull = HAL_GPIO_PULL_NONE;
|
||||
HAL_GPIO_Init(digitalPinToPort(PIN_WIRE_SDA), &GPIO_InitStruct);
|
||||
// SCL
|
||||
GPIO_InitStruct.Pin = (HAL_PinsTypeDef)digitalPinToBitMask(PIN_WIRE_SCL);
|
||||
HAL_GPIO_Init(digitalPinToPort(PIN_WIRE_SCL), &GPIO_InitStruct);
|
||||
|
||||
twiIsOn = false;
|
||||
}
|
||||
|
||||
@ -252,7 +238,7 @@ uint8_t twi_masterWriteTo(uint8_t address, uint8_t* data, uint8_t length, uint8_
|
||||
|
||||
// parse errors
|
||||
// check separately, because in hal libraries not all functions look at this
|
||||
if (hi2c.Instance->ISR & I2C_ISR_NACKF_M)
|
||||
if (HAL_I2C_Get_Interrupts_Status(&hi2c) & I2C_ISR_NACKF_M)
|
||||
hi2c.ErrorCode = I2C_ERROR_NACK;
|
||||
if (hi2c.ErrorCode == (HAL_I2C_ErrorTypeDef)I2C_ERROR_TIMEOUT) ret = I2C_TIMEOUT; // timeout
|
||||
else if (hi2c.ErrorCode == (HAL_I2C_ErrorTypeDef)I2C_ERROR_NACK) ret = I2C_NACK_DATA; // didn't receive ACK
|
||||
@ -275,39 +261,35 @@ i2c_status_e twi_slaveWrite(uint8_t *txData, uint8_t bytesNum)
|
||||
if ((hi2c.ErrorCode != I2C_ERROR_NONE))
|
||||
HAL_I2C_Reset(&hi2c);
|
||||
|
||||
// отправка данных
|
||||
// send data
|
||||
HAL_StatusTypeDef error_code = HAL_OK;
|
||||
|
||||
hi2c.Instance->CR2 &= ~I2C_CR2_RELOAD_M;
|
||||
HAL_I2C_Clear_Reload(&hi2c);
|
||||
if (!(HAL_I2C_Get_CR1_Content(&hi2c) & I2C_CR1_NOSTRETCH_M)) // NOSTRETCH = 0
|
||||
HAL_I2C_Reset_TXDR_Content(&hi2c);
|
||||
HAL_I2C_Write_TXDR(&hi2c, txData[0]); // first recording is made in advance
|
||||
|
||||
if (!(hi2c.Instance->CR1 & I2C_CR1_NOSTRETCH_M)) // NOSTRETCH = 0
|
||||
hi2c.Instance->ISR |= I2C_ISR_TXE_M; // Reset TXDR contents
|
||||
|
||||
hi2c.Instance->TXDR = txData[0]; // The first recording is made in advance
|
||||
|
||||
// Запись байта
|
||||
// write byte
|
||||
for (uint32_t tx_count = 1; tx_count < bytesNum; tx_count++)
|
||||
{
|
||||
if ((error_code = HAL_I2C_Slave_WaitTXIS(&hi2c, TIMEOUT_TICKS)) != HAL_OK)
|
||||
{
|
||||
// неудачная запись
|
||||
hi2c.Instance->ISR |= I2C_ISR_TXE_M; // Reset TXDR contents
|
||||
hi2c.Instance->ICR |= I2C_ICR_STOPCF_M; // Clear the STOP detection flag on the bus
|
||||
// failed to write
|
||||
HAL_I2C_Reset_TXDR_Content(&hi2c);
|
||||
HAL_I2C_Reset_Interrupt_Flag(&hi2c, I2C_ICR_STOPCF_M); // Clear the STOP detection flag on the bus
|
||||
HAL_I2C_Reset(&hi2c);
|
||||
return I2C_TIMEOUT;
|
||||
}
|
||||
hi2c.Instance->TXDR = txData[tx_count];
|
||||
HAL_I2C_Write_TXDR(&hi2c, txData[tx_count]);
|
||||
}
|
||||
|
||||
if ((error_code = HAL_I2C_WaitBusy(&hi2c, TIMEOUT_TICKS)) != HAL_OK)
|
||||
{
|
||||
// неудачное завершение транзакции
|
||||
// failed to complete transaction
|
||||
HAL_I2C_Reset(&hi2c);
|
||||
return I2C_TIMEOUT;
|
||||
}
|
||||
|
||||
hi2c.Instance->ISR |= I2C_ISR_TXE_M; // Reset TXDR contents
|
||||
hi2c.Instance->ICR |= I2C_ICR_STOPCF_M; // Clear the STOP detection flag on the bus
|
||||
HAL_I2C_Reset_TXDR_Content(&hi2c);
|
||||
HAL_I2C_Reset_Interrupt_Flag(&hi2c, I2C_ICR_STOPCF_M); // Clear the STOP detection flag on the bus
|
||||
|
||||
return I2C_OK;
|
||||
}
|
||||
@ -343,25 +325,24 @@ void twi_attachSlaveTxEvent( void (*function)(void) )
|
||||
*/
|
||||
void twi_interruptHandler(void)
|
||||
{
|
||||
uint32_t int_mask = hi2c.Instance->CR1 & I2C_INTMASK; // interrupts allowed
|
||||
uint32_t interrupt_status = hi2c.Instance->ISR; // current flags
|
||||
uint32_t int_mask = HAL_I2C_Get_CR1_Content(&hi2c) & I2C_INTMASK; // interrupts allowed
|
||||
uint32_t interrupt_status = HAL_I2C_Get_Interrupts_Status(&hi2c); // current flags
|
||||
|
||||
// master calls by address, device in slave mode
|
||||
if ((interrupt_status & I2C_ISR_ADDR_M) && (int_mask & I2C_CR1_ADDRIE_M))
|
||||
{
|
||||
// reset ADDR flag
|
||||
hi2c.Instance->ICR |= I2C_ICR_ADDRCF_M;
|
||||
HAL_I2C_Reset_Interrupt_Flag(&hi2c, I2C_ICR_ADDRCF_M);
|
||||
|
||||
// look at the transmission direction and respond to the request
|
||||
if (interrupt_status & I2C_ISR_DIR_M) // master reads, slave sends
|
||||
// отправляем данные
|
||||
twi_onSlaveTransmit();
|
||||
twi_onSlaveTransmit(); // slave send data
|
||||
else // master writes, slave reads
|
||||
{
|
||||
twi_rxBufferIndex = 0; // write from the beginning of the buffer
|
||||
hi2c.State = HAL_I2C_STATE_BUSY;
|
||||
hi2c.Instance->CR2 &= ~I2C_CR2_RELOAD_M;
|
||||
// wait for interrupts because of receiving a byte or a stop condition
|
||||
HAL_I2C_Clear_Reload(&hi2c);
|
||||
// wait for interrupts by receiving a byte or a stop condition
|
||||
}
|
||||
}
|
||||
|
||||
@ -369,15 +350,15 @@ void twi_interruptHandler(void)
|
||||
if ((interrupt_status & I2C_ISR_RXNE_M) && (int_mask & I2C_CR1_RXIE_M))
|
||||
{
|
||||
// put new byte into buffer
|
||||
twi_rxBuffer[twi_rxBufferIndex++] = (uint8_t)hi2c.Instance->RXDR;
|
||||
twi_rxBuffer[twi_rxBufferIndex++] = HAL_I2C_Get_RXDR(&hi2c);
|
||||
}
|
||||
|
||||
// master sent a STOP to the bus
|
||||
if ((interrupt_status & I2C_ISR_STOPF_M) && (int_mask & I2C_CR1_STOPIE_M))
|
||||
{
|
||||
hi2c.State = HAL_I2C_STATE_END;
|
||||
hi2c.Instance->ISR |= I2C_ISR_TXE_M; // Reset TXDR contents
|
||||
hi2c.Instance->ICR |= I2C_ICR_STOPCF_M; // Clear the STOP detection flag on the bus
|
||||
HAL_I2C_Reset_TXDR_Content(&hi2c);
|
||||
HAL_I2C_Reset_Interrupt_Flag(&hi2c, I2C_ICR_STOPCF_M); // Clear the STOP detection flag on the bus
|
||||
// pass the received data to callback function
|
||||
twi_onSlaveReceive(twi_rxBuffer, twi_rxBufferIndex);
|
||||
}
|
||||
|
||||
@ -106,7 +106,7 @@ inline void unblockSpiPin(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
|
||||
|
||||
Loading…
Reference in New Issue
Block a user