Compare commits

...

2 commits
stable ... mvp

Author SHA1 Message Date
dc82804d22 stash 2022-11-01 16:46:58 +01:00
a7c949ad0e create minimum viable product
- 12 x 16bit registers
 - [2*n] = 12bit constant current value
 - [2*n+1] = 8bit PWM value
2022-09-24 08:37:46 +02:00
2 changed files with 104 additions and 189 deletions

View file

@ -20,7 +20,7 @@ TARGET = stm32f030c8t_test
# building variables
######################################
# debug build?
DEBUG = 0
DEBUG = 1
# optimization
OPT = -O3

View file

@ -58,44 +58,10 @@ void SystemClock_Config(void);
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
void initPWM(TIM_HandleTypeDef timer, uint32_t channel, uint16_t period,
uint16_t pulse) {
//HAL_TIM_PWM_Stop(&timer, channel);
// stop generation of pwm
TIM_OC_InitTypeDef sConfigOC;
timer.Init.Period = period;
// set the period duration
HAL_TIM_PWM_Init(&timer); // reinititialise with new period value
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = pulse;
// set the pulse duration
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
HAL_TIM_PWM_ConfigChannel(&timer, &sConfigOC, channel);
HAL_TIM_PWM_Start(&timer, channel);
}
void setPWM(TIM_HandleTypeDef timer, uint32_t channel, uint16_t period,
uint16_t pulse) {
//HAL_TIM_PWM_Stop(&timer, channel);
// stop generation of pwm
TIM_OC_InitTypeDef sConfigOC;
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = pulse;
// set the pulse duration
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
HAL_TIM_PWM_ConfigChannel(&timer, &sConfigOC, channel);
//HAL_TIM_PWM_Start(&timer, channel);
}
void setDAC(uint8_t channel, uint16_t val) {
uint16_t frame = val & 0x0FFF | 0x7000 & (channel << 12);
HAL_SPI_Transmit(&hspi1, (uint8_t *) &frame, 1, 100);
}
bool dirty = false;
uint16_t mem[12] = {0};
bool dirty = true;
//uint16_t mem[12] = {0xFFF, 0xFF, 0xFFF, 0xFF, 0xFFF, 0xFF, 0xFFF, 0xFF, 0xFFF, 0xFF, 0xFFF, 0xFF};
uint16_t mem[12] = {0x300, 0x40, 0x300, 0x40, 0x300, 0x40, 0x300, 0x40, 0x300, 0x40, 0x300, 0x40};
#define BUFFER_SIZE 64
uint16_t RX_Buffer[BUFFER_SIZE] = {0};
@ -104,10 +70,9 @@ 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 data: 16;
unsigned addr: 12;
unsigned op: 4;
} __attribute__((packed));
@ -117,15 +82,14 @@ void HAL_SPI_CpltCallback(SPI_HandleTypeDef *hspi) {
} __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);
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) {
@ -139,8 +103,19 @@ void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi) {
void HAL_SPI_TxCpltCallback(SPI_HandleTypeDef *hspi) {
//HAL_SPI_Transmit_DMA(&hspi2, (uint8_t *) TX_Buffer, 4);
}
void setDAC(uint8_t channel, uint16_t val) {
uint16_t frame = (val & 0x0FFF) | (0x7000 & (channel << 12));
HAL_SPI_Transmit(&hspi1, (uint8_t * ) & frame, 1, 100);
}
/* USER CODE END 0 */
static uint8_t pwm_lookup[] = {0, 1, 2, 3, 10, 11, 12, 20, 21, 32, 32, 42, 42, 52, 52, 61, 61, 70, 70, 79, 79, 88, 88,
98, 98, 107, 107, 117, 117, 127, 127, 137, 137, 147, 147, 157, 157, 167, 167, 177, 177,
187, 187, 197, 197, 207, 207, 217, 217, 227, 227, 237, 237, 247, 247, 255, 255};
/**
* @brief The application entry point.
* @retval int
@ -176,63 +151,49 @@ int main(void) {
MX_SPI1_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;
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);
TIM1->ARR = 254;
TIM1->CCR1 = 10;
TIM1->CCR2 = 10;
TIM1->CCR3 = 10;
TIM1->CCR4 = 10;
TIM1->CCMR1 = 0x6868;
TIM1->CCMR2 = 0x6868;
TIM1->CCER = 0x1111;
TIM1->EGR |= TIM_EGR_UG;
TIM1->BDTR |= TIM_BDTR_MOE;
TIM1->CR1 |= TIM_CR1_CEN;
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);
TIM3->ARR = 254;
TIM3->CCR1 = 10;
TIM3->CCR2 = 10;
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, GPIO_PIN_SET);*/
TIM3->CCMR1 = 0x6868;
TIM3->CCER = 0x1111;
TIM3->EGR |= TIM_EGR_UG;
TIM3->BDTR |= TIM_BDTR_MOE;
TIM3->CR1 |= TIM_CR1_CEN;
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);
HAL_GPIO_WritePin(SIGNAL_LED_GPIO_Port, SIGNAL_LED_Pin, GPIO_PIN_RESET);
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]);
}
TIM1->CCR1 = 0xFF & mem[1];
TIM1->CCR2 = 0xFF & mem[3];
TIM1->CCR3 = 0xFF & mem[5];
TIM1->CCR4 = 0xFF & mem[7];
TIM3->CCR1 = 0xFF & mem[9];
TIM3->CCR2 = 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);
/*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 */
/* Infinite loop */
@ -242,38 +203,54 @@ int main(void) {
/* USER CODE BEGIN 3 */
/*
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);
#if 1
if(dirty) {
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]);
}
TIM1->CCR1 = 0xFF & mem[1];
TIM1->CCR2 = 0xFF & mem[3];
TIM1->CCR3 = 0xFF & mem[5];
TIM1->CCR4 = 0xFF & mem[7];
TIM3->CCR1 = 0xFF & mem[9];
TIM3->CCR2 = 0xFF & mem[11];
dirty = false;
}
#else
setDAC(0, 50 << 4);
setDAC(1, 50 << 4);
setDAC(2, 50 << 4);
setDAC(3, 50 << 4);
setDAC(4, 50 << 4);
setDAC(5, 50 << 4);
HAL_GPIO_WritePin(SIGNAL_LED_GPIO_Port, SIGNAL_LED_Pin, GPIO_PIN_SET);
for (int i = 0; i < sizeof(pwm_lookup); i++) {
uint8_t j = pwm_lookup[i];
TIM1->CCR1 = j;
TIM1->CCR2 = j;
TIM1->CCR3 = j;
TIM1->CCR4 = j;
TIM3->CCR1 = j;
TIM3->CCR2 = j;
/*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);
}*/
}
TIM1->CCR1 = 256;
TIM1->CCR2 = 256;
TIM1->CCR3 = 256;
TIM1->CCR4 = 256;
TIM3->CCR1 = 256;
TIM3->CCR2 = 256;
for (int i = 50; i < 256; i++) {
setDAC(0, i << 4);
setDAC(1, i << 4);
@ -283,75 +260,13 @@ int main(void) {
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_GPIO_WritePin(SIGNAL_LED_GPIO_Port, SIGNAL_LED_Pin, GPIO_PIN_RESET);
//HAL_SPI
HAL_Delay(200);
/*uint16_t val = 682;
setDAC(0, val);
setDAC(1, val);
setDAC(2, val);
setDAC(3, val);
setDAC(4, val);
setDAC(5, val);
*/
#endif
// 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);
}
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 */
}