Skip to content

Commit

Permalink
started working on serial comms
Browse files Browse the repository at this point in the history
  • Loading branch information
robotsrulz committed Feb 20, 2017
1 parent 3ae132b commit 43f91c2
Show file tree
Hide file tree
Showing 10 changed files with 469 additions and 127 deletions.
4 changes: 4 additions & 0 deletions FlashDebug.jscript
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
h
loadbin bin\Debug\MKS-TFT32.hex, 0
r
q
10 changes: 7 additions & 3 deletions Inc/ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,20 @@ typedef struct
SDCARD_INSERT,
SDCARD_REMOVE,
USBDRIVE_INSERT,
USBDRIVE_REMOVE
USBDRIVE_REMOVE,
SHOW_STATUS
} ucEventID;
union {
unsigned int touchXY;
} ucData;
} xEvent_t;
} xUIEvent_t;

extern QueueHandle_t xUIEventQueue;

typedef void (*volatile eventProcessor_t) (xEvent_t *);
#define MAXSTATSIZE 320/8
extern uint8_t statString[MAXSTATSIZE+1];

typedef void (*volatile eventProcessor_t) (xUIEvent_t *);
extern eventProcessor_t processEvent;

typedef enum {
Expand Down
26 changes: 24 additions & 2 deletions MKS-TFT32.ebp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@
<Linker>
<Add option="-Wl,--gc-sections" />
</Linker>
<ExtraCommands>
<Add after='&quot;C:\Program Files (x86)\SEGGER\JLink_V612f\JLink.exe&quot; -if swd -device STM32F107VC -speed 4000 -CommanderScript $(ProjectDir)FlashDebug.jscript' />
<Mode before="0" />
<Mode after="2" />
</ExtraCommands>
</Target>
<Target title="Release">
<Option output="bin\Release\MKS-TFT32.elf" />
Expand Down Expand Up @@ -197,6 +202,9 @@
<Unit filename="Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_ll_usb.c">
<Option compilerVar="CC" />
</Unit>
<Unit filename="FlashDebug.jscript">
<Option target="Debug" />
</Unit>
<Unit filename="Inc\eeprom.h" />
<Unit filename="Inc\fatfs.h" />
<Unit filename="Inc\ffconf.h" />
Expand Down Expand Up @@ -304,6 +312,9 @@
<Unit filename="Middlewares\Third_Party\FreeRTOS\Source\timers.c">
<Option compilerVar="CC" />
</Unit>
<Unit filename="MKS_TFT.jflash">
<Option target="Debug" />
</Unit>
<Unit filename="Src\cp866-8x14.c">
<Option compilerVar="CC" />
</Unit>
Expand All @@ -325,6 +336,9 @@
<Unit filename="Src\main.c">
<Option compilerVar="CC" />
</Unit>
<Unit filename="Src\serial_io.c">
<Option compilerVar="CC" />
</Unit>
<Unit filename="Src\spiflash_w25q16dv.c">
<Option compilerVar="CC" />
</Unit>
Expand Down Expand Up @@ -356,7 +370,15 @@
<Extensions>
<code_completion />
<debugger>
<target_debugging_settings target="Release" active_interface="ST-link">
<target_debugging_settings target="Debug" active_interface="ST-link">
<debug_interface interface_id="J-link" ip_address="" ip_port="" path="" executable="" description="" dont_start_server="false" backoff_time="" options="0" reg_filter="0" active_family="" gdb_before_conn="" gdb_after_conn="">
<family_options family_id="Generic">
<option opt_id="ID_CMD_BEFORE" opt_value="" />
<option opt_id="ID_GDB_SERVER_CL" opt_value="" />
<option opt_id="ID_CMD_RESET" opt_value="" />
<option opt_id="ID_CMD_AFTER" opt_value="" />
</family_options>
</debug_interface>
<debug_interface interface_id="ST-link" ip_address="localhost" ip_port="4242" path="${EMBITZ}\share\contrib" executable="STLinkGDB.exe" description="" dont_start_server="false" backoff_time="1000" options="0" reg_filter="0" active_family="STMicroelectronics" gdb_before_conn="" gdb_after_conn="">
<family_options family_id="STMicroelectronics">
<option opt_id="ID_JTAG_SWD" opt_value="swd" />
Expand All @@ -373,7 +395,7 @@
</family_options>
</debug_interface>
</target_debugging_settings>
<target_debugging_settings target="Debug" active_interface="ST-link">
<target_debugging_settings target="Release" active_interface="ST-link">
<debug_interface interface_id="ST-link" ip_address="localhost" ip_port="4242" path="${EMBITZ}\share\contrib" executable="STLinkGDB.exe" description="" dont_start_server="false" backoff_time="1000" options="0" reg_filter="0" active_family="STMicroelectronics" gdb_before_conn="" gdb_after_conn="">
<family_options family_id="STMicroelectronics">
<option opt_id="ID_JTAG_SWD" opt_value="swd" />
Expand Down
73 changes: 73 additions & 0 deletions MKS_TFT.jflash
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
AppVersion = 61206
FileVersion = 2
[GENERAL]
ConnectMode = 0
CurrentFile = ""
DataFileSAddr = 0x00000000
GUIMode = 0
HostName = ""
TargetIF = 1
USBPort = 0
USBSerialNo = 0x00000000
[JTAG]
IRLen = 0
MultipleTargets = 0
NumDevices = 0
Speed0 = 4000
Speed1 = 4000
TAP_Number = 0
UseAdaptive0 = 0
UseAdaptive1 = 0
UseMaxSpeed0 = 0
UseMaxSpeed1 = 0
[CPU]
NumInitSteps = 1
InitStep0_Action = "Reset"
InitStep0_Value0 = 0x00000000
InitStep0_Value1 = 0x00000000
InitStep0_Comment = "Reset and halt target"
NumExitSteps = 0
UseScriptFile = 0
ScriptFile = ""
UseRAM = 1
RAMAddr = 0x20000000
RAMSize = 0x00010000
CheckCoreID = 1
CoreID = 0x3BA00477
CoreIDMask = 0x0F000FFF
UseAutoSpeed = 0x00000001
ClockSpeed = 0x00000000
EndianMode = 0
ChipName = "ST STM32F107VC"
[FLASH]
aRangeSel[1] = 0-127
BankName = "Internal flash"
BankSelMode = 1
BaseAddr = 0x08000000
NumBanks = 1
[PRODUCTION]
AutoPerformsErase = 1
AutoPerformsProgram = 1
AutoPerformsSecure = 0
AutoPerformsStartApp = 0
AutoPerformsUnsecure = 0
AutoPerformsVerify = 1
EnableTargetPower = 0
EraseType = 2
MonitorVTref = 0
MonitorVTrefMax = 0x0000157C
MonitorVTrefMin = 0x000003E8
OverrideTimeouts = 0
ProgramSN = 0
SerialFile = ""
SNAddr = 0x00000000
SNInc = 0x00000001
SNLen = 0x00000004
SNListFile = ""
SNValue = 0x00000001
StartAppType = 0
TargetPowerDelay = 0x00000014
TimeoutErase = 0x00003A98
TimeoutProgram = 0x00002710
TimeoutVerify = 0x00002710
VerifyType = 1
94 changes: 85 additions & 9 deletions Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ SPI_HandleTypeDef hspi3;

UART_HandleTypeDef huart2;
UART_HandleTypeDef huart3;
DMA_HandleTypeDef hdma_usart2_rx;

TIM_HandleTypeDef htim2;

Expand All @@ -65,12 +66,19 @@ static osThreadId sdcardHandlerHandle; // sd card insert/remove
QueueHandle_t xUIEventQueue;
QueueHandle_t xPCommEventQueue;

#define MAXCOMM1SIZE 0xff // Biggest string the user will type
static uint8_t comm1RxBuffer = '\000'; // where we store that one character that just came in
static uint8_t comm1RxString[MAXCOMM1SIZE]; // where we build our string from characters coming in
static int comm1RxIndex = 0; // index for going though comm1RxString

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
void Error_Handler(void);

static void MX_GPIO_Init(void);
static void MX_DMA_Init(void);
static void MX_TIM2_Init(void);
static void MX_I2C1_Init(void);
static void MX_SPI1_Init(void);
Expand All @@ -92,6 +100,8 @@ void HAL_TIM_MspPostInit(TIM_HandleTypeDef *htim);
/* USER CODE BEGIN 0 */
static SemaphoreHandle_t xTouchSemaphore;
static SemaphoreHandle_t xSDSemaphore;
static SemaphoreHandle_t xComm1Semaphore;
static SemaphoreHandle_t xComm2Semaphore;
/* USER CODE END 0 */

int main(void)
Expand All @@ -110,6 +120,7 @@ int main(void)

/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_DMA_Init();
MX_TIM2_Init();
MX_I2C1_Init();
MX_SPI1_Init();
Expand All @@ -128,7 +139,9 @@ int main(void)
/* USER CODE BEGIN RTOS_SEMAPHORES */
/* add semaphores, ... */
xTouchSemaphore = xSemaphoreCreateBinary();
xSDSemaphore = xSemaphoreCreateBinary();
xSDSemaphore = xSemaphoreCreateBinary();
xComm1Semaphore = xSemaphoreCreateBinary();
xComm2Semaphore = xSemaphoreCreateBinary();
/* USER CODE END RTOS_SEMAPHORES */

/* USER CODE BEGIN RTOS_TIMERS */
Expand Down Expand Up @@ -158,12 +171,12 @@ int main(void)

/* USER CODE BEGIN RTOS_QUEUES */
/* add queues, ... */
xUIEventQueue = xQueueCreate(1, sizeof(xEvent_t));
xUIEventQueue = xQueueCreate(1, sizeof(xUIEvent_t));
if (xUIEventQueue == NULL) {
/* Queue was not created and must not be used. */
}

xPCommEventQueue = xQueueCreate(10, sizeof(xEvent_t));
xPCommEventQueue = xQueueCreate(10, sizeof(xUIEvent_t));
if (xPCommEventQueue == NULL) {
/* Queue was not created and must not be used. */
}
Expand Down Expand Up @@ -341,6 +354,20 @@ static void MX_USART3_UART_Init(void)

}

/**
* Enable DMA controller clock
*/
static void MX_DMA_Init(void)
{
/* DMA controller clock enable */
__HAL_RCC_DMA1_CLK_ENABLE();

/* DMA interrupt init */
/* DMA1_Channel6_IRQn interrupt configuration */
HAL_NVIC_SetPriority(DMA1_Channel6_IRQn, 5, 0);
HAL_NVIC_EnableIRQ(DMA1_Channel6_IRQn);
}

/** Configure pins as
* Analog
* Input
Expand Down Expand Up @@ -576,7 +603,7 @@ void StartTouchHandlerTask(void const * argument) {

HAL_GPIO_WritePin(TOUCH_nCS_GPIO_Port, TOUCH_nCS_Pin, GPIO_PIN_SET);

xEvent_t event;
xUIEvent_t event;
event.ucEventID = TOUCH_DOWN_EVENT;

xTouchX = Lcd_Touch_Get_Closest_Average(x);
Expand All @@ -590,7 +617,7 @@ void StartTouchHandlerTask(void const * argument) {
} else {

if (xTouchX && xTouchY) {
xEvent_t event;
xUIEvent_t event;
event.ucEventID = TOUCH_UP_EVENT;
event.ucData.touchXY = ((unsigned int) xTouchX << 16) + xTouchY;
xQueueSendToBack(xUIEventQueue, &event, 1000);
Expand All @@ -614,7 +641,7 @@ void StartSDHandlerTask(void const * argument) {

osDelay(100);

xEvent_t event;
xUIEvent_t event;
event.ucEventID =
(HAL_GPIO_ReadPin(SDCARD_DETECT_GPIO_Port, SDCARD_DETECT_Pin)
== GPIO_PIN_RESET) ? SDCARD_INSERT : SDCARD_REMOVE;
Expand All @@ -625,8 +652,24 @@ void StartSDHandlerTask(void const * argument) {

void StartComm1Task(void const * argument) {

__HAL_UART_FLUSH_DRREGISTER(&huart2);
HAL_UART_Receive_DMA(&huart2, &comm1RxBuffer, 1);

while (1) {
osDelay(1);
if(xSemaphoreTake(xComm1Semaphore, 5000 /* FIXME */ ) == pdTRUE ) {

xUIEvent_t event;
event.ucEventID = SHOW_STATUS;
xQueueSendToBack(xUIEventQueue, &event, 1000);
}
else
{
// osDelay(1);
static const char m115[] = "M115";

HAL_UART_Transmit(&huart2, m115, /* sizeof(m115)*/ 4, 1000);
osDelay(20);
}
}
}

Expand All @@ -637,6 +680,39 @@ void StartComm2Task(void const * argument) {
}
}

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if (huart->Instance == USART2)
{
__HAL_UART_FLUSH_DRREGISTER(&huart2); // Clear the buffer to prevent overrun

int i = 0;

if (comm1RxBuffer == '\n' || comm1RxBuffer == '\r') // If Enter
{
snprintf(statString, MAXSTATSIZE, "%s", comm1RxString);


comm1RxString[comm1RxIndex] = 0;
comm1RxIndex = 0;
for (i = 0; i < MAXSTATSIZE; i++) comm1RxString[i] = 0; // Clear the string buffer

BaseType_t xHigherPriorityTaskWoken = pdFALSE;
xSemaphoreGiveFromISR(xComm1Semaphore, &xHigherPriorityTaskWoken);
}
else
{
comm1RxString[comm1RxIndex] = comm1RxBuffer; // Add that character to the string
comm1RxIndex++;
if (comm1RxIndex > MAXSTATSIZE) // User typing too much, we can't have commands that big
{
comm1RxIndex = 0;
for (i = 0; i < MAXSTATSIZE; i++) comm1RxString[i] = 0; // Clear the string buffer
}
}
//
}
}
/* USER CODE END 4 */

/* StartUITask function */
Expand All @@ -653,7 +729,7 @@ void StartUITask(void const * argument) {
osDelay(50);
HAL_TIM_OC_Stop_IT(&htim2, TIM_CHANNEL_3);

xEvent_t event;
xUIEvent_t event;
event.ucEventID = INIT_EVENT;
(*processEvent) (&event);

Expand All @@ -662,7 +738,7 @@ void StartUITask(void const * argument) {
for (;;) {
if (xUIEventQueue != 0) {

xEvent_t event;
xUIEvent_t event;

if (xQueueReceive(xUIEventQueue, &event, (TickType_t ) 500)) {

Expand Down
Loading

0 comments on commit 43f91c2

Please sign in to comment.