2018/02/23



/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
** This notice applies to any and all portions of this file
* that are not between comment pairs USER CODE BEGIN and
* USER CODE END. Other portions of this file, whether
* inserted by the user or by software development tools
* are owned by their respective copyright owners.
*
* COPYRIGHT(c) 2018 STMicroelectronics
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of STMicroelectronics nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "stm32f0xx_hal.h"

/* USER CODE BEGIN Includes */
#include
#include
#include
#include
/* USER CODE END Includes */

/* Private variables ---------------------------------------------------------*/
SPI_HandleTypeDef hspi1;

TIM_HandleTypeDef htim3;

UART_HandleTypeDef huart1;

/* USER CODE BEGIN PV */
/* Private variables ---------------------------------------------------------*/
#define _NOP() asm("NOP")

#define RxBufferSize 0x40

char RxBuffer1[RxBufferSize];
uint16_t RxCounter1 = 0;

char key_value[2];

uint16_t sensor_flag = 0;
/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_SPI1_Init(void);
static void MX_TIM3_Init(void);
static void MX_USART1_UART_Init(void);

void HAL_TIM_MspPostInit(TIM_HandleTypeDef *htim);


/* USER CODE BEGIN PFP */
/* Private function prototypes -----------------------------------------------*/

/* USER CODE END PFP */

/* USER CODE BEGIN 0 */
#ifdef __GNUC__
/* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
set to 'Yes') calls __io_putchar() */
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */

int _write(int file, char *ptr, int len)
{
int DataIdx;
for (DataIdx = 0; DataIdx < len; DataIdx++){ __io_putchar( *ptr++ );}
return len;
}

PUTCHAR_PROTOTYPE
{
/* Place your implementation of fputc here */
/* e.g. write a character to the EVAL_COM1 and Loop until the end of transmission */
HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 1);

return ch;
}

uint32_t UART1_get_Number(void){
uint32_t KeyIn, i;
uint8_t key_value;
uint8_t L6470_RxBuffer[8], L6470_TxBuffer[8];
/* Receive a string (Max RxBufferSize bytes) from the Hyperterminal ended by '\r' (Enter key) */
do
{
if((HAL_UART_GetState(&huart1) != HAL_UART_STATE_ERROR)&&(RxCounter1 < RxBufferSize))
{
//key_value = USART_ReceiveData(USART1);
while(HAL_UART_Receive(&huart1, (uint8_t *)&key_value, 1, 1) != HAL_OK){
//_NOP();
if(sensor_flag){
switch(sensor_flag){
case 1<<13:
printf("Sensor 0\r\n");
L6470_TxBuffer[0] = 0xB8;
L6470_DRV(0, L6470_TxBuffer, L6470_RxBuffer, 1);
break;
case 1<<10:
printf("Sensor 1\r\n");
L6470_TxBuffer[0] = 0xB8;
L6470_DRV(0, L6470_TxBuffer, L6470_RxBuffer, 1);
break;
case 1<<9:
printf("Sensor 2\r\n");
break;
case 1<<8:
printf("Sensor 3\r\n");
break;
}
}
}
//USART_SendData(USART1, key_value);
HAL_UART_Transmit(&huart1, (uint8_t *)&key_value, 1, 1);
if(key_value == '\r'){
RxBuffer1[RxCounter1]='\0';
printf("\n");
KeyIn = atoi(RxBuffer1);

RxCounter1 = 0;
for(i = 0; i < RxBufferSize; i++)
RxBuffer1[i] = 0;

//USART_ReceiveData(USART1);
HAL_UART_Receive(&huart1, (uint8_t *)&key_value, 1, 1);
return KeyIn;
}
else{
RxBuffer1[RxCounter1++] = key_value;
}
}
}while((key_value != '\r')&&(RxCounter1 != RxBufferSize));
return -1;
}

void L6470_DRV(uint8_t DRV_num, uint8_t *wp, uint8_t *rp, uint8_t param_num){
uint8_t i;
GPIO_TypeDef * L_SCS_Port;
uint16_t L_SCS_Pin;

if(DRV_num == 0){//Rotation Axis
L_SCS_Port = SCS_GPIO_Port;
L_SCS_Pin = SCS_Pin;
}

for(i=0;i HAL_GPIO_WritePin(L_SCS_Port, L_SCS_Pin, GPIO_PIN_RESET);
HAL_SPI_TransmitReceive(&hspi1, wp+i, rp+i, 1, 1000);
while (__HAL_SPI_GET_FLAG(&hspi1, SPI_FLAG_BSY) != 0);
HAL_GPIO_WritePin(L_SCS_Port, L_SCS_Pin, GPIO_PIN_SET);
HAL_Delay(1);
}
}
/* USER CODE END 0 */

/**
* @brief The application entry point.
*
* @retval None
*/
int main(void)
{
/* USER CODE BEGIN 1 */
uint16_t KeyIn = 0;
uint8_t cnt_newline = 0;

uint8_t L6470_RxBuffer[8], L6470_TxBuffer[8];
uint8_t L6470_Axis, L6470_Param, L6470_Value0, L6470_Value1, L6470_Value2, L6470_tmp;
uint8_t L6470_Run_Dir;
uint32_t L6470_Run_Spd, L6470_step, L6470_Axis01_0ffset, L6470_Axis02_0ffset;
uint8_t L6470_StepClock_Dir;
uint8_t L6470_Move_Dir;
uint32_t L6470_Move_N_STEP;
uint32_t L6470_GoTo_ABS_POS;
uint8_t L6470_GoTo_DIR_Dir;
uint32_t L6470_GoTo_DIR_ABS_POS;
uint8_t L6470_GoUntil_ACT, L6470_GoUntil_DIR;
uint32_t L6470_GoUntil_SPD;
uint8_t L6470_ReleaseSW_ACT, L6470_ReleaseSW_DIR;
/* USER CODE END 1 */

/* MCU Configuration----------------------------------------------------------*/

/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();

/* USER CODE BEGIN Init */

/* USER CODE END Init */

/* Configure the system clock */
SystemClock_Config();

/* USER CODE BEGIN SysInit */

/* USER CODE END SysInit */

/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_SPI1_Init();
MX_TIM3_Init();
MX_USART1_UART_Init();
/* USER CODE BEGIN 2 */
HAL_GPIO_WritePin(SR_GPIO_Port, SR_Pin, GPIO_PIN_RESET);
HAL_Delay(100);
HAL_GPIO_WritePin(SR_GPIO_Port, SR_Pin, GPIO_PIN_SET);
HAL_Delay(100);

L6470_TxBuffer[0] = 0xA8;
L6470_DRV(0, L6470_TxBuffer, L6470_RxBuffer, 1);
HAL_Delay(10);

L6470_TxBuffer[0] = 0x18;
L6470_TxBuffer[1] = 0x2e;
L6470_TxBuffer[2] = 0x8B;
L6470_TxBuffer[3] = 0;
L6470_DRV(0, L6470_TxBuffer, L6470_RxBuffer, 4);
HAL_Delay(10);

printf("\r\n\r\ntest_LAZAREV V001 FW Ver 0.0.1 Start -->\r\n");
printf("1. HELP : Show menu explanation\r\n");
printf("2. L6470 Reset reset\r\n");
printf("3. L6470 Reset set\r\n");
printf("4. L6470 SetParam : Transmit and Receive SPI\r\n");
printf("5. L6470 GetParam : Transmit and Receive SPI\r\n");
printf("6. L6470 Run(DIR, SPD) : Transmit and Receive SPI\r\n");
printf("7. L6470 StepClock(DIR) : Transmit and Receive SPI\r\n");
printf("8. L6470 Move(DIR, N_STEP) : Transmit and Receive SPI\r\n");
printf("9. L6470 GoTo(ABS_POS) : Transmit and Receive SPI\r\n");
printf("10. L6470 GoTo_DIR(DIR, ABS_POS) : Transmit and Receive SPI\r\n");
printf("11. L6470 GoUntil(ACT, DIR, SPD) : Transmit and Receive SPI\r\n");
printf("12. L6470 ReleaseSW(ACT, DIR) : Transmit and Receive SPI\r\n");
printf("13. L6470 GoHome : Transmit and Receive SPI\r\n");
printf("14. L6470 GoMark : Transmit and Receive SPI\r\n");
printf("15. L6470 ResetPos : Transmit and Receive SPI\r\n");
printf("16. L6470 ResetDevice : Transmit and Receive SPI\r\n");
printf("17. L6470 SoftStop : Transmit and Receive SPI\r\n");
printf("18. L6470 HardStop : Transmit and Receive SPI\r\n");
printf("19. L6470 SoftHiZ : Transmit and Receive SPI\r\n");
printf("21. L6470 HardHiZ : Transmit and Receive SPI\r\n");
printf("21. L6470 GeStatus : Transmit and Receive SPI\r\n");
printf("22. L6470 Search Home\r\n");
printf("JBElec>");
fflush(stdout);
/* USER CODE END 2 */

/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
PHASE1:
KeyIn = UART1_get_Number();
switch(KeyIn){
case 1:
printf("1. HELP : Show menu explanation\r\n");
printf("2. L6470 Reset reset\r\n");
printf("3. L6470 Reset set\r\n");
printf("4. L6470 SetParam : Transmit and Receive SPI\r\n");
printf("5. L6470 GetParam : Transmit and Receive SPI\r\n");
printf("6. L6470 Run(DIR, SPD) : Transmit and Receive SPI\r\n");
printf("7. L6470 StepClock(DIR) : Transmit and Receive SPI\r\n");
printf("8. L6470 Move(DIR, N_STEP) : Transmit and Receive SPI\r\n");
printf("9. L6470 GoTo(ABS_POS) : Transmit and Receive SPI\r\n");
printf("10. L6470 GoTo_DIR(DIR, ABS_POS) : Transmit and Receive SPI\r\n");
printf("11. L6470 GoUntil(ACT, DIR, SPD) : Transmit and Receive SPI\r\n");
printf("12. L6470 ReleaseSW(ACT, DIR) : Transmit and Receive SPI\r\n");
printf("13. L6470 GoHome : Transmit and Receive SPI\r\n");
printf("14. L6470 GoMark : Transmit and Receive SPI\r\n");
printf("15. L6470 ResetPos : Transmit and Receive SPI\r\n");
printf("16. L6470 ResetDevice : Transmit and Receive SPI\r\n");
printf("17. L6470 SoftStop : Transmit and Receive SPI\r\n");
printf("18. L6470 HardStop : Transmit and Receive SPI\r\n");
printf("19. L6470 SoftHiZ : Transmit and Receive SPI\r\n");
printf("20. L6470 HardHiZ : Transmit and Receive SPI\r\n");
printf("21. L6470 GeStatus : Transmit and Receive SPI\r\n");
printf("22. L6470 Search Home\r\n");

break;
case 2:
printf("2. L6470 Reset reset\r\n");
HAL_GPIO_WritePin(SR_GPIO_Port, SR_Pin, GPIO_PIN_RESET);

break;
case 3:
printf("3. L6470 Reset set\r\n");
HAL_GPIO_WritePin(SR_GPIO_Port, SR_Pin, GPIO_PIN_SET);

break;
case 4:
printf("4. L6470 SetParam : Transmit and Receive SPI\r\n");

SetParam_axis:
printf(" Input Axis[1:0] >");fflush(stdout);
L6470_Axis = UART1_get_Number();
if(L6470_Axis < 0 || L6470_Axis > 1){
printf("Invalid input value\r\n");
goto SetParam_axis;
}

SetParam_param:
printf(" Input Param[27:0] >");fflush(stdout);
L6470_Param = UART1_get_Number();
if(L6470_Param < 0 || L6470_Param > 27){
printf("Invalid input value\r\n");
goto SetParam_param;
}

SetParam_value2:
printf(" Input Value2[255:0] >");fflush(stdout);
L6470_Value2 = UART1_get_Number();
if(L6470_Value2 < 0 || L6470_Value2 > 255){
printf("Invalid input value\r\n");
goto SetParam_value2;
}

SetParam_value1:
printf(" Input Value1[255:0] >");fflush(stdout);
L6470_Value1 = UART1_get_Number();
if(L6470_Value1 < 0 || L6470_Value1 > 255){
printf("Invalid input value\r\n");
goto SetParam_value1;
}

SetParam_value0:
printf(" Input Value0[255:0] >");fflush(stdout);
L6470_Value0 = UART1_get_Number();
if(L6470_Value0 < 0 || L6470_Value0 > 255){
printf("Invalid input value\r\n");
goto SetParam_value0;
}

L6470_TxBuffer[0] = L6470_Param;
L6470_TxBuffer[1] = L6470_Value2;
L6470_TxBuffer[2] = L6470_Value1;
L6470_TxBuffer[3] = L6470_Value0;
L6470_DRV(L6470_Axis, L6470_TxBuffer, L6470_RxBuffer, 4);

break;
case 5:
printf("5. L6470 GetParam : Transmit and Receive SPI\r\n");

GetParam_axis:
printf(" Input Axis[1:0] >");fflush(stdout);
L6470_Axis = UART1_get_Number();
if(L6470_Axis < 0 || L6470_Axis > 1){
printf("Invalid input value\r\n");
goto GetParam_axis;
}

GetParam_param:
printf(" Input Param[27:0] >");fflush(stdout);
L6470_Param = UART1_get_Number();
if(L6470_Param < 0 || L6470_Param > 27){
printf("Invalid input value\r\n");
goto GetParam_param;
}

L6470_TxBuffer[0] = 0x20 | L6470_Param;
L6470_TxBuffer[1] = 0;
L6470_TxBuffer[2] = 0;
L6470_TxBuffer[3] = 0;

L6470_RxBuffer[0] = 0;
L6470_RxBuffer[1] = 0;
L6470_RxBuffer[2] = 0;
L6470_RxBuffer[3] = 0;
L6470_DRV(L6470_Axis, L6470_TxBuffer, L6470_RxBuffer, 4);

printf("%x address Byte2 : %x, Byte1 : %x, Byte0 : %x\r\n", L6470_Param, L6470_RxBuffer[1], L6470_RxBuffer[2], L6470_RxBuffer[3]);

break;
case 6:
printf("6. L6470 Run(DIR, SPD) : Transmit and Receive SPI\r\n");

Run_axis:
printf(" Input Axis[1:0] >");fflush(stdout);
L6470_Axis = UART1_get_Number();
if(L6470_Axis < 0 || L6470_Axis > 1){
printf("Invalid input value\r\n");
goto Run_axis;
}

Run_dir:
printf(" Input DIR[1:0] >");fflush(stdout);
L6470_Run_Dir = UART1_get_Number();
if(L6470_Run_Dir < 0 || L6470_Run_Dir > 1){
printf("Invalid input value\r\n");
goto Run_dir;
}

Run_spd:
printf(" Input SPD[1048575:0] >");fflush(stdout);
L6470_Run_Spd = UART1_get_Number();
if(L6470_Run_Spd < 0 || L6470_Run_Spd > 1048575){
printf("Invalid input value\r\n");
goto Run_spd;
}

L6470_TxBuffer[0] = 0x50 | L6470_Run_Dir;
L6470_TxBuffer[1] = L6470_Run_Spd >> 16;
L6470_TxBuffer[2] = L6470_Run_Spd >> 8;
L6470_TxBuffer[3] = L6470_Run_Spd;
L6470_DRV(L6470_Axis, L6470_TxBuffer, L6470_RxBuffer, 4);

break;
case 7:
printf("7. L6470 StepClock(DIR) : Transmit and Receive SPI\r\n");

StepClock_axis:
printf(" Input Axis[1:0] >");fflush(stdout);
L6470_Axis = UART1_get_Number();
if(L6470_Axis < 0 || L6470_Axis > 1){
printf("Invalid input value\r\n");
goto StepClock_axis;
}

StepClock_dir:
printf(" Input DIR[1:0] >");fflush(stdout);
L6470_StepClock_Dir = UART1_get_Number();
if(L6470_StepClock_Dir < 0 || L6470_StepClock_Dir > 1){
printf("Invalid input value\r\n");
goto StepClock_dir;
}

L6470_TxBuffer[0] = 0x58 | L6470_StepClock_Dir;
L6470_DRV(L6470_Axis, L6470_TxBuffer, L6470_RxBuffer, 1);

break;
case 8:
printf("8. L6470 Move(DIR, N_STEP) : Transmit and Receive SPI\r\n");

Move_axis:
printf(" Input Axis[1:0] >");fflush(stdout);
L6470_Axis = UART1_get_Number();
if(L6470_Axis < 0 || L6470_Axis > 1){
printf("Invalid input value\r\n");
goto Move_axis;
}

Move_dir:
printf(" Input DIR[1:0] >");fflush(stdout);
L6470_Move_Dir = UART1_get_Number();
if(L6470_Move_Dir < 0 || L6470_Move_Dir > 1){
printf("Invalid input value\r\n");
goto Move_dir;
}

Move_N_STEP:
printf(" Input N_STEP[4194303:0] >");fflush(stdout);
L6470_Move_N_STEP = UART1_get_Number();
if(L6470_Move_N_STEP < 0 || L6470_Move_N_STEP > 4194303){
printf("Invalid input value\r\n");
goto Move_N_STEP;
}

L6470_TxBuffer[0] = 0x40 | L6470_Move_Dir;
L6470_TxBuffer[1] = L6470_Move_N_STEP >> 16;
L6470_TxBuffer[2] = L6470_Move_N_STEP >> 8;
L6470_TxBuffer[3] = L6470_Move_N_STEP;
L6470_DRV(L6470_Axis, L6470_TxBuffer, L6470_RxBuffer, 4);

break;
case 9:
printf("9. L6470 GoTo(ABS_POS) : Transmit and Receive SPI\r\n");

GoTo_ABS_axis:
printf(" Input Axis[1:0] >");fflush(stdout);
L6470_Axis = UART1_get_Number();
if(L6470_Axis < 0 || L6470_Axis > 1){
printf("Invalid input value\r\n");
goto GoTo_ABS_axis;
}

GoTo_ABS_POS:
printf(" Input ABS_POS[4194303:0] >");fflush(stdout);
L6470_GoTo_ABS_POS = UART1_get_Number();
if(L6470_GoTo_ABS_POS < 0 || L6470_GoTo_ABS_POS > 4194303){
printf("Invalid input value\r\n");
goto GoTo_ABS_POS;
}

L6470_TxBuffer[0] = 0x60;
L6470_TxBuffer[1] = L6470_GoTo_ABS_POS >> 16;
L6470_TxBuffer[2] = L6470_GoTo_ABS_POS >> 8;
L6470_TxBuffer[3] = L6470_GoTo_ABS_POS;
L6470_DRV(L6470_Axis, L6470_TxBuffer, L6470_RxBuffer, 4);

break;
case 10:
printf("10. L6470 GoTo_DIR(DIR, ABS_POS) : Transmit and Receive SPI\r\n");

GoTo_DIR_axis:
printf(" Input Axis[1:0] >");fflush(stdout);
L6470_Axis = UART1_get_Number();
if(L6470_Axis < 0 || L6470_Axis > 1){
printf("Invalid input value\r\n");
goto GoTo_DIR_axis;
}

GoTo_DIR_dir:
printf(" Input DIR[1:0] >");fflush(stdout);
L6470_GoTo_DIR_Dir = UART1_get_Number();
if(L6470_GoTo_DIR_Dir < 0 || L6470_GoTo_DIR_Dir > 1){
printf("Invalid input value\r\n");
goto GoTo_DIR_dir;
}

GoTo_DIR_ABS_POS:
printf(" Input ABS_POS[4194303:0] >");fflush(stdout);
L6470_GoTo_DIR_ABS_POS = UART1_get_Number();
if(L6470_GoTo_DIR_ABS_POS < 0 || L6470_GoTo_DIR_ABS_POS > 4194303){
printf("Invalid input value\r\n");
goto GoTo_DIR_ABS_POS;
}

L6470_TxBuffer[0] = 0x68 | L6470_GoTo_DIR_Dir;
L6470_TxBuffer[1] = L6470_GoTo_DIR_ABS_POS >> 16;
L6470_TxBuffer[2] = L6470_GoTo_DIR_ABS_POS >> 8;
L6470_TxBuffer[3] = L6470_GoTo_DIR_ABS_POS;
L6470_DRV(L6470_Axis, L6470_TxBuffer, L6470_RxBuffer, 4);

break;
case 11:
printf("11. L6470 GoUntil(ACT, DIR, SPD) : Transmit and Receive SPI\r\n");

GoUntil_axis:
printf(" Input Axis[1:0] >");fflush(stdout);
L6470_Axis = UART1_get_Number();
if(L6470_Axis < 0 || L6470_Axis > 1){
printf("Invalid input value\r\n");
goto GoUntil_axis;
}

GoUntil_ACT:
printf(" Input ACT[1:0] >");fflush(stdout);
L6470_GoUntil_ACT = UART1_get_Number();
if(L6470_GoUntil_ACT < 0 || L6470_GoUntil_ACT > 1){
printf("Invalid input value\r\n");
goto GoUntil_ACT;
}

GoUntil_DIR:
printf(" Input DIR[1:0] >");fflush(stdout);
L6470_GoUntil_DIR = UART1_get_Number();
if(L6470_GoUntil_DIR < 0 || L6470_GoUntil_DIR > 1){
printf("Invalid input value\r\n");
goto GoUntil_DIR;
}

GoUntil_SPD:
printf(" Input SPD[1048575:0] >");fflush(stdout);
L6470_GoUntil_SPD = UART1_get_Number();
if(L6470_GoUntil_SPD < 0 || L6470_GoUntil_SPD > 1048575){
printf("Invalid input value\r\n");
goto GoUntil_SPD;
}

L6470_TxBuffer[0] = 0x82 | L6470_GoUntil_ACT << 3 | L6470_GoUntil_DIR;
L6470_TxBuffer[1] = L6470_GoUntil_SPD >> 16;
L6470_TxBuffer[2] = L6470_GoUntil_SPD >> 8;
L6470_TxBuffer[3] = L6470_GoUntil_SPD;
L6470_DRV(L6470_Axis, L6470_TxBuffer, L6470_RxBuffer, 4);

break;
case 12:
printf("12. L6470 ReleaseSW(ACT, DIR) : Transmit and Receive SPI\r\n");

ReleaseSW_axis:
printf(" Input Axis[1:0] >");fflush(stdout);
L6470_Axis = UART1_get_Number();
if(L6470_Axis < 0 || L6470_Axis > 1){
printf("Invalid input value\r\n");
goto ReleaseSW_axis;
}

ReleaseSW_ACT:
printf(" Input ACT[1:0] >");fflush(stdout);
L6470_ReleaseSW_ACT = UART1_get_Number();
if(L6470_ReleaseSW_ACT < 0 || L6470_ReleaseSW_ACT > 1){
printf("Invalid input value\r\n");
goto ReleaseSW_ACT;
}

ReleaseSW_DIR:
printf(" Input DIR[1:0] >");fflush(stdout);
L6470_ReleaseSW_DIR = UART1_get_Number();
if(L6470_ReleaseSW_DIR < 0 || L6470_ReleaseSW_DIR > 1){
printf("Invalid input value\r\n");
goto ReleaseSW_DIR;
}

L6470_TxBuffer[0] = 0x92 | L6470_ReleaseSW_ACT << 3 | L6470_ReleaseSW_DIR;
L6470_DRV(L6470_Axis, L6470_TxBuffer, L6470_RxBuffer, 1);

break;
case 13:
printf("13. L6470 GoHome : Transmit and Receive SPI\r\n");

GoHome_axis:
printf(" Input Axis[1:0] >");fflush(stdout);
L6470_Axis = UART1_get_Number();
if(L6470_Axis < 0 || L6470_Axis > 1){
printf("Invalid input value\r\n");
goto GoHome_axis;
}

L6470_TxBuffer[0] = 0x70;
L6470_DRV(L6470_Axis, L6470_TxBuffer, L6470_RxBuffer, 1);

break;
case 14:
printf("14. L6470 GoMark : Transmit and Receive SPI\r\n");

GoMark_axis:
printf(" Input Axis[1:0] >");fflush(stdout);
L6470_Axis = UART1_get_Number();
if(L6470_Axis < 0 || L6470_Axis > 1){
printf("Invalid input value\r\n");
goto GoMark_axis;
}

L6470_TxBuffer[0] = 0x78;
L6470_DRV(L6470_Axis, L6470_TxBuffer, L6470_RxBuffer, 1);

break;
case 15:
printf("15. L6470 ResetPos : Transmit and Receive SPI\r\n");

ResetPos_axis:
printf(" Input Axis[1:0] >");fflush(stdout);
L6470_Axis = UART1_get_Number();
if(L6470_Axis < 0 || L6470_Axis > 1){
printf("Invalid input value\r\n");
goto ResetPos_axis;
}

L6470_TxBuffer[0] = 0xD8;
L6470_DRV(L6470_Axis, L6470_TxBuffer, L6470_RxBuffer, 1);

break;
case 16:
printf("16. L6470 ResetDevice : Transmit and Receive SPI\r\n");

ResetDevice_axis:
printf(" Input Axis[1:0] >");fflush(stdout);
L6470_Axis = UART1_get_Number();
if(L6470_Axis < 0 || L6470_Axis > 1){
printf("Invalid input value\r\n");
goto ResetDevice_axis;
}

L6470_TxBuffer[0] = 0xC0;
L6470_DRV(L6470_Axis, L6470_TxBuffer, L6470_RxBuffer, 1);

break;
case 17:
printf("17. L6470 SoftStop : Transmit and Receive SPI\r\n");

SoftStop_axis:
printf(" Input Axis[1:0] >");fflush(stdout);
L6470_Axis = UART1_get_Number();
if(L6470_Axis < 0 || L6470_Axis > 1){
printf("Invalid input value\r\n");
goto SoftStop_axis;
}

L6470_TxBuffer[0] = 0xB0;
L6470_DRV(L6470_Axis, L6470_TxBuffer, L6470_RxBuffer, 1);

break;
case 18:
printf("18. L6470 HardStop : Transmit and Receive SPI\r\n");

HardStop_axis:
printf(" Input Axis[1:0] >");fflush(stdout);
L6470_Axis = UART1_get_Number();
if(L6470_Axis < 0 || L6470_Axis > 1){
printf("Invalid input value\r\n");
goto HardStop_axis;
}

L6470_TxBuffer[0] = 0xB8;
L6470_DRV(L6470_Axis, L6470_TxBuffer, L6470_RxBuffer, 1);

break;
case 19:
printf("19. L6470 SoftHiZ : Transmit and Receive SPI\r\n");

SoftHiZ_axis:
printf(" Input Axis[1:0] >");fflush(stdout);
L6470_Axis = UART1_get_Number();
if(L6470_Axis < 0 || L6470_Axis > 1){
printf("Invalid input value\r\n");
goto SoftHiZ_axis;
}

L6470_TxBuffer[0] = 0xA0;
L6470_DRV(L6470_Axis, L6470_TxBuffer, L6470_RxBuffer, 1);

break;
case 20:
printf("20. L6470 HardHiZ : Transmit and Receive SPI\r\n");

HardHiZ_axis:
printf(" Input Axis[1:0] >");fflush(stdout);
L6470_Axis = UART1_get_Number();
if(L6470_Axis < 0 || L6470_Axis > 1){
printf("Invalid input value\r\n");
goto HardHiZ_axis;
}

L6470_TxBuffer[0] = 0xA8;
L6470_DRV(L6470_Axis, L6470_TxBuffer, L6470_RxBuffer, 1);

break;
case 21:
printf("21. L6470 GeStatus : Transmit and Receive SPI\r\n");

GeStatus_axis:
printf(" Input Axis[1:0] >");fflush(stdout);
L6470_Axis = UART1_get_Number();
if(L6470_Axis < 0 || L6470_Axis > 1){
printf("Invalid input value\r\n");
goto GeStatus_axis;
}

L6470_TxBuffer[0] = 0xD0;
L6470_TxBuffer[1] = 0;
L6470_TxBuffer[2] = 0;
L6470_TxBuffer[3] = 0;

L6470_RxBuffer[0] = 0;
L6470_RxBuffer[1] = 0;
L6470_RxBuffer[2] = 0;
L6470_RxBuffer[3] = 0;
L6470_DRV(L6470_Axis, L6470_TxBuffer, L6470_RxBuffer, 4);

printf("STATUS Byte2 : %x, Byte1 : %x, Byte0 : %x\r\n", L6470_RxBuffer[1], L6470_RxBuffer[2], L6470_RxBuffer[3]);

break;
case 22:
printf("22. L6470 Search Home\r\n");

if(sensor_flag == (1<<10)){
sensor_flag = 0;
L6470_Run_Dir = 1;
L6470_Run_Spd = 10000;

L6470_TxBuffer[0] = 0x50 | L6470_Run_Dir;
L6470_TxBuffer[1] = L6470_Run_Spd >> 16;
L6470_TxBuffer[2] = L6470_Run_Spd >> 8;
L6470_TxBuffer[3] = L6470_Run_Spd;
L6470_DRV(0, L6470_TxBuffer, L6470_RxBuffer, 4);

while(sensor_flag != (1<<10));
L6470_TxBuffer[0] = 0xB8;
L6470_DRV(0, L6470_TxBuffer, L6470_RxBuffer, 1);
sensor_flag = 0;

HAL_Delay(500);

L6470_Run_Dir = 0;
L6470_Run_Spd = 10000;

L6470_TxBuffer[0] = 0x50 | L6470_Run_Dir;
L6470_TxBuffer[1] = L6470_Run_Spd >> 16;
L6470_TxBuffer[2] = L6470_Run_Spd >> 8;
L6470_TxBuffer[3] = L6470_Run_Spd;
L6470_DRV(0, L6470_TxBuffer, L6470_RxBuffer, 4);

while(sensor_flag != (1<<10));
L6470_TxBuffer[0] = 0xB8;
L6470_DRV(0, L6470_TxBuffer, L6470_RxBuffer, 1);
sensor_flag = 0;
}
else{
L6470_Run_Dir = 0;
L6470_Run_Spd = 10000;

L6470_TxBuffer[0] = 0x50 | L6470_Run_Dir;
L6470_TxBuffer[1] = L6470_Run_Spd >> 16;
L6470_TxBuffer[2] = L6470_Run_Spd >> 8;
L6470_TxBuffer[3] = L6470_Run_Spd;
L6470_DRV(0, L6470_TxBuffer, L6470_RxBuffer, 4);

while(sensor_flag != (1<<10));
L6470_TxBuffer[0] = 0xB8;
L6470_DRV(0, L6470_TxBuffer, L6470_RxBuffer, 1);
sensor_flag = 0;
}

L6470_TxBuffer[0] = 0xB8;
L6470_DRV(0, L6470_TxBuffer, L6470_RxBuffer, 1);

HAL_Delay(500);

L6470_Run_Dir = 1;
L6470_Run_Spd = 2000;

L6470_TxBuffer[0] = 0x50 | L6470_Run_Dir;
L6470_TxBuffer[1] = L6470_Run_Spd >> 16;
L6470_TxBuffer[2] = L6470_Run_Spd >> 8;
L6470_TxBuffer[3] = L6470_Run_Spd;
L6470_DRV(0, L6470_TxBuffer, L6470_RxBuffer, 4);

while(sensor_flag != (1<<10));
L6470_TxBuffer[0] = 0xB8;
L6470_DRV(0, L6470_TxBuffer, L6470_RxBuffer, 1);
sensor_flag = 0;

L6470_TxBuffer[0] = 0xB8;
L6470_DRV(0, L6470_TxBuffer, L6470_RxBuffer, 1);

HAL_Delay(2000);

L6470_Move_Dir = 1;
L6470_Move_N_STEP = 10000;

L6470_TxBuffer[0] = 0x40 | L6470_Move_Dir;
L6470_TxBuffer[1] = L6470_Move_N_STEP >> 16;
L6470_TxBuffer[2] = L6470_Move_N_STEP >> 8;
L6470_TxBuffer[3] = L6470_Move_N_STEP;
L6470_DRV(0, L6470_TxBuffer, L6470_RxBuffer, 4);

while(HAL_GPIO_ReadPin(BS_GPIO_Port, BS_Pin) == 0);

//0(R) Axiz : ResetPos
L6470_TxBuffer[0] = 0xD8;
L6470_DRV(0, L6470_TxBuffer, L6470_RxBuffer, 1);

L6470_TxBuffer[0] = 0x05;
L6470_TxBuffer[1] = 0x00;
L6470_TxBuffer[2] = 0x14;
L6470_TxBuffer[3] = 0x00;
L6470_DRV(0, L6470_TxBuffer, L6470_RxBuffer, 4);
HAL_Delay(10);

L6470_TxBuffer[0] = 0x06;
L6470_TxBuffer[1] = 0x00;
L6470_TxBuffer[2] = 0x14;
L6470_TxBuffer[3] = 0x00;
L6470_DRV(0, L6470_TxBuffer, L6470_RxBuffer, 4);
HAL_Delay(10);

L6470_TxBuffer[0] = 0x07;
L6470_TxBuffer[1] = 0x00;
L6470_TxBuffer[2] = 0x14;
L6470_TxBuffer[3] = 0x00;
L6470_DRV(0, L6470_TxBuffer, L6470_RxBuffer, 4);
HAL_Delay(10);

L6470_TxBuffer[0] = 0x08;
L6470_TxBuffer[1] = 0x00;
L6470_TxBuffer[2] = 0x05;
L6470_TxBuffer[3] = 0x00;
L6470_DRV(0, L6470_TxBuffer, L6470_RxBuffer, 4);
HAL_Delay(10);

printf("HomeSearch end\r\n");

break;

default:
printf("Invalid input value\r\n");
break;
}
printf("JBElec>");
fflush(stdout);

goto PHASE1;
/* USER CODE END WHILE */

/* USER CODE BEGIN 3 */

}
/* USER CODE END 3 */

}

/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{

RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_ClkInitTypeDef RCC_ClkInitStruct;
RCC_PeriphCLKInitTypeDef PeriphClkInit;

/**Initializes the CPU, AHB and APB busses clocks
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = 16;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL12;
RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV1;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}

/**Initializes the CPU, AHB and APB busses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;

if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}

PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1;
PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK1;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}

/**Configure the Systick interrupt time
*/
HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

/**Configure the Systick
*/
HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

/* SysTick_IRQn interrupt configuration */
HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
}

/* SPI1 init function */
static void MX_SPI1_Init(void)
{

/* SPI1 parameter configuration*/
hspi1.Instance = SPI1;
hspi1.Init.Mode = SPI_MODE_MASTER;
hspi1.Init.Direction = SPI_DIRECTION_2LINES;
hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
hspi1.Init.CLKPolarity = SPI_POLARITY_HIGH;
hspi1.Init.CLKPhase = SPI_PHASE_2EDGE;
hspi1.Init.NSS = SPI_NSS_SOFT;
hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;
hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
hspi1.Init.CRCPolynomial = 7;
hspi1.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
hspi1.Init.NSSPMode = SPI_NSS_PULSE_DISABLE;
if (HAL_SPI_Init(&hspi1) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}

}

/* TIM3 init function */
static void MX_TIM3_Init(void)
{

TIM_MasterConfigTypeDef sMasterConfig;
TIM_OC_InitTypeDef sConfigOC;

htim3.Instance = TIM3;
htim3.Init.Prescaler = 0;
htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
htim3.Init.Period = 0;
htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_PWM_Init(&htim3) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}

sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}

sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = 0;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_3) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}

HAL_TIM_MspPostInit(&htim3);

}

/* USART1 init function */
static void MX_USART1_UART_Init(void)
{

huart1.Instance = USART1;
huart1.Init.BaudRate = 115200;
huart1.Init.WordLength = UART_WORDLENGTH_8B;
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_NONE;
huart1.Init.Mode = UART_MODE_TX_RX;
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_UART_Init(&huart1) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}

}

/** Configure pins as
* Analog
* Input
* Output
* EVENT_OUT
* EXTI
*/
static void MX_GPIO_Init(void)
{

GPIO_InitTypeDef GPIO_InitStruct;

/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOF_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();

/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(DIR_485_GPIO_Port, DIR_485_Pin, GPIO_PIN_RESET);

/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOA, STATUS_2_Pin|STATUS_3_Pin|SCS_Pin, GPIO_PIN_SET);

/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOB, SR_Pin|SW_Pin|STATUS_0_Pin|STATUS_1_Pin, GPIO_PIN_SET);

/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOB, GPO_1_Pin|GPO_0_Pin, GPIO_PIN_RESET);

/*Configure GPIO pin : DIR_485_Pin */
GPIO_InitStruct.Pin = DIR_485_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(DIR_485_GPIO_Port, &GPIO_InitStruct);

/*Configure GPIO pins : STATUS_2_Pin STATUS_3_Pin SCS_Pin */
GPIO_InitStruct.Pin = STATUS_2_Pin|STATUS_3_Pin|SCS_Pin;
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);

/*Configure GPIO pins : FLAG_Pin BS_Pin */
GPIO_InitStruct.Pin = FLAG_Pin|BS_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

/*Configure GPIO pins : SR_Pin SW_Pin GPO_1_Pin GPO_0_Pin
STATUS_0_Pin STATUS_1_Pin */
GPIO_InitStruct.Pin = SR_Pin|SW_Pin|GPO_1_Pin|GPO_0_Pin
|STATUS_0_Pin|STATUS_1_Pin;
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);

/*Configure GPIO pins : SENSOR_3_Pin SENSOR_2_Pin SENSOR_1_Pin SENSOR_0_Pin */
GPIO_InitStruct.Pin = SENSOR_3_Pin|SENSOR_2_Pin|SENSOR_1_Pin|SENSOR_0_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING_FALLING;
GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

/* EXTI interrupt init*/
HAL_NVIC_SetPriority(EXTI4_15_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(EXTI4_15_IRQn);

}

/* USER CODE BEGIN 4 */
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
switch(GPIO_Pin)
{
case GPIO_PIN_8: //SENSOR_3
sensor_flag = 1 << 8;
break;
case GPIO_PIN_9: //SENSOR_2
sensor_flag = 1 << 9;
break;
case GPIO_PIN_10: //SENSOR_1
sensor_flag = 1 << 10;
break;
case GPIO_PIN_13: //SENSOR_0
sensor_flag = 1 << 13;
break;
}
}
/* USER CODE END 4 */

/**
* @brief This function is executed in case of error occurrence.
* @param file: The file name as string.
* @param line: The line in file as a number.
* @retval None
*/
void _Error_Handler(char *file, int line)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
while(1)
{
}
/* USER CODE END Error_Handler_Debug */
}

#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

/**
* @}
*/

/**
* @}
*/

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

댓글 없음:

댓글 쓰기