/* Copyright (C) Dean Camera, 2017. dean [at] fourwalledcubicle [dot] com www.lufa-lib.org */ #include "RTC.h" #if defined(DUMMY_RTC) /** Current dummy RTC time and date */ static volatile TimeDate_t DummyRTC_Count; void RTC_Init(void) { DummyRTC_Count.Hour = 0; DummyRTC_Count.Minute = 0; DummyRTC_Count.Second = 0; DummyRTC_Count.Day = 1; DummyRTC_Count.Month = 1; DummyRTC_Count.Year = 00; } void RTC_Tick500ms(void) { static bool HalfSecondElapsed = false; HalfSecondElapsed = !HalfSecondElapsed; if (HalfSecondElapsed == false) return; if (++DummyRTC_Count.Second < 60) return; DummyRTC_Count.Second = 0; if (++DummyRTC_Count.Minute < 60) return; DummyRTC_Count.Minute = 0; if (++DummyRTC_Count.Hour < 24) return; DummyRTC_Count.Hour = 0; static const char MonthLength[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; uint8_t DaysInMonth = MonthLength[DummyRTC_Count.Month - 1]; /* Check if we need to account for a leap year */ if ((DummyRTC_Count.Month == 2) && ((!(DummyRTC_Count.Year % 400)) || ((DummyRTC_Count.Year % 100) && !(DummyRTC_Count.Year % 4)))) { DaysInMonth++; } if (++DummyRTC_Count.Day <= DaysInMonth) return; DummyRTC_Count.Day = 1; if (++DummyRTC_Count.Month <= 12) return; DummyRTC_Count.Month = 1; DummyRTC_Count.Year++; } bool RTC_SetTimeDate(const TimeDate_t* NewTimeDate) { GlobalInterruptDisable(); DummyRTC_Count = *NewTimeDate; GlobalInterruptEnable(); return true; } bool RTC_GetTimeDate(TimeDate_t* const TimeDate) { GlobalInterruptDisable(); *TimeDate = DummyRTC_Count; GlobalInterruptEnable(); return true; } #else void RTC_Init(void) { /* Unused for a real external DS1307 RTC device */ } void RTC_Tick500ms(void) { /* Unused for a real external DS1307 RTC device */ } bool RTC_SetTimeDate(const TimeDate_t* NewTimeDate) { DS1307_DateTimeRegs_t NewRegValues; const uint8_t WriteAddress = 0; // Convert new time data to the DS1307's time register layout NewRegValues.Byte1.Fields.TenSec = (NewTimeDate->Second / 10); NewRegValues.Byte1.Fields.Sec = (NewTimeDate->Second % 10); NewRegValues.Byte1.Fields.CH = false; NewRegValues.Byte2.Fields.TenMin = (NewTimeDate->Minute / 10); NewRegValues.Byte2.Fields.Min = (NewTimeDate->Minute % 10); NewRegValues.Byte3.Fields.TenHour = (NewTimeDate->Hour / 10); NewRegValues.Byte3.Fields.Hour = (NewTimeDate->Hour % 10); NewRegValues.Byte3.Fields.TwelveHourMode = false; // Convert new date data to the DS1307's date register layout NewRegValues.Byte4.Fields.DayOfWeek = 0; NewRegValues.Byte5.Fields.TenDay = (NewTimeDate->Day / 10); NewRegValues.Byte5.Fields.Day = (NewTimeDate->Day % 10); NewRegValues.Byte6.Fields.TenMonth = (NewTimeDate->Month / 10); NewRegValues.Byte6.Fields.Month = (NewTimeDate->Month % 10); NewRegValues.Byte7.Fields.TenYear = (NewTimeDate->Year / 10); NewRegValues.Byte7.Fields.Year = (NewTimeDate->Year % 10); // Write the new Time and Date into the DS1307 if (TWI_WritePacket(DS1307_ADDRESS, 10, &WriteAddress, sizeof(WriteAddress), (uint8_t*)&NewRegValues, sizeof(DS1307_DateTimeRegs_t)) != TWI_ERROR_NoError) { return false; } return true; } bool RTC_GetTimeDate(TimeDate_t* const TimeDate) { DS1307_DateTimeRegs_t CurrentRegValues; const uint8_t ReadAddress = 0; // Read in the stored Time and Date from the DS1307 if (TWI_ReadPacket(DS1307_ADDRESS, 10, &ReadAddress, sizeof(ReadAddress), (uint8_t*)&CurrentRegValues, sizeof(DS1307_DateTimeRegs_t)) != TWI_ERROR_NoError) { return false; } // Convert stored time value into decimal TimeDate->Second = (CurrentRegValues.Byte1.Fields.TenSec * 10) + CurrentRegValues.Byte1.Fields.Sec; TimeDate->Minute = (CurrentRegValues.Byte2.Fields.TenMin * 10) + CurrentRegValues.Byte2.Fields.Min; TimeDate->Hour = (CurrentRegValues.Byte3.Fields.TenHour * 10) + CurrentRegValues.Byte3.Fields.Hour; // Convert stored date value into decimal TimeDate->Day = (CurrentRegValues.Byte5.Fields.TenDay * 10) + CurrentRegValues.Byte5.Fields.Day; TimeDate->Month = (CurrentRegValues.Byte6.Fields.TenMonth * 10) + CurrentRegValues.Byte6.Fields.Month; TimeDate->Year = (CurrentRegValues.Byte7.Fields.TenYear * 10) + CurrentRegValues.Byte7.Fields.Year; return true; } #endif 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
/*
The MIT License (MIT)

Copyright (c) 2016 Fred Sundvik

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#include "lcd_backlight.h"
#include "hal.h"

#define RED_PIN 1
#define GREEN_PIN 2
#define BLUE_PIN 3
#define CHANNEL_RED FTM0->CHANNEL[0]
#define CHANNEL_GREEN FTM0->CHANNEL[1]
#define CHANNEL_BLUE FTM0->CHANNEL[2]

#define RGB_PORT PORTC
#define RGB_PORT_GPIO GPIOC

// Base FTM clock selection (72 MHz system clock)
// @ 0xFFFF period, 72 MHz / (0xFFFF * 2) = Actual period
// Higher pre-scalar will use the most power (also look the best)
// Pre-scalar calculations
// 0 -      72 MHz -> 549 Hz
// 1 -      36 MHz -> 275 Hz
// 2 -      18 MHz -> 137 Hz
// 3 -       9 MHz ->  69 Hz (Slightly visible flicker)
// 4 -   4 500 kHz ->  34 Hz (Visible flickering)
// 5 -   2 250 kHz ->  17 Hz
// 6 -   1 125 kHz ->   9 Hz
// 7 - 562 500  Hz ->   4 Hz
// Using a higher pre-scalar without flicker is possible but FTM0_MOD will need to be reduced
// Which will reduce the brightness range
#define PRESCALAR_DEFINE 0

void lcd_backlight_hal_init(void) {
	// Setup Backlight
    SIM->SCGC6 |= SIM_SCGC6_FTM0;
    FTM0->CNT = 0; // Reset counter

	// PWM Period
	// 16-bit maximum
	FTM0->MOD = 0xFFFF;

	// Set FTM to PWM output - Edge Aligned, Low-true pulses
#define CNSC_MODE FTM_SC_CPWMS | FTM_SC_PS(4) | FTM_SC_CLKS(0)
	CHANNEL_RED.CnSC = CNSC_MODE;
	CHANNEL_GREEN.CnSC = CNSC_MODE;
	CHANNEL_BLUE.CnSC = CNSC_MODE;

	// System clock, /w prescalar setting
	FTM0->SC = FTM_SC_CLKS(1) | FTM_SC_PS(PRESCALAR_DEFINE);

	CHANNEL_RED.CnV = 0;
	CHANNEL_GREEN.CnV = 0;
	CHANNEL_BLUE.CnV = 0;

	RGB_PORT_GPIO->PDDR |= (1 << RED_PIN);
	RGB_PORT_GPIO->PDDR |= (1 << GREEN_PIN);
	RGB_PORT_GPIO->PDDR |= (1 << BLUE_PIN);

#define RGB_MODE PORTx_PCRn_SRE | PORTx_PCRn_DSE | PORTx_PCRn_MUX(4)
    RGB_PORT->PCR[RED_PIN] = RGB_MODE;
    RGB_PORT->PCR[GREEN_PIN] = RGB_MODE;
    RGB_PORT->PCR[BLUE_PIN] = RGB_MODE;
}

void lcd_backlight_hal_color(uint16_t r, uint16_t g, uint16_t b) {
	CHANNEL_RED.CnV = r;
	CHANNEL_GREEN.CnV = g;
	CHANNEL_BLUE.CnV = b;
}