Air_58Printer/main/module/Printer_bsp.c
2024-08-17 23:09:42 +08:00

344 lines
9.2 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "include/Sys_Printer.h"
#include "Printer_Config.h"
#include "driver/spi_master.h"
#include <stdint.h>
static const char *TAG = "Printer_bsp.c";
gpio_state_t LED0 = {
.state = 1,
.pin_num = 4,
.config = {
.mode = GPIO_MODE_OUTPUT,
.pin_bit_mask = 1ULL << 4,
.pull_up_en = 1
}
};
gpio_state_t LED1 = {
.state = 1,
.pin_num = 8,
.config = {
.mode = GPIO_MODE_OUTPUT,
.pin_bit_mask = 1ULL << 8,
.pull_up_en = 1
}
};
gpio_state_t STB = {
.state = 0,
.pin_num = STB_PIN,
.config = {
.mode = GPIO_MODE_OUTPUT,
.pin_bit_mask = 1ULL << STB_PIN,
.pull_up_en = 0
}
};
gpio_state_t VH_EN = {
.state = 0,
.pin_num = VH_PIN,
.config = {
.mode = GPIO_MODE_OUTPUT,
.pin_bit_mask = 1ULL << VH_PIN,
.pull_up_en = 0
}
};
gpio_state_t MOTOR_CHA = {
.state = 0,
.pin_num = INA_PIN,
.config = {
.mode = GPIO_MODE_OUTPUT,
.pin_bit_mask = 1ULL << INA_PIN,
.pull_up_en = 1
}
};
gpio_state_t MOTOR_CHB = {
.state = 0,
.pin_num = INB_PIN,
.config = {
.mode = GPIO_MODE_OUTPUT,
.pin_bit_mask = 1ULL << INB_PIN,
.pull_up_en = 1
}
};
gpio_state_t MOTOR_CHC = {
.state = 0,
.pin_num = INC_PIN,
.config = {
.mode = GPIO_MODE_OUTPUT,
.pin_bit_mask = 1ULL << INC_PIN,
.pull_up_en = 1
}
};
gpio_state_t MOTOR_CHD = {
.state = 0,
.pin_num = IND_PIN,
.config = {
.mode = GPIO_MODE_OUTPUT,
.pin_bit_mask = 1ULL << IND_PIN,
.pull_up_en = 1
}
};
gpio_state_t FEED_KEY = {
.state = 0,
.pin_num = KEY_FEED_PIN,
.config = {
.mode = GPIO_MODE_INPUT,
.pin_bit_mask = 1ULL << KEY_FEED_PIN,
.pull_up_en = 1
}
};
gpio_state_t LAT = {
.state = 0,
.pin_num = LAT_PIN,
.config = {
.mode = GPIO_MODE_OUTPUT,
.pin_bit_mask = 1ULL << LAT_PIN,
.pull_up_en = 1
}
};
void BSP_GPIO_Init(void)
{
ESP_ERROR_CHECK(gpio_config(&LED0.config));
ESP_ERROR_CHECK(gpio_config(&LED1.config));
ESP_ERROR_CHECK(gpio_config(&VH_EN.config));
ESP_ERROR_CHECK(gpio_config(&MOTOR_CHA.config));
ESP_ERROR_CHECK(gpio_config(&MOTOR_CHB.config));
ESP_ERROR_CHECK(gpio_config(&MOTOR_CHC.config));
ESP_ERROR_CHECK(gpio_config(&MOTOR_CHD.config));
ESP_ERROR_CHECK(gpio_config(&FEED_KEY.config));
ESP_ERROR_CHECK(gpio_config(&LAT.config));
ESP_ERROR_CHECK(gpio_config(&STB.config));
VH_EN.state = 1;
gpio_set_level(VH_EN.pin_num,VH_EN.state);
}
//步进电机驱动部分
uint8_t motorTable[8][4] =
{
{1, 0, 0, 0},
{1, 0, 1, 0},
{0, 0, 1, 0},
{0, 1, 1, 0},
{0, 1, 0, 0},
{0, 1, 0, 1},
{0, 0, 0, 1},
{1, 0, 0, 1}
};
uint8_t motorPos = 0;
uint8_t Motor_GO(uint8_t DIR)
{
if(DIR == 1)
{
++motorPos;
if (motorPos == 8)
{
motorPos = 0;
}
}else if(DIR == 0){
if(motorPos == 0)
{
motorPos = 7;
}else{
--motorPos;
}
if (motorPos == 0)
{
motorPos = 7;
}
}else{
gpio_set_level(MOTOR_CHA.pin_num,0);
gpio_set_level(MOTOR_CHB.pin_num,0);
gpio_set_level(MOTOR_CHC.pin_num,0);
gpio_set_level(MOTOR_CHD.pin_num,0);
return 0xff;
}
gpio_set_level(MOTOR_CHA.pin_num,motorTable[motorPos][0]);
gpio_set_level(MOTOR_CHB.pin_num,motorTable[motorPos][1]);
gpio_set_level(MOTOR_CHC.pin_num,motorTable[motorPos][2]);
gpio_set_level(MOTOR_CHD.pin_num,motorTable[motorPos][3]);
return motorPos;
}
spi_transaction_t spi_printer_dmadata;
spi_device_handle_t Printer_spi;
//uint8_t cars = 0;
void Printer_spidata_isok(spi_transaction_t *trans)//数据发送完成回调函数
{
xEventGroupSetBits(Printer_group,EVENT_SPI_SEND_Done);
}
esp_err_t bsp_spi_printer_init(spi_device_handle_t* handle)
{
//00 定义错误标志
esp_err_t e;
//01 配置总线初始化结构体
static spi_bus_config_t bus_cfg; //总线配置结构体
bus_cfg.miso_io_num = -1; //miso
bus_cfg.mosi_io_num = GPIO_MOSI; //mosi
bus_cfg.sclk_io_num = GPIO_SCLK; //sclk
bus_cfg.quadhd_io_num = -1; // HD
bus_cfg.quadwp_io_num = -1; // WP
bus_cfg.max_transfer_sz = TH_WIDTH; //非DMA最大64bytes,DMA最大4092bytes
//bus_cfg.intr_flags = 0; //这个用于设置中断优先级的,0是默认
bus_cfg.flags = SPICOMMON_BUSFLAG_MASTER;
//这个用于设置初始化的时候要检测哪些选项。比如这里设置的是spi初始化为主机模式是否成功。检测结果通过spi_bus_initialize函数的
//返回值进行返回。如果初始化为主机模式成功,就会返回esp_ok
//02 初始化总线配置结构体
e = spi_bus_initialize(1, &bus_cfg, SPI_DMA_CH_AUTO);
if (e != ESP_OK)
{
printf("bus initialize failed!\n");
return e;
}
//03 配置设备结构体
static spi_device_interface_config_t interface_cfg; //设备配置结构体
interface_cfg.address_bits = 0; //配置地址位长度
interface_cfg.command_bits = 0; //配置命令位长度
//与address_bits是一样的
interface_cfg.dummy_bits = 0; //配置dummy长度
interface_cfg.clock_speed_hz = 5 * 1000 * 1000; //配置时钟频率
interface_cfg.flags = SPI_DEVICE_NO_DUMMY,
interface_cfg.mode = 0; //设置SPI通讯的相位特性和采样边沿。包括了mode0-3四种。要看从设备能够使用哪种模式
interface_cfg.duty_cycle_pos = 0; //配置占空比
//设置时钟的占空比,比例是 pos*1/256,默认为0,也就是50%占空比
interface_cfg.queue_size = 6; //传输队列的长度,表示可以在通讯的时候挂起多少个spi通讯。在中断通讯模式的时候会把当前spi通讯进程挂起到队列中
interface_cfg.post_cb = &Printer_spidata_isok;
//04 设备初始化
e = spi_bus_add_device(1, &interface_cfg, handle);
if (e != ESP_OK)
{
printf("device config error\n");
return e;
}
memset(&spi_printer_dmadata,0,sizeof(spi_printer_dmadata));//初始化结构体
spi_printer_dmadata.length = TH_WIDTH*8;
return ESP_OK;
}
void spi_printer_data_send(spi_device_handle_t handle, uint8_t* pBuffer)
{
spi_printer_dmadata.tx_buffer = pBuffer;
spi_device_polling_transmit(handle, &spi_printer_dmadata);
}
#define TIMER_DIVIDER (16) // Hardware timer clock divider
SemaphoreHandle_t xTimerSemaphore;
gptimer_handle_t gptimer_SysTick = NULL;
gptimer_handle_t gptimer_Heat = NULL;
SemaphoreHandle_t SysTick_queue;
gptimer_config_t timer_config = {
.clk_src = GPTIMER_CLK_SRC_DEFAULT,
.direction = GPTIMER_COUNT_UP,
.resolution_hz = 1000000, // 1MHz, 1 tick=1us
};
extern uint8_t Motor_GO(uint8_t DIR);
static bool IRAM_ATTR SysTick_callback(gptimer_handle_t timer, const gptimer_alarm_event_data_t *edata, void *user_data)
{
static portBASE_TYPE xHigherPriorityTaskWoken;
xSemaphoreGiveFromISR(SysTick_queue,xHigherPriorityTaskWoken);
return true;
}
static bool IRAM_ATTR Heat_callback(gptimer_handle_t timer, const gptimer_alarm_event_data_t *edata, void *user_data)
{
static uint32_t heat_count = 0;
static portBASE_TYPE xHigherPriorityTaskWoken;
if(heat_count!=0)
{
xEventGroupSetBitsFromISR(Printer_group,EVENT_Heat_Done,xHigherPriorityTaskWoken);
}else{
heat_count = 1;
}
return true;
}
void GPtimer_init(void)
{
SysTick_queue = xSemaphoreCreateBinary();
xTimerSemaphore = xSemaphoreCreateMutex();
ESP_ERROR_CHECK(gptimer_new_timer(&timer_config, &gptimer_SysTick));
gptimer_event_callbacks_t sys_Ticking_cbs = {
.on_alarm = SysTick_callback,
};
ESP_ERROR_CHECK(gptimer_register_event_callbacks(gptimer_SysTick, &sys_Ticking_cbs, NULL));
ESP_LOGI(TAG, "Enable timer");
ESP_ERROR_CHECK(gptimer_enable(gptimer_SysTick));
gptimer_alarm_config_t alarm_config_SysTick = {
.alarm_count = 1000, // period = 1ms
.flags.auto_reload_on_alarm = true,
};
ESP_ERROR_CHECK(gptimer_set_alarm_action(gptimer_SysTick, &alarm_config_SysTick));
ESP_ERROR_CHECK(gptimer_start(gptimer_SysTick));
ESP_ERROR_CHECK(gptimer_new_timer(&timer_config, &gptimer_Heat));
gptimer_event_callbacks_t gptimer_Heat_cbs = {
.on_alarm = Heat_callback,
};
ESP_ERROR_CHECK(gptimer_register_event_callbacks(gptimer_Heat, &gptimer_Heat_cbs, NULL));
ESP_LOGI(TAG, "Enable timer");
ESP_ERROR_CHECK(gptimer_enable(gptimer_Heat));
gptimer_alarm_config_t alarm_config_Heat = {
.alarm_count = 1000*1000, // period = 1ms
.flags.auto_reload_on_alarm = false,
};
ESP_ERROR_CHECK(gptimer_set_alarm_action(gptimer_Heat, &alarm_config_Heat));
ESP_ERROR_CHECK(gptimer_start(gptimer_Heat));
}
uint16_t Set_heat_timer_value(uint32_t time)
{
uint64_t old_time = 0;
gptimer_get_raw_count(gptimer_Heat,&old_time);
gptimer_alarm_config_t alarm_config_Heat = {
.alarm_count = old_time + time, // period = 1ms
.flags.auto_reload_on_alarm = false,
};
ESP_ERROR_CHECK(gptimer_set_alarm_action(gptimer_Heat, &alarm_config_Heat));
return alarm_config_Heat.alarm_count;
}
void Printer_Bsp_Init(void)
{
BSP_GPIO_Init();
GPtimer_init();
bsp_spi_printer_init(&Printer_spi);
}