diff --git a/peripherals/cc48x6/build.md b/peripherals/cc48x6/build.md new file mode 100644 index 0000000..91d6d0a --- /dev/null +++ b/peripherals/cc48x6/build.md @@ -0,0 +1,5 @@ +``` +sudo docker run --rm -ti -v /.../fiatlux/:/src alpine:latest +apk add make gcc-arm-none-eabi newlib-arm-none-eabi +make +``` diff --git a/peripherals/cc48x6/calc_lookup.ods b/peripherals/cc48x6/calc_lookup.ods new file mode 100644 index 0000000..5f0e655 Binary files /dev/null and b/peripherals/cc48x6/calc_lookup.ods differ diff --git a/peripherals/cc48x6/firmware/Makefile b/peripherals/cc48x6/firmware/Makefile index d73a2aa..cc25610 100644 --- a/peripherals/cc48x6/firmware/Makefile +++ b/peripherals/cc48x6/firmware/Makefile @@ -181,13 +181,8 @@ $(BUILD_DIR)/%.o: %.c Makefile | $(BUILD_DIR) $(BUILD_DIR)/%.o: %.s Makefile | $(BUILD_DIR) $(AS) -c $(CFLAGS) $< -o $@ -$(BUILD_DIR)/$(TARGET).elf: $(OBJECTS) Makefile $(VERSIONCMD) - $(V)mkdir -p $(dir $@) - $(V)$(eval VERSION_OBJECT = $(shell mktemp /tmp/fiatlux_version_object.XXXXXX.o)) - $(V)$(VERSIONCMD) $(notdir $@) $(VERSION_OBJECT) $(CC) $(CPPFLAGS) $(CXXFLAGS) - @echo link $@ - $(V)$(CC) $(OBJECTS) $(VERSION_OBJECT) $(LDFLAGS) -o $@ - $(V)rm $(VERSION_OBJECT) +$(BUILD_DIR)/$(TARGET).elf: $(OBJECTS) Makefile + $(V)$(CC) $(OBJECTS) $(LDFLAGS) -o $@ $(SZ) $@ $(BUILD_DIR)/%.hex: $(BUILD_DIR)/%.elf | $(BUILD_DIR) @@ -210,4 +205,4 @@ clean: ####################################### -include $(wildcard $(BUILD_DIR)/*.d) -# *** EOF *** \ No newline at end of file +# *** EOF *** diff --git a/peripherals/cc48x6/firmware/Src/main.c b/peripherals/cc48x6/firmware/Src/main.c index 1198d8d..3b17891 100644 --- a/peripherals/cc48x6/firmware/Src/main.c +++ b/peripherals/cc48x6/firmware/Src/main.c @@ -68,7 +68,13 @@ uint16_t counter = 0; volatile 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}; -uint16_t mem[12] = {0xFFF, 0xFF, 0xFFF, 0xFF, 0xFFF, 0xFF, 0xFFF, 0xFF, 0xFFF, 0xFF, 0xFFF, 0xFF}; +//uint16_t mem[12] = {0xFFF, 0xFF, 0xFFF, 0xFF, 0xFFF, 0xFF, 0xFFF, 0xFF, 0xFFF, 0xFF, 0xFFF, 0xFF}; +uint16_t mem[12] = {0x300, 0xff, 0x300, 0xff, 0x300, 0xff, 0x300, 0xff, 0x300, 0xff, 0x300, 0xff}; + + +uint16_t lookup_ana[256] = {0, 750, 750, 750, 750, 750, 750, 750, 750, 750, 750, 750, 750, 750, 750, 750, 750, 750, 750, 750, 750, 750, 750, 750, 750, 750, 750, 750, 750, 750, 750, 750, 750, 750, 750, 750, 750, 750, 750, 750, 750, 750, 750, 750, 750, 750, 750, 755, 771, 787, 803, 819, 835, 851, 867, 883, 899, 915, 931, 947, 964, 980, 996, 1012, 1028, 1044, 1060, 1076, 1092, 1108, 1124, 1140, 1156, 1172, 1188, 1204, 1220, 1237, 1253, 1269, 1285, 1301, 1317, 1333, 1349, 1365, 1381, 1397, 1413, 1429, 1445, 1461, 1477, 1493, 1510, 1526, 1542, 1558, 1574, 1590, 1606, 1622, 1638, 1654, 1670, 1686, 1702, 1718, 1734, 1750, 1766, 1783, 1799, 1815, 1831, 1847, 1863, 1879, 1895, 1911, 1927, 1943, 1959, 1975, 1991, 2007, 2023, 2039, 2056, 2072, 2088, 2104, 2120, 2136, 2152, 2168, 2184, 2200, 2216, 2232, 2248, 2264, 2280, 2296, 2312, 2329, 2345, 2361, 2377, 2393, 2409, 2425, 2441, 2457, 2473, 2489, 2505, 2521, 2537, 2553, 2569, 2585, 2602, 2618, 2634, 2650, 2666, 2682, 2698, 2714, 2730, 2746, 2762, 2778, 2794, 2810, 2826, 2842, 2858, 2875, 2891, 2907, 2923, 2939, 2955, 2971, 2987, 3003, 3019, 3035, 3051, 3067, 3083, 3099, 3115, 3131, 3148, 3164, 3180, 3196, 3212, 3228, 3244, 3260, 3276, 3292, 3308, 3324, 3340, 3356, 3372, 3388, 3404, 3421, 3437, 3453, 3469, 3485, 3501, 3517, 3533, 3549, 3565, 3581, 3597, 3613, 3629, 3645, 3661, 3677, 3694, 3710, 3726, 3742, 3758, 3774, 3790, 3806, 3822, 3838, 3854, 3870, 3886, 3902, 3918, 3934, 3950, 3967, 3983, 3999, 4015, 4031, 4047, 4063, 4079, 4095}; + +uint16_t lookup_pwm[256] = {0, 6, 11, 17, 22, 28, 33, 39, 44, 50, 55, 61, 67, 72, 78, 83, 89, 94, 100, 105, 111, 116, 122, 128, 133, 139, 144, 150, 155, 161, 166, 172, 177, 183, 188, 194, 200, 205, 211, 216, 222, 227, 233, 238, 244, 249, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}; uint16_t conf[8] = {version, 0, 0, 0, 0, 0, 0, 0}; @@ -88,50 +94,6 @@ void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) { void HAL_SPI_CpltCallback(SPI_HandleTypeDef *hspi) { - if(RX_Buffer[0]) { - union { - struct { - unsigned data: 12; - unsigned chan: 4; - } __attribute__((packed)); - uint16_t raw; - - } frame = {.raw=RX_Buffer[0]}; - - toggle = !toggle; - counter++; - - if(toggle) { - HAL_GPIO_WritePin(SIGNAL_LED_GPIO_Port, SIGNAL_LED_Pin, GPIO_PIN_SET); - } else { - HAL_GPIO_WritePin(SIGNAL_LED_GPIO_Port, SIGNAL_LED_Pin, GPIO_PIN_RESET); - } - - if(frame.chan <= 6 && frame.chan >= 1) { - frame.chan--; - if(frame.data > 0x300) { - mem[frame.chan * 2 + 1] = 0xFF; - mem[frame.chan * 2] = frame.data; - } else { - mem[frame.chan * 2 + 1] = (frame.data * 0xFF) / 0x300; - mem[frame.chan * 2] = 0x300; - } - - dirty = true; - TX_Buffer[0] = RX_Buffer[0]; - - } else if(frame.chan == 15 && frame.data < 8) { - TX_Buffer[0] = conf[frame.data]; - } else { - TX_Buffer[0] = 0; - } - - } - - HAL_GPIO_WritePin(INIT_OUT_GPIO_Port, INIT_OUT_Pin, GPIO_PIN_SET); - for (volatile int i = 0; i < 3; i++); - HAL_GPIO_WritePin(INIT_OUT_GPIO_Port, INIT_OUT_Pin, GPIO_PIN_RESET); - } void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hspi) { @@ -185,7 +147,7 @@ int main(void) { MX_TIM3_Init(); /* USER CODE BEGIN 2 */ MX_SPI1_Init(); - MX_SPI2_Init(); + //MX_SPI2_Init(); TIM1->ARR = 254; TIM1->CCR1 = 10; @@ -209,7 +171,48 @@ int main(void) { TIM3->BDTR |= TIM_BDTR_MOE; // Enable Main Output TIM3->CR1 |= TIM_CR1_CEN; // Enable Counter - HAL_GPIO_WritePin(SIGNAL_LED_GPIO_Port, SIGNAL_LED_Pin, GPIO_PIN_RESET); +/* + // GPIO CONFIG I2C2 + RCC->AHBENR |= RCC_AHBENR_GPIOBEN; // Eneable Clock + GPIOB->MODER &= ~(GPIO_MODER_MODER14_Msk | GPIO_MODER_MODER13_Msk); + GPIOB->MODER |= GPIO_MODER_MODER14_1 | GPIO_MODER_MODER13_1; // GPIOB 14 & 13 -> Alternate Function + GPIOB->AFR[1] &= ~(GPIO_AFRH_AFSEL14_Msk | GPIO_AFRH_AFSEL13_Msk); + GPIOB->AFR[1] |= (5 << GPIO_AFRH_AFSEL14_Pos) | (5 << GPIO_AFRH_AFSEL13_Pos); // AF5 (I2C2) + GPIOB->OTYPER |= (GPIO_OTYPER_OT_14 | GPIO_OTYPER_OT_13); // Output type: open drain + GPIOB->PUPDR &= ~(GPIO_PUPDR_PUPDR14_Msk | GPIO_PUPDR_PUPDR13_Msk); + GPIOB->PUPDR |= GPIO_PUPDR_PUPDR14_0 | GPIO_PUPDR_PUPDR13_0; // Enable Internal Pull up + + + // Enable I2C2 + RCC->APB1ENR |= RCC_APB1ENR_I2C2EN; // Eneable Clock + I2C2->CR1 &= ~I2C_CR1_PE; // Reset Periph. + //while(I2C2->CR1 & I2C_CR1_PE) {} + + I2C2->OAR1 = I2C_OAR1_OA1EN | (5 << 1); // Enable, Addr: 5 + I2C2->OAR2 = 0; + I2C2->CR1 |= I2C_CR1_NOSTRETCH; //Disable Clock stretching + I2C2->CR1 |= I2C_CR1_PE; // Enable Periph +*/ + + // GPIO CONFIG UART2 + RCC->AHBENR |= RCC_AHBENR_GPIOAEN; + GPIOA->MODER &= ~GPIO_MODER_MODER3_Msk; + GPIOA->MODER |= GPIO_MODER_MODER3_1; //GPIOA3 Alternate Function + GPIOA->AFR[0] &= ~GPIO_AFRL_AFSEL3_Msk; + GPIOA->AFR[0] |= 1 << GPIO_AFRL_AFSEL3_Pos; // AF1 -> UART2 + GPIOA->OTYPER |= GPIO_OTYPER_OT_3; + + // Enable UART2 + RCC->APB1ENR |= RCC_APB1ENR_USART2EN; + USART2->CR1 = 0; + USART2->CR2 = 0; + USART2->CR3 = 0; + + USART2->BRR = 48000000 / 115200; + //USART2->CR1 = USART_CR1_RE | USART_CR1_UE; + USART2->CR1 = USART_CR1_RXNEIE | USART_CR1_RE | USART_CR1_UE; + NVIC_EnableIRQ(USART2_IRQn); + uint16_t frame = 0b1001000000000000; // DAC128S085: Enable Write Through mode (Instant output change on write) HAL_SPI_Transmit(&hspi1, (uint8_t * ) & frame, 1, 100); @@ -241,28 +244,70 @@ int main(void) { /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ + //HAL_Delay(1000); + + GPIOA->ODR ^= 0x08; + + if (0) {//(USART2->ISR & USART_ISR_RXNE) { //(I2C2->ISR & I2C_ISR_ADDR) { + uint16_t v = USART2->RDR; + for(int i = 0; i < 12; i++) mem[i] = v; + for(int i = 0; i < 12; i+=2) mem[i] = 0xfff; + //mem[0] = 0xfff; mem[1] = 0xff; + } - //if(dirty) { - for (int i = 0; i < 6; ++i) { - setDAC(i, mem[i * 2]); + if(dirty) { + 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; } - 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; - - //} - } /* USER CODE END 3 */ } +volatile uint8_t uart_proto_state = 0; + +void USART2_IRQHandler(void) +{ + if (USART2->ISR & USART_ISR_RXNE) + { + uint16_t v = USART2->RDR; + if (uart_proto_state == 0) { + uart_proto_state = v; + if (v == 0) dirty = true; + }else{ + uint16_t idx = (uart_proto_state - 1) / 2; + if (idx < sizeof(mem) / sizeof(mem[0])) + { + if(uart_proto_state & 1) { + mem[idx] &= 0x00ff; + mem[idx] |= (v << 8) & 0xff00; + }else{ + mem[idx] &= 0xff00; + mem[idx] |= v & 0x00ff; + } + } + if (uart_proto_state == 128) { + for(int i = 0; i < 6; i++) { + mem[i*2+0] = lookup_ana[v]; + mem[i*2+1] = lookup_pwm[v]; + } + } + uart_proto_state = 0; + } + } +} + /** * @brief System Clock Configuration * @retval None diff --git a/peripherals/cc48x6/firmware/Src/tim.c b/peripherals/cc48x6/firmware/Src/tim.c index 4e674f1..9ef8dc0 100644 --- a/peripherals/cc48x6/firmware/Src/tim.c +++ b/peripherals/cc48x6/firmware/Src/tim.c @@ -43,7 +43,7 @@ void MX_TIM1_Init(void) /* USER CODE END TIM1_Init 1 */ htim1.Instance = TIM1; - htim1.Init.Prescaler = 0; + htim1.Init.Prescaler = 90; htim1.Init.CounterMode = TIM_COUNTERMODE_UP; htim1.Init.Period = 65535; htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; @@ -114,7 +114,7 @@ void MX_TIM3_Init(void) /* USER CODE END TIM3_Init 1 */ htim3.Instance = TIM3; - htim3.Init.Prescaler = 0; + htim3.Init.Prescaler = 90; htim3.Init.CounterMode = TIM_COUNTERMODE_UP; htim3.Init.Period = 65535; htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; diff --git a/peripherals/cc48x6/test.py b/peripherals/cc48x6/test.py new file mode 100644 index 0000000..47b9b66 --- /dev/null +++ b/peripherals/cc48x6/test.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python3 +import serial +import time +ser = serial.Serial("/dev/ttyUSB0", 115200) + +def setCh(ch, ana, pwm): + ser.write(bytearray([ch*4+1, ana >> 8, ch*4+2, ana & 0xff, ch*4+4, pwm, 0,0])) + +def setG(val): + ser.write(bytearray([128,val,0])) + +setCh(0,0,0) +setCh(1,0,0) +setCh(2,0,0) +setCh(3,0,0) +setCh(4,0,0) +setCh(5,0,0) + +if False: #for i in range(0, 4095, 5): + print(i) + setCh(0,i,255) + time.sleep(0.1) + +for i in range(255): + print(i) + setG(i) + time.sleep(0.5)