Skip to content

Commit

Permalink
Introduced eeprom module, along with:
Browse files Browse the repository at this point in the history
>Changed the linker flag to have multiplication possible
>Changed the build script to reuse the same command to build emulator and flash target
  • Loading branch information
Abhijith Sriram committed Oct 11, 2024
1 parent a35c69a commit 2df281f
Show file tree
Hide file tree
Showing 13 changed files with 107 additions and 18 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 1,4 @@
vsCode_workspace.code-workspace
out/
.vscode/
.vscode/
eeprom.hex
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 31,12 @@ elseif(${BUILD_TARGET} MATCHES "MCU")
set(FINAL_EXE_FILE ${CMAKE_CURRENT_BINARY_DIR}/final_exec.elf)
set(FINAL_TARGET_FILE ${CMAKE_CURRENT_BINARY_DIR}/final_exec.hex)
add_executable(${OUTPUT_TARGET} ${CMAKE_CURRENT_BINARY_DIR}/dummy.c)
# Check https://stackoverflow.com/questions/21735624/c-application-linking-failing-with-undefined-reference-to-mulhi3
# for the reason to add a new library path
set_target_properties(${OUTPUT_TARGET} PROPERTIES
OUTPUT_NAME ${FINAL_EXE_FILE}
COMPILE_FLAGS "-mmcu=atmega328p -g"
LINK_FLAGS "-mmcu=atmega328p")
LINK_FLAGS "-mmcu=atmega328p -L${LINKER_LIB_LOC}")
target_link_libraries(${OUTPUT_TARGET} PRIVATE APPL)

# convert the final executable into binary for flashing
Expand Down
2 changes: 2 additions & 0 deletions arduino.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 12,8 @@ set(CMAKE_OBJCOPY "avr-objcopy")
set(CMAKE_SIZE "avr-size")
set(CMAKE_STRIP "avr-strip")
set(CMAKE_FIND_ROOT_PATH "C:\\Program Files (x86)\\WinAVR\\bin")
set(LINKER_LIB_LOC "\"$ENV{ProgramFiles\(x86\)}\\WinAVR\\lib\\gcc\\avr\\4.3.3\"")
set(CMAKE_VERBOSE_MAKEFILE ON)

# target information
set(CMAKE_SYSTEM_NAME Generic)
Expand Down
22 changes: 14 additions & 8 deletions build.bat
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 6,8 @@ IF EXIST ".\out\" (
rmdir .\out /q /s
)

SET CMAKE_ARGS=-DAPPL_TO_BUILD
SET CMAKE_ARGS=-S %~dp0 -B %~dp0\out\build -DAPPL_TO_BUILD

@REM Get the application name to build
IF [%~1] == [] (
goto display_help
Expand All @@ -19,21 20,26 @@ IF [%~2] == [] (
) ELSE IF "%~2" EQU "EMULATOR" (
@REM Building for emulator, output will be a dll file
SET CMAKE_ARGS=!CMAKE_ARGS! -DBUILD_TARGET=EMULATOR
cmake -S %~dp0 -B %~dp0\out\build !CMAKE_ARGS!
cmake --build %~dp0\out\build
goto :eof
goto :build_target

) ELSE IF "%~2" EQU "MCU" (
@REM Building to flash the arduino, output will be .hex file
SET CMAKE_ARGS=!CMAKE_ARGS! -DBUILD_TARGET=MCU
@REM We need to have a different build generator when cross-compiling else the compiler settings
@REM defaults to msvc. If this happens then we cannot build binary for arduino, this is why we use -G argument
cmake -S %~dp0 -B %~dp0\out\build -G"MinGW Makefiles" !CMAKE_ARGS!
make --directory=%~dp0\out\build
goto :eof
SET CMAKE_ARGS=!CMAKE_ARGS! -DBUILD_TARGET=MCU -G"MinGW Makefiles"
goto :build_target

) ELSE (
goto :display_help
)

:build_target
echo Generating the build files
cmake !CMAKE_ARGS!
echo Building the target
cmake --build %~dp0\out\build
goto :eof

:display_help
echo Help for building MCU code
echo The batch script takes two positional arguments.
Expand Down
1 change: 1 addition & 0 deletions src/appl/blinkLed/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 6,5 @@ add_library(blinkLed blinkLed.c)
set_target_properties(blinkLed PROPERTIES FOLDER "appls/blinkLed")
target_include_directories(blinkLed PRIVATE
${CMAKE_SOURCE_DIR}/src/lib/utils
${CMAKE_SOURCE_DIR}/src/lib/eeprom
${CMAKE_SOURCE_DIR}/src/lib/pins)
8 changes: 6 additions & 2 deletions src/appl/blinkLed/blinkLed.c
Original file line number Diff line number Diff line change
@@ -1,20 1,24 @@
#include "utils.h"
#include "pin.h"
#include "eeprom.h"

#define LED_DELAY (uint16_t)300
#define DELAY_TIMER (uint8_t)10
#define DELAY_ADDRESS (uint16_t)0x5FU

int main()
{
/* Configure pin PB5 to output*/
Configure_pinPort(PORTB, (uint8_t)PINB5, OUTPUT);
uint8_t delay_timer = (uint8_t)30;

/* The main loop for the application */
while(1)
{
Write_pin(PORTB, (uint8_t)PINB5, PIN_HIGH);
Delay_ms(LED_DELAY);
Delay_ms((uint16_t)(delay_timer * 10U));
Write_pin(PORTB, (uint8_t)PINB5, PIN_LOW);
Delay_ms(LED_DELAY);
Delay_ms((uint16_t)(delay_timer * 10U));
}
return 0;
}
3 changes: 2 additions & 1 deletion src/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 3,8 @@ cmake_minimum_required(VERSION 3.25.0)
message("Building the lib")
add_subdirectory(utils)
add_subdirectory(pins)
add_subdirectory(eeprom)

file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/lib/dummy_lib.c "")
add_library(LIB STATIC ${CMAKE_CURRENT_BINARY_DIR}/lib/dummy_lib.c)
target_link_libraries(LIB PINS UTILS)
target_link_libraries(LIB PINS UTILS EEPROM)
7 changes: 7 additions & 0 deletions src/lib/eeprom/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 1,7 @@
cmake_minimum_required(VERSION 3.25.0)

message("Building EEPROM")
set(SOURCES eeprom.c)
set(HEADERS eeprom.h)
add_library(EEPROM STATIC eeprom.c)
set_target_properties(EEPROM PROPERTIES FOLDER "lib/eeprom")
43 changes: 43 additions & 0 deletions src/lib/eeprom/eeprom.c
Original file line number Diff line number Diff line change
@@ -0,0 1,43 @@
#include "eeprom.h"

void ReadFromMemory(uint16_t memory_address, uint8_t* data_read)
{
/* Make sure there are no current writing operations going on */
while (GetEepeValue() == (uint8_t)1)
{
/* Wait for eepe value to go to zero */
}
*EEAR = memory_address;
*(EECR) |= (1<<EERE);
*data_read = *EEDR;
}

void WriteToMemory(uint16_t memory_address, uint8_t data_toWrite)
{

/* Make sure there are no current writing operations going on */
while (GetEepeValue() == (uint8_t)1)
{
/* Wait for eepe value to go to zero */
}

/* Write the address and data to the appropriate registers */
*EEAR = memory_address;
*EEDR = data_toWrite;

/* Set value to the control register. Maintain this order! */
*(EECR) |= (1<<EEMPE);
*(EECR) |= (1<<EEPE);

while (GetEepeValue() == (uint8_t)1)
{
/* Wait for eepe value to go to zero */
}
}

static uint8_t GetEepeValue()
{
uint8_t eepe_mask = ((uint8_t) 1 << EEPE);
uint8_t eepe_value = (*(EECR) & eepe_mask) >> EEPE;
return eepe_value;
}
17 changes: 17 additions & 0 deletions src/lib/eeprom/eeprom.h
Original file line number Diff line number Diff line change
@@ -0,0 1,17 @@
#ifndef __EEPROM__
#define __EEPROM__

#include <stdint.h>

#define EEAR ((uint16_t*)0x41U) /* Register containing the memory address to be read or write */
#define EEDR ((uint8_t*)0x40U) /* Register containing the data to be read or written */
#define EECR ((uint8_t*)0x3FU) /* Register to indicate reading or writing operation to eeprom */
#define EEMPE (uint8_t)2
#define EEPE (uint8_t)1
#define EERE (uint8_t)0

void ReadFromMemory(uint16_t memory_address, uint8_t* data_read);
void WriteToMemory(uint16_t memory_address, uint8_t data_toWrite);
static uint8_t GetEepeValue();

#endif
4 changes: 2 additions & 2 deletions src/lib/utils/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 14,7 @@ void Delay_ms(uint16_t delayTime_ms)
{
/* Set the presclaer to 1024. This means for 16MHz the clock register will overflow
after 16ms. See Timer0 control (Section 15) for 0x5U value */
*((uint8_t*)TCCR0B) = 0x5U;
*((uint8_t*)TCCR0B) = 0x3U;
uint16_t num_overflows = (uint16_t)0;
uint8_t current_time = (uint8_t)0;
uint8_t previous_time = (uint8_t)0;
Expand All @@ -34,7 34,7 @@ void Delay_ms(uint16_t delayTime_ms)
/* Do nothing */
}
/* We will stay inside the function until the timer is reached*/
elapsed_time = (MAXTIME*num_overflows);
elapsed_time = (TIME_1MS*num_overflows);
previous_time = current_time;
}
/* Reset the overflow counter to start a new delay function */
Expand Down
6 changes: 3 additions & 3 deletions src/lib/utils/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 3,9 @@

#include <stdint.h>

#define CLK_FREQ_MHZ (uint8_t)16
#define MAXTIME (uint16_t)16
#define CLKSCALE (uint16_t)1024
#define CLK_FREQ_MHZ (uint64_t)16000000
#define CLKSCALE (uint16_t)64
#define TIME_1MS (uint16_t)1

void Delay_ms(uint16_t delayTime_ms);

Expand Down
5 changes: 5 additions & 0 deletions write-eeprom.bat
Original file line number Diff line number Diff line change
@@ -0,0 1,5 @@
@echo off
SETLOCAL EnableDelayedExpansion
cls
@REM https://www.scadacore.com/tools/programming-calculators/online-checksum-calculator/
"C:\Program Files (x86)\WinAVR\bin\avrdude.exe" -C "C:\Program Files (x86)\WinAVR\bin\avrdude.conf" -v -p ATmega328P -P COM3 -c arduino -b 115200 -D -U eeprom:w:eeprom.hex:i

0 comments on commit 2df281f

Please sign in to comment.