diff --git a/Core/Inc/ComsRead.h b/Core/Inc/ComsRead.h new file mode 100644 index 0000000..e41781b --- /dev/null +++ b/Core/Inc/ComsRead.h @@ -0,0 +1,17 @@ +/* + * ComsRead.h + * + * Created on: Mar 29, 2026 + * Author: ewars + */ + +#ifndef INC_COMSREAD_H_ +#define INC_COMSREAD_H_ + +void readColour_Hours(uint8_t * data); +void readColour_Minutes(uint8_t * data); +void readColour_Seconds(uint8_t * data); +void readColour_Background(uint8_t * data); + + +#endif /* INC_COMSREAD_H_ */ diff --git a/Core/Inc/ds1307.h b/Core/Inc/ds1307.h index 6bbabfa..75e70bd 100644 --- a/Core/Inc/ds1307.h +++ b/Core/Inc/ds1307.h @@ -96,5 +96,8 @@ void ds1307_config(uint8_t seconds, uint8_t minutes, uint8_t hours,ds1307_days_t void ds1307_log_uart(ds1307_dev_t *ds1307_dev); +uint8_t ds1307_read_user_RAM(uint8_t addr, uint8_t * data, uint8_t len); +uint8_t ds1307_write_user_RAM(uint8_t addr, uint8_t * data, uint8_t len); + #endif /* DS1307_H_ */ diff --git a/Core/Inc/myComms.h b/Core/Inc/myComms.h index 9ad3c43..b4e85a9 100644 --- a/Core/Inc/myComms.h +++ b/Core/Inc/myComms.h @@ -18,6 +18,8 @@ typedef enum { com_setColour_minute, com_setColour_second, com_setColour_background, + com_readNV, + com_writeNV, com_MAX_command } commands_e; diff --git a/Core/Inc/ws2812_effect.h b/Core/Inc/ws2812_effect.h index 7825208..34c5724 100644 --- a/Core/Inc/ws2812_effect.h +++ b/Core/Inc/ws2812_effect.h @@ -15,6 +15,7 @@ void ws_Effect_RoudTheClock(void); void ws_Effect_RandRound(void); void ws_Effect_RandRand(void); +void ws_effect_setDisplayColoursNV(void); void ws_effect_display_time(uint8_t hour, uint8_t minute, uint8_t second); void ws_effect_display_hour(uint8_t hour); diff --git a/Core/Src/ds1307.c b/Core/Src/ds1307.c index b5232c6..2bc472e 100644 --- a/Core/Src/ds1307.c +++ b/Core/Src/ds1307.c @@ -42,6 +42,54 @@ static uint8_t ds1307_read_byte(uint8_t ds1307_reg_addres); static uint8_t ds1307_bcd_decode(uint8_t data); static uint8_t ds1307_bcd_encode(uint8_t data); +/* + * + Function designed to read embedded RAM data. + Address range 0x08 to 0x3F + 56 bytes of data + + addr: address of data to read 0x08 to 0x3F + data: pointer to data + len: number of bytes of data to read + + */ + +uint8_t ds1307_read_user_RAM(uint8_t addr, uint8_t * data, uint8_t len){ + + HAL_StatusTypeDef status = HAL_OK; + + for(uint8_t i = 0; i < len; i++){ + uint8_t resp = ds1307_read_byte(addr + i); + *(data + i) = resp; + } + + return status; +} + +/* + * + Function designed to write embedded RAM data. + Address range 0x08 to 0x3F + 56 bytes of data + + addr: address of data to read 0x08 to 0x3F + data: pointer to data + len: number of bytes of data to read + + */ + +uint8_t ds1307_write_user_RAM(uint8_t addr, uint8_t * data, uint8_t len){ + HAL_StatusTypeDef status = HAL_OK; + + for(uint8_t i = 0; i < len; i++){ + + ds1307_write_byte(addr + i, *(data + i)); + + } + + return status; +} + /** * @brief Write byte data from an specific address ds1307 RTC * diff --git a/Core/Src/main.c b/Core/Src/main.c index f97f14a..4025563 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -124,11 +124,8 @@ int main(void) MX_USB_DEVICE_Init(); /* USER CODE BEGIN 2 */ - HAL_TIM_Base_Start(&htim2); - ws2812_Init(); - uint32_t mytimer = 0; // RTC config @@ -136,6 +133,11 @@ int main(void) // ds1307_config(0, 10, 19, THURSDAY, 26, MARCH, 2026, +1, 33); + HAL_TIM_Base_Start(&htim2); + ws2812_Init(); + //ws_effect_setDisplayColoursNV(); + + /* USER CODE END 2 */ /* Infinite loop */ diff --git a/Core/Src/myComms.c b/Core/Src/myComms.c index fbdecb8..cb3130a 100644 --- a/Core/Src/myComms.c +++ b/Core/Src/myComms.c @@ -15,6 +15,12 @@ #include "ds1307.h" + +#define NV_COLOUR_HOURS_ADDR 0x08 +#define NV_COLOUR_MINUTES_ADDR 0x09 +#define NV_COLOUR_SECONDS_ADDR 0x0A +#define NV_COLOUR_BACKGND_ADDR 0x0B + typedef struct { uint8_t cmdId; @@ -28,7 +34,10 @@ typedef struct { uint8_t crc; } uartMessage_s; +static void cmd_readNV(uint32_t data); +static void cmd_writeNV(uint8_t * data); +static uint8_t nok[] = "NOK\n"; static uint8_t ok[] = "OK\n"; uint8_t myId[] = "Clock\n"; @@ -41,9 +50,44 @@ void (*commands[])(uint32_t) = { cmd_GetTime, cmd_setColour_hour, cmd_setColour_minute, + cmd_setColour_second, + cmd_setColour_background, + cmd_readNV, + cmd_writeNV, cmd_setColour_second }; +static void cmd_readNV(uint32_t data){ + HAL_StatusTypeDef status = HAL_ERROR; + + uint8_t addr = (uint8_t) data; + uint8_t respBuf[3] = {0}; + + status = ds1307_read_user_RAM(addr, respBuf, sizeof(respBuf)); + + if(HAL_OK == status){ + CDC_Transmit_FS(respBuf, sizeof(respBuf)); + } + else{ + CDC_Transmit_FS(nok, sizeof(nok)); + } +} + +static void cmd_writeNV(uint8_t * data){ + HAL_StatusTypeDef status = HAL_ERROR; + + + status = ds1307_write_user_RAM(data[0], &data[1], 3); + + if(HAL_OK == status){ + CDC_Transmit_FS(ok, sizeof(ok)); + } + else{ + CDC_Transmit_FS(nok, sizeof(nok)); + } + +} + void setRxFlag(void){ RxComplete_fl = 1; @@ -131,6 +175,15 @@ void cmd_setColour_hour(uint32_t colour){ clock_setColourHours(r, g, b); + uint8_t nv[] = { + NV_COLOUR_HOURS_ADDR, + r, + g, + b + }; + + cmd_writeNV(nv); + CDC_Transmit_FS(ok, sizeof(ok)); } @@ -144,6 +197,16 @@ void cmd_setColour_minute(uint32_t colour){ clock_setColourMinutes(r, g, b); + + uint8_t nv[] = { + NV_COLOUR_MINUTES_ADDR, + r, + g, + b + }; + + cmd_writeNV(nv); + CDC_Transmit_FS(ok, sizeof(ok)); } @@ -157,6 +220,16 @@ void cmd_setColour_second(uint32_t colour){ clock_setColourSeconds(r, g, b); + + uint8_t nv[] = { + NV_COLOUR_SECONDS_ADDR, + r, + g, + b + }; + + cmd_writeNV(nv); + CDC_Transmit_FS(ok, sizeof(ok)); } @@ -170,5 +243,15 @@ void cmd_setColour_background(uint32_t colour){ clock_setColourBackground(r, g, b); + + uint8_t nv[] = { + NV_COLOUR_BACKGND_ADDR, + r, + g, + b + }; + + cmd_writeNV(nv); + CDC_Transmit_FS(ok, sizeof(ok)); } diff --git a/Core/Src/ws2812_effect.c b/Core/Src/ws2812_effect.c index a092e42..2ccce23 100644 --- a/Core/Src/ws2812_effect.c +++ b/Core/Src/ws2812_effect.c @@ -9,6 +9,13 @@ #include "ws2812_effect.h" #include "clock.h" #include +#include "ds1307.h" + +#define NV_COLOUR_HOURS_ADDR 0x08 +#define NV_COLOUR_MINUTES_ADDR 0x09 +#define NV_COLOUR_SECONDS_ADDR 0x0A +#define NV_COLOUR_BACKGND_ADDR 0x0B + typedef struct { uint8_t r; @@ -21,6 +28,32 @@ static colour_s colour_minutes = {0, 250, 0}; static colour_s colour_seconds = {0, 0, 250}; static colour_s colour_background = {0, 0, 0}; +void ws_effect_setDisplayColoursNV(void){ + + uint8_t rxBuf[3] = {0}; + + ds1307_read_user_RAM(NV_COLOUR_HOURS_ADDR, rxBuf, sizeof(rxBuf)); + colour_hours.r = rxBuf[0]; + colour_hours.g = rxBuf[1]; + colour_hours.b = rxBuf[2]; + + ds1307_read_user_RAM(NV_COLOUR_MINUTES_ADDR, rxBuf, sizeof(rxBuf)); + colour_minutes.r = rxBuf[0]; + colour_minutes.g = rxBuf[1]; + colour_minutes.b = rxBuf[2]; + + ds1307_read_user_RAM(NV_COLOUR_SECONDS_ADDR, rxBuf, sizeof(rxBuf)); + colour_seconds.r = rxBuf[0]; + colour_seconds.g = rxBuf[1]; + colour_seconds.b = rxBuf[2]; + + ds1307_read_user_RAM(NV_COLOUR_BACKGND_ADDR, rxBuf, sizeof(rxBuf)); + colour_background.r = rxBuf[0]; + colour_background.g = rxBuf[1]; + colour_background.b = rxBuf[2]; + +} + void clock_setColourHours(uint8_t r, uint8_t g, uint8_t b){ colour_hours.r = r; colour_hours.g = g;