Control code is now in main loop, keystrokes are send over i2c to keyboard device, added basic upload of code over i2c

This commit is contained in:
2020-12-22 01:05:05 +01:00
parent 40aa71275a
commit 279a81716a
11 changed files with 457 additions and 345 deletions

View File

@@ -10,8 +10,8 @@
// The top 4 address bits determine which device is used. (16 pages, 256 devices)
// 0xFF means that the device does not exist
// WATCH OUT THE DEVICE ADDRESS NEED TO BE REVERSED (MSB IS ON THE RIGHT)
uint8_t memory_map_0[8] = {0b00010000, 0b10001000, 0b01001000, 0b11001000, 0b00101000, 0b10101000, 0b01101000, 0b11101000};
uint8_t memory_map_1[8] = {0b00001000, 0b10001000, 0b01001000, 0b11001000, 0b00101000, 0b10101000, 0b01101000, 0b11101000};
uint8_t memory_map_0[16] = {0b00010000, 0b10001000, 0b01001000, 0b11001000, 0b00101000, 0b10101000, 0b01101000, 0b11101000, 0b00011000, 0b10011000, 0b01011000, 0b11011000, 0b00111000, 0b10111000, 0b01111000, 0b11111000};
uint8_t memory_map_1[16] = {0b00001000, 0b10001000, 0b01001000, 0b11001000, 0b00101000, 0b10101000, 0b01101000, 0b11101000, 0b00011000, 0b10011000, 0b01011000, 0b11011000, 0b00111000, 0b10111000, 0b01111000, 0b11111000};
Control control;
@@ -24,10 +24,6 @@ Control control;
uint8_t get_device(uint16_t address) {
uint8_t page = address >> 12;
if (page >= 8) {
return 0xFF;
}
if (control.memory_config == 0) {
return memory_map_0[page];
} else if (control.memory_config == 1) {
@@ -143,14 +139,21 @@ void handle_io_read() {
uint8_t address = read_address() & 0xFF;
switch (address) {
case 0x02:
write_data(control.input.c);
control.input.received = 0;
break;
// Stand in for graphics hardware
/* case 0x03: */
/* write_data(0x01); */
/* break; */
case 0x03:
write_data(0x01 | 0x02*control.input.received);
break;
// Stand in for the keyboard hardware
/* case 0x1E: */
/* write_data(control.input.c); */
/* control.input.received = 0; */
/* break; */
// Stand in for the keyboard hardware
/* case 0x1F: */
/* write_data(0x01 * control.input.received); */
/* break; */
case 0x08:
if (control.storage.ready && control.storage.action == 0x20) {
@@ -183,9 +186,13 @@ void handle_io_read() {
write_data(0x08*control.storage.ready);
break;
default:
printf("IO Read: 0x00 @ %.2X\n\r", address);
write_data(0x00);
default: {
/* uint8_t value = read_data(); */
/* #<{(| if (value == 0) { |)}># */
/* printf("IO Read: %.2X @ %.2X\n\r", value, address); */
/* #<{(| } |)}># */
return;
}
}
enable_data_out(1);
@@ -364,6 +371,7 @@ uint8_t control_receive_program(uint8_t byte) {
if (i >= control.eeprom.length) {
i = 0;
c = 0;
control.eeprom.programming = 1;
return 1;
}

69
Src/firmware.c Normal file
View File

@@ -0,0 +1,69 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "firmware.h"
extern I2C_HandleTypeDef hi2c1;
extern UART_HandleTypeDef huart2;
typedef enum {
TARGET_NONE = 0,
TARGET_ROM,
TARGET_I2C
} UpdateTarget;
void firmware_update() {
printf("FIRMWARE UPDATE\n\r");
UpdateTarget target;
HAL_UART_Receive(&huart2, &target, 1, 1000);
printf("T: %i\n\r", target);
uint8_t address = 0;
if (target == TARGET_I2C) {
HAL_UART_Receive(&huart2, &address, 1, 1000);
printf("A: %i\n\r", address);
}
uint8_t length_buffer[2];
HAL_UART_Receive(&huart2, length_buffer, 2, 1000);
uint16_t length = length_buffer[0] + (length_buffer[1] << 8);
printf("L: %i\n\r", length);
uint8_t* data = malloc(length);
HAL_UART_Receive(&huart2, data, length, 1000);
printf("Firmware received!\n\r");
printf("Uploading to target...\n\r");
switch (target) {
case TARGET_I2C: {
// Upload the application
uint8_t upload_command[4 + length];
upload_command[0] = 0x02;
upload_command[1] = 0x01;
for (uint16_t offset = 0; offset < 8; ++offset) {
upload_command[2] = ((offset*0x80) >> 8) & 0xFF;
upload_command[3] = (offset*0x80) & 0xFF;
// We need to handle the last section properly
memcpy(&upload_command[4], &data[offset*0x80], 0x80);
HAL_I2C_Master_Transmit(&hi2c1, address << 1, upload_command, sizeof(upload_command), 1000);
}
// Start application
uint8_t start_command[] = {0x01, 0x80};
HAL_I2C_Master_Transmit(&hi2c1, address << 1, start_command, sizeof(start_command), 1000);
break;
}
default:
printf("Target not implemented!\n\r");
break;
}
printf("Complete!\n\r");
}

View File

@@ -30,6 +30,7 @@
#include <stdio.h>
#include "restart.h"
#include "control.h"
#include "firmware.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
@@ -49,9 +50,9 @@
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
SD_HandleTypeDef hsd;
I2C_HandleTypeDef hi2c1;
TIM_HandleTypeDef htim3;
SD_HandleTypeDef hsd;
UART_HandleTypeDef huart2;
@@ -63,8 +64,8 @@ extern Control control;
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_TIM3_Init(void);
static void MX_USART2_UART_Init(void);
static void MX_I2C1_Init(void);
static void MX_SDIO_SD_Init(void);
/* USER CODE BEGIN PFP */
@@ -78,8 +79,6 @@ upload_callback_t upload_callback = NULL;
uint8_t byte;
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) {
if (huart->Instance == USART2) {
HAL_UART_Receive_IT(&huart2, &byte, 1);
/* printf("0x%X\n\r", byte); */
if (upload_callback) {
if (upload_callback(byte)) {
upload_callback = NULL;
@@ -87,6 +86,7 @@ void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) {
printf("Done\n\r");
}
} else {
static uint8_t cmd_buffer[128];
static uint32_t cmd_buffer_len = 0;
@@ -135,6 +135,7 @@ void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) {
// ONLY SEND THE FIRST CHAR
if (cmd_buffer_len > 0) {
send_key(byte);
HAL_I2C_Master_Transmit(&hi2c1, 0x04 << 1, &byte, 1, 1000);
}
cmd_buffer_len = 0;
} else if (handle_command && is_command) {
@@ -161,6 +162,11 @@ void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) {
case '#':
send_key('#');
HAL_I2C_Master_Transmit(&hi2c1, 0x04 << 1, &byte, 1, 1000);
break;
case 'a':
firmware_update();
break;
default:
@@ -175,6 +181,7 @@ void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) {
is_command = 0;
}
}
HAL_UART_Receive_IT(&huart2, &byte, 1);
}
}
@@ -278,8 +285,8 @@ int main(void)
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_TIM3_Init();
MX_USART2_UART_Init();
MX_I2C1_Init();
MX_SDIO_SD_Init();
MX_FATFS_Init();
MX_USB_DEVICE_Init();
@@ -294,8 +301,6 @@ int main(void)
HAL_UART_Receive_IT(&huart2, &byte, 1);
HAL_TIM_Base_Start_IT(&htim3);
/* USER CODE END 2 */
/* Infinite loop */
@@ -305,6 +310,7 @@ int main(void)
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
// @todo Speed up the main loop
uint8_t temp = HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_4);
if (!temp && zrst) {
printf("Restarting Z80\n\r");
@@ -327,6 +333,8 @@ int main(void)
rst = temp;
restart_check();
control_execute_state();
}
/* USER CODE END 3 */
}
@@ -373,6 +381,40 @@ void SystemClock_Config(void)
}
}
/**
* @brief I2C1 Initialization Function
* @param None
* @retval None
*/
static void MX_I2C1_Init(void)
{
/* USER CODE BEGIN I2C1_Init 0 */
/* USER CODE END I2C1_Init 0 */
/* USER CODE BEGIN I2C1_Init 1 */
/* USER CODE END I2C1_Init 1 */
hi2c1.Instance = I2C1;
hi2c1.Init.ClockSpeed = 100000;
hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
hi2c1.Init.OwnAddress1 = 0;
hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
hi2c1.Init.OwnAddress2 = 0;
hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
if (HAL_I2C_Init(&hi2c1) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN I2C1_Init 2 */
/* USER CODE END I2C1_Init 2 */
}
/**
* @brief SDIO Initialization Function
* @param None
@@ -401,51 +443,6 @@ static void MX_SDIO_SD_Init(void)
}
/**
* @brief TIM3 Initialization Function
* @param None
* @retval None
*/
static void MX_TIM3_Init(void)
{
/* USER CODE BEGIN TIM3_Init 0 */
/* USER CODE END TIM3_Init 0 */
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};
/* USER CODE BEGIN TIM3_Init 1 */
/* USER CODE END TIM3_Init 1 */
htim3.Instance = TIM3;
htim3.Init.Prescaler = 40;
htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
htim3.Init.Period = 1;
htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_Base_Init(&htim3) != HAL_OK)
{
Error_Handler();
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN TIM3_Init 2 */
/* USER CODE END TIM3_Init 2 */
}
/**
* @brief USART2 Initialization Function
* @param None
@@ -537,10 +534,8 @@ static void MX_GPIO_Init(void)
GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
/*Configure GPIO pins : PC13 PC0 PC3 PC6
PC7 */
GPIO_InitStruct.Pin = GPIO_PIN_13|GPIO_PIN_0|GPIO_PIN_3|GPIO_PIN_6
|GPIO_PIN_7;
/*Configure GPIO pins : PC13 PC0 PC3 */
GPIO_InitStruct.Pin = GPIO_PIN_13|GPIO_PIN_0|GPIO_PIN_3;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
@@ -552,16 +547,18 @@ static void MX_GPIO_Init(void)
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
/*Configure GPIO pins : PA0 PA1 PA8 PA9
PA10 PA14 PA15 */
GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_8|GPIO_PIN_9
|GPIO_PIN_10|GPIO_PIN_14|GPIO_PIN_15;
/*Configure GPIO pins : PA0 PA1 PA8 PA14
PA15 */
GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_8|GPIO_PIN_14
|GPIO_PIN_15;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/*Configure GPIO pins : PA4 PA5 PA6 */
GPIO_InitStruct.Pin = GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6;
/*Configure GPIO pins : PA4 PA5 PA6 PA9
PA10 */
GPIO_InitStruct.Pin = GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_9
|GPIO_PIN_10;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLDOWN;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
@@ -589,11 +586,21 @@ static void MX_GPIO_Init(void)
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
/*Configure GPIO pins : PD12 PD13 PD14 PD15
PD0 PD1 PD3 PD4
/*Configure GPIO pins : PD12 PD13 PD14 PD15 */
GPIO_InitStruct.Pin = GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLDOWN;
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
/*Configure GPIO pins : PC6 PC7 */
GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLDOWN;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
/*Configure GPIO pins : PD0 PD1 PD3 PD4
PD5 PD6 PD7 */
GPIO_InitStruct.Pin = GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15
|GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_3|GPIO_PIN_4
GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_3|GPIO_PIN_4
|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
@@ -610,11 +617,6 @@ static void MX_GPIO_Init(void)
}
/* USER CODE BEGIN 4 */
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) {
if (htim->Instance == htim3.Instance) {
control_execute_state();
}
}
/* USER CODE END 4 */
/**

View File

@@ -77,6 +77,71 @@ void HAL_MspInit(void)
/* USER CODE END MspInit 1 */
}
/**
* @brief I2C MSP Initialization
* This function configures the hardware resources used in this example
* @param hi2c: I2C handle pointer
* @retval None
*/
void HAL_I2C_MspInit(I2C_HandleTypeDef* hi2c)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if(hi2c->Instance==I2C1)
{
/* USER CODE BEGIN I2C1_MspInit 0 */
/* USER CODE END I2C1_MspInit 0 */
__HAL_RCC_GPIOB_CLK_ENABLE();
/**I2C1 GPIO Configuration
PB6 ------> I2C1_SCL
PB7 ------> I2C1_SDA
*/
GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7;
GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF4_I2C1;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/* Peripheral clock enable */
__HAL_RCC_I2C1_CLK_ENABLE();
/* USER CODE BEGIN I2C1_MspInit 1 */
/* USER CODE END I2C1_MspInit 1 */
}
}
/**
* @brief I2C MSP De-Initialization
* This function freeze the hardware resources used in this example
* @param hi2c: I2C handle pointer
* @retval None
*/
void HAL_I2C_MspDeInit(I2C_HandleTypeDef* hi2c)
{
if(hi2c->Instance==I2C1)
{
/* USER CODE BEGIN I2C1_MspDeInit 0 */
/* USER CODE END I2C1_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_I2C1_CLK_DISABLE();
/**I2C1 GPIO Configuration
PB6 ------> I2C1_SCL
PB7 ------> I2C1_SDA
*/
HAL_GPIO_DeInit(GPIOB, GPIO_PIN_6|GPIO_PIN_7);
/* USER CODE BEGIN I2C1_MspDeInit 1 */
/* USER CODE END I2C1_MspDeInit 1 */
}
}
/**
* @brief SD MSP Initialization
* This function configures the hardware resources used in this example
@@ -162,56 +227,6 @@ void HAL_SD_MspDeInit(SD_HandleTypeDef* hsd)
}
/**
* @brief TIM_Base MSP Initialization
* This function configures the hardware resources used in this example
* @param htim_base: TIM_Base handle pointer
* @retval None
*/
void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base)
{
if(htim_base->Instance==TIM3)
{
/* USER CODE BEGIN TIM3_MspInit 0 */
/* USER CODE END TIM3_MspInit 0 */
/* Peripheral clock enable */
__HAL_RCC_TIM3_CLK_ENABLE();
/* TIM3 interrupt Init */
HAL_NVIC_SetPriority(TIM3_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(TIM3_IRQn);
/* USER CODE BEGIN TIM3_MspInit 1 */
/* USER CODE END TIM3_MspInit 1 */
}
}
/**
* @brief TIM_Base MSP De-Initialization
* This function freeze the hardware resources used in this example
* @param htim_base: TIM_Base handle pointer
* @retval None
*/
void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* htim_base)
{
if(htim_base->Instance==TIM3)
{
/* USER CODE BEGIN TIM3_MspDeInit 0 */
/* USER CODE END TIM3_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_TIM3_CLK_DISABLE();
/* TIM3 interrupt DeInit */
HAL_NVIC_DisableIRQ(TIM3_IRQn);
/* USER CODE BEGIN TIM3_MspDeInit 1 */
/* USER CODE END TIM3_MspDeInit 1 */
}
}
/**
* @brief UART MSP Initialization
* This function configures the hardware resources used in this example

View File

@@ -57,7 +57,6 @@
/* External variables --------------------------------------------------------*/
extern PCD_HandleTypeDef hpcd_USB_OTG_FS;
extern TIM_HandleTypeDef htim3;
extern UART_HandleTypeDef huart2;
/* USER CODE BEGIN EV */
@@ -199,20 +198,6 @@ void SysTick_Handler(void)
/* please refer to the startup file (startup_stm32f4xx.s). */
/******************************************************************************/
/**
* @brief This function handles TIM3 global interrupt.
*/
void TIM3_IRQHandler(void)
{
/* USER CODE BEGIN TIM3_IRQn 0 */
/* USER CODE END TIM3_IRQn 0 */
HAL_TIM_IRQHandler(&htim3);
/* USER CODE BEGIN TIM3_IRQn 1 */
/* USER CODE END TIM3_IRQn 1 */
}
/**
* @brief This function handles USART2 global interrupt.
*/