create minimum viable product

- 12 x 16bit registers
 - [2*n] = 12bit constant current value
 - [2*n+1] = 8bit PWM value
This commit is contained in:
j3d1 2022-09-24 07:41:37 +02:00
parent bbbf5ea91e
commit a7c949ad0e

View file

@ -58,6 +58,50 @@ void SystemClock_Config(void);
/* Private user code ---------------------------------------------------------*/ /* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */ /* USER CODE BEGIN 0 */
bool dirty = false;
uint16_t mem[12] = {0};
#define BUFFER_SIZE 64
uint16_t RX_Buffer[BUFFER_SIZE] = {0};
void HAL_SPI_CpltCallback(SPI_HandleTypeDef *hspi) {
if(RX_Buffer[0]) {
union {
struct {
unsigned data: 16;
unsigned addr: 12;
unsigned op: 4;
} __attribute__((packed));
struct {
uint16_t high;
uint16_t low;
} __attribute__((packed));
} frame = {.low=RX_Buffer[0], .high = RX_Buffer[1]};
if(frame.op == 0xA && frame.addr <= 16) {
mem[frame.addr] = frame.data;
dirty = true;
}
}
HAL_SPI_Receive_DMA(&hspi2, (uint8_t *) RX_Buffer, 4);
}
void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hspi) {
HAL_SPI_CpltCallback(hspi);
}
void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi) {
HAL_SPI_CpltCallback(hspi);
}
void HAL_SPI_TxCpltCallback(SPI_HandleTypeDef *hspi) {
//HAL_SPI_Transmit_DMA(&hspi2, (uint8_t *) TX_Buffer, 4);
}
void initPWM(TIM_HandleTypeDef timer, uint32_t channel, uint16_t period, void initPWM(TIM_HandleTypeDef timer, uint32_t channel, uint16_t period,
uint16_t pulse) { uint16_t pulse) {
//HAL_TIM_PWM_Stop(&timer, channel); //HAL_TIM_PWM_Stop(&timer, channel);
@ -90,55 +134,9 @@ void setPWM(TIM_HandleTypeDef timer, uint32_t channel, uint16_t period,
} }
void setDAC(uint8_t channel, uint16_t val) { void setDAC(uint8_t channel, uint16_t val) {
uint16_t frame = val & 0x0FFF | 0x7000 & (channel << 12); uint16_t frame = (val & 0x0FFF) | (0x7000 & (channel << 12));
HAL_SPI_Transmit(&hspi1, (uint8_t *) &frame, 1, 100); HAL_SPI_Transmit(&hspi1, (uint8_t *) &frame, 1, 100);
} }
bool dirty = false;
uint16_t mem[12] = {0};
#define BUFFER_SIZE 64
uint16_t RX_Buffer[BUFFER_SIZE] = {0};
uint16_t TX_Buffer[BUFFER_SIZE] = {0};
void HAL_SPI_CpltCallback(SPI_HandleTypeDef *hspi) {
if(RX_Buffer[0]) {
//HAL_SPI_Transmit_DMA(&hspi2, (uint8_t *) TX_Buffer, 4);
union {
struct {
unsigned dat: 16;
unsigned addr: 12;
unsigned op: 4;
} __attribute__((packed));
struct {
uint16_t high;
uint16_t low;
} __attribute__((packed));
} frame = {.low=RX_Buffer[0], .high = RX_Buffer[1]};
frame.op = 0xe;
TX_Buffer[1] = frame.high;
TX_Buffer[0] = frame.low;
HAL_SPI_TransmitReceive_DMA(&hspi2, (uint8_t *) TX_Buffer, (uint8_t *) RX_Buffer, 4);
} else {
//HAL_GPIO_WritePin(GPIOB, GPIO_PIN_14, GPIO_PIN_SET);
HAL_SPI_Receive_DMA(&hspi2, (uint8_t *) RX_Buffer, 4);
//HAL_GPIO_WritePin(GPIOB, GPIO_PIN_14, GPIO_PIN_RESET);
}
}
void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hspi) {
HAL_SPI_CpltCallback(hspi);
}
void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi) {
HAL_SPI_CpltCallback(hspi);
}
void HAL_SPI_TxCpltCallback(SPI_HandleTypeDef *hspi) {
//HAL_SPI_Transmit_DMA(&hspi2, (uint8_t *) TX_Buffer, 4);
}
/* USER CODE END 0 */ /* USER CODE END 0 */
/** /**
@ -176,63 +174,27 @@ int main(void) {
MX_SPI1_Init(); MX_SPI1_Init();
MX_SPI2_Init(); MX_SPI2_Init();
/*GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_11;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_4 | GPIO_PIN_5; HAL_GPIO_WritePin(SIGNAL_LED_GPIO_Port, SIGNAL_LED_Pin, GPIO_PIN_RESET);
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_9, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_10, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_11, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, GPIO_PIN_SET);*/
setDAC(0, 50 << 4);
setDAC(1, 50 << 4);
setDAC(2, 50 << 4);
setDAC(3, 50 << 4);
setDAC(4, 50 << 4);
setDAC(5, 50 << 4);
initPWM(htim1, TIM_CHANNEL_1, 255, 10);
initPWM(htim1, TIM_CHANNEL_2, 255, 10);
initPWM(htim1, TIM_CHANNEL_3, 255, 10);
initPWM(htim1, TIM_CHANNEL_4, 255, 10);
initPWM(htim3, TIM_CHANNEL_1, 255, 10);
initPWM(htim3, TIM_CHANNEL_2, 255, 10);
uint16_t frame = 0b1001000000000000; uint16_t frame = 0b1001000000000000;
HAL_SPI_Transmit(&hspi1, (uint8_t *) &frame, 1, 100); HAL_SPI_Transmit(&hspi1, (uint8_t *) &frame, 1, 100);
for (int i = 0; i < 6; ++i) {
setDAC(i, mem[i * 2]);
}
initPWM(htim1, TIM_CHANNEL_1, 255, 0xFF & mem[1]);
initPWM(htim1, TIM_CHANNEL_2, 255, 0xFF & mem[3]);
initPWM(htim1, TIM_CHANNEL_3, 255, 0xFF & mem[5]);
initPWM(htim1, TIM_CHANNEL_4, 255, 0xFF & mem[7]);
initPWM(htim3, TIM_CHANNEL_1, 255, 0xFF & mem[9]);
initPWM(htim3, TIM_CHANNEL_2, 255, 0xFF & mem[11]);
HAL_GPIO_WritePin(SIGNAL_LED_GPIO_Port, SIGNAL_LED_Pin, GPIO_PIN_SET);
HAL_SPI_Receive_DMA(&hspi2, (uint8_t *) RX_Buffer, 4); HAL_SPI_Receive_DMA(&hspi2, (uint8_t *) RX_Buffer, 4);
/*Configure GPIO pin : PtPin */
/*GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = GPIO_PIN_14;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);*/
/*GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_11;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);*/
/* USER CODE END 2 */ /* USER CODE END 2 */
/* Infinite loop */ /* Infinite loop */
@ -242,116 +204,25 @@ int main(void) {
/* USER CODE BEGIN 3 */ /* USER CODE BEGIN 3 */
/* if(dirty) {
setPWM(htim1, TIM_CHANNEL_1, 255, 255);
setPWM(htim1, TIM_CHANNEL_2, 255, 255);
setPWM(htim1, TIM_CHANNEL_3, 255, 255);
setPWM(htim1, TIM_CHANNEL_4, 255, 255);
setPWM(htim3, TIM_CHANNEL_1, 255, 255);
setPWM(htim3, TIM_CHANNEL_1, 255, 255);*/
/*for (int j = 0; j < 20; j++)
for (int i = 0; i < 20; i++) {
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_9, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_10, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_11, GPIO_PIN_SET);
HAL_GPIO_WritePin(SIGNAL_LED_GPIO_Port, SIGNAL_LED_Pin, GPIO_PIN_SET);
HAL_Delay(j);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_9, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_10, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_11, GPIO_PIN_RESET);
HAL_GPIO_WritePin(SIGNAL_LED_GPIO_Port, SIGNAL_LED_Pin, GPIO_PIN_RESET); HAL_GPIO_WritePin(SIGNAL_LED_GPIO_Port, SIGNAL_LED_Pin, GPIO_PIN_RESET);
HAL_Delay(19-j); for (int i = 0; i < 6; ++i) {
}*/ setDAC(i, mem[i * 2]);
/*for (int i = 50; i < 256; i++) {
setPWM(htim1, TIM_CHANNEL_1, 255, i);
setPWM(htim1, TIM_CHANNEL_2, 255, i);
setPWM(htim1, TIM_CHANNEL_3, 255, i);
setPWM(htim1, TIM_CHANNEL_4, 255, i);
HAL_Delay(5);
}*/
for (int i = 50; i < 256; i++) {
setDAC(0, i << 4);
setDAC(1, i << 4);
setDAC(2, i << 4);
setDAC(3, i << 4);
setDAC(4, i << 4);
setDAC(5, i << 4);
HAL_Delay(5);
}
for (int i = 0; i < 206; i++) {
setDAC(0, (255 - i) << 4);
setDAC(1, (255 - i) << 4);
setDAC(2, (255 - i) << 4);
setDAC(3, (255 - i) << 4);
setDAC(4, (255 - i) << 4);
setDAC(5, (255 - i) << 4);
HAL_Delay(5);
} }
initPWM(htim1, TIM_CHANNEL_1, 255, 0xFF & mem[1]);
initPWM(htim1, TIM_CHANNEL_2, 255, 0xFF & mem[3]);
initPWM(htim1, TIM_CHANNEL_3, 255, 0xFF & mem[5]);
initPWM(htim1, TIM_CHANNEL_4, 255, 0xFF & mem[7]);
initPWM(htim3, TIM_CHANNEL_1, 255, 0xFF & mem[9]);
initPWM(htim3, TIM_CHANNEL_2, 255, 0xFF & mem[11]);
//HAL_SPI dirty = false;
HAL_GPIO_WritePin(SIGNAL_LED_GPIO_Port, SIGNAL_LED_Pin, GPIO_PIN_SET);
/*uint16_t val = 682;
setDAC(0, val);
setDAC(1, val);
setDAC(2, val);
setDAC(3, val);
setDAC(4, val);
setDAC(5, val);
*/
// HAL_Delay(500);
//HAL_GPIO_WritePin(SIGNAL_LED_GPIO_Port, SIGNAL_LED_Pin, GPIO_PIN_SET);
//HAL_Delay(500);
initPWM(htim1, TIM_CHANNEL_1, 255, 255);
initPWM(htim1, TIM_CHANNEL_2, 255, 255);
initPWM(htim1, TIM_CHANNEL_3, 255, 255);
initPWM(htim1, TIM_CHANNEL_4, 255, 255);
initPWM(htim3, TIM_CHANNEL_1, 255, 255);
initPWM(htim3, TIM_CHANNEL_2, 255, 255);
for (int i = 50; i < 256; i++) {
setDAC(0, i << 4);
setDAC(1, i << 4);
setDAC(2, i << 4);
setDAC(3, i << 4);
setDAC(4, i << 4);
setDAC(5, i << 4);
HAL_Delay(5);
}
for (int i = 0; i < 206; i++) {
setDAC(0, (255 - i) << 4);
setDAC(1, (255 - i) << 4);
setDAC(2, (255 - i) << 4);
setDAC(3, (255 - i) << 4);
setDAC(4, (255 - i) << 4);
setDAC(5, (255 - i) << 4);
HAL_Delay(5);
} }
HAL_Delay(50);
initPWM(htim1, TIM_CHANNEL_1, 255, 10);
initPWM(htim1, TIM_CHANNEL_2, 255, 10);
initPWM(htim1, TIM_CHANNEL_3, 255, 10);
initPWM(htim1, TIM_CHANNEL_4, 255, 10);
initPWM(htim3, TIM_CHANNEL_1, 255, 10);
initPWM(htim3, TIM_CHANNEL_2, 255, 10);
/*HAL_Delay(5000);
val = 0xFFF;
setDAC(0, val);
setDAC(1, val);
setDAC(2, val);
setDAC(3, val);
setDAC(4, val);
setDAC(5, val);*/
} }
/* USER CODE END 3 */ /* USER CODE END 3 */
} }