过往分享
【国民技术N32项目移植】1. 新建工程+LED与按键跳坑总结
前言
本文讲解使用N32G457的硬件IIC驱动OLED显示。
本文基于RT-Thread实现,OLED的驱动均为手写,没有使用RT-Thread OS提供的OLED库(如SSD1306,因为使用硬件IIC不能与库兼容起来,如果需要兼容则需要修改源码,后期实现)。
硬件连接
通过查看数据手册知道I2C引脚的连接,本文实现使用了PB8(SCL)与PB9(SDA),为硬件I2C1。
如下图:
OLED与N32G45x的链接
OLED |
N32G45x |
VCC |
3.3V |
GND |
GND |
SCL |
PB8 |
SDA |
PB9 |
软件实现
知道了硬件连接,接下来就开始进行软件编写了,参考了官方提供的I2C的驱动,在此基础上做了更新,修改了Slave地址以及驱动速度等操作,对于引脚的修改可以根据实际情况进行更新。
如下为I2C的驱动代码:
app_hw_i2c.c
#include <board.h>
#include <rtthread.h>
#include "app_hw_i2c.h"
#define I2CT_FLAG_TIMEOUT ((uint32_t)0x1000)
#define I2CT_LONG_TIMEOUT ((uint32_t)(10 * I2CT_FLAG_TIMEOUT))
#define I2C_MASTER_ADDR (0x30U)
#define I2C1_REMAP
#define I2Cx I2C1
#define I2Cx_SCL_PIN GPIO_PIN_8
#define I2Cx_SDA_PIN GPIO_PIN_9
#define GPIOx GPIOB
#ifdef NON_REENTRANT
static uint32_t Mutex_Flag = 0;
#endif
static __IO uint32_t I2CTimeout;
static CommCtrl_t Comm_Flag = C_READY;
void CommTimeOut_CallBack(ErrCode_t errcode);
void i2c_config(void)
{
I2C_InitType i2c1_master;
GPIO_InitType i2c1_gpio;
RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_I2C1, ENABLE);
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOB, ENABLE);
#ifdef I2C1_REMAP
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_AFIO, ENABLE);
GPIO_ConfigPinRemap(GPIO_RMP_I2C1, ENABLE);
#endif
i2c1_gpio.Pin = I2Cx_SCL_PIN | I2Cx_SDA_PIN;
i2c1_gpio.GPIO_Speed = GPIO_Speed_2MHz;
i2c1_gpio.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_InitPeripheral(GPIOx, &i2c1_gpio);
I2C_DeInit(I2C1);
i2c1_master.BusMode = I2C_BUSMODE_I2C;
i2c1_master.FmDutyCycle = I2C_FMDUTYCYCLE_2;
i2c1_master.OwnAddr1 = I2C_MASTER_ADDR;
i2c1_master.AckEnable = I2C_ACKEN;
i2c1_master.AddrMode = I2C_ADDR_MODE_7BIT;
i2c1_master.ClkSpeed = 400000;
I2C_Init(I2C1, &i2c1_master);
I2C_Enable(I2C1, ENABLE);
}
int i2c_master_send(uint8_t slave_addr, uint8_t* data, uint8_t len)
{
uint8_t* sendBufferPtr = data;
#ifdef NON_REENTRANT
if (Mutex_Flag)
return -1;
else
Mutex_Flag = 1;
#endif
I2CTimeout = I2CT_LONG_TIMEOUT;
while (I2C_GetFlag(I2C1, I2C_FLAG_BUSY))
{
if ((I2CTimeout--) == 0)
{
CommTimeOut_CallBack(MASTER_BUSY);
}
}
if (Comm_Flag == C_READY)
{
Comm_Flag = C_START_BIT;
I2C_GenerateStart(I2C1, ENABLE);
}
I2CTimeout = I2CT_LONG_TIMEOUT;
while (!I2C_CheckEvent(I2C1, I2C_EVT_MASTER_MODE_FLAG))
{
if ((I2CTimeout--) == 0)
{
CommTimeOut_CallBack(MASTER_MODE);
}
}
I2C_SendAddr7bit(I2C1, slave_addr, I2C_DIRECTION_SEND);
I2CTimeout = I2CT_LONG_TIMEOUT;
while (!I2C_CheckEvent(I2C1, I2C_EVT_MASTER_TXMODE_FLAG))
{
if ((I2CTimeout--) == 0)
{
CommTimeOut_CallBack(MASTER_TXMODE);
}
}
Comm_Flag = C_READY;
while (len-- > 0)
{
I2C_SendData(I2C1, *sendBufferPtr++);
I2CTimeout = I2CT_LONG_TIMEOUT;
while (!I2C_CheckEvent(I2C1, I2C_EVT_MASTER_DATA_SENDING))
{
if ((I2CTimeout--) == 0)
{
CommTimeOut_CallBack(MASTER_SENDING);
}
}
}
I2CTimeout = I2CT_LONG_TIMEOUT;
while (!I2C_CheckEvent(I2C1, I2C_EVT_MASTER_DATA_SENDED))
{
if ((I2CTimeout--) == 0)
{
CommTimeOut_CallBack(MASTER_SENDED);
}
}
if (Comm_Flag == C_READY)
{
Comm_Flag = C_STOP_BIT;
I2C_GenerateStop(I2C1, ENABLE);
}
while (I2C_GetFlag(I2C1, I2C_FLAG_BUSY))
{
if ((I2CTimeout--) == 0)
{
CommTimeOut_CallBack(MASTER_BUSY);
}
}
Comm_Flag = C_READY;
#ifdef NON_REENTRANT
if (Mutex_Flag)
Mutex_Flag = 0;
else
return -2;
#endif
return 0;
}
int i2c_master_recv(uint8_t slave_addr, uint8_t* data, uint8_t len)
{
uint8_t* recvBufferPtr = data;
#ifdef NON_REENTRANT
if (Mutex_Flag)
return -1;
else
Mutex_Flag = 1;
#endif
I2CTimeout = I2CT_LONG_TIMEOUT;
while (I2C_GetFlag(I2C1, I2C_FLAG_BUSY))
{
if ((I2CTimeout--) == 0)
{
CommTimeOut_CallBack(MASTER_BUSY);
}
}
I2C_ConfigAck(I2C1, ENABLE);
if (Comm_Flag == C_READY)
{
Comm_Flag = C_START_BIT;
I2C_GenerateStart(I2C1, ENABLE);
}
I2CTimeout = I2CT_LONG_TIMEOUT;
while (!I2C_CheckEvent(I2C1, I2C_EVT_MASTER_MODE_FLAG))
{
if ((I2CTimeout--) == 0)
{
CommTimeOut_CallBack(MASTER_MODE);
}
}
I2C_SendAddr7bit(I2C1, slave_addr, I2C_DIRECTION_RECV);
I2CTimeout = I2CT_LONG_TIMEOUT;
while (!I2C_CheckEvent(I2C1, I2C_EVT_MASTER_RXMODE_FLAG))
{
if ((I2CTimeout--) == 0)
{
CommTimeOut_CallBack(MASTER_RXMODE);
}
}
Comm_Flag = C_READY;
if (len == 1)
{
I2C_ConfigAck(I2C1, DISABLE);
(void)(I2C1->STS1);
(void)(I2C1->STS2);
if (Comm_Flag == C_READY)
{
Comm_Flag = C_STOP_BIT;
I2C_GenerateStop(I2C1, ENABLE);
}
I2CTimeout = I2CT_LONG_TIMEOUT;
while (!I2C_GetFlag(I2C1, I2C_FLAG_RXDATNE))
{
if ((I2CTimeout--) == 0)
{
CommTimeOut_CallBack(MASTER_RECVD);
}
}
*recvBufferPtr++ = I2C_RecvData(I2C1);
len--;
}
else if (len == 2)
{
I2C1->CTRL1 |= 0x0800;
(void)(I2C1->STS1);
(void)(I2C1->STS2);
I2C_ConfigAck(I2C1, DISABLE);
I2CTimeout = I2CT_LONG_TIMEOUT;
while (!I2C_GetFlag(I2C1, I2C_FLAG_BYTEF))
{
if ((I2CTimeout--) == 0)
{
CommTimeOut_CallBack(MASTER_BYTEF);
}
}
if (Comm_Flag == C_READY)
{
Comm_Flag = C_STOP_BIT;
I2C_GenerateStop(I2C1, ENABLE);
}
*recvBufferPtr++ = I2C_RecvData(I2C1);
len--;
*recvBufferPtr++ = I2C_RecvData(I2C1);
len--;
}
else
{
I2C_ConfigAck(I2C1, ENABLE);
(void)(I2C1->STS1);
(void)(I2C1->STS2);
while (len)
{
if (len == 3)
{
I2CTimeout = I2CT_LONG_TIMEOUT;
while (!I2C_GetFlag(I2C1, I2C_FLAG_BYTEF))
{
if ((I2CTimeout--) == 0)
{
CommTimeOut_CallBack(MASTER_BYTEF);
}
}
I2C_ConfigAck(I2C1, DISABLE);
*recvBufferPtr++ = I2C_RecvData(I2C1);
len--;
I2CTimeout = I2CT_LONG_TIMEOUT;
while (!I2C_GetFlag(I2C1, I2C_FLAG_BYTEF))
{
if ((I2CTimeout--) == 0)
{
CommTimeOut_CallBack(MASTER_BYTEF);
}
}
if (Comm_Flag == C_READY)
{
Comm_Flag = C_STOP_BIT;
I2C_GenerateStop(I2C1, ENABLE);
}
*recvBufferPtr++ = I2C_RecvData(I2C1);
len--;
*recvBufferPtr++ = I2C_RecvData(I2C1);
len--;
break;
}
I2CTimeout = I2CT_LONG_TIMEOUT;
while (!I2C_CheckEvent(I2C1, I2C_EVT_MASTER_DATA_RECVD_FLAG))
{
if ((I2CTimeout--) == 0)
{
CommTimeOut_CallBack(MASTER_RECVD);
}
}
*recvBufferPtr++ = I2C_RecvData(I2C1);
len--;
}
}
I2CTimeout = I2CT_LONG_TIMEOUT;
while (I2C_GetFlag(I2C1, I2C_FLAG_BUSY))
{
if ((I2CTimeout--) == 0)
{
CommTimeOut_CallBack(MASTER_BUSY);
}
}
Comm_Flag = C_READY;
#ifdef NON_REENTRANT
if (Mutex_Flag)
Mutex_Flag = 0;
else
return -2;
#endif
return 0;
}
void CommTimeOut_CallBack(ErrCode_t errcode)
{
rt_kprintf("...ErrCode:%d\r\n", errcode);
}
app_hw_i2c.h
#ifndef APP_HW_I2C_H_
#define APP_HW_I2C_H_
#define NON_REENTRANT
typedef enum
{
C_READY = 0,
C_START_BIT,
C_STOP_BIT
} CommCtrl_t;
typedef enum
{
MASTER_OK = 0,
MASTER_BUSY,
MASTER_MODE,
MASTER_TXMODE,
MASTER_RXMODE,
MASTER_SENDING,
MASTER_SENDED,
MASTER_RECVD,
MASTER_BYTEF,
MASTER_BUSERR,
MASTER_UNKNOW,
SLAVE_OK = 20,
SLAVE_BUSY,
SLAVE_MODE,
SLAVE_BUSERR,
SLAVE_UNKNOW
}ErrCode_t;
extern void i2c_config(void);
extern int i2c_master_send(uint8_t slave_addr, uint8_t* data, uint8_t len);
extern int i2c_master_recv(uint8_t slave_addr, uint8_t* data, uint8_t len);
#endif
如下为OLED的驱动代码。
app_oled.c
#include <board.h>
#include "codetab.h"
#include "app_oled.h"
#include "app_hw_i2c.h"
#include <rtthread.h>
#include <string.h>
#include <stdio.h>
#define OLED_I2C_SLAVE_ADDR (0x78U)
uint8_t OLED_GRAM[128][8];
void OLED_WrDat(unsigned char IIC_Data)
{
uint8_t i2c_transmitter[2]={0x40, IIC_Data};
i2c_transmitter[0] = 0x40;
i2c_transmitter[1] = IIC_Data;
i2c_master_send(OLED_I2C_SLAVE_ADDR, &i2c_transmitter[0], 2);
}
void OLED_WrCmd(unsigned char IIC_Command)
{
uint8_t i2c_transmitter[2]={0x00, IIC_Command};
i2c_transmitter[0] = 0x00;
i2c_transmitter[1] = IIC_Command;
i2c_master_send(OLED_I2C_SLAVE_ADDR, &i2c_transmitter[0], 2);
}
void OLED_OFF(void)
{
OLED_WrCmd(0X8D);
OLED_WrCmd(0X10);
OLED_WrCmd(0XAE);
}
void OLED_ON(void)
{
OLED_WrCmd(0X8D);
OLED_WrCmd(0X14);
OLED_WrCmd(0XAF);
}
void OLED_Fill(uint8_t x1,uint8_t y1,uint8_t x2,uint8_t y2,uint8_t dot)
{
uint8_t x,y;
for(x=x1;x<=x2;x++)
for(y=y1;y<=y2;y++)
OLED_DrawPoint(x,y,dot);
OLED_Refresh_Gram();
}
void OLED_Clear(void)
{
uint8_t i,n;
for(i=0;i<8;i++)
for(n=0;n<128;n++)
OLED_GRAM[n][i]=0X00;
OLED_Refresh_Gram();
}
void OLED_Init(void)
{
OLED_WrCmd(0xAE);
OLED_WrCmd(0xD5);
OLED_WrCmd(80);
OLED_WrCmd(0xA8);
OLED_WrCmd(0X3F);
OLED_WrCmd(0xD3);
OLED_WrCmd(0X00);
OLED_WrCmd(0x40);
OLED_WrCmd(0x8D);
OLED_WrCmd(0x14);
OLED_WrCmd(0x20);
OLED_WrCmd(0x02);
OLED_WrCmd(0xA1);
OLED_WrCmd(0xC0);
OLED_WrCmd(0xDA);
OLED_WrCmd(0x12);
OLED_WrCmd(0x81);
OLED_WrCmd(0xEF);
OLED_WrCmd(0xD9);
OLED_WrCmd(0xf1);
OLED_WrCmd(0xDB);
OLED_WrCmd(0x30);
OLED_WrCmd(0xA4);
OLED_WrCmd(0xA6);
OLED_WrCmd(0xAF);
OLED_Clear();
}
void OLED_Refresh_Gram(void)
{
uint8_t i,n;
for(i=0;i<8;i++)
{
OLED_WrCmd (0xb0+i);
OLED_WrCmd (0x00);
OLED_WrCmd (0x10);
for(n=0;n<128;n++)
OLED_WrDat(OLED_GRAM[n][i]);
}
}
void OLED_DrawPoint(uint8_t x,uint8_t y,uint8_t t)
{
uint8_t pos,bx,temp=0;
if(x>127||y>63)return;
pos=7-y/8;
bx=y%8;
temp=1<<(7-bx);
if(t)
OLED_GRAM[x][pos]|=temp;
else
OLED_GRAM[x][pos]&=~temp;
}
void OLED_ShowChar(uint8_t x,uint8_t y,uint8_t chr,uint8_t size,uint8_t mode)
{
uint8_t temp,t,t1;
uint8_t y0=y;
uint8_t csize=(size/8+((size%8)?1:0))*(size/2);
chr=chr-' ';
for(t=0;t<csize;t++)
{
if(size==12)temp=asc2_1206[chr][t];
else if(size==16)temp=asc2_1608[chr][t];
else if(size==24)temp=asc2_2412[chr][t];
else return;
for(t1=0;t1<8;t1++)
{
if(temp&0x80)
OLED_DrawPoint(x,y,mode);
else OLED_DrawPoint(x,y,!mode);
temp<<=1;
y++;
if((y-y0)==size)
{
y=y0;
x++;
break;
}
}
}
}
void OLED_ShowString(uint8_t x,uint8_t y,const uint8_t *p,uint8_t size,uint8_t mode)
{
while((*p<='~')&&(*p>=' '))
{
if(x>(128-(size/2))){x=0;y+=size;}
if(y>(64-size)){y=x=0;OLED_Clear();}
OLED_ShowChar(x,y,*p,size,mode);
x+=size/2;
p++;
}
}
uint32_t mypow(uint8_t m, uint8_t n)
{
uint32_t result = 1;
while(n--)
result *= m;
return result;
}
void OLED_ShowNum(uint8_t x, uint8_t y, uint32_t num, uint8_t len, uint8_t size, uint8_t mode)
{
uint8_t t, temp;
uint8_t enshow = 0;
for(t=0; t<len; t++)
{
temp = (num / mypow(10,len-t-1)) % 10;
if((enshow == 0) && (t < (len-1)))
{
if(temp == 0)
{
OLED_ShowChar(x+(size/2)*t,y,'0',size, mode);
continue;
}
else
enshow = 1;
}
OLED_ShowChar(x+(size/2)*t,y,temp+'0',size, mode);
}
}
void OLED_ShowHz(uint8_t x, uint8_t y, const char hz_c[], uint8_t mode)
{
uint8_t temp, t, t1;
uint8_t y0 = y;
uint8_t csize = 32u;
uint8_t chr = 0;
uint8_t flag = 0;
for (t=0; t<(sizeof(hz_char)/sizeof(hz_char[0])); t++)
{
if(rt_strcmp(hz_char[t], hz_c) == 0)
{
chr = t;
flag = 1;
break;
}
}
if(1 == flag)
{
chr <<= 1u;
for(t=0; t<csize; t++)
{
if(16U == t)
chr += 1;
temp=(0 == mode) ? (hz_code[chr][t%16]):(~hz_code[chr][t%16]);
for(t1=0; t1<8; t1++)
{
if(temp & 0x80)
OLED_DrawPoint(x, y, 0);
else
OLED_DrawPoint(x, y, 1);
temp <<= 1;
y++;
if(16U == (y-y0))
{
y = y0;
x++;
break;
}
}
}
}
}
void OLED_ShowHzStringRow(uint8_t x, uint8_t y, const char *hz_s, uint8_t mode)
{
uint8_t i;
char chr[4];
uint8_t len;
len = strlen(hz_s);
for(i=0; i<(len-1); i+=3)
{
rt_sprintf(chr, "%c%c%c", hz_s[i], hz_s[i+1], hz_s[i+2]);
OLED_ShowHz(x+((i/3)<<4), y, chr, mode);
}
}
void OLED_ShowHzStringColumn(uint8_t x, uint8_t y, const char *hz_s, uint8_t mode)
{
uint8_t i;
char chr[4];
uint8_t len;
len = strlen(hz_s);
for(i=0; i<(len-1); i+=3)
{
rt_sprintf(chr, "%c%c%c", hz_s[i], hz_s[i+1], hz_s[i+2]);
OLED_ShowHz(x, y+((i/3)<<4), chr, mode);
}
}
void OLED_ShowHzFromIndex(uint8_t x, uint8_t y, uint8_t chr, uint8_t mode)
{
uint8_t temp, t, t1;
uint8_t y0 = y;
uint8_t csize = 32u;
chr <<= 1u;
for(t=0; t<csize; t++)
{
if(16U == t)
chr += 1;
temp=(0 == mode) ? (hz_code[chr][t%16]):(~hz_code[chr][t%16]);
for(t1=0; t1<8; t1++)
{
if(temp & 0x80)
OLED_DrawPoint(x, y, 0);
else
OLED_DrawPoint(x, y, 1);
temp <<= 1;
y++;
if(16U == (y-y0))
{
y = y0;
x++;
break;
}
}
}
}
void OLED_HzStringFromIndex(uint8_t x, uint8_t y, uint8_t chr_S, uint8_t chr_E, uint8_t mode)
{
uint8_t i;
for(i=chr_S; i<=chr_E; i++)
{
OLED_ShowHzFromIndex(x+16*(i-chr_S), y, i, mode);
}
}
void OLED_DrawBmpImg(uint8_t x,
uint8_t y,
const uint8_t bmp[][16],
uint32_t len,
uint8_t mode)
{
uint16_t t, t1;
uint8_t y0 = y;
uint8_t temp = 0u;
uint16_t len2 = (uint16_t)(len>>4u);
for(t=0; t<len; t++)
{
temp=(0 == mode) ?
(bmp[(uint8_t)(t/16)][(uint8_t)(t%16)]):(~bmp[(uint8_t)(t/16)][(uint8_t)(t%16)]);
for(t1=0; t1<8; t1++)
{
if(temp & 0x80)
OLED_DrawPoint(x, y, 0);
else
OLED_DrawPoint(x, y, 1);
temp <<= 1u;
y++;
if((y-y0) == len2)
{
y = y0;
x ++;
break;
}
}
}
}
void OLED_DrawBmpImg2(uint8_t x,
uint8_t y,
const uint8_t bmp[][8],
uint32_t len,
uint8_t size,
uint8_t mode)
{
uint16_t t, t1;
uint8_t y_end;
uint8_t temp;
uint16_t len2 = 0u;
y_end = y;
len2 = (uint16_t)((len) / ((uint8_t)(size/8)));
for(t=0; t<len; t++)
{
temp=(0 == mode) ?
(bmp[(uint8_t)(t/8)][(uint8_t)(t%8)]):(~bmp[(uint8_t)(t/8)][(uint8_t)(t%8)]);
for(t1=0; t1<8; t1++)
{
if(temp&0x80)
OLED_DrawPoint(x, y, 0);
else
OLED_DrawPoint(x, y, 1);
temp <<= 1u;
y++;
if((y-y_end) == len2)
{
y = y_end;
x ++;
break;
}
}
}
}
app_oled.h
#ifndef APP_OLED_H_
#define APP_OLED_H_
#include "stdint.h"
extern void OLED_Init(void);
extern void OLED_Fill(uint8_t x1,uint8_t y1,uint8_t x2,uint8_t y2,uint8_t dot);
extern void OLED_Clear(void);
extern void OLED_Refresh_Gram(void);
extern void OLED_DrawPoint(uint8_t x,uint8_t y,uint8_t t);
extern void OLED_ShowHz(uint8_t x, uint8_t y, const char hz_c[], uint8_t mode);
extern void OLED_ShowChar(uint8_t x,uint8_t y,uint8_t chr,uint8_t size,uint8_t mode);
extern void OLED_ShowNum(uint8_t x, uint8_t y, uint32_t num, uint8_t len, uint8_t size, uint8_t mode);
extern void OLED_ShowString(uint8_t x,uint8_t y,const uint8_t *p,uint8_t size,uint8_t mode);
extern void OLED_ShowBMP(uint8_t x,uint8_t y,uint8_t mode,uint8_t bmp[][16],uint32_t len);
extern void OLED_ShowHz(uint8_t x, uint8_t y, const char hz_c[], uint8_t mode);
extern void OLED_ShowHzStringRow(uint8_t x, uint8_t y, const char *hz_s, uint8_t mode);
extern void OLED_ShowHzStringColumn(uint8_t x, uint8_t y, const char *hz_s, uint8_t mode);
extern void OLED_ShowHzFromIndex(uint8_t x, uint8_t y, uint8_t chr, uint8_t mode);
#endif
如下为main函数实现示例:
OLED_ShowString(12, 0, (const unsigned char*)"N32G45XVL-STB", 16, 1);
OLED_ShowString(24, 16, (const unsigned char*)"elecfans", 16, 1);
OLED_ShowString(64, 48, (const unsigned char*)"- hehung", 16, 1);
OLED_Refresh_Gram();
实现效果
如下为试验结果,可以正常驱动。