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:
klassents committed 2024-09-11 10:50:22 +03:00
1 parent f020720e1f
commit 8f0a5b0a94
17 files changed
+433 -313

No files matched your search

+25 -44
View File
@@ -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);
}