Browse Source

can run success

master
zhaijunyuan 5 years ago
parent
commit
48bd651c10
  1. 4
      Makefile
  2. 2
      lib/lib.mk
  3. 195
      targets/xc_ppf1901/can.c
  4. 8
      targets/xc_ppf1901/can.h
  5. 4
      targets/xc_ppf1901/lmk.c
  6. 44
      targets/xc_ppf1901/main.c
  7. 28
      targets/xc_ppf1901/max6884.c
  8. 1
      targets/xc_ppf1901/target.mk

4
Makefile

@ -50,8 +50,8 @@ COMM_CFLAGS += -mlittle-endian -mthumb -fno-common -Wall -ffunction-sections -fd
include $(lib_dir)/lib.mk
target ?= vocoder
#target ?= xc_ppf1901
#target ?= vocoder
target ?= xc_ppf1901
target_dir := $(top_dir)/targets/$(target)

2
lib/lib.mk

@ -30,7 +30,7 @@ lib_srcs += \
$(lib_dir)/serial_ymodem.c \
$(lib_dir)/stdio_ymodem.c \
$(lib_dir)/sdio_sd.c \
$(lib_dir)/printf.c \
# $(lib_dir)/printf.c \
lib_objs = $(lib_srcs:.c=.o)

195
targets/xc_ppf1901/can.c

@ -0,0 +1,195 @@
#include <stdio.h>
#include "stm32f4xx.h"
/* query APB1 clocks */
static uint32_t get_PCLK1_clocks()
{
RCC_ClocksTypeDef clks;
RCC_GetClocksFreq(&clks);
return clks.PCLK1_Frequency;
}
int can_set_baudrate(CAN_InitTypeDef *conf, unsigned int baudrate)
{
int found = 0;
int calcBaudrate;
uint8_t sjw = 1, bs1 = 1, bs2 = 1;
uint16_t prescaler = 1;
uint32_t frequency;
frequency = get_PCLK1_clocks();
for (; sjw <= 4 && !found; ) {
for (; bs1 <= 16 && !found; ) {
for (; bs2 <= 8 && !found; ) {
for (; prescaler <= 1024 && !found; ) {
calcBaudrate = (int)(frequency / (prescaler * (sjw + bs1 + bs2)));
if (calcBaudrate == baudrate) {
if (sjw == 1)
conf->CAN_SJW = CAN_SJW_1tq;
else if (sjw == 2)
conf->CAN_SJW = CAN_SJW_2tq;
else if (sjw == 3)
conf->CAN_SJW = CAN_SJW_3tq;
else if (sjw == 4)
conf->CAN_SJW = CAN_SJW_4tq;
if (bs1 == 1)
conf->CAN_BS1 = CAN_BS1_1tq;
else if (bs1 == 2)
conf->CAN_BS1 = CAN_BS1_2tq;
else if (bs1 == 3)
conf->CAN_BS1 = CAN_BS1_3tq;
else if (bs1 == 4)
conf->CAN_BS1 = CAN_BS1_4tq;
else if (bs1 == 5)
conf->CAN_BS1 = CAN_BS1_5tq;
else if (bs1 == 6)
conf->CAN_BS1 = CAN_BS1_6tq;
else if (bs1 == 7)
conf->CAN_BS1 = CAN_BS1_7tq;
else if (bs1 == 8)
conf->CAN_BS1 = CAN_BS1_8tq;
else if (bs1 == 9)
conf->CAN_BS1 = CAN_BS1_9tq;
else if (bs1 == 10)
conf->CAN_BS1 = CAN_BS1_10tq;
else if (bs1 == 11)
conf->CAN_BS1 = CAN_BS1_11tq;
else if (bs1 == 12)
conf->CAN_BS1 = CAN_BS1_12tq;
else if (bs1 == 13)
conf->CAN_BS1 = CAN_BS1_13tq;
else if (bs1 == 14)
conf->CAN_BS1 = CAN_BS1_14tq;
else if (bs1 == 15)
conf->CAN_BS1 = CAN_BS1_15tq;
else if (bs1 == 16)
conf->CAN_BS1 = CAN_BS1_16tq;
if (bs2 == 1)
conf->CAN_BS2 = CAN_BS2_1tq;
else if (bs2 == 2)
conf->CAN_BS2 = CAN_BS2_2tq;
else if (bs2 == 3)
conf->CAN_BS2 = CAN_BS2_2tq;
else if (bs2 == 4)
conf->CAN_BS2 = CAN_BS2_2tq;
conf->CAN_Prescaler = prescaler;
found = 1;
}
prescaler++;
}
if (!found) {
prescaler = 1;
bs2++;
}
}
if (!found) {
bs2 = 1;
bs1++;
}
}
if (!found) {
bs1 = 1;
sjw++;
}
}
return found;
}
void can_setup()
{
GPIO_InitTypeDef GPIO_InitStructure;
CAN_InitTypeDef CAN_InitStructure;
CAN_FilterInitTypeDef CAN_FilterInitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource11,GPIO_AF_CAN1);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource12,GPIO_AF_CAN1);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// CAN_DeInit(CAN1);
// CAN_StructInit(&CAN_InitStructure);
CAN_InitStructure.CAN_TTCM = DISABLE;
CAN_InitStructure.CAN_ABOM = DISABLE;
CAN_InitStructure.CAN_AWUM = DISABLE;
CAN_InitStructure.CAN_NART = ENABLE;
CAN_InitStructure.CAN_RFLM = DISABLE;
CAN_InitStructure.CAN_TXFP = DISABLE;
CAN_InitStructure.CAN_Mode = CAN_Mode_Normal;
CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;
CAN_InitStructure.CAN_BS1 = CAN_BS1_3tq;
CAN_InitStructure.CAN_BS2 = CAN_BS2_1tq;
CAN_InitStructure.CAN_Prescaler = 9;
printf("IN BS1=%d, BS2=%d, prescaler=%d\r\n", CAN_InitStructure.CAN_BS1, CAN_InitStructure.CAN_BS2, CAN_InitStructure.CAN_Prescaler);
// can_set_baudrate(&CAN_InitStructure, 200000);
printf("OU BS1=%d, BS2=%d, prescaler=%d\r\n", CAN_InitStructure.CAN_BS1, CAN_InitStructure.CAN_BS2, CAN_InitStructure.CAN_Prescaler);
CAN_Init(CAN1, &CAN_InitStructure);
CAN_FilterInitStructure.CAN_FilterNumber = 0;
CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask;
CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit;
CAN_FilterInitStructure.CAN_FilterIdHigh = 0x0009;
CAN_FilterInitStructure.CAN_FilterIdLow = 0x0000;
CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0x0001;
CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0x0001;
CAN_FilterInitStructure.CAN_FilterFIFOAssignment = CAN_Filter_FIFO0;
CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;
CAN_FilterInit(&CAN_FilterInitStructure);
}
int can_test()
{
uint8_t mbox, buf;
int i;
CanTxMsg msg;
CanRxMsg msg1;
msg.StdId = 0x11;
msg.IDE = CAN_ID_STD;
msg.RTR = CAN_RTR_Data;
msg.DLC = 0x01;
msg.Data[0] = 0x55;
mbox = CAN_Transmit(CAN1, &msg);
i = 0;
printf("mbox = %d\r\n",mbox);
while((CAN_TransmitStatus(CAN1, mbox) == CAN_TxStatus_Failed) && (i < 0xFFF))
{
i++;
// printf("delay\r\n");
}
if(i > 0xFFF)
{
printf("err 1\r\n");
return mbox;
}
i = 0;
while ((CAN_MessagePending(CAN1,CAN_FIFO0) == 0) && (i < 0xfff))
{
++i;
printf("err 2\r\n");
return 0;
}
CAN_Receive(CAN1,CAN_FIFO0,&msg1);
buf = msg1.Data[0];
printf("receive data = %x\n", buf);
return 0;
}

8
targets/xc_ppf1901/can.h

@ -0,0 +1,8 @@
#ifndef CAN_H
#define CAN_H
void can_setup();
int can_test();
#endif

4
targets/xc_ppf1901/lmk.c

@ -54,14 +54,14 @@ static reg_cfg_t lmk1regs[] = {
{0x0125, 0x00},
{0x0126, 0x00},
{0x0127, 0x10},
{0x0128, 0x0F},
{0x0128, 0x1E},
{0x0129, 0x55},
{0x012A, 0x55},
{0x012B, 0x00},
{0x012C, 0x22},
{0x012D, 0x00},
{0x012E, 0x40},
{0x012F, 0x00},
{0x012F, 0x11},
{0x0130, 0x1E},
{0x0131, 0x55},
{0x0132, 0x55},

44
targets/xc_ppf1901/main.c

@ -13,33 +13,18 @@
#include "tick.h"
#include "max31730.h"
#include "max6884.h"
#include "can.h"
i2c_t _i2c;
int send()
{
union {
uint8_t d8[4];
}tx_data;
uint8_t tx_len;
tx_data.d8[0] = 0x55;
tx_data.d8[1] = 0xaa;
tx_data.d8[2] = 0x20;
tx_len = 3;
i2s_xfer(0, tx_data.d8, tx_len, NULL,0);
return 0;
}
int main()
{
signed char data_6884[8];
signed char data_6884[16];
// float data[8];
// signed char ID;
uint64_t prev = 0, now;
gpio_init(PH4, GPIO_OUTPUT);
gpio_init(PB0, GPIO_OUTPUT);
@ -75,11 +60,10 @@ int main()
dwt_wait_ms(100);
gpio_set(PB0);
dwt_wait_ms(100);
gpio_set(PB0);
i2s_init(&i2s_spi_cfg);
dwt_wait_ms(1000);
dwt_wait_ms(50);
if(lmk_init(1) == 0)
printf("cs 1 init success!!!\r\n");
@ -87,14 +71,19 @@ int main()
// max31730_init(0x4f);
// max31730_init(0x1c);
/*max6884***********************************/
gpio_clear(PH4);
dwt_wait_ms(100);
gpio_set(PH4);
max6884_init();
/******************************************/
// can_setup();
while (1) {
// send();
now = millis();
if ((now - prev) > 100) {
if ((now - prev) > 1000) {
#if 0
//data[0] = max31730_read_local(0x4f);
//i2c730_read(0x4f, 0x50, &ID, 1);
@ -108,8 +97,17 @@ int main()
printf("max31730_temperature%d = %f\r\n",i,data[i]);
}
#else
data_6884[0] = max6884_read(0x24);
printf("max6884 = %x\r\n",data_6884[0]);
/* MAX6884 ***********************************************/
for(int i=0; i<12; i++)
{
data_6884[i] = max6884_read(0x18+i);
// data_16 = max6884_read(0x96);
printf("max6884 register %d = %x\r\n",i,data_6884[i]);
// printf("max6884 register 0x16 = %x\r\n",data_16);
}
/***************************************************************/
// can_test();
#endif
prev = now;
}

28
targets/xc_ppf1901/max6884.c

@ -1,17 +1,31 @@
#include <stdio.h>
#include "_i2c.h"
#include "max6884.h"
#include "dwt.h"
#include "gpio.h"
#include "cmd.h"
#define ADDR 0x52
void max6884_init()
{
i2c6884_write(ADDR,0x16,0x20, 1);
i2c6884_write(ADDR,0x11,0x00, 1);
uint8_t reset = 0xc4;
gpio_init(PH3, GPIO_INPUT); //WDOn
gpio_init(PH6, GPIO_OUTPUT); //WDIn
i2c6884_write(ADDR,0x96,0x00, 1);
/* i2c6884_write(ADDR,0x11,0x00, 1);
i2c6884_write(ADDR,0x12,0x00, 1);
i2c6884_write(ADDR,0x13,0x00, 1);
i2c6884_write(ADDR,0x14,0x00, 1);
i2c6884_write(ADDR,0x15,0x00, 1);
*/
i2c6884_write(ADDR,0x8f,0x1f, 1);
i2c_write(ADDR, &reset, 1);
dwt_wait_ms(100);
// i2c6884_write(ADDR,0x15,0xED, 1);
}
signed char max6884_read(uint8_t reg)
@ -20,3 +34,13 @@ signed char max6884_read(uint8_t reg)
i2c6884_read(ADDR, reg, &buf, 1);
return buf;
}
static int do_wdi(cmd_tbl_t s, int argc, char *argv[])
{
gpio_toggle(PH6);
printf("wdi input !!!\r\n");
return 0;
}
CON_CMD(wdi, "write to eeprom", NULL, do_wdi);

1
targets/xc_ppf1901/target.mk

@ -20,6 +20,7 @@ vocoder_srcs := \
$(target_dir)/lmk.c \
$(target_dir)/max31730.c \
$(target_dir)/max6884.c \
$(target_dir)/can.c \
$(target_dir)/main.c
iap_srcs := \

Loading…
Cancel
Save