mirror of
https://github.com/DashyFox/StackSport.git
synced 2025-05-04 15:20:16 +00:00
124 lines
3.0 KiB
C
124 lines
3.0 KiB
C
#include "EEPROM.h"
|
|
#include "pca9685.h"
|
|
#include "usbd_cdc_if.h"
|
|
|
|
|
|
void SaveShot(unsigned char number, struct Shot* shot)
|
|
{
|
|
print("SaveShot ");
|
|
printNumber(number);
|
|
unsigned char Buf[SHOT_BLOCKSIZE];
|
|
memset(Buf, 0x00, sizeof(Buf));
|
|
|
|
Buf[0] = number;
|
|
Buf[1] = shot->countRepeatShot;
|
|
Buf[2] = shot->speedRollerTop;
|
|
Buf[3] = shot->speedRollerBottom;
|
|
Buf[4] = shot->speedScrew;
|
|
Buf[5] = shot->rotationAxial;
|
|
Buf[6] = shot->rotationHorizontal;
|
|
Buf[7] = shot->rotationVertical;
|
|
|
|
FLASH_WriteBlock(START_ADR_SHOT, number, Buf);
|
|
}
|
|
|
|
struct Shot GetShot( unsigned char number)
|
|
{
|
|
struct Shot shot;
|
|
|
|
unsigned char Buf[SHOT_BLOCKSIZE];
|
|
FLASH_ReadBlock(START_ADR_SHOT, number, Buf);
|
|
shot.isExist = Buf[0];
|
|
shot.countRepeatShot = Buf[1];
|
|
shot.speedRollerTop = Buf[2];
|
|
shot.speedRollerBottom = Buf[3];
|
|
shot.speedScrew = Buf[4];
|
|
shot.rotationAxial = Buf[5];
|
|
shot.rotationHorizontal = Buf[6];
|
|
shot.rotationVertical = Buf[7];
|
|
|
|
return shot;
|
|
}
|
|
|
|
void FLASH_WriteBlock(uint16_t startAddr, uint8_t number, uint8_t *writeData)
|
|
{
|
|
uint8_t dataSize;
|
|
// protect and select
|
|
switch (startAddr) {
|
|
case START_ADR_STAT:
|
|
number = 0;
|
|
dataSize = STAT_BLOCKSIZE;
|
|
break;
|
|
case START_ADR_SHOT:
|
|
dataSize = SHOT_BLOCKSIZE;
|
|
if(number > MAX_NUMBER_SHOTS)
|
|
return;
|
|
break;
|
|
case START_ADR_PROGRAM:
|
|
dataSize = PROGRAM_BLOCKSIZE;
|
|
if(number > MAX_NUMBER_PROGRAMS)
|
|
return;
|
|
break;
|
|
case START_ADR_MACRO:
|
|
dataSize = MACRO_BLOCKSIZE;
|
|
if(number > MAX_NUMBER_MACRO)
|
|
return;
|
|
break;
|
|
default:
|
|
return;
|
|
break;
|
|
}
|
|
|
|
uint16_t blockAddr16 = (uint16_t)(startAddr + (uint16_t)(number*dataSize));
|
|
uint8_t blockAddr[2] = {HIBYTE(blockAddr16), LOBYTE(blockAddr16)};
|
|
|
|
unsigned char Buf[dataSize+2];
|
|
memset(Buf, 0x00, sizeof(Buf));
|
|
Buf[0] = blockAddr[0];
|
|
Buf[1] = blockAddr[1];
|
|
|
|
for( unsigned char i = 0; i < (dataSize); i++ ) Buf[i+2] = writeData[i];
|
|
|
|
HAL_I2C_Master_Transmit(&hi2c1, (AT24C_ADRESS << 1), Buf, (dataSize + 2), 10);
|
|
HAL_Delay(1);
|
|
}
|
|
|
|
void FLASH_ReadBlock(uint16_t startAddr, uint8_t number, uint8_t *readData){
|
|
uint8_t dataSize;
|
|
// protect and select
|
|
switch (startAddr) {
|
|
case START_ADR_STAT:
|
|
number = 0;
|
|
dataSize = STAT_BLOCKSIZE;
|
|
break;
|
|
case START_ADR_SHOT:
|
|
dataSize = SHOT_BLOCKSIZE;
|
|
if(number > MAX_NUMBER_SHOTS)
|
|
return;
|
|
break;
|
|
case START_ADR_PROGRAM:
|
|
dataSize = PROGRAM_BLOCKSIZE;
|
|
if(number > MAX_NUMBER_PROGRAMS)
|
|
return;
|
|
break;
|
|
case START_ADR_MACRO:
|
|
dataSize = MACRO_BLOCKSIZE;
|
|
if(number > MAX_NUMBER_MACRO)
|
|
return;
|
|
break;
|
|
default:
|
|
return;
|
|
break;
|
|
}
|
|
|
|
memset(readData, 0x00, dataSize);
|
|
|
|
uint16_t blockAddr16 = (uint16_t)(startAddr + (uint16_t)(number*dataSize));
|
|
uint8_t blockAddr[2] = {HIBYTE(blockAddr16), LOBYTE(blockAddr16)};
|
|
|
|
HAL_I2C_Master_Transmit(&hi2c1, (AT24C_ADRESS << 1), blockAddr, 2, 10);
|
|
HAL_Delay(1);
|
|
HAL_I2C_Master_Receive(&hi2c1, (AT24C_ADRESS << 1), readData, dataSize, 10);
|
|
HAL_Delay(1);
|
|
}
|