EEPROM & Digital Volume I2C driver using generic I2C HAL driver
This commit is contained in:
committed by
Raphael Coeffic
parent
bc6c53681c
commit
df83a82285
@@ -535,7 +535,8 @@ set(FIRMWARE_TARGET_SRC
|
||||
${LED_DRIVER}
|
||||
${LCD_DRIVER}
|
||||
board.cpp
|
||||
i2c_driver.cpp
|
||||
volume_i2c.cpp
|
||||
eeprom_driver.cpp
|
||||
backlight_driver.cpp
|
||||
trainer_driver.cpp
|
||||
)
|
||||
|
||||
@@ -105,7 +105,6 @@ void boardInit()
|
||||
AUDIO_RCC_AHB1Periph |
|
||||
BACKLIGHT_RCC_AHB1Periph |
|
||||
ADC_RCC_AHB1Periph |
|
||||
I2C_B1_RCC_AHB1Periph |
|
||||
SD_RCC_AHB1Periph |
|
||||
HAPTIC_RCC_AHB1Periph |
|
||||
INTMODULE_RCC_AHB1Periph |
|
||||
@@ -128,7 +127,6 @@ void boardInit()
|
||||
HAPTIC_RCC_APB1Periph |
|
||||
INTERRUPT_xMS_RCC_APB1Periph |
|
||||
TIMER_2MHz_RCC_APB1Periph |
|
||||
I2C_B1_RCC_APB1Periph |
|
||||
SD_RCC_APB1Periph |
|
||||
TRAINER_RCC_APB1Periph |
|
||||
TELEMETRY_RCC_APB1Periph |
|
||||
@@ -238,7 +236,6 @@ void boardInit()
|
||||
init2MhzTimer();
|
||||
init1msTimer();
|
||||
__enable_irq();
|
||||
i2cInit();
|
||||
usbInit();
|
||||
|
||||
#if defined(DEBUG)
|
||||
|
||||
@@ -749,14 +749,6 @@ uint8_t isBacklightEnabled();
|
||||
}
|
||||
#endif
|
||||
|
||||
// I2C driver: EEPROM + Audio Volume
|
||||
#define EEPROM_SIZE (32*1024)
|
||||
|
||||
void i2cInit();
|
||||
void eepromReadBlock(uint8_t * buffer, size_t address, size_t size);
|
||||
void eepromStartWrite(uint8_t * buffer, size_t address, size_t size);
|
||||
uint8_t eepromIsTransferComplete();
|
||||
|
||||
// Debug driver
|
||||
void debugPutc(const char c);
|
||||
|
||||
|
||||
@@ -0,0 +1,171 @@
|
||||
/*
|
||||
* Copyright (C) EdgeTX
|
||||
*
|
||||
* Based on code named
|
||||
* opentx - https://github.com/opentx/opentx
|
||||
* th9x - http://code.google.com/p/th9x
|
||||
* er9x - http://code.google.com/p/er9x
|
||||
* gruvin9x - http://code.google.com/p/gruvin9x
|
||||
*
|
||||
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License version 2 as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This program 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 General Public License for more details.
|
||||
*/
|
||||
|
||||
#include "stm32_hal_ll.h"
|
||||
#include "stm32_hal.h"
|
||||
#include "stm32_i2c_driver.h"
|
||||
#include "hal.h"
|
||||
|
||||
#include "hal/eeprom_driver.h"
|
||||
|
||||
static int enable_wp_clock(GPIO_TypeDef *GPIOx)
|
||||
{
|
||||
if (GPIOx == GPIOB)
|
||||
__HAL_RCC_GPIOB_CLK_ENABLE();
|
||||
else if (GPIOx == GPIOD)
|
||||
__HAL_RCC_GPIOD_CLK_ENABLE();
|
||||
else if (GPIOx == GPIOF)
|
||||
__HAL_RCC_GPIOF_CLK_ENABLE();
|
||||
else
|
||||
return -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void init_wp_pin()
|
||||
{
|
||||
enable_wp_clock(EEPROM_WP_GPIO);
|
||||
|
||||
LL_GPIO_InitTypeDef pinInit;
|
||||
LL_GPIO_StructInit(&pinInit);
|
||||
|
||||
pinInit.Pin = EEPROM_WP_GPIO_PIN;
|
||||
pinInit.Speed = LL_GPIO_SPEED_FREQ_LOW;
|
||||
pinInit.Mode = LL_GPIO_MODE_OUTPUT;
|
||||
pinInit.OutputType = LL_GPIO_OUTPUT_OPENDRAIN;
|
||||
pinInit.Pull = LL_GPIO_PULL_UP;
|
||||
LL_GPIO_Init(EEPROM_WP_GPIO, &pinInit);
|
||||
|
||||
// Enable write operations on EEPROM
|
||||
LL_GPIO_ResetOutputPin(EEPROM_WP_GPIO, EEPROM_WP_GPIO_PIN);
|
||||
}
|
||||
|
||||
void eepromInit()
|
||||
{
|
||||
stm32_i2c_deinit(EEPROM_I2C_BUS);
|
||||
stm32_i2c_init(EEPROM_I2C_BUS, I2C_B1_CLK_RATE);
|
||||
init_wp_pin();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Wait for EEPROM Standby state
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static bool I2C_EE_WaitEepromStandbyState(void)
|
||||
{
|
||||
if (stm32_i2c_is_dev_ready(EEPROM_I2C_BUS, EEPROM_I2C_ADDRESS, 20 /* 20ms */) < 0)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static void eepromWaitEepromStandbyState(void)
|
||||
{
|
||||
// TODO: seriously? no timeout, no bailout ???
|
||||
while (!I2C_EE_WaitEepromStandbyState()) {
|
||||
eepromInit();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Reads a block of data from the EEPROM.
|
||||
* @param pBuffer : pointer to the buffer that receives the data read
|
||||
* from the EEPROM.
|
||||
* @param ReadAddr : EEPROM's internal address to read from.
|
||||
* @param NumByteToRead : number of bytes to read from the EEPROM.
|
||||
* @retval None
|
||||
*/
|
||||
static bool I2C_EE_ReadBlock(uint8_t* pBuffer, uint16_t ReadAddr, uint16_t NumByteToRead)
|
||||
{
|
||||
if (stm32_i2c_read(EEPROM_I2C_BUS, EEPROM_I2C_ADDRESS, ReadAddr, 1,
|
||||
pBuffer, NumByteToRead, 100) < 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void eepromReadBlock(uint8_t * buffer, size_t address, size_t size)
|
||||
{
|
||||
while (!I2C_EE_ReadBlock(buffer, address, size)) {
|
||||
eepromInit();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Writes more than one byte to the EEPROM with a single WRITE cycle.
|
||||
* @note The number of byte can't exceed the EEPROM page size.
|
||||
* @param pBuffer : pointer to the buffer containing the data to be
|
||||
* written to the EEPROM.
|
||||
* @param WriteAddr : EEPROM's internal address to write to.
|
||||
* @param NumByteToWrite : number of bytes to write to the EEPROM.
|
||||
* @retval None
|
||||
*/
|
||||
static bool I2C_EE_PageWrite(uint8_t * pBuffer, uint16_t WriteAddr, uint8_t NumByteToWrite)
|
||||
{
|
||||
if (stm32_i2c_write(EEPROM_I2C_BUS, EEPROM_I2C_ADDRESS, WriteAddr, 1,
|
||||
pBuffer, NumByteToWrite, 100) < 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static void eepromPageWrite(uint8_t* pBuffer, uint16_t WriteAddr, uint8_t NumByteToWrite)
|
||||
{
|
||||
while (!I2C_EE_PageWrite(pBuffer, WriteAddr, NumByteToWrite)) {
|
||||
eepromInit();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Writes buffer of data to the I2C EEPROM.
|
||||
* @param buffer : pointer to the buffer containing the data to be
|
||||
* written to the EEPROM.
|
||||
* @param address : EEPROM's internal address to write to.
|
||||
* @param size : number of bytes to write to the EEPROM.
|
||||
* @retval None
|
||||
*/
|
||||
void eepromWriteBlock(uint8_t * buffer, size_t address, size_t size)
|
||||
{
|
||||
uint8_t offset = address % EEPROM_PAGESIZE;
|
||||
uint8_t count = EEPROM_PAGESIZE - offset;
|
||||
if (size < count) {
|
||||
count = size;
|
||||
}
|
||||
while (count > 0) {
|
||||
eepromPageWrite(buffer, address, count);
|
||||
eepromWaitEepromStandbyState();
|
||||
address += count;
|
||||
buffer += count;
|
||||
size -= count;
|
||||
count = EEPROM_PAGESIZE;
|
||||
if (size < EEPROM_PAGESIZE) {
|
||||
count = size;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t eepromIsTransferComplete()
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
@@ -2105,60 +2105,52 @@
|
||||
#endif
|
||||
#define LCD_RCC_APB2Periph 0
|
||||
|
||||
// I2C Bus: EEPROM and CAT5137 digital pot for volume control
|
||||
#define I2C_B1_RCC_APB1Periph RCC_APB1Periph_I2C1
|
||||
// I2C Bus 1: EEPROM and CAT5137 digital pot for volume control
|
||||
#define I2C_B1 I2C1
|
||||
#define I2C_B1_GPIO_AF GPIO_AF_I2C1
|
||||
#if defined(PCBXLITE) || defined(PCBX9LITE)
|
||||
#define I2C_B1_RCC_AHB1Periph (RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOD)
|
||||
#define I2C_B1_SPI_GPIO GPIOB
|
||||
#define I2C_B1_SDA_GPIO_PIN GPIO_Pin_9 // PB.09
|
||||
#define I2C_B1_SCL_GPIO_PIN GPIO_Pin_8 // PB.08
|
||||
#define I2C_B1_WP_GPIO GPIOD
|
||||
#define I2C_B1_WP_GPIO_PIN GPIO_Pin_7 // PD.07
|
||||
#define I2C_B1_SCL_GPIO_PinSource GPIO_PinSource8
|
||||
#define I2C_B1_SDA_GPIO_PinSource GPIO_PinSource9
|
||||
#elif defined(PCBX7ACCESS)
|
||||
#define I2C_B1_RCC_AHB1Periph RCC_AHB1Periph_GPIOB
|
||||
#define I2C_B1_SPI_GPIO GPIOB
|
||||
#define I2C_B1_SDA_GPIO_PIN GPIO_Pin_9 // PB.09
|
||||
#define I2C_B1_SCL_GPIO_PIN GPIO_Pin_8 // PB.08
|
||||
#define I2C_B1_WP_GPIO GPIOB
|
||||
#define I2C_B1_WP_GPIO_PIN GPIO_Pin_5 // PB.05
|
||||
#define I2C_B1_SDA_GPIO_PinSource GPIO_PinSource9
|
||||
#define I2C_B1_SCL_GPIO_PinSource GPIO_PinSource8
|
||||
#elif defined(RADIO_ZORRO)
|
||||
#define I2C_B1_RCC_AHB1Periph RCC_AHB1Periph_GPIOB
|
||||
#define I2C_B1_SPI_GPIO GPIOB
|
||||
#define I2C_B1_SDA_GPIO_PIN GPIO_Pin_9 // PB.09
|
||||
#define I2C_B1_SCL_GPIO_PIN GPIO_Pin_8 // PB.08
|
||||
#define I2C_B1_WP_GPIO GPIOD
|
||||
#define I2C_B1_WP_GPIO_PIN GPIO_Pin_10 // PD.10
|
||||
#define I2C_B1_SDA_GPIO_PinSource GPIO_PinSource9
|
||||
#define I2C_B1_SCL_GPIO_PinSource GPIO_PinSource8
|
||||
#elif defined(RADIO_X9DP2019)
|
||||
#define I2C_B1_RCC_AHB1Periph (RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOF)
|
||||
#define I2C_B1_SPI_GPIO GPIOB
|
||||
#define I2C_B1_SDA_GPIO_PIN GPIO_Pin_9 // PB.09
|
||||
#define I2C_B1_SCL_GPIO_PIN GPIO_Pin_8 // PB.08
|
||||
#define I2C_B1_WP_GPIO GPIOF
|
||||
#define I2C_B1_WP_GPIO_PIN GPIO_Pin_0 // PF.00
|
||||
#define I2C_B1_SCL_GPIO_PinSource GPIO_PinSource8
|
||||
#define I2C_B1_SDA_GPIO_PinSource GPIO_PinSource9
|
||||
#define I2C_B1_GPIO_AF LL_GPIO_AF_4
|
||||
|
||||
#if defined(PCBXLITE) || defined(PCBX9LITE) || defined(PCBX7ACCESS) || \
|
||||
defined(RADIO_ZORRO) || defined(RADIO_X9DP2019)
|
||||
#define I2C_B1_GPIO GPIOB
|
||||
#define I2C_B1_SDA_GPIO_PIN LL_GPIO_PIN_9 // PB.09
|
||||
#define I2C_B1_SCL_GPIO_PIN LL_GPIO_PIN_8 // PB.08
|
||||
#else
|
||||
#define I2C_B1_RCC_AHB1Periph RCC_AHB1Periph_GPIOB
|
||||
#define I2C_B1_SPI_GPIO GPIOB
|
||||
#define I2C_B1_SCL_GPIO_PIN GPIO_Pin_6 // PB.06
|
||||
#define I2C_B1_SDA_GPIO_PIN GPIO_Pin_7 // PB.07
|
||||
#define I2C_B1_WP_GPIO GPIOB
|
||||
#define I2C_B1_WP_GPIO_PIN GPIO_Pin_9 // PB.09
|
||||
#define I2C_B1_SCL_GPIO_PinSource GPIO_PinSource6
|
||||
#define I2C_B1_SDA_GPIO_PinSource GPIO_PinSource7
|
||||
#define I2C_ADDRESS_VOLUME 0x5C
|
||||
#define I2C_B1_GPIO GPIOB
|
||||
#define I2C_B1_SCL_GPIO_PIN LL_GPIO_PIN_6 // PB.06
|
||||
#define I2C_B1_SDA_GPIO_PIN LL_GPIO_PIN_7 // PB.07
|
||||
#endif
|
||||
|
||||
// EEPROM
|
||||
#if defined(PCBXLITE) || defined(PCBX9LITE)
|
||||
#define EEPROM_WP_GPIO GPIOD
|
||||
#define EEPROM_WP_GPIO_PIN LL_GPIO_PIN_7 // PD.07
|
||||
#elif defined(PCBX7ACCESS)
|
||||
#define EEPROM_WP_GPIO GPIOB
|
||||
#define EEPROM_WP_GPIO_PIN LL_GPIO_PIN_5 // PB.05
|
||||
#elif defined(RADIO_ZORRO)
|
||||
#define EEPROM_WP_GPIO GPIOD
|
||||
#define EEPROM_WP_GPIO_PIN LL_GPIO_PIN_10 // PD.10
|
||||
#elif defined(RADIO_X9DP2019)
|
||||
#define EEPROM_WP_GPIO GPIOF
|
||||
#define EEPROM_WP_GPIO_PIN LL_GPIO_PIN_0 // PF.00
|
||||
#else
|
||||
#define EEPROM_WP_GPIO GPIOB
|
||||
#define EEPROM_WP_GPIO_PIN LL_GPIO_PIN_9 // PB.09
|
||||
#endif
|
||||
|
||||
// I2C Volume control
|
||||
#if !defined(SOFTWARE_VOLUME)
|
||||
#define VOLUME_I2C_ADDRESS 0x2E
|
||||
#define VOLUME_I2C_BUS I2C_Bus_1
|
||||
#endif
|
||||
|
||||
#define I2C_B1_CLK_RATE 400000
|
||||
#define I2C_ADDRESS_EEPROM 0xA2
|
||||
#define I2C_FLASH_PAGESIZE 64
|
||||
|
||||
// EEPROM
|
||||
#define EEPROM_I2C_ADDRESS 0x51
|
||||
#define EEPROM_I2C_BUS I2C_Bus_1
|
||||
#define EEPROM_PAGESIZE 64
|
||||
#define EEPROM_SIZE (32*1024)
|
||||
|
||||
// Second I2C Bus: IMU
|
||||
#if defined(PCBXLITES)
|
||||
@@ -2166,12 +2158,10 @@
|
||||
#define I2C_B2_RCC_APB1Periph RCC_APB1Periph_I2C3
|
||||
#define I2C_B2 I2C3
|
||||
#define I2C_B2_SCL_GPIO GPIOA
|
||||
#define I2C_B2_SCL_GPIO_PIN GPIO_Pin_8 // PA.08
|
||||
#define I2C_B2_SCL_GPIO_PIN LL_GPIO_PIN_8 // PA.08
|
||||
#define I2C_B2_SDA_GPIO GPIOC
|
||||
#define I2C_B2_SDA_GPIO_PIN GPIO_Pin_9 // PC.09
|
||||
#define I2C_B2_GPIO_AF GPIO_AF_I2C3
|
||||
#define I2C_B2_SCL_GPIO_PinSource GPIO_PinSource8
|
||||
#define I2C_B2_SDA_GPIO_PinSource GPIO_PinSource9
|
||||
#define I2C_B2_SDA_GPIO_PIN LL_GPIO_PIN_9 // PC.09
|
||||
#define I2C_B2_GPIO_AF LL_GPIO_AF_4
|
||||
#define I2C_B2_SPEED 400000
|
||||
#else
|
||||
#define I2C_B2_RCC_AHB1Periph 0
|
||||
|
||||
@@ -1,293 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) EdgeTX
|
||||
*
|
||||
* Based on code named
|
||||
* opentx - https://github.com/opentx/opentx
|
||||
* th9x - http://code.google.com/p/th9x
|
||||
* er9x - http://code.google.com/p/er9x
|
||||
* gruvin9x - http://code.google.com/p/gruvin9x
|
||||
*
|
||||
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License version 2 as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This program 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 General Public License for more details.
|
||||
*/
|
||||
|
||||
#include "board.h"
|
||||
|
||||
void eepromPageWrite(uint8_t* pBuffer, uint16_t WriteAddr, uint8_t NumByteToWrite);
|
||||
void eepromWaitEepromStandbyState(void);
|
||||
|
||||
void i2cInit()
|
||||
{
|
||||
I2C_DeInit(I2C_B1);
|
||||
|
||||
GPIO_InitTypeDef GPIO_InitStructure;
|
||||
GPIO_InitStructure.GPIO_Pin = I2C_B1_WP_GPIO_PIN;
|
||||
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
|
||||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
|
||||
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
|
||||
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
|
||||
GPIO_Init(I2C_B1_WP_GPIO, &GPIO_InitStructure);
|
||||
GPIO_ResetBits(I2C_B1_WP_GPIO, I2C_B1_WP_GPIO_PIN);
|
||||
|
||||
I2C_InitTypeDef I2C_InitStructure;
|
||||
I2C_InitStructure.I2C_ClockSpeed = I2C_B1_CLK_RATE;
|
||||
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
|
||||
I2C_InitStructure.I2C_OwnAddress1 = 0x00;
|
||||
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
|
||||
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
|
||||
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
|
||||
I2C_Init(I2C_B1, &I2C_InitStructure);
|
||||
I2C_Cmd(I2C_B1, ENABLE);
|
||||
|
||||
GPIO_PinAFConfig(I2C_B1_SPI_GPIO, I2C_B1_SCL_GPIO_PinSource, I2C_B1_GPIO_AF);
|
||||
GPIO_PinAFConfig(I2C_B1_SPI_GPIO, I2C_B1_SDA_GPIO_PinSource, I2C_B1_GPIO_AF);
|
||||
|
||||
GPIO_InitStructure.GPIO_Pin = I2C_B1_SCL_GPIO_PIN | I2C_B1_SDA_GPIO_PIN;
|
||||
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
|
||||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
|
||||
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
|
||||
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
|
||||
GPIO_Init(I2C_B1_SPI_GPIO, &GPIO_InitStructure);
|
||||
}
|
||||
|
||||
#define I2C_TIMEOUT_MAX 1000
|
||||
bool I2C_WaitEvent(uint32_t event)
|
||||
{
|
||||
uint32_t timeout = I2C_TIMEOUT_MAX;
|
||||
while (!I2C_CheckEvent(I2C_B1, event)) {
|
||||
if ((timeout--) == 0) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool I2C_WaitEventCleared(uint32_t event)
|
||||
{
|
||||
uint32_t timeout = I2C_TIMEOUT_MAX;
|
||||
while (I2C_CheckEvent(I2C_B1, event)) {
|
||||
if ((timeout--) == 0) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Reads a block of data from the EEPROM.
|
||||
* @param pBuffer : pointer to the buffer that receives the data read
|
||||
* from the EEPROM.
|
||||
* @param ReadAddr : EEPROM's internal address to read from.
|
||||
* @param NumByteToRead : number of bytes to read from the EEPROM.
|
||||
* @retval None
|
||||
*/
|
||||
bool I2C_EE_ReadBlock(uint8_t* pBuffer, uint16_t ReadAddr, uint16_t NumByteToRead)
|
||||
{
|
||||
if (!I2C_WaitEventCleared(I2C_FLAG_BUSY))
|
||||
return false;
|
||||
|
||||
I2C_GenerateSTART(I2C_B1, ENABLE);
|
||||
if (!I2C_WaitEvent(I2C_EVENT_MASTER_MODE_SELECT))
|
||||
return false;
|
||||
|
||||
I2C_Send7bitAddress(I2C_B1, I2C_ADDRESS_EEPROM, I2C_Direction_Transmitter);
|
||||
if (!I2C_WaitEvent(I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
|
||||
return false;
|
||||
|
||||
I2C_SendData(I2C_B1, (uint8_t)((ReadAddr & 0xFF00) >> 8));
|
||||
if (!I2C_WaitEvent(I2C_EVENT_MASTER_BYTE_TRANSMITTING))
|
||||
return false;
|
||||
I2C_SendData(I2C_B1, (uint8_t)(ReadAddr & 0x00FF));
|
||||
if (!I2C_WaitEvent(I2C_EVENT_MASTER_BYTE_TRANSMITTED))
|
||||
return false;
|
||||
|
||||
I2C_GenerateSTART(I2C_B1, ENABLE);
|
||||
if (!I2C_WaitEvent(I2C_EVENT_MASTER_MODE_SELECT))
|
||||
return false;
|
||||
|
||||
I2C_Send7bitAddress(I2C_B1, I2C_ADDRESS_EEPROM, I2C_Direction_Receiver);
|
||||
if (!I2C_WaitEvent(I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED))
|
||||
return false;
|
||||
|
||||
if (NumByteToRead > 1) {
|
||||
I2C_AcknowledgeConfig(I2C_B1, ENABLE);
|
||||
}
|
||||
|
||||
while (NumByteToRead) {
|
||||
if (NumByteToRead == 1) {
|
||||
I2C_AcknowledgeConfig(I2C_B1, DISABLE);
|
||||
}
|
||||
if (!I2C_WaitEvent(I2C_EVENT_MASTER_BYTE_RECEIVED))
|
||||
return false;
|
||||
*pBuffer++ = I2C_ReceiveData(I2C_B1);
|
||||
NumByteToRead--;
|
||||
}
|
||||
|
||||
I2C_GenerateSTOP(I2C_B1, ENABLE);
|
||||
return true;
|
||||
}
|
||||
|
||||
void eepromReadBlock(uint8_t * buffer, size_t address, size_t size)
|
||||
{
|
||||
while (!I2C_EE_ReadBlock(buffer, address, size)) {
|
||||
i2cInit();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Writes buffer of data to the I2C EEPROM.
|
||||
* @param buffer : pointer to the buffer containing the data to be
|
||||
* written to the EEPROM.
|
||||
* @param address : EEPROM's internal address to write to.
|
||||
* @param size : number of bytes to write to the EEPROM.
|
||||
* @retval None
|
||||
*/
|
||||
void eepromWriteBlock(uint8_t * buffer, size_t address, size_t size)
|
||||
{
|
||||
uint8_t offset = address % I2C_FLASH_PAGESIZE;
|
||||
uint8_t count = I2C_FLASH_PAGESIZE - offset;
|
||||
if (size < count) {
|
||||
count = size;
|
||||
}
|
||||
while (count > 0) {
|
||||
eepromPageWrite(buffer, address, count);
|
||||
eepromWaitEepromStandbyState();
|
||||
address += count;
|
||||
buffer += count;
|
||||
size -= count;
|
||||
count = I2C_FLASH_PAGESIZE;
|
||||
if (size < I2C_FLASH_PAGESIZE) {
|
||||
count = size;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t eepromIsTransferComplete()
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Writes more than one byte to the EEPROM with a single WRITE cycle.
|
||||
* @note The number of byte can't exceed the EEPROM page size.
|
||||
* @param pBuffer : pointer to the buffer containing the data to be
|
||||
* written to the EEPROM.
|
||||
* @param WriteAddr : EEPROM's internal address to write to.
|
||||
* @param NumByteToWrite : number of bytes to write to the EEPROM.
|
||||
* @retval None
|
||||
*/
|
||||
bool I2C_EE_PageWrite(uint8_t * pBuffer, uint16_t WriteAddr, uint8_t NumByteToWrite)
|
||||
{
|
||||
if (!I2C_WaitEventCleared(I2C_FLAG_BUSY))
|
||||
return false;
|
||||
|
||||
I2C_GenerateSTART(I2C_B1, ENABLE);
|
||||
if (!I2C_WaitEvent(I2C_EVENT_MASTER_MODE_SELECT))
|
||||
return false;
|
||||
|
||||
I2C_Send7bitAddress(I2C_B1, I2C_ADDRESS_EEPROM, I2C_Direction_Transmitter);
|
||||
if (!I2C_WaitEvent(I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
|
||||
return false;
|
||||
|
||||
I2C_SendData(I2C_B1, (uint8_t)((WriteAddr & 0xFF00) >> 8));
|
||||
if (!I2C_WaitEvent(I2C_EVENT_MASTER_BYTE_TRANSMITTING))
|
||||
return false;
|
||||
I2C_SendData(I2C_B1, (uint8_t)(WriteAddr & 0x00FF));
|
||||
if (!I2C_WaitEvent(I2C_EVENT_MASTER_BYTE_TRANSMITTING))
|
||||
return false;
|
||||
|
||||
/* While there is data to be written */
|
||||
while (NumByteToWrite--) {
|
||||
I2C_SendData(I2C_B1, *pBuffer);
|
||||
if (!I2C_WaitEvent(I2C_EVENT_MASTER_BYTE_TRANSMITTING))
|
||||
return false;
|
||||
pBuffer++;
|
||||
}
|
||||
|
||||
if (!I2C_WaitEvent(I2C_EVENT_MASTER_BYTE_TRANSMITTED))
|
||||
return false;
|
||||
|
||||
I2C_GenerateSTOP(I2C_B1, ENABLE);
|
||||
return true;
|
||||
}
|
||||
|
||||
void eepromPageWrite(uint8_t* pBuffer, uint16_t WriteAddr, uint8_t NumByteToWrite)
|
||||
{
|
||||
while (!I2C_EE_PageWrite(pBuffer, WriteAddr, NumByteToWrite)) {
|
||||
i2cInit();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Wait for EEPROM Standby state
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
bool I2C_EE_WaitEepromStandbyState(void)
|
||||
{
|
||||
do {
|
||||
I2C_GenerateSTART(I2C_B1, ENABLE);
|
||||
if (!I2C_WaitEvent(I2C_EVENT_MASTER_MODE_SELECT))
|
||||
return false;
|
||||
|
||||
I2C_Send7bitAddress(I2C_B1, I2C_ADDRESS_EEPROM, I2C_Direction_Transmitter);
|
||||
} while (!I2C_WaitEvent(I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
|
||||
|
||||
I2C_GenerateSTOP(I2C_B1, ENABLE);
|
||||
return true;
|
||||
}
|
||||
|
||||
void eepromWaitEepromStandbyState(void)
|
||||
{
|
||||
while (!I2C_EE_WaitEepromStandbyState()) {
|
||||
i2cInit();
|
||||
}
|
||||
}
|
||||
|
||||
#if !defined(BOOT) && !defined(SOFTWARE_VOLUME)
|
||||
const uint8_t volumeScale[VOLUME_LEVEL_MAX+1] = {
|
||||
0, 1, 2, 3, 5, 9, 13, 17, 22, 27, 33, 40,
|
||||
64, 82, 96, 105, 112, 117, 120, 122, 124, 125, 126, 127
|
||||
};
|
||||
|
||||
void setScaledVolume(uint8_t volume)
|
||||
{
|
||||
if (volume > VOLUME_LEVEL_MAX) {
|
||||
volume = VOLUME_LEVEL_MAX;
|
||||
}
|
||||
|
||||
setVolume(volumeScale[volume]);
|
||||
}
|
||||
|
||||
void setVolume(uint8_t volume)
|
||||
{
|
||||
if (!I2C_WaitEventCleared(I2C_FLAG_BUSY))
|
||||
return;
|
||||
|
||||
I2C_GenerateSTART(I2C_B1, ENABLE);
|
||||
if (!I2C_WaitEvent(I2C_EVENT_MASTER_MODE_SELECT))
|
||||
return;
|
||||
|
||||
I2C_Send7bitAddress(I2C_B1, I2C_ADDRESS_VOLUME, I2C_Direction_Transmitter);
|
||||
if (!I2C_WaitEvent(I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
|
||||
return;
|
||||
|
||||
I2C_SendData(I2C_B1, 0);
|
||||
if (!I2C_WaitEvent(I2C_EVENT_MASTER_BYTE_TRANSMITTING))
|
||||
return;
|
||||
I2C_SendData(I2C_B1, volume);
|
||||
if (!I2C_WaitEvent(I2C_EVENT_MASTER_BYTE_TRANSMITTED))
|
||||
return;
|
||||
|
||||
I2C_GenerateSTOP(I2C_B1, ENABLE);
|
||||
}
|
||||
|
||||
int32_t getVolume()
|
||||
{
|
||||
return -1; // TODO
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Copyright (C) EdgeTX
|
||||
*
|
||||
* Based on code named
|
||||
* opentx - https://github.com/opentx/opentx
|
||||
* th9x - http://code.google.com/p/th9x
|
||||
* er9x - http://code.google.com/p/er9x
|
||||
* gruvin9x - http://code.google.com/p/gruvin9x
|
||||
*
|
||||
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License version 2 as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This program 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 General Public License for more details.
|
||||
*/
|
||||
|
||||
#if !defined(SOFTWARE_VOLUME)
|
||||
|
||||
#include "stm32_i2c_driver.h"
|
||||
#include "board.h"
|
||||
|
||||
static const uint8_t volumeScale[VOLUME_LEVEL_MAX+1] = {
|
||||
0, 1, 2, 3, 5, 9, 13, 17, 22, 27, 33, 40,
|
||||
64, 82, 96, 105, 112, 117, 120, 122, 124, 125, 126, 127
|
||||
};
|
||||
|
||||
int32_t getVolume()
|
||||
{
|
||||
uint8_t value = 0;
|
||||
if (stm32_i2c_read(VOLUME_I2C_BUS, VOLUME_I2C_ADDRESS, 0, 1, &value, 1, 10) < 0)
|
||||
return -1;
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
void setVolume(uint8_t volume)
|
||||
{
|
||||
// stm32_i2c_init(VOLUME_I2C_BUS, I2C_B1_CLK_RATE);
|
||||
stm32_i2c_write(VOLUME_I2C_BUS, VOLUME_I2C_ADDRESS, 0, 1, &volume, 1, 10);
|
||||
}
|
||||
|
||||
void setScaledVolume(uint8_t volume)
|
||||
{
|
||||
if (volume > VOLUME_LEVEL_MAX) {
|
||||
volume = VOLUME_LEVEL_MAX;
|
||||
}
|
||||
|
||||
setVolume(volumeScale[volume]);
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user