2020-08-23 21:40:16 +02:00
|
|
|
/* Reflow Oven Controller
|
|
|
|
*
|
|
|
|
* Copyright (C) 2020 Mario Hüttel <mario.huettel@gmx.net>
|
|
|
|
*
|
|
|
|
* This file is part of the Reflow Oven Controller Project.
|
|
|
|
*
|
|
|
|
* The reflow oven controller 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.
|
|
|
|
*
|
|
|
|
* The Reflow Oven Control Firmware 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.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with the reflow oven controller project.
|
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stm-periph/backup-ram.h>
|
|
|
|
#include <stm-periph/clock-enable-manager.h>
|
|
|
|
#include <stm32/stm32f4xx.h>
|
2020-09-04 21:03:53 +02:00
|
|
|
#include <helper-macros/helper-macros.h>
|
2020-08-23 21:40:16 +02:00
|
|
|
|
2020-09-04 21:03:53 +02:00
|
|
|
#define BACKUP_RAM_BASE BKPSRAM_BASE
|
|
|
|
#define BACKUP_RAM_SIZE 4096U
|
|
|
|
#define BACKUP_RAM_SIZE_WORDS (BACKUP_RAM_SIZE / 4U)
|
|
|
|
#define BACKUP_RAM_END_ADDR (BACKUP_RAM_BASE + BACKUP_RAM_SIZE - 1U)
|
|
|
|
|
|
|
|
#define backup_ram ((volatile uint32_t *)BACKUP_RAM_BASE)
|
|
|
|
|
|
|
|
#if !is_power_of_two(BACKUP_RAM_SIZE)
|
|
|
|
#error "Backup RAM size ahs to be a power of two!"
|
|
|
|
#endif
|
|
|
|
|
2020-09-04 21:33:54 +02:00
|
|
|
void backup_ram_init(bool use_backup_regulator)
|
2020-08-23 21:40:16 +02:00
|
|
|
{
|
|
|
|
rcc_manager_enable_clock(&RCC->APB1ENR, BITMASK_TO_BITNO(RCC_APB1ENR_PWREN));
|
|
|
|
|
|
|
|
/* Enable access to backup RAM register set */
|
|
|
|
PWR->CR |= PWR_CR_DBP;
|
|
|
|
|
2020-09-04 21:33:54 +02:00
|
|
|
if (use_backup_regulator) {
|
|
|
|
/* Enable the backup regulator */
|
|
|
|
PWR->CSR |= PWR_CSR_BRE;
|
2020-09-04 21:03:53 +02:00
|
|
|
|
2020-09-04 21:33:54 +02:00
|
|
|
/* Wait until regulator is ready */
|
|
|
|
while (!(PWR->CSR & PWR_CSR_BRR));
|
|
|
|
}
|
2020-09-04 21:03:53 +02:00
|
|
|
|
2020-08-23 21:40:16 +02:00
|
|
|
/* Enable clock for backup ram interface */
|
|
|
|
rcc_manager_enable_clock(&RCC->AHB1ENR, BITMASK_TO_BITNO(RCC_AHB1ENR_BKPSRAMEN));
|
|
|
|
}
|
|
|
|
|
2020-09-04 21:03:53 +02:00
|
|
|
void backup_ram_disable(void)
|
2020-08-23 21:40:16 +02:00
|
|
|
{
|
2020-09-04 21:03:53 +02:00
|
|
|
/* Disable access to backup RAM register set */
|
|
|
|
PWR->CR &= ~PWR_CR_DBP;
|
2020-08-23 21:40:16 +02:00
|
|
|
rcc_manager_disable_clock(&RCC->APB1ENR, BITMASK_TO_BITNO(RCC_APB1ENR_PWREN));
|
2020-09-04 21:03:53 +02:00
|
|
|
rcc_manager_enable_clock(&RCC->AHB1ENR, BITMASK_TO_BITNO(RCC_AHB1ENR_BKPSRAMEN));
|
2020-08-23 21:40:16 +02:00
|
|
|
}
|
|
|
|
|
2020-09-04 21:03:53 +02:00
|
|
|
void backup_ram_wipe(void)
|
2020-08-23 21:40:16 +02:00
|
|
|
{
|
2020-09-04 21:03:53 +02:00
|
|
|
uint32_t i;
|
2020-08-23 21:40:16 +02:00
|
|
|
|
2020-09-04 21:03:53 +02:00
|
|
|
for (i = 0; i < BACKUP_RAM_SIZE_WORDS; i++)
|
|
|
|
backup_ram[i] = 0UL;
|
2020-08-23 21:40:16 +02:00
|
|
|
}
|
|
|
|
|
2020-09-04 21:03:53 +02:00
|
|
|
int backup_ram_get_data(uint32_t addr, uint32_t *data, uint32_t count)
|
2020-08-23 21:40:16 +02:00
|
|
|
{
|
2020-09-04 21:03:53 +02:00
|
|
|
volatile uint32_t *ptr;
|
2020-08-23 21:40:16 +02:00
|
|
|
|
2020-09-04 21:03:53 +02:00
|
|
|
if (!data)
|
|
|
|
return -1002;
|
|
|
|
if (addr >= BACKUP_RAM_SIZE_WORDS)
|
|
|
|
return -1001;
|
|
|
|
|
|
|
|
ptr = &backup_ram[addr];
|
|
|
|
|
|
|
|
for (; count > 0; count--)
|
|
|
|
*(data++) = *(ptr++);
|
|
|
|
|
|
|
|
return 0;
|
2020-08-23 21:40:16 +02:00
|
|
|
}
|
|
|
|
|
2020-09-04 21:03:53 +02:00
|
|
|
int backup_ram_write_data(uint32_t addr, const uint32_t *data, uint32_t count)
|
2020-08-23 21:40:16 +02:00
|
|
|
{
|
2020-09-04 21:03:53 +02:00
|
|
|
volatile uint32_t *ptr;
|
|
|
|
|
|
|
|
if (!data)
|
|
|
|
return -1002;
|
|
|
|
if (addr >= BACKUP_RAM_SIZE_WORDS)
|
|
|
|
return -1001;
|
|
|
|
|
|
|
|
ptr = &backup_ram[addr];
|
|
|
|
|
|
|
|
for (; count > 0; count--)
|
|
|
|
*(ptr++) = *(data++);
|
2020-08-23 21:40:16 +02:00
|
|
|
|
2020-09-04 21:03:53 +02:00
|
|
|
return 0;
|
2020-08-23 21:40:16 +02:00
|
|
|
}
|
2020-09-05 15:15:31 +02:00
|
|
|
|
|
|
|
uint32_t backup_ram_get_size_in_words(void)
|
|
|
|
{
|
|
|
|
return (uint32_t)BACKUP_RAM_SIZE_WORDS;
|
|
|
|
}
|
2020-11-01 20:43:59 +01:00
|
|
|
|
|
|
|
volatile void *backup_ram_get_base_ptr(void)
|
|
|
|
{
|
|
|
|
return backup_ram;
|
|
|
|
}
|